core dumped ?完了?

15 篇文章 0 订阅
3 篇文章 0 订阅

 

微信公众号:linux码头

 

core dumped:当程序在运行过程中发生异常,这时linux系统可以把程序出错的内存

内容存储在一个core文件中,又叫核心转存。

 

应用程序在运行过程汇总经常会遇到segment fault,通常产生的原因:

数组访问越界

访问空指针

栈溢出

修改只读内存 

 

linux系统默认关闭core dumped功能,但可以通过ulimit命令打开或关闭

core dumped功能。

打开 ulimit -c unlimited

关闭 ulimit -c 0

 

发生core dumped后,可以使用gdb进行查看core文件内容,定位程序出错位置

用法:gdb 程序名 core文件名

在编译文件时加上-g,对于执行的程序产生core文件后,gdb后可以找出错误

记得养成编译时加上-g的习惯。

 

 

案例一:空指针赋值          

#include <stdio.h>
int main()
{        
    int *ptr = NULL;       
    *ptr = 1314;
    return 0;
}

 

没加-g调试选项:

 

chh001@lucky:~$ gcc a.c -o a
chh001@lucky:~$ ./a
Segmentation fault (core dumped)
chh001@lucky:~$ gdb a core
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 a...(no debugging symbols found)...done.
[New LWP 47460]
Core was generated by `./a'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004004e6 in main ()
(gdb) 

 

加上-g调试选项:

 

chh001@lucky:~$ gcc -g a.c -o a
chh001@lucky:~$ ./a
Segmentation fault (core dumped)
chh001@lucky:~$ gdb a core
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 a...done.
[New LWP 47499]
Core was generated by `./a'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004004e6 in main () at a.c:6
6        *p = 1314;     --->错误处
(gdb) 

 

案例2:修改只读变量

#include <stdio.h>
int main()
{
    char *p = "iloveyou";
    p[0]='h';
    return 0;
}

 

chh001@lucky:~$ gcc -g a.c -o a
chh001@lucky:~$ ./a
Segmentation fault (core dumped)
chh001@lucky:~$ gdb a core
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 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 a...done.
warning: exec file is newer than core file.
[New LWP 47499]
Core was generated by `./a'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004004e6 in main () at a.c:6
6        p[0]='h';
(gdb) 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"Segmentation fault (core dumped)"是一个错误提示,它表示程序发生了段错误(Segmentation fault),并将核心转储(core dumped)。 段错误是一种常见的程序错误,通常是由于访问无效的内存地址或者试图使用未初始化的指针导致的。当程序发生段错误时,操作系统会终止程序的执行,并生成一个核心转储文件,其中包含有关程序崩溃时的内存状态和调用堆栈信息。 通常情况下,Segmentation fault错误是由于编程错误、内存问题或者操作系统问题引起的。可能的原因包括: 1. 无效的内存访问:例如访问已释放的内存、数组越界、指针问题等。 2. 未初始化的指针:使用了未初始化的指针变量。 3. 操作系统限制:例如尝试访问未分配给程序的内存区域、访问只读内存等。 要解决这个问题,你可以尝试以下步骤: 1. 检查代码:仔细检查你的代码,特别是与内存操作相关的部分,确保没有访问无效内存或使用未初始化的指针。 2. 调试工具:使用调试工具如gdb(GNU调试器)来分析核心转储文件,查看错误发生的位置和调用堆栈信息,以便更好地理解问题的来源。 3. 确保环境和依赖项正确:确保你的环境和依赖项都是正确的,并按照正确的方式安装和配置。 4. 寻求帮助:如果问题仍然存在,你可以在相关的社区论坛、开发者论坛或技术支持渠道上寻求帮助。其他开发者或专家可能能够提供更具体的建议和解决方案。 需要注意的是,Segmentation fault错误可能需要进一步的调试和分析才能确定具体原因和解决方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值