43972
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
|
43972
|
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 |
*/
|
50858
|
23 |
|
|
24 |
|
43972
|
25 |
package org.graalvm.compiler.printer;
|
|
26 |
|
46640
|
27 |
import static org.graalvm.compiler.debug.DebugOptions.PrintCFG;
|
|
28 |
|
43972
|
29 |
import java.io.BufferedOutputStream;
|
|
30 |
import java.io.File;
|
|
31 |
import java.io.FileOutputStream;
|
46640
|
32 |
import java.io.IOException;
|
43972
|
33 |
import java.io.OutputStream;
|
47798
|
34 |
import java.nio.file.Path;
|
43972
|
35 |
import java.util.ArrayList;
|
|
36 |
import java.util.Collections;
|
|
37 |
import java.util.List;
|
|
38 |
|
|
39 |
import org.graalvm.compiler.bytecode.BytecodeDisassembler;
|
|
40 |
import org.graalvm.compiler.code.CompilationResult;
|
|
41 |
import org.graalvm.compiler.code.DisassemblerProvider;
|
47084
|
42 |
import org.graalvm.compiler.core.common.CompilationIdentifier;
|
43972
|
43 |
import org.graalvm.compiler.core.common.alloc.Trace;
|
|
44 |
import org.graalvm.compiler.core.common.alloc.TraceBuilderResult;
|
|
45 |
import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
|
|
46 |
import org.graalvm.compiler.core.gen.NodeLIRBuilder;
|
46640
|
47 |
import org.graalvm.compiler.debug.DebugContext;
|
43972
|
48 |
import org.graalvm.compiler.debug.DebugDumpHandler;
|
|
49 |
import org.graalvm.compiler.debug.DebugDumpScope;
|
|
50 |
import org.graalvm.compiler.debug.GraalError;
|
|
51 |
import org.graalvm.compiler.debug.TTY;
|
|
52 |
import org.graalvm.compiler.graph.Graph;
|
|
53 |
import org.graalvm.compiler.java.BciBlockMapping;
|
|
54 |
import org.graalvm.compiler.lir.LIR;
|
|
55 |
import org.graalvm.compiler.lir.debug.IntervalDumper;
|
46536
|
56 |
import org.graalvm.compiler.lir.gen.LIRGenerationResult;
|
43972
|
57 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
58 |
import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
|
|
59 |
import org.graalvm.compiler.nodes.cfg.ControlFlowGraph;
|
46640
|
60 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
61 |
import org.graalvm.compiler.serviceprovider.GraalServices;
|
|
62 |
|
|
63 |
import jdk.vm.ci.code.CodeCacheProvider;
|
|
64 |
import jdk.vm.ci.code.InstalledCode;
|
|
65 |
import jdk.vm.ci.meta.JavaMethod;
|
|
66 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
54328
|
67 |
import jdk.vm.ci.services.Services;
|
43972
|
68 |
|
|
69 |
/**
|
|
70 |
* Observes compilation events and uses {@link CFGPrinter} to produce a control flow graph for the
|
|
71 |
* <a href="http://java.net/projects/c1visualizer/">C1 Visualizer</a>.
|
|
72 |
*/
|
|
73 |
public class CFGPrinterObserver implements DebugDumpHandler {
|
|
74 |
|
|
75 |
private CFGPrinter cfgPrinter;
|
|
76 |
private File cfgFile;
|
|
77 |
private JavaMethod curMethod;
|
47084
|
78 |
private CompilationIdentifier curCompilation;
|
43972
|
79 |
private List<String> curDecorators = Collections.emptyList();
|
|
80 |
|
|
81 |
@Override
|
46640
|
82 |
public void dump(DebugContext debug, Object object, String format, Object... arguments) {
|
46509
|
83 |
String message = String.format(format, arguments);
|
43972
|
84 |
try {
|
46640
|
85 |
dumpSandboxed(debug, object, message);
|
43972
|
86 |
} catch (Throwable ex) {
|
|
87 |
TTY.println("CFGPrinter: Exception during output of " + message + ": " + ex);
|
|
88 |
ex.printStackTrace();
|
|
89 |
}
|
|
90 |
}
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Looks for the outer most method and its {@link DebugDumpScope#decorator}s in the current
|
|
94 |
* debug scope and opens a new compilation scope if this pair does not match the current method
|
|
95 |
* and decorator pair.
|
|
96 |
*/
|
46640
|
97 |
private boolean checkMethodScope(DebugContext debug) {
|
43972
|
98 |
JavaMethod method = null;
|
47084
|
99 |
CompilationIdentifier compilation = null;
|
43972
|
100 |
ArrayList<String> decorators = new ArrayList<>();
|
46640
|
101 |
for (Object o : debug.context()) {
|
43972
|
102 |
if (o instanceof JavaMethod) {
|
|
103 |
method = (JavaMethod) o;
|
|
104 |
decorators.clear();
|
|
105 |
} else if (o instanceof StructuredGraph) {
|
|
106 |
StructuredGraph graph = (StructuredGraph) o;
|
|
107 |
if (graph.method() != null) {
|
|
108 |
method = graph.method();
|
|
109 |
decorators.clear();
|
47084
|
110 |
compilation = graph.compilationId();
|
43972
|
111 |
}
|
|
112 |
} else if (o instanceof DebugDumpScope) {
|
|
113 |
DebugDumpScope debugDumpScope = (DebugDumpScope) o;
|
|
114 |
if (debugDumpScope.decorator) {
|
|
115 |
decorators.add(debugDumpScope.name);
|
|
116 |
}
|
47084
|
117 |
} else if (o instanceof CompilationResult) {
|
|
118 |
CompilationResult compilationResult = (CompilationResult) o;
|
|
119 |
compilation = compilationResult.getCompilationId();
|
43972
|
120 |
}
|
|
121 |
}
|
|
122 |
|
47084
|
123 |
if (method == null && compilation == null) {
|
43972
|
124 |
return false;
|
|
125 |
}
|
|
126 |
|
47084
|
127 |
if (compilation != null) {
|
|
128 |
if (!compilation.equals(curCompilation) || !curDecorators.equals(decorators)) {
|
|
129 |
cfgPrinter.printCompilation(compilation);
|
|
130 |
}
|
|
131 |
} else {
|
|
132 |
if (!method.equals(curMethod) || !curDecorators.equals(decorators)) {
|
|
133 |
cfgPrinter.printCompilation(method);
|
|
134 |
}
|
43972
|
135 |
}
|
47084
|
136 |
curCompilation = compilation;
|
43972
|
137 |
curMethod = method;
|
|
138 |
curDecorators = decorators;
|
|
139 |
return true;
|
|
140 |
}
|
|
141 |
|
|
142 |
private static boolean isFrontendObject(Object object) {
|
|
143 |
return object instanceof Graph || object instanceof BciBlockMapping;
|
|
144 |
}
|
|
145 |
|
|
146 |
private LIR lastLIR = null;
|
|
147 |
private IntervalDumper delayedIntervals = null;
|
|
148 |
|
46640
|
149 |
public void dumpSandboxed(DebugContext debug, Object object, String message) {
|
|
150 |
OptionValues options = debug.getOptions();
|
|
151 |
boolean dumpFrontend = PrintCFG.getValue(options);
|
43972
|
152 |
if (!dumpFrontend && isFrontendObject(object)) {
|
|
153 |
return;
|
|
154 |
}
|
|
155 |
|
|
156 |
if (cfgPrinter == null) {
|
|
157 |
try {
|
47798
|
158 |
Path dumpFile = debug.getDumpPath(".cfg", false);
|
|
159 |
cfgFile = dumpFile.toFile();
|
43972
|
160 |
OutputStream out = new BufferedOutputStream(new FileOutputStream(cfgFile));
|
|
161 |
cfgPrinter = new CFGPrinter(out);
|
46640
|
162 |
} catch (IOException e) {
|
46680
|
163 |
throw (GraalError) new GraalError("Could not open %s", cfgFile == null ? "[null]" : cfgFile.getAbsolutePath()).initCause(e);
|
43972
|
164 |
}
|
|
165 |
}
|
|
166 |
|
46640
|
167 |
if (!checkMethodScope(debug)) {
|
43972
|
168 |
return;
|
|
169 |
}
|
|
170 |
try {
|
|
171 |
if (curMethod instanceof ResolvedJavaMethod) {
|
|
172 |
cfgPrinter.method = (ResolvedJavaMethod) curMethod;
|
|
173 |
}
|
|
174 |
|
|
175 |
if (object instanceof LIR) {
|
|
176 |
cfgPrinter.lir = (LIR) object;
|
|
177 |
} else {
|
46640
|
178 |
cfgPrinter.lir = debug.contextLookup(LIR.class);
|
43972
|
179 |
}
|
46640
|
180 |
cfgPrinter.nodeLirGenerator = debug.contextLookup(NodeLIRBuilder.class);
|
|
181 |
cfgPrinter.res = debug.contextLookup(LIRGenerationResult.class);
|
43972
|
182 |
if (cfgPrinter.nodeLirGenerator != null) {
|
|
183 |
cfgPrinter.target = cfgPrinter.nodeLirGenerator.getLIRGeneratorTool().target();
|
|
184 |
}
|
|
185 |
if (cfgPrinter.lir != null && cfgPrinter.lir.getControlFlowGraph() instanceof ControlFlowGraph) {
|
|
186 |
cfgPrinter.cfg = (ControlFlowGraph) cfgPrinter.lir.getControlFlowGraph();
|
|
187 |
}
|
|
188 |
|
46640
|
189 |
CodeCacheProvider codeCache = debug.contextLookup(CodeCacheProvider.class);
|
43972
|
190 |
if (codeCache != null) {
|
|
191 |
cfgPrinter.target = codeCache.getTarget();
|
|
192 |
}
|
|
193 |
|
|
194 |
if (object instanceof BciBlockMapping) {
|
|
195 |
BciBlockMapping blockMap = (BciBlockMapping) object;
|
|
196 |
cfgPrinter.printCFG(message, blockMap);
|
|
197 |
if (blockMap.code.getCode() != null) {
|
|
198 |
cfgPrinter.printBytecodes(new BytecodeDisassembler(false).disassemble(blockMap.code));
|
|
199 |
}
|
|
200 |
|
|
201 |
} else if (object instanceof LIR) {
|
|
202 |
// Currently no node printing for lir
|
|
203 |
cfgPrinter.printCFG(message, cfgPrinter.lir.codeEmittingOrder(), false);
|
|
204 |
lastLIR = (LIR) object;
|
|
205 |
if (delayedIntervals != null) {
|
|
206 |
cfgPrinter.printIntervals(message, delayedIntervals);
|
|
207 |
delayedIntervals = null;
|
|
208 |
}
|
|
209 |
} else if (object instanceof ScheduleResult) {
|
|
210 |
cfgPrinter.printSchedule(message, (ScheduleResult) object);
|
|
211 |
} else if (object instanceof StructuredGraph) {
|
|
212 |
if (cfgPrinter.cfg == null) {
|
|
213 |
StructuredGraph graph = (StructuredGraph) object;
|
57537
|
214 |
ScheduleResult scheduleResult = GraalDebugHandlersFactory.tryGetSchedule(debug, graph);
|
|
215 |
if (scheduleResult != null) {
|
|
216 |
cfgPrinter.cfg = scheduleResult.getCFG();
|
|
217 |
}
|
|
218 |
}
|
|
219 |
if (cfgPrinter.cfg != null) {
|
43972
|
220 |
cfgPrinter.printCFG(message, cfgPrinter.cfg.getBlocks(), true);
|
|
221 |
}
|
|
222 |
} else if (object instanceof CompilationResult) {
|
|
223 |
final CompilationResult compResult = (CompilationResult) object;
|
|
224 |
cfgPrinter.printMachineCode(disassemble(codeCache, compResult, null), message);
|
|
225 |
} else if (object instanceof InstalledCode) {
|
46640
|
226 |
CompilationResult compResult = debug.contextLookup(CompilationResult.class);
|
43972
|
227 |
if (compResult != null) {
|
|
228 |
cfgPrinter.printMachineCode(disassemble(codeCache, compResult, (InstalledCode) object), message);
|
|
229 |
}
|
|
230 |
} else if (object instanceof IntervalDumper) {
|
|
231 |
if (lastLIR == cfgPrinter.lir) {
|
|
232 |
cfgPrinter.printIntervals(message, (IntervalDumper) object);
|
|
233 |
} else {
|
|
234 |
if (delayedIntervals != null) {
|
46640
|
235 |
debug.log("Some delayed intervals were dropped (%s)", delayedIntervals);
|
43972
|
236 |
}
|
|
237 |
delayedIntervals = (IntervalDumper) object;
|
|
238 |
}
|
|
239 |
} else if (object instanceof AbstractBlockBase<?>[]) {
|
|
240 |
cfgPrinter.printCFG(message, (AbstractBlockBase<?>[]) object, false);
|
|
241 |
} else if (object instanceof Trace) {
|
|
242 |
cfgPrinter.printCFG(message, ((Trace) object).getBlocks(), false);
|
|
243 |
} else if (object instanceof TraceBuilderResult) {
|
|
244 |
cfgPrinter.printTraces(message, (TraceBuilderResult) object);
|
|
245 |
}
|
|
246 |
} finally {
|
|
247 |
cfgPrinter.target = null;
|
|
248 |
cfgPrinter.lir = null;
|
46536
|
249 |
cfgPrinter.res = null;
|
43972
|
250 |
cfgPrinter.nodeLirGenerator = null;
|
|
251 |
cfgPrinter.cfg = null;
|
|
252 |
cfgPrinter.flush();
|
|
253 |
}
|
|
254 |
}
|
|
255 |
|
|
256 |
/** Lazy initialization to delay service lookup until disassembler is actually needed. */
|
|
257 |
static class DisassemblerHolder {
|
|
258 |
private static final DisassemblerProvider disassembler;
|
|
259 |
|
|
260 |
static {
|
|
261 |
DisassemblerProvider selected = null;
|
54328
|
262 |
String arch = Services.getSavedProperties().get("os.arch");
|
43972
|
263 |
for (DisassemblerProvider d : GraalServices.load(DisassemblerProvider.class)) {
|
|
264 |
String name = d.getName().toLowerCase();
|
54328
|
265 |
if (arch.equals("aarch64")) {
|
|
266 |
if (name.contains("hsdis-objdump")) {
|
|
267 |
selected = d;
|
|
268 |
break;
|
|
269 |
}
|
|
270 |
} else {
|
|
271 |
if (name.contains("hcf") || name.contains("hexcodefile")) {
|
|
272 |
selected = d;
|
|
273 |
break;
|
|
274 |
}
|
43972
|
275 |
}
|
|
276 |
}
|
|
277 |
if (selected == null) {
|
|
278 |
selected = new DisassemblerProvider() {
|
|
279 |
@Override
|
|
280 |
public String getName() {
|
|
281 |
return "nop";
|
|
282 |
}
|
|
283 |
};
|
|
284 |
}
|
|
285 |
disassembler = selected;
|
|
286 |
}
|
|
287 |
}
|
|
288 |
|
|
289 |
private static String disassemble(CodeCacheProvider codeCache, CompilationResult compResult, InstalledCode installedCode) {
|
|
290 |
DisassemblerProvider dis = DisassemblerHolder.disassembler;
|
|
291 |
if (installedCode != null) {
|
|
292 |
return dis.disassembleInstalledCode(codeCache, compResult, installedCode);
|
|
293 |
}
|
|
294 |
return dis.disassembleCompiledCode(codeCache, compResult);
|
|
295 |
}
|
|
296 |
|
|
297 |
@Override
|
|
298 |
public void close() {
|
|
299 |
if (cfgPrinter != null) {
|
|
300 |
cfgPrinter.close();
|
|
301 |
cfgPrinter = null;
|
|
302 |
curDecorators = Collections.emptyList();
|
|
303 |
curMethod = null;
|
47084
|
304 |
curCompilation = null;
|
43972
|
305 |
}
|
|
306 |
}
|
46344
|
307 |
|
|
308 |
public String getDumpPath() {
|
|
309 |
if (cfgFile != null) {
|
|
310 |
return cfgFile.getAbsolutePath();
|
|
311 |
}
|
|
312 |
return null;
|
|
313 |
}
|
43972
|
314 |
}
|