Search code examples
c++cmultithreadinggdbptrace

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.


Solution

  • 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.