简体   繁体   English

gdb如何附加到多线程进程

[英]how does gdb attach to multithread process

When we use gdb attach to debug a running process, we could use gdb attach pid ,if the process have two or more threads, the pid is the main thread tid. 当我们使用gdb attach调试正在运行的进程时,可以使用gdb attach pid ,如果该进程有两个或多个线程,则pid是主线程tid。

Now I want to implement a simple debugger to debug multi-thread process, but when I use my debugger to attach a multi-thread process, only the main thread suspended. 现在,我想实现一个简单的调试器来调试多线程进程,但是当我使用调试器附加多线程进程时,只有main thread挂起。

I want to know why only use the main thread tid, the gdb can attach all thread of this process, how does gdb suspend all the threads? 我想知道为什么只使用主线程tid, gdb可以附加该进程的所有线程,gdb如何挂起所有线程? we assume that when we use gdb attach , all the thread have been created. 我们假设使用gdb attach ,所有线程都已创建。

ptrace PTRACE_ATTACHSIGSTOP发送到该进程,该进程将挂起整个进程,即所有线程。

I want to know why only use the main thread tid, the gdb can attach all thread of this process, how does gdb suspend all the threads? 我想知道为什么只使用主线程tid,gdb可以附加该进程的所有线程,gdb如何挂起所有线程?

When you do attach PROCESS_PID gdb internally calls ptrace (PTRACE_ATTACH) for each thread . 当您attach PROCESS_PID gdb内部为每个线程调用ptrace(PTRACE_ATTACH)。 on Linux you can check it yourself with: 在Linux上,您可以自己检查:

$ strace -e ptrace -p GDB_PROCESS_PID

Just run a program with s few threads, run gdb and before running attach PROCESS_PID run strace in another console. 只需运行具有几个线程的程序,运行gdb,然后在另一个控制台中运行attach PROCESS_PID之前运行strace。 You must see ptrace (PTRACE_ATTACH) for each thread. 您必须为每个线程看到ptrace(PTRACE_ATTACH)。

The main thread TID having the same numerical value as the process PID is a historical accident of Linux systems; 具有与进程PID相同数值的主线程TID是Linux系统的历史事故; it isn't the case on other Unix systems. 在其他Unix系统上则不是这样。

When gdb (or any debugger) attaches to a process using ptrace, all the threads of that process are suspended. 当gdb(或任何调试器)使用ptrace附加到进程时,该进程的所有线程都将被挂起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM