core dumped ?完了?

微信公众号:嵌入式图像处理

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) 

猜你喜欢

转载自blog.csdn.net/m0_37797953/article/details/84920953
今日推荐