Linux调试之(五)gdb调试coredump

本文详细介绍了Linux系统中core文件的含义,如何配置生成core文件,包括开启core文件开关、设置文件大小及命名路径,并提供了调试core文件的方法,包括通过gdb加载exec文件和core文件来查看堆栈信息,帮助开发者有效定位和解决问题。
摘要由CSDN通过智能技术生成

1.什么是core文件?

有问题的程序运行后,产生“段错误 (核心已转储)”时生成的具有堆栈信息和调试信息的文件。


2.怎样配置生成 core 文件

(1)core文件开关

①使用 ulimit -c 查看core开关,如果为0表示关闭,不会生成core文件;

②使用 ulimit -c [filesize] 设置core文件大小,当最小设置为4之后才会生成core文件;

③使用 ulimit -c unlimited 设置core文件大小为不限制,这是常用的做法;

④如果需要开机就执行,则需要将这句命令写到 /etc/profile 等文件。

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

在这里插入图片描述


(2)core文件命名和保存路径

core文件有默认的名称和路径,但为了方便我们通常会自己命名和指定保存路径方法如下:

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

命名的参数列表:

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

3.调试core文件

(1)test.c
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>

struct{
   
   int rlim_cur;
   int rlim_max;
}rlimt;

#define CORE_SIZE   1024 * 1024 * 500
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值