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: The output result of executing the above code is: In the example, we used the We often need to debug local variables within a function. We can use it. The output result of executing the above code is: In the above example, the counter increments by 1 with each call. In the example, we used Command line debugging Graphical interface debugging The command line debuggers are: The graphics debuggers are: 4.28.1. Example #
function myfunction () print(debug.traceback("Stack trace")) print(debug.getinfo(1)) print("Stack trace end") return 10 end myfunction () print(debug.getinfo(1))
Stack trace stack traceback: test2.lua:2: in function 'myfunction' test2.lua:8: in main chunk [C]: ? table: 0054C6C8 Stack trace end
debug library traceback and getinfo function. getinfo the table that the function uses to return function information.Another example #
setupvalue function to set these local variables. Examples are as follows: 4.28.2. 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())
1 2 index 1 k = 1 index 2 n = 2 11
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 #
RemDebug 、 clidebugger 、 ctrace 、 xdbLua 、 LuaInterface - Debugger 、 Rldb 、 ModDebug . SciTE 、 Decoda 、 ZeroBrane Studio 、 akdebugger 、 luaedit .