Replace DumpBacktrace with Chromium's StackTrace implementation.

Adds support for dumping the stack on Windows. Also enables in-process
stack dumping in d8 to dump the stack on exceptions and signals.

This CL changes the format of stack dumps from:
 1: V8_Fatal
 2: 0x1ac6ba5
 3: v8::internal::interpreter::BytecodeGenerator::Visit(v8::internal::AstNode*)
 4: v8::internal::interpreter::BytecodeGenerator::VisitForAccumulatorValue(v8::internal::Expression*)
 ...

To:
  ./out/x64.debug/d8(v8::base::debug::StackTrace::StackTrace()+0x1e) [0x1c6ee5e]
  ./out/x64.debug/d8() [0x1c6ede5]
  /lib/x86_64-linux-gnu/libpthread.so.0(+0x10330) [0x7fa01193e330]
  ./out/x64.debug/d8(v8::base::OS::Abort()+0x12) [0x1c6cea2]
  ./out/x64.debug/d8() [0x1c67538]
  ./out/x64.debug/d8() [0x1ac80b5]
  ./out/x64.debug/d8(v8::internal::interpreter::BytecodeGenerator
::Visit(v8::internal::AstNode*)+0x3cb) [0x1ac323b]
  ./out/x64.debug/d8(v8::internal::interpreter::BytecodeGenerator
::VisitForAccumulatorValue(v8::internal::Expression*)+0x40) [0x1ac2570]

