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