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.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism


Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger


show all menu

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

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