Module debug
The Debug Library.
This library provides the functionality of the debug interface to Lua programs. You should exert care when using this library. Several of its functions violate basic assumptions about Lua code (e.g., that variables local to a function cannot be accessed from outside; that userdata metatables cannot be changed by Lua code; that Lua programs do not crash) and therefore can compromise otherwise secure code. Moreover, some functions in this library may be slow.
All functions in this library are provided inside the debug table.
All functions that operate over a thread have an optional first argument
which is the thread to operate over. The default is always the current thread.
Type debug
| debug.debug() |
Enters an interactive mode with the user, running each string that the user enters. |
| debug.gethook(thread) |
Returns the current hook settings of the thread, as three values: the
current hook function, the current hook mask, and the current hook count
(as set by the |
| debug.getinfo(thread, func, what) |
Returns a table with information about a function. |
| debug.getlocal(thread, f, local) |
This function returns the name and the value of the |
| debug.getmetatable(value) |
Returns the metatable of the given |
| debug.getregistry() |
Returns the registry table. |
| debug.getupvalue(f, up) |
This function returns the name and the value of the upvalue with index
|
| debug.getuservalue(u) |
Returns the Lua value associated to |
| debug.sethook(thread, hook, mask, count) |
Sets the given function as a hook. |
| debug.setlocal(thread, level, local, value) |
This function assigns the value |
| debug.setmetatable(value, table) |
Sets the metatable for the given |
| debug.setupvalue(func, up, value) |
This function assigns the value |
| debug.traceback(thread, message, level) |
If |
| debug.upvalueid(f, n) |
Returns an unique identifier (as a light userdata) for the upvalue |
| debug.upvaluejoin(f1, n1, f2, n2) |
Make the |
Type debug
Field(s)
- debug.debug()
-
Enters an interactive mode with the user, running each string that the user enters.
Using simple commands and other debug facilities, the user can inspect global and local variables, change their values, evaluate expressions, and so on. A line containing only the word
contfinishes this function, so that the caller continues its execution.Note that commands for
debug.debugare not lexically nested within any function, and so have no direct access to local variables.
- debug.gethook(thread)
-
Returns the current hook settings of the thread, as three values: the current hook function, the current hook mask, and the current hook count (as set by the
debug.sethookfunction).Parameter
-
#thread thread: thread to handle (optional).
-
- debug.getinfo(thread, func, what)
-
Returns a table with information about a function.
You can give the function directly, or you can give a number as the value of
func, which means the function running at levelfuncof the call stack of the given thread: level 0 is the current function (getinfoitself); level 1 is the function that calledgetinfo; and so on. Iffunctionis a number larger than the number of active functions, thengetinforeturns nil.The returned table can contain all the fields returned by
lua_getinfo, with the stringwhatdescribing which fields to fill in. The default forwhatis to get all information available, except the table of valid lines. If present, the option 'f' adds a field namedfuncwith the function itself. If present, the option 'L' adds a field namedactivelineswith the table of valid lines.For instance, the expression
debug.getinfo(1,"n").namereturns a table with a name for the current function, if a reasonable name can be found, and the expressiondebug.getinfo(print)returns a table with all available information about theprintfunction.Parameters
-
#thread thread: thread to handle (optional). -
func: the function or a number which means the function running at levelfunc. -
#string what: used to precise information returned (optional).
Return value
#table: with information about the function
func. -
- debug.getlocal(thread, f, local)
-
This function returns the name and the value of the
localvariable with index local of the function at levelfof the stack.This function accesses not only xplicit local variables, but also parameters, temporaries, etc.
The first parameter or local variable has index
1, and so on, until the last active variable. Negative indices refer to vararg parameters;-1is the first vararg parameter. The function returns nil if there is no variable with the given index, and raises an error when called with a level out of range. (You can calldebug.getinfoto check whether the level is valid.)Variable names starting with '(' (open parenthesis) represent internal variables (loop control variables, temporaries, varargs, and C function locals).
The parameter
fmay also be a function. In that case, getlocal returns only the name of function parameters.Parameters
-
#thread thread: thread which owns the local variable (optional). -
f: the stack level or a function -
#number local: the index of the local variable.
Return values
-
#string: The name and the value of the local variable with index
localof the function at levellevelof the stack. -
#nil: if no variable was found
-
- debug.getmetatable(value)
-
Returns the metatable of the given
valueor nil if it does not have a metatable.Parameter
-
value: value to handle.
Return value
#table: the metatable of the given
objector nil if it does not have a metatable. -
- debug.getregistry()
-
Returns the registry table.
Return value
#table: The registry table
- debug.getupvalue(f, up)
-
This function returns the name and the value of the upvalue with index
upof the functionf.The function returns nil if there is no upvalue with the given index.
Parameters
-
f: function which owns the upvalue. -
#number up: index of upvalue.
Return values
-
The name and the value of the upvalue of the function
f. -
#nil: no upvalue found.
-
- debug.getuservalue(u)
-
Returns the Lua value associated to
u.If
uis not a userdata, returns nil.Parameter
-
#userdata u: userdata
Return values
-
the value of the userdata
-
#nil: no userdata found
-
- debug.sethook(thread, hook, mask, count)
-
Sets the given function as a hook.
The string
maskand the numbercountdescribe when 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;"l": the hook is called every time Lua enters a new line of code.
With a
countdifferent from zero, the hook is called after everycountinstructions.When called without arguments,
debug.sethookturns off the hook.When the hook is called, its first parameter is a string describing the event that has triggered its call:
"call","return"(or `"tail return", when simulating a return from a tail call),"line"`, and"count". For line events, the hook also gets the new line number as its second parameter. Inside a hook, you can callgetinfowith level 2 to get more information about the running function (level 0 is thegetinfofunction, and level 1 is the hook function).Parameters
-
#thread thread: thread on which the hook is set (optional). -
hook: a function which takes two argument : event as string and line number. -
#string mask: could be"c","r"or"l". -
#number count: the hook is called after everycountinstructions (optional).
- debug.setlocal(thread, level, local, value)
-
This function assigns the value
valueto the local variable with indexlocalof the function at levellevelof the stack.The function returns nil if there is no local variable with the given index, and raises an error when called with a
levelout of range. (You can callgetinfoto check whether the level is valid.) Otherwise, it returns the name of the local variable. See debug.getlocal for more information about variable indices and names.Parameters
-
#thread thread: thread which owns the local variable (optional). -
#number level: the stack level. -
#number local: the index of the local variable. -
value: the new value.
Return values
-
#string: the name of the variable if it succeed.
-
#nil: no local variable with the given index.
-
- debug.setmetatable(value, table)
-
Sets the metatable for the given
valueto the giventable(which can be nil).Returns
value.Parameters
-
value: value to handle. -
#table table: the metatable forobject.
Return value
the given value
-
- debug.setupvalue(func, up, value)
-
This function assigns the value
valueto the upvalue with indexupof the functionf.The function returns nil if there is no upvalue with the given index. Otherwise, it returns the name of the upvalue.
Parameters
-
func: function which owns the upvalue. -
#number up: index of the upvalue. -
value: the new value.
Return values
-
#string: the name of the upvalue if it succeed.
-
#nil: if there no upvalue with the given index.
-
- debug.traceback(thread, message, level)
-
If
messageis present but is neither a#stringnor nil, this function returns message without further processing.Otherwise, it returns a string with a traceback of the call stack. An optional
messagestring is appended at the beginning of the traceback. An optionallevelnumber tells at which level to start the traceback (default is 1, the function calling traceback).Parameters
-
#thread thread: thread which owns the local variable (optional). -
#string message: original message (optional). -
#number level: (1 by default, optional).
Return value
#string: message with additional traceback informations.
-
- debug.upvalueid(f, n)
-
Returns an unique identifier (as a light userdata) for the upvalue
nfrom the given function.These unique identifiers allow a program to check whether different closures share upvalues. Lua closures that share an upvalue (that is, that access a same external local variable) will return identical ids for those upvalue indices.
Parameters
-
f: function which owns the upvalue. -
#number n: index of the upvalue.
Return value
#userdata: id of the upvalue
-
- debug.upvaluejoin(f1, n1, f2, n2)
-
Make the
n1-th upvalue of the Lua closuref1refer to then2-th upvalue of the Lua closuref2.Parameters
-
f1: function. -
#number n1: upvalue index in the function. -
f2: targeted function. -
#number n2: upvalue index in the targeted function.
-