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

How to programmatically cause a core dump in C/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.


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 <google/coredumper.h>...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 :)


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 keyboardSIGILL        4       Core    Illegal InstructionSIGABRT       6       Core    Abort signal from abort(3)SIGFPE        8       Core    Floating point exceptionSIGSEGV      11       Core    Invalid memory reference

Make sure that you enable core dumps:

ulimit -c unlimited