blob: 15322f6126dcb9efbf31216effd5e2871b79346c [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"
13
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014extern "C" V8_NORETURN void V8_Fatal(const char* file, int line,
15 const char* format, ...);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016
Ben Murdochda12d292016-06-02 14:46:10 +010017extern "C" void V8_RuntimeError(const char* file, int line,
18 const char* message);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000019
20// The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during
21// development, but they should not be relied on in the final product.
22#ifdef DEBUG
23#define FATAL(msg) \
24 V8_Fatal(__FILE__, __LINE__, "%s", (msg))
25#define UNIMPLEMENTED() \
26 V8_Fatal(__FILE__, __LINE__, "unimplemented code")
27#define UNREACHABLE() \
28 V8_Fatal(__FILE__, __LINE__, "unreachable code")
29#else
30#define FATAL(msg) \
31 V8_Fatal("", 0, "%s", (msg))
32#define UNIMPLEMENTED() \
33 V8_Fatal("", 0, "unimplemented code")
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034#define UNREACHABLE() V8_Fatal("", 0, "unreachable code")
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035#endif
36
37
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038namespace v8 {
39namespace base {
40
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041// CHECK dies with a fatal error if condition is not true. It is *not*
42// controlled by DEBUG, so the check will be executed regardless of
43// compilation mode.
44//
45// We make sure CHECK et al. always evaluates their arguments, as
46// doing CHECK(FunctionWithSideEffect()) is a common idiom.
47#define CHECK(condition) \
48 do { \
49 if (V8_UNLIKELY(!(condition))) { \
50 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", #condition); \
51 } \
52 } while (0)
53
54
55#ifdef DEBUG
56
57// Helper macro for binary operators.
58// Don't use this macro directly in your code, use CHECK_EQ et al below.
59#define CHECK_OP(name, op, lhs, rhs) \
60 do { \
61 if (std::string* _msg = ::v8::base::Check##name##Impl( \
62 (lhs), (rhs), #lhs " " #op " " #rhs)) { \
63 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", _msg->c_str()); \
64 delete _msg; \
65 } \
66 } while (0)
67
68#else
69
70// Make all CHECK functions discard their log strings to reduce code
71// bloat for official release builds.
72
73#define CHECK_OP(name, op, lhs, rhs) CHECK((lhs)op(rhs))
74
75#endif
76
77
78// Build the error message string. This is separate from the "Impl"
79// function template because it is not performance critical and so can
80// be out of line, while the "Impl" code should be inline. Caller
81// takes ownership of the returned string.
82template <typename Lhs, typename Rhs>
83std::string* MakeCheckOpString(Lhs const& lhs, Rhs const& rhs,
84 char const* msg) {
85 std::ostringstream ss;
86 ss << msg << " (" << lhs << " vs. " << rhs << ")";
87 return new std::string(ss.str());
88}
89
90// Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
91// in logging.cc.
92#define DEFINE_MAKE_CHECK_OP_STRING(type) \
93 extern template std::string* MakeCheckOpString<type, type>( \
94 type const&, type const&, char const*);
95DEFINE_MAKE_CHECK_OP_STRING(int)
96DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int)
97DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int)
98DEFINE_MAKE_CHECK_OP_STRING(unsigned int)
99DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int)
100DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int)
101DEFINE_MAKE_CHECK_OP_STRING(char const*)
102DEFINE_MAKE_CHECK_OP_STRING(void const*)
103#undef DEFINE_MAKE_CHECK_OP_STRING
104
105
106// Helper functions for CHECK_OP macro.
107// The (int, int) specialization works around the issue that the compiler
108// will not instantiate the template version of the function on values of
109// unnamed enum type - see comment below.
110// The (float, float) and (double, double) instantiations are explicitly
111// externialized to ensure proper 32/64-bit comparisons on x86.
112#define DEFINE_CHECK_OP_IMPL(NAME, op) \
113 template <typename Lhs, typename Rhs> \
114 V8_INLINE std::string* Check##NAME##Impl(Lhs const& lhs, Rhs const& rhs, \
115 char const* msg) { \
116 return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \
117 } \
118 V8_INLINE std::string* Check##NAME##Impl(int lhs, int rhs, \
119 char const* msg) { \
120 return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \
121 } \
122 extern template std::string* Check##NAME##Impl<float, float>( \
123 float const& lhs, float const& rhs, char const* msg); \
124 extern template std::string* Check##NAME##Impl<double, double>( \
125 double const& lhs, double const& rhs, char const* msg);
126DEFINE_CHECK_OP_IMPL(EQ, ==)
127DEFINE_CHECK_OP_IMPL(NE, !=)
128DEFINE_CHECK_OP_IMPL(LE, <=)
129DEFINE_CHECK_OP_IMPL(LT, < )
130DEFINE_CHECK_OP_IMPL(GE, >=)
131DEFINE_CHECK_OP_IMPL(GT, > )
132#undef DEFINE_CHECK_OP_IMPL
133
134#define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs)
135#define CHECK_NE(lhs, rhs) CHECK_OP(NE, !=, lhs, rhs)
136#define CHECK_LE(lhs, rhs) CHECK_OP(LE, <=, lhs, rhs)
137#define CHECK_LT(lhs, rhs) CHECK_OP(LT, <, lhs, rhs)
138#define CHECK_GE(lhs, rhs) CHECK_OP(GE, >=, lhs, rhs)
139#define CHECK_GT(lhs, rhs) CHECK_OP(GT, >, lhs, rhs)
140#define CHECK_NULL(val) CHECK((val) == nullptr)
141#define CHECK_NOT_NULL(val) CHECK((val) != nullptr)
142#define CHECK_IMPLIES(lhs, rhs) CHECK(!(lhs) || (rhs))
143
144
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145// Exposed for making debugging easier (to see where your function is being
146// called, just add a call to DumpBacktrace).
147void DumpBacktrace();
148
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000149} // namespace base
150} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151
152
153// The DCHECK macro is equivalent to CHECK except that it only
154// generates code in debug builds.
155#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156#define DCHECK(condition) CHECK(condition)
157#define DCHECK_EQ(v1, v2) CHECK_EQ(v1, v2)
158#define DCHECK_NE(v1, v2) CHECK_NE(v1, v2)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000159#define DCHECK_GT(v1, v2) CHECK_GT(v1, v2)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160#define DCHECK_GE(v1, v2) CHECK_GE(v1, v2)
161#define DCHECK_LT(v1, v2) CHECK_LT(v1, v2)
162#define DCHECK_LE(v1, v2) CHECK_LE(v1, v2)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163#define DCHECK_NULL(val) CHECK_NULL(val)
164#define DCHECK_NOT_NULL(val) CHECK_NOT_NULL(val)
165#define DCHECK_IMPLIES(v1, v2) CHECK_IMPLIES(v1, v2)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166#else
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167#define DCHECK(condition) ((void) 0)
168#define DCHECK_EQ(v1, v2) ((void) 0)
169#define DCHECK_NE(v1, v2) ((void) 0)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000170#define DCHECK_GT(v1, v2) ((void) 0)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000171#define DCHECK_GE(v1, v2) ((void) 0)
172#define DCHECK_LT(v1, v2) ((void) 0)
173#define DCHECK_LE(v1, v2) ((void) 0)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000174#define DCHECK_NULL(val) ((void) 0)
175#define DCHECK_NOT_NULL(val) ((void) 0)
176#define DCHECK_IMPLIES(v1, v2) ((void) 0)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000177#endif
178
179#endif // V8_BASE_LOGGING_H_