public static void agentmain(String args,Instrumentation inst){ try{ System.err.println("传进来的参数为"+args); File f = new File(args); byte[] targetClassFile = new byte[(int)f.length()]; DataInputStream dis = new DataInputStream(new FileInputStream(f)); dis.readFully(targetClassFile); dis.close(); DynamicClassLoader myLoader = new DynamicClassLoader(); Class targetClazz = myLoader.findClass(targetClassFile); System.err.println("目标class类全路径为"+targetClazz.getName()); ClassDefinition clazzDef = new ClassDefinition(Class.forName(targetClazz.getName()), targetClassFile); inst.redefineClasses(clazzDef); System.err.println("重新定义"+args+"完成!!"); }catch(Exception e){ e.printStackTrace(); } }
public void redefineClasses(ClassDefinition... definitions) throws ClassNotFoundException { if (!isRedefineClassesSupported()) { throw new UnsupportedOperationException("redefineClasses is not supported in this environment"); } if (definitions == null) { throw new NullPointerException("null passed as 'definitions' in redefineClasses"); } for (int i = 0; i < definitions.length; ++i) { if (definitions[i] == null) { throw new NullPointerException("element of 'definitions' is null in redefineClasses"); } } if (definitions.length == 0) { return; // short-circuit if there are no changes requested } redefineClasses0(mNativeAgent, definitions); }
public static boolean redefineClasses(ClassDefinition[] classDefinitions) { try { if (isRedefineClassesSupported()) { getInstrumentation().redefineClasses(classDefinitions); return true; } } catch (Throwable throwable) { if (classDefinitions != null) { for (ClassDefinition classDefinition : classDefinitions) { logger.info(classDefinition.getDefinitionClass().toString()); } } logger.log(Level.INFO, throwable.getMessage(), throwable); } return false; }
private void reload(Entry e) throws IOException, ClassNotFoundException, UnmodifiableClassException { System.err.println(e.file); cdefs.clear(); if (e.loaderRef != null) { ClassLoader cl = e.loaderRef.get(); if (cl != null) { request(e, cl); if (e.children != null) { for (Entry ce : e.children) { request(ce, cl); } } // System.err.println(cdefs); Agent.inst.redefineClasses(cdefs.toArray(new ClassDefinition[0])); } else { e.loaderRef = null; } } }
@Override public synchronized Class<?> loadClass(String name, byte[] barr) throws UnmodifiableClassException { Class<?> clazz=null; try { clazz = loadClass(name,false,false); // we do not load existing class from disk } catch (ClassNotFoundException cnf) {} // if class already exists if(clazz!=null) { try { InstrumentationFactory.getInstrumentation(config).redefineClasses(new ClassDefinition(clazz,barr)); } catch (ClassNotFoundException e) { // the documentation clearly sais that this exception only exists for backward compatibility and never happen } return clazz; } // class not exists yet return _loadClass(name, barr); }
@Override public synchronized Class<?> loadClass(String name, byte[] barr) throws UnmodifiableClassException { Class<?> clazz=null; try { clazz = loadClass(name); } catch (ClassNotFoundException cnf) {} // if class already exists if(clazz!=null) { try { InstrumentationFactory.getInstrumentation(config).redefineClasses(new ClassDefinition(clazz,barr)); } catch (ClassNotFoundException e) { // the documentation clearly sais that this exception only exists for backward compatibility and never happen } aprint.e("redefined:memory:"+clazz.getName()); return clazz; } // class not exists yet return _loadClass(name, barr); }
public void redefineClasses(ClassDefinition[] definitions) throws ClassNotFoundException { if (!isRedefineClassesSupported()) { throw new UnsupportedOperationException("redefineClasses is not supported in this environment"); } if (definitions == null) { throw new NullPointerException("null passed as 'definitions' in redefineClasses"); } for (int i = 0; i < definitions.length; ++i) { if (definitions[i] == null) { throw new NullPointerException("element of 'definitions' is null in redefineClasses"); } } if (definitions.length == 0) { return; // short-circuit if there are no changes requested } redefineClasses0(mNativeAgent, definitions); }
public void redefineClasses(Map<Class<?>, byte[]> classes) throws ClassNotFoundException, UnmodifiableClassException { if (PluginManager.getInstance().getInstrumentation() == null) { throw new IllegalStateException("Instrumentation agent is not properly installed!"); } ClassDefinition[] definitions = new ClassDefinition[classes.size()]; int i = 0; for (Map.Entry<Class<?>, byte[]> entry : classes.entrySet()) { definitions[i++] = new ClassDefinition(entry.getKey(), entry.getValue()); URL classResource = getClassResource(entry.getKey()); try (OutputStream fileWriter = new FileOutputStream(new File(classResource.toURI()))) { fileWriter.write(entry.getValue()); } catch (Exception e) { throw new RuntimeException(e); } } PluginManager.getInstance().getInstrumentation().redefineClasses(definitions); }
/** * Redefine the supplied set of classes using the supplied bytecode. * * This method operates on a set in order to allow interdependent changes to more than one class at the same time * (a redefinition of class A can require a redefinition of class B). * * @param reloadMap class -> new bytecode * @see java.lang.instrument.Instrumentation#redefineClasses(java.lang.instrument.ClassDefinition...) */ public void hotswap(Map<Class<?>, byte[]> reloadMap) { if (instrumentation == null) { throw new IllegalStateException("Plugin manager is not correctly initialized - no instrumentation available."); } synchronized (reloadMap) { ClassDefinition[] definitions = new ClassDefinition[reloadMap.size()]; String[] classNames = new String[reloadMap.size()]; int i = 0; for (Map.Entry<Class<?>, byte[]> entry : reloadMap.entrySet()) { classNames[i] = entry.getKey().getName(); definitions[i++] = new ClassDefinition(entry.getKey(), entry.getValue()); } try { LOGGER.reload("Reloading classes {} (autoHotswap)", Arrays.toString(classNames)); synchronized (hotswapLock) { instrumentation.redefineClasses(definitions); } LOGGER.debug("... reloaded classes {} (autoHotswap)", Arrays.toString(classNames)); } catch (Exception e) { throw new IllegalStateException("Unable to redefine classes", e); } reloadMap.clear(); } }
@Override public void redefine(ClassBytecodes[] cbcs) throws ClassInstallException, NotImplementedException, EngineTerminationException { int count = cbcs.length; boolean[] status = new boolean[cbcs.length]; int success = 0; NbRemoteLoader ldr = prepareClassLoader(); for (int i = 0; i < count; i++) { String name = cbcs[i].name(); byte[] replaceBytecode = cbcs[i].bytecodes(); Long id = ldr.getClassId(name); if (id != null) { Class defined = ldr.getClassOfId(id); try { agent.getInstrumentation().redefineClasses( new ClassDefinition(defined, replaceBytecode) ); status[i] = true; success++; } catch (ClassNotFoundException | UnmodifiableClassException ex) { Logger.getLogger(AgentWorker.class.getName()).log(Level.SEVERE, null, ex); } } if (success < count) { throw new ClassInstallException("Could not redefine classes", status); } } }
/** * Update edits made to classes in the current VM. */ public static void apply() { try { Recaf.INSTANCE.logging.info("Applying class redefinintions:"); ClassDefinition[] definitions = Marker.getDefinitions(); for (ClassDefinition define : definitions) { Recaf.INSTANCE.logging.info(define.getDefinitionClass().getName(), 1); } instrument.redefineClasses(definitions); Recaf.INSTANCE.logging.info("Done applying redefinitions."); } catch (Exception e) { Recaf.INSTANCE.logging.error(e); } }
public static ClassDefinition[] getDefinitions() throws Exception { Class[] classes = getClasses(); ClassDefinition[] definitions = new ClassDefinition[classes.length]; for (int i = 0; i < classes.length; i++) { Class clazz = classes[i]; byte[] clazzBytes = Asm.toBytes(getNode(clazz)); definitions[i] = new ClassDefinition(clazz, clazzBytes); } return definitions; }
private static void doRedefine(Class<?> clazz) throws Exception { // Load the second version of this class. File f = new File(clazz.getName() + ".class"); System.out.println("Reading test class from " + f); InputStream redefineStream = new FileInputStream(f); byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream); ClassDefinition redefineParamBlock = new ClassDefinition( clazz, redefineBuffer); RedefineMethodWithAnnotationsAgent.getInstrumentation().redefineClasses( new ClassDefinition[] {redefineParamBlock}); }
private static void doRedefine(Class<?> clazz) throws Exception { // Load the second version of this class. File f = new File(clazz.getName() + ".class"); System.out.println("Reading test class from " + f.getAbsolutePath()); InputStream redefineStream = new FileInputStream(f); byte[] redefineBuffer = NamedBuffer.loadBufferFromStream(redefineStream); ClassDefinition redefineParamBlock = new ClassDefinition( clazz, redefineBuffer); RedefineMethodInBacktraceAgent.getInstrumentation().redefineClasses( new ClassDefinition[] {redefineParamBlock}); }
static void redefine() { if (instr == null) { System.out.println("NOT REDEFINED"); return; } ClassDefinition cd = new ClassDefinition(CLS, getClassFile(1)); try { instr.redefineClasses(cd); } catch (Exception e) { throw new Error(e); } System.out.println("REDEFINED"); }
public static void premain(String agentArgs, final Instrumentation inst) throws Exception { String s = agentArgs.substring(0, agentArgs.indexOf(".class")); clz = Class.forName(s.replace('/', '.')); InputStream in; Module m = clz.getModule(); if (m != null) { in = m.getResourceAsStream(agentArgs); } else { ClassLoader loader = RedefineClassWithNativeMethodAgent.class.getClassLoader(); in = loader.getResourceAsStream(agentArgs); } if (in == null) { throw new Exception("Cannot find class: " + agentArgs); } byte[] buffer = in.readAllBytes(); new Timer(true).schedule(new TimerTask() { public void run() { try { System.out.println("Instrumenting"); ClassDefinition cld = new ClassDefinition(clz, buffer); inst.redefineClasses(new ClassDefinition[] { cld }); } catch (Exception e) { e.printStackTrace(); } } }, 500); }
public void redefineClasses(List<String> classNameList) { HashSet<String> paramSet = new HashSet<String>(); for (int i = 0; i < classNameList.size(); i++) { String className = classNameList.get(i); paramSet.add(className); } Class[] classes = inst.getAllLoadedClasses(); ArrayList<ClassDefinition> definitionList = new ArrayList<ClassDefinition>(); boolean allSuccess = true; for (int i = 0; paramSet.size() > 0 && i < classes.length; i++) { if (paramSet.contains(classes[i].getName())) { try { byte[] buff = ClassUtil.getByteCode(classes[i]); if (buff == null) { continue; } definitionList.add(new ClassDefinition(classes[i], buff)); paramSet.remove(classes[i].getName()); } catch (Exception e) { allSuccess = false; Logger.error(e); break; } } } if (definitionList.size() > 0 && allSuccess) { AgentTransformer transformer = new AgentTransformer(); inst.addTransformer(transformer); try { inst.redefineClasses(definitionList.toArray(new ClassDefinition[definitionList.size()])); } catch (Throwable th) { throw new RuntimeException("Failed to redefine classes", th); } finally { inst.removeTransformer(transformer); } } }
private static ClassDefinition createReDefinition(File from){ try { final byte[] bytes = Files.readAllBytes(from.toPath()); final ClassType type = ClassFileInput.readClassType(new ByteArrayInputStream(bytes)); final String name = type.getName(); Class clazz = Class.forName(name); return new ClassDefinition(clazz, bytes); } catch (Exception e) { System.err.println("Failed to create definition for " + from.getAbsolutePath()); e.printStackTrace(); return null; } }
@RequestHandler(RequestCmd.REDEFINE_CLASSES) public Pack redefineClasses(Pack param) { final MapPack p = new MapPack(); ListValue classLv = ((MapPack) param).getList("class"); HashSet<String> paramSet = new HashSet<String>(); for (int i = 0; i < classLv.size(); i++) { String className = classLv.getString(i); paramSet.add(className); } Class[] classes = JavaAgent.getInstrumentation().getAllLoadedClasses(); ArrayList<ClassDefinition> definitionList = new ArrayList<ClassDefinition>(); boolean allSuccess = true; for (int i = 0; paramSet.size() > 0 && i < classes.length; i++) { if (paramSet.contains(classes[i].getName())) { try { byte[] buff = ClassUtil.getByteCode(classes[i]); if (buff == null) { continue; } definitionList.add(new ClassDefinition(classes[i], buff)); paramSet.remove(classes[i].getName()); } catch (Exception e) { p.put("success", new BooleanValue(false)); p.put("error", e.toString()); allSuccess = false; break; } } } if (definitionList.size() > 0 && allSuccess) { try { JavaAgent.getInstrumentation().redefineClasses(definitionList.toArray(new ClassDefinition[definitionList.size()])); p.put("success", new BooleanValue(true)); } catch (Throwable th) { p.put("success", new BooleanValue(false)); p.put("error", th.toString()); } } return p; }