Notes on Java Debugger (NetBeans)

Some notes on using NetBeans Java Debugger. They also apply in principal to other Java debuggers.

1, Set line breakpoint (the most common breakpoint), optionally make them conditional breakpoint to filter out irelevant hits. Sometimes I found conditional breakpoint doesn't seem to work and the execution always pauses regardless of the condition.

2, Use field breakpoint to intercept field access and/or modification. This is very useful to track all access and mutation of the field without having to inspect its setter and getter. And some fields don't even have setter or getter methods.

3, Use method breakpoint to inspect method entry and exit. This is just a variation of line breakpoint.

4, Use class breakpoint to track class loading and unloading. This is useful when you just have a vague idea that a certain class will be executed but don't know which part. When the class breakpoint is hit, you can then note down the calling stack trace.

5, Exception breakpoint is also available but haven't tried that.

6, Rewind (step back) the calling sequence by popping off the top element in the calling stack. You can do it from Debugger menu, or the context menu of a particular thread.

7, Add debug buttons to tools bar, including "Attach Debugger ..." button. If "Attach Debugger button is not included by default, you can customize the tool bar by dragging it into the bar. This is very convenient for attaching debugger to remote java process.

8, Apply code changes dynamically to the debugging session, without restarting the remote java application. Debug > Apply Code Changes, or Apply Code Changes button in tool bar. Note that the code changes are not effective in current debugging session. To make it permanent, need to rebuild and restart the remote application.

9, You can start multiple debugging sessions by attaching to different java processes. For example, to debug both the client side and server side operations. Pay attention to where the execution is, the client side, or the server side? Sometimes you can't tell where you are by just looking at the current class name, because some classes are loaded by both the client side and the server side programs.

10, To copy the current stack trace, go to Debug > Evaluate Express, enter
The stack trace will appear in the application's log file or console.

11, If you need to step into source code outside your main project, get hold of its source code if available, and create a IDE project off it. That may involve checking it out from different repo, with different scm tool, or download source zip/jar files from maven repo. A decompiler may be able to handle some simple classes, but tend to produce incomplete source code for large, complext classes.

12, Always attach JDK source code and javadoc to your IDE. NetBeans Tools > Java Platform > JDK 1.6 > Sources. You will need to download the JDK source installer and install it to disk. Source files for JDK classes are located in j2se/src/share/classes.

Finally, shortcuts:

Step Over        F8
Step Into F7
Continue F5
Breakpoints Ctrl-Shift-5
Variables Ctrl-Shift-1
Watches Ctrl-Shift-2
Terminate Debug Shift-F5