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