Java Magazine, Jan/Feb 2017
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2017 34 tools The following example requests a capability to generate exception events and sets up the callback to receive notifications about all thrown Java exceptions both caught and uncaught jvmtiCapabilities capabilities 0 capabilities can_ generate_ exception_ events 1 jvmti AddCapabilities capabilities jvmtiEventCallbacks cb 0 cb Exception ExceptionCallback jvmti SetEventCallbacks cb sizeof cb jvmti SetEventNotificationMode JVMTI_ ENABLE JVMTI_ EVENT_ EXCEPTION NULL The callback function receives all details about an exception a thread a method and the bytecode index for the thrown exception The callback also has a reference to the JNI environment This means you can invoke any JNI function from within For instance you can use JNI to call Throwable printStackTrace Thus an agent will print all the exceptions including ignored exceptions just before they are caught void JNICALL ExceptionCallback jvmtiEnv jvmti JNIEnv env jthread thread jmethodID method jlocation location jobject exception jmethodID catch_ method jlocation catch_ location jclass cls env FindClass java lang Throwable jmethodID print_ method env GetMethodID cls printStackTrace V env CallVoidMethod exception print_ method You can do a lot more useful things with the JVM TI Besides exceptions it is possible to trace class loading garbage collection lock contention thread activity and more JVM TI is often confused with the Java debugger agent There is a popular misconception that JVM TI compromises security and degrades the performance of Java applications However the Java Debug Wire Protocol JDWP agent is just one example of a JVM TI based tool the technology itself does not imply security or performance consequences Whether an application will sufer from agent overhead solely depends on what the agent does and which capabilities it requests Consider JVM TI as a sort of extension to JNI This technology is definitely worth trying Changes in JDK 9 All the technologies discussed previously including private APIs will remain functional in the upcoming JDK 9 However the new module system imposes certain restrictions on how you can access these APIs No longer will tools jar and sa jdi jar be separate libraries JDK 9 serviceability features are supplied in the dedicated modules Table 1 shows the location of key JAR files in Java 9 By default applications cannot access an API from the modules that do not export packages externally In order to use the private APIs you need to explicitly break the encap FEATURE MODULE PUBLIC JVMSTAT PERFORMANCE COUNTERS jdk jvmstat NO DYNAMIC ATTACH API jdk attach YES INSTRUMENTATION AP java instrument YES SERVICEABILITY AGENT jdk hotspot agent NO com sun tools attach VirtualMachine is accessible from outside but sun tools attach HotSpotVirtualMachine is not Table 1 The location of serviceability APIs in Java 9
You must have JavaScript enabled to view digital editions.