@SuppressWarnings("unused") public static void premain(String args, Instrumentation inst) { boolean verbose = false; if (args != null) { if (args.contains("verbose")) { verbose = true; } } inst.addTransformer(new FileStreamInstrumentationTransformer(verbose), true); System.out.println("OrcAgent is registered."); if (!inst.isRetransformClassesSupported()) { System.err.println("WARNING: JVM does not support class retransformation."); } else { try { inst.retransformClasses(FileInputStream.class, FileOutputStream.class); } catch (UnmodifiableClassException e) { System.err.println("Unable to modify FileStream classes."); e.printStackTrace(); } } Runtime.getRuntime() .addShutdownHook( new Thread( new Runnable() { public void run() { System.err.println(); System.err.println(FileStreamStatistics.asString()); } })); }
private void instrumentApplication() throws FileNotFoundException, UnmodifiableClassException { if (!instrumentation.isRetransformClassesSupported()) { throw new UnmodifiableClassException(); } instrumentation.addTransformer(this, true); for (Class<?> c : instrumentation.getAllLoadedClasses()) { if (isInstrumentClass(c)) { if (configuration.isAsyncTransformation()) { try { blockingQueue.put(c); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } else { try { instrumentation.retransformClasses(new Class[] {c}); } catch (Throwable e) { LOG.error("Could not transform " + c.getName(), e); } } } } if (configuration.isAsyncTransformation() && !blockingQueue.isEmpty()) { startTransformThread(); } }
@Override public void configurationChanged() { if (started.get()) { if (configuration.isFilterChanged()) { List<ClassInfo> deltas = context.buildDeltaList(); if (deltas != null && !deltas.isEmpty()) { for (ClassInfo classInfo : deltas) { if (configuration.isAsyncTransformation()) { try { blockingQueue.put(classInfo.getOriginalClass()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } else { try { instrumentation.retransformClasses(new Class[] {classInfo.getOriginalClass()}); } catch (Throwable e) { LOG.error("Could not transform " + classInfo.getClassName(), e); } } } if (configuration.isAsyncTransformation() && !blockingQueue.isEmpty()) { startTransformThread(); } } } } }
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}); } } } }
private void transform(Pattern pattern, List<Class<?>> classes, File dir) { ClassDumpTransformer transformer = new ClassDumpTransformer(pattern, dir); instrumentation.addTransformer(transformer, true); try { for (Class<?> clazz : classes) { try { instrumentation.retransformClasses(clazz); } catch (UnmodifiableClassException e) { log.log(Level.WARNING, "Unexpected error", e); } } } finally { instrumentation.removeTransformer(transformer); } }
public static void retransform(Class<?> clazz) throws UnmodifiableClassException { instrumentation.retransformClasses(clazz); }