Gdb调试多进程程序
Gdb调试多进程程序
程序经常使用fork/exec创建多进程程序。多进程程序有自己独立的地址空间,这是多进程调试首要注意的地方。Gdb功能强大,对调试多线程提供很多支持。
方法1:调试多进程最土的办法:attach pid
Attach是调试进程的常用办法,只要有可执行程序以及相应PID,即可工作。当然,为方便调试,可以在进程启动后,设定sleep一段时间,如30s,这样即可有充足的时间来attach。
方法2: set follow-fork-mode child + main断点
当设置set follow-fork-mode child,gdb将在fork之后直接执行子进程,知道碰到断点后停止。如何设置子进程的断点呢?在父进程中是无法知道子进程的地址空间的(只有等程序载入后方可知)。Gdb提供一个很方便的机制:main函数的断点将被子进程继承(毕竟main是任何程序的入口)。
注意:程序在main停下后,可尝试设置断点。断点是否有效,取决于gdb是否已经载入目标程序的地址空间。
方法3: set follow-fork-mode child + catch exec
Cache点是一种特殊的breakpoint。Gdb能够catch的事件很多,如throw/catch/exception/syscall/exec/fork/vfork
等。其中和多进程关系最大的就是
exec/fork
事件。
举例:
GNU gdb Fedora (6.8-27.el5) Copyright (C) 2008 Free Software Foundation, Inc. (gdb) catch exec Catchpoint 1 (exec) (gdb) set follow-fork-mode child (gdb) r -d *** Catchpoint 1 (exec'd /****/ binary), 0x0000003c68800a70 in _start () from /lib64/ld-linux-x86-64.so.2 (gdb) bt #0 0x0000003c68800a70 in _start () from /lib64/ld-linux-x86-64.so.2 #1 0x0000000000000003 in ?? () #2 0x00007fff65c6e85a in ?? () #3 0x00007fff65c6e85d in ?? () #4 0x00007fff65c6e860 in ?? () (gdb) b lib.cc:8720 No symbol table is loaded. Use the "file" command. (gdb) c Continuing (gdb) bt #0 0x0000003c68800a70 in _start () from /lib64/ld-linux-x86-64.so.2 #1 0x0000000000000002 in ?? () #2 0x00007fff1af7682a in ?? () #3 0x0000000000000000 in ?? () (gdb) b lib.cc:8720 Breakpoint 2 at 0x15f9694: file lib.cc, line 8720. (gdb) c Continuing. [Thread debugging using libthread_db enabled] [Thread 0x40861940 (LWP 12602) exited] [Switching to process 12630] 0x0000003c6980d81c in vfork () from /lib64/libpthread.so.0 Warning: Cannot insert breakpoint 2. Error accessing memory address 0x15f9694: Input/output error. (gdb) bt #0 0x0000003c6980d81c in vfork () from /lib64/libpthread.so.0 #1 0x000000000040c3fb in ?? () #2 0x00002adeab604000 in ?? () #3 0x01000000004051ef in ?? () #4 0x00007fffff4a42f0 in ?? () #5 0x686365746e6f6972 in ?? () #6 0x0000000d0000000c in ?? () #7 0x0000000b0000000a in ?? () #8 0x0000000000000000 in ?? () (gdb) delete 2 --此处当breakpoint无效时,必须删除,否则程序无法继续 (gdb) c Continuing. [New process 12630] Executing new program: /****/ binary warning: Cannot initialize thread debugging library: generic error [Switching to process 12630] Catchpoint 1 (exec'd /****/ binary), 0x0000003c68800a70 in _start () from /lib64/ld-linux-x86-64.so.2 (gdb) bt #0 0x0000003c68800a70 in _start () from /lib64/ld-linux-x86-64.so.2 #1 0x0000000000000009 in ?? () Backtrace stopped: previous frame inner to this frame (corrupt stack?) (gdb) b lib.cc:8720 Breakpoint 4 at 0x15f9694: file lib.cc, line 8720. (gdb) b type.cc:32 Breakpoint 5 at 0x1693050: file type.cc, line 32. (gdb) c Continuing. (gdb) -- 和正常程序调试一样 |
说明:
catch exec
后,程序将在
fork/vfork/exec
处停下。并非每次停下后,设置断点都是有效的。如提供断点无效,需要删除,否则程序无法继续。要能够在新进程中设置断点,一定要等到新进程的地址空间被载入后,设置断点是才有效
(exec
将改变原程序的地址空间
)
。上述例子,主要想展示如何对新进程设置断点!
注意:
1)
程序地址非常重要
(
代码和数据地址一样重要
)
。使用
gdb
时,多多注意和利用地址信息。
2)
On some systems, when a child process is spawned by vfork, you cannot debug the child or parent until an exec call completes.
方法
4
:
info inferiors/inferiors inferiors
设置
set detach-on-fork off/set follow-exec-mode new
。
If you choose to set `detach-on-fork' mode off, then gdb will retain control of all forked processes (including nested forks). You can list the forked processes under the control of gdb by using the info inferiors
command, and switch from one fork to another by using the inferior
command.
所使用的gdb不支持set detach-on-fork off/set follow-exec-mode new/info inferiors
。不清楚。
Gdb调试多进程程序的更多相关文章
- 使用 GDB 调试多进程程序
使用 GDB 调试多进程程序 GDB 是 linux 系统上常用的调试工具,本文介绍了使用 GDB 调试多进程程序的几种方法,并对各种方法进行比较. 3 评论 田 强 (tianq@cn.ibm.co ...
- 用GDB调试多进程程序
在子进程中sleep.然后attach上去. gdb --pid=123456 ps出子进程的id,gdb attach 进程号. http://www.ibm.com/developerworks/ ...
- gdb常用命令及使用gdb调试多进程多线程程序
一.常用普通调试命令 1.简单介绍GDB 介绍: gdb是Linux环境下的代码调试⼯具.使⽤:需要在源代码⽣成的时候加上 -g 选项.开始使⽤: gdb binFile退出: ctrl + d 或 ...
- gdb常用命令及gdb调试多进程/线程程序&coredump
一.常用普通调试命令 1.简单介绍GDB 介绍: gdb是Linux环境下的代码调试⼯具.使⽤:需要在源代码⽣成的时候加上 -g 选项.开始使⽤: gdb binFile退出: ctrl + d 或 ...
- gdb调试多进程多线程程序
一.调试的指令 1.list命令 list linenum 显示程序第linenum行的周围的程序 list function 显示程序名为function的函数的源程序 list 显示当前行后面的源 ...
- gdb调试多进程和多线程命令
gdb调试多进程和多线程命令 来源:http://blog.csdn.net/pbymw8iwm/article/details/7876797 1. 默认设置下,在调试多进程程序时GDB只会调试主 ...
- [转]gdb调试多进程和多线程命令
1. 默认设置下,在调试多进程程序时GDB只会调试主进程.但是GDB(>V7.0)支持多进程的分别以及同时调试,换句话说,GDB可以同时调试多个程序.只需要设置follow-fork-mode( ...
- gdb调试多进程和多线程命令(转)
1. 默认设置下,在调试多进程程序时GDB只会调试主进程.但是GDB(>V7.0)支持多进程的分别以及同时调试,换句话说,GDB可以同时调试多个程序.只需要设置follow-fork-mode( ...
- 使用gdb调试多线程程序总结
转:使用gdb调试多线程程序总结 一直对GDB多线程调试接触不多,最近因为工作有了一些接触,简单作点记录吧. 先介绍一下GDB多线程调试的基本命令. info threads 显示当前可调试的所有线程 ...
随机推荐
- Centos 6.5 安装ELK
安装java 查看java安装路径 修改环境变量 添加如下内容: export JAVA_HOME=/usr/java/jdk1.8.0_91 export PATH=$JAVA_HOME/bin:$ ...
- linux中判断一个命令是否执行成功
每一条基本命令执行后都有一个返回码,该返回码是用$?表示,执行成功的返回码是0,例如:if [ $? -ne 0 ];then 上一命令执行失败时的操作else 上一命令执行成功时的操作fi例如lin ...
- 透明ActionBar
代码方式: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSta ...
- 升级Windows 10 正式版过程记录与经验
升级Windows 10 正式版过程记录与经验 [多图预警]共50张,约4.6MB 系统概要: 预装Windows 8.1中文版 64位 C盘Users 文件夹已经挪动到D盘,并在原处建立了符号链接. ...
- Linux 下编译openjdk
操作系统ubuntu14.04 openjdk版本 7u4 openjdk7u4可以在https://jdk7.java.net/source.html下载 一.构建编译环境 sudo apt-g ...
- spring中的定时调度实现TimerFactoryBean引起的隐患
手中的一个老项目,其中使用的TimerFactoryBean实现的调度任务.一般都是spring quartz实现,这种的着实少见.正因为少见资料比较少,当初为了确认这个会不会2个调度任务同时并行执行 ...
- 《Inside UE4》目录
<Inside UE4>目录 InsideUE4 UE4无疑是非常优秀的世界上最顶尖的引擎之一,性能和效果都非常出众,编辑器工作流也非常的出色,更难得宝贵的是完全的开源让我们有机会去从中吸 ...
- Python3的decode()与encode()
python3的decode()与encode() Tags: Python Python3 对于从python2.7过来的人,对python3的感受就是python3对文本以及二进制数据做了比较清晰 ...
- usb驱动开发24之接口驱动
从第一节我们已经知道,usb_generic_driver在自己的生命线里,以一己之力将设备的各个接口送给了linux的设备模型,让usb总线的match函数,也就是usb_device_match, ...
- FineUI(专业版)实现百变通知框(无JavaScript代码)!
博客园已经越来越不公正了,居然说我这篇文章没有实质的内容!! 我其实真的想问哪些通篇几十个字,没任何代码和技术分享,嚷嚷着送书的文章的就能雄霸博客园首页几天,我这篇文章偏偏就为管理员所容不下. 其实我 ...