Debugging

VisualAge for Java includes an integrated visual debugger with a rich set of features. This section outlines some of these features.

Opening the debugger

You can open the debugger manually by selecting Debug, Debugger from the Window menu. If a program is running, you can suspend its thread and view its stack and variable values. Alternatively, the debugger will automatically open, with the current thread suspended, for any of several reasons:

Setting breakpoints

When a program is running in the IDE and encounters a breakpoint, the running thread is suspended and the Debugger browser is opened so that you can work with the method stack and inspect variable values. In the IDE, you can set breakpoints in any text pane that is displaying source. Suppose that you want to set a breakpoint in the writeToDoFile method in the ToDoList class from the To-Do List program.

To set this breakpoint:

  1. Select the ToDoList class in the Workbench. Expand the class to show its methods.
  2. Select the writeToDoFile method. The source for the method is shown in the Source pane.
  3. Double-click mouse button 1 in the left margin of the Source pane beside the following line (in the loop that writes items):
    dataOutStream.write(fillList.get(i)+crlf);
  4. A breakpoint indicator appears in the margin of the Source pane beside this line:

You can also set a breakpoint on a line that does not already have a breakpoint by following these steps:

  1. Move the cursor to the line.
  2. Click mouse button 2 and select Breakpoint from the pop-up menu.

Removing breakpoints

To remove a breakpoint in a source pane, double-click on the breakpoint indicator. You can also remove a breakpoint by following these steps:

  1. Move the cursor to the line.
  2. Click mouse button 2 and select Breakpoint from the pop-up menu.

Try removing the breakpoint you just set. Now reset it. You will be using this breakpoint in the next section to examine the features of the Debugger browser.

Using the Debugger browser

The Debugger browser opens automatically when the program you are executing reaches an active breakpoint or has an unhandled exception.

Now that we have set a breakpoint, let's run the To-Do List program to see what happens:

  1. In the Workbench, select the ToDoList class. Select the Run toolbar button
    trunicon.gif (1013 bytes).
  2. When the To-Do File program appears, add at least three items to the To-Do List and then select Save To-Do File. When the Save To-Do File dialog appears, enter a file name and select Save. The Debugger browser appears. It should look like this:

    The thread you are debugging is selected in the All Programs/Threads list. The list of methods below the thread is the current stack. When you select a method in the stack the Visible Variables pane shows its visible variables. The Source pane shows the source where the breakpoint is set.

  3. Select the Resume toolbar button to continue execution of the program.

    Because this breakpoint is inside a loop that writes each item to the file, the thread is suspended again and the Debugger window displays it.
  4. Examine some of the variables in the Visible Variables list. For example, to see the value of the loop counter variable i, select int i from the Visible Variables pane (it's at the bottom of the list). Its value appears in the Value pane:

    This value of the loop counter is exactly what you would expect after the loop has been executed once.

  5. Now let's disable this breakpoint:
  6. You can update and save code in the Source pane of the Debugger window. When you resume execution of the program, you see the changes you made to the code. For example, suppose that you wanted to change the writeToDoFile method so that items were written to the file in reverse order. You could make this change by modifying the beginning of the for loop to look like this:
    for (int i = fillList.getSize()-1; i >= 0; i--) {

    Make this change in the Source pane of the Debugger page, and then select Save from the Edit menu.

  7. Now select Resume from the tool bar to continue execution of the program. In the To-Do File program, add the following values to the To-Do List and then select Save To-Do File to save them to a file:
  8. Now select Open To-Do File and open the file you just saved. The To-Do List should look like this:

Before you continue, return to the Breakpoints page and enable your breakpoints again by clicking  the Global Enable Breakpoints button into the down position.

Other things you can do with the integrated debugger

The debugger has many other features that you will find helpful for debugging your programs. To learn more about the following tasks, as well as others, see the online help for the integrated debugger.

Set conditional breakpoints
Sometimes you want a breakpoint to suspend the thread only under certain conditions. A breakpoint can be configured so that an expression is evaluated before the debugger decides to suspend execution. If the expression includes a boolean that evaluates to true, the breakpoint suspends execution as usual. If it evaluates to false, the breakpoint is ignored.

To configure a breakpoint, click mouse button 2 on its symbol in the margin of a source pane. Select Modify from the pop-up menu. Enter the expression in the field. See the online help for the integrated debugger for more details on configuring breakpoints.

Set external and caught exception breakpoints
Besides setting breakpoints in code in the workspace, you can also set breakpoints on methods in external classes (classes that reside outside the workspace, in the file system, and that are loaded at run-time). You can also specify exception types that will break execution if they are thrown, even if your code catches and handles them. See the online help for the integrated debugger for more details on external breakpoints and breakpoints on caught exceptions.

Step through code
When a running thread has been suspended, you can step through code line by line or method by method, in a variety of ways. This is a controlled way of checking variable values at each point in your program.

Using inspectors to view and modify variables
You can open an inspector window to look closely at a particular variable in a suspended thread. The inspector lets you view and modify variable values and evaluate expressions.