www.gibmonks.com

Main Page

Previous Page
Next Page

[Page 1389 (continued)]

M.3. The print and set Commands

In the preceding section, you learned how to use the debugger's print command to examine the value of a variable during program execution. In this section, you will learn how to use the print command to examine the value of more complex expressions. You will also learn the set command, which allows the programmer to assign new values to variables. We assume you are working in the directory containing this appendix's examples and have compiled for debugging with the -g compiler option.

1.
Starting debugging. Type gdb figM_03 to start the GNU debugger.

2.
Inserting a breakpoint. Set a breakpoint at line 25 in the source code by typing break 25 (Fig. M.15).

Figure M.15. Setting a breakpoint in the program.

 (gdb) break 25
 Breakpoint 1 at 0x8048871: file figM_03.cpp, line 25.
 (gdb)


3.
Running the program and reaching a breakpoint. Type run to begin the debugging process (Fig. M.16). This will cause main to execute until the breakpoint at line 25 is reached. This suspends program execution and switches the program into break mode. The statement in line 25 is the next statement that will execute.

Figure M.16. Running the program until the breakpoint at line 25 is reached.
(This item is displayed on page 1390 in the print version)

 (gdb) run
 Starting program: /home/student/Debug/figM_03
 account1 balance: $50

 Enter withdrawal amount for account1: 13

 attempting to subtract 13 from account1 balance


 Breakpoint 1, main () at figM_03.cpp:25
 25         account1.debit( withdrawalAmount ); // try to subtract from
 account1
 (gdb)


4.
Evaluating arithmetic and boolean expressions. Recall from Section M.2 that once the program has entered break mode, you can explore the values of the program's variables using the debugger's print command. You can also use the print command to evaluate arithmetic and boolean expressions. Type print withdrawalAmount - 2. Note that the print command returns the value 11 (Fig. M.17). However, this command does not actually change the value of withdrawalAmount. Type print withdrawalAmount == 11. Expressions containing the == symbol are treated as boolean expressions. The value returned is false (Fig. M.17) because withdrawalAmount does not currently contain the value 11withdrawalAmount is still 13.


[Page 1390]


Figure M.17. Printing expressions with the debugger.

 (gdb) print withdrawalAmount - 2
 $1 = 11
 (gdb) print withdrawalAmount == 11
 $2 = false
 (gdb)


5.
Modifying values. The debugger allows you to change the values of variables during the program's execution. This can be valuable for experimenting with different values and for locating logic errors in programs. You can use the debugger's set command to change the value of a variable. Type set withdrawalAmount = 42. The debugger changes the value of withdrawalAmount. Type print withdrawalAmount to display its new value (Fig. M.18).

Figure M.18. Setting the value of a variable while in break mode.

 (gdb) set withdrawalAmount = 42
 (gdb) print withdrawalAmount
 $3 = 42
 (gdb)


6.
Viewing the program result. Type continue to continue program execution. Line 25 of Fig. M.3 executes, passing withdrawalAmount to Account member function debit. Function main then displays the new balance. Note that the result is $8 (Fig. M.19). This shows that the preceding step changed the value of withdrawalAmount from its initial value (13) to 42.

Figure M.19. Using a modified variable in the execution of a program.
(This item is displayed on page 1391 in the print version)

 (gdb) continue
 Continuing.
 account1 balance: $8

 Program exited normally.
 (gdb)


7.
Using the quit command. Use the quit command to end the debugging session (Fig. M.20). This command causes the debugger to terminate.

Figure M.20. Exiting the debugger using the quit command.
(This item is displayed on page 1391 in the print version)

 (gdb) quit
 ~/Debug$


In this section, you learned how to use the debugger's print command to evaluate arithmetic and boolean expressions. You also learned how to use the set command to modify the value of a variable during your program's execution.


Previous Page
Next Page