// Copyright 2012 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_BASE_LOGGING_H_ #define V8_BASE_LOGGING_H_ #include #include #include #include "src/base/base-export.h" #include "src/base/build_config.h" #include "src/base/compiler-specific.h" #include "src/base/template-utils.h" V8_BASE_EXPORT V8_NOINLINE void V8_Dcheck(const char* file, int line, const char* message); #ifdef DEBUG // In debug, include file, line, and full error message for all // FATAL() calls. [[noreturn]] PRINTF_FORMAT(3, 4) V8_BASE_EXPORT V8_NOINLINE void V8_Fatal(const char* file, int line, const char* format, ...); #define FATAL(...) V8_Fatal(__FILE__, __LINE__, __VA_ARGS__) #elif !defined(OFFICIAL_BUILD) // In non-official release, include full error message, but drop file & line // numbers. It saves binary size to drop the |file| & |line| as opposed to just // passing in "", 0 for them. [[noreturn]] PRINTF_FORMAT(1, 2) V8_BASE_EXPORT V8_NOINLINE void V8_Fatal(const char* format, ...); #define FATAL(...) V8_Fatal(__VA_ARGS__) #else // In official builds, include only messages that contain parameters because // single-message errors can always be derived from stack traces. [[noreturn]] V8_BASE_EXPORT V8_NOINLINE void V8_FatalNoContext(); [[noreturn]] PRINTF_FORMAT(1, 2) V8_BASE_EXPORT V8_NOINLINE void V8_Fatal(const char* format, ...); // FATAL(msg) -> V8_FatalNoContext() // FATAL(msg, ...) -> V8_Fatal() #define FATAL_HELPER(_7, _6, _5, _4, _3, _2, _1, _0, ...) _0 #define FATAL_DISCARD_ARG(arg) V8_FatalNoContext() #define FATAL(...) \ FATAL_HELPER(__VA_ARGS__, V8_Fatal, V8_Fatal, V8_Fatal, V8_Fatal, V8_Fatal, \ V8_Fatal, V8_Fatal, FATAL_DISCARD_ARG) \ (__VA_ARGS__) #endif #define UNIMPLEMENTED() FATAL("unimplemented code") #define UNREACHABLE() FATAL("unreachable code") namespace v8 { namespace base { // Overwrite the default function that prints a stack trace. V8_BASE_EXPORT void SetPrintStackTrace(void (*print_stack_trace_)()); // Override the default function that handles DCHECKs. V8_BASE_EXPORT void SetDcheckFunction(void (*dcheck_Function)(const char*, int, const char*)); // In official builds, assume all check failures can be debugged given just the // stack trace. #if !defined(DEBUG) && defined(OFFICIAL_BUILD) #define CHECK_FAILED_HANDLER(message) FATAL("ignored") #else #define CHECK_FAILED_HANDLER(message) FATAL("Check failed: %s.", message) #endif // CHECK dies with a fatal error if condition is not true. It is *not* // controlled by DEBUG, so the check will be executed regardless of // compilation mode. // // We make sure CHECK et al. always evaluates their arguments, as // doing CHECK(FunctionWithSideEffect()) is a common idiom. #define CHECK_WITH_MSG(condition, message) \ do { \ if (V8_UNLIKELY(!(condition))) { \ CHECK_FAILED_HANDLER(message); \ } \ } while (false) #define CHECK(condition) CHECK_WITH_MSG(condition, #condition) #ifdef DEBUG #define DCHECK_WITH_MSG(condition, message) \ do { \ if (V8_UNLIKELY(!(condition))) { \ V8_Dcheck(__FILE__, __LINE__, message); \ } \ } while (false) #define DCHECK(condition) DCHECK_WITH_MSG(condition, #condition) // Helper macro for binary operators. // Don't use this macro directly in your code, use CHECK_EQ et al below. #define CHECK_OP(name, op, lhs, rhs) \ do { \ if (std::string* _msg = ::v8::base::Check##name##Impl< \ typename ::v8::base::pass_value_or_ref::type, \ typename ::v8::base::pass_value_or_ref::type>( \ (lhs), (rhs), #lhs " " #op " " #rhs)) { \ FATAL("Check failed: %s.", _msg->c_str()); \ delete _msg; \ } \ } while (false) #define DCHECK_OP(name, op, lhs, rhs) \ do { \ if (std::string* _msg = ::v8::base::Check##name##Impl< \ typename ::v8::base::pass_value_or_ref::type, \ typename ::v8::base::pass_value_or_ref::type>( \ (lhs), (rhs), #lhs " " #op " " #rhs)) { \ V8_Dcheck(__FILE__, __LINE__, _msg->c_str()); \ delete _msg; \ } \ } while (false) #else // Make all CHECK functions discard their log strings to reduce code // bloat for official release builds. #define CHECK_OP(name, op, lhs, rhs) \ do { \ bool _cmp = ::v8::base::Cmp##name##Impl< \ typename ::v8::base::pass_value_or_ref::type, \ typename ::v8::base::pass_value_or_ref::type>((lhs), \ (rhs)); \ CHECK_WITH_MSG(_cmp, #lhs " " #op " " #rhs); \ } while (false) #define DCHECK_WITH_MSG(condition, msg) void(0); #endif // Define PrintCheckOperand for each T which defines operator<< for ostream. template typename std::enable_if< !std::is_function::type>::value && has_output_operator::value>::type PrintCheckOperand(std::ostream& os, T val) { os << std::forward(val); } // Provide an overload for functions and function pointers. Function pointers // don't implicitly convert to void* but do implicitly convert to bool, so // without this function pointers are always printed as 1 or 0. (MSVC isn't // standards-conforming here and converts function pointers to regular // pointers, so this is a no-op for MSVC.) template typename std::enable_if< std::is_function::type>::value>::type PrintCheckOperand(std::ostream& os, T val) { os << reinterpret_cast(val); } // Define PrintCheckOperand for enums which have no operator<<. template typename std::enable_if::value && !has_output_operator::value>::type PrintCheckOperand(std::ostream& os, T val) { using underlying_t = typename std::underlying_type::type; // 8-bit types are not printed as number, so extend them to 16 bit. using int_t = typename std::conditional< std::is_same::value, uint16_t, typename std::conditional::value, int16_t, underlying_t>::type>::type; PrintCheckOperand(os, static_cast(static_cast(val))); } // Define default PrintCheckOperand for non-printable types. template typename std::enable_if::value && !std::is_enum::value>::type PrintCheckOperand(std::ostream& os, T val) { os << ""; } // Define specializations for character types, defined in logging.cc. #define DEFINE_PRINT_CHECK_OPERAND_CHAR(type) \ template <> \ V8_BASE_EXPORT void PrintCheckOperand(std::ostream & os, type ch); \ template <> \ V8_BASE_EXPORT void PrintCheckOperand(std::ostream & os, \ type * cstr); \ template <> \ V8_BASE_EXPORT void PrintCheckOperand(std::ostream & os, \ const type* cstr); DEFINE_PRINT_CHECK_OPERAND_CHAR(char) DEFINE_PRINT_CHECK_OPERAND_CHAR(signed char) DEFINE_PRINT_CHECK_OPERAND_CHAR(unsigned char) #undef DEFINE_PRINT_CHECK_OPERAND_CHAR // Build the error message string. This is separate from the "Impl" // function template because it is not performance critical and so can // be out of line, while the "Impl" code should be inline. Caller // takes ownership of the returned string. template std::string* MakeCheckOpString(Lhs lhs, Rhs rhs, char const* msg) { std::ostringstream ss; ss << msg << " ("; PrintCheckOperand(ss, lhs); ss << " vs. "; PrintCheckOperand(ss, rhs); ss << ")"; return new std::string(ss.str()); } // Commonly used instantiations of MakeCheckOpString<>. Explicitly instantiated // in logging.cc. #define EXPLICIT_CHECK_OP_INSTANTIATION(type) \ extern template V8_BASE_EXPORT std::string* MakeCheckOpString( \ type, type, char const*); \ extern template V8_BASE_EXPORT void PrintCheckOperand(std::ostream&, \ type); EXPLICIT_CHECK_OP_INSTANTIATION(int) EXPLICIT_CHECK_OP_INSTANTIATION(long) // NOLINT(runtime/int) EXPLICIT_CHECK_OP_INSTANTIATION(long long) // NOLINT(runtime/int) EXPLICIT_CHECK_OP_INSTANTIATION(unsigned int) EXPLICIT_CHECK_OP_INSTANTIATION(unsigned long) // NOLINT(runtime/int) EXPLICIT_CHECK_OP_INSTANTIATION(unsigned long long) // NOLINT(runtime/int) EXPLICIT_CHECK_OP_INSTANTIATION(void const*) #undef EXPLICIT_CHECK_OP_INSTANTIATION // comparison_underlying_type provides the underlying integral type of an enum, // or std::decay::type if T is not an enum. Booleans are converted to // "unsigned int", to allow "unsigned int == bool" comparisons. template struct comparison_underlying_type { // std::underlying_type must only be used with enum types, thus use this // {Dummy} type if the given type is not an enum. enum Dummy {}; using decay = typename std::decay::type; static constexpr bool is_enum = std::is_enum::value; using underlying = typename std::underlying_type< typename std::conditional::type>::type; using type_or_bool = typename std::conditional::type; using type = typename std::conditional::value, unsigned int, type_or_bool>::type; }; // Cast a value to its underlying type #define MAKE_UNDERLYING(Type, value) \ static_cast::type>(value) // is_signed_vs_unsigned::value is true if both types are integral, Lhs is // signed, and Rhs is unsigned. False in all other cases. template struct is_signed_vs_unsigned { using lhs_underlying = typename comparison_underlying_type::type; using rhs_underlying = typename comparison_underlying_type::type; static constexpr bool value = std::is_integral::value && std::is_integral::value && std::is_signed::value && std::is_unsigned::value; }; // Same thing, other way around: Lhs is unsigned, Rhs signed. template struct is_unsigned_vs_signed : public is_signed_vs_unsigned {}; // Specialize the compare functions for signed vs. unsigned comparisons. // std::enable_if ensures that this template is only instantiable if both Lhs // and Rhs are integral types, and their signedness does not match. #define MAKE_UNSIGNED(Type, value) \ static_cast::type>::type>(value) #define DEFINE_SIGNED_MISMATCH_COMP(CHECK, NAME, IMPL) \ template \ V8_INLINE typename std::enable_if::value, bool>::type \ Cmp##NAME##Impl(Lhs lhs, Rhs rhs) { \ return IMPL; \ } DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, EQ, lhs >= 0 && MAKE_UNSIGNED(Lhs, lhs) == MAKE_UNDERLYING(Rhs, rhs)) DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, LT, lhs < 0 || MAKE_UNSIGNED(Lhs, lhs) < MAKE_UNDERLYING(Rhs, rhs)) DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, LE, lhs <= 0 || MAKE_UNSIGNED(Lhs, lhs) <= MAKE_UNDERLYING(Rhs, rhs)) DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, NE, !CmpEQImpl(lhs, rhs)) DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, GT, !CmpLEImpl(lhs, rhs)) DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, GE, !CmpLTImpl(lhs, rhs)) DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, EQ, CmpEQImpl(rhs, lhs)) DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, NE, CmpNEImpl(rhs, lhs)) DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, LT, CmpGTImpl(rhs, lhs)) DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, LE, CmpGEImpl(rhs, lhs)) DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, GT, CmpLTImpl(rhs, lhs)) DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, GE, CmpLEImpl(rhs, lhs)) #undef MAKE_UNSIGNED #undef DEFINE_SIGNED_MISMATCH_COMP // Helper functions for CHECK_OP macro. // The (float, float) and (double, double) instantiations are explicitly // externalized to ensure proper 32/64-bit comparisons on x86. // The Cmp##NAME##Impl function is only instantiable if one of the two types is // not integral or their signedness matches (i.e. whenever no specialization is // required, see above). Otherwise it is disabled by the enable_if construct, // and the compiler will pick a specialization from above. #define DEFINE_CHECK_OP_IMPL(NAME, op) \ template \ V8_INLINE \ typename std::enable_if::value && \ !is_unsigned_vs_signed::value, \ bool>::type Cmp##NAME##Impl(Lhs lhs, Rhs rhs) { \ return lhs op rhs; \ } \ template \ V8_INLINE std::string* Check##NAME##Impl(Lhs lhs, Rhs rhs, \ char const* msg) { \ using LhsPassT = typename pass_value_or_ref::type; \ using RhsPassT = typename pass_value_or_ref::type; \ bool cmp = Cmp##NAME##Impl(lhs, rhs); \ return V8_LIKELY(cmp) \ ? nullptr \ : MakeCheckOpString(lhs, rhs, msg); \ } \ extern template V8_BASE_EXPORT std::string* Check##NAME##Impl( \ float lhs, float rhs, char const* msg); \ extern template V8_BASE_EXPORT std::string* \ Check##NAME##Impl(double lhs, double rhs, \ char const* msg); DEFINE_CHECK_OP_IMPL(EQ, ==) DEFINE_CHECK_OP_IMPL(NE, !=) DEFINE_CHECK_OP_IMPL(LE, <=) DEFINE_CHECK_OP_IMPL(LT, < ) DEFINE_CHECK_OP_IMPL(GE, >=) DEFINE_CHECK_OP_IMPL(GT, > ) #undef DEFINE_CHECK_OP_IMPL #define CHECK_EQ(lhs, rhs) CHECK_OP(EQ, ==, lhs, rhs) #define CHECK_NE(lhs, rhs) CHECK_OP(NE, !=, lhs, rhs) #define CHECK_LE(lhs, rhs) CHECK_OP(LE, <=, lhs, rhs) #define CHECK_LT(lhs, rhs) CHECK_OP(LT, <, lhs, rhs) #define CHECK_GE(lhs, rhs) CHECK_OP(GE, >=, lhs, rhs) #define CHECK_GT(lhs, rhs) CHECK_OP(GT, >, lhs, rhs) #define CHECK_NULL(val) CHECK((val) == nullptr) #define CHECK_NOT_NULL(val) CHECK((val) != nullptr) #define CHECK_IMPLIES(lhs, rhs) \ CHECK_WITH_MSG(!(lhs) || (rhs), #lhs " implies " #rhs) } // namespace base } // namespace v8 // The DCHECK macro is equivalent to CHECK except that it only // generates code in debug builds. #ifdef DEBUG #define DCHECK_EQ(lhs, rhs) DCHECK_OP(EQ, ==, lhs, rhs) #define DCHECK_NE(lhs, rhs) DCHECK_OP(NE, !=, lhs, rhs) #define DCHECK_GT(lhs, rhs) DCHECK_OP(GT, >, lhs, rhs) #define DCHECK_GE(lhs, rhs) DCHECK_OP(GE, >=, lhs, rhs) #define DCHECK_LT(lhs, rhs) DCHECK_OP(LT, <, lhs, rhs) #define DCHECK_LE(lhs, rhs) DCHECK_OP(LE, <=, lhs, rhs) #define DCHECK_NULL(val) DCHECK((val) == nullptr) #define DCHECK_NOT_NULL(val) DCHECK((val) != nullptr) #define DCHECK_IMPLIES(lhs, rhs) \ DCHECK_WITH_MSG(!(lhs) || (rhs), #lhs " implies " #rhs) #else #define DCHECK(condition) ((void) 0) #define DCHECK_EQ(v1, v2) ((void) 0) #define DCHECK_NE(v1, v2) ((void) 0) #define DCHECK_GT(v1, v2) ((void) 0) #define DCHECK_GE(v1, v2) ((void) 0) #define DCHECK_LT(v1, v2) ((void) 0) #define DCHECK_LE(v1, v2) ((void) 0) #define DCHECK_NULL(val) ((void) 0) #define DCHECK_NOT_NULL(val) ((void) 0) #define DCHECK_IMPLIES(v1, v2) ((void) 0) #endif #endif // V8_BASE_LOGGING_H_