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?
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 thisfoo-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 usegdb 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
(andg++
), 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 thecore
.If you used
clang
, this procedure will probably not work, becauseclang
doesn't produce identical code with and without-g
.