让程序崩溃后生成Core Dump

在Linux下,程序崩溃是很头疼的事情(其实Windows更是如此)。

我们可以生成core dump文件,并用gdb重现崩溃时的场景。

ulimit设置core dump开关和大小

1 ulimit -c unlimited

测试代码:

01 #include <stdio.h>
02  
03  
04 int main(int argc, char* argv[])
05 {
06     char * p = NULL;
07  
08     *p = 123;
09  
10     return 0;
11 }

编译:

1 gcc -g
2  ./main.c -o ./main.bin

执行,提示出错:

1 ./main.bin
2 段错误 (core dumped)

用gdb调试复原:

1 gdb ./main.bin --core=./core

gbd信息:

view source
print ?
01 GNU gdb (GDB) 7.1-ubuntu
02 Copyright (C) 2010 Free Software Foundation, Inc.
03 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
04 This is free software: you are free to change and redistribute it.
05 There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
06 and "show warranty" for details.
07 This GDB was configured as "i486-linux-gnu".
08 For bug reporting instructions, please see:
09 <http://www.gnu.org/software/gdb/bugs/>...
10 Reading symbols from /tmp/main.bin...done.
11 [New Thread 18055]
12  
13 warning: Can't read pathname for load map: 输入/输出错误.
14 Reading symbols from /lib/tls/i686/cmov/libc.so.6...(no debugging symbols found)...done.
15 Loaded symbols for /lib/tls/i686/cmov/libc.so.6
16 Reading symbols from /lib/ld-linux.so.2...(no debugging symbols found)...done.
17 Loaded symbols for /lib/ld-linux.so.2
18 Core was generated by `./main.bin'.
19 Program terminated with signal 11, Segmentation fault.
20 #0  0x080483c4 in main (argc=1, argv=0xbfbbafa4) at ./main.c:8
21 8       *p = 123;

你可能感兴趣的:(让程序崩溃后生成Core Dump)