public static void premain(String args, Instrumentation inst) throws Exception { try { String[] agentArgs; if (args == null) agentArgs = new String[] {""}; else agentArgs = args.split(","); if (!agentArgs[0].equals("instrumenting")) jarFileName = agentArgs[0]; BaseClassTransformer rct = null; rct = new BaseClassTransformer(); if (agentArgs[0].equals("instrumenting")) { initVMClasses = new HashSet<String>(); for (Class<?> c : inst.getAllLoadedClasses()) { ((Set<String>) initVMClasses).add(c.getName()); } } if (!agentArgs[0].equals("instrumenting")) { inst.addTransformer(rct); Tracer.setLocals(new CounterThreadLocal()); Tracer.overrideAll(true); for (Class<?> c : inst.getAllLoadedClasses()) { try { if (c.isInterface()) continue; if (c.isArray()) continue; byte[] bytes = rct.getBytes(c.getName()); if (bytes == null) { continue; } inst.redefineClasses(new ClassDefinition[] {new ClassDefinition(c, bytes)}); } catch (Throwable e) { synchronized (System.err) { System.err.println("" + c + " failed..."); e.printStackTrace(); } } } Runtime.getRuntime() .addShutdownHook( new Thread() { public void run() { Tracer.mark(); try { PrintStream ps = new PrintStream("bailout.txt"); ps.println("Bailouts: " + Tracer.getBailoutCount()); ps.close(); } catch (Exception e) { } Tracer.unmark(); } }); if ("true".equals(System.getProperty("bci.observerOn"))) Tracer.overrideAll(false); } } catch (Exception e) { e.printStackTrace(); } }
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(); } }
/** * 根据类名正则表达式搜 * * @param inst * @return */ public static Set<Class<?>> searchClassByClassRegex(Instrumentation inst, String classRegex) { final Set<Class<?>> matchs = new HashSet<Class<?>>(); for (Class<?> clazz : inst.getAllLoadedClasses()) { if (clazz.getName().matches(classRegex)) { matchs.add(clazz); } } // for return matchs; }
/** * 根据父类来搜索 * * @param inst * @param supers * @return */ public static Set<Class<?>> searchClassBySupers(Instrumentation inst, Set<Class<?>> supers) { final Set<Class<?>> matchs = new HashSet<Class<?>>(); for (Class<?> clazz : inst.getAllLoadedClasses()) { for (Class<?> superClass : supers) { if (superClass.isAssignableFrom(clazz)) { matchs.add(clazz); break; } } } // for return matchs; }
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}); } } } }
@Override public void dumpClass(String patternString, String dirName) { Pattern pattern = Pattern.compile(patternString); File dir = new File(dirName); List<Class<?>> classes = new ArrayList<>(); for (Class<?> clazz : instrumentation.getAllLoadedClasses()) { if (pattern.matcher(clazz.getName()).matches()) { classes.add(clazz); } } if (!classes.isEmpty()) { transform(pattern, classes, dir); } else { log.log(Level.WARNING, "(SpilledBeans) Failed to match any classes against " + patternString); } }