Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Lua Debugging (Debug)

Lua Debugging (Debug)

Lua provides the debug library for creating our own custom debuggers. Lua itself does not have a built-in debugger, but many developers have shared their Lua debugger code.

The debug library in Lua contains the following functions:

No. Method & Purpose
1. **debug(): **

Enter an interactive user mode, running each string entered by the user. Using simple commands and other debug settings, the user can inspect global and local variables, change variable values, evaluate expressions, and so on.


Entering a line containing only cont ends this function, so the caller can continue running.

| | 2. | **getfenv(object): **

Returns the environment of an object.

| | 3. | **gethook(optional thread): **

Returns three values representing the thread hook settings: the current hook function, the current hook mask, and the current hook count

| | 4. | **getinfo ([thread,] f [, what]): **

Returns a table with information about a function. You can provide the function directly, or use a number f to represent it. The number f represents the function at the corresponding level of the call stack running in the specified thread: level 0 represents the current function (getinfo itself); level 1 represents the function that called getinfo (unless it is a tail call, which is not counted in the stack); and so on. If f is a number larger than the number of active functions, getinfo returns nil.

| | 5. | **debug.getlocal ([thread,] f, local): **

This function returns the name and value of the local variable with index local of the function at stack level f. This function is used not only to access explicitly defined local variables, but also parameters, temporary variables, etc.

| | 6. | **getmetatable(value): **

Pushes the metatable of the value at the given index onto the stack. If the index is invalid, or the value has no metatable, the function returns 0 and pushes nothing onto the stack.

| | 7. | **getregistry(): **

Returns the registry table, a predefined table that can be used to save any Lua value that C code wants to save.

| | 8. | **getupvalue (f, up)**

This function returns the name and value of the up-th upvalue of function f. If the function does not have that upvalue, it returns nil.
A variable name beginning with '(' (opening parenthesis) indicates a variable with no name (a code block with debug information removed).

| | 10. | **sethook ([thread,] hook, mask [, count]):**

Sets a function as the hook function. The string mask and the number count determine when the hook will be called. The mask is a string composed of the following characters, each with its own meaning:

| | 11. | **setlocal ([thread,] level, local, value): **

This function assigns value to the local-th local variable of the function at level level on the stack. If there is no such variable, the function returns nil. If level is out of range, an error is thrown.

| | 12. | **setmetatable (value, table): **

Sets the metatable of value to table (which can be nil). Returns value.

| | 13. | **setupvalue (f, up, value): **

This function sets value as the up-th upvalue of function f. If the function does not have that upvalue, it returns nil; otherwise, it returns the name of the upvalue.

| | 14. | **traceback ([thread,] [message [, level]]):**

If message is present and is not a string or nil, the function returns message directly without any processing. Otherwise, it returns the stack traceback information of the call stack. The optional string message is added at the beginning of the traceback information. The optional number level indicates from which stack level to start the traceback (default is 1, i.e., where traceback is called).

|

The above table lists the debugging functions we commonly use; next we can look at some simple examples:

Example

function

myfunction

(

)

print

(

debug

.

traceback

(

"Stack trace"

)

)

print

(

debug

.

getinfo

(

1

)

)

print

(

"Stack trace end"

)

       

return

10

end

myfunction

(

)

print

(

debug

.

getinfo

(

1

)

)

The output of the code above is:


Stack trace
stack traceback:
    test2.lua:2: in function 'myfunction'
    test2.lua:8: in main chunk
    [C]: ?
table: 0054C6C8
Stack trace end

In this example, we used the traceback and getinfo functions of the debug library; the getinfo function is used to return a table of function information.

Another example

We often need to debug the local variables inside a function. We can use the setupvalue function to set these local variables. Example as follows:

Example

function

newCounter

(

)

 

local

n

=

0

 

local

k

=

0

 

return

function

(

)

    k

=

n

    n

=

n

1

   

return

n

   

end

end

counter

=

newCounter

(

)

print

(

counter

(

)

)

print

(

counter

(

)

)

local

i

=

1

repeat

  name

,

val

=

debug

.

getupvalue

(

counter

,

i

)

 

if

name

then

   

print

(

"index"

,

i

,

name

,

"="

,

val

)

       

if

(

name

==

"n"

)

then

               

debug

.

setupvalue

(

counter

,

2

,

10

)

       

end

    i

=

i

1

 

end

-- if

until

not

name

print

(

counter

(

)

)

The output of the code above is:


1
2
index    1    k    =    1
index    2    n    =    2
11

In the above example, the counter increments by 1 on each call. We used the getupvalue function to view the current state of the local variable. We can set the local variable to a new value. In the example, before setting, the value of n is 2; using the setupvalue function we set it to 10. Now we call the function, and the output after execution is 11 instead of 3.


Debug types

Command-line debuggers include: RemDebug, clidebugger, ctrace, xdbLua, LuaInterface - Debugger, Rldb, ModDebug.

Graphical interface debuggers include: SciTE, Decoda, ZeroBrane Studio, akdebugger, luaedit.