44

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?

1

3 Answers 3

58

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.

Sign up to request clarification or add additional context in comments.

Comments

13

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.

1 Comment

This seems converse to the question, but it's good to know this command exists anyway.
1

use break conditions

(gdb) break frik.c:13 thread 28 if bartab > lim

see Debugging with GDB

Edit:

(gdb) break <thread_function_entry_point> thread 2
(gdb) break <thread_function_entry_point> thread 1
(gdb) thread 1
(gdb) continue
(gdb) ... thread 1 finishes
(gdb) thread 2
(gdb) continue

You can put these commands inside a .gdbrc file.

3 Comments

My requirement is I want one thread to finish 1st then start running the 2nd one.
(gdb) break <thread_function_entry_point> thread 2 (gdb) break <thread_function_entry_point> thread 1 (gdb) thread 1 (gdb) continue (gdb) ... thread 1 finishes (gdb) thread 2 (gdb) continue
This will not work: 'continue' (by default) resumes all threads.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.