/ src / processor / basic_code_modules.cc
basic_code_modules.cc
  1  // Copyright 2006 Google LLC
  2  //
  3  // Redistribution and use in source and binary forms, with or without
  4  // modification, are permitted provided that the following conditions are
  5  // met:
  6  //
  7  //     * Redistributions of source code must retain the above copyright
  8  // notice, this list of conditions and the following disclaimer.
  9  //     * Redistributions in binary form must reproduce the above
 10  // copyright notice, this list of conditions and the following disclaimer
 11  // in the documentation and/or other materials provided with the
 12  // distribution.
 13  //     * Neither the name of Google LLC nor the names of its
 14  // contributors may be used to endorse or promote products derived from
 15  // this software without specific prior written permission.
 16  //
 17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  
 29  // basic_code_modules.cc: Contains all of the CodeModule objects that
 30  // were loaded into a single process.
 31  //
 32  // See basic_code_modules.h for documentation.
 33  //
 34  // Author: Mark Mentovai
 35  
 36  #ifdef HAVE_CONFIG_H
 37  #include <config.h>  // Must come first
 38  #endif
 39  
 40  #include "processor/basic_code_modules.h"
 41  
 42  #include <assert.h>
 43  
 44  #include <vector>
 45  
 46  #include "google_breakpad/processor/code_module.h"
 47  #include "processor/linked_ptr.h"
 48  #include "processor/logging.h"
 49  #include "processor/range_map-inl.h"
 50  
 51  namespace google_breakpad {
 52  
 53  using std::vector;
 54  
 55  BasicCodeModules::BasicCodeModules(const CodeModules* that,
 56                                     MergeRangeStrategy strategy)
 57      : main_address_(0), map_() {
 58    BPLOG_IF(ERROR, !that) << "BasicCodeModules::BasicCodeModules requires "
 59                              "|that|";
 60    assert(that);
 61  
 62    map_.SetMergeStrategy(strategy);
 63  
 64    const CodeModule *main_module = that->GetMainModule();
 65    if (main_module)
 66      main_address_ = main_module->base_address();
 67  
 68    unsigned int count = that->module_count();
 69    for (unsigned int i = 0; i < count; ++i) {
 70      // Make a copy of the module and insert it into the map.  Use
 71      // GetModuleAtIndex because ordering is unimportant when slurping the
 72      // entire list, and GetModuleAtIndex may be faster than
 73      // GetModuleAtSequence.
 74      linked_ptr<const CodeModule> module(that->GetModuleAtIndex(i)->Copy());
 75      if (!map_.StoreRange(module->base_address(), module->size(), module)) {
 76        BPLOG(ERROR) << "Module " << module->code_file()
 77                     << " could not be stored";
 78      }
 79    }
 80  
 81    // Report modules with shrunk ranges.
 82    for (unsigned int i = 0; i < count; ++i) {
 83      linked_ptr<const CodeModule> module(that->GetModuleAtIndex(i)->Copy());
 84      uint64_t delta = 0;
 85      if (map_.RetrieveRange(module->base_address() + module->size() - 1,
 86                             &module, NULL /* base */, &delta, NULL /* size */) &&
 87          delta > 0) {
 88        BPLOG(INFO) << "The range for module " << module->code_file()
 89                    << " was shrunk down by " << HexString(delta) << " bytes.";
 90        linked_ptr<CodeModule> shrunk_range_module(module->Copy());
 91        shrunk_range_module->SetShrinkDownDelta(delta);
 92        shrunk_range_modules_.push_back(shrunk_range_module);
 93      }
 94    }
 95  
 96    // TODO(ivanpe): Report modules with conflicting ranges.  The list of such
 97    // modules should be copied from |that|.
 98  }
 99  
100  BasicCodeModules::BasicCodeModules() : main_address_(0), map_() { }
101  
102  BasicCodeModules::~BasicCodeModules() {
103  }
104  
105  unsigned int BasicCodeModules::module_count() const {
106    return map_.GetCount();
107  }
108  
109  const CodeModule* BasicCodeModules::GetModuleForAddress(
110      uint64_t address) const {
111    linked_ptr<const CodeModule> module;
112    if (!map_.RetrieveRange(address, &module, NULL /* base */, NULL /* delta */,
113                            NULL /* size */)) {
114      BPLOG(INFO) << "No module at " << HexString(address);
115      return NULL;
116    }
117  
118    return module.get();
119  }
120  
121  const CodeModule* BasicCodeModules::GetMainModule() const {
122    return GetModuleForAddress(main_address_);
123  }
124  
125  const CodeModule* BasicCodeModules::GetModuleAtSequence(
126      unsigned int sequence) const {
127    linked_ptr<const CodeModule> module;
128    if (!map_.RetrieveRangeAtIndex(sequence, &module, NULL /* base */,
129                                   NULL /* delta */, NULL /* size */)) {
130      BPLOG(ERROR) << "RetrieveRangeAtIndex failed for sequence " << sequence;
131      return NULL;
132    }
133  
134    return module.get();
135  }
136  
137  const CodeModule* BasicCodeModules::GetModuleAtIndex(
138      unsigned int index) const {
139    // This class stores everything in a RangeMap, without any more-efficient
140    // way to walk the list of CodeModule objects.  Implement GetModuleAtIndex
141    // using GetModuleAtSequence, which meets all of the requirements, and
142    // in addition, guarantees ordering.
143    return GetModuleAtSequence(index);
144  }
145  
146  const CodeModules* BasicCodeModules::Copy() const {
147    return new BasicCodeModules(this, map_.GetMergeStrategy());
148  }
149  
150  vector<linked_ptr<const CodeModule> >
151  BasicCodeModules::GetShrunkRangeModules() const {
152    return shrunk_range_modules_;
153  }
154  
155  }  // namespace google_breakpad