/thirdparty/breakpad/common/module.cc

http://github.com/tomahawk-player/tomahawk · C++ · 294 lines · 197 code · 39 blank · 58 comment · 55 complexity · fb11b520852865000ec60a10359b0dde MD5 · raw file

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