Search in sources :

Example 16 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project quasar by puniverse.

the class QuasarInstrumentor method instrumentClass.

@VisibleForTesting
byte[] instrumentClass(ClassLoader loader, String className, InputStream is, boolean forceInstrumentation) throws IOException {
    className = className != null ? className.replace('.', '/') : null;
    byte[] cb = toByteArray(is);
    MethodDatabase db = getMethodDatabase(loader);
    if (className != null) {
        log(LogLevel.INFO, "TRANSFORM: %s %s", className, (db.getClassEntry(className) != null && db.getClassEntry(className).requiresInstrumentation()) ? "request" : "");
        examine(className, "quasar-1-preinstr", cb);
    } else {
        log(LogLevel.INFO, "TRANSFORM: null className");
    }
    // Phase 1, add a label before any suspendable calls, event API is enough
    final ClassReader r1 = new ClassReader(cb);
    final ClassWriter cw1 = new ClassWriter(r1, 0);
    final LabelSuspendableCallSitesClassVisitor ic1 = new LabelSuspendableCallSitesClassVisitor(cw1, db);
    r1.accept(ic1, 0);
    cb = cw1.toByteArray();
    examine(className, "quasar-2", cb);
    // Phase 2, instrument, tree API
    final ClassReader r2 = new ClassReader(cb);
    final ClassWriter cw2 = new DBClassWriter(db, r2);
    final ClassVisitor cv2 = (check && EXAMINED_CLASS == null) ? new CheckClassAdapter(cw2) : cw2;
    final InstrumentClass ic2 = new InstrumentClass(cv2, db, forceInstrumentation);
    try {
        r2.accept(ic2, ClassReader.SKIP_FRAMES);
        cb = cw2.toByteArray();
    } catch (final Exception e) {
        if (ic2.hasSuspendableMethods()) {
            error("Unable to instrument class " + className, e);
            throw e;
        } else {
            if (!MethodDatabase.isProblematicClass(className))
                log(LogLevel.DEBUG, "Unable to instrument class " + className);
            return null;
        }
    }
    examine(className, "quasar-4", cb);
    // Phase 4, fill suspendable call offsets, event API is enough
    final OffsetClassReader r3 = new OffsetClassReader(cb);
    final ClassWriter cw3 = new ClassWriter(r3, 0);
    final SuspOffsetsAfterInstrClassVisitor ic3 = new SuspOffsetsAfterInstrClassVisitor(cw3, db);
    r3.accept(ic3, 0);
    cb = cw3.toByteArray();
    // DEBUG
    if (EXAMINED_CLASS != null) {
        examine(className, "quasar-5-final", cb);
        if (check) {
            ClassReader r4 = new ClassReader(cb);
            ClassVisitor cv4 = new CheckClassAdapter(new TraceClassVisitor(null), true);
            r4.accept(cv4, 0);
        }
    }
    return cb;
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) VisibleForTesting(co.paralleluniverse.common.util.VisibleForTesting)

Example 17 with TraceClassVisitor

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

the class ClientWeavingHook method weave.

@Override
public void weave(WovenClass wovenClass) {
    Bundle consumerBundle = wovenClass.getBundleWiring().getBundle();
    Set<WeavingData> wd = activator.getWeavingData(consumerBundle);
    if (wd != null) {
        activator.log(Level.FINE, "Weaving class " + wovenClass.getClassName());
        ClassReader cr = new ClassReader(wovenClass.getBytes());
        ClassWriter cw = new OSGiFriendlyClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES, wovenClass.getBundleWiring().getClassLoader());
        TCCLSetterVisitor tsv = new TCCLSetterVisitor(cw, wovenClass.getClassName(), wd);
        cr.accept(tsv, ClassReader.SKIP_FRAMES);
        if (tsv.isWoven()) {
            wovenClass.setBytes(cw.toByteArray());
            if (tsv.additionalImportRequired())
                wovenClass.getDynamicImports().add(addedImport);
            if (activator.isLogEnabled(Level.FINEST)) {
                StringWriter stringWriter = new StringWriter();
                ClassReader reader = new ClassReader(wovenClass.getBytes());
                ClassVisitor tracer = new TraceClassVisitor(new PrintWriter(stringWriter));
                ClassVisitor checker = new CheckClassAdapter(tracer, true);
                reader.accept(checker, 0);
                activator.log(Level.FINEST, "Woven class bytecode: \n" + stringWriter.toString());
            }
        }
    }
}
Also used : WeavingData(org.apache.aries.spifly.WeavingData) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) Bundle(org.osgi.framework.Bundle) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) TCCLSetterVisitor(org.apache.aries.spifly.weaver.TCCLSetterVisitor) PrintWriter(java.io.PrintWriter)

Example 18 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project jacoco by jacoco.

the class FramesTest method dump.

