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 <stdarg.h>
29
30#include "v8.h"
31
32#include "platform.h"
33
34// TODO(isolates): is it necessary to lift this?
35static int fatal_error_handler_nesting_depth = 0;
36
37// Contains protection against recursive calls (faults while handling faults).
38extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) {
39  i::AllowHandleDereference allow_deref;
40  i::AllowDeferredHandleDereference allow_deferred_deref;
41  fflush(stdout);
42  fflush(stderr);
43  fatal_error_handler_nesting_depth++;
44  // First time we try to print an error message
45  if (fatal_error_handler_nesting_depth < 2) {
46    i::OS::PrintError("\n\n#\n# Fatal error in %s, line %d\n# ", file, line);
47    va_list arguments;
48    va_start(arguments, format);
49    i::OS::VPrintError(format, arguments);
50    va_end(arguments);
51    i::OS::PrintError("\n#\n");
52    i::OS::DumpBacktrace();
53  }
54  // First two times we may try to print a stack dump.
55  if (fatal_error_handler_nesting_depth < 3) {
56    if (i::FLAG_stack_trace_on_abort) {
57      // Call this one twice on double fault
58      i::Isolate::Current()->PrintStack(stderr);
59    }
60  }
61  i::OS::Abort();
62}
63
64
65void CheckEqualsHelper(const char* file,
66                       int line,
67                       const char* expected_source,
68                       v8::Handle<v8::Value> expected,
69                       const char* value_source,
70                       v8::Handle<v8::Value> value) {
71  if (!expected->Equals(value)) {
72    v8::String::Utf8Value value_str(value);
73    v8::String::Utf8Value expected_str(expected);
74    V8_Fatal(file, line,
75             "CHECK_EQ(%s, %s) failed\n#   Expected: %s\n#   Found: %s",
76             expected_source, value_source, *expected_str, *value_str);
77  }
78}
79
80
81void CheckNonEqualsHelper(const char* file,
82                          int line,
83                          const char* unexpected_source,
84                          v8::Handle<v8::Value> unexpected,
85                          const char* value_source,
86                          v8::Handle<v8::Value> value) {
87  if (unexpected->Equals(value)) {
88    v8::String::Utf8Value value_str(value);
89    V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n#   Value: %s",
90             unexpected_source, value_source, *value_str);
91  }
92}
93
94
95void API_Fatal(const char* location, const char* format, ...) {
96  i::OS::PrintError("\n#\n# Fatal error in %s\n# ", location);
97  va_list arguments;
98  va_start(arguments, format);
99  i::OS::VPrintError(format, arguments);
100  va_end(arguments);
101  i::OS::PrintError("\n#\n\n");
102  i::OS::Abort();
103}
104
105
106namespace v8 { namespace internal {
107
108  bool EnableSlowAsserts() { return FLAG_enable_slow_asserts; }
109
110  intptr_t HeapObjectTagMask() { return kHeapObjectTagMask; }
111
112} }  // namespace v8::internal
113
114