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
PrettyPrintChar(int ch)26 std::string PrettyPrintChar(int ch) {
27 std::ostringstream oss;
28 switch (ch) {
29 #define CHAR_PRINT_CASE(ch) \
30 case ch: \
31 oss << #ch; \
32 break;
33
34 CHAR_PRINT_CASE('\0')
35 CHAR_PRINT_CASE('\'')
36 CHAR_PRINT_CASE('\\')
37 CHAR_PRINT_CASE('\a')
38 CHAR_PRINT_CASE('\b')
39 CHAR_PRINT_CASE('\f')
40 CHAR_PRINT_CASE('\n')
41 CHAR_PRINT_CASE('\r')
42 CHAR_PRINT_CASE('\t')
43 CHAR_PRINT_CASE('\v')
44 #undef CHAR_PRINT_CASE
45 default:
46 if (std::isprint(ch)) {
47 oss << '\'' << ch << '\'';
48 } else {
49 oss << std::hex << "\\x" << static_cast<unsigned int>(ch);
50 }
51 }
52 return oss.str();
53 }
54
DefaultDcheckHandler(const char * file,int line,const char * message)55 void DefaultDcheckHandler(const char* file, int line, const char* message) {
56 #ifdef DEBUG
57 V8_Fatal(file, line, "Debug check failed: %s.", message);
58 #else
59 // This case happens only for unit tests.
60 V8_Fatal("Debug check failed: %s.", message);
61 #endif
62 }
63
64 } // namespace
65
SetPrintStackTrace(void (* print_stack_trace)())66 void SetPrintStackTrace(void (*print_stack_trace)()) {
67 g_print_stack_trace = print_stack_trace;
68 }
69
SetDcheckFunction(void (* dcheck_function)(const char *,int,const char *))70 void SetDcheckFunction(void (*dcheck_function)(const char*, int, const char*)) {
71 g_dcheck_function = dcheck_function ? dcheck_function : &DefaultDcheckHandler;
72 }
73
74 // Define specialization to pretty print characters (escaping non-printable
75 // characters) and to print c strings as pointers instead of strings.
76 #define DEFINE_PRINT_CHECK_OPERAND_CHAR(type) \
77 template <> \
78 std::string PrintCheckOperand<type>(type ch) { \
79 return PrettyPrintChar(ch); \
80 } \
81 template <> \
82 std::string PrintCheckOperand<type*>(type * cstr) { \
83 return PrintCheckOperand<void*>(cstr); \
84 } \
85 template <> \
86 std::string PrintCheckOperand<const type*>(const type* cstr) { \
87 return PrintCheckOperand<const void*>(cstr); \
88 }
89
90 DEFINE_PRINT_CHECK_OPERAND_CHAR(char)
91 DEFINE_PRINT_CHECK_OPERAND_CHAR(signed char)
92 DEFINE_PRINT_CHECK_OPERAND_CHAR(unsigned char)
93 #undef DEFINE_PRINT_CHECK_OPERAND_CHAR
94
95 // Explicit instantiations for commonly used comparisons.
96 #define DEFINE_MAKE_CHECK_OP_STRING(type) \
97 template std::string* MakeCheckOpString<type, type>(type, type, \
98 char const*); \
99 template std::string PrintCheckOperand<type>(type);
100 DEFINE_MAKE_CHECK_OP_STRING(int)
101 DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int)
102 DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int)
103 DEFINE_MAKE_CHECK_OP_STRING(unsigned int)
104 DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int)
105 DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int)
106 DEFINE_MAKE_CHECK_OP_STRING(void const*)
107 #undef DEFINE_MAKE_CHECK_OP_STRING
108
109 } // namespace base
110 } // namespace v8
111
112 namespace {
113
114 // FailureMessage is a stack allocated object which has a special marker field
115 // at the start and at the end. This makes it possible to retrieve the embedded
116 // message from the stack.
117 //
118 class FailureMessage {
119 public:
FailureMessage(const char * format,va_list arguments)120 explicit FailureMessage(const char* format, va_list arguments) {
121 memset(&message_, 0, arraysize(message_));
122 v8::base::OS::VSNPrintF(&message_[0], arraysize(message_), format,
123 arguments);
124 }
125
126 static const uintptr_t kStartMarker = 0xdecade10;
127 static const uintptr_t kEndMarker = 0xdecade11;
128 static const int kMessageBufferSize = 512;
129
130 uintptr_t start_marker_ = kStartMarker;
131 char message_[kMessageBufferSize];
132 uintptr_t end_marker_ = kEndMarker;
133 };
134
135 } // namespace
136
137 #ifdef DEBUG
V8_Fatal(const char * file,int line,const char * format,...)138 void V8_Fatal(const char* file, int line, const char* format, ...) {
139 #else
140 void V8_Fatal(const char* format, ...) {
141 const char* file = "";
142 int line = 0;
143 #endif
144 va_list arguments;
145 va_start(arguments, format);
146 // Format the error message into a stack object for later retrieveal by the
147 // crash processor.
148 FailureMessage message(format, arguments);
149 va_end(arguments);
150
151 fflush(stdout);
152 fflush(stderr);
153 // Print the formatted message to stdout without cropping the output.
154 v8::base::OS::PrintError("\n\n#\n# Fatal error in %s, line %d\n# ", file,
155 line);
156
157 // Print the error message.
158 va_start(arguments, format);
159 v8::base::OS::VPrintError(format, arguments);
160 va_end(arguments);
161 // Print the message object's address to force stack allocation.
162 v8::base::OS::PrintError("\n#\n#\n#\n#FailureMessage Object: %p", &message);
163
164 if (v8::base::g_print_stack_trace) v8::base::g_print_stack_trace();
165
166 fflush(stderr);
167 v8::base::OS::Abort();
168 }
169
170 #if !defined(DEBUG) && defined(OFFICIAL_BUILD)
171 void V8_FatalNoContext() {
172 v8::base::OS::PrintError("V8 CHECK or FATAL\n");
173 if (v8::base::g_print_stack_trace) v8::base::g_print_stack_trace();
174 fflush(stderr);
175 v8::base::OS::Abort();
176 }
177 #endif
178
179 void V8_Dcheck(const char* file, int line, const char* message) {
180 v8::base::g_dcheck_function(file, line, message);
181 }
182