Skip to content

Debugging Java

December 3, 2007

Well I have already worked on points 1 and 2.
1* Basic – breakpoints
2* Primal – debug messages

  • JDK Logging
  • Apache Commons Logging
  • Log4j

Thread.currentThread().getStackTrace();

3* Hot Shot – dynamic proxy
It it only implements the immediate interfaces implemented by the class of the instance being wrapped by the proxy. It does not implement any interface of its’ ancestors. This can be easily fixed by traversing the list of ancestors and picking all the interfaces along the way.

{ font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }–>



public interface Foo {
Object bar(Object obj) throws BazException;
}
public class FooImpl implements Foo {
Object bar(Object obj) throws BazException {// ...}
}

public class DebugProxy implements java.lang.reflect.InvocationHandler {
private Object obj;
public static Object newInstance(Object obj)
{

return java.lang.reflect.Proxy.newProxyInstance(
obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(),
new DebugProxy(obj));
}

private DebugProxy(Object obj)
{

this.obj = obj;
}

public Object invoke(Object proxy, Method m, Object[] args)throws Throwable
{
Object result;
try {
System.out.println("before method " + m.getName());
result = m.invoke(obj, args);
} catch (InvocationTargetException e) {
throw e.getTargetException();
} catch (Exception e) {
throw new RuntimeException("unexpected invocation exception: " +
e.getMessage());
} finally {
System.out.println("after method " + m.getName());
}
return result;
}
}

//To construct a DebugProxy for an implementation of the Foo interface and call one of its methods:
Foo foo = (Foo) DebugProxy.newInstance(new FooImpl());
foo.bar(null)

http://java.sun.com/j2se/1.3/docs/guide/reflection/proxy.html#examples

It is best when: It’s a great solution for event handlers. You can set up a dummy event handler with a generic proxy in seconds and see the sequence of events. This is simplest and quickest method when it comes to understanding event handlers.

4* Brute Force – run-time profiler
A profiler is a performance analysis tool that measures the behavior of a program as it runs, particularly the frequency and duration of function calls. The output is a stream of recorded events (a trace) or a statistical summary of the events observed (a profile). Profilers use a wide variety of techniques to collect data, including hardware interrupts, code instrumentation, operating system hooks and performance monitors.

It is best when: You want a complete picture for a very specific operation (i.e. very short execution flow). Best to use eclipse testing and performance tools (TPTP)

5* New Age – through aspects (Use AOPhttp://www.eclipse.org/ajdt/

When tracing the complete execution:

  1. A profiler is the simplest way. IMHO, every developer should be able to operate a profiler and have one readily available in the arsenal.
  2. The next choice would be AOP. It’s the second choice only because setting up a profiler is simpler when it comes packaged JARs. However, AOP gives more control. For example, I can see the method arguments and print them.
  3. Use debug messages.

When tracing events:

  1. Use AOP. Selectively capture just the interesting calls.
  2. Use a Dynamic Proxy. It’s like using debug messages, but a more elegant solution. AOP may be more complex first, but in the long run I would go for AOP.
  3. Use debug messages.
  4. Use breakpoints.

Using AOP does not necessarily require rebuilding the code (compile-time weaving), it can also be done dynamically at class loading (load-time weaving), and there are many tools available (commercial, free and open-source) that utilize the technology.
The combination of a Java 6 agent, on-the-fly bytecode instrumentation, and a scripting language, can be considered as a different category in your classification of execution tracing methods.http://www.adaptj.com
DTrace is only available on Solaris, Mac OS-X Leopard, FreeBSD and OpenBSD.

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: