linux下生成core dump文件方法及设置

当程序运行的过程中异常终止或崩溃,操作系统会将程序当时的内存状态记录下来,保存在一个文件中,这种行为就叫做Core Dump(中文有的翻译成“核心转储”)。我们可以认为 core dump是“内存快照”,但实际上,除了内存信息之外,还有些关键的程序运行状态也会同时 dump 下来,例如寄存器信息(包括程序指针、栈指针等)、内存管理信息、其他处理器和操作系统状态和信息。core dump 对于编程人员诊断和调试程序是非常有帮助的,因为对于有些程序错误是很难重现的,例如指针异常,而 core dump 文件可以再现程序出错时的情景。

 

1. 打开core开关

ulimit -c filesize
 
 1)使用ulimit -c命令可查看core文件的生成开关。若结果为0,则表示关闭了此功能,不会生成core文件。
 2)使用ulimit -c filesize命令,可以限制core文件的大小(filesize的单位为kbyte)。若ulimit -c unlimited,则表示core文件的大小不受限制。如果生成的信息超过此大小,将会被裁剪,最终生成一个不完整的core文件。在调试此core文件的时候,gdb会提示错误。
一般设置成unlimited即可。
永久设置, 修改/etc/security/limits.conf
文件:
  
#* soft core 0
修改成:
* soft core unlimited

在正式运营的时候,如果有coredump之后自动拉起的机制,比较容易发生core文件堆满磁盘的情况,需要注意。

 2. core文件的格式和路径: 修改/proc/sys/kernel/core_pattern

输入可执行文件运行命令的同一路径下。
若系统生成的core文件不带其它任何扩展名称,则全部命名为core。新的core文件生成将覆盖原来的core文件。

根据情况修改如下:

echo '/data01/corefiles/core_%e_%u_%t_%s.%p' > /proc/sys/kernel/core_pattern
注意  proc  是内存文件,使用vi 是不能 编译的,所有只能使用append 的方式.

  

/proc 这个目录是虚拟在内存中的,不在硬盘保存,
proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。
它以文件系统的方式为访问系统内核数据的操作提供接口。用户和应用程序可以通过proc得到系统的信息,
并可以改变内核的某些参数。由于系统的信息,如进程,是动态改变的,所以用户或应用程序读取proc文件时,
proc文件系统是动态从系统内核读出所需信息并提交的。
各种参数说明
%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 添加命令名
永久设置, 修改/etc/sysctl.conf配置文件,添加一行:
 
kernel.core_pattern= /data01/corefiles/core_%e_%u_%t_%s.%p

然后执行sysctl -p。

 

3. core文件是否带pid: 修改/proc/sys/kernel/core_uses_pid
如果这个文件的内容被配置成1,那么即使core_pattern中没有设置%p,最后生成的core dump文件名仍会加上进程ID。
永久设置, 修改/etc/sysctl.conf配置文件,添加一行:

kernel.core_uses_pid = 1
然后执行sysctl -p。

4. 使用core文件

在core文件所在目录下键入:

gdb -c core
它会启动GNU的调试器,来调试core文件,并且会显示生成此core文件的程序名,中止此程序的信号等等
如果你已经知道是由什么程序生成此core文件的,比如MyServer崩溃了生成core.12345,那么用此指令调试:
 
gdb -c core MyServer

5. 一个小方法来测试产生core文件

kill -s SIGSEGV $$
查看生成的core文件:
[root@localhost tmp]# gdb -c core_bash_0_1498014539_11.99469
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-92.el6)
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Missing separate debuginfo for the main executable file
Try: yum --enablerepo='*-debug*' install /usr/lib/debug/.build-id/e7/124dc03566f456ba8e4a88850251818d64b178
[New Thread 99469]
Core was generated by `-bash'.
Program terminated with signal 11, Segmentation fault.
#0 0x000000317a232777 in ?? ()
查看上面倒数三行:
 
Core was generated by `-bash'.
Program terminated with signal 11, Segmentation fault.
#0 0x000000317a232777 in ?? ()
 
core文件是由bash产生的,是由于接收到信号11产生了段错误。
这里是执行上面bash命令产生的,发送的SIGSEGV信号,而该信号的作用是Invalid memory reference,内存引用异常。由于是bash引起的,所以外部表现会是当前会话中断。
 
 
 
注:
1、下面是列举的信号对应数值及含义:
       Signal     Value     Action   Comment
       ----------------------------------------------------------------------
       SIGHUP        1       Term    Hangup detected on controlling terminal
                                     or death of controlling process
       SIGINT        2       Term    Interrupt from keyboard
       SIGQUIT       3       Core    Quit from keyboard
       SIGILL        4       Core    Illegal Instruction
       SIGABRT       6       Core    Abort signal from abort(3)
       SIGFPE        8       Core    Floating point exception
       SIGKILL       9       Term    Kill signal
       SIGSEGV      11       Core    Invalid memory reference
       SIGPIPE      13       Term    Broken pipe: write to pipe with no
                                     readers
       SIGALRM      14       Term    Timer signal from alarm(2)
       SIGTERM      15       Term    Termination signal
       SIGUSR1   30,10,16    Term    User-defined signal 1
       SIGUSR2   31,12,17    Term    User-defined signal 2
       SIGCHLD   20,17,18    Ign     Child stopped or terminated
       SIGCONT   19,18,25    Cont    Continue if stopped
       SIGSTOP   17,19,23    Stop    Stop process
       SIGTSTP   18,20,24    Stop    Stop typed at tty
       SIGTTIN   21,21,26    Stop    tty input for background process
       SIGTTOU   22,22,27    Stop    tty output for background process
2、/etc/security/limits.conf文件中soft、hard的区别:
 

 
至于limits.conf文件中含义
# End of file
@users soft nofile 100001
@users hard nofile 100002
@root soft nofile 100001
@root hard nofile 100002
@users  用户组 (@+组名形式); soft/hard  (限制类型 soft 警告数 ,hard 严格限制数);nofile 限制对象(nofile 文件数,nproc的话就是进程数 );100001 ,限制值。
限制对象具体含义如下:
 
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open file descriptors
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority

参考:

http://bbs.chinaunix.net/thread-1999643-1-1.html

http://linux.vbird.org/linux_basic/0320bash.php#variable_ulimit

http://younglibin.iteye.com/blog/1890701

man 7 signal

cat /etc/security/limits.conf

原文地址:https://www.cnblogs.com/xiaotengyi/p/7059461.html