在Linux下,程序崩潰是很頭疼的事情(其實Windows更是如此)。
我們可以生成core dump文件,并用gdb重現崩潰時的場景。
ulimit設置core dump開關和大小
1
|
ulimit -c unlimited |
測試代碼:
1
2
3
4
5
6
7
8
9
10
|
#include <stdio.h> int main( int argc, char * argv[]) { char * p = NULL; *p = 123; return 0; } |
編譯:
1
2
|
gcc -g ./main .c -o . /main.bin |
執行,提示出錯:
1
2
|
./main.bin 段錯誤 (core dumped) |
用gdb調試復原:
1
|
gdb . /main .bin --core=. /core |
gbd信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
GNU gdb (GDB) 7.1-ubuntu Copyright (C) 2010 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 "i486-linux-gnu" . For bug reporting instructions, please see: <http: //www .gnu.org /software/gdb/bugs/ >... Reading symbols from /tmp/main .bin... done . [New Thread 18055] warning: Can't read pathname for load map: 輸入/輸出錯誤. Reading symbols from /lib/tls/i686/cmov/libc .so.6...(no debugging symbols found)... done . Loaded symbols for /lib/tls/i686/cmov/libc .so.6 Reading symbols from /lib/ld-linux .so.2...(no debugging symbols found)... done . Loaded symbols for /lib/ld-linux .so.2 Core was generated by `. /main .bin'. Program terminated with signal 11, Segmentation fault. #0 0x080483c4 in main (argc=1, argv=0xbfbbafa4) at ./main.c:8 8 *p = 123; |