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

【gdb】设置观察点

时间:2023-10-16 09:33:34浏览次数:39  
标签:11 Thread int libthread db 观察点 gdb 设置

设置观察点

1. 例子

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

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

int main(int argc, char *argv[])
{
	pthread_t t1;

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

	sleep(1000);
	return 0;
}

gdb可以使用“watch”命令设置观察点,也就是当一个变量值发生变化时,程序会停下来。以上面程序为例:

(gdb) start
Temporary breakpoint 1 at 0x4005a8: 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) watch a
Hardware watchpoint 2: 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 8813)]
[Switching to Thread 0x7ffff782c700 (LWP 8813)]
Hardware watchpoint 2: a

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

Old value = 1
New value = 2
thread1_func (p_arg=0x4006d8) at a.c:11
11                      sleep(10);

可以看到,使用“watch a”命令以后,当a的值变化:由0变成1,由1变成2,程序都会停下来。此外也可以使用“watch *(data type*)address”这样的命令,仍以上面程序为例:

(gdb) p &a
$1 = (int *) 0x6009c8 <a>
(gdb) watch *(int*)0x6009c8
Hardware watchpoint 2: *(int*)0x6009c8
(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 15431)]
[Switching to Thread 0x7ffff782c700 (LWP 15431)]
Hardware watchpoint 2: *(int*)0x6009c8

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

Old value = 1
New value = 2
thread1_func (p_arg=0x4006d8) at a.c:11
11                      sleep(10);

先得到a的地址:0x6009c8,接着用“watch *(int*)0x6009c8”设置观察点,可以看到同“watch a”命令效果一样。
观察点可以通过软件或硬件的方式实现,取决于具体的系统。但是软件实现的观察点会导致程序运行很慢,使用时需注意。参见gdb手册.

如果系统支持硬件观测的话,当设置观测点是会打印如下信息: Hardware watchpoint num: expr

如果不想用硬件观测点的话可如下设置: set can-use-hw-watchpoints

 

列出当前所设置了的所有观察点:

info watchpoints

watch 所设置的断点也可以用控制断点的命令来控制。如 disable、enable、delete等

 

参考资料

1. 设置断点

标签:11,Thread,int,libthread,db,观察点,gdb,设置
From: https://www.cnblogs.com/sunbines/p/17766662.html

相关文章

  • 【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......
  • 【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”......
  • 【gdb】进入和退出图形化调试界面
    进入和退出图形化调试界面1.例子#include<stdio.h>voidfun1(void){inti=0;i++;i=i*2;printf("%d\n",i);}voidfun2(void){intj=0;fun1();j++;j=j*2;print......