作者:破砂锅  (转)

开源的GDB被广泛使用在Linux、OSX、Unix和各种嵌入式系统(例如手机),这次它又带给我们一个惊喜。

多线程调试之痛

调试器(如VS2008和老版GDB)往往只支持all-stop模式,调试多线程程序时,如果某个线程断在一个断点上,你的调试器会让整个程序freeze,直到你continue这个线程,程序中的其他线程才会继续运行。这个限制使得被调试的程序不能够像真实环境中那样运行--当某个线程断在一个断点上,让其他线程并行运行。

GDBv7.0引入的non-stop模式使得这个问题迎刃而解。在这个模式下,

  • 当某个或多个线程断在一个断点上,其他线程仍会并行运行
  • 你可以选择某个被断的线程,并让它继续运行

让我们想象一下,有了这个功能后

  • 当其他线程断在断点上时,程序里的定时器线程可以正常的运行了,从而避免不必要得超时
  • 当其他线程断在断点上时,程序里的watchdog线程可以正常的运行了,从而避免嵌入式硬件以为系统崩溃而重启
  • 可以控制多个线程运行的顺序,从而重现deadlock场景了。由于GDB可以用python脚本驱动调试,理论上可以对程序在不同的线程运行顺序下进行自动化测试。

因此,non-stop模式理所当然成为多线程调试“必杀技”。这2009年下半年之后发布的Linux版本里都带有GDBv7.0之后的版本。很好奇,不知道VS2010里是不是也支持类似的调试模式了。

演示GDB的non-stop模式

让破砂锅用一个C++小程序在Ubuntu Linux  09.10下demo这个必杀技。虽然我的demo使用命令行版gdb,如果你喜欢图形化的调试器,Eclipse2009年5月之后的版本可以轻松的调 用这个功能,详情参见Eclipse参见http://live.eclipse.org/node/723

1. 编译以下程序nonstop


 1 // gdb non-stop mode demo  2 // build instruction: g++ -g -o nonstop nonstop.cpp -lboost_thread  3   4 #include <iostream>  5 #include <boost/thread/thread.hpp>  6   7 struct op  8 {  9         op(int id): m_id(id) {} 10  11         void operator()() 12         { 13                 std::cout << m_id << " begin" << std::endl; 14                 std::cout << m_id << " end" << std::endl; 15         } 16  17         int m_id; 18 }; 19  20 int main(int argc, char ** argv) 21 { 22         boost::thread t1(op(1)), t2(op(2)), t3(op(3)); 23         t1.join(); t2.join(); t3.join(); 24         return 0; 25 } 26 

2. 把一下3行添加到~/.gdbinit来打开non-stop模式

set target-async 1 set pagination off set non-stop on

3. 启动gdb,设断点,运行.可以看到主线程1是running,3个子线程都断在断点上,而不是只有一个子线程断在断点上.


~/devroot/nonstop$ gdb ./nonstop GNU gdb (GDB) 7.0-ubuntu Reading symbols from /home/frankwu/devroot/nonstop/nonstop...done. (gdb) break 14 Breakpoint 1 at 0x402058: file nonstop.cpp, line 14. (gdb) break 24 Breakpoint 3 at 0x401805: file nonstop.cpp, line 24. (gdb) run Starting program: /home/frankwu/devroot/nonstop/nonstop [Thread debugging using libthread_db enabled] [New Thread 0x7ffff6c89910 (LWP 2762)] [New Thread 0x7ffff6488910 (LWP 2763)] 1 begin Breakpoint 1, op::operator() (this=0x605118) at nonstop.cpp:14 14                  std::cout << m_id << " end" << std::endl; 2 begin Breakpoint 1, op::operator() (this=0x605388) at nonstop.cpp:14 14                  std::cout << m_id << " end" << std::endl; [New Thread 0x7ffff5c87910 (LWP 2764)] 3 begin Breakpoint 1, op::operator() (this=0x605618) at nonstop.cpp:14 14                  std::cout << m_id << " end" << std::endl; (gdb) info threads   4 Thread 0x7ffff5c87910 (LWP 2764)  op::operator() (this=0x605618) at nonstop.cpp:14   3 Thread 0x7ffff6488910 (LWP 2763)  op::operator() (this=0x605388) at nonstop.cpp:14   2 Thread 0x7ffff6c89910 (LWP 2762)  op::operator() (this=0x605118) at nonstop.cpp:14 * 1 Thread 0x7ffff7fe3710 (LWP 2759)  (running)

4. 让线程3继续运行,注意我顾意把主线程1也continue,这是我发现的workaround,否则gdb不能切回thread 1.


(gdb) thread apply 3 1 continue
Thread 3 (Thread 0x7ffff6488910 (LWP 2763)): Continuing.
Thread 1 (Thread 0x7ffff7fe3710 (LWP 2759)): Continuing. Cannot execute this command while the selected thread is running. 2 end [Thread 0x7ffff6488910 (LWP 2763) exited]
warning: Unknown thread 3.
Thread 1 (Thread 0x7ffff7fe3710 (LWP 2759)): Continuing. Cannot execute this command while the selected thread is running. (gdb) info threads   4 Thread 0x7ffff5c87910 (LWP 2764)  op::operator() (this=0x605618) at nonstop.cpp:14   2 Thread 0x7ffff6c89910 (LWP 2762)  op::operator() (this=0x605118) at nonstop.cpp:14 * 1 Thread 0x7ffff7fe3710 (LWP 2759)  (running)