Review-Url: https://codereview.chromium.org/2248393002
Cr-Commit-Position: refs/heads/master@{#38717}
This commit is contained in:
rmcilroy 2016-08-18 07:25:19 -07:00 committed by Commit bot
parent 1c2c2f43cd
commit 49c14f63ef
10 changed files with 1015 additions and 71 deletions

View File

@ -2036,12 +2036,15 @@ v8_source_set("v8_libbase") {
"src/base/build_config.h",
"src/base/cpu.cc",
"src/base/cpu.h",
"src/base/debug/stack_trace.cc",
"src/base/debug/stack_trace.h",
"src/base/division-by-constant.cc",
"src/base/division-by-constant.h",
"src/base/file-utils.cc",
"src/base/file-utils.h",
"src/base/flags.h",
"src/base/format-macros.h",
"src/base/free_deleter.h",
"src/base/functional.cc",
"src/base/functional.h",
"src/base/hashmap.h",
@ -2079,11 +2082,16 @@ v8_source_set("v8_libbase") {
defines = []
if (is_posix) {
sources += [ "src/base/platform/platform-posix.cc" ]
sources += [
"src/base/platform/platform-posix.cc",
]
}
if (is_linux) {
sources += [ "src/base/platform/platform-linux.cc" ]
sources += [
"src/base/debug/stack_trace_posix.cc",
"src/base/platform/platform-linux.cc",
]
libs = [
"dl",
@ -2096,18 +2104,31 @@ v8_source_set("v8_libbase") {
"rt",
]
if (host_os == "mac") {
sources += [ "src/base/platform/platform-macos.cc" ]
sources += [
"src/base/debug/stack_trace_posix.cc",
"src/base/platform/platform-macos.cc",
]
} else {
sources += [ "src/base/platform/platform-linux.cc" ]
sources += [
"src/base/debug/stack_trace_posix.cc",
"src/base/platform/platform-linux.cc",
]
}
} else {
sources += [ "src/base/platform/platform-linux.cc" ]
sources += [
"src/base/debug/stack_trace_android.cc",
"src/base/platform/platform-linux.cc",
]
}
} else if (is_mac) {
sources += [ "src/base/platform/platform-macos.cc" ]
sources += [
"src/base/debug/stack_trace_posix.cc",
"src/base/platform/platform-macos.cc",
]
} else if (is_win) {
# TODO(jochen): Add support for cygwin.
sources += [
"src/base/debug/stack_trace_win.cc",
"src/base/platform/platform-win32.cc",
"src/base/win32-headers.h",
]
@ -2115,6 +2136,8 @@ v8_source_set("v8_libbase") {
defines += [ "_CRT_RAND_S" ] # for rand_s()
libs = [
"dbghelp.lib",
"shlwapi.lib",
"winmm.lib",
"ws2_32.lib",
]

View File

@ -0,0 +1,40 @@
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/base/debug/stack_trace.h"
#include <string.h>
#include <algorithm>
#include <sstream>
#include "src/base/macros.h"
namespace v8 {
namespace base {
namespace debug {
StackTrace::StackTrace(const void* const* trace, size_t count) {
count = std::min(count, arraysize(trace_));
if (count) memcpy(trace_, trace, count * sizeof(trace_[0]));
count_ = count;
}
StackTrace::~StackTrace() {}
const void* const* StackTrace::Addresses(size_t* count) const {
*count = count_;
if (count_) return trace_;
return NULL;
}
std::string StackTrace::ToString() const {
std::stringstream stream;
OutputToStream(&stream);
return stream.str();
}
} // namespace debug
} // namespace base
} // namespace v8

View File

@ -0,0 +1,96 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Slightly adapted for inclusion in V8.
// Copyright 2016 the V8 project authors. All rights reserved.
#ifndef V8_BASE_DEBUG_STACK_TRACE_H_
#define V8_BASE_DEBUG_STACK_TRACE_H_
#include <stddef.h>
#include <iosfwd>
#include <string>
#include "src/base/build_config.h"
#if V8_OS_POSIX
#include <unistd.h>
#endif
#if V8_OS_WIN
struct _EXCEPTION_POINTERS;
struct _CONTEXT;
#endif
namespace v8 {
namespace base {
namespace debug {
// Enables stack dump to console output on exception and signals.
// When enabled, the process will quit immediately. This is meant to be used in
// tests only!
bool EnableInProcessStackDumping();
void DisableSignalStackDump();
// A stacktrace can be helpful in debugging. For example, you can include a
// stacktrace member in a object (probably around #ifndef NDEBUG) so that you
// can later see where the given object was created from.
class StackTrace {
public:
// Creates a stacktrace from the current location.
StackTrace();
// Creates a stacktrace from an existing array of instruction
// pointers (such as returned by Addresses()). |count| will be
// trimmed to |kMaxTraces|.
StackTrace(const void* const* trace, size_t count);
#if V8_OS_WIN
// Creates a stacktrace for an exception.
// Note: this function will throw an import not found (StackWalk64) exception
// on system without dbghelp 5.1.
explicit StackTrace(_EXCEPTION_POINTERS* exception_pointers);
explicit StackTrace(const _CONTEXT* context);
#endif
// Copying and assignment are allowed with the default functions.
~StackTrace();
// Gets an array of instruction pointer values. |*count| will be set to the
// number of elements in the returned array.
const void* const* Addresses(size_t* count) const;
// Prints the stack trace to stderr.
void Print() const;
// Resolves backtrace to symbols and write to stream.
void OutputToStream(std::ostream* os) const;
// Resolves backtrace to symbols and returns as string.
std::string ToString() const;
private:
#if V8_OS_WIN
void InitTrace(const _CONTEXT* context_record);
#endif
// From http://msdn.microsoft.com/en-us/library/bb204633.aspx,
// the sum of FramesToSkip and FramesToCapture must be less than 63,
// so set it to 62. Even if on POSIX it could be a larger value, it usually
// doesn't give much more information.
static const int kMaxTraces = 62;
void* trace_[kMaxTraces];
// The number of valid frames in |trace_|.
size_t count_;
};
} // namespace debug
} // namespace base
} // namespace v8
#endif // V8_BASE_DEBUG_STACK_TRACE_H_

View File

@ -0,0 +1,91 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Slightly adapted for inclusion in V8.
// Copyright 2016 the V8 project authors. All rights reserved.
#include "src/base/debug/stack_trace.h"
#include <signal.h>
#include <stddef.h>
#include <string.h>
#include <unwind.h>
#include <src/base/platform/platform.h>
#include <iomanip>
#include <ostream>
namespace {
struct StackCrawlState {
StackCrawlState(uintptr_t* frames, size_t max_depth)
: frames(frames),
frame_count(0),
max_depth(max_depth),
have_skipped_self(false) {}
uintptr_t* frames;
size_t frame_count;
size_t max_depth;
bool have_skipped_self;
};
_Unwind_Reason_Code TraceStackFrame(_Unwind_Context* context, void* arg) {
StackCrawlState* state = static_cast<StackCrawlState*>(arg);
uintptr_t ip = _Unwind_GetIP(context);
// The first stack frame is this function itself. Skip it.
if (ip != 0 && !state->have_skipped_self) {
state->have_skipped_self = true;
return _URC_NO_REASON;
}
state->frames[state->frame_count++] = ip;
if (state->frame_count >= state->max_depth)
return _URC_END_OF_STACK;
return _URC_NO_REASON;
}
} // namespace
namespace v8 {
namespace base {
namespace debug {
bool EnableInProcessStackDumping() {
// When running in an application, our code typically expects SIGPIPE
// to be ignored. Therefore, when testing that same code, it should run
// with SIGPIPE ignored as well.
// TODO(phajdan.jr): De-duplicate this SIGPIPE code.
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_handler = SIG_IGN;
sigemptyset(&action.sa_mask);
return (sigaction(SIGPIPE, &action, NULL) == 0);
}
void DisableSignalStackDump() {
}
StackTrace::StackTrace() {
StackCrawlState state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces);
_Unwind_Backtrace(&TraceStackFrame, &state);
count_ = state.frame_count;
}
void StackTrace::Print() const {
std::string backtrace = ToString();
OS::Print("%s\n", backtrace.c_str());
}
void StackTrace::OutputToStream(std::ostream* os) const {
for (size_t i = 0; i < count_; ++i) {
*os << "#" << std::setw(2) << i << trace_[i] << "\n";
}
}
} // namespace debug
} // namespace base
} // namespace v8

View File

@ -0,0 +1,445 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Slightly adapted for inclusion in V8.
// Copyright 2016 the V8 project authors. All rights reserved.
#include "src/base/debug/stack_trace.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <map>
#include <memory>
#include <ostream>
#include <string>
#include <vector>
#if V8_LIBC_GLIBC || V8_OS_BSD
#include <cxxabi.h>
#include <execinfo.h>
#endif
#if V8_OS_MACOSX
#include <AvailabilityMacros.h>
#endif
#include "src/base/build_config.h"
#include "src/base/free_deleter.h"
#include "src/base/logging.h"
#include "src/base/macros.h"
namespace v8 {
namespace base {
namespace debug {
namespace internal {
// POSIX doesn't define any async-signal safe function for converting
// an integer to ASCII. We'll have to define our own version.
// itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the
// conversion was successful or NULL otherwise. It never writes more than "sz"
// bytes. Output will be truncated as needed, and a NUL character is always
// appended.
char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding);
} // namespace internal
namespace {
volatile sig_atomic_t in_signal_handler = 0;
bool dump_stack_in_signal_handler = 1;
// The prefix used for mangled symbols, per the Itanium C++ ABI:
// http://www.codesourcery.com/cxx-abi/abi.html#mangling
const char kMangledSymbolPrefix[] = "_Z";
// Characters that can be used for symbols, generated by Ruby:
// (('a'..'z').to_a+('A'..'Z').to_a+('0'..'9').to_a + ['_']).join
const char kSymbolCharacters[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
// Demangles C++ symbols in the given text. Example:
//
// "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]"
// =>
// "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]"
void DemangleSymbols(std::string* text) {
// Note: code in this function is NOT async-signal safe (std::string uses
// malloc internally).
#if V8_LIBC_GLIBC || V8_OS_BSD
std::string::size_type search_from = 0;
while (search_from < text->size()) {
// Look for the start of a mangled symbol, from search_from.
std::string::size_type mangled_start =
text->find(kMangledSymbolPrefix, search_from);
if (mangled_start == std::string::npos) {
break; // Mangled symbol not found.
}
// Look for the end of the mangled symbol.
std::string::size_type mangled_end =
text->find_first_not_of(kSymbolCharacters, mangled_start);
if (mangled_end == std::string::npos) {
mangled_end = text->size();
}
std::string mangled_symbol =
text->substr(mangled_start, mangled_end - mangled_start);
// Try to demangle the mangled symbol candidate.
int status = 0;
std::unique_ptr<char, FreeDeleter> demangled_symbol(
abi::__cxa_demangle(mangled_symbol.c_str(), NULL, 0, &status));
if (status == 0) { // Demangling is successful.
// Remove the mangled symbol.
text->erase(mangled_start, mangled_end - mangled_start);
// Insert the demangled symbol.
text->insert(mangled_start, demangled_symbol.get());
// Next time, we'll start right after the demangled symbol we inserted.
search_from = mangled_start + strlen(demangled_symbol.get());
} else {
// Failed to demangle. Retry after the "_Z" we just found.
search_from = mangled_start + 2;
}
}
#endif // V8_LIBC_GLIBC || V8_OS_BSD
}
class BacktraceOutputHandler {
public:
virtual void HandleOutput(const char* output) = 0;
protected:
virtual ~BacktraceOutputHandler() {}
};
void OutputPointer(void* pointer, BacktraceOutputHandler* handler) {
// This should be more than enough to store a 64-bit number in hex:
// 16 hex digits + 1 for null-terminator.
char buf[17] = {'\0'};
handler->HandleOutput("0x");
internal::itoa_r(reinterpret_cast<intptr_t>(pointer), buf, sizeof(buf), 16,
12);
handler->HandleOutput(buf);
}
void ProcessBacktrace(void* const* trace, size_t size,
BacktraceOutputHandler* handler) {
// NOTE: This code MUST be async-signal safe (it's used by in-process
// stack dumping signal handler). NO malloc or stdio is allowed here.
handler->HandleOutput("\n");
handler->HandleOutput("==== C stack trace ===============================\n");
handler->HandleOutput("\n");
bool printed = false;
// Below part is async-signal unsafe (uses malloc), so execute it only
// when we are not executing the signal handler.
if (in_signal_handler == 0) {
std::unique_ptr<char*, FreeDeleter> trace_symbols(
backtrace_symbols(trace, static_cast<int>(size)));
if (trace_symbols.get()) {
for (size_t i = 0; i < size; ++i) {
std::string trace_symbol = trace_symbols.get()[i];
DemangleSymbols(&trace_symbol);
handler->HandleOutput(" ");
handler->HandleOutput(trace_symbol.c_str());
handler->HandleOutput("\n");
}
printed = true;
}
}
if (!printed) {
for (size_t i = 0; i < size; ++i) {
handler->HandleOutput(" [");
OutputPointer(trace[i], handler);
handler->HandleOutput("]\n");
}
}
}
void PrintToStderr(const char* output) {
// NOTE: This code MUST be async-signal safe (it's used by in-process
// stack dumping signal handler). NO malloc or stdio is allowed here.
write(STDERR_FILENO, output, strlen(output));
}
void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) {
// NOTE: This code MUST be async-signal safe.
// NO malloc or stdio is allowed here.
// Record the fact that we are in the signal handler now, so that the rest
// of StackTrace can behave in an async-signal-safe manner.
in_signal_handler = 1;
PrintToStderr("Received signal ");
char buf[1024] = {0};
internal::itoa_r(signal, buf, sizeof(buf), 10, 0);
PrintToStderr(buf);
if (signal == SIGBUS) {
if (info->si_code == BUS_ADRALN)
PrintToStderr(" BUS_ADRALN ");
else if (info->si_code == BUS_ADRERR)
PrintToStderr(" BUS_ADRERR ");
else if (info->si_code == BUS_OBJERR)
PrintToStderr(" BUS_OBJERR ");
else
PrintToStderr(" <unknown> ");
} else if (signal == SIGFPE) {
if (info->si_code == FPE_FLTDIV)
PrintToStderr(" FPE_FLTDIV ");
else if (info->si_code == FPE_FLTINV)
PrintToStderr(" FPE_FLTINV ");
else if (info->si_code == FPE_FLTOVF)
PrintToStderr(" FPE_FLTOVF ");
else if (info->si_code == FPE_FLTRES)
PrintToStderr(" FPE_FLTRES ");
else if (info->si_code == FPE_FLTSUB)
PrintToStderr(" FPE_FLTSUB ");
else if (info->si_code == FPE_FLTUND)
PrintToStderr(" FPE_FLTUND ");
else if (info->si_code == FPE_INTDIV)
PrintToStderr(" FPE_INTDIV ");
else if (info->si_code == FPE_INTOVF)
PrintToStderr(" FPE_INTOVF ");
else
PrintToStderr(" <unknown> ");
} else if (signal == SIGILL) {
if (info->si_code == ILL_BADSTK)
PrintToStderr(" ILL_BADSTK ");
else if (info->si_code == ILL_COPROC)
PrintToStderr(" ILL_COPROC ");
else if (info->si_code == ILL_ILLOPN)
PrintToStderr(" ILL_ILLOPN ");
else if (info->si_code == ILL_ILLADR)
PrintToStderr(" ILL_ILLADR ");
else if (info->si_code == ILL_ILLTRP)
PrintToStderr(" ILL_ILLTRP ");
else if (info->si_code == ILL_PRVOPC)
PrintToStderr(" ILL_PRVOPC ");
else if (info->si_code == ILL_PRVREG)
PrintToStderr(" ILL_PRVREG ");
else
PrintToStderr(" <unknown> ");
} else if (signal == SIGSEGV) {
if (info->si_code == SEGV_MAPERR)
PrintToStderr(" SEGV_MAPERR ");
else if (info->si_code == SEGV_ACCERR)
PrintToStderr(" SEGV_ACCERR ");
else
PrintToStderr(" <unknown> ");
}
if (signal == SIGBUS || signal == SIGFPE || signal == SIGILL ||
signal == SIGSEGV) {
internal::itoa_r(reinterpret_cast<intptr_t>(info->si_addr), buf,
sizeof(buf), 16, 12);
PrintToStderr(buf);
}
PrintToStderr("\n");
if (dump_stack_in_signal_handler) {
debug::StackTrace().Print();
PrintToStderr("[end of stack trace]\n");
}
if (::signal(signal, SIG_DFL) == SIG_ERR) _exit(1);
}
class PrintBacktraceOutputHandler : public BacktraceOutputHandler {
public:
PrintBacktraceOutputHandler() {}
void HandleOutput(const char* output) override {
// NOTE: This code MUST be async-signal safe (it's used by in-process
// stack dumping signal handler). NO malloc or stdio is allowed here.
PrintToStderr(output);
}
private:
DISALLOW_COPY_AND_ASSIGN(PrintBacktraceOutputHandler);
};
class StreamBacktraceOutputHandler : public BacktraceOutputHandler {
public:
explicit StreamBacktraceOutputHandler(std::ostream* os) : os_(os) {}
void HandleOutput(const char* output) override { (*os_) << output; }
private:
std::ostream* os_;
DISALLOW_COPY_AND_ASSIGN(StreamBacktraceOutputHandler);
};
void WarmUpBacktrace() {
// Warm up stack trace infrastructure. It turns out that on the first
// call glibc initializes some internal data structures using pthread_once,
// and even backtrace() can call malloc(), leading to hangs.
//
// Example stack trace snippet (with tcmalloc):
//
// #8 0x0000000000a173b5 in tc_malloc
// at ./third_party/tcmalloc/chromium/src/debugallocation.cc:1161
// #9 0x00007ffff7de7900 in _dl_map_object_deps at dl-deps.c:517
// #10 0x00007ffff7ded8a9 in dl_open_worker at dl-open.c:262
// #11 0x00007ffff7de9176 in _dl_catch_error at dl-error.c:178
// #12 0x00007ffff7ded31a in _dl_open (file=0x7ffff625e298 "libgcc_s.so.1")
// at dl-open.c:639
// #13 0x00007ffff6215602 in do_dlopen at dl-libc.c:89
// #14 0x00007ffff7de9176 in _dl_catch_error at dl-error.c:178
// #15 0x00007ffff62156c4 in dlerror_run at dl-libc.c:48
// #16 __GI___libc_dlopen_mode at dl-libc.c:165
// #17 0x00007ffff61ef8f5 in init
// at ../sysdeps/x86_64/../ia64/backtrace.c:53
// #18 0x00007ffff6aad400 in pthread_once
// at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_once.S:104
// #19 0x00007ffff61efa14 in __GI___backtrace
// at ../sysdeps/x86_64/../ia64/backtrace.c:104
// #20 0x0000000000752a54 in base::debug::StackTrace::StackTrace
// at base/debug/stack_trace_posix.cc:175
// #21 0x00000000007a4ae5 in
// base::(anonymous namespace)::StackDumpSignalHandler
// at base/process_util_posix.cc:172
// #22 <signal handler called>
StackTrace stack_trace;
}
} // namespace
bool EnableInProcessStackDumping() {
// When running in an application, our code typically expects SIGPIPE
// to be ignored. Therefore, when testing that same code, it should run
// with SIGPIPE ignored as well.
struct sigaction sigpipe_action;
memset(&sigpipe_action, 0, sizeof(sigpipe_action));
sigpipe_action.sa_handler = SIG_IGN;
sigemptyset(&sigpipe_action.sa_mask);
bool success = (sigaction(SIGPIPE, &sigpipe_action, NULL) == 0);
// Avoid hangs during backtrace initialization, see above.
WarmUpBacktrace();
struct sigaction action;
memset(&action, 0, sizeof(action));
action.sa_flags = SA_RESETHAND | SA_SIGINFO;
action.sa_sigaction = &StackDumpSignalHandler;
sigemptyset(&action.sa_mask);
success &= (sigaction(SIGILL, &action, NULL) == 0);
success &= (sigaction(SIGABRT, &action, NULL) == 0);
success &= (sigaction(SIGFPE, &action, NULL) == 0);
success &= (sigaction(SIGBUS, &action, NULL) == 0);
success &= (sigaction(SIGSEGV, &action, NULL) == 0);
success &= (sigaction(SIGSYS, &action, NULL) == 0);
dump_stack_in_signal_handler = true;
return success;
}
void DisableSignalStackDump() {
dump_stack_in_signal_handler = false;
}
StackTrace::StackTrace() {
// NOTE: This code MUST be async-signal safe (it's used by in-process
// stack dumping signal handler). NO malloc or stdio is allowed here.
// Though the backtrace API man page does not list any possible negative
// return values, we take no chance.
count_ = static_cast<size_t>(backtrace(trace_, arraysize(trace_)));
}
void StackTrace::Print() const {
// NOTE: This code MUST be async-signal safe (it's used by in-process
// stack dumping signal handler). NO malloc or stdio is allowed here.
PrintBacktraceOutputHandler handler;
ProcessBacktrace(trace_, count_, &handler);
}
void StackTrace::OutputToStream(std::ostream* os) const {
StreamBacktraceOutputHandler handler(os);
ProcessBacktrace(trace_, count_, &handler);
}
namespace internal {
// NOTE: code from sandbox/linux/seccomp-bpf/demo.cc.
char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding) {
// Make sure we can write at least one NUL byte.
size_t n = 1;
if (n > sz) return NULL;
if (base < 2 || base > 16) {
buf[0] = '\000';
return NULL;
}
char* start = buf;
uintptr_t j = i;
// Handle negative numbers (only for base 10).
if (i < 0 && base == 10) {
// This does "j = -i" while avoiding integer overflow.
j = static_cast<uintptr_t>(-(i + 1)) + 1;
// Make sure we can write the '-' character.
if (++n > sz) {
buf[0] = '\000';
return NULL;
}
*start++ = '-';
}
// Loop until we have converted the entire number. Output at least one
// character (i.e. '0').
char* ptr = start;
do {
// Make sure there is still enough space left in our output buffer.
if (++n > sz) {
buf[0] = '\000';
return NULL;
}
// Output the next digit.
*ptr++ = "0123456789abcdef"[j % base];
j /= base;
if (padding > 0) padding--;
} while (j > 0 || padding > 0);
// Terminate the output with a NUL character.
*ptr = '\000';
// Conversion to ASCII actually resulted in the digits being in reverse
// order. We can't easily generate them in forward order, as we can't tell
// the number of characters needed until we are done converting.
// So, now, we reverse the string (except for the possible "-" sign).
while (--ptr > start) {
char ch = *ptr;
*ptr = *start;
*start++ = ch;
}
return buf;
}
} // namespace internal
} // namespace debug
} // namespace base
} // namespace v8

View File

@ -0,0 +1,248 @@
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Slightly adapted for inclusion in V8.
// Copyright 2016 the V8 project authors. All rights reserved.
#include "src/base/debug/stack_trace.h"
#include <windows.h>
#include <dbghelp.h>
#include <Shlwapi.h>
#include <stddef.h>
#include <iostream>
#include <memory>
#include "src/base/logging.h"
#include "src/base/macros.h"
namespace v8 {
namespace base {
namespace debug {
namespace {
// Previous unhandled filter. Will be called if not NULL when we intercept an
// exception. Only used in unit tests.
LPTOP_LEVEL_EXCEPTION_FILTER g_previous_filter = NULL;
bool g_dump_stack_in_signal_handler = true;
bool g_initialized_symbols = false;
DWORD g_init_error = ERROR_SUCCESS;
// Prints the exception call stack.
// This is the unit tests exception filter.
long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) { // NOLINT
if (g_dump_stack_in_signal_handler) {
debug::StackTrace(info).Print();
}
if (g_previous_filter) return g_previous_filter(info);
return EXCEPTION_CONTINUE_SEARCH;
}
void GetExePath(wchar_t* path_out) {
GetModuleFileName(NULL, path_out, MAX_PATH);
path_out[MAX_PATH - 1] = L'\0';
PathRemoveFileSpec(path_out);
}
bool InitializeSymbols() {
if (g_initialized_symbols) return g_init_error == ERROR_SUCCESS;
g_initialized_symbols = true;
// Defer symbol load until they're needed, use undecorated names, and get line
// numbers.
SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_UNDNAME | SYMOPT_LOAD_LINES);
if (!SymInitialize(GetCurrentProcess(), NULL, TRUE)) {
g_init_error = GetLastError();
// TODO(awong): Handle error: SymInitialize can fail with
// ERROR_INVALID_PARAMETER.
// When it fails, we should not call debugbreak since it kills the current
// process (prevents future tests from running or kills the browser
// process).
return false;
}
// When transferring the binaries e.g. between bots, path put
// into the executable will get off. To still retrieve symbols correctly,
// add the directory of the executable to symbol search path.
// All following errors are non-fatal.
const size_t kSymbolsArraySize = 1024;
std::unique_ptr<wchar_t[]> symbols_path(new wchar_t[kSymbolsArraySize]);
// Note: The below function takes buffer size as number of characters,
// not number of bytes!
if (!SymGetSearchPathW(GetCurrentProcess(), symbols_path.get(),
kSymbolsArraySize)) {
g_init_error = GetLastError();
return false;
}
wchar_t exe_path[MAX_PATH];
GetExePath(exe_path);
std::wstring new_path(std::wstring(symbols_path.get()) + L";" +
std::wstring(exe_path));
if (!SymSetSearchPathW(GetCurrentProcess(), new_path.c_str())) {
g_init_error = GetLastError();
return false;
}
g_init_error = ERROR_SUCCESS;
return true;
}
// For the given trace, attempts to resolve the symbols, and output a trace
// to the ostream os. The format for each line of the backtrace is:
//
// <tab>SymbolName[0xAddress+Offset] (FileName:LineNo)
//
// This function should only be called if Init() has been called. We do not
// LOG(FATAL) here because this code is called might be triggered by a
// LOG(FATAL) itself. Also, it should not be calling complex code that is
// extensible like PathService since that can in turn fire CHECKs.
void OutputTraceToStream(const void* const* trace, size_t count,
std::ostream* os) {
for (size_t i = 0; (i < count) && os->good(); ++i) {
const int kMaxNameLength = 256;
DWORD_PTR frame = reinterpret_cast<DWORD_PTR>(trace[i]);
// Code adapted from MSDN example:
// http://msdn.microsoft.com/en-us/library/ms680578(VS.85).aspx
ULONG64 buffer[(sizeof(SYMBOL_INFO) + kMaxNameLength * sizeof(wchar_t) +
sizeof(ULONG64) - 1) /
sizeof(ULONG64)];
memset(buffer, 0, sizeof(buffer));
// Initialize symbol information retrieval structures.
DWORD64 sym_displacement = 0;
PSYMBOL_INFO symbol = reinterpret_cast<PSYMBOL_INFO>(&buffer[0]);
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
symbol->MaxNameLen = kMaxNameLength - 1;
BOOL has_symbol =
SymFromAddr(GetCurrentProcess(), frame, &sym_displacement, symbol);
// Attempt to retrieve line number information.
DWORD line_displacement = 0;
IMAGEHLP_LINE64 line = {};
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
BOOL has_line = SymGetLineFromAddr64(GetCurrentProcess(), frame,
&line_displacement, &line);
// Output the backtrace line.
(*os) << "\t";
if (has_symbol) {
(*os) << symbol->Name << " [0x" << trace[i] << "+" << sym_displacement
<< "]";
} else {
// If there is no symbol information, add a spacer.
(*os) << "(No symbol) [0x" << trace[i] << "]";
}
if (has_line) {
(*os) << " (" << line.FileName << ":" << line.LineNumber << ")";
}
(*os) << "\n";
}
}
} // namespace
bool EnableInProcessStackDumping() {
// Add stack dumping support on exception on windows. Similar to OS_POSIX
// signal() handling in process_util_posix.cc.
g_previous_filter = SetUnhandledExceptionFilter(&StackDumpExceptionFilter);
g_dump_stack_in_signal_handler = true;
// Need to initialize symbols early in the process or else this fails on
// swarming (since symbols are in different directory than in the exes) and
// also release x64.
return InitializeSymbols();
}
void DisableSignalStackDump() {
g_dump_stack_in_signal_handler = false;
}
// Disable optimizations for the StackTrace::StackTrace function. It is
// important to disable at least frame pointer optimization ("y"), since
// that breaks CaptureStackBackTrace() and prevents StackTrace from working
// in Release builds (it may still be janky if other frames are using FPO,
// but at least it will make it further).
#if defined(COMPILER_MSVC)
#pragma optimize("", off)
#endif
StackTrace::StackTrace() {
// When walking our own stack, use CaptureStackBackTrace().
count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL);
}
#if defined(COMPILER_MSVC)
#pragma optimize("", on)
#endif
StackTrace::StackTrace(EXCEPTION_POINTERS* exception_pointers) {
InitTrace(exception_pointers->ContextRecord);
}
StackTrace::StackTrace(const CONTEXT* context) { InitTrace(context); }
void StackTrace::InitTrace(const CONTEXT* context_record) {
// StackWalk64 modifies the register context in place, so we have to copy it
// so that downstream exception handlers get the right context. The incoming
// context may have had more register state (YMM, etc) than we need to unwind
// the stack. Typically StackWalk64 only needs integer and control registers.
CONTEXT context_copy;
memcpy(&context_copy, context_record, sizeof(context_copy));
context_copy.ContextFlags = CONTEXT_INTEGER | CONTEXT_CONTROL;
// When walking an exception stack, we need to use StackWalk64().
count_ = 0;
// Initialize stack walking.
STACKFRAME64 stack_frame;
memset(&stack_frame, 0, sizeof(stack_frame));
#if defined(_WIN64)
int machine_type = IMAGE_FILE_MACHINE_AMD64;
stack_frame.AddrPC.Offset = context_record->Rip;
stack_frame.AddrFrame.Offset = context_record->Rbp;
stack_frame.AddrStack.Offset = context_record->Rsp;
#else
int machine_type = IMAGE_FILE_MACHINE_I386;
stack_frame.AddrPC.Offset = context_record->Eip;
stack_frame.AddrFrame.Offset = context_record->Ebp;
stack_frame.AddrStack.Offset = context_record->Esp;
#endif
stack_frame.AddrPC.Mode = AddrModeFlat;
stack_frame.AddrFrame.Mode = AddrModeFlat;
stack_frame.AddrStack.Mode = AddrModeFlat;
while (StackWalk64(machine_type, GetCurrentProcess(), GetCurrentThread(),
&stack_frame, &context_copy, NULL,
&SymFunctionTableAccess64, &SymGetModuleBase64, NULL) &&
count_ < arraysize(trace_)) {
trace_[count_++] = reinterpret_cast<void*>(stack_frame.AddrPC.Offset);
}
for (size_t i = count_; i < arraysize(trace_); ++i) trace_[i] = NULL;
}
void StackTrace::Print() const { OutputToStream(&std::cerr); }
void StackTrace::OutputToStream(std::ostream* os) const {
InitializeSymbols();
if (g_init_error != ERROR_SUCCESS) {
(*os) << "Error initializing symbols (" << g_init_error
<< "). Dumping unresolved backtrace:\n";
for (size_t i = 0; (i < count_) && os->good(); ++i) {
(*os) << "\t" << trace_[i] << "\n";
}
} else {
(*os) << "\n";
(*os) << "==== C stack trace ===============================\n";
(*os) << "\n";
OutputTraceToStream(trace_, count_, os);
}
}
} // namespace debug
} // namespace base
} // namespace v8

