/thirdparty/breakpad/processor/module_serializer.cc

http://github.com/tomahawk-player/tomahawk · C++ · 200 lines · 117 code · 29 blank · 54 comment · 18 complexity · d58cff81d5381b8b8cebe13056957fda MD5 · raw file

  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. // module_serializer.cc: ModuleSerializer implementation.
  31. //
  32. // See module_serializer.h for documentation.
  33. //
  34. // Author: Siyang Xie (lambxsy@google.com)
  35. #include "processor/module_serializer.h"
  36. #include <map>
  37. #include <string>
  38. #include "processor/basic_code_module.h"
  39. #include "processor/logging.h"
  40. namespace google_breakpad {
  41. // Definition of static member variable in SimplerSerializer<Funcion>, which
  42. // is declared in file "simple_serializer-inl.h"
  43. RangeMapSerializer< MemAddr, linked_ptr<BasicSourceLineResolver::Line> >
  44. SimpleSerializer<BasicSourceLineResolver::Function>::range_map_serializer_;
  45. size_t ModuleSerializer::SizeOf(const BasicSourceLineResolver::Module &module) {
  46. size_t total_size_alloc_ = 0;
  47. // Compute memory size for each map component in Module class.
  48. int map_index = 0;
  49. map_sizes_[map_index++] = files_serializer_.SizeOf(module.files_);
  50. map_sizes_[map_index++] = functions_serializer_.SizeOf(module.functions_);
  51. map_sizes_[map_index++] = pubsym_serializer_.SizeOf(module.public_symbols_);
  52. for (int i = 0; i < WindowsFrameInfo::STACK_INFO_LAST; ++i)
  53. map_sizes_[map_index++] =
  54. wfi_serializer_.SizeOf(&(module.windows_frame_info_[i]));
  55. map_sizes_[map_index++] = cfi_init_rules_serializer_.SizeOf(
  56. module.cfi_initial_rules_);
  57. map_sizes_[map_index++] = cfi_delta_rules_serializer_.SizeOf(
  58. module.cfi_delta_rules_);
  59. // Header size.
  60. total_size_alloc_ = kNumberMaps_ * sizeof(u_int32_t);
  61. for (int i = 0; i < kNumberMaps_; ++i)
  62. total_size_alloc_ += map_sizes_[i];
  63. // Extra one byte for null terminator for C-string copy safety.
  64. ++total_size_alloc_;
  65. return total_size_alloc_;
  66. }
  67. char *ModuleSerializer::Write(const BasicSourceLineResolver::Module &module,
  68. char *dest) {
  69. // Write header.
  70. memcpy(dest, map_sizes_, kNumberMaps_ * sizeof(u_int32_t));
  71. dest += kNumberMaps_ * sizeof(u_int32_t);
  72. // Write each map.
  73. dest = files_serializer_.Write(module.files_, dest);
  74. dest = functions_serializer_.Write(module.functions_, dest);
  75. dest = pubsym_serializer_.Write(module.public_symbols_, dest);
  76. for (int i = 0; i < WindowsFrameInfo::STACK_INFO_LAST; ++i)
  77. dest = wfi_serializer_.Write(&(module.windows_frame_info_[i]), dest);
  78. dest = cfi_init_rules_serializer_.Write(module.cfi_initial_rules_, dest);
  79. dest = cfi_delta_rules_serializer_.Write(module.cfi_delta_rules_, dest);
  80. // Write a null terminator.
  81. dest = SimpleSerializer<char>::Write(0, dest);
  82. return dest;
  83. }
  84. char* ModuleSerializer::Serialize(
  85. const BasicSourceLineResolver::Module &module, unsigned int *size) {
  86. // Compute size of memory to allocate.
  87. unsigned int size_to_alloc = SizeOf(module);
  88. // Allocate memory for serialized data.
  89. char *serialized_data = new char[size_to_alloc];
  90. if (!serialized_data) {
  91. BPLOG(ERROR) << "ModuleSerializer: memory allocation failed, "
  92. << "size to alloc: " << size_to_alloc;
  93. if (size) *size = 0;
  94. return NULL;
  95. }
  96. // Write serialized data to allocated memory chunk.
  97. char *end_address = Write(module, serialized_data);
  98. // Verify the allocated memory size is equal to the size of data been written.
  99. unsigned int size_written =
  100. static_cast<unsigned int>(end_address - serialized_data);
  101. if (size_to_alloc != size_written) {
  102. BPLOG(ERROR) << "size_to_alloc differs from size_written: "
  103. << size_to_alloc << " vs " << size_written;
  104. }
  105. // Set size and return the start address of memory chunk.
  106. if (size)
  107. *size = size_to_alloc;
  108. return serialized_data;
  109. }
  110. bool ModuleSerializer::SerializeModuleAndLoadIntoFastResolver(
  111. const BasicSourceLineResolver::ModuleMap::const_iterator &iter,
  112. FastSourceLineResolver *fast_resolver) {
  113. BPLOG(INFO) << "Converting symbol " << iter->first.c_str();
  114. // Cast SourceLineResolverBase::Module* to BasicSourceLineResolver::Module*.
  115. BasicSourceLineResolver::Module* basic_module =
  116. dynamic_cast<BasicSourceLineResolver::Module*>(iter->second);
  117. unsigned int size = 0;
  118. scoped_array<char> symbol_data(Serialize(*basic_module, &size));
  119. if (!symbol_data.get()) {
  120. BPLOG(ERROR) << "Serialization failed for module: " << basic_module->name_;
  121. return false;
  122. }
  123. BPLOG(INFO) << "Serialized Symbol Size " << size;
  124. // Copy the data into string.
  125. // Must pass string to LoadModuleUsingMapBuffer(), instead of passing char* to
  126. // LoadModuleUsingMemoryBuffer(), becaused of data ownership/lifetime issue.
  127. string symbol_data_string(symbol_data.get(), size);
  128. symbol_data.reset();
  129. scoped_ptr<CodeModule> code_module(
  130. new BasicCodeModule(0, 0, iter->first, "", "", "", ""));
  131. return fast_resolver->LoadModuleUsingMapBuffer(code_module.get(),
  132. symbol_data_string);
  133. }
  134. void ModuleSerializer::ConvertAllModules(
  135. const BasicSourceLineResolver *basic_resolver,
  136. FastSourceLineResolver *fast_resolver) {
  137. // Check for NULL pointer.
  138. if (!basic_resolver || !fast_resolver)
  139. return;
  140. // Traverse module list in basic resolver.
  141. BasicSourceLineResolver::ModuleMap::const_iterator iter;
  142. iter = basic_resolver->modules_->begin();
  143. for (; iter != basic_resolver->modules_->end(); ++iter)
  144. SerializeModuleAndLoadIntoFastResolver(iter, fast_resolver);
  145. }
  146. bool ModuleSerializer::ConvertOneModule(
  147. const string &moduleid,
  148. const BasicSourceLineResolver *basic_resolver,
  149. FastSourceLineResolver *fast_resolver) {
  150. // Check for NULL pointer.
  151. if (!basic_resolver || !fast_resolver)
  152. return false;
  153. BasicSourceLineResolver::ModuleMap::const_iterator iter;
  154. iter = basic_resolver->modules_->find(moduleid);
  155. if (iter == basic_resolver->modules_->end())
  156. return false;
  157. return SerializeModuleAndLoadIntoFastResolver(iter, fast_resolver);
  158. }
  159. char* ModuleSerializer::SerializeSymbolFileData(
  160. const string &symbol_data, unsigned int *size) {
  161. scoped_ptr<BasicSourceLineResolver::Module> module(
  162. new BasicSourceLineResolver::Module("no name"));
  163. scoped_array<char> buffer(new char[symbol_data.size() + 1]);
  164. strcpy(buffer.get(), symbol_data.c_str());
  165. if (!module->LoadMapFromMemory(buffer.get())) {
  166. return NULL;
  167. }
  168. buffer.reset(NULL);
  169. return Serialize(*(module.get()), size);
  170. }
  171. } // namespace google_breakpad