C代码的coredump

159 篇文章 0 订阅

要点:

程序异常终止之后会生成core代码文件程序添加-g编译选项,从而包含调试信息结合gdb和coredump文件定位异常点


程序异常终止之后会生成coredump文件,不同的系统、不同的设置,这个自动生成的coredump文件也不一样。


示例代码:

#include <stdio.h>
 
int core_dump() {
    int i;
 
    for (i = 5; i >= 0; i--) {
        printf("(%d, %d)\n", i, 100 / i);
    }
 
    return 0;
}
 
int main() {
    core_dump();
 
    return 0;
}

以上代码存在除0异常,编译运行:

gcc -g core_dump.c -o core_dump
./core_dump

运行结果:

(5, 20)
(4, 25)
(3, 33)
(2, 50)
(1, 100)
[1]    9662 floating point exception (core dumped) 

为了更清楚地找到代码出错的地方,可以使用gdb来分析coredump文件:
~/examples/cpp/core_dump % gdb ./a.out core
GNU gdb (GDB) 7.7
Copyright (C) 2014 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 "i686-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http: www.gnu.org="" software="" gdb="" bugs="">.
Find the GDB manual and other documentation resources online at:
<http: www.gnu.org="" software="" gdb="" documentation="">.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
[New LWP 9662]
 
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?
Core was generated by `./a.out'.
Program terminated with signal SIGFPE, Arithmetic exception.
#0  0x08048415 in core_dump ()
(gdb) where
#0  0x08048415 in core_dump ()
#1  0x0804844b in main ()
(gdb)

可以看到上面并没有给出具体的代码&行号,因为需要添加-g编译选项。为此重新编译、运行,此时的coredumnp文件就提供了全面的信息:
~/examples/cpp/core_dump % gdb ./a.out core
GNU gdb (GDB) 7.7
Copyright (C) 2014 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 "i686-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http: www.gnu.org="" software="" gdb="" bugs="">.
Find the GDB manual and other documentation resources online at:
<http: www.gnu.org="" software="" gdb="" documentation="">.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
[New LWP 9758]
 
warning: Could not load shared library symbols for linux-gate.so.1.
Do you need "set solib-search-path" or "set sysroot"?
Core was generated by `./a.out'.
Program terminated with signal SIGFPE, Arithmetic exception.
#0  0x08048415 in core_dump () at main.c:7
7                       printf("(%d, %d)\n", i, 100 / i);
(gdb) where
#0  0x08048415 in core_dump () at main.c:7
#1  0x0804844b in main () at main.c:14
(gdb) 

补充:

当然,并不是所有的异常都会生成coredump文件,具体请参考《Unix环境高级 编程 (2nd)》第十章 信号。有些环境缺省设置coredump文件是0字节,为此需要ulimit -c 1024(文件大小)。

ulimit -c unlimited

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值