在Linux下,程序崩溃是很头疼的事情(其实Windows更是如此)。
我们可以生成core dump文件,并用gdb重现崩溃时的场景。
ulimit设置core dump开关和大小
1 | ulimit -c unlimited |
测试代码:
1
2
3
4
5
6
7
8
9
10
11
|
#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;
|