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

【gdb】设置读写观察点

时间:2023-10-16 10:23:28浏览次数:29  
标签:10 Thread 读写 观察点 gdb func arg

设置读写观察点

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

(gdb) aw a
Hardware access (read/write) watchpoint 1: a
(gdb) r
Starting program: /data2/home/nanxiao/a
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
[New Thread 0x7ffff782c700 (LWP 16938)]
[Switching to Thread 0x7ffff782c700 (LWP 16938)]
Hardware access (read/write) watchpoint 1: a

Value = 0
0x00000000004005c6 in thread1_func (p_arg=0x40076c) at a.c:10
10                      a++;
(gdb) c
Continuing.
Hardware access (read/write) watchpoint 1: a

Old value = 0
New value = 1
thread1_func (p_arg=0x40076c) at a.c:11
11                      sleep(10);
(gdb) c
Continuing.
[New Thread 0x7ffff6e2b700 (LWP 16939)]
[Switching to Thread 0x7ffff6e2b700 (LWP 16939)]
Hardware access (read/write) watchpoint 1: a

Value = 1
0x00000000004005f2 in thread2_func (p_arg=0x400775) at a.c:19
19                      printf("%d\n", a);;
(gdb) c
Continuing.
1
[Switching to Thread 0x7ffff782c700 (LWP 16938)]
Hardware access (read/write) watchpoint 1: a

Value = 1
0x00000000004005c6 in thread1_func (p_arg=0x40076c) at a.c:10
10                      a++;

可以看到,使用“aw a”命令(awawatch命令的缩写)以后,每次读取或改变a的值都会让程序停下来。
需要注意的是awatch命令只对硬件观察点才生效。

 

参考资料

1. 设置读写观察点

2. gdb手册

标签:10,Thread,读写,观察点,gdb,func,arg
From: https://www.cnblogs.com/sunbines/p/17766790.html

相关文章

  • 【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......
  • 使用Aead加密支持随机读写的文件
    关联数据的认证加密Aead(authenticatedencryptionwithassociateddata),是一种同时具备保密性,完整性和可认证性的加密形式,加密过程采用数据分组形式,对同一个密钥,每次加密需要使用不重复的Nonce(NumberusedonlyOnce),加密后生成验证数据标签(Tag)用于解密时验证,并且可以附加一段......
  • 【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,......