Lua Academy · Lesson

The debug Library in Depth

Use debug.traceback, getinfo, getlocal, getupvalue for introspection.

Lesson 1 of 413 steps

The debug Library in Depth is a free Lua Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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 20

Modifying 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)  -- 100

Stack 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
end

debug.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
end

Caution

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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “The debug Library in Depth” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “The debug Library in Depth”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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

  1. The debug Library in Depth
  2. Remote Debugging with MobDebug
  3. Profiling with LuaProfiler
  4. Code Coverage and Test Instrumentation
← Back to Lua Academy