Live debugging java with btrace

Today I had to do a live debugging on a java application. I found a nice tool named btrace (no, it’s not that btrace…but THIS btrace ;).

Btrace seems magical:

  1. is the java version of SystemTap or DTrace;
  2. doesn’t need java debugging nor has  further requirements;
  3. doesn’t stop your application.

Once downloaded, you have to:

  1. create a small java program specifying what to trace;
  2. find the pid of your target jvm instance;
  3. add the required classpath to btrace.

Then run it, with:

# ./btrace -cp $YOUR_CLASSPATH  $JVM_PID MyClassTracer.java

The MyClassTracer.java is something like this (example take from here):

import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;
import java.io.File;
// every time new File(String) is called, print the file name
@BTrace
public class HelloBTrace {
@OnMethod(
clazz="java.io.File", method=""
)
public static void onNewFile(File self, String name) {
print("new file ");
println(name);
}

}