debug.getstack
Gets one value or all values from a Luau stack frame.
Syntax
debug.getstack(level: number, index?: number) -> any | {any}
debug.getstack(thread: thread, level: number, index?: number) -> any | {any}Parameters
| Parameter | Type | Description |
|---|---|---|
level | number | The stack level (1 = current function) |
index | number? | The stack slot index. Omit it to return every value at the level |
thread | thread? | Optional thread to inspect before level |
Returns
| Type | Description |
|---|---|
any or {any} | The selected value, or an array of every value when index is omitted |
Description
debug.getstack retrieves values from a Luau call frame. It throws if level refers to a C closure, including level 0.
Example
local marker = "caller value"
local function inspectCaller()
local callerStack = debug.getstack(2)
for index, value in ipairs(callerStack) do
print(index, value)
assert(debug.getstack(2, index) == value)
end
end
inspectCaller()Stack Levels
| Level | Meaning |
|---|---|
| 1 | Current function |
| 2 | Calling function |
| 3 | Caller of the caller |
| ... | And so on |
Notes
- Stack indices correspond to local variable slots
- The exact index depends on the function's compiled bytecode
- Values can include locals, parameters, functions, and temporary stack slots
- Stack layout is compiler-dependent; do not assume an index without inspecting the frame
Related Functions
debug.setstack- Modify a stack value