Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 8 | #include <cstring> |
| 9 | #include <sstream> |
| 10 | #include <string> |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12 | #include "src/base/build_config.h" |
| 13 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14 | extern "C" V8_NORETURN void V8_Fatal(const char* file, int line, |
| 15 | const char* format, ...); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 17 | extern "C" void V8_RuntimeError(const char* file, int line, |
| 18 | const char* message); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 19 | |
| 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 34 | #define UNREACHABLE() V8_Fatal("", 0, "unreachable code") |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 38 | namespace v8 { |
| 39 | namespace base { |
| 40 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 41 | // 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. |
| 82 | template <typename Lhs, typename Rhs> |
| 83 | std::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*); |
| 95 | DEFINE_MAKE_CHECK_OP_STRING(int) |
| 96 | DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int) |
| 97 | DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int) |
| 98 | DEFINE_MAKE_CHECK_OP_STRING(unsigned int) |
| 99 | DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int) |
| 100 | DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int) |
| 101 | DEFINE_MAKE_CHECK_OP_STRING(char const*) |
| 102 | DEFINE_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); |
| 126 | DEFINE_CHECK_OP_IMPL(EQ, ==) |
| 127 | DEFINE_CHECK_OP_IMPL(NE, !=) |
| 128 | DEFINE_CHECK_OP_IMPL(LE, <=) |
| 129 | DEFINE_CHECK_OP_IMPL(LT, < ) |
| 130 | DEFINE_CHECK_OP_IMPL(GE, >=) |
| 131 | DEFINE_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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | // Exposed for making debugging easier (to see where your function is being |
| 146 | // called, just add a call to DumpBacktrace). |
| 147 | void DumpBacktrace(); |
| 148 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 149 | } // namespace base |
| 150 | } // namespace v8 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 151 | |
| 152 | |
| 153 | // The DCHECK macro is equivalent to CHECK except that it only |
| 154 | // generates code in debug builds. |
| 155 | #ifdef DEBUG |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 156 | #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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 159 | #define DCHECK_GT(v1, v2) CHECK_GT(v1, v2) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 160 | #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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 163 | #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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 166 | #else |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 167 | #define DCHECK(condition) ((void) 0) |
| 168 | #define DCHECK_EQ(v1, v2) ((void) 0) |
| 169 | #define DCHECK_NE(v1, v2) ((void) 0) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 170 | #define DCHECK_GT(v1, v2) ((void) 0) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 171 | #define DCHECK_GE(v1, v2) ((void) 0) |
| 172 | #define DCHECK_LT(v1, v2) ((void) 0) |
| 173 | #define DCHECK_LE(v1, v2) ((void) 0) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 174 | #define DCHECK_NULL(val) ((void) 0) |
| 175 | #define DCHECK_NOT_NULL(val) ((void) 0) |
| 176 | #define DCHECK_IMPLIES(v1, v2) ((void) 0) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 177 | #endif |
| 178 | |
| 179 | #endif // V8_BASE_LOGGING_H_ |