2.5 The ‘watch’ and ‘clear’ Commands

One can also set breakpoint on a variable to monitor any read or write instructions over it. This kind of breakpoints are called data breakpoint or ‘watchpoint’. If any statement reads a watched variable or writes over it, the debugger paused and shows the change and the statement accessed the variable. Likewise, any expression over the variable can be watched. The ‘watch’ command is used to monitor any read or write accesses over a variable. ‘rwatch’ and ‘awatch’ commands only intercept a read and write access, respectively.

Watching commands in GDB

1.
One way to put a watchpoint over a variable is to pause the execution where it is declared, and then place a watchpoint. Breakpoints are used to pause at the declaration statement. For example, if we are going to watch any accesses to size variable in the Stack class, a breakpoint has to be placed at line ‘main.cpp:11’, where the Stack class is instantiated. After size is marked to be watched, any further access is reported, like the default constructor and push function in the Stack class (Figure 23). After any pause for a watchpoint, one can use ‘cont’ to resume the execution until the next breakpoint.

Figure 23: Watching any access to Stack.size.

PIC


2.
Since watchpoints are like break points, one can see a list of all breakpoints using ‘info breakpoints’ or ‘info watchpoints’. A break point can be deleted using ‘delete’, or ‘d’, command. Figure 24 shows how one or all breakpoints are cleared.

Figure 24: Clear breakpoints.

PIC


Watching in Visual Studio

1.
Once a breakpoint pauses a program execution, one can add any live variable around the breakpoint to the watch list. Later he can watch any changes on the variable in the ‘watch’ window. By right clicking over the variable name, once can add its name to the watch list. For example, the variable size is added to watch list in Figure 25.

Figure 25: Watching a variable’s values.

PIC


One can also add any expression to be watched

2.
All the active breakpoints can be deleted or disabled from ‘Debugging’ menue.