测试代码如下:
#include <unistd.h>
#include <stdio.h>
static unsigned int _crash_test(void)
{
unsigned int value = 100;
unsigned int divide = 0;
unsigned int result = 0;
result = value / divide;
return result;
}
int main(int argc, char* argv[])
{
unsigned int result = 0;
printf("\ntest main");
result = _crash_test();
while(1)
{
sleep(5);
}
exit(0);
}
运行这个程序,系统会挂掉,可以看到会发生floating point exception。
sh-# ./test_main
Floating point exception (core dumped)
通过查看core dump档案,发现系统已经产生了SIGFPE信号,
这种错误通常可能是因为除零操作引起的。
sh-# ./gdb ./core_test_main_sig8_pid6739 ./test_main
GNU gdb (GDB) 7.1
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 "arm-linux".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from ./test_main...done.
warning: exec file is newer than core file.
[New Thread 6739]
Reading symbols from /lib/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib/ld-linux.so.3...(no debugging symbols found)...done.
Loaded symbols for /lib/ld-linux.so.3
Core was generated by `./test_main'.
Program terminated with signal 8, Arithmetic exception.
#0 0x400a4630 in raise () from /lib/libc.so.6
bt
#0 0x400a4630 in raise () from /lib/libc.so.6
#1 0x00008668 in __div0 ()
at ../../gcc-4.2.1/gcc/config/arm/lib1funcs.asm:1000
#2 0x0000863c in __udivsi3 ()
at ../../gcc-4.2.1/gcc/config/arm/lib1funcs.asm:754
#3 0x000083f4 in _crash_test ()
#4 0x00008430 in main ()
sh-#
在callstack中,看到了_crash_test函数,这是程序员自己编写的程序。
通过进一步检查程序源代码发现,果然是因为除零操作引起的程序错误。
如果看不到callstack,但是却有程序出错时的地址,
那也可以使用addr2line这个工具来尝试恢复程序出错的位置。
# addr2line 0x83f4 -e ./test_main -f
_crash_test
#