Search in sources :

Example 6 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project Galacticraft by micdoodle8.

the class ASMHelper method dump.

public static void dump(Acceptor acceptor, File file, boolean filterImportant, boolean sortLocals, boolean textify) {
    try {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        PrintWriter pout = new PrintWriter(file);
        ClassVisitor cv = new TraceClassVisitor(null, textify ? new Textifier() : new ASMifier(), pout);
        if (filterImportant) {
            cv = new ImportantInsnVisitor(cv);
        }
        if (sortLocals) {
            cv = new LocalVariablesSorterVisitor(cv);
        }
        acceptor.accept(cv);
        pout.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ASMifier(org.objectweb.asm.util.ASMifier) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) Textifier(org.objectweb.asm.util.Textifier) PrintWriter(java.io.PrintWriter)

Example 7 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project soot by Sable.

the class AbstractASMBackend method generateTextualRepresentation.

/**
 * Outputs the bytecode generated as a textual representation
 * @param pw The PrintWriter the textual representation is written to
 */
public void generateTextualRepresentation(PrintWriter pw) {
    cv = new TraceClassVisitor(pw);
    generateByteCode();
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor)

Example 8 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project drill by apache.

the class ReplaceMethodInvoke method main.

// private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ReplaceMethodInvoke.class);
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);
    // | ClassReader.SKIP_DEBUG);
    cr.accept(v2, ClassReader.EXPAND_FRAMES);
    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);
    }
}
Also used : LocalPersistentStoreProvider(org.apache.drill.exec.store.sys.store.provider.LocalPersistentStoreProvider) SystemOptionManager(org.apache.drill.exec.server.options.SystemOptionManager) Textifier(org.objectweb.asm.util.Textifier) URL(java.net.URL) ClassWriter(org.objectweb.asm.ClassWriter) QueryClassLoader(org.apache.drill.exec.compile.QueryClassLoader) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) DrillConfig(org.apache.drill.common.config.DrillConfig) ClassReader(org.objectweb.asm.ClassReader) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 9 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project drill by apache.

the class AsmUtil method logClass.

/**
 * Write a class to the log.
 *
 * <p>
 * Writes at level TRACE.
 *
 * @param logger
 *          the logger to write to
 * @param logTag
 *          a tag to print to the log
 * @param classNode
 *          the class
 */
