Reducing debug info for stack traces, while preserving for gdb

Witold Baryluk witold.baryluk at gmail.com
Wed Dec 14 22:23:49 UTC 2022


Hi,

I am trying to split debug symbols out of final binary, to reduce 
its size. But I would like to preserve at least line numbers in 
the stacktraces from `Exception`s.

Linux, amd64. gdc 12.2.0 on Debian testing.

Binary is compiled using `-O3 -g -g3` (and possibly -gz).

I then use `objcopy` to split debug info from final ELF binary 
file into .exe and .debug files.

I use `objcopy --only-keep-debug --compress-debug-sections=zlib 
binary binary.debug` to produce .debug file.

Then I use normally `objcopy --strip-debug binary binary.exe` 
(possibly with --compress-debug-sections=zlib) to produce binary.

This makes it possibly to use in gdb without issues (using `gdb 
-s binary.debug -e binary` for example, or by utilizing 
`--add-gnu-debuglink=` option in `objcopy`).

But this causes stacktraces to miss line numbers (and columns). 
(function names are still there, as these are derived from symbol 
tables instead).

I tried selectively removing DWARF debug sections, but it looks 
that at least these are required:

`.debug_info`
`.debug_aranges`
`.debug_abbrev`
`.debug_line`
`.debug_str`
`.debug_line_str`
`.debug_rnglists`

So, I can only remove these:

`objcopy -R .debug_loc -R .debug_macro -R .debug_ranges -R 
.debug_loclists`

(`.debug_loc` and `.debug_ranges` are not even generated by gcc, 
so it does not matter probably).

The issue is, this saves me very little space. `.debug_macro` and 
`.debug_loclists` are rather small.

The bulk of information is in `.debug_info`. But I believe it 
contains way more information than is really needed to just 
produce line numbers.



I did inspect final binaries , and it is using DWARF version 5.

I also tried `-gas-loc-support` , but no change.

Using `-g1` makes stack traces work nicely, by making 
`.debug_info` smaller, but then debugging in `gdb` is very 
limited. One option would be to compile application twice, once 
with `-g1` and once with `-g3`. But I really do not think this is 
supported, or reliable, even if I enable deterministic builds.

In one article ( 
https://support.backtrace.io/hc/en-us/articles/360040105792-DWARF#RemovingDebugInformation )  I read that for C/C++ in GCC, it is enough to preserve only `.debug_frame` and `.debug_line` to get function, source filenames and line numbers. But that is not true for gdc and its stacktrace handler.


I did read about this gdb extension, which is interesting, 
https://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html , 
but I did not try, and it is probably only supported in gdb (i.e. 
addr2libe, Phobos, libunwind do not  support it).


Any ideas?



More information about the D.gnu mailing list