1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_BASE_LOGGING_H_
18 #define ART_RUNTIME_BASE_LOGGING_H_
19 
20 #include <memory>
21 #include <ostream>
22 
23 #include "base/macros.h"
24 
25 namespace art {
26 
27 enum LogSeverity {
28   VERBOSE,
29   DEBUG,
30   INFO,
31   WARNING,
32   ERROR,
33   FATAL,
34   INTERNAL_FATAL,  // For Runtime::Abort.
35 };
36 
37 // The members of this struct are the valid arguments to VLOG and VLOG_IS_ON in code,
38 // and the "-verbose:" command line argument.
39 struct LogVerbosity {
40   bool class_linker;  // Enabled with "-verbose:class".
41   bool compiler;
42   bool gc;
43   bool heap;
44   bool jdwp;
45   bool jit;
46   bool jni;
47   bool monitor;
48   bool oat;
49   bool profiler;
50   bool signals;
51   bool startup;
52   bool third_party_jni;  // Enabled with "-verbose:third-party-jni".
53   bool threads;
54   bool verifier;
55 };
56 
57 // Global log verbosity setting, initialized by InitLogging.
58 extern LogVerbosity gLogVerbosity;
59 
60 // 0 if not abort, non-zero if an abort is in progress. Used on fatal exit to prevents recursive
61 // aborts. Global declaration allows us to disable some error checking to ensure fatal shutdown
62 // makes forward progress.
63 extern unsigned int gAborting;
64 
65 // Configure logging based on ANDROID_LOG_TAGS environment variable.
66 // We need to parse a string that looks like
67 //
68 //      *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
69 //
70 // The tag (or '*' for the global level) comes first, followed by a colon
71 // and a letter indicating the minimum priority level we're expected to log.
72 // This can be used to reveal or conceal logs with specific tags.
73 extern void InitLogging(char* argv[]);
74 
75 // Returns the command line used to invoke the current tool or null if InitLogging hasn't been
76 // performed.
77 extern const char* GetCmdLine();
78 
79 // The command used to start the ART runtime, such as "/system/bin/dalvikvm". If InitLogging hasn't
80 // been performed then just returns "art"
81 extern const char* ProgramInvocationName();
82 
83 // A short version of the command used to start the ART runtime, such as "dalvikvm". If InitLogging
84 // hasn't been performed then just returns "art"
85 extern const char* ProgramInvocationShortName();
86 
87 // Logs a message to logcat on Android otherwise to stderr. If the severity is FATAL it also causes
88 // an abort. For example: LOG(FATAL) << "We didn't expect to reach here";
89 #define LOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, -1).stream()
90 
91 // A variant of LOG that also logs the current errno value. To be used when library calls fail.
92 #define PLOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, errno).stream()
93 
94 // Marker that code is yet to be implemented.
95 #define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
96 
97 // Is verbose logging enabled for the given module? Where the module is defined in LogVerbosity.
98 #define VLOG_IS_ON(module) UNLIKELY(::art::gLogVerbosity.module)
99 
100 // Variant of LOG that logs when verbose logging is enabled for a module. For example,
101 // VLOG(jni) << "A JNI operation was performed";
102 #define VLOG(module) \
103   if (VLOG_IS_ON(module)) \
104     ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
105 
106 // Return the stream associated with logging for the given module.
107 #define VLOG_STREAM(module) ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
108 
109 // Check whether condition x holds and LOG(FATAL) if not. The value of the expression x is only
110 // evaluated once. Extra logging can be appended using << after. For example,
111 // CHECK(false == true) results in a log message of "Check failed: false == true".
112 #define CHECK(x) \
113   if (UNLIKELY(!(x))) \
114     ::art::LogMessage(__FILE__, __LINE__, ::art::FATAL, -1).stream() \
115         << "Check failed: " #x << " "
116 
117 // Helper for CHECK_xx(x,y) macros.
118 #define CHECK_OP(LHS, RHS, OP) \
119   for (auto _values = ::art::MakeEagerEvaluator(LHS, RHS); \
120        UNLIKELY(!(_values.lhs OP _values.rhs)); /* empty */) \
121     ::art::LogMessage(__FILE__, __LINE__, ::art::FATAL, -1).stream() \
122         << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
123         << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
124 
125 
126 // Check whether a condition holds between x and y, LOG(FATAL) if not. The value of the expressions
127 // x and y is evaluated once. Extra logging can be appended using << after. For example,
128 // CHECK_NE(0 == 1, false) results in "Check failed: false != false (0==1=false, false=false) ".
129 #define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
130 #define CHECK_NE(x, y) CHECK_OP(x, y, !=)
131 #define CHECK_LE(x, y) CHECK_OP(x, y, <=)
132 #define CHECK_LT(x, y) CHECK_OP(x, y, <)
133 #define CHECK_GE(x, y) CHECK_OP(x, y, >=)
134 #define CHECK_GT(x, y) CHECK_OP(x, y, >)
135 
136 // Helper for CHECK_STRxx(s1,s2) macros.
137 #define CHECK_STROP(s1, s2, sense) \
138   if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \
139     LOG(::art::FATAL) << "Check failed: " \
140         << "\"" << s1 << "\"" \
141         << (sense ? " == " : " != ") \
142         << "\"" << s2 << "\""
143 
144 // Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not.
145 #define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
146 #define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
147 
148 // Perform the pthread function call(args), LOG(FATAL) on error.
149 #define CHECK_PTHREAD_CALL(call, args, what) \
150   do { \
151     int rc = call args; \
152     if (rc != 0) { \
153       errno = rc; \
154       PLOG(::art::FATAL) << # call << " failed for " << what; \
155     } \
156   } while (false)
157 
158 // CHECK that can be used in a constexpr function. For example,
159 //    constexpr int half(int n) {
160 //      return
161 //          DCHECK_CONSTEXPR(n >= 0, , 0)
162 //          CHECK_CONSTEXPR((n & 1) == 0), << "Extra debugging output: n = " << n, 0)
163 //          n / 2;
164 //    }
165 #define CHECK_CONSTEXPR(x, out, dummy) \
166   (UNLIKELY(!(x))) ? (LOG(::art::FATAL) << "Check failed: " << #x out, dummy) :
167 
168 
169 // DCHECKs are debug variants of CHECKs only enabled in debug builds. Generally CHECK should be
170 // used unless profiling identifies a CHECK as being in performance critical code.
171 #if defined(NDEBUG)
172 static constexpr bool kEnableDChecks = false;
173 #else
174 static constexpr bool kEnableDChecks = true;
175 #endif
176 
177 #define DCHECK(x) if (::art::kEnableDChecks) CHECK(x)
178 #define DCHECK_EQ(x, y) if (::art::kEnableDChecks) CHECK_EQ(x, y)
179 #define DCHECK_NE(x, y) if (::art::kEnableDChecks) CHECK_NE(x, y)
180 #define DCHECK_LE(x, y) if (::art::kEnableDChecks) CHECK_LE(x, y)
181 #define DCHECK_LT(x, y) if (::art::kEnableDChecks) CHECK_LT(x, y)
182 #define DCHECK_GE(x, y) if (::art::kEnableDChecks) CHECK_GE(x, y)
183 #define DCHECK_GT(x, y) if (::art::kEnableDChecks) CHECK_GT(x, y)
184 #define DCHECK_STREQ(s1, s2) if (::art::kEnableDChecks) CHECK_STREQ(s1, s2)
185 #define DCHECK_STRNE(s1, s2) if (::art::kEnableDChecks) CHECK_STRNE(s1, s2)
186 #if defined(NDEBUG)
187 #define DCHECK_CONSTEXPR(x, out, dummy)
188 #else
189 #define DCHECK_CONSTEXPR(x, out, dummy) CHECK_CONSTEXPR(x, out, dummy)
190 #endif
191 
192 // Temporary class created to evaluate the LHS and RHS, used with MakeEagerEvaluator to infer the
193 // types of LHS and RHS.
194 template <typename LHS, typename RHS>
195 struct EagerEvaluator {
EagerEvaluatorEagerEvaluator196   EagerEvaluator(LHS l, RHS r) : lhs(l), rhs(r) { }
197   LHS lhs;
198   RHS rhs;
199 };
200 
201 // Helper function for CHECK_xx.
202 template <typename LHS, typename RHS>
MakeEagerEvaluator(LHS lhs,RHS rhs)203 static inline EagerEvaluator<LHS, RHS> MakeEagerEvaluator(LHS lhs, RHS rhs) {
204   return EagerEvaluator<LHS, RHS>(lhs, rhs);
205 }
206 
207 // Explicitly instantiate EagerEvalue for pointers so that char*s aren't treated as strings. To
208 // compare strings use CHECK_STREQ and CHECK_STRNE. We rely on signed/unsigned warnings to
209 // protect you against combinations not explicitly listed below.
210 #define EAGER_PTR_EVALUATOR(T1, T2) \
211   template <> struct EagerEvaluator<T1, T2> { \
212     EagerEvaluator(T1 l, T2 r) \
213         : lhs(reinterpret_cast<const void*>(l)), \
214           rhs(reinterpret_cast<const void*>(r)) { } \
215     const void* lhs; \
216     const void* rhs; \
217   }
218 EAGER_PTR_EVALUATOR(const char*, const char*);
219 EAGER_PTR_EVALUATOR(const char*, char*);
220 EAGER_PTR_EVALUATOR(char*, const char*);
221 EAGER_PTR_EVALUATOR(char*, char*);
222 EAGER_PTR_EVALUATOR(const unsigned char*, const unsigned char*);
223 EAGER_PTR_EVALUATOR(const unsigned char*, unsigned char*);
224 EAGER_PTR_EVALUATOR(unsigned char*, const unsigned char*);
225 EAGER_PTR_EVALUATOR(unsigned char*, unsigned char*);
226 EAGER_PTR_EVALUATOR(const signed char*, const signed char*);
227 EAGER_PTR_EVALUATOR(const signed char*, signed char*);
228 EAGER_PTR_EVALUATOR(signed char*, const signed char*);
229 EAGER_PTR_EVALUATOR(signed char*, signed char*);
230 
231 // Data for the log message, not stored in LogMessage to avoid increasing the stack size.
232 class LogMessageData;
233 
234 // A LogMessage is a temporarily scoped object used by LOG and the unlikely part of a CHECK. The
235 // destructor will abort if the severity is FATAL.
236 class LogMessage {
237  public:
238   LogMessage(const char* file, unsigned int line, LogSeverity severity, int error);
239 
240   ~LogMessage();  // TODO: enable LOCKS_EXCLUDED(Locks::logging_lock_).
241 
242   // Returns the stream associated with the message, the LogMessage performs output when it goes
243   // out of scope.
244   std::ostream& stream();
245 
246   // The routine that performs the actual logging.
247   static void LogLine(const char* file, unsigned int line, LogSeverity severity, const char* msg);
248 
249   // A variant of the above for use with little stack.
250   static void LogLineLowStack(const char* file, unsigned int line, LogSeverity severity,
251                               const char* msg);
252 
253  private:
254   const std::unique_ptr<LogMessageData> data_;
255 
256   DISALLOW_COPY_AND_ASSIGN(LogMessage);
257 };
258 
259 // Allows to temporarily change the minimum severity level for logging.
260 class ScopedLogSeverity {
261  public:
262   explicit ScopedLogSeverity(LogSeverity level);
263   ~ScopedLogSeverity();
264 
265  private:
266   LogSeverity old_;
267 };
268 
269 }  // namespace art
270 
271 #endif  // ART_RUNTIME_BASE_LOGGING_H_
272