本文讲述了实现core文件自动生成的配置方法,具体执行步骤如下:
1.编辑环境配置文件,让shell启动时自动设置ulimit
登陆 LINUX 服务器,任意位置键入 echo"ulimit -c 1024" >> /etc/profile 退出 LINUX 重新登陆 LINUX
键入 ulimit -c, 如果显示 1024 那么说明 coredump 已经被开启。
1024 制产生的 core 文件限的大小不能超过 1024kb,可以使用参数unlimited,取消该限制 ulimit -c unlimited
vi /etc/profile
ulimit -c unlimited > /dev/null2>&1
2.更改core文件生成路径
/proc/sys/kernel/core_uses_pid可以控制产生的 core文件的文件名中是否添加 pid 作为扩展 ,如果添加则文件内容为 1 ,否则为 0
/proc/sys/kernel/core_pattern可以设置格式化的 core文件保存位置或文件名,比如原来文件内容是 core-%e
可以这样修改 :
echo "/corefile/core-%e-%p-%t"> core_pattern
将会控制所产生的 core文件会存放到/corefile 目录下,产生的文件名为 core- 命令名 -pid- 时间戳
以下是参数列表 :
%p -insert pid into filename添加 pid
%u - insertcurrent uid into filename添加当前 uid
%g - insert current gidinto filename添加当前 gid
%s - insert signal thatcaused the coredump into the filename添加导致产生 core的信号
%t - insert UNIX time thatthe coredump occurred into filename添加 core文件生成时的 unix 时间
%h - insert hostname wherethe coredump happened into filename添加主机名
%e -insert coredumping executable name into filename添加命令名
vi /etc/sysctl.conf
kernel.core_uses_pid = 1
kernel.core_pattern=/tmp/core-%e-%p
3.sysctl配置生效
sysctl -p /etc/sysctl.conf
这样就可以实现以后发生coredump时,core文件就可以自动生成到/tmp目录下。方便调试!
调试core
gcc -o main -g a.c
gdb main /tmp/core-main-10815
参考: http://blog.youkuaiyun.com/mrjy1475726263/article/details/44116289/