LUA debug 工具函数

LUA debug 工具函数

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
local print = print
local debug = debug
local string = string
local io = io
local type = type
local pairs = pairs

module "dt"

function bt()
local dinfo = debug.getinfo(2)
local func_line = dinfo.linedefined
local filename = dinfo.short_src
local func_name = dinfo.name or"main"
print ("func_line:", func_line, filename, func_name)

local a =1
local var_tb ={}
while true
do
local name, value = debug.getlocal(2, a)
if not name then break end

var_tb[name]= value
print(string.format("%s=", name), value)
a = a +1
end

while true
do
io.write(string.format(">>"))
local cmd = io.read()
if cmd =="c"or cmd =="n"or cmd =="go"or cmd =="g"
then
break
end

local b = string.find(cmd," ")
if b ==nil
then
print ("usage p + var")
else
local sub_cmd = string.sub(cmd,1, b-1)
if sub_cmd =="p"or sub_cmd =="print"
or sub_cmd =="dump"or sub_cmd =="d"
then
local sub_var = string.sub(cmd, b +1)
local value = var_tb[sub_var]
print(string.format("%s=", sub_var), value)

if type(value)=="table"
then
for k, v in pairs(value)
do
print("", k , v)
end
end
else
print ("usage p + var", sub_cmd)
end
end
end
end

--[[ 示例

require "dt"
dt.bt()

--]]