Line data Source code
1 : // Copyright 2006-2008 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 : #include "src/base/logging.h"
6 :
7 : #include <cctype>
8 : #include <cstdarg>
9 : #include <cstdio>
10 : #include <cstdlib>
11 :
12 : #include "src/base/debug/stack_trace.h"
13 : #include "src/base/platform/platform.h"
14 :
15 : namespace v8 {
16 : namespace base {
17 :
18 : namespace {
19 :
20 : void DefaultDcheckHandler(const char* file, int line, const char* message);
21 :
22 : void (*g_print_stack_trace)() = nullptr;
23 :
24 : void (*g_dcheck_function)(const char*, int, const char*) = DefaultDcheckHandler;
25 :
26 0 : void PrettyPrintChar(std::ostream& os, int ch) {
27 0 : switch (ch) {
28 : #define CHAR_PRINT_CASE(ch) \
29 : case ch: \
30 : os << #ch; \
31 : break;
32 :
33 0 : CHAR_PRINT_CASE('\0')
34 0 : CHAR_PRINT_CASE('\'')
35 0 : CHAR_PRINT_CASE('\\')
36 0 : CHAR_PRINT_CASE('\a')
37 0 : CHAR_PRINT_CASE('\b')
38 0 : CHAR_PRINT_CASE('\f')
39 0 : CHAR_PRINT_CASE('\n')
40 0 : CHAR_PRINT_CASE('\r')
41 0 : CHAR_PRINT_CASE('\t')
42 0 : CHAR_PRINT_CASE('\v')
43 : #undef CHAR_PRINT_CASE
44 : default:
45 0 : if (std::isprint(ch)) {
46 0 : os << '\'' << ch << '\'';
47 : } else {
48 0 : auto flags = os.flags(std::ios_base::hex);
49 0 : os << "\\x" << static_cast<unsigned int>(ch);
50 0 : os.flags(flags);
51 : }
52 : }
53 0 : }
54 :
55 0 : void DefaultDcheckHandler(const char* file, int line, const char* message) {
56 0 : V8_Fatal(file, line, "Debug check failed: %s.", message);
57 : }
58 :
59 : } // namespace
60 :
61 107128 : void SetPrintStackTrace(void (*print_stack_trace)()) {
62 107128 : g_print_stack_trace = print_stack_trace;
63 107128 : }
64 :
65 5 : void SetDcheckFunction(void (*dcheck_function)(const char*, int, const char*)) {
66 5 : g_dcheck_function = dcheck_function ? dcheck_function : &DefaultDcheckHandler;
67 5 : }
68 :
69 : // Define specialization to pretty print characters (escaping non-printable
70 : // characters) and to print c strings as pointers instead of strings.
71 : #define DEFINE_PRINT_CHECK_OPERAND_CHAR(type) \
72 : template <> \
73 : void PrintCheckOperand<type>(std::ostream & os, type ch) { \
74 : PrettyPrintChar(os, ch); \
75 : } \
76 : template <> \
77 : void PrintCheckOperand<type*>(std::ostream & os, type * cstr) { \
78 : os << static_cast<void*>(cstr); \
79 : } \
80 : template <> \
81 : void PrintCheckOperand<const type*>(std::ostream & os, const type* cstr) { \
82 : os << static_cast<const void*>(cstr); \
83 : }
84 :
85 0 : DEFINE_PRINT_CHECK_OPERAND_CHAR(char)
86 0 : DEFINE_PRINT_CHECK_OPERAND_CHAR(signed char)
87 0 : DEFINE_PRINT_CHECK_OPERAND_CHAR(unsigned char)
88 : #undef DEFINE_PRINT_CHECK_OPERAND_CHAR
89 :
90 : // Explicit instantiations for commonly used comparisons.
91 : #define DEFINE_MAKE_CHECK_OP_STRING(type) \
92 : template std::string* MakeCheckOpString<type, type>(type, type, \
93 : char const*); \
94 : template void PrintCheckOperand<type>(std::ostream&, type);
95 : DEFINE_MAKE_CHECK_OP_STRING(int)
96 : DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int)
97 : DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int)
98 : DEFINE_MAKE_CHECK_OP_STRING(unsigned int)
99 : DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int)
100 : DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int)
101 : DEFINE_MAKE_CHECK_OP_STRING(void const*)
102 : #undef DEFINE_MAKE_CHECK_OP_STRING
103 :
104 :
105 : // Explicit instantiations for floating point checks.
106 : #define DEFINE_CHECK_OP_IMPL(NAME) \
107 : template std::string* Check##NAME##Impl<float, float>(float lhs, float rhs, \
108 : char const* msg); \
109 : template std::string* Check##NAME##Impl<double, double>( \
110 : double lhs, double rhs, char const* msg);
111 : DEFINE_CHECK_OP_IMPL(EQ)
112 : DEFINE_CHECK_OP_IMPL(NE)
113 : DEFINE_CHECK_OP_IMPL(LE)
114 : DEFINE_CHECK_OP_IMPL(LT)
115 : DEFINE_CHECK_OP_IMPL(GE)
116 : DEFINE_CHECK_OP_IMPL(GT)
117 : #undef DEFINE_CHECK_OP_IMPL
118 :
119 : } // namespace base
120 : } // namespace v8
121 :
122 :
123 : // Contains protection against recursive calls (faults while handling faults).
124 0 : void V8_Fatal(const char* file, int line, const char* format, ...) {
125 0 : fflush(stdout);
126 0 : fflush(stderr);
127 : v8::base::OS::PrintError("\n\n#\n# Fatal error in %s, line %d\n# ", file,
128 0 : line);
129 : va_list arguments;
130 0 : va_start(arguments, format);
131 0 : v8::base::OS::VPrintError(format, arguments);
132 0 : va_end(arguments);
133 0 : v8::base::OS::PrintError("\n#\n");
134 :
135 0 : if (v8::base::g_print_stack_trace) v8::base::g_print_stack_trace();
136 :
137 0 : fflush(stderr);
138 0 : v8::base::OS::Abort();
139 : }
140 :
141 0 : void V8_Dcheck(const char* file, int line, const char* message) {
142 0 : v8::base::g_dcheck_function(file, line, message);
143 0 : }
|