linux系统crash问题练习二:除零异常

测试代码如下:
#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
#

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值