Debugging Dwight Deugo dwightespirity com Nesa Matic nesaespirity
Debugging Dwight Deugo (dwight@espirity. com) Nesa Matic (nesa@espirity. com)
Additional Contributors n None as of September, 2005 2 © 2003 -2005, Espirity Inc.
Module Road Map 1. Eclipse Debugging 3 © 2003 -2005, Espirity Inc.
Module Road Map 1. Eclipse Debugging § § § Debug Perspective Debug Session Breakpoint Debug Views Breakpoint Types Evaluating and displaying expressions 4 © 2003 -2005, Espirity Inc.
Debugging in Eclipse n The Java Debugger Part of Eclipse Java Development Tools (JDT) q More than System. out. printn( error ) q Detects errors as code executes q Correct errors as code executes q Actions you can perform debugging include: q Control Execution ; Set simple breakpoints ; Set conditional breakpoints ; Review and change variable values ; Hot code replace ; 5 © 2003 -2005, Espirity Inc.
Debug Perspective Threads and Monitor View Variable View Editor View Console View Tasks View 6 Outline View © 2003 -2005, Espirity Inc.
Simple Breakpoint n Breakpoint q q n Stops the execution of a program at the point Thread suspends at the location where the breakpoint is set Setting a breakpoint q q CTRL+Shift+B at current point in editor line Double click in editors marker bar a current line 7 © 2003 -2005, Espirity Inc.
Deleting Breakpoints n n Double click on the breakpoint in the editor CTRL+Shift+B at current point in editor line 8 © 2003 -2005, Espirity Inc.
Starting a Debugging Session n Select Java class containing the following: q q n n main() method Resulting execution will pass breakpoint Select Run Debug As… Java Application Or Select Debug As… Java Application from the debug menu 9 © 2003 -2005, Espirity Inc.
Debug Session n n Execution suspends prior to the line with a breakpoint You can set multiple breakpoints 10 © 2003 -2005, Espirity Inc.
Control Execution From Breakpoint… n Step Into or F 5: q q q n Step Over or F 6 q n For methods, execute method and suspend on first statement in the method For assignments, similar to Step Over For conditionals, similar to Step Over Execute next statement Step Return or F 7 q Resume execution to the end of the method on the next line after it was invoked 11 © 2003 -2005, Espirity Inc.
…Control Execution From Breakpoint n Resume or F 8 q n Continue execution until program ends or another breakpoint is reached Terminate q Stops the current execution thread 12 © 2003 -2005, Espirity Inc.
Variables and Fields n To see the values bound to fields: q q q Use Variables View Select variable in editor and select Inspect Select variable in editor and select Display 13 © 2003 -2005, Espirity Inc.
Code Debugging in this Module public class Debug { private int something = 0; private Vector list = new Vector(); public void first. Method(){ third. Method(something); something = something + 1; } public void second. Method(){ third. Method(something); something = something + 2; } public void third. Method(int value){ something = something + value; } public static void main(String[] args) { Debug debug = new Debug(); debug. first. Method(); debug. second. Method(); } } 14 © 2003 -2005, Espirity Inc.
Variables View n Shows all fields of instance where breakpoint occurred Select this to see all fields q Select any field to see value q If field is bound to an object, you can select Inspect from the menu to view its fields and values q 15 © 2003 -2005, Espirity Inc.
Changing Field Values n To change field value: Select field in Variables view q Select Change Value… from the context menu q Enter new value into Set Variable Value window q Click OK q 16 © 2003 -2005, Espirity Inc.
Expressions View n n Remembers all objects you have inspected Displays the fields of the object q q n You can see the values of the fields You can Inspect the fields Opens when: q q You Inspect an object You click on the Watch from the context menu 17 © 2003 -2005, Espirity Inc.
Breakpoint View n n Lists all available breakpoints Can be used for manipulating breakpoints (through the views menu): q q q n n Enabling Disabling Removing Also displays breakpoints properties Accessed like other debugging views 18 © 2003 -2005, Espirity Inc.
Debug View n Shows: q q q n Active threads Current stack frame when execution has stopped Previous stack frames Method and variables are shown in the editor for the selected frame q Update in the editor updates the source 19 © 2003 -2005, Espirity Inc.
Breakpoint Types n Breakpoints can be set for the following Java entities: Line (simple breakpoint) q Method q Field (Watchpoint) q Java Exception q n Each breakpoint is set a different way and has different properties 20 © 2003 -2005, Espirity Inc.
Method Breakpoints n To set method breakpoint: q q n Select method in the Outline View From context menu select Toggle Method Breakpoint To set breakpoint’s properties: q q q Select breakpoint in editor Select Breakpoint Properties. . from context menu Set properties as desired ; n Entry, exit, enable hit count Execution suspends on entry/exit into method 21 © 2003 -2005, Espirity Inc.
Field Breakpoints n n Also known as watchpoint To set the watchpoint: q q n Select field in the Outline View From context menu select Toggle Watchpoint To set watchpoint’s properties: q q q Select breakpoint in editor Select Breakpoint Properties. . from context menu Set properties as desired ; n Access/modification, enable Execution suspended on access/modification of field 22 © 2003 -2005, Espirity Inc.
Java Exception Breakpoint n To Add Java Exception Point: q q q Select Add Java Exception Point from menu Enter exception type Specify what triggers a breakpoint: ; ; ; Caught exception Uncaught exception Both 23 © 2003 -2005, Espirity Inc.
How To Debug n Here are simple steps for debugging in Eclipse: Set your breakpoints q Hit a breakpoint during execution q Walk/step through code to other breakpoints q Follow along in editor q Inspect/Watch interesting fields q Watch the Console for things to happen q 24 © 2003 -2005, Espirity Inc.
Summary n You have learned: The views in the Debug Perspective q Typical debug session q How to use the Inspector q About the different types of breakpoints q How to set breakpoints q How step around your code doing debugging q 25 © 2003 -2005, Espirity Inc.
Labs! Lab: Debugging in Eclipse 26 © 2003 -2005, Espirity Inc.
- Slides: 26