lua 列印函式呼叫堆疊
阿新 • • 發佈:2019-02-11
local _trace = debug.traceback
local _t_concat = table.concat
local _log = print
function print_stack(...)
local out = {'[TRACE]'}
local n = select('#', ...)
for i=1, n, 1 do
local v = select(i,...)
out[#out+1] = tostring(v)
end
out[#out+1] = '\n'
out[#out+1] = _trace("", 2)
_log(_t_concat(out,' '))
end
function hookfunc(event,line)
local s = debug.getinfo(2)
--hook lua 函式 A
if s.name == 'A' then
print('called function A')
end
--只hook C的函式
if s.short_src == '[C]' and s.name == 'max' then
print_stack('called max')
end
end
debug.sethook(hookfunc,'c')
function A()
math.max(1,2);
end
A();
local _t_concat = table.concat
local _log = print
function print_stack(...)
local out = {'[TRACE]'}
local n = select('#', ...)
for i=1, n, 1 do
local v = select(i,...)
out[#out+1] = tostring(v)
end
out[#out+1] = '\n'
out[#out+1] = _trace("", 2)
_log(_t_concat(out,' '))
end
function hookfunc(event,line)
local s = debug.getinfo(2)
--hook lua 函式 A
if s.name == 'A' then
print('called function A')
end
--只hook C的函式
if s.short_src == '[C]' and s.name == 'max' then
print_stack('called max')
end
end
debug.sethook(hookfunc,'c')
function A()
math.max(1,2);
end
A();