How to include stack trace when doing print debugging

While using print debugging, is it also possible to print the stack trace?

This is to help see the path the code took until the print.

You can use

std.debug.dumpCurrentStackTrace(null);

e.g.:

const std = @import("std");

fn test1() void {
    test2();
}

fn test2() void {
    test3();
}

fn test3() void {
    std.debug.dumpCurrentStackTrace(null);
}

pub fn main() void {
    test1();
}
4 Likes

You can also use the @errorReturnTrace() builtin:

const std = @import("std");

pub fn main() !void {
    std.testing.expect(false) catch {
        if (@errorReturnTrace()) |trace| {
            std.debug.dumpStackTrace(trace.*);
        }
    };
}
4 Likes