5. 让另外两个线程继续运行而结束,主线程断在第24行,最后结束.


(gdb) thread apply 4 2 1 continue
Thread 4 (Thread 0x7ffff5c87910 (LWP 2764)): Continuing.
Thread 2 (Thread 0x7ffff6c89910 (LWP 2762)): Continuing.
Thread 1 (Thread 0x7ffff7fe3710 (LWP 2759)): Continuing. Cannot execute this command while the selected thread is running. 3 end 1 end [Thread 0x7ffff5c87910 (LWP 2764) exited] [Thread 0x7ffff6c89910 (LWP 2762) exited]
Breakpoint 3, main (argc=1, argv=0x7fffffffe348) at nonstop.cpp:24 24          return 0; (gdb) continue Thread 1 (Thread 0x7ffff7fe3710 (LWP 2759)): Continuing.
Program exited normally.

参考资料

Debugging with GDB

Reverse Debugging, Multi-Process and Non-Stop Debugging Come to the CDT

多线程调试必杀技 - GDB的non-stop模式的更多相关文章

  1. GDB 多线程调试:只停止断点的线程,其他线程任然执行; 或只运行某些线程 其他线程中断

    多线程调试之痛 调试器(如VS2008和老版GDB)往往只支持all-stop模式,调试多线程程序时,如果某个线程断在一个断点上,你的调试器会让整个程序freeze,直到你continue这个线程,程 ...

  2. [转] linux下的c/c++调试器gdb

    PS:1. 断点C++类函数,用b 命名空间::类名::方法名 2. 编译参数一定要加-g,才可断点调试 http://www.cnblogs.com/xd502djj/archive/2012/08 ...

  3. linux下的c/c++调试器gdb

    Reference:  http://www.cnblogs.com/xd502djj/archive/2012/08/30/2663960.html linux下的c/c++调试器gdb gdbLi ...

  4. [skill][gdb] gdb 多线程调试

    中文快速入门: http://coolshell.cn/articles/3643.html (关于多线程的部署说的并不太对) 进阶: 多进程相关概念: inferiors 是什么? http://m ...

  5. GDB常用调试命令以及多进程多线程调试

    http://blog.csdn.net/freeelinux/article/details/53700266 一:普通命令   1.list命令 list  linenum      显示程序第l ...

  6. gdb 多线程调试

    gdb 多线程调试 http://hi.baidu.com/hcq11/blog/item/9f5bfc6e696209d680cb4a25.html  http://hi.baidu.com/lit ...

  7. GDB多线程调试

    一.多线程调试1. 多线程调试,最重要的几个命令:info threads                        查看当前进程的线程.                              ...

  8. Linux系统编程@多线程与多进程GDB调试

    博客内容参考自 http://www.cnblogs.com/xuxm2007/archive/2011/04/01/2002162.html http://blog.csdn.net/pbymw8i ...

  9. 利用GDB进行多线程调试

    一.多线程调试 多线程调试重要就是下面几个命令: info thread 查看当前进程的线程. thread <ID> 切换调试的线程为指定ID的线程. break file.c:100 ...

随机推荐

  1. PHP实现菜单无限极分类

    菜单数据 这里我们的菜单数据是临时数据, 没有从数据库中查询处理,数据基本和数据库中的的相似. 数据如下: $items = array( 1 => array('id' => 1, 'p ...

  2. set IDENTITY_INSERT on 和 off 的设置

    qlserver 批量插入记录时,对有标识列的字段要设置 set IDENTITY_INSERT 表名 on,然后再执行插入记录操作;插入完毕后恢复为 off 设置 格式:  set IDENTITY ...

  3. 2015309南皓芯实验二 Java面向对象程序设计

    一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 1.没有Linux基础的同学建议先学习< ...

  4. 计算a+b

    输入: 1 2 结果: 3 说明:只能输入数字,两数间只有一个空格 代码如下:(vc6编译) #include <stdio.h> #include <conio.h> voi ...

  5. day5模块学习--configparser模块

       使用ConfigParser模块读写ini文件(http://blog.csdn.net/linda1000/article/details/11729561) ConfigParserPyth ...

  6. 修改linux下默认的python版本

    首先在终端输入:python --verison 查看本机默认采用的python 版本 接着进入/usr/local/lib 目录查看当前系统中安装了多少个python版本 如果只有一个,就安装你需要 ...

  7. Educational Codeforces Round 45 (Rated for Div. 2) G - GCD Counting

    G - GCD Counting 思路:我猜测了一下gcd的个数不会很多,然后我就用dfs回溯的时候用map暴力合并就好啦. 终判被卡了MLE.....  需要每次清空一下子树的map... #inc ...

  8. 001 Anaconda的介绍与安装

    1.官网 www.continuum.io 2.ananconda的版本 同一个版本下对应一个python3与python2,在这里下载使用python 2.7的版本. 3.概述 Anaconda是一 ...

  9. GTK, GTK+, Qt, KDE, GNOME, Unity的区别与联系

    GTK,GTK+, Qt是图形界面开发库(GUI Toolkit),用户可以使用这些开发库编写GUI应用,其中GTK+是GTK的升级版. KDE,GNOME,Unity是linux下的桌面环境(Des ...

  10. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 C.二元-K个二元组最小值和最大-优先队列+贪心(思维)

    链接:https://ac.nowcoder.com/acm/contest/558/C来源:牛客网 小猫在研究二元组. 小猫在研究最大值. 给定N个二元组(a1,b1),(a2,b2),…,(aN, ...