The Debug Library in Lua

王朝other·作者佚名  2006-05-20
窄屏简体版  字體: |||超大  

在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",指定数量的指令执行后的事件只需要在第三个参数中传入一个数字就可以了。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航