1 What can Java Instrument do? The biggest effect?

  1. 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;

  2. 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?

  1. JDK 1.5: Static Instrument is supported, which means that the Instrument is set up statically before JVM startup.

  2. 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?

  1. Based on JVMTI agent;
  2. JVMTI: A set of agent mechanisms that provide a set of native programming interfaces for JVM-RELATED tools;
  3. 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?

  1. 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;
  2. 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?

  1. 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;
  2. 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?

  1. The transform method in ClassFileTransformer operates on class definitions;
  2. 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?

  1. Premain-class: specifies the name of the Class containing the Premain method;
  2. Agent-class: specifies the name of the Class containing the agentMain method.
  3. 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;
  4. Can-re-define -Class: Specifies whether the classes required by this proxy Can be redefined. Default: false;
  5. Can-retransform-class: whether classes required by this agent Can be reconverted. Default is false;
  6. 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?

  1. ClassFileTransformer: defines pre-processing classes before class loading;
  2. 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?

  1. When the JVM starts, the Instrument Agent is loaded by passing in the Agent JAR using the JVM parameter javaAgent.
  2. During Instrument Agent initialization, the JVMTI initialization function eventHandlerVMinit is registered.
  3. 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;
  4. The eventHandlerVMinit initialfunction registers the ClassFileLoadHook function for class resolution;
  5. 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.
  6. 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.
  7. Reload the Class and repeat steps 5-6.