1. Basic concepts
Java agent
It is being JDK1.5
introduced. It is a Java
technology that can dynamically modify bytecode. After the java class is compiled, the bytecode is formed JVM
and JVM
executed, 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 agent
technology.
Two, applicable scenarios
-
java agent
Abilityjava
to intercept and modify bytecode before loading bytecode -
In the
jvm
modified 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 agent
the implementation principle, you must first understand java
the class loading mechanism (you don't understand it yourself), this is java agent
the premise of understanding .
The second thing to understand is JVMTI
and JVMTIAgent
, the following are introduced separately
3.1 、 JVMTI
JVMTI
Is JVM Tool Interface
an abbreviation, is JVM
exposed to the user interface to a set of extensions to use, JVMTI
is based on event-driven, JVM
each 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
JVMTIAgent
Is a dynamic library, using the JVMTI
exposed user-interface logic ( eclipse
, idea
and other code debugging tools is achieved through this)
JVMTIAgent
There are three main methods:
Agent_OnLoad
Method, ifagent
loaded at startup, on the implementation of this method
Agent_OnAttach
method, ifagent
not loaded at boot time, is our firstattach
to the target thread, and then sent to the corresponding target processload
command to loadagent
, Call theAgent_OnAttach
function during loadingAgent_OnUnload
Method,agent
called when uninstalling
3.3、instrument agent
instrument agent
Implemented Agent_OnLoad
methods and Agent_OnAttach
methods, that is agent
, it can be loaded at startup , or dynamically loaded during runtime agent
, and the mechanism of dynamic loading of agent
dependencies JVM
during runtime attach
is realized by sending load
commands to loadagent
3.4, JVM Attach mechanism
jvm attach
The function of inter-process communication JVM
provided by the mechanism JVM
allows 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 pid
parameters are passed to the required dump
thread. Execution, this is one kind java attach
.
3.5. Implementation of Class Transform
The transform
scene that is requested when the class is loaded for the first time , the ClassFileLoad
event is emitted when the class file is loaded , and it is handed over instrument agent
to java agent
the ClassFileTransformer
implementation 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 agent
Implementation of bytecode enhancement to the process:
1.修改字节码、2.加载新字节码、3.替换旧字节码
- Java dynamic proxy
Java Proxy API
The invoke
corresponding code logic is intercepted by the method. Proxy
It is interface-oriented, Class
and all method calls that are proxied will call invoke
methods through reflection .
Disadvantages: high performance overhead
-
Instrumentation API provided by Java 5
Adapt to the scene: suitable for monitoring
Disadvantages: not suitable for handling flexible code logic
Instrumentation API
Not only can the bytecode be enhanced, but also getObjectSize(Object o)
the exact size of an object can be calculated by calling the method.
- ASM
ASM
It is a framework that provides bytecode analysis and manipulation. CGlib
The framework is based on ASM
implementation, and the commonly used framework Hibernate、Spring
is based on CGlib
implementation 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 API
and 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.
TraceClassVisitor
Print the byte stream information we need by initializing one ClassWriter
and one Printer
object. It is convenient to compare class files and analyze the structure of bytecode files.