相关函数
# debug.debug():调试模式
命令行进入调试模式,可调试函数、表达式等,输入cont退出debug模式
# debug.gethook(optional thread)
返回三个表示线程钩子设置的值: 当前钩子函数,当前钩子掩码,当前钩子计数
# debug.getinfo([thread,] f [, what]): 返回函数的表信息
f:函数、或者用一个数字f表示
数字f(f可为任意数字)表示运行在指定线程的调用栈对应层次上的函数:
0层表示当前函数(getinfo 自身);
1层表示调用 getinfo 的函数 (除非是尾调用,这种情况不计入栈);
如果f是一个比活动函数数量还大的数字, getinfo返回 nil
# debug.getmetatable(value):返回给定对象的元表,如果不存在返回nil
Returns the metatable of the given value
or nil if it does not have a metatable.
# debug.getregistry():返回注册表
Returns the registry table
# debug.getuservalue(u, n):获取第n个user值,还有boolean值,找到返回true,没找到返回false
Returns the n-th user value associated to the userdata u plus a boolean,
false if the userdata does not have that value.
# debug.sethook ([thread,] hook, mask [, count]):将一个函数作为钩子函数设入
字符串 mask 以及数字 count 决定了钩子将在何时调用,mask可选值:
'c': 每当 Lua 调用一个函数时,调用钩子
'r': 每当 Lua 从一个函数内返回时,调用钩子
'l': 每当 Lua 进入新的一行时,调用钩子
# debug.setlocal ([thread,] level, local, value):
将value赋给栈上第level层函数的第local个局部变量
如果没有那个变量,函数返回nil
如果level越界,抛出一个错误
# debug.setmetatable (value, table):将value的元表设为 table(可以是 nil),返回value
Sets the metatable for the given value to the given table
(which can be nil). Returns value.
# debug.setuservalue (udata, value, n):设置用户自定义变量的第n个值为value
Sets the given value as the n-th user value associated to the given udata.
udata must be a full userdata.
Returns udata, or fail if the userdata does not have that value
# debug.traceback ([thread,] [message [, level]])
如果有message,且不是字符串或nil,函数不做任何处理直接返回message,否则返回调用栈的栈回溯信息
message如果是字符串,会被添加在栈回溯信息的开头
level(可选)指明从栈的哪一层开始回溯(默认为 1 ,即调用 traceback 的那里)
# debug.upvalueid (f, n):从给定函数中返回第n个upvalue的标识符
Returns a unique identifier (as a light userdata) for the
upvalue numbered n from the given function.
# debug.upvaluejoin (f1, n1, f2, n2)
Make the n1-th upvalue of the Lua closure f1
refer to the n2-th upvalue of the Lua closure f2.
debug.getlocal([thread,] f, local):获取函数内部设置的局部变量
This function returns the name and the value of the local
variable with index local of the function at level f of the
stack. This function accesses not only explicit local variables,
but also parameters and temporary values.
* 返回索引为local的局部变量的name、value
* 该函数可获取局部变量、参数、临时变量
The first parameter or local variable has index 1, and so on,
following the order that they are declared in the code, counting
only the variables that are active in the current scope of the function.
* 第一个局部变量的索引为1
Compile-time constants may not appear in this listing, if they were
optimized away by the compiler. Negative indices refer to vararg arguments;
-1 is the first vararg argument.
* 负数表示参数:-1表示第一个参数
The function returns fail if there is no variable with the given index,
and raises an error when called with a level out of range. (You can call
debug.getinfo to check whether the level is valid.)
* 如果没有对应的局部变量,会报错
Variable names starting with '(' (open parenthesis) represent variables with no
known names (internal variables such as loop control variables, and variables from
chunks saved without debug information).
* 以"("开头的变量名表示没有显示的名称
The parameter f may also be a function. In that case, getlocal returns only the
name of function parameters.
* f可以是函数,表示获取指定函数的参数
debug.setlocal( [thread,] level, local, value ):设置函数的内部局部变量、调用该函数的函数的局部变量
This function assigns the value value to the local variable
with index local of the function at level level of the stack.
* 设置函数的局部变量,local为索引,level为所在函数的栈
The function returns fail if there is no local variable with
the given index, and raises an error when called with a level
out of range. (You can call getinfo to check whether the level
is valid.) Otherwise, it returns the name of the local variable.
* 如果没有对应的局部变量,会报错
debug.getupvalue(f, up):返回函数使用的外部局部变量,索引up表示第几个外部变量
This function returns the name and the value of the
upvalue with index up of the function f. The function
returns fail if there is no upvalue with the given index.
* 返回指定函数f的索引为up的upvalue(外部变量)的name、value
* 如果没有返回nil
For Lua functions, upvalues are the external local variables
that the function uses, and that are consequently included
in its closure.
* 如果是lua函数,upvalues是函数使用的外部局部变量
For C functions, this function uses the empty string ""
as a name for all upvalues.
* 如果是c函数,所有upvalue的name都是""
Variable name '?' (interrogation mark) represents variables
with no known names (variables from chunks saved without
debug information).
* ?表示变量没有显示的名称
debug.setupvalue(f, up, value):设置函数第up个外部变量的值
This function assigns the value value to the upvalue with index up of the
function f. The function returns fail if there is no upvalue with the given
index. Otherwise, it returns the name of the upvalue.
* 设置函数第up个外部局部变量的value,设置成功返回该局部变量的name
* 如果没有该变量,返回nil
示例:debug命令行调试
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 进入命令行调试模式
> debug.debug()
-- 自定义函数
lua_debug> function fun(a,b) print(a+b) end
-- 调试函数:fun(1)
lua_debug> fun(1)
(debug command):1: attempt to perform arithmetic on a nil value (local 'b')
-- 调试函数:fun(1,2)
lua_debug> fun(1,2)
3
-- 退出命令行调试模式
lua_debug> cont
示例:traceback
-- debug脚本
huli@hudeMacBook-Pro lua % cat debug.lua
function fun()
print(debug.traceback("traceback ==> "))
end
fun()
-- 运行脚本
huli@hudeMacBook-Pro lua % lua debug.lua
traceback ==>
stack traceback:
debug.lua:2: in function 'fun'
debug.lua:5: in main chunk
[C]: in ?
示例:getinfo
huli@hudeMacBook-Pro lua % cat debug.lua
-- 自定义的函数
function fun()
i=0
j=0
print(i,j)
end
-- 获取函数信息
for key,value in pairs(debug.getinfo(fun)) do
print(key,value)
end
-- 执行脚本
huli@hudeMacBook-Pro lua % lua debug.lua
func function: 0x600002b08cf0
short_src de.lua
namewhat
nups 1
currentline -1
istailcall false
ntransfer 0
isvararg false
nparams 0
ftransfer 0
lastlinedefined 5
linedefined 1
source @de.lua
what Lua
示例:getupvalue、setupvalue
-- lua脚本
huli@hudeMacBook-Pro lua % cat debug.lua
-- 外部局部变量(非局部变量debug.getupvalue不会读取)
local i=0
local j=0
function fun()
i=i+1
j=j+1
print(i,j)
end
-- 读取函数fun使用的外部局部变量
print(debug.getupvalue(fun,1))
print(debug.getupvalue(fun,2))
-- 执行函数
fun()
-- 读取函数fun使用的外部局部变量
print(debug.getupvalue(fun,1))
print(debug.getupvalue(fun,2))
-- 设置函数fun使用的外部局部变量
debug.setupvalue(fun,1,10)
debug.setupvalue(fun,2,10)
-- 执行函数
fun()
-- 执行脚本
huli@hudeMacBook-Pro lua % lua debug.lua
i 0
j 0
1 1
i 1
j 1
11 11
示例:upvalueid
-- upvalueid 脚本
huli@hudeMacBook-Pro lua % cat debug.lua
local i=0
local j=0
function fun()
i=i+1
j=j+1
print(i,j)
end
print(debug.upvalueid(fun,1))
print(debug.upvalueid(fun,2))
-- 执行脚本,返回userdata数据
huli@hudeMacBook-Pro lua % lua debug.lua
userdata: 0x600000ed8d20
userdata: 0x600000ed8d50
示例:getlocal、setlocal
huli@hudeMacBook-Pro lua % cat debug.lua
local a=0
local b=0
print("主函数局部变量debug前")
print('a',a)
print('b',b)
function fun()
local i=0
local j=0
print("\n函数局部变量debug前")
print("debug.getlocal(1,1)", debug.getlocal(1, 1))
print("debug.getlocal(1,2)", debug.getlocal(1, 2))
debug.setlocal(1,1,1) --设置1层(fun函数)的局部变量,index=1的局部变量值为4
debug.setlocal(1,2,2) --设置1层(fun函数)的局部变量,index=1的局部变量值为4
debug.setlocal(2,1,3) --设置2层(调用fun函数的main函数)的局部变量,index=1的局部变量值为4
debug.setlocal(2,2,4) --设置2层调用fun函数的main函数)的局部变量,index=2的局部变量值为4
print("\n函数局部变量debug后:")
print("debug.getlocal(1,1)", debug.getlocal(1,1))
print("debug.getlocal(1,2)", debug.getlocal(1,2))
end
print()
--print(debug.setlocal(2,1,1))
--print(debug.setlocal(2,2,2))
fun()
print("\n主函数局部变量debug后")
print("a",a)
print("b",b)
-- 执行脚本
huli@hudeMacBook-Pro lua % lua debug.lua
主函数局部变量debug前
a 0
b 0
函数局部变量debug前
debug.getlocal(1,1) i 0
debug.getlocal(1,2) j 0
函数局部变量debug后:
debug.getlocal(1,1) i 1
debug.getlocal(1,2) j 2
主函数局部变量debug后
a 3
b 4