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