core dump文件

一、core 文件的生成

    C程序因为segment fault(段错误)崩溃时,如果系统core dump功能是打开的,那么将会有内存映像转储到硬盘上来,之后可以用gdb对core文件进行分析,core 文件本质是一个内存映象 ( 同时加上调试信息 ) ,还原系统发生段错误时刻的堆栈情况。

    在shell中输入命令若返回值为1则说明该功能已打开

ulimit -c

    以下命令为调整相应功能的指令,但是仅在此次设置时有效

ulimit -c 0 不产生core文件

ulimit -c 100 设置core文件最大为100k

ulimit -c unlimited 不限制core文件大小

    在用户的 ~/.bash_profile 里加上 ulimit -c unlimited 来让特定的用户可以产生 core 文件

echo "ulimit -c 1024" >> ~/.bash_profile

    在/etc/profile文件中写入可对所有用户有效

echo "ulimit -c 1024" >> /etc/profile

重新登陆 shell

二、core文件命名方式

设置 Core Dump 的核心转储文件目录和命名规则

cat /proc/sys/kernel/core_uses_pid 

可以控制产生的 core 文件的文件名中是否添加 pid 作为扩展 ,如果添加则文件内容为 1 ,否则为 0

cat /proc/sys/kernel/core_pattern 

可以设置格式化的 core 文件保存位置或文件名 ,比如原来文件内容是 core

可以这样修改 :

sudo echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern

将会控制所产生的 core 文件会存放到 /corefile 目录下(前提是这个文件夹提前存在,否则也生成不了),产生的文件名为 core- 命令名 -pid- 时间戳

core dump不能用Tab补齐名字,还是不要太长的好。

以下是参数列表 :

    %p - insert pid into filename 添加 pid

    %u - insert current uid into filename 添加当前 uid

    %g - insert current gid into filename 添加当前 gid

    %s - insert signal that caused the coredump into the filename 添加导致产生 core 的信号

    %t - insert UNIX time that the coredump occurred into filename 添加 core 文件生成时的 unix 时间

    %h - insert hostname where the coredump happened into filename 添加主机名

    %e - insert coredumping executable name into filename 添加命令名

三、调试

#include <stdio.h>

int func(int*p) {

        *p = 0;

}

int main() {

        func(NULL);

        return 0;

}

生成可执行文件并运行

gcc -g -o main a.c

root@ubuntu:~# ./main

Segmentation fault (core dumped)

gdb查看信息

gdb main /tmp/core-main-10815 

Program terminated with signal 11, Segmentation fault.

#0  0x080483ba in func (p=0x0) at a.c:5

5          *p = 0;

    在载入core的时候有个小技巧,如果你事先不知道这个core文件是由哪个程序产生的,你可以先随便找个代替一下,比如/usr/bin/w就是不错的选择。比如我们采用这种方法载入上边产生的core,gdb会有类似的输出:

    /usr/bin/w coreCore was generated by ./coredump

    Program terminated with signal 11, Segmentation fault.

    #0  0x080483a7 in ?? ()

可以看到GDB已经提示你了,这个core是由哪个程序产生的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在Linux下程序不寻常退出时,内核会在当前工作目录下生成一个core文件(是一个内存映像,同时加上调试信息)。使...
    随风化作雨阅读 46,994评论 2 15
  • core dump:(内存快照)在linux中当程序发生异常中止或者崩溃时,操作系统会将程序当时的内存状况记录下来...
    zs0zrc阅读 1,570评论 0 1
  • 什么是 core dump ? 进程接收到某些信号(signal)后, 异常终止,并记录当前内存等状态的镜像文件,...
    Lionel_素心阅读 6,223评论 0 3
  • 1、core文件简介 core文件其实就是内存的映像,当程序崩溃时,存储内存的相应信息,主用用于对程序进行调试。当...
    六弦极品阅读 451评论 0 0
  • 什么是coredump Coredump叫做核心转储,它是进程运行时在突然崩溃的那一刻的一个内存快照。操作系统在程...
    java菜阅读 4,269评论 0 4