- Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtrace.lua
More file actions
Latest commit
208 lines (189 loc) · 3.91 KB
/
Copy pathtrace.lua
File metadata and controls
208 lines (189 loc) · 3.91 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
localdebug=debug
localprint=print
localipairs=ipairs
localpairs=pairs
localstring=string
localrawget=rawget
localinfo= { file= {} }
localtrace= {}
trace.print=print
localfunctionsetname(level)
info.filename=debug.getinfo(level+1,"S").short_src
ifinfo.file[info.filename] ==nilthen
info.file[info.filename] = {}
end
info.var=info.file[info.filename]
end
localfunctionsplit(name)
localkeys= {}
forkeyinname:gmatch("[^.]+") do
keys[#keys+1] =key
end
returnkeys
end
localfunctionget_table_field(tbl,keys)
fori,keyinipairs(keys) do
iftbl[key] then
tbl=tbl[key]
else
returnnil
end
end
returntbl
end
localfunctionis_table_field(var_name,name,v)
iftype(v) =="table" then
locallen=#name
ifvar_name:sub(1,len) ==nameandvar_name:sub(len+1,len+1) =="." then
returntrue
end
end
returnfalse
end
localfunctionmake_local(index,name,is_field)
returnfunction()
local_ , value=debug.getlocal(4 , index)
ifnotis_fieldthen
returnname,"local",value
else
localkeys=split(name)
table.remove(keys,1)
returnname,"local",get_table_field(value,keys)
end
end
end
localfunctionmake_upvalue(func, index,name,is_field)
returnfunction()
local_, value=debug.getupvalue(func, index)
ifnotis_fieldthen
returnname,"upvalue",value
else
localkeys=split(name)
table.remove(keys,1)
returnname,"upvalue",get_table_field(value,keys)
end
end
end
localfunctionmake_global(env, name)
returnfunction()
ifrawget(env,name) then
returnname,"global",rawget(env,name)
else
localkeys=split(name)
returnname,"global",get_table_field(env,keys)
end
end
end
localfunctiongen_var(var_name, level)
locali=1
whiletruedo
localname,v=debug.getlocal(5,i)
ifname==var_namethen
returnmake_local(i,var_name)
end
ifis_table_field(var_name,name,v) then
returnmake_local(i,var_name,true)
end
ifname==nilthen
break
end
i=i+1
end
i=1
localf=debug.getinfo(5, "f").func
whiletruedo
localname, v=debug.getupvalue(f,i)
ifname==var_namethen
returnmake_upvalue(f,i,var_name)
end
ifis_table_field(var_name,name,v) then
returnmake_upvalue(f,i,var_name,true)
end
ifname==nilthen
break
end
i=i+1
end
localname,env=debug.getupvalue(f,1)
ifname=='_ENV' then
returnmake_global(env, var_name)
end
end
localfunctiongen_vars(var, call)
localret= {}
for_,kinipairs(var) do
localf=gen_var(k, call)
iffthen
table.insert(ret, f)
end
end
returnret
end
localfunctionhookline(var , call, line)
trace.print(info.filename, ":" , line)
ifinfo.var[line] ==nilthen
info.var[line] =gen_vars(var, call)
end
for_,vinipairs(info.var[line]) do
localname , type , value=v()
ifinfo.last[name] ~=valuethen
trace.print(name , type, value)
info.last[name] =value
end
end
end
localfunctionhook(var , level)
localcall=0
localindex= {}
forwinstring.gmatch(var, "%S+") do
table.insert(index,w)
end
localfunctionf (mode, line)
ifmode=='return' then
ifcall<=0then
debug.sethook()
trace.on=nil
return
end
setname(3)
call=call-1
ifcall==levelthen
debug.sethook(f,'crl')
end
elseifmode=='call' then
setname(2)
call=call+1
ifcall>levelthen
debug.sethook(f,'cr')
end
elseifmode=='tail call' then
setname(2)
elseifmode=='line' then
hookline(index , call, line)
end
end
returnf
end
localfunctionup(level, f)
localcall=0
returnfunction(mode)
ifmode=='return' then
call=call+1
ifcall==levelthen
setname(3)
debug.sethook(f,'crl')
end
elseifmode=='call' then
call=call-1
end
end
end
functiontrace.trace(var , level)
iftrace.onthen
return
end
trace.on=true
info.last= {}
debug.sethook(up(2 , hook(varor"" , levelor0)) , 'cr')
end
returntrace