|
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 // simple_serializer-inl.h: template specializations for following types: |
|
31 // bool, const char *(C-string), string, |
|
32 // Line, Function, PublicSymbol, WindowsFrameInfo and their linked pointers. |
|
33 // |
|
34 // See simple_serializer.h for moredocumentation. |
|
35 // |
|
36 // Author: Siyang Xie (lambxsy@google.com) |
|
37 |
|
38 #ifndef PROCESSOR_SIMPLE_SERIALIZER_INL_H__ |
|
39 #define PROCESSOR_SIMPLE_SERIALIZER_INL_H__ |
|
40 |
|
41 #include <string> |
|
42 |
|
43 #include "processor/simple_serializer.h" |
|
44 #include "map_serializers-inl.h" |
|
45 |
|
46 #include "google_breakpad/processor/basic_source_line_resolver.h" |
|
47 #include "processor/basic_source_line_resolver_types.h" |
|
48 #include "processor/linked_ptr.h" |
|
49 #include "processor/windows_frame_info.h" |
|
50 |
|
51 namespace google_breakpad { |
|
52 |
|
53 // Specializations of SimpleSerializer: bool |
|
54 template<> |
|
55 class SimpleSerializer<bool> { |
|
56 public: |
|
57 static size_t SizeOf(bool boolean) { return 1; } |
|
58 |
|
59 static char *Write(bool boolean, char *dest) { |
|
60 *dest = static_cast<char>(boolean? 255 : 0); |
|
61 return ++dest; |
|
62 } |
|
63 }; |
|
64 |
|
65 // Specializations of SimpleSerializer: string |
|
66 template<> |
|
67 class SimpleSerializer<string> { |
|
68 public: |
|
69 static size_t SizeOf(const string &str) { return str.size() + 1; } |
|
70 |
|
71 static char *Write(const string &str, char *dest) { |
|
72 strcpy(dest, str.c_str()); |
|
73 return dest + SizeOf(str); |
|
74 } |
|
75 }; |
|
76 |
|
77 // Specializations of SimpleSerializer: C-string |
|
78 template<> |
|
79 class SimpleSerializer<const char*> { |
|
80 public: |
|
81 static size_t SizeOf(const char *cstring) { |
|
82 return strlen(cstring) + 1; |
|
83 } |
|
84 |
|
85 static char *Write(const char *cstring, char *dest) { |
|
86 strcpy(dest, cstring); |
|
87 return dest + SizeOf(cstring); |
|
88 } |
|
89 }; |
|
90 |
|
91 // Specializations of SimpleSerializer: Line |
|
92 template<> |
|
93 class SimpleSerializer<BasicSourceLineResolver::Line> { |
|
94 typedef BasicSourceLineResolver::Line Line; |
|
95 public: |
|
96 static size_t SizeOf(const Line &line) { |
|
97 return SimpleSerializer<MemAddr>::SizeOf(line.address) |
|
98 + SimpleSerializer<MemAddr>::SizeOf(line.size) |
|
99 + SimpleSerializer<int32_t>::SizeOf(line.source_file_id) |
|
100 + SimpleSerializer<int32_t>::SizeOf(line.line); |
|
101 } |
|
102 static char *Write(const Line &line, char *dest) { |
|
103 dest = SimpleSerializer<MemAddr>::Write(line.address, dest); |
|
104 dest = SimpleSerializer<MemAddr>::Write(line.size, dest); |
|
105 dest = SimpleSerializer<int32_t>::Write(line.source_file_id, dest); |
|
106 dest = SimpleSerializer<int32_t>::Write(line.line, dest); |
|
107 return dest; |
|
108 } |
|
109 }; |
|
110 |
|
111 // Specializations of SimpleSerializer: PublicSymbol |
|
112 template<> |
|
113 class SimpleSerializer<BasicSourceLineResolver::PublicSymbol> { |
|
114 typedef BasicSourceLineResolver::PublicSymbol PublicSymbol; |
|
115 public: |
|
116 static size_t SizeOf(const PublicSymbol &pubsymbol) { |
|
117 return SimpleSerializer<string>::SizeOf(pubsymbol.name) |
|
118 + SimpleSerializer<MemAddr>::SizeOf(pubsymbol.address) |
|
119 + SimpleSerializer<int32_t>::SizeOf(pubsymbol.parameter_size); |
|
120 } |
|
121 static char *Write(const PublicSymbol &pubsymbol, char *dest) { |
|
122 dest = SimpleSerializer<string>::Write(pubsymbol.name, dest); |
|
123 dest = SimpleSerializer<MemAddr>::Write(pubsymbol.address, dest); |
|
124 dest = SimpleSerializer<int32_t>::Write(pubsymbol.parameter_size, dest); |
|
125 return dest; |
|
126 } |
|
127 }; |
|
128 |
|
129 // Specializations of SimpleSerializer: WindowsFrameInfo |
|
130 template<> |
|
131 class SimpleSerializer<WindowsFrameInfo> { |
|
132 public: |
|
133 static size_t SizeOf(const WindowsFrameInfo &wfi) { |
|
134 unsigned int size = 0; |
|
135 size += sizeof(int32_t); // wfi.type_ |
|
136 size += SimpleSerializer<int32_t>::SizeOf(wfi.valid); |
|
137 size += SimpleSerializer<uint32_t>::SizeOf(wfi.prolog_size); |
|
138 size += SimpleSerializer<uint32_t>::SizeOf(wfi.epilog_size); |
|
139 size += SimpleSerializer<uint32_t>::SizeOf(wfi.parameter_size); |
|
140 size += SimpleSerializer<uint32_t>::SizeOf(wfi.saved_register_size); |
|
141 size += SimpleSerializer<uint32_t>::SizeOf(wfi.local_size); |
|
142 size += SimpleSerializer<uint32_t>::SizeOf(wfi.max_stack_size); |
|
143 size += SimpleSerializer<bool>::SizeOf(wfi.allocates_base_pointer); |
|
144 size += SimpleSerializer<string>::SizeOf(wfi.program_string); |
|
145 return size; |
|
146 } |
|
147 static char *Write(const WindowsFrameInfo &wfi, char *dest) { |
|
148 dest = SimpleSerializer<int32_t>::Write( |
|
149 static_cast<const int32_t>(wfi.type_), dest); |
|
150 dest = SimpleSerializer<int32_t>::Write(wfi.valid, dest); |
|
151 dest = SimpleSerializer<uint32_t>::Write(wfi.prolog_size, dest); |
|
152 dest = SimpleSerializer<uint32_t>::Write(wfi.epilog_size, dest); |
|
153 dest = SimpleSerializer<uint32_t>::Write(wfi.parameter_size, dest); |
|
154 dest = SimpleSerializer<uint32_t>::Write(wfi.saved_register_size, dest); |
|
155 dest = SimpleSerializer<uint32_t>::Write(wfi.local_size, dest); |
|
156 dest = SimpleSerializer<uint32_t>::Write(wfi.max_stack_size, dest); |
|
157 dest = SimpleSerializer<bool>::Write(wfi.allocates_base_pointer, dest); |
|
158 return SimpleSerializer<string>::Write(wfi.program_string, dest); |
|
159 } |
|
160 }; |
|
161 |
|
162 // Specializations of SimpleSerializer: Linked_ptr version of |
|
163 // Line, Function, PublicSymbol, WindowsFrameInfo. |
|
164 template<> |
|
165 class SimpleSerializer< linked_ptr<BasicSourceLineResolver::Line> > { |
|
166 typedef BasicSourceLineResolver::Line Line; |
|
167 public: |
|
168 static size_t SizeOf(const linked_ptr<Line> &lineptr) { |
|
169 if (lineptr.get() == NULL) return 0; |
|
170 return SimpleSerializer<Line>::SizeOf(*(lineptr.get())); |
|
171 } |
|
172 static char *Write(const linked_ptr<Line> &lineptr, char *dest) { |
|
173 if (lineptr.get()) |
|
174 dest = SimpleSerializer<Line>::Write(*(lineptr.get()), dest); |
|
175 return dest; |
|
176 } |
|
177 }; |
|
178 |
|
179 template<> |
|
180 class SimpleSerializer<BasicSourceLineResolver::Function> { |
|
181 // Convenient type names. |
|
182 typedef BasicSourceLineResolver::Function Function; |
|
183 typedef BasicSourceLineResolver::Line Line; |
|
184 public: |
|
185 static size_t SizeOf(const Function &func) { |
|
186 unsigned int size = 0; |
|
187 size += SimpleSerializer<string>::SizeOf(func.name); |
|
188 size += SimpleSerializer<MemAddr>::SizeOf(func.address); |
|
189 size += SimpleSerializer<MemAddr>::SizeOf(func.size); |
|
190 size += SimpleSerializer<int32_t>::SizeOf(func.parameter_size); |
|
191 size += range_map_serializer_.SizeOf(func.lines); |
|
192 return size; |
|
193 } |
|
194 |
|
195 static char *Write(const Function &func, char *dest) { |
|
196 dest = SimpleSerializer<string>::Write(func.name, dest); |
|
197 dest = SimpleSerializer<MemAddr>::Write(func.address, dest); |
|
198 dest = SimpleSerializer<MemAddr>::Write(func.size, dest); |
|
199 dest = SimpleSerializer<int32_t>::Write(func.parameter_size, dest); |
|
200 dest = range_map_serializer_.Write(func.lines, dest); |
|
201 return dest; |
|
202 } |
|
203 private: |
|
204 // This static member is defined in module_serializer.cc. |
|
205 static RangeMapSerializer< MemAddr, linked_ptr<Line> > range_map_serializer_; |
|
206 }; |
|
207 |
|
208 template<> |
|
209 class SimpleSerializer< linked_ptr<BasicSourceLineResolver::Function> > { |
|
210 typedef BasicSourceLineResolver::Function Function; |
|
211 public: |
|
212 static size_t SizeOf(const linked_ptr<Function> &func) { |
|
213 if (!func.get()) return 0; |
|
214 return SimpleSerializer<Function>::SizeOf(*(func.get())); |
|
215 } |
|
216 |
|
217 static char *Write(const linked_ptr<Function> &func, char *dest) { |
|
218 if (func.get()) |
|
219 dest = SimpleSerializer<Function>::Write(*(func.get()), dest); |
|
220 return dest; |
|
221 } |
|
222 }; |
|
223 |
|
224 template<> |
|
225 class SimpleSerializer< linked_ptr<BasicSourceLineResolver::PublicSymbol> > { |
|
226 typedef BasicSourceLineResolver::PublicSymbol PublicSymbol; |
|
227 public: |
|
228 static size_t SizeOf(const linked_ptr<PublicSymbol> &pubsymbol) { |
|
229 if (pubsymbol.get() == NULL) return 0; |
|
230 return SimpleSerializer<PublicSymbol>::SizeOf(*(pubsymbol.get())); |
|
231 } |
|
232 static char *Write(const linked_ptr<PublicSymbol> &pubsymbol, char *dest) { |
|
233 if (pubsymbol.get()) |
|
234 dest = SimpleSerializer<PublicSymbol>::Write(*(pubsymbol.get()), dest); |
|
235 return dest; |
|
236 } |
|
237 }; |
|
238 |
|
239 template<> |
|
240 class SimpleSerializer< linked_ptr<WindowsFrameInfo> > { |
|
241 public: |
|
242 static size_t SizeOf(const linked_ptr<WindowsFrameInfo> &wfi) { |
|
243 if (wfi.get() == NULL) return 0; |
|
244 return SimpleSerializer<WindowsFrameInfo>::SizeOf(*(wfi.get())); |
|
245 } |
|
246 static char *Write(const linked_ptr<WindowsFrameInfo> &wfi, char *dest) { |
|
247 if (wfi.get()) |
|
248 dest = SimpleSerializer<WindowsFrameInfo>::Write(*(wfi.get()), dest); |
|
249 return dest; |
|
250 } |
|
251 }; |
|
252 |
|
253 } // namespace google_breakpad |
|
254 |
|
255 #endif // PROCESSOR_SIMPLE_SERIALIZER_INL_H__ |