4.28. Lua debugging

发布时间 : 2025-10-25 13:34:10 UTC      

Page Views: 9 views

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

debug() :Enter a user interaction mode and run each string entered by theuser Using simple commands and other debugging settings, users can review global and local variables, change the value of variables, evaluate some expressions, and so on. Entering a string that contains only cont ends the function so that the caller can continue to run down.

2

getfenv(object) : Returns the environment variable of the object

3

gethook(optional thread) : Returns three values that represent thread hooksettings: current hook function, current hook mask, and 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 the number f to represent the function. The number f represents the function running on the corresponding level of the call stack of the specified thread: layer 0 represents the current function (getinfo itself); layer 1 represents the function calling getinfo (which is not counted in the stack unless it is a tail call); and so on. If f is a number larger than the number of active functions, getinfo returns nil.

5

debug.getlocal ([thread,] f, local) :This function returns the name and value of a local variable whose index is local at the f layer of the stack This function is used to access not only explicitly defined local variables,but also formal parameters, temporary variables, and so on.

6

getmetatable(value) :Pushes the meta-table of the value pointed to by a given index onto the stack If the index is invalid, or if the value does nothave a meta-table, the function returns 0 and does not press anything on the stack.

7

getregistry() :Returns the registry table, which is a predefined table that can be used to hold any Lua value that the C code wants to save.

8

getupvalue (f, up) :This function returns the name and value of the up upper value of function f. If the function does not have that upper value, it returns nil. A variable name that begins with’(‘(open parenthesis) represents an unnamed variable (a block of code that removes debugging information).

10

sethook ([thread,] hook, mask [, count]) :Set a function as a hook function The string mask and the numeric count determine when the hook will be called. A mask is a string composed of the following characters, each with its own meaning:

‘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

setlocal ([thread,] level, local, value) :This function assigns value to the local local variable of the level layer function on the stack If there is no such variable, the function returns nil. If the level crosses the bounds, an error is thrown.

12

setmetatable (value, table) :Set the meta-table of value to table (which can be nil). Return to value.

13

setupvalue (f, up, value) :This function sets value to the up upper valueof function f If the function does not have that upper value, return nil. Otherwise, return the name of the upper value.

14

traceback ([thread,] [message [, level]]) :If message exists and is not astring or nil, the function returns message without doing any processing. Otherwise, it returns stack backtracking information for the call stack. Thestring optional message is added at the beginning of the stack backtrackinginformation. The numeric optional level indicates which layer of the stack to start backtracking from (the default is 1, where the traceback is called).

The above table lists our commonly used debugging functions, and then we canlook at some simple examples:

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

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:

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

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 .

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。