Debugging multithreaded (pthreads) applications with GDB

GDB supports debugging MT apps; almost all the usual commands work normally, just a few commands tend to be thread-specific. Here are the key ones to be aware of:

  • See all visible threads:
(gdb) info threads Id     Target  Id              Frame<thr#>  Thread  <addr> (LWP ...) in <function> [at <srcfile>]
  • Switch context to a particular thread by using the thread <thread#> command.
  • Apply a given command to all threads of the process: (gdb) thread apply all <cmd>
  • Show the stack (GDB's backtrace or bt command) of all threads (the following example output is from our earlier MT app, mt_iobuf_rfct_dbg; first, we show the threads via the thread find . command):
(gdb) thread find .Thread 1 has target name ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.