使用gdb调试多线程程序
查看当前线程信息
将进程中的各个线程信息显示出来
(gdb) info threads
切换到指定进程
(gdb) thread tid
向指定的线程发送自定的指令
(gdb) thread apply tid/all args
常用的指定是查看所有线程的调用堆栈 thread apply all bt
,这个指令与 pstack
命令有些相似。
gdb默认会自动捕捉新产生线程
会在产生一个新的线程时会显示出LWP的字样提示用户,LWP = light weight process
可以设置gdb是否提示线程相关的事件
(gdb) set print thread-events on/off
(gdb) show print thread-events
为指定的线程设置断点
含有多线程的程序,可以为单独的线程设置断点
(gdb) break linespec thread tid
任何时候当你的程序在GDB模式下停止的时候,包括当前调试线程的所有线程都会停下来,不会对继续对当前进程造成更改。这时你可以在线程间进行切换,查看整个进程的执行状况。
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.
防止gdb自动切换线程
在调试gdb程序时,在单步执行时,会出现线程间跳转切换,这样对跟踪代码执行状态十分不方便。
可以通过设置 scheduler-locking
让gdb在所调试的线程中运行,防止线程的自动切换。
(gdb) set scheduler-locking step
可以执行以下命令查看当前 scheduler-locking
的设置
(gdb) show scheduler-locking
scheduler-locking
有三种模式
-
off
任何线程在任何时候都能执行
-
on
只有当前线程能够执行
-
step
为单步执行优化的模式,比较适合一般的调试
Set the scheduler locking mode. If it is off, then there is no locking and any thread may run at any time. If on, then only the current thread may run when the inferior is resumed. The step mode optimizes for single-stepping. It stops other threads from "seizing the prompt" by preempting the current thread while you are stepping. Other threads will only rarely (or never) get a chance to run when you step. They are more likely to run when you `next' over a function call, and they are completely free to run when you use commands like `continue', `until', or `finish'. However, unless another thread hits a breakpoint during its timeslice, they will never steal the GDB prompt away from the thread that you are debugging.