]> git.r.bdr.sh - rbdr/pico-engine/blob - LuaDebugger.cpp
18f39092950c09875f4404510b7a8e49002d3260
[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 case LUA_HOOKTAILRET:
68 LuaHookRet (lua);
69 break;
70 case LUA_HOOKLINE:
71 LuaHookLine (lua);
72 break;
73 case LUA_HOOKCOUNT:
74 LuaHookCount (lua);
75 break;
76 }
77 }
78
79 CLuaDebugger::CLuaDebugger (CLuaVirtualMachine& vm) : m_iCountMask (10), m_vm (vm)
80 {
81 // Clear all current hooks
82 if (vm.Ok ())
83 {
84 vm.AttachDebugger (this);
85 lua_sethook ((lua_State *) vm, LuaHook, 0, m_iCountMask);
86 }
87 }
88
89 CLuaDebugger::~CLuaDebugger (void)
90 {
91 // Clear all current hooks
92 if (m_vm.Ok ())
93 {
94 lua_sethook ((lua_State *) m_vm, LuaHook, 0, m_iCountMask);
95 }
96 }
97
98 void CLuaDebugger::SetHooksFlag (int iMask)
99 {
100 // Set hooks
101 lua_sethook ((lua_State *) m_vm, LuaHook, iMask, m_iCountMask);
102 }
103
104 void CLuaDebugger::ErrorRun (int iErrorCode)
105 {
106 switch (iErrorCode)
107 {
108 case LUA_ERRRUN:
109 printf ("LUA_ERRRUN\n");
110 break;
111 case LUA_ERRMEM:
112 printf ("LUA_ERRMEM\n");
113 break;
114 case LUA_ERRERR:
115 printf ("LUA_ERRERR\n");
116 break;
117 }
118
119 // Get the error string that appears on top of stack when a function
120 // fails to run
121 printf ("Error: %s\n", lua_tostring ((lua_State *) m_vm, -1));
122 }