Lua之sethook学习
2015-06-17 00:27
302 查看
debug.sethook ([thread,] hook, mask [, count])
Sets the given function as a hook.The stringmaskand the number
countdescribewhen the hook will be called.The string mask may have the following characters,with the given meaning:
"c": The hook is called every time Lua calls a function; 当函数被调用的时候触发,且在函数获得参数之前调用;
"r": The hook is called every time Lua returns from a function; 当函数返回之前触发hook,且不能在此时获得函数的返回值;
"l": The hook is called every time Lua enters a new line of code. 当执行到某一行代码之前触发hook
With a
countdifferent from zero,the hook is called after every
countinstructions.
When called without arguments,debug.sethook turns off the hook.
When the hook is called, its first parameter is a stringdescribing the event that has triggered its call:
"call",
"return"(or
"tail return"),
"line", and
"count".For line events,the hook also gets the new line number as its second parameter.Inside a hook,you can call
getinfowith level 2 to get more information aboutthe running function(level 0 is the
getinfofunction,and
level 1 is the hook function),unless the event is
"tail return".In this case, Lua is only simulating the return,and a call to
getinfowill return invalid data.
debug.getinfo(n)
n = 0 : 表示getinfo本身;
n = 1: 表示hook函数;
n = 2: 表示当前正在运行的函数;
debug.sethook(
optional thread,
hook function,
hook mask , -- string with "c" and/or "r" and/or "l"
optional instruction count) --[b]instruction指令行统计[/b]
下面举个例子,如何使用sethook来定位出错的代码在哪一行:
function test1() c = nil a = 1 b = a + 3 print(b) print(c.x) end function test2() c = {} table.insert(c,1) print("table size is : ".. #c) end function hook() print(debug.traceback()) end debug.sethook(hook,"l") test1()
输出堆信息如下:
>lua -e "io.stdout:setvbuf 'no'" "test.lua" stack traceback: test.lua:17: in function <test.lua:16> test.lua:21: in main chunk [C]: ? stack traceback: test.lua:17: in function <test.lua:16> test.lua:3: in function 'test1' test.lua:21: in main chunk [C]: ? stack traceback: test.lua:17: in function <test.lua:16> test.lua:4: in function 'test1' test.lua:21: in main chunk [C]: ? stack traceback: test.lua:17: in function <test.lua:16> test.lua:5: in function 'test1' test.lua:21: in main chunk [C]: ? stack traceback: test.lua:17: in function <test.lua:16> test.lua:6: in function 'test1' test.lua:21: in main chunk [C]: ? 4 stack traceback: test.lua:17: in function <test.lua:16> test.lua:7: in function 'test1' test.lua:21: in main chunk [C]: ? lua: test.lua:7: attempt to index global 'c' (a nil value) stack traceback: test.lua:7: in function 'test1' test.lua:21: in main chunk [C]: ? >Exit code: 1
相关文章推荐
- Lua之pcall和xpcall之学习
- Lua如何设置局部变量值进行调试
- VC中出现error C2064: term does not evaluate to a function
- Lua5.3 注册表 _G _ENV
- lua元表
- Lua5.3 注册表 _G _ENV
- 该怎样理解 Lua 逻辑运算符 and 和 or 的设计?
- Lua for Windows 安装方法
- Lua与C——扩展Lua(四)
- OpenWrt Web 开发 LuCI框架 lua语言
- 转 Lua标准库: table函数, 数学函数, 字符串函数/格式化/配对,
- Lua与C——调用Lua函数(三)
- Lua与C——执行Lua脚本(二)
- Lua时间转换
- Lua与C通信——Lua API(一)
- 【原】lua的table深拷贝
- lua string 库
- Lua 学习笔记三 Mac安装Lua
- Evaluate Reverse Polish Notation
- Lua脚本认知小结