1 /*
   2  * Copyright (c) 2012, 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.debug;
  24 
  25 import java.io.PrintStream;
  26 import java.util.Collection;
  27 
  28 import org.graalvm.compiler.debug.DebugContext.Scope;
  29 import org.graalvm.compiler.options.OptionValues;
  30 
  31 import jdk.vm.ci.meta.JavaMethod;
  32 
  33 public interface DebugConfig {
  34 
  35     /**
  36      * Returns the option values used to configure this object.
  37      */
  38     OptionValues getOptions();
  39 
  40     /**
  41      * Determines the current log level in {@code scope}.
  42      */
  43     int getLogLevel(DebugContext.Scope scope);
  44 
  45     /**
  46      * Determines the current dump level in {@code scope}.
  47      */
  48     int getDumpLevel(DebugContext.Scope scope);
  49 
  50     /**
  51      * Determines if logging is enabled for any {@link JavaMethod} in {@code scope}'s
  52      * {@linkplain Scope#getCurrentContext() context}.
  53      */
  54     boolean isLogEnabledForMethod(DebugContext.Scope scope);
  55 
  56     /**
  57      * Determines if counting is enabled in {@code scope}.
  58      *
  59      * @see DebugContext#counter(CharSequence)
  60      */
  61     boolean isCountEnabled(DebugContext.Scope scope);
  62 
  63     /**
  64      * Determines if memory use tracking is {@code scope}.
  65      *
  66      * @see DebugContext#memUseTracker(CharSequence)
  67      */
  68     boolean isMemUseTrackingEnabled(DebugContext.Scope scope);
  69 
  70     /**
  71      * Determines if dumping is enabled for any {@link JavaMethod} in {@code scope}'s
  72      * {@linkplain Scope#getCurrentContext() context}.
  73      */
  74     boolean isDumpEnabledForMethod(DebugContext.Scope scope);
  75 
  76     /**
  77      * @see DebugContext#isVerifyEnabled()
  78      */
  79     boolean isVerifyEnabled(DebugContext.Scope scope);
  80 
  81     /**
  82      * @see DebugContext#isVerifyEnabledForMethod()
  83      */
  84     boolean isVerifyEnabledForMethod(DebugContext.Scope scope);
  85 
  86     /**
  87      * @see DebugContext#timer(CharSequence)
  88      */
  89     boolean isTimeEnabled(DebugContext.Scope scope);
  90 
  91     /**
  92      * Handles notification of an exception occurring within a debug scope.
  93      *
  94      * @return the exception object that is to be propagated to parent scope. A value of
  95      *         {@code null} indicates that {@code e} is to be propagated.
  96      */
  97     RuntimeException interceptException(DebugContext debug, Throwable e);
  98 
  99     /**
 100      * Gets an unmodifiable view of the dump handlers registered with this configuration.
 101      */
 102     Collection<DebugDumpHandler> dumpHandlers();
 103 
 104     /**
 105      * Gets the {@link PrintStream} for logging.
 106      */
 107     PrintStream output();
 108 
 109     /**
 110      * Gets an unmodifiable view of the verify handlers registered with this configuration.
 111      */
 112     Collection<DebugVerifyHandler> verifyHandlers();
 113 
 114     default void closeDumpHandlers(boolean ignoreErrors) {
 115         for (DebugDumpHandler handler : dumpHandlers()) {
 116             try {
 117                 handler.close();
 118             } catch (Throwable e) {
 119                 if (!ignoreErrors) {
 120                     throw e;
 121                 }
 122             }
 123         }
 124     }
 125 
 126     /**
 127      * Extracts a {@link JavaMethod} from an opaque debug context.
 128      *
 129      * @return the {@link JavaMethod} represented by {@code context} or null
 130      */
 131     static JavaMethod asJavaMethod(Object context) {
 132         if (context instanceof JavaMethodContext) {
 133             return ((JavaMethodContext) context).asJavaMethod();
 134         }
 135         if (context instanceof JavaMethod) {
 136             return (JavaMethod) context;
 137         }
 138         return null;
 139     }
 140 }