加載可執行程序和core dump文件

例子

#include <stdio.h>

int main(void) {
        int *p = NULL;
        printf("hello world\n");
        *p = 0;
        return 0;
}

技巧

例子程序訪問了一個空指針,所以程序會crash併產生core dump文件。用gdb調試core dump文件,通常用這個命令形式:“gdb path/to/the/executable path/to/the/coredump”,然後gdb會顯示程序crash的位置:

bash-3.2# gdb -q /data/nan/a /var/core/core.a.22268.1402638140
Reading symbols from /data/nan/a...done.
[New LWP 1]
[Thread debugging using libthread_db enabled]
[New Thread 1 (LWP 1)]
Core was generated by `./a'.
Program terminated with signal 11, Segmentation fault.
#0  0x0000000000400cdb in main () at a.c:6
6               *p = 0;

有時我們想在gdb啟動後,動態加載可執行程序和core dump文件,這時可以用“file”和“core”(core-file命令縮寫)命令。“file”命令用來讀取可執行文件的符號表信息,而“core”命令則是指定core dump文件的位置:

bash-3.2# gdb -q
(gdb) file /data/nan/a
Reading symbols from /data/nan/a...done.
(gdb) core /var/core/core.a.22268.1402638140
[New LWP 1]
[Thread debugging using libthread_db enabled]
[New Thread 1 (LWP 1)]
Core was generated by `./a'.
Program terminated with signal 11, Segmentation fault.
#0  0x0000000000400cdb in main () at a.c:6
6               *p = 0;

可以看到gdb同樣顯示程序crash的位置。

這兩個命令可參見gdb手冊

貢獻者

nanxiao