用调试符号重新编译exe,用于exe生成的核心转储,不带调试符号

时间:2014-08-04 06:07:30

标签: gcc gdb

我有一个缺少调试信息的核心转储。转储是由一个不那么可重复的错误引起的。

由于我确切知道源版本和原始构建命令及优化级别,是否可以为此可执行文件生成调试信息?

1 个答案:

答案 0 :(得分:1)

是的,这是可能的。这是一个相当冗长的例子。

制作崩溃的程序(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;
}   

编译版本,不带调试符号:

$ gcc crash.c -o crash

使用调试符号编译版本:

$ gcc -g crash.c -o crash_debug

使用二进制生成core文件,不带调试符号:

$ ./crash AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Segmentation fault (core dumped)

使用gdb和二进制不带调试符号来查看核心:

$ 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 ()

正如我们所看到的,gdb找不到任何调试符号。 现在让我们尝试使用构建的二进制文件启动gdb以包含调试符号和core文件:

$ 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   }

这有效!

另一种方式,正如@ dbrank0建议的那样,将使用symbol-file命令从不同的二进制文件中加载符号:

$ 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$

希望这有帮助!