📜  Lua-调试

📅  最后修改于: 2020-10-16 05:17:23             🧑  作者: Mango


Lua提供了一个调试库,该库提供了所有原始函数供我们创建自己的调试器。即使没有内置的Lua调试器,我们也有很多Lua调试器,这些调试器由各种开发人员创建,其中许多都是开源的。

下表列出了Lua调试库中可用的功能及其用法。

Sr.No. Method & Purpose
1

debug()

Enters interactive mode for debugging, which remains active till we type in only cont in a line and press enter. User can inspect variables during this mode using other functions.

2

getfenv(object)

Returns the environment of object.

3

gethook(optional thread)

Returns the current hook settings of the thread, as three values − the current hook function, the current hook mask, and the current hook count.

4

getinfo(optional thread, function or stack level, optional flag)

Returns a table with info about a function. You can give the function directly, or you can give a number as the value of function, which means the function running at level function of the call stack of the given thread − level 0 is the current function (getinfo itself); level 1 is the function that called getinfo; and so on. If function is a number larger than the number of active functions, then getinfo returns nil.

5

getlocal(optional thread, stack level, local index)

Returns the name and the value of the local variable with index local of the function at level of the stack.Returns nil if there is no local variable with the given index, and raises an error when called with a level out of range.

6

getmetatable(value)

Returns the metatable of the given object or nil if it does not have a metatable.

7

getregistry()

Returns the registry table,a pre-defined table that can be used by any C code to store whatever Lua value it needs to store.

8

getupvalue(function, upvalue index)

This function returns the name and the value of the upvalue with index up of the function func. The function returns nil if there is no upvalue with the given index.

9

setfenv(function or thread or userdata, environment table)

Sets the environment of the given object to the given table. Returns object.

10

sethook(optional thread, hook function, hook mask string with “c” and/or “r” and/or “l”, optional instruction count)

Sets the given function as a hook. The string mask and the number count describes when the hook will be called. Here, c, r and l are called every time Lua calls, returns, and enters every line of code in a function respectively.

11

setlocal(optional thread, stack level, local index, value)

Assigns the value to the local variable with index local of the function at level of the stack. The function returns nil if there is no local variable with the given index, and raises an error when called with a level out of range. Otherwise, it returns the name of the local variable.

12

setmetatable(value, metatable)

Sets the metatable for the given object to the given table (which can be nil).

13

setupvalue(function, upvalue index, value)

This function assigns the value to the upvalue with index up of the function func. The function returns nil if there is no upvalue with the given index. Otherwise, it returns the name of the upvalue.

14

traceback(optional thread, optional message string, optional level argument)

Builds an extended error message with a traceback.

上面的列表是Lua中调试功能的完整列表,我们经常使用一个库,该库使用上述功能并提供了更容易的调试。使用这些函数并创建我们自己的调试器非常复杂,因此不可取。无论如何,我们将看到一个简单使用调试功能的示例。

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.trace函数打印堆栈跟踪。 debug.getinfo获取函数的当前表。

调试-示例

我们经常需要知道函数的局部变量以进行调试。为此,我们可以使用getupvalue并使用setupvalue来设置这些局部变量。下面显示了一个简单的示例。

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函数查看局部变量的当前状态。然后,我们将局部变量设置为新值。在此,在调用设置操作之前,n为2。使用setupvalue函数,它将更新为10。现在,当我们调用counter函数,它将返回11而不是3。

调试类型

  • 命令行调试
  • 图形调试

命令行调试

命令行调试是一种调试类型,它使用命令行在命令和打印语句的帮助下进行调试。有许多可用于Lua的命令行调试器,下面列出了其中一些。

  • RemDebug -RemDebug是Lua 5.0和5.1的远程调试器。它使您可以远程控制另一个Lua程序的执行,设置断点并检查程序的当前状态。 RemDebug也可以调试CGILua脚本。

  • clidebugger-用纯Lua编写的Lua 5.1的简单命令行界面调试器。它不依赖于标准Lua 5.1库以外的任何东西。它受到RemDebug的启发,但没有远程功能。

  • ctrace-跟踪Lua API调用的工具。

  • xdbLua -Windows平台的简单Lua命令行调试器。

  • LuaInterface-调试器-此项目是LuaInterface的调试器扩展。它将内置的Lua调试接口提升到更高的水平。与调试器的交互是通过事件和方法调用来完成的。

  • Rldb-这是通过套接字的远程Lua调试器,在Windows和Linux上均可用。它可以为您提供比现有功能更多的功能。

  • ModDebug-这允许远程控制另一个Lua程序的执行,设置断点并检查程序的当前状态。

图形调试

在IDE的帮助下可以进行图形化调试,在此您可以对各种状态(如变量值,堆栈跟踪和其他相关信息)进行可视化调试。在IDE中有一个可视化表示形式,并借助断点,逐步进入,逐步执行和其他按钮来逐步控制执行。

Lua有许多图形调试器,其中包括以下内容。

  • SciTE -Lua的默认Windows IDE提供了多种调试功能,例如断点,进入,进入,跳过,监视变量等。

  • Decoda-这是具有远程调试支持的图形调试器。

  • ZeroBrane Studio -Lua IDE,具有集成的远程调试器,堆栈视图,监视视图,远程控制台,静态分析器等。与LuaJIT,Love2d,Moai和其他Lua引擎一起使用; Windows,OSX和Linux。开源。

  • akdebugger -Eclipse的调试器和编辑器Lua插件。

  • luaedit-它具有远程调试,本地调试,语法突出显示,完成建议列表,参数提议引擎,高级断点管理(包括断点和命中数的条件系统),函数列表,全局和局部变量列表,监视,面向解决方案的管理的功能。