0% found this document useful (0 votes)
27 views

GDB With Multiple Threads

Gdb provides facilities for debugging multi-threaded programs including automatically notifying gdb of new threads, switching between threads using the "thread" command, viewing existing threads with "info threads", and setting thread-specific breakpoints with "b sourceline thread threadno". Gdb stops and restarts all threads together to examine the overall program state. However, thread scheduling is still controlled by the operating system so other threads may execute while single-stepping the current thread.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

GDB With Multiple Threads

Gdb provides facilities for debugging multi-threaded programs including automatically notifying gdb of new threads, switching between threads using the "thread" command, viewing existing threads with "info threads", and setting thread-specific breakpoints with "b sourceline thread threadno". Gdb stops and restarts all threads together to examine the overall program state. However, thread scheduling is still controlled by the operating system so other threads may execute while single-stepping the current thread.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

I.

Gdb with multiple threads


Gdb provides these facilities for debugging multi-thread programs:
Automatic notification of new threads: For debugging purposes, gdb associates its
own thread numbera small integer assigned in thread-creation orderwith each thread
in your program. Whenever gdb detects a new thread in your program, it displays both
gdbs thread number and the target systems identification for the thread

Switching between threads: thread threadno, a command to switch among threads

Enquiring about existing threads: info threads, a command to inquire about existing
threads. The information displayed is:
1. the thread number assigned by gdb
2. the target systems thread identifier
3. the current stack frame summary for that thread
4. One thread information is preceeded by a *. This implies the current thread under
gdb

Thread-specific breakpoints: b sourceline thread threadno, a command to put a


breakpoint on a particular source statement for a specific thread given by threadno.

1. Compile the program sample_threads.c


make f make_threads
2. Run the program with gdb
gdb threads
(gdb) b main
(gdb) r
Execute the program step-by-step till both the threads are created. After that see the
information of all the threads:

Try switching the current thread which is under gdbs control, e.g. if thread 2 needs to be
made the current thread, then
(gdb) thread 2
Try putting thread specific breakpoints. E.g. threadFunction is called by both threads 2
and 3. We can put the breakpoint on this function only for thread 3 as follows:
(gdb) b threadFunction thread 3
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.
Conversely, whenever you restart the program, all threads start executing. This is true
even when single-stepping with commands like step or next. Since thread scheduling is up to
your debugging targets operating system (not controlled by gdb), other threads may execute
more than one statement while the current thread completes a single step.
Remember, gdb does cause your multi-threaded program to behave differently than it would
without gdb.
For details on using gdb with a multiple threads program, refer the following sections in the
gdb manual:
1. Section 4.9: Debugging programs with multiple threads
2. Section 5.4: Stopping and starting multi-thread programs
3. Section 5.1.2: Watchpoints in programs with multiple threads

You might also like