ubuntu生成&设置core文件,调试段错误

6 篇文章 0 订阅

在用ubuntu系统编码c++程序时,经常遇到段错误,以下介绍ubuntu系统下如何生成core文件,设置core文件路径,并进行调试core文件,找到段错误原因。

1.允许ubuntu系统生成core文件

ulimit -a

查看core文件有关所有信息

如果core file size为0,需要设置ulimit -c unlimited,在个人配置文件~/.bashrc或者/etc/profile中添加才可以永久生效,否则仅在当前窗口生效

2.设置core文件生成路径

  • 设置pid作为文件扩展名

echo “1” > /proc/sys/kernel/core_uses_pid

  • 控制core文件保存位置和文件名格式

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

%p - insert pid into filename 添加pid(进程id)
%u - insert current uid into filename 添加当前uid(用户id)
%g - insert current gid into filename 添加当前gid(用户组id)
%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的命令名

上面的设置大概只是暂时生效,永久生效需要在系统配置文件/etc/sysctl.conf中设置

kernel.core_uses_pid = 1
kernel.core_pattern = /corefile/core-%e-%p-%t

 然后执行sysctl -p立即生效,检查是否生效:

cat kernel.core_uses_pid
cat kernel.core_pattern

查看输出是否为自己设置的路径

如果重启电脑之后还是无法生效,则需要再次执行sysctl -p

3.调试core文件

bt:打印堆栈信息
where: 定位core的地方
f n: 进入指定的堆栈
p 变量名:打印变量名,查看错误情况

参考:

https://blog.csdn.net/a395381306/article/details/99759941

Linux生成core文件、core文件路径设置_faith的运维开发之路的博客-CSDN博客_linux core配置

LINUX C/C++ GDB 调试core文件_Hamlee67的博客-CSDN博客

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值