创作人QQ:851301776,邮箱:lfr890207@163.com
欢迎大家一起技术交流,本博客主要是自己学习的心得体会,只为每天进步一点点!
个人座右铭:
1.没有横空出世,只要厚积一定发。
2.你可以学历不高,你可以不上学,但你不能不学习
一、watch命令
watch命令是一个强大的命令,它可以用来监视一个变量或者一段内存,当这个变量或者该出内存处的值发生变化是,gdb就会终端下来。被监视的某个变量或者某个内存地址会产生一个watch point(观察点)。
1.形式1
整型变量(int)i,监视i的值变化:watch i;
2.形式2
指针类型(char *p)
(1)watch p:是查看*(&p),监控的是p变量本身
(2)watch *p:是监控p所指向内存的内容。
我们需要查看地址,因为是要查看内存地址上的数据是咋样变化的。
3.形式三
一个数组或者内存区间char buff[128]:watch buff,这是对buff的128个数据进行了监控。
举例:
#include <stdio.h>
#include <string.h>
char mem[ 8];
char buff[256];
void initpBuf(char *pBuf)
{
int i, j;
mem[0] = '0';
mem[1] = '1';
mem[2] = '2';
mem[3] = '3';
mem[4] = '4';
mem[5] = '5';
mem[6] = '6';
mem[7] = '7';
for(i=2;i<8;i++)
{
for(j=0;j<16;j++)
pBuf[i*16+j] = i*16+j;
}
return ;
}
void ptrBuf(char *pBuf)
{
int i, j;
for(i=2;i<8;i++)
{
for(j=0;j<16;j++)
printf("%c ", pBuf[i*16+j]);
printf("\n");
}
return ;
}
int main(void){
initpBuf(buff);
ptrBuf(buff);
return 0;
}
测试:
mrlee@mrlee-virtual-machine:~/test/test/test/test/test$ gdb a.out
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...done.
(gdb) b main
Breakpoint 1 at 0x400676: file watch.c, line 45.
(gdb) wathc mem
Undefined command: "wathc". Try "help".
(gdb) watch mem
Hardware watchpoint 2: mem
(gdb) r
Starting program: /home/mrlee/test/test/test/test/test/a.out
Breakpoint 1, main () at watch.c:45
45 initpBuf(buff);
(gdb) c
Continuing.
Hardware watchpoint 2: mem
Old value = "\000\000\000\000\000\000\000"
New value = "0\000\000\000\000\000\000"
initpBuf (pBuf=0x601080 <buff> "") at watch.c:12
12 mem[1] = '1';
(gdb) c
Continuing.
Hardware watchpoint 2: mem
Old value = "0\000\000\000\000\000\000"
New value = "01\000\000\000\000\000"
initpBuf (pBuf=0x601080 <buff> "") at watch.c:13
13 mem[2] = '2';
(gdb) c
Continuing.
Hardware watchpoint 2: mem
Old value = "01\000\000\000\000\000"
New value = "012\000\000\000\000"
initpBuf (pBuf=0x601080 <buff> "") at watch.c:14
14 mem[3] = '3';
(gdb) c
Continuing.
Hardware watchpoint 2: mem
Old value = "012\000\000\000\000"
New value = "0123\000\000\000"
initpBuf (pBuf=0x601080 <buff> "") at watch.c:15
15 mem[4] = '4';
(gdb) c
Continuing.
Hardware watchpoint 2: mem
Old value = "0123\000\000\000"
New value = "01234\000\000"
initpBuf (pBuf=0x601080 <buff> "") at watch.c:16
16 mem[5] = '5';
(gdb) c
Continuing.
Hardware watchpoint 2: mem
Old value = "01234\000\000"
New value = "012345\000"
initpBuf (pBuf=0x601080 <buff> "") at watch.c:17
17 mem[6] = '6';
(gdb) c
Continuing.
Hardware watchpoint 2: mem
Old value = "012345\000"
New value = "0123456"
initpBuf (pBuf=0x601080 <buff> "") at watch.c:18
18 mem[7] = '7';
(gdb) c
Continuing.
Hardware watchpoint 2: mem
Old value = "0123456"
New value = "01234567"
initpBuf (pBuf=0x601080 <buff> "") at watch.c:20
20 for(i=2;i<8;i++)
(gdb) c
Continuing.
! " # $ % & ' ( ) * + , - . /
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
@ A B C D E F G H I J K L M N O
P Q R S T U V W X Y Z [ \ ] ^ _
` a b c d e f g h i j k l m n o
p q r s t u v w x y z { | } ~
[Inferior 1 (process 74235) exited normally]
(gdb) info watch
Num Type Disp Enb Address What
2 hw watchpoint keep y mem
breakpoint already hit 8 times
(gdb) delete 2
(gdb) ls
Undefined command: "ls". Try "help".
(gdb) info watch
No watchpoints.
(gdb)
4.取消watch
先用info watch查看watch的变量,然后根据编号使用delete删除对应的watch变量
5.watch局部变量
(1)以以上代码中i为类,在启动之前,首先在对应函数打断点(b initpBuf),在进入到这个断点的时候设置到监控的变量,然后输入c