Robins blogg

2007-02-01

Java debugging tip - tracing

Filed under: — site admin @ 23:02

I almost accidentally discovered a debugging trick in java. Often doing lots of System.out.println() is the best way to debug code. Adding a line is usually not a problem, but sometimes it is, e.g. when debugging a remote application or if you want to trace a method call whose source is not easily available for modification. With a debugger, such as the one in Eclipse it’s easy. The key is that you can have conditional breakpoints whose condition is any java code snippet. So here’s how to do it.


Place a conditional breakpoint at the interesting place. You do that by placing a breakpoint the usual way. Then rightclick on the breakpoint symbol to the left in the editor and select Breakpoint properties.


Mark “Enable condition” and type some code. You can get pretty wild about what your code does here. It even runs your code, not just standard java stuff.


Click ok and do something and watch all those invocations of the Throwable(String) contructor in Eclipse’s Console window.

Note that I didn’t write the name of the constructor argument. That’s because the java runtime doesn’t know the name of the argument when code isn’t compiled with full debugging information. Eclipse then synthesizes names, i.e. arg0, arg1 etc. For code with full debug information you use the declared name of the argument).

Powered by WordPress