www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 1393 (continued)]

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)

 (gdb) run
 Starting program: /home/student/Debug/figM_03

 Breakpoint 1, main () at figM_03.cpp:14
 14         Account account1( 50 ); // create Account object
 (gdb)


3.
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)

 (gdb) watch account1.balance
 Hardware watchpoint 2: account1.balance
 (gdb)


4.
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.


[Page 1394]
Figure M.28. Stepping into the constructor.

 (gdb) step
 Account (this=0xbffffd70, initialBalance=50) at Account.cpp:12
 12         balance = 0; // assume that the balance begins at 0
 (gdb) step
 Hardware watchpoint 2: account1.balance

 Old value = 1073833120
 New value = 0
 Account (this=0xbffffd70, initialBalance=50) at Account.cpp:16
 16         if ( initialBalance > 0 )
 (gdb)


5.
Finishing the constructor. Type step to execute line 16, then type step again to execute line 17. The debugger will notify you that data member balance's value has changed from 0 to 50 (Fig. M.29).

Figure M.29. Reaching a watchpoint notification.

 (gdb) step
 17            balance = initialBalance;
 (gdb) step
 Hardware watchpoint 2: account1.balance

 Old value = 0
 New value = 50
 Account (this=0xbffffd70, initialBalance=50) at Account.cpp:20
 20         if ( initialBalance < 0 )
 (gdb)



[Page 1395]
6.
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).

Figure M.30. Entering break mode when a variable is changed.

 (gdb) continue
 Continuing.
 account1 balance: $50

 Enter withdrawal amount for account1: 13

 attempting to subtract 13 from account1 balance

 Hardware watchpoint 2: account1.balance

 Old value = 50
 New value = 37
 0x08048a01 in Account::debit (this=0xbffffd70, amount=13) at Account.cpp:34
 34            balance = balance - amount;
(gdb)


7.
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).


[Page 1396]


Figure M.32. Resetting the watch on a data member.

 (gdb) run
 Starting program: /home/student/Debug/figM_03

 Breakpoint 1, main () at figM_03.cpp:14
 14         Account account1( 50 ); // create Account object
 (gdb) watch account1.balance
 Hardware watchpoint 3: account1.balance
 (gdb) continue
 Continuing.
 Hardware watchpoint 3: account1.balance

 Old value = 1073833120
 New value = 0
 Account (this=0xbffffd70, initialBalance=50) at Account.cpp:16
 16         if ( initialBalance > 0 )
 (gdb)


9.
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.

Figure M.33. Removing a watch.

 (gdb) delete 3
 (gdb) continue
 Continuing.
 account1 balance: $50

 Enter withdrawal amount for account1: 13

 attempting to subtract 13 from account1 balance

 end of function
 account1 balance: $37

 Program exited normally.
 (gdb)


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.


Previous Page
Next Page