0
votes

I wanted to instrument some methods/classes in Java. For that purpose I wrote an Java agent that instruments only a few classes that I choose.

I use the following code to get the loaded classes and filter some of them:

 instrumentation.addTransformer(myTransformer,true);

 Class[] loadedClasses=instrumentation.getAllLoadedClasses();

 Class[] modifiableClasses=ModifiableClasses(loadedClasses,instrumentation);

Class[] filteredClasses=filterClasses(modifiableClasses);

if(instrumentation.isRetransformClassesSupported()){
        System.out.println("retransformation is Supported");
        instrumentation.retransformClasses(filteredClasses);
     }

this should not create any problem filterClasses returns some classes that I want to be instrumented normally I want to instrument these Classes:

  • Ljava/nio/Buffer;
  • Ljava/util/HashMap$Entry;
  • Ljava/nio/HeapCharBuffer;
  • Ljava/nio/CharBuffer;
  • Ljava/lang/ClassLoader;
  • Ljava/lang/Class;
  • Ljava/util/HashMap;
  • Ljava/nio/ByteBuffer;
  • Ljava/lang/System;
  • Ljava/io/BufferedWriter;
  • Ljava/lang/String;
  • Ljava/io/OutputStreamWriter;
  • Ljava/io/BufferedOutputStream;
  • Ljava/nio/charset/CoderResult;
  • Ljava/io/Writer;
  • Ljava/util/HashSet;
  • Ljava/nio/charset/CharsetEncoder;
  • Ljava/io/FileOutputStream;
  • Ljava/io/PrintStream;

everything works fine I print the instrumented and unistrumented classes in specific folders for debug. I use the asm CoreAPI to add some instructions at the beginning and the end of methods I dont instrument Constructors,native methods, abstract methods and static variables. When I just instrument the new classes that are being loaded everything works fine. I guess I dont understand something with the ClassLoader or something specific here is what I get on the Console:

    Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(Unknown Source)
    at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(Unknown Source)
     Caused by: java.lang.InternalError
    at sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
    at sun.instrument.InstrumentationImpl.retransformClasses(Unknown Source)
    at my.Agent.Watcher.premain(Watcher.java:88)
    ... 6 more
    FATAL ERROR in native method: processing of -javaagent failed

I am thankful for any help I can get even a link or a hint in the right direction.

1

1 Answers

0
votes

Sorry guys my mistake after looking more precisely in the instrumented classes I found Errors which I will have to solve, if I want this to work. An admin/mod can close this thread .