Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Lua debugging (Debug)


May 12, 2021 Lua


Table of contents


Lua debugging (Debug)

Lua provides the debug library to provide the ability to create our custom regulators. Lua itself doesn't have a built-in regulator, but many developers share their Lua regulator code.

The debug library in Lua contains the following functions:

sethook ([thread,] hook, mask [, count]):
Serial number Methods and uses
1. debug():

Enter a user interaction mode and run each string entered by the user. U sing simple commands and other debug settings, users can review global and local variables, change the value of variables, evaluate some expressions, and so on.
Entering a string that contains only cont ends the function so that the caller can continue to run down.

2. getfenv(object):

Returns the object's environment variable.

3. gethook(optional thread):

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

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

Returns a table of information about a function. Y ou can provide the function directly, or you can represent it with a number f. T he number f represents a function running on the corresponding level of the call stack of the specified thread: layer 0 represents the current function (getinfo itself); I f f is a number bigger 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 at the f-level of the stack where the function is indexed. T his function is used not only to access explicitly defined local variables, but also to include parameters, temporary variables, and so on.

6. getmetatable(value):

Press the metasurger of the value to which the given index points onto the stack. If the index is invalid, or if the value does not have a metasheet, the function returns 0 and does not press anything on the stack.

7. getregistry():

Returns to the registry table, which is a predefined table that can be used to hold lua values that any C code wants to save.

8. getupvalue (f, up)

This function returns the name and value of the top value of the up of the function f. I f the function does not have that upper value, return nil .
A variable that starts with '(' (open parentheses) represents a variable without a name (removes the block of code for debug information).

10. Set a function as a hook function. T he string mask and the number count determine when the hook will be called. A mask is a string of the following characters, each of which has its meaning:

  • c Whenever Lua calls a function, the hook is called;
  • r Call the hook whenever Lua returns from within a function;
  • l Whenever Lua enters a new line, the hook is called.
11. setlocal ([thread,] level, local, value):

This function assigns value to the local variable of the level layer function on the stack. I f there is no variable, the function returns nil. If level crosses the line, throw an error.

12. setmetatable (value, table):

Set the metasheet of value to table (which can be nil). Return value.

13. setupvalue (f, up, value):

This function places value as the top value of the up of the function f. If the function does not have that upper value, return nil otherwise, return the name of the upper value.

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

If the message has, and is not a string or nil, the function does nothing to return the message directly. O therwise, it returns stack backtracking information for the call stack. S tring optional message is added at the beginning of the stack backtracking information. The number optional level indicates which layer of the stack to start backtracking (the default is 1, which is where traceback is called).

The table above lists our commonly used debug functions, and let's look at some simple examples:

function myfunction ()
print(debug.traceback("Stack trace"))
print(debug.getinfo(1))
print("Stack trace end")
    return 10
end
myfunction ()
print(debug.getinfo(1))

The above code output results in:

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

In the example, we use the traceback and getinfo functions of the debug library, which are tables that return function information.

Another instance

We often need to debug local variables within functions. W e can use the getupvalue function to set these local variables. Here's an 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 above code output results in:

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

In the above example, the counter adds 1 on each call. I n the example, we used the getupvalue function to see the current state of the local variable. W e can set the local variable to the new value. I n the instance, the value of the first n is set to 2, which is set to 10 using the setupvalue function. Now we call the function and the output after execution is 11 instead of 3.


The debug type

  • Command-line debugging
  • Graphic interface debugging

Command-line debuggers are: RemDebug, Clidebugger, ctrace, xdbLua, Lua Interface - Debugger, Rldb, ModDebug.

The graphics debuggers are: SciTE, Decoda, ZeroBrane Studio, akdebugger, luaedit.