OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com> | |
31 | |
32 // module.cc: Implement google_breakpad::Module. See module.h. | |
33 | |
34 #include <cerrno> | |
35 #include <cstring> | |
36 | |
37 #include "common/module.h" | |
38 | |
39 namespace google_breakpad { | |
40 | |
41 Module::Module(const string &name, const string &os, | |
42 const string &architecture, const string &id) : | |
43 name_(name), | |
44 os_(os), | |
45 architecture_(architecture), | |
46 id_(id), | |
47 load_address_(0) { } | |
48 | |
49 Module::~Module() { | |
50 for (FileByNameMap::iterator it = files_.begin(); it != files_.end(); it++) | |
51 delete it->second; | |
52 for (vector<Function *>::iterator it = functions_.begin(); | |
53 it != functions_.end(); it++) | |
54 delete *it; | |
55 for (vector<StackFrameEntry *>::iterator it = stack_frame_entries_.begin(); | |
56 it != stack_frame_entries_.end(); it++) | |
57 delete *it; | |
58 } | |
59 | |
60 void Module::SetLoadAddress(Address address) { | |
61 load_address_ = address; | |
62 } | |
63 | |
64 void Module::AddFunction(Function *function) { | |
65 functions_.push_back(function); | |
66 } | |
67 | |
68 void Module::AddFunctions(vector<Function *>::iterator begin, | |
69 vector<Function *>::iterator end) { | |
70 functions_.insert(functions_.end(), begin, end); | |
71 } | |
72 | |
73 void Module::AddStackFrameEntry(StackFrameEntry *stack_frame_entry) { | |
74 stack_frame_entries_.push_back(stack_frame_entry); | |
75 } | |
76 | |
77 void Module::GetFunctions(vector<Function *> *vec, | |
78 vector<Function *>::iterator i) { | |
79 vec->insert(i, functions_.begin(), functions_.end()); | |
80 } | |
81 | |
82 Module::File *Module::FindFile(const string &name) { | |
83 // A tricky bit here. The key of each map entry needs to be a | |
84 // pointer to the entry's File's name string. This means that we | |
85 // can't do the initial lookup with any operation that would create | |
86 // an empty entry for us if the name isn't found (like, say, | |
87 // operator[] or insert do), because such a created entry's key will | |
88 // be a pointer the string passed as our argument. Since the key of | |
89 // a map's value type is const, we can't fix it up once we've | |
90 // created our file. lower_bound does the lookup without doing an | |
91 // insertion, and returns a good hint iterator to pass to insert. | |
92 // Our "destiny" is where we belong, whether we're there or not now. | |
93 FileByNameMap::iterator destiny = files_.lower_bound(&name); | |
94 if (destiny == files_.end() | |
95 || *destiny->first != name) { // Repeated string comparison, boo hoo. | |
96 File *file = new File; | |
97 file->name = name; | |
98 file->source_id = -1; | |
99 destiny = files_.insert(destiny, | |
100 FileByNameMap::value_type(&file->name, file)); | |
101 } | |
102 return destiny->second; | |
103 } | |
104 | |
105 Module::File *Module::FindFile(const char *name) { | |
106 string name_string = name; | |
107 return FindFile(name_string); | |
108 } | |
109 | |
110 Module::File *Module::FindExistingFile(const string &name) { | |
111 FileByNameMap::iterator it = files_.find(&name); | |
112 return (it == files_.end()) ? NULL : it->second; | |
113 } | |
114 | |
115 void Module::GetFiles(vector<File *> *vec) { | |
116 vec->clear(); | |
117 for (FileByNameMap::iterator it = files_.begin(); it != files_.end(); it++) | |
118 vec->push_back(it->second); | |
119 } | |
120 | |
121 void Module::GetStackFrameEntries(vector<StackFrameEntry *> *vec) { | |
122 *vec = stack_frame_entries_; | |
123 } | |
124 | |
125 void Module::AssignSourceIds() { | |
126 // First, give every source file an id of -1. | |
127 for (FileByNameMap::iterator file_it = files_.begin(); | |
128 file_it != files_.end(); file_it++) | |
129 file_it->second->source_id = -1; | |
130 | |
131 // Next, mark all files actually cited by our functions' line number | |
132 // info, by setting each one's source id to zero. | |
133 for (vector<Function *>::const_iterator func_it = functions_.begin(); | |
134 func_it != functions_.end(); func_it++) { | |
135 Function *func = *func_it; | |
136 for (vector<Line>::iterator line_it = func->lines.begin(); | |
137 line_it != func->lines.end(); line_it++) | |
138 line_it->file->source_id = 0; | |
139 } | |
140 | |
141 // Finally, assign source ids to those files that have been marked. | |
142 // We could have just assigned source id numbers while traversing | |
143 // the line numbers, but doing it this way numbers the files in | |
144 // lexicographical order by name, which is neat. | |
145 int next_source_id = 0; | |
146 for (FileByNameMap::iterator file_it = files_.begin(); | |
147 file_it != files_.end(); file_it++) | |
148 if (! file_it->second->source_id) | |
149 file_it->second->source_id = next_source_id++; | |
150 } | |
151 | |
152 bool Module::ReportError() { | |
153 fprintf(stderr, "error writing symbol file: %s\n", | |
154 strerror (errno)); | |
155 return false; | |
156 } | |
157 | |
158 bool Module::WriteRuleMap(const RuleMap &rule_map, FILE *stream) { | |
159 for (RuleMap::const_iterator it = rule_map.begin(); | |
160 it != rule_map.end(); it++) { | |
161 if (it != rule_map.begin() && | |
162 0 > putc(' ', stream)) | |
163 return false; | |
164 if (0 > fprintf(stream, "%s: %s", it->first.c_str(), it->second.c_str())) | |
165 return false; | |
166 } | |
167 return true; | |
168 } | |
169 | |
170 bool Module::Write(FILE *stream) { | |
171 if (0 > fprintf(stream, "MODULE %s %s %s %s\n", | |
172 os_.c_str(), architecture_.c_str(), id_.c_str(), | |
173 name_.c_str())) | |
174 return ReportError(); | |
175 | |
176 AssignSourceIds(); | |
177 | |
178 // Write out files. | |
179 for (FileByNameMap::iterator file_it = files_.begin(); | |
180 file_it != files_.end(); file_it++) { | |
181 File *file = file_it->second; | |
182 if (file->source_id >= 0) { | |
183 if (0 > fprintf(stream, "FILE %d %s\n", | |
184 file->source_id, file->name.c_str())) | |
185 return ReportError(); | |
186 } | |
187 } | |
188 | |
189 // Write out functions and their lines. | |
190 for (vector<Function *>::const_iterator func_it = functions_.begin(); | |
191 func_it != functions_.end(); func_it++) { | |
192 Function *func = *func_it; | |
193 if (0 > fprintf(stream, "FUNC %llx %llx %llx %s\n", | |
194 (unsigned long long) (func->address - load_address_), | |
195 (unsigned long long) func->size, | |
196 (unsigned long long) func->parameter_size, | |
197 func->name.c_str())) | |
198 return ReportError(); | |
199 for (vector<Line>::iterator line_it = func->lines.begin(); | |
200 line_it != func->lines.end(); line_it++) | |
201 if (0 > fprintf(stream, "%llx %llx %d %d\n", | |
202 (unsigned long long) (line_it->address - load_address_), | |
203 (unsigned long long) line_it->size, | |
204 line_it->number, | |
205 line_it->file->source_id)) | |
206 return ReportError(); | |
207 } | |
208 | |
209 // Write out 'STACK CFI INIT' and 'STACK CFI' records. | |
210 vector<StackFrameEntry *>::const_iterator frame_it; | |
211 for (frame_it = stack_frame_entries_.begin(); | |
212 frame_it != stack_frame_entries_.end(); frame_it++) { | |
213 StackFrameEntry *entry = *frame_it; | |
214 if (0 > fprintf(stream, "STACK CFI INIT %llx %llx ", | |
215 (unsigned long long) entry->address - load_address_, | |
216 (unsigned long long) entry->size) | |
217 || !WriteRuleMap(entry->initial_rules, stream) | |
218 || 0 > putc('\n', stream)) | |
219 return ReportError(); | |
220 | |
221 // Write out this entry's delta rules as 'STACK CFI' records. | |
222 for (RuleChangeMap::const_iterator delta_it = entry->rule_changes.begin(); | |
223 delta_it != entry->rule_changes.end(); delta_it++) { | |
224 if (0 > fprintf(stream, "STACK CFI %llx ", | |
225 (unsigned long long) delta_it->first - load_address_) | |
226 || !WriteRuleMap(delta_it->second, stream) | |
227 || 0 > putc('\n', stream)) | |
228 return ReportError(); | |
229 } | |
230 } | |
231 | |
232 return true; | |
233 } | |
234 | |
235 } // namespace google_breakpad | |
OLD | NEW |