Linux平台Core Dump分析段错误

Linux平台通过core dump分析段错误

当Linux平台碰到段错误(Segmentation fault)的时候,可以通过开启core dump,让其产生core dump文件。
下面是在ubuntu 18.08使用core dump文件快速定位段错误的例子

1. 开启core dump

~$ echo "/tmp/core-%e-%t" > /proc/sys/kernel/core_pattern
~$ ulimit -c unlimited

2. 复现段错误

示例代码(seg_fault.c):

void segmentation_fault()
{
    char *p = "hello";
    *p = '\0';
}
int main()
{
    segmentation_fault();
    return 0;
}

GCC编译(开启 -g)

~$ gcc -g seg_fault.c -o seg_fault

运行结果

~$ ./seg_fault
Segmentation fault (core dumped)

Segmentation fault (core dumped) 说明已经产生core dump文件

~$ ls /tmp | grep core
core-seg_fault-1552306767

3. 通过gdb查看段错误发生点

使用方法:

~$ gdb [options] [executable-file [core-file or process-id]]
~$ gdb ./seg_fault /tmp/core-seg_fault-1552306767
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 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 "x86_64-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 ./seg_fault...done.
[New LWP 4041]
Core was generated by `./seg_fault'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x000055cb02f8360d in segmentation_fault () at seg_fault.c:4
4		*p = '\0';
(gdb) 

#0 0x000055cb02f8360d in segmentation_fault () at seg_fault.c:4 说明段错误发生在代码第四行

*p = ‘\0’;

如果segmantation fault发生的地点(#0)不是很清晰,通过bt命令也可以回溯代码的调用情况

(gdb) bt
#0  0x000055cb02f8360d in segmentation_fault () at seg_fault.c:4
#1  0x000055cb02f83621 in main () at seg_fault.c:8
(gdb) 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值