debug_print_backtrace
debug_print_backtrace(int $options = 0, int $limit = 0): void
Prints the current call stack directly (innermost frame first) - essentially debug_backtrace() rendered as #0.. text; supports DEBUG_BACKTRACE_IGNORE_ARGS and a frame limit.
C C23
#include <stdio.h>
// C has no backtrace facility: this trace is hand-maintained with __func__
static const char *trace[8];
static int depth = 0;
static void print_trace(void) {
for (int i = depth - 1; i >= 0; i--)
printf("#%d %s()\n", depth - 1 - i, trace[i]);
}
static void c(void) {
trace[depth++] = __func__;
print_trace();
depth--;
}
static void b(void) { trace[depth++] = __func__; c(); depth--; }
static void a(void) { trace[depth++] = __func__; b(); depth--; }
int main(void) {
a();
return 0;
}
#0 c() #1 b() #2 a()
C++ C++26
#include <iostream>
#include <string>
#include <vector>
// C++ has no portable stack walking: this trace is hand-maintained per call
static std::vector<std::string> g_trace;
static void c() {
g_trace.push_back("c");
for (size_t i = 0; i < g_trace.size(); i++)
std::cout << "#" << i << " " << g_trace[g_trace.size() - 1 - i] << "\n";
g_trace.pop_back();
}
static void b() { g_trace.push_back("b"); c(); g_trace.pop_back(); }
static void a() { g_trace.push_back("a"); b(); g_trace.pop_back(); }
int main() {
a();
return 0;
}
#0 c #1 b #2 a
C# v11.0.100
// C#: System.Diagnostics.StackTrace captures frames; keep only this program's
TraceDemo.a();
static class TraceDemo
{
public static void c()
{
var st = new System.Diagnostics.StackTrace(true);
foreach (var f in st.GetFrames())
{
if (f.GetMethod()?.DeclaringType?.Name == "TraceDemo")
Console.WriteLine(f.GetMethod()?.Name);
}
}
public static void b() { c(); }
public static void a() { b(); }
}
c b a
Go v1.27.4
package main
import (
"fmt"
"runtime"
"strings"
)
func c() {
pcs := make([]uintptr, 16)
n := runtime.Callers(1, pcs)
frames := runtime.CallersFrames(pcs[:n])
for {
f, more := frames.Next()
name := strings.TrimPrefix(f.Function, "main.")
if !strings.Contains(name, ".") && name != "main" { // keep only our own frames
fmt.Println(name)
}
if !more {
break
}
}
}
func b() { c() }
func a() { b() }
func main() { a() }
c b a
Java v27.0.1
public class Main {
static void c() {
// Java: Thread.getStackTrace() returns frames; keep only our own
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
if (e.getClassName().equals("Main") && !e.getMethodName().equals("main")) {
System.out.println(e.getMethodName());
}
}
}
static void b() { c(); }
static void a() { b(); }
public static void main(String[] args) { a(); }
}
c b a
JavaScript v26.13.1
// JS: no print-trace builtin; Error().stack is the closest (normalized to names)
function c() {
const names = new Error("trace").stack
.split("\n")
.map((l) => l.match(/at (c|b|a) \(/)?.[1])
.filter(Boolean);
console.log(names.join("\n"));
}
function b() { c(); }
function a() { b(); }
a();
c b a
PHP v8.6.0
function c(): void {
ob_start();
debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); // prints the trace directly
$raw = ob_get_clean();
// strip environment-specific file paths and line numbers, keep the shape
echo preg_replace('/^#(\d+) [^(]*\(\d+\): /m', '#$1 ', $raw);
}
function b(): void { c(); }
function a(): void { b(); }
a();
#0 c() #1 b() #2 a()
Python v3.15.2
# Python: traceback.extract_stack() returns frames; we print just the names
import traceback
def c():
for frame in reversed(traceback.extract_stack()):
print(frame.name)
def b(): c()
def a(): b()
a()
c b a <module>
Rust v1.99.1
use std::backtrace::Backtrace;
#[inline(never)]
fn c() {
// std::backtrace is the closest; keep only our own function symbols
let bt = Backtrace::force_capture();
for line in bt.to_string().lines() {
let t = line.trim_start();
let Some(rest) = t.strip_prefix(|ch: char| ch.is_ascii_digit()) else { continue };
let Some(symbol) = rest.trim_start_matches([':', ' ']).split(' ').next() else { continue };
if let Some(name) = symbol.strip_prefix("main::") {
if name.chars().next().is_some_and(|ch| ch.is_ascii_lowercase()) {
println!("{name}");
}
}
}
}
#[inline(never)]
fn b() { c(); std::hint::black_box(0u8); }
#[inline(never)]
fn a() { b(); std::hint::black_box(0u8); }
fn main() { a(); }
c b a
TypeScript v26.13.1
// TS: same as JS - stack from Error, normalized to function names
function c(): void {
const names: string[] = new Error("trace").stack
?.split("\n")
.map((l: string) => l.match(/at (c|b|a) \(/)?.[1] ?? "")
.filter((s: string) => s !== "") ?? [];
console.log(names.join("\n"));
}
function b(): void { c(); }
function a(): void { b(); }
a();
c b a
Language differences
C: no language-level backtrace; POSIX backtrace() only yields raw addresses and the symbolication format differs per platform (the demo hand-maintains a trace with __func__).
C++: <stacktrace> (C++23) is still incomplete in mainstream implementations; the portable route is manual or a library (the demo follows the C approach).
C#: StackTrace.GetFrames() returns frame objects you can filter - better for programmatic use than a text trace.
Go: runtime.Callers + CallersFrames give you frames (including runtime frames that you must filter out).
Java: Thread.getStackTrace() returns StackTraceElement[] with class, method and line number.
JavaScript: no print-style API - Error().stack is a string you parse yourself (the demo extracts function names with a regex).
PHP: print-style - it directly emits "#0 func() called at [file:line]" text and returns nothing; use debug_backtrace() to get the data as an array (see that entry); limit caps the frames; the demo normalizes paths and line numbers so the output stays environment-independent.
Python: traceback.extract_stack() returns the frame list (the demo prints only function names; the print-style counterpart is traceback.print_stack()).
Rust: std::backtrace can capture a trace, but symbolication is platform-dependent; in release builds tail-call merging must be prevented (the demo uses black_box).
TypeScript: same as JavaScript.