]> git.r.bdr.sh - rbdr/pico-engine/blob - LuaDebugger.cpp
Update project so it compiles
[rbdr/pico-engine] / LuaDebugger.cpp
1 // ---------------------------------------------------------------------------
2 // FILE NAME : LuaDebugger.cpp
3 // ---------------------------------------------------------------------------
4 // DESCRIPTION :
5 //
6 // Simple debugging routines
7 //
8 // ---------------------------------------------------------------------------
9 // VERSION : 1.00
10 // DATE : 1-Sep-2005
11 // AUTHOR : Richard Shephard
12 // ---------------------------------------------------------------------------
13 // LIBRARY INCLUDE FILES
14 #include "luadebugger.h"
15 // ---------------------------------------------------------------------------
16
17 // typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
18 static void LuaHookCall (lua_State *lua)
19 {
20
21 printf ("---- Call Stack ----\n");
22 // printf ("[Level] [Function] [# args] [@line] [src]\n");
23
24 lua_Debug ar;
25
26 // Look at call stack
27 for (int iLevel = 0; lua_getstack (lua, iLevel, &ar) != 0; ++iLevel)
28 {
29 if (lua_getinfo (lua, "Snlu", &ar) != 0)
30 {
31 printf ("%d %s %s %d @%d %s\n", iLevel, ar.namewhat, ar.name, ar.nups, ar.linedefined, ar.short_src);
32 }
33 }
34 }
35
36 static void LuaHookRet (lua_State *lua)
37 {
38
39 }
40
41 static void LuaHookLine (lua_State *lua)
42 {
43 lua_Debug ar;
44 lua_getstack (lua, 0, &ar);
45
46
47 if (lua_getinfo (lua, "Sl", &ar) != 0)
48 {
49 printf ("exe %s on line %d\n", ar.short_src, ar.currentline);
50 }
51 }
52
53 static void LuaHookCount (lua_State *lua)
54 {
55 LuaHookLine (lua);
56 }
57
58 static void LuaHook (lua_State *lua, lua_Debug *ar)
59 {
60 // Handover to the correct hook
61 switch (ar->event)
62 {
63 case LUA_HOOKCALL:
64 LuaHookCall (lua);
65 break;
66 case LUA_HOOKRET:
67 LuaHookRet (lua);
68 break;
69 case LUA_HOOKLINE:
70 LuaHookLine (lua);
71 break;
72 case LUA_HOOKCOUNT:
73 LuaHookCount (lua);
74 break;
75 }
76 }
77
78 CLuaDebugger::CLuaDebugger (CLuaVirtualMachine& vm) : m_iCountMask (10), m_vm (vm)
79 {
80 // Clear all current hooks
81 if (vm.Ok ())
82 {
83 vm.AttachDebugger (this);
84 lua_sethook ((lua_State *) vm, LuaHook, 0, m_iCountMask);
85 }
86 }
87
88 CLuaDebugger::~CLuaDebugger (void)
89 {
90 // Clear all current hooks
91 if (m_vm.Ok ())
92 {
93 lua_sethook ((lua_State *) m_vm, LuaHook, 0, m_iCountMask);
94 }
95 }
96
97 void CLuaDebugger::SetHooksFlag (int iMask)
98 {
99 // Set hooks
100 lua_sethook ((lua_State *) m_vm, LuaHook, iMask, m_iCountMask);
101 }
102
103 void CLuaDebugger::ErrorRun (int iErrorCode)
104 {
105 switch (iErrorCode)
106 {
107 case LUA_ERRRUN:
108 printf ("LUA_ERRRUN\n");
109 break;
110 case LUA_ERRMEM:
111 printf ("LUA_ERRMEM\n");
112 break;
113 case LUA_ERRERR:
114 printf ("LUA_ERRERR\n");
115 break;
116 }
117
118 // Get the error string that appears on top of stack when a function
119 // fails to run
120 printf ("Error: %s\n", lua_tostring ((lua_State *) m_vm, -1));
121 }