1. Preface
has been engaged in the development of Linux under the background, often dealing with core files. Remember when you first started working on Linux, the program suddenly collapsed and there was no log. I was overwhelmed and my colleagues asked me to look at the core, and I was asking what is core and how to look at it. Colleagues despise the eyes, I still in the eye. Later learned from the core file analysis of the reasons, through GDB to see where the program hangs, before and after the analysis of variables, to find out the cause of the problem. It was amazing how the core file was created. Does the system generate automatically, but I write an illegal program test on my Linux system without creating a core problem? What's going on here? Today, in the Ngnix source code, found in the program to set the core dump, and what is going on? How does the company find the generated core file with the process name, process ID, and time? Take these questions today to say how the core files are generated and how they are configured.
2. Basic Concepts
When the program runs abnormally terminates or crashes, the operating system records the memory state of the program at the time and saves it in a file, which is called the core dump (translated into "kernel dumps" in Chinese). We can assume that core dump is a "memory snapshot", but in fact, in addition to the memory information, there are some key program run states also dump down, such as register information (including program pointers, stack pointers, etc.), memory management information, other processor and operating system status and information. Core dump is very helpful for programmers to diagnose and debug programs, because some program errors can be difficult to reproduce, such as a pointer exception, and the core dump file reproduces the scenario when the program goes wrong.
3. Open Core Dump
It can be opened using the command Ulimit or in the program via the SETRLIMIT system call.
The program opens CORE dump and can be viewed and set by the following API Rlimit_core
#include <sys/resource.h>int getrlimit (intstruct rlimit *rlim); int setrlimit (intconststruct rlimit *rlim);
The reference program is as follows:
#include <unistd.h>#include<sys/time.h>#include<sys/resource.h>#include<stdio.h>#defineCore_size 1024 * 1024 * 500intMain () {structRlimit RLMT; if(Getrlimit (Rlimit_core, &RLMT) = =-1) { return-1; } printf ("before set rlimit CORE dump current is:%d, Max is:%d\n", (int) Rlmt.rlim_cur, (int) Rlmt.rlim_max); Rlmt.rlim_cur=(rlim_t) core_size; Rlmt.rlim_max=(rlim_t) core_size; if(Setrlimit (Rlimit_core, &RLMT) = =-1) { return-1; } if(Getrlimit (Rlimit_core, &RLMT) = =-1) { return-1; } printf ("After set rlimit CORE dump is:%d, Max is:%d\n", (int) Rlmt.rlim_cur, (int) Rlmt.rlim_max); /*test illegal memory, generate core file*/ int*ptr =NULL; *ptr =Ten; return 0;}
Execute the./main, and the resulting core file looks like this
GDB debugs The core file to see where the program hangs. After core dump, use the command gdb program core
to view the core file, where program is the executable name and core is the generated core file name.
4. References
Http://www.cnblogs.com/hazir/p/linxu_core_dump.html
Http://www.cnblogs.com/niocai/archive/2012/04/01/2428128.html
http://baidutech.blog.51cto.com/4114344/904419/
Linux under Core dump "Summary"