1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/crashreporter/google-breakpad/src/processor/module_serializer.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,200 @@ 1.4 +// Copyright (c) 2010, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 +// 1.33 +// module_serializer.cc: ModuleSerializer implementation. 1.34 +// 1.35 +// See module_serializer.h for documentation. 1.36 +// 1.37 +// Author: Siyang Xie (lambxsy@google.com) 1.38 + 1.39 +#include "processor/module_serializer.h" 1.40 + 1.41 +#include <map> 1.42 +#include <string> 1.43 + 1.44 +#include "processor/basic_code_module.h" 1.45 +#include "processor/logging.h" 1.46 + 1.47 +namespace google_breakpad { 1.48 + 1.49 +// Definition of static member variable in SimplerSerializer<Funcion>, which 1.50 +// is declared in file "simple_serializer-inl.h" 1.51 +RangeMapSerializer< MemAddr, linked_ptr<BasicSourceLineResolver::Line> > 1.52 +SimpleSerializer<BasicSourceLineResolver::Function>::range_map_serializer_; 1.53 + 1.54 +size_t ModuleSerializer::SizeOf(const BasicSourceLineResolver::Module &module) { 1.55 + size_t total_size_alloc_ = 0; 1.56 + 1.57 + // Compute memory size for each map component in Module class. 1.58 + int map_index = 0; 1.59 + map_sizes_[map_index++] = files_serializer_.SizeOf(module.files_); 1.60 + map_sizes_[map_index++] = functions_serializer_.SizeOf(module.functions_); 1.61 + map_sizes_[map_index++] = pubsym_serializer_.SizeOf(module.public_symbols_); 1.62 + for (int i = 0; i < WindowsFrameInfo::STACK_INFO_LAST; ++i) 1.63 + map_sizes_[map_index++] = 1.64 + wfi_serializer_.SizeOf(&(module.windows_frame_info_[i])); 1.65 + map_sizes_[map_index++] = cfi_init_rules_serializer_.SizeOf( 1.66 + module.cfi_initial_rules_); 1.67 + map_sizes_[map_index++] = cfi_delta_rules_serializer_.SizeOf( 1.68 + module.cfi_delta_rules_); 1.69 + 1.70 + // Header size. 1.71 + total_size_alloc_ = kNumberMaps_ * sizeof(uint32_t); 1.72 + 1.73 + for (int i = 0; i < kNumberMaps_; ++i) 1.74 + total_size_alloc_ += map_sizes_[i]; 1.75 + 1.76 + // Extra one byte for null terminator for C-string copy safety. 1.77 + ++total_size_alloc_; 1.78 + 1.79 + return total_size_alloc_; 1.80 +} 1.81 + 1.82 +char *ModuleSerializer::Write(const BasicSourceLineResolver::Module &module, 1.83 + char *dest) { 1.84 + // Write header. 1.85 + memcpy(dest, map_sizes_, kNumberMaps_ * sizeof(uint32_t)); 1.86 + dest += kNumberMaps_ * sizeof(uint32_t); 1.87 + // Write each map. 1.88 + dest = files_serializer_.Write(module.files_, dest); 1.89 + dest = functions_serializer_.Write(module.functions_, dest); 1.90 + dest = pubsym_serializer_.Write(module.public_symbols_, dest); 1.91 + for (int i = 0; i < WindowsFrameInfo::STACK_INFO_LAST; ++i) 1.92 + dest = wfi_serializer_.Write(&(module.windows_frame_info_[i]), dest); 1.93 + dest = cfi_init_rules_serializer_.Write(module.cfi_initial_rules_, dest); 1.94 + dest = cfi_delta_rules_serializer_.Write(module.cfi_delta_rules_, dest); 1.95 + // Write a null terminator. 1.96 + dest = SimpleSerializer<char>::Write(0, dest); 1.97 + return dest; 1.98 +} 1.99 + 1.100 +char* ModuleSerializer::Serialize( 1.101 + const BasicSourceLineResolver::Module &module, unsigned int *size) { 1.102 + // Compute size of memory to allocate. 1.103 + unsigned int size_to_alloc = SizeOf(module); 1.104 + 1.105 + // Allocate memory for serialized data. 1.106 + char *serialized_data = new char[size_to_alloc]; 1.107 + if (!serialized_data) { 1.108 + BPLOG(ERROR) << "ModuleSerializer: memory allocation failed, " 1.109 + << "size to alloc: " << size_to_alloc; 1.110 + if (size) *size = 0; 1.111 + return NULL; 1.112 + } 1.113 + 1.114 + // Write serialized data to allocated memory chunk. 1.115 + char *end_address = Write(module, serialized_data); 1.116 + // Verify the allocated memory size is equal to the size of data been written. 1.117 + unsigned int size_written = 1.118 + static_cast<unsigned int>(end_address - serialized_data); 1.119 + if (size_to_alloc != size_written) { 1.120 + BPLOG(ERROR) << "size_to_alloc differs from size_written: " 1.121 + << size_to_alloc << " vs " << size_written; 1.122 + } 1.123 + 1.124 + // Set size and return the start address of memory chunk. 1.125 + if (size) 1.126 + *size = size_to_alloc; 1.127 + return serialized_data; 1.128 +} 1.129 + 1.130 +bool ModuleSerializer::SerializeModuleAndLoadIntoFastResolver( 1.131 + const BasicSourceLineResolver::ModuleMap::const_iterator &iter, 1.132 + FastSourceLineResolver *fast_resolver) { 1.133 + BPLOG(INFO) << "Converting symbol " << iter->first.c_str(); 1.134 + 1.135 + // Cast SourceLineResolverBase::Module* to BasicSourceLineResolver::Module*. 1.136 + BasicSourceLineResolver::Module* basic_module = 1.137 + dynamic_cast<BasicSourceLineResolver::Module*>(iter->second); 1.138 + 1.139 + unsigned int size = 0; 1.140 + scoped_array<char> symbol_data(Serialize(*basic_module, &size)); 1.141 + if (!symbol_data.get()) { 1.142 + BPLOG(ERROR) << "Serialization failed for module: " << basic_module->name_; 1.143 + return false; 1.144 + } 1.145 + BPLOG(INFO) << "Serialized Symbol Size " << size; 1.146 + 1.147 + // Copy the data into string. 1.148 + // Must pass string to LoadModuleUsingMapBuffer(), instead of passing char* to 1.149 + // LoadModuleUsingMemoryBuffer(), becaused of data ownership/lifetime issue. 1.150 + string symbol_data_string(symbol_data.get(), size); 1.151 + symbol_data.reset(); 1.152 + 1.153 + scoped_ptr<CodeModule> code_module( 1.154 + new BasicCodeModule(0, 0, iter->first, "", "", "", "")); 1.155 + 1.156 + return fast_resolver->LoadModuleUsingMapBuffer(code_module.get(), 1.157 + symbol_data_string); 1.158 +} 1.159 + 1.160 +void ModuleSerializer::ConvertAllModules( 1.161 + const BasicSourceLineResolver *basic_resolver, 1.162 + FastSourceLineResolver *fast_resolver) { 1.163 + // Check for NULL pointer. 1.164 + if (!basic_resolver || !fast_resolver) 1.165 + return; 1.166 + 1.167 + // Traverse module list in basic resolver. 1.168 + BasicSourceLineResolver::ModuleMap::const_iterator iter; 1.169 + iter = basic_resolver->modules_->begin(); 1.170 + for (; iter != basic_resolver->modules_->end(); ++iter) 1.171 + SerializeModuleAndLoadIntoFastResolver(iter, fast_resolver); 1.172 +} 1.173 + 1.174 +bool ModuleSerializer::ConvertOneModule( 1.175 + const string &moduleid, 1.176 + const BasicSourceLineResolver *basic_resolver, 1.177 + FastSourceLineResolver *fast_resolver) { 1.178 + // Check for NULL pointer. 1.179 + if (!basic_resolver || !fast_resolver) 1.180 + return false; 1.181 + 1.182 + BasicSourceLineResolver::ModuleMap::const_iterator iter; 1.183 + iter = basic_resolver->modules_->find(moduleid); 1.184 + if (iter == basic_resolver->modules_->end()) 1.185 + return false; 1.186 + 1.187 + return SerializeModuleAndLoadIntoFastResolver(iter, fast_resolver); 1.188 +} 1.189 + 1.190 +char* ModuleSerializer::SerializeSymbolFileData( 1.191 + const string &symbol_data, unsigned int *size) { 1.192 + scoped_ptr<BasicSourceLineResolver::Module> module( 1.193 + new BasicSourceLineResolver::Module("no name")); 1.194 + scoped_array<char> buffer(new char[symbol_data.size() + 1]); 1.195 + strcpy(buffer.get(), symbol_data.c_str()); 1.196 + if (!module->LoadMapFromMemory(buffer.get())) { 1.197 + return NULL; 1.198 + } 1.199 + buffer.reset(NULL); 1.200 + return Serialize(*(module.get()), size); 1.201 +} 1.202 + 1.203 +} // namespace google_breakpad