How to: Enable/Generate/Debug Core Dump In Linux?

http://www.itsprite.com/how-to-enablegeneratedebug-core-dump-in-linux/

What is Core Dump?

If the running programs were terminated abnormal or crashed, the memory status ot the program will be recored by OS and saved in a file, this file is called “Core Dump” . We also can think of core dump as “memory snapshots”, but in fact, in addition to the memoryinformation, some crucial state of program will also be dump down, such as register information (including the program pointer, the stack pointer etc.), memory management information, other processor state. Core dump is very helpful for programmers to diagnose and debug the program, since some bugs are difficult to reproduce, such as pointer exception, and we can analyze the core dump file to reproduce that program bugs.

How to generate Core Dump?

Under what circumstances the program terminates abnormally or crashes would happen, if we use the command “kill -9” to kill a process will trigger a core dump? the experiments show that this is impossible.

Linux signal is a kind of asynchronous event handling mechanism, each signal has its default operation, you can view the signal and the default processing way in here. The default action include Ingore (ignore signal ), Stop(suspend process), Terminate (terminate process), and Core(termination and core dump) etc.. If we are using the default action, then, the following signals will produce core dump:

Signal Action comment
 SIGQUIT  Core  Quit from keyboard
 SIGILL  Core  Illegal Instruction
 SIGABRT  Core  Abort signal from abort
 SIGSEGV  Core  Invalid memory reference
 SIGTRAP  Core  Trace/breakpoint trap

Of course, not limited to the above signals. This is why we use “Ctrl+z” to suspend a process or“Ctrl+C” to terminate a process will not generate a core dump, because the former will sendSIGTSTP signal to the process, its default action is to suspend process (Stop Process); the latter will send a SIGINT signal to the process, its default action is to terminate the process (Terminate Process). As mentioned above the “kill -9″ command will issue the SIGKILL signal, which defaults to terminate the process. But if we use the “Ctrl+\” to terminate a process, the process will be issued SIGQUIT signal , it will produce a core dump. There are other situations will produce core dump, such as: program calls abort () function, memory access error, illegal instruction and so on.

Two Examples as below:

1. compare both “Ctrl+C” and “Ctrl +\” in the terminal interface

[[email protected] ~]# ulimit -c
0
[[email protected] ~]# ulimit -c unlimited
[[email protected] ~]# sleep 10
^C
[[email protected] ~]# ls
anaconda-ks.cfg install.log install.log.syslog
[[email protected] ~]# sleep 10
^\Quit (core dumped)

[[email protected] ~]# ls
anaconda-ks.cfg core.2329 install.log install.log.syslog

2. small program generate core dump

#include <stdio.h>

int main()
{
int *null_p = NULL;
*null_p = 10; //write to null pointer memory area,Segmentation fault will happen
return 0;
}
[[email protected] ~]# gcc seg.c -o seg
[[email protected] ~]# ./seg
Segmentation fault (core dumped)
[[email protected] ~]# ls
anaconda-ks.cfg core.2410 install.log install.log.syslog seg seg.c

How to enable Core Dump function in linux?

1. enable core dump

If you enter “ulimit -c” command and get the result value as 0, it indicate that core dump is disabled by default, it would not generate core dump file.

We can use the command “ulimit -c unlimited” to enable the core dump function, and does not limit the core dump file size; if you need to restrict the size of the file, change the “unlimited” as you want to generate the core file maximum size, pay attention to the unit is blocks (KB).

Using the above command will only effective for terminal current environment, if you want to be permanent, you can modify the file /etc/security/limits.conf  file.  add the below one line:

# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#&lt;domain&gt; &lt;type&gt; &lt;item&gt; &lt;value&gt;
<span style="color: #008000;">   *       soft  core    unlimited</span>

2. Change The Saving Location Of Core Dump File

  • The default generated core file is saved in the executable file’s directory, file name is core.
  • By modifying the /proc/sys/kernel/core_uses_pid file to generate core file is automatically add pid number.For example: “echo 1 > /proc/sys/kernel/core_uses_pid“, the generated core file will become core.pid, wherein PID represents the process of PID.

How to use gdb to debug Core dump file?

