The fundamental idea behind interactive debugging is to discover critical points in your program where errors occur. To do this, you can stop execution at various places and look at the values of critical variables. In addition, you may observe your program execution and check its flow of control.
When you check the values of critical variables (those you believe to be important in causing your program's problems), the debugger will tell you what values the variables actually hold - this may not be the same as what you want them to hold, or think they should hold. If the value of some variable is not what you expected, you need to search for the reasons that the value is incorrect. Was it read improperly? Or calculated improperly? Or not passed back from a subprogram correctly?
The flow of control in a program is the order in which statements are executed. You can observe this flow by running the program under the debugger, which at all times displays a pointer to the statement to be executed next. Is the flow of control what you think it should be? If not, you may discover dangling elses, missing subprogram calls, infinite loops, and other semantic errors.
Running & Debugging Your Program: Debugger Windows & Commands
The Codewarrior interactive debugger is actually a program separate from the program you use to compile and run your C++ program. To utilize the debugger, activate it while your project and program windows are visible. To do this, choose Enable Debugger from the Project menu. Then, choose Debug from the Project menu to start your program executing under the debugger's control.
After you have started the run, a new window will appear on the screen, Std C++ Console PPC. This window is called the program window. This window shows you three things: first, your program, in the source pane. An arrow called the current statement arrow points at the next statement to be executed. In the upper left, you will see the stack or call chain pane. This pane shows you the current sequence of active subprogram calls. Finally, you will see a pane which displays names of variables, and their current values. This is the variables or locals pane.
In the variables pane, you will see variable names in the left hand column. Then as you run the program, you will see the values of these variables displayed in the right column of this pane. You will see local variables (declared in the currently executing function) displayed here.
Next you can start execution of your program, under the debugger's control. There are several options for controlling your program's execution, using the debugger's Debug menu.
First, choose Run from the Project menu to start your program executing. If there are breakpoints, execution will stop before a statement they point at is reached (see below). Otherwise execution will continue until the program finishes, unless you do something to stop it.
You can use Stop under the Debug menu to suspend execution of your program. That is, it will stop, but you can re-start the run from where it left off if you like by choosing Run.
Choosing Kill will completely halt execution of your program.
When you kill your program, the Std C++ Console PPC window will disappear. When you choose Run again, this program window will re-appear.
You can use Step Over, Step Into, and Step Out to execute your code one statement at a time. The Step-Over command steps over function calls, keeping the current statement arrow within the current block. The Step-Into command moves the current statement arrow into a called function. Step-out allows you to exit a function that you are currently in.
As you execute your code under the debugger, keep in mind that the primary tasks are toexamine the values of critical variables, and observe the program's flow of control. The overall goal, of course, is to discover points in the program where errors occur.
A Special Note on Infinite Loops
If your program contains an infinite loop, you can easily detect it using Step Over, Step Into, or Step Out. At some point you will see the program get "stuck" inside some loop. In this situation the current statement arrow will just keep going through the same code over, and over, and over, and over... In this case, choose Kill under the Control menu to halt execution.
Using Breakpoints
Sometimes even stepping with Step Over will be too slow. You may want to run your program at normal speed up to a certain point, then stop it and check the values of critical variables at that point. Breakpoints are specific statement(s) at which you want execution to be interrupted.
To insert a breakpoint in your code, click on the gray line beside a statement. The line will become a red full circle, indicating that the breakpoint is set. When you run the program now under the debugger's control, execution will automatically halt at this breakpoint.
You can insert as many breakpoints as you like, but only in front of executable C++ statements (not comments, declarations, etc.).
To remove a breakpoint, click on the full red circle so that it becomes a flat gray line again. To remove all breakpoints simultaneously, select Clear All Breakpoints from the Edit menu.
To run a program at normal speed until a breakpoint is encountered, choose Run from the debugger's Project menu. Once the program has been interrupted by a breakpoint, you can resume execution or debug using any of the commands described above. The stepping commands may prove particularly useful here.
Changing the Value of a Variable "On the Fly"
You can answer questions such as, "Well, what if the variable X did have the correct value of 2.3 instead of this wrong value?" by actually altering the value of the variable during the run, via the debugger. In the variables pane, double-click on the value of the variable desired. Then, just type in a new value. When you resume execution, X will have the value you just typed in.
Function Pop-Up Menu If you click on the {} displayed at the bottom left of the program window, you will see a list of the subprograms contained in your program, in their order of declaration. If you then select a particular subprogram in the list, it will immediately be displayed in the source pane.
Viewing Data Types You can check the data types of your variables by having them displayed in the locals pane. To do this, select the pane first by clicking on it. Then, choose Show Types from the Data menu.
Displaying Values in a Structured Type Variable
To see all of the values in an array or struct variable in the locals pane, you must click on the small gray triangle which points at that variable's name in the variables pane. You will see the values displayed below the name of the variable. As an alternative, you can double-click on the variable name itself, and the values of its components will be displayed in a new window.
Compiler and Debugger Preferences
To change Codewarrior's settings, select PPC Std C++ Console Settings under the Codewarrior Edit menu. Choose Language Settings or Debugger to alter these preferences. You can also alter certain debugger settings by choosing Preferences under the Edit menu.
For Further Information
Full documentation on the Codewarrior debugger is available on-line at campus computing sites.