Coredump 核心转储 Permission denied & Fsync failed & Operation not permitted

核心转储

应用程序崩溃时,我们希望能够得知更多的信息以便调试和定位问题,coredump 文件就能提供上述信息,进程崩溃时,操作系统会将程序当时的内存状态记录下来。Linux 默认是关闭这个功能的,下面介绍如何打开和使用该功能。

开启 core dump

  • 设置转储文件大小
// 可通过该命令查询当前的系统限制规则
ulimit -a

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 257439
max locked memory       (kbytes, -l) 16384
max memory size         (kbytes, -m) unlimited
...

可以看到 core file size 为 0,表示 coredump 功能未开启。

ulimit -c 1024 限制 core 文件大小为 1024KB,或者 ulimit -c unlimited 设置为不受限制。

  • 设置转储文件位置和文件名格式

sudo bash -c 'echo 0 > /proc/sys/kernel/core_uses_pid'
sudo bash -c 'echo "/tmp/core_%e_%s_%u_%g_%p_%t" > /proc/sys/kernel/core_pattern'

Ubuntu 18.04 上非 root 用户必须使用上面的命令格式,其他平台未验证,否则会报各种权限异常

%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 时间
     (seconds since 0:00h, 1 Jan 1970)
%h - insert hostname where the coredump happened into filename 添加主机名
%e - insert coredumping executable name into filename 添加触发 core 文件生成的程序名

core 文件的使用

gdb execute_file coredump_file

之后就是 gdb 相关的操作,这里不展开了。

猜你喜欢

转载自blog.csdn.net/FJDJFKDJFKDJFKD/article/details/109525823