简单调试器 |
|
_ERRORMESSAGE 获取堆栈回溯,并且可以修改 _ALERT 以调用 debug,以便在发生错误时检查全局变量。只需添加少量代码,您甚至可以转储局部变量。 [1]
这段代码 [2] 中存在一个错误。
1 require'simpledebugger.lua'
2
3 function here( t, str, pat )
4 assert( type(t,'table'), 'expecting a table' )
5 assert( type(str,'string'), 'expecting a string' )
6 pat = pat or '$(%b<>)'
7 local f = function(w)
8 local m = t[strsub(w,2,-2)] or w
9 if type(m,'function') then m = m(t) end
10 return tostring(m)
11 end
12 return( gsub( str, pat, f ) )
13 end
14
15 temp = [[
16 this is a $<x> for $<y>
17 ]]
18 local tab = { x = 'test', y = 'fun' }
19 write( here( tab, temp ) )
20 write( here( temp, tab ) )
21 print"game over"
$ lua -v testdebugger.lua Lua 4.1 (work4) Copyright (C) 1994-2001 TeCGraf, PUC-Rio this is a test for fun error: assertion failed! expecting a table stack traceback: 1: function `assert' [C] 2: function `here' at line 4 [file `testdebugger.lua'] 3: main of file `testdebugger.lua' at line 20 lua_debug>
lua_debug> locals(2)
t = " \
this is a $<x> for $<y>\
"
str = { -- table: 0x80649b8
y = "fun",
x = "test"
}
pat = nil
lua_debug>
cont 退出 debug。
lua_debug> locals(3)
tab = { -- table: 0x80649b8
y = "fun",
x = "test"
}
lua_debug> cont
stop"it",我们可以检查函数 here 中的变量。
$ lua -v testdebugger.lua Lua 4.1 (work4) Copyright (C) 1994-2001 TeCGraf, PUC-Rio error: it stack traceback: 1: function <7:file `testdebugger.lua'> at line 9 2: function `gsub' [C] 3: function `here' at line 13 [file `testdebugger.lua'] 4: main of file `testdebugger.lua' at line 20 lua_debug> locals() w = "<x>" m = "test" lua_debug> cont error: it stack traceback: 1: function <7:file `testdebugger.lua'> at line 9 2: function `gsub' [C] 3: function `here' at line 13 [file `testdebugger.lua'] 4: main of file `testdebugger.lua' at line 20 lua_debug> locals() w = "<y>" m = "fun" lua_debug> cont this is a test for fun game over