小应用大智慧-Coredump故障分析

Q:当一个程序运行时出错时,我们可以立即使用GDB工具进行调试,然而,当程序运行了3天之后出现了错误,难道我们还要使用GDB调试3天吗?
A:答案是否定的。我们此时需要另一个工具了,那就是Coredump。

–Coredump定义
它又叫核心转存,当程序运行过程中发生异常,Linux系统会讲异常信息保存在一个名为core的文件中,这个过程叫做Coredump。

–segment fault
CoreDump通常情况对应于什么样的错误呢?答案是段错误。
在Linux 应用程序执行过程中,通常会遇到段错误,出现段错误的原因有:
1. 数组访问越界
2. 访问空指针
3. 栈溢出
4. 修改只读内存

–CoreDump使能
通常情况下CoreDump的使能开关是关闭的,可以通过ulimit命令打开/关闭COreDump功能:

打开:ulimit -c unlimited
关闭:ulimit -c 0

–Core文件分析
当发生Core异常时,可以通过gdb查看core文件内容,以定位文件出错的位置。
用法:gdb 程序名 core文件名

下面通过两个实例分析CoreDump故障分析:

segment1.c

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 int main()
  5 {
  6     int *ptr = NULL;
  8     *ptr = 0;

  9     return 0;
 10 }          

编译segment1.c
gcc -g segment1.c -o segment1
注:由于需要使用gdb工具,这里必须加上-g,否则无法使用gdb。
执行segment1:./segment1 出现段错误
此时我们会发现该文件夹下会多一个core文件,它保存着程序异常信息

–通过使用gdb打开core文件:
gdb ./segment1 core

root@ubuntu:~/app/CoreDump# gdb ./segment1 core
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
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-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 ./segment1...done.

warning: core file may not match specified executable file.
[New LWP 5098]
Core was generated by `./segment'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x080483fd in main () at segment1.c:8
8       *ptr = 0;
(gdb) 

从打印的信息来看,gdb将异常信息定位在了第8行,通过查看程序第8行得知,是访问了空指针的错误。

再看一个示例:
segment2.c

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 int main()
  6 {
  7 
  8     char *ptr = "123456";
  9     ptr[0] = '7';
 10     return 0;
 11 }

编译、执行程序
打开core文件:
gdb ./segment2 core

root@ubuntu:~/app/CoreDump# gdb ./segment2 core
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
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-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 ./segment2...done.

warning: core file may not match specified executable file.
[New LWP 5098]
Core was generated by `./segment'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x080483fd in main () at segment2.c:9
9       ptr[0] = '7';
(gdb) 

此时gdb将异常定位到第9行,从而分析出修改了只读常量造成的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值