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 | |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 20 | #include <cerrno> |
| 21 | #include <cstring> |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 22 | #include <iostream> // NOLINT |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 23 | #include <sstream> |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 24 | #include <signal.h> |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 25 | #include "base/macros.h" |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 26 | #include "log_severity.h" |
Sebastien Hertz | 916041f | 2013-08-05 16:17:42 +0200 | [diff] [blame] | 27 | #include "UniquePtr.h" |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 28 | |
| 29 | #define CHECK(x) \ |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 30 | if (UNLIKELY(!(x))) \ |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 31 | ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \ |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 32 | << "Check failed: " #x << " " |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 33 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 34 | #define CHECK_OP(LHS, RHS, OP) \ |
Mathieu Chartier | 9b3c3cd | 2013-08-12 17:41:54 -0700 | [diff] [blame] | 35 | for (auto _values = ::art::MakeEagerEvaluator(LHS, RHS); \ |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 36 | UNLIKELY(!(_values.lhs OP _values.rhs)); /* empty */) \ |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 37 | ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \ |
| 38 | << "Check failed: " << #LHS << " " << #OP << " " << #RHS \ |
| 39 | << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") " |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 40 | |
| 41 | #define CHECK_EQ(x, y) CHECK_OP(x, y, ==) |
| 42 | #define CHECK_NE(x, y) CHECK_OP(x, y, !=) |
| 43 | #define CHECK_LE(x, y) CHECK_OP(x, y, <=) |
| 44 | #define CHECK_LT(x, y) CHECK_OP(x, y, <) |
| 45 | #define CHECK_GE(x, y) CHECK_OP(x, y, >=) |
| 46 | #define CHECK_GT(x, y) CHECK_OP(x, y, >) |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 47 | |
| 48 | #define CHECK_STROP(s1, s2, sense) \ |
Ian Rogers | caab8c4 | 2011-10-12 12:11:18 -0700 | [diff] [blame] | 49 | if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \ |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 50 | LOG(FATAL) << "Check failed: " \ |
| 51 | << "\"" << s1 << "\"" \ |
| 52 | << (sense ? " == " : " != ") \ |
| 53 | << "\"" << s2 << "\"" |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 54 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 55 | #define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true) |
| 56 | #define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false) |
| 57 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 58 | #define CHECK_PTHREAD_CALL(call, args, what) \ |
| 59 | do { \ |
| 60 | int rc = call args; \ |
| 61 | if (rc != 0) { \ |
| 62 | errno = rc; \ |
| 63 | PLOG(FATAL) << # call << " failed for " << what; \ |
| 64 | } \ |
| 65 | } while (false) |
| 66 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 67 | #ifndef NDEBUG |
| 68 | |
| 69 | #define DCHECK(x) CHECK(x) |
| 70 | #define DCHECK_EQ(x, y) CHECK_EQ(x, y) |
| 71 | #define DCHECK_NE(x, y) CHECK_NE(x, y) |
| 72 | #define DCHECK_LE(x, y) CHECK_LE(x, y) |
| 73 | #define DCHECK_LT(x, y) CHECK_LT(x, y) |
| 74 | #define DCHECK_GE(x, y) CHECK_GE(x, y) |
| 75 | #define DCHECK_GT(x, y) CHECK_GT(x, y) |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 76 | #define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2) |
| 77 | #define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2) |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 78 | |
| 79 | #else // NDEBUG |
| 80 | |
| 81 | #define DCHECK(condition) \ |
| 82 | while (false) \ |
| 83 | CHECK(condition) |
| 84 | |
| 85 | #define DCHECK_EQ(val1, val2) \ |
| 86 | while (false) \ |
| 87 | CHECK_EQ(val1, val2) |
| 88 | |
| 89 | #define DCHECK_NE(val1, val2) \ |
| 90 | while (false) \ |
| 91 | CHECK_NE(val1, val2) |
| 92 | |
| 93 | #define DCHECK_LE(val1, val2) \ |
| 94 | while (false) \ |
| 95 | CHECK_LE(val1, val2) |
| 96 | |
| 97 | #define DCHECK_LT(val1, val2) \ |
| 98 | while (false) \ |
| 99 | CHECK_LT(val1, val2) |
| 100 | |
| 101 | #define DCHECK_GE(val1, val2) \ |
| 102 | while (false) \ |
| 103 | CHECK_GE(val1, val2) |
| 104 | |
| 105 | #define DCHECK_GT(val1, val2) \ |
| 106 | while (false) \ |
| 107 | CHECK_GT(val1, val2) |
| 108 | |
| 109 | #define DCHECK_STREQ(str1, str2) \ |
| 110 | while (false) \ |
| 111 | CHECK_STREQ(str1, str2) |
| 112 | |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 113 | #define DCHECK_STRNE(str1, str2) \ |
| 114 | while (false) \ |
| 115 | CHECK_STRNE(str1, str2) |
| 116 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 117 | #endif |
| 118 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 119 | #define LOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, -1).stream() |
| 120 | #define PLOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, errno).stream() |
Elliott Hughes | eb4f614 | 2011-07-15 17:43:51 -0700 | [diff] [blame] | 121 | |
Carl Shapiro | 6c21dc1 | 2011-06-20 15:20:52 -0700 | [diff] [blame] | 122 | #define LG LOG(INFO) |
| 123 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 124 | #define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented " |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 125 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 126 | #define VLOG_IS_ON(module) UNLIKELY(::art::gLogVerbosity.module) |
| 127 | #define VLOG(module) if (VLOG_IS_ON(module)) ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream() |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 128 | #define VLOG_STREAM(module) ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream() |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 129 | |
Elliott Hughes | 3ea7e99 | 2011-10-11 18:48:16 -0700 | [diff] [blame] | 130 | // |
| 131 | // Implementation details beyond this point. |
| 132 | // |
| 133 | |
| 134 | namespace art { |
| 135 | |
| 136 | template <typename LHS, typename RHS> |
| 137 | struct EagerEvaluator { |
| 138 | EagerEvaluator(LHS lhs, RHS rhs) : lhs(lhs), rhs(rhs) { } |
| 139 | LHS lhs; |
| 140 | RHS rhs; |
| 141 | }; |
| 142 | |
Elliott Hughes | aa6a588 | 2012-01-13 19:39:16 -0800 | [diff] [blame] | 143 | // We want char*s to be treated as pointers, not strings. If you want them treated like strings, |
| 144 | // you'd need to use CHECK_STREQ and CHECK_STRNE anyway to compare the characters rather than their |
| 145 | // addresses. We could express this more succinctly with std::remove_const, but this is quick and |
| 146 | // easy to understand, and works before we have C++0x. We rely on signed/unsigned warnings to |
| 147 | // protect you against combinations not explicitly listed below. |
| 148 | #define EAGER_PTR_EVALUATOR(T1, T2) \ |
| 149 | template <> struct EagerEvaluator<T1, T2> { \ |
| 150 | EagerEvaluator(T1 lhs, T2 rhs) \ |
| 151 | : lhs(reinterpret_cast<const void*>(lhs)), \ |
| 152 | rhs(reinterpret_cast<const void*>(rhs)) { } \ |
| 153 | const void* lhs; \ |
| 154 | const void* rhs; \ |
| 155 | } |
| 156 | EAGER_PTR_EVALUATOR(const char*, const char*); |
| 157 | EAGER_PTR_EVALUATOR(const char*, char*); |
| 158 | EAGER_PTR_EVALUATOR(char*, const char*); |
| 159 | EAGER_PTR_EVALUATOR(char*, char*); |
| 160 | EAGER_PTR_EVALUATOR(const unsigned char*, const unsigned char*); |
| 161 | EAGER_PTR_EVALUATOR(const unsigned char*, unsigned char*); |
| 162 | EAGER_PTR_EVALUATOR(unsigned char*, const unsigned char*); |
| 163 | EAGER_PTR_EVALUATOR(unsigned char*, unsigned char*); |
| 164 | EAGER_PTR_EVALUATOR(const signed char*, const signed char*); |
| 165 | EAGER_PTR_EVALUATOR(const signed char*, signed char*); |
| 166 | EAGER_PTR_EVALUATOR(signed char*, const signed char*); |
| 167 | EAGER_PTR_EVALUATOR(signed char*, signed char*); |
| 168 | |
Mathieu Chartier | 9b3c3cd | 2013-08-12 17:41:54 -0700 | [diff] [blame] | 169 | template <typename LHS, typename RHS> |
| 170 | EagerEvaluator<LHS, RHS> MakeEagerEvaluator(LHS lhs, RHS rhs) { |
| 171 | return EagerEvaluator<LHS, RHS>(lhs, rhs); |
| 172 | } |
| 173 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 174 | // This indirection greatly reduces the stack impact of having |
| 175 | // lots of checks/logging in a function. |
| 176 | struct LogMessageData { |
| 177 | public: |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 178 | LogMessageData(const char* file, int line, LogSeverity severity, int error); |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 179 | std::ostringstream buffer; |
Ian Rogers | c4ee12e | 2013-05-16 11:19:53 -0700 | [diff] [blame] | 180 | const char* const file; |
| 181 | const int line_number; |
| 182 | const LogSeverity severity; |
| 183 | const int error; |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 184 | |
| 185 | private: |
| 186 | DISALLOW_COPY_AND_ASSIGN(LogMessageData); |
| 187 | }; |
| 188 | |
Elliott Hughes | 3ea7e99 | 2011-10-11 18:48:16 -0700 | [diff] [blame] | 189 | class LogMessage { |
| 190 | public: |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 191 | LogMessage(const char* file, int line, LogSeverity severity, int error) |
| 192 | : data_(new LogMessageData(file, line, severity, error)) { |
| 193 | } |
Sebastien Hertz | 74c0704 | 2013-05-17 14:04:12 +0200 | [diff] [blame] | 194 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 195 | ~LogMessage(); // TODO: enable LOCKS_EXCLUDED(Locks::logging_lock_). |
Sebastien Hertz | 74c0704 | 2013-05-17 14:04:12 +0200 | [diff] [blame] | 196 | |
| 197 | std::ostream& stream() { |
| 198 | return data_->buffer; |
| 199 | } |
Elliott Hughes | 3ea7e99 | 2011-10-11 18:48:16 -0700 | [diff] [blame] | 200 | |
| 201 | private: |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 202 | static void LogLine(const LogMessageData& data, const char*); |
Elliott Hughes | 3ea7e99 | 2011-10-11 18:48:16 -0700 | [diff] [blame] | 203 | |
Sebastien Hertz | 916041f | 2013-08-05 16:17:42 +0200 | [diff] [blame] | 204 | const UniquePtr<LogMessageData> data_; |
Elliott Hughes | 3ea7e99 | 2011-10-11 18:48:16 -0700 | [diff] [blame] | 205 | |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 206 | friend void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context); |
Ian Rogers | c4ee12e | 2013-05-16 11:19:53 -0700 | [diff] [blame] | 207 | friend class Mutex; |
Elliott Hughes | 3ea7e99 | 2011-10-11 18:48:16 -0700 | [diff] [blame] | 208 | DISALLOW_COPY_AND_ASSIGN(LogMessage); |
| 209 | }; |
| 210 | |
Elliott Hughes | e091855 | 2011-10-28 17:18:29 -0700 | [diff] [blame] | 211 | // A convenience to allow any class with a "Dump(std::ostream& os)" member function |
| 212 | // but without an operator<< to be used as if it had an operator<<. Use like this: |
| 213 | // |
| 214 | // os << Dumpable<MyType>(my_type_instance); |
| 215 | // |
| 216 | template<typename T> |
| 217 | class Dumpable { |
| 218 | public: |
| 219 | explicit Dumpable(T& value) : value_(value) { |
| 220 | } |
| 221 | |
| 222 | void Dump(std::ostream& os) const { |
| 223 | value_.Dump(os); |
| 224 | } |
| 225 | |
| 226 | private: |
| 227 | T& value_; |
Shih-wei Liao | 24782c6 | 2012-01-08 12:46:11 -0800 | [diff] [blame] | 228 | |
Elliott Hughes | e091855 | 2011-10-28 17:18:29 -0700 | [diff] [blame] | 229 | DISALLOW_COPY_AND_ASSIGN(Dumpable); |
| 230 | }; |
| 231 | |
| 232 | template<typename T> |
| 233 | std::ostream& operator<<(std::ostream& os, const Dumpable<T>& rhs) { |
| 234 | rhs.Dump(os); |
| 235 | return os; |
| 236 | } |
| 237 | |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 238 | // Helps you use operator<< in a const char*-like context such as our various 'F' methods with |
| 239 | // format strings. |
| 240 | template<typename T> |
| 241 | class ToStr { |
| 242 | public: |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 243 | explicit ToStr(const T& value) { |
Elliott Hughes | 3f6635a | 2012-06-19 13:37:49 -0700 | [diff] [blame] | 244 | std::ostringstream os; |
| 245 | os << value; |
| 246 | s_ = os.str(); |
| 247 | } |
| 248 | |
| 249 | const char* c_str() const { |
| 250 | return s_.c_str(); |
| 251 | } |
| 252 | |
| 253 | const std::string& str() const { |
| 254 | return s_; |
| 255 | } |
| 256 | |
| 257 | private: |
| 258 | std::string s_; |
| 259 | DISALLOW_COPY_AND_ASSIGN(ToStr); |
| 260 | }; |
| 261 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 262 | // The members of this struct are the valid arguments to VLOG and VLOG_IS_ON in code, |
| 263 | // and the "-verbose:" command line argument. |
| 264 | struct LogVerbosity { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 265 | bool class_linker; // Enabled with "-verbose:class". |
Anwar Ghuloum | 75a43f1 | 2013-08-13 17:22:14 -0700 | [diff] [blame] | 266 | bool verifier; |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 267 | bool compiler; |
| 268 | bool heap; |
| 269 | bool gc; |
| 270 | bool jdwp; |
| 271 | bool jni; |
| 272 | bool monitor; |
| 273 | bool startup; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 274 | bool third_party_jni; // Enabled with "-verbose:third-party-jni". |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 275 | bool threads; |
| 276 | }; |
| 277 | |
| 278 | extern LogVerbosity gLogVerbosity; |
Brian Carlstrom | 81b8871 | 2012-11-05 19:21:30 -0800 | [diff] [blame] | 279 | |
| 280 | // Used on fatal exit. Prevents recursive aborts. Allows us to disable |
| 281 | // some error checking to ensure fatal shutdown makes forward progress. |
Ian Rogers | f08e473 | 2013-04-09 09:45:49 -0700 | [diff] [blame] | 282 | extern unsigned int gAborting; |
Brian Carlstrom | 81b8871 | 2012-11-05 19:21:30 -0800 | [diff] [blame] | 283 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 284 | extern void InitLogging(char* argv[]); |
| 285 | |
| 286 | extern const char* GetCmdLine(); |
| 287 | extern const char* ProgramInvocationName(); |
| 288 | extern const char* ProgramInvocationShortName(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 289 | |
Elliott Hughes | 3ea7e99 | 2011-10-11 18:48:16 -0700 | [diff] [blame] | 290 | } // namespace art |
| 291 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 292 | #endif // ART_RUNTIME_BASE_LOGGING_H_ |