First, JavaWhat is an agent?
The proxy class can only be set by specifying the Javaagent parameter when the JVM is started by command-line arguments in JDK5, and JDK6 is not limited to setting the proxy class by configuration parameters when the JVM is started, JDK6 through the Attach method in the Java Tool API. We can also easily set the load proxy class dynamically during the run to achieve the instrumentation purpose. The biggest function of instrumentation is the dynamic change and operation of class definition.
Second, what can javaagent do?
The main functions of javaagent are as follows:
Can be intercepted before loading the class file to modify the bytecode
You can change the bytecode of a class that has already been loaded at run time, but there are a number of limitations in this case
And some other niche features.
Get all the classes that have been loaded
Gets all classes that have been initialized (the Clinit method is executed, which is a subset of the above)
Get the size of an object
Adding a jar to the bootstrapclasspath as a high priority is bootstrapclassloader loaded
Add a jar to the classpath for Appclassloard to load
Set the prefix for some native methods, and make regular matches when looking for native methods
It sounds cool to imagine that the program can be executed according to the logic we expect.
A City simple Agent implementation
A concrete example of how to develop a simple Agent is described below. The Agent is written in C + + (the reader can download to the complete code at the end), and he outputs all the called function names by listening to the Jvmti_event_method_entry event and registering the corresponding callback function to respond to the event. Interested readers can also refer to this basic process and extend and customize it through the rich functions provided by JVMTI.
Design of Agent
Implementations are provided in the Methodtraceagent class. In order, he handles environment initialization, parameter parsing, registering functions, registering event responses, and each function is abstracted into a specific function.
The Agent_onload function creates this class when the agent is loaded and invokes each of these methods in turn to implement the function of the agent.
Run procedure 1 as follows:
Figure 1. Agent Timing Diagram
Agent Compilation and operation
The compilation of the Agent is very simple, there is no essential difference between compiling a regular dynamic link library, but it is necessary to include some of the header files provided by the JDK.
Windows:
- Linux:
- 、
A running Java class is provided in the accompanying code file, and the result is as shown in the default case:
Figure 2. Default Run Output
- Now, before we run the program, we tell Java to load the compiled agent first:
This time the output is 3. As shown:
Figure 3. Output after adding Agent
The first method that can be used when the program runs to Methodtracetest is that the agent outputs the event. "First" is the parameter that the Agent runs, and if you do not specify, all the triggering events of the incoming method will be output, and if the reader removes the parameter and then runs it, it will find that a very basic class library function has been called before running the main function.
Iv. Summary
The Java virtual machine provides a complete set of functions through JVMTI to help users detect and manage the running state of the virtual machine, which is mainly implemented by the Agent to interoperate with the user. This approach is not only available to users through the Agent, but in fact many of the tools inside the JDK, such as instrumentation and JDI, are used in this way. This eliminates the need to bind these tools to virtual machines, reducing the load and memory footprint of virtual machines.
Plain English Javaagent