Core file generation under Linux

1 Introduction

  I have been engaged in background development under linux, and often deal with core files. I still remember that when I first started developing under linux, the program suddenly crashed without any logs. I was at a loss, my colleague asked me to look at the core, but I asked what is the core and how to look at it. I still remember the contemptuous eyes of my colleagues. Later, I learned to analyze the cause from the core file, use gdb to see where the program hangs, analyze the variables before and after, and find out the cause of the problem. At that time, I thought it was amazing, how did the core file come about? Could it be that the system will generate it automatically, but I wrote an illegal program test on my linux system, and there was no core problem? What's the matter? When I was in the source code of ngnix today, I found that core dump can be set in the program. What's the matter? It is found in the company that the generated core files all have process name, process ID, and time. How is this done? Today, with these questions, let’s talk about how the core file is generated and configured.

2. Basic concepts

   When the program terminates abnormally or crashes during the running process, the operating system will record the memory status of the program at that time and save it in a file. This behavior is called Core Dump (some Chinese translate it into "core dump"). We can think of core dump as a "memory snapshot", but in fact, in addition to memory information, some key program running states will also be dumped at the same time, 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 are difficult to reproduce, such as pointer exceptions, and core dump files can reproduce the situation when the program goes wrong.

3. Open core dump

  You can use the command ulimit to open it, or you can use the setrlimit system call to open it in the program.

Open core dump in the program, you can view and set RLIMIT_CORE through the following API

#include <sys/resource.h>

int getrlimit(int resource, struct rlimit *rlim);
int setrlimit(int resource, const struct rlimit *rlim);

The reference program is as follows:

copy code

#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#define CORE_SIZE   1024 * 1024 * 500
int main()
{
    struct rlimit 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 current is:%d, max is:%d\n", (int)rlmt.rlim_cur, (int)rlmt.rlim_max); /*Test illegal memory and generate core file* 

    / 
    int *ptr = NULL; 
    *ptr = 10; 

    return 0; 
}

copy code

Execute ./main, the generated core file is as follows

GDB debugs the core file and checks where the program hangs. After the core dump, use the command  gdb program core to view the core file, where program is the name of the executable program, and core is the name of the generated core file.

Guess you like

Origin blog.csdn.net/gp18391818575/article/details/110638725