The pain of multi-threaded debugging
Debuggers (such as VS2008 and older gdb) tend to support only all-stop mode, when debugging multithreaded programs, if a thread breaks on a breakpoint, your debugger will let the entire program freeze until you continue the thread and other threads in the program will continue to run. This restriction prevents the program being debugged from running as it does in the real world-when a thread breaks on a breakpoint and other threads run in parallel.
The Non-stop model introduced by GDBv7.0 makes this problem solved. In this mode,
- When one or more threads break on one breakpoint, other threads still run in parallel
- You can select a broken thread and let it continue to run
Let's imagine that with this feature
- When other threads break on a breakpoint, the timer thread in the program can run normally, thus avoiding the need to timeout
- When other threads break on a breakpoint, the watchdog thread in the program can run normally, preventing the embedded hardware from restarting when the system crashes
- You can control the order in which multiple threads run, thus reproducing the deadlock scene. Since GDB can be used to drive debugging with a Python script, it is theoretically possible to test the program automatically in a different thread run order.
Therefore, the Non-stop mode is naturally a multi-threaded debugging "must kill skill". The Linux version released after the second half of 2009 years has a GDBv7.0 version. Very curious, do not know whether the VS2010 also support similar debug mode.
1. Prerequisites
Gdb-v Viewing version information
Non-stop mode not supported until 7.0
2. Add 3 lines to ~/.gdbinit to open Non-stop mode
Set 1 setoffset non on
Then attach to the running thread
gdb thrname thrid
3. You can then break a thread in a way that other threads keep running; or run only some threads other threads interrupt
3.1 down breakpoint to a thread
b file:line thrnum
Thrnum can be viewed in gdb with the info THR command
When this thread executes to a breakpoint, the thread stops, but the other thread does not stop, and continues to execute
3.2 Switch to a thread
THR Xthrnum
Thrnum can be viewed in gdb with the info THR command
Then enter the command to continue executing this thread C
Or execute a few threads
THR apply thrNum1 thrNum2 ... continue
thrNum1 thrNum2 ... For Line program number (view with info THR command)
GDB multithreaded Debugging: Only the thread that stops the breakpoint, the other line Cheng Jinghan execution; or run only some threads other threads interrupt