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 java.util.ArrayList;
  26 import java.util.List;
  27 
  28 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
  29 import org.graalvm.compiler.debug.DebugContext;
  30 import org.graalvm.compiler.debug.DebugDumpHandler;
  31 import org.graalvm.compiler.debug.DebugHandler;
  32 import org.graalvm.compiler.debug.DebugHandlersFactory;
  33 import org.graalvm.compiler.debug.DebugOptions;
  34 import org.graalvm.compiler.debug.TTY;
  35 import org.graalvm.compiler.graph.Node;
  36 import org.graalvm.compiler.nodeinfo.Verbosity;
  37 import org.graalvm.compiler.nodes.util.GraphUtil;
  38 import org.graalvm.compiler.options.OptionValues;
  39 import org.graalvm.compiler.serviceprovider.ServiceProvider;
  40 
  41 @ServiceProvider(DebugHandlersFactory.class)
  42 public class GraalDebugHandlersFactory implements DebugHandlersFactory {
  43 
  44     private final SnippetReflectionProvider snippetReflection;
  45 
  46     public GraalDebugHandlersFactory() {
  47         this.snippetReflection = null;
  48     }
  49 
  50     public GraalDebugHandlersFactory(SnippetReflectionProvider snippetReflection) {
  51         this.snippetReflection = snippetReflection;
  52     }
  53 
  54     @Override
  55     public List<DebugHandler> createHandlers(OptionValues options) {
  56         List<DebugHandler> handlers = new ArrayList<>();
  57         handlers.add(new GraphPrinterDumpHandler((debug, graph) -> new BinaryGraphPrinter(debug, snippetReflection)));
  58         if (DebugOptions.PrintCanonicalGraphStrings.getValue(options)) {
  59             handlers.add(new GraphPrinterDumpHandler((debug, graph) -> createStringPrinter(snippetReflection)));
  60         }
  61         handlers.add(new NodeDumper());
  62         if (DebugOptions.PrintCFG.getValue(options) || DebugOptions.PrintBackendCFG.getValue(options)) {
  63             if (DebugOptions.PrintCFG.getValue(options)) {
  64                 TTY.out.println("Complete C1Visualizer dumping slows down PrintBinaryGraphs: use -Dgraal.PrintCFG=false to disable it");
  65             }
  66             handlers.add(new CFGPrinterObserver());
  67         }
  68         handlers.add(new NoDeadCodeVerifyHandler());
  69         return handlers;
  70     }
  71 
  72     private static class NodeDumper implements DebugDumpHandler {
  73         @Override
  74         public void dump(DebugContext debug, Object object, String format, Object... arguments) {
  75             if (object instanceof Node) {
  76                 Node node = (Node) object;
  77                 String location = GraphUtil.approxSourceLocation(node);
  78                 String nodeName = node.toString(Verbosity.Debugger);
  79                 if (location != null) {
  80                     debug.log("Context obj %s (approx. location: %s)", nodeName, location);
  81                 } else {
  82                     debug.log("Context obj %s", nodeName);
  83                 }
  84             }
  85         }
  86     }
  87 
  88     private static CanonicalStringGraphPrinter createStringPrinter(SnippetReflectionProvider snippetReflection) {
  89         return new CanonicalStringGraphPrinter(snippetReflection);
  90     }
  91 
  92 }