Docs / Debug

Debug

Constants, upvalues, stack, protos, registry — under debug.* and as top-level globals.

Library#05 of 23 Functions14
debug.getconstant(f: function | number, idx: number) → any
Constant at index idx from the function's constant table.
aliasesgetconstant (global)
lua
local function f() return "hello" end
print(debug.getconstant(f, 1))  --> hello
debug.getconstants(f: function | number) → {any}
All constants in the function's proto.
aliasesgetconstants (global)
lua
for i, k in ipairs(debug.getconstants(f)) do
    print(i, k)
end
debug.setconstant(f, idx: number, value: any)
Overwrite a constant. Type must match the original slot's type.
aliasessetconstant (global)
lua
debug.setconstant(f, 1, "patched")
print(f())  --> patched
debug.getproto(f, idx: number, active: boolean?) → function | {function}
Get a child proto by index. If active=true, returns all live closures created from it.
aliasesgetproto (global)
lua
local inner = debug.getproto(outer, 1)
inner()
debug.getprotos(f) → {function}
All child protos of the function.
aliasesgetprotos (global)
lua
for i, p in ipairs(debug.getprotos(f)) do
    print(i, debug.getinfo(p).name)
end
debug.getupvalue(f, idx: number) → any
Read an upvalue by index (1-based).
aliasesgetupvalue (global)
lua
local x = 10
local get = function() return x end
print(debug.getupvalue(get, 1))  --> 10
debug.getupvalues(f) → {any}
All upvalues of a closure.
aliasesgetupvalues (global)
lua
for i, v in ipairs(debug.getupvalues(get)) do print(i, v) end
debug.setupvalue(f, idx: number, value: any)
Overwrite an upvalue.
aliasessetupvalue (global)
lua
debug.setupvalue(get, 1, 999)
print(get())  --> 999
debug.getstack(level: number, idx: number?) → any | {any}
Read a stack value at frame level. Without idx returns the whole stack.
aliasesgetstack (global)
lua
local function f()
    local a, b = 1, 2
    print(debug.getstack(1, 1))  --> 1
end
f()
debug.setstack(level: number, idx: number, value: any)
Write a stack slot.
aliasessetstack (global)
lua
local function f()
    local x = 1
    debug.setstack(1, 1, 42)
    print(x)  --> 42
end
f()
debug.getinfo(f | level: number, what: string?) → table
Debug info about a function or stack frame — source, currentline, nups, numparams, etc.
aliasesgetinfo (global)
lua
local info = debug.getinfo(1)
print(info.source, info.currentline)
debug.getregistry() → table
Direct access to Lua's registry.
aliasesgetregistry (global)
lua
local reg = debug.getregistry()
print(reg["_LOADED"])
debug.getmetatable(value: any) → table?
Raw metatable read — ignores __metatable.
lua
local mt = debug.getmetatable(game)
print(rawget(mt, "__namecall"))
debug.setmetatable(value: any, mt: table) → any
Raw metatable write — ignores __metatable.
lua
debug.setmetatable(myUdata, { __index = function() return 0 end })