wait_for_proc may fail, and should simply waitpid instead of ptracing
- which means that we need to continue the process after starting it,
the same as we do when attaching
diff --git a/sysdeps/linux-gnu/trace.c b/sysdeps/linux-gnu/trace.c
index 9613271..20c42a8 100644
--- a/sysdeps/linux-gnu/trace.c
+++ b/sysdeps/linux-gnu/trace.c
@@ -108,26 +108,21 @@
}
/* There's a (hopefully) brief period of time after the child process
- * exec's when we can't trace it yet. Here we wait for kernel to
+ * forks when we can't trace it yet. Here we wait for kernel to
* prepare the process. */
-void
+int
wait_for_proc(pid_t pid)
{
- size_t i;
- for (i = 0; i < 100; ++i) {
- /* We read from memory address 0, but that shouldn't
- * be a problem: the reading will just fail. We are
- * looking for a particular reason of failure. */
- if (ptrace(PTRACE_PEEKTEXT, pid, 0, 0) != -1
- || errno != ESRCH)
- return;
-
- usleep(1000);
+ /* man ptrace: PTRACE_ATTACH attaches to the process specified
+ in pid. The child is sent a SIGSTOP, but will not
+ necessarily have stopped by the completion of this call;
+ use wait() to wait for the child to stop. */
+ if (waitpid(pid, NULL, __WALL) != pid) {
+ perror ("trace_pid: waitpid");
+ return -1;
}
- fprintf(stderr, "\
-I consistently fail to read a word from the freshly launched process.\n\
-I'll now try to proceed with tracing, but this shouldn't be happening.\n");
+ return 0;
}
int
@@ -140,16 +135,7 @@
if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0)
return -1;
- /* man ptrace: PTRACE_ATTACH attaches to the process specified
- in pid. The child is sent a SIGSTOP, but will not
- necessarily have stopped by the completion of this call;
- use wait() to wait for the child to stop. */
- if (waitpid (pid, NULL, __WALL) != pid) {
- perror ("trace_pid: waitpid");
- return -1;
- }
-
- return 0;
+ return wait_for_proc(pid);
}
void