From 87855248f1fab83caf002418196a34051d359f2c Mon Sep 17 00:00:00 2001 From: jimblandy Date: Mon, 5 Apr 2010 19:40:17 +0000 Subject: Breakpad symbol dumper: Move Linux dumping classes into src/common. The Linux symbol dumper's classes are reasonably portable, and should be usable for the Mac dumper as well. Move them to src/common, along with their unit tests. Update #include directives and Makefile. a=jimblandy, r=nealsid git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@567 4c0a9323-5329-0410-9bdc-e9ce6186880e --- src/common/module.cc | 235 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 235 insertions(+) create mode 100644 src/common/module.cc (limited to 'src/common/module.cc') diff --git a/src/common/module.cc b/src/common/module.cc new file mode 100644 index 00000000..fd36f3f9 --- /dev/null +++ b/src/common/module.cc @@ -0,0 +1,235 @@ +// Copyright (c) 2010 Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +// Original author: Jim Blandy + +// module.cc: Implement google_breakpad::Module. See module.h. + +#include +#include + +#include "common/module.h" + +namespace google_breakpad { + +Module::Module(const string &name, const string &os, + const string &architecture, const string &id) : + name_(name), + os_(os), + architecture_(architecture), + id_(id), + load_address_(0) { } + +Module::~Module() { + for (FileByNameMap::iterator it = files_.begin(); it != files_.end(); it++) + delete it->second; + for (vector::iterator it = functions_.begin(); + it != functions_.end(); it++) + delete *it; + for (vector::iterator it = stack_frame_entries_.begin(); + it != stack_frame_entries_.end(); it++) + delete *it; +} + +void Module::SetLoadAddress(Address address) { + load_address_ = address; +} + +void Module::AddFunction(Function *function) { + functions_.push_back(function); +} + +void Module::AddFunctions(vector::iterator begin, + vector::iterator end) { + functions_.insert(functions_.end(), begin, end); +} + +void Module::AddStackFrameEntry(StackFrameEntry *stack_frame_entry) { + stack_frame_entries_.push_back(stack_frame_entry); +} + +void Module::GetFunctions(vector *vec, + vector::iterator i) { + vec->insert(i, functions_.begin(), functions_.end()); +} + +Module::File *Module::FindFile(const string &name) { + // A tricky bit here. The key of each map entry needs to be a + // pointer to the entry's File's name string. This means that we + // can't do the initial lookup with any operation that would create + // an empty entry for us if the name isn't found (like, say, + // operator[] or insert do), because such a created entry's key will + // be a pointer the string passed as our argument. Since the key of + // a map's value type is const, we can't fix it up once we've + // created our file. lower_bound does the lookup without doing an + // insertion, and returns a good hint iterator to pass to insert. + // Our "destiny" is where we belong, whether we're there or not now. + FileByNameMap::iterator destiny = files_.lower_bound(&name); + if (destiny == files_.end() + || *destiny->first != name) { // Repeated string comparison, boo hoo. + File *file = new File; + file->name = name; + file->source_id = -1; + destiny = files_.insert(destiny, + FileByNameMap::value_type(&file->name, file)); + } + return destiny->second; +} + +Module::File *Module::FindFile(const char *name) { + string name_string = name; + return FindFile(name_string); +} + +Module::File *Module::FindExistingFile(const string &name) { + FileByNameMap::iterator it = files_.find(&name); + return (it == files_.end()) ? NULL : it->second; +} + +void Module::GetFiles(vector *vec) { + vec->clear(); + for (FileByNameMap::iterator it = files_.begin(); it != files_.end(); it++) + vec->push_back(it->second); +} + +void Module::GetStackFrameEntries(vector *vec) { + *vec = stack_frame_entries_; +} + +void Module::AssignSourceIds() { + // First, give every source file an id of -1. + for (FileByNameMap::iterator file_it = files_.begin(); + file_it != files_.end(); file_it++) + file_it->second->source_id = -1; + + // Next, mark all files actually cited by our functions' line number + // info, by setting each one's source id to zero. + for (vector::const_iterator func_it = functions_.begin(); + func_it != functions_.end(); func_it++) { + Function *func = *func_it; + for (vector::iterator line_it = func->lines.begin(); + line_it != func->lines.end(); line_it++) + line_it->file->source_id = 0; + } + + // Finally, assign source ids to those files that have been marked. + // We could have just assigned source id numbers while traversing + // the line numbers, but doing it this way numbers the files in + // lexicographical order by name, which is neat. + int next_source_id = 0; + for (FileByNameMap::iterator file_it = files_.begin(); + file_it != files_.end(); file_it++) + if (! file_it->second->source_id) + file_it->second->source_id = next_source_id++; +} + +bool Module::ReportError() { + fprintf(stderr, "error writing symbol file: %s\n", + strerror (errno)); + return false; +} + +bool Module::WriteRuleMap(const RuleMap &rule_map, FILE *stream) { + for (RuleMap::const_iterator it = rule_map.begin(); + it != rule_map.end(); it++) { + if (it != rule_map.begin() && + 0 > putc(' ', stream)) + return false; + if (0 > fprintf(stream, "%s: %s", it->first.c_str(), it->second.c_str())) + return false; + } + return true; +} + +bool Module::Write(FILE *stream) { + if (0 > fprintf(stream, "MODULE %s %s %s %s\n", + os_.c_str(), architecture_.c_str(), id_.c_str(), + name_.c_str())) + return ReportError(); + + AssignSourceIds(); + + // Write out files. + for (FileByNameMap::iterator file_it = files_.begin(); + file_it != files_.end(); file_it++) { + File *file = file_it->second; + if (file->source_id >= 0) { + if (0 > fprintf(stream, "FILE %d %s\n", + file->source_id, file->name.c_str())) + return ReportError(); + } + } + + // Write out functions and their lines. + for (vector::const_iterator func_it = functions_.begin(); + func_it != functions_.end(); func_it++) { + Function *func = *func_it; + if (0 > fprintf(stream, "FUNC %llx %llx %llx %s\n", + (unsigned long long) (func->address - load_address_), + (unsigned long long) func->size, + (unsigned long long) func->parameter_size, + func->name.c_str())) + return ReportError(); + for (vector::iterator line_it = func->lines.begin(); + line_it != func->lines.end(); line_it++) + if (0 > fprintf(stream, "%llx %llx %d %d\n", + (unsigned long long) (line_it->address - load_address_), + (unsigned long long) line_it->size, + line_it->number, + line_it->file->source_id)) + return ReportError(); + } + + // Write out 'STACK CFI INIT' and 'STACK CFI' records. + vector::const_iterator frame_it; + for (frame_it = stack_frame_entries_.begin(); + frame_it != stack_frame_entries_.end(); frame_it++) { + StackFrameEntry *entry = *frame_it; + if (0 > fprintf(stream, "STACK CFI INIT %llx %llx ", + (unsigned long long) entry->address - load_address_, + (unsigned long long) entry->size) + || !WriteRuleMap(entry->initial_rules, stream) + || 0 > putc('\n', stream)) + return ReportError(); + + // Write out this entry's delta rules as 'STACK CFI' records. + for (RuleChangeMap::const_iterator delta_it = entry->rule_changes.begin(); + delta_it != entry->rule_changes.end(); delta_it++) { + if (0 > fprintf(stream, "STACK CFI %llx ", + (unsigned long long) delta_it->first - load_address_) + || !WriteRuleMap(delta_it->second, stream) + || 0 > putc('\n', stream)) + return ReportError(); + } + } + + return true; +} + +} // namespace google_breakpad -- cgit v1.2.1