Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continue one thread at a time when debugging a multithreaded program in GDB?

I have a program which uses two threads. I have put the break point in both the threads. While running the program under gdb I want to switch between the threads and make them run. (thread t1 is active and running and thread t2; when paused on the breakpoint. I want to stop T1 running and run the T2).

Is there any way that I can schedule the threads in gdb?

like image 303
Arpit Avatar asked Apr 15 '10 08:04

Arpit


People also ask

How do I follow a thread in GDB?

Use the "info threads" command to see the IDs of currently known threads. The GDB thread debugging facility allows you to observe all threads while your program runs--but whenever GDB takes control, one thread in particular is always the focus of debugging. This thread is called the current thread.

How do I complete a loop in GDB?

Is there a gdb command to finish a loop construct? Execute until on the last line of the loop, or until NNN where NNN is the last line of the loop. (gdb) help until Execute until the program reaches a source line greater than the current or a specified location (same args as break command) within the current frame.

Does GDB breakpoint stop all threads?

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


2 Answers

By default, GDB stops all threads when any breakpoint is hit, and resumes all threads when you issue any command (such as continue, next, step, finish, etc.) which requires that the inferior process (the one you are debugging) start to execute.

However, you can tell GDB not to do that:

(gdb) help set scheduler-locking  Set mode for locking scheduler during execution. off  == no locking (threads may preempt at any time) on   == full locking (no thread except the current thread may run) step == scheduler locked during every single-step operation.     In this mode, no other thread may run during a step command.     Other threads may run while stepping over a function call ('next'). 

So you'll want to set breakpoints, then set scheduler-locking on, then continue or finish in thread 1 (thread 2 is still stopped), then Ctrl-C to regain control of GDB, switch to thread 2, continue (thread 1 is still stopped), etc.

Beware: by setting scheduler-locking on it is very easy to cause the inferior process to self-deadlock.

like image 68
Employed Russian Avatar answered Oct 09 '22 19:10

Employed Russian


If you're using GDB 7 or later, try "non-stop mode".

http://sourceware.org/gdb/current/onlinedocs/gdb/Non_002dStop-Mode.html

The "scheduler-locking on" command previously mentioned allows you step one thread with the others stopped. Non-stop mode allows you to step one thread with the others active.

like image 35
PFee Avatar answered Oct 09 '22 17:10

PFee