public static void logClass(final Logger logger, final String logTag, final ClassNode classNode) {
    if (logger.isTraceEnabled()) {
        logger.trace(logTag);
        final StringWriter stringWriter = new StringWriter();
        final PrintWriter printWriter = new PrintWriter(stringWriter);
        final TraceClassVisitor traceClassVisitor = new TraceClassVisitor(printWriter);
        classNode.accept(traceClassVisitor);
        logger.trace(stringWriter.toString());
    }
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 10 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project presto by prestodb.

the class ClassGenerator method defineClasses.

public Map<String, Class<?>> defineClasses(List<ClassDefinition> classDefinitions) {
    ClassInfoLoader classInfoLoader = createClassInfoLoader(classDefinitions, classLoader);
    Map<String, byte[]> bytecodes = new LinkedHashMap<>();
    for (ClassDefinition classDefinition : classDefinitions) {
        // We call the simpler class writer first to get any errors out using simpler setting.
        // This helps when we have large queries that can potentially cause COMPUTE_FRAMES
        // (used by SmartClassWriter for doing more thorough analysis)
        ClassWriter simpleClassWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        classDefinition.visit(simpleClassWriter);
        try {
            simpleClassWriter.toByteArray();
        } catch (ClassTooLargeException | MethodTooLargeException largeCodeException) {
            throw new ByteCodeTooLargeException(largeCodeException);
        } catch (RuntimeException e) {
            throw new CompilationException("Error compiling class: " + classDefinition.getName(), e);
        }
        ClassWriter writer = new SmartClassWriter(classInfoLoader);
        try {
            classDefinition.visit(fakeLineNumbers ? new AddFakeLineNumberClassVisitor(writer) : writer);
        } catch (IndexOutOfBoundsException | NegativeArraySizeException e) {
            StringWriter out = new StringWriter();
            classDefinition.visit(new TraceClassVisitor(null, new Textifier(), new PrintWriter(out)));
            throw new IllegalArgumentException("Error processing class definition:\n" + out, e);
        }
        byte[] bytecode;
        try {
            bytecode = writer.toByteArray();
        } catch (ClassTooLargeException | MethodTooLargeException largeCodeException) {
            throw new ByteCodeTooLargeException(largeCodeException);
        } catch (RuntimeException e) {
            throw new CompilationException("Error compiling class: " + classDefinition.getName(), e);
        }
        bytecodes.put(classDefinition.getType().getJavaClassName(), bytecode);
        if (runAsmVerifier) {
            ClassReader reader = new ClassReader(bytecode);
            CheckClassAdapter.verify(reader, classLoader, true, new PrintWriter(output));
        }
    }
    dumpClassPath.ifPresent(path -> bytecodes.forEach((className, bytecode) -> {
        String name = typeFromJavaClassName(className).getClassName() + ".class";
        Path file = path.resolve(name).toAbsolutePath();
        try {
            createDirectories(file.getParent());
            Files.write(file, bytecode);
        } catch (IOException e) {
            throw new UncheckedIOException("Failed to write generated class file: " + file, e);
        }
    }));
    if (dumpRawBytecode) {
        for (byte[] bytecode : bytecodes.values()) {
            ClassReader classReader = new ClassReader(bytecode);
            classReader.accept(new TraceClassVisitor(new PrintWriter(output)), ClassReader.EXPAND_FRAMES);
        }
    }
    Map<String, Class<?>> classes = classLoader.defineClasses(bytecodes);
    try {
        for (Class<?> clazz : classes.values()) {
            Reflection.initialize(clazz);
        }
    } catch (VerifyError e) {
        throw new RuntimeException(e);
    }
    return classes;
}
Also used : ClassWriter(org.objectweb.asm.ClassWriter) MethodHandle(java.lang.invoke.MethodHandle) CharStreams.nullWriter(com.google.common.io.CharStreams.nullWriter) LinkedHashMap(java.util.LinkedHashMap) ImmutableList(com.google.common.collect.ImmutableList) ParameterizedType.typeFromJavaClassName(com.facebook.presto.bytecode.ParameterizedType.typeFromJavaClassName) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) MethodTooLargeException(org.objectweb.asm.MethodTooLargeException) Path(java.nio.file.Path) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) PrintWriter(java.io.PrintWriter) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) StringWriter(java.io.StringWriter) Reflection(com.google.common.reflect.Reflection) IOException(java.io.IOException) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) Files.createDirectories(java.nio.file.Files.createDirectories) ClassInfoLoader.createClassInfoLoader(com.facebook.presto.bytecode.ClassInfoLoader.createClassInfoLoader) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) ClassReader(org.objectweb.asm.ClassReader) Writer(java.io.Writer) Optional(java.util.Optional) ClassTooLargeException(org.objectweb.asm.ClassTooLargeException) Textifier(org.objectweb.asm.util.Textifier) UncheckedIOException(java.io.UncheckedIOException) Textifier(org.objectweb.asm.util.Textifier) LinkedHashMap(java.util.LinkedHashMap) ClassInfoLoader.createClassInfoLoader(com.facebook.presto.bytecode.ClassInfoLoader.createClassInfoLoader) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) MethodTooLargeException(org.objectweb.asm.MethodTooLargeException) PrintWriter(java.io.PrintWriter) Path(java.nio.file.Path) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ClassWriter(org.objectweb.asm.ClassWriter) ClassTooLargeException(org.objectweb.asm.ClassTooLargeException) ClassReader(org.objectweb.asm.ClassReader)

Aggregations

TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)39 PrintWriter (java.io.PrintWriter)31 ClassReader (org.objectweb.asm.ClassReader)24 ClassWriter (org.objectweb.asm.ClassWriter)16 StringWriter (java.io.StringWriter)14 ClassVisitor (org.objectweb.asm.ClassVisitor)10 Textifier (org.objectweb.asm.util.Textifier)10 IOException (java.io.IOException)6 File (java.io.File)5 ClassNode (org.objectweb.asm.tree.ClassNode)5 ArrayList (java.util.ArrayList)3 ASMifier (org.objectweb.asm.util.ASMifier)3 CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)3 NeverNullArgAnalyzerAdapter (edu.columbia.cs.psl.phosphor.instrumenter.analyzer.NeverNullArgAnalyzerAdapter)2 FileInputStream (java.io.FileInputStream)2 URL (java.net.URL)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 DrillConfig (org.apache.drill.common.config.DrillConfig)2 QueryClassLoader (org.apache.drill.exec.compile.QueryClassLoader)2