How to programmatically cause a core dump in C/C++

C++CLinuxCoredumpAbort

C++ Problem Overview


I would like to force a core dump at a specific location in my C++ application.

I know I can do it by doing something like:

int * crash = NULL;
*crash = 1;

But I would like to know if there is a cleaner way?

I am using Linux by the way.

C++ Solutions


Solution 1 - C++

Raising of signal number 6 (SIGABRT in Linux) is one way to do it (though keep in mind that SIGABRT is not required to be 6 in all POSIX implementations so you may want to use the SIGABRT value itself if this is anything other than quick'n'dirty debug code).

#include <signal.h>
: : :
raise (SIGABRT);

Calling abort() will also cause a core dump, and you can even do this without terminating your process by calling fork() followed by abort() in the child only - see this answer for details.

Solution 2 - C++

A few years ago, Google released the coredumper library.

> # Overview > > The coredumper library can be compiled into applications to create core dumps of the running program -- without terminating. It supports both single- and multi-threaded core dumps, even if the kernel does not natively support multi-threaded core files. > > Coredumper is distributed under the terms of the BSD License. > > # Example > > This is by no means a complete example; it simply gives you a feel for what the coredumper API looks like. > > #include > ... > WriteCoreDump('core.myprogram'); > /* Keep going, we generated a core file, > * but we didn't crash. > */

It's not what you were asking for, but maybe it's even better :)

Solution 3 - C++

As listed in the signal manpage, any signal with the action listed as 'core' will force a core dump. Some examples are:

SIGQUIT       3       Core    Quit from keyboard
SIGILL        4       Core    Illegal Instruction
SIGABRT       6       Core    Abort signal from abort(3)
SIGFPE        8       Core    Floating point exception
SIGSEGV      11       Core    Invalid memory reference

Make sure that you enable core dumps:

ulimit -c unlimited

Solution 4 - C++

#include <stdlib.h>   // C
//#include <cstdlib>  // C++

void core_dump(void)
{
    abort();
}

Solution 5 - C++

Invoke

abort();

Related, sometimes you'd like a back trace without an actual core dump, and allow the program to continue running: check out glibc backtrace() and backtrace_symbols() functions: http://www.gnu.org/s/libc/manual/html_node/Backtraces.html

Solution 6 - C++

Another way of generating a core dump:

$ bash
$ kill -s SIGSEGV $$

Just create a new instance of the bash and kill it with specified signal. The $$ is the PID of the shell. Otherwise you are killing your current bash and will be logged out, terminal closed or disconnected.

$ bash 
$ kill -s SIGABRT $$
$ bash
$ kill -s SIGFPE $$

Solution 7 - C++

You can use kill(2) to send signal.

#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);

So,

kill(getpid(), SIGSEGV);

Solution 8 - C++

Sometimes it may be appropriate to do something like this:

int st = 0;
pid_t p = fork();

if (!p) {
    signal(SIGABRT, SIG_DFL);
    abort(); // having the coredump of the exact copy of the calling thread
} else {
    waitpid(p, &st, 0); // rip the zombie
}

// here the original process continues to live

One problem with this simple approach is that only one thread will be coredumped.

Solution 9 - C++

 #include <stdio.h>
 #include <stdlib.h>
 int main()
 {
   printf("\n");
   printf("Process is aborting\n");
   abort();
   printf("Control not reaching here\n");
   return 0;
 }

use this approach wherever you want :)

Solution 10 - C++

#include <assert.h>
.
.
.
     assert(!"this should not happen");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhhafezView Question on Stackoverflow
Solution 1 - C++paxdiabloView Answer on Stackoverflow
Solution 2 - C++ephemientView Answer on Stackoverflow
Solution 3 - C++SuPraView Answer on Stackoverflow
Solution 4 - C++Jonathan LefflerView Answer on Stackoverflow
Solution 5 - C++smcameronView Answer on Stackoverflow
Solution 6 - C++ZonkView Answer on Stackoverflow
Solution 7 - C++Eugene YokotaView Answer on Stackoverflow
Solution 8 - C++rka444View Answer on Stackoverflow
Solution 9 - C++karthik339View Answer on Stackoverflow
Solution 10 - C++sigjuiceView Answer on Stackoverflow