Debugging a Program with Code Warrior

Starting the Debugger

To use the debugger, choose

   Project -> Enable Debugger

followed by

   Project -> Debug

A new window should appear titled something like TaxTotals.class (Thread 0x6F94DD0). For convenience, we will refer to it as the debugging window. This window has three panes:

Above the three panes is a row of control buttons that can be used to execute the program in a controlled fashion.

  1. The first (leftmost) button is the Run button that runs the program in normal fashion. This button is useful if you have a logic error and wish to find the statement at which your program is crashing.
  2. The second button is the Stop button that can be used to halt an executing program. This is particularly useful for halting the execution of a program containing an infinite loop
  3. The third button is the Kill button which dismisses the debugging window.
  4. The fourth button is the Step Over button, which is one of the most useful debugging operations. This button lets you execute the program one line at a time, stepping "over" any method calls. This operation can also be performed by choosing Debug -> Step Over, or by using the keyboard shortcut Cntrl-s.
  5. The fifth button is the Step Into button, which another useful operation. It also allows you to execute your program a statement at a time, but if the statement contains a method call, this button will step "into" that method, letting you trace its execution the same way. This operation can also be performed by choosing Debug -> Step Into, or by using the keyboard shortcut Cntrl-t.
  6. The six button is the Step Out button. If you ever step into a method that you wish you hadn't, this operation runs the remainder of that method. This operation can also be performed by choosing Debug -> Step Out, or by using the keyboard shortcut Cntrl-u.

Note also that a new Debug menu has appeared in place of the Window menu.

The blue arrow should be pointing at the open curley-brace of the main function in Begin by using step over to move to the first statement, which is an output statement. Rather than stepping into the call to readInt(), use step over to move to the next statement. (We find Cntrl-s to be the most convenient method.) The debugger will wait for you to enter the number of employees. Locate the application (JBoundApp.dbg) and type your input value in there.

Using step over, step through the remainder of the program, one line at a time. If necessary, you can always use Kill followed by Project -> Debug to begin again at the program's beginning.

Remember that when you reach the input statement, you need to enter data in the application window. Enter the data (e.g., 3) and continue stepping. In particular, watch how the program behaves when execution reaches the for loop.


Examining Object Values

A nice feature of this debugger is that every accessible variable is displayed in the Variables pane. When a statement executes that changes the value of a given variable, CodeWarrior highlights that particular variable in red. This allows you to examine the values of your program's variables and see how the execution of a statement affects them.

Note also that when you reach the end of the for loop, step over takes you back to its beginning, and count turns red and its value changes. Keep stepping through the statements until you understand how a loop causes a sequence of statements to be repeated.



As was mentioned earlier, you can always restart the debugger at the beginning of the program by choosing Debug -> Kill and then choosing Project -> Debug.

However, if you continue stepping until you reach the end of the program, the CodeWarrior debugger will display the console window when the program terminates. You can then quit your application in the usual manner and choose Project -> Debug if you want to debug again.

Using either of these methods, restart the debugger at the program's beginning.


Stepping Into and Out Of a Method

While there are situations where we want to step over a method (such as a module method like readInt() in class Integer), there are other situations where we want to step into the method. For example, to examine the behavior of our computeTotalCost() method, we might want to step into it, rather than over it.

To illustrate, run your program using step over and enter some value less than the luxury tax minimum for cost and a value for rate. Then continue to step over until execution reaches the line where computeTotalCost() is called. If you now step into computeTotalCost(), the blue arrow should "jump" from the call in to the first line of method computeTotalCost() in Note that the Variables pane changes, showing the method's parameters and their values. The objects from the main function disappear since they cannot be accessed within computeTotalCost().

You can then step through this method using the step over or step into methods as we have seen before.

Note how the program behaves when execution reaches the if statement: the code that computes "normal" cost is performed, while the code that computes luxury tax is skipped. Continue stepping until you reach the end of the method. When you step again, you should return to the statement where computeTotalCost() was called. Step again and you can see in the Variables pane the return value from computeTotalCost() that is assigned to totalCost.

Continue stepping through the program, and when it pauses for input again, this time enter a value greater than the luxury tax minimum for cost (and a value for rate). When the program stops at computeTotalCost() again, step into computeTotalCost() and note that this time control skips the 'normal' part of the if but moves through its luxury tax part.

As we said earlier, if at any time you want to leave a method (this is especially useful if you mistakenly step into a system method), choose the step out operation. This will complete the execution of the method currently executing and stop back in its caller.


Quitting the Debugger

Whenever you want, you can quit using the debugger and return to the normal CodeWarrior environment by using the Kill operation, followed by Project -> Disable Debugger.



The key things to remember in using the debugger are these:

I make it a practice to remember the two keyboard shortcuts (Cntrl-s and Cntrl-t), because I use them most frequently when debugging. I use the other debugging commands much less frequently, and so I tend to use the menu or button choices for them:



Back to the Lab

Back to the Table of Contents

Back to the Introduction

Copyright 2000 by Prentice Hall. All rights reserved.