GDB调试多进程|多线程程序
1. 默认设置下,在调试多进程程序时GDB只会调试主进程。但是GDB(>V7.0)支持多进程的分别以及同时调试,换句话说,GDB可以同时调试多个程序。只需要设置follow-fork-mode(默认值:parent)和detach-on-fork(默认值:on)即可。
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 <pthread.h>
void processA();
void processB();
void * processAworker(void *arg);
int main(int argc, const char *argv[])
{
int pid;
pid = fork();
if(pid != 0)
processA();
else
processB();
return 0;
}
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 != 0 )
{
printf("ProcessA: Can not create new thread.");
}
processAworker(NULL);
sleep(1);
}
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");
}
输出:
ProcessB: 803 step1
ProcessB: 803 step2
ProcessB: 803 step3
ProcessA: 802 thread 3077555904 step2
ProcessA: 802 thread 3077555904 step3
ProcessA: 802 thread 3077553008 step2
ProcessA: 802 thread 3077553008 step3
调试:
1. 调试主进程,block子进程。
(gdb) show detach-on-fork
Whether gdb will detach the child of a fork is off.
(gdb) catch fork
Catchpoint 1 (fork)
(gdb) r
[Thread debugging using libthread_db enabled]
Catchpoint 1 (forked process 3475), 0x00110424 in __kernel_vsyscall ()
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.47.el6.i686
(gdb) break test.c:14
Breakpoint 2 at 0x8048546: file test.c, line 14.
(gdb) cont
[New process 3475]
[Thread debugging using libthread_db enabled]
Breakpoint 2, main (argc=1, argv=0xbffff364) at test.c:14
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.47.el6.i686
(gdb) info inferiors
Num Description Executable
2 process 3475 /home/cnwuwil/labs/c-lab/test
* 1 process 3472 /home/cnwuwil/labs/c-lab/test
2. 切换到子进程:
[Switching to inferior 2 [process 3475] (/home/cnwuwil/labs/c-lab/test)]
[Switching to thread 2 (Thread 0xb7fe86c0 (LWP 3475))]
#0 0x00110424 in ?? ()
(gdb) info inferiors
Num Description Executable
* 2 process 3475 /home/cnwuwil/labs/c-lab/test
1 process 3472 /home/cnwuwil/labs/c-lab/test
(gdb) inferior 1
[Switching to inferior 1 [process 3472] (/home/cnwuwil/labs/c-lab/test)]
[Switching to thread 1 (Thread 0xb7fe86c0 (LWP 3472))]
#0 main (argc=1, argv=0xbffff364) at test.c:14
(gdb) info inferiors
Num Description Executable
2 process 3475 /home/cnwuwil/labs/c-lab/test
* 1 process 3472 /home/cnwuwil/labs/c-lab/test
3. 设断点继续调试主进程,主进程产生两个子线程:
Breakpoint 3 at 0x804867d: file test.c, line 50. (2 locations)
(gdb) cont
ProcessA: 3472 step1
[New Thread 0xb7fe7b70 (LWP 3562)]
ProcessA: 3472 thread 3086911168 step2
Breakpoint 3, processAworker (arg=0x0) at test.c:50
(gdb) info inferiors
Num Description Executable
2 process 3475 /home/cnwuwil/labs/c-lab/test
* 1 process 3472 /home/cnwuwil/labs/c-lab/test
(gdb) info threads
3 Thread 0xb7fe7b70 (LWP 3562) 0x00110424 in __kernel_vsyscall ()
2 Thread 0xb7fe86c0 (LWP 3475) 0x00110424 in ?? ()
* 1 Thread 0xb7fe86c0 (LWP 3472) processAworker (arg=0x0) at test.c:50
4. 切换到主进程中的子线程,注意:线程2为前面产生的子进程
[Switching to thread 3 (Thread 0xb7fe7b70 (LWP 3562))]#0 0x00110424 in __kernel_vsyscall ()
(gdb) cont
ProcessA: 3472 thread 3086911168 step3
ProcessA: 3472 thread 3086908272 step2
[Switching to Thread 0xb7fe7b70 (LWP 3562)]
Breakpoint 3, processAworker (arg=0x0) at test.c:50
(gdb) info threads
* 3 Thread 0xb7fe7b70 (LWP 3562) processAworker (arg=0x0) at test.c:50
2 Thread 0xb7fe86c0 (LWP 3475) 0x00110424 in ?? ()
1 Thread 0xb7fe86c0 (LWP 3472) 0x00110424 in __kernel_vsyscall ()
(gdb) thread 1
转载于:https://www.cnblogs.com/chezxiaoqiang/archive/2012/03/14/2674388.html
GDB调试多进程|多线程程序相关推荐
- [Linux]gdb调试多进程多线程例程
gdb相信学linux的同学已经比较熟悉了吧,它是linux下代码调试工具.我们在写c语言,c++的代码时经常会用到,它有一些常用的调试命令: run(r):运行程序,如果有断点在下一个断点处停止 s ...
- GDB 调试多进程或者多线程应用
GDB 是 linux 系统上常用的 c/c++ 调试工具, 功能十分强大. 对于较为复杂的系统, 比如多进程系统, 如何使用 GDB 调试呢? 考虑下面这个三进程系统 : 进程 ProcessChi ...
- 使用gdb调试多进程和多线程
GDB调试多进程 1. 默认设置下,在调试多进程程序时GDB只会调试主进程.但是GDB(>V7.0)支持多进程的分别以及同时调试,换句话说,GDB可以同时调试多个程序.只需要设置follow-f ...
- GDB调试多进程程序或同时调试多个程序
转自:http://hellogcc.blogbus.com/logs/71170939.html 在以前,如果在GDB中想调试多进程的程序,需要在fork以前"set follow-for ...
- 使用 GDB 调试多进程程序
使用 GDB 调试多进程程序 来源 https://www.ibm.com/developerworks/cn/linux/l-cn-gdbmp/index.html GDB 是 linux 系统上常 ...
- gdb调试分析多线程死锁
gdb调试分析多线程死锁 #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include ...
- Eclipse+CDT+GDB调试android NDK程序
http://www.cnblogs.com/shadox/archive/2011/12/02/2272564.html Eclipse+CDT+GDB调试android NDK程序 Eclipse ...
- GNU 工具链调试信息分离和挂载:支持 GDB 调试 RELEASE 版本程序
GNU 工具链调试信息分离和挂载:支持 GDB 调试 RELEASE 版本程序 一般地,release 版本的程序都不带调试信息,无法使用 gdb 调试. GNU 编译工具链和二进制工具链做法如下: ...
- gdb调试多进程和多线程命令 .
1. 默认设置下,在调试多进程程序时GDB只会调试主进程.但是GDB(>V7.0)支持多进程的 分别以及同时 调试,换句话说,GDB可以同时调试多个程序.只需要设置follow-fork-mod ...
最新文章
- 机器学习实战笔记(Python实现)-01-机器学习实战
- 温州大学《深度学习》课程课件(六、优化算法)
- linux中ssh连接慢的一种解决方法
- Redis运维和开发学习笔记-全书思维导图
- 现在好多人做 局域网聊天
- 苹果cms模板_苹果cms是什么东西?
- cad2007 钢筋符号显示为问号
- 如何在面试中脱颖而出?
- 网页播放器(CKplayer)的视频怎么下载——m3u8简单探索
- 软件质量属性:易用性
- ubntu系统克隆到其他计算机的方法
- 敏捷计划是否基于功能,是否更好?
- 南京工业大学计算机考研调剂,2020年南京工业大学考研调剂信息
- 创新科技普济天下,华为无线重构世界
- SpringBoot2——web开发(组件使用)
- 手机游戏可以投屏吗?手机游戏怎么投屏到电脑?
- Win11图标变暗怎么办?Win11图标变暗的解决方法
- Qt使用QCustomPlot画坐标图教程
- 输出100内所含5或5的倍数的值(Python)
- [入门教程](python numpy入门)
热门文章
- 不懂这11个隐藏技巧,别说你会用微信
- spring boot2.x设置session有效时间_Spring 源码解析 Scopes 之 Request 、Session 、Application...
- java 整数溢出检测,Java如何处理整数下溢和溢出以及如何检查它?
- 顺德机器人应用与维修专业收入_保定万维科技技校专业课程巡礼——计算机应用与维修...
- 移动设备web文字单位_移动设备如何塑造现代Web设计
- 如何设置ad18捕捉图标_图标设计中的像素捕捉
- 面了三次字节,他的一些感悟
- 线程间的协作(2)——生产者与消费者模式
- Kamp;R练习题6-1统计关键词出现的次数
- 得到时间字符串|程序当前的目录