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