gdb 调试 coredump

一 coredump 概述

Coredump叫做核心转储,它是进程运行时在突然崩溃的那一刻的一个内存快照。操作系统在程序发生异常而异常在进程内部又没有被捕获的情况下,会把进程此刻内存、寄存器状态、运行堆栈等信息转储保存在一个文件里。该文件为二进制文件,linux 使用gdb分析,windows下使用windebug分析。

备注:编译时需要加-g 选项使程序生成调试信息

二 配置生成 core文件

1 core 文件开关

(1) 使用 ulimit -c 查看core开关,如果为0表示关闭,不会生成core文件
(2) 使用 ulimit -c [filesize] 设置core文件大小,当最小设置为4之后才会生成core文件
(3) 使用 ulimit -c unlimited 设置core文件大小为不限制,这是常用的做法
(4) 如果需要开机就执行,则需要将这句命令写到 /etc/profile 等文件。

2 core文件命名和保存路径

(1) core文件有默认的名称和路径
默认名称:core
默认路径: 执行程序目录
备注【为了方便,我们通常会自己命名和指定保存路径】

(2) 可以通过 /proc/sys/kernel/core_pattern 设置 core 文件名和保存路径
方法如下:

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

备注
<1> 如果有:sudo echo x > 时的 Permission denied错误,则用下面命令

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

<2> corefile 文件夹需要存在

(3) 参数列表
%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 添加 执行文件的名字

三 调试core文件

方法1
(1) gdb [exec file] [core file]
(2) 执行 bt 或 where 看堆栈信息
在这里插入图片描述
方法2
(1) gdb -c [core file]
(2) file [exec file]
(3) 执行 bt 或 where 查看堆栈信息
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值