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 <cstring>
9 #include <sstream>
10 #include <string>
11 
12 #include "src/base/base-export.h"
13 #include "src/base/build_config.h"
14 #include "src/base/compiler-specific.h"
15 
16 extern "C" PRINTF_FORMAT(3, 4) V8_NORETURN V8_BASE_EXPORT
17     void V8_Fatal(const char* file, int line, const char* format, ...);
18 
19 // The FATAL, UNREACHABLE and UNIMPLEMENTED macros are useful during
20 // development, but they should not be relied on in the final product.
21 #ifdef DEBUG
22 #define FATAL(msg)                              \
23   V8_Fatal(__FILE__, __LINE__, "%s", (msg))
24 #define UNIMPLEMENTED()                         \
25   V8_Fatal(__FILE__, __LINE__, "unimplemented code")
26 #define UNREACHABLE()                           \
27   V8_Fatal(__FILE__, __LINE__, "unreachable code")
28 #else
29 #define FATAL(msg)                              \
30   V8_Fatal("", 0, "%s", (msg))
31 #define UNIMPLEMENTED()                         \
32   V8_Fatal("", 0, "unimplemented code")
33 #define UNREACHABLE() V8_Fatal("", 0, "unreachable code")
34 #endif
35 
36 
37 namespace v8 {
38 namespace base {
39 
40 // CHECK dies with a fatal error if condition is not true.  It is *not*
41 // controlled by DEBUG, so the check will be executed regardless of
42 // compilation mode.
43 //
44 // We make sure CHECK et al. always evaluates their arguments, as
45 // doing CHECK(FunctionWithSideEffect()) is a common idiom.
46 #define CHECK(condition)                                             \
47   do {                                                               \
48     if (V8_UNLIKELY(!(condition))) {                                 \
49       V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", #condition); \
50     }                                                                \
51   } while (0)
52 
53 
54 #ifdef DEBUG
55 
56 // Helper macro for binary operators.
57 // Don't use this macro directly in your code, use CHECK_EQ et al below.
58 #define CHECK_OP(name, op, lhs, rhs)                                    \
59   do {                                                                  \
60     if (std::string* _msg = ::v8::base::Check##name##Impl(              \
61             (lhs), (rhs), #lhs " " #op " " #rhs)) {                     \
62       V8_Fatal(__FILE__, __LINE__, "Check failed: %s.", _msg->c_str()); \
63       delete _msg;                                                      \
64     }                                                                   \
65   } while (0)
66 
67 #else
68 
69 // Make all CHECK functions discard their log strings to reduce code
70 // bloat for official release builds.
71 
72 #define CHECK_OP(name, op, lhs, rhs) CHECK((lhs)op(rhs))
73 
74 #endif
75 
76 
77 // Build the error message string.  This is separate from the "Impl"
78 // function template because it is not performance critical and so can
79 // be out of line, while the "Impl" code should be inline. Caller
80 // takes ownership of the returned string.
81 template <typename Lhs, typename Rhs>
MakeCheckOpString(Lhs const & lhs,Rhs const & rhs,char const * msg)82 std::string* MakeCheckOpString(Lhs const& lhs, Rhs const& rhs,
83                                char const* msg) {
84   std::ostringstream ss;
85   ss << msg << " (" << lhs << " vs. " << rhs << ")";
86   return new std::string(ss.str());
87 }
88 
89 // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated
90 // in logging.cc.
91 #define DEFINE_MAKE_CHECK_OP_STRING(type)                                    \
92   extern template V8_BASE_EXPORT std::string* MakeCheckOpString<type, type>( \
93       type const&, type const&, char const*);
94 DEFINE_MAKE_CHECK_OP_STRING(int)
95 DEFINE_MAKE_CHECK_OP_STRING(long)       // NOLINT(runtime/int)
96 DEFINE_MAKE_CHECK_OP_STRING(long long)  // NOLINT(runtime/int)
97 DEFINE_MAKE_CHECK_OP_STRING(unsigned int)
98 DEFINE_MAKE_CHECK_OP_STRING(unsigned long)       // NOLINT(runtime/int)
99 DEFINE_MAKE_CHECK_OP_STRING(unsigned long long)  // NOLINT(runtime/int)
100 DEFINE_MAKE_CHECK_OP_STRING(char const*)
101 DEFINE_MAKE_CHECK_OP_STRING(void const*)
102 #undef DEFINE_MAKE_CHECK_OP_STRING
103 
104 
105 // Helper functions for CHECK_OP macro.
106 // The (int, int) specialization works around the issue that the compiler
107 // will not instantiate the template version of the function on values of
108 // unnamed enum type - see comment below.
109 // The (float, float) and (double, double) instantiations are explicitly
110 // externialized to ensure proper 32/64-bit comparisons on x86.
111 #define DEFINE_CHECK_OP_IMPL(NAME, op)                                         \
112   template <typename Lhs, typename Rhs>                                        \
113   V8_INLINE std::string* Check##NAME##Impl(Lhs const& lhs, Rhs const& rhs,     \
114                                            char const* msg) {                  \
115     return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \
116   }                                                                            \
117   V8_INLINE std::string* Check##NAME##Impl(int lhs, int rhs,                   \
118                                            char const* msg) {                  \
119     return V8_LIKELY(lhs op rhs) ? nullptr : MakeCheckOpString(lhs, rhs, msg); \
120   }                                                                            \
121   extern template V8_BASE_EXPORT std::string* Check##NAME##Impl<float, float>( \
122       float const& lhs, float const& rhs, char const* msg);                    \
123   extern template V8_BASE_EXPORT std::string*                                  \
124       Check##NAME##Impl<double, double>(double const& lhs, double const& rhs,  \
125                                         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 
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 
149 }  // namespace base
150 }  // namespace v8
151 
152 
153 // The DCHECK macro is equivalent to CHECK except that it only
154 // generates code in debug builds.
155 #ifdef DEBUG
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)
159 #define DCHECK_GT(v1, v2)      CHECK_GT(v1, v2)
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)
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)
166 #else
167 #define DCHECK(condition)      ((void) 0)
168 #define DCHECK_EQ(v1, v2)      ((void) 0)
169 #define DCHECK_NE(v1, v2)      ((void) 0)
170 #define DCHECK_GT(v1, v2)      ((void) 0)
171 #define DCHECK_GE(v1, v2)      ((void) 0)
172 #define DCHECK_LT(v1, v2)      ((void) 0)
173 #define DCHECK_LE(v1, v2)      ((void) 0)
174 #define DCHECK_NULL(val)       ((void) 0)
175 #define DCHECK_NOT_NULL(val)   ((void) 0)
176 #define DCHECK_IMPLIES(v1, v2) ((void) 0)
177 #endif
178 
179 #endif  // V8_BASE_LOGGING_H_
180