private static void dump(Iterable<AbstractInsnNode> il) { Textifier textifier = new Textifier(); MethodVisitor visitor = new TraceMethodVisitor(textifier); for (Iterator<AbstractInsnNode> it = il.iterator(); it.hasNext(); ) { AbstractInsnNode in = it.next(); in.accept(visitor); } StringWriter writer = new StringWriter(); try (PrintWriter pw = new PrintWriter(writer)) { textifier.print(pw); } System.out.println(writer.toString()); }
/** compiles to bytecode, and returns debugging output */ static String toString(Class<?> iface, String source, CompilerSettings settings) { StringWriter output = new StringWriter(); PrintWriter outputWriter = new PrintWriter(output); Textifier textifier = new Textifier(); try { Compiler.compile(iface, "<debugging>", source, settings, textifier); } catch (Exception e) { textifier.print(outputWriter); e.addSuppressed(new Exception("current bytecode: \n" + output)); IOUtils.reThrowUnchecked(e); throw new AssertionError(); } textifier.print(outputWriter); return output.toString(); }
@Override public void visitEnd() { try { accept(inner); super.visitEnd(); } catch(Exception e){ Textifier t = new Textifier(); accept(new TraceMethodVisitor(t)); StringBuilderWriter sw = new StringBuilderWriter(); PrintWriter pw = new PrintWriter(sw); t.print(pw); pw.flush(); String bytecode = sw.getBuilder().toString(); logger.error(String.format("Failure while rendering method %s, %s, %s. ByteCode:\n %s", name, desc, signature, bytecode), e); throw new RuntimeException(String.format("Failure while rendering method %s, %s, %s. ByteCode:\n %s", name, desc, signature, bytecode), e); } }
public static void main(String[] args) throws Exception { final String k2 = "org/apache/drill/Pickle.class"; final URL url = Resources.getResource(k2); final byte[] clazz = Resources.toByteArray(url); final ClassReader cr = new ClassReader(clazz); final ClassWriter cw = writer(); final TraceClassVisitor visitor = new TraceClassVisitor(cw, new Textifier(), new PrintWriter(System.out)); final ValueHolderReplacementVisitor v2 = new ValueHolderReplacementVisitor(visitor, true); cr.accept(v2, ClassReader.EXPAND_FRAMES );//| ClassReader.SKIP_DEBUG); final byte[] output = cw.toByteArray(); Files.write(output, new File("/src/scratch/bytes/S.class")); check(output); final DrillConfig c = DrillConfig.forClient(); final SystemOptionManager m = new SystemOptionManager(c, new LocalPStoreProvider(c)); m.init(); try (QueryClassLoader ql = new QueryClassLoader(DrillConfig.create(), m)) { ql.injectByteCode("org.apache.drill.Pickle$OutgoingBatch", output); Class<?> clz = ql.loadClass("org.apache.drill.Pickle$OutgoingBatch"); clz.getMethod("x").invoke(null); } }
/** * 格式化输出字节码 * @param bytecode */ public static void viewByteCode(byte[] bytecode) { ClassReader cr = new ClassReader(bytecode); ClassNode cn = new ClassNode(); cr.accept(cn, 0); final List<MethodNode> mns = cn.methods; Printer printer = new Textifier(); TraceMethodVisitor mp = new TraceMethodVisitor(printer); for (MethodNode mn : mns) { InsnList inList = mn.instructions; System.out.println(mn.name); for (int i = 0; i < inList.size(); i++) { inList.get(i).accept(mp); StringWriter sw = new StringWriter(); printer.print(new PrintWriter(sw)); printer.getText().clear(); System.out.print(sw.toString()); } } }
public static void main(String[] args) throws Exception { final String k2 = "org/apache/drill/Pickle.class"; final URL url = Resources.getResource(k2); final byte[] clazz = Resources.toByteArray(url); final ClassReader cr = new ClassReader(clazz); final ClassWriter cw = writer(); final TraceClassVisitor visitor = new TraceClassVisitor(cw, new Textifier(), new PrintWriter(System.out)); final ValueHolderReplacementVisitor v2 = new ValueHolderReplacementVisitor(visitor, true); cr.accept(v2, ClassReader.EXPAND_FRAMES );//| ClassReader.SKIP_DEBUG); final byte[] output = cw.toByteArray(); Files.write(output, new File("/src/scratch/bytes/S.class")); check(output); final DrillConfig c = DrillConfig.forClient(); final SystemOptionManager m = new SystemOptionManager(PhysicalPlanReaderTestFactory.defaultLogicalPlanPersistence(c), new LocalPersistentStoreProvider(c), c); m.init(); try (QueryClassLoader ql = new QueryClassLoader(DrillConfig.create(), m)) { ql.injectByteCode("org.apache.drill.Pickle$OutgoingBatch", output); Class<?> clz = ql.loadClass("org.apache.drill.Pickle$OutgoingBatch"); clz.getMethod("x").invoke(null); } }
@Override public void visitEnd() { super.visitEnd(); if (!found && errorNoMatch) { StringWriter output = new StringWriter(); PrintWriter writer = new PrintWriter(output); writer.append("Cannot find nodes"); if (methods.size() > 0) { writer.append(" for methods ").append(methods.toString()); } writer.println(); Printer printer = new Textifier(); TraceMethodVisitor visitor = new TraceMethodVisitor(printer); for (AbstractInsnNode node : nodes) { node.accept(visitor); } printer.print(writer); throw new IllegalStateException(output.toString()); } }
public static void viewByteCode(byte[] bytecode) { ClassReader cr = new ClassReader(bytecode); ClassNode cn = new ClassNode(); cr.accept(cn, 0); final List<MethodNode> mns = cn.methods; Printer printer = new Textifier(); TraceMethodVisitor mp = new TraceMethodVisitor(printer); for (MethodNode mn : mns) { InsnList inList = mn.instructions; System.out.println(mn.name); for (int i = 0; i < inList.size(); i++) { inList.get(i).accept(mp); StringWriter sw = new StringWriter(); printer.print(new PrintWriter(sw)); printer.getText().clear(); System.out.print(sw.toString()); } } }
/** Returns a human readable string for the code that this {@link BytecodeProducer} generates. */ public final String trace() { // TODO(lukes): textifier has support for custom label names by overriding appendLabel. // Consider trying to make use of (using the Label.info field? adding a custom NamedLabel // sub type?) Textifier textifier = new Textifier(Opcodes.ASM6) { { // reset tab sizes. Since we don't care about formatting class names or method // signatures (only code). We only need to set the tab2,tab3 and ltab settings (tab is // for class members). this.tab = null; // trigger an error if used. this.tab2 = " "; // tab setting for instructions this.tab3 = ""; // tab setting for switch cases this.ltab = ""; // tab setting for labels } }; gen(new CodeBuilder(new TraceMethodVisitor(textifier), 0, "trace", "()V")); StringWriter writer = new StringWriter(); textifier.print(new PrintWriter(writer)); return writer.toString(); // Note textifier always adds a trailing newline }
@Override protected void setUp() throws Exception { super.setUp(); jsr = new JSRInlinerAdapter(Opcodes.ASM5, null, 0, "m", "()V", null, null) { @Override public void visitEnd() { System.err.println("started w/ method:" + name); Textifier t = new Textifier(); TraceMethodVisitor mv = new TraceMethodVisitor(t); for (int i = 0; i < instructions.size(); ++i) { instructions.get(i).accept(mv); System.err.print(Integer.toString(i + 100000).substring(1)); System.err.print(" : " + t.text.get(i)); } super.visitEnd(); System.err.println("finished w/ method:" + name); } }; exp = new MethodNode(0, "m", "()V", null, null); }
public static void printMethod(PrintStream out, MethodNode method) { final Textifier textifier = new Textifier(); final TraceMethodVisitor mv = new TraceMethodVisitor(textifier); out.println(method.name + method.desc); for (int j = 0; j < method.instructions.size(); ++j) { method.instructions.get(j).accept(mv); final StringBuffer s = new StringBuffer(); while (s.length() < method.maxStack + method.maxLocals + 1) { s.append(' '); } out.print(Integer.toString(j + 100000).substring(1)); out.print(" " + s + " : " + textifier.text.get(j)); } for (int j = 0; j < method.tryCatchBlocks.size(); ++j) { ((TryCatchBlockNode) method.tryCatchBlocks.get(j)).accept(mv); out.print(" " + textifier.text.get(method.instructions.size()+j)); } out.println(" MAXSTACK " + method.maxStack); out.println(" MAXLOCALS " + method.maxLocals); out.println(); }
@Override protected void setUp() throws Exception { super.setUp(); jsr = new JSRInlinerAdapter(null, 0, "m", "()V", null, null) { @Override public void visitEnd() { System.err.println("started w/ method:" + name); Textifier t = new Textifier(); TraceMethodVisitor mv = new TraceMethodVisitor(t); for (int i = 0; i < instructions.size(); ++i) { instructions.get(i).accept(mv); System.err.print(Integer.toString(i + 100000).substring(1)); System.err.print(" : " + t.text.get(i)); } super.visitEnd(); System.err.println("finished w/ method:" + name); } }; exp = new MethodNode(0, "m", "()V", null, null); }
private static void dump(MethodNode method) { Textifier textifier = new Textifier(); method.accept(new TraceMethodVisitor(textifier)); StringWriter writer = new StringWriter(); try (PrintWriter pw = new PrintWriter(writer)) { textifier.print(pw); } System.out.println(writer.toString()); }
@Override public String toString() { triggerDelayedParsingIfNeccessary(); TraceMethodVisitor visitor = new TraceMethodVisitor(new Textifier()); node.accept(visitor); StringWriter writer = new StringWriter(); visitor.p.print(new PrintWriter(writer)); return writer.toString(); }
private void writeBytecodeToDisk(final byte[] clazz, Path folder) throws IOException { final ClassReader reader = new ClassReader(clazz); CharArrayWriter buffer = new CharArrayWriter(); reader.accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter( buffer)), ClassReader.EXPAND_FRAMES); Path outFile = folder.resolve(currentClass.asJavaName() + ".txt"); Files.write(outFile, Collections.singleton(buffer.toString()), StandardCharsets.UTF_8, StandardOpenOption.CREATE); }
public void traceMethod(String className, final String method) { new ClassReader(inMemoryClassFileManager.classBytes(className)).accept( new ClassVisitor(Opcodes.ASM5) { PrintWriter pw = new PrintWriter(System.out); Textifier p = new Textifier(); @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if(name.equals(method)) { p.visitMethod(access, name, desc, signature, exceptions); return new TraceMethodVisitor(p); } return null; } @Override public void visitEnd() { p.visitClassEnd(); if (pw != null) { p.print(pw); pw.flush(); } } }, ClassReader.SKIP_FRAMES ); }
public String toString() { Textifier t = new Textifier(); accept(new TraceMethodVisitor(t)); StringWriter sw = new StringWriter(); t.print(new PrintWriter(sw)); return sw.toString(); }
public static String textify(byte[] bytes) { Printer textifier = new Textifier(); StringWriter sw = new StringWriter(); new ClassReader(bytes) .accept( new TraceClassVisitor(null, textifier, new PrintWriter(sw, true)), ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE); return sw.toString(); }
public static String toString(MethodNode mn) { TraceMethodVisitor tcv = new TraceMethodVisitor(null, new Textifier()); mn.accept(tcv); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); pw.println(mn.name + " " + mn.desc); tcv.p.print(pw); pw.flush(); return sw.toString(); }
public static List<String> toString(InsnList instructions) { Printer p = new Textifier(); TraceMethodVisitor mp = new TraceMethodVisitor(p); instructions.accept(mp); return p.getText().stream() .map(Object::toString) .map(String::trim) .collect(toList()); }
public static String toString(AbstractInsnNode instruction) { Printer p = new Textifier(); TraceMethodVisitor mp = new TraceMethodVisitor(p); instruction.accept(mp); String text = p.getText().stream() .map(Object::toString) .collect(joining("\n")); return text; }
@Override public String toString() { StringWriter sw = new StringWriter(); new ClassReader(data) .accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter(sw)), 0); return sw.toString(); }
@Override public String toString() { StringWriter writer = new StringWriter(); rawNode.accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter( writer))); return writer.toString(); }
private String toString(MethodTree mt) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); TraceMethodVisitor mv = new TraceMethodVisitor(new Textifier()); mt.rawNode().accept(mv); try (PrintWriter pw = new PrintWriter(bos)) { mv.p.print(pw); } return "Byte code is \n" + new String(bos.toByteArray()); }
public String dumpBytecode(final byte[] bytecode) { if (bytecode == null) { throw new NullPointerException("bytecode must not be null"); } return writeBytecode(bytecode, new Textifier()); }
/** * Prints an individual method behavior. [debugging] * * @param node the node to print * * @since 1.00 */ public static void print(MethodNode node) { Textifier tf = new Textifier(); node.accept(new TraceMethodVisitor(tf)); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); tf.print(pw); pw.flush(); }
/** * Prints an instruction list. * * @param insn instructions * * @since 1.00 */ public static void print(InsnList insn) { Textifier tf = new Textifier(); MethodVisitor vis = new TraceMethodVisitor(tf); for (int i = 0; i < insn.size(); i++) { insn.get(i).accept(vis); } PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); tf.print(pw); pw.flush(); }
private String getText(final MethodNode mn) { Textifier tv = new Textifier(); TraceMethodVisitor tmv = new TraceMethodVisitor(tv); mn.accept(tmv); StringBuilder sb = new StringBuilder(); for (int i = 0; i < tv.text.size(); i++) { sb.append(tv.text.get(i)); } return sb.toString(); }
private static void printMethod(final Analyzer a, final PrintStream out, final MethodNode method) { final Frame[] frames = a.getFrames(); final Textifier textifier = new Textifier(); final TraceMethodVisitor mv = new TraceMethodVisitor(textifier); out.println(method.name + method.desc); for (int j = 0; j < method.instructions.size(); ++j) { method.instructions.get(j).accept(mv); final StringBuffer s = new StringBuffer(); final Frame f = frames[j]; if (f == null) { s.append('?'); } else { for (int k = 0; k < f.getLocals(); ++k) { s.append(getShortName(f.getLocal(k).toString())).append(' '); } s.append(" : "); for (int k = 0; k < f.getStackSize(); ++k) { s.append(getShortName(f.getStack(k).toString())).append(' '); } } while (s.length() < method.maxStack + method.maxLocals + 1) { s.append(' '); } out.print(Integer.toString(j + 100000).substring(1)); out.print(" " + s + " : " + textifier.text.get(j)); } for (int j = 0; j < method.tryCatchBlocks.size(); ++j) { ((TryCatchBlockNode) method.tryCatchBlocks.get(j)).accept(mv); out.print(" " + textifier.text.get(method.instructions.size()+j)); } out.println(" MAXSTACK " + method.maxStack); out.println(" MAXLOCALS " + method.maxLocals); out.println(); }
private String getText(final MethodNode mn) { Textifier tv = new Textifier(); TraceMethodVisitor tmv = new TraceMethodVisitor(tv); mn.accept(tmv); StringBuffer sb = new StringBuffer(); for (int i = 0; i < tv.text.size(); i++) { sb.append(tv.text.get(i)); } return sb.toString(); }
private Stream<String> dumpClassFile(InputStream stream) throws IOException { byte[] textifiedClass; try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); PrintWriter pw = new PrintWriter(bos)) { // NOPMD required by API ClassReader reader = new ClassReader(stream); TraceClassVisitor traceVisitor = new TraceClassVisitor(null, new Textifier(), pw); reader.accept(traceVisitor, asmFlags); textifiedClass = bos.toByteArray(); } try (InputStreamReader streamReader = new InputStreamReader(new ByteArrayInputStream(textifiedClass))) { return CharStreams.readLines(streamReader).stream(); } }
/** * Prints a disassembled view of the given annotation. * * @param desc * the class descriptor of the annotation class. * @param visible * <tt>true</tt> if the annotation is visible at runtime. * @return a visitor to visit the annotation values. */ @Override public Textifier visitAnnotation(final String desc, final boolean visible) { buf.setLength(0); buf.append("withAnnotation("); buf.append(StringUtils.replace(StringUtils.removeEnd(StringUtils.removeStart(desc, "L"), ";"), "/", ".")); buf.append(".class"); text.add(buf.toString()); text.add(").//\n"); return createTextifier(); }
@SuppressWarnings("unchecked") public <T extends TypeEntry> void emitOuterType(T ast) { ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); this.cw = writer; if (VERIFY_EMITTED_BYTECODE) { this.cw = new CheckClassAdapter(this.cw); } AstEmitter<AbstractEmitterContext, T> emitter = (AstEmitter<AbstractEmitterContext, T>) this.set.getAstEmitter(ast.getClass()); if (emitter == null) { throw new IllegalArgumentException("No emitter for ast entry " + ast.getClass().getName()); } emitter.emit(this, ast); this.cw.visitEnd(); byte[] clazz = writer.toByteArray(); if (DUMP_INSTRUCTIONS_AFTER_WRITE) { ClassReader cr = new ClassReader(clazz); ClassNode cn = new ClassNode(); cr.accept(cn, 0); List<MethodNode> methods = cn.methods; for (MethodNode mn : methods) { System.out.println("Method: " + mn.name + mn.desc); Printer printer = new Textifier(); TraceMethodVisitor mp = new TraceMethodVisitor(printer); for (Iterator<AbstractInsnNode> it = mn.instructions.iterator(); it.hasNext();) { AbstractInsnNode insn = it.next(); insn.accept(mp); } StringWriter sw = new StringWriter(); printer.print(new PrintWriter(sw)); String s = sw.toString(); if (s.endsWith("\n")) { s = s.substring(0, s.length() - 1); } System.out.println(s); mn.instructions.accept(mp); } } try { this.out.write(clazz); } catch (IOException e) { Throwables.propagate(e); } }
public static String toString(MethodNode mn) { Textifier t = new Textifier(); TraceMethodVisitor tmv = new TraceMethodVisitor(t); mn.accept(tmv); return t.toString(); }
public MethodRecorder() { printer = new Textifier(); visitor = new TraceMethodVisitor(printer); }
@Override protected Textifier createTextifier() { return new LoggableTextifier(); }