28
src/base/free_deleter.h Normal file
View File

@ -0,0 +1,28 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Slightly adapted for inclusion in V8.
// Copyright 2016 the V8 project authors. All rights reserved.
#ifndef V8_BASE_FREE_DELETER_H_
#define V8_BASE_FREE_DELETER_H_
#include <stdlib.h>
namespace v8 {
namespace base {
// Function object which invokes 'free' on its parameter, which must be
// a pointer. Can be used to store malloc-allocated pointers in std::unique_ptr:
//
// std::unique_ptr<int, base::FreeDeleter> foo_ptr(
// static_cast<int*>(malloc(sizeof(int))));
struct FreeDeleter {
inline void operator()(void* ptr) const { free(ptr); }
};
} // namespace base
} // namespace v8
#endif // V8_BASE_FREE_DELETER_H_

View File

@ -4,17 +4,10 @@
#include "src/base/logging.h"
#if V8_LIBC_GLIBC || V8_OS_BSD
#include <cxxabi.h>
#include <dlfcn.h>
#include <execinfo.h>
#elif V8_OS_QNX
#include <backtrace.h>
#endif // V8_LIBC_GLIBC || V8_OS_BSD
#include <cstdio>
#include <cstdlib>
#include "src/base/debug/stack_trace.h"
#include "src/base/platform/platform.h"
namespace v8 {
@ -49,53 +42,6 @@ DEFINE_CHECK_OP_IMPL(GE)
DEFINE_CHECK_OP_IMPL(GT)
#undef DEFINE_CHECK_OP_IMPL
// Attempts to dump a backtrace (if supported).
void DumpBacktrace() {
#if V8_LIBC_GLIBC || V8_OS_BSD
void* trace[100];
int size = backtrace(trace, arraysize(trace));
OS::PrintError("\n==== C stack trace ===============================\n\n");
if (size == 0) {
OS::PrintError("(empty)\n");
} else {
for (int i = 1; i < size; ++i) {
OS::PrintError("%2d: ", i);
Dl_info info;
char* demangled = NULL;
if (!dladdr(trace[i], &info) || !info.dli_sname) {
OS::PrintError("%p\n", trace[i]);
} else if ((demangled = abi::__cxa_demangle(info.dli_sname, 0, 0, 0))) {
OS::PrintError("%s\n", demangled);
free(demangled);
} else {
OS::PrintError("%s\n", info.dli_sname);
}
}
}
#elif V8_OS_QNX
char out[1024];
bt_accessor_t acc;
bt_memmap_t memmap;
bt_init_accessor(&acc, BT_SELF);
bt_load_memmap(&acc, &memmap);
bt_sprn_memmap(&memmap, out, sizeof(out));
OS::PrintError(out);
bt_addr_t trace[100];
int size = bt_get_backtrace(&acc, trace, arraysize(trace));
OS::PrintError("\n==== C stack trace ===============================\n\n");
if (size == 0) {
OS::PrintError("(empty)\n");
} else {
bt_sprnf_addrs(&memmap, trace, size, const_cast<char*>("%a\n"),
out, sizeof(out), NULL);
OS::PrintError(out);
}
bt_unload_memmap(&memmap);
bt_release_accessor(&acc);
#endif // V8_LIBC_GLIBC || V8_OS_BSD
}
} // namespace base
} // namespace v8
@ -111,7 +57,12 @@ extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) {
v8::base::OS::VPrintError(format, arguments);
va_end(arguments);
v8::base::OS::PrintError("\n#\n");
v8::base::DumpBacktrace();
v8::base::debug::StackTrace trace;
trace.Print();
fflush(stderr);
// Avoid dumping stack trace on abort signal.
v8::base::debug::DisableSignalStackDump();
v8::base::OS::Abort();
}

