#include #include #include #include #include #include static int thread_res = 0; int test_ptrace(pid_t p) { int res; printf("thread is %d\n", getpid()); printf("%d wants to ptrace %d\n", getpid(), p); res = ptrace(PTRACE_ATTACH, p, NULL, NULL); if (res == 0) { res = ptrace(PTRACE_KILL, p, NULL, NULL); } return res; } static void * test_ptrace_start(void * args){ pid_t p = *(pid_t *)args; thread_res = test_ptrace(p); pthread_exit((void *)&thread_res); } int main(int argc, char *argv[]) { const int ptrace_from_thread = 1; pthread_t thread; int res; pid_t p = fork(); switch (p) { case -1: exit(1); break; case 0: /* Child */ printf("child is %d\n", getpid()); sleep(100); exit(0); break; default: /* Parent */ printf("parent is %d\n", getpid()); break; } if (ptrace_from_thread > 0) { pthread_create(&thread, NULL, test_ptrace_start, (void *)&p); pthread_join(thread, NULL); res = thread_res; } else { res = test_ptrace(p); } if (res == 0) { printf("ptrace of %d successful\n", p); } else { printf("ptrace of %d failed\n", p); } exit(0); }