我们已经有了agent,下面我们测试一下。测试需要有一个目标JVM,所以我们简单模拟一个,具体如下:
public class TestMain {
public static void main(String[] args) throws InterruptedException {
System.out.println("JVMTI agent Test start");
int i = 0;
while (true) {
Thread.sleep(1000);
i++;
}
}
}
死循环的目的在于不能让它结束,方便我们使用JVMTI agent进行操作。运行后结果如下:
JVMTI agent Test start
http://blog.tianya.cn/post-7268325-116516510-1.shtml
http://www.xici.net/d231821300.htm
http://club.mil.news.sohu.com/man/thread/47be5m612q5
http://club.mil.news.sohu.com/man/thread/47beaywlh4x
http://www.xici.net/d231824527.htm
了解更多 AttachAPI看这里:[AttachAPI是什么][4]
public class TestAgent {
public static void main(String[] args) throws AttachNotSupportedException, IOException, AgentLoadException,
AgentInitializationException {
String pid = "831"; // java进程pid
String agentPath = "/Users/sunjie/Desktop/libagent.so"; // agent.so的路径
String options = null;// 传入agent的参数
VirtualMachine virtualMachine = com.sun.tools.attach.VirtualMachine.attach(pid);
virtualMachine.loadAgentPath(agentPath, options);
virtualMachine.detach();
}
}
这里我们需要引入${JAVA_HOME}/lib/
http://blog.tianya.cn/post-7268325-116516510-1.shtml JVMTI agent Test start
cls sig=Ljava/lang/ClassLoader$NativeLibrary;
cls sig=Ljava/util/concurrent/ConcurrentMap;
cls sig=Ljava/lang/Error;
cls sig=[Ljava/lang/Error;
cls sig=Ljava/util/Set;
cls sig=Ljava/util/WeakHashMap;
cls sig=Ljava/lang/ref/Reference;
cls sig=[Ljava/lang/ref/Reference;
cls sig=Ljava/lang/StackOverflowError;
http://blog.tianya.cn/post-7268325-116516510-1.shtml
http://www.xici.net/d231821300.htm
http://club.mil.news.sohu.com/man/thread/47be5m612q5
http://club.mil.news.sohu.com/man/thread/47beaywlh4x
http://www.xici.net/d231824527.htm
注意,这里传入了*options参数:opt1,不过,我们的agent并没有使用他。
了解更多JVMTI的功能看这里:[JVMTI提供哪些功能][5]