#include #include #include #include #include #include #include #include #include #include void start_debugger() { int pid; if ((pid = fork()) == 0) { printf("child\n"); ptrace(PTRACE_TRACEME, 0, NULL, NULL); kill(getpid(), SIGSTOP); while(1){ printf("child: should never be reached\n"); sleep(1); } } else if (pid > 0) { int status; printf("parent\n"); assert(waitpid(pid, &status, 0) == pid); assert(WIFSTOPPED(status)); assert(WSTOPSIG(status) == SIGSTOP); #ifdef ADDITIONAL_SIGSTOP // send additional SIGSTOP to work around possible PTRACE_DETACH bug assert(kill(pid, SIGSTOP) == 0); #endif assert(ptrace(PTRACE_DETACH, pid, NULL, SIGSTOP) == 0); char buf[1024]; snprintf(buf, 1024, "gdb gdb_attach %d", pid); printf("parent: cmdl: %s\n", buf); system(buf); } else { printf("parent: fork failed\n"); } } int main(int argc, char *argv[]) { start_debugger(); return 0; }