1 /* 2 * Copyright (c) 2015, 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 25 import static org.graalvm.compiler.debug.GraalDebugConfig.Options.DumpPath; 26 import static org.graalvm.compiler.debug.GraalDebugConfig.Options.PrintBinaryGraphPort; 27 import static org.graalvm.compiler.debug.GraalDebugConfig.Options.PrintBinaryGraphs; 28 import static org.graalvm.compiler.debug.GraalDebugConfig.Options.PrintCanonicalGraphStringsDirectory; 29 import static org.graalvm.compiler.debug.GraalDebugConfig.Options.PrintGraphHost; 30 import static org.graalvm.compiler.debug.GraalDebugConfig.Options.PrintGraphFileName; 31 import static org.graalvm.compiler.debug.GraalDebugConfig.Options.PrintXmlGraphPort; 32 33 import java.io.IOException; 34 import java.io.InterruptedIOException; 35 import java.net.InetSocketAddress; 36 import java.net.Socket; 37 import java.nio.channels.ClosedByInterruptException; 38 import java.nio.channels.FileChannel; 39 import java.nio.channels.SocketChannel; 40 import java.nio.file.Files; 41 import java.nio.file.Path; 42 import java.nio.file.StandardOpenOption; 43 44 import org.graalvm.compiler.debug.Debug; 45 import org.graalvm.compiler.debug.DebugConfig; 46 import org.graalvm.compiler.debug.DebugConfigCustomizer; 47 import org.graalvm.compiler.debug.DebugDumpHandler; 48 import org.graalvm.compiler.debug.GraalDebugConfig.Options; 49 import org.graalvm.compiler.debug.TTY; 50 import org.graalvm.compiler.graph.Node; 51 import org.graalvm.compiler.nodeinfo.Verbosity; 52 import org.graalvm.compiler.nodes.util.GraphUtil; 53 import org.graalvm.compiler.options.OptionValues; 54 import org.graalvm.compiler.options.UniquePathUtilities; 55 import org.graalvm.compiler.serviceprovider.ServiceProvider; 56 57 @ServiceProvider(DebugConfigCustomizer.class) 58 public class GraalDebugConfigCustomizer implements DebugConfigCustomizer { 59 60 @Override 61 public void customize(DebugConfig config) { 62 OptionValues options = config.getOptions(); 63 if (Options.PrintGraphFile.getValue(options)) { 64 config.dumpHandlers().add(new GraphPrinterDumpHandler(() -> createFilePrinter(options))); 65 } else { 66 config.dumpHandlers().add(new GraphPrinterDumpHandler(() -> createNetworkPrinter(options))); 67 } 68 if (Options.PrintCanonicalGraphStrings.getValue(options)) { 69 config.dumpHandlers().add(new GraphPrinterDumpHandler(() -> createStringPrinter(options))); 70 } 71 config.dumpHandlers().add(new NodeDumper()); 72 if (Options.PrintCFG.getValue(options) || Options.PrintBackendCFG.getValue(options)) { 73 if (Options.PrintBinaryGraphs.getValue(options) && Options.PrintCFG.getValue(options)) { 74 TTY.out.println("Complete C1Visualizer dumping slows down PrintBinaryGraphs: use -Dgraal.PrintCFG=false to disable it"); 75 } 76 config.dumpHandlers().add(new CFGPrinterObserver(Options.PrintCFG.getValue(options))); 77 } 78 config.verifyHandlers().add(new NoDeadCodeVerifyHandler()); 79 } 80 81 private static class NodeDumper implements DebugDumpHandler { 82 @Override 83 public void dump(Object object, String format, Object... arguments) { 84 if (object instanceof Node) { 85 String location = GraphUtil.approxSourceLocation((Node) object); 86 String node = ((Node) object).toString(Verbosity.Debugger); 87 if (location != null) { 88 Debug.log("Context obj %s (approx. location: %s)", node, location); 89 } else { 90 Debug.log("Context obj %s", node); 91 } 92 } 93 } 94 95 @Override 96 public void close() { 97 } 98 99 @Override 100 public void addCapability(Object capability) { 101 } 102 } 103 104 private static CanonicalStringGraphPrinter createStringPrinter(OptionValues options) { 105 // Construct the path to the directory. 106 Path path = UniquePathUtilities.getPath(options, PrintCanonicalGraphStringsDirectory, Options.DumpPath, ""); 107 return new CanonicalStringGraphPrinter(path); 108 } 109 110 private static GraphPrinter createNetworkPrinter(OptionValues options) throws IOException { 111 String host = PrintGraphHost.getValue(options); 112 int port = PrintBinaryGraphs.getValue(options) ? PrintBinaryGraphPort.getValue(options) : PrintXmlGraphPort.getValue(options); 113 try { 114 GraphPrinter printer; 115 if (Options.PrintBinaryGraphs.getValue(options)) { 116 printer = new BinaryGraphPrinter(SocketChannel.open(new InetSocketAddress(host, port))); 117 } else { 118 printer = new IdealGraphPrinter(new Socket(host, port).getOutputStream(), true); 119 } 120 TTY.println("Connected to the IGV on %s:%d", host, port); 121 return printer; 122 } catch (ClosedByInterruptException | InterruptedIOException e) { 123 /* 124 * Interrupts should not count as errors because they may be caused by a cancelled Graal 125 * compilation. ClosedByInterruptException occurs if the SocketChannel could not be 126 * opened. InterruptedIOException occurs if new Socket(..) was interrupted. 127 */ 128 return null; 129 } catch (IOException e) { 130 if (!Options.PrintGraphFile.hasBeenSet(options)) { 131 TTY.println(String.format("Could not connect to the IGV on %s:%d - falling back to file dumping...", host, port)); 132 return createFilePrinter(options); 133 } else { 134 throw new IOException(String.format("Could not connect to the IGV on %s:%d", host, port), e); 135 } 136 } 137 } 138 139 private static Path getFilePrinterPath(OptionValues options) { 140 // Construct the path to the file. 141 return UniquePathUtilities.getPath(options, PrintGraphFileName, DumpPath, PrintBinaryGraphs.getValue(options) ? "bgv" : "gv.xml"); 142 } 143 144 private static GraphPrinter createFilePrinter(OptionValues options) throws IOException { 145 Path path = getFilePrinterPath(options); 146 try { 147 GraphPrinter printer; 148 if (Options.PrintBinaryGraphs.getValue(options)) { 149 printer = new BinaryGraphPrinter(FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW)); 150 } else { 151 printer = new IdealGraphPrinter(Files.newOutputStream(path), true); 152 } 153 TTY.println("Dumping IGV graphs to %s", path.toAbsolutePath().toString()); 154 return printer; 155 } catch (IOException e) { 156 throw new IOException(String.format("Failed to open %s to dump IGV graphs", path), e); 157 } 158 } 159 }