Stopping and starting multithreaded programs

When your program has multiple threads, you can choose whether to set breakpoints on all threads, or on a particular thread.

(See "Debugging programs with multiple threads.")

break linespec thread threadno or break linespec thread threadno if ...
The linespec specifies source lines; there are several ways of writing them, but the effect is always to specify some source line.

Use the qualifier thread threadno with a breakpoint command to specify that you want GDB to stop the program only when a particular thread reaches this breakpoint. The threadno is one of the numeric thread identifiers assigned by GDB, shown in the first column of the info threads display.

If you don't specify thread threadno when you set a breakpoint, the breakpoint applies to all threads of your program.

You can use the thread qualifier on conditional breakpoints as well; in this case, place thread threadno before the breakpoint condition, like this:

(gdb) break frik.c:13 thread 28 if bartab > lim
Note: Thread-specific breakpoints are implemented on the gdb side by stopping any thread that hits the instruction, and then determining if that thread is the desired one. This can cause threads to be reordered in scheduling lists, and may cause the desired thread to never reach the breakpoint in question.

Whenever your program stops under GDB for any reason, all threads of execution stop, not just the current thread. This lets you examine the overall state of the program, including switching between threads, without worrying that things may change underfoot.

Conversely, whenever you restart the program, all threads start executing. This is true even when single-stepping with commands like step or next, or when functions are called in a breakpoint condition.

In particular, GDB can't single-step all threads in lockstep. Since thread scheduling is up to the microkernel (not controlled by GDB), other threads may execute more than one statement while the current thread completes a single step. Moreover, in general, other threads stop in the middle of a statement, rather than at a clean statement boundary, when the program stops.

You might even find your program stopped in another thread after continuing or even single-stepping. This happens whenever some other thread runs into a breakpoint, a signal, or an exception before the first thread completes whatever you requested.