Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / v8 / src / checks.cc
1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above
9 //       copyright notice, this list of conditions and the following
10 //       disclaimer in the documentation and/or other materials provided
11 //       with the distribution.
12 //     * Neither the name of Google Inc. nor the names of its
13 //       contributors may be used to endorse or promote products derived
14 //       from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "checks.h"
29
30 #if V8_LIBC_GLIBC || V8_OS_BSD
31 # include <cxxabi.h>
32 # include <execinfo.h>
33 #elif V8_OS_QNX
34 # include <backtrace.h>
35 #endif  // V8_LIBC_GLIBC || V8_OS_BSD
36 #include <stdio.h>
37
38 #include "platform.h"
39 #include "v8.h"
40
41
42 // Attempts to dump a backtrace (if supported).
43 static V8_INLINE void DumpBacktrace() {
44 #if V8_LIBC_GLIBC || V8_OS_BSD
45   void* trace[100];
46   int size = backtrace(trace, ARRAY_SIZE(trace));
47   char** symbols = backtrace_symbols(trace, size);
48   i::OS::PrintError("\n==== C stack trace ===============================\n\n");
49   if (size == 0) {
50     i::OS::PrintError("(empty)\n");
51   } else if (symbols == NULL) {
52     i::OS::PrintError("(no symbols)\n");
53   } else {
54     for (int i = 1; i < size; ++i) {
55       i::OS::PrintError("%2d: ", i);
56       char mangled[201];
57       if (sscanf(symbols[i], "%*[^(]%*[(]%200[^)+]", mangled) == 1) {  // NOLINT
58         int status;
59         size_t length;
60         char* demangled = abi::__cxa_demangle(mangled, NULL, &length, &status);
61         i::OS::PrintError("%s\n", demangled != NULL ? demangled : mangled);
62         free(demangled);
63       } else {
64         i::OS::PrintError("??\n");
65       }
66     }
67   }
68   free(symbols);
69 #elif V8_OS_QNX
70   char out[1024];
71   bt_accessor_t acc;
72   bt_memmap_t memmap;
73   bt_init_accessor(&acc, BT_SELF);
74   bt_load_memmap(&acc, &memmap);
75   bt_sprn_memmap(&memmap, out, sizeof(out));
76   i::OS::PrintError(out);
77   bt_addr_t trace[100];
78   int size = bt_get_backtrace(&acc, trace, ARRAY_SIZE(trace));
79   i::OS::PrintError("\n==== C stack trace ===============================\n\n");
80   if (size == 0) {
81     i::OS::PrintError("(empty)\n");
82   } else {
83     bt_sprnf_addrs(&memmap, trace, size, const_cast<char*>("%a\n"),
84                    out, sizeof(out), NULL);
85     i::OS::PrintError(out);
86   }
87   bt_unload_memmap(&memmap);
88   bt_release_accessor(&acc);
89 #endif  // V8_LIBC_GLIBC || V8_OS_BSD
90 }
91
92
93 // Contains protection against recursive calls (faults while handling faults).
94 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) {
95   i::AllowHandleDereference allow_deref;
96   i::AllowDeferredHandleDereference allow_deferred_deref;
97   fflush(stdout);
98   fflush(stderr);
99   i::OS::PrintError("\n\n#\n# Fatal error in %s, line %d\n# ", file, line);
100   va_list arguments;
101   va_start(arguments, format);
102   i::OS::VPrintError(format, arguments);
103   va_end(arguments);
104   i::OS::PrintError("\n#\n");
105   DumpBacktrace();
106   fflush(stderr);
107   i::OS::Abort();
108 }
109
110
111 void CheckEqualsHelper(const char* file,
112                        int line,
113                        const char* expected_source,
114                        v8::Handle<v8::Value> expected,
115                        const char* value_source,
116                        v8::Handle<v8::Value> value) {
117   if (!expected->Equals(value)) {
118     v8::String::Utf8Value value_str(value);
119     v8::String::Utf8Value expected_str(expected);
120     V8_Fatal(file, line,
121              "CHECK_EQ(%s, %s) failed\n#   Expected: %s\n#   Found: %s",
122              expected_source, value_source, *expected_str, *value_str);
123   }
124 }
125
126
127 void CheckNonEqualsHelper(const char* file,
128                           int line,
129                           const char* unexpected_source,
130                           v8::Handle<v8::Value> unexpected,
131                           const char* value_source,
132                           v8::Handle<v8::Value> value) {
133   if (unexpected->Equals(value)) {
134     v8::String::Utf8Value value_str(value);
135     V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n#   Value: %s",
136              unexpected_source, value_source, *value_str);
137   }
138 }
139
140
141 namespace v8 { namespace internal {
142
143   intptr_t HeapObjectTagMask() { return kHeapObjectTagMask; }
144
145 } }  // namespace v8::internal