Linux core dump文件介绍及调试

本文介绍了core文件的概念,如何在系统中开启或关闭其生成,以及如何通过core文件进行程序调试。包括设置核心转储文件目录、命名规则和使用gdb工具解析core文件。通过实例演示了如何生成和利用core文件进行调试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.core文件的简单介绍

在一个程序崩溃时,它一般会在程序的当前目录下生成一个core文件。core文件仅仅是一个内存映象(同时加上调试信息),主要是用来调试的。

2. 开启或关闭core文件的生成

以下命令可以检查生成core文件的选项是否打开:

ulimit –a

该命令将显示所有的用户定制,其中选项-a代表“all”。查看结果中形如:core file size          (blocks, -c) 0的内容,如果红色文字为0,则表示未打开core文件;否则,已打开并代表core文件的最大kb字节数。

用以下命令来系统为当前bash的当前用户生成core文件,生成的文件一般在程序的当前目录下

#ulimit –c unlimited

unlimited表示coredump文件大小无限制。

另外,可以用以下命令来阻止系统生成core文件:

ulimit -c 0

0表示文件大小为0。

 

也可以修改系统文件来调整core选项

在/etc/profile通常会有这样一句话来禁止产生core文件,通常这种设置是合理的:

ulimit -S -c 0 > /dev/null 2>&1

但是在开发过程中有时为了调试问题,还是需要在特定的用户环境下打开core文件产生的设置

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

也可以用ulimit -c 1024,表示限制产生的core文件的大小不能超过1024kb

 

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

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

/proc/sys/kernel/core_pattern可以设置格式化的core文件保存位置或文件名,比如原来文件内容是core-%e

可以这样修改:

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

将会控制所产生的core文件会存放到/corefile目录下,产生的文件名为core-命令名-pid-时间戳

以下是参数列表:

   %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 添加命令名

 

4. 使用core文件来调试程序

例如,a.c程序内容如下:


#include<stdio.h>
int main()
{
   int a = 10;
   int b;
   a =  a- 10;
   b = 10 / a; //会出现除以0的错误,程序会崩溃
   printf("%d", b);
}


1、用-g参数编译a.c程序文件,并执行

#gcc –g a.c –o a.out

#./a.out

Floating point exception (core dumped)

#ls

a.c           a.out                   core

当前目录下生成了core文件。


2、使用gdb调试core文件

#gdb –core=core

GNU gdb (Ubuntu 7.7-0ubuntu3.1) 7.7

Copyright (C) 2014 Free SoftwareFoundation, Inc.

License GPLv3+: GNU GPL version 3 or later<http://gnu.org/licenses/gpl.html>

This is free software: you are free tochange and redistribute it.

There is NO WARRANTY, to the extentpermitted by law.  Type "showcopying"

and "show warranty" for details.

This GDB was configured as"x86_64-linux-gnu".

Type "show configuration" forconfiguration details.

For bug reporting instructions, please see:

<http://www.gnu.org/software/gdb/bugs/>.

Find the GDB manual and other documentationresources online at:

<http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".

Type "apropos word" to search forcommands related to "word".

[New LWP 29993]

Core was generated by `./a.out'.

Program terminated with signal SIGFPE,Arithmetic exception.

#0 0x0000000000400546 in ?? ()

(gdb)

此时会显示生成此core文件的程序名(a.out),中止此程序的信号(SIGFPE)等等。

(gdb) bt

#0 0x0000000000400546 in ?? ()

#1 0x00007fff27fbf7d0 in ?? ()

#2 0x0000000000000000 in ?? ()

此时bt还不能看到调用堆栈,需要将程序的symbol读进来

(gdb) file ./a.out

Reading symbols from ./a.out...done.

这时就可以使用bt查看调用堆栈,用list查看程序在哪行代码中止了。

(gdb) bt

#0 0x0000000000400546 in main () at a.c:8

(gdb) list

1       #include <stdio.h>

2      

3       int main()

4       {

5         int a = 10;

6         int b;

7         a =  a - 10;

8         b =  10 / a;

9         printf("%d", b);

10     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值