看到有的文章列的原因,目前没有碰到过
为了生成coredump文件,编译选项中需要添加-g选项
添加-g后可使用gdb调试,调用栈也能显示行号
ulimit -c
ulimit -c 1024 // 或其他数值/unlimited
ulimit -c unlimited
在/etc/security/limits.conf文件中进行配置
vim /etc/security/limits.conf
配置如下字段:
| * | soft | core | unlimited |
echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
sysctl -w kernel.core_pattern=/corefile/core-%e-%p-%t
在/etc/sysctl.conf中添加配置
kernel.core_uses_pid = 1
kernel.core_pattern = /your/own/path/core_%e_%p_%t
然后执行:
sysctl -p
查看cat /proc/sys/kernel/core_pattern如果设置未生效则可在目标目录如corefile下执行sysctl -p或者重启电脑后执行sysctl -p
【参数含义】
%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 添加命令名
debug模式下使用,即编译选项加-g
gdb 可执行文件
r # 运行
bt # 查看调用栈
bt或where查看调用栈
如果是多线程可通过如下命令查看每个线程的调用栈情况
info thread # 查看所有线程
thread apply all bt # 查看所有线程的调用栈情况
按照3中的流程打开core文件设置,使程序能在coredump文件发生时记录core文件
但要防止core文件过大,需要定期清理
拿到core文件后执行如下语句复现问题
gdb executable_file core_file
【本地复现】
本地gdb executable_file长时播包测试直到问题复现
最好也设置core文件存储,否则因为其他原因导致系统死机无法查看现场
考虑到2中描述的coredump可能的原因,如果不能复现,可以做一些排查工作
valgrind -v ./executable_file然后直接查看ERROR SUMMARY
容器内启动的程序,可在容器创建时设置core文件存储,设置方式见3.3章节
通过设置Dockerfile中CMD或ENTRYPOINT命令来设置,dockerfile创建镜像命令
可设置自动设置脚本setenv.sh如下
#! /bin/bash
sysctl -w kernel.core_pattern=/corefile/core-%e-%p-%t
sysctl -p
修改文件权限
chmod 777 setenv.sh
然后Dockerfile中添加
CMD ["./setenv.sh"]
【参考文章】
如何配置生效core文件
配置生效core文件和压缩coredump文件
coredump问题排查方法
coredump常见原因1
coredump常见原因2
coredump常见原因3
coredump文件是如何产生的 (推荐)
除gdb外的其他定位coredump方法
created by shuaixio, 2022.11.20