Java Magazine, Jan/Feb 2017
ORACLE COM JAVAMAGAZINE JANUARY FEBRUARY 2017 32 tools neers for debugging crashes inside the JDK However they later realized that it could be helpful for a wider group of developers and now it is bundled with the regular JDK Start using the SA by including JAVA_ HOME lib sa jdi jar in the classpath but remember that the API is not standard and is subject to change in any future JDK release Custom tools typically extend an existing Tool class which is already capable of parsing arguments and attaching to a running VM You just need to implement custom logic inside the overridden run method import sun jvm hotspot runtime VM import sun jvm hotspot tools Tool public class MyTool extends Tool @ Override public void run Actual implementation VM getVM public static void main String args new MyTool execute args VM getVM is the starting point to access Java HotSpot VM internal structures The next example employs SystemDictionary to traverse all loaded classes with their class loaders A similar technique might be useful in detecting memory leaks related to class loading VM getVM getSystemDictionary classesDo klass loader String className klass getName asString System out print className String loaderName loader null Bootstrap ClassLoader loader getKlass getName asString System out println loaded by loaderName That was rather simple The real power of the SA is to restore VM structures either from the memory of a live Java process or from the core dump of an abnormally terminated process when the operating system is configured to create such dumps The SA provides the reflection like API to inspect Java objects and to extract the required fields Unlike the reflection which works from within the same process the SA reads memory of a diferent process or parses a core dump file Tools based on this feature can do impressive tricks such as stealing private keys from a running web server The following code scans the heap of a target process looking for the instances of java security PrivateKey and printing their contents Klass keyClass VM getVM getSystemDictionary find java security PrivateKey null null VM getVM getObjectHeap iterateObjectsOfKlass new DefaultHeapVisitor @ Override public boolean doObj Oop obj InstanceKlass c InstanceKlass obj getKlass OopField f OopField c findField key B TypeArray key TypeArray f getValue obj key printOn System out return false keyClass
You must have JavaScript enabled to view digital editions.