When core dump, use the command “GDB programname corename” to view the core file,

[[email protected] ~]# gdb seg core.2410
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
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/>...
Reading symbols from /root/seg...(no debugging symbols found)...done.
[New Thread 2410]
Missing separate debuginfo for
Try: yum --disablerepo=‘*‘ --enablerepo=‘*-debug*‘ install /usr/lib/debug/.build-id/80/1b9608daa2cd5f7035ad415e9c7dd06ebdb0a2
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Core was generated by `./seg‘.
Program terminated with signal 11, Segmentation fault.
#0 0x0000000000400484 in main ()
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64
(gdb) where
#0 0x0000000000400484 in main ()
(gdb) info frame
Stack level 0, frame at 0x7fff24e1a970:
rip = 0x400484 in main; saved rip 0x7f46a9a81d1d
时间: 2024-08-07 04:07:52

How to: Enable/Generate/Debug Core Dump In Linux?的相关文章

[skill][debug][gdb] 使用core dump 进行GDB

core dump 扫盲:https://wiki.archlinux.org/index.php/Core_dump 1.  人为制作 core dump 1.1  实时在线生成core dump.进程不会停止. [[email protected] ~]# pgrep KingKong 3850 [[email protected] ~]# gdb (gdb) attach 3850 (gdb) generate-core-file Saved corefile core.3850 (gdb

linux core dump debug

1.check core dump; ulimit -c or ulimit -a [email protected]:~$ ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 31156 max locked memory (kbyt

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

转自:http://blog.csdn.net/mrjy1475726263/article/details/44116289 源自:http://andyniu.iteye.com/blog/1965571 core dump的概念: A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has te

Segment fault及LINUX core dump详解

源自:http://andyniu.iteye.com/blog/1965571 core dump的概念: A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally (crashed). In practice, other key pieces of

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

core dump的概念: A core dump is the recorded state of the working memory of a computer program at a specific time, generally when the program has terminated abnormally (crashed). In practice, other key pieces of program state are usually dumped at the s

Linux上Core Dump文件的形成和分析

原文: http://baidutech.blog.51cto.com/4114344/904419 Core,又称之为Core Dump文件,是Unix/Linux操作系统的一种机制,对于线上服务而言,Core令人闻之色变,因为出Core的过程意味着服务暂时不能正常响应,需要恢复,并且随着吐Core进程的内存空间越大,此过程可能持续很长一段时间(例如当进程占用60G+以上内存时,完整Core文件需要15分钟才能完全写到磁盘上),这期间产生的流量损失,不可估量. 凡事皆有两面性,OS在出Core

记一次Java Core Dump分析过程

#背景提要 很久没有亲自动手部署代码了,命令行的亲切感越来越低.放飞了键盘,习惯了鼠标操作的windows环境.冷不丁实操部署也是不错的. 常常在部署时,运维同学对于[hs_err_pid]文件视而不见.殊不知这是Java 虚拟机崩溃日志. #这次是如何分析问题的? 一.首先查看日志头文件 # # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0x00007f90e

关于内核转储(core dump)的设置方法

原作者:http://blog.csdn.net/wj_j2ee/article/details/7161586 1. 内核转储作用 (1) 内核转储的最大好处是能够保存问题发生时的状态. (2) 只要有可执行文件和内核转储,就可以知道进程当时的状态. (3) 只要获取内核转储,那么即使没有复现环境,也能调试. 2. 启用内核转储 1.1 查看内核转储是否有效 在终端中输入以下命令,查看内核转储是否有效. #ulimit -c 0 -c 表示内核转储文件的大小限制,现在显示为零,表示不能用. 可

什么是core dump linux下用core和gdb查询出现&quot;段错误&quot;的地方

什么是core dump   linux下用core和gdb查询出现"段错误"的地方 http://blog.chinaunix.net/uid-26833883-id-3193279.html 有些时候我们在一段C代码的时候,由于对一个非法内存进行了操作,在程序运行的过程中,出现了"段错误". 呵呵,这种问题我想很多人会经常遇到.遇到这种问题是非常无语的,只是提示了"段错误",接着什么都没 有,如果我们一味的去看代码找太疼苦了,因为我们都相信自