GDB scheduler-locking 命令详解

GDB scheduler-locking 命令详解

GDB> show scheduler-locking     //显示线程的scheduler-locking状态
GDB> set scheduler-locking on    //调试加锁当前线程,停止所有其他线程

 

set scheduler-locking mode
Set the scheduler locking mode. It applies to normal execution, record mode, and replay 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 prevents other threads from preempting the current thread while you are stepping, so that the focus of debugging does not change unexpectedly. Other threads never get a chance to run when you step, 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, GDB does not change the current thread away from the thread that you are debugging. The replay mode behaves like off in record mode and like on in replay mode.

show scheduler-locking
Display the current scheduler locking mode.

By default, when you issue one of the execution commands such as continue, next or step, GDB allows only threads of the current inferior to run. For example, if GDB is attached to two inferiors, each with two threads, the continue command resumes only the two threads of the current inferior. This is useful, for example, when you debug a program that forks and you want to hold the parent stopped (so that, for instance, it doesn’t run to exit), while you debug the child. In other situations, you may not be interested in inspecting the current state of any of the processes GDB is attached to, and you may want to resume them all until some breakpoint is hit. In the latter case, you can instruct GDB to allow all threads of all the inferiors to run with the set schedule-multiple command.

set schedule-multiple
Set the mode for allowing threads of multiple processes to be resumed when an execution command is issued. When on, all threads of all processes are allowed to run. When off, only the threads of the current process are resumed. The default is off. The scheduler-locking mode takes precedence when set to on, or while you are stepping and set to step.

show schedule-multiple
Display the current mode for resuming the execution of threads of multiple processes.

 

GDB> set scheduler-locking off
GDB> show schedule
schedule-multiple  scheduler-locking  
GDB> show scheduler-locking 
Mode for locking scheduler during execution is "off".
GDB> set scheduler-locking step
GDB> show scheduler-locking 
Mode for locking scheduler during execution is "step".
GDB> set scheduler-locking on
GDB> show scheduler-locking 
Mode for locking scheduler during execution is "on".

 

GDB设置线程锁

前面提到,使用 GDB 调试多线程程序时,默认的调试模式为:一个线程暂停运行,其它线程也随即暂停;一个线程启动运行,其它线程也随即启动。要知道,这种调试机制确实能帮我们更好地监控各个线程的“一举一动”,但并非适用于所有场景。

一些场景中,我们可能只想让某一特定线程运行,其它线程仍维持暂停状态。要想达到这样的效果,就需要借助 set scheduler-locking 命令。 此命令可以帮我们将其它线程都“锁起来”,使后续执行的命令只对当前线程或者指定线程有效,而对其它线程无效。

set scheduler-locking 命令的语法格式如下:

(gdb) set scheduler-locking mode

其中,参数 mode 的值有 3 个,分别为 off、on 和 step,它们的含义分别是:

  • off:不锁定线程,任何线程都可以随时执行;

  • on:锁定线程,只有当前线程或指定线程可以运行;

  • step:当单步执行某一线程时,其它线程不会执行,同时保证在调试过程中当前线程不会发生改变。但如果该模式下执行 continue、until、finish 命令,则其它线程也会执行,并且如果某一线程执行过程遇到断点,则 GDB 调试器会将该线程作为当前线程。


举个例子:

(gdb) info threads
  Id   Target Id                                                                 Frame
  1    Thread 0x7ffff7da0740 (LWP 54279) "main.exe"    __pthread_clockjoin_ex (threadid=140737351644928, thread_return=0x0, clockid=<optimized out>, abstime=<optimized out>, block=<optimized out>) at pthread_join_common.c:145
* 2    Thread 0x7ffff7d9f700 (LWP 54283) "main.exe"     thread_job (name=0x555555556027) at main.c:6
  3    Thread 0x7ffff759e700 (LWP 54284) "main.exe"    thread_job (name=0x555555556033) at main.c:6
(gdb) set scheduler-locking on
(gdb) next
7     printf("this is %s\n",thread_name);
(gdb) info threads
  Id   Target Id                                                                 Frame
  1    Thread 0x7ffff7da0740 (LWP 54279) "main.exe"    __pthread_clockjoin_ex (threadid=140737351644928, thread_return=0x0, clockid=<optimized out>, abstime=<optimized out>, block=<optimized out>) at pthread_join_common.c:145
* 2    Thread 0x7ffff7d9f700 (LWP 54283) "main.exe"     thread_job (name=0x555555556027) at main.c:7
  3    Thread 0x7ffff759e700 (LWP 54284) "main.exe"    thread_job (name=0x555555556033) at main.c:6
(gdb)

可以看到,通过执行 set scheduler-locking on 命令,接下来的 next 命令只对当前线程(2号线程)有效,其它线程仍保持原有的暂停状态。

同时,我们可以通过执行 show scheduler-locking 命令,查看各个线程锁定的状态,例如:

(gdb) show scheduler-locking
Mode for locking scheduler during execution is "on".

显然,当前 set scheduler-locking 命令的值为 on。

posted @ 2017-10-20 11:52  SolidMango  阅读(8597)  评论(0编辑  收藏  举报