Repository navigation
Closed
Labels
debug-infoDebug information of binary generated by Zig is not as expected.enhancementSolving this issue will likely involve adding new logic or components to the codebase.
Milestone
Description
Problem: for TigerBetele, we want our release binary to be relatively small and self-contained, but we also want to see at least a minimal backtrace on assert faliures, overflow errors, indexing out of bounds and such.
As far as I am aware:
- No combination of Zig build flags gets us the desired result: either a binary is huge, or the stacktrace is absent
- This should be theoretically possible, as Rust does that
Specifically (non-expert, might be talking nonsense here), I think including frame pointers, and debug symbols (but not the entire of debug info) should give both a small executable, and a reasonable stack trace.
Here's a reproducible log of my experiments:
This is what I tried:
- Baseline, just
ReleaseSafe: huge binary because of debuginfo, good backtrace module.omit_frame_pointer = false; module.strip = false;: small file, but no backtrace at alladdObjCopystep with.strip = .debug: small file, but bad backtrace:
thread 33065 panic: integer overflow
Unwind information for `exe:0x10318a0` was not available, trace may be incomplete
???:?:?: 0x100c274 in ??? (exe)
???:?:?: 0x100bbc8 in ??? (exe)
???:?:?: 0x100bbb8 in ??? (exe)
???:?:?: 0x100bb93 in ??? (exe)
???:?:?: 0x100ba41 in ??? (exe)
In comparison, here's what Rust gives with strip=debuginfo:
thread 'main' panicked at ./main.rs:3:5:
attempt to multiply with overflow
stack backtrace:
0: rust_begin_unwind
1: core::panicking::panic_fmt
2: core::panicking::panic
3: main::foo
4: main::bar
5: main::main
I think 3) is what I want big-picture wise, but a few details prevent it from being actually useful:
- having names, and not just addresses, would be great.
- it would be great to have line number information for the root failure. I think we can get that for
asserts, by providing our own assert using@src(), but that's also important for things like arithmertic overflows - The build.zig incantaiton with separate ObjCopy invocation to get the desired behavior is gnarly. I'd love to just
module.strip = .strip_debuginfo_keep_symbols. See the repo linked above for build.zig.
Reacted by leafi, Will Manning, zfl9, Max Milton and Joran Dirk Greef
Reacted by Joran Dirk Greef, Will Manning, clo ver, djg, Chaitanya B Bhandari and Max Milton
Metadata
Metadata
Assignees
Labels
debug-infoDebug information of binary generated by Zig is not as expected.enhancementSolving this issue will likely involve adding new logic or components to the codebase.
Activity
xdBronch commentedon Jan 11, 2024
vaguely related #18387
possible solution could close both issues?
nektro commentedon Jan 11, 2024
perhaps https://sourceware.org/gdb/current/onlinedocs/gdb.html/Separate-Debug-Files.html could also be used?
matklad commentedon Jan 11, 2024
For TigerBeetle it’s important to ship only a single binary. I think what could work is printing only addresses “in the field”, and then correlating that back to symbols offline, but this is a much worse developer experience than just having a backtraces.
frmdstryr commentedon Jan 12, 2024
matklad commentedon Jan 12, 2024
matklad commentedon Jan 12, 2024
I would maybe highlight this part a bit more: in terms of user-visible features, that's just a part of the overall "crash reports are useful" package.
Implementation wise, this is mostly unrelated to debug info stripping and requires passing
@src()to panic messages, an orthogonal feature.frmdstryr commentedon Jan 12, 2024
Wow adding
.strip=falsemakes it much better... thanks for the tip!frmdstryr commentedon Jan 12, 2024
I see now there's also an
unwind_tablesanderror_tracingoptions but no docs/comments on what it either does. Maybe that toggles with optimize too?xdBronch commentedon Jan 12, 2024
error_tracing is off by default on all modes but debug as of #18160 so not really relevant in this case
leroycep commentedon Nov 17, 2024
Pre-emptive edit: Comparing the sizes between Zig's strip option and other methods of stripping an executable, Zig's can be a lot more aggressive. I would need to learn more about how Zig strips binaries before I could know if any debug info format would make a difference.
I've done a bit of research on this, and one possibility is using GNU's MiniDebugInfo format (at least on systems that use ELF, I'm not sure about other systems). It adds a
.gnu_debugdatasection that contains an LZMA compressed symbol table.I went ahead and patched the example from @matklad to add an
-Dstrip=mini_debuginfooption. Since Zig doesn't support MiniDebugInfo yet, I run the program from gdb to compare the backtraces.Here are the sessions for each of the
-Dstripoptions:`zig-0.13 build -Dstrip=none`
`zig-0.13 build -Dstrip=strip`
`zig-0.13 build -Dstrip=objcopy`
`zig-0.13 build -Dstrip=llvm_objcopy`
`zig-0.13 build -Dstrip=mini_debuginfo`
Comparison of the executable sizes:
Comparison of executable sizes using `bloaty`:
[LOAD #1 [R]]It looks like setting
exe.root_module.stripis cutting out a lot more from the binary than just runningstripon the finished executable. I would need to familiarize myself with that to say more.Edit: forget to include the changes I added to the repro
[PATCH] Add mini_debuginfo option to stacktrace repro
xdBronch commentedon Nov 17, 2024
the reason setting zigs strip option is so much smaller than stripping it afterwards is it causes parts of the default panic handler to not be compiled in
zig/lib/std/debug.zig
Line 552 in 3a6a8b8
zig/lib/std/debug.zig
Lines 190 to 193 in 3a6a8b8
leroycep commentedon Nov 17, 2024
Ah, okay. I guess that means that the comparison would be better done on a larger project, instead of on a tiny program only meant to demonstrate if the printing works or not.
leroycep commentedon Nov 17, 2024
Experimenting on a slightly larger project, I get
923.9 KBusingexe.root_module.strip = true, vs1.1 MBfor a binary containing MiniDebugInfo, vs5.5 MBfor a binary containing the full debug info.alexrp commentedon Nov 20, 2024
I think
std.debugshould be taught to read symbols from the binary's dynamic symbol table. Then users can build with-O <Release*> -fno-omit-frame-pointer -rdynamic(and-f(async-)unwind-tablesafter my upcoming PR) and things should Just Work.We might consider adding a convenience flag such as
-fstack-tracesthat enables all the right options to just get working stack traces regardless of build mode / stripping options.leroycep commentedon Nov 21, 2024
Alright, I've gotten simple backtraces working:
However it currently swaps out the DWARF debug info backend entirely, so symbol names are the best you'll get in any mode. It also doesn't support the dynamic symbol table yet, it only reads from the regular symbol table.
That code is living on the
elf-symbol-debuginfobranch in my Zig repo. I don't think this properly solves the problem at the moment.I think we want
std.debug.SelfInfoto try to use DWARF debug info, and fallback to reading ELF symbols. We can probably handle that by introducing something likeElfDebugInfo = union(enum) { dwarf: Dwarf, symbol_table: Elf }.2 remaining items
leroycep commentedon Nov 25, 2024
I'll have to take a look at #19650, but at the moment I'm looking at making
-fstrip=debuginfoa thingleroycep commentedon Dec 1, 2024
Adding a few comments after having done some work on the pull request:
I can confirm that
objcopy --strip-debugis the operation you want.-fno-omit-frame-pointeris unecessary, asobjcopy --strip-debugwill retain unwinding information (.eh_frameandeh_frame_hdr) by default:My idea for tackling this is replacing
-fstrip,-gdwarf32, and-gdwarf64with a single-fdebuginfooption:-fdebuginfo=none: don't emit any debugging information (equivalent to the current-fstrip)-fdebuginfo=symbols: emits symbol and unwind tables for lightweight backtraces (equivalent toobjcopy --strip-debug)-fdebuginfo=dwarf32: emits DWARF debugging information-fdebuginfo=dwarf64: emits DWARF debugging information using thedwarf64format, which allows debug information larger than 4 GiB.-fdebuginfo=code_view: Windows specific CodeView debugging info-gfrom clang and gcc, so maybe-gor--debugwould be a better flag?Although I'm not sure that would quite match what is going on:
-fdebuginfo=[dwarf32|dwarf64|code_view]adds additional debug markup to the LLVM IR,-fdebuginfo=noneomits the debug markup and passes a flag to strip the executable to the linker, and-fdebuginfo=symbolswould just omit the debug markup and not do anything special.Also, most of the time people will not care what the specific debugging format is, so having some aliases like
-fdebuginfo=fullwould make sense.Anyway, if
-fdebuginfois the way we go, I was thinking of the following defaults:-fdebuginfo=This would definitely make sense to do, especially if we want to change the default backtrace in
-fdebuginfo=symbolsfrom this:To something like this:
Getting the backtraces to work only requires changing the standard library, so I'm thinking about splitting
-fdebuginfo(or whatever it ends up being) into a separate PR, and focusing on the necessary changes to the standard library for the first PR.mlugg commentedon Oct 1, 2025
I would consider the main issue here to have been resolved by b64535e. You can now strip debug information from your binary with
objcopy, in which case stack traces will include the symbol names instead of just???.I agree that the Zig compiler should have the ability to strip debug info while still emitting symbols (something like #22591); I'll open a follow-up issue for that specific enhancement shortly.
Your suggestion of preserving line number information for the safety check failure is something I'm not so sure about; either we would have to inject source lines into the panic handler call, which bloats the machine code, or we would have to selectively emit DWARF so we have just the source locations we care about, which sounds quite tricky. if you'd like to open a follow-up issue proposing that then go ahead, but I'm not going to do so myself.
mlugg commentedon Oct 1, 2025
#25419 is the follow-up issue tracking a compiler enhancement which allows stripping debug info while still emitting symbols as normal.
matklad commentedon Oct 1, 2025
Huh, I was just trying to verify if this is fixed on latest main, to close the issuer, but I hit some internal compiler error (on aarch64-macos):
But I trust that b64535e solves it fully.
I won't be opening an issue for location on panic right away --- while abstractly it is useful, it's not clear just how much it would be useful, given that backtraces now work.
Thanks again @leroycep and @mlugg for tackling this!