M.4. Controlling Execution Using the step, finish and next Commands

Sometimes you will need to execute a program line by line to find and fix errors. Walking through a portion of your program this way can help you verify that a function's code executes correctly. In this section, you will learn how to use the debugger for this task. The commands you learn here allow you to execute a function line by line, execute all the statements of a function at once or execute only the remaining statements of a function (if you have already executed some statements within the function). Once again, we assume you are working in the directory containing this appendix's examples and have compiled for debugging with the -g compiler option.


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


Setting a breakpoint. Type break 25 to set a breakpoint at line 25.


Running the program. Run the program by typing run. After the program displays its two output messages, the debugger indicates that the breakpoint has been reached and displays the code at line 25. The debugger and program then pause and wait for the next command to be entered.



Using the step command. The step command executes the next statement in the program. If the next statement to execute is a function call, control transfers to the called function. The step command enables you to enter a function and study the individual statements of that function. For instance, you can use the print and set commands to view and modify the variables within the function. Type step to enter the debit member function of class Account (Fig. M.2). The debugger indicates that the step has been completed and displays the next executable statement (Fig. M.21)in this case, line 33 of class Account (Fig. M.2).

Figure M.21. Using the step command to enter a function.

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

 (gdb) step
 Account::debit (this=0xbffffd70, amount=13) at Account.cpp:33
 33 if ( amount <= balance ) // debit amount does not exceed balance


Using the finish command. After you have stepped into the debit member function, type finish. This command executes the remaining statements in the function and returns control to the place where the function was called. The finish command executes the remaining statements in member function debit, then pauses at line 28 in main (Fig. M.22). In lengthy functions, you may want to look at a few key lines of code, then continue debugging the caller's code. The finish command is useful for situations in which you do not want to continue stepping through the entire function line by line.

Figure M.22. Using the finish command to complete execution of a function and return to the calling function.

 (gdb) finish
 Run till exit from #0 Account::debit (this=0xbffffd70, amount=13)
 at Account.cpp:33
 main () at figM_03.cpp:28
 28 cout << "account1 balance: " << account1.getBalance() << endl;


Using the continue command to continue execution. Enter the continue command to continue execution. No additional breakpoints are reached, so the program terminates.


Running the program again. Breakpoints persist until the end of the debugging session in which they are seteven after execution of the program, all breakpoints are maintained. The breakpoint you set in Step 2 will be there in the next execution of the program. Type run to run the program. As in Step 3, the program runs until the breakpoint at line 25 is reached, then the debugger pauses and waits for the next command (Fig. M.23).

Figure M.23. Restarting the program.

 (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


Using the next command. Type next. This command behaves like the step command, except when the next statement to execute contains a function call. In that case, the called function executes in its entirety and the program advances to the next executable line after the function call (Fig. M.24). In Step 4, the step command enters the called function. In this example, the next command causes Account member function debit to execute, then the debugger pauses at line 28.

Figure M.24. Using the next command to execute a function in its entirety.

 (gdb) next
 28 cout << "account1 balance: $" << account1.getBalance() << endl;


Using the quit command. Use the quit command to end the debugging session (Fig. M.25). While the program is running, this command causes the program to immediately terminate rather than execute the remaining statements in main.

Figure M.25. Exiting the debugger using the quit command.

 (gdb) quit
 The program is running. Exit anyway? (y or n) y

In this section, you learned how to use the debugger's step and finish commands to debug functions called during your program's execution. You saw how the next command can be used to step over a function call. You also learned that the quit command ends a debugging session.

C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627
Simiral book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net