Tutorial - Eclipse: Advanced Debugging

Advanced Debugging

This tutorial is on some of the advanced features that eclipse provides for debugging. There is also a tutorial on the basics of debugging available.

Changing a Variable's Value

Changing a variable's value is often a useful thing to do when debugging code to find out if a bug occurs only for a single value of some variable or if it is a more general problem. Variables can be changed when a program is suspended and then when the code is resumed the variables will be set to their new values. To set a variable go to the Variables view and select the variable you want to change. Right click on the variable and select Change Value... Once you have change the value click ok. The variable now has a new value.

variables.jpg

Breakpoints and Watchpoints Properties

Breakpoints and watchpoints have some properties that you may want to change from time to time. Both can have a Hit Count this can be set to some integer value greater than 0. Setting this to say 5 would mean that the fifth time that the program got to this breakpoint the program will be suspended. This can be useful in a loop where there is a bug in the 103rd loop. Setting a Hit Count of 103 is much easier than click resume 103 times. Watch points also have Field Access and Field Modification check-boxes. When checked the Field Access box will suspend the program every time a field is read (accessed). When checked the Field Modification box will suspend the program every time a field is changed. These can be helpful when you know that a field is being changed or used somewhere but you don't know where. Breakpoints also have conditional breakpoints but they are covered in the next section of this page.

properties.jpg

Conditional Breakpoints

Sometimes a simple breakpoint just won't do. For example say you have a bug inside loop that loops several hundred times but the bug only occurs on the 304 time. A break point on some line in the loop will mean that you will need to resume the program 303 times before you get to the bug. Eclipse has given us a better way to do this, conditional breakpoints. Conditional breakpoints are set on a line just like normal breakpoints. The difference is that they only stop the program at that line if some condition is true. In our example above we might set a conditional breakpoint on a line in the loop and set the condition that the counter equals 303. This would then mean that the program will only be suspended on that line when counter equals 303. To set a conditional breakpoint just set a breakpoint as normal then right click on the breakpoint and select Breakpoint Properties. In the window that opens click Enable Conditional. Then type in counter (or some variable name) == 303 (or whatever value you want to set).

conditional.jpg

The Display View

The display view is one of the most interesting. It is possibly also one of the least useful. It is like a scrapbook, you can just paste or write code into it. Where it becomes interesting is that the code can then be highlighted and executed in the current context of the program. This might be useful for locating bugs in loops or trying a change to the code. The way you execute the code is to write or paste code in the display view. Highlight the code you want to execute. Right click on the highlighted code and select execute.

display.jpg

Starting up | Hello World | Running Programs | Debugging | Export to Jar