43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package org.graalvm.compiler.printer;
|
|
24 |
|
46640
|
25 |
import static org.graalvm.compiler.debug.DebugOptions.PrintCFG;
|
|
26 |
import static org.graalvm.compiler.printer.GraalDebugHandlersFactory.createDumpPath;
|
|
27 |
|
43972
|
28 |
import java.io.BufferedOutputStream;
|
|
29 |
import java.io.File;
|
|
30 |
import java.io.FileOutputStream;
|
46640
|
31 |
import java.io.IOException;
|
43972
|
32 |
import java.io.OutputStream;
|
|
33 |
import java.util.ArrayList;
|
|
34 |
import java.util.Collections;
|
|
35 |
import java.util.List;
|
|
36 |
|
|
37 |
import org.graalvm.compiler.bytecode.BytecodeDisassembler;
|
|
38 |
import org.graalvm.compiler.code.CompilationResult;
|
|
39 |
import org.graalvm.compiler.code.DisassemblerProvider;
|
|
40 |
import org.graalvm.compiler.core.common.alloc.Trace;
|
|
41 |
import org.graalvm.compiler.core.common.alloc.TraceBuilderResult;
|
|
42 |
import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
|
|
43 |
import org.graalvm.compiler.core.gen.NodeLIRBuilder;
|
46640
|
44 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
45 |
import org.graalvm.compiler.debug.DebugDumpHandler;
|
|
46 |
import org.graalvm.compiler.debug.DebugDumpScope;
|
|
47 |
import org.graalvm.compiler.debug.GraalError;
|
|
48 |
import org.graalvm.compiler.debug.TTY;
|
|
49 |
import org.graalvm.compiler.graph.Graph;
|
|
50 |
import org.graalvm.compiler.java.BciBlockMapping;
|
|
51 |
import org.graalvm.compiler.lir.LIR;
|
46344
|
52 |
import org.graalvm.compiler.lir.alloc.trace.GlobalLivenessInfo;
|
43972
|
53 |
import org.graalvm.compiler.lir.debug.IntervalDumper;
|
46536
|
54 |
import org.graalvm.compiler.lir.gen.LIRGenerationResult;
|
43972
|
55 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
56 |
import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
|
|
57 |
import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
|
46640
|
58 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
59 |
import org.graalvm.compiler.serviceprovider.GraalServices;
|
|
60 |
|
|
61 |
import jdk.vm.ci.code.CodeCacheProvider;
|
|
62 |
import jdk.vm.ci.code.InstalledCode;
|
|
63 |
import jdk.vm.ci.meta.JavaMethod;
|
|
64 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Observes compilation events and uses {@link CFGPrinter} to produce a control flow graph for the
|
|
68 |
* <a href="http://java.net/projects/c1visualizer/">C1 Visualizer</a>.
|
|
69 |
*/
|
|
70 |
public class CFGPrinterObserver implements DebugDumpHandler {
|
|
71 |
|
|
72 |
private CFGPrinter cfgPrinter;
|
|
73 |
private File cfgFile;
|
|
74 |
private JavaMethod curMethod;
|
|
75 |
private List<String> curDecorators = Collections.emptyList();
|
|
76 |
|
|
77 |
@Override
|
46640
|
78 |
public void dump(DebugContext debug, Object object, String format, Object... arguments) {
|
46509
|
79 |
String message = String.format(format, arguments);
|
43972
|
80 |
try {
|
46640
|
81 |
dumpSandboxed(debug, object, message);
|
43972
|
82 |
} catch (Throwable ex) {
|
|
83 |
TTY.println("CFGPrinter: Exception during output of " + message + ": " + ex);
|
|
84 |
ex.printStackTrace();
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Looks for the outer most method and its {@link DebugDumpScope#decorator}s in the current
|
|
90 |
* debug scope and opens a new compilation scope if this pair does not match the current method
|
|
91 |
* and decorator pair.
|
|
92 |
*/
|
46640
|
93 |
private boolean checkMethodScope(DebugContext debug) {
|
43972
|
94 |
JavaMethod method = null;
|
|
95 |
ArrayList<String> decorators = new ArrayList<>();
|
46640
|
96 |
for (Object o : debug.context()) {
|
43972
|
97 |
if (o instanceof JavaMethod) {
|
|
98 |
method = (JavaMethod) o;
|
|
99 |
decorators.clear();
|
|
100 |
} else if (o instanceof StructuredGraph) {
|
|
101 |
StructuredGraph graph = (StructuredGraph) o;
|
|
102 |
if (graph.method() != null) {
|
|
103 |
method = graph.method();
|
|
104 |
decorators.clear();
|
|
105 |
}
|
|
106 |
} else if (o instanceof DebugDumpScope) {
|
|
107 |
DebugDumpScope debugDumpScope = (DebugDumpScope) o;
|
|
108 |
if (debugDumpScope.decorator) {
|
|
109 |
decorators.add(debugDumpScope.name);
|
|
110 |
}
|
|
111 |
}
|
|
112 |
}
|
|
113 |
|
|
114 |
if (method == null) {
|
|
115 |
return false;
|
|
116 |
}
|
|
117 |
|
|
118 |
if (!method.equals(curMethod) || !curDecorators.equals(decorators)) {
|
|
119 |
cfgPrinter.printCompilation(method);
|
|
120 |
}
|
|
121 |
curMethod = method;
|
|
122 |
curDecorators = decorators;
|
|
123 |
return true;
|
|
124 |
}
|
|
125 |
|
|
126 |
private static boolean isFrontendObject(Object object) {
|
|
127 |
return object instanceof Graph || object instanceof BciBlockMapping;
|
|
128 |
}
|
|
129 |
|
|
130 |
private LIR lastLIR = null;
|
|
131 |
private IntervalDumper delayedIntervals = null;
|
|
132 |
|
46640
|
133 |
public void dumpSandboxed(DebugContext debug, Object object, String message) {
|
|
134 |
OptionValues options = debug.getOptions();
|
|
135 |
boolean dumpFrontend = PrintCFG.getValue(options);
|
43972
|
136 |
if (!dumpFrontend && isFrontendObject(object)) {
|
|
137 |
return;
|
|
138 |
}
|
|
139 |
|
|
140 |
if (cfgPrinter == null) {
|
|
141 |
try {
|
46640
|
142 |
Graph graph = debug.contextLookupTopdown(Graph.class);
|
|
143 |
cfgFile = createDumpPath(options, graph, "cfg", false).toFile();
|
43972
|
144 |
OutputStream out = new BufferedOutputStream(new FileOutputStream(cfgFile));
|
|
145 |
cfgPrinter = new CFGPrinter(out);
|
46640
|
146 |
} catch (IOException e) {
|
|
147 |
throw (GraalError) new GraalError("Could not open %s", cfgFile.getAbsolutePath()).initCause(e);
|
43972
|
148 |
}
|
|
149 |
}
|
|
150 |
|
46640
|
151 |
if (!checkMethodScope(debug)) {
|
43972
|
152 |
return;
|
|
153 |
}
|
|
154 |
try {
|
|
155 |
if (curMethod instanceof ResolvedJavaMethod) {
|
|
156 |
cfgPrinter.method = (ResolvedJavaMethod) curMethod;
|
|
157 |
}
|
|
158 |
|
|
159 |
if (object instanceof LIR) {
|
|
160 |
cfgPrinter.lir = (LIR) object;
|
|
161 |
} else {
|
46640
|
162 |
cfgPrinter.lir = debug.contextLookup(LIR.class);
|
43972
|
163 |
}
|
46640
|
164 |
cfgPrinter.nodeLirGenerator = debug.contextLookup(NodeLIRBuilder.class);
|
|
165 |
cfgPrinter.livenessInfo = debug.contextLookup(GlobalLivenessInfo.class);
|
|
166 |
cfgPrinter.res = debug.contextLookup(LIRGenerationResult.class);
|
43972
|
167 |
if (cfgPrinter.nodeLirGenerator != null) {
|
|
168 |
cfgPrinter.target = cfgPrinter.nodeLirGenerator.getLIRGeneratorTool().target();
|
|
169 |
}
|
|
170 |
if (cfgPrinter.lir != null && cfgPrinter.lir.getControlFlowGraph() instanceof ControlFlowGraph) {
|
|
171 |
cfgPrinter.cfg = (ControlFlowGraph) cfgPrinter.lir.getControlFlowGraph();
|
|
172 |
}
|
|
173 |
|
46640
|
174 |
CodeCacheProvider codeCache = debug.contextLookup(CodeCacheProvider.class);
|
43972
|
175 |
if (codeCache != null) {
|
|
176 |
cfgPrinter.target = codeCache.getTarget();
|
|
177 |
}
|
|
178 |
|
|
179 |
if (object instanceof BciBlockMapping) {
|
|
180 |
BciBlockMapping blockMap = (BciBlockMapping) object;
|
|
181 |
cfgPrinter.printCFG(message, blockMap);
|
|
182 |
if (blockMap.code.getCode() != null) {
|
|
183 |
cfgPrinter.printBytecodes(new BytecodeDisassembler(false).disassemble(blockMap.code));
|
|
184 |
}
|
|
185 |
|
|
186 |
} else if (object instanceof LIR) {
|
|
187 |
// Currently no node printing for lir
|
|
188 |
cfgPrinter.printCFG(message, cfgPrinter.lir.codeEmittingOrder(), false);
|
|
189 |
lastLIR = (LIR) object;
|
|
190 |
if (delayedIntervals != null) {
|
|
191 |
cfgPrinter.printIntervals(message, delayedIntervals);
|
|
192 |
delayedIntervals = null;
|
|
193 |
}
|
|
194 |
} else if (object instanceof ScheduleResult) {
|
|
195 |
cfgPrinter.printSchedule(message, (ScheduleResult) object);
|
|
196 |
} else if (object instanceof StructuredGraph) {
|
|
197 |
if (cfgPrinter.cfg == null) {
|
|
198 |
StructuredGraph graph = (StructuredGraph) object;
|
|
199 |
cfgPrinter.cfg = ControlFlowGraph.compute(graph, true, true, true, false);
|
|
200 |
cfgPrinter.printCFG(message, cfgPrinter.cfg.getBlocks(), true);
|
|
201 |
} else {
|
|
202 |
cfgPrinter.printCFG(message, cfgPrinter.cfg.getBlocks(), true);
|
|
203 |
}
|
|
204 |
|
|
205 |
} else if (object instanceof CompilationResult) {
|
|
206 |
final CompilationResult compResult = (CompilationResult) object;
|
|
207 |
cfgPrinter.printMachineCode(disassemble(codeCache, compResult, null), message);
|
|
208 |
} else if (object instanceof InstalledCode) {
|
46640
|
209 |
CompilationResult compResult = debug.contextLookup(CompilationResult.class);
|
43972
|
210 |
if (compResult != null) {
|
|
211 |
cfgPrinter.printMachineCode(disassemble(codeCache, compResult, (InstalledCode) object), message);
|
|
212 |
}
|
|
213 |
} else if (object instanceof IntervalDumper) {
|
|
214 |
if (lastLIR == cfgPrinter.lir) {
|
|
215 |
cfgPrinter.printIntervals(message, (IntervalDumper) object);
|
|
216 |
} else {
|
|
217 |
if (delayedIntervals != null) {
|
46640
|
218 |
debug.log("Some delayed intervals were dropped (%s)", delayedIntervals);
|
43972
|
219 |
}
|
|
220 |
delayedIntervals = (IntervalDumper) object;
|
|
221 |
}
|
|
222 |
} else if (object instanceof AbstractBlockBase<?>[]) {
|
|
223 |
cfgPrinter.printCFG(message, (AbstractBlockBase<?>[]) object, false);
|
|
224 |
} else if (object instanceof Trace) {
|
|
225 |
cfgPrinter.printCFG(message, ((Trace) object).getBlocks(), false);
|
|
226 |
} else if (object instanceof TraceBuilderResult) {
|
|
227 |
cfgPrinter.printTraces(message, (TraceBuilderResult) object);
|
|
228 |
}
|
|
229 |
} finally {
|
|
230 |
cfgPrinter.target = null;
|
|
231 |
cfgPrinter.lir = null;
|
46536
|
232 |
cfgPrinter.res = null;
|
43972
|
233 |
cfgPrinter.nodeLirGenerator = null;
|
46344
|
234 |
cfgPrinter.livenessInfo = null;
|
43972
|
235 |
cfgPrinter.cfg = null;
|
|
236 |
cfgPrinter.flush();
|
|
237 |
}
|
|
238 |
}
|
|
239 |
|
|
240 |
/** Lazy initialization to delay service lookup until disassembler is actually needed. */
|
|
241 |
static class DisassemblerHolder {
|
|
242 |
private static final DisassemblerProvider disassembler;
|
|
243 |
|
|
244 |
static {
|
|
245 |
DisassemblerProvider selected = null;
|
|
246 |
for (DisassemblerProvider d : GraalServices.load(DisassemblerProvider.class)) {
|
|
247 |
String name = d.getName().toLowerCase();
|
|
248 |
if (name.contains("hcf") || name.contains("hexcodefile")) {
|
|
249 |
selected = d;
|
|
250 |
break;
|
|
251 |
}
|
|
252 |
}
|
|
253 |
if (selected == null) {
|
|
254 |
selected = new DisassemblerProvider() {
|
|
255 |
@Override
|
|
256 |
public String getName() {
|
|
257 |
return "nop";
|
|
258 |
}
|
|
259 |
};
|
|
260 |
}
|
|
261 |
disassembler = selected;
|
|
262 |
}
|
|
263 |
}
|
|
264 |
|
|
265 |
private static String disassemble(CodeCacheProvider codeCache, CompilationResult compResult, InstalledCode installedCode) {
|
|
266 |
DisassemblerProvider dis = DisassemblerHolder.disassembler;
|
|
267 |
if (installedCode != null) {
|
|
268 |
return dis.disassembleInstalledCode(codeCache, compResult, installedCode);
|
|
269 |
}
|
|
270 |
return dis.disassembleCompiledCode(codeCache, compResult);
|
|
271 |
}
|
|
272 |
|
|
273 |
@Override
|
|
274 |
public void close() {
|
|
275 |
if (cfgPrinter != null) {
|
|
276 |
cfgPrinter.close();
|
|
277 |
cfgPrinter = null;
|
|
278 |
curDecorators = Collections.emptyList();
|
|
279 |
curMethod = null;
|
|
280 |
}
|
|
281 |
}
|
46344
|
282 |
|
|
283 |
public String getDumpPath() {
|
|
284 |
if (cfgFile != null) {
|
|
285 |
return cfgFile.getAbsolutePath();
|
|
286 |
}
|
|
287 |
return null;
|
|
288 |
}
|
43972
|
289 |
}
|