Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate debug symbols from a C static library and later load it in GDB

I have a static library that is built with object files which have been compiled with "-g" flag. I separate out the debug info of this static library into a separate file such as : objcopy --only-keep-debug lib_mylib.o lib_mylib.o.debug

and then link my application with this static library (after stripping debug info from it) as gcc -g driver.c -o driver -L. -l_mylib

Can someone guide how can I load the debug info from the static library from lib_mylib.o.debug into GDB so that I can debug the code contributed by static libraries in my application ? I get below error in GDB when trying to load it via add-symbol-file command:

(gdb) add-symbol-file lib_mylib.a.debug 0x0
add symbol table from file "lib_mylib.a.debug" at
    .text_addr = 0x0
(y or n) y
`/home/sbunny/tmp/static-lib/lib_mylib.a.debug': can't read symbols: File format not recognized.
like image 521
sbunny Avatar asked Dec 01 '25 23:12

sbunny


1 Answers

Don't do this:

objcopy --only-keep-debug lib_mylib.o lib_mylib.o.debug

(It only works for shared libraries.)

Do this instead:

cp lib_mylib.o lib_mylib.o.debug

Even easier may be to keep lib_mylib.o untouched, and strip debug symbols from the executable at link time:

gcc -g driver.c -o driver -L. -l_mylib -Wl,-s

And easier still: link the binary with debug info and keep it for debugging, but use stripped executable where you need it to be stripped:

gcc -g driver.c -o driver-dbg -L. -l_mylib &&
strip -g -o driver driver-dbg

With above, you wouldn't need to add-symbol-file, just point GDB to driver-dbg and it will do everything automatically.

like image 198
Employed Russian Avatar answered Dec 04 '25 13:12

Employed Russian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!