#include #include #include #include #include #include #include int main(void) { int status; pid_t pid; struct reg regs; struct ptrace_lwpinfo lwpinfo; if ((pid = fork()) < 0) { perror("fork"); return 1; } else if (!pid) { /* child */ if (ptrace(PT_TRACE_ME, 0, NULL, 0) < 0) { perror("PT_TRACE_ME"); _exit(1); } raise(SIGTRAP); for (;;) { getpid(); } } else { /* parent */ for (;;) { waitpid(pid, &status, 0); printf("status %x\n", status); assert(WIFSTOPPED(status)); assert(WSTOPSIG(status) == SIGTRAP); if (ptrace(PT_GETREGS, pid, ®s, 0) < 0) { perror("PT_GETREGS"); ptrace(PT_KILL, pid, NULL, 0); return (2); } printf("eip %p\n", regs.r_eip); if (ptrace(PT_STEP, pid, (caddr_t)1, 0) < 0) { perror("PT_STEP"); ptrace(PT_KILL, pid, NULL, 0); return (2); } } } }