设置core文件
创建:2025-04-18 22:05
更新:2025-04-18 22:06

配置操作系统使其产生core文件

首先通过ulimit命令查看一下系统是否配置支持了dump core的功能。通过ulimit -c或ulimit -a,可以查看core file大小的配置情况,如果为0,则表示系统关闭了dump core。可以通过ulimit -c unlimited来打开。若发生了段错误,但没有core dump,是由于系统禁止core文件的生成。

解决方法: $ ulimit -c unlimited (只对当前shell进程有效)
或在 ~/.bashrc 的最后加入: ulimit -c unlimited (一劳永逸)

# ulimit -c
0
$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited

设置core文件

sudo sysctl -w kernel.core_uses_pid=1

    %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 添加命令名

sudo sysctl -w kernel.core_pattern=./core.%p.%t

注意: virtualbox虚拟机挂在目录无法生成core, 可以将程序拷贝到非挂载目录运行,可生成core文件