I am writing my first java agent. I am trying to instrument a project that I picked from git repo (not developed by me).
I have written my Agent class with the premain method and implement some logging ( logs the number of lines executed using the ASM bytecode manipulation framework)
However, I see that even the inbuilt java function calls/ classes are getting instrumented which is incorrect. I only want the files in my project to be instrumented. To achieve this, I added a filter shown below -
public static void premain(String agentArgs, Instrumentation inst) {
System.out.println("Premain called");
inst.addTransformer(new ClassFileTransformer() {
public byte[] transform(ClassLoader classLoader,
String className,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] bytes)throws IllegalClassFormatException {
// ASM Code
if(className.startsWith("org/mytestpackage/")){
ClassReader reader = new ClassReader(bytes);
ClassWriter writer = new ClassWriter(reader, 0);
ClassTransformVisitor visitor = new ClassTransformVisitor(writer);
reader.accept(visitor, 0);
return writer.toByteArray();
}
return null;
}
});
}
After I added this filter, the premain is being called but I am getting some exception
initializationError(org.mytestpackage.TestAllPackages) Time elapsed: 0.002 sec <<< ERROR!
java.lang.VerifyError: (class: org/mytestpackage/TestAllPackages, method: main signature: ([Ljava/lang/String;)V) Stack size too large
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
Before I added this filter ( if condition based on the className), I was able to see my logic work for inbuilt java classes and methods. If fails after adding the filter.
Appreciate any help, TIA.