OpenSolaris

You are not signed in. Sign in or register.

Java DTrace API

The Java DTrace API is available in Solaris Nevada build 35 or later, and you can browse the source online. The javadoc is not yet available to browse online, but if you have the Java DTrace API installed on your system, you can view the javadoc here:

file:///usr/share/lib/java/javadoc/dtrace/api/index.html

The jar is installed at

/usr/share/lib/java/dtrace.jar

Here is the latest source (licensed under CDDL):

java_dtrace_api-s11-b59-src.tar.gz

Please note that the Java DTrace API depends on changes to DTrace in Solaris 11. Those changes and the Java DTrace API will be backported to Solaris 10 update 4. The easiest way to get the API now is to install Solaris Express. The above source tarball, when unpacked, creates a single directory named libdtrace_jni (located in /onnv/onnv-gate/usr/src/lib in the source browser) and is provided only as a convenience (an alternative to downloading files one at a time). You can build the API from source only if you have libdtrace from Solaris 11 build 30 or later.

Check out the API Diagram and Quick Start Guide in the Related Documentation section of the javadoc package summary. The Quick Start Guide includes program examples; here's the simplest one: TestApi.java. The significant code block shows how easy it is to use the API:

    try {
        consumer.open();
        consumer.compile(file, macroArgs);
        consumer.enable();
        consumer.go();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

This gets an open libdtrace handle, compiles the given D program file, enables the compiled probes, and begins tracing in a background thread. You can compile it as follows:

javac -cp /usr/share/lib/java/dtrace.jar TestAPI.java

Try running it with a simple D program file such as the following:

    BEGIN
    {
            trace("hello, world!");
            exit(0);
    }

If you save the D program as hello.d you can run it as follows:

java -cp .:/usr/share/lib/java/dtrace.jar TestAPI hello.d

The output should look like this:

org.opensolaris.os.dtrace.ProbeData[epid = 1, cpu = 1, enabledProbeDescription = dtrace:::BEGIN, flow = null, records = ["hello, world!", 0]]

dtrace(1M) Emulation

I wrote a lengthier test program that emulates the dtrace command. It supports many of the dtrace(1M) options, so it's a good example of how to access common DTtrace functionality using the API. You can get the jdtrace source and instructions here.