blob: c4461fab07f67281766d3758c8a062306d4c9898 [file] [log] [blame]
Elliott Hughes42ee1422011-09-06 12:33:32 -07001/*
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 Shapiro6c21dc12011-06-20 15:20:52 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_LOGGING_H_
18#define ART_RUNTIME_BASE_LOGGING_H_
Carl Shapiro6c21dc12011-06-20 15:20:52 -070019
Elliott Hugheseb4f6142011-07-15 17:43:51 -070020#include <cerrno>
21#include <cstring>
Carl Shapiro6c21dc12011-06-20 15:20:52 -070022#include <iostream> // NOLINT
Elliott Hugheseb4f6142011-07-15 17:43:51 -070023#include <sstream>
Brian Carlstromaf1b8922012-11-27 15:19:57 -080024#include <signal.h>
Mingyao Yang42d65c52014-04-18 16:49:39 -070025#include <vector>
Elliott Hughes76160052012-12-12 16:31:20 -080026#include "base/macros.h"
Elliott Hugheseb4f6142011-07-15 17:43:51 -070027#include "log_severity.h"
Sebastien Hertz916041f2013-08-05 16:17:42 +020028#include "UniquePtr.h"
Carl Shapiro6c21dc12011-06-20 15:20:52 -070029
30#define CHECK(x) \
Ian Rogerscaab8c42011-10-12 12:11:18 -070031 if (UNLIKELY(!(x))) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070032 ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
Elliott Hughes710a0cb2011-08-16 14:32:37 -070033 << "Check failed: " #x << " "
Elliott Hugheseb4f6142011-07-15 17:43:51 -070034
Elliott Hughes1f359b02011-07-17 14:27:17 -070035#define CHECK_OP(LHS, RHS, OP) \
Mathieu Chartier9b3c3cd2013-08-12 17:41:54 -070036 for (auto _values = ::art::MakeEagerEvaluator(LHS, RHS); \
Elliott Hughes362f9bc2011-10-17 18:56:41 -070037 UNLIKELY(!(_values.lhs OP _values.rhs)); /* empty */) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070038 ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
39 << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
40 << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
Elliott Hughes1f359b02011-07-17 14:27:17 -070041
42#define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
43#define CHECK_NE(x, y) CHECK_OP(x, y, !=)
44#define CHECK_LE(x, y) CHECK_OP(x, y, <=)
45#define CHECK_LT(x, y) CHECK_OP(x, y, <)
46#define CHECK_GE(x, y) CHECK_OP(x, y, >=)
47#define CHECK_GT(x, y) CHECK_OP(x, y, >)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070048
49#define CHECK_STROP(s1, s2, sense) \
Ian Rogerscaab8c42011-10-12 12:11:18 -070050 if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070051 LOG(FATAL) << "Check failed: " \
52 << "\"" << s1 << "\"" \
53 << (sense ? " == " : " != ") \
54 << "\"" << s2 << "\""
Carl Shapiro6c21dc12011-06-20 15:20:52 -070055
Elliott Hughes1f359b02011-07-17 14:27:17 -070056#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
57#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
58
Elliott Hughes8d768a92011-09-14 16:35:25 -070059#define CHECK_PTHREAD_CALL(call, args, what) \
60 do { \
61 int rc = call args; \
62 if (rc != 0) { \
63 errno = rc; \
64 PLOG(FATAL) << # call << " failed for " << what; \
65 } \
66 } while (false)
67
Carl Shapiro6c21dc12011-06-20 15:20:52 -070068#ifndef NDEBUG
69
70#define DCHECK(x) CHECK(x)
71#define DCHECK_EQ(x, y) CHECK_EQ(x, y)
72#define DCHECK_NE(x, y) CHECK_NE(x, y)
73#define DCHECK_LE(x, y) CHECK_LE(x, y)
74#define DCHECK_LT(x, y) CHECK_LT(x, y)
75#define DCHECK_GE(x, y) CHECK_GE(x, y)
76#define DCHECK_GT(x, y) CHECK_GT(x, y)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070077#define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2)
78#define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070079
80#else // NDEBUG
81
82#define DCHECK(condition) \
83 while (false) \
84 CHECK(condition)
85
86#define DCHECK_EQ(val1, val2) \
87 while (false) \
88 CHECK_EQ(val1, val2)
89
90#define DCHECK_NE(val1, val2) \
91 while (false) \
92 CHECK_NE(val1, val2)
93
94#define DCHECK_LE(val1, val2) \
95 while (false) \
96 CHECK_LE(val1, val2)
97
98#define DCHECK_LT(val1, val2) \
99 while (false) \
100 CHECK_LT(val1, val2)
101
102#define DCHECK_GE(val1, val2) \
103 while (false) \
104 CHECK_GE(val1, val2)
105
106#define DCHECK_GT(val1, val2) \
107 while (false) \
108 CHECK_GT(val1, val2)
109
110#define DCHECK_STREQ(str1, str2) \
111 while (false) \
112 CHECK_STREQ(str1, str2)
113
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700114#define DCHECK_STRNE(str1, str2) \
115 while (false) \
116 CHECK_STRNE(str1, str2)
117
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700118#endif
119
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700120#define LOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, -1).stream()
121#define PLOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, errno).stream()
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700122
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700123#define LG LOG(INFO)
124
Elliott Hughes8d768a92011-09-14 16:35:25 -0700125#define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
Elliott Hughes53b61312011-08-12 18:28:20 -0700126
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800127#define VLOG_IS_ON(module) UNLIKELY(::art::gLogVerbosity.module)
128#define VLOG(module) if (VLOG_IS_ON(module)) ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700129#define VLOG_STREAM(module) ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800130
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700131//
132// Implementation details beyond this point.
133//
134
135namespace art {
136
137template <typename LHS, typename RHS>
138struct EagerEvaluator {
139 EagerEvaluator(LHS lhs, RHS rhs) : lhs(lhs), rhs(rhs) { }
140 LHS lhs;
141 RHS rhs;
142};
143
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800144// We want char*s to be treated as pointers, not strings. If you want them treated like strings,
145// you'd need to use CHECK_STREQ and CHECK_STRNE anyway to compare the characters rather than their
146// addresses. We could express this more succinctly with std::remove_const, but this is quick and
147// easy to understand, and works before we have C++0x. We rely on signed/unsigned warnings to
148// protect you against combinations not explicitly listed below.
149#define EAGER_PTR_EVALUATOR(T1, T2) \
150 template <> struct EagerEvaluator<T1, T2> { \
151 EagerEvaluator(T1 lhs, T2 rhs) \
152 : lhs(reinterpret_cast<const void*>(lhs)), \
153 rhs(reinterpret_cast<const void*>(rhs)) { } \
154 const void* lhs; \
155 const void* rhs; \
156 }
157EAGER_PTR_EVALUATOR(const char*, const char*);
158EAGER_PTR_EVALUATOR(const char*, char*);
159EAGER_PTR_EVALUATOR(char*, const char*);
160EAGER_PTR_EVALUATOR(char*, char*);
161EAGER_PTR_EVALUATOR(const unsigned char*, const unsigned char*);
162EAGER_PTR_EVALUATOR(const unsigned char*, unsigned char*);
163EAGER_PTR_EVALUATOR(unsigned char*, const unsigned char*);
164EAGER_PTR_EVALUATOR(unsigned char*, unsigned char*);
165EAGER_PTR_EVALUATOR(const signed char*, const signed char*);
166EAGER_PTR_EVALUATOR(const signed char*, signed char*);
167EAGER_PTR_EVALUATOR(signed char*, const signed char*);
168EAGER_PTR_EVALUATOR(signed char*, signed char*);
169
Mathieu Chartier9b3c3cd2013-08-12 17:41:54 -0700170template <typename LHS, typename RHS>
171EagerEvaluator<LHS, RHS> MakeEagerEvaluator(LHS lhs, RHS rhs) {
172 return EagerEvaluator<LHS, RHS>(lhs, rhs);
173}
174
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700175// This indirection greatly reduces the stack impact of having
176// lots of checks/logging in a function.
177struct LogMessageData {
178 public:
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800179 LogMessageData(const char* file, int line, LogSeverity severity, int error);
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700180 std::ostringstream buffer;
Ian Rogersc4ee12e2013-05-16 11:19:53 -0700181 const char* const file;
182 const int line_number;
183 const LogSeverity severity;
184 const int error;
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700185
186 private:
187 DISALLOW_COPY_AND_ASSIGN(LogMessageData);
188};
189
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700190class LogMessage {
191 public:
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800192 LogMessage(const char* file, int line, LogSeverity severity, int error)
193 : data_(new LogMessageData(file, line, severity, error)) {
194 }
Sebastien Hertz74c07042013-05-17 14:04:12 +0200195
Ian Rogers719d1a32014-03-06 12:13:39 -0800196 ~LogMessage(); // TODO: enable LOCKS_EXCLUDED(Locks::logging_lock_).
Sebastien Hertz74c07042013-05-17 14:04:12 +0200197
198 std::ostream& stream() {
199 return data_->buffer;
200 }
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700201
202 private:
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800203 static void LogLine(const LogMessageData& data, const char*);
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700204
Sebastien Hertz916041f2013-08-05 16:17:42 +0200205 const UniquePtr<LogMessageData> data_;
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700206
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800207 friend void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context);
Ian Rogersc4ee12e2013-05-16 11:19:53 -0700208 friend class Mutex;
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700209 DISALLOW_COPY_AND_ASSIGN(LogMessage);
210};
211
Elliott Hughese0918552011-10-28 17:18:29 -0700212// A convenience to allow any class with a "Dump(std::ostream& os)" member function
213// but without an operator<< to be used as if it had an operator<<. Use like this:
214//
215// os << Dumpable<MyType>(my_type_instance);
216//
217template<typename T>
218class Dumpable {
219 public:
220 explicit Dumpable(T& value) : value_(value) {
221 }
222
223 void Dump(std::ostream& os) const {
224 value_.Dump(os);
225 }
226
227 private:
228 T& value_;
Shih-wei Liao24782c62012-01-08 12:46:11 -0800229
Elliott Hughese0918552011-10-28 17:18:29 -0700230 DISALLOW_COPY_AND_ASSIGN(Dumpable);
231};
232
233template<typename T>
234std::ostream& operator<<(std::ostream& os, const Dumpable<T>& rhs) {
235 rhs.Dump(os);
236 return os;
237}
238
Mathieu Chartierafe49982014-03-27 10:55:04 -0700239template<typename T>
240class ConstDumpable {
241 public:
242 explicit ConstDumpable(const T& value) : value_(value) {
243 }
244
245 void Dump(std::ostream& os) const {
246 value_.Dump(os);
247 }
248
249 private:
250 const T& value_;
251
252 DISALLOW_COPY_AND_ASSIGN(ConstDumpable);
253};
254
255template<typename T>
256std::ostream& operator<<(std::ostream& os, const ConstDumpable<T>& rhs) {
257 rhs.Dump(os);
258 return os;
259}
260
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700261// Helps you use operator<< in a const char*-like context such as our various 'F' methods with
262// format strings.
263template<typename T>
264class ToStr {
265 public:
Elliott Hughes74847412012-06-20 18:10:21 -0700266 explicit ToStr(const T& value) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700267 std::ostringstream os;
268 os << value;
269 s_ = os.str();
270 }
271
272 const char* c_str() const {
273 return s_.c_str();
274 }
275
276 const std::string& str() const {
277 return s_;
278 }
279
280 private:
281 std::string s_;
282 DISALLOW_COPY_AND_ASSIGN(ToStr);
283};
284
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800285// The members of this struct are the valid arguments to VLOG and VLOG_IS_ON in code,
286// and the "-verbose:" command line argument.
287struct LogVerbosity {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700288 bool class_linker; // Enabled with "-verbose:class".
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700289 bool verifier;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800290 bool compiler;
291 bool heap;
292 bool gc;
293 bool jdwp;
294 bool jni;
295 bool monitor;
296 bool startup;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700297 bool third_party_jni; // Enabled with "-verbose:third-party-jni".
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800298 bool threads;
Dave Allison5cd33752014-04-15 15:57:58 -0700299 bool signals;
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800300};
301
302extern LogVerbosity gLogVerbosity;
Brian Carlstrom81b88712012-11-05 19:21:30 -0800303
Mingyao Yang42d65c52014-04-18 16:49:39 -0700304extern std::vector<std::string> gVerboseMethods;
305
Brian Carlstrom81b88712012-11-05 19:21:30 -0800306// Used on fatal exit. Prevents recursive aborts. Allows us to disable
307// some error checking to ensure fatal shutdown makes forward progress.
Ian Rogersf08e4732013-04-09 09:45:49 -0700308extern unsigned int gAborting;
Brian Carlstrom81b88712012-11-05 19:21:30 -0800309
Elliott Hughes0d39c122012-06-06 16:41:17 -0700310extern void InitLogging(char* argv[]);
311
312extern const char* GetCmdLine();
313extern const char* ProgramInvocationName();
314extern const char* ProgramInvocationShortName();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800315
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700316} // namespace art
317
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700318#endif // ART_RUNTIME_BASE_LOGGING_H_