1

Possible Duplicate:
How can a C program produce a core dump of itself without terminating?

I would like to generate a core from a C program without terminating the running process. OS is Solaris.

I know that gcore can be used for this purpose. But I have no idea how to use 'gcore' from a C program.

Community
  • 1
  • 1
Pratap D
  • 311
  • 3
  • 7
  • 14
  • Does this need to be done programatically? You can attach in gdb and run the command generate-core-file to get a core. – dbeer Nov 05 '12 at 17:14
  • I don't want the program to end prematurely , I want it to run ,but would like to capture core at a particular line while continuing its flow. btw, I can't use gdb too, because it occurs in a production environment. I can only change the code and copy the execs to prod m/c, run the program and check the core dump. – Pratap D Nov 05 '12 at 17:18
  • Ex: gencore() on AIX is described here at http://pic.dhe.ibm.com/infocenter/aix/v6r1/index.jsp?topic=%2Fcom.ibm.aix.basetechref%2Fdoc%2Fbasetrf1%2Fgencore.htm, I am looking for a similar way of programming on solaris.. – Pratap D Nov 05 '12 at 17:31

1 Answers1

1

gcore is implemented in C and the source is available, so it's obviously possible to replicate what it does. However, looking at the source reveals that it makes use of the semi-private libproc library, whose coredump generating code is fairly convoluted.

It seems the best option to simply invoke gcore using system(3) or some of its variants:

int gcore(pid_t pid) 
{
  char *cmd[256];
  snprintf(cmd, sizeof cmd, "gcore %ld", (long) pid);
  // returns 0 on success, -1 on system failure (errno set), or >0 on
  // gcore failure (in which case use the W* macros defined in
  // <sys/wait.h> to extract additional information from value).
  return system(cmd);
}

(Link to <sys/wait.h> docs.)

alanc
  • 4,102
  • 21
  • 24
user4815162342
  • 141,790
  • 18
  • 296
  • 355