当前位置:Gxlcms > 数据库问题 > gdb调试多进程与多线程

gdb调试多进程与多线程

时间:2021-07-01 10:21:17 帮助过:2人阅读

break 行号:设置断点

clean 行号:清除断点

info break:显示断点

finish:退出函数

bt:查看函数堆栈

break 函数名,在函数的位置设置断点

delete breakpoint 删除所有的断点

disable/enable breakpoint 1 使能断点

多进程的调试第一个方法:

通过 set detach-on-fork off  实现同时调试两个进程,一个在运行一个阻塞在fork处

通过set follow-fork-mode parent/child设置是调试父进程还是子进程

设置完之后,查询正在调试的进程 info inferior 

切换到正在调试的进程 inferior number(前面的数字不是进程号)

在切换过去的进程设置断点,c执行就可以


第二种方法:可以方便的调试任意种进程

通过attach 进程号 

通过这个进行调试的时候,一般要在进程的开始位置设置让其休眠一段时间方便attach,这个运行的时候需要root权限。进程号的获取可以通过 pstree -ap | prep pthread

也要在进程的位置设置断点 切换过去 c执行


多线程调试:

首先进行设置set scheduler-locking on

在线程的入口函数或者入口函数内部设置断点,

程序向下执行,直到产生线程(不能设置了断点就直接c)

info thread

通过thread 2跳到指定的线程

c执行

内部调试

调试完返回主线程



#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{
  	sleep(20);
  	processB();	
  }
//    processB();



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

  }

线程调试步骤:

1.start

2.set scheduler-locking on

3.b 50(线程的内部)

4.n 直到产生新的线程

5.info thread 

6.thread 2

7.c

8.n

9thread 1


还有一个命令:

thread apply id1 id 2 command

进程的调试:

1.start

2.set detach-on-fork off

3.b processB(可以设置这个函数)

4.n

5.info inferior

6.inferior 2

7 c

如果想换一个进程

inferior 其他进程号


二:首先在想调试的进程前部设置等待时间

1.sudo gdb pthread

2.b 60 (要设置到内部)

3.fork之后 pstree -ap| grep pthread

4.attach 调试的进程号

5. c

6.内部调试



gdb调试多进程与多线程

标签:

人气教程排行