创作人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

Linux gdb调试二(watch)相关推荐

  1. linux子系统gdp调试,Linux GDB调试 详述

    今天来分享下gdb的简单调试,我这里写了个例子 三个.c文件 func1.c func2.c main.c 首先生成可调试的执行文件 gcc -g func1.c func2.c main.c -o ...

  2. gdb 调试_一文入门Linux下gdb调试(二)

    点击"蓝字"关注我吧 作者:良知犹存 转载授权以及围观:欢迎添加微信号:Conscience_Remains 总述     今天我们介绍一下core dump文件,Core dum ...

  3. linux gdb网络调试,一文入门Linux下gdb调试(二)

    本文转载自[微信公众号:羽林君,ID:Conscience_Remains] 总述 今天我们介绍一下core dump文件,Core dump叫做核心转储,它是进程运行时在突然崩溃的那一刻的一个内存快 ...

  4. Linux gdb调试(4):多进程与多线程调试

    一,gdb的基础知识 1>介绍: gdb是Linux环境下的代码调试工具. 2>使用:需要在源代码生成的时候加上 -g 选项. 3>开始使用: gdb binFile 4>退出 ...

  5. Linux GDB调试

    Linux 段错误调试 core 文件调试 编译时 加上 -g 使编译出的文件带 调试信息 gcc -g main.c -o main 使编译出的可执行文件带调试信息gdb main //对 main ...

  6. [Linux]gdb调试多进程多线程例程

    gdb相信学linux的同学已经比较熟悉了吧,它是linux下代码调试工具.我们在写c语言,c++的代码时经常会用到,它有一些常用的调试命令: run(r):运行程序,如果有断点在下一个断点处停止 s ...

  7. Linux——gdb调试时多进程切换方法(attach/follow-fork-mode)

    对于程序中创建子进程的情况,进行gdb调试时会默认选择父进程进行调试,假如需要对子进程进行调试就需要使用特殊方法. 共有两种方法可供选择: 目录 一.attach子进程PID ①.运行进程 ②获取进程 ...

  8. Linux GDB调试死锁问题

    1. 死锁介绍 1.1 锁的简介 由于多线程的模式下,各个线程并发运行(注意"并发和"并行"的区别),为了保证各个线程对公共资源的访问时出现数据不一致性的问题,出现了锁的 ...

  9. Linux GDB调试完全教程

    转自 http://blog.csdn.net/gatieme 本文将主要介绍linux下的强大调试工具是怎么完成这些工作的. 之所以要调试程序,是因为程序的运行结果和预期结果不一致,或者程序出现运行 ...

最新文章

  1. php中mkdir()函数的权限问题
  2. KubeShere安装Redis
  3. R语言笔记2:读写数据所需的主要函数、与外部环境交互
  4. 自动化部署必备技能—定制化RPM包[转载]
  5. easyNeurons 神经网络入门教程
  6. 仿射加密简述和Win32版本实现
  7. 三十五、深入Java中的泛型(下篇)
  8. 前端学习(2224):react之函数式组件
  9. linux内核ddr初始化,X-007-UBOOT-DDR的初始化(Bubblegum-96平台)
  10. 推荐算法---FM,协同过滤
  11. CentOS/Ubuntu制作自动安装arm iso镜像
  12. 两个员工,一个做事认真但效率低,一个迟到早退但效率高,只能留一个我该留哪个?
  13. Java ClassNotFoundException – java.lang.ClassNotFoundException
  14. 内核运行之前访问IO
  15. Skype for business企业语音配置之二创建拨号计划
  16. golang的https服务器
  17. 喷墨打印机的使用与维护
  18. 新版酷享云支付系统第三方第四方支付源码
  19. html中背景条纹效果,使用CSS线性渐变 制作条纹背景
  20. Maven 项目自动构建 Docker 镜像推送到 Docker 服务器

热门文章

  1. 发现”不喜欢自己的专业“,其实是件好事
  2. 摄像头图像测试1-基本概念
  3. 如何在网上获取全国计算机等级考试证书电子版
  4. vue拖拽组件生成页面代码,vue可视化拖拽组件模板
  5. 真假3D:VR成像技术探秘
  6. GitHub账号注册与登录
  7. 红旗Linux操作系统下载(转)
  8. MurmurHash算法初探
  9. 关于tomcat卸载+安装问题
  10. localhost 已拒绝连接(命令窗口安装版)还有安装教程