Lua debugging

The following content is reproduced from http://www.runoob.com/lua/lua-debug.html

Lua debugging (Debug)

Lua provides a debug library to provide the function of creating our custom debugger. Lua itself does not have a built-in debugger, but many developers share their Lua debugger code.

The debug library in Lua contains the following functions :

serial number Methods & Uses
1. debug():

Enter a user interaction mode and run each string entered by the user. Using simple commands and other debugging settings, users can review global and local variables, change the value of variables, calculate some expressions, and so on.
Entering a string containing only cont will end this function, so that the caller can continue to run down.

2. getfenv(object):

Return the environment variables of the object.

3. gethook(optional thread):

Returns three values ​​representing thread hook settings: current hook function, current hook mask, 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 a 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: level 0 represents the current function (getinfo itself); level 1 represents the function that calls getinfo (unless it is a tail call, which is not counted in the stack); etc. . If f is a number greater than the number of active functions, getinfo returns nil.

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

This function returns the name and value of the local variable whose index is local at layer f of the stack. This function is not only used to access explicitly defined local variables, but also includes formal parameters, temporary variables, etc.

6. getmetatable(value):

Push the meta table of the value pointed to by the given index onto the stack. If the index is invalid, or there is no metatable for this value, the function will return 0 and nothing will be pushed onto the stack.

7. getregistry():

Return to the registry table, which is a predefined table that can be used to save any Lua values ​​that C code wants to save.

8. getupvalue (f, up)

This function returns the name and value of the up value of function f. If the function does not have that up value, it returns nil.
Variable names beginning with'(‘ (opening brackets) indicate variables without names (code blocks without debugging information).

10. sethook ([thread,] hook, mask [, count]):

Set a function as a hook function. The string mask and the number count determine when the hook will be called. A mask is a string composed of the following characters, each character has its meaning:

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

This function assigns value to the local variable of the level-level function on the stack. If there is no such variable, the function returns nil. If the level is out of range, an error is thrown.

12. setmetatable (value, table):

Set the meta table of value to table (it can be nil). Return value.

13. setupvalue (f, up, value):

This function sets value to the up value of function f. If the function does not have that upvalue, it returns nil. Otherwise, it returns the name of the upvalue.

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

If the message has, and it is not a string or nil, the function returns the message without any processing. Otherwise, it returns the stack traceback information of the call stack. The string optional message is added at the beginning of the stack traceback message. The numeric option level indicates which level of the stack to start the traceback (the default is 1, which is where traceback is called).

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

function myfunction ()< /span>print (debug.traceback( "Stack trace"))< span class="kwd" style="border:0px;color:rgb(0,0,136);">print(debug.getinfo< /span>(1))print span>("Stack trace end")return 10endmyfunction ()print(debug.getinfo (1))

The output result of executing the above code is:

Stack tracestack traceback:< span class="pln" style="border:0px;color:rgb(0,0,0);">test2.lua:2: in function 'myfunction'test2.< /span>lua:8: in main chunk[C]: ?table< /span>: 0054C6C8Stack< /span> trace end

In the example, we use the traceback and getinfo functions of the debug library, The getinfo function is used to return a table of function information.

Another example

We often need to debug local variables in functions. We can use the getupvalue function to set these local variables. Examples are as follows:

function newCounter () local n = < span class="lit" style="border:0px;color:rgb(0,102,102);">0 local k = 0  return function < /span>() k = nn = n + 1 < /span>return n endendcounter = newCounter  ()print(counter())print(counter< span class="pun" style="border:0px;color:rgb(102,102,0);">())local i =  1 repeat name, val < span class="pun" style="border:0px;color:rgb(102,102,0);">= debug.getupvalue(< /span>counter, i) if name then print  ("index", i, name, "=", val) if(name == "n") then span>debug.setupvalue (counter,2,10) end i = i < span class="pun" style="border:0px;color:rgb(102,102,0);">+ 1 end< span class="pln" style="border:0px;color:rgb(0,0,0);"> -- if< /span>until not nameprint(counter()) 

The output result of executing the above code is:

12index1k = < span class="lit" style="border:0px;color:rgb(0,102,102);">1index    2    n    =< span class="pln" style="border:0px;color:rgb(0,0,0);">    211

在以上实例中,计数器在每次调用时都会自增1 .实例中我们使用了 getupvalue 函数查看局部变量的当前状态。我们可以设置局部变量为新值。实例中,在设置前 n 的值为 2,使用 setupvalue 函数将其设置为 10。现在我们调用函数,执行后输出为 11 而不是 3。


调试类型

  • 命令行调试
  • 图形界面调试

命令行调试器有:RemDebug、clidebugger、ctrace、xdbLua、LuaInterface – Debugger、Rldb、ModDebug。

图形界调试器有:SciTE、Decoda、ZeroBrane Studio、akdebugger、luaedit。

以下内容转载自 http://www.runoob.com/lua/lua-debug.html

