5

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

Adrián Romero
  • 620
  • 7
  • 17
  • 1
    From here: http://stackoverflow.com/questions/318647/what-is-a-good-way-to-dump-a-linux-core-file-from-inside-a-process. It is not a system call but a library `google-coredumper` http://code.google.com/p/google-coredumper. –  May 06 '14 at 06:06

3 Answers3

5
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

coder hacker
  • 4,819
  • 1
  • 25
  • 50
  • I was just about to write a comment including `((int*)0)[0] = 42;` ... :-) – cmaster - reinstate monica May 06 '14 at 04:37
  • 2
    why not just `abort()` in the child? If it doesn't create a dump, presumably it is because `ulimit` is preventing it, and then `SIGSEGV` won't create a dump either. Besides, even if `abort()` doesn't create a dump, it also will not return. So the OR and second expression seems useless. Am I missing something? – Ben Voigt May 06 '14 at 04:48
  • @cmaster: You can lose some parentheses with `0[(int*)0]` – Ben Voigt May 06 '14 at 04:50
  • @cmaster thank you so much, now i didn't find the core dump file, i read that i could be able find it in /var/cache/abrt or /var/crash but any this folders exists in my linux i'm using Debian 7.4 – Adrián Romero May 06 '14 at 04:53
  • @Bob Try find / -name core – coder hacker May 06 '14 at 04:57
  • 1
    That will be due to the limit on the core dump files. Current systems set the default for this to zero so that no core dump is ever generated. Look here for instructions: http://askubuntu.com/questions/220905/how-to-remove-limit-on-core-dump-file-size – cmaster - reinstate monica May 06 '14 at 05:00
  • @TejasPatel i changed the core pattern with echo '/home/adiran/Documentos/coredumps/core_%e.%p' | sudo tee /proc/sys/kernel/core_pattern also run my program with ulimit -c unlimited option – Adrián Romero May 06 '14 at 05:09
  • 1
    `raise(SIGSEGV)` would be somehow nicer, wouldn't it? – alk May 06 '14 at 10:11
  • Jesus is my health insurance and & undefined behavior my exception mechanism. :) – Petr Skocik Mar 23 '17 at 10:20
  • It shouldn't even compile. `abort()` is a void, _Noreturn function. – Petr Skocik Mar 23 '17 at 10:50
1

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

raise(SIGABRT);

*this is equivalent to calling abort() directly.

Sandeep_black
  • 1,352
  • 17
  • 18
1

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.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142