What about saving a watchpoint number and then deleting the watchpoint using this number?
This is an example. I have a C++ program. I set three watchpoints when the breakpoint on the line 5 is hit. For the watchpoint #2 I save a gdb command file in order to delete it later. When the breakpoint on the 9 is hit I just execute this gdb command file:
This is 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;
}
This is .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
This is just a sligtly changed version of the .gdbinit that instead of saving a file with the command to delete the watchpoint saves the watchpoint number:
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
If you set a watchpoint using an address in this way:
(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)
You can also later find this address since it is dislayed in 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)