-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathltrace.lua
More file actions
303 lines (272 loc) · 6.67 KB
/
ltrace.lua
File metadata and controls
303 lines (272 loc) · 6.67 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
--[[
Path : ltrace.lua
Author : Roc <RocAltair@gmail.com>
CreateTime : 2016-01-21 19:15:34
Description : Description
--]]
local IS_LUAJIT = jit and true or false
local ltrace_getenv
local getstack_from
local getstack
local dumpvalue
local dumptable
local dumpframe
local dumpstack
local getfulltrace
local mTABLE_MAX_DEEP = 3
local mVALUE_MAX_LEN = 5120
local traceback = {}
local setfenv = _G.setfenv or function(f, t)
f = (type(f) == 'function' and f or debug.getinfo(f + 1, 'f').func)
local name
local up = 0
repeat
up = up + 1
name = debug.getupvalue(f, up)
until name == '_ENV' or name == nil
if name then
debug.upvaluejoin(f, up, function() return name end, 1) -- use unique upvalue
debug.setupvalue(f, up, t)
end
return f
end
local getfenv = _G.getfenv or function(f)
f = (type(f) == 'function' and f or debug.getinfo(f + 1, 'f').func)
local name, val
local up = 0
repeat
up = up + 1
name, val = debug.getupvalue(f, up)
until name == '_ENV' or name == nil
return val
end
local function canPrint(k, v)
if type(v) ~= "table" then
return true
end
if v == _G then
return false
end
return true
end
function ltrace_getenv(realframe)
local env = {}
local indexmap = {}
local i = 1
local funcinfo = debug.getinfo(realframe, "nlSf")
if not funcinfo then
return
end
local k, v = debug.getlocal(realframe, i)
while k do
indexmap[k] = i
env[k] = v
i = i + 1
k, v = debug.getlocal(realframe, i)
end
setmetatable(env, {__index = getfenv(funcinfo.func)})
return env, funcinfo, indexmap
end
function getstack_from(callfile, callline, maxframesz)
assert(callfile and callline)
local realframe = 0
local framestack = {}
local env, funcinfo, indexmap = ltrace_getenv(realframe)
while funcinfo do
if funcinfo.currentline == callline and funcinfo.short_src == callfile then
break
end
realframe = realframe + 1
env, funcinfo, indexmap = ltrace_getenv(realframe)
end
while funcinfo do
realframe = realframe + 1
env, funcinfo, indexmap = ltrace_getenv(realframe)
if not funcinfo then
break
end
table.insert(framestack, {
realframe = realframe,
env = env,
funcinfo = funcinfo,
indexmap = indexmap,
})
if maxframesz and #framestack >= maxframesz then
break
end
end
return framestack
end
local function get_params(func)
if not IS_LUAJIT or _VERSION <= "Lua 5.1" then
return {}, -1, false
end
local info = debug.getinfo(func, "u")
if not info then return end
local argNameList = {}
for i = 1, info.nparams do
local name = debug.getlocal(func, i)
table.insert(argNameList, name)
end
if info.isvararg then
table.insert(argNameList, "...")
end
return argNameList, info.nparams, info.isvararg
end
local uncomparedTypeMap = {
["table"] = true,
["userdata"] = true,
["function"] = true,
["boolean"] = true,
["thread"] = true,
}
local function compare(a, b)
local ta = type(a)
local tb = type(b)
if ta ~= tb or uncomparedTypeMap[ta] or uncomparedTypeMap[tb] then
return tostring(a) < tostring(b)
end
return a < b
end
local function pairs_orderly(t, comp)
comp = comp or compare
local keys = {}
for k, v in pairs(t) do
table.insert(keys, k)
end
local size = #keys
table.sort(keys, comp)
local i = 0
return function(tbl, k)
i = i + 1
if i > size then
return
end
return keys[i], t[keys[i]]
end
end
local ignoreMap = {
}
function dumptable(value, depth)
assert(type(value) == "table")
local rettbl = {}
depth = (depth or 0) + 1
if depth > mTABLE_MAX_DEEP then
return "{...}"
end
table.insert(rettbl, '{')
local content = {}
for k, v in pairs_orderly(value) do
if not ignoreMap[k] and canPrint(k, v) then
local line = {}
table.insert(line, dumpkey(k, depth) .. "=")
table.insert(line, dumpvalue(v, depth))
table.insert(content, table.concat(line))
end
end
table.insert(rettbl, table.concat(content, ", "))
table.insert(rettbl, '}')
return table.concat(rettbl)
end
function dumpkey(key, depth)
local vtype = type(key)
if vtype == "table" then
return "[" .. dumptable(key, depth) .. "]"
elseif vtype == "string" then
if key:match("^%d") or key:match("%w+") ~= key then
return "[" .. string.format("%q", key) .. "]"
end
return tostring(key)
end
return "[" .. tostring(key) .. "]"
end
function dumpvalue(v, depth)
local vtype = type(v)
if vtype == "table" then
return dumptable(v, depth)
elseif vtype == "string" then
return string.format("%q", v)
elseif vtype == "number" or vtype == "boolean" then
return tostring(v)
end
return string.format("<%s>", tostring(v))
end
function dumpframe(frameidx, env, funcinfo, indexmap)
local fix = -1
if funcinfo.what ~= "Lua" then
return string.format("%d[C] : in <%s>",
frameidx + fix,
funcinfo.name or funcinfo.what or funcinfo.namewhat
)
end
local out = {}
local args = get_params(funcinfo.func)
local funcstr = ""
if funcinfo.name then
funcstr = string.format("in <%s(%s)>",
funcinfo.name,
table.concat(args, ","))
end
table.insert(out,
string.format("%d @%s:%d %s",
frameidx + fix,
funcinfo.short_src or "<stdin>",
funcinfo.currentline,
funcstr))
local valuelines = {}
local function compare(k1, k2)
return indexmap[k1] < indexmap[k2]
end
for k, v in pairs_orderly(env, compare) do
if canPrint(k, v) then
local vs = dumpvalue(v)
if #vs > mVALUE_MAX_LEN then
vs = vs:sub(1, mVALUE_MAX_LEN) .. "..."
end
-- table.insert(valuelines, string.format("\t%s<%s>:%s", k, indexmap[k] or "", vs))
table.insert(valuelines, string.format("\t%s:%s", k, vs))
end
end
table.insert(out, table.concat(valuelines, "\n"))
return table.concat(out, "\n")
end
function dumpstack(stack)
local list = {}
for i, v in pairs(stack) do
local s = dumpframe(i, v.env, v.funcinfo, v.indexmap)
table.insert(list, s)
end
return list
end
function getstack(level, maxframesz)
level = level or 2
local info = debug.getinfo(level, "nlSf")
local stacklist = getstack_from(info.short_src, info.currentline, maxframesz)
return stacklist
end
function getfulltrace(level, maxframesz)
local fullstack = getstack(level, maxframesz)
local list = dumpstack(fullstack)
local ret = {}
for i, v in pairs(list) do
table.insert(ret, v)
end
return table.concat(ret, "\n")
end
function settdmaxdeep(depth)
mTABLE_MAX_DEEP = depth or mTABLE_MAX_DEEP
end
function setvdmaxlen(len)
mVALUE_MAX_LEN = len or mVALUE_MAX_LEN
end
traceback.setvdmaxlen = setvdmaxlen
traceback.settdmaxdeep = settdmaxdeep
traceback.getenv = getenv
traceback.getstack_from = getstack_from
traceback.getstack = getstack
traceback.dumpvalue = dumpvalue
traceback.dumptable = dumptable
traceback.dumpframe = dumpframe
traceback.dumpstack = dumpstack
traceback.getfulltrace = getfulltrace
return traceback