1 What can Java Instrument do? The biggest effect?
It allows developers to build an application-independent Agent that monitors and assists programs running on the JVM and, more importantly, replaces and modifies certain class definitions;
Biggest benefits: An AOP implementation supported at the virtual machine level;
2 What changes does Java Instrument support in JDK 1.5 and 1.6?
JDK 1.5: Static Instrument is supported, which means that the Instrument is set up statically before JVM startup.
JDK 1.6: Supports dynamic Instrument, which is the dynamic setting of Instrument after JVM startup. Support for native code Instrument; Support dynamic classpath change;
3 Which JVM mechanism is Java Instrument implemented based on? What is JVMTI and what can it do?
- Based on JVMTI agent;
- JVMTI: A set of agent mechanisms that provide a set of native programming interfaces for JVM-RELATED tools;
- JVMTI can support third-party tools to connect to and access the JVM in a proxy manner, and use the rich programming interface provided by JVMTI to complete many jVM-RELATED functions.
4 When can the Instrument Premain and AgentMain methods be executed?
- Premain execution time: When the JVM starts, EventHandlerVMinit initialization function will be called sun. Instrument. LoadClassAndCallPremain method to execute the Premain instrumentationImpl Class – the Class of the specified Class Premain method;
- When the agentMain is executed: After the JVM is started, an Instrument is attached via the VirtualMachine, such as: Vm. LoadAgent (jar), invokes the sun. The instrument. The loadClassAndCallAgentmain method to execute the Agentmain instrumentationImpl Class – the Class of the specified Class Agentmain method;
5 What are the two parameters in Instrument Premain and AgentMain: agentArgs and inst? What difference does it make?
- AgentArgs: An input parameter to the agent command line, passed along with “-javaAgent”. Unlike the main function, this parameter is a string rather than an array of strings;
- Inst: Java lang. Instrument. Instrumentation as an example, introduced by the JVM automatically, focused almost all function method, such as: class, classpath operation, etc.;
6 Java. Lang. Instrument. What is ClassFileTransformer, have what effect?
- The transform method in ClassFileTransformer operates on class definitions;
- In front of the class in the JVM bytecode, the JVM will call ClassFileTransformer. Transform methods, so as to realize to manipulate to modify the class definition, achieve AOP functionality; Compared with JDK dynamic proxy, CGLIB and other AOP implementation technologies, new classes will not be generated, and the original class does not need to have interfaces;
7 How do I dynamically attach agent to agentMain?
Attach an Instrument using a VirtualMachine, such as vm.loadAgent(jar).
8 Meta-INF/mainfest.mf parameter list?
- Premain-class: specifies the name of the Class containing the Premain method;
- Agent-class: specifies the name of the Class containing the agentMain method.
- Boot-class-path: specifies the list of paths searched by the Boot classloader. The bootstrap classloader searches these paths after the platform’s mechanism fails;
- Can-re-define -Class: Specifies whether the classes required by this proxy Can be redefined. Default: false;
- Can-retransform-class: whether classes required by this agent Can be reconverted. Default is false;
- Can-set-native-method-prefix: Whether the Native Method Prefix required by this proxy Can be Set. The default value is false.
2 two core apis ClassFileTransformer, Instrumention?
- ClassFileTransformer: defines pre-processing classes before class loading;
- Instrumentation: intensifier
Add /removeTransformer: Add /remove ClasFileTransformer;
(2) retransformerClasses: specifies which classes, if already loaded, are reconverted, that is, trigger a reloading of class definitions; Reloaded classes cannot modify the old class declaration, such as: cannot add attributes, cannot modify the method declaration, etc.
(3) redefineClasses: specifies which classes will trigger a reloading of the class definition. Instead of reloading the classes, bytecode will be passed directly to the JVM.
(4) getAllLoadedClasses: get the currently loaded Class set;
GetInitiatedClasses: Get the class definition loaded by a specific ClassLoader;
(6) getObjectSize: get the size of the space occupied by an object;
(7) appendToBootstrapClassLoaderSearch/appentToSystemClassLoaderSearch: add BootstrapClassLoader/SystemClassLoader search path;
(8) isNativeMethodPrefixSupported/SetNativeMethodPrefix: whether the JVM support intercept Native Method;
10 How does Java Instrument Work?
- When the JVM starts, the Instrument Agent is loaded by passing in the Agent JAR using the JVM parameter javaAgent.
- During Instrument Agent initialization, the JVMTI initialization function eventHandlerVMinit is registered.
- When the JVM starts, the initialization function eventHandlerVMinit is called to start the Instrument Agent, With the sun. Instrument. InstrumentationImpl loadClassAndCallPremain methods in the Class to initialize Premain – Class of the specified Class Premain method;
- The eventHandlerVMinit initialfunction registers the ClassFileLoadHook function for class resolution;
- Before parsing Class, the JVM calls JVMTI ClassFileLoadHook function, hook function call sun. Instrument. InstrumentationImpl transform method in the Class, The transformer method of the TransformerManager finally calls the transform method of our custom Transformer class.
- Because the bytecode is changed before Class parsing, it is directly replaced by the data stream of the modified bytecode, and finally enters the Class parsing without affecting the whole Class parsing.
- Reload the Class and repeat steps 5-6.