Make SourceLineResolver use RangeMap (#13). r=bryner

- Eliminate MemAddrMap from source_line_resolver.cc and adapt it to use
   RangeMap, also used by minidump.cc.
 - RangeMap operates on both a base address and a size, where MemAddrMap
   only used a base address, so the dumped symbol file format is modified
   to include size information.  dump_syms produces these files and
   SourceLineResolver consumes them.
 - Provide updated test data conforming to the new dumped symbol format.

http://groups.google.com/group/airbag-dev/browse_thread/thread/e9403cf3ad6336a1


git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@21 4c0a9323-5329-0410-9bdc-e9ce6186880e
This commit is contained in:
mmentovai 2006-09-08 18:03:56 +00:00
parent 6dd21d3baf
commit cb9fd5b773
13 changed files with 8197 additions and 7572 deletions

174
src/processor/linked_ptr.h Normal file
View file

@ -0,0 +1,174 @@
// 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.
// A "smart" pointer type with reference tracking. Every pointer to a
// particular object is kept on a circular linked list. When the last pointer
// to an object is destroyed or reassigned, the object is deleted.
//
// Used properly, this deletes the object when the last reference goes away.
// There are several caveats:
// - Like all reference counting schemes, cycles lead to leaks.
// - Each smart pointer is actually two pointers (8 bytes instead of 4).
// - Every time a pointer is assigned, the entire list of pointers to that
// object is traversed. This class is therefore NOT SUITABLE when there
// will often be more than two or three pointers to a particular object.
// - References are only tracked as long as linked_ptr<> objects are copied.
// If a linked_ptr<> is converted to a raw pointer and back, BAD THINGS
// will happen (double deletion).
//
// A good use of this class is storing object references in STL containers.
// You can safely put linked_ptr<> in a vector<>.
// Other uses may not be as good.
//
// Note: If you use an incomplete type with linked_ptr<>, the class
// *containing* linked_ptr<> must have a constructor and destructor (even
// if they do nothing!).
#ifndef PROCESSOR_LINKED_PTR_H__
#define PROCESSOR_LINKED_PTR_H__
// This is used internally by all instances of linked_ptr<>. It needs to be
// a non-template class because different types of linked_ptr<> can refer to
// the same object (linked_ptr<Superclass>(obj) vs linked_ptr<Subclass>(obj)).
// So, it needs to be possible for different types of linked_ptr to participate
// in the same circular linked list, so we need a single class type here.
//
// DO NOT USE THIS CLASS DIRECTLY YOURSELF. Use linked_ptr<T>.
class linked_ptr_internal {
public:
// Create a new circle that includes only this instance.
void join_new() {
next_ = this;
}
// Join an existing circle.
void join(linked_ptr_internal const* ptr) {
linked_ptr_internal const* p = ptr;
while (p->next_ != ptr) p = p->next_;
p->next_ = this;
next_ = ptr;
}
// Leave whatever circle we're part of. Returns true iff we were the
// last member of the circle. Once this is done, you can join() another.
bool depart() {
if (next_ == this) return true;
linked_ptr_internal const* p = next_;
while (p->next_ != this) p = p->next_;
p->next_ = next_;
return false;
}
private:
mutable linked_ptr_internal const* next_;
};
template <typename T>
class linked_ptr {
public:
typedef T element_type;
// Take over ownership of a raw pointer. This should happen as soon as
// possible after the object is created.
explicit linked_ptr(T* ptr = NULL) { capture(ptr); }
~linked_ptr() { depart(); }
// Copy an existing linked_ptr<>, adding ourselves to the list of references.
template <typename U> linked_ptr(linked_ptr<U> const& ptr) { copy(&ptr); }
linked_ptr(linked_ptr const& ptr) { copy(&ptr); }
// Assignment releases the old value and acquires the new.
template <typename U> linked_ptr& operator=(linked_ptr<U> const& ptr) {
depart();
copy(&ptr);
return *this;
}
linked_ptr& operator=(linked_ptr const& ptr) {
if (&ptr != this) {
depart();
copy(&ptr);
}
return *this;
}
// Smart pointer members.
void reset(T* ptr = NULL) { depart(); capture(ptr); }
T* get() const { return value_; }
T* operator->() const { return value_; }
T& operator*() const { return *value_; }
// Release ownership of the pointed object and returns it.
// Sole ownership by this linked_ptr object is required.
T* release() {
bool last = link_.depart();
T* v = value_;
value_ = NULL;
return v;
}
bool operator==(T* p) const { return value_ == p; }
bool operator!=(T* p) const { return value_ != p; }
template <typename U>
bool operator==(linked_ptr<U> const& ptr) const {
return value_ == ptr.get();
}
template <typename U>
bool operator!=(linked_ptr<U> const& ptr) const {
return value_ != ptr.get();
}
private:
template <typename U>
friend class linked_ptr;
T* value_;
linked_ptr_internal link_;
void depart() {
if (link_.depart()) delete value_;
}
void capture(T* ptr) {
value_ = ptr;
link_.join_new();
}
template <typename U> void copy(linked_ptr<U> const* ptr) {
value_ = ptr->get();
if (value_)
link_.join(&ptr->link_);
else
link_.join_new();
}
};
template<typename T> inline
bool operator==(T* ptr, const linked_ptr<T>& x) {
return ptr == x.get();
}
template<typename T> inline
bool operator!=(T* ptr, const linked_ptr<T>& x) {
return ptr != x.get();
}
// A function to convert T* into linked_ptr<T>
// Doing e.g. make_linked_ptr(new FooBarBaz<type>(arg)) is a shorter notation
// for linked_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg))
template <typename T>
linked_ptr<T> make_linked_ptr(T* ptr) {
return linked_ptr<T>(ptr);
}
#endif // PROCESSOR_LINKED_PTR_H__

View file

@ -49,7 +49,7 @@ class RangeMap {
// Locates the range encompassing the supplied address. If there is
// no such range, or if there is a parameter error, returns false.
bool RetrieveRange(const AddressType& address, EntryType* entry);
bool RetrieveRange(const AddressType& address, EntryType* entry) const;
// Empties the range map, restoring it to the state it was when it was
// initially created.
@ -125,7 +125,7 @@ bool RangeMap<AddressType, EntryType>::StoreRange(const AddressType& base,
template<typename AddressType, typename EntryType>
bool RangeMap<AddressType, EntryType>::RetrieveRange(
const AddressType& address,
EntryType* entry) {
EntryType* entry) const {
if (!entry)
return false;

View file

@ -19,6 +19,8 @@
#include <utility>
#include "processor/source_line_resolver.h"
#include "google/stack_frame.h"
#include "processor/linked_ptr.h"
#include "processor/range_map.h"
using std::map;
using std::vector;
@ -27,62 +29,29 @@ using __gnu_cxx::hash;
namespace google_airbag {
// MemAddrMap is a map subclass which has the following properties:
// - stores pointers to an "entry" type, which are deleted on destruction
// - suitable for address lookup via FindContainingEntry
template<class T>
class SourceLineResolver::MemAddrMap : public map<MemAddr, T*> {
public:
~MemAddrMap();
// Find the entry which "contains" a given relative address, that is,
// the entry with the highest address not greater than the given address.
// Returns NULL if there is no such entry.
T* FindContainingEntry(MemAddr address) const;
private:
typedef map<MemAddr, T*> MapType;
};
template<class T>
SourceLineResolver::MemAddrMap<T>::~MemAddrMap() {
typename MapType::iterator it;
for (it = MapType::begin(); it != MapType::end(); ++it) {
delete it->second;
}
}
template<class T>
T* SourceLineResolver::MemAddrMap<T>::FindContainingEntry(
MemAddr address) const {
typename MapType::const_iterator it = MapType::lower_bound(address);
if (it->first != address) {
if (it == MapType::begin()) {
// Nowhere to go, so no entry contains the address
return NULL;
}
--it; // back up to the entry before address
}
return it->second;
}
struct SourceLineResolver::Line {
Line(MemAddr addr, int file_id, int source_line)
: address(addr), source_file_id(file_id), line(source_line) { }
Line(MemAddr addr, MemAddr code_size, int file_id, int source_line)
: address(addr)
, size(code_size)
, source_file_id(file_id)
, line(source_line) { }
MemAddr address;
MemAddr size;
int source_file_id;
int line;
};
struct SourceLineResolver::Function {
Function(const string &function_name, MemAddr function_address)
: name(function_name), address(function_address) { }
Function(const string &function_name,
MemAddr function_address,
MemAddr code_size)
: name(function_name), address(function_address), size(code_size) { }
string name;
MemAddr address;
MemAddrMap<Line> lines;
MemAddr size;
RangeMap<MemAddr, linked_ptr<Line> > lines;
};
class SourceLineResolver::Module {
@ -111,7 +80,7 @@ class SourceLineResolver::Module {
string name_;
FileMap files_;
MemAddrMap<Function> functions_;
RangeMap<MemAddr, linked_ptr<Function> > functions_;
};
SourceLineResolver::SourceLineResolver() : modules_(new ModuleMap) {
@ -166,7 +135,8 @@ bool SourceLineResolver::Module::LoadMap(const string &map_file) {
if (!cur_func) {
return false;
}
functions_.insert(make_pair(cur_func->address, cur_func));
functions_.StoreRange(cur_func->address, cur_func->size,
linked_ptr<Function>(cur_func));
} else {
if (!cur_func) {
return false;
@ -175,7 +145,8 @@ bool SourceLineResolver::Module::LoadMap(const string &map_file) {
if (!line) {
return false;
}
cur_func->lines.insert(make_pair(line->address, line));
cur_func->lines.StoreRange(line->address, line->size,
linked_ptr<Line>(line));
}
}
@ -185,14 +156,14 @@ bool SourceLineResolver::Module::LoadMap(const string &map_file) {
void SourceLineResolver::Module::LookupAddress(MemAddr address,
StackFrame *frame) const {
Function *func = functions_.FindContainingEntry(address);
if (!func) {
linked_ptr<Function> func;
if (!functions_.RetrieveRange(address, &func)) {
return;
}
frame->function_name = func->name;
Line *line = func->lines.FindContainingEntry(address);
if (!line) {
linked_ptr<Line> line;
if (!func->lines.RetrieveRange(address, &line)) {
return;
}
@ -231,12 +202,17 @@ SourceLineResolver::Function* SourceLineResolver::Module::ParseFunction(
return NULL;
}
char *size = strtok(NULL, " ");
if (!size) {
return NULL;
}
char *name = strtok(NULL, "\r\n");
if (!name) {
return NULL;
}
return new Function(name, strtoull(addr, NULL, 16));
return new Function(name, strtoull(addr, NULL, 16), strtoull(size, NULL, 16));
}
SourceLineResolver::Line* SourceLineResolver::Module::ParseLine(
@ -247,6 +223,11 @@ SourceLineResolver::Line* SourceLineResolver::Module::ParseLine(
return NULL;
}
char *size = strtok(NULL, " ");
if (!size) {
return NULL;
}
char *line_num_str = strtok(NULL, "\r\n");
if (!line_num_str) {
return NULL;
@ -257,7 +238,10 @@ SourceLineResolver::Line* SourceLineResolver::Module::ParseLine(
return NULL;
}
return new Line(strtoull(addr, NULL, 16), source_file, line_number);
return new Line(strtoull(addr, NULL, 16),
strtoull(size, NULL, 16),
source_file,
line_number);
}
size_t SourceLineResolver::HashString::operator()(const string &s) const {

Binary file not shown.

File diff suppressed because it is too large Load diff

View file

@ -1,12 +1,12 @@
FILE 1 file1_1.cc
FILE 2 file1_2.cc
FILE 3 file1_3.cc
FUNC 1000 Function1_1
1000 44 1
1004 45 1
1008 46 1
FUNC 1100 Function1_2
1100 65 2
1104 66 2
FUNC 1200 Function1_3
FUNC 1300 Function1_4
FUNC 1000 c Function1_1
1000 4 44 1
1004 4 45 1
1008 4 46 1
FUNC 1100 8 Function1_2
1100 4 65 2
1104 4 66 2
FUNC 1200 100 Function1_3
FUNC 1300 100 Function1_4

View file

@ -1,12 +1,12 @@
FILE 1 file2_1.cc
FILE 2 file2_2.cc
FILE 3 file2_3.cc
FUNC 2000 Function2_1
1000 54 1
1004 55 1
1008 56 1
FUNC 2170 Function2_2
2170 10 2
2176 12 2
217a 13 2
2180 21 2
FUNC 2000 c Function2_1
1000 4 54 1
1004 4 55 1
1008 4 56 1
FUNC 2170 14 Function2_2
2170 6 10 2
2176 4 12 2
217a 6 13 2
2180 4 21 2