The Debug Library in Lua 选择自 xbcqu 的 Blog

本文介绍了Lua中的调试库,包括自醒函数和hook函数两大类。自醒函数如debug.getinfo可用于获取当前运行程序的信息,而hook函数则允许开发者在特定事件发生时执行回调函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
The Debug Library in Lua     选择自 xbcqu 的 Blog
关键字  The Debug Library in Lua
出处 

在lua 中没有给我们提供一个debugger,但是给我们提供了一个debug库,这个库在debugtable中定义了所有的函数,为我们写一个 debugger提供了原始的素材,只要我们充分利用它,就可以写一个自己的debugger,但是 在使用的时候我们应该比较吝啬一些,因为这些函数在lua中,并不保证它的执行效率的,如果在最终发布的版本中不需要这些函数,我们可以将其屏蔽掉,只需 要执行 debug = nil就可以了。

debug库包含了两种类型的函数:自醒函数(introspective functions) and hooks functions,introspective functions可以用来查看当前程序的一些信息的,hooks函数是用来跟踪程序的执行的。

关于debug库有一个重要概念就是桟的级别,桟的级别是一个数字,它指代一个特定的函数,这个函数是当前作用中的,被调用还没有返回的函数,调用debug库的函数的级别是1,调用这个函数的函数的级别是2,等等。
 
在lua的debug库中,主要的introspective函数是debug.getinfo函数,他的第一个参数可能是一个函数或者一个stack level,函数的返回值是一个关于这个函数的数据的一个table,table可能的域如下:

source --- Where the function was defined. If the function was defined in a string (through loadstring), source is that string. If the function was defined in a file, source is the file name prefixed with a `@′.

short_src --- A short version of source (up to 60 characters), useful for error messages.

linedefined --- The line of the source where the function was defined.

what --- What this function is. Options are "Lua" if foo is a regular Lua function, "C" if it is a C function, or "main" if it is the main part of a Lua chunk.

name --- A reasonable name for the function.

namewhat --- What the previous field means. This field may be "global", "local", "method", "field", or "" (the empty string). The empty string means that Lua did not find a name for the function.

nups --- Number of upvalues of that function.

func --- The function itself; see later.

hooks functions
debug库的中的sethook函数,允许我们注册一个在我们程序特定事件时调用的函数,这些特定事件是:
每一次调用函数的时候的事件;
每一次从函数中返回的事件;
lua开始执行一个新行的事件;
指定数量的指令执行后的事件;
lua调用hooks的时候,会传一个字符串,用来描述这个事件的:"call", "return", "line", or "count".,此外对于行事件,还会传一个行号的信息,一个简单的例子:

    function trace (event, line)
      local s = debug.getinfo(2).short_src
      print(s .. ":" .. line)
    end
   
    debug.sethook(trace, "l")   -- l
sethook 在使用的时候,需要传入三个参数,第一个是回掉的函数,第二个是用来镜像到相应事件的一个简单的描述,call->"c", return->"r", line->"l",指定数量的指令执行后的事件只需要在第三个参数中传入一个数字就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值