V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
logging.cc
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 void PrettyPrintChar(std::ostream& os, int ch) {
27  switch (ch) {
28 #define CHAR_PRINT_CASE(ch) \
29  case ch: \
30  os << #ch; \
31  break;
32 
33  CHAR_PRINT_CASE('\0')
34  CHAR_PRINT_CASE('\'')
35  CHAR_PRINT_CASE('\\')
36  CHAR_PRINT_CASE('\a')
37  CHAR_PRINT_CASE('\b')
38  CHAR_PRINT_CASE('\f')
39  CHAR_PRINT_CASE('\n')
40  CHAR_PRINT_CASE('\r')
41  CHAR_PRINT_CASE('\t')
42  CHAR_PRINT_CASE('\v')
43 #undef CHAR_PRINT_CASE
44  default:
45  if (std::isprint(ch)) {
46  os << '\'' << ch << '\'';
47  } else {
48  auto flags = os.flags(std::ios_base::hex);
49  os << "\\x" << static_cast<unsigned int>(ch);
50  os.flags(flags);
51  }
52  }
53 }
54 
55 void DefaultDcheckHandler(const char* file, int line, const char* message) {
56  V8_Fatal(file, line, "Debug check failed: %s.", message);
57 }
58 
59 } // namespace
60 
61 void SetPrintStackTrace(void (*print_stack_trace)()) {
62  g_print_stack_trace = print_stack_trace;
63 }
64 
65 void SetDcheckFunction(void (*dcheck_function)(const char*, int, const char*)) {
66  g_dcheck_function = dcheck_function ? dcheck_function : &DefaultDcheckHandler;
67 }
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 DEFINE_PRINT_CHECK_OPERAND_CHAR(char)
86 DEFINE_PRINT_CHECK_OPERAND_CHAR(signed char)
87 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 namespace {
123 
124 // FailureMessage is a stack allocated object which has a special marker field
125 // at the start and at the end. This makes it possible to retrieve the embedded
126 // message from the stack.
127 //
128 class FailureMessage {
129  public:
130  explicit FailureMessage(const char* format, va_list arguments) {
131  memset(&message_, 0, arraysize(message_));
132  v8::base::OS::VSNPrintF(&message_[0], arraysize(message_), format,
133  arguments);
134  }
135 
136  static const uintptr_t kStartMarker = 0xdecade10;
137  static const uintptr_t kEndMarker = 0xdecade11;
138  static const int kMessageBufferSize = 512;
139 
140  uintptr_t start_marker_ = kStartMarker;
141  char message_[kMessageBufferSize];
142  uintptr_t end_marker_ = kEndMarker;
143 };
144 
145 } // namespace
146 
147 void V8_Fatal(const char* file, int line, const char* format, ...) {
148  va_list arguments;
149  va_start(arguments, format);
150  // Format the error message into a stack object for later retrieveal by the
151  // crash processor.
152  FailureMessage message(format, arguments);
153  va_end(arguments);
154 
155  fflush(stdout);
156  fflush(stderr);
157  // Print the formatted message to stdout without cropping the output.
158  v8::base::OS::PrintError("\n\n#\n# Fatal error in %s, line %d\n# ", file,
159  line);
160 
161  // Print the error message.
162  va_start(arguments, format);
163  v8::base::OS::VPrintError(format, arguments);
164  va_end(arguments);
165  // Print the message object's address to force stack allocation.
166  v8::base::OS::PrintError("\n#\n#\n#\n#FailureMessage Object: %p", &message);
167 
168  if (v8::base::g_print_stack_trace) v8::base::g_print_stack_trace();
169 
170  fflush(stderr);
171  v8::base::OS::Abort();
172 }
173 
174 void V8_Dcheck(const char* file, int line, const char* message) {
175  v8::base::g_dcheck_function(file, line, message);
176 }
Definition: libplatform.h:13