BTrace - dynamic, safe tracing system for the Java platform

A. Sundararajan's Weblogより。実行中のJVMに張り付いて,いろいろ情報を引き出すツール。なにげにスゴくないか?これ。


面白いのは,BTrace programと呼ばれるJavaコードでトレースする内容を記述できること。さらに付け加えると,コンパイルする必要がない(してもいいけど)。
たとえば,サンプルについてきてるJInfo.java(下記参照)

import com.sun.btrace.annotations.*;
import static com.sun.btrace.BTraceUtils.*;

@BTrace
public class JInfo {
  static {
    println("System Properties:");
    printProperties();
    println("VM Flags:");
    printVmArguments();
    println("OS Enviroment:");
    printEnv();
    exit(0);
  }
}

ってのを動かすと,即座にシステムプロパティの内容がダンプされる。
#これだけでも鼻血吹くっつの。JMX(MBean)に手を出さなくてよかった。:-P

> btrace 245 JInfo.java
※ 245ってのは jpsで得たJVM-PID。


AOPまがいなことをやってるのだけれど,あくまでトレーサのため,いろいろ制約もあるみたい。

BTrace Restrictions
To guarantee that the tracing actions are "read-only" (i.e., the trace actions don't change the state of the program traced) and bounded (i.e., trace actions terminate in bounded time), a BTrace program is allowed to do only a restricted set of actions. In particular, a BTrace class

  • can not create new objects.
  • can not create new arrays.
  • can not throw exceptions.
  • can not catch exceptions.
  • can not make arbitrary instance or static method calls - only the public static methods of com.sun.btrace.BTraceUtils class may be called from a BTrace program.
  • can not assign to static or instance fields of target program's classes and objects. But, BTrace class can assign to it's own static fields ("trace state" can be mutated).
  • can not have instance fields and methods. Only static public void returning methods are allowed for a BTrace class. And all fields have to be static.
  • can not have outer, inner, nested or local classes.
  • can not have synchronized blocks or synchronized methods.
  • can not have loops (for, while, do..while)
  • can not extend arbitrary class (super class has to be java.lang.Object)
  • can not implement interfaces.
  • can not contains assert statements.
  • can not use class literals.
BTrace User's Guide

でも,まあそうゆうもんでしょ(パッチ当てとかに使うなってことで)。


残念なことは,JDK6以上のJVMじゃないと使えないことね。むしろ,運用管理面を考えればとっととJDK6にシフトしろって話でもある。
VisualVMといい,運管まわりの充実っぷりを考えれば妥当だわな。>JDK6移行