开发者

ptrace and threads

开发者 https://www.devze.com 2023-04-01 22:26 出处:网络
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 wait

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:

To keep track of all the children I setup ptrace with PTRACE_O_TRACEFORK, PTRACE_O_TRACEVFORK, PTRACE_O_TRACECLONE and PTRACE_O_TRACEEXIT.

While everything is working quite fine with child processes, I cannot observe the threads of the application. I get the SIGTRAP from the process creating the thread but I don't get any signals from the thread.

Is there anything special with threads and ptrace? How does strace keep track of threads (I could not find any special routines dedicated to threads in the code of strace)?

This is how I use ptrace in my application:

  • First I attach to a process: ptrace(PTRACE_ATTACH, pid, NULL, NULL);
  • Then, I call waitpid(): trace_pid = waitpid(-1, &status, 0);
  • Set ptrace options: ptrace(PTRACE_SETOPTIONS, pid, NULL, PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXIT);

After attaching to pid I'm calling waitpid() in a loop and call ptrace(PTRACE_SETOPTIONS... for every new task reported by ptrace. Of course, I continue the tasks with SIGCONT after event handling.


Finally, I've found the solution myself: I got the signals from all the threads by calling

waitpid(-1, &status, __WALL)

instead of

waitpid(-1, &status, 0)
0

精彩评论

暂无评论...
验证码 换一张
取 消