1

可能的重复:
C 程序如何在不终止的情况下生成自身的核心转储?

我想从 C 程序生成一个核心而不终止正在运行的进程。操作系统是 Solaris。

我知道 gcore 可以用于此目的。但我不知道如何在 C 程序中使用“gcore”。

4

1 回答 1

1

gcore是用 C 实现的,并且源代码是可用的,因此显然可以复制它的功能。但是,查看源代码会发现它使用了半私有libproc库,其核心转储生成代码相当复杂。

gcore简单地调用usingsystem(3)或其一些变体似乎是最好的选择:

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);
}

(链接到<sys/wait.h>文档。)

于 2012-11-05T17:55:13.010 回答