How to generate a core dump in Linux on a segmentation fault?

LinuxBashUnixCoredumpTcsh

Linux Problem Overview


I have a process in Linux that's getting a segmentation fault. How can I tell it to generate a core dump when it fails?

Linux Solutions


Solution 1 - Linux

This depends on what shell you are using. If you are using bash, then the ulimit command controls several settings relating to program execution, such as whether you should dump core. If you type

ulimit -c unlimited

then that will tell bash that its programs can dump cores of any size. You can specify a size such as 52M instead of unlimited if you want, but in practice this shouldn't be necessary since the size of core files will probably never be an issue for you.

In tcsh, you'd type

limit coredumpsize unlimited

Solution 2 - Linux

As explained above the real question being asked here is how to enable core dumps on a system where they are not enabled. That question is answered here.

If you've come here hoping to learn how to generate a core dump for a hung process, the answer is

gcore <pid>

if gcore is not available on your system then

kill -ABRT <pid>

Don't use kill -SEGV as that will often invoke a signal handler making it harder to diagnose the stuck process

Solution 3 - Linux

To check where the core dumps are generated, run:

sysctl kernel.core_pattern

or:

cat /proc/sys/kernel/core_pattern

where %e is the process name and %t the system time. You can change it in /etc/sysctl.conf and reloading by sysctl -p.

If the core files are not generated (test it by: sleep 10 & and killall -SIGSEGV sleep), check the limits by: ulimit -a.

If your core file size is limited, run:

ulimit -c unlimited

to make it unlimited.

Then test again, if the core dumping is successful, you will see “(core dumped)” after the segmentation fault indication as below:

> Segmentation fault: 11 (core dumped)

See also: <https://stackoverflow.com/q/2065912/55075>


Ubuntu

In Ubuntu the core dumps are handled by Apport and can be located in /var/crash/. However, it is disabled by default in stable releases.

For more details, please check: Where do I find the core dump in Ubuntu?.

macOS

For macOS, see: <https://stackoverflow.com/q/9412156/55075>

Solution 4 - Linux

What I did at the end was attach gdb to the process before it crashed, and then when it got the segfault I executed the generate-core-file command. That forced generation of a core dump.

Solution 5 - Linux

Maybe you could do it this way, this program is a demonstration of how to trap a segmentation fault and shells out to a debugger (this is the original code used under AIX) and prints the stack trace up to the point of a segmentation fault. You will need to change the sprintf variable to use gdb in the case of Linux.

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <stdarg.h>
 
static void signal_handler(int);
static void dumpstack(void);
static void cleanup(void);
void init_signals(void);
void panic(const char *, ...);
 
struct sigaction sigact;
char *progname;
 
int main(int argc, char **argv) {
    char *s;
    progname = *(argv);
    atexit(cleanup);
    init_signals();
    printf("About to seg fault by assigning zero to *s\n");
    *s = 0;
    sigemptyset(&sigact.sa_mask);
    return 0;
}
 
void init_signals(void) {
    sigact.sa_handler = signal_handler;
    sigemptyset(&sigact.sa_mask);
    sigact.sa_flags = 0;
    sigaction(SIGINT, &sigact, (struct sigaction *)NULL);
 
    sigaddset(&sigact.sa_mask, SIGSEGV);
    sigaction(SIGSEGV, &sigact, (struct sigaction *)NULL);
 
    sigaddset(&sigact.sa_mask, SIGBUS);
    sigaction(SIGBUS, &sigact, (struct sigaction *)NULL);
 
    sigaddset(&sigact.sa_mask, SIGQUIT);
    sigaction(SIGQUIT, &sigact, (struct sigaction *)NULL);
 
    sigaddset(&sigact.sa_mask, SIGHUP);
    sigaction(SIGHUP, &sigact, (struct sigaction *)NULL);
 
    sigaddset(&sigact.sa_mask, SIGKILL);
    sigaction(SIGKILL, &sigact, (struct sigaction *)NULL);
}
 
static void signal_handler(int sig) {
    if (sig == SIGHUP) panic("FATAL: Program hanged up\n");
    if (sig == SIGSEGV || sig == SIGBUS){
        dumpstack();
        panic("FATAL: %s Fault. Logged StackTrace\n", (sig == SIGSEGV) ? "Segmentation" : ((sig == SIGBUS) ? "Bus" : "Unknown"));
    }
    if (sig == SIGQUIT) panic("QUIT signal ended program\n");
    if (sig == SIGKILL) panic("KILL signal ended program\n");
    if (sig == SIGINT) ;
}
 
void panic(const char *fmt, ...) {
    char buf[50];
    va_list argptr;
    va_start(argptr, fmt);
    vsprintf(buf, fmt, argptr);
    va_end(argptr);
    fprintf(stderr, buf);
    exit(-1);
}
 
static void dumpstack(void) {
    /* Got this routine from http://www.whitefang.com/unix/faq_toc.html
    ** Section 6.5. Modified to redirect to file to prevent clutter
    */
    /* This needs to be changed... */
    char dbx[160];

    sprintf(dbx, "echo 'where\ndetach' | dbx -a %d > %s.dump", getpid(), progname);
    /* Change the dbx to gdb */

    system(dbx);
    return;
}
 
void cleanup(void) {
    sigemptyset(&sigact.sa_mask);
    /* Do any cleaning up chores here */
}

You may have to additionally add a parameter to get gdb to dump the core as shown here in this blog here.

Solution 6 - Linux

There are more things that may influence the generation of a core dump. I encountered these:

  • the directory for the dump must be writable. By default this is the current directory of the process, but that may be changed by setting /proc/sys/kernel/core_pattern.
  • in some conditions, the kernel value in /proc/sys/fs/suid_dumpable may prevent the core to be generated.

