Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recomile exe with debugging symbols, use for core dump generated by exe without debugging symbols

Tags:

gcc

gdb

I have a core dump that lacks debugging information. The dump was caused by a not-so-reproducible bug.

Since I know exactly which version of source and the original build commands and optimization levels, is it possible to generate debugging information for this executable?

like image 220
nishantjr Avatar asked Sep 10 '25 08:09

nishantjr


1 Answers

Yes this is possible. Here is a rather verbose example.

Program to produce a crash (crash.c):

#include <stdio.h>                                                                             
#include <string.h>

int func(char *str){
    char buff[32];
    strcpy(buff,str);
    return 0;
}   

int main(int argc, char *argv[]){
    func(argv[1]);
    return 0;
}   

Compile a version without debug symbols:

$ gcc crash.c -o crash

Compile a version with debug symbols:

$ gcc -g crash.c -o crash_debug

Generate a core file using the binary without debug symbols:

$ ./crash AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Segmentation fault (core dumped)

Use gdb and the binary without debug symbols to look at the core:

$ gdb -q ./crash  core                                                                         

warning: ~/.gdbinit.local: No such file or directory
Reading symbols from ./crash...(no debugging symbols found)...done.

warning: exec file is newer than core file.
[New LWP 7768]
Core was generated by `./crash AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000000000040052b in func ()

As we can see, gdb could not find any debug symbols. Now lets try to start gdb with the binary that was build to include debug symbols and the core file:

$ gdb -q ./crash_debug  core                                                                   

warning: ~/.gdbinit.local: No such file or directory
Reading symbols from ./crash_debug...done.

warning: core file may not match specified executable file.
[New LWP 7768]
Core was generated by `./crash AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000000000040052b in func (str=0x7fff4bb66f73 'A' <repeats 52 times>) at crash.c:8
8   }

This works!

Another way, as @dbrank0 suggested, would be using the symbol-file command to load the symbols from a different binary:

$ gdb -q -c core                                                                               

warning: ~/.gdbinit.local: No such file or directory
[New LWP 7768]
Core was generated by `./crash AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000000000040052b in ?? ()
gdb$ symbol-file crash_debug 
Reading symbols from crash_debug...done.
gdb$ bt
#0  0x000000000040052b in func (str=0x7fff4bb66f73 'A' <repeats 52 times>) at crash.c:8
#1  0x4141414141414141 in ?? ()
#2  0x00007f0041414141 in ?? ()
#3  0x0000000200000000 in ?? ()
#4  0x0000000000000000 in ?? ()
gdb$

Hope this helps!

like image 101
mofoe Avatar answered Sep 13 '25 08:09

mofoe