简体   繁体   中英

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.

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.

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? we assume that when we use gdb attach , all the thread have been created.

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?

When you do attach PROCESS_PID gdb internally calls ptrace (PTRACE_ATTACH) for each thread . on Linux you can check it yourself with:

$ 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. You must see ptrace (PTRACE_ATTACH) for each thread.

The main thread TID having the same numerical value as the process PID is a historical accident of Linux systems; it isn't the case on other Unix systems.

When gdb (or any debugger) attaches to a process using ptrace, all the threads of that process are suspended.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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