当前位置:网站首页>十八、一起学习Lua 调试(Debug)
十八、一起学习Lua 调试(Debug)
2022-08-10 11:47:00 【m0_66404702】
Lua 调试(Debug)
Lua 提供了 debug 库用于提供创建我们自定义调试器的功能。Lua 本身并未有内置的调试器,但很多开发者共享了他们的 Lua 调试器代码。

Lua 中 debug 库包含以下函数:


上表列出了我们常用的调试函数,接下来我们可以看些简单的例子:
实例
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 库的 traceback 和 getinfo 函数, getinfo 函数用于返回函数信息的表。
另一个实例
我们经常需要调试函数的内的局部变量。我们可以使用 getupvalue 函数来设置这些局部变量。实例如下:
实例
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在以上实例中,计数器在每次调用时都会自增1。实例中我们使用了 getupvalue 函数查看局部变量的当前状态。我们可以设置局部变量为新值。实例中,在设置前 n 的值为 2,使用 setupvalue 函数将其设置为 10。现在我们调用函数,执行后输出为 11 而不是 3。
调试类型
- 命令行调试
- 图形界面调试
命令行调试器有:RemDebug、clidebugger、ctrace、xdbLua、LuaInterface - Debugger、Rldb、ModDebug。
图形界调试器有:SciTE、Decoda、ZeroBrane Studio、akdebugger、luaedit。
边栏推荐
- LeetCode 138. 复制带随机指针的链表
- LeetCode 146. LRU 缓存
- 加密游戏:游戏的未来
- 三星计划2023年开始在越南生产半导体零部件
- 【集合】HashSet和ArrayList的查找Contains()时间复杂度
- 第六届”蓝帽杯“全国大学生网络安全技能大赛半决赛部分WriteUp
- Can CLIP also do segmentation tasks?The University of Göttingen proposed a model CLIPSeg that uses text and image prompts to perform three segmentation tasks at the same time, draining CLIP capabiliti
- Flutter气泡框实现
- LeetCode 86. Delimited Linked List
- 人脸考勤是选择人脸比对1:1还是人脸搜索1:N?
猜你喜欢

Does face attendance choose face comparison 1:1 or face search 1:N?

你是怎么知道数据库 Htap 能力强弱的?怎么能看出来

孩子自律性不够?猿辅导:计划表要注意“留白”给孩子更多掌控感

22年BATJ大厂必问面试题(复盘):JVM+微服务+多线程+锁+高并发

Behind IDC's No. 1 position, what kind of "video cloud" is Alibaba Cloud building?

A detailed explanation of implementation api embed

网络基础(第一节)

嘉为蓝鲸荣获工信部“数字技术融合创新应用解决方案”

Analysis of the implementation principle of UUID from the perspective of source code

Chapter9 : De Novo Molecular Design with Chemical Language Models
随机推荐
LeetCode 19. 删除链表的倒数第 N 个结点
camshift实现目标跟踪
常量及数据类型你还记得多少?
技术人必看!数据治理是什么?它对数据中台建设重要吗?
LeetCode 109. 有序链表转换二叉搜索树
LeetCode 86. Delimited Linked List
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
搜索--09
LeetCode 362. Design Hit Counter(计数器)
嘉为蓝鲸荣获工信部“数字技术融合创新应用解决方案”
阿里云贾朝辉:云XR平台支持彼真科技呈现国风科幻虚拟演唱会
Flutter气泡框实现
dedecms supports one-click import of Word content
The author of open source also has a life problem
因为找不到lombok而找不到符号log
An enhanced dynamic packet buffer management.论文核心部分
APP automation testing practice based on UiAutomator2+PageObject mode
浮动及其特点
Servlet---Solve the problem of Chinese garbled characters in post requests
bat脚本——提取多个文件夹到指定路径