summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormaruel@chromium.org <maruel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-08 23:53:53 +0000
committermaruel@chromium.org <maruel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-09-08 23:53:53 +0000
commit0fe31f2ba217cceb277b670e2812667c3aee8e80 (patch)
tree3d2adea86b5d61d297318ea60c3faf1b1636c996
parent2f66ed584522a2383f0f7193b264b3da7928eb82 (diff)
downloadchromium_src-0fe31f2ba217cceb277b670e2812667c3aee8e80.zip
chromium_src-0fe31f2ba217cceb277b670e2812667c3aee8e80.tar.gz
chromium_src-0fe31f2ba217cceb277b670e2812667c3aee8e80.tar.bz2
Print stack trace on exception in unit tests on Windows.
Also remove std::vector<> from StackTrace to reduce heap usage during potential unstable execution. TEST=none BUG=http://crbug.com/20996 Review URL: http://codereview.chromium.org/201050 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25685 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/debug_util.cc6
-rw-r--r--base/debug_util.h26
-rw-r--r--base/debug_util_posix.cc24
-rw-r--r--base/debug_util_win.cc56
-rw-r--r--base/test_suite.h20
5 files changed, 89 insertions, 43 deletions
diff --git a/base/debug_util.cc b/base/debug_util.cc
index 2db3aa7..2a6cac2 100644
--- a/base/debug_util.cc
+++ b/base/debug_util.cc
@@ -19,8 +19,8 @@ bool DebugUtil::WaitForDebugger(int wait_seconds, bool silent) {
}
const void *const *StackTrace::Addresses(size_t* count) {
- *count = trace_.size();
- if (trace_.size())
- return &trace_[0];
+ *count = count_;
+ if (count_)
+ return trace_;
return NULL;
}
diff --git a/base/debug_util.h b/base/debug_util.h
index de4a208..8ccb020 100644
--- a/base/debug_util.h
+++ b/base/debug_util.h
@@ -14,24 +14,40 @@
#include "base/basictypes.h"
+#if defined(OS_WIN)
+struct _EXCEPTION_POINTERS;
+#endif
+
// 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:
- // Create a stacktrace from the current location
+ // Creates a stacktrace from the current location
StackTrace();
- // Get an array of instruction pointer values.
+#if defined(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.
+ StackTrace(_EXCEPTION_POINTERS* exception_pointers);
+#endif
+ // Gets an array of instruction pointer values.
// count: (output) the number of elements in the returned array
const void *const *Addresses(size_t* count);
- // Print a backtrace to stderr
+ // Prints a backtrace to stderr
void PrintBacktrace();
- // Resolve backtrace to symbols and write to stream.
+ // Resolves backtrace to symbols and write to stream.
void OutputToStream(std::ostream* os);
private:
- std::vector<void*> trace_;
+ // 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 MAX_TRACES = 62;
+ void* trace_[MAX_TRACES];
+ int count_;
DISALLOW_EVIL_CONSTRUCTORS(StackTrace);
};
diff --git a/base/debug_util_posix.cc b/base/debug_util_posix.cc
index 84aebc3..dd22710 100644
--- a/base/debug_util_posix.cc
+++ b/base/debug_util_posix.cc
@@ -116,42 +116,30 @@ void DebugUtil::BreakDebugger() {
}
StackTrace::StackTrace() {
- const int kMaxCallers = 256;
-
- void* callers[kMaxCallers];
- int count = backtrace(callers, kMaxCallers);
-
// Though the backtrace API man page does not list any possible negative
- // return values, we still still exclude them because they would break the
- // memcpy code below.
- if (count > 0) {
- trace_.resize(count);
- memcpy(&trace_[0], callers, sizeof(callers[0]) * count);
- } else {
- trace_.resize(0);
- }
+ // return values, we take no chance.
+ count_ = std::max(backtrace(trace_, arraysize(trace_)), 0);
}
void StackTrace::PrintBacktrace() {
fflush(stderr);
- backtrace_symbols_fd(&trace_[0], trace_.size(), STDERR_FILENO);
+ backtrace_symbols_fd(trace_, count_, STDERR_FILENO);
}
void StackTrace::OutputToStream(std::ostream* os) {
- scoped_ptr_malloc<char*> trace_symbols(
- backtrace_symbols(&trace_[0], trace_.size()));
+ scoped_ptr_malloc<char*> trace_symbols(backtrace_symbols(trace_, count_));
// If we can't retrieve the symbols, print an error and just dump the raw
// addresses.
if (trace_symbols.get() == NULL) {
(*os) << "Unable get symbols for backtrace (" << strerror(errno)
<< "). Dumping raw addresses in trace:\n";
- for (size_t i = 0; i < trace_.size(); ++i) {
+ for (int i = 0; i < count_; ++i) {
(*os) << "\t" << trace_[i] << "\n";
}
} else {
(*os) << "Backtrace:\n";
- for (size_t i = 0; i < trace_.size(); ++i) {
+ for (int i = 0; i < count_; ++i) {
(*os) << "\t" << trace_symbols.get()[i] << "\n";
}
}
diff --git a/base/debug_util_win.cc b/base/debug_util_win.cc
index 08bfb41..8adbf8f 100644
--- a/base/debug_util_win.cc
+++ b/base/debug_util_win.cc
@@ -105,10 +105,12 @@ class SymbolContext {
// 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.
- void OutputTraceToStream(const std::vector<void*>& trace, std::ostream* os) {
+ void OutputTraceToStream(const void* const* trace,
+ int count,
+ std::ostream* os) {
AutoLock lock(lock_);
- for (size_t i = 0; (i < trace.size()) && os->good(); ++i) {
+ for (size_t i = 0; (i < count) && os->good(); ++i) {
const int kMaxNameLength = 256;
DWORD_PTR frame = reinterpret_cast<DWORD_PTR>(trace[i]);
@@ -220,19 +222,41 @@ void DebugUtil::BreakDebugger() {
}
StackTrace::StackTrace() {
- // From http://msdn.microsoft.com/en-us/library/bb204633(VS.85).aspx,
- // the sum of FramesToSkip and FramesToCapture must be less than 63,
- // so set it to 62.
- const int kMaxCallers = 62;
+ // When walking our own stack, use CaptureStackBackTrace().
+ count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL);
+}
- void* callers[kMaxCallers];
- // TODO(ajwong): Migrate this to StackWalk64.
- int count = CaptureStackBackTrace(0, kMaxCallers, callers, NULL);
- if (count > 0) {
- trace_.resize(count);
- memcpy(&trace_[0], callers, sizeof(callers[0]) * count);
- } else {
- trace_.resize(0);
+StackTrace::StackTrace(EXCEPTION_POINTERS* exception_pointers) {
+ // 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 = exception_pointers->ContextRecord->Rip;
+ stack_frame.AddrFrame.Offset = exception_pointers->ContextRecord->Rbp;
+ stack_frame.AddrStack.Offset = exception_pointers->ContextRecord->Rsp;
+#else
+ int machine_type = IMAGE_FILE_MACHINE_I386;
+ stack_frame.AddrPC.Offset = exception_pointers->ContextRecord->Eip;
+ stack_frame.AddrFrame.Offset = exception_pointers->ContextRecord->Ebp;
+ stack_frame.AddrStack.Offset = exception_pointers->ContextRecord->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,
+ exception_pointers->ContextRecord,
+ NULL,
+ &SymFunctionTableAccess64,
+ &SymGetModuleBase64,
+ NULL) &&
+ count_ < arraysize(trace_)) {
+ trace_[count_++] = reinterpret_cast<void*>(stack_frame.AddrPC.Offset);
}
}
@@ -246,11 +270,11 @@ void StackTrace::OutputToStream(std::ostream* os) {
if (error != ERROR_SUCCESS) {
(*os) << "Error initializing symbols (" << error
<< "). Dumping unresolved backtrace:\n";
- for (size_t i = 0; (i < trace_.size()) && os->good(); ++i) {
+ for (int i = 0; (i < count_) && os->good(); ++i) {
(*os) << "\t" << trace_[i] << "\n";
}
} else {
(*os) << "Backtrace:\n";
- context->OutputTraceToStream(trace_, os);
+ context->OutputTraceToStream(trace_, count_, os);
}
}
diff --git a/base/test_suite.h b/base/test_suite.h
index e616b80..4a90a23 100644
--- a/base/test_suite.h
+++ b/base/test_suite.h
@@ -40,7 +40,22 @@ static void TestSuiteCrashHandler(int signal) {
StackTrace().PrintBacktrace();
_exit(1);
}
-#endif
+#endif // OS_POSIX
+
+#if defined(OS_WIN)
+// Previous unhandled filter. Will be called if not NULL when we intercept an
+// exception.
+__declspec(selectany) LPTOP_LEVEL_EXCEPTION_FILTER g_previous_filter = NULL;
+
+// Prints the exception call stack.
+// This is the unit tests exception filter.
+inline long WINAPI UnitTestExceptionFilter(EXCEPTION_POINTERS* info) {
+ StackTrace(info).PrintBacktrace();
+ if (g_previous_filter)
+ return g_previous_filter(info);
+ return EXCEPTION_EXECUTE_HANDLER;
+}
+#endif // OS_WIN
class TestSuite {
public:
@@ -155,6 +170,9 @@ class TestSuite {
// As a hack workaround, just #ifdef out this code for Purify builds.
logging::SetLogAssertHandler(UnitTestAssertHandler);
#endif // !defined(PURIFY)
+ // Add stack dumping support on exception on windows. Similar to OS_POSIX
+ // signal() handling above.
+ g_previous_filter = SetUnhandledExceptionFilter(&UnitTestExceptionFilter);
}
#endif // defined(OS_WIN)