GDB:以编程方式删除观察点

时间:2012-11-01 08:31:50

标签: gdb watchpoint

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

我使用附加到断点的命令在内存位置设置观察点。我希望在另一个断点处清除观察点,但我无法弄清楚如何在没有观察点数的情况下清除观察点。是否有可以按内存位置删除观察点的命令?

2 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:1)

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

这是一个例子。我有一个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)