Latest web development tutorials

Lua debugging (Debug)

Lua provides debug library for providing us create a custom converter functions. Lua itself does not have a built-in governor, but many developers share their governor Lua code.

Lua in the debug library includes the following functions:

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

Enter a user interactive mode, running each string entered by the user. Using simple commands and other debug settings, the user can review the global and local variables, change the values ​​of variables to calculate a number of expressions, and so on.
String input line containing only cont will end this function, so that the caller can continue to run down.

2. getfenv (object):

Returns object environment variable.

3. gethook (optional thread):

Returns represent three thread hook set: the current hook function, the current hook mask, and the current hook count

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

Information about the function returns a table. You can provide this function, you can also use a digital representation of the function f. Digital function f represents running on the specified thread's call stack corresponding to levels: level 0 indicates the current function (getinfo itself); 1 layer indicates that the call getinfo function (unless it is the end of the call, this situation is not included in the stack); etc. . If f is a function of the number of activities than the larger number, getinfo returns nil.

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

This function returns the name and the value of the index of the function f layer stack for local local variables. This function is only used to access the local variables explicitly defined, including parameters, temporary variables.

6. getmetatable (value):

The given index points to the table element values ​​onto the stack. If the index is invalid, or the value of the yuan is no table, the function returns 0 and does not push anything to the stack.

7. getregistry ():

Back registry table, which is out of a predefined table can be used to save any C code you want to save Lua value.

8. getupvalue (f, up)

This function returns the names and values ​​of the function f the first one up on values. If it does not on that value, it returns nil.
In '(' (open parentheses) represent the variable name beginning with no variable name (except to debug the code block information).

10. It will function as a hook set into. String mask and the digital count determines the hook will be called when. The mask is a combination of the following characters into a string, each character has its own meaning:

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

This function will be the first local value assigned to the first stack local variable level layer function. If you do not have that variable, the function returns nil. If the cross-border level, throw an error.

12. setmetatable (value, table):

The value of the element table set table (can be nil). Return value.

13. setupvalue (f, up, value) :

This function will first set up a value on the value of the function f. If you do not have that function on a value, it returns nil otherwise, it returns the name of the previous values.

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

If you have a message, and not a string or nil, function without any treatment directly back message. Otherwise, it returns the call stack stack traceback information. Optional message string is added at the beginning of the stack traceback information. Digital options specified level from which layer stack began backtracking (default is 1, which calls where traceback of).

The table is our common debugging function, then 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 results:

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 debug library functions, getinfo function returns a table function information.

Another example

We often need to debug local variables within a function. We can use getupvalue function to set these local variables. Examples are 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())

Execute the above code output results:

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

In the example above, the counter will be incremented each time a call 1. Examples we used getupvalue function to view the current status of local variables. We can set a local variable to a new value. Example, before setting n is 2, use setupvalue function set it to 10. Now we call the function after the execution of the output is 11 instead of 3.


Debugging Type

  • Command-line debugger
  • Graphical debugger

Command-line debugger has: RemDebug, clidebugger, ctrace, xdbLua, LuaInterface - Debugger, Rldb, ModDebug.

Graphical interface debugger has: SciTE, Decoda, ZeroBrane Studio, akdebugger, luaedit.