Lua debugging (Debug)

Lua debugging (Debug)

Lua provides debug libraries for providing us create a custom debugger functionality. Lua itself does not have a built-in debugger, but many developers share their Lua debugger code. Lua in debug library includes the following functions:

No. Method & uses
1 debug (): entering a mode of user interaction, each string input by the user operation. Using simple commands and other debug settings, the user can review the global and local variables, change the value of the variable number of calculation expressions, and the like. Enter the string contains only one line cont will end the function, so that the caller can continue to run down.
2 getfenv (object): Returns the object environment variables.
3 gethook (optional thread): Returns the thread hook setting indicates three values: the current hook function, the current hook mask, the current count of the hook
4 getinfo ([thread,] f [, what]): Returns information about a function table. You can directly provide this function, the function can also be expressed by a number f. F represents a number in the call stack operating function level corresponding to the specified thread: layer 0 indicates the current function (getinfo itself); 1 represents a layer called function getinfo (unless tail call, this situation is not included in the stack); and the like . If f is a number greater than the activities of digital functions, getinfo returns nil.
5 debug.getlocal ([thread,] f, local): This function returns the index of the function f is a layer stack local variable names and values ​​of the local. This function is used to access local variables only explicitly defined, also include parameter, temporary variables.
6 getmetatable (value): The given index value table element pointed to by the stack. If the index is invalid, or the value table element is not, the function returns 0 and does nothing to the pressure on the stack.
7 getregistry (): returns the Registry table, which is out of a predefined table, can be used to store any code that you want to save C Lua value.
8 getupvalue (f, up) This function returns the names and values ​​of the function f up on a value. If it does not on that value, returns nil. With '(' variable name (open parenthesis) indicates the beginning of a variable name is not (in addition to debug code block information).
9 sethook ([thread,] hook, mask [, count]): The function of a hook as a function of the set. String mask and the digital count determines the hook will be called when. Mask is a combination of the following characters into the string, each character has its own meaning: 'c': Whenever Lua calls a function call to the hook; 'r': Whenever Lua returns from a function call to the hook ; 'l': every time Lua enters a new line, call the hook.
10 setlocal ([thread,] level, local, value): This function assigns the value of the first stack of local variables local level layer function. If you do not have that variable, the function returns nil. If the cross-border level, throw an error.
11 setmetatable (value, table): The value of the table element to Table (may be nil). Return value.
12 setupvalue (f, up, value): This function will first set up a value on the value of the function f. If the function is not on that value, otherwise it returns nil, returns the name of the value.
13 traceback ([thread,] [message [, level]]): If there are message, or nil and not a string, the function returns the message directly without any processing. Otherwise, it returns the call stack stack traceback information. String optional message added at the beginning of the stack traceback information. Digital options from which layer stack level indicates the start backtracking (default is 1, that is where the traceback of the call).

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))

The output is performed above code:

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

In order example, we used to debug traceback and getinfo function library, getinfo function returns the table function information.

Another example

We often need to debug local variables within the 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())

The output is performed above code:

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

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


Debug type

  • Command-line debugger
  • Graphical debugger

There are command-line debugger: RemDebug, clidebugger, ctrace, xdbLua, LuaInterface - Debugger, Rldb, ModDebug.

The debugger has a graphical interface: SciTE, Decoda, ZeroBrane Studio, akdebugger, luaedit.

Published 62 original articles · won praise 5 · Views 3943

Guess you like

Origin blog.csdn.net/qq_42194657/article/details/102819688