debug_backtrace
debug_backtrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT, int $limit = 0): array
Returns the call-stack frames as an array (each frame carries function/line/file/class); IGNORE_ARGS drops args to save memory, limit caps depth.
C C23
#include <execinfo.h>
#include <stdio.h>
static void inner(void) {
void *frames[8];
/* POSIX backtrace(3): addresses only; symbol names need backtrace_symbols.
Frame count depends on call structure - we print a boolean instead */
int n = backtrace(frames, 8);
printf("%d\n", n >= 2);
}
static void outer(void) { inner(); }
int main(void) {
outer();
return 0;
}
1
C++ C++26
#include <iostream>
#include <string>
#include <vector>
// C++ lacks a std call-stack API (<stacktrace> needs link extras on GCC):
// record the chain by hand - names stay stable, no paths/line numbers
static std::vector<std::string> g_stack;
static void inner() {
std::cout << "top: " << g_stack.back() << "\n";
}
static void outer() {
g_stack.push_back("outer");
inner();
}
int main() {
g_stack.push_back("main");
outer();
return 0;
}
top: outer
C# v11.0.100
// Environment.StackTrace is a ready-made string; NoInlining keeps the frame
// names visible (release JIT would inline small methods away)
using System.Runtime.CompilerServices;
class Program
{
[MethodImpl(MethodImplOptions.NoInlining)]
static string Probe() => Environment.StackTrace;
[MethodImpl(MethodImplOptions.NoInlining)]
static void Outer() => Console.WriteLine(
Probe().Contains("Probe") && Probe().Contains("Outer") ? "chain ok" : "missing");
static void Main() => Outer();
}
chain ok
Go v1.27.4
package main
import (
"fmt"
"runtime"
"strings"
)
//go:noinline
func probe() string {
pcs := make([]uintptr, 8)
n := runtime.Callers(1, pcs)
names := []string{}
frames := runtime.CallersFrames(pcs[:n])
for {
frame, more := frames.Next()
names = append(names, frame.Function)
if !more {
break
}
}
chain := strings.Join(names, " ")
if strings.Contains(chain, "probe") && strings.Contains(chain, "outer") {
return "chain ok"
}
return "missing"
}
//go:noinline
func outer() string { return probe() }
func main() {
fmt.Println(outer())
}
chain ok
Java v27.0.1
public class Main {
static int pad;
// Body > 325 bytecodes keeps HotSpot from inlining, so frame names survive
static boolean probe() {
int s = 0;
for (int i = 0; i < 400; i++) {
s += i * (i % 7);
}
pad = s;
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
if (e.getMethodName().equals("probe")) {
return true;
}
}
return false;
}
static boolean outer() {
boolean ok = probe();
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
if (e.getMethodName().equals("outer")) {
return ok;
}
}
return false;
}
public static void main(String[] args) {
System.out.println("chain ok: " + outer());
}
}
chain ok: true
JavaScript v26.13.1
// new Error().stack is non-standard but a de-facto standard across engines;
// we only probe frame names - paths/line numbers vary between engines
function probe() {
const stack = new Error().stack || "";
return stack.includes("probe") && stack.includes("outer") ? "chain ok" : "missing";
}
function outer() {
return probe();
}
console.log(outer());
chain ok
PHP v8.6.0
// frame[0] is the function calling debug_backtrace itself; IGNORE_ARGS saves memory
function inner(): array {
return array_column(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2), 'function');
}
function outer(): array {
return inner();
}
echo json_encode(outer()), "\n";
// Fixed depth: frames[1] is the immediate caller of probe
function probe(): string {
$frames = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
return $frames[1]['function'];
}
function middle(): string {
return probe();
}
echo middle();
["inner","outer"] middle
Python v3.15.2
import traceback
def probe():
# Frame names only - deterministic, no paths/line numbers
names = [f.name for f in traceback.extract_stack()]
return "chain ok" if "probe" in names and "outer" in names else "missing"
def outer():
return probe()
print(outer())
chain ok
Rust v1.99.1
fn main() {
// Rust 1.65+: std::backtrace::Backtrace; force_capture ignores RUST_BACKTRACE.
// Frame text carries paths/addresses, so we keep only deterministic facts
let bt = std::backtrace::Backtrace::force_capture();
println!("non-empty: {}", !bt.to_string().is_empty());
println!("status: {:?}", bt.status());
}
non-empty: true status: Captured
TypeScript v26.13.1
// Same non-standard Error().stack as JS; typed wrapper keeps it lint-friendly
function probe(): string {
const stack = new Error().stack ?? "";
return stack.includes("probe") && stack.includes("outer") ? "chain ok" : "missing";
}
function outer(): string {
return probe();
}
console.log(outer());
chain ok
Language differences
C: backtrace(3) (a glibc extension, not in POSIX) - addresses only; symbol names need a second pass through backtrace_symbols; under -O2 small functions can be inlined and frames vanish.
C++: only since C++23 with std::stacktrace, and GCC needs an extra -lstdc++exp link; the cross-platform reality is hand-recorded chains or third-party libs.
C#: Environment.StackTrace is a ready-made string (with paths/line numbers) - release JIT inlines small methods and erases frame names, so [MethodImpl(MethodImplOptions.NoInlining)] is required.
Go: runtime.Callers gives PC arrays, runtime.CallersFrames symbolizes them; compiler inlining swallows frames - //go:noinline is the standard defense.
Java: Thread.currentThread().getStackTrace() (or StackWalker on Java 9+) - HotSpot inlining really erases frames; method bodies over 325 bytecodes (default inlining threshold) are safe; stack capture via exceptions (new Exception().getStackTrace()) is affected the same way.
JavaScript: new Error().stack is non-standard but a de-facto engine standard; the text format differs per engine (V8/Firefox/JSC each have one), good for contains-probing but not parsing.
PHP: debug_backtrace returns structured frames (function/line/file/class/object/args/type) - frame [0] is the function calling debug_backtrace itself, [1] is its caller; DEBUG_BACKTRACE_IGNORE_ARGS saves memory (args can be huge), limit caps depth; determinism strategy here: print fixed-depth function names only, never line numbers/paths.
Python: traceback.extract_stack() returns structured FrameSummary objects (name/line/filename/lineno) - interpreter frames are never JIT-erased (CPython has no method inlining), frame names are always real.
Rust: std std::backtrace::Backtrace since 1.65 - capture() respects RUST_BACKTRACE, force_capture() does not; under -O tail calls eliminate frames (outer was eliminated in testing), and frame text carries paths so pick facts carefully.
TypeScript: same as JavaScript (Error().stack de-facto standard); the type layer carries no stack info (erased at compile time).