Search code examples
cdebuggingcoredump

How can a C program produce a core dump of itself without terminating?


I want a C program to produce a core dump under certain circumstances. This is a program that runs in a production environment and isn't easily stopped and restarted to adjust other kinds of debugging code. Also, since it's in a production environment, I don't want to call abort(). The issues under investigation aren't easily replicated in a non-production environment. What I'd like is for the program, when it detects certain issues, to produce a core dump on its own, preferably with enough information to rename the file, and then continue.


Solution

  • void create_dump(void)
    {
        if(!fork()) {
            // Crash the app in your favorite way here
            *((void*)0) = 42;
        }
    }
    

    Fork the process then crash the child - it'll give you a snapshot whenever you want