Wednesday, August 15, 2012

Eclipse debug options

http://www.vogella.com/articles/EclipseDebugging/article.html

For debugging we will create an example project. Create a Java project with the name de.vogella.combug.first and add the package de.vogella.combug.first. Also create the following classes.

package de.vogella.combug.first;

public class Counter {
  private int result = 0;

  public int getResult() {
    return result;
  }

  public void count() {
    for (int i = 0; i < 100; i++) {
      result += i + 1;
    }
  }
} 


package de.vogella.combug.first;

public class Main {
  
/** * @param args */
public static void main(String[] args) { Counter counter = new Counter(); counter.count(); System.out.println("We have counted " + counter.getResult()); } }

4. Debug

4.1. Setting Breakpoints

To set breakpoints right click in the small left margin in your source code editor and select "Toggle Breakpoint". Or you can double-click on this position.

Setting a breakpoint

For example in the following screenshot we set an breakpoint on the line Counter counter = new Counter();.

Showing a defined breakpoint

4.2. Starting the Debugger

To debug your application, select a Java file which contains a main method, right click on it and select Debug AsJava Application.

Start the debugger

If you have not defined any breakpoints, this will run your program as normal. To debug the program you need to define breakpoints.
If you start the debugger the first time, Eclipse asks you if you want to switch to the debug perspective. Answer "yes". You should then see a perspective similar to the following.

Switch to perspective

You can use F5 / F6, F7 and F8 to step through your coding. The meaning of these keys is explained in the following table.
Table 1. Debugging Key bindings
Command Description
F5 Goes to the next step in your program. If the next step is a method / function this command will jump into the associated code.
F6 F6 will step over the call, i.e. it will call a method / function without entering the associated code.
F7 F7 will go to the caller of the method/ function. This will leave the current code and go to the calling code.
F8 Use F8 to go to the next breakpoint. If no further breakpoint is encountered the program will run normally.


These commands are also available in the toolbar of Eclipse. The following picture displays the buttons and their related keyboard shortcuts.

Debug Shortcuts

4.3. Stack

The call stack show the parts of the program which are currently executed and how they relate to each other. The current stack is displayed in the "Debug" View.

Showing the stack view

4.4. Evaluating variables

The Variables Ciew displays fields and local variables from the current stack. Please note you need to run the debugger to see the variables in this View.

Variables View

Use the drop-down menu to display static variables.

Drop-down menu for static variables

Via the drop-down menu you can also customize the displayed columns. For example, you can show the actual type of each variable declaration. For this select LayoutSelect Columns...Type.

Showing the actual type of the variables

Another nice feature is the "New Detail Formatter" in which you can define how a variable is displayed. For example the toString() method in our Counter class shows something meaningless, e.g. de.vogella.combug.first.Counter@587c94. Right mouse on the variableand select "New Detail Formater".

New Detail Formater Screenshot

Add the following code to get the current value of result variable in the Counter class of this example.

Detailer formater example

5. Advanced Debugging

The following section shows advanced options for debugging. It is not based on the example introduced previously.

5.1. Skip all breakpoints

If you want to temporary de-activate all your breakpoints you can press the button "Skip all breakpoints". This button is visible, if you select the "Breakpoints" tab.
If you press this button again, your breakpoints will be reactivated.

De-activating all breakpoints

5.2. Breakpoint Properties

After setting a breakpoint you can select the properties of the breakpoint, via right-clickBreakpoint Properties. You can define a condition that restricts when the breakpoint will become active.
For example, specify that the breakpoint should only be active after it has been reached 12 or more times (Hit Count).
Or, you can put in a conditional expression (which you can also use for logging). Execution will stop at the breakpoint, if the condition evaluates to true.

Breakpoint Properties


Breakpoint Properties

5.3. Watchpoint

A watchpoint is a breakpoint set on a field. The debugger will stop whenever that field is read or changed.
You can set a watchpoint by double-clicking on the left margin, next to the field declaration. In the properties of a watchpoint you can define if the execution should stop during read access (Field Access) and during write access (Field Modification).

Watchpoint

5.4. Exception Breakpoint

You can also set breakpoints which are triggered when exceptioins are thrown. To define an exception breakpoint click on the "Add Java Exception Breakpoint" icon in the "Breakpoints" View toolbar.

Exception Breakpoint

You can define if you want to stop for caught and / or uncaught exceptions.

5.5. Method Breakpoint

A method breakpoint is defined by double-clicking in the left margin of the editor next to the method header.
You can define if you want to stop the program before entering or after leaving the method.

Method Breakpoint

5.6. Class Load Breakpoint

A Class Load Breakpoint will stop when the class is loaded.
To set a class load breakpoint, right-click on a class in the Outline View and choose "Toggle Class Load Breakpoint".

Toogle class load breakpoint

5.7. Hit Count

For every breakpoint you can define a hit count in it's properties. The application is stopped once the breakpoint is reached the number of times defined in the hit count.

5.8. Drop to frame

Eclipse allows you to select any level (frame) in the call stack during debugging and set the JVM to restart from that point.
This allows you to rerun a part of your program. Be aware that variables which have been modified by code that already run will remain modified.
Changes made to variables or external data, e.g. files, databases, will not be reset when you drop to a previous frame.
To use this feature, select a level in your stack and press the "Drop to Frame" button in the toolbar of the "Debug" View.
For example you can restart your "for" loop. The field "result" will not be reseted in this case.


No comments:

Post a Comment