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