blob: ebab129f93630eac5d6f54b2ee16a709928f91ea [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include <cxxabi.h>
9#include <dlfcn.h>
10#include <execinfo.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#elif V8_OS_QNX
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include <backtrace.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013#endif // V8_LIBC_GLIBC || V8_OS_BSD
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014
15#include <cstdio>
16#include <cstdlib>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017
18#include "src/base/platform/platform.h"
19
20namespace v8 {
21namespace base {
22
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000023// 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*);
27DEFINE_MAKE_CHECK_OP_STRING(int)
28DEFINE_MAKE_CHECK_OP_STRING(long) // NOLINT(runtime/int)
29DEFINE_MAKE_CHECK_OP_STRING(long long) // NOLINT(runtime/int)
30DEFINE_MAKE_CHECK_OP_STRING(unsigned int)
31DEFINE_MAKE_CHECK_OP_STRING(unsigned long) // NOLINT(runtime/int)
32DEFINE_MAKE_CHECK_OP_STRING(unsigned long long) // NOLINT(runtime/int)
33DEFINE_MAKE_CHECK_OP_STRING(char const*)
34DEFINE_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);
44DEFINE_CHECK_OP_IMPL(EQ)
45DEFINE_CHECK_OP_IMPL(NE)
46DEFINE_CHECK_OP_IMPL(LE)
47DEFINE_CHECK_OP_IMPL(LT)
48DEFINE_CHECK_OP_IMPL(GE)
49DEFINE_CHECK_OP_IMPL(GT)
50#undef DEFINE_CHECK_OP_IMPL
51
52
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053// Attempts to dump a backtrace (if supported).
54void DumpBacktrace() {
55#if V8_LIBC_GLIBC || V8_OS_BSD
56 void* trace[100];
57 int size = backtrace(trace, arraysize(trace));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 OS::PrintError("\n==== C stack trace ===============================\n\n");
59 if (size == 0) {
60 OS::PrintError("(empty)\n");
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 } else {
62 for (int i = 1; i < size; ++i) {
63 OS::PrintError("%2d: ", i);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064 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);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070 free(demangled);
71 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072 OS::PrintError("%s\n", info.dli_sname);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 }
74 }
75 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076#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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099} // namespace base
100} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101
102
103// Contains protection against recursive calls (faults while handling faults).
104extern "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}
Ben Murdochda12d292016-06-02 14:46:10 +0100118
119extern "C" void V8_RuntimeError(const char* file, int line,
120 const char* message) {
121 fflush(stdout);
122 fflush(stderr);
123 v8::base::OS::PrintError("\n\n#\n# Runtime error in %s, line %d\n# ", file,
124 line);
125 v8::base::OS::PrintError("\n# %s\n", message);
126 v8::base::DumpBacktrace();
127 fflush(stderr);
128}