Click to enter _ more _java thousand ask
1. What is JVMTI?
JVMTI (JVM Tool Interface) is the lowest level in the JPDA system, provided by the Java Virtual Machine native programming interface, is Jvmpi (Java virtual machines Profiler Interface) and Updated version of Jvmdi (Java Virtual machine Debug Interface).
Understand JPDA system look here: JPDA is what
As we know from its development, JVMTI provides debug (debug) and Analysis (Profiler) functionality, as well as monitoring (monitoring), threading analysis, and coverage analysis (coverage Analysis) and other functions. Thanks to the power of JVMTI, it is the foundation for implementing Java debuggers and other Java run-state testing and analysis tools. There are many mature integration tools that provide JVMTI implementations (such as Sun, IBM, and some open source projects such as Apache Harmony DRLVM), which are powerful and easy to use, but in some specific cases developers often have special needs, This time you need to customize tools to achieve your goals.
JVMTI is a set of native code interfaces, so we need to use C + + and JNI. It is generally developed using JVMTI in the form of an agent (written in C + +) that can use the JVMTI function, set a callback function, get current run state information from the JVM, and manipulate the running state of the virtual machine.
2. How to load JVMTI Agent
After we have compiled the agent into a dynamic link library, we can load the agent in two ways: Boot load mode, active load mode. Specific as follows:
Boot load mode
Loading it when the Java program starts is actually specifying the loading agent at Java startup, as follows:
-agentlib:<agent-lib-name>=<options>
Note that the path here is the relative path to the environment variable, for example, when Java-agentlib:libagent=opt,java is started, it is loaded at the path defined by the PATH environment variable libagent.so
-agentpath:<path-to-agent>=<options>
Here is the absolute path, for example java-agentpath:/home/admin/agentlib/libagent.so=opt
Active load Mode
After Java 5, the agent can be loaded at runtime, via the API of the Com.sun.tools.attach package (need to introduce ${java_home}/lib/tools.jar). The use is very simple, as follows:
public Class Testagent {public static void main (string[] args) throws Attachnotsupportedexception, IOException, Agentloadexception, agentinitializationexception {String pid = "831" ; //the Java process ID that you want to load String Agentpath = "/users/sunjie/desktop/libagent.so" ; //agent.so path String options = null ; //the parameters of the incoming agent Virtualmachine virtualmachine = Com.sun.tools.attach.VirtualMachine.attach (PID); Virtualmachine.loadagentpath (Agentpath, Options); Virtualmachine.detach (); }}
Learn Attachapi look here: [Attachapi is what][3]
3. How the JVMTI agent works
Agent Start-up
The agent is loaded at the start of the Java Virtual machine, at this point in time:
- All Java classes have not been initialized
- All object instances have not been created
- All Java code is not executed.
But at this time, we have been able to:
- Capability parameters for Operation JVMTI
- Using System parameters
After loading the agent by starting load mode, the virtual opportunity first looks for an agent entry function:
*jvm*options*reserved)
If the load mode is running, it is:
*jvm*options*reserved);
In this function, the virtual machine passes in a JAVAVM pointer, and the parameters of the command line. We can get jvmtienv through *JVM, that is, we can use the JVMTI function, although the different JVM implementations may not provide the same details, but in the same way. As follows:
jvmtiEnv *jvmti;(*jvm)->GetEnv(jvmJVMTI_VERSION_1_1);
The second parameter here is version information, and the functionality and handling of different JVMTI environments may vary, but it will remain the same in the same virtual machine.
uninstallation of Agent
When the agent finishes the task, or the JVM shuts down, the virtual opportunity invokes the function to complete the final cleanup task, as follows:
void JNICALL Agent_OnUnload(JavaVM *jvm)
4, how to write JVMTI Agent program
As long as there is a certain C + + Foundation can write JVMTI agent, see here: [How to write JVMTI agent][4]
Java thousand asked _08jdk detailed (013) _JVMTI is what