首页 > 其他分享 >【gdb】设置读观察点

【gdb】设置读观察点

时间:2023-10-16 10:24:41浏览次数:35  
标签:设置 10 func Thread 观察点 gdb thread1

设置读观察点

1. 例子

#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可以使用“rwatch”命令设置读观察点,也就是当发生读取变量行为时,程序就会暂停住。以上面程序为例:

(gdb) start
Temporary breakpoint 1 at 0x4005f3: file a.c, line 19.
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:19
19              pthread_create(&t1, NULL, thread1_func, "Thread 1");
(gdb) rw a
Hardware read watchpoint 2: a
(gdb) c
Continuing.
[New Thread 0x7ffff782c700 (LWP 5540)]
[Switching to Thread 0x7ffff782c700 (LWP 5540)]
Hardware read watchpoint 2: a

Value = 0
0x00000000004005c6 in thread1_func (p_arg=0x40071c) at a.c:10
10                      printf("%d\n", a);
(gdb) c
Continuing.
0
Hardware read watchpoint 2: a

Value = 0
0x00000000004005c6 in thread1_func (p_arg=0x40071c) at a.c:10
10                      printf("%d\n", a);
(gdb) c
Continuing.
0
Hardware read watchpoint 2: a

Value = 0
0x00000000004005c6 in thread1_func (p_arg=0x40071c) at a.c:10
10                      printf("%d\n", a);

可以看到,使用“rw a”命令(rwrwatch命令的缩写)以后,每次访问a的值都会让程序停下来。
需要注意的是rwatch命令只对硬件观察点才生效

 

参考资料

1. 设置读观察点

2. gdb手册

标签:设置,10,func,Thread,观察点,gdb,thread1
From: https://www.cnblogs.com/sunbines/p/17766780.html

相关文章

  • 【gdb】设置读写观察点
    设置读写观察点1.例子:#include<stdio.h>#include<pthread.h>#include<unistd.h>inta=0;void*thread1_func(void*p_arg){while(1){a++;sleep(10);}}void*thread2_func(void*p_arg){while(1){a++;sleep(1......
  • 【gdb】
      #include<stdio.h>#include<pthread.h>#include<unistd.h>inta=0;void*thread1_func(void*p_arg){while(1){a++;sleep(10);}}void*thread2_func(void*p_arg){while(1){a++;sleep(10);}}......
  • 【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......
  • 将复选框设置为已选中状态,使用jQuery
    内容来自DOChttps://q.houxu6.top/?s=将复选框设置为已选中状态,使用jQuery我想使用jQuery来选中一个复选框,就像这样:$(".myCheckBox").checked(true);或者$(".myCheckBox").selected(true);有这样的方法吗?现代jQuery使用.prop():$('.myCheckbox').prop('checked',......
  • 【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......