views:

2063

answers:

4

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.

+1  A: 

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

Also, the Sun has gcore: http://docs.sun.com/app/docs/doc/816-5165/gcore-1?a=view

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.

Chris
+2  A: 

Sun describes how to get a core file on Solaris, HP-UX, Redhat, and Winddows 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

mat_geek
+2  A: 

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 http://sourceforge.net/projects/lsstack/ 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

njsf
+20  A: 
void create_dump(void)
{
 if(!fork()) {
  // Crash the app in your favorite way here
  abort() || (*((void*)0) = 42);
 }
}

Fork the process then crash the child - it'll give you a snapshot whenever you want

Paul Betts
Ooohh, that's very nifty - I've always had to raise(6) and restart the process.
paxdiablo
This kills the parent. The child returns 0 from fork().
Martin York
Oops - thanks Martin!
Paul Betts
Smartest answer I've seen for a while!
kizzx2