I am trying to instrument java class through attach API ( I want to instrument java.sql.DriverManager.getConnection() method specifically and record the returning Connection object).
So , I am doing the store and load operations and calling up a method defined in one of my class ( say , foo.TestRecordingClass.myFooRecordingMethod()) .
Now , my problem is that whenever I am trying to call the method mentioned earlier ( through attach API : agentmain is called ) , I am getting a NoClassDefFoundError.
But , the same thing instruments perfectly with premain , a.k.a Xbootclasspath approach .
Thanks in advance :-)
Edit 1 :
Ok , I am able to solve the problem using appendToBootstrapClassLoaderSearch(JarFile) method of instrumentation object.
Now , one more problem I am facing , which is loading one this object onto stack.
Theoretically , if I do aload_0 operation , that should load the current object , then I can pass it to any method , right ?
In the method visitInsn ( i.e while returning from method ) , I am doing this :
super.visitVarInsn(Opcodes.ALOAD, 0);
super.visitMethodInsn(Opcodes.INVOKESTATIC, fooClassName, "fooMethodReturnInstrumentor", "(Ljava/lang/Object;)V");
This should load the object itself , but I am not able to retrieve anything.