1. 默认设置下,在调试多进程程序时GDB只会调试主进程。但是GDB(>V7.0)支持多进程的分别以及同时调试,换句话说,GDB可以同时调试多个程序。只需要设置follow-fork-mode(默认值:parent)和detach-on-fork(默认值:on)即可。

      follow-fork-mode  detach-on-fork   说明
parent                   on               只调试主进程(GDB默认)
child                     on               只调试子进程
parent                   off              同时调试两个进程,gdb跟主进程,子进程block在fork位置
child                     off              同时调试两个进程,gdb跟子进程,主进程block在fork位置

设置方法:set follow-fork-mode [parent|child]   set detach-on-fork [on|off]

查询正在调试的进程:info inferiors
   切换调试的进程: inferior <infer number>
   添加新的调试进程: add-inferior [-copies n] [-exec executable] ,可以用file executable来分配给inferior可执行文件。
   其他:remove-inferiors infno, detach inferior

2. GDB默认支持调试多线程,跟主线程,子线程block在create thread。
   查询线程:info threads
   切换调试线程:thread <thread number>

例程:

 #include <stdio.h>
#include <pthread.h> void processA();
void processB();
void * processAworker(void *arg); int main(int argc, const char *argv[])
{
int pid; pid = fork(); if(pid != )
processA();
else
processB(); return ;
} void processA()
{
pid_t pid = getpid();
char prefix[] = "ProcessA: ";
char tprefix[] = "thread ";
int tstatus;
pthread_t pt; printf("%s%lu %s\n", prefix, pid, "step1"); tstatus = pthread_create(&pt, NULL, processAworker, NULL);
if( tstatus != )
{
printf("ProcessA: Can not create new thread.");
} processAworker(NULL);
sleep();
} void * processAworker(void *arg)
{
pid_t pid = getpid();
pthread_t tid = pthread_self();
char prefix[] = "ProcessA: ";
char tprefix[] = "thread "; printf("%s%lu %s%lu %s\n", prefix, pid, tprefix, tid, "step2");
printf("%s%lu %s%lu %s\n", prefix, pid, tprefix, tid, "step3"); return NULL;
} void processB()
{
pid_t pid = getpid();
char prefix[] = "ProcessB: ";
printf("%s%lu %s\n", prefix, pid, "step1");
printf("%s%lu %s\n", prefix, pid, "step2");
printf("%s%lu %s\n", prefix, pid, "step3"); }

输出:

[cnwuwil@centos c-lab]$ ./test
ProcessA: step1
ProcessB: step1
ProcessB: step2
ProcessB: step3
ProcessA: thread step2
ProcessA: thread step3
ProcessA: thread step2
ProcessA: thread step3

调试:
1. 调试主进程,block子进程。

(gdb) set detach-on-fork off
(gdb) show detach-on-fork
Whether gdb will detach the child of a fork is off.
(gdb) catch fork
Catchpoint (fork)
(gdb) r
[Thread debugging using libthread_db enabled] Catchpoint (forked process ), 0x00110424 in __kernel_vsyscall ()
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.47.el6.i686
(gdb) break test.c:
Breakpoint at 0x8048546: file test.c, line .
(gdb) cont
[New process ]
[Thread debugging using libthread_db enabled] Breakpoint , main (argc=, argv=0xbffff364) at test.c:
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.47.el6.i686
(gdb) info inferiors
Num Description Executable
process /home/cnwuwil/labs/c-lab/test
* process /home/cnwuwil/labs/c-lab/test

2. 切换到子进程:

(gdb) inferior
[Switching to inferior [process ] (/home/cnwuwil/labs/c-lab/test)]
[Switching to thread (Thread 0xb7fe86c0 (LWP ))]
# 0x00110424 in ?? ()
(gdb) info inferiors
Num Description Executable
* process /home/cnwuwil/labs/c-lab/test
process /home/cnwuwil/labs/c-lab/test
(gdb) inferior
[Switching to inferior [process ] (/home/cnwuwil/labs/c-lab/test)]
[Switching to thread (Thread 0xb7fe86c0 (LWP ))]
# main (argc=, argv=0xbffff364) at test.c:
(gdb) info inferiors
Num Description Executable
process /home/cnwuwil/labs/c-lab/test
* process /home/cnwuwil/labs/c-lab/test

3. 设断点继续调试主进程,主进程产生两个子线程:

(gdb) break test.c:
Breakpoint at 0x804867d: file test.c, line . ( locations)
(gdb) cont
ProcessA: step1
[New Thread 0xb7fe7b70 (LWP )]
ProcessA: thread step2 Breakpoint , processAworker (arg=0x0) at test.c:
(gdb) info inferiors
Num Description Executable
process /home/cnwuwil/labs/c-lab/test
* process /home/cnwuwil/labs/c-lab/test
(gdb) info threads
Thread 0xb7fe7b70 (LWP ) 0x00110424 in __kernel_vsyscall ()
Thread 0xb7fe86c0 (LWP ) 0x00110424 in ?? ()
* Thread 0xb7fe86c0 (LWP ) processAworker (arg=0x0) at test.c:

4. 切换到主进程中的子线程,注意:线程2为前面产生的子进程

