Debug facilities
Tarantool users can benefit from built-in debug facilities that are part of:
The debug library provides an interface for debugging Lua programs. All
functions in this library reside in the debug table. Those functions that
operate on a thread have an optional first parameter that specifies the thread
to operate on. The default is always the current thread.
Note
This library should be used only for debugging and profiling and not as a regular programming tool, as the functions provided here can take too long to run. Besides, several of these functions can compromise otherwise secure code.
Below is a list of all debug functions.
| Name | Use |
|---|---|
| debug.debug() | Enter an interactive mode |
| debug.getfenv() | Get an object’s environment |
| debug.gethook() | Get a thread’s current hook settings |
| debug.getinfo() | Get information about a function |
| debug.getlocal() | Get a local variable’s name and value |
| debug.getmetatable() | Get an object’s metatable |
| debug.getregistry() | Get the registry table |
| debug.getupvalue() | Get an upvalue’s name and value |
| debug.setfenv() | Set an object’s environment |
| debug.sethook() | Set a given function as a hook |
| debug.setlocal() | Assign a value to a local variable |
| debug.setmetatable() | Set an object’s metatable |
| debug.setupvalue() | Assign a value to an upvalue |
| debug.sourcedir() | Get the source directory name |
| debug.sourcefile() | Get the source file name |
| debug.traceback() | Get a traceback of the call stack |
-
debug.debug()¶ Enters an interactive mode and runs each string that the user types in. The user can, among other things, inspect global and local variables, change their values and evaluate expressions.
Enter
contto exit this function, so that the caller can continue its execution.Note
Commands for
debug.debug()are not lexically nested within any function and so have no direct access to local variables.
-
debug.getfenv(object)¶ Parameters: - object – object to get the environment of
Return: the environment of the
object
-
debug.gethook([thread])¶ Return: the current hook settings of the
threadas three values:- the current hook function
- the current hook mask
- the current hook count as set by the
debug.sethook()function
-
debug.getinfo([thread, ]function[, what])¶ Parameters: - function – function to get information on
- what (
string) – what information on thefunctionto return
Return: a table with information about the
functionYou can pass in a
functiondirectly, or you can give a number that specifies a function running at levelfunctionof the call stack of the giventhread: level 0 is the current function (getinfo()itself), level 1 is the function that calledgetinfo(), and so on. Iffunctionis a number larger than the number of active functions,getinfo()returnsnil.The default for
whatis to get all information available, except the table of valid lines. If present, the optionfadds a field namedfuncwith the function itself. If present, the optionLadds a field namedactivelineswith the table of valid lines.
-
debug.getlocal([thread, ]level, local)¶ Parameters: - level (
number) – level of the stack - local (
number) – index of the local variable
Return: the name and the value of the local variable with the index
localof the function at levellevelof the stack ornilif there is no local variable with the given index; raises an error iflevelis out of rangeNote
You can call
debug.getinfo()to check whether the level is valid.- level (
-
debug.getmetatable(object)¶ Parameters: - object – object to get the metatable of
Return: a metatable of the
objectornilif it does not have a metatable
-
debug.getregistry()¶ Return: the registry table
-
debug.getupvalue(func, up)¶ Parameters: - func (
function) – function to get the upvalue of - up (
number) – index of the function upvalue
Return: the name and the value of the upvalue with the index
upof the functionfuncornilif there is no upvalue with the given index- func (
-
debug.setfenv(object, table)¶ Sets the environment of the
objectto thetable.Parameters: - object – object to change the environment of
- table (
table) – table to set the object environment to
Return: the
object
-
debug.sethook([thread, ]hook, mask[, count])¶ Sets the given function as a hook. When called without arguments, turns the hook off.
Parameters: - hook (
function) – function to set as a hook - mask (
string) –describes when the
hookwill be called; may have the following values:c- thehookis called every time Lua calls a functionr- thehookis called every time Lua returns from a functionl- thehookis called every time Lua enters a new line of code
- count (
number) – describes when thehookwill be called; when different from zero, thehookis called after everycountinstructions.
- hook (
-
debug.setlocal([thread, ]level, local, value)¶ Assigns the value
valueto the local variable with the indexlocalof the function at levellevelof the stack.Parameters: - level (
number) – level of the stack - local (
number) – index of the local variable - value – value to assign to the local variable
Return: the name of the local variable or
nilif there is no local variable with the given index; raises an error iflevelis out of rangeNote
You can call
debug.getinfo()to check whether the level is valid.- level (
-
debug.setmetatable(object, table)¶ Sets the metatable of the
objectto thetable.Parameters: - object – object to change the metatable of
- table (
table) – table to set the object metatable to
-
debug.setupvalue(func, up, value)¶ Assigns the value
valueto the upvalue with the indexupof the functionfunc.Parameters: - func (
function) – function to set the upvalue of - up (
number) – index of the function upvalue - value – value to assign to the function upvalue
Return: the name of the upvalue or
nilif there is no upvalue with the given index- func (
-
debug.sourcedir([level])¶ Parameters: - level (
number) – the level of the call stack which should contain the path (default is 2)
Return: a string with the relative path to the source file directory
Instead of
debug.sourcedir()one can saydebug.__dir__which means the same thing.Determining the real path to a directory is only possible if the function was defined in a Lua file (this restriction may not apply for loadstring() since Lua will store the entire string in debug info).
If
debug.sourcedir()is part of areturnargument, then it should be inside parentheses:return (debug.sourcedir()).- level (
-
debug.sourcefile([level])¶ Parameters: - level (
number) – the level of the call stack which should contain the path (default is 2)
Return: a string with the relative path to the source file
Instead of
debug.sourcefile()one can saydebug.__file__which means the same thing.Determining the real path to a file is only possible if the function was defined in a Lua file (this restriction may not apply to
loadstring()since Lua will store the entire string in debug info).If
debug.sourcefile()is part of areturnargument, then it should be inside parentheses:return (debug.sourcefile()).- level (
-
debug.traceback([thread, ][message][, level])¶ Parameters: - message (
string) – an optional message prepended to the traceback - level (
number) – specifies at which level to start the traceback (default is 1)
Return: a string with a traceback of the call stack
- message (
Debug example:
Make a file in the /tmp directory named example.lua, containing:
function w()
print(debug.sourcedir())
print(debug.sourcefile())
print(debug.traceback())
print(debug.getinfo(1)['currentline'])
end
w()
Execute tarantool /tmp/example.lua. Expect to see this:
/tmp
/tmp/example.lua
stack traceback:
/tmp/example.lua:4: in function 'w'
/tmp/example.lua:7: in main chunk
5