private String dump(byte[] bytes) {
    final StringWriter buffer = new StringWriter();
    final PrintWriter writer = new PrintWriter(buffer);
    new ClassReader(bytes).accept(new MaxStackEliminator(new TraceClassVisitor(writer)), ClassReader.EXPAND_FRAMES);
    return buffer.toString();
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) ClassReader(org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter)

Example 19 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project bazel by bazelbuild.

the class JavacTurbineTest method textify.

static String textify(byte[] bytes) {
    StringWriter sw = new StringWriter();
    ClassReader cr = new ClassReader(bytes);
    cr.accept(new TraceClassVisitor(null, new Textifier(), new PrintWriter(sw, true)), 0);
    return sw.toString();
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) StringWriter(java.io.StringWriter) ClassReader(org.objectweb.asm.ClassReader) Textifier(org.objectweb.asm.util.Textifier) PrintWriter(java.io.PrintWriter)

Example 20 with TraceClassVisitor

use of org.objectweb.asm.util.TraceClassVisitor in project elasticsearch by elastic.

the class SSource method write.

public void write() {
    // Create the ClassWriter.
    int classFrames = ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS;
    int classAccess = Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER | Opcodes.ACC_FINAL;
    String classBase = BASE_CLASS_TYPE.getInternalName();
    String className = CLASS_TYPE.getInternalName();
    String[] classInterfaces = new String[] { Type.getType(scriptInterface.getInterface()).getInternalName() };
    ClassWriter writer = new ClassWriter(classFrames);
    ClassVisitor visitor = writer;
    // if picky is enabled, turn on some checks. instead of VerifyError at the end, you get a helpful stacktrace.
    if (settings.isPicky()) {
        visitor = new SimpleChecksAdapter(visitor);
    }
    if (debugStream != null) {
        visitor = new TraceClassVisitor(visitor, debugStream, null);
    }
    visitor.visit(WriterConstants.CLASS_VERSION, classAccess, className, null, classBase, classInterfaces);
    visitor.visitSource(Location.computeSourceName(name, source), null);
    // Write the constructor:
    MethodWriter constructor = new MethodWriter(Opcodes.ACC_PUBLIC, CONSTRUCTOR, visitor, globals.getStatements(), settings);
    constructor.visitCode();
    constructor.loadThis();
    constructor.loadArgs();
    constructor.invokeConstructor(BASE_CLASS_TYPE, CONSTRUCTOR);
    constructor.returnValue();
    constructor.endMethod();
    // Write the method defined in the interface:
    MethodWriter executeMethod = new MethodWriter(Opcodes.ACC_PUBLIC, scriptInterface.getExecuteMethod(), visitor, globals.getStatements(), settings);
    executeMethod.visitCode();
    write(executeMethod, globals);
    executeMethod.endMethod();
    // Write all functions:
    for (SFunction function : functions) {
        function.write(visitor, settings, globals);
    }
    // Write all synthetic functions. Note that this process may add more :)
    while (!globals.getSyntheticMethods().isEmpty()) {
        List<SFunction> current = new ArrayList<>(globals.getSyntheticMethods().values());
        globals.getSyntheticMethods().clear();
        for (SFunction function : current) {
            function.write(visitor, settings, globals);
        }
    }
    // Write the constants
    if (false == globals.getConstantInitializers().isEmpty()) {
        Collection<Constant> inits = globals.getConstantInitializers().values();
        // Fields
        for (Constant constant : inits) {
            visitor.visitField(Opcodes.ACC_FINAL | Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, constant.name, constant.type.getDescriptor(), null, null).visitEnd();
        }
        // Initialize the constants in a static initializer
        final MethodWriter clinit = new MethodWriter(Opcodes.ACC_STATIC, WriterConstants.CLINIT, visitor, globals.getStatements(), settings);
        clinit.visitCode();
        for (Constant constant : inits) {
            constant.initializer.accept(clinit);
            clinit.putStatic(CLASS_TYPE, constant.name, constant.type);
        }
        clinit.returnValue();
        clinit.endMethod();
    }
    // Write any uses$varName methods for used variables
    for (org.objectweb.asm.commons.Method usesMethod : scriptInterface.getUsesMethods()) {
        MethodWriter ifaceMethod = new MethodWriter(Opcodes.ACC_PUBLIC, usesMethod, visitor, globals.getStatements(), settings);
        ifaceMethod.visitCode();
        ifaceMethod.push(reserved.getUsedVariables().contains(usesMethod.getName().substring("uses$".length())));
        ifaceMethod.returnValue();
        ifaceMethod.endMethod();
    }
    // End writing the class and store the generated bytes.
    visitor.visitEnd();
    bytes = writer.toByteArray();
}
Also used : Constant(org.elasticsearch.painless.Constant) MethodWriter(org.elasticsearch.painless.MethodWriter) ArrayList(java.util.ArrayList) ClassVisitor(org.objectweb.asm.ClassVisitor) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) SimpleChecksAdapter(org.elasticsearch.painless.SimpleChecksAdapter)

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