您的位置:首页 > 其它

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

2017-12-22 11:25 525 查看
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://blog.csdn.net/lhl_blog/article/details/8888010

inux下应用程序的调试工具主要就是gdb,可能你已经习惯了IDE形式的调试工具。也许刚开始使用gdb作为调试工具,会有诸多的不变,但是一旦你学会了如何使用gdb你就会被其富有魔力的功能所吸引的,下面开始逐步的学习linux下gdb的使用方式。

一直以来对于gdb在多线程调试方面的应用好奇,最近,由于项目需要,学习了linux下的gdb在多线程下的调试方法。下面就结合一个简单的案例介绍一下gdb的多线程调试方法。其中,本例子还介绍了如何调试链接有静态库的多线程应用程序。

1.理论介绍

gdb支持的用于多线程调试的工具如下:

能够自动的提醒新线程的创建。

‘thred threadno’,实现在不同线程间切换。

‘info thead’,可以查看存在的线程信息。

‘thread applay [threadno] [all] args’ ,在指定的线程上执行特定的命令args。

可以在线程中设置特定的断点。

‘set print thread-events’,用于设定是否提示线程启动或停止时的信息。

‘set libthread-db-search-path path’,用于是用户可以自己制定libthread-db 的路径信息。

'set scheduler-locking mode',在某些操作系统中,你可以通过锁住OS的调度行为,这样可以就可以改变GDB默认的行为,达到同一时间只有一个线程在运行的目的。

off:没有锁定,所有线程可以在任何时间运行。

on:锁定线程,只有当前线程在运行。

step:该模式是对single-stepping模式的优化。此模式会阻止其他线程在当前线程单步调试时,抢占当前线。因此调试的焦点不会被以外的改变。其他线程不可能抢占当前的调试线程。其他线程只有下列情况下会重新获得运行的机会:

当你‘next’一个函数调用的时候。

当你使用诸如‘continue’、‘until‘、’finish‘命令的时候。

其他线程遇到设置好的断点的时候。

1.1线程创建提醒

当应用程序创建线程的时候,如果你设置了’set print thread-events‘,那么他会自动提示新创建线程的信息,GNU/linux 下的提示信息如下:



[cpp] view plaincopy

[New Thread 0x41e02940 (LWP 25582)]



1.2显示线程信息

info thread用于显示系统中正在运行的所有线程的信息。信息主要包括如下几项:

GDB分配的id号。

目标系统定义的线程id(systag)。

线程的名字,如果存在的话,会被显示出来。用户可以自定义线程的名字,或者由程序自己指定。

线程相关的栈的信息。

其中,*表示当前正在运行的线程,下面是一个多线程的相关信息。



[cpp] view plaincopy

(gdb) info threads

Id Target Id Frame

3 process 35 thread 27 0x34e5 in sigpause ()

2 process 35 thread 23 0x34e5 in sigpause ()

* 1 process 35 thread 13 main (argc=1, argv=0x7ffffff8)

at threadtest.c:68


1.3切换线程

thread threadno用于在同步线程之间实现切换。threadno即上面显示的GDB自定义的线程的id号。线程切换成功后,会打印该线程的相关信息,比如栈信息。



[cpp] view plaincopy

(gdb) thread 2

[Switching to thread 2 (Thread 0xb7fdab70 (LWP 12747))]

#0 some_function (ignore=0x0) at example.c:8

8 printf ("hello\n");


变量$_thread 记录了当前线程的id号。你或许在设置断点条件或脚本的时候会用到该变量。

1.4执行命令

thread apply[threadid|all] command,该工具用于在一个或多个线程执行指定的命令command。threadid可以是一个或多个线程id,或是一个范围值,例如,1-3

1.5定义/find线程名

thread name,可以通过该工具实现线程名的重新定义。一般,系统会为每一个线程定义一个名字,例如GNU/linux,使用该命令后会将系统定义的线程名称覆盖掉。

thread find [regexp],其中regexp可以是线程的systag,例如,LWP 25582中的25582,或线程名(系统定义的或用户自定义的)

2.实例调试

下面通过一个实例,具体演示一下gdb thread调试。

2.1静态库编译

下面为一个简单的函数用于打印不同的字符串。



[cpp] view plaincopy

#include<iostream>

#include<string>

#include"print.h"

using namespace std;

void print(string words)

{

std::cout << words << std::endl;

}


将其编译成静态库



[cpp] view plaincopy

g++ -c print.cpp

[cpp] view plaincopy

ar crs libprint.a print.o



2.2 链接静态库

下面为一个多线程程序的打印程序,很简单



[cpp] view plaincopy

#include<iostream>

#include<pthread.h>

#include"print.h"

void* threadPrintHello(void* arg)

{

while(1)

{

sleep(5);

print(string("Hello"));

}

}

void* threadPrintWorld(void* arg)

{

while(1)

{

sleep(5);

print(string("World"));

}

}

int main( int argc , char* argv[])

{

pthread_t pid_hello , pid_world;

int ret = 0;

ret = pthread_create(&pid_hello , NULL , threadPrintHello , NULL);

if( ret != 0 )

{

std::cout << "Create threadHello error" << std::endl;

return -1;

}

ret = pthread_create(&pid_world , NULL , threadPrintWorld , NULL);

if( ret != 0 )

{

std::cout << "Create threadWorld error" << std::endl;

return -1;

}

while(1)

{

sleep(10);

std::cout << "In main thread" << std::endl;

}

pthread_join(pid_hello , NULL);

pthread_join(pid_world , NULL);

return 0;

}



编译程序



[cpp] view plaincopy

g++ -o thread thread.cpp -lpthread -lprint



2.3调试程序

启动程序



[cpp] view plaincopy

$./thread



进程id



[cpp] view plaincopy

$ps aux |grep thred

[cpp] view plaincopy

1000 24931 0.0 0.0 21892 912 pts/0 tl+ 03:04 0:00 src/thread


attach该进程



[cpp] view plaincopy

$sudo gdb thread 24931



显示线程信息

[cpp] view plaincopy

(gdb) info thread

Id Target Id Frame

* 3 Thread 0xb7471b40 (LWP 24932) "threadPrintHello" threadPrintHello (arg=0x0) at thread.cpp:10

2 Thread 0xb6c70b40 (LWP 24933) "thread" 0xb7779424 in __kernel_vsyscall ()

1 Thread 0xb7473700 (LWP 24931) "thread" 0xb7779424 in __kernel_vsyscall ()

(原文地址:http://www.cnblogs.com/wliangde/p/3737040.html)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: