46640
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
|
46640
|
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 |
|
46640
|
25 |
package org.graalvm.compiler.printer;
|
|
26 |
|
|
27 |
import java.util.ArrayList;
|
|
28 |
import java.util.List;
|
|
29 |
|
|
30 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
|
57537
|
31 |
import org.graalvm.compiler.debug.DebugCloseable;
|
46640
|
32 |
import org.graalvm.compiler.debug.DebugContext;
|
|
33 |
import org.graalvm.compiler.debug.DebugDumpHandler;
|
|
34 |
import org.graalvm.compiler.debug.DebugHandler;
|
|
35 |
import org.graalvm.compiler.debug.DebugHandlersFactory;
|
|
36 |
import org.graalvm.compiler.debug.DebugOptions;
|
|
37 |
import org.graalvm.compiler.graph.Node;
|
|
38 |
import org.graalvm.compiler.nodeinfo.Verbosity;
|
57537
|
39 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
40 |
import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult;
|
46640
|
41 |
import org.graalvm.compiler.nodes.util.GraphUtil;
|
|
42 |
import org.graalvm.compiler.options.OptionValues;
|
57537
|
43 |
import org.graalvm.compiler.phases.schedule.SchedulePhase;
|
46640
|
44 |
import org.graalvm.compiler.serviceprovider.ServiceProvider;
|
|
45 |
|
|
46 |
@ServiceProvider(DebugHandlersFactory.class)
|
|
47 |
public class GraalDebugHandlersFactory implements DebugHandlersFactory {
|
|
48 |
|
|
49 |
private final SnippetReflectionProvider snippetReflection;
|
|
50 |
|
|
51 |
public GraalDebugHandlersFactory() {
|
|
52 |
this.snippetReflection = null;
|
|
53 |
}
|
|
54 |
|
|
55 |
public GraalDebugHandlersFactory(SnippetReflectionProvider snippetReflection) {
|
|
56 |
this.snippetReflection = snippetReflection;
|
|
57 |
}
|
|
58 |
|
|
59 |
@Override
|
|
60 |
public List<DebugHandler> createHandlers(OptionValues options) {
|
|
61 |
List<DebugHandler> handlers = new ArrayList<>();
|
47798
|
62 |
handlers.add(new GraphPrinterDumpHandler((debug, graph) -> new BinaryGraphPrinter(debug, snippetReflection)));
|
46640
|
63 |
if (DebugOptions.PrintCanonicalGraphStrings.getValue(options)) {
|
47798
|
64 |
handlers.add(new GraphPrinterDumpHandler((debug, graph) -> createStringPrinter(snippetReflection)));
|
46640
|
65 |
}
|
|
66 |
handlers.add(new NodeDumper());
|
|
67 |
if (DebugOptions.PrintCFG.getValue(options) || DebugOptions.PrintBackendCFG.getValue(options)) {
|
|
68 |
handlers.add(new CFGPrinterObserver());
|
|
69 |
}
|
|
70 |
handlers.add(new NoDeadCodeVerifyHandler());
|
|
71 |
return handlers;
|
|
72 |
}
|
|
73 |
|
|
74 |
private static class NodeDumper implements DebugDumpHandler {
|
|
75 |
@Override
|
|
76 |
public void dump(DebugContext debug, Object object, String format, Object... arguments) {
|
|
77 |
if (object instanceof Node) {
|
|
78 |
Node node = (Node) object;
|
|
79 |
String location = GraphUtil.approxSourceLocation(node);
|
|
80 |
String nodeName = node.toString(Verbosity.Debugger);
|
|
81 |
if (location != null) {
|
|
82 |
debug.log("Context obj %s (approx. location: %s)", nodeName, location);
|
|
83 |
} else {
|
|
84 |
debug.log("Context obj %s", nodeName);
|
|
85 |
}
|
|
86 |
}
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
private static CanonicalStringGraphPrinter createStringPrinter(SnippetReflectionProvider snippetReflection) {
|
|
91 |
return new CanonicalStringGraphPrinter(snippetReflection);
|
|
92 |
}
|
|
93 |
|
57537
|
94 |
@SuppressWarnings("try")
|
|
95 |
static ScheduleResult tryGetSchedule(DebugContext debug, StructuredGraph graph) {
|
|
96 |
ScheduleResult scheduleResult = graph.getLastSchedule();
|
|
97 |
if (scheduleResult == null) {
|
|
98 |
// Also provide a schedule when an error occurs
|
|
99 |
if (DebugOptions.PrintGraphWithSchedule.getValue(graph.getOptions()) || debug.contextLookup(Throwable.class) != null) {
|
|
100 |
try (DebugCloseable noIntercept = debug.disableIntercept()) {
|
|
101 |
SchedulePhase schedule = new SchedulePhase(graph.getOptions());
|
|
102 |
schedule.apply(graph);
|
|
103 |
scheduleResult = graph.getLastSchedule();
|
|
104 |
} catch (Throwable t) {
|
|
105 |
}
|
|
106 |
}
|
|
107 |
}
|
|
108 |
return scheduleResult;
|
|
109 |
}
|
46640
|
110 |
}
|