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.h: ModuleSerializer serializes a loaded symbol,
31 // i.e., a loaded BasicSouceLineResolver::Module instance, into a memory
32 // chunk of data. The serialized data can be read and loaded by
33 // FastSourceLineResolver without CPU & memory-intensive parsing.
34 //
35 // Author: Siyang Xie (lambxsy@google.com)
36 
37 #ifndef PROCESSOR_MODULE_SERIALIZER_H__
38 #define PROCESSOR_MODULE_SERIALIZER_H__
39 
40 #include <map>
41 #include <string>
42 
43 #include "google_breakpad/processor/basic_source_line_resolver.h"
44 #include "google_breakpad/processor/fast_source_line_resolver.h"
45 #include "processor/basic_source_line_resolver_types.h"
46 #include "processor/fast_source_line_resolver_types.h"
47 #include "processor/linked_ptr.h"
48 #include "processor/map_serializers-inl.h"
49 #include "processor/simple_serializer-inl.h"
50 #include "processor/windows_frame_info.h"
51 
52 namespace google_breakpad {
53 
54 // ModuleSerializer serializes a loaded BasicSourceLineResolver::Module into a
55 // chunk of memory data. ModuleSerializer also provides interface to compute
56 // memory size of the serialized data, write serialized data directly into
57 // memory, convert ASCII format symbol data into serialized binary data, and
58 // convert loaded BasicSourceLineResolver::Module into
59 // FastSourceLineResolver::Module.
60 class ModuleSerializer {
61  public:
62   // Compute the size of memory required to serialize a module.  Return the
63   // total size needed for serialization.
64   size_t SizeOf(const BasicSourceLineResolver::Module &module);
65 
66   // Write a module into an allocated memory chunk with required size.
67   // Return the "end" of data, i.e., the address after the final byte of data.
68   char* Write(const BasicSourceLineResolver::Module &module, char *dest);
69 
70   // Serializes a loaded Module object into a chunk of memory data and returns
71   // the address of memory chunk.  If size != NULL, *size is set to the memory
72   // size allocated for the serialized data.
73   // Caller takes the ownership of the memory chunk (allocated on heap), and
74   // owner should call delete [] to free the memory after use.
75   char* Serialize(const BasicSourceLineResolver::Module &module,
76                   unsigned int *size = NULL);
77 
78   // Given the string format symbol_data, produces a chunk of serialized data.
79   // Caller takes ownership of the serialized data (on heap), and owner should
80   // call delete [] to free the memory after use.
81   char* SerializeSymbolFileData(const string &symbol_data,
82                                 unsigned int *size = NULL);
83 
84   // Serializes one loaded module with given moduleid in the basic source line
85   // resolver, and loads the serialized data into the fast source line resolver.
86   // Return false if the basic source line doesn't have a module with the given
87   // moduleid.
88   bool ConvertOneModule(const string &moduleid,
89                         const BasicSourceLineResolver *basic_resolver,
90                         FastSourceLineResolver *fast_resolver);
91 
92   // Serializes all the loaded modules in a basic source line resolver, and
93   // loads the serialized data into a fast source line resolver.
94   void ConvertAllModules(const BasicSourceLineResolver *basic_resolver,
95                          FastSourceLineResolver *fast_resolver);
96 
97  private:
98   // Convenient type names.
99   typedef BasicSourceLineResolver::Line Line;
100   typedef BasicSourceLineResolver::Function Function;
101   typedef BasicSourceLineResolver::PublicSymbol PublicSymbol;
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 };
124 
125 }  // namespace google_breakpad
126 
127 #endif  // PROCESSOR_MODULE_SERIALIZER_H__
128