Stacktrace
Based on game build 714014 | Last updated: 2026-03-10
Overview
stacktrace.lua is a utility module that provides functions for capturing, formatting, and reporting Lua stack traces during runtime. It is used primarily for debugging—capturing execution context when errors occur or for manual inspection. The module exposes functions that wrap Lua’s debug library to produce human-readable tracebacks with source location, function names, line ranges, and local variable values.
Usage example
local StackTrace = require("stacktrace")
-- Capture and print a stack trace manually
print(StackTrace())
-- Or override the default traceback handler
debug.traceback = StackTrace
Dependencies & tags
Components used: None identified
Tags: None identified
Properties
No public properties
Main functions
SaveToString(v)
- Description: Safely converts a Lua value to a string representation using
xpcallto prevent errors fromtostring()or metamethods. Truncates output exceeding 1024 characters and appends[**truncated**]. - Parameters:
v(any) - The value to convert. - Returns:
string- A safe string representation of the value, potentially truncated.
getformatinfo(info)
- Description: Formats a
debug.getinfotable into a standardized traceback line, including source path (filtered), line number (if available), function type, name, and line range. - Parameters:
info(table|nil) - A table returned bydebug.getinfo(). Ifnil, returns"**error**". - Returns:
string- A formatted traceback line string.
getdebuglocals(res, level)
- Description: Captures local variables for a given stack level and appends formatted entries to the provided
resarray. Handles special formatting forself, function references, and tables withIsValid()methods. - Parameters:
res(table) - Array to append formatted local variable strings to.level(number) - Stack level (passed todebug.getlocal).
- Returns:
string- Concatenated result of all local variable entries (unused in practice sinceresis mutated in-place).
getdebugstack(res, start, top, bottom)
- Description: Iterates over stack frames starting at
start, formats each usinggetformatinfo, and records locals viagetdebuglocals. Uses heuristics to limit depth. - Parameters:
res(table) - Array to append formatted stack entries to.start(number?) - Starting stack level offset; defaults to1.top(number?) - Number of top frames to capture; defaults to12.bottom(number?) - Number of bottom frames to capture; defaults to10.
- Returns:
table- The mutatedresarray containing formatted traceback lines.
DoStackTrace(err)
- Description: Generates a full stack trace prefixed with an optional error message. Parses multi-line error strings and formats the traceback consistently.
- Parameters:
err(string?) - Optional error message string to prefix the traceback. - Returns:
string- Complete stack trace as a multi-line string.
StackTrace(err)
- Description: A robust wrapper around
DoStackTraceusingxpcallto prevent failures during trace generation (e.g., due to panics or broken metamethods). - Parameters:
err(any) - Error message or value to include in the traceback. - Returns:
string- A safe traceback string, or a fallback string if the main function fails.
StackTraceToLog()
- Description: Generates a stack trace and prints it to the console via
print(). - Parameters: None.
- Returns: Nothing.
Events & listeners
Not applicable