gdb調試多進程和多線程命令

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 != 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");

? }

輸出:
[cnwuwil@centos c-lab]$ ./test
ProcessA: 802 step1
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) 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 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. 切換到子進程:
(gdb) inferior 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. 設斷點繼續調試主進程,主進程產生兩個子線程:
(gdb) break test.c:50
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為前面產生的子進程
(gdb) thread 3
[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





本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445102.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445102.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445102.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python外卷(10)--取整

"取整"那些事1.python 內置函數1.1int()--向下取整1.2round()--四舍五入2.math模塊取整函數2.1 math.floor()--向下取整2.2 math.ceil()--向上取整2.3 math.modf()--分別取小數部分和整數部分3.numpy模塊取整函數3.1 numpy.floor()--向下取整3.2 numpy.ceil()--向上取…

模擬銀行家算法

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h>#define ElemType PCB #define Status int #define true 1 #define false 0 #define OK 1 #define ERROR 0 #define RESOURCE_NUM …

Lua 與 C混合編程 .

本文通過程序實例說明C調用lua腳本和lua調用C的方法: 先建立一個 test.c文件: #include <stdio.h> #include <stdlib.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" #pragma comment(lib, "lua5.1.lib&qu…

模擬固定分區分配

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PCB #define Status int…

Linux下的lua和boost c++的搭建和安裝

先下載lua &#xff0c;boost c http://www.lua.org/versions.html#5.2 http://www.boost.org/ http://sourceforge.net/projects/luabind/ 1. 安裝lua [rootlocalhost ~]#tar zxvf lua-5.1.2.tar.gz -C /usr/local [rootlocalhost ~]# cd /usr/local/ [rootlocalhost lo…

模擬基本分頁存儲

介紹 data.h #ifndef _Data_h_ #define _Data_h_#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>#define LIST_INIT_SIZE 10 #define LISTINCREMENT 2 #define true 1 #define false 0 #define PCBType PC…

常用正則表達式和shell命令列表

取當前目錄下普通文件的后綴名列表&#xff1a; ls -l | awk /^-/{print $NF} |awk -F. {print $NF}|awk !/^$/ 匹配0和正整數的正則表達式&#xff08;除0以外&#xff0c;其它數字不能以0開頭&#xff09;&#xff1a; (^0$)|(^[0-9]\d*$) 匹配中文字符的正則表達式&#xff…

無限踩坑系列(7)-Latex使用Tips

Latex常用命令1.latex注釋2.圖片左邊對齊3.字母頭上加聲調4.腳注5.公式中加空格6.字體加粗體7.公式換行8.\textsuperscript{*}9.\begin{itemize}10.\operatorname{trace}11.\noindent12.\textcircled{}圓圈數字一些TIPs1.latex注釋 單行使用百分號%注釋 多行使用如下命令,在編…

在CentOS6.2下安裝DNS服務軟件Bind并快速配置簡單實例

[實踐Ok]在CentOS6.2下安裝DNS并快速配置實例&#xff0c;共八步&#xff0c;心路歷程如下&#xff1a;背景介紹&#xff1a;在日常的開發中&#xff0c;往往會在測試機和外網的Http的Url實際接口是不一樣的&#xff0c;在測試機一個Url地址&#xff0c;在外網中又是一個地址。…

模擬動態分區分配

介紹 list.h #ifndef _List_h_ #define _List_h_#include "Data.h"//******* 鏈表 *******// Status InitLinkList(LinkList *L); void PCBAssign(PCBType *e1, PCBType e2); Status GetElemt_L(LinkList L,int i,PCBType *e); Status ListIn…

python模塊(4)-Collections

collections1.collection.counter(list)2.collections.defaultdict()3.collection.dequecollections是Python內建的一個集合模塊&#xff0c;提供了許多有用的集合類。collections在python官方文檔中的解釋是High-performance container datatypes1.collection.counter(list) …

js知識點匯總

1.本門課的作用&#xff08;JavaScript的作用&#xff09;所有基于Web的程序開發基礎 2.一種計算機客戶端腳本語言&#xff0c;主要在Web瀏覽器解釋執行。 3.瀏覽器中Javascript&#xff0c;用于與用戶交互&#xff0c;以及實現頁面中各種動態特效 4.在HTML文件中&#xff0…

redis——內存概述

Redis通過自己的方法管理內存,&#xff0c;主要方法有zmalloc(),zrealloc()&#xff0c; zcalloc()和zfree(), 分別對應C中的malloc(), realloc()、 calloc()和free()。相關代碼在zmalloc.h和zmalloc.c中。 Redis自己管理內存的好處主要有兩個&#xff1a;可以利用內存池等手段…

Windows下如何用C語言清空特定文件夾中的所有文件

#include "iostream.h" //由于該博客系統發布是不能顯示正常&#xff0c;代碼如需調試&#xff0c;只需將改成""即可 #include "string.h" #include "stdlib.h" #include "time.h" #include "math.h" #include…

MachineLearning(5)-去量綱:歸一化、標準化

去量綱&#xff1a;歸一化、標準化1.歸一化(Normalization)1.1 Min-Max Normalization1.2 非線性Normalization2.標準化(Standardlization)2.1 Z-score Normalization3.標準化在梯度下降算法中的重要性本博文為葫蘆書《百面機器學習》閱讀筆記。去量綱化 可以消除特征之間量綱的…

GDB調試技術(一)

啟動GDB的方法有以下幾種: 1、gdb <program> program也就是你的執行文件,一般在當然目錄下。 2、gdb <program> core 用gdb同時調試一個運行程序和core文件,core是程序非法執行后core dump后產生的文件。 3、

GDB調試技術(二)

1) 恢復程序運行和單步調試 當程序被停住了,你可以用continue命令恢復程序的運行直到程序結束,或下一個斷點到來。也可以使用step或next命令單步跟蹤程序。 continue [ignore-count] c [ignore-count] fg [ignore-count] 恢復程序運行,直到程序結束,或是下一個斷點到…

關于Java中String的問題

String 對象的兩種創建方式&#xff1a; String str1 "abcd";//先檢查字符串常量池中有沒有"abcd"&#xff0c;如果字符串常量池中沒有&#xff0c;則創建一個&#xff0c;然后 str1 指向字符串常量池中的對象&#xff0c;如果有&#xff0c;則直接將 st…

學點數學(3)-函數空間

函數空間1.距離&#xff1a;從具體到抽象2.范數3.內積4.拓撲本博文為觀看《上海交通大學公開課-數學之旅-函數空間 》所整理筆記&#xff0c;公開課視頻連接&#xff1a;http://open.163.com/newview/movie/free?pidM8PTB0GHI&midM8PTBUHT0數學中的空間 是 大家研究工作的…

Makefile編寫詳解--項目開發

預備知識&#xff1a; gcc 的3個參數&#xff1a; 1. -o 指定目標文件 gcc sources/main.c -o bin/main 2. -c 編譯的時候只生產目標文件不鏈接 gcc -c sources/main.c -o obj/main.o 3. -I 主要指定頭文件的搜索路徑 gcc -I headers -c main.c -o main.o 4. -l 指定靜…