您的位置:首页 > 编程语言 > Lua

lua 打印函数调用堆栈

2017-03-02 15:47 549 查看
 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(); 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: