Java Code Examples for java.lang.instrument.Instrumentation#isRetransformClassesSupported()

The following examples show how to use java.lang.instrument.Instrumentation#isRetransformClassesSupported() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AgentAttachmentRule.java    From garmadon with Apache License 2.0 6 votes vote down vote up
@Override
public Statement apply(Statement base, FrameworkMethod method, Object target) {
    Enforce enforce = method.getAnnotation(Enforce.class);
    if (enforce != null) {
        if (!available) {
            return new NoOpStatement("The executing JVM does not support runtime attachment");
        }
        Instrumentation instrumentation = ByteBuddyAgent.install(ByteBuddyAgent.AttachmentProvider.DEFAULT);
        if (enforce.redefinesClasses() && !instrumentation.isRedefineClassesSupported()) {
            return new NoOpStatement("The executing JVM does not support class redefinition");
        } else if (enforce.retransformsClasses() && !instrumentation.isRetransformClassesSupported()) {
            return new NoOpStatement("The executing JVM does not support class retransformation");
        } else if (enforce.nativeMethodPrefix() && !instrumentation.isNativeMethodPrefixSupported()) {
            return new NoOpStatement("The executing JVM does not support class native method prefixes");
        }
    }
    return base;
}
 
Example 2
Source File: SnoopInstructionTransformer.java    From JQF with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public static void premain(String agentArgs, Instrumentation inst) throws ClassNotFoundException {

    preloadClasses();

    inst.addTransformer(new SnoopInstructionTransformer(), true);
    if (inst.isRetransformClassesSupported()) {
      for (Class clazz : inst.getAllLoadedClasses()) {
        try {
          String cname = clazz.getName().replace(".","/");
          if (shouldExclude(cname) == false) {
            if (inst.isModifiableClass(clazz)) {
              inst.retransformClasses(clazz);
            } else {
              println("[WARNING] Could not instrument " + clazz);
            }
          }
        } catch (Exception e){
          if (verbose) {
            println("[WARNING] Could not instrument " + clazz);
            e.printStackTrace();
          }
        }
      }
    }
  }
 
