在用ubuntu系统编码c++程序时,经常遇到段错误,以下介绍ubuntu系统下如何生成core文件,设置core文件路径,并进行调试core文件,找到段错误原因。
1.允许ubuntu系统生成core文件
ulimit -a
查看core文件有关所有信息
如果core file size为0,需要设置ulimit -c unlimited,在个人配置文件~/.bashrc或者/etc/profile中添加才可以永久生效,否则仅在当前窗口生效
2.设置core文件生成路径
echo “1” > /proc/sys/kernel/core_uses_pid
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博客
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)