Home | History | Annotate | Download | only in src
      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/checks.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 #include <stdio.h>
     14 
     15 #include "src/platform.h"
     16 #include "src/v8.h"
     17 
     18 namespace v8 {
     19 namespace internal {
     20 
     21 intptr_t HeapObjectTagMask() { return kHeapObjectTagMask; }
     22 
     23 // Attempts to dump a backtrace (if supported).
     24 void DumpBacktrace() {
     25 #if V8_LIBC_GLIBC || V8_OS_BSD
     26   void* trace[100];
     27   int size = backtrace(trace, ARRAY_SIZE(trace));
     28   char** symbols = backtrace_symbols(trace, size);
     29   OS::PrintError("\n==== C stack trace ===============================\n\n");
     30   if (size == 0) {
     31     OS::PrintError("(empty)\n");
     32   } else if (symbols == NULL) {
     33     OS::PrintError("(no symbols)\n");
     34   } else {
     35     for (int i = 1; i < size; ++i) {
     36       OS::PrintError("%2d: ", i);
     37       char mangled[201];
     38       if (sscanf(symbols[i], "%*[^(]%*[(]%200[^)+]", mangled) == 1) {  // NOLINT
     39         int status;
     40         size_t length;
     41         char* demangled = abi::__cxa_demangle(mangled, NULL, &length, &status);
     42         OS::PrintError("%s\n", demangled != NULL ? demangled : mangled);
     43         free(demangled);
     44       } else {
     45         OS::PrintError("??\n");
     46       }
     47     }
     48   }
     49   free(symbols);
     50 #elif V8_OS_QNX
     51   char out[1024];
     52   bt_accessor_t acc;
     53   bt_memmap_t memmap;
     54   bt_init_accessor(&acc, BT_SELF);
     55   bt_load_memmap(&acc, &memmap);
     56   bt_sprn_memmap(&memmap, out, sizeof(out));
     57   OS::PrintError(out);
     58   bt_addr_t trace[100];
     59   int size = bt_get_backtrace(&acc, trace, ARRAY_SIZE(trace));
     60   OS::PrintError("\n==== C stack trace ===============================\n\n");
     61   if (size == 0) {
     62     OS::PrintError("(empty)\n");
     63   } else {
     64     bt_sprnf_addrs(&memmap, trace, size, const_cast<char*>("%a\n"),
     65                    out, sizeof(out), NULL);
     66     OS::PrintError(out);
     67   }
     68   bt_unload_memmap(&memmap);
     69   bt_release_accessor(&acc);
     70 #endif  // V8_LIBC_GLIBC || V8_OS_BSD
     71 }
     72 
     73 } }  // namespace v8::internal
     74 
     75 
     76 // Contains protection against recursive calls (faults while handling faults).
     77 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) {
     78   fflush(stdout);
     79   fflush(stderr);
     80   i::OS::PrintError("\n\n#\n# Fatal error in %s, line %d\n# ", file, line);
     81   va_list arguments;
     82   va_start(arguments, format);
     83   i::OS::VPrintError(format, arguments);
     84   va_end(arguments);
     85   i::OS::PrintError("\n#\n");
     86   v8::internal::DumpBacktrace();
     87   fflush(stderr);
     88   i::OS::Abort();
     89 }
     90 
     91 
     92 void CheckEqualsHelper(const char* file,
     93                        int line,
     94                        const char* expected_source,
     95                        v8::Handle<v8::Value> expected,
     96                        const char* value_source,
     97                        v8::Handle<v8::Value> value) {
     98   if (!expected->Equals(value)) {
     99     v8::String::Utf8Value value_str(value);
    100     v8::String::Utf8Value expected_str(expected);
    101     V8_Fatal(file, line,
    102              "CHECK_EQ(%s, %s) failed\n#   Expected: %s\n#   Found: %s",
    103              expected_source, value_source, *expected_str, *value_str);
    104   }
    105 }
    106 
    107 
    108 void CheckNonEqualsHelper(const char* file,
    109                           int line,
    110                           const char* unexpected_source,
    111                           v8::Handle<v8::Value> unexpected,
    112                           const char* value_source,
    113                           v8::Handle<v8::Value> value) {
    114   if (unexpected->Equals(value)) {
    115     v8::String::Utf8Value value_str(value);
    116     V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n#   Value: %s",
    117              unexpected_source, value_source, *value_str);
    118   }
    119 }
    120