java agent

1. Basic concepts

Java agentIt is being JDK1.5introduced. It is a Javatechnology that can dynamically modify bytecode. After the java class is compiled, the bytecode is formed JVMand JVMexecuted, and the bytecode information is obtained before the bytecode is executed, and the bytecode is modified to complete some additional functions. This is a java agenttechnology.

Two, applicable scenarios

  1. java agentAbility javato intercept and modify bytecode before loading bytecode

  2. In the jvmmodified bytecode loaded during operation

Through the above two, it is possible to modify the bytecode at the collection points of some frameworks or technologies, to monitor the application, or to add additional operations (print log, print method execution time, Input parameters and results of the collection method, etc.)

Three, realization principle

To understand java agentthe implementation principle, you must first understand javathe class loading mechanism (you don't understand it yourself), this is java agentthe premise of understanding .

The second thing to understand is JVMTIand JVMTIAgent, the following are introduced separately

3.1 、 JVMTI

JVMTIIs JVM Tool Interfacean abbreviation, is JVMexposed to the user interface to a set of extensions to use, JVMTIis based on event-driven, JVMeach perform certain logic will call some of the events of the callback interface, these interfaces can extend itself to the user to implement their own logic

3.2 、 JVMTIAgent

JVMTIAgentIs a dynamic library, using the JVMTIexposed user-interface logic ( eclipse, ideaand other code debugging tools is achieved through this)

JVMTIAgentThere are three main methods:

  1. Agent_OnLoadMethod, if agentloaded at startup, on the implementation of this method
    Agent_OnAttachmethod, if agentnot loaded at boot time, is our first attachto the target thread, and then sent to the corresponding target process loadcommand to load
  2. agent, Call the Agent_OnAttachfunction during loading
  3. Agent_OnUnloadMethod, agentcalled when uninstalling

3.3、instrument agent

instrument agentImplemented Agent_OnLoadmethods and Agent_OnAttachmethods, that is agent, it can be loaded at startup , or dynamically loaded during runtime agent, and the mechanism of dynamic loading of agentdependencies JVMduring runtime attachis realized by sending loadcommands to loadagent

3.4, JVM Attach mechanism

jvm attachThe function of inter-process communication JVMprovided by the mechanism JVMallows one process to pass commands to another process and perform some internal operations, such as threading dump, then it needs to be executed jstack, and then the pidparameters are passed to the required dumpthread. Execution, this is one kind java attach.

3.5. Implementation of Class Transform

The transformscene that is requested when the class is loaded for the first time , the ClassFileLoadevent is emitted when the class file is loaded , and it is handed over instrument agentto java agentthe ClassFileTransformerimplementation of the bytecode modification registered in the call

3.6 Implementation of Class Redefind

Class redefinition, mainly used on classes that have been loaded

4. What are the technical frameworks to achieve bytecode enhancement?

java agentImplementation of bytecode enhancement to the process:

1.修改字节码、2.加载新字节码、3.替换旧字节码

  1. Java dynamic proxy

Java Proxy APIThe invokecorresponding code logic is intercepted by the method. ProxyIt is interface-oriented, Classand all method calls that are proxied will call invokemethods through reflection .

Disadvantages: high performance overhead

  1. Instrumentation API provided by Java 5

    Adapt to the scene: suitable for monitoring

    Disadvantages: not suitable for handling flexible code logic

Instrumentation APINot only can the bytecode be enhanced, but also getObjectSize(Object o)the exact size of an object can be calculated by calling the method.

  1. ASM

ASMIt is a framework that provides bytecode analysis and manipulation. CGlibThe framework is based on ASMimplementation, and the commonly used framework Hibernate、Springis based on CGlibimplementation AOP.

ASM shields users from the length and offset of the bytecode of the entire class, and can flexibly and conveniently parse and manipulate the bytecode. Mainly provide Core APIand Tree API.

Core API main classes (interface):

ClassVisitor、ClassAdapter、ClassReader、ClassWriter

The main classes (interfaces) of Tree API:

Tools:

TraceClassVisitor、CheckClassAdapter、ASMifier、Type

TraceClassVisitor can print the byte[] byte array provided by ClassWriter.

TraceClassVisitorPrint the byte stream information we need by initializing one ClassWriterand one Printerobject. It is convenient to compare class files and analyze the structure of bytecode files.

Guess you like

Origin blog.csdn.net/shang_xs/article/details/111432106