首页 > 其他分享 >【gdb】

【gdb】

时间:2023-10-16 10:03:53浏览次数:31  
标签:11 func Thread gdb sleep thread1

 

 

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
int a = 0;

void *thread1_func(void *p_arg)
{
  while (1)
  {
    a++;
    sleep(10);
  }
}

void *thread2_func(void *p_arg)
{
  while (1)
  {
    a++;
    sleep(10);
  }
}

int main(void)
{
  pthread_t t1, t2;

  pthread_create(&t1, NULL, thread1_func, "Thread 1");
  pthread_create(&t2, NULL, thread2_func, "Thread 2");

  sleep(1000);
  return 0;
}

gdb可以使用“watch expr thread threadnum”命令设置观察点只针对特定线程生效,也就是只有编号为threadnum的线程改变了变量的值,程序才会停下来,其它编号线程改变变量的值不会让程序停住。以上面程序为例:

(gdb) start
Temporary breakpoint 1 at 0x4005d4: file a.c, line 28.
Starting program: /data2/home/nanxiao/a
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".

Temporary breakpoint 1, main () at a.c:28
28              pthread_create(&t1, NULL, thread1_func, "Thread 1");
(gdb) n
[New Thread 0x7ffff782c700 (LWP 25443)]
29              pthread_create(&t2, NULL, thread2_func, "Thread 2");
(gdb)
[New Thread 0x7ffff6e2b700 (LWP 25444)]
31              sleep(1000);
(gdb) i threads
  Id   Target Id         Frame
  3    Thread 0x7ffff6e2b700 (LWP 25444) 0x00007ffff7915911 in clone () from /lib64/libc.so.6
  2    Thread 0x7ffff782c700 (LWP 25443) 0x00007ffff78d9bcd in nanosleep () from /lib64/libc.so.6
* 1    Thread 0x7ffff7fe9700 (LWP 25413) main () at a.c:31
(gdb) wa a thread 2
Hardware watchpoint 2: a
(gdb) c
Continuing.
[Switching to Thread 0x7ffff782c700 (LWP 25443)]
Hardware watchpoint 2: a

Old value = 1
New value = 3
thread1_func (p_arg=0x400718) at a.c:11
11                      sleep(10);
(gdb) c
Continuing.
Hardware watchpoint 2: a

Old value = 3
New value = 5
thread1_func (p_arg=0x400718) at a.c:11
11                      sleep(10);
(gdb) c
Continuing.
Hardware watchpoint 2: a

Old value = 5
New value = 7
thread1_func (p_arg=0x400718) at a.c:11
11                      sleep(10);

可以看到,使用“wa a thread 2”命令(wawatch命令的缩写)以后,只有thread1_func改变a的值才会让程序停下来。
需要注意的是这种针对特定线程设置观察点方式只对硬件观察点才生效

 

参考资料

1. gdb手册

2. 设置观察点只针对特定线程生效

标签:11,func,Thread,gdb,sleep,thread1
From: https://www.cnblogs.com/sunbines/p/17766717.html

相关文章

  • 【gdb】输出信息多时不会暂停输出
    输出信息多时不会暂停输出有时当gdb输出信息较多时,gdb会暂停输出,并会打印“---Type<return>tocontinue,orq<return>toquit---”这样的提示信息,如下面所示:81process26391020xff04af84in__lwp_park()from/usr/lib/libc.so.180process25735660xff04......
  • 【gdb】设置观察点
    设置观察点1.例子#include<stdio.h>#include<pthread.h>#include<unistd.h>inta=0;void*thread1_func(void*p_arg){ while(1) { a++; sleep(10); }}intmain(intargc,char*argv[]){ pthread_tt1; pthread_create(&t1,NUL......
  • 【gdb】显示gdb版本信息
    显示gdb版本信息使用gdb时,如果想查看gdb版本信息,可以使用“showversion”命令:(gdb)showversionGNUgdb(GDB)7.7.1Copyright(C)2014FreeSoftwareFoundation,Inc.LicenseGPLv3+:GNUGPLversion3orlater<http://gnu.org/licenses/gpl.html>Thisisfreesof......
  • 【gdb】启动时不显示提示信息
    启动时不显示提示信息$gdbGNUgdb(GDB)7.7.50.20140228-cvsCopyright(C)2014FreeSoftwareFoundation,Inc.LicenseGPLv3+:GNUGPLversion3orlater<http://gnu.org/licenses/gpl.html>Thisisfreesoftware:youarefreetochangeandredistributeit.The......
  • 【gdb】gdb退出时不显示提示信息
    gdb退出时不显示提示信息gdb在退出时会提示:Adebuggingsessionisactive.Inferior1[process29686]willbekilled.Quitanyway?(yorn)n如果不想显示这个信息,则可以在gdb中使用如下命令把提示信息关掉:(gdb)setconfirmoff也可以把这个命令加到.gdbi......
  • 【gdb】只允许一个线程运行
    只允许一个线程运行1.例子:#include<stdio.h>#include<pthread.h>#include<unistd.h>inta=0;intb=0;void*thread1_func(void*p_arg){while(1){a++;sleep(1);}}void*thread2_func(void*p_arg){while(1){b+......
  • 【gdb】调试子进程
    调试子进程1.例子#include<stdio.h>#include<sys/types.h>#include<unistd.h>intmain(void){pid_tpid;pid=fork();if(pid<0){exit(1);}elseif(pid>0){exit(0);}printf("helloworld\n&qu......
  • 【gdb】调试已经运行的进程
     调试已经运行的进程1.例子:#include<stdio.h>#include<pthread.h>void*thread_func(void*p_arg){while(1){printf("%s\n",(char*)p_arg);sleep(10);}}intmain(void){pthread_tt1,t2;pthread_create(&t1,NULL,......
  • 【gdb】让catchpoint只触发一次
    让catchpoint只触发一次1.例子:#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<unistd.h>intmain(void){pid_tpid;inti=0;for(i=0;i<2;i++){ pid=fork(); if(pid<0)......
  • 【gdb】打印内存的值
    打印内存的值1.例子#include<stdio.h>intmain(void){inti=0;chara[100];for(i=0;i<sizeof(a);i++){a[i]=i;}return0;}gdb中使用“x”命令来打印内存的值,格式为“x/nfuaddr”......