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: // simple_serializer-inl.h: template specializations for following types: michael@0: // bool, const char *(C-string), string, michael@0: // Line, Function, PublicSymbol, WindowsFrameInfo and their linked pointers. michael@0: // michael@0: // See simple_serializer.h for moredocumentation. michael@0: // michael@0: // Author: Siyang Xie (lambxsy@google.com) michael@0: michael@0: #ifndef PROCESSOR_SIMPLE_SERIALIZER_INL_H__ michael@0: #define PROCESSOR_SIMPLE_SERIALIZER_INL_H__ michael@0: michael@0: #include michael@0: michael@0: #include "processor/simple_serializer.h" michael@0: #include "map_serializers-inl.h" michael@0: michael@0: #include "google_breakpad/processor/basic_source_line_resolver.h" michael@0: #include "processor/basic_source_line_resolver_types.h" michael@0: #include "processor/linked_ptr.h" michael@0: #include "processor/windows_frame_info.h" michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: // Specializations of SimpleSerializer: bool michael@0: template<> michael@0: class SimpleSerializer { michael@0: public: michael@0: static size_t SizeOf(bool boolean) { return 1; } michael@0: michael@0: static char *Write(bool boolean, char *dest) { michael@0: *dest = static_cast(boolean? 255 : 0); michael@0: return ++dest; michael@0: } michael@0: }; michael@0: michael@0: // Specializations of SimpleSerializer: string michael@0: template<> michael@0: class SimpleSerializer { michael@0: public: michael@0: static size_t SizeOf(const string &str) { return str.size() + 1; } michael@0: michael@0: static char *Write(const string &str, char *dest) { michael@0: strcpy(dest, str.c_str()); michael@0: return dest + SizeOf(str); michael@0: } michael@0: }; michael@0: michael@0: // Specializations of SimpleSerializer: C-string michael@0: template<> michael@0: class SimpleSerializer { michael@0: public: michael@0: static size_t SizeOf(const char *cstring) { michael@0: return strlen(cstring) + 1; michael@0: } michael@0: michael@0: static char *Write(const char *cstring, char *dest) { michael@0: strcpy(dest, cstring); michael@0: return dest + SizeOf(cstring); michael@0: } michael@0: }; michael@0: michael@0: // Specializations of SimpleSerializer: Line michael@0: template<> michael@0: class SimpleSerializer { michael@0: typedef BasicSourceLineResolver::Line Line; michael@0: public: michael@0: static size_t SizeOf(const Line &line) { michael@0: return SimpleSerializer::SizeOf(line.address) michael@0: + SimpleSerializer::SizeOf(line.size) michael@0: + SimpleSerializer::SizeOf(line.source_file_id) michael@0: + SimpleSerializer::SizeOf(line.line); michael@0: } michael@0: static char *Write(const Line &line, char *dest) { michael@0: dest = SimpleSerializer::Write(line.address, dest); michael@0: dest = SimpleSerializer::Write(line.size, dest); michael@0: dest = SimpleSerializer::Write(line.source_file_id, dest); michael@0: dest = SimpleSerializer::Write(line.line, dest); michael@0: return dest; michael@0: } michael@0: }; michael@0: michael@0: // Specializations of SimpleSerializer: PublicSymbol michael@0: template<> michael@0: class SimpleSerializer { michael@0: typedef BasicSourceLineResolver::PublicSymbol PublicSymbol; michael@0: public: michael@0: static size_t SizeOf(const PublicSymbol &pubsymbol) { michael@0: return SimpleSerializer::SizeOf(pubsymbol.name) michael@0: + SimpleSerializer::SizeOf(pubsymbol.address) michael@0: + SimpleSerializer::SizeOf(pubsymbol.parameter_size); michael@0: } michael@0: static char *Write(const PublicSymbol &pubsymbol, char *dest) { michael@0: dest = SimpleSerializer::Write(pubsymbol.name, dest); michael@0: dest = SimpleSerializer::Write(pubsymbol.address, dest); michael@0: dest = SimpleSerializer::Write(pubsymbol.parameter_size, dest); michael@0: return dest; michael@0: } michael@0: }; michael@0: michael@0: // Specializations of SimpleSerializer: WindowsFrameInfo michael@0: template<> michael@0: class SimpleSerializer { michael@0: public: michael@0: static size_t SizeOf(const WindowsFrameInfo &wfi) { michael@0: unsigned int size = 0; michael@0: size += sizeof(int32_t); // wfi.type_ michael@0: size += SimpleSerializer::SizeOf(wfi.valid); michael@0: size += SimpleSerializer::SizeOf(wfi.prolog_size); michael@0: size += SimpleSerializer::SizeOf(wfi.epilog_size); michael@0: size += SimpleSerializer::SizeOf(wfi.parameter_size); michael@0: size += SimpleSerializer::SizeOf(wfi.saved_register_size); michael@0: size += SimpleSerializer::SizeOf(wfi.local_size); michael@0: size += SimpleSerializer::SizeOf(wfi.max_stack_size); michael@0: size += SimpleSerializer::SizeOf(wfi.allocates_base_pointer); michael@0: size += SimpleSerializer::SizeOf(wfi.program_string); michael@0: return size; michael@0: } michael@0: static char *Write(const WindowsFrameInfo &wfi, char *dest) { michael@0: dest = SimpleSerializer::Write( michael@0: static_cast(wfi.type_), dest); michael@0: dest = SimpleSerializer::Write(wfi.valid, dest); michael@0: dest = SimpleSerializer::Write(wfi.prolog_size, dest); michael@0: dest = SimpleSerializer::Write(wfi.epilog_size, dest); michael@0: dest = SimpleSerializer::Write(wfi.parameter_size, dest); michael@0: dest = SimpleSerializer::Write(wfi.saved_register_size, dest); michael@0: dest = SimpleSerializer::Write(wfi.local_size, dest); michael@0: dest = SimpleSerializer::Write(wfi.max_stack_size, dest); michael@0: dest = SimpleSerializer::Write(wfi.allocates_base_pointer, dest); michael@0: return SimpleSerializer::Write(wfi.program_string, dest); michael@0: } michael@0: }; michael@0: michael@0: // Specializations of SimpleSerializer: Linked_ptr version of michael@0: // Line, Function, PublicSymbol, WindowsFrameInfo. michael@0: template<> michael@0: class SimpleSerializer< linked_ptr > { michael@0: typedef BasicSourceLineResolver::Line Line; michael@0: public: michael@0: static size_t SizeOf(const linked_ptr &lineptr) { michael@0: if (lineptr.get() == NULL) return 0; michael@0: return SimpleSerializer::SizeOf(*(lineptr.get())); michael@0: } michael@0: static char *Write(const linked_ptr &lineptr, char *dest) { michael@0: if (lineptr.get()) michael@0: dest = SimpleSerializer::Write(*(lineptr.get()), dest); michael@0: return dest; michael@0: } michael@0: }; michael@0: michael@0: template<> michael@0: class SimpleSerializer { michael@0: // Convenient type names. michael@0: typedef BasicSourceLineResolver::Function Function; michael@0: typedef BasicSourceLineResolver::Line Line; michael@0: public: michael@0: static size_t SizeOf(const Function &func) { michael@0: unsigned int size = 0; michael@0: size += SimpleSerializer::SizeOf(func.name); michael@0: size += SimpleSerializer::SizeOf(func.address); michael@0: size += SimpleSerializer::SizeOf(func.size); michael@0: size += SimpleSerializer::SizeOf(func.parameter_size); michael@0: size += range_map_serializer_.SizeOf(func.lines); michael@0: return size; michael@0: } michael@0: michael@0: static char *Write(const Function &func, char *dest) { michael@0: dest = SimpleSerializer::Write(func.name, dest); michael@0: dest = SimpleSerializer::Write(func.address, dest); michael@0: dest = SimpleSerializer::Write(func.size, dest); michael@0: dest = SimpleSerializer::Write(func.parameter_size, dest); michael@0: dest = range_map_serializer_.Write(func.lines, dest); michael@0: return dest; michael@0: } michael@0: private: michael@0: // This static member is defined in module_serializer.cc. michael@0: static RangeMapSerializer< MemAddr, linked_ptr > range_map_serializer_; michael@0: }; michael@0: michael@0: template<> michael@0: class SimpleSerializer< linked_ptr > { michael@0: typedef BasicSourceLineResolver::Function Function; michael@0: public: michael@0: static size_t SizeOf(const linked_ptr &func) { michael@0: if (!func.get()) return 0; michael@0: return SimpleSerializer::SizeOf(*(func.get())); michael@0: } michael@0: michael@0: static char *Write(const linked_ptr &func, char *dest) { michael@0: if (func.get()) michael@0: dest = SimpleSerializer::Write(*(func.get()), dest); michael@0: return dest; michael@0: } michael@0: }; michael@0: michael@0: template<> michael@0: class SimpleSerializer< linked_ptr > { michael@0: typedef BasicSourceLineResolver::PublicSymbol PublicSymbol; michael@0: public: michael@0: static size_t SizeOf(const linked_ptr &pubsymbol) { michael@0: if (pubsymbol.get() == NULL) return 0; michael@0: return SimpleSerializer::SizeOf(*(pubsymbol.get())); michael@0: } michael@0: static char *Write(const linked_ptr &pubsymbol, char *dest) { michael@0: if (pubsymbol.get()) michael@0: dest = SimpleSerializer::Write(*(pubsymbol.get()), dest); michael@0: return dest; michael@0: } michael@0: }; michael@0: michael@0: template<> michael@0: class SimpleSerializer< linked_ptr > { michael@0: public: michael@0: static size_t SizeOf(const linked_ptr &wfi) { michael@0: if (wfi.get() == NULL) return 0; michael@0: return SimpleSerializer::SizeOf(*(wfi.get())); michael@0: } michael@0: static char *Write(const linked_ptr &wfi, char *dest) { michael@0: if (wfi.get()) michael@0: dest = SimpleSerializer::Write(*(wfi.get()), dest); michael@0: return dest; michael@0: } michael@0: }; michael@0: michael@0: } // namespace google_breakpad michael@0: michael@0: #endif // PROCESSOR_SIMPLE_SERIALIZER_INL_H__