2012-11-01 89 views
2

是否可以在不知道觀察點編號的情況下刪除觀察點?GDB:以編程方式刪除觀察點

我正在使用附加到斷點的命令來設置內存位置上的觀察點。我希望在另一個斷點清除觀察點,但我無法弄清楚如何清除沒有觀察點編號的觀察點。有沒有可以通過內存位置刪除觀察點的命令?

回答

4

最簡單的方法dislayed您也可以日後查找該地址是使用$ bpnum便利變量,您可能希望將其存儲在另一個便利變量中,以便稍後創建斷點/觀察點時不會更改。

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

正是我在找什麼,謝謝。 – leecbaker

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 

這僅僅是一個slgtly更改版本的.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)