The debug Library in Depth
Use debug.traceback, getinfo, getlocal, getupvalue for introspection.
The debug Library in Depth is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
debug Library Overview
The debug library is Lua's introspection toolkit. It lets you inspect and modify the runtime state of any function, coroutine, or stack frame.
debug.getinfo Deep Dive
debug.getinfo(f, what) accepts what-flags: "n" (name), "S" (source), "l" (current line), "u" (upvalues), "f" (function ref).
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()Inspecting Locals
debug.getlocal(level, n) returns the name and value of the nth local at a given stack level. Level 1 = current function.
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 20Modifying Locals
debug.setlocal(level, n, value) changes the value of a local variable at runtime — powerful for debugging and testing.
Upvalue Inspection
debug.getupvalue(fn, n) returns the name and value of the nth upvalue of a function. Iterate with increasing n until nil is returned.
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) -- 100Stack Walk
Walk the entire call stack by incrementing the level until debug.getinfo returns 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
enddebug.traceback
debug.traceback(msg, level) returns a formatted stack trace string. The most practical debug function for production error handling.
Coroutine Inspection
Pass a coroutine as the first argument to debug.getinfo to inspect its stack.
local co = coroutine.create(function()
print(debug.getinfo(1, "l").currentline)
end)
coroutine.resume(co)debug.sethook for Tracing
Trace all function calls in a program by setting a call hook that prints function names.
debug.sethook(function(event)
if event == "call" then
local info = debug.getinfo(2, "Sn")
print("CALL:", info.name or "?")
end
end, "c")Portable Assertions
Use debug.getinfo(2, "Sl") to include file and line information in assertion errors.
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
endCaution
The debug library can violate encapsulation, slow programs significantly (especially debug.sethook), and is not available in all environments. Use only in development and testing.
debug Library Question
What does debug.getupvalue(fn, 1) return?
Recap: debug Library in Depth
The debug library provides stack inspection (getinfo), local inspection/modification (getlocal/setlocal), upvalue access (getupvalue/setupvalue), and hook-based tracing (sethook). Use responsibly in development only.
Frequently asked questions
Is the “The debug Library in Depth” lesson free?
Yes — the full text of “The debug Library in Depth” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “The debug Library in Depth”?
Use debug.traceback, getinfo, getlocal, getupvalue for introspection. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The debug Library in Depth” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The debug Library in Depth
- Remote Debugging with MobDebug
- Profiling with LuaProfiler
- Code Coverage and Test Instrumentation