Reputation: 1406
If I have some multithreaded process and want to trace it with gdb
using attach
command, to which thread it will connect (e.g. current running or main)? I know that I can discover it with info threads
but I want to know which thread it will choose by default.
Upvotes: 0
Views: 195
Reputation: 3075
On Linux, where thread ids exist in the same space as process ids, it appears you can run gdb -p tid
to attach to the thread with given tid
and its owning process, without knowing the pid
. Because the main thread of a process has tid == pid
, it makes sense that running gdb -p pid
connects to the main thread.
Example code that connects gdb to the currently executing thread, e.g. for generating a pretty stack trace: https://github.com/facebook/rocksdb/pull/11150
Upvotes: 0
Reputation: 2336
For Linux, all of the threads are stopped by the ptrace
command when gdb attaches.
It has been my experience that gdb defaults to the main thread for C/C++ applications. If you attach to a process and do a 'bt' it will list the stack for 'main'.
Information is available for all threads however. gdb can look at the thread(s) information in the /proc
filesystem. The proc contains information about each thread in the tasks
area. Details about the stack address is located in the stat
file as well as the maps
file. Details are also available regarding the register values for each thread.
Along the lines of your question, I've often wondered why stepping through a multithreaded application will cause gdb to jump from thread to thread. I think that gdb is still at the mercy of the kernel scheduler so that a step on a thread may lead to a different thread getting the CPU resource and a breakpoint being triggered.
Upvotes: 1