/thirdparty/breakpad/processor/simple_symbol_supplier.cc

http://github.com/tomahawk-player/tomahawk · C++ · 200 lines · 135 code · 26 blank · 39 comment · 21 complexity · 28125320fb6e94d3437c65eb1ba610cf MD5 · raw file

  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. // simple_symbol_supplier.cc: A simple SymbolSupplier implementation
  30. //
  31. // See simple_symbol_supplier.h for documentation.
  32. //
  33. // Author: Mark Mentovai
  34. #include "processor/simple_symbol_supplier.h"
  35. #include <assert.h>
  36. #include <string.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <algorithm>
  40. #include <iostream>
  41. #include <fstream>
  42. #include "google_breakpad/processor/code_module.h"
  43. #include "google_breakpad/processor/system_info.h"
  44. #include "processor/logging.h"
  45. #include "processor/pathname_stripper.h"
  46. namespace google_breakpad {
  47. static bool file_exists(const string &file_name) {
  48. struct stat sb;
  49. return stat(file_name.c_str(), &sb) == 0;
  50. }
  51. SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFile(
  52. const CodeModule *module, const SystemInfo *system_info,
  53. string *symbol_file) {
  54. BPLOG_IF(ERROR, !symbol_file) << "SimpleSymbolSupplier::GetSymbolFile "
  55. "requires |symbol_file|";
  56. assert(symbol_file);
  57. symbol_file->clear();
  58. for (unsigned int path_index = 0; path_index < paths_.size(); ++path_index) {
  59. SymbolResult result;
  60. if ((result = GetSymbolFileAtPathFromRoot(module, system_info,
  61. paths_[path_index],
  62. symbol_file)) != NOT_FOUND) {
  63. return result;
  64. }
  65. }
  66. return NOT_FOUND;
  67. }
  68. SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFile(
  69. const CodeModule *module,
  70. const SystemInfo *system_info,
  71. string *symbol_file,
  72. string *symbol_data) {
  73. assert(symbol_data);
  74. symbol_data->clear();
  75. SymbolSupplier::SymbolResult s = GetSymbolFile(module, system_info, symbol_file);
  76. if (s == FOUND) {
  77. std::ifstream in(symbol_file->c_str());
  78. std::getline(in, *symbol_data, std::string::traits_type::to_char_type(
  79. std::string::traits_type::eof()));
  80. in.close();
  81. }
  82. return s;
  83. }
  84. SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetCStringSymbolData(
  85. const CodeModule *module,
  86. const SystemInfo *system_info,
  87. string *symbol_file,
  88. char **symbol_data) {
  89. assert(symbol_data);
  90. string symbol_data_string;
  91. SymbolSupplier::SymbolResult s =
  92. GetSymbolFile(module, system_info, symbol_file, &symbol_data_string);
  93. if (s == FOUND) {
  94. unsigned int size = symbol_data_string.size() + 1;
  95. *symbol_data = new char[size];
  96. if (*symbol_data == NULL) {
  97. BPLOG(ERROR) << "Memory allocation for size " << size << " failed";
  98. return INTERRUPT;
  99. }
  100. memcpy(*symbol_data, symbol_data_string.c_str(), size - 1);
  101. (*symbol_data)[size - 1] = '\0';
  102. memory_buffers_.insert(make_pair(module->code_file(), *symbol_data));
  103. }
  104. return s;
  105. }
  106. void SimpleSymbolSupplier::FreeSymbolData(const CodeModule *module) {
  107. if (!module) {
  108. BPLOG(INFO) << "Cannot free symbol data buffer for NULL module";
  109. return;
  110. }
  111. map<string, char *>::iterator it = memory_buffers_.find(module->code_file());
  112. if (it == memory_buffers_.end()) {
  113. BPLOG(INFO) << "Cannot find symbol data buffer for module "
  114. << module->code_file();
  115. return;
  116. }
  117. delete [] it->second;
  118. memory_buffers_.erase(it);
  119. }
  120. SymbolSupplier::SymbolResult SimpleSymbolSupplier::GetSymbolFileAtPathFromRoot(
  121. const CodeModule *module, const SystemInfo *system_info,
  122. const string &root_path, string *symbol_file) {
  123. BPLOG_IF(ERROR, !symbol_file) << "SimpleSymbolSupplier::GetSymbolFileAtPath "
  124. "requires |symbol_file|";
  125. assert(symbol_file);
  126. symbol_file->clear();
  127. if (!module)
  128. return NOT_FOUND;
  129. // Start with the base path.
  130. string path = root_path;
  131. // Append the debug (pdb) file name as a directory name.
  132. path.append("/");
  133. string debug_file_name = PathnameStripper::File(module->debug_file());
  134. if (debug_file_name.empty()) {
  135. BPLOG(ERROR) << "Can't construct symbol file path without debug_file "
  136. "(code_file = " <<
  137. PathnameStripper::File(module->code_file()) << ")";
  138. return NOT_FOUND;
  139. }
  140. path.append(debug_file_name);
  141. // Append the identifier as a directory name.
  142. path.append("/");
  143. string identifier = module->debug_identifier();
  144. if (identifier.empty()) {
  145. BPLOG(ERROR) << "Can't construct symbol file path without debug_identifier "
  146. "(code_file = " <<
  147. PathnameStripper::File(module->code_file()) <<
  148. ", debug_file = " << debug_file_name << ")";
  149. return NOT_FOUND;
  150. }
  151. path.append(identifier);
  152. // Transform the debug file name into one ending in .sym. If the existing
  153. // name ends in .pdb, strip the .pdb. Otherwise, add .sym to the non-.pdb
  154. // name.
  155. path.append("/");
  156. string debug_file_extension;
  157. if (debug_file_name.size() > 4)
  158. debug_file_extension = debug_file_name.substr(debug_file_name.size() - 4);
  159. std::transform(debug_file_extension.begin(), debug_file_extension.end(),
  160. debug_file_extension.begin(), tolower);
  161. if (debug_file_extension == ".pdb") {
  162. path.append(debug_file_name.substr(0, debug_file_name.size() - 4));
  163. } else {
  164. path.append(debug_file_name);
  165. }
  166. path.append(".sym");
  167. if (!file_exists(path)) {
  168. BPLOG(INFO) << "No symbol file at " << path;
  169. return NOT_FOUND;
  170. }
  171. *symbol_file = path;
  172. return FOUND;
  173. }
  174. } // namespace google_breakpad