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