OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_BASE_LOGGING_H_ | 5 #ifndef V8_BASE_LOGGING_H_ |
6 #define V8_BASE_LOGGING_H_ | 6 #define V8_BASE_LOGGING_H_ |
7 | 7 |
8 #include <cstring> | 8 #include <stdint.h> |
9 #include <sstream> | 9 #include <string.h> |
10 #include <string> | |
11 | 10 |
12 #include "src/base/build_config.h" | 11 #include "src/base/build_config.h" |
13 | 12 |
14 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...); | 13 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...); |
15 | 14 |
16 | 15 |
17 // The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during | 16 // The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during |
18 // development, but they should not be relied on in the final product. | 17 // development, but they should not be relied on in the final product. |
19 #ifdef DEBUG | 18 #ifdef DEBUG |
20 #define FATAL(msg) \ | 19 #define FATAL(msg) \ |
21 V8_Fatal(__FILE__, __LINE__, "%s", (msg)) | 20 V8_Fatal(__FILE__, __LINE__, "%s", (msg)) |
22 #define UNIMPLEMENTED() \ | 21 #define UNIMPLEMENTED() \ |
23 V8_Fatal(__FILE__, __LINE__, "unimplemented code") | 22 V8_Fatal(__FILE__, __LINE__, "unimplemented code") |
24 #define UNREACHABLE() \ | 23 #define UNREACHABLE() \ |
25 V8_Fatal(__FILE__, __LINE__, "unreachable code") | 24 V8_Fatal(__FILE__, __LINE__, "unreachable code") |
26 #else | 25 #else |
27 #define FATAL(msg) \ | 26 #define FATAL(msg) \ |
28 V8_Fatal("", 0, "%s", (msg)) | 27 V8_Fatal("", 0, "%s", (msg)) |
29 #define UNIMPLEMENTED() \ | 28 #define UNIMPLEMENTED() \ |
30 V8_Fatal("", 0, "unimplemented code") | 29 V8_Fatal("", 0, "unimplemented code") |
31 #define UNREACHABLE() ((void) 0) | 30 #define UNREACHABLE() ((void) 0) |
32 #endif | 31 #endif |
33 | 32 |
34 | 33 |
| 34 // The CHECK macro checks that the given condition is true; if not, it |
| 35 // prints a message to stderr and aborts. |
| 36 #define CHECK(condition) \ |
| 37 do { \ |
| 38 if (V8_UNLIKELY(!(condition))) { \ |
| 39 V8_Fatal(__FILE__, __LINE__, "CHECK(%s) failed", #condition); \ |
| 40 } \ |
| 41 } while (0) |
| 42 |
| 43 |
| 44 // Helper function used by the CHECK_EQ function when given int |
| 45 // arguments. Should not be called directly. |
| 46 inline void CheckEqualsHelper(const char* file, int line, |
| 47 const char* expected_source, int expected, |
| 48 const char* value_source, int value) { |
| 49 if (V8_UNLIKELY(expected != value)) { |
| 50 V8_Fatal(file, line, |
| 51 "CHECK_EQ(%s, %s) failed\n# Expected: %i\n# Found: %i", |
| 52 expected_source, value_source, expected, value); |
| 53 } |
| 54 } |
| 55 |
| 56 |
| 57 // Helper function used by the CHECK_EQ function when given int64_t |
| 58 // arguments. Should not be called directly. |
| 59 inline void CheckEqualsHelper(const char* file, int line, |
| 60 const char* expected_source, |
| 61 int64_t expected, |
| 62 const char* value_source, |
| 63 int64_t value) { |
| 64 if (V8_UNLIKELY(expected != value)) { |
| 65 // Print int64_t values in hex, as two int32s, |
| 66 // to avoid platform-dependencies. |
| 67 V8_Fatal(file, line, |
| 68 "CHECK_EQ(%s, %s) failed\n#" |
| 69 " Expected: 0x%08x%08x\n# Found: 0x%08x%08x", |
| 70 expected_source, value_source, |
| 71 static_cast<uint32_t>(expected >> 32), |
| 72 static_cast<uint32_t>(expected), |
| 73 static_cast<uint32_t>(value >> 32), |
| 74 static_cast<uint32_t>(value)); |
| 75 } |
| 76 } |
| 77 |
| 78 |
| 79 // Helper function used by the CHECK_NE function when given int |
| 80 // arguments. Should not be called directly. |
| 81 inline void CheckNonEqualsHelper(const char* file, |
| 82 int line, |
| 83 const char* unexpected_source, |
| 84 int unexpected, |
| 85 const char* value_source, |
| 86 int value) { |
| 87 if (V8_UNLIKELY(unexpected == value)) { |
| 88 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %i", |
| 89 unexpected_source, value_source, value); |
| 90 } |
| 91 } |
| 92 |
| 93 |
| 94 // Helper function used by the CHECK function when given string |
| 95 // arguments. Should not be called directly. |
| 96 inline void CheckEqualsHelper(const char* file, |
| 97 int line, |
| 98 const char* expected_source, |
| 99 const char* expected, |
| 100 const char* value_source, |
| 101 const char* value) { |
| 102 if (V8_UNLIKELY((expected == NULL && value != NULL) || |
| 103 (expected != NULL && value == NULL) || |
| 104 (expected != NULL && value != NULL && |
| 105 strcmp(expected, value) != 0))) { |
| 106 V8_Fatal(file, line, |
| 107 "CHECK_EQ(%s, %s) failed\n# Expected: %s\n# Found: %s", |
| 108 expected_source, value_source, expected, value); |
| 109 } |
| 110 } |
| 111 |
| 112 |
| 113 inline void CheckNonEqualsHelper(const char* file, |
| 114 int line, |
| 115 const char* expected_source, |
| 116 const char* expected, |
| 117 const char* value_source, |
| 118 const char* value) { |
| 119 if (V8_UNLIKELY(expected == value || (expected != NULL && value != NULL && |
| 120 strcmp(expected, value) == 0))) { |
| 121 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %s", |
| 122 expected_source, value_source, value); |
| 123 } |
| 124 } |
| 125 |
| 126 |
| 127 // Helper function used by the CHECK function when given pointer |
| 128 // arguments. Should not be called directly. |
| 129 inline void CheckEqualsHelper(const char* file, |
| 130 int line, |
| 131 const char* expected_source, |
| 132 const void* expected, |
| 133 const char* value_source, |
| 134 const void* value) { |
| 135 if (V8_UNLIKELY(expected != value)) { |
| 136 V8_Fatal(file, line, |
| 137 "CHECK_EQ(%s, %s) failed\n# Expected: %p\n# Found: %p", |
| 138 expected_source, value_source, |
| 139 expected, value); |
| 140 } |
| 141 } |
| 142 |
| 143 |
| 144 inline void CheckNonEqualsHelper(const char* file, |
| 145 int line, |
| 146 const char* expected_source, |
| 147 const void* expected, |
| 148 const char* value_source, |
| 149 const void* value) { |
| 150 if (V8_UNLIKELY(expected == value)) { |
| 151 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n# Value: %p", |
| 152 expected_source, value_source, value); |
| 153 } |
| 154 } |
| 155 |
| 156 |
| 157 inline void CheckNonEqualsHelper(const char* file, |
| 158 int line, |
| 159 const char* expected_source, |
| 160 int64_t expected, |
| 161 const char* value_source, |
| 162 int64_t value) { |
| 163 if (V8_UNLIKELY(expected == value)) { |
| 164 V8_Fatal(file, line, |
| 165 "CHECK_EQ(%s, %s) failed\n# Expected: %f\n# Found: %f", |
| 166 expected_source, value_source, expected, value); |
| 167 } |
| 168 } |
| 169 |
| 170 |
| 171 #define CHECK_EQ(expected, value) CheckEqualsHelper(__FILE__, __LINE__, \ |
| 172 #expected, expected, #value, value) |
| 173 |
| 174 |
| 175 #define CHECK_NE(unexpected, value) CheckNonEqualsHelper(__FILE__, __LINE__, \ |
| 176 #unexpected, unexpected, #value, value) |
| 177 |
| 178 |
| 179 #define CHECK_GT(a, b) CHECK((a) > (b)) |
| 180 #define CHECK_GE(a, b) CHECK((a) >= (b)) |
| 181 #define CHECK_LT(a, b) CHECK((a) < (b)) |
| 182 #define CHECK_LE(a, b) CHECK((a) <= (b)) |
| 183 |
| 184 |
35 namespace v8 { | 185 namespace v8 { |
36 namespace base { | 186 namespace base { |
37 | 187 |
38 // CHECK dies with a fatal error if condition is not true. It is *not* | |
39 // controlled by DEBUG, so the check will be executed regardless of | |
40 // compilation mode. | |
41 // | |
42 // We make sure CHECK et al. always evaluates their arguments, as | |
43 // doing CHECK(FunctionWithSideEffect()) is a common idiom. | |
44 #define CHECK(condition) \ | |
45 do { \ | |
46 if (V8_UNLIKELY(!(condition))) { \ | |
47 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", #condition); \ | |
48 } \ | |
49 } while (0) | |
50 | |
51 | |
52 #ifdef DEBUG | |
53 | |
54 // Helper macro for binary operators. | |
55 // Don't use this macro directly in your code, use CHECK_EQ et al below. | |
56 #define CHECK_OP(name, op, lhs, rhs) \ | |
57 do { \ | |
58 if (std::string* _msg = ::v8::base::Check##name##Impl( \ | |
59 (lhs), (rhs), #lhs " " #op " " #rhs)) { \ | |
60 V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", _msg->c_str()); \ | |
61 delete _msg; \ | |
62 } \ | |
63 } while (0) | |
64 | |
65 #else | |
66 | |
67 // Make all CHECK functions discard their log strings to reduce code | |
68 // bloat for official release builds. | |
69 | |
70 #define CHECK_OP(name, op, lhs, rhs) CHECK((lhs)op(rhs)) | |
71 | |
72 #endif | |
73 | |
74 | |
75 // Build the error message string. This is separate from the "Impl" | |
76 // function template because it is not performance critical and so can | |
77 // be out of line, while the "Impl" code should be inline. Caller | |
78 // takes ownership of the returned string. | |
79 template <typename Lhs, typename Rhs> | |
80 std::string* MakeCheckOpString(Lhs const& lhs, Rhs const& rhs, | |
81 char const* msg) { | |
82 std::ostringstream ss; | |
83 ss << msg << " (" << lhs << " vs. " << rhs << ")"; | |
84 return new std::string(ss.str()); | |
85 } | |
86 | |
87 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated | |
88 // in logging.cc. | |
89 #define DEFINE_MAKE_CHECK_OP_STRING(type) \ | |
90 extern template std::string* MakeCheckOpString<type, type>( \ | |
91 type const&, type const&, char const*); | |
92 DEFINE_MAKE_CHECK_OP_STRING(int) | |
93 DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int) | |
94 DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int) | |
95 DEFINE_MAKE_CHECK_OP_STRING(unsigned int) | |
96 DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int) | |
97 DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int) | |
98 DEFINE_MAKE_CHECK_OP_STRING(char const*) | |
99 DEFINE_MAKE_CHECK_OP_STRING(void const*) | |
100 #undef DEFINE_MAKE_CHECK_OP_STRING | |
101 | |
102 | |
103 // Helper functions for CHECK_OP macro. | |
104 // The (int, int) specialization works around the issue that the compiler | |
105 // will not instantiate the template version of the function on values of | |
106 // unnamed enum type - see comment below. | |
107 // The (float, float) and (double, double) instantiations are explicitly | |
108 // externialized to ensure proper 32/64-bit comparisons on x86. | |
109 #define DEFINE_CHECK_OP_IMPL(NAME, op) \ | |
110 template <typename Lhs, typename Rhs> \ | |
111 V8_INLINE std::string* Check##NAME##Impl(Lhs const& lhs, Rhs const& rhs, \ | |
112 char const* msg) { \ | |
113 return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \ | |
114 } \ | |
115 V8_INLINE std::string* Check##NAME##Impl(int lhs, int rhs, \ | |
116 char const* msg) { \ | |
117 return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \ | |
118 } \ | |
119 extern template std::string* Check##NAME##Impl<float, float>( \ | |
120 float const& lhs, float const& rhs, char const* msg); \ | |
121 extern template std::string* Check##NAME##Impl<double, double>( \ | |
122 double const& lhs, double const& rhs, char const* msg); | |
123 DEFINE_CHECK_OP_IMPL(EQ, ==) | |
124 DEFINE_CHECK_OP_IMPL(NE, !=) | |
125 DEFINE_CHECK_OP_IMPL(LE, <=) | |
126 DEFINE_CHECK_OP_IMPL(LT, < ) | |
127 DEFINE_CHECK_OP_IMPL(GE, >=) | |
128 DEFINE_CHECK_OP_IMPL(GT, > ) | |
129 #undef DEFINE_CHECK_OP_IMPL | |
130 | |
131 #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs) | |
132 #define CHECK_NE(lhs, rhs) CHECK_OP(NE, !=, lhs, rhs) | |
133 #define CHECK_LE(lhs, rhs) CHECK_OP(LE, <=, lhs, rhs) | |
134 #define CHECK_LT(lhs, rhs) CHECK_OP(LT, <, lhs, rhs) | |
135 #define CHECK_GE(lhs, rhs) CHECK_OP(GE, >=, lhs, rhs) | |
136 #define CHECK_GT(lhs, rhs) CHECK_OP(GT, >, lhs, rhs) | |
137 #define CHECK_NULL(val) CHECK((val) == nullptr) | |
138 #define CHECK_NOT_NULL(val) CHECK((val) != nullptr) | |
139 #define CHECK_IMPLIES(lhs, rhs) CHECK(!(lhs) || (rhs)) | |
140 | |
141 | |
142 // Exposed for making debugging easier (to see where your function is being | 188 // Exposed for making debugging easier (to see where your function is being |
143 // called, just add a call to DumpBacktrace). | 189 // called, just add a call to DumpBacktrace). |
144 void DumpBacktrace(); | 190 void DumpBacktrace(); |
145 | 191 |
146 } // namespace base | 192 } } // namespace v8::base |
147 } // namespace v8 | |
148 | 193 |
149 | 194 |
150 // The DCHECK macro is equivalent to CHECK except that it only | 195 // The DCHECK macro is equivalent to CHECK except that it only |
151 // generates code in debug builds. | 196 // generates code in debug builds. |
152 // TODO(bmeurer): DCHECK_RESULT(expr) must die! | |
153 #ifdef DEBUG | 197 #ifdef DEBUG |
154 #define DCHECK_RESULT(expr) CHECK(expr) | 198 #define DCHECK_RESULT(expr) CHECK(expr) |
155 #define DCHECK(condition) CHECK(condition) | 199 #define DCHECK(condition) CHECK(condition) |
156 #define DCHECK_EQ(v1, v2) CHECK_EQ(v1, v2) | 200 #define DCHECK_EQ(v1, v2) CHECK_EQ(v1, v2) |
157 #define DCHECK_NE(v1, v2) CHECK_NE(v1, v2) | 201 #define DCHECK_NE(v1, v2) CHECK_NE(v1, v2) |
158 #define DCHECK_GE(v1, v2) CHECK_GE(v1, v2) | 202 #define DCHECK_GE(v1, v2) CHECK_GE(v1, v2) |
159 #define DCHECK_LT(v1, v2) CHECK_LT(v1, v2) | 203 #define DCHECK_LT(v1, v2) CHECK_LT(v1, v2) |
160 #define DCHECK_LE(v1, v2) CHECK_LE(v1, v2) | 204 #define DCHECK_LE(v1, v2) CHECK_LE(v1, v2) |
161 #define DCHECK_NULL(val) CHECK_NULL(val) | |
162 #define DCHECK_NOT_NULL(val) CHECK_NOT_NULL(val) | |
163 #define DCHECK_IMPLIES(v1, v2) CHECK_IMPLIES(v1, v2) | |
164 #else | 205 #else |
165 #define DCHECK_RESULT(expr) (expr) | 206 #define DCHECK_RESULT(expr) (expr) |
166 #define DCHECK(condition) ((void) 0) | 207 #define DCHECK(condition) ((void) 0) |
167 #define DCHECK_EQ(v1, v2) ((void) 0) | 208 #define DCHECK_EQ(v1, v2) ((void) 0) |
168 #define DCHECK_NE(v1, v2) ((void) 0) | 209 #define DCHECK_NE(v1, v2) ((void) 0) |
169 #define DCHECK_GE(v1, v2) ((void) 0) | 210 #define DCHECK_GE(v1, v2) ((void) 0) |
170 #define DCHECK_LT(v1, v2) ((void) 0) | 211 #define DCHECK_LT(v1, v2) ((void) 0) |
171 #define DCHECK_LE(v1, v2) ((void) 0) | 212 #define DCHECK_LE(v1, v2) ((void) 0) |
172 #define DCHECK_NULL(val) ((void) 0) | |
173 #define DCHECK_NOT_NULL(val) ((void) 0) | |
174 #define DCHECK_IMPLIES(v1, v2) ((void) 0) | |
175 #endif | 213 #endif |
176 | 214 |
| 215 #define DCHECK_NOT_NULL(p) DCHECK_NE(NULL, p) |
| 216 |
177 #endif // V8_BASE_LOGGING_H_ | 217 #endif // V8_BASE_LOGGING_H_ |
OLD | NEW |