core文件

1 core文件简单介绍

 在一个程序崩溃时,一般会在指定目录下生成一个core文件,core文件是一个内存映像,同时加上调试信息

 使用gdb查看core文件可以指示出导致程序出错的代码所在的文件和行数

2 开启或关闭core文件的生成

 关闭core文件生成:ulimit -c 0

 检查core文件生成选项:ulimit -a(检查所有的用户定制,其中包括core文件),或ulimit -c只查看core文件选项,为0则关闭core文件生成

 通过系统配置文件调整core选项:

 可以在/etc/profile中添加:

#No core files by default
ulimit -S -c 0 > /dev/null2>&1

 同时可以在特定用户的配置文件总打开core文件生成选项,在~/.bash_profile加上ulimit -c unlimited让特定用户可以产生core文件

 还可以设定core文件大小,例如ulimit -c 1024会限制core文件的大小不能超过1024kb

3 设置core文件的名称和生成路径

 /proc/sys/kernel/core_use_pid的文件内容决定core文件的文件名中是否添加pid作为扩展,为1则添加,为0不添加

 /proc/sys/kernel/core_pattern可以设置格式化的core文件保存位置或文件名格式

 例如:echo "/corefile/core-%e-%p-%t" > /proc/sys/kernel/core_pattern

 即将core文件保存在/corefile文件夹下,core文件命名方式为:core-命令名-pid-时间戳

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

 4 使用core文件

 core文件需要用gdb来查看

 (1)在core文件所在目录键入gdb -c core来调试core文件,会显示生成此core文件的命令名,终止此程序的信号等

 (2)使用gdb调试程序时:gdb ./a.out

              core-file core.xxxx

原文地址:https://www.cnblogs.com/buptlyn/p/4158440.html