Monday, June 11, 2012

generate a core dump in linux

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?

Source: Tips4all


  1. 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

  2. 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.

  3. 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);
    printf("About to seg fault by assigning zero to *s\n");
    *s = 0;
    return 0;

    void init_signals(void){
    sigact.sa_handler = signal_handler;
    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){
    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);
    fprintf(stderr, buf);

    static void dumpstack(void){
    /* Got this routine from
    ** 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 */


    void cleanup(void){
    /* 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.

  4. 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.

  5. 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.