-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathldebug.lua
More file actions
342 lines (305 loc) · 6.59 KB
/
Copy pathldebug.lua
File metadata and controls
342 lines (305 loc) · 6.59 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
local sformat = string.format
local tconcat = table.concat
local tinsert = table.insert
local tostring = tostring
local srep = string.rep
local stack_level --next cmd's counter
local in_hooking = false
local quit_debug = false
local hk_base_level = 6 --M.probe -> dispatch_input -> pcall -> dbg_cmd[xxx] -> debug_hook - > debug.sethook
local dbg_base_level = 4 --M.probe/_hook -> dispatch_input -> pcall -> dbg_cmd[xxx]
local cur_list_file
local cur_list_line
local last_list_line
local function list_source_codes(file, start_line, follow_line)
if not start_line then
if not last_list_line then
start_line = cur_list_line
else
start_line = last_list_line + 1
end
end
follow_line = follow_line or 4
local end_line = start_line + follow_line - 1
local line_num = 1
for context in io.lines(file) do
if line_num >= start_line then
print(line_num,":",context)
last_list_line = line_num
end
if line_num >= end_line then
break
end
line_num = line_num + 1
end
end
local function tostring_r(root)
if type(root) ~= "table" then
return tostring(root)
end
local cache = { [root] = "." }
local function _dump(t,space,name)
local temp = {}
for k,v in pairs(t) do
local key = tostring(k)
if cache[v] then
tinsert(temp,"+" .. key .. " {" .. cache[v].."}")
elseif type(v) == "table" then
local new_key = name .. "." .. key
cache[v] = new_key
tinsert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. srep(" ",#key),new_key))
else
tinsert(temp,"+" .. key .. " [" .. tostring(v).."]")
end
end
return tconcat(temp,"\n"..space)
end
return _dump(root, "","")
end
local function find_var(level, varname)
local i = -1
while true do --变参local
local name,v = debug.getlocal(level, i)
if name == nil then
break
end
if varname == name then
return v
end
i = i - 1
end
i = 1
while true do --local
local name,v = debug.getlocal(level, i)
if name == nil then
break
end
if varname == name then
return v
end
i = i + 1
end
local info = debug.getinfo(level, "f")
if not info then
return
end
i = 1
while true do --upvalue
local name,v = debug.getupvalue(info.func, i)
if name == nil then
break
end
if varname == name then
return v
end
i = i + 1
end
return _ENV[varname] --env
end
local function reply(s, ...)
s = "gdb>" .. s
print(sformat(s, ...))
end
local dbg_cmd = {}
local function dispatch_input()
while true do
local c = io.read()
local cc,param
if dbg_cmd[c] then
cc = c
elseif #c > 1 and dbg_cmd[c:sub(1,1)] then --p and f
cc = c:sub(1,1)
param = c:sub(3)
end
if cc then
local ok, err = pcall(dbg_cmd[cc], param)
if ok then
if err then
break
end
else
reply ("cmd:[%s] err:%s", cc, err)
end
elseif c == "" then
dbg_cmd.l(true)
else
reply ("unknow cmd:" .. c)
end
end
end
local function _hook(event)
if stack_level then
if event == "call" or event == "tail call" then
stack_level = stack_level + 1
elseif event == "return" then
stack_level = stack_level - 1
end
if stack_level > 0 then
return
end
stack_level = nil
end
if event ~= "line" then --like outside hook-func's return event
return
end
local info = debug.getinfo(2, "Sln")
reply ("%s:%s-> %s",info.short_src, info.currentline, info.name)
cur_list_file = info.short_src
cur_list_line = info.currentline
in_hooking = true
dispatch_input()
if quit_debug then
return
end
if not in_hooking then
reply "Program continue running"
end
end
local function debug_hook(back_level, db_cmd)
debug.sethook(function ( event, line)
if event == "call" or event == "tail call" then
back_level = back_level + 1
elseif event == "return" then
back_level = back_level - 1
end
if back_level == 0 then
if db_cmd == "next" then
stack_level = 0
debug.sethook(_hook, "crl")
elseif db_cmd == "step" then
debug.sethook(_hook, "l")
end
end
end, "cr")
end
function dbg_cmd.h()
reply [[
s : run step
n : run next
r : run until return
l : show below codes
p var : print var
c : continue to next probe
h : help message
bt : show stack frame
f level : show locals at level frame
q : quit interactive mode]]
end
function dbg_cmd.l(enter_cmd)
if enter_cmd then
list_source_codes(cur_list_file)
else
list_source_codes(cur_list_file, cur_list_line)
end
end
function dbg_cmd.p( name )
if not name or #name == 0 then
reply "need a var name"
return
end
local var = find_var(dbg_base_level + 2, name)
reply(tostring_r(var))
end
function dbg_cmd.bt()
local i = 1
local tmp = {}
while true do
local info = debug.getinfo(dbg_base_level + i, "Sl")
if info == nil then
break
end
local source = sformat("[%d] %s:%d",i, info.short_src,info.currentline)
tinsert(tmp, source)
i = i + 1
end
reply(tconcat(tmp, "\n"))
end
function dbg_cmd.f(level)
level = level or 1
local s = dbg_base_level + level
local info = debug.getinfo(s, "uf")
local tmp = {}
for i = 1, info.nparams do
local name , value = debug.getlocal(s,i)
tinsert(tmp, sformat("P %s : %s", name, tostring_r(value)))
end
if info.isvararg then
local index = -1
while true do
local name,value = debug.getlocal(s,index)
if name == nil then
break
end
tinsert(tmp, sformat("P [%d] : %s", -index, tostring_r(value)))
index = index - 1
end
end
local index = info.nparams + 1
while true do
local name ,value = debug.getlocal(s,index)
if name == nil then
break
end
tinsert(tmp, sformat("L %s : %s", name, tostring_r(value)))
index = index + 1
end
for i = 1, info.nups do
local name, value = debug.getupvalue(info.func,i)
if name ~= "_ENV" then
tinsert(tmp, sformat("U %s : %s", name, tostring_r(value)))
end
end
reply(table.concat(tmp, "\n"))
end
function dbg_cmd.n()
if in_hooking then
stack_level = 0
debug.sethook(_hook, "crl")
else
debug_hook(hk_base_level, "next")
end
return true
end
function dbg_cmd.s()
if in_hooking then
debug.sethook(_hook,"l")
else
debug_hook(hk_base_level, "step")
end
return true
end
function dbg_cmd.q()
in_hooking = false
debug.sethook()
quit_debug = true
return true
end
function dbg_cmd.c()
in_hooking = false
debug.sethook()
return true
end
function dbg_cmd.r()
if in_hooking then
debug_hook(1, "step")
else
debug_hook(1 + hk_base_level, "step")
end
return true
end
local M = {}
function M.probe()
if quit_debug then
return
end
local info = debug.getinfo(2, "Sln")
reply ("%s:%s> %s",info.short_src, info.currentline, info.name)
cur_list_file = info.short_src
cur_list_line = info.currentline
if in_hooking then
debug.sethook()
in_hooking = false
end
dispatch_input()
end
return M