0Pricing
Lua Academy · 课时

深入了解 debug 库

使用 debug.traceback、getinfo、getlocal 和 getupvalue 进行自省。

深入了解 debug 库 是 CoddyKit 上的免费 Lua Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Lua Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Lua Academy 课程共包含 4 节课。

debug 库概览

debug 库是 Lua 的运行时内省工具集。它可以检查和修改任何函数、协程或栈帧的运行时状态。

深入了解 debug.getinfo

debug.getinfo(f, what) 接受以下 what 标志:"n"(名称)、"S"(源代码)、"l"(当前行)、"u"(上值)和 "f"(函数引用)。

local function foo()
  local info = debug.getinfo(1, "nSlu")
  print(info.name)        -- foo
  print(info.short_src)   -- source file
  print(info.currentline) -- line number
  print(info.nups)        -- number of upvalues
end
foo()

检查局部变量

debug.getlocal(level, n) 返回给定栈级别上第 n 个局部变量的名称和值。级别 1 表示当前函数。

local function example()
  local x, y = 10, 20
  local i = 1
  while true do
    local name, val = debug.getlocal(1, i)
    if not name then break end
    print(name, val)
    i = i + 1
  end
end
example()  -- x 10 / y 20

修改局部变量

debug.setlocal(level, n, value) 会在运行时更改局部变量的值,这对调试和测试非常有用。

检查上值

debug.getupvalue(fn, n) 返回函数第 n 个上值的名称和值。不断增加 n 进行迭代,直到返回 nil。

local count = 0
local function inc() count = count + 1 end
print(debug.getupvalue(inc, 1))  -- count  0
debug.setupvalue(inc, 1, 99)
inc()
print(count)  -- 100

遍历栈

不断增加级别,直到 debug.getinfo 返回 nil,即可遍历完整的调用栈。

local function printStackTrace()
  local level = 1
  while true do
    local info = debug.getinfo(level, "Sln")
    if not info then break end
    print(level, info.short_src, info.currentline, info.name)
    level = level + 1
  end
end

debug.traceback

debug.traceback(msg, level) 返回格式化的栈追踪字符串。这是生产环境错误处理最实用的调试函数。

检查协程

将协程作为 debug.getinfo 的第一个参数传入,即可检查其栈。

local co = coroutine.create(function()
  print(debug.getinfo(1, "l").currentline)
end)
coroutine.resume(co)

使用 debug.sethook 进行追踪

设置一个会打印函数名称的 call 钩子,即可追踪程序中的所有函数调用。

debug.sethook(function(event)
  if event == "call" then
    local info = debug.getinfo(2, "Sn")
    print("CALL:", info.name or "?")
  end
end, "c")

可移植的断言

使用 debug.getinfo(2, "Sl") 在断言错误中包含文件和行信息。

local function assert2(cond, msg)
  if not cond then
    local info = debug.getinfo(2, "Sl")
    error(("[%s:%d] %s"):format(info.short_src, info.currentline, msg), 2)
  end
end

注意事项

调试库可能破坏封装性,显著降低程序速度(尤其是 debug.sethook),并且并非在所有环境中都可用。请仅在开发和测试期间使用。

debug 库问题

debug.getupvalue(fn, 1) 会返回什么?

回顾:深入了解 debug 库

调试库提供栈检查(getinfo)、局部变量检查和修改(getlocal/setlocal)、上值访问(getupvalue/setupvalue)以及基于钩子的追踪(sethook)。请仅在开发期间负责任地使用。

常见问题解答

「深入了解 debug 库」课时是免费的吗?

是的 — 「深入了解 debug 库」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Lua Academy 课程的其余内容,请升级到 CoddyKit PRO。 Lua Academy 课程共包含 4 节课。

「深入了解 debug 库」这节课中我会学到什么?

使用 debug.traceback、getinfo、getlocal 和 getupvalue 进行自省。 你通过在浏览器中直接运行的动手代码来练习 Lua Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Lua Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Lua Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「深入了解 debug 库」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Lua Academy 课中编写并运行代码吗?

能。每节 Lua Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 深入了解 debug 库
  2. 使用 MobDebug 进行远程调试
  3. 使用 LuaProfiler 进行性能分析
  4. 代码覆盖率和测试检测
← 返回 Lua Academy