Linux Coredump 配置生成实例

2 篇文章 0 订阅

介绍

Coredump是Linux程序运行时的内存映象,当程序发生异常被捕获存储,用于程序调试分析。

 

开启

为程序存储coredump(又称core)文件需要系统配置支持。

# ulimit -c
0

# ulimit -c unlimited
# ulimit -c 1024

0表示不生成文件。可以通过 ulimit -c <size> 设置一个大小,单位为KB,不限制则使用unlimited

通过命令设置只会在当前控制台生效,若要永久生效,则需要改/etc/security/limits.conf文件。

 

如里大小设置的小了会怎么样:

BFD: Warning: /var/core/core_core_dump is truncated: expected core file size >= 245760, found: 12288.

在调试时会提示上面的警告。大小需根据程序实际内存占用情况评估。

 

存储路径

默认coredump文件存在在程序运行的目录下。

/etc/sysctl.conf  中可以配置core文件的存储路径和文件的命名格式。

如:kernel.core_pattern = /var/core/core_%e

配置参数还有:

%c 转储文件的大小上限

%e 所dump的文件名

%g 所dump的进程的实际组ID

%h 主机名

%p 所dump的进程PID

%s 导致本次coredump的信号

%t 转储时刻(由1970年1月1日起计的秒数)

%u 所dump进程的实际用户ID

配置后,可以通过 sysctl  -p 命令生效。

 

实例

#include <stdio.h>

void func()
{
    int *ptr = NULL;
    *ptr = 10;
}

int main()
{
    func(); 

    return 0;
}

编译运行(编译带-g参数,保留调试信息)

# gcc -o core_dump core_dump.c -g

# ./core_dump 
Segmentation fault (core dumped)

调试:

# gdb ./core_dump /var/core/core_core_dump 
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-115.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /wayne/programs/debug/core_dump...done.
[New LWP 20335]
Core was generated by `./core_dump'.
Program terminated with signal 11, Segmentation fault.
#0  0x00000000004004dd in func () at core_dump.c:6
6           *ptr = 10;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7_6.6.x86_64
(gdb) bt
#0  0x00000000004004dd in func () at core_dump.c:6
#1  0x00000000004004f3 in main () at core_dump.c:11
(gdb) quit

 

关于abrt

abrt是一个监控应用进程崩溃的服务,其会收集程序包括coredump文件在内的相关异常印象,方便问题排查。

对于独立运行的C程序,需要一定的条件修改才能支持,否则会有如下提示:

相关配置在/etc/abrt下。

 

参考:

abrt

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/deployment_guide/ch-abrt

https://www.centos.org/forums/viewtopic.php?t=5962

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值