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