How to enable core dump in my Linux C++ program
You need to set ulimit -c
. If you have 0 for this parameter a coredump file is not created. So do this: ulimit -c unlimited
and check if everything is correct ulimit -a
. The coredump file is created when an application has done for example something inappropriate. The name of the file on my system is core.<process-pid-here>
.
How to enable program to dump core on linux?
You can either enable it for yourself by doing:
ulimit -c unlimited
If you want this to be persistent put it in ~/.profile
Alterantively, you can enable core dumps for all users on the system put the following line in /etc/sercurity/limits.conf (at least on ubuntu):
* 0 core -1
How to generate core dump file in Linux?
The generation of core dump files it is not always enabled. Try with the ulimit command.
How can a C program produce a core dump of itself without terminating?
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
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.
Change application core dump directory with c program
You can change core dump pattern globally through /proc/sys/kernel/core_pattern
.
But if you only want to change the core dump directory of one process, you can do what Apache web server does - register a signal handler that changes the current directory right before core dumping:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/resource.h>
#define COREDUMP_DIR "/tmp"
static void sig_coredump (int sig)
{
struct sigaction sa;
// Change to the directory we want the core to be dumped
chdir (COREDUMP_DIR);
// Clear the signal handler for the signal
memset (&sa, 0, sizeof (sa));
sa.sa_handler = SIG_DFL;
sigemptyset (&sa.sa_mask);
sigaction (sig, &sa, NULL);
// Send the signal again
raise (sig);
}
int main (int argc, char **argv)
{
struct sigaction sa;
// Set up the signal handler for all signals that
// can cause the core dump
memset (&sa, 0, sizeof (sa));
sa.sa_handler = sig_coredump;
sigemptyset (&sa.sa_mask);
sigaction (SIGSEGV, &sa, NULL);
sigaction (SIGBUS, &sa, NULL);
sigaction (SIGABRT, &sa, NULL);
sigaction (SIGILL, &sa, NULL);
sigaction (SIGFPE, &sa, NULL);
// Enable core dump
struct rlimit core_limit;
core_limit.rlim_cur = RLIM_INFINITY;
core_limit.rlim_max = RLIM_INFINITY;
if (setrlimit (RLIMIT_CORE, &core_limit) == -1) {
perror ("setrlimit");
}
// Trigger core dump
raise (SIGSEGV);
return 0;
}
Note that as this relies on the crashing application itself setting up and being able to run the signal handler, it can't be 100% bullet-proof - signal may be delivered before signal handler is set up or signal handling itself may get corrupted.
Core dump in Linux
The usual solution is to build with -g and to strip off the debug information before releasing the file. Look for the 'strip' command.
You keep the file with debug information and use it to debug core dumps you get from customers.
If you want to print the human readable backtrace on the users machine you'll need to distribute binaries with (some) debug information.
Look for the 'backtrace()' function in glibc.
Note that core dumps will be created (if ulimit is set appropriately) even if your binary doesn't contain debug information.
The best way to ensure the creation of a core dump is probably to execute your binary from a script which sets ulimit before running the binary.
Related Topics
Do I Need an Extern "C" Block to Include Standard Posix C Headers
Rdrand and Rdseed Intrinsics on Various Compilers
Why Does C++ Allow Variable Length Arrays That Aren't Dynamically Allocated
When to Use Shared_Ptr and When to Use Raw Pointers
C/C++ MACro/Template Blackmagic to Generate Unique Name
C++ Undefined References with Static Library
Why Does Boost.Asio Not Support an Event-Based Interface
Static Analysis Tool to Detect Abi Breaks in C++
How to Print 0X0A Instead of 0Xa Using Cout
C++ & Boost: Encode/Decode Utf-8
Any Solution to Unpack a Vector to Function Arguments in C++
C++ Using Class Method as a Function Pointer Type
Visual C++ 2008 Express Download Link Dead
How to Create Unique_Ptr That Holds an Allocated Array
Popen Simultaneous Read and Write
How to Overload Unary Minus Operator in C++
Makefile: How to Correctly Include Header File and Its Directory