Debugging programs with multiple threads
In Neutrino, a single program may have more than one thread of execution. Each thread has its own registers and execution stack, and perhaps private memory.
GDB provides these facilities for debugging multithreaded programs:
- thread threadno, a command to switch between threads
- info threads, a command to inquire about existing threads
- thread apply [threadno] [all] args, a command to apply a command to a list of threads
- thread-specific breakpoints
The GDB thread debugging facility lets you 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. Debugging commands show program information from the perspective of the current thread.
GDB associates its own thread number—always a single integer—with each thread in your program.
- info threads
- Display a summary of all threads currently in your
program. GDB displays for each thread (in this order):
- Thread number assigned by GDB
- Target system's thread identifier (systag)
- Current stack frame summary for that thread.
An asterisk * to the left of the GDB thread number indicates the current thread. For example:
(gdb) info threads 3 process 35 thread 27 0x34e5 in sigpause () 2 process 35 thread 23 0x34e5 in sigpause () * 1 process 35 thread 13 main (argc=1, argv=0x7ffffff8) at threadtest.c:68
- thread threadno
- Make thread number threadno the current thread. The command
argument threadno is the internal GDB thread number, as
shown in the first field of the info threads display.
GDB responds by displaying the system identifier of the thread
you selected and its current stack frame summary:
(gdb) thread 2 [Switching to process 35 thread 23] 0x34e5 in sigpause ()
- thread apply [threadno] [all] args
- The thread apply command lets you apply a command to one or more threads. Specify the numbers of the threads that you want affected with the command argument threadno. To apply a command to all threads, use thread apply all args.
Whenever GDB stops your program because of a breakpoint or a signal, it automatically selects the thread where that breakpoint or signal happened. GDB alerts you to the context switch with a message of the form [Switching to systag] to identify the thread.
See Stopping and starting multithreaded programs for more information about how GDB behaves when you stop and start programs with multiple threads.
See Setting watchpoints for information about watchpoints in programs with multiple threads.