Lua debugging
Lua
provide debug
the library is used to provide the ability to create our custom debugger. Lua itself doesn’t have a built-in debugger, butmany developers share their Lua debugger code.
Lua
in debug
the library contains the following functions:
Serial number |
Method and use |
---|---|
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
10 |
‘c’: functions: call hooks whenever Lua calls a function; ‘r’: functions: callhooks whenever Lua returns from within a function; ‘l’: call hookswhenever Lua enters a new line. |
11 |
|
12 |
|
13 |
|
14 |
|
The above table lists our commonly used debugging functions, and then we canlook 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 result of executing the above code 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 example, we used the debug
library traceback
and getinfo
function. getinfo
the table that the function uses to return function information.
Another example
We often need to debug local variables within a function. We can use it. setupvalue
function to set these local variables. Examples are 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 result of executing the above code is:
1
2
index 1 k = 1
index 2 n = 2
11
In the above example, the counter increments by 1 with each call. In the example, we used getupvalue
function to view the current state of a local variable. We can set the local variable to the new value. In the instance, the value of n before setting is 2, using the setupvalue
function to set it to 10. Now we call the function and the output after execution is 11 instead of 3.
Debug type
Command line debugging
Graphical interface debugging
The command line debuggers are: RemDebug
、 clidebugger
、 ctrace
、 xdbLua
、 LuaInterface - Debugger
、 Rldb
、 ModDebug
.
The graphics debuggers are: SciTE
、 Decoda
、 ZeroBrane Studio
、 akdebugger
、 luaedit
.