M.5. The watch Command

In this section, we present the watch command, which tells the debugger to watch a data member. When that data member is about to change, the debugger will notify you. In this section, you will learn how to use the watch command to see how the Account object's data member balance is modified during the execution of the program.

1.

Starting the debugger. Start the debugger by typing gdb figM_03.

2.

Running the program. Type break 14 to set a breakpoint at line 14. Run the program with the command run. The debugger and program will pause at the breakpoint at line 14 (Fig. M.26).

Figure M.26. Running the program until the first breakpoint.

(This item is displayed on page 1394 in the print version)

Watching a class's data member. Set a watch on account1's balance data member by typing watch account1.balance (Fig. M.27). This watch is labeled as watchpoint 2 because watchpoints are labeled with the same numbers as breakpoints. You can set a watch on any variable or data member of an object currently in scope during execution of the debugger. Whenever the value of a watched variable changes, the debugger enters break mode and notifies you that the value has changed.

Figure M.27. Setting a watchpoint on a data member.

(This item is displayed on page 1394 in the print version)

Continuing the program. Step into the Account constructor with the command step. The debugger will display line 12 of Fig. M.2, which is the first line in the constructor. Use the step command again to execute this line of code. The debugger will now notify you that data member balance's value will change (Fig. M.28). When the program begins, an instance of Account is created. When the constructor for this object runs, data member balance is first assigned the value 0. The debugger notifies you that the value of balance has been changed.

Withdrawing money from the account. Type continue to continue execution and enter a withdrawal value at the prompt. The program executes normally. Line 25 of Fig. M.3 calls Account member function debit to reduce the Account object's balance by a specified amount. Line 34 of Fig. M.2 inside function debit changes the value of balance. The debugger notifies you of this change and enters break mode (Fig. M.30).

Continuing execution. Type continuethe program will finish executing function main because the program does not attempt any additional changes to balance. The debugger removes the watch on account1's balance data member because the variable goes out of scope when function main ends. Removing the watchpoint causes the debugger to enter break mode. Type continue again to finish execution of the program (Fig. M.31).

Figure M.31. Continuing to the end of the program.

(gdb) continue
Continuing.
end of function
account1 balance: $37
Watchpoint 2 deleted because the program has left the block in
which its expression is valid.
0x4012fa65 in exit () from /lib/libc.so.6
(gdb) continue
Continuing.
Program exited normally.
(gdb)

8.

Restarting the debugger and resetting the watch on the variable. Type run to restart the debugger. Once again, set a watch on account1 data member balance by typing watch account1.balance. This watchpoint is labeled as watchpoint 3. Type continue to continue execution (Fig. M.32).

Removing the watch on the data member. Suppose you want to watch a data member for only part of a program's execution. You can remove the debugger's watch on variable balance by typing delete 3 (Fig. M.33). Type continuethe program will finish executing without reentering break mode.

In this section, you learned how to use the watch command to enable the debugger to notify you of changes to the value of a data member throughout the life of a program. You also learned how to use the delete command to remove a watch on a data member before the end of the program.