Lua 调试(Debug)

Lua 提供了 debug 库用于提供创建我们自定义调试器的功能。 Lua 本身并未有内置的调试器,但很多开发者共享了他们的 Lua 调试器代码。

Lua 中 debug 库包含以下函数:

序号 方法 & 用途
1. debug():

进入一个用户交互模式,运行用户输入的每个字符串。使用简单的命令以及其它调试设置,用户可以检阅全局变量和局部变量, 改变变量的值,计算一些表达式,等等。 
输入一行仅包含 cont 的字符串将结束这个函数, 这样调用者就可以继续向下运行。

2. getfenv(object):

返回对象的环境变量。

3. gethook(optional thread):

返回三个表示线程钩子设置的值: 当前钩子函数,当前钩子掩码,当前钩子计数

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

返回关于一个函数信息的表。你可以直接提供该函数, 也可以用一个数字 f 表示该函数。数字 f 表示运行在指定线程的调用栈对应层次上的函数: 0 层表示当前函数(getinfo 自身); 1 层表示调用 getinfo 的函数 (除非是尾调用,这种情况不计入栈);等等。如果 f 是一个比活动函数数量还大的数字, getinfo 返回 nil。

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

此函数返回在栈的 f 层处函数的索引为 local 的局部变量 的名字和值。这个函数不仅用于访问显式定义的局部变量,也包括形参、临时变量等。

6. getmetatable(value):

把给定索引指向的值的元表压入堆栈。如果索引无效,或是这个值没有元表,函数将返回 0 并且不会向栈上压任何东西。

7. getregistry():

返回注册表表,这是一个预定义出来的表, 可以用来保存任何 C 代码想保存的 Lua 值。

8. getupvalue (f, up)

此函数返回函数 f 的第 up 个上值的名字和值。如果该函数没有那个上值,返回 nil 。 
以 ‘(‘ (开括号)打头的变量名表示没有名字的变量 (去除了调试信息的代码块)。

10. sethook ([thread,] hook, mask [, count]):

将一个函数作为钩子函数设入。 字符串 mask 以及数字 count 决定了钩子将在何时调用。 掩码是由下列字符组合成的字符串,每个字符有其含义:

  • c‘: 每当 Lua 调用一个函数时,调用钩子;
  • r‘: 每当 Lua 从一个函数内返回时,调用钩子;
  • l‘: 每当 Lua 进入新的一行时,调用钩子。
11. setlocal ([thread,] level, local, value):

这个函数将 value 赋给 栈上第 level 层函数的第 local 个局部变量。如果没有那个变量,函数返回 nil 。如果 level 越界,抛出一个错误。

12. setmetatable (value, table):

将 value 的元表设为 table (可以是 nil)。返回 value。

13. setupvalue (f, up, value):

这个函数将 value 设为函数 f 的第 up 个上值。如果函数没有那个上值,返回 nil 否则,返回该上值的名字。

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

如果 message 有,且不是字符串或 nil, 函数不做任何处理直接返回 message。否则,它返回调用栈的栈回溯信息。字符串可选项 message 被添加在栈回溯信息的开头。数字可选项 level 指明从栈的哪一层开始回溯 (默认为 1 ,即调用 traceback 的那里)。

上表列出了我们常用的调试函数,接下来我们可以看些简单的例子:

function myfunction ()print(debug.traceback("Stack trace"))print(debug.getinfo(1))print("Stack trace end")    return 10endmyfunction ()print(debug.getinfo(1))

执行以上代码输出结果为:

Stack tracestack traceback:    test2.lua:2: in function 'myfunction'    test2.lua:8: in main chunk    [C]:< /span> ?table: 0054C6C8Stack trace end

在以实例中,我们使用到了 debug 库的 traceback 和 getinfo 函数, getinfo 函数用于返回函数信息的表。

另一个实例

我们经常需要调试函数的内的局部变量。我们可以使用 getupvalue 函数来设置这些局部变量。实例如下:

function newCounter ()  local n = 0  local k = 0  return function ()    k = n    n =  n + 1    return n    endendcounter = newCounter ()print(counter())print(counter())local i = 1< /span>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 -- ifuntil not nameprint(counter())

执行以上代码输出结果为:

12index    1    k    =    1index    2    n    =    211

在以上实例中,计数器在每次调用时都会自增1。实例中我们使用了 getupvalue 函数查看局部变量的当前状态。我们可以设置局部变量为新值。实例中,在设置前 n 的值为 2,使用 setupvalue 函数将其设置为 10。现在我们调用函数,执行后输出为 11 而不是 3。


调试类型

  • 命令行调试
  • 图形界面调试

命令行调试器有:RemDebug、clidebugger、ctrace、xdbLua、LuaInterface – Debugger、Rldb、ModDebug。

图形界调试器有:SciTE、Decoda、ZeroBrane Studio、akdebugger、luaedit。

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3217 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.