blob: f250ce01b3559cf188c2f85b3dbb173fdd03cde1 [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
Elliott Hughes07ed66b2012-12-12 18:34:25 -080017#ifndef ART_SRC_BASE_LOGGING_H_
18#define ART_SRC_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>
Elliott Hughes76160052012-12-12 16:31:20 -080025#include "base/macros.h"
Elliott Hugheseb4f6142011-07-15 17:43:51 -070026#include "log_severity.h"
Carl Shapiro6c21dc12011-06-20 15:20:52 -070027
28#define CHECK(x) \
Ian Rogerscaab8c42011-10-12 12:11:18 -070029 if (UNLIKELY(!(x))) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070030 ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
Elliott Hughes710a0cb2011-08-16 14:32:37 -070031 << "Check failed: " #x << " "
Elliott Hugheseb4f6142011-07-15 17:43:51 -070032
Elliott Hughes1f359b02011-07-17 14:27:17 -070033#define CHECK_OP(LHS, RHS, OP) \
Ian Rogerscaab8c42011-10-12 12:11:18 -070034 for (::art::EagerEvaluator<typeof(LHS), typeof(RHS)> _values(LHS, RHS); \
Elliott Hughes362f9bc2011-10-17 18:56:41 -070035 UNLIKELY(!(_values.lhs OP _values.rhs)); /* empty */) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070036 ::art::LogMessage(__FILE__, __LINE__, FATAL, -1).stream() \
37 << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
38 << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
Elliott Hughes1f359b02011-07-17 14:27:17 -070039
40#define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
41#define CHECK_NE(x, y) CHECK_OP(x, y, !=)
42#define CHECK_LE(x, y) CHECK_OP(x, y, <=)
43#define CHECK_LT(x, y) CHECK_OP(x, y, <)
44#define CHECK_GE(x, y) CHECK_OP(x, y, >=)
45#define CHECK_GT(x, y) CHECK_OP(x, y, >)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070046
47#define CHECK_STROP(s1, s2, sense) \
Ian Rogerscaab8c42011-10-12 12:11:18 -070048 if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \
Elliott Hughesf5a7a472011-10-07 14:31:02 -070049 LOG(FATAL) << "Check failed: " \
50 << "\"" << s1 << "\"" \
51 << (sense ? " == " : " != ") \
52 << "\"" << s2 << "\""
Carl Shapiro6c21dc12011-06-20 15:20:52 -070053
Elliott Hughes1f359b02011-07-17 14:27:17 -070054#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
55#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
56
Elliott Hughes8d768a92011-09-14 16:35:25 -070057#define CHECK_PTHREAD_CALL(call, args, what) \
58 do { \
59 int rc = call args; \
60 if (rc != 0) { \
61 errno = rc; \
62 PLOG(FATAL) << # call << " failed for " << what; \
63 } \
64 } while (false)
65
Carl Shapiro6c21dc12011-06-20 15:20:52 -070066#ifndef NDEBUG
67
68#define DCHECK(x) CHECK(x)
69#define DCHECK_EQ(x, y) CHECK_EQ(x, y)
70#define DCHECK_NE(x, y) CHECK_NE(x, y)
71#define DCHECK_LE(x, y) CHECK_LE(x, y)
72#define DCHECK_LT(x, y) CHECK_LT(x, y)
73#define DCHECK_GE(x, y) CHECK_GE(x, y)
74#define DCHECK_GT(x, y) CHECK_GT(x, y)
Elliott Hugheseb4f6142011-07-15 17:43:51 -070075#define DCHECK_STREQ(s1, s2) CHECK_STREQ(s1, s2)
76#define DCHECK_STRNE(s1, s2) CHECK_STRNE(s1, s2)
Carl Shapiro6c21dc12011-06-20 15:20:52 -070077
78#else // NDEBUG
79
80#define DCHECK(condition) \
81 while (false) \
82 CHECK(condition)
83
84#define DCHECK_EQ(val1, val2) \
85 while (false) \
86 CHECK_EQ(val1, val2)
87
88#define DCHECK_NE(val1, val2) \
89 while (false) \
90 CHECK_NE(val1, val2)
91
92#define DCHECK_LE(val1, val2) \
93 while (false) \
94 CHECK_LE(val1, val2)
95
96#define DCHECK_LT(val1, val2) \
97 while (false) \
98 CHECK_LT(val1, val2)
99
100#define DCHECK_GE(val1, val2) \
101 while (false) \
102 CHECK_GE(val1, val2)
103
104#define DCHECK_GT(val1, val2) \
105 while (false) \
106 CHECK_GT(val1, val2)
107
108#define DCHECK_STREQ(str1, str2) \
109 while (false) \
110 CHECK_STREQ(str1, str2)
111
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700112#define DCHECK_STRNE(str1, str2) \
113 while (false) \
114 CHECK_STRNE(str1, str2)
115
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700116#endif
117
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700118#define LOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, -1).stream()
119#define PLOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, errno).stream()
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700120
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700121#define LG LOG(INFO)
122
Elliott Hughes8d768a92011-09-14 16:35:25 -0700123#define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
Elliott Hughes53b61312011-08-12 18:28:20 -0700124
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800125#define VLOG_IS_ON(module) UNLIKELY(::art::gLogVerbosity.module)
126#define VLOG(module) if (VLOG_IS_ON(module)) ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
127
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700128//
129// Implementation details beyond this point.
130//
131
132namespace art {
133
134template <typename LHS, typename RHS>
135struct EagerEvaluator {
136 EagerEvaluator(LHS lhs, RHS rhs) : lhs(lhs), rhs(rhs) { }
137 LHS lhs;
138 RHS rhs;
139};
140
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800141// We want char*s to be treated as pointers, not strings. If you want them treated like strings,
142// you'd need to use CHECK_STREQ and CHECK_STRNE anyway to compare the characters rather than their
143// addresses. We could express this more succinctly with std::remove_const, but this is quick and
144// easy to understand, and works before we have C++0x. We rely on signed/unsigned warnings to
145// protect you against combinations not explicitly listed below.
146#define EAGER_PTR_EVALUATOR(T1, T2) \
147 template <> struct EagerEvaluator<T1, T2> { \
148 EagerEvaluator(T1 lhs, T2 rhs) \
149 : lhs(reinterpret_cast<const void*>(lhs)), \
150 rhs(reinterpret_cast<const void*>(rhs)) { } \
151 const void* lhs; \
152 const void* rhs; \
153 }
154EAGER_PTR_EVALUATOR(const char*, const char*);
155EAGER_PTR_EVALUATOR(const char*, char*);
156EAGER_PTR_EVALUATOR(char*, const char*);
157EAGER_PTR_EVALUATOR(char*, char*);
158EAGER_PTR_EVALUATOR(const unsigned char*, const unsigned char*);
159EAGER_PTR_EVALUATOR(const unsigned char*, unsigned char*);
160EAGER_PTR_EVALUATOR(unsigned char*, const unsigned char*);
161EAGER_PTR_EVALUATOR(unsigned char*, unsigned char*);
162EAGER_PTR_EVALUATOR(const signed char*, const signed char*);
163EAGER_PTR_EVALUATOR(const signed char*, signed char*);
164EAGER_PTR_EVALUATOR(signed char*, const signed char*);
165EAGER_PTR_EVALUATOR(signed char*, signed char*);
166
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700167// This indirection greatly reduces the stack impact of having
168// lots of checks/logging in a function.
169struct LogMessageData {
170 public:
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800171 LogMessageData(const char* file, int line, LogSeverity severity, int error);
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700172 std::ostringstream buffer;
Ian Rogersc4ee12e2013-05-16 11:19:53 -0700173 const char* const file;
174 const int line_number;
175 const LogSeverity severity;
176 const int error;
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700177
178 private:
179 DISALLOW_COPY_AND_ASSIGN(LogMessageData);
180};
181
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700182class LogMessage {
183 public:
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800184 LogMessage(const char* file, int line, LogSeverity severity, int error)
185 : data_(new LogMessageData(file, line, severity, error)) {
186 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700187 ~LogMessage() LOCKS_EXCLUDED(Locks::logging_lock_);
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700188 std::ostream& stream();
189
190 private:
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800191 static void LogLine(const LogMessageData& data, const char*);
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700192
Ian Rogersc4ee12e2013-05-16 11:19:53 -0700193 LogMessageData* const data_;
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700194
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800195 friend void HandleUnexpectedSignal(int signal_number, siginfo_t* info, void* raw_context);
Ian Rogersc4ee12e2013-05-16 11:19:53 -0700196 friend class Mutex;
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700197 DISALLOW_COPY_AND_ASSIGN(LogMessage);
198};
199
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700200// Prints a hex dump in this format:
201//
202// 01234560: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef
203// 01234568: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef
204class HexDump {
205 public:
206 HexDump(const void* address, size_t byte_count, bool show_actual_addresses = false);
207 void Dump(std::ostream& os) const;
208
209 private:
210 const void* address_;
211 size_t byte_count_;
212 bool show_actual_addresses_;
Elliott Hughes878820f2012-03-29 20:00:04 -0700213
214 // TODO: Remove the #if when Mac OS build server no longer uses GCC 4.2.*.
215#if GCC_VERSION >= 40300
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700216 DISALLOW_COPY_AND_ASSIGN(HexDump);
Elliott Hughes878820f2012-03-29 20:00:04 -0700217#endif
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700218};
219std::ostream& operator<<(std::ostream& os, const HexDump& rhs);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700220
Elliott Hughese0918552011-10-28 17:18:29 -0700221// A convenience to allow any class with a "Dump(std::ostream& os)" member function
222// but without an operator<< to be used as if it had an operator<<. Use like this:
223//
224// os << Dumpable<MyType>(my_type_instance);
225//
226template<typename T>
227class Dumpable {
228 public:
229 explicit Dumpable(T& value) : value_(value) {
230 }
231
232 void Dump(std::ostream& os) const {
233 value_.Dump(os);
234 }
235
236 private:
237 T& value_;
Shih-wei Liao24782c62012-01-08 12:46:11 -0800238
239// TODO: Remove the #if when Mac OS build server no longer uses GCC 4.2.*.
240#if GCC_VERSION >= 40300
Elliott Hughese0918552011-10-28 17:18:29 -0700241 DISALLOW_COPY_AND_ASSIGN(Dumpable);
Shih-wei Liao24782c62012-01-08 12:46:11 -0800242#endif
Elliott Hughese0918552011-10-28 17:18:29 -0700243};
244
245template<typename T>
246std::ostream& operator<<(std::ostream& os, const Dumpable<T>& rhs) {
247 rhs.Dump(os);
248 return os;
249}
250
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251template<typename T>
252class MutatorLockedDumpable {
253 public:
254 explicit MutatorLockedDumpable(T& value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700255 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : value_(value) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256 }
257
Ian Rogersb726dcb2012-09-05 08:57:23 -0700258 void Dump(std::ostream& os) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700259 value_.Dump(os);
260 }
261
262 private:
263 T& value_;
264
265// TODO: Remove the #if when Mac OS build server no longer uses GCC 4.2.*.
266#if GCC_VERSION >= 40300
267 DISALLOW_COPY_AND_ASSIGN(MutatorLockedDumpable);
268#endif
269};
270
271template<typename T>
272std::ostream& operator<<(std::ostream& os, const MutatorLockedDumpable<T>& rhs)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700273// TODO: should be SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) however annotalysis
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700274// currently fails for this.
275 NO_THREAD_SAFETY_ANALYSIS {
276 rhs.Dump(os);
277 return os;
278}
279
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700280// Helps you use operator<< in a const char*-like context such as our various 'F' methods with
281// format strings.
282template<typename T>
283class ToStr {
284 public:
Elliott Hughes74847412012-06-20 18:10:21 -0700285 explicit ToStr(const T& value) {
Elliott Hughes3f6635a2012-06-19 13:37:49 -0700286 std::ostringstream os;
287 os << value;
288 s_ = os.str();
289 }
290
291 const char* c_str() const {
292 return s_.c_str();
293 }
294
295 const std::string& str() const {
296 return s_;
297 }
298
299 private:
300 std::string s_;
301 DISALLOW_COPY_AND_ASSIGN(ToStr);
302};
303
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800304// The members of this struct are the valid arguments to VLOG and VLOG_IS_ON in code,
305// and the "-verbose:" command line argument.
306struct LogVerbosity {
307 bool class_linker; // Enabled with "-verbose:class".
308 bool compiler;
309 bool heap;
310 bool gc;
311 bool jdwp;
312 bool jni;
313 bool monitor;
314 bool startup;
315 bool third_party_jni; // Enabled with "-verbose:third-party-jni".
316 bool threads;
317};
318
319extern LogVerbosity gLogVerbosity;
Brian Carlstrom81b88712012-11-05 19:21:30 -0800320
321// Used on fatal exit. Prevents recursive aborts. Allows us to disable
322// some error checking to ensure fatal shutdown makes forward progress.
Ian Rogersf08e4732013-04-09 09:45:49 -0700323extern unsigned int gAborting;
Brian Carlstrom81b88712012-11-05 19:21:30 -0800324
Elliott Hughes0d39c122012-06-06 16:41:17 -0700325extern void InitLogging(char* argv[]);
326
327extern const char* GetCmdLine();
328extern const char* ProgramInvocationName();
329extern const char* ProgramInvocationShortName();
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800330
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700331} // namespace art
332
Elliott Hughes07ed66b2012-12-12 18:34:25 -0800333#endif // ART_SRC_BASE_LOGGING_H_