(转)关于Linux核心转储文件 core dump

所谓核心转储文件是内含进程终止时内存映像的一个文件。
产生条件:特定的信号会引发进程创建一个核心转储文件并终止运行。
     包括哪些特定信号,请参见http://man7.org/linux/man-pages/man7/signal.7.html
          关于core文件更详细内容http://man7.org/linux/man-pages/man5/core.5.html

接下来我们说一些关于core文件常见的情况和问题。

1.core文件的生成开关和大小限制

输入ulimit -a命令我们可以看到第一行core文件大小为0,即我们关闭了core文件产生,若不为0,就打开了呗。或ulimit -c只显示core文件大小。
        使用ulimit -c filesize命令,可以限制core文件的大小(filesize的单位为kbyte)。如果生成的信息超过此大小,将会被裁剪,最终生成一个不完整的core文件。在调试此core文件的时候,gdb会提示错误。若ulimit -c unlimited,则表示core文件的大小不受限制。
如果想让修改永久生效,则需要修改配置文件,如 .bash_profile、/etc/profile或/etc/security/limits.conf。
 
2.core文件的生成路径和名称
core文件默认认生成路径是进程的工作目录中,默认名为core。若系统生成的core文件采用默认的名core,则新的core文件生成将覆盖原来的core文件。
1)/proc/sys/kernel/core_uses_pid可以控制core文件的文件名中是否添加pid作为扩展。文件内容为1,表示添加pid作为扩展名,生成的core文件格式为core.xxxx;为0则表示生成的core文件同一命名为core。
可通过以下命令修改此文件:
echo "1" > /proc/sys/kernel/core_uses_pid
2)/proc/sys/kernel/core_pattern可以控制core文件保存位置和文件名格式。(从Linux2.6版本才有,之前版本可能没有此文件)
可通过以下命令修改此文件:
echo "/corefile/core-%e-%p-%t" >/proc/sys/kernel/core_pattern,可以将core文件统一生成到/corefile目录下,产生的文件名为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 添加命令名
注意若你指定的文件夹不存在,则core文件不会生成。

3.core文件的查看
gdb [exec file] [core file]
如:
gdb ./test test.core
在进入gdb后, 用bt命令查看backtrace以检查发生程序运行到哪里, 来定位core文件->行.
GDB中键入where,也会看到程序崩溃时堆栈信息(当前函数之前的所有已调用函数的列表(包括当前函数),gdb只显示最近几个)
咳,天下文章一大抄
参考 http://blog.chinaunix.net/uid-21411227-id-1826911.html
http://www.nginx.cn/1521.html
http://www.cnblogs.com/xuxm2007/archive/2010/10/22/1858095.html
       强制进程产生coredump,检测死锁以及进程快照   http://blog.chinaunix.net/uid-23629988-id-175809.html
---------------------
作者:二手码农
来源:CSDN
原文:https://blog.csdn.net/hu_jiacheng/article/details/40950471
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/heluan/p/10278831.html