Watchpoints are similar to breakpoints. However, watchpoints are not set for functions or lines of code. Watchpoints are set on variables. When those variables are read or written, the watchpoint is triggered and program execution stops.
It is difficult to understand watchpoint commands by themselves, so the following simple example program will be used in the command usage examples.
#include <stdio.h> int main(int argc, char **argv) { int x = 30; int y = 10; x = y; return 0; }
Use the watch command. The argument to the watch command is an expression that is evaluated. This implies that the variabel you want to set a watchpoint on must be in the current scope. So, to set a watchpoint on a non-global variable, you must have set a breakpoint that will stop your program when the variable is in scope. You set the watchpoint after the program breaks.
*NOTE* You may notice in the example below that the line of code printed doesn't match with the line that changes the variable x. This is because the store instruction that sets off the watchpoint is the last in the sequence necessary to do the 'x=y' assignment. So the debugger has already gone on to the next line of code. In the examples, a breakpoint has been set on the 'main' function and has been triggered to stop the program.
(gdb) watch x Hardware watchpoint 4: x (gdb) c Continuing. Hardware watchpoint 4: x Old value = -1073743192 New value = 11 main (argc=1, argv=0xbffffaf4) at test.c:10 10 return 0;
Use the rwatch command. Usage is identical to the watch command.
(gdb) rwatch y Hardware read watchpoint 4: y (gdb) continue Continuing. Hardware read watchpoint 4: y Value = 1073792976 main (argc=1, argv=0xbffffaf4) at test.c:8 8 x = y;
Use the awatch command. Usage is identical to the watch command.
You can set read watchpoints on memory locations:
(gdb) p &g_cvfds
$15 = (cv_fd_table *) 0xdc2680 <g_cvfds>
(gdb) awatch *0xdc2680
Hardware access (read/write) watchpoint 3: *0xdc2680
Active watchpoints show up the breakpoint list. Use the info breakpoints command to get this list. Then use the disable command to turn off a watchpoint, just like disabling a breakpoint.
(gdb) info breakpoints Num Type Disp Enb Address What 1 breakpoint keep y 0x080483c6 in main at test.c:5 breakpoint already hit 1 time 4 hw watchpoint keep y x breakpoint already hit 1 time (gdb) disable 4