Debug corefile without symbols

100 Views Asked by At

context: my program is running on server, which is compiled without debug option(hard to change that config).

I get the corefile which doesn't contain the symbol, and I have the source code(unfortunately error cannot be reproduced locally).

I know strip(https://man7.org/linux/man-pages/man1/strip.1.html) can remove symbols from ELF. Is there a tool can add them back?

1

There are 1 best solutions below

0
On

Is there a tool can add them back?

No, but you can give GDB a binary with debug symbols instead of the binary which actually produced the core.

The best practice is to always build with debug symbols, e.g. gcc -g -O2 foo.c -o foo-g, keep this foo-g for future debugging, but ship to production a stripped binary: strip -g -o foo foo-g`.

When you get a core from production, simply use gdb foo-g core, and you will have a much easier time debugging.


But let's assume you didn't follow the best practice, and built your binary with gcc -O2 foo.c -o foo. Can you do anything to recover debug info?

With gcc (and g++), you should be able to. Simply rebuild the application from exactly the same sources, using exactly the same command-line flags, but appending -g. Use newly-built binary to debug the core.

If you used clang, this procedure will probably not work, because clang doesn't produce identical code with and without -g.