title: ptrace and threads
body: <p>I'm working on an linux application incorporating ptrace to observe the threads of another process. When the application I observe forks a child process this already works quite well. By calling waitpid in my application I can obtain the following signals in the observing application:</p>
  
  <ul>
  <li><code>SIGSTOP</code> form the child process</li>
  <li><code>SIGTRAP</code> from the parent</li>
  </ul>
  
  <p>To keep track of all the children I setup ptrace with <code>PTRACE_O_TRACEFORK</code>, <code>PTRACE_O_TRACEVFORK</code>, <code>PTRACE_O_TRACECLONE</code> and <code>PTRACE_O_TRACEEXIT</code>.</p>
  
  <p>While everything is working quite fine with child processes, I cannot observe the threads of the application. I get the <code>SIGTRAP</code> from the process creating the thread but I don't get any signals from the thread.</p>
  
  <p>Is there anything special with threads and <code>ptrace</code>? How does <code>strace</code> keep track of threads (I could not find any special routines dedicated to threads in the code of <code>strace</code>)?</p>
  
  <p>This is how I use <code>ptrace</code> in my application:</p>
  
  <ul>
  <li>First I attach to a process: <code>ptrace(PTRACE_ATTACH, pid, NULL, NULL);</code></li>
  <li>Then, I call <code>waitpid()</code>: <code>trace_pid = waitpid(-1, &amp;status, 0);</code></li>
  <li>Set <code>ptrace</code> options: <code>ptrace(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXIT);</code></li>
  </ul>
  
  <p>After attaching to pid I'm calling <code>waitpid()</code> in a loop and call <code>ptrace(PTRACE_SETOPTIONS...</code> for every new task reported by <code>ptrace</code>. Of course, I continue the tasks with <code>SIGCONT</code> after event handling.</p>