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 #if V8_LIBC_GLIBC || V8_OS_BSD
8 #include <cxxabi.h>
9 #include <dlfcn.h>
10 #include <execinfo.h>
11 #elif V8_OS_QNX
12 #include <backtrace.h>
13 #endif // V8_LIBC_GLIBC || V8_OS_BSD
14
15 #include <cstdio>
16 #include <cstdlib>
17
18 #include "src/base/platform/platform.h"
19
20 namespace v8 {
21 namespace base {
22
23 // Explicit instantiations for commonly used comparisons.
24 #define DEFINE_MAKE_CHECK_OP_STRING(type) \
25 template std::string* MakeCheckOpString<type, type>( \
26 type const&, type const&, char const*);
27 DEFINE_MAKE_CHECK_OP_STRING(int)
DEFINE_MAKE_CHECK_OP_STRING(long)28 DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int)
29 DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int)
30 DEFINE_MAKE_CHECK_OP_STRING(unsigned int)
31 DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int)
32 DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int)
33 DEFINE_MAKE_CHECK_OP_STRING(char const*)
34 DEFINE_MAKE_CHECK_OP_STRING(void const*)
35 #undef DEFINE_MAKE_CHECK_OP_STRING
36
37
38 // Explicit instantiations for floating point checks.
39 #define DEFINE_CHECK_OP_IMPL(NAME) \
40 template std::string* Check##NAME##Impl<float, float>( \
41 float const& lhs, float const& rhs, char const* msg); \
42 template std::string* Check##NAME##Impl<double, double>( \
43 double const& lhs, double const& rhs, char const* msg);
44 DEFINE_CHECK_OP_IMPL(EQ)
45 DEFINE_CHECK_OP_IMPL(NE)
46 DEFINE_CHECK_OP_IMPL(LE)
47 DEFINE_CHECK_OP_IMPL(LT)
48 DEFINE_CHECK_OP_IMPL(GE)
49 DEFINE_CHECK_OP_IMPL(GT)
50 #undef DEFINE_CHECK_OP_IMPL
51
52
53 // Attempts to dump a backtrace (if supported).
54 void DumpBacktrace() {
55 #if V8_LIBC_GLIBC || V8_OS_BSD
56 void* trace[100];
57 int size = backtrace(trace, arraysize(trace));
58 OS::PrintError("\n==== C stack trace ===============================\n\n");
59 if (size == 0) {
60 OS::PrintError("(empty)\n");
61 } else {
62 for (int i = 1; i < size; ++i) {
63 OS::PrintError("%2d: ", i);
64 Dl_info info;
65 char* demangled = NULL;
66 if (!dladdr(trace[i], &info) || !info.dli_sname) {
67 OS::PrintError("%p\n", trace[i]);
68 } else if ((demangled = abi::__cxa_demangle(info.dli_sname, 0, 0, 0))) {
69 OS::PrintError("%s\n", demangled);
70 free(demangled);
71 } else {
72 OS::PrintError("%s\n", info.dli_sname);
73 }
74 }
75 }
76 #elif V8_OS_QNX
77 char out[1024];
78 bt_accessor_t acc;
79 bt_memmap_t memmap;
80 bt_init_accessor(&acc, BT_SELF);
81 bt_load_memmap(&acc, &memmap);
82 bt_sprn_memmap(&memmap, out, sizeof(out));
83 OS::PrintError(out);
84 bt_addr_t trace[100];
85 int size = bt_get_backtrace(&acc, trace, arraysize(trace));
86 OS::PrintError("\n==== C stack trace ===============================\n\n");
87 if (size == 0) {
88 OS::PrintError("(empty)\n");
89 } else {
90 bt_sprnf_addrs(&memmap, trace, size, const_cast<char*>("%a\n"),
91 out, sizeof(out), NULL);
92 OS::PrintError(out);
93 }
94 bt_unload_memmap(&memmap);
95 bt_release_accessor(&acc);
96 #endif // V8_LIBC_GLIBC || V8_OS_BSD
97 }
98
99 } // namespace base
100 } // namespace v8
101
102
103 // Contains protection against recursive calls (faults while handling faults).
V8_Fatal(const char * file,int line,const char * format,...)104 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) {
105 fflush(stdout);
106 fflush(stderr);
107 v8::base::OS::PrintError("\n\n#\n# Fatal error in %s, line %d\n# ", file,
108 line);
109 va_list arguments;
110 va_start(arguments, format);
111 v8::base::OS::VPrintError(format, arguments);
112 va_end(arguments);
113 v8::base::OS::PrintError("\n#\n");
114 v8::base::DumpBacktrace();
115 fflush(stderr);
116 v8::base::OS::Abort();
117 }
118