【问题标题】:GDB: programmatically delete a watchpointGDB:以编程方式删除观察点
【发布时间】:2012-11-03 17:52:34
【问题描述】:

是否可以在不知道观察点编号的情况下删除观察点?

我正在使用附加到断点的命令在内存位置设置观察点。我想在另一个断点清除观察点,但我不知道如何在没有观察点编号的情况下清除观察点。有没有可以按内存位置删除观察点的命令?

【问题讨论】:

    标签: gdb watchpoint


    【解决方案1】:

    最简单的方法是使用 $bpnum 便利变量,您可能希望将其存储在另一个便利变量中,以便以后创建断点/观察点时它不会改变。

    (gdb) watch y
    (gdb) set $foo_bp=$bpnum
    Hardware watchpoint 2: y
    (gdb) p $foo_bp
    $1 = 2
    (gdb) delete $foo_bp
    

    【讨论】:

      【解决方案2】:

      如何保存观察点编号,然后使用该编号删除观察点?

      这是一个例子。我有一个 C++ 程序。当第 5 行的断点被命中时,我设置了三个观察点。对于观察点 #2,我保存了一个 gdb 命令文件,以便稍后将其删除。当 9 上的断点被命中时,我只需执行这个 gdb 命令文件:

      这是 main.cpp:

      #include <stdio.h>
      int main()
      {
        int v3=2, v2=1, v1 =0 ;
        printf("Set a watchpoint\n");
      
        v1 = 1;
        v1 = 2;
        printf("Clear the watchpoint\n");
      
        v1 = 3;
        v1 = 4;
      
        return 0;
      }
      

      这是.gdbinit:

      file ./a.out
      b 5
      commands
      watch v2
      watch v1
      set pagination off
      shell rm -f all_watchpoints
      set logging file all_watchpoints
      set logging on
      info watchpoints
      set logging off
      shell rm -f delete_my_watchpoint
      shell tail -n 1 all_watchpoints | awk ' {print "delete "$1 }' > delete_my_watchpoint
      watch v3
      echo Done\n
      c
      end
      b 9
      commands
      source delete_my_watchpoint
      info watchpoints
      end
      r
      

      这只是 .gdbinit 的一个略微更改的版本,它不是使用删除观察点的命令保存文件而是保存观察点编号:

      file ./a.out
      b 5
      commands
      watch v2
      watch v1
      set pagination off
      shell rm -f all_watchpoints
      set logging file all_watchpoints
      set logging on
      info watchpoints
      set logging off
      shell rm -f delete_my_watchpoint
      shell tail -n 1 all_watchpoints | awk ' {print "set $watchpoint_to_delete_later="$1 }' > save_my_watchpoint_number
      source save_my_watchpoint_number
      shell rm -f save_my_watchpoint_number
      shell rm -f all_watchpoints
      watch v3
      echo Done\n
      c
      end
      b 9
      commands
      delete $watchpoint_to_delete_later
      info watchpoints
      end
      r
      

      如果您以这种方式使用地址设置观察点:

      (gdb) watch *((int*)0x22ff44)
      Hardware watchpoint 3: *((int*)0x22ff44)
      (gdb) info watchpoints
      Num     Type           Disp Enb Address    What
      3       hw watchpoint  keep y              *((int*)0x22ff44)  
      

      你也可以稍后找到这个地址,因为它显示在info watchpoints

      (gdb) set logging file all_watchpoints
      (gdb) set logging on
      Copying output to all_watchpoints.
      (gdb) info watchpoints
      Num     Type           Disp Enb Address    What
      3       hw watchpoint  keep y              *((int*)0x22ff44)
      4       hw watchpoint  keep y              *((int*)0x22ff48)
      5       hw watchpoint  keep y              *((int*)0x22ff4B)
      (gdb) set logging of
      Done logging to all_watchpoints.
      (gdb) shell grep 0x22ff48 all_watchpoints
      4       hw watchpoint  keep y              *((int*)0x22ff48)
      (gdb) shell grep 0x22ff48 all_watchpoints | awk ' {print $1}'
      4
      (gdb) shell grep 0x22ff48 all_watchpoints | awk ' {print "delete "$1}' > delete_watchpoint
      (gdb) source delete_watchpoint
      (gdb) info watchpoints
      Num     Type           Disp Enb Address    What
      3       hw watchpoint  keep y              *((int*)0x22ff44)
      5       hw watchpoint  keep y              *((int*)0x22ff4B)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-14
        • 2011-11-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多