java. Lang. instrument agent details

java. Lang. instrument agent using

java. Lang. instrument package is introduced in jdk5. Programmers can dynamically modify class code by modifying the bytecode of methods. This is usually a preprocessing operation before calling the main method of a class, which is implemented by specifying the proxy class of the class in Java. Before the bytecode of the class is loaded into the JVM, the transform method of classfiletransformer will be called, so as to realize the function of modifying the original class method and realize AOP. The advantage of this is that a new class will not be generated like AOP realized by dynamic proxy or cglib technology, and there is no need for the original class to have an interface.

(1) An agent is an interceptor in front of your main method, that is, it executes the agent code before the main method is executed. The agent code runs in the same JVM as your main method, is loaded by the same system classloader, and is managed by the same security policy and context. The name of agent is somewhat misleading. It is different from the agent we generally understand. Java agent is relatively simple to use. How to write a Java agent? You only need to implement the method of premain: public static void premain (string agentargs, instrumentation Inst). If the above definition of premain cannot be found in JDK 6, You will also try to call the following premain definition: public static void premain (string agentargs)

(2) The agent class must be made into a jar package, and then the meta-inf / mainifest.mf in it must contain the attribute premain class. The following is an example of manifest.mf:

Manifest-Version: 1.0 Premain-Class:MyAgent1 Created-By:1.6. 0_ 06

Then put manifest Add MF to your jar package. The following is the list of manifest attributes of the agent jar file: premain class if an agent is specified when the JVM is started, this attribute specifies the agent class, that is, the class containing the premain method. This property is required if an agent is specified at JVM startup. If the property does not exist, the JVM aborts. Note: this attribute is the class name, not the file name or path. Agent class if the implementation supports the mechanism of starting the agent at a certain time after VM startup, this attribute specifies the agent class. That is, the class containing the agentmain method. This property is required. If it does not exist, the agent will not start. Note: This is the class name, not the file name or path. Boot class path sets the list of paths to boot class loader search. Paths represent directories or libraries (usually referenced as jars or zip libraries on many platforms). After the platform specific mechanism for finding classes fails, the boot class loader searches for these paths. Search for paths in the order listed. Paths in the list are separated by one or more spaces. Paths use the path component syntax of hierarchical URIs. If the path is slashed ("/") )At the beginning, it is an absolute path, otherwise it is a relative path. The relative path is resolved according to the absolute path of the proxy jar file. Malformed paths and nonexistent paths are ignored. If the agent is started at some time after the VM is started, the path that does not represent the jar file is ignored. This property is optional. Can redefine classes Boolean (true or false, regardless of case). Whether the classes required by this agent can be redefined. Values other than true are treated as false. This property is optional and the default value is false. Can retransform classes Boolean (true or false, regardless of case). Whether the class required by this agent can be re converted. Values other than true are treated as false. This property is optional and the default value is false. Can set native method prefix Boolean (true or false, regardless of case). Whether the native method prefix required by this agent can be set. Values other than true are considered false. This property is optional and the default value is false.

(3) All the jar packages of these agents will be automatically added to the classpath of the program, so you don't need to add them to the classpath manually, unless you want to specify the order of Classpaths.

(4) There is no limit to the number of - javaagent parameters in a java program, so you can add any number of Java agents. All Java agents will be executed in the order you define. For example:

java -javaagent:MyAgent1. jar -javaagent:MyAgent2. jar -jar MyProgram. jar

Suppose myprogram The main function in jar is in myprogram. MyAgent1. jar,MyAgent2. Jar. The classes that implement premain in the two jar packages are myagent1, and the execution order of myagent2 will be as follows:

MyAgent1. premain -> MyAgent2. premain -> MyProgram. main

(5) In addition, premain after the main function will not be executed, for example:

java -javaagent:MyAgent1. jar -jar MyProgram. jar -javaagent:MyAgent2. jar

Myagent2 is on myprogram Jar, so the premain of myagent2 will not be executed, so the execution result will be:

MyAgent1. premain -> MyProgram. main

(6) Each Java agent can receive a string type parameter, that is, agentargs in premain, which is defined in Java option. For example:

java -javaagent:MyAgent2. jar=thisIsAgentArgs -jar MyProgram. jar

The value of agentargs received by premain myagent2 will be "thisisagentargs" (excluding double quotes).

(7) Instrumentation in the parameter: add your own defined classfiletransformer through instrumentation Inst in the parameter to change the class file. The custom transformer here implements the transform method, which provides the modification of the bytecode of the actual class to be executed, and even the execution of other class methods. For example, write the agent class :

Write classfiletransformer class:

The above two classes are the core of the agent. When the JVM is started, perfmonagent will be called before the application is loaded Premain, and then perfmonagent A customized classfiletransformer, perfmonxformer, is instantiated in premain and passed through inst.addtransformer (trans); Add the instance of perfmonxformer to the instrumentation instance (imported from the JVM), which makes perfmonxformer.transform be called when the classes in the application are loaded. You can change the loaded classes in this method. It's really magical. In order to change the bytecode of the class, I used JBoss's javassist. Although you don't have to use it, JBoss's javassist is really powerful, which makes it easy for you to change the bytecode of the class.

In the above method, by changing the bytecode of the class, I added long stime = system nanoTime();, Add system. At the exit of the method out. println(“methodClassName.methodName:”+(System.nanoTime()-stime));

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>