JVMTI develop a simple tutorial Agent

JVM TI is JDK provides a common programming interface (API) set for developers JVM monitoring, fault location and performance tuning tools.
By JVMTI, we can develop a wide range of JVMTI Agent. This form of expression is an Agent to c / c ++ language dynamic shared libraries.

JVMTI Agent Principle: when to start or run Java, dynamic load an external module into the Java-based dynamic process written JVM TI, and then trigger source JVM native threads Attach Listener to perform this dynamic module callback function. In the body of the function, you can get a wide variety of VM-level information, event registration VM interest, and even control VM behavior.

JVMTI from the function can be divided into four categories:

  1. Heap
    access to information for all classes, information objects, object references relationship, Full GC start / end, an object recycling events.

  2. Thread stack
    obtain information about all threads, thread group information, control thread (start, suspend, resume, interrupt ...), Thread Monitor (Lock), to obtain the thread stack, control the stack, the method forcefully returned method stacks local variables.

  3. Class & Object & Method & Field meta information
    class information, symbol table, a method table, redefine class (hotswap), retransform class, object information, information Fields, method information.

  4. Tools
    thread cpu consuming, classloader path changes, and other system properties acquired.

Development jvm ti agent, simply speaking, is to develop a c / c ++ shared library. Suffix is ​​in the windows dll, under Linux / unix is ​​so, under mac is dylib. So we create a project and build environment, remember the form of a shared library (share library) to build.

JVMTI way to start
JVMTI There are two ways to start, when the first one is to start with the java process, automatically load shared libraries, hereinafter referred to as the mode A. Another way is, java runtime by loading a dynamic attach api, hereinafter referred to as mode B.

A mode of implementation is to pass through a special option starts java, the following examples:

java -agentlib: = Sample
note here is that the shared library path environment variable path, e.g. java -agentlib: foo = opt1, the path will be defined in the linux or windows of LD_LIBRARY_PATH PATH environment variable when opt2, java start at the loading foo. so or foo.dll, can not find the Throws

java -agentpath: = Sample
this way is an absolute path to load shared libraries, such as java -agentpath: /home/admin/agentlib/foo.so=opt1,opt2

B implementation way is through attach api, this is a pure java api, which is responsible for dynamically dynamic module attach to the specified process id of the java process and trigger a callback. Examples are as follows:

import java.io.IOException;
import com.sun.tools.attach.VirtualMachine;

public class VMAttacher {

public static void main(String[] args) throws Exception {
 // args[0]为java进程id
     VirtualMachine virtualMachine = com.sun.tools.attach.VirtualMachine.attach(args[0]);
     // args[1]为共享库路径,args[2]为传递给agent的参数
     virtualMachine.loadAgentPath(args[1], args[2]);
     virtualMachine.detach();
}

}
The Attach the API located $ JAVA_HOME / lib / tools.jar, at compile-time, this need jar into the classpath. E.g

javac -cp $JAVA_HOME/lib/tools.jar VMAttacher.java

Published 118 original articles · won praise 558 · views 500 000 +

Guess you like

Origin blog.csdn.net/yuan1164345228/article/details/105122458