Skip to content

Instantly share code, notes, and snippets.

@pfmiles
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfmiles/1bfd57f80f7fcb012c54 to your computer and use it in GitHub Desktop.
Save pfmiles/1bfd57f80f7fcb012c54 to your computer and use it in GitHub Desktop.
利用instrumentation打印运行过程中实际使用到的java类
Manifest-Version: 1.0
Premain-Class: test.PrintClsAgent
public class PrintClsAgent {
public static void premain(String args, Instrumentation inst) {
File f = new File(args);
try {
final FileWriter fw = new FileWriter(f);
inst.addTransformer(new ClassFileTransformer() {
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
ProtectionDomain protectionDomain, byte[] classfileBuffer)
throws IllegalClassFormatException {
try {
fw.write(className + "\n");
fw.flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
return classfileBuffer;
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static void agentmain(String args, Instrumentation inst) {
premain(args, inst);
}
}
@pfmiles
Copy link
Author

pfmiles commented May 1, 2014

运行时传给vm参数:-javaagent:/tmp/prtCls.jar=/tmp/class.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment