Search in sources :

Example 1 with BTraceProbeFactory

use of com.sun.btrace.runtime.BTraceProbeFactory in project btrace by btraceio.

the class Compiler method compile.

private Map<String, byte[]> compile(MemoryJavaFileManager manager, Iterable<? extends JavaFileObject> compUnits, Writer err, String sourcePath, final String classPath) {
    // to collect errors, warnings etc.
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    // javac options
    List<String> options = new ArrayList<>();
    options.add("-Xlint:all");
    options.add("-g:lines");
    options.add("-deprecation");
    options.add("-source");
    options.add("1.7");
    options.add("-target");
    options.add("1.7");
    if (sourcePath != null) {
        options.add("-sourcepath");
        options.add(sourcePath);
    }
    if (classPath != null) {
        options.add("-classpath");
        options.add(classPath);
    }
    // create a compilation task
    JavacTask task = (JavacTask) compiler.getTask(err, manager, diagnostics, options, null, compUnits);
    Verifier btraceVerifier = new Verifier();
    task.setTaskListener(btraceVerifier);
    // we add BTrace Verifier as a (JSR 269) Processor
    List<Processor> processors = new ArrayList<>(1);
    processors.add(btraceVerifier);
    task.setProcessors(processors);
    final PrintWriter perr = (err instanceof PrintWriter) ? (PrintWriter) err : new PrintWriter(err);
    // print dignostics messages in case of failures.
    if (task.call() == false || containsErrors(diagnostics)) {
        for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
            printDiagnostic(diagnostic, perr);
        }
        perr.flush();
        return null;
    }
    // collect .class bytes of all compiled classes
    Map<String, byte[]> result = new HashMap<>();
    try {
        Map<String, byte[]> classBytes = manager.getClassBytes();
        List<String> classNames = btraceVerifier.getClassNames();
        for (String name : classNames) {
            if (classBytes.containsKey(name)) {
                dump(name + "_before", classBytes.get(name));
                ClassReader cr = new ClassReader(classBytes.get(name));
                ClassWriter cw = new CompilerClassWriter(classPath, perr);
                cr.accept(new Postprocessor(cw), ClassReader.EXPAND_FRAMES + ClassReader.SKIP_DEBUG);
                byte[] classData = cw.toByteArray();
                dump(name + "_after", classData);
                if (generatePack) {
                    // temp hack; need turn off verifier
                    SharedSettings.GLOBAL.setTrusted(true);
                    BTraceProbeNode bpn = (BTraceProbeNode) new BTraceProbeFactory(SharedSettings.GLOBAL).createProbe(classData);
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    try (DataOutputStream dos = new DataOutputStream(bos)) {
                        BTraceProbePersisted bpp = BTraceProbePersisted.from(bpn);
                        bpp.write(dos);
                    }
                    classData = bos.toByteArray();
                }
                result.put(name, classData);
            }
        }
    } catch (IOException e) {
        e.printStackTrace(perr);
    } finally {
        try {
            manager.close();
        } catch (IOException exp) {
        }
    }
    return result;
}
Also used : Processor(javax.annotation.processing.Processor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Diagnostic(javax.tools.Diagnostic) JavaFileObject(javax.tools.JavaFileObject) DiagnosticCollector(javax.tools.DiagnosticCollector) JavacTask(com.sun.source.util.JavacTask) ClassWriter(com.sun.btrace.org.objectweb.asm.ClassWriter) BTraceProbeFactory(com.sun.btrace.runtime.BTraceProbeFactory) BTraceProbePersisted(com.sun.btrace.runtime.BTraceProbePersisted) ClassReader(com.sun.btrace.org.objectweb.asm.ClassReader) BTraceProbeNode(com.sun.btrace.runtime.BTraceProbeNode)

Example 2 with BTraceProbeFactory

use of com.sun.btrace.runtime.BTraceProbeFactory in project btrace by btraceio.

the class Client method load.

// Internals only below this point
private BTraceProbe load(byte[] buf, boolean canLoadPack) {
    BTraceProbeFactory f = new BTraceProbeFactory(settings, canLoadPack);
    debugPrint("loading BTrace class");
    BTraceProbe cn = f.createProbe(buf, argsMap);
    if (cn != null) {
        if (isDebug()) {
            if (cn.isVerified()) {
                debugPrint("loaded '" + cn.getClassName() + "' successfully");
            } else {
                debugPrint(cn.getClassName() + " failed verification");
                return null;
            }
        }
    }
    return BTraceProbePersisted.from(cn);
}
Also used : BTraceProbeFactory(com.sun.btrace.runtime.BTraceProbeFactory)

Example 3 with BTraceProbeFactory

use of com.sun.btrace.runtime.BTraceProbeFactory in project btrace by btraceio.

the class ProbeDump method main.

public static void main(String[] args) throws Exception {
    String path = args[0];
    BTraceProbeFactory bpf = new BTraceProbeFactory(SharedSettings.GLOBAL);
    BTraceProbe bp = bpf.createProbe(new FileInputStream(path));
    FileSystem fs = FileSystems.getDefault();
    Path p = fs.getPath(args[1]);
    Files.write(p.resolve(bp.getClassName().replace(".", "_") + "_full.class"), bp.getFullBytecode());
    Files.write(p.resolve(bp.getClassName().replace(".", "_") + "_dh.class"), bp.getDataHolderBytecode());
}
Also used : Path(java.nio.file.Path) BTraceProbe(com.sun.btrace.runtime.BTraceProbe) BTraceProbeFactory(com.sun.btrace.runtime.BTraceProbeFactory) FileSystem(java.nio.file.FileSystem) FileInputStream(java.io.FileInputStream)

Aggregations

BTraceProbeFactory (com.sun.btrace.runtime.BTraceProbeFactory)3 ClassReader (com.sun.btrace.org.objectweb.asm.ClassReader)1 ClassWriter (com.sun.btrace.org.objectweb.asm.ClassWriter)1 BTraceProbe (com.sun.btrace.runtime.BTraceProbe)1 BTraceProbeNode (com.sun.btrace.runtime.BTraceProbeNode)1 BTraceProbePersisted (com.sun.btrace.runtime.BTraceProbePersisted)1 JavacTask (com.sun.source.util.JavacTask)1 FileInputStream (java.io.FileInputStream)1 FileSystem (java.nio.file.FileSystem)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Processor (javax.annotation.processing.Processor)1 Diagnostic (javax.tools.Diagnostic)1 DiagnosticCollector (javax.tools.DiagnosticCollector)1 JavaFileObject (javax.tools.JavaFileObject)1