1 /* gcore - create core file of running process */
4 #include <sys/ptrace.h>
12 int main(int argc
, char *argv
[])
18 printf("usage: %s <pid>\n", argv
[0]);
24 if (ptrace(T_ATTACH
, pid
, 0, 0) != 0) {
25 perror("ptrace(T_ATTACH)");
29 if (waitpid(pid
, &status
, 0) != pid
) {
34 while (WIFSTOPPED(status
) && WSTOPSIG(status
) != SIGSTOP
) {
35 /* whatever happens here is fine */
36 ptrace(T_RESUME
, pid
, 0, WSTOPSIG(status
));
38 if (waitpid(pid
, &status
, 0) != pid
) {
44 if (!WIFSTOPPED(status
)) {
45 fprintf(stderr
, "process died while attaching\n");
49 if (ptrace(T_DUMPCORE
, pid
, 0, 0)) {
50 fprintf(stderr
, "warning, dumpcore failed (%s)\n", strerror(errno
));
53 if (ptrace(T_DETACH
, pid
, 0, 0)) {
54 fprintf(stderr
, "warning, detaching failed (%s)\n", strerror(errno
));