/ src / processor / module_serializer.h
module_serializer.h
  1  // Copyright 2010 Google LLC
  2  //
  3  // Redistribution and use in source and binary forms, with or without
  4  // modification, are permitted provided that the following conditions are
  5  // met:
  6  //
  7  //     * Redistributions of source code must retain the above copyright
  8  // notice, this list of conditions and the following disclaimer.
  9  //     * Redistributions in binary form must reproduce the above
 10  // copyright notice, this list of conditions and the following disclaimer
 11  // in the documentation and/or other materials provided with the
 12  // distribution.
 13  //     * Neither the name of Google LLC nor the names of its
 14  // contributors may be used to endorse or promote products derived from
 15  // this software without specific prior written permission.
 16  //
 17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  //
 29  // module_serializer.h: ModuleSerializer serializes a loaded symbol,
 30  // i.e., a loaded BasicSouceLineResolver::Module instance, into a memory
 31  // chunk of data. The serialized data can be read and loaded by
 32  // FastSourceLineResolver without CPU & memory-intensive parsing.
 33  //
 34  // Author: Siyang Xie (lambxsy@google.com)
 35  
 36  #ifndef PROCESSOR_MODULE_SERIALIZER_H__
 37  #define PROCESSOR_MODULE_SERIALIZER_H__
 38  
 39  #include <map>
 40  #include <string>
 41  
 42  #include "google_breakpad/processor/basic_source_line_resolver.h"
 43  #include "google_breakpad/processor/fast_source_line_resolver.h"
 44  #include "processor/basic_source_line_resolver_types.h"
 45  #include "processor/fast_source_line_resolver_types.h"
 46  #include "processor/linked_ptr.h"
 47  #include "processor/map_serializers-inl.h"
 48  #include "processor/simple_serializer-inl.h"
 49  #include "processor/windows_frame_info.h"
 50  
 51  namespace google_breakpad {
 52  
 53  // ModuleSerializer serializes a loaded BasicSourceLineResolver::Module into a
 54  // chunk of memory data. ModuleSerializer also provides interface to compute
 55  // memory size of the serialized data, write serialized data directly into
 56  // memory, convert ASCII format symbol data into serialized binary data, and
 57  // convert loaded BasicSourceLineResolver::Module into
 58  // FastSourceLineResolver::Module.
 59  class ModuleSerializer {
 60   public:
 61    // Compute the size of memory required to serialize a module.  Return the
 62    // total size needed for serialization.
 63    size_t SizeOf(const BasicSourceLineResolver::Module& module);
 64  
 65    // Write a module into an allocated memory chunk with required size.
 66    // Return the "end" of data, i.e., the address after the final byte of data.
 67    char* Write(const BasicSourceLineResolver::Module& module, char* dest);
 68  
 69    // Serializes a loaded Module object into a chunk of memory data and returns
 70    // the address of memory chunk.  If size != NULL, *size is set to the memory
 71    // size allocated for the serialized data.
 72    // Caller takes the ownership of the memory chunk (allocated on heap), and
 73    // owner should call delete [] to free the memory after use.
 74    char* Serialize(const BasicSourceLineResolver::Module& module,
 75                    size_t* size = nullptr);
 76  
 77    // Given the string format symbol_data, produces a chunk of serialized data.
 78    // Caller takes ownership of the serialized data (on heap), and owner should
 79    // call delete [] to free the memory after use.
 80    char* SerializeSymbolFileData(const string& symbol_data,
 81                                  size_t* size = nullptr);
 82  
 83    // Serializes one loaded module with given moduleid in the basic source line
 84    // resolver, and loads the serialized data into the fast source line resolver.
 85    // Return false if the basic source line doesn't have a module with the given
 86    // moduleid.
 87    bool ConvertOneModule(const string& moduleid,
 88                          const BasicSourceLineResolver* basic_resolver,
 89                          FastSourceLineResolver* fast_resolver);
 90  
 91    // Serializes all the loaded modules in a basic source line resolver, and
 92    // loads the serialized data into a fast source line resolver.
 93    void ConvertAllModules(const BasicSourceLineResolver* basic_resolver,
 94                           FastSourceLineResolver* fast_resolver);
 95  
 96   private:
 97    // Convenient type names.
 98    typedef BasicSourceLineResolver::Line Line;
 99    typedef BasicSourceLineResolver::Function Function;
100    typedef BasicSourceLineResolver::PublicSymbol PublicSymbol;
101    typedef BasicSourceLineResolver::InlineOrigin InlineOrigin;
102  
103    // Internal implementation for ConvertOneModule and ConvertAllModules methods.
104    bool SerializeModuleAndLoadIntoFastResolver(
105        const BasicSourceLineResolver::ModuleMap::const_iterator& iter,
106        FastSourceLineResolver* fast_resolver);
107  
108    // Number of Maps that Module class contains.
109    static const int32_t kNumberMaps_ =
110        FastSourceLineResolver::Module::kNumberMaps_;
111  
112    // Memory sizes required to serialize map components in Module.
113    uint32_t map_sizes_[kNumberMaps_];
114  
115    // Serializers for each individual map component in Module class.
116    StdMapSerializer<int, string> files_serializer_;
117    RangeMapSerializer<MemAddr, linked_ptr<Function> > functions_serializer_;
118    AddressMapSerializer<MemAddr, linked_ptr<PublicSymbol> > pubsym_serializer_;
119    ContainedRangeMapSerializer<MemAddr,
120                                linked_ptr<WindowsFrameInfo> > wfi_serializer_;
121    RangeMapSerializer<MemAddr, string> cfi_init_rules_serializer_;
122    StdMapSerializer<MemAddr, string> cfi_delta_rules_serializer_;
123    StdMapSerializer<int, linked_ptr<InlineOrigin>> inline_origin_serializer_;
124  };
125  
126  }  // namespace google_breakpad
127  
128  #endif  // PROCESSOR_MODULE_SERIALIZER_H__