Upstream version 6.35.121.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 namespace v8 {
42 namespace internal {
43
44 intptr_t HeapObjectTagMask() { return kHeapObjectTagMask; }
45
46 // Attempts to dump a backtrace (if supported).
47 void DumpBacktrace() {
48 #if V8_LIBC_GLIBC || V8_OS_BSD
49   void* trace[100];
50   int size = backtrace(trace, ARRAY_SIZE(trace));
51   char** symbols = backtrace_symbols(trace, size);
52   OS::PrintError("\n==== C stack trace ===============================\n\n");
53   if (size == 0) {
54     OS::PrintError("(empty)\n");
55   } else if (symbols == NULL) {
56     OS::PrintError("(no symbols)\n");
57   } else {
58     for (int i = 1; i < size; ++i) {
59       OS::PrintError("%2d: ", i);
60       char mangled[201];
61       if (sscanf(symbols[i], "%*[^(]%*[(]%200[^)+]", mangled) == 1) {  // NOLINT
62         int status;
63         size_t length;
64         char* demangled = abi::__cxa_demangle(mangled, NULL, &length, &status);
65         OS::PrintError("%s\n", demangled != NULL ? demangled : mangled);
66         free(demangled);
67       } else {
68         OS::PrintError("??\n");
69       }
70     }
71   }
72   free(symbols);
73 #elif V8_OS_QNX
74   char out[1024];
75   bt_accessor_t acc;
76   bt_memmap_t memmap;
77   bt_init_accessor(&acc, BT_SELF);
78   bt_load_memmap(&acc, &memmap);
79   bt_sprn_memmap(&memmap, out, sizeof(out));
80   OS::PrintError(out);
81   bt_addr_t trace[100];
82   int size = bt_get_backtrace(&acc, trace, ARRAY_SIZE(trace));
83   OS::PrintError("\n==== C stack trace ===============================\n\n");
84   if (size == 0) {
85     OS::PrintError("(empty)\n");
86   } else {
87     bt_sprnf_addrs(&memmap, trace, size, const_cast<char*>("%a\n"),
88                    out, sizeof(out), NULL);
89     OS::PrintError(out);
90   }
91   bt_unload_memmap(&memmap);
92   bt_release_accessor(&acc);
93 #endif  // V8_LIBC_GLIBC || V8_OS_BSD
94 }
95
96 } }  // namespace v8::internal
97
98
99 // Contains protection against recursive calls (faults while handling faults).
100 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) {
101   i::AllowHandleDereference allow_deref;
102   i::AllowDeferredHandleDereference allow_deferred_deref;
103   fflush(stdout);
104   fflush(stderr);
105   i::OS::PrintError("\n\n#\n# Fatal error in %s, line %d\n# ", file, line);
106   va_list arguments;
107   va_start(arguments, format);
108   i::OS::VPrintError(format, arguments);
109   va_end(arguments);
110   i::OS::PrintError("\n#\n");
111   v8::internal::DumpBacktrace();
112   fflush(stderr);
113   i::OS::Abort();
114 }
115
116
117 void CheckEqualsHelper(const char* file,
118                        int line,
119                        const char* expected_source,
120                        v8::Handle<v8::Value> expected,
121                        const char* value_source,
122                        v8::Handle<v8::Value> value) {
123   if (!expected->Equals(value)) {
124     v8::String::Utf8Value value_str(value);
125     v8::String::Utf8Value expected_str(expected);
126     V8_Fatal(file, line,
127              "CHECK_EQ(%s, %s) failed\n#   Expected: %s\n#   Found: %s",
128              expected_source, value_source, *expected_str, *value_str);
129   }
130 }
131
132
133 void CheckNonEqualsHelper(const char* file,
134                           int line,
135                           const char* unexpected_source,
136                           v8::Handle<v8::Value> unexpected,
137                           const char* value_source,
138                           v8::Handle<v8::Value> value) {
139   if (unexpected->Equals(value)) {
140     v8::String::Utf8Value value_str(value);
141     V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n#   Value: %s",
142              unexpected_source, value_source, *value_str);
143   }
144 }