blob: 45bc3c4cc6c3d5df4fd357ac59c0cf4a25c78c4a [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_BASE_LOGGING_H_
6#define V8_BASE_LOGGING_H_
7
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include <cstring>
9#include <sstream>
10#include <string>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/base/build_config.h"
Ben Murdochc5610432016-08-08 18:44:38 +010013#include "src/base/compiler-specific.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014
Ben Murdochc5610432016-08-08 18:44:38 +010015extern "C" PRINTF_FORMAT(3, 4) V8_NORETURN
16 void V8_Fatal(const char* file, int line, const char* format, ...);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017
Ben Murdochda12d292016-06-02 14:46:10 +010018extern "C" void V8_RuntimeError(const char* file, int line,
19 const char* message);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020
21// The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during
22// development, but they should not be relied on in the final product.
23#ifdef DEBUG
24#define FATAL(msg) \
25 V8_Fatal(__FILE__, __LINE__, "%s", (msg))
26#define UNIMPLEMENTED() \
27 V8_Fatal(__FILE__, __LINE__, "unimplemented code")
28#define UNREACHABLE() \
29 V8_Fatal(__FILE__, __LINE__, "unreachable code")
30#else
31#define FATAL(msg) \
32 V8_Fatal("", 0, "%s", (msg))
33#define UNIMPLEMENTED() \
34 V8_Fatal("", 0, "unimplemented code")
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035#define UNREACHABLE() V8_Fatal("", 0, "unreachable code")
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036#endif
37
38
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039namespace v8 {
40namespace base {
41
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042// CHECK dies with a fatal error if condition is not true. It is *not*
43// controlled by DEBUG, so the check will be executed regardless of
44// compilation mode.
45//
46// We make sure CHECK et al. always evaluates their arguments, as
47// doing CHECK(FunctionWithSideEffect()) is a common idiom.
48#define CHECK(condition) \
49 do { \
50 if (V8_UNLIKELY(!(condition))) { \
51 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", #condition); \
52 } \
53 } while (0)
54
55
56#ifdef DEBUG
57
58// Helper macro for binary operators.
59// Don't use this macro directly in your code, use CHECK_EQ et al below.
60#define CHECK_OP(name, op, lhs, rhs) \
61 do { \
62 if (std::string* _msg = ::v8::base::Check##name##Impl( \
63 (lhs), (rhs), #lhs " " #op " " #rhs)) { \
64 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", _msg->c_str()); \
65 delete _msg; \
66 } \
67 } while (0)
68
69#else
70
71// Make all CHECK functions discard their log strings to reduce code
72// bloat for official release builds.
73
74#define CHECK_OP(name, op, lhs, rhs) CHECK((lhs)op(rhs))
75
76#endif
77
78
79// Build the error message string. This is separate from the "Impl"
80// function template because it is not performance critical and so can
81// be out of line, while the "Impl" code should be inline. Caller
82// takes ownership of the returned string.
83template <typename Lhs, typename Rhs>
84std::string* MakeCheckOpString(Lhs const& lhs, Rhs const& rhs,
85 char const* msg) {
86 std::ostringstream ss;
87 ss << msg << " (" << lhs << " vs. " << rhs << ")";
88 return new std::string(ss.str());
89}
90
91// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
92// in logging.cc.
93#define DEFINE_MAKE_CHECK_OP_STRING(type) \
94 extern template std::string* MakeCheckOpString<type, type>( \
95 type const&, type const&, char const*);
96DEFINE_MAKE_CHECK_OP_STRING(int)
97DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int)
98DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int)
99DEFINE_MAKE_CHECK_OP_STRING(unsigned int)
100DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int)
101DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int)
102DEFINE_MAKE_CHECK_OP_STRING(char const*)
103DEFINE_MAKE_CHECK_OP_STRING(void const*)
104#undef DEFINE_MAKE_CHECK_OP_STRING
105
106
107// Helper functions for CHECK_OP macro.
108// The (int, int) specialization works around the issue that the compiler
109// will not instantiate the template version of the function on values of
110// unnamed enum type - see comment below.
111// The (float, float) and (double, double) instantiations are explicitly
112// externialized to ensure proper 32/64-bit comparisons on x86.
113#define DEFINE_CHECK_OP_IMPL(NAME, op) \
114 template <typename Lhs, typename Rhs> \
115 V8_INLINE std::string* Check##NAME##Impl(Lhs const& lhs, Rhs const& rhs, \
116 char const* msg) { \
117 return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \
118 } \
119 V8_INLINE std::string* Check##NAME##Impl(int lhs, int rhs, \
120 char const* msg) { \
121 return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \
122 } \
123 extern template std::string* Check##NAME##Impl<float, float>( \
124 float const& lhs, float const& rhs, char const* msg); \
125 extern template std::string* Check##NAME##Impl<double, double>( \
126 double const& lhs, double const& rhs, char const* msg);
127DEFINE_CHECK_OP_IMPL(EQ, ==)
128DEFINE_CHECK_OP_IMPL(NE, !=)
129DEFINE_CHECK_OP_IMPL(LE, <=)
130DEFINE_CHECK_OP_IMPL(LT, < )
131DEFINE_CHECK_OP_IMPL(GE, >=)
132DEFINE_CHECK_OP_IMPL(GT, > )
133#undef DEFINE_CHECK_OP_IMPL
134
135#define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs)
136#define CHECK_NE(lhs, rhs) CHECK_OP(NE, !=, lhs, rhs)
137#define CHECK_LE(lhs, rhs) CHECK_OP(LE, <=, lhs, rhs)
138#define CHECK_LT(lhs, rhs) CHECK_OP(LT, <, lhs, rhs)
139#define CHECK_GE(lhs, rhs) CHECK_OP(GE, >=, lhs, rhs)
140#define CHECK_GT(lhs, rhs) CHECK_OP(GT, >, lhs, rhs)
141#define CHECK_NULL(val) CHECK((val) == nullptr)
142#define CHECK_NOT_NULL(val) CHECK((val) != nullptr)
143#define CHECK_IMPLIES(lhs, rhs) CHECK(!(lhs) || (rhs))
144
145
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146// Exposed for making debugging easier (to see where your function is being
147// called, just add a call to DumpBacktrace).
148void DumpBacktrace();
149
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000150} // namespace base
151} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152
153
154// The DCHECK macro is equivalent to CHECK except that it only
155// generates code in debug builds.
156#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157#define DCHECK(condition) CHECK(condition)
158#define DCHECK_EQ(v1, v2) CHECK_EQ(v1, v2)
159#define DCHECK_NE(v1, v2) CHECK_NE(v1, v2)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000160#define DCHECK_GT(v1, v2) CHECK_GT(v1, v2)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161#define DCHECK_GE(v1, v2) CHECK_GE(v1, v2)
162#define DCHECK_LT(v1, v2) CHECK_LT(v1, v2)
163#define DCHECK_LE(v1, v2) CHECK_LE(v1, v2)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000164#define DCHECK_NULL(val) CHECK_NULL(val)
165#define DCHECK_NOT_NULL(val) CHECK_NOT_NULL(val)
166#define DCHECK_IMPLIES(v1, v2) CHECK_IMPLIES(v1, v2)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168#define DCHECK(condition) ((void) 0)
169#define DCHECK_EQ(v1, v2) ((void) 0)
170#define DCHECK_NE(v1, v2) ((void) 0)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171#define DCHECK_GT(v1, v2) ((void) 0)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172#define DCHECK_GE(v1, v2) ((void) 0)
173#define DCHECK_LT(v1, v2) ((void) 0)
174#define DCHECK_LE(v1, v2) ((void) 0)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000175#define DCHECK_NULL(val) ((void) 0)
176#define DCHECK_NOT_NULL(val) ((void) 0)
177#define DCHECK_IMPLIES(v1, v2) ((void) 0)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178#endif
179
180#endif // V8_BASE_LOGGING_H_