1. 首先生成core檔案,示例代碼如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void core1()
{
char *p = NULL;
*p = 'a';
printf("p:%s\n", p);
}
int main()
{
core1();
return 0;
}
[[email protected] aup]$ gcc coredump.c -o coredump -g
[[email protected] aup]$ ./coredump
段錯誤 (核心已轉儲)
[[email protected] aup]$ ll core.*
-rw-------. 1 ljq ljq 217088 5月 16 15:45 core.5004
core檔案已生成。
2. 使用gdb調試core檔案,定位錯誤位置:
[[email protected] aup]$ gdb coredump core.5004
GNU gdb (GDB) Fedora 7.10-23.fc23
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from coredump...done.
[New LWP 5004]
Core was generated by `./coredump'. 指出core檔案是由corddump産生
Program terminated with signal SIGSEGV, Segmentation fault. 程式接受到信号SIGSEGV段錯誤而終止
#0 0x0804843b in core1 () at coredump.c:8 程式終止時執行到coredump.c第8行,即:*p = 'a';,進而判斷執行到該行時程式異常退出
8 *p = 'a';
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.22-3.fc23.i686
(gdb) bt backtrace指令檢視函數調用棧順序
#0 0x0804843b in core1 () at coredump.c:8 最後一個被調用的函數是core1,如果有更多的函數被調用,這裡也會顯示出來
#1 0x0804846a in main () at coredump.c:14 corddump.c第14行調用core1函數
(gdb)