View File

@ -39,6 +39,7 @@
#ifndef V8_SHARED
#include "src/api.h"
#include "src/base/cpu.h"
#include "src/base/debug/stack_trace.h"
#include "src/base/logging.h"
#include "src/base/platform/platform.h"
#include "src/base/sys-info.h"
@ -2538,6 +2539,9 @@ static void DumpHeapConstants(i::Isolate* isolate) {
int Shell::Main(int argc, char* argv[]) {
std::ofstream trace_file;
#ifndef V8_SHARED
v8::base::debug::EnableInProcessStackDumping();
#endif
#if (defined(_WIN32) || defined(_WIN64))
UINT new_flags =
SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX;

View File

@ -1767,10 +1767,13 @@
'base/cpu.h',
'base/division-by-constant.cc',
'base/division-by-constant.h',
'base/debug/stack_trace.cc',
'base/debug/stack_trace.h',
'base/file-utils.cc',
'base/file-utils.h',
'base/flags.h',
'base/format-macros.h',
'base/free_deleter.h',
'base/functional.cc',
'base/functional.h',
'base/hashmap.h',
@ -1817,14 +1820,16 @@
],
},
'sources': [
'base/debug/stack_trace_posix.cc',
'base/platform/platform-linux.cc',
'base/platform/platform-posix.cc'
'base/platform/platform-posix.cc',
],
}
],
['OS=="android"', {
'sources': [
'base/platform/platform-posix.cc'
'base/debug/stack_trace_android.cc',
'base/platform/platform-posix.cc',
],
'link_settings': {
'target_conditions': [
@ -1878,8 +1883,9 @@
],
},
'sources': [
'base/debug/stack_trace_posix.cc',
'base/platform/platform-posix.cc',
'base/qnx-math.h',
'base/qnx-math.h'
],
'target_conditions': [
['_toolset=="host" and host_os=="linux"', {
@ -1906,8 +1912,9 @@
'-L/usr/local/lib -lexecinfo',
]},
'sources': [
'base/debug/stack_trace_posix.cc',
'base/platform/platform-freebsd.cc',
'base/platform/platform-posix.cc'
'base/platform/platform-posix.cc',
],
}
],
@ -1928,8 +1935,9 @@
'-L/usr/pkg/lib -Wl,-R/usr/pkg/lib -lexecinfo',
]},
'sources': [
'base/debug/stack_trace_posix.cc',
'base/platform/platform-openbsd.cc',
'base/platform/platform-posix.cc'
'base/platform/platform-posix.cc',
],
}
],
@ -1945,15 +1953,17 @@
'-lnsl -lrt',
]},
'sources': [
'base/debug/stack_trace_posix.cc',
'base/platform/platform-solaris.cc',
'base/platform/platform-posix.cc'
'base/platform/platform-posix.cc',
],
}
],
['OS=="mac"', {
'sources': [
'base/debug/stack_trace_posix.cc',
'base/platform/platform-macos.cc',
'base/platform/platform-posix.cc'
'base/platform/platform-posix.cc',
]},
],
['OS=="win"', {
@ -1971,11 +1981,13 @@
'conditions': [
['build_env=="Cygwin"', {
'sources': [
'base/debug/stack_trace_posix.cc',
'base/platform/platform-cygwin.cc',
'base/platform/platform-posix.cc'
'base/platform/platform-posix.cc',
],
}, {
'sources': [
'base/debug/stack_trace_win.cc',
'base/platform/platform-win32.cc',
'base/win32-headers.h',
],
@ -1986,12 +1998,18 @@
},
}, {
'sources': [
'base/debug/stack_trace_win.cc',
'base/platform/platform-win32.cc',
'base/win32-headers.h',
],
'msvs_disabled_warnings': [4351, 4355, 4800],
'link_settings': {
'libraries': [ '-lwinmm.lib', '-lws2_32.lib' ],
'libraries': [
'-ldbghelp.lib',
'-lshlwapi.lib',
'-lwinmm.lib',
'-lws2_32.lib'
],
},
}],
],