blob: a9cc99b0853a6d9a9535be6038a9cb54c3e6c989 [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
Ian Rogers700a4022014-05-19 16:49:03 -070020#include <memory>
Ian Rogerscf7f1912014-10-22 22:06:39 -070021#include <ostream>
Ian Rogers700a4022014-05-19 16:49:03 -070022
Elliott Hughes76160052012-12-12 16:31:20 -080023#include "base/macros.h"
Carl Shapiro6c21dc12011-06-20 15:20:52 -070024
Ian Rogersc7dd2952014-10-21 23:31:19 -070025namespace art {
26
27enum LogSeverity {
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 compiler;
42 bool gc;
43 bool heap;
44 bool jdwp;
45 bool jni;
46 bool monitor;
47 bool profiler;
48 bool signals;
49 bool startup;
50 bool third_party_jni; // Enabled with "-verbose:third-party-jni".
51 bool threads;
52 bool verifier;
53};
54
55// Global log verbosity setting, initialized by InitLogging.
56extern LogVerbosity gLogVerbosity;
57
Ian Rogersc7dd2952014-10-21 23:31:19 -070058// Configure logging based on ANDROID_LOG_TAGS environment variable.
59// We need to parse a string that looks like
60//
61// *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
62//
63// The tag (or '*' for the global level) comes first, followed by a colon
64// and a letter indicating the minimum priority level we're expected to log.
65// This can be used to reveal or conceal logs with specific tags.
66extern void InitLogging(char* argv[]);
67
68// Returns the command line used to invoke the current tool or nullptr if InitLogging hasn't been
69// performed.
70extern const char* GetCmdLine();
71
72// The command used to start the ART runtime, such as "/system/bin/dalvikvm". If InitLogging hasn't
73// been performed then just returns "art"
74extern const char* ProgramInvocationName();
75
76// A short version of the command used to start the ART runtime, such as "dalvikvm". If InitLogging
77// hasn't been performed then just returns "art"
78extern const char* ProgramInvocationShortName();
79
80// Logs a message to logcat on Android otherwise to stderr. If the severity is FATAL it also causes
81// an abort. For example: LOG(FATAL) << "We didn't expect to reach here";
82#define LOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, -1).stream()
83
84// A variant of LOG that also logs the current errno value. To be used when library calls fail.
85#define PLOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, errno).stream()
86
87// Marker that code is yet to be implemented.
88#define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
89
90// Is verbose logging enabled for the given module? Where the module is defined in LogVerbosity.
91#define VLOG_IS_ON(module) UNLIKELY(::art::gLogVerbosity.module)
92
93// Variant of LOG that logs when verbose logging is enabled for a module. For example,
94// VLOG(jni) << "A JNI operation was performed";
95#define VLOG(module) \
96 if (VLOG_IS_ON(module)) \
97 ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
98
99// Return the stream associated with logging for the given module.
100#define VLOG_STREAM(module) ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
101
102// Check whether condition x holds and LOG(FATAL) if not. The value of the expression x is only
103// evaluated once. Extra logging can be appended using << after. For example,
104// CHECK(false == true) results in a log message of "Check failed: false == true".
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700105#define CHECK(x) \
Ian Rogerscaab8c42011-10-12 12:11:18 -0700106 if (UNLIKELY(!(x))) \
Ian Rogersc7dd2952014-10-21 23:31:19 -0700107 ::art::LogMessage(__FILE__, __LINE__, ::art::FATAL, -1).stream() \
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700108 << "Check failed: " #x << " "
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700109
Ian Rogersc7dd2952014-10-21 23:31:19 -0700110// Helper for CHECK_xx(x,y) macros.
Elliott Hughes1f359b02011-07-17 14:27:17 -0700111#define CHECK_OP(LHS, RHS, OP) \
Mathieu Chartier9b3c3cd2013-08-12 17:41:54 -0700112 for (auto _values = ::art::MakeEagerEvaluator(LHS, RHS); \
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700113 UNLIKELY(!(_values.lhs OP _values.rhs)); /* empty */) \
Ian Rogersc7dd2952014-10-21 23:31:19 -0700114 ::art::LogMessage(__FILE__, __LINE__, ::art::FATAL, -1).stream() \
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700115 << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
116 << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
Elliott Hughes1f359b02011-07-17 14:27:17 -0700117
Ian Rogersc7dd2952014-10-21 23:31:19 -0700118
119// Check whether a condition holds between x and y, LOG(FATAL) if not. The value of the expressions
120// x and y is evaluated once. Extra logging can be appended using << after. For example,
121// CHECK_NE(0 == 1, false) results in "Check failed: false != false (0==1=false, false=false) ".
Elliott Hughes1f359b02011-07-17 14:27:17 -0700122#define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
123#define CHECK_NE(x, y) CHECK_OP(x, y, !=)
124#define CHECK_LE(x, y) CHECK_OP(x, y, <=)
125#define CHECK_LT(x, y) CHECK_OP(x, y, <)
126#define CHECK_GE(x, y) CHECK_OP(x, y, >=)
127#define CHECK_GT(x, y) CHECK_OP(x, y, >)
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700128
Ian Rogersc7dd2952014-10-21 23:31:19 -0700129// Helper for CHECK_STRxx(s1,s2) macros.
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700130#define CHECK_STROP(s1, s2, sense) \
Ian Rogerscaab8c42011-10-12 12:11:18 -0700131 if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \
Ian Rogersc7dd2952014-10-21 23:31:19 -0700132 LOG(::art::FATAL) << "Check failed: " \
133 << "\"" << s1 << "\"" \
134 << (sense ? " == " : " != ") \
135 << "\"" << s2 << "\""
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700136
Ian Rogersc7dd2952014-10-21 23:31:19 -0700137// Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not.
Elliott Hughes1f359b02011-07-17 14:27:17 -0700138#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
139#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
140
Ian Rogersc7dd2952014-10-21 23:31:19 -0700141// Perform the pthread function call(args), LOG(FATAL) on error.
Elliott Hughes8d768a92011-09-14 16:35:25 -0700142#define CHECK_PTHREAD_CALL(call, args, what) \
143 do { \
144 int rc = call args; \
145 if (rc != 0) { \
146 errno = rc; \
Ian Rogersc7dd2952014-10-21 23:31:19 -0700147 PLOG(::art::FATAL) << # call << " failed for " << what; \
Elliott Hughes8d768a92011-09-14 16:35:25 -0700148 } \
149 } while (false)
150
Vladimir Marko83642482014-06-11 12:12:07 +0100151// CHECK that can be used in a constexpr function. For example,
152// constexpr int half(int n) {
153// return
154// DCHECK_CONSTEXPR(n >= 0, , 0)
155// CHECK_CONSTEXPR((n & 1) == 0), << "Extra debugging output: n = " << n, 0)
156// n / 2;
157// }
158#define CHECK_CONSTEXPR(x, out, dummy) \
Ian Rogersc7dd2952014-10-21 23:31:19 -0700159 (UNLIKELY(!(x))) ? (LOG(::art::FATAL) << "Check failed: " << #x out, dummy) :
Vladimir Marko83642482014-06-11 12:12:07 +0100160
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700161
Ian Rogersc7dd2952014-10-21 23:31:19 -0700162// DCHECKs are debug variants of CHECKs only enabled in debug builds. Generally CHECK should be
163// used unless profiling identifies a CHECK as being in performance critical code.
164#if defined(NDEBUG)
165static constexpr bool kEnableDChecks = false;
166#else
167static constexpr bool kEnableDChecks = true;
Carl Shapiro6c21dc12011-06-20 15:20:52 -0700168#endif
169
Ian Rogersc7dd2952014-10-21 23:31:19 -0700170#define DCHECK(x) if (::art::kEnableDChecks) CHECK(x)
171#define DCHECK_EQ(x, y) if (::art::kEnableDChecks) CHECK_EQ(x, y)
172#define DCHECK_NE(x, y) if (::art::kEnableDChecks) CHECK_NE(x, y)
173#define DCHECK_LE(x, y) if (::art::kEnableDChecks) CHECK_LE(x, y)
174#define DCHECK_LT(x, y) if (::art::kEnableDChecks) CHECK_LT(x, y)
175#define DCHECK_GE(x, y) if (::art::kEnableDChecks) CHECK_GE(x, y)
176#define DCHECK_GT(x, y) if (::art::kEnableDChecks) CHECK_GT(x, y)
177#define DCHECK_STREQ(s1, s2) if (::art::kEnableDChecks) CHECK_STREQ(s1, s2)
178#define DCHECK_STRNE(s1, s2) if (::art::kEnableDChecks) CHECK_STRNE(s1, s2)
179#if defined(NDEBUG)
180#define DCHECK_CONSTEXPR(x, out, dummy)
181#else
182#define DCHECK_CONSTEXPR(x, out, dummy) CHECK_CONSTEXPR(x, out, dummy)
183#endif
Elliott Hugheseb4f6142011-07-15 17:43:51 -0700184
Ian Rogersc7dd2952014-10-21 23:31:19 -0700185// Temporary class created to evaluate the LHS and RHS, used with MakeEagerEvaluator to infer the
186// types of LHS and RHS.
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700187template <typename LHS, typename RHS>
188struct EagerEvaluator {
Ian Rogers02875c52014-09-25 17:36:39 -0700189 EagerEvaluator(LHS l, RHS r) : lhs(l), rhs(r) { }
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700190 LHS lhs;
191 RHS rhs;
192};
193
Ian Rogersc7dd2952014-10-21 23:31:19 -0700194// Helper function for CHECK_xx.
195template <typename LHS, typename RHS>
196static inline EagerEvaluator<LHS, RHS> MakeEagerEvaluator(LHS lhs, RHS rhs) {
197 return EagerEvaluator<LHS, RHS>(lhs, rhs);
198}
199
200// Explicitly instantiate EagerEvalue for pointers so that char*s aren't treated as strings. To
201// compare strings use CHECK_STREQ and CHECK_STRNE. We rely on signed/unsigned warnings to
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800202// protect you against combinations not explicitly listed below.
203#define EAGER_PTR_EVALUATOR(T1, T2) \
204 template <> struct EagerEvaluator<T1, T2> { \
Ian Rogers02875c52014-09-25 17:36:39 -0700205 EagerEvaluator(T1 l, T2 r) \
206 : lhs(reinterpret_cast<const void*>(l)), \
207 rhs(reinterpret_cast<const void*>(r)) { } \
Elliott Hughesaa6a5882012-01-13 19:39:16 -0800208 const void* lhs; \
209 const void* rhs; \
210 }
211EAGER_PTR_EVALUATOR(const char*, const char*);
212EAGER_PTR_EVALUATOR(const char*, char*);
213EAGER_PTR_EVALUATOR(char*, const char*);
214EAGER_PTR_EVALUATOR(char*, char*);
215EAGER_PTR_EVALUATOR(const unsigned char*, const unsigned char*);
216EAGER_PTR_EVALUATOR(const unsigned char*, unsigned char*);
217EAGER_PTR_EVALUATOR(unsigned char*, const unsigned char*);
218EAGER_PTR_EVALUATOR(unsigned char*, unsigned char*);
219EAGER_PTR_EVALUATOR(const signed char*, const signed char*);
220EAGER_PTR_EVALUATOR(const signed char*, signed char*);
221EAGER_PTR_EVALUATOR(signed char*, const signed char*);
222EAGER_PTR_EVALUATOR(signed char*, signed char*);
223
Ian Rogersc7dd2952014-10-21 23:31:19 -0700224// Data for the log message, not stored in LogMessage to avoid increasing the stack size.
225class LogMessageData;
Mathieu Chartier9b3c3cd2013-08-12 17:41:54 -0700226
Ian Rogersc7dd2952014-10-21 23:31:19 -0700227// A LogMessage is a temporarily scoped object used by LOG and the unlikely part of a CHECK. The
228// destructor will abort if the severity is FATAL.
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700229class LogMessage {
230 public:
Ian Rogersc7dd2952014-10-21 23:31:19 -0700231 LogMessage(const char* file, unsigned int line, LogSeverity severity, int error);
Sebastien Hertz74c07042013-05-17 14:04:12 +0200232
Ian Rogers719d1a32014-03-06 12:13:39 -0800233 ~LogMessage(); // TODO: enable LOCKS_EXCLUDED(Locks::logging_lock_).
Sebastien Hertz74c07042013-05-17 14:04:12 +0200234
Ian Rogersc7dd2952014-10-21 23:31:19 -0700235 // Returns the stream associated with the message, the LogMessage performs output when it goes
236 // out of scope.
237 std::ostream& stream();
238
239 // The routine that performs the actual logging.
240 static void LogLine(const char* file, unsigned int line, LogSeverity severity, const char* msg);
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700241
Ian Rogersf4d4da12014-11-11 16:10:33 -0800242 // A variant of the above for use with little stack.
243 static void LogLineLowStack(const char* file, unsigned int line, LogSeverity severity,
244 const char* msg);
245
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700246 private:
Ian Rogers700a4022014-05-19 16:49:03 -0700247 const std::unique_ptr<LogMessageData> data_;
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700248
249 DISALLOW_COPY_AND_ASSIGN(LogMessage);
250};
251
252} // namespace art
253
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700254#endif // ART_RUNTIME_BASE_LOGGING_H_