0
点赞
收藏
分享

微信扫一扫

Linux的core文件

在程序不寻常退出时,内核会在当前工作目录下生成一个core文件(是一个内存映像,同时加上调试信息)。使用gdb来查看core文件,可以指示出导致程序出错的代码所在文件和行数。

1.core文件的生成开关和大小限制 
(1)  使用ulimit -c命令可查看core文件的生成开关。若结果为0,则表示关闭了此功能,不会生成core文件。

通过上面的命令修改后,一般都只是对当前会话起作用,当你下次重新登录后,还是要重新输入上面的命令,所以很麻烦。我们可以把通过修改 /etc/profile文件 来使系统每次自动打开。
步骤如下:
1.首先打开/etc/profile文件
一般都可以在文件中找到 这句语句:ulimit -S -c 0 > /dev/null 2>&1.ok,根据上面的例子,我们只要把那个0 改为 unlimited 就ok了。然后保存退出。
2.通过source /etc/profile 使当期设置生效。
3.通过ulimit -c 查看下是否已经打开。

 

(2)  使用ulimit -c filesize命令,可以限制core文件的大小(filesize的单位为kbyte)。若ulimit -c unlimited,则表示core文件的大小不受限制。如果生成的信息超过此大小,将会被裁剪,最终生成一个不完整的core文件。在调试此core文件的时候,gdb会提示错误。

在RH8的环境文件/etc/profile中,我们可以看到系统是如何配置ulimit的:
#grep ulimit /etc/profile
ulimit -S -c 0 > /dev/null 2>&1
这条语句设置了对软件资源和对core文件大小的设置

 

2代码测试

#include <stdio.h>

char *str = "test";

void core_test()
{
str[1] = 'T';
}

int main()
{
core_test();

return 0;
}

# gcc b.c 
# ./a.out 
段错误 (核心已转储)
# ls
a.out  b.c  core
# gdb a.out core

GNU gdb (Ubuntu 7.7-0ubuntu3.1) 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 "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.out...(no debugging symbols found)...done.

warning: exec file is newer than core file.
[New LWP 30512]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00000000004004fc in core_test ()
(gdb) bt
#0  0x00000000004004fc in core_test ()
#1  0x000000000040050f in main ()
(gdb) 

 

 

 

 

 


举报

相关推荐

0 条评论