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