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 
8 #include <string.h>
9 
10 #include "include/v8stdint.h"
11 #include "src/base/build_config.h"
12 
13 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...);
14 
15 
16 // The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during
17 // development, but they should not be relied on in the final product.
18 #ifdef DEBUG
19 #define FATAL(msg)                              \
20   V8_Fatal(__FILE__, __LINE__, "%s", (msg))
21 #define UNIMPLEMENTED()                         \
22   V8_Fatal(__FILE__, __LINE__, "unimplemented code")
23 #define UNREACHABLE()                           \
24   V8_Fatal(__FILE__, __LINE__, "unreachable code")
25 #else
26 #define FATAL(msg)                              \
27   V8_Fatal("", 0, "%s", (msg))
28 #define UNIMPLEMENTED()                         \
29   V8_Fatal("", 0, "unimplemented code")
30 #define UNREACHABLE() ((void) 0)
31 #endif
32 
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) do {                                       \
37     if (!(condition)) {                                             \
38       V8_Fatal(__FILE__, __LINE__, "CHECK(%s) failed", #condition); \
39     }                                                               \
40   } while (0)
41 
42 
43 // Helper function used by the CHECK_EQ function when given int
44 // arguments.  Should not be called directly.
CheckEqualsHelper(const char * file,int line,const char * expected_source,int expected,const char * value_source,int value)45 inline void CheckEqualsHelper(const char* file, int line,
46                               const char* expected_source, int expected,
47                               const char* value_source, int value) {
48   if (expected != value) {
49     V8_Fatal(file, line,
50              "CHECK_EQ(%s, %s) failed\n#   Expected: %i\n#   Found: %i",
51              expected_source, value_source, expected, value);
52   }
53 }
54 
55 
56 // Helper function used by the CHECK_EQ function when given int64_t
57 // arguments.  Should not be called directly.
CheckEqualsHelper(const char * file,int line,const char * expected_source,int64_t expected,const char * value_source,int64_t value)58 inline void CheckEqualsHelper(const char* file, int line,
59                               const char* expected_source,
60                               int64_t expected,
61                               const char* value_source,
62                               int64_t value) {
63   if (expected != value) {
64     // Print int64_t values in hex, as two int32s,
65     // to avoid platform-dependencies.
66     V8_Fatal(file, line,
67              "CHECK_EQ(%s, %s) failed\n#"
68              "   Expected: 0x%08x%08x\n#   Found: 0x%08x%08x",
69              expected_source, value_source,
70              static_cast<uint32_t>(expected >> 32),
71              static_cast<uint32_t>(expected),
72              static_cast<uint32_t>(value >> 32),
73              static_cast<uint32_t>(value));
74   }
75 }
76 
77 
78 // Helper function used by the CHECK_NE function when given int
79 // arguments.  Should not be called directly.
CheckNonEqualsHelper(const char * file,int line,const char * unexpected_source,int unexpected,const char * value_source,int value)80 inline void CheckNonEqualsHelper(const char* file,
81                                  int line,
82                                  const char* unexpected_source,
83                                  int unexpected,
84                                  const char* value_source,
85                                  int value) {
86   if (unexpected == value) {
87     V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n#   Value: %i",
88              unexpected_source, value_source, value);
89   }
90 }
91 
92 
93 // Helper function used by the CHECK function when given string
94 // arguments.  Should not be called directly.
CheckEqualsHelper(const char * file,int line,const char * expected_source,const char * expected,const char * value_source,const char * value)95 inline void CheckEqualsHelper(const char* file,
96                               int line,
97                               const char* expected_source,
98                               const char* expected,
99                               const char* value_source,
100                               const char* value) {
101   if ((expected == NULL && value != NULL) ||
102       (expected != NULL && value == NULL) ||
103       (expected != NULL && value != NULL && strcmp(expected, value) != 0)) {
104     V8_Fatal(file, line,
105              "CHECK_EQ(%s, %s) failed\n#   Expected: %s\n#   Found: %s",
106              expected_source, value_source, expected, value);
107   }
108 }
109 
110 
CheckNonEqualsHelper(const char * file,int line,const char * expected_source,const char * expected,const char * value_source,const char * value)111 inline void CheckNonEqualsHelper(const char* file,
112                                  int line,
113                                  const char* expected_source,
114                                  const char* expected,
115                                  const char* value_source,
116                                  const char* value) {
117   if (expected == value ||
118       (expected != NULL && value != NULL && strcmp(expected, value) == 0)) {
119     V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n#   Value: %s",
120              expected_source, value_source, value);
121   }
122 }
123 
124 
125 // Helper function used by the CHECK function when given pointer
126 // arguments.  Should not be called directly.
CheckEqualsHelper(const char * file,int line,const char * expected_source,const void * expected,const char * value_source,const void * value)127 inline void CheckEqualsHelper(const char* file,
128                               int line,
129                               const char* expected_source,
130                               const void* expected,
131                               const char* value_source,
132                               const void* value) {
133   if (expected != value) {
134     V8_Fatal(file, line,
135              "CHECK_EQ(%s, %s) failed\n#   Expected: %p\n#   Found: %p",
136              expected_source, value_source,
137              expected, value);
138   }
139 }
140 
141 
CheckNonEqualsHelper(const char * file,int line,const char * expected_source,const void * expected,const char * value_source,const void * value)142 inline void CheckNonEqualsHelper(const char* file,
143                                  int line,
144                                  const char* expected_source,
145                                  const void* expected,
146                                  const char* value_source,
147                                  const void* value) {
148   if (expected == value) {
149     V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n#   Value: %p",
150              expected_source, value_source, value);
151   }
152 }
153 
154 
CheckNonEqualsHelper(const char * file,int line,const char * expected_source,int64_t expected,const char * value_source,int64_t value)155 inline void CheckNonEqualsHelper(const char* file,
156                               int line,
157                               const char* expected_source,
158                               int64_t expected,
159                               const char* value_source,
160                               int64_t value) {
161   if (expected == value) {
162     V8_Fatal(file, line,
163              "CHECK_EQ(%s, %s) failed\n#   Expected: %f\n#   Found: %f",
164              expected_source, value_source, expected, value);
165   }
166 }
167 
168 
169 #define CHECK_EQ(expected, value) CheckEqualsHelper(__FILE__, __LINE__, \
170   #expected, expected, #value, value)
171 
172 
173 #define CHECK_NE(unexpected, value) CheckNonEqualsHelper(__FILE__, __LINE__, \
174   #unexpected, unexpected, #value, value)
175 
176 
177 #define CHECK_GT(a, b) CHECK((a) > (b))
178 #define CHECK_GE(a, b) CHECK((a) >= (b))
179 #define CHECK_LT(a, b) CHECK((a) < (b))
180 #define CHECK_LE(a, b) CHECK((a) <= (b))
181 
182 
183 namespace v8 {
184 namespace base {
185 
186 // Exposed for making debugging easier (to see where your function is being
187 // called, just add a call to DumpBacktrace).
188 void DumpBacktrace();
189 
190 } }  // namespace v8::base
191 
192 
193 // The DCHECK macro is equivalent to CHECK except that it only
194 // generates code in debug builds.
195 #ifdef DEBUG
196 #define DCHECK_RESULT(expr)    CHECK(expr)
197 #define DCHECK(condition)      CHECK(condition)
198 #define DCHECK_EQ(v1, v2)      CHECK_EQ(v1, v2)
199 #define DCHECK_NE(v1, v2)      CHECK_NE(v1, v2)
200 #define DCHECK_GE(v1, v2)      CHECK_GE(v1, v2)
201 #define DCHECK_LT(v1, v2)      CHECK_LT(v1, v2)
202 #define DCHECK_LE(v1, v2)      CHECK_LE(v1, v2)
203 #else
204 #define DCHECK_RESULT(expr)    (expr)
205 #define DCHECK(condition)      ((void) 0)
206 #define DCHECK_EQ(v1, v2)      ((void) 0)
207 #define DCHECK_NE(v1, v2)      ((void) 0)
208 #define DCHECK_GE(v1, v2)      ((void) 0)
209 #define DCHECK_LT(v1, v2)      ((void) 0)
210 #define DCHECK_LE(v1, v2)      ((void) 0)
211 #endif
212 
213 #define DCHECK_NOT_NULL(p)  DCHECK_NE(NULL, p)
214 
215 // "Extra checks" are lightweight checks that are enabled in some release
216 // builds.
217 #ifdef ENABLE_EXTRA_CHECKS
218 #define EXTRA_CHECK(condition) CHECK(condition)
219 #else
220 #define EXTRA_CHECK(condition) ((void) 0)
221 #endif
222 
223 #endif  // V8_BASE_LOGGING_H_
224