Lua debugging (Debug)


Lua provides a debug library to provide the function of creating our custom governor. Lua itself does not have a built-in governor, but many developers have shared their Lua governor code.

The debug library in Lua contains the following functions:

sethook ([thread,] hook, mask [, count]):
Serial numberMethod & Purpose
1.debug():

Enter a user interactive mode and run Each string entered by the user. Using simple commands and other debugging settings, users can inspect global and local variables, change the values ​​of variables, evaluate some expressions, and more.
Entering a line containing only cont will end this function, so the caller can continue running downwards.

2.getfenv(object):

Returns the environment variable of the object.

3.gethook(optional thread):

Returns three values ​​representing thread hook settings: Current hook Function, current hook mask, current hook count

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

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

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

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

6.getmetatable(value):

Push the metatable of the value pointed to by the given index onto the stack . If the index is invalid, or there is no metatable for the value, the function returns 0 and does not push anything onto the stack.

7.getregistry():

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

8.getupvalue (f, up)

This function returns the up-th upper value of function f name and value. If the function does not have that upper value, nil is returned.
Variable names starting with '(' (open brackets) represent variables without names (code blocks with debugging information removed).

10. Set a function as a 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 character has its meaning:


  • 'c': Whenever Lua calls a function, the hook is called;

  • 'r': Whenever Lua returns from a function, the hook is called;

  • 'l': The hook is called every time Lua enters a new line.

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

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

12.setmetatable (value, table):

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

13.setupvalue (f, up, value):

This function sets value to function f The up value. If the function does not have that upper value, it returns nil. Otherwise, it returns the name of the upper value.

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

If message has , and is not a string or nil, the function returns the message directly without any processing. Otherwise, it returns the stack traceback information of the call stack. The optional string message is appended to the beginning of the stack traceback. The numeric optional level specifies at which level of the stack to start tracing back (defaults to 1 , which is where traceback is called).

The above table lists our commonly used debugging functions. Next we can 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))

Execute the above code output The result is:

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

In the above 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 local variables within functions. We can use the getupvalue function to set these local variables. The example is as follows:

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 result of executing the above code is:

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

In the above example, the counter will increase by 1 every time it is called. In the example, we use the getupvalue function to view the current status of local variables. We can set local variables to new values. In the example, the value of n before setting is 2, and the setupvalue function is used to set it to 10. Now we call the function and the output after execution is 11 instead of 3.


Debugging type

  • Command line debugging

  • Graphical interface debugging

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

Debuggers in the graphics world include: SciTE, Decoda, ZeroBrane Studio, akdebugger, and luaedit.