debugginggdb

GDB How to generate a core dump file?


I always use GDB in an interactive session (gdb --args <program>)

Two questions:

  1. Is it possible to configure .gdbinit to automatically generate the core dump file?

  2. Related to first Q, if the program crashes is there a GDB session command to manually generate the core dump file?


Solution

  • Is it possible to configure .gdbinit to automatically generate the core dump file?

    Maybe: configure to generate the core dump when ?

    if the program crashes is there a GDB session command to manually generate the core dump file?

    (gdb) help gcore
    generate-core-file, gcore
    Save a core file with the current state of the debugged process.
    Usage: generate-core-file [FILENAME]
    Argument is optional filename.  Default filename is 'core.PROCESS_ID'.
    

    Update:

    i'd like to auto-generate the core file on receiving a SIGABRT/segmentation fault etc?

    The only way I know is to set up a signal handler for all signals of interest, set a breakpoint on the signal handler, and add commands to that breakpoint. Something along the lines of:

    struct sigaction sa{};
    sa.sa_handler = my_handler;
    sigaction(SIGSEGV, &sa, NULL);
    sigaction(SIGABRT, &sa, NULL);
    ...
    
    (gdb) break my_handler
    (gdb) commands $bpnum
    gcore
    end
    

    (Above commands should work from ~/.gdbinit as well.)