michael@0: // Copyright (c) 2010, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // module_serializer.cc: ModuleSerializer implementation. michael@0: // michael@0: // See module_serializer.h for documentation. michael@0: // michael@0: // Author: Siyang Xie (lambxsy@google.com) michael@0: michael@0: #include "processor/module_serializer.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "processor/basic_code_module.h" michael@0: #include "processor/logging.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: // Definition of static member variable in SimplerSerializer, which michael@0: // is declared in file "simple_serializer-inl.h" michael@0: RangeMapSerializer< MemAddr, linked_ptr > michael@0: SimpleSerializer::range_map_serializer_; michael@0: michael@0: size_t ModuleSerializer::SizeOf(const BasicSourceLineResolver::Module &module) { michael@0: size_t total_size_alloc_ = 0; michael@0: michael@0: // Compute memory size for each map component in Module class. michael@0: int map_index = 0; michael@0: map_sizes_[map_index++] = files_serializer_.SizeOf(module.files_); michael@0: map_sizes_[map_index++] = functions_serializer_.SizeOf(module.functions_); michael@0: map_sizes_[map_index++] = pubsym_serializer_.SizeOf(module.public_symbols_); michael@0: for (int i = 0; i < WindowsFrameInfo::STACK_INFO_LAST; ++i) michael@0: map_sizes_[map_index++] = michael@0: wfi_serializer_.SizeOf(&(module.windows_frame_info_[i])); michael@0: map_sizes_[map_index++] = cfi_init_rules_serializer_.SizeOf( michael@0: module.cfi_initial_rules_); michael@0: map_sizes_[map_index++] = cfi_delta_rules_serializer_.SizeOf( michael@0: module.cfi_delta_rules_); michael@0: michael@0: // Header size. michael@0: total_size_alloc_ = kNumberMaps_ * sizeof(uint32_t); michael@0: michael@0: for (int i = 0; i < kNumberMaps_; ++i) michael@0: total_size_alloc_ += map_sizes_[i]; michael@0: michael@0: // Extra one byte for null terminator for C-string copy safety. michael@0: ++total_size_alloc_; michael@0: michael@0: return total_size_alloc_; michael@0: } michael@0: michael@0: char *ModuleSerializer::Write(const BasicSourceLineResolver::Module &module, michael@0: char *dest) { michael@0: // Write header. michael@0: memcpy(dest, map_sizes_, kNumberMaps_ * sizeof(uint32_t)); michael@0: dest += kNumberMaps_ * sizeof(uint32_t); michael@0: // Write each map. michael@0: dest = files_serializer_.Write(module.files_, dest); michael@0: dest = functions_serializer_.Write(module.functions_, dest); michael@0: dest = pubsym_serializer_.Write(module.public_symbols_, dest); michael@0: for (int i = 0; i < WindowsFrameInfo::STACK_INFO_LAST; ++i) michael@0: dest = wfi_serializer_.Write(&(module.windows_frame_info_[i]), dest); michael@0: dest = cfi_init_rules_serializer_.Write(module.cfi_initial_rules_, dest); michael@0: dest = cfi_delta_rules_serializer_.Write(module.cfi_delta_rules_, dest); michael@0: // Write a null terminator. michael@0: dest = SimpleSerializer::Write(0, dest); michael@0: return dest; michael@0: } michael@0: michael@0: char* ModuleSerializer::Serialize( michael@0: const BasicSourceLineResolver::Module &module, unsigned int *size) { michael@0: // Compute size of memory to allocate. michael@0: unsigned int size_to_alloc = SizeOf(module); michael@0: michael@0: // Allocate memory for serialized data. michael@0: char *serialized_data = new char[size_to_alloc]; michael@0: if (!serialized_data) { michael@0: BPLOG(ERROR) << "ModuleSerializer: memory allocation failed, " michael@0: << "size to alloc: " << size_to_alloc; michael@0: if (size) *size = 0; michael@0: return NULL; michael@0: } michael@0: michael@0: // Write serialized data to allocated memory chunk. michael@0: char *end_address = Write(module, serialized_data); michael@0: // Verify the allocated memory size is equal to the size of data been written. michael@0: unsigned int size_written = michael@0: static_cast(end_address - serialized_data); michael@0: if (size_to_alloc != size_written) { michael@0: BPLOG(ERROR) << "size_to_alloc differs from size_written: " michael@0: << size_to_alloc << " vs " << size_written; michael@0: } michael@0: michael@0: // Set size and return the start address of memory chunk. michael@0: if (size) michael@0: *size = size_to_alloc; michael@0: return serialized_data; michael@0: } michael@0: michael@0: bool ModuleSerializer::SerializeModuleAndLoadIntoFastResolver( michael@0: const BasicSourceLineResolver::ModuleMap::const_iterator &iter, michael@0: FastSourceLineResolver *fast_resolver) { michael@0: BPLOG(INFO) << "Converting symbol " << iter->first.c_str(); michael@0: michael@0: // Cast SourceLineResolverBase::Module* to BasicSourceLineResolver::Module*. michael@0: BasicSourceLineResolver::Module* basic_module = michael@0: dynamic_cast(iter->second); michael@0: michael@0: unsigned int size = 0; michael@0: scoped_array symbol_data(Serialize(*basic_module, &size)); michael@0: if (!symbol_data.get()) { michael@0: BPLOG(ERROR) << "Serialization failed for module: " << basic_module->name_; michael@0: return false; michael@0: } michael@0: BPLOG(INFO) << "Serialized Symbol Size " << size; michael@0: michael@0: // Copy the data into string. michael@0: // Must pass string to LoadModuleUsingMapBuffer(), instead of passing char* to michael@0: // LoadModuleUsingMemoryBuffer(), becaused of data ownership/lifetime issue. michael@0: string symbol_data_string(symbol_data.get(), size); michael@0: symbol_data.reset(); michael@0: michael@0: scoped_ptr code_module( michael@0: new BasicCodeModule(0, 0, iter->first, "", "", "", "")); michael@0: michael@0: return fast_resolver->LoadModuleUsingMapBuffer(code_module.get(), michael@0: symbol_data_string); michael@0: } michael@0: michael@0: void ModuleSerializer::ConvertAllModules( michael@0: const BasicSourceLineResolver *basic_resolver, michael@0: FastSourceLineResolver *fast_resolver) { michael@0: // Check for NULL pointer. michael@0: if (!basic_resolver || !fast_resolver) michael@0: return; michael@0: michael@0: // Traverse module list in basic resolver. michael@0: BasicSourceLineResolver::ModuleMap::const_iterator iter; michael@0: iter = basic_resolver->modules_->begin(); michael@0: for (; iter != basic_resolver->modules_->end(); ++iter) michael@0: SerializeModuleAndLoadIntoFastResolver(iter, fast_resolver); michael@0: } michael@0: michael@0: bool ModuleSerializer::ConvertOneModule( michael@0: const string &moduleid, michael@0: const BasicSourceLineResolver *basic_resolver, michael@0: FastSourceLineResolver *fast_resolver) { michael@0: // Check for NULL pointer. michael@0: if (!basic_resolver || !fast_resolver) michael@0: return false; michael@0: michael@0: BasicSourceLineResolver::ModuleMap::const_iterator iter; michael@0: iter = basic_resolver->modules_->find(moduleid); michael@0: if (iter == basic_resolver->modules_->end()) michael@0: return false; michael@0: michael@0: return SerializeModuleAndLoadIntoFastResolver(iter, fast_resolver); michael@0: } michael@0: michael@0: char* ModuleSerializer::SerializeSymbolFileData( michael@0: const string &symbol_data, unsigned int *size) { michael@0: scoped_ptr module( michael@0: new BasicSourceLineResolver::Module("no name")); michael@0: scoped_array buffer(new char[symbol_data.size() + 1]); michael@0: strcpy(buffer.get(), symbol_data.c_str()); michael@0: if (!module->LoadMapFromMemory(buffer.get())) { michael@0: return NULL; michael@0: } michael@0: buffer.reset(NULL); michael@0: return Serialize(*(module.get()), size); michael@0: } michael@0: michael@0: } // namespace google_breakpad