Example 3
Source File: JavaAgent.java    From util4j with Apache License 2.0 6 votes vote down vote up
public static void agentmain(String args, Instrumentation inst){
    INST=inst;
    System.out.println("agentmain----agentArgs:"+args);
    Class clazz=null;
    for (Class allLoadedClass : inst.getAllLoadedClasses()) {
        if(allLoadedClass.getName().equals(args)){
            clazz=allLoadedClass;break;
        }
    }
    System.out.println("agentmain----,clazz:"+clazz);
    if(clazz!=null){
        try {
            Method initMethod=clazz.getDeclaredMethod("init", new Class[]{Instrumentation.class});
            initMethod.setAccessible(true);
            Object result = initMethod.invoke(null, inst);
            System.out.println("agentmain------,initSuccess");
            if(inst.isRetransformClassesSupported() && result!=null){
                ClassFileTransformer classFileTransformer=(ClassFileTransformer)result;
                inst.addTransformer(classFileTransformer,true);
                System.out.println("agentmain------,hookClassFileTransformer success");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
 
Example 4
Source File: TestLambdaFormRetransformation.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void premain(String args, Instrumentation instrumentation) {
    if (!instrumentation.isRetransformClassesSupported()) {
        System.out.println("Class retransformation is not supported.");
        return;
    }
    System.out.println("Calling lambda to ensure that lambda forms were created");

    Agent.lambda.run();

    System.out.println("Registering class file transformer");

    instrumentation.addTransformer(new Agent());

    for (Class c : instrumentation.getAllLoadedClasses()) {
        if (c.getName().contains("LambdaForm") &&
            instrumentation.isModifiableClass(c)) {
            System.out.format("We've found a modifiable lambda form: %s%n", c.getName());
            try {
                instrumentation.retransformClasses(c);
            } catch (UnmodifiableClassException e) {
                throw new AssertionError("Modification of modifiable class " +
                                         "caused UnmodifiableClassException", e);
            }
        }
    }
}
 
Example 5
Source File: AbstractInstrumentationAgent.java    From tascalate-javaflow with Apache License 2.0 6 votes vote down vote up
protected void attach(String args, Instrumentation instrumentation) throws Exception {
    log.info("Installing agent...");
    
    // Collect classes before ever adding transformer!
    Set<String> ownPackages = new HashSet<String>(FIXED_OWN_PACKAGES);
    ownPackages.add(packageNameOf(getClass()) + '.');
    
    ClassFileTransformer transformer = createTransformer();
    instrumentation.addTransformer(transformer);
    if ("skip-retransform".equals(args)) {
        log.info("skip-retransform argument passed, skipping re-transforming classes");
    } else if (!instrumentation.isRetransformClassesSupported()) {
        log.info("JVM does not support re-transform, skipping re-transforming classes");
    } else {
        retransformClasses(instrumentation, ownPackages);
    }
    System.setProperty(transformer.getClass().getName(), "true");
    log.info("Agent was installed dynamically");
}
 
Example 6
Source File: LiveWeavingServiceImpl.java    From glowroot with Apache License 2.0 6 votes vote down vote up
public static void initialReweave(Set<PointcutClassName> pointcutClassNames,
        Class<?>[] initialLoadedClasses, Instrumentation instrumentation) {
    if (!instrumentation.isRetransformClassesSupported()) {
        return;
    }
    Set<Class<?>> classes = getExistingModifiableSubClasses(pointcutClassNames,
            initialLoadedClasses, instrumentation);
    for (Class<?> clazz : classes) {
        if (clazz.isInterface()) {
            continue;
        }
        try {
            instrumentation.retransformClasses(clazz);
        } catch (UnmodifiableClassException e) {
            // IBM J9 VM Java 6 throws UnmodifiableClassException even though call to
            // isModifiableClass() in getExistingModifiableSubClasses() returns true
            logger.debug(e.getMessage(), e);
        }
    }
}
 
Example 7
Source File: AgentAttachmentRule.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
public Statement apply(Statement base, FrameworkMethod method, Object target) {
    Enforce enforce = method.getAnnotation(Enforce.class);
    if (enforce != null) {
        if (!available) {
            return new NoOpStatement("The executing JVM does not support runtime attachment");
        }
        Instrumentation instrumentation = ByteBuddyAgent.install(ByteBuddyAgent.AttachmentProvider.DEFAULT);
        if (enforce.redefinesClasses() && !instrumentation.isRedefineClassesSupported()) {
            return new NoOpStatement("The executing JVM does not support class redefinition");
        } else if (enforce.retransformsClasses() && !instrumentation.isRetransformClassesSupported()) {
            return new NoOpStatement("The executing JVM does not support class retransformation");
        } else if (enforce.nativeMethodPrefix() && !instrumentation.isNativeMethodPrefixSupported()) {
            return new NoOpStatement("The executing JVM does not support class native method prefixes");
        }
    }
    return base;
}
 
Example 8
Source File: AgentAttachmentRule.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
public Statement apply(Statement base, FrameworkMethod method, Object target) {
    Enforce enforce = method.getAnnotation(Enforce.class);
    if (enforce != null) {
        if (!available) {
            return new NoOpStatement("The executing JVM does not support runtime attachment");
        }
        Instrumentation instrumentation = ByteBuddyAgent.install(ByteBuddyAgent.AttachmentProvider.DEFAULT);
        if (enforce.redefinesClasses() && !instrumentation.isRedefineClassesSupported()) {
            return new NoOpStatement("The executing JVM does not support class redefinition");
        } else if (enforce.retransformsClasses() && !instrumentation.isRetransformClassesSupported()) {
            return new NoOpStatement("The executing JVM does not support class retransformation");
        } else if (enforce.nativeMethodPrefix() && !instrumentation.isNativeMethodPrefixSupported()) {
            return new NoOpStatement("The executing JVM does not support class native method prefixes");
        }
    }
    return base;
}
 
Example 9
Source File: RedefineIntrinsicTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception {
    if (inst.isRedefineClassesSupported() && inst.isRetransformClassesSupported()) {
        inst.addTransformer(new Redefiner(), true);
        Class<?>[] allClasses = inst.getAllLoadedClasses();
        for (int i = 0; i < allClasses.length; i++) {
            Class<?> c = allClasses[i];
            if (c == Intrinsic.class) {
                inst.retransformClasses(new Class<?>[]{c});
            }
        }
    }
}
 
Example 10
Source File: RedefineClassTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void agentmain(@SuppressWarnings("unused") String args, Instrumentation inst) throws Exception {
    if (inst.isRedefineClassesSupported() && inst.isRetransformClassesSupported()) {
        inst.addTransformer(new FooTransformer(), true);
        Class<?>[] allClasses = inst.getAllLoadedClasses();
        for (int i = 0; i < allClasses.length; i++) {
            Class<?> c = allClasses[i];
            if (c == Foo.class) {
                inst.retransformClasses(new Class<?>[]{c});
            }
        }
    }
}
 
Example 11
Source File: AllocationInstrumenter.java    From allocation-instrumenter with Apache License 2.0 4 votes vote down vote up
public static void premain(String agentArgs, Instrumentation inst) {
  AllocationRecorder.setInstrumentation(inst);

  // Force eager class loading here.  The instrumenter relies on these classes.  If we load them
  // for the first time during instrumentation, the instrumenter will try to rewrite them.  But
  // the instrumenter needs these classes to run, so it will try to load them during that rewrite
  // pass.  This results in a ClassCircularityError.
  try {
    Class.forName("sun.security.provider.PolicyFile");
    Class.forName("java.util.ResourceBundle");
    Class.forName("java.util.Date");
  } catch (Throwable t) {
    // NOP
  }

  if (!inst.isRetransformClassesSupported()) {
    System.err.println("Some JDK classes are already loaded and will not be instrumented.");
  }

  // Don't try to rewrite classes loaded by the bootstrap class
  // loader if this class wasn't loaded by the bootstrap class
  // loader.
  if (AllocationRecorder.class.getClassLoader() != null) {
    canRewriteBootstrap = false;
    // The loggers aren't installed yet, so we use println.
    System.err.println("Class loading breakage: Will not be able to instrument JDK classes");
    return;
  }

  canRewriteBootstrap = true;
  List<String> args = Arrays.asList(agentArgs == null ? new String[0] : agentArgs.split(","));

  // When "subclassesAlso" is specified, samplers are also invoked when
  // SubclassOfA.<init> is called while only class A is specified to be
  // instrumented.
  ConstructorInstrumenter.subclassesAlso = args.contains("subclassesAlso");
  inst.addTransformer(new ConstructorInstrumenter(), inst.isRetransformClassesSupported());

  if (!args.contains("manualOnly")) {
    bootstrap(inst);
  }
}