OLD | NEW |
| (Empty) |
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 // minidump.h: A minidump reader. | |
31 // | |
32 // The basic structure of this module tracks the structure of the minidump | |
33 // file itself. At the top level, a minidump file is represented by a | |
34 // Minidump object. Like most other classes in this module, Minidump | |
35 // provides a Read method that initializes the object with information from | |
36 // the file. Most of the classes in this file are wrappers around the | |
37 // "raw" structures found in the minidump file itself, and defined in | |
38 // minidump_format.h. For example, each thread is represented by a | |
39 // MinidumpThread object, whose parameters are specified in an MDRawThread | |
40 // structure. A properly byte-swapped MDRawThread can be obtained from a | |
41 // MinidumpThread easily by calling its thread() method. | |
42 // | |
43 // Most of the module lazily reads only the portion of the minidump file | |
44 // necessary to fulfill the user's request. Calling Minidump::Read | |
45 // only reads the minidump's directory. The thread list is not read until | |
46 // it is needed, and even once it's read, the memory regions for each | |
47 // thread's stack aren't read until they're needed. This strategy avoids | |
48 // unnecessary file input, and allocating memory for data in which the user | |
49 // has no interest. Note that although memory allocations for a typical | |
50 // minidump file are not particularly large, it is possible for legitimate | |
51 // minidumps to be sizable. A full-memory minidump, for example, contains | |
52 // a snapshot of the entire mapped memory space. Even a normal minidump, | |
53 // with stack memory only, can be large if, for example, the dump was | |
54 // generated in response to a crash that occurred due to an infinite- | |
55 // recursion bug that caused the stack's limits to be exceeded. Finally, | |
56 // some users of this library will unfortunately find themselves in the | |
57 // position of having to process potentially-hostile minidumps that might | |
58 // attempt to cause problems by forcing the minidump processor to over- | |
59 // allocate memory. | |
60 // | |
61 // Memory management in this module is based on a strict | |
62 // you-don't-own-anything policy. The only object owned by the user is | |
63 // the top-level Minidump object, the creation and destruction of which | |
64 // must be the user's own responsibility. All other objects obtained | |
65 // through interaction with this module are ultimately owned by the | |
66 // Minidump object, and will be freed upon the Minidump object's destruction. | |
67 // Because memory regions can potentially involve large allocations, a | |
68 // FreeMemory method is provided by MinidumpMemoryRegion, allowing the user | |
69 // to release data when it is no longer needed. Use of this method is | |
70 // optional but recommended. If freed data is later required, it will | |
71 // be read back in from the minidump file again. | |
72 // | |
73 // There is one exception to this memory management policy: | |
74 // Minidump::ReadString will return a string object to the user, and the user | |
75 // is responsible for its deletion. | |
76 // | |
77 // Author: Mark Mentovai | |
78 | |
79 #ifndef GOOGLE_BREAKPAD_PROCESSOR_MINIDUMP_H__ | |
80 #define GOOGLE_BREAKPAD_PROCESSOR_MINIDUMP_H__ | |
81 | |
82 #include <unistd.h> | |
83 | |
84 #include <iostream> | |
85 #include <map> | |
86 #include <string> | |
87 #include <vector> | |
88 | |
89 #include "google_breakpad/common/minidump_format.h" | |
90 #include "google_breakpad/processor/code_module.h" | |
91 #include "google_breakpad/processor/code_modules.h" | |
92 #include "google_breakpad/processor/memory_region.h" | |
93 | |
94 | |
95 namespace google_breakpad { | |
96 | |
97 | |
98 using std::map; | |
99 using std::string; | |
100 using std::vector; | |
101 | |
102 | |
103 class Minidump; | |
104 template<typename AddressType, typename EntryType> class RangeMap; | |
105 | |
106 | |
107 // MinidumpObject is the base of all Minidump* objects except for Minidump | |
108 // itself. | |
109 class MinidumpObject { | |
110 public: | |
111 virtual ~MinidumpObject() {} | |
112 | |
113 bool valid() const { return valid_; } | |
114 | |
115 protected: | |
116 explicit MinidumpObject(Minidump* minidump); | |
117 | |
118 // Refers to the Minidump object that is the ultimate parent of this | |
119 // Some MinidumpObjects are owned by other MinidumpObjects, but at the | |
120 // root of the ownership tree is always a Minidump. The Minidump object | |
121 // is kept here for access to its seeking and reading facilities, and | |
122 // for access to data about the minidump file itself, such as whether | |
123 // it should be byte-swapped. | |
124 Minidump* minidump_; | |
125 | |
126 // MinidumpObjects are not valid when created. When a subclass populates | |
127 // its own fields, it can set valid_ to true. Accessors and mutators may | |
128 // wish to consider or alter the valid_ state as they interact with | |
129 // objects. | |
130 bool valid_; | |
131 }; | |
132 | |
133 | |
134 // This class exists primarily to provide a virtual destructor in a base | |
135 // class common to all objects that might be stored in | |
136 // Minidump::mStreamObjects. Some object types (MinidumpContext) will | |
137 // never be stored in Minidump::mStreamObjects, but are represented as | |
138 // streams and adhere to the same interface, and may be derived from | |
139 // this class. | |
140 class MinidumpStream : public MinidumpObject { | |
141 public: | |
142 virtual ~MinidumpStream() {} | |
143 | |
144 protected: | |
145 explicit MinidumpStream(Minidump* minidump); | |
146 | |
147 private: | |
148 // Populate (and validate) the MinidumpStream. minidump_ is expected | |
149 // to be positioned at the beginning of the stream, so that the next | |
150 // read from the minidump will be at the beginning of the stream. | |
151 // expected_size should be set to the stream's length as contained in | |
152 // the MDRawDirectory record or other identifying record. A class | |
153 // that implements MinidumpStream can compare expected_size to a | |
154 // known size as an integrity check. | |
155 virtual bool Read(u_int32_t expected_size) = 0; | |
156 }; | |
157 | |
158 | |
159 // MinidumpContext carries a CPU-specific MDRawContext structure, which | |
160 // contains CPU context such as register states. Each thread has its | |
161 // own context, and the exception record, if present, also has its own | |
162 // context. Note that if the exception record is present, the context it | |
163 // refers to is probably what the user wants to use for the exception | |
164 // thread, instead of that thread's own context. The exception thread's | |
165 // context (as opposed to the exception record's context) will contain | |
166 // context for the exception handler (which performs minidump generation), | |
167 // and not the context that caused the exception (which is probably what the | |
168 // user wants). | |
169 class MinidumpContext : public MinidumpStream { | |
170 public: | |
171 virtual ~MinidumpContext(); | |
172 | |
173 // Returns an MD_CONTEXT_* value such as MD_CONTEXT_X86 or MD_CONTEXT_PPC | |
174 // identifying the CPU type that the context was collected from. The | |
175 // returned value will identify the CPU only, and will have any other | |
176 // MD_CONTEXT_* bits masked out. Returns 0 on failure. | |
177 u_int32_t GetContextCPU() const; | |
178 | |
179 // Returns raw CPU-specific context data for the named CPU type. If the | |
180 // context data does not match the CPU type or does not exist, returns | |
181 // NULL. | |
182 const MDRawContextAMD64* GetContextAMD64() const; | |
183 const MDRawContextARM* GetContextARM() const; | |
184 const MDRawContextPPC* GetContextPPC() const; | |
185 const MDRawContextSPARC* GetContextSPARC() const; | |
186 const MDRawContextX86* GetContextX86() const; | |
187 | |
188 // Print a human-readable representation of the object to stdout. | |
189 void Print(); | |
190 | |
191 private: | |
192 friend class MinidumpThread; | |
193 friend class MinidumpException; | |
194 | |
195 explicit MinidumpContext(Minidump* minidump); | |
196 | |
197 bool Read(u_int32_t expected_size); | |
198 | |
199 // Free the CPU-specific context structure. | |
200 void FreeContext(); | |
201 | |
202 // If the minidump contains a SYSTEM_INFO_STREAM, makes sure that the | |
203 // system info stream gives an appropriate CPU type matching the context | |
204 // CPU type in context_cpu_type. Returns false if the CPU type does not | |
205 // match. Returns true if the CPU type matches or if the minidump does | |
206 // not contain a system info stream. | |
207 bool CheckAgainstSystemInfo(u_int32_t context_cpu_type); | |
208 | |
209 // Store this separately because of the weirdo AMD64 context | |
210 u_int32_t context_flags_; | |
211 | |
212 // The CPU-specific context structure. | |
213 union { | |
214 MDRawContextBase* base; | |
215 MDRawContextX86* x86; | |
216 MDRawContextPPC* ppc; | |
217 MDRawContextAMD64* amd64; | |
218 // on Solaris SPARC, sparc is defined as a numeric constant, | |
219 // so variables can NOT be named as sparc | |
220 MDRawContextSPARC* ctx_sparc; | |
221 MDRawContextARM* arm; | |
222 } context_; | |
223 }; | |
224 | |
225 | |
226 // MinidumpMemoryRegion does not wrap any MDRaw structure, and only contains | |
227 // a reference to an MDMemoryDescriptor. This object is intended to wrap | |
228 // portions of a minidump file that contain memory dumps. In normal | |
229 // minidumps, each MinidumpThread owns a MinidumpMemoryRegion corresponding | |
230 // to the thread's stack memory. MinidumpMemoryList also gives access to | |
231 // memory regions in its list as MinidumpMemoryRegions. This class | |
232 // adheres to MemoryRegion so that it may be used as a data provider to | |
233 // the Stackwalker family of classes. | |
234 class MinidumpMemoryRegion : public MinidumpObject, | |
235 public MemoryRegion { | |
236 public: | |
237 virtual ~MinidumpMemoryRegion(); | |
238 | |
239 static void set_max_bytes(u_int32_t max_bytes) { max_bytes_ = max_bytes; } | |
240 static u_int32_t max_bytes() { return max_bytes_; } | |
241 | |
242 // Returns a pointer to the base of the memory region. Returns the | |
243 // cached value if available, otherwise, reads the minidump file and | |
244 // caches the memory region. | |
245 const u_int8_t* GetMemory() const; | |
246 | |
247 // The address of the base of the memory region. | |
248 u_int64_t GetBase() const; | |
249 | |
250 // The size, in bytes, of the memory region. | |
251 u_int32_t GetSize() const; | |
252 | |
253 // Frees the cached memory region, if cached. | |
254 void FreeMemory(); | |
255 | |
256 // Obtains the value of memory at the pointer specified by address. | |
257 bool GetMemoryAtAddress(u_int64_t address, u_int8_t* value) const; | |
258 bool GetMemoryAtAddress(u_int64_t address, u_int16_t* value) const; | |
259 bool GetMemoryAtAddress(u_int64_t address, u_int32_t* value) const; | |
260 bool GetMemoryAtAddress(u_int64_t address, u_int64_t* value) const; | |
261 | |
262 // Print a human-readable representation of the object to stdout. | |
263 void Print(); | |
264 | |
265 private: | |
266 friend class MinidumpThread; | |
267 friend class MinidumpMemoryList; | |
268 | |
269 explicit MinidumpMemoryRegion(Minidump* minidump); | |
270 | |
271 // Identify the base address and size of the memory region, and the | |
272 // location it may be found in the minidump file. | |
273 void SetDescriptor(MDMemoryDescriptor* descriptor); | |
274 | |
275 // Implementation for GetMemoryAtAddress | |
276 template<typename T> bool GetMemoryAtAddressInternal(u_int64_t address, | |
277 T* value) const; | |
278 | |
279 // The largest memory region that will be read from a minidump. The | |
280 // default is 1MB. | |
281 static u_int32_t max_bytes_; | |
282 | |
283 // Base address and size of the memory region, and its position in the | |
284 // minidump file. | |
285 MDMemoryDescriptor* descriptor_; | |
286 | |
287 // Cached memory. | |
288 mutable vector<u_int8_t>* memory_; | |
289 }; | |
290 | |
291 | |
292 // MinidumpThread contains information about a thread of execution, | |
293 // including a snapshot of the thread's stack and CPU context. For | |
294 // the thread that caused an exception, the context carried by | |
295 // MinidumpException is probably desired instead of the CPU context | |
296 // provided here. | |
297 class MinidumpThread : public MinidumpObject { | |
298 public: | |
299 virtual ~MinidumpThread(); | |
300 | |
301 const MDRawThread* thread() const { return valid_ ? &thread_ : NULL; } | |
302 MinidumpMemoryRegion* GetMemory(); | |
303 MinidumpContext* GetContext(); | |
304 | |
305 // The thread ID is used to determine if a thread is the exception thread, | |
306 // so a special getter is provided to retrieve this data from the | |
307 // MDRawThread structure. Returns false if the thread ID cannot be | |
308 // determined. | |
309 bool GetThreadID(u_int32_t *thread_id) const; | |
310 | |
311 // Print a human-readable representation of the object to stdout. | |
312 void Print(); | |
313 | |
314 private: | |
315 // These objects are managed by MinidumpThreadList. | |
316 friend class MinidumpThreadList; | |
317 | |
318 explicit MinidumpThread(Minidump* minidump); | |
319 | |
320 // This works like MinidumpStream::Read, but is driven by | |
321 // MinidumpThreadList. No size checking is done, because | |
322 // MinidumpThreadList handles that directly. | |
323 bool Read(); | |
324 | |
325 MDRawThread thread_; | |
326 MinidumpMemoryRegion* memory_; | |
327 MinidumpContext* context_; | |
328 }; | |
329 | |
330 | |
331 // MinidumpThreadList contains all of the threads (as MinidumpThreads) in | |
332 // a process. | |
333 class MinidumpThreadList : public MinidumpStream { | |
334 public: | |
335 virtual ~MinidumpThreadList(); | |
336 | |
337 static void set_max_threads(u_int32_t max_threads) { | |
338 max_threads_ = max_threads; | |
339 } | |
340 static u_int32_t max_threads() { return max_threads_; } | |
341 | |
342 unsigned int thread_count() const { | |
343 return valid_ ? thread_count_ : 0; | |
344 } | |
345 | |
346 // Sequential access to threads. | |
347 MinidumpThread* GetThreadAtIndex(unsigned int index) const; | |
348 | |
349 // Random access to threads. | |
350 MinidumpThread* GetThreadByID(u_int32_t thread_id); | |
351 | |
352 // Print a human-readable representation of the object to stdout. | |
353 void Print(); | |
354 | |
355 private: | |
356 friend class Minidump; | |
357 | |
358 typedef map<u_int32_t, MinidumpThread*> IDToThreadMap; | |
359 typedef vector<MinidumpThread> MinidumpThreads; | |
360 | |
361 static const u_int32_t kStreamType = MD_THREAD_LIST_STREAM; | |
362 | |
363 explicit MinidumpThreadList(Minidump* aMinidump); | |
364 | |
365 bool Read(u_int32_t aExpectedSize); | |
366 | |
367 // The largest number of threads that will be read from a minidump. The | |
368 // default is 256. | |
369 static u_int32_t max_threads_; | |
370 | |
371 // Access to threads using the thread ID as the key. | |
372 IDToThreadMap id_to_thread_map_; | |
373 | |
374 // The list of threads. | |
375 MinidumpThreads* threads_; | |
376 u_int32_t thread_count_; | |
377 }; | |
378 | |
379 | |
380 // MinidumpModule wraps MDRawModule, which contains information about loaded | |
381 // code modules. Access is provided to various data referenced indirectly | |
382 // by MDRawModule, such as the module's name and a specification for where | |
383 // to locate debugging information for the module. | |
384 class MinidumpModule : public MinidumpObject, | |
385 public CodeModule { | |
386 public: | |
387 virtual ~MinidumpModule(); | |
388 | |
389 static void set_max_cv_bytes(u_int32_t max_cv_bytes) { | |
390 max_cv_bytes_ = max_cv_bytes; | |
391 } | |
392 static u_int32_t max_cv_bytes() { return max_cv_bytes_; } | |
393 | |
394 static void set_max_misc_bytes(u_int32_t max_misc_bytes) { | |
395 max_misc_bytes_ = max_misc_bytes; | |
396 } | |
397 static u_int32_t max_misc_bytes() { return max_misc_bytes_; } | |
398 | |
399 const MDRawModule* module() const { return valid_ ? &module_ : NULL; } | |
400 | |
401 // CodeModule implementation | |
402 virtual u_int64_t base_address() const { | |
403 return valid_ ? module_.base_of_image : static_cast<u_int64_t>(-1); | |
404 } | |
405 virtual u_int64_t size() const { return valid_ ? module_.size_of_image : 0; } | |
406 virtual string code_file() const; | |
407 virtual string code_identifier() const; | |
408 virtual string debug_file() const; | |
409 virtual string debug_identifier() const; | |
410 virtual string version() const; | |
411 virtual const CodeModule* Copy() const; | |
412 | |
413 // The CodeView record, which contains information to locate the module's | |
414 // debugging information (pdb). This is returned as u_int8_t* because | |
415 // the data can be of types MDCVInfoPDB20* or MDCVInfoPDB70*, or it may be | |
416 // of a type unknown to Breakpad, in which case the raw data will still be | |
417 // returned but no byte-swapping will have been performed. Check the | |
418 // record's signature in the first four bytes to differentiate between | |
419 // the various types. Current toolchains generate modules which carry | |
420 // MDCVInfoPDB70 by default. Returns a pointer to the CodeView record on | |
421 // success, and NULL on failure. On success, the optional |size| argument | |
422 // is set to the size of the CodeView record. | |
423 const u_int8_t* GetCVRecord(u_int32_t* size); | |
424 | |
425 // The miscellaneous debug record, which is obsolete. Current toolchains | |
426 // do not generate this type of debugging information (dbg), and this | |
427 // field is not expected to be present. Returns a pointer to the debugging | |
428 // record on success, and NULL on failure. On success, the optional |size| | |
429 // argument is set to the size of the debugging record. | |
430 const MDImageDebugMisc* GetMiscRecord(u_int32_t* size); | |
431 | |
432 // Print a human-readable representation of the object to stdout. | |
433 void Print(); | |
434 | |
435 private: | |
436 // These objects are managed by MinidumpModuleList. | |
437 friend class MinidumpModuleList; | |
438 | |
439 explicit MinidumpModule(Minidump* minidump); | |
440 | |
441 // This works like MinidumpStream::Read, but is driven by | |
442 // MinidumpModuleList. No size checking is done, because | |
443 // MinidumpModuleList handles that directly. | |
444 bool Read(); | |
445 | |
446 // Reads indirectly-referenced data, including the module name, CodeView | |
447 // record, and miscellaneous debugging record. This is necessary to allow | |
448 // MinidumpModuleList to fully construct MinidumpModule objects without | |
449 // requiring seeks to read a contiguous set of MinidumpModule objects. | |
450 // All auxiliary data should be available when Read is called, in order to | |
451 // allow the CodeModule getters to be const methods. | |
452 bool ReadAuxiliaryData(); | |
453 | |
454 // The largest number of bytes that will be read from a minidump for a | |
455 // CodeView record or miscellaneous debugging record, respectively. The | |
456 // default for each is 1024. | |
457 static u_int32_t max_cv_bytes_; | |
458 static u_int32_t max_misc_bytes_; | |
459 | |
460 // True after a successful Read. This is different from valid_, which is | |
461 // not set true until ReadAuxiliaryData also completes successfully. | |
462 // module_valid_ is only used by ReadAuxiliaryData and the functions it | |
463 // calls to determine whether the object is ready for auxiliary data to | |
464 // be read. | |
465 bool module_valid_; | |
466 | |
467 // True if debug info was read from the module. Certain modules | |
468 // may contain debug records in formats we don't support, | |
469 // so we can just set this to false to ignore them. | |
470 bool has_debug_info_; | |
471 | |
472 MDRawModule module_; | |
473 | |
474 // Cached module name. | |
475 const string* name_; | |
476 | |
477 // Cached CodeView record - this is MDCVInfoPDB20 or (likely) | |
478 // MDCVInfoPDB70, or possibly something else entirely. Stored as a u_int8_t | |
479 // because the structure contains a variable-sized string and its exact | |
480 // size cannot be known until it is processed. | |
481 vector<u_int8_t>* cv_record_; | |
482 | |
483 // If cv_record_ is present, cv_record_signature_ contains a copy of the | |
484 // CodeView record's first four bytes, for ease of determinining the | |
485 // type of structure that cv_record_ contains. | |
486 u_int32_t cv_record_signature_; | |
487 | |
488 // Cached MDImageDebugMisc (usually not present), stored as u_int8_t | |
489 // because the structure contains a variable-sized string and its exact | |
490 // size cannot be known until it is processed. | |
491 vector<u_int8_t>* misc_record_; | |
492 }; | |
493 | |
494 | |
495 // MinidumpModuleList contains all of the loaded code modules for a process | |
496 // in the form of MinidumpModules. It maintains a map of these modules | |
497 // so that it may easily provide a code module corresponding to a specific | |
498 // address. | |
499 class MinidumpModuleList : public MinidumpStream, | |
500 public CodeModules { | |
501 public: | |
502 virtual ~MinidumpModuleList(); | |
503 | |
504 static void set_max_modules(u_int32_t max_modules) { | |
505 max_modules_ = max_modules; | |
506 } | |
507 static u_int32_t max_modules() { return max_modules_; } | |
508 | |
509 // CodeModules implementation. | |
510 virtual unsigned int module_count() const { | |
511 return valid_ ? module_count_ : 0; | |
512 } | |
513 virtual const MinidumpModule* GetModuleForAddress(u_int64_t address) const; | |
514 virtual const MinidumpModule* GetMainModule() const; | |
515 virtual const MinidumpModule* GetModuleAtSequence( | |
516 unsigned int sequence) const; | |
517 virtual const MinidumpModule* GetModuleAtIndex(unsigned int index) const; | |
518 virtual const CodeModules* Copy() const; | |
519 | |
520 // Print a human-readable representation of the object to stdout. | |
521 void Print(); | |
522 | |
523 private: | |
524 friend class Minidump; | |
525 | |
526 typedef vector<MinidumpModule> MinidumpModules; | |
527 | |
528 static const u_int32_t kStreamType = MD_MODULE_LIST_STREAM; | |
529 | |
530 explicit MinidumpModuleList(Minidump* minidump); | |
531 | |
532 bool Read(u_int32_t expected_size); | |
533 | |
534 // The largest number of modules that will be read from a minidump. The | |
535 // default is 1024. | |
536 static u_int32_t max_modules_; | |
537 | |
538 // Access to modules using addresses as the key. | |
539 RangeMap<u_int64_t, unsigned int> *range_map_; | |
540 | |
541 MinidumpModules *modules_; | |
542 u_int32_t module_count_; | |
543 }; | |
544 | |
545 | |
546 // MinidumpMemoryList corresponds to a minidump's MEMORY_LIST_STREAM stream, | |
547 // which references the snapshots of all of the memory regions contained | |
548 // within the minidump. For a normal minidump, this includes stack memory | |
549 // (also referenced by each MinidumpThread, in fact, the MDMemoryDescriptors | |
550 // here and in MDRawThread both point to exactly the same data in a | |
551 // minidump file, conserving space), as well as a 256-byte snapshot of memory | |
552 // surrounding the instruction pointer in the case of an exception. Other | |
553 // types of minidumps may contain significantly more memory regions. Full- | |
554 // memory minidumps contain all of a process' mapped memory. | |
555 class MinidumpMemoryList : public MinidumpStream { | |
556 public: | |
557 virtual ~MinidumpMemoryList(); | |
558 | |
559 static void set_max_regions(u_int32_t max_regions) { | |
560 max_regions_ = max_regions; | |
561 } | |
562 static u_int32_t max_regions() { return max_regions_; } | |
563 | |
564 unsigned int region_count() const { return valid_ ? region_count_ : 0; } | |
565 | |
566 // Sequential access to memory regions. | |
567 MinidumpMemoryRegion* GetMemoryRegionAtIndex(unsigned int index); | |
568 | |
569 // Random access to memory regions. Returns the region encompassing | |
570 // the address identified by address. | |
571 MinidumpMemoryRegion* GetMemoryRegionForAddress(u_int64_t address); | |
572 | |
573 // Print a human-readable representation of the object to stdout. | |
574 void Print(); | |
575 | |
576 private: | |
577 friend class Minidump; | |
578 | |
579 typedef vector<MDMemoryDescriptor> MemoryDescriptors; | |
580 typedef vector<MinidumpMemoryRegion> MemoryRegions; | |
581 | |
582 static const u_int32_t kStreamType = MD_MEMORY_LIST_STREAM; | |
583 | |
584 explicit MinidumpMemoryList(Minidump* minidump); | |
585 | |
586 bool Read(u_int32_t expected_size); | |
587 | |
588 // The largest number of memory regions that will be read from a minidump. | |
589 // The default is 256. | |
590 static u_int32_t max_regions_; | |
591 | |
592 // Access to memory regions using addresses as the key. | |
593 RangeMap<u_int64_t, unsigned int> *range_map_; | |
594 | |
595 // The list of descriptors. This is maintained separately from the list | |
596 // of regions, because MemoryRegion doesn't own its MemoryDescriptor, it | |
597 // maintains a pointer to it. descriptors_ provides the storage for this | |
598 // purpose. | |
599 MemoryDescriptors *descriptors_; | |
600 | |
601 // The list of regions. | |
602 MemoryRegions *regions_; | |
603 u_int32_t region_count_; | |
604 }; | |
605 | |
606 | |
607 // MinidumpException wraps MDRawExceptionStream, which contains information | |
608 // about the exception that caused the minidump to be generated, if the | |
609 // minidump was generated in an exception handler called as a result of | |
610 // an exception. It also provides access to a MinidumpContext object, | |
611 // which contains the CPU context for the exception thread at the time | |
612 // the exception occurred. | |
613 class MinidumpException : public MinidumpStream { | |
614 public: | |
615 virtual ~MinidumpException(); | |
616 | |
617 const MDRawExceptionStream* exception() const { | |
618 return valid_ ? &exception_ : NULL; | |
619 } | |
620 | |
621 // The thread ID is used to determine if a thread is the exception thread, | |
622 // so a special getter is provided to retrieve this data from the | |
623 // MDRawExceptionStream structure. Returns false if the thread ID cannot | |
624 // be determined. | |
625 bool GetThreadID(u_int32_t *thread_id) const; | |
626 | |
627 MinidumpContext* GetContext(); | |
628 | |
629 // Print a human-readable representation of the object to stdout. | |
630 void Print(); | |
631 | |
632 private: | |
633 friend class Minidump; | |
634 | |
635 static const u_int32_t kStreamType = MD_EXCEPTION_STREAM; | |
636 | |
637 explicit MinidumpException(Minidump* minidump); | |
638 | |
639 bool Read(u_int32_t expected_size); | |
640 | |
641 MDRawExceptionStream exception_; | |
642 MinidumpContext* context_; | |
643 }; | |
644 | |
645 // MinidumpAssertion wraps MDRawAssertionInfo, which contains information | |
646 // about an assertion that caused the minidump to be generated. | |
647 class MinidumpAssertion : public MinidumpStream { | |
648 public: | |
649 virtual ~MinidumpAssertion(); | |
650 | |
651 const MDRawAssertionInfo* assertion() const { | |
652 return valid_ ? &assertion_ : NULL; | |
653 } | |
654 | |
655 string expression() const { | |
656 return valid_ ? expression_ : ""; | |
657 } | |
658 | |
659 string function() const { | |
660 return valid_ ? function_ : ""; | |
661 } | |
662 | |
663 string file() const { | |
664 return valid_ ? file_ : ""; | |
665 } | |
666 | |
667 // Print a human-readable representation of the object to stdout. | |
668 void Print(); | |
669 | |
670 private: | |
671 friend class Minidump; | |
672 | |
673 static const u_int32_t kStreamType = MD_ASSERTION_INFO_STREAM; | |
674 | |
675 explicit MinidumpAssertion(Minidump* minidump); | |
676 | |
677 bool Read(u_int32_t expected_size); | |
678 | |
679 MDRawAssertionInfo assertion_; | |
680 string expression_; | |
681 string function_; | |
682 string file_; | |
683 }; | |
684 | |
685 | |
686 // MinidumpSystemInfo wraps MDRawSystemInfo and provides information about | |
687 // the system on which the minidump was generated. See also MinidumpMiscInfo. | |
688 class MinidumpSystemInfo : public MinidumpStream { | |
689 public: | |
690 virtual ~MinidumpSystemInfo(); | |
691 | |
692 const MDRawSystemInfo* system_info() const { | |
693 return valid_ ? &system_info_ : NULL; | |
694 } | |
695 | |
696 // GetOS and GetCPU return textual representations of the operating system | |
697 // and CPU that produced the minidump. Unlike most other Minidump* methods, | |
698 // they return string objects, not weak pointers. Defined values for | |
699 // GetOS() are "mac", "windows", and "linux". Defined values for GetCPU | |
700 // are "x86" and "ppc". These methods return an empty string when their | |
701 // values are unknown. | |
702 string GetOS(); | |
703 string GetCPU(); | |
704 | |
705 // I don't know what CSD stands for, but this field is documented as | |
706 // returning a textual representation of the OS service pack. On other | |
707 // platforms, this provides additional information about an OS version | |
708 // level beyond major.minor.micro. Returns NULL if unknown. | |
709 const string* GetCSDVersion(); | |
710 | |
711 // If a CPU vendor string can be determined, returns a pointer to it, | |
712 // otherwise, returns NULL. CPU vendor strings can be determined from | |
713 // x86 CPUs with CPUID 0. | |
714 const string* GetCPUVendor(); | |
715 | |
716 // Print a human-readable representation of the object to stdout. | |
717 void Print(); | |
718 | |
719 private: | |
720 friend class Minidump; | |
721 | |
722 static const u_int32_t kStreamType = MD_SYSTEM_INFO_STREAM; | |
723 | |
724 explicit MinidumpSystemInfo(Minidump* minidump); | |
725 | |
726 bool Read(u_int32_t expected_size); | |
727 | |
728 MDRawSystemInfo system_info_; | |
729 | |
730 // Textual representation of the OS service pack, for minidumps produced | |
731 // by MiniDumpWriteDump on Windows. | |
732 const string* csd_version_; | |
733 | |
734 // A string identifying the CPU vendor, if known. | |
735 const string* cpu_vendor_; | |
736 }; | |
737 | |
738 | |
739 // MinidumpMiscInfo wraps MDRawMiscInfo and provides information about | |
740 // the process that generated the minidump, and optionally additional system | |
741 // information. See also MinidumpSystemInfo. | |
742 class MinidumpMiscInfo : public MinidumpStream { | |
743 public: | |
744 const MDRawMiscInfo* misc_info() const { | |
745 return valid_ ? &misc_info_ : NULL; | |
746 } | |
747 | |
748 // Print a human-readable representation of the object to stdout. | |
749 void Print(); | |
750 | |
751 private: | |
752 friend class Minidump; | |
753 | |
754 static const u_int32_t kStreamType = MD_MISC_INFO_STREAM; | |
755 | |
756 explicit MinidumpMiscInfo(Minidump* minidump_); | |
757 | |
758 bool Read(u_int32_t expected_size_); | |
759 | |
760 MDRawMiscInfo misc_info_; | |
761 }; | |
762 | |
763 | |
764 // MinidumpBreakpadInfo wraps MDRawBreakpadInfo, which is an optional stream in | |
765 // a minidump that provides additional information about the process state | |
766 // at the time the minidump was generated. | |
767 class MinidumpBreakpadInfo : public MinidumpStream { | |
768 public: | |
769 const MDRawBreakpadInfo* breakpad_info() const { | |
770 return valid_ ? &breakpad_info_ : NULL; | |
771 } | |
772 | |
773 // These thread IDs are used to determine if threads deserve special | |
774 // treatment, so special getters are provided to retrieve this data from | |
775 // the MDRawBreakpadInfo structure. The getters return false if the thread | |
776 // IDs cannot be determined. | |
777 bool GetDumpThreadID(u_int32_t *thread_id) const; | |
778 bool GetRequestingThreadID(u_int32_t *thread_id) const; | |
779 | |
780 // Print a human-readable representation of the object to stdout. | |
781 void Print(); | |
782 | |
783 private: | |
784 friend class Minidump; | |
785 | |
786 static const u_int32_t kStreamType = MD_BREAKPAD_INFO_STREAM; | |
787 | |
788 explicit MinidumpBreakpadInfo(Minidump* minidump_); | |
789 | |
790 bool Read(u_int32_t expected_size_); | |
791 | |
792 MDRawBreakpadInfo breakpad_info_; | |
793 }; | |
794 | |
795 | |
796 // Minidump is the user's interface to a minidump file. It wraps MDRawHeader | |
797 // and provides access to the minidump's top-level stream directory. | |
798 class Minidump { | |
799 public: | |
800 // path is the pathname of a file containing the minidump. | |
801 explicit Minidump(const string& path); | |
802 // input is an istream wrapping minidump data. Minidump holds a | |
803 // weak pointer to input, and the caller must ensure that the stream | |
804 // is valid as long as the Minidump object is. | |
805 explicit Minidump(std::istream& input); | |
806 | |
807 virtual ~Minidump(); | |
808 | |
809 // path may be empty if the minidump was not opened from a file | |
810 virtual string path() const { | |
811 return path_; | |
812 } | |
813 static void set_max_streams(u_int32_t max_streams) { | |
814 max_streams_ = max_streams; | |
815 } | |
816 static u_int32_t max_streams() { return max_streams_; } | |
817 | |
818 static void set_max_string_length(u_int32_t max_string_length) { | |
819 max_string_length_ = max_string_length; | |
820 } | |
821 static u_int32_t max_string_length() { return max_string_length_; } | |
822 | |
823 virtual const MDRawHeader* header() const { return valid_ ? &header_ : NULL; } | |
824 | |
825 // Reads the minidump file's header and top-level stream directory. | |
826 // The minidump is expected to be positioned at the beginning of the | |
827 // header. Read() sets up the stream list and map, and validates the | |
828 // Minidump object. | |
829 virtual bool Read(); | |
830 | |
831 // The next set of methods are stubs that call GetStream. They exist to | |
832 // force code generation of the templatized API within the module, and | |
833 // to avoid exposing an ugly API (GetStream needs to accept a garbage | |
834 // parameter). | |
835 virtual MinidumpThreadList* GetThreadList(); | |
836 MinidumpModuleList* GetModuleList(); | |
837 MinidumpMemoryList* GetMemoryList(); | |
838 MinidumpException* GetException(); | |
839 MinidumpAssertion* GetAssertion(); | |
840 MinidumpSystemInfo* GetSystemInfo(); | |
841 MinidumpMiscInfo* GetMiscInfo(); | |
842 MinidumpBreakpadInfo* GetBreakpadInfo(); | |
843 | |
844 // The next set of methods are provided for users who wish to access | |
845 // data in minidump files directly, while leveraging the rest of | |
846 // this class and related classes to handle the basic minidump | |
847 // structure and known stream types. | |
848 | |
849 unsigned int GetDirectoryEntryCount() const { | |
850 return valid_ ? header_.stream_count : 0; | |
851 } | |
852 const MDRawDirectory* GetDirectoryEntryAtIndex(unsigned int index) const; | |
853 | |
854 // The next 2 methods are lower-level I/O routines. They use fd_. | |
855 | |
856 // Reads count bytes from the minidump at the current position into | |
857 // the storage area pointed to by bytes. bytes must be of sufficient | |
858 // size. After the read, the file position is advanced by count. | |
859 bool ReadBytes(void* bytes, size_t count); | |
860 | |
861 // Sets the position of the minidump file to offset. | |
862 bool SeekSet(off_t offset); | |
863 | |
864 // Returns the current position of the minidump file. | |
865 off_t Tell(); | |
866 | |
867 // The next 2 methods are medium-level I/O routines. | |
868 | |
869 // ReadString returns a string which is owned by the caller! offset | |
870 // specifies the offset that a length-encoded string is stored at in the | |
871 // minidump file. | |
872 string* ReadString(off_t offset); | |
873 | |
874 // SeekToStreamType positions the file at the beginning of a stream | |
875 // identified by stream_type, and informs the caller of the stream's | |
876 // length by setting *stream_length. Because stream_map maps each stream | |
877 // type to only one stream in the file, this might mislead the user into | |
878 // thinking that the stream that this seeks to is the only stream with | |
879 // type stream_type. That can't happen for streams that these classes | |
880 // deal with directly, because they're only supposed to be present in the | |
881 // file singly, and that's verified when stream_map_ is built. Users who | |
882 // are looking for other stream types should be aware of this | |
883 // possibility, and consider using GetDirectoryEntryAtIndex (possibly | |
884 // with GetDirectoryEntryCount) if expecting multiple streams of the same | |
885 // type in a single minidump file. | |
886 bool SeekToStreamType(u_int32_t stream_type, u_int32_t* stream_length); | |
887 | |
888 bool swap() const { return valid_ ? swap_ : false; } | |
889 | |
890 // Print a human-readable representation of the object to stdout. | |
891 void Print(); | |
892 | |
893 private: | |
894 // MinidumpStreamInfo is used in the MinidumpStreamMap. It lets | |
895 // the Minidump object locate interesting streams quickly, and | |
896 // provides a convenient place to stash MinidumpStream objects. | |
897 struct MinidumpStreamInfo { | |
898 MinidumpStreamInfo() : stream_index(0), stream(NULL) {} | |
899 ~MinidumpStreamInfo() { delete stream; } | |
900 | |
901 // Index into the MinidumpDirectoryEntries vector | |
902 unsigned int stream_index; | |
903 | |
904 // Pointer to the stream if cached, or NULL if not yet populated | |
905 MinidumpStream* stream; | |
906 }; | |
907 | |
908 typedef vector<MDRawDirectory> MinidumpDirectoryEntries; | |
909 typedef map<u_int32_t, MinidumpStreamInfo> MinidumpStreamMap; | |
910 | |
911 template<typename T> T* GetStream(T** stream); | |
912 | |
913 // Opens the minidump file, or if already open, seeks to the beginning. | |
914 bool Open(); | |
915 | |
916 // The largest number of top-level streams that will be read from a minidump. | |
917 // Note that streams are only read (and only consume memory) as needed, | |
918 // when directed by the caller. The default is 128. | |
919 static u_int32_t max_streams_; | |
920 | |
921 // The maximum length of a UTF-16 string that will be read from a minidump | |
922 // in 16-bit words. The default is 1024. UTF-16 strings are converted | |
923 // to UTF-8 when stored in memory, and each UTF-16 word will be represented | |
924 // by as many as 3 bytes in UTF-8. | |
925 static unsigned int max_string_length_; | |
926 | |
927 MDRawHeader header_; | |
928 | |
929 // The list of streams. | |
930 MinidumpDirectoryEntries* directory_; | |
931 | |
932 // Access to streams using the stream type as the key. | |
933 MinidumpStreamMap* stream_map_; | |
934 | |
935 // The pathname of the minidump file to process, set in the constructor. | |
936 // This may be empty if the minidump was opened directly from a stream. | |
937 const string path_; | |
938 | |
939 // The stream for all file I/O. Used by ReadBytes and SeekSet. | |
940 // Set based on the path in Open, or directly in the constructor. | |
941 std::istream* stream_; | |
942 | |
943 // swap_ is true if the minidump file should be byte-swapped. If the | |
944 // minidump was produced by a CPU that is other-endian than the CPU | |
945 // processing the minidump, this will be true. If the two CPUs are | |
946 // same-endian, this will be false. | |
947 bool swap_; | |
948 | |
949 // Validity of the Minidump structure, false immediately after | |
950 // construction or after a failed Read(); true following a successful | |
951 // Read(). | |
952 bool valid_; | |
953 }; | |
954 | |
955 | |
956 } // namespace google_breakpad | |
957 | |
958 | |
959 #endif // GOOGLE_BREAKPAD_PROCESSOR_MINIDUMP_H__ | |
OLD | NEW |