Domanda

i have a question: exists any system call for generate a core dump?

I know which a core dump could be generated by a signal, but i want know if it's possible generated from system call

È stato utile?

Soluzione

void createdump(void)
{
    if(!fork()) { //child process
        // Crash the app
        abort() || (*((void*)0) = 42);
    }
}

What ever place you wan't to dump call the function. This will create a child and crash it. So you can get dump even without exiting your program

Altri suggerimenti

You can also raise the SIGABRT signal to get the core dump.

raise(SIGABRT);

*this is equivalent to calling abort() directly.

Same idea as code hacker's answer, but compilable, with error handling, and without the zombie:

#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int 
coredump(void)
{
    pid_t p,r;
    if(0>(p=fork()))
       return -1;
    if(0==p)
        abort() /*child*/;
    /*reap zombie*/
    do r=waitpid(p,0,0); while(0>r && EINTR==errno);
    if(0>r) { 
       perror("waitpid shouldn't have failed");
       abort();
    }
    return 0;
}

This still has the rather obvious deficiency in that that it won't work with multithreaded processes.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top