32

I want a C program to produce a core dump under certain circumstances. This is a program that runs in a production environment and isn't easily stopped and restarted to adjust other kinds of debugging code. Also, since it's in a production environment, I don't want to call abort(). The issues under investigation aren't easily replicated in a non-production environment. What I'd like is for the program, when it detects certain issues, to produce a core dump on its own, preferably with enough information to rename the file, and then continue.

Joshua Swink
  • 3,380
  • 3
  • 29
  • 27

5 Answers5

61
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

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • Ooohh, that's very nifty - I've always had to raise(6) and restart the process. – paxdiablo Sep 25 '08 at 05:44
  • Can I use this method to generate a core-dump when handling a segfault signal once, but do not wish to terminate? I would like to write a full-screen program, and I'm still weighting my options, on how to handle possible errors without leaving my program, if that is possible. – Adam L. S. May 30 '12 at 22:52
  • 4
    My reading suggests that a forked child only gets the current thread, not any other threads. This means your core dump will only contain information about the current thread - not any other threads you have running. If you need all threads you might be able to use this: https://code.google.com/p/google-coredumper/ – Michael Anderson Mar 18 '13 at 07:47
  • 1
    If abort dosen't return than why or – coder hacker May 06 '14 at 04:55
  • 1
    What is the meaning of `|| (*((void*)0) = 42)` ? – hudac Jun 02 '16 at 20:39
  • @hudac It's just another (more comedic, less clear) way of crashing the process. Specifically, it causes a segmentation fault. – Phlarx Mar 24 '17 at 18:25
  • Wont forking the current process confuse other processes that depend on its service? Even if you are crashing it, a http daemon might receive requests in the child process and terminate, possibly crashing some functionality? – XChikuX Jun 04 '18 at 17:47
  • @MichaelAnderson Could you elaborate on which readings ? I am considering whether to use this technique or search further. p.s. regarding coredumper, I am concerned it was not updated in years at this moment, at least from what I could find. – Yuriy Vasylenko Feb 26 '20 at 09:24
  • @MartinYork - Ana has updated the answer, so now the code will kill the child and the parent will continue. But I guess, the parent will need to perform a [wait](https://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html) on the child, else there will be a zombie process in the system. – Pradeep Anchan Jul 01 '22 at 15:42
  • 1
    @XChikuX the child process is created with a single thread (see `man fork`), which is the one that crashes. This means that there is no time for the child process to do side effects between fork and crash – pqnet Mar 13 '23 at 16:43
7

Another way might be to use the Google Coredumper library. This creates a similar result to the fork+abort technique but plays nicer with multithreaded apps (suspends all threads for a little while before forking so that they don't make a mess in the child).

Example:

    #include <google/coredumper.h>
    ...
    WriteCoreDump('core.myprogram');
    /* Keep going, we generated a core file,
     * but we didn't crash.
     */
regnarg
  • 676
  • 8
  • 10
4

Sun describes how to get a core file on Solaris, HP-UX, Redhat, and Windows here.

Solaris has the gcore program. HP-UX may have it. Otherwise use gdb and its gcore commmand. Windows has win-dbg-root\tlist.exe and win-dbg-root\adplus.vbs

alanc
  • 4,102
  • 21
  • 24
mat_geek
  • 2,481
  • 3
  • 24
  • 29
3

Do you really want a core, or just a stacktrace ? If all you want is a stacktrace you could take a look at the opensource here and try and integrate the code from there, or maybe just calling it from the command line is enough.

I believe some code in the gdb project might also be useful.

Another think you might want to do is to use gdb to attach to a running process.

$ gdb /path/to/exec 1234 # 1234 is the pid of the running process
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
njsf
  • 2,729
  • 1
  • 21
  • 17
  • Note: the question about running gdb from the application itself: http://stackoverflow.com/questions/3151779/how-its-better-to-invoke-gdb-from-program-to-print-its-stacktrace – Vi. Apr 16 '14 at 00:00
2

The source code to produce a core dump is in 'gcore', which is part of the gdb package.

Also, the Sun has gcore.

Also, you have to have a separate process running the core dump, as the current process must be suspended. You'll find the details in the gcore source, or you can just run your platform's gcore with your process as the target.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
Chris
  • 4,852
  • 1
  • 22
  • 17