(gdb) thread
[Switching to thread (Thread 0xb7fe7b70 (LWP ))]# 0x00110424 in __kernel_vsyscall ()
(gdb) cont
ProcessA: thread step3
ProcessA: thread step2
[Switching to Thread 0xb7fe7b70 (LWP )] Breakpoint , processAworker (arg=0x0) at test.c:
(gdb) info threads
* Thread 0xb7fe7b70 (LWP ) processAworker (arg=0x0) at test.c:
Thread 0xb7fe86c0 (LWP ) 0x00110424 in ?? ()
Thread 0xb7fe86c0 (LWP ) 0x00110424 in __kernel_vsyscall ()
(gdb) thread

转自:http://blog.csdn.net/pbymw8iwm/article/details/7876797

gdb调试多进程和多线程命令(转)的更多相关文章

  1. gdb调试多进程和多线程命令

     gdb调试多进程和多线程命令 来源:http://blog.csdn.net/pbymw8iwm/article/details/7876797 1. 默认设置下,在调试多进程程序时GDB只会调试主 ...

  2. [转]gdb调试多进程和多线程命令

    1. 默认设置下,在调试多进程程序时GDB只会调试主进程.但是GDB(>V7.0)支持多进程的分别以及同时调试,换句话说,GDB可以同时调试多个程序.只需要设置follow-fork-mode( ...

  3. gdb常用命令及使用gdb调试多进程多线程程序

    一.常用普通调试命令 1.简单介绍GDB 介绍: gdb是Linux环境下的代码调试⼯具.使⽤:需要在源代码⽣成的时候加上 -g 选项.开始使⽤: gdb binFile退出: ctrl + d 或 ...

  4. gdb常用命令及gdb调试多进程/线程程序&coredump

    一.常用普通调试命令 1.简单介绍GDB 介绍: gdb是Linux环境下的代码调试⼯具.使⽤:需要在源代码⽣成的时候加上 -g 选项.开始使⽤: gdb binFile退出: ctrl + d 或 ...

  5. Gdb调试多进程程序

    Gdb调试多进程程序 程序经常使用fork/exec创建多进程程序.多进程程序有自己独立的地址空间,这是多进程调试首要注意的地方.Gdb功能强大,对调试多线程提供很多支持. 方法1:调试多进程最土的办 ...

  6. 使用 GDB 调试多进程程序

    使用 GDB 调试多进程程序 GDB 是 linux 系统上常用的调试工具,本文介绍了使用 GDB 调试多进程程序的几种方法,并对各种方法进行比较. 3 评论 田 强 (tianq@cn.ibm.co ...

  7. gdb调试多进程多线程程序

    一.调试的指令 1.list命令 list linenum 显示程序第linenum行的周围的程序 list function 显示程序名为function的函数的源程序 list 显示当前行后面的源 ...

  8. 用GDB调试多进程程序

    在子进程中sleep.然后attach上去. gdb --pid=123456 ps出子进程的id,gdb attach 进程号. http://www.ibm.com/developerworks/ ...

  9. GDB调试-从入门到实践

    你好,我是雨乐! 在上篇文章中,我们分析了线上coredump产生的原因,其中用到了coredump分析工具gdb,这几天一直有读者在问,能不能写一篇关于gdb调试方面的文章,今天借助此文,分享一些工 ...

随机推荐

  1. XAMPP配置vhosts多站点/绝对正确

    XAMPP有时候你需要一些顶级域名访问方式来访问你本地的项目也就是虚拟主机配置,这时候就需要配置虚拟主机,给你的目录绑定一个域名,实现多域名绑定访问. 在Mac 下一直使用 MAMP 搭建本地 php ...

  2. python 执行顺序

    从上往下顺序执行,定义的方法和类要写在调用之前, 如果有 if __name__ == '__main__'   改方法所在的文件作为启动文件时会被调用,如果作为模块被调用时不会被执行.

  3. (1)java版本

    jdk9新增 jshell工具, 类似python的命令行界面.

  4. 洛谷 P1177 【模板】快速排序 【快速排序/multiset排序】

    题目描述 利用快速排序算法将读入的N个数从小到大排序后输出. 快速排序是信息学竞赛的必备算法之一.对于快速排序不是很了解的同学可以自行上网查询相关资料,掌握后独立完成.(C++选手请不要试图使用STL ...

  5. [linux]压缩、解压命令

    .tar.gz 和 .tgz 解压:tar zxvf FileName.tar.gz 压缩:tar zcvf FileName.tar.gz DirName tar 解包:tar xvf FileNa ...

  6. 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)

    layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...

  7. [Codeforces 10E] Greedy Change

    Brief Introduction: 给你一些种类的硬币,用最少的硬币数表示X 求最小的使贪心算法错误的X Algorithm: 一道论文题,<A Polynomial-time Algori ...

  8. CodeForces - 981E Addition on Segments

    考虑每个点i在什么情况下会成为最大值. 当选的区间子集是 包含i的区间的一个子集的时候,i肯定会是最大值. 所以我们就可以用这种方法得到所有点的可能的最大值是多少... 也就是说,最后的局面可以仅由一 ...

  9. 【数论】【素数判定】CODEVS 2851 菜菜买气球

    素数判定模板. #include<cstdio> #include<map> using namespace std; ],ans=-,l,r,n,sum[]; bool is ...

  10. 通过python的logging模块输出日志文件

    import logging import sys #获取logger实例 logger = logging.getLogger("baseSpider") # 括号后面填运行的文 ...