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

ParameterTypeDescription
levelnumberThe stack level (1 = current function)
indexnumber?The stack slot index. Omit it to return every value at the level
threadthread?Optional thread to inspect before level

Returns

TypeDescription
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

LevelMeaning
1Current function
2Calling function
3Caller 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