zig icon indicating copy to clipboard operation
zig copied to clipboard

std: add std.options.debug_stacktrace_kind

Open nektro opened this issue 1 year ago • 3 comments

given the following test file we can run it with the various option values.

const std = @import("std");

pub const std_options = std.Options{
    .debug_stacktrace_kind = .full,
};

pub fn main() !void {
    foo();
}

fn foo() void {
    bar();
}

fn bar() void {
    qux();
}

fn qux() void {
    lorem();
}

fn lorem() void {
    ipsum();
}

fn ipsum() void {
    @panic("oops!");
}
thread 182792 panic: oops!
/home/meghan/src/zig/test4.zig:28:5: 0x10762c0 in ipsum (test4)
    @panic("oops!");
    ^
/home/meghan/src/zig/test4.zig:24:10: 0x10687e8 in lorem (test4)
    ipsum();
         ^
/home/meghan/src/zig/test4.zig:20:10: 0x1038158 in qux (test4)
    lorem();
         ^
/home/meghan/src/zig/test4.zig:16:8: 0x1036098 in bar (test4)
    qux();
       ^
/home/meghan/src/zig/test4.zig:12:8: 0x1033938 in foo (test4)
    bar();
       ^
/home/meghan/src/zig/test4.zig:8:8: 0x1033928 in main (test4)
    foo();
       ^
/home/meghan/src/zig/lib/std/start.zig:511:37: 0x1033904 in posixCallMainAndExit (test4)
            const result = root.main() catch |err| {
                                    ^
/home/meghan/src/zig/lib/std/start.zig:253:5: 0x1033441 in _start (test4)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)
Aborted (core dumped)
thread 182911 panic: oops!
/home/meghan/src/zig/test4.zig:28:5: 0x1073880 in ipsum (test4)
/home/meghan/src/zig/test4.zig:24:10: 0x10660e8 in lorem (test4)
/home/meghan/src/zig/test4.zig:20:10: 0x1037a78 in qux (test4)
/home/meghan/src/zig/test4.zig:16:8: 0x10359b8 in bar (test4)
/home/meghan/src/zig/test4.zig:12:8: 0x1033258 in foo (test4)
/home/meghan/src/zig/test4.zig:8:8: 0x1033248 in main (test4)
/home/meghan/src/zig/lib/std/start.zig:511:37: 0x1033224 in posixCallMainAndExit (test4)
/home/meghan/src/zig/lib/std/start.zig:253:5: 0x1032d61 in _start (test4)
???:?:?: 0x0 in ??? (???)
Aborted (core dumped)
thread 183137 panic: oops!
Aborted (core dumped)

beyond stylistic desires, the various options also have performance and binary size ramifications. .none exits faster not having to inspect the program's debug info and results in a smaller file. however since this does not require the file to be stripped to get the effect, using an external debugger remains totally possible.

-rwxr-xr-x 1 meghan users 2227856 Apr 14 02:01 ./test4-full
-rwxr-xr-x 1 meghan users 2208816 Apr 14 02:03 ./test4-slim
-rwxr-xr-x 1 meghan users 1921440 Apr 14 02:04 ./test4-none

nektro avatar Apr 14 '24 09:04 nektro

a version that prints out stack addresses without needing any debug info (so no file, line, or symbol name info) could be useful too

Khitiara avatar May 08 '24 20:05 Khitiara

afaik that's already what happens if you use .full and strip. it will print out the addresses with the line info replaced with ????

nektro avatar May 08 '24 20:05 nektro

if you strip debug info then dumpStackTrace prints out a warning and returns early, and writeStackTrace returns error.MissingDebugInfo, though printSourceAtAddress does work for what you describe

Khitiara avatar May 08 '24 21:05 Khitiara