zoukankan      html  css  js  c++  java
  • [转]GDB-----2.watchpoint

    TODO需要在ARM下验证

    1. 前言

    watchpoint,顾名思义,其一般用来观察某个变量/内存地址的状态(也可以是表达式),如可以监控该变量/内存值是否被程序读/写情况。

    在gdb中可通过下面的几种方法来设置watchpoint:

        (gdb) watch 
            在指定变量/内存地址(表达式)expr设置一个watchpoint。
            一但expr值有变化时,将停住程序。
        (gdb) rwatch 
           当expr被读时,停住程序。
        (gdb) awatch 
           当expr被读或被写时,停住程序。
        (gdb) info watchpoints 
            列出当前所设置了的所有观察点。(info break也可查看)
    

    2. gdb watchpoint实践

    GDB十分钟快速入门教程的gdb-sample.c为例,
    在gdb-sample.c中, 变量n总共被改变了3次,如果我们下个watchpoint在n变量处,因为n变量改变了3次而响应3次watchpoint,因而程序会将被调试器暂停运行3次:

    编译gdb-sample.c,并使用gdb 加载gdb-sample:


    $ gcc gdb-sample.c -o gdb-sample -g 
    $ gdb ./gdb-sample 
    GNU gdb (GDB) 7.0.50.20090928-cvs 
    Copyright (C) 2009 Free Software Foundation, Inc. 
    License GPLv3+: GNU GPL version 3 or later 
    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 “i686-pc-linux-gnu”. 
    For bug reporting instructions, please see: 
    … 
    Reading symbols from /home/ddd/gdb-sample…done. 
    (gdb) 

    watchpoint只能在程序启动后设置,先在main那下个断点,让程序启动后暂停在main函数处:


    (gdb) b main 
    Breakpoint 1 at 0x80483ad: file gdb-sample.c, line 19. 
    (gdb) r 
    Starting program: /home/ddd/gdb-sample

    Breakpoint 1, main () at gdb-sample.c:19 
    19 n = 1; 
    (gdb) 

    给n变量下个watchpoint:


    (gdb) watch n 
    Hardware watchpoint 2: n 
    (gdb) 

    敲入”c”命令让程序恢复运行,这时候程序会停止在第一次n变量改变处


    20 n++; 

    并提示即将运行的下一条的语句:


    23 n--;

    (gdb) c 
    Continuing. 
    Hardware watchpoint 2: n

    Old value = -1208017424 
    New value = 2 
    main () at gdb-sample.c:23 
    23 n--; 
    (gdb) 

    重复如上操作,程序还会停止两次,所有gdb输出如下:


    (gdb) c 
    Continuing. 
    Hardware watchpoint 2: n

    Old value = 2 
    New value = 1 
    main () at gdb-sample.c:25 
    25 nGlobalVar += 100; 
    (gdb) —-> 这次停止是由 ”23 n–; “改变变量n的值引起的

    (gdb) c 
    Continuing. 
    n = 1, nGlobalVar = 88 
    tempFunction is called, a = 1, b = 2 
    Hardware watchpoint 2: n

    Old value = 1 
    New value = 3 
    main () at gdb-sample.c:31 
    31 printf(“n = %d”, n); 
    (gdb) —-> 这次停止是由 ” 30 n = tempFunction(1, 2); “改变变量n的值引起的

    (gdb) c 
    Continuing.

    Watchpoint 2 deleted because the program has left the block in 
    which its expression is valid. 
    0xb7e91450 in __libc_start_main () from /lib/tls/i686/cmov/libc.so.6 
    (gdb) 

    2: watchpoint在gdb中的实现原理

    watchpoint可以看成是一种特殊的”断点”, 其实现一般需要CPU支持硬件断点,如果纯软件实现watchpoint,那好像会很耗CPU.(我没
    去看gdb的软0watchpoint的实现,有时间得去研究下,不过如果让我来实现这个功能(和同事讨论过),应该是设置watchpoint所在的
    那个页表为不可读/访问,然后在缺页处理那检测当前的页和地址是否是软设置watchpoint所在的页和watchpoint的地址,如果是,则
    说明可以假设该watchpoint发生了)

    目前支持watchpoint硬件断点的arch有x86,ppc和mips。

    如果支持硬件断点,那么可以将监控的操作交给硬件来完成,而gdb这边只要做个简单的逻辑处理就行.
    还是以上面的gdb-sample.c为例:

    当gdb执行watch n命令后,gdb会在n变量所在的内存地址上下个硬件写断点


    (gdb) watch n 
    Hardware watchpoint 2: n 

    (如果是 rwatch n命令,gdb会在n变量所在的内存地址上下个硬件读断点)
    (tips: gdb 通过系统调用ptrace()去修改调试寄存器值,从而达到实现硬件断点的目的)

    这样只要系统操作了n变量(内存地址),就会触发一个硬件断点中断。
    gdb捕获到这个断点中断后,就会将新的n变量值和改变前的值做比较,
    1)如果n变量的值改变了,则将程序停止。
    2)如果n变量的值没有改变了,则程序继续运行。

    关于硬件断点,可以参考x86 调试寄存器 一文。

    3: 远程gdb server的watchpoint 实现

    如果调试本地应用程序,gdb可以直接通过ptrace发出的信号得到watchpoint信息。

    如果远程调试程序,gdb怎么从远程gdb server那得到watchpoint信息呢?

    说到这里,又不得不搬出 GDB远程串行协议了..
    在GDB远程串行协议里定义了gdb server和gdb所有的通信规则,所以要告诉gdb,远程gdb server那边踩中watchpoint了,还得通过那个协议来传达。

    在GDB远程串行协议的Stop-Reply-Packets里定义了如何传达watchpoint信息:

    ?View Code TEXT
     
    `T AA n1:r1;n2:r2;...'
        The program received signal number AA (a two-digit hexadecimal number).
        This is equivalent to an `S' response, except that the `n:r' pairs can
        carry values of important registers and other information directly in
        the stop reply packet, reducing round-trip latency. Single-step and
        breakpoint traps are reported this way. Each `n:r' pair is interpreted
        as follows:
            * If n is a recognized stop reason, it describes a more specific
        event that stopped the target. The currently defined stop reasons are
        listed below. aa should be `05', the trap signal. At most one stop 
        reason should be present.
     
        The currently defined stop reasons are:
        `watch'
        `rwatch'
        `awatch'
         The packet indicates a watchpoint hit, and r is the data address, in hex.

    所以只要在Stop-Reply-Packets里添加 watch+断点地址格式的数据,gdb就知道那边踩中watchpoint了.

    3. 参考文档

    本文地址:
    http://www.kgdb.info/gdb_watchpoint/
    版权所有 © 转载时必须以链接形式注明作者和原始出处!

  • 相关阅读:
    sockjs-node/info 请求错误
    vue项目中 —— rem布局适配方案
    JS判断对象是否为空
    JS生成一个特定格式随机数
    vue项目中axios请求网络接口封装
    vue报错——error Unexpected console statement no-console
    vue切换页面修改网页的标题
    处理js两个数相乘的坑
    js科学计数法问题
    (9)kendo UI使用基础介绍与问题整理——Validator/基础说明、实例介绍
  • 原文地址:https://www.cnblogs.com/smartjourneys/p/6978186.html
Copyright © 2011-2022 走看看