package org.graalvm.compiler.printer;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.graalvm.compiler.bytecode.BytecodeDisassembler;
import org.graalvm.compiler.code.CompilationResult;
import org.graalvm.compiler.code.DisassemblerProvider;
import org.graalvm.compiler.core.common.alloc.Trace;
import org.graalvm.compiler.core.common.alloc.TraceBuilderResult;
import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
import org.graalvm.compiler.core.gen.NodeLIRBuilder;
import org.graalvm.compiler.debug.Debug;
import org.graalvm.compiler.debug.DebugDumpHandler;
import org.graalvm.compiler.debug.DebugDumpScope;
import org.graalvm.compiler.debug.GraalDebugConfig.Options;
import org.graalvm.compiler.debug.GraalError;
import org.graalvm.compiler.debug.TTY;
import org.graalvm.compiler.graph.Graph;
import org.graalvm.compiler.java.BciBlockMapping;
import org.graalvm.compiler.lir.LIR;
import org.graalvm.compiler.lir.debug.IntervalDumper;
import org.graalvm.compiler.nodes.StructuredGraph;
import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
import org.graalvm.compiler.options.UniquePathUtilities;
import org.graalvm.compiler.serviceprovider.GraalServices;
import jdk.vm.ci.code.CodeCacheProvider;
import jdk.vm.ci.code.InstalledCode;
import jdk.vm.ci.meta.JavaMethod;
import jdk.vm.ci.meta.ResolvedJavaMethod;
public class CFGPrinterObserver implements DebugDumpHandler {
private CFGPrinter cfgPrinter;
private File cfgFile;
private JavaMethod curMethod;
private List<String> curDecorators = Collections.emptyList();
private final boolean dumpFrontend;
public CFGPrinterObserver(boolean dumpFrontend) {
this.dumpFrontend = dumpFrontend;
}
@Override
public void dump(Object object, String message) {
try {
dumpSandboxed(object, message);
} catch (Throwable ex) {
TTY.println("CFGPrinter: Exception during output of " + message + ": " + ex);
ex.printStackTrace();
}
}
private boolean checkMethodScope() {
JavaMethod method = null;
ArrayList<String> decorators = new ArrayList<>();
for (Object o : Debug.context()) {
if (o instanceof JavaMethod) {
method = (JavaMethod) o;
decorators.clear();
} else if (o instanceof StructuredGraph) {
StructuredGraph graph = (StructuredGraph) o;
if (graph.method() != null) {
method = graph.method();
decorators.clear();
}
} else if (o instanceof DebugDumpScope) {
DebugDumpScope debugDumpScope = (DebugDumpScope) o;
if (debugDumpScope.decorator) {
decorators.add(debugDumpScope.name);
}
}
}
if (method == null) {
return false;
}
if (!method.equals(curMethod) || !curDecorators.equals(decorators)) {
cfgPrinter.printCompilation(method);
TTY.println("CFGPrinter: Dumping method %s to %s", method, cfgFile.getAbsolutePath());
}
curMethod = method;
curDecorators = decorators;
return true;
}
private static boolean isFrontendObject(Object object) {
return object instanceof Graph || object instanceof BciBlockMapping;
}
private LIR lastLIR = null;
private IntervalDumper delayedIntervals = null;
public void dumpSandboxed(Object object, String message) {
if (!dumpFrontend && isFrontendObject(object)) {
return;
}
if (cfgPrinter == null) {
cfgFile = getCFGPath().toFile();
try {
OutputStream out = new BufferedOutputStream(new FileOutputStream(cfgFile));
cfgPrinter = new CFGPrinter(out);
} catch (FileNotFoundException e) {
throw new GraalError("Could not open " + cfgFile.getAbsolutePath());
}
TTY.println("CFGPrinter: Output to file %s", cfgFile.getAbsolutePath());
}
if (!checkMethodScope()) {
return;
}
try {
if (curMethod instanceof ResolvedJavaMethod) {
cfgPrinter.method = (ResolvedJavaMethod) curMethod;
}
if (object instanceof LIR) {
cfgPrinter.lir = (LIR) object;
} else {
cfgPrinter.lir = Debug.contextLookup(LIR.class);
}
cfgPrinter.nodeLirGenerator = Debug.contextLookup(NodeLIRBuilder.class);
if (cfgPrinter.nodeLirGenerator != null) {
cfgPrinter.target = cfgPrinter.nodeLirGenerator.getLIRGeneratorTool().target();
}
if (cfgPrinter.lir != null && cfgPrinter.lir.getControlFlowGraph() instanceof ControlFlowGraph) {
cfgPrinter.cfg = (ControlFlowGraph) cfgPrinter.lir.getControlFlowGraph();
}
CodeCacheProvider codeCache = Debug.contextLookup(CodeCacheProvider.class);
if (codeCache != null) {
cfgPrinter.target = codeCache.getTarget();
}
if (object instanceof BciBlockMapping) {
BciBlockMapping blockMap = (BciBlockMapping) object;
cfgPrinter.printCFG(message, blockMap);
if (blockMap.code.getCode() != null) {
cfgPrinter.printBytecodes(new BytecodeDisassembler(false).disassemble(blockMap.code));
}
} else if (object instanceof LIR) {
cfgPrinter.printCFG(message, cfgPrinter.lir.codeEmittingOrder(), false);
lastLIR = (LIR) object;
if (delayedIntervals != null) {
cfgPrinter.printIntervals(message, delayedIntervals);
delayedIntervals = null;
}
} else if (object instanceof ScheduleResult) {
cfgPrinter.printSchedule(message, (ScheduleResult) object);
} else if (object instanceof StructuredGraph) {
if (cfgPrinter.cfg == null) {
StructuredGraph graph = (StructuredGraph) object;
cfgPrinter.cfg = ControlFlowGraph.compute(graph, true, true, true, false);
cfgPrinter.printCFG(message, cfgPrinter.cfg.getBlocks(), true);
} else {
cfgPrinter.printCFG(message, cfgPrinter.cfg.getBlocks(), true);
}
} else if (object instanceof CompilationResult) {
final CompilationResult compResult = (CompilationResult) object;
cfgPrinter.printMachineCode(disassemble(codeCache, compResult, null), message);
} else if (object instanceof InstalledCode) {
CompilationResult compResult = Debug.contextLookup(CompilationResult.class);
if (compResult != null) {
cfgPrinter.printMachineCode(disassemble(codeCache, compResult, (InstalledCode) object), message);
}
} else if (object instanceof IntervalDumper) {
if (lastLIR == cfgPrinter.lir) {
cfgPrinter.printIntervals(message, (IntervalDumper) object);
} else {
if (delayedIntervals != null) {
Debug.log("Some delayed intervals were dropped (%s)", delayedIntervals);
}
delayedIntervals = (IntervalDumper) object;
}
} else if (object instanceof AbstractBlockBase<?>[]) {
cfgPrinter.printCFG(message, (AbstractBlockBase<?>[]) object, false);
} else if (object instanceof Trace) {
cfgPrinter.printCFG(message, ((Trace) object).getBlocks(), false);
} else if (object instanceof TraceBuilderResult) {
cfgPrinter.printTraces(message, (TraceBuilderResult) object);
}
} finally {
cfgPrinter.target = null;
cfgPrinter.lir = null;
cfgPrinter.nodeLirGenerator = null;
cfgPrinter.cfg = null;
cfgPrinter.flush();
}
}
public static Path getCFGPath() {
return UniquePathUtilities.getPath(Options.PrintCFGFileName, Options.DumpPath, "cfg");
}
static class DisassemblerHolder {
private static final DisassemblerProvider disassembler;
static {
DisassemblerProvider selected = null;
for (DisassemblerProvider d : GraalServices.load(DisassemblerProvider.class)) {
String name = d.getName().toLowerCase();
if (name.contains("hcf") || name.contains("hexcodefile")) {
selected = d;
break;
}
}
if (selected == null) {
selected = new DisassemblerProvider() {
@Override
public String getName() {
return "nop";
}
};
}
disassembler = selected;
}
}
private static String disassemble(CodeCacheProvider codeCache, CompilationResult compResult, InstalledCode installedCode) {
DisassemblerProvider dis = DisassemblerHolder.disassembler;
if (installedCode != null) {
return dis.disassembleInstalledCode(codeCache, compResult, installedCode);
}
return dis.disassembleCompiledCode(codeCache, compResult);
}
@Override
public void close() {
if (cfgPrinter != null) {
cfgPrinter.close();
cfgPrinter = null;
curDecorators = Collections.emptyList();
curMethod = null;
}
}
}