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 "processor/simple_serializer.h" |
42 | |
43 | #include <cstdint> |
44 | #include <string> |
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/map_serializers-inl.h" |
50 | #include "processor/windows_frame_info.h" |
51 | |
52 | namespace google_breakpad { |
53 | |
54 | // Specializations of SimpleSerializer: bool |
55 | template<> |
56 | class SimpleSerializer<bool> { |
57 | public: |
58 | static size_t SizeOf(bool boolean) { return 1; } |
59 | |
60 | static char* Write(bool boolean, char* dest) { |
61 | *dest = static_cast<char>(boolean? 255 : 0); |
62 | return ++dest; |
63 | } |
64 | |
65 | static const char* Read(const char* source, bool* value) { |
66 | *value = ((*source) == 0 ? false : true); |
67 | return ++source; |
68 | } |
69 | }; |
70 | |
71 | // Specializations of SimpleSerializer: string |
72 | template<> |
73 | class SimpleSerializer<string> { |
74 | public: |
75 | static size_t SizeOf(const string& str) { return str.size() + 1; } |
76 | |
77 | static char* Write(const string& str, char* dest) { |
78 | strcpy(dest, str.c_str()); |
79 | return dest + SizeOf(str); |
80 | } |
81 | }; |
82 | |
83 | // Specializations of SimpleSerializer: C-string |
84 | template<> |
85 | class SimpleSerializer<const char*> { |
86 | public: |
87 | static size_t SizeOf(const char* cstring) { |
88 | return strlen(cstring) + 1; |
89 | } |
90 | |
91 | static char* Write(const char* cstring, char* dest) { |
92 | strcpy(dest, cstring); |
93 | return dest + SizeOf(cstring); |
94 | } |
95 | }; |
96 | |
97 | // Specializations of SimpleSerializer: Line |
98 | template<> |
99 | class SimpleSerializer<BasicSourceLineResolver::Line> { |
100 | typedef BasicSourceLineResolver::Line Line; |
101 | public: |
102 | static size_t SizeOf(const Line& line) { |
103 | return SimpleSerializer<MemAddr>::SizeOf(line.address) |
104 | + SimpleSerializer<MemAddr>::SizeOf(line.size) |
105 | + SimpleSerializer<int32_t>::SizeOf(line.source_file_id) |
106 | + SimpleSerializer<int32_t>::SizeOf(line.line); |
107 | } |
108 | static char* Write(const Line& line, char* dest) { |
109 | dest = SimpleSerializer<MemAddr>::Write(line.address, dest); |
110 | dest = SimpleSerializer<MemAddr>::Write(line.size, dest); |
111 | dest = SimpleSerializer<int32_t>::Write(line.source_file_id, dest); |
112 | dest = SimpleSerializer<int32_t>::Write(line.line, dest); |
113 | return dest; |
114 | } |
115 | }; |
116 | |
117 | // Specializations of SimpleSerializer: InlineOrigin |
118 | template <> |
119 | class SimpleSerializer<BasicSourceLineResolver::InlineOrigin> { |
120 | typedef BasicSourceLineResolver::InlineOrigin InlineOrigin; |
121 | |
122 | public: |
123 | static size_t SizeOf(const InlineOrigin& origin) { |
124 | return SimpleSerializer<bool>::SizeOf(origin.has_file_id) + |
125 | SimpleSerializer<int32_t>::SizeOf(origin.source_file_id) + |
126 | SimpleSerializer<string>::SizeOf(origin.name); |
127 | } |
128 | static char* Write(const InlineOrigin& origin, char* dest) { |
129 | dest = SimpleSerializer<bool>::Write(origin.has_file_id, dest); |
130 | dest = SimpleSerializer<int32_t>::Write(origin.source_file_id, dest); |
131 | dest = SimpleSerializer<string>::Write(origin.name, dest); |
132 | return dest; |
133 | } |
134 | }; |
135 | |
136 | // Specializations of SimpleSerializer: PublicSymbol |
137 | template<> |
138 | class SimpleSerializer<BasicSourceLineResolver::PublicSymbol> { |
139 | typedef BasicSourceLineResolver::PublicSymbol PublicSymbol; |
140 | public: |
141 | static size_t SizeOf(const PublicSymbol& pubsymbol) { |
142 | return SimpleSerializer<string>::SizeOf(pubsymbol.name) |
143 | + SimpleSerializer<MemAddr>::SizeOf(pubsymbol.address) |
144 | + SimpleSerializer<int32_t>::SizeOf(pubsymbol.parameter_size) |
145 | + SimpleSerializer<bool>::SizeOf(pubsymbol.is_multiple); |
146 | } |
147 | static char* Write(const PublicSymbol& pubsymbol, char* dest) { |
148 | dest = SimpleSerializer<string>::Write(pubsymbol.name, dest); |
149 | dest = SimpleSerializer<MemAddr>::Write(pubsymbol.address, dest); |
150 | dest = SimpleSerializer<int32_t>::Write(pubsymbol.parameter_size, dest); |
151 | dest = SimpleSerializer<bool>::Write(pubsymbol.is_multiple, dest); |
152 | return dest; |
153 | } |
154 | }; |
155 | |
156 | // Specializations of SimpleSerializer: WindowsFrameInfo |
157 | template<> |
158 | class SimpleSerializer<WindowsFrameInfo> { |
159 | public: |
160 | static size_t SizeOf(const WindowsFrameInfo& wfi) { |
161 | unsigned int size = 0; |
162 | size += sizeof(int32_t); // wfi.type_ |
163 | size += SimpleSerializer<int32_t>::SizeOf(wfi.valid); |
164 | size += SimpleSerializer<uint32_t>::SizeOf(wfi.prolog_size); |
165 | size += SimpleSerializer<uint32_t>::SizeOf(wfi.epilog_size); |
166 | size += SimpleSerializer<uint32_t>::SizeOf(wfi.parameter_size); |
167 | size += SimpleSerializer<uint32_t>::SizeOf(wfi.saved_register_size); |
168 | size += SimpleSerializer<uint32_t>::SizeOf(wfi.local_size); |
169 | size += SimpleSerializer<uint32_t>::SizeOf(wfi.max_stack_size); |
170 | size += SimpleSerializer<bool>::SizeOf(wfi.allocates_base_pointer); |
171 | size += SimpleSerializer<string>::SizeOf(wfi.program_string); |
172 | return size; |
173 | } |
174 | static char* Write(const WindowsFrameInfo& wfi, char* dest) { |
175 | dest = SimpleSerializer<int32_t>::Write( |
176 | static_cast<const int32_t>(wfi.type_), dest); |
177 | dest = SimpleSerializer<int32_t>::Write(wfi.valid, dest); |
178 | dest = SimpleSerializer<uint32_t>::Write(wfi.prolog_size, dest); |
179 | dest = SimpleSerializer<uint32_t>::Write(wfi.epilog_size, dest); |
180 | dest = SimpleSerializer<uint32_t>::Write(wfi.parameter_size, dest); |
181 | dest = SimpleSerializer<uint32_t>::Write(wfi.saved_register_size, dest); |
182 | dest = SimpleSerializer<uint32_t>::Write(wfi.local_size, dest); |
183 | dest = SimpleSerializer<uint32_t>::Write(wfi.max_stack_size, dest); |
184 | dest = SimpleSerializer<bool>::Write(wfi.allocates_base_pointer, dest); |
185 | return SimpleSerializer<string>::Write(wfi.program_string, dest); |
186 | } |
187 | }; |
188 | |
189 | // Specializations of SimpleSerializer: Linked_ptr version of |
190 | // Line, InlineOrigin, Inline, Function, PublicSymbol, WindowsFrameInfo. |
191 | template<> |
192 | class SimpleSerializer< linked_ptr<BasicSourceLineResolver::Line> > { |
193 | typedef BasicSourceLineResolver::Line Line; |
194 | public: |
195 | static size_t SizeOf(const linked_ptr<Line>& lineptr) { |
196 | if (lineptr.get() == NULL) return 0; |
197 | return SimpleSerializer<Line>::SizeOf(*(lineptr.get())); |
198 | } |
199 | static char* Write(const linked_ptr<Line>& lineptr, char* dest) { |
200 | if (lineptr.get()) |
201 | dest = SimpleSerializer<Line>::Write(*(lineptr.get()), dest); |
202 | return dest; |
203 | } |
204 | }; |
205 | |
206 | template <> |
207 | class SimpleSerializer<linked_ptr<BasicSourceLineResolver::InlineOrigin>> { |
208 | typedef BasicSourceLineResolver::InlineOrigin InlineOrigin; |
209 | |
210 | public: |
211 | static size_t SizeOf(const linked_ptr<InlineOrigin>& origin_ptr) { |
212 | if (origin_ptr.get() == NULL) |
213 | return 0; |
214 | return SimpleSerializer<InlineOrigin>::SizeOf(*(origin_ptr.get())); |
215 | } |
216 | static char* Write(const linked_ptr<InlineOrigin>& origin_ptr, char* dest) { |
217 | if (origin_ptr.get()) |
218 | dest = SimpleSerializer<InlineOrigin>::Write(*(origin_ptr.get()), dest); |
219 | return dest; |
220 | } |
221 | }; |
222 | |
223 | // Specializations of SimpleSerializer: Inline |
224 | template <> |
225 | class SimpleSerializer<linked_ptr<BasicSourceLineResolver::Inline>>; |
226 | template <> |
227 | class SimpleSerializer<BasicSourceLineResolver::Inline> { |
228 | typedef BasicSourceLineResolver::Inline Inline; |
229 | |
230 | public: |
231 | inline static size_t SizeOf(const Inline& in); |
232 | inline static char* Write(const Inline& in, char* dest); |
233 | }; |
234 | |
235 | template <> |
236 | class SimpleSerializer<linked_ptr<BasicSourceLineResolver::Inline>> { |
237 | typedef BasicSourceLineResolver::Inline Inline; |
238 | |
239 | public: |
240 | static size_t SizeOf(const linked_ptr<Inline>& inline_ptr) { |
241 | if (inline_ptr.get() == NULL) |
242 | return 0; |
243 | return SimpleSerializer<Inline>::SizeOf(*(inline_ptr.get())); |
244 | } |
245 | static char* Write(const linked_ptr<Inline>& inline_ptr, char* dest) { |
246 | if (inline_ptr.get()) |
247 | dest = SimpleSerializer<Inline>::Write(*(inline_ptr.get()), dest); |
248 | return dest; |
249 | } |
250 | }; |
251 | |
252 | size_t SimpleSerializer<BasicSourceLineResolver::Inline>::SizeOf( |
253 | const Inline& in) { |
254 | return SimpleSerializer<bool>::SizeOf(in.has_call_site_file_id) + |
255 | SimpleSerializer<int32_t>::SizeOf(in.inline_nest_level) + |
256 | SimpleSerializer<int32_t>::SizeOf(in.call_site_line) + |
257 | SimpleSerializer<int32_t>::SizeOf(in.call_site_file_id) + |
258 | SimpleSerializer<int32_t>::SizeOf(in.origin_id) + |
259 | sizeof(uint32_t) + // This is to store the size of inline_ranges. |
260 | (in.inline_ranges.size() * sizeof(MemAddr) * 2); |
261 | } |
262 | |
263 | char* SimpleSerializer<BasicSourceLineResolver::Inline>::Write(const Inline& in, |
264 | char* dest) { |
265 | dest = SimpleSerializer<bool>::Write(in.has_call_site_file_id, dest); |
266 | dest = SimpleSerializer<int32_t>::Write(in.inline_nest_level, dest); |
267 | dest = SimpleSerializer<int32_t>::Write(in.call_site_line, dest); |
268 | dest = SimpleSerializer<int32_t>::Write(in.call_site_file_id, dest); |
269 | dest = SimpleSerializer<int32_t>::Write(in.origin_id, dest); |
270 | // Write the size of inline_ranges. |
271 | dest = SimpleSerializer<int32_t>::Write(in.inline_ranges.size(), dest); |
272 | for (const std::pair<MemAddr, MemAddr>& range : in.inline_ranges) { |
273 | dest = SimpleSerializer<MemAddr>::Write(range.first, dest); |
274 | dest = SimpleSerializer<MemAddr>::Write(range.second, dest); |
275 | } |
276 | return dest; |
277 | } |
278 | |
279 | template<> |
280 | class SimpleSerializer<BasicSourceLineResolver::Function> { |
281 | // Convenient type names. |
282 | typedef BasicSourceLineResolver::Function Function; |
283 | typedef BasicSourceLineResolver::Line Line; |
284 | typedef BasicSourceLineResolver::Inline Inline; |
285 | |
286 | public: |
287 | static size_t SizeOf(const Function& func) { |
288 | unsigned int size = 0; |
289 | size += SimpleSerializer<string>::SizeOf(func.name); |
290 | size += SimpleSerializer<MemAddr>::SizeOf(func.address); |
291 | size += SimpleSerializer<MemAddr>::SizeOf(func.size); |
292 | size += SimpleSerializer<int32_t>::SizeOf(func.parameter_size); |
293 | size += SimpleSerializer<bool>::SizeOf(func.is_multiple); |
294 | // This extra size is used to store the size of serialized func.inlines, so |
295 | // we know where to start de-serialize func.lines. |
296 | size += sizeof(int32_t); |
297 | size += inline_range_map_serializer_.SizeOf(&func.inlines); |
298 | size += range_map_serializer_.SizeOf(func.lines); |
299 | return size; |
300 | } |
301 | |
302 | static char* Write(const Function& func, char* dest) { |
303 | dest = SimpleSerializer<string>::Write(func.name, dest); |
304 | dest = SimpleSerializer<MemAddr>::Write(func.address, dest); |
305 | dest = SimpleSerializer<MemAddr>::Write(func.size, dest); |
306 | dest = SimpleSerializer<int32_t>::Write(func.parameter_size, dest); |
307 | dest = SimpleSerializer<bool>::Write(func.is_multiple, dest); |
308 | char* old_dest = dest; |
309 | dest += sizeof(int32_t); |
310 | dest = inline_range_map_serializer_.Write(&func.inlines, dest); |
311 | // Write the size of serialized func.inlines. The size doesn't include size |
312 | // field itself. |
313 | SimpleSerializer<MemAddr>::Write(dest - old_dest - sizeof(int32_t), |
314 | old_dest); |
315 | dest = range_map_serializer_.Write(func.lines, dest); |
316 | return dest; |
317 | } |
318 | private: |
319 | // This static member is defined in module_serializer.cc. |
320 | static RangeMapSerializer<MemAddr, linked_ptr<Line>> range_map_serializer_; |
321 | static ContainedRangeMapSerializer<MemAddr, linked_ptr<Inline>> |
322 | inline_range_map_serializer_; |
323 | }; |
324 | |
325 | template<> |
326 | class SimpleSerializer< linked_ptr<BasicSourceLineResolver::Function> > { |
327 | typedef BasicSourceLineResolver::Function Function; |
328 | public: |
329 | static size_t SizeOf(const linked_ptr<Function>& func) { |
330 | if (!func.get()) return 0; |
331 | return SimpleSerializer<Function>::SizeOf(*(func.get())); |
332 | } |
333 | |
334 | static char* Write(const linked_ptr<Function>& func, char* dest) { |
335 | if (func.get()) |
336 | dest = SimpleSerializer<Function>::Write(*(func.get()), dest); |
337 | return dest; |
338 | } |
339 | }; |
340 | |
341 | template<> |
342 | class SimpleSerializer< linked_ptr<BasicSourceLineResolver::PublicSymbol> > { |
343 | typedef BasicSourceLineResolver::PublicSymbol PublicSymbol; |
344 | public: |
345 | static size_t SizeOf(const linked_ptr<PublicSymbol>& pubsymbol) { |
346 | if (pubsymbol.get() == NULL) return 0; |
347 | return SimpleSerializer<PublicSymbol>::SizeOf(*(pubsymbol.get())); |
348 | } |
349 | static char* Write(const linked_ptr<PublicSymbol>& pubsymbol, char* dest) { |
350 | if (pubsymbol.get()) |
351 | dest = SimpleSerializer<PublicSymbol>::Write(*(pubsymbol.get()), dest); |
352 | return dest; |
353 | } |
354 | }; |
355 | |
356 | template<> |
357 | class SimpleSerializer< linked_ptr<WindowsFrameInfo> > { |
358 | public: |
359 | static size_t SizeOf(const linked_ptr<WindowsFrameInfo>& wfi) { |
360 | if (wfi.get() == NULL) return 0; |
361 | return SimpleSerializer<WindowsFrameInfo>::SizeOf(*(wfi.get())); |
362 | } |
363 | static char* Write(const linked_ptr<WindowsFrameInfo>& wfi, char* dest) { |
364 | if (wfi.get()) |
365 | dest = SimpleSerializer<WindowsFrameInfo>::Write(*(wfi.get()), dest); |
366 | return dest; |
367 | } |
368 | }; |
369 | |
370 | } // namespace google_breakpad |
371 | |
372 | #endif // PROCESSOR_SIMPLE_SERIALIZER_INL_H__ |
373 | |