Experimenting with the ptrace()
system call, I am trying to trace another thread of the same process. According to the man page, both the tracer and the tracee are specific threads (not processes), so I don't see a reason why it should not work. So far, I have tried the following:
- use
PTRACE_TRACEME
from theclone()
d child: the call succeeds, but does not do what I want, probably because the parent of the to-be-traced thread is not the thread that calledclone()
- use
PTRACE_ATTACH
orPTRACE_SEIZE
from the parent thread: this always fails withEPERM
, even if the process runs as root and withprctl(PR_SET_DUMPABLE, 1)
In all cases, waitpid(-1, &status, __WALL)
fails with ECHILD
(same when passing the child pid explicitly).
What should I do to make it work?
If it is not possible at all, is it by desing or a bug in the kernel (I am using version 3.8.0). In the former case, could you point me to the right bit of the documentation?
As @mic_e pointed out, this is a known fact about the kernel - not quite a bug, but not quite correct either. See the kernel mailing list thread about it. To provide an excerpt from Linus Torvalds:
The solution is to actually start the process that is being traced in a subprocess - you'll need to make the ptracing process be the parent of the other.
Here's an outline of doing this based on another answer that I wrote: