赞
踩
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.study</groupId>
<artifactId>instrustment</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.bcel</groupId>
<artifactId>bcel</artifactId>
<version>6.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.javassist/javassist -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.19.0-GA</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.rimerosolutions.maven.plugins</groupId>
<artifactId>wrapper-maven-plugin</artifactId>
<version>0.0.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<verbose />
<bootclasspath>${java.home}/lib/rt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
public class MyMain {
public static void main(String args[]) {
myTest();
}
private static void myTest() {
try {
Thread.currentThread().sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Instrumentation;
public class PerfMonAgent {
private static Instrumentation inst = null;
public static void premain(String agentArgs, Instrumentation _inst) {
System.out.println("PerfMonAgent.premain() was called.");
inst = _inst;
ClassFileTransformer trans = new PerfMonXformer();
System.out.println("Adding a PerfMonXformer instance to the JVM.");
inst.addTransformer(trans);
}
}
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.security.ProtectionDomain;
import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtBehavior;
import javassist.CtClass;
import javassist.NotFoundException;
public class PerfMonXformer implements ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
byte[] transformed = null;
// System.out.println("Transforming " + className);
ClassPool pool = ClassPool.getDefault();
CtClass cl = null;
try {
cl = pool.makeClass(new java.io.ByteArrayInputStream(classfileBuffer));
if (cl.isInterface() == false) {
CtBehavior[] methods = cl.getDeclaredBehaviors();
for (int i = 0; i < methods.length; i++) {
if (methods[i].isEmpty() == false) {
doMethod(methods[i]);
}
}
transformed = cl.toBytecode();
}
} catch (Exception e) {
System.err.println("Could not instrument " + className + ", exception : " + e.getMessage());
} finally {
if (cl != null) {
cl.detach();
}
}
return transformed;
}
private void doMethod(CtBehavior method) throws NotFoundException, CannotCompileException {
if ("myTest".equalsIgnoreCase(method.getName())) {
// 添加局部变量,如果不同过addLocalVariable设置,在调用属性时将出现compile error: no such field:
// startTime
method.addLocalVariable("startTime", CtClass.longType);
method.insertBefore("System.out.println(startTime);");
method.insertBefore("startTime = System.currentTimeMillis();");
// method.insertBefore("long startTime = System.currentTimeMillis();System.out.println(startTime);");
method.insertBefore("System.out.println(\"insert before ......\");");
method.insertAfter("System.out.println(\"leave " + method.getName()
+ " and time is :\"+System.currentTimeMillis()+\":\" + (System.currentTimeMillis() - startTime));");
}
}
}
java -cp /home/your_home/.m2/repository/org/javassist/javassist/3.19.0-GA/javassist-3.19.0-GA.jar:./ -javaagent:/yourpath/target/PerMonAgent.jar MyMain
----------------
MANIFEST.MF
Manifest-Version: 1.0
Premain-Class: PerfMonAgent
Can-Redefine-Classes: true
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。