Add support to the StackWalker for resolving symbols, using a

caller-implemented SymbolSupplier object to get a symbol file.

Add a CrashReportProcessor object which provides a simple API for processing
a CrashReport struct, given a SymbolSupplier and a minidump file.

r=mmentovai (#17))


git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@18 4c0a9323-5329-0410-9bdc-e9ce6186880e
This commit is contained in:
bryner 2006-09-08 02:35:53 +00:00
parent 39716226cf
commit d5e66382d1
17 changed files with 14492 additions and 3332 deletions

View file

@ -0,0 +1,72 @@
// Copyright (C) 2006 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "google/crash_report.h"
#include "google/crash_report_processor.h"
#include "processor/minidump.h"
#include "processor/stackwalker_x86.h"
namespace google_airbag {
CrashReportProcessor::CrashReportProcessor(SymbolSupplier *supplier)
: supplier_(supplier) {
}
CrashReportProcessor::~CrashReportProcessor() {
}
bool CrashReportProcessor::ProcessReport(CrashReport *report,
const string &minidump_file) {
int fd = open(minidump_file.c_str(), O_RDONLY);
if (fd == -1) {
return false;
}
Minidump dump(fd);
if (!dump.Read()) {
return false;
}
MinidumpException *exception = dump.GetException();
if (!exception) {
return false;
}
MinidumpThreadList *threads = dump.GetThreadList();
if (!threads) {
return false;
}
// TODO(bryner): get all the threads
MinidumpThread *thread = threads->GetThreadByID(exception->GetThreadID());
if (!thread) {
return false;
}
MinidumpMemoryRegion *thread_memory = thread->GetMemory();
if (!thread_memory) {
return false;
}
// TODO(bryner): figure out which StackWalker we want
StackwalkerX86 walker(exception->GetContext(), thread_memory,
dump.GetModuleList(), supplier_, report);
walker.Walk(&report->stack_frames);
return true;
}
} // namespace google_airbag

View file

@ -0,0 +1,107 @@
// Copyright (C) 2006 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Unit test for CrashReporter. Uses a pre-generated minidump and
// corresponding symbol file, and checks the contents of the CrashReport.
#include <string>
#include "google/crash_report.h"
#include "google/crash_report_processor.h"
#include "google/symbol_supplier.h"
#include "processor/minidump.h"
using std::string;
using google_airbag::CrashReportProcessor;
using google_airbag::CrashReport;
#define ASSERT_TRUE(cond) \
if (!(cond)) { \
fprintf(stderr, "FAILED: %s at %s:%d\n", #cond, __FILE__, __LINE__); \
return false; \
}
#define ASSERT_EQ(e1, e2) ASSERT_TRUE((e1) == (e2))
namespace google_airbag {
class TestSymbolSupplier : public SymbolSupplier {
public:
virtual string GetSymbolFile(MinidumpModule *module,
const CrashReport *report);
};
string TestSymbolSupplier::GetSymbolFile(MinidumpModule *module,
const CrashReport *report) {
if (*(module->GetName()) == "c:\\test_app.exe") {
return string(getenv("srcdir") ? getenv("srcdir") : ".") +
"/src/processor/testdata/minidump2.sym";
}
return "";
}
} // namespace google_airbag
using google_airbag::TestSymbolSupplier;
static bool RunTests() {
TestSymbolSupplier supplier;
CrashReportProcessor processor(&supplier);
CrashReport report;
string minidump_file = string(getenv("srcdir") ? getenv("srcdir") : ".") +
"/src/processor/testdata/minidump2.dmp";
ASSERT_TRUE(processor.ProcessReport(&report, minidump_file));
ASSERT_EQ(report.stack_frames.size(), 4);
ASSERT_EQ(report.stack_frames[0].module_base, 0x400000);
ASSERT_EQ(report.stack_frames[0].module_name, "c:\\test_app.exe");
ASSERT_EQ(report.stack_frames[0].function_name, "CrashFunction");
ASSERT_EQ(report.stack_frames[0].source_file_name, "c:\\test_app.cc");
ASSERT_EQ(report.stack_frames[0].source_line, 36);
ASSERT_EQ(report.stack_frames[1].module_base, 0x400000);
ASSERT_EQ(report.stack_frames[1].module_name, "c:\\test_app.exe");
ASSERT_EQ(report.stack_frames[1].function_name, "main");
ASSERT_EQ(report.stack_frames[1].source_file_name, "c:\\test_app.cc");
ASSERT_EQ(report.stack_frames[1].source_line, 42);
// This comes from the CRT
ASSERT_EQ(report.stack_frames[2].module_base, 0x400000);
ASSERT_EQ(report.stack_frames[2].module_name, "c:\\test_app.exe");
ASSERT_EQ(report.stack_frames[2].function_name, "__tmainCRTStartup");
ASSERT_EQ(report.stack_frames[2].source_file_name,
"f:\\rtm\\vctools\\crt_bld\\self_x86\\crt\\src\\crt0.c");
ASSERT_EQ(report.stack_frames[2].source_line, 318);
// No debug info available for kernel32.dll
ASSERT_EQ(report.stack_frames[3].module_base, 0x7c800000);
ASSERT_EQ(report.stack_frames[3].module_name,
"C:\\WINDOWS\\system32\\kernel32.dll");
ASSERT_TRUE(report.stack_frames[3].function_name.empty());
ASSERT_TRUE(report.stack_frames[3].source_file_name.empty());
ASSERT_EQ(report.stack_frames[3].source_line, 0);
return true;
}
int main(int argc, char *argv[]) {
if (!RunTests()) {
return 1;
}
return 0;
}

View file

@ -30,13 +30,10 @@
#define open _open
#endif // !_WIN32
#include <memory>
#include "processor/minidump.h"
#include "processor/stackwalker_x86.h"
using std::auto_ptr;
using namespace google_airbag;
@ -95,17 +92,15 @@ int main(int argc, char** argv) {
exit(1);
}
StackwalkerX86 stackwalker = StackwalkerX86(context, stack_memory, modules);
StackwalkerX86 stackwalker = StackwalkerX86(context, stack_memory,
modules, NULL, NULL);
auto_ptr<StackFrames> stack(stackwalker.Walk());
if (!stack.get()) {
fprintf(stderr, "stackwalker->Walk() failed\n");
exit(1);
}
StackFrames stack;
stackwalker.Walk(&stack);
unsigned int index;
for (index = 0 ; index < stack->size() ; index++) {
StackFrame frame = stack->at(index);
for (index = 0 ; index < stack.size() ; index++) {
StackFrame frame = stack.at(index);
printf("[%2d] ebp = 0x%08llx eip = 0x%08llx \"%s\" + 0x%08llx\n",
index,
frame.frame_pointer,

View file

@ -145,7 +145,7 @@ bool SourceLineResolver::LoadModule(const string &module_name,
void SourceLineResolver::FillSourceLineInfo(StackFrame *frame) const {
ModuleMap::const_iterator it = modules_->find(frame->module_name);
if (it != modules_->end()) {
it->second->LookupAddress(frame->instruction, frame);
it->second->LookupAddress(frame->instruction - frame->module_base, frame);
}
}

View file

@ -23,6 +23,8 @@
#include "processor/stackwalker.h"
#include "processor/minidump.h"
#include "processor/source_line_resolver.h"
#include "google/symbol_supplier.h"
namespace google_airbag {
@ -31,13 +33,19 @@ namespace google_airbag {
using std::auto_ptr;
Stackwalker::Stackwalker(MemoryRegion* memory, MinidumpModuleList* modules)
: memory_(memory), modules_(modules) {
Stackwalker::Stackwalker(MemoryRegion* memory, MinidumpModuleList* modules,
SymbolSupplier *supplier, const CrashReport *report)
: memory_(memory),
modules_(modules),
supplier_(supplier),
report_(report) {
}
StackFrames* Stackwalker::Walk() {
auto_ptr<StackFrames> frames(new StackFrames());
void Stackwalker::Walk(StackFrames *frames) {
frames->clear();
bool resolve_symbols = (modules_ && supplier_);
SourceLineResolver resolver;
// Begin with the context frame, and keep getting callers until there are
// no more.
@ -56,6 +64,13 @@ StackFrames* Stackwalker::Walk() {
if (module) {
frame->module_name = *(module->GetName());
frame->module_base = module->base_address();
if (resolve_symbols) {
string symbol_file = supplier_->GetSymbolFile(module, report_);
if (!symbol_file.empty()) {
resolver.LoadModule(*(module->GetName()), symbol_file);
}
resolver.FillSourceLineInfo(frame.get());
}
}
}
@ -70,8 +85,6 @@ StackFrames* Stackwalker::Walk() {
// Get the next frame.
valid = GetCallerFrame(frame.get());
}
return frames.release();
}

View file

@ -36,24 +36,31 @@ namespace google_airbag {
class MinidumpModuleList;
class SymbolSupplier;
struct CrashReport;
class Stackwalker {
public:
virtual ~Stackwalker() {}
// Produces a vector of StackFrames by calling GetContextFrame and
// Fills the given vector of StackFrames by calling GetContextFrame and
// GetCallerFrame, and populating the returned frames with module
// offset and name information if possible. The caller takes ownership
// of the StackFrames object and is responsible for freeing it.
StackFrames* Walk();
// offset and name information if possible.
void Walk(StackFrames *frames);
protected:
// memory identifies a MemoryRegion that provides the stack memory
// for the stack to walk. modules, if non-NULL, is a MinidumpModuleList
// that is used to look up which code module each stack frame is
// associated with.
Stackwalker(MemoryRegion* memory, MinidumpModuleList* modules);
// associated with. supplier is an optional caller-supplied SymbolSupplier
// implementation. If supplier is NULL, source line info will not be
// resolved. The CrashReport object will be passed to the SymbolSupplier's
// GetSymbolFile method.
Stackwalker(MemoryRegion* memory,
MinidumpModuleList* modules,
SymbolSupplier* supplier,
const CrashReport* report);
// The stack memory to walk. Subclasses will require this region to
// get information from the stack.
@ -74,6 +81,12 @@ class Stackwalker {
// A list of modules, for populating each StackFrame's module information.
// This field is optional and may be NULL.
MinidumpModuleList* modules_;
// The optional SymbolSupplier for resolving source line info.
SymbolSupplier* supplier_;
// The CrashReport object which is passed to the SymbolSupplier. May be null.
const CrashReport* report_;
};

View file

@ -28,8 +28,10 @@ namespace google_airbag {
StackwalkerX86::StackwalkerX86(MinidumpContext* context,
MemoryRegion* memory,
MinidumpModuleList* modules)
: Stackwalker(memory, modules),
MinidumpModuleList* modules,
SymbolSupplier* supplier,
const CrashReport* report)
: Stackwalker(memory, modules, supplier, report),
last_frame_pointer_(0) {
if (memory_->GetBase() + memory_->GetSize() - 1 > 0xffffffff) {
// The x86 is a 32-bit CPU, the limits of the supplied stack are invalid.

View file

@ -40,11 +40,13 @@ class StackwalkerX86 : public Stackwalker {
public:
// context is a MinidumpContext object that gives access to x86-specific
// register state corresponding to the innermost called frame to be
// included in the stack. memory and modules are passed directly through
// included in the stack. The other arguments are passed directly through
// to the base Stackwalker constructor.
StackwalkerX86(MinidumpContext* context,
MemoryRegion* memory,
MinidumpModuleList* modules);
MinidumpModuleList* modules,
SymbolSupplier* supplier,
const CrashReport* report);
private:
// Implementation of Stackwalker, using x86 context (%ebp, %eip) and

BIN
src/processor/testdata/minidump2.dmp vendored Executable file

Binary file not shown.

10686
src/processor/testdata/minidump2.sym vendored Executable file

File diff suppressed because it is too large Load diff

45
src/processor/testdata/test_app.cc vendored Normal file
View file

@ -0,0 +1,45 @@
// This file is used to generate minidump2.dmp and minidump2.sym.
// cl /Zi /Fetest_app.exe test_app.cc dbghelp.lib
// Then run test_app to generate a dump, and dump_syms to create the .sym file.
#include <windows.h>
#include <dbghelp.h>
static LONG HandleException(EXCEPTION_POINTERS *exinfo) {
HANDLE dump_file = CreateFile("dump.dmp",
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
MINIDUMP_EXCEPTION_INFORMATION except_info;
except_info.ThreadId = GetCurrentThreadId();
except_info.ExceptionPointers = exinfo;
except_info.ClientPointers = false;
MiniDumpWriteDump(GetCurrentProcess(),
GetCurrentProcessId(),
dump_file,
MiniDumpNormal,
&except_info,
NULL,
NULL);
CloseHandle(dump_file);
return EXCEPTION_EXECUTE_HANDLER;
}
void CrashFunction() {
int *i = NULL;
*i = 5; // crash!
}
int main(int argc, char *argv[]) {
__try {
CrashFunction();
} __except(HandleException(GetExceptionInformation())) {
}
return 0;
}