标签搜索

Lua使用调试库hook函数调用

anker
2021-06-27 / 0 评论 / 330 阅读 / 正在检测是否收录...

自带的调试库支持设置调用钩子。

debug.sethook(hook_fun, "c")

如果第2个参数为空,则表示清除钩子。钩子中可以得到Luab函数名。

local function hook_fun()
     local funcinfo = debug.getinfo(2, 'nS')
     local name = funcinfo.name or 'anonymous'
     local line = string.format("%d", funcinfo.linedefined or 0)
     local source = funcinfo.short_src or 'C_FUNC'
     local fun_name = string.format("%-30s: %s: %s", name, source, line)
     skynet.error(fun_name)
end
0

评论 (0)

取消