There are more situations which may prevent the generation that are described in the man page - try man core.

Solution 7 - Linux

For Ubuntu 14.04

  1. Check core dump enabled:

     ulimit -a
    
  2. One of the lines should be :

     core file size          (blocks, -c) unlimited
    
  3. If not :

gedit ~/.bashrc and add ulimit -c unlimited to end of file and save, re-run terminal.

  1. Build your application with debug information :

    In Makefile -O0 -g

  2. Run application that create core dump (core dump file with name ‘core’ should be created near application_name file):

     ./application_name
    
  3. Run under gdb:

     gdb application_name core
    

Solution 8 - Linux

In order to activate the core dump do the following:

  1. In /etc/profile comment the line:

     # ulimit -S -c 0 > /dev/null 2>&1
    
  2. In /etc/security/limits.conf comment out the line:

     *               soft    core            0
    
  3. execute the cmd limit coredumpsize unlimited and check it with cmd limit:

     # limit coredumpsize unlimited
     # limit
     cputime      unlimited
     filesize     unlimited
     datasize     unlimited
     stacksize    10240 kbytes
     coredumpsize unlimited
     memoryuse    unlimited
     vmemoryuse   unlimited
     descriptors  1024
     memorylocked 32 kbytes
     maxproc      528383
     #
    
  4. to check if the corefile gets written you can kill the relating process with cmd kill -s SEGV <PID> (should not be needed, just in case no core file gets written this can be used as a check):

     # kill -s SEGV <PID>
    

Once the corefile has been written make sure to deactivate the coredump settings again in the relating files (1./2./3.) !

Solution 9 - Linux

Ubuntu 19.04

All other answers themselves didn't help me. But the following sum up did the job

Create ~/.config/apport/settings with the following content:

[main]
unpackaged=true

(This tells apport to also write core dumps for custom apps)

check: ulimit -c. If it outputs 0, fix it with

ulimit -c unlimited

Just for in case restart apport:

sudo systemctl restart apport

Crash files are now written in /var/crash/. But you cannot use them with gdb. To use them with gdb, use

apport-unpack <location_of_report> <target_directory>

Further information:

  • Some answers suggest changing core_pattern. Be aware, that that file might get overwritten by the apport service on restarting.
  • Simply stopping apport did not do the job
  • The ulimit -c value might get changed automatically while you're trying other answers of the web. Be sure to check it regularly during setting up your core dump creation.

References:

Solution 10 - Linux

By default you will get a core file. Check to see that the current directory of the process is writable, or no core file will be created.

Solution 11 - Linux

Better to turn on core dump programmatically using system call setrlimit.

example:

#include <sys/resource.h>

bool enable_core_dump(){    
    struct rlimit corelim;

    corelim.rlim_cur = RLIM_INFINITY;
    corelim.rlim_max = RLIM_INFINITY;

    return (0 == setrlimit(RLIMIT_CORE, &corelim));
}

Solution 12 - Linux

It's worth mentioning that if you have a systemd set up, then things are a little bit different. The set up typically would have the core files be piped, by means of core_pattern sysctl value, through systemd-coredump(8). The core file size rlimit would typically be configured as "unlimited" already.

It is then possible to retrieve the core dumps using coredumpctl(1).

The storage of core dumps, etc. is configured by coredump.conf(5). There are examples of how to get the core files in the coredumpctl man page, but in short, it would look like this:

Find the core file:

[vps@phoenix]~$ coredumpctl list test_me | tail -1
Sun 2019-01-20 11:17:33 CET   16163  1224  1224  11 present /home/vps/test_me

Get the core file:

[vps@phoenix]~$ coredumpctl -o test_me.core dump 16163

Solution 13 - Linux

This is typically sufficient:

ulimit -c unlimited

Note this will not persist between ssh sections! To add persistence:

echo '* soft core unlimited' >> /etc/security/limits.conf

Now, if you're using Ubuntu, "apport" is probably running. Here's how to check:

sudo systemctl status apport.service

If it is, you'll probably find core dumps in one of these places:

/var/lib/apport/coredump 
/var/crash

If you want to change the location of core dumps

Make sure that you have the permissions to create files and the directory exists in the directory you're sending a core dump to!

Here's an example. Note this will not persist across reboots:

sysctl -w kernel.core_pattern=/coredumps/core-%e-%s-%u-%g-%p-%t
mkdir /coredumps

Make sure that the process that's crashing has access to write to this. The easiest way would be an example like this:

chmod 777 /coredumps

Test that core dumps works

> crash.c
gcc -Wl,--defsym=main=0 crash.c
./a.out
==output== Segmentation fault (core dumped)

If it doesn't say "core dumped" above, something isn't working.

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
QuestionNathan FellmanView Question on Stackoverflow
Solution 1 - LinuxEli CourtwrightView Answer on Stackoverflow
Solution 2 - LinuxGeorge CoView Answer on Stackoverflow
Solution 3 - LinuxkenorbView Answer on Stackoverflow
Solution 4 - LinuxNathan FellmanView Answer on Stackoverflow
Solution 5 - Linuxt0mm13bView Answer on Stackoverflow
Solution 6 - LinuxmlutescuView Answer on Stackoverflow
Solution 7 - LinuxmrgloomView Answer on Stackoverflow
Solution 8 - LinuxEdgar JordiView Answer on Stackoverflow
Solution 9 - LinuxDarkTrickView Answer on Stackoverflow
Solution 10 - LinuxMark HarrisonView Answer on Stackoverflow
Solution 11 - LinuxkgbookView Answer on Stackoverflow
Solution 12 - LinuxPawel VeselovView Answer on Stackoverflow
Solution 13 - LinuxtheicfireView Answer on Stackoverflow