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