NEW RESEARCH REPORT: Overcoming the limitations of coding agents in complex software systems

WatchPoint

Debugging multithreaded programs in GDB

How to get thread info in GDB

In this episode, our guest contributor is Satabdi Das, Software Development Engineer at AWS. Satabdi is an expert at developing and debugging complex software systems in C++. She is also a speaker at C++ community events. Over to her!

In this tutorial, I am going to show you a few GDB commands for debugging multi-threaded programs. Running GDB on C++ programs with multiple threads is difficult, but these commands are going to give you some visibility into the state of your program and help you debug multi-threaded programs.

When I run a multi-threaded program on GDB, I’d like to see the thread information. I am going to use the following commands to get the information about the existing threads:

info threads

And the output is going to look like the following:

GDB info threads listing four mat_mult threads, with thread 2 the current thread

The star to the left of thread number 2 indicates that this is the current thread. I haven’t specified any arguments to this command. So it displays information about all the threads. You can specify the list of threads that you want to see.

For example, I can get information for threads 1 and 2 using the command info threads 1 2

GDB info threads 1 2 listing only threads 1 and 2

How to switch thread in GDB

Sometimes I want to switch between threads and check what a specific thread is doing. I am going to use the command thread 1 to switch to thread number 1.

GDB thread 1 command switching to thread 1, stopped in clone

Let’s execute the next line on GDB.

GDB next command that switches to thread 3, which hits breakpoint 1 in dotProduct

Note that gdb switched to a different thread. This can be annoying when you want to trace the execution for a particular thread. You can use the command set scheduler-locking on

And now GDB is going to run only the current thread.


GDB set scheduler-locking on, so repeated next commands stay on thread 2

Setting a breakpoint in GDB

Next, I am going to set a thread-specific breakpoint. I am going to set a breakpoint on line 16 for the current thread using the command b 16 thread 4.

Since I have set the breakpoint only for this thread, GDB is not switching between threads.

GDB thread-specific breakpoint on line 16 of mat_mult.cpp, hit by thread 2

Using GDB Thread Apply All bt

The last command I am going to show you is to apply a certain command to a group of threads. For example, I want to print the backtrace of all the threads and I am going to use the command thread apply all bt

GDB thread apply all bt printing the backtrace of every thread, starting with thread 2

That’s all I wanted to share with you in this tutorial. Hope you’ve learnt something new about GDB today.

Want debugging tips directly in your inbox?

Sure, why not?

Become a C/C++ Debugging Ninja.

Get debugging tutorials straight to your inbox

Become a C/C++ Debugging Ninja.

Get debugging tutorials straight to your inbox

Become a C/C++ Debugging Ninja.

Get debugging tutorials straight to your inbox