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