This source file includes following definitions.
- StackDumpExceptionFilter
- GetInstance
- init_error
- OutputTraceToStream
- EnableInProcessStackDumping
- Print
- OutputToStream
#include "base/debug/stack_trace.h"
#include <windows.h>
#include <dbghelp.h>
#include <iostream>
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/strings/string_util.h"
#include "base/synchronization/lock.h"
#include "base/win/windows_version.h"
namespace base {
namespace debug {
namespace {
LPTOP_LEVEL_EXCEPTION_FILTER g_previous_filter = NULL;
long WINAPI StackDumpExceptionFilter(EXCEPTION_POINTERS* info) {
debug::StackTrace(info).Print();
if (g_previous_filter)
return g_previous_filter(info);
return EXCEPTION_CONTINUE_SEARCH;
}
class SymbolContext {
public:
static SymbolContext* GetInstance() {
return
Singleton<SymbolContext, LeakySingletonTraits<SymbolContext> >::get();
}
DWORD init_error() const {
return init_error_;
}
void OutputTraceToStream(const void* const* trace,
size_t count,
std::ostream* os) {
base::AutoLock lock(lock_);
for (size_t i = 0; (i < count) && os->good(); ++i) {
const int kMaxNameLength = 256;
DWORD_PTR frame = reinterpret_cast<DWORD_PTR>(trace[i]);
ULONG64 buffer[
(sizeof(SYMBOL_INFO) +
kMaxNameLength * sizeof(wchar_t) +
sizeof(ULONG64) - 1) /
sizeof(ULONG64)];
memset(buffer, 0, sizeof(buffer));
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);
DWORD line_displacement = 0;
IMAGEHLP_LINE64 line = {};
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
BOOL has_line = SymGetLineFromAddr64(GetCurrentProcess(), frame,
&line_displacement, &line);
(*os) << "\t";
if (has_symbol) {
(*os) << symbol->Name << " [0x" << trace[i] << "+"
<< sym_displacement << "]";
} else {
(*os) << "(No symbol) [0x" << trace[i] << "]";
}
if (has_line) {
(*os) << " (" << line.FileName << ":" << line.LineNumber << ")";
}
(*os) << "\n";
}
}
private:
friend struct DefaultSingletonTraits<SymbolContext>;
SymbolContext() : init_error_(ERROR_SUCCESS) {
SymSetOptions(SYMOPT_DEFERRED_LOADS |
SYMOPT_UNDNAME |
SYMOPT_LOAD_LINES);
if (!SymInitialize(GetCurrentProcess(), NULL, TRUE)) {
init_error_ = GetLastError();
DLOG(ERROR) << "SymInitialize failed: " << init_error_;
return;
}
init_error_ = ERROR_SUCCESS;
if (base::win::GetVersion() < base::win::VERSION_VISTA)
return;
wchar_t symbols_path[1024];
if (!SymGetSearchPathW(GetCurrentProcess(),
symbols_path,
arraysize(symbols_path))) {
DLOG(WARNING) << "SymGetSearchPath failed: ";
return;
}
FilePath module_path;
if (!PathService::Get(FILE_EXE, &module_path)) {
DLOG(WARNING) << "PathService::Get(FILE_EXE) failed.";
return;
}
std::wstring new_path(std::wstring(symbols_path) +
L";" + module_path.DirName().value());
if (!SymSetSearchPathW(GetCurrentProcess(), new_path.c_str())) {
DLOG(WARNING) << "SymSetSearchPath failed.";
return;
}
}
DWORD init_error_;
base::Lock lock_;
DISALLOW_COPY_AND_ASSIGN(SymbolContext);
};
}
bool EnableInProcessStackDumping() {
g_previous_filter = SetUnhandledExceptionFilter(&StackDumpExceptionFilter);
RouteStdioToConsole();
return true;
}
#if defined(COMPILER_MSVC)
#pragma optimize("", off)
#endif
StackTrace::StackTrace() {
count_ = CaptureStackBackTrace(0, arraysize(trace_), trace_, NULL);
}
#if defined(COMPILER_MSVC)
#pragma optimize("", on)
#endif
StackTrace::StackTrace(EXCEPTION_POINTERS* exception_pointers) {
count_ = 0;
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);
}
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 {
SymbolContext* context = SymbolContext::GetInstance();
DWORD error = context->init_error();
if (error != ERROR_SUCCESS) {
(*os) << "Error initializing symbols (" << error
<< "). Dumping unresolved backtrace:\n";
for (int i = 0; (i < count_) && os->good(); ++i) {
(*os) << "\t" << trace_[i] << "\n";
}
} else {
(*os) << "Backtrace:\n";
context->OutputTraceToStream(trace_, count_, os);
}
}
}
}