| 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 <stdint.h> |
| 83 |
| 84 | #ifndef _WIN32 |
| 85 | #include <unistd.h> |
| 86 | #endif |
| 87 |
| 88 | #include <iostream> |
| 89 | #include <map> |
| 90 | #include <string> |
| 91 | #include <vector> |
| 92 |
| 93 | #include "common/basictypes.h" |
| 94 | #include "common/using_std_string.h" |
| 95 | #include "google_breakpad/processor/code_module.h" |
| 96 | #include "google_breakpad/processor/code_modules.h" |
| 97 | #include "google_breakpad/processor/dump_context.h" |
| 98 | #include "google_breakpad/processor/dump_object.h" |
| 99 | #include "google_breakpad/processor/memory_region.h" |
| 100 | #include "google_breakpad/processor/proc_maps_linux.h" |
| 101 |
| 102 |
| 103 | namespace google_breakpad { |
| 104 |
| 105 |
| 106 | using std::map; |
| 107 | using std::vector; |
| 108 |
| 109 |
| 110 | class Minidump; |
| 111 | template<typename AddressType, typename EntryType> class RangeMap; |
| 112 |
| 113 |
| 114 | // MinidumpObject is the base of all Minidump* objects except for Minidump |
| 115 | // itself. |
| 116 | class MinidumpObject : public DumpObject { |
| 117 | public: |
| 118 | virtual ~MinidumpObject() {} |
| 119 |
| 120 | protected: |
| 121 | explicit MinidumpObject(Minidump* minidump); |
| 122 |
| 123 | // Refers to the Minidump object that is the ultimate parent of this |
| 124 | // Some MinidumpObjects are owned by other MinidumpObjects, but at the |
| 125 | // root of the ownership tree is always a Minidump. The Minidump object |
| 126 | // is kept here for access to its seeking and reading facilities, and |
| 127 | // for access to data about the minidump file itself, such as whether |
| 128 | // it should be byte-swapped. |
| 129 | Minidump* minidump_; |
| 130 | }; |
| 131 |
| 132 |
| 133 | // This class exists primarily to provide a virtual destructor in a base |
| 134 | // class common to all objects that might be stored in |
| 135 | // Minidump::mStreamObjects. Some object types will never be stored in |
| 136 | // Minidump::mStreamObjects, but are represented as streams and adhere to the |
| 137 | // same interface, and may be derived from this class. |
| 138 | class MinidumpStream : public MinidumpObject { |
| 139 | public: |
| 140 | virtual ~MinidumpStream() {} |
| 141 |
| 142 | protected: |
| 143 | explicit MinidumpStream(Minidump* minidump); |
| 144 |
| 145 | private: |
| 146 | // Populate (and validate) the MinidumpStream. minidump_ is expected |
| 147 | // to be positioned at the beginning of the stream, so that the next |
| 148 | // read from the minidump will be at the beginning of the stream. |
| 149 | // expected_size should be set to the stream's length as contained in |
| 150 | // the MDRawDirectory record or other identifying record. A class |
| 151 | // that implements MinidumpStream can compare expected_size to a |
| 152 | // known size as an integrity check. |
| 153 | virtual bool Read(uint32_t expected_size) = 0; |
| 154 | }; |
| 155 |
| 156 |
| 157 | // MinidumpContext carries a CPU-specific MDRawContext structure, which |
| 158 | // contains CPU context such as register states. Each thread has its |
| 159 | // own context, and the exception record, if present, also has its own |
| 160 | // context. Note that if the exception record is present, the context it |
| 161 | // refers to is probably what the user wants to use for the exception |
| 162 | // thread, instead of that thread's own context. The exception thread's |
| 163 | // context (as opposed to the exception record's context) will contain |
| 164 | // context for the exception handler (which performs minidump generation), |
| 165 | // and not the context that caused the exception (which is probably what the |
| 166 | // user wants). |
| 167 | class MinidumpContext : public DumpContext { |
| 168 | public: |
| 169 | virtual ~MinidumpContext(); |
| 170 |
| 171 | protected: |
| 172 | explicit MinidumpContext(Minidump* minidump); |
| 173 |
| 174 | private: |
| 175 | friend class MinidumpThread; |
| 176 | friend class MinidumpException; |
| 177 |
| 178 | bool Read(uint32_t expected_size); |
| 179 |
| 180 | // If the minidump contains a SYSTEM_INFO_STREAM, makes sure that the |
| 181 | // system info stream gives an appropriate CPU type matching the context |
| 182 | // CPU type in context_cpu_type. Returns false if the CPU type does not |
| 183 | // match. Returns true if the CPU type matches or if the minidump does |
| 184 | // not contain a system info stream. |
| 185 | bool CheckAgainstSystemInfo(uint32_t context_cpu_type); |
| 186 |
| 187 | // Refers to the Minidump object that is the ultimate parent of this |
| 188 | // Some MinidumpObjects are owned by other MinidumpObjects, but at the |
| 189 | // root of the ownership tree is always a Minidump. The Minidump object |
| 190 | // is kept here for access to its seeking and reading facilities, and |
| 191 | // for access to data about the minidump file itself, such as whether |
| 192 | // it should be byte-swapped. |
| 193 | Minidump* minidump_; |
| 194 | }; |
| 195 |
| 196 |
| 197 | // MinidumpMemoryRegion does not wrap any MDRaw structure, and only contains |
| 198 | // a reference to an MDMemoryDescriptor. This object is intended to wrap |
| 199 | // portions of a minidump file that contain memory dumps. In normal |
| 200 | // minidumps, each MinidumpThread owns a MinidumpMemoryRegion corresponding |
| 201 | // to the thread's stack memory. MinidumpMemoryList also gives access to |
| 202 | // memory regions in its list as MinidumpMemoryRegions. This class |
| 203 | // adheres to MemoryRegion so that it may be used as a data provider to |
| 204 | // the Stackwalker family of classes. |
| 205 | class MinidumpMemoryRegion : public MinidumpObject, |
| 206 | public MemoryRegion { |
| 207 | public: |
| 208 | virtual ~MinidumpMemoryRegion(); |
| 209 |
| 210 | static void set_max_bytes(uint32_t max_bytes) { max_bytes_ = max_bytes; } |
| 211 | static uint32_t max_bytes() { return max_bytes_; } |
| 212 |
| 213 | // Returns a pointer to the base of the memory region. Returns the |
| 214 | // cached value if available, otherwise, reads the minidump file and |
| 215 | // caches the memory region. |
| 216 | const uint8_t* GetMemory() const; |
| 217 |
| 218 | // The address of the base of the memory region. |
| 219 | uint64_t GetBase() const; |
| 220 |
| 221 | // The size, in bytes, of the memory region. |
| 222 | uint32_t GetSize() const; |
| 223 |
| 224 | // Frees the cached memory region, if cached. |
| 225 | void FreeMemory(); |
| 226 |
| 227 | // Obtains the value of memory at the pointer specified by address. |
| 228 | bool GetMemoryAtAddress(uint64_t address, uint8_t* value) const; |
| 229 | bool GetMemoryAtAddress(uint64_t address, uint16_t* value) const; |
| 230 | bool GetMemoryAtAddress(uint64_t address, uint32_t* value) const; |
| 231 | bool GetMemoryAtAddress(uint64_t address, uint64_t* value) const; |
| 232 |
| 233 | // Print a human-readable representation of the object to stdout. |
| 234 | void Print() const; |
| 235 |
| 236 | protected: |
| 237 | explicit MinidumpMemoryRegion(Minidump* minidump); |
| 238 |
| 239 | private: |
| 240 | friend class MinidumpThread; |
| 241 | friend class MinidumpMemoryList; |
| 242 |
| 243 | // Identify the base address and size of the memory region, and the |
| 244 | // location it may be found in the minidump file. |
| 245 | void SetDescriptor(MDMemoryDescriptor* descriptor); |
| 246 |
| 247 | // Implementation for GetMemoryAtAddress |
| 248 | template<typename T> bool GetMemoryAtAddressInternal(uint64_t address, |
| 249 | T* value) const; |
| 250 |
| 251 | // The largest memory region that will be read from a minidump. The |
| 252 | // default is 1MB. |
| 253 | static uint32_t max_bytes_; |
| 254 |
| 255 | // Base address and size of the memory region, and its position in the |
| 256 | // minidump file. |
| 257 | MDMemoryDescriptor* descriptor_; |
| 258 |
| 259 | // Cached memory. |
| 260 | mutable vector<uint8_t>* memory_; |
| 261 | }; |
| 262 |
| 263 |
| 264 | // MinidumpThread contains information about a thread of execution, |
| 265 | // including a snapshot of the thread's stack and CPU context. For |
| 266 | // the thread that caused an exception, the context carried by |
| 267 | // MinidumpException is probably desired instead of the CPU context |
| 268 | // provided here. |
| 269 | // Note that a MinidumpThread may be valid() even if it does not |
| 270 | // contain a memory region or context. |
| 271 | class MinidumpThread : public MinidumpObject { |
| 272 | public: |
| 273 | virtual ~MinidumpThread(); |
| 274 |
| 275 | const MDRawThread* thread() const { return valid_ ? &thread_ : NULL; } |
| 276 | // GetMemory may return NULL even if the MinidumpThread is valid, |
| 277 | // if the thread memory cannot be read. |
| 278 | virtual MinidumpMemoryRegion* GetMemory(); |
| 279 | // GetContext may return NULL even if the MinidumpThread is valid. |
| 280 | virtual MinidumpContext* GetContext(); |
| 281 |
| 282 | // The thread ID is used to determine if a thread is the exception thread, |
| 283 | // so a special getter is provided to retrieve this data from the |
| 284 | // MDRawThread structure. Returns false if the thread ID cannot be |
| 285 | // determined. |
| 286 | virtual bool GetThreadID(uint32_t *thread_id) const; |
| 287 |
| 288 | // Print a human-readable representation of the object to stdout. |
| 289 | void Print(); |
| 290 |
| 291 | // Returns the start address of the thread stack memory region. Returns 0 if |
| 292 | // MinidumpThread is invalid. Note that this method can be called even when |
| 293 | // the thread memory cannot be read and GetMemory returns NULL. |
| 294 | virtual uint64_t GetStartOfStackMemoryRange() const; |
| 295 |
| 296 | protected: |
| 297 | explicit MinidumpThread(Minidump* minidump); |
| 298 |
| 299 | private: |
| 300 | // These objects are managed by MinidumpThreadList. |
| 301 | friend class MinidumpThreadList; |
| 302 |
| 303 | // This works like MinidumpStream::Read, but is driven by |
| 304 | // MinidumpThreadList. No size checking is done, because |
| 305 | // MinidumpThreadList handles that directly. |
| 306 | bool Read(); |
| 307 |
| 308 | MDRawThread thread_; |
| 309 | MinidumpMemoryRegion* memory_; |
| 310 | MinidumpContext* context_; |
| 311 | }; |
| 312 |
| 313 |
| 314 | // MinidumpThreadList contains all of the threads (as MinidumpThreads) in |
| 315 | // a process. |
| 316 | class MinidumpThreadList : public MinidumpStream { |
| 317 | public: |
| 318 | virtual ~MinidumpThreadList(); |
| 319 |
| 320 | static void set_max_threads(uint32_t max_threads) { |
| 321 | max_threads_ = max_threads; |
| 322 | } |
| 323 | static uint32_t max_threads() { return max_threads_; } |
| 324 |
| 325 | virtual unsigned int thread_count() const { |
| 326 | return valid_ ? thread_count_ : 0; |
| 327 | } |
| 328 |
| 329 | // Sequential access to threads. |
| 330 | virtual MinidumpThread* GetThreadAtIndex(unsigned int index) const; |
| 331 |
| 332 | // Random access to threads. |
| 333 | MinidumpThread* GetThreadByID(uint32_t thread_id); |
| 334 |
| 335 | // Print a human-readable representation of the object to stdout. |
| 336 | void Print(); |
| 337 |
| 338 | protected: |
| 339 | explicit MinidumpThreadList(Minidump* aMinidump); |
| 340 |
| 341 | private: |
| 342 | friend class Minidump; |
| 343 |
| 344 | typedef map<uint32_t, MinidumpThread*> IDToThreadMap; |
| 345 | typedef vector<MinidumpThread> MinidumpThreads; |
| 346 |
| 347 | static const uint32_t kStreamType = MD_THREAD_LIST_STREAM; |
| 348 |
| 349 | bool Read(uint32_t aExpectedSize); |
| 350 |
| 351 | // The largest number of threads that will be read from a minidump. The |
| 352 | // default is 256. |
| 353 | static uint32_t max_threads_; |
| 354 |
| 355 | // Access to threads using the thread ID as the key. |
| 356 | IDToThreadMap id_to_thread_map_; |
| 357 |
| 358 | // The list of threads. |
| 359 | MinidumpThreads* threads_; |
| 360 | uint32_t thread_count_; |
| 361 | }; |
| 362 |
| 363 |
| 364 | // MinidumpModule wraps MDRawModule, which contains information about loaded |
| 365 | // code modules. Access is provided to various data referenced indirectly |
| 366 | // by MDRawModule, such as the module's name and a specification for where |
| 367 | // to locate debugging information for the module. |
| 368 | class MinidumpModule : public MinidumpObject, |
| 369 | public CodeModule { |
| 370 | public: |
| 371 | virtual ~MinidumpModule(); |
| 372 |
| 373 | static void set_max_cv_bytes(uint32_t max_cv_bytes) { |
| 374 | max_cv_bytes_ = max_cv_bytes; |
| 375 | } |
| 376 | static uint32_t max_cv_bytes() { return max_cv_bytes_; } |
| 377 |
| 378 | static void set_max_misc_bytes(uint32_t max_misc_bytes) { |
| 379 | max_misc_bytes_ = max_misc_bytes; |
| 380 | } |
| 381 | static uint32_t max_misc_bytes() { return max_misc_bytes_; } |
| 382 |
| 383 | const MDRawModule* module() const { return valid_ ? &module_ : NULL; } |
| 384 |
| 385 | // CodeModule implementation |
| 386 | virtual uint64_t base_address() const { |
| 387 | return valid_ ? module_.base_of_image : static_cast<uint64_t>(-1); |
| 388 | } |
| 389 | virtual uint64_t size() const { return valid_ ? module_.size_of_image : 0; } |
| 390 | virtual string code_file() const; |
| 391 | virtual string code_identifier() const; |
| 392 | virtual string debug_file() const; |
| 393 | virtual string debug_identifier() const; |
| 394 | virtual string version() const; |
| 395 | virtual const CodeModule* Copy() const; |
| 396 |
| 397 | // The CodeView record, which contains information to locate the module's |
| 398 | // debugging information (pdb). This is returned as uint8_t* because |
| 399 | // the data can be of types MDCVInfoPDB20* or MDCVInfoPDB70*, or it may be |
| 400 | // of a type unknown to Breakpad, in which case the raw data will still be |
| 401 | // returned but no byte-swapping will have been performed. Check the |
| 402 | // record's signature in the first four bytes to differentiate between |
| 403 | // the various types. Current toolchains generate modules which carry |
| 404 | // MDCVInfoPDB70 by default. Returns a pointer to the CodeView record on |
| 405 | // success, and NULL on failure. On success, the optional |size| argument |
| 406 | // is set to the size of the CodeView record. |
| 407 | const uint8_t* GetCVRecord(uint32_t* size); |
| 408 |
| 409 | // The miscellaneous debug record, which is obsolete. Current toolchains |
| 410 | // do not generate this type of debugging information (dbg), and this |
| 411 | // field is not expected to be present. Returns a pointer to the debugging |
| 412 | // record on success, and NULL on failure. On success, the optional |size| |
| 413 | // argument is set to the size of the debugging record. |
| 414 | const MDImageDebugMisc* GetMiscRecord(uint32_t* size); |
| 415 |
| 416 | // Print a human-readable representation of the object to stdout. |
| 417 | void Print(); |
| 418 |
| 419 | private: |
| 420 | // These objects are managed by MinidumpModuleList. |
| 421 | friend class MinidumpModuleList; |
| 422 |
| 423 | explicit MinidumpModule(Minidump* minidump); |
| 424 |
| 425 | // This works like MinidumpStream::Read, but is driven by |
| 426 | // MinidumpModuleList. No size checking is done, because |
| 427 | // MinidumpModuleList handles that directly. |
| 428 | bool Read(); |
| 429 |
| 430 | // Reads indirectly-referenced data, including the module name, CodeView |
| 431 | // record, and miscellaneous debugging record. This is necessary to allow |
| 432 | // MinidumpModuleList to fully construct MinidumpModule objects without |
| 433 | // requiring seeks to read a contiguous set of MinidumpModule objects. |
| 434 | // All auxiliary data should be available when Read is called, in order to |
| 435 | // allow the CodeModule getters to be const methods. |
| 436 | bool ReadAuxiliaryData(); |
| 437 |
| 438 | // The largest number of bytes that will be read from a minidump for a |
| 439 | // CodeView record or miscellaneous debugging record, respectively. The |
| 440 | // default for each is 1024. |
| 441 | static uint32_t max_cv_bytes_; |
| 442 | static uint32_t max_misc_bytes_; |
| 443 |
| 444 | // True after a successful Read. This is different from valid_, which is |
| 445 | // not set true until ReadAuxiliaryData also completes successfully. |
| 446 | // module_valid_ is only used by ReadAuxiliaryData and the functions it |
| 447 | // calls to determine whether the object is ready for auxiliary data to |
| 448 | // be read. |
| 449 | bool module_valid_; |
| 450 |
| 451 | // True if debug info was read from the module. Certain modules |
| 452 | // may contain debug records in formats we don't support, |
| 453 | // so we can just set this to false to ignore them. |
| 454 | bool has_debug_info_; |
| 455 |
| 456 | MDRawModule module_; |
| 457 |
| 458 | // Cached module name. |
| 459 | const string* name_; |
| 460 |
| 461 | // Cached CodeView record - this is MDCVInfoPDB20 or (likely) |
| 462 | // MDCVInfoPDB70, or possibly something else entirely. Stored as a uint8_t |
| 463 | // because the structure contains a variable-sized string and its exact |
| 464 | // size cannot be known until it is processed. |
| 465 | vector<uint8_t>* cv_record_; |
| 466 |
| 467 | // If cv_record_ is present, cv_record_signature_ contains a copy of the |
| 468 | // CodeView record's first four bytes, for ease of determinining the |
| 469 | // type of structure that cv_record_ contains. |
| 470 | uint32_t cv_record_signature_; |
| 471 |
| 472 | // Cached MDImageDebugMisc (usually not present), stored as uint8_t |
| 473 | // because the structure contains a variable-sized string and its exact |
| 474 | // size cannot be known until it is processed. |
| 475 | vector<uint8_t>* misc_record_; |
| 476 | }; |
| 477 |
| 478 |
| 479 | // MinidumpModuleList contains all of the loaded code modules for a process |
| 480 | // in the form of MinidumpModules. It maintains a map of these modules |
| 481 | // so that it may easily provide a code module corresponding to a specific |
| 482 | // address. |
| 483 | class MinidumpModuleList : public MinidumpStream, |
| 484 | public CodeModules { |
| 485 | public: |
| 486 | virtual ~MinidumpModuleList(); |
| 487 |
| 488 | static void set_max_modules(uint32_t max_modules) { |
| 489 | max_modules_ = max_modules; |
| 490 | } |
| 491 | static uint32_t max_modules() { return max_modules_; } |
| 492 |
| 493 | // CodeModules implementation. |
| 494 | virtual unsigned int module_count() const { |
| 495 | return valid_ ? module_count_ : 0; |
| 496 | } |
| 497 | virtual const MinidumpModule* GetModuleForAddress(uint64_t address) const; |
| 498 | virtual const MinidumpModule* GetMainModule() const; |
| 499 | virtual const MinidumpModule* GetModuleAtSequence( |
| 500 | unsigned int sequence) const; |
| 501 | virtual const MinidumpModule* GetModuleAtIndex(unsigned int index) const; |
| 502 | virtual const CodeModules* Copy() const; |
| 503 |
| 504 | // Print a human-readable representation of the object to stdout. |
| 505 | void Print(); |
| 506 |
| 507 | protected: |
| 508 | explicit MinidumpModuleList(Minidump* minidump); |
| 509 |
| 510 | private: |
| 511 | friend class Minidump; |
| 512 |
| 513 | typedef vector<MinidumpModule> MinidumpModules; |
| 514 |
| 515 | static const uint32_t kStreamType = MD_MODULE_LIST_STREAM; |
| 516 |
| 517 | bool Read(uint32_t expected_size); |
| 518 |
| 519 | // The largest number of modules that will be read from a minidump. The |
| 520 | // default is 1024. |
| 521 | static uint32_t max_modules_; |
| 522 |
| 523 | // Access to modules using addresses as the key. |
| 524 | RangeMap<uint64_t, unsigned int> *range_map_; |
| 525 |
| 526 | MinidumpModules *modules_; |
| 527 | uint32_t module_count_; |
| 528 | }; |
| 529 |
| 530 |
| 531 | // MinidumpMemoryList corresponds to a minidump's MEMORY_LIST_STREAM stream, |
| 532 | // which references the snapshots of all of the memory regions contained |
| 533 | // within the minidump. For a normal minidump, this includes stack memory |
| 534 | // (also referenced by each MinidumpThread, in fact, the MDMemoryDescriptors |
| 535 | // here and in MDRawThread both point to exactly the same data in a |
| 536 | // minidump file, conserving space), as well as a 256-byte snapshot of memory |
| 537 | // surrounding the instruction pointer in the case of an exception. Other |
| 538 | // types of minidumps may contain significantly more memory regions. Full- |
| 539 | // memory minidumps contain all of a process' mapped memory. |
| 540 | class MinidumpMemoryList : public MinidumpStream { |
| 541 | public: |
| 542 | virtual ~MinidumpMemoryList(); |
| 543 |
| 544 | static void set_max_regions(uint32_t max_regions) { |
| 545 | max_regions_ = max_regions; |
| 546 | } |
| 547 | static uint32_t max_regions() { return max_regions_; } |
| 548 |
| 549 | unsigned int region_count() const { return valid_ ? region_count_ : 0; } |
| 550 |
| 551 | // Sequential access to memory regions. |
| 552 | MinidumpMemoryRegion* GetMemoryRegionAtIndex(unsigned int index); |
| 553 |
| 554 | // Random access to memory regions. Returns the region encompassing |
| 555 | // the address identified by address. |
| 556 | virtual MinidumpMemoryRegion* GetMemoryRegionForAddress(uint64_t address); |
| 557 |
| 558 | // Print a human-readable representation of the object to stdout. |
| 559 | void Print(); |
| 560 |
| 561 | private: |
| 562 | friend class Minidump; |
| 563 | friend class MockMinidumpMemoryList; |
| 564 |
| 565 | typedef vector<MDMemoryDescriptor> MemoryDescriptors; |
| 566 | typedef vector<MinidumpMemoryRegion> MemoryRegions; |
| 567 |
| 568 | static const uint32_t kStreamType = MD_MEMORY_LIST_STREAM; |
| 569 |
| 570 | explicit MinidumpMemoryList(Minidump* minidump); |
| 571 |
| 572 | bool Read(uint32_t expected_size); |
| 573 |
| 574 | // The largest number of memory regions that will be read from a minidump. |
| 575 | // The default is 256. |
| 576 | static uint32_t max_regions_; |
| 577 |
| 578 | // Access to memory regions using addresses as the key. |
| 579 | RangeMap<uint64_t, unsigned int> *range_map_; |
| 580 |
| 581 | // The list of descriptors. This is maintained separately from the list |
| 582 | // of regions, because MemoryRegion doesn't own its MemoryDescriptor, it |
| 583 | // maintains a pointer to it. descriptors_ provides the storage for this |
| 584 | // purpose. |
| 585 | MemoryDescriptors *descriptors_; |
| 586 |
| 587 | // The list of regions. |
| 588 | MemoryRegions *regions_; |
| 589 | uint32_t region_count_; |
| 590 | }; |
| 591 |
| 592 |
| 593 | // MinidumpException wraps MDRawExceptionStream, which contains information |
| 594 | // about the exception that caused the minidump to be generated, if the |
| 595 | // minidump was generated in an exception handler called as a result of an |
| 596 | // exception. It also provides access to a MinidumpContext object, which |
| 597 | // contains the CPU context for the exception thread at the time the exception |
| 598 | // occurred. |
| 599 | class MinidumpException : public MinidumpStream { |
| 600 | public: |
| 601 | virtual ~MinidumpException(); |
| 602 |
| 603 | const MDRawExceptionStream* exception() const { |
| 604 | return valid_ ? &exception_ : NULL; |
| 605 | } |
| 606 |
| 607 | // The thread ID is used to determine if a thread is the exception thread, |
| 608 | // so a special getter is provided to retrieve this data from the |
| 609 | // MDRawExceptionStream structure. Returns false if the thread ID cannot |
| 610 | // be determined. |
| 611 | bool GetThreadID(uint32_t *thread_id) const; |
| 612 |
| 613 | MinidumpContext* GetContext(); |
| 614 |
| 615 | // Print a human-readable representation of the object to stdout. |
| 616 | void Print(); |
| 617 |
| 618 | private: |
| 619 | friend class Minidump; |
| 620 |
| 621 | static const uint32_t kStreamType = MD_EXCEPTION_STREAM; |
| 622 |
| 623 | explicit MinidumpException(Minidump* minidump); |
| 624 |
| 625 | bool Read(uint32_t expected_size); |
| 626 |
| 627 | MDRawExceptionStream exception_; |
| 628 | MinidumpContext* context_; |
| 629 | }; |
| 630 |
| 631 | // MinidumpAssertion wraps MDRawAssertionInfo, which contains information |
| 632 | // about an assertion that caused the minidump to be generated. |
| 633 | class MinidumpAssertion : public MinidumpStream { |
| 634 | public: |
| 635 | virtual ~MinidumpAssertion(); |
| 636 |
| 637 | const MDRawAssertionInfo* assertion() const { |
| 638 | return valid_ ? &assertion_ : NULL; |
| 639 | } |
| 640 |
| 641 | string expression() const { |
| 642 | return valid_ ? expression_ : ""; |
| 643 | } |
| 644 |
| 645 | string function() const { |
| 646 | return valid_ ? function_ : ""; |
| 647 | } |
| 648 |
| 649 | string file() const { |
| 650 | return valid_ ? file_ : ""; |
| 651 | } |
| 652 |
| 653 | // Print a human-readable representation of the object to stdout. |
| 654 | void Print(); |
| 655 |
| 656 | private: |
| 657 | friend class Minidump; |
| 658 |
| 659 | static const uint32_t kStreamType = MD_ASSERTION_INFO_STREAM; |
| 660 |
| 661 | explicit MinidumpAssertion(Minidump* minidump); |
| 662 |
| 663 | bool Read(uint32_t expected_size); |
| 664 |
| 665 | MDRawAssertionInfo assertion_; |
| 666 | string expression_; |
| 667 | string function_; |
| 668 | string file_; |
| 669 | }; |
| 670 |
| 671 |
| 672 | // MinidumpSystemInfo wraps MDRawSystemInfo and provides information about |
| 673 | // the system on which the minidump was generated. See also MinidumpMiscInfo. |
| 674 | class MinidumpSystemInfo : public MinidumpStream { |
| 675 | public: |
| 676 | virtual ~MinidumpSystemInfo(); |
| 677 |
| 678 | const MDRawSystemInfo* system_info() const { |
| 679 | return valid_ ? &system_info_ : NULL; |
| 680 | } |
| 681 |
| 682 | // GetOS and GetCPU return textual representations of the operating system |
| 683 | // and CPU that produced the minidump. Unlike most other Minidump* methods, |
| 684 | // they return string objects, not weak pointers. Defined values for |
| 685 | // GetOS() are "mac", "windows", and "linux". Defined values for GetCPU |
| 686 | // are "x86" and "ppc". These methods return an empty string when their |
| 687 | // values are unknown. |
| 688 | string GetOS(); |
| 689 | string GetCPU(); |
| 690 |
| 691 | // I don't know what CSD stands for, but this field is documented as |
| 692 | // returning a textual representation of the OS service pack. On other |
| 693 | // platforms, this provides additional information about an OS version |
| 694 | // level beyond major.minor.micro. Returns NULL if unknown. |
| 695 | const string* GetCSDVersion(); |
| 696 |
| 697 | // If a CPU vendor string can be determined, returns a pointer to it, |
| 698 | // otherwise, returns NULL. CPU vendor strings can be determined from |
| 699 | // x86 CPUs with CPUID 0. |
| 700 | const string* GetCPUVendor(); |
| 701 |
| 702 | // Print a human-readable representation of the object to stdout. |
| 703 | void Print(); |
| 704 |
| 705 | protected: |
| 706 | explicit MinidumpSystemInfo(Minidump* minidump); |
| 707 | MDRawSystemInfo system_info_; |
| 708 |
| 709 | // Textual representation of the OS service pack, for minidumps produced |
| 710 | // by MiniDumpWriteDump on Windows. |
| 711 | const string* csd_version_; |
| 712 |
| 713 | private: |
| 714 | friend class Minidump; |
| 715 |
| 716 | static const uint32_t kStreamType = MD_SYSTEM_INFO_STREAM; |
| 717 |
| 718 | bool Read(uint32_t expected_size); |
| 719 |
| 720 | // A string identifying the CPU vendor, if known. |
| 721 | const string* cpu_vendor_; |
| 722 | }; |
| 723 |
| 724 |
| 725 | // MinidumpMiscInfo wraps MDRawMiscInfo and provides information about |
| 726 | // the process that generated the minidump, and optionally additional system |
| 727 | // information. See also MinidumpSystemInfo. |
| 728 | class MinidumpMiscInfo : public MinidumpStream { |
| 729 | public: |
| 730 | const MDRawMiscInfo* misc_info() const { |
| 731 | return valid_ ? &misc_info_ : NULL; |
| 732 | } |
| 733 |
| 734 | // Print a human-readable representation of the object to stdout. |
| 735 | void Print(); |
| 736 |
| 737 | private: |
| 738 | friend class Minidump; |
| 739 | friend class TestMinidumpMiscInfo; |
| 740 |
| 741 | static const uint32_t kStreamType = MD_MISC_INFO_STREAM; |
| 742 |
| 743 | explicit MinidumpMiscInfo(Minidump* minidump_); |
| 744 |
| 745 | bool Read(uint32_t expected_size_); |
| 746 |
| 747 | MDRawMiscInfo misc_info_; |
| 748 |
| 749 | // Populated by Read. Contains the converted strings from the corresponding |
| 750 | // UTF-16 fields in misc_info_ |
| 751 | string standard_name_; |
| 752 | string daylight_name_; |
| 753 | string build_string_; |
| 754 | string dbg_bld_str_; |
| 755 | }; |
| 756 |
| 757 |
| 758 | // MinidumpBreakpadInfo wraps MDRawBreakpadInfo, which is an optional stream in |
| 759 | // a minidump that provides additional information about the process state |
| 760 | // at the time the minidump was generated. |
| 761 | class MinidumpBreakpadInfo : public MinidumpStream { |
| 762 | public: |
| 763 | const MDRawBreakpadInfo* breakpad_info() const { |
| 764 | return valid_ ? &breakpad_info_ : NULL; |
| 765 | } |
| 766 |
| 767 | // These thread IDs are used to determine if threads deserve special |
| 768 | // treatment, so special getters are provided to retrieve this data from |
| 769 | // the MDRawBreakpadInfo structure. The getters return false if the thread |
| 770 | // IDs cannot be determined. |
| 771 | bool GetDumpThreadID(uint32_t *thread_id) const; |
| 772 | bool GetRequestingThreadID(uint32_t *thread_id) const; |
| 773 |
| 774 | // Print a human-readable representation of the object to stdout. |
| 775 | void Print(); |
| 776 |
| 777 | private: |
| 778 | friend class Minidump; |
| 779 |
| 780 | static const uint32_t kStreamType = MD_BREAKPAD_INFO_STREAM; |
| 781 |
| 782 | explicit MinidumpBreakpadInfo(Minidump* minidump_); |
| 783 |
| 784 | bool Read(uint32_t expected_size_); |
| 785 |
| 786 | MDRawBreakpadInfo breakpad_info_; |
| 787 | }; |
| 788 |
| 789 | // MinidumpMemoryInfo wraps MDRawMemoryInfo, which provides information |
| 790 | // about mapped memory regions in a process, including their ranges |
| 791 | // and protection. |
| 792 | class MinidumpMemoryInfo : public MinidumpObject { |
| 793 | public: |
| 794 | const MDRawMemoryInfo* info() const { return valid_ ? &memory_info_ : NULL; } |
| 795 |
| 796 | // The address of the base of the memory region. |
| 797 | uint64_t GetBase() const { return valid_ ? memory_info_.base_address : 0; } |
| 798 |
| 799 | // The size, in bytes, of the memory region. |
| 800 | uint64_t GetSize() const { return valid_ ? memory_info_.region_size : 0; } |
| 801 |
| 802 | // Return true if the memory protection allows execution. |
| 803 | bool IsExecutable() const; |
| 804 |
| 805 | // Return true if the memory protection allows writing. |
| 806 | bool IsWritable() const; |
| 807 |
| 808 | // Print a human-readable representation of the object to stdout. |
| 809 | void Print(); |
| 810 |
| 811 | private: |
| 812 | // These objects are managed by MinidumpMemoryInfoList. |
| 813 | friend class MinidumpMemoryInfoList; |
| 814 |
| 815 | explicit MinidumpMemoryInfo(Minidump* minidump_); |
| 816 |
| 817 | // This works like MinidumpStream::Read, but is driven by |
| 818 | // MinidumpMemoryInfoList. No size checking is done, because |
| 819 | // MinidumpMemoryInfoList handles that directly. |
| 820 | bool Read(); |
| 821 |
| 822 | MDRawMemoryInfo memory_info_; |
| 823 | }; |
| 824 |
| 825 | // MinidumpMemoryInfoList contains a list of information about |
| 826 | // mapped memory regions for a process in the form of MDRawMemoryInfo. |
| 827 | // It maintains a map of these structures so that it may easily provide |
| 828 | // info corresponding to a specific address. |
| 829 | class MinidumpMemoryInfoList : public MinidumpStream { |
| 830 | public: |
| 831 | virtual ~MinidumpMemoryInfoList(); |
| 832 |
| 833 | unsigned int info_count() const { return valid_ ? info_count_ : 0; } |
| 834 |
| 835 | const MinidumpMemoryInfo* GetMemoryInfoForAddress(uint64_t address) const; |
| 836 | const MinidumpMemoryInfo* GetMemoryInfoAtIndex(unsigned int index) const; |
| 837 |
| 838 | // Print a human-readable representation of the object to stdout. |
| 839 | void Print(); |
| 840 |
| 841 | private: |
| 842 | friend class Minidump; |
| 843 |
| 844 | typedef vector<MinidumpMemoryInfo> MinidumpMemoryInfos; |
| 845 |
| 846 | static const uint32_t kStreamType = MD_MEMORY_INFO_LIST_STREAM; |
| 847 |
| 848 | explicit MinidumpMemoryInfoList(Minidump* minidump_); |
| 849 |
| 850 | bool Read(uint32_t expected_size); |
| 851 |
| 852 | // Access to memory info using addresses as the key. |
| 853 | RangeMap<uint64_t, unsigned int> *range_map_; |
| 854 |
| 855 | MinidumpMemoryInfos* infos_; |
| 856 | uint32_t info_count_; |
| 857 | }; |
| 858 |
| 859 | // MinidumpLinuxMaps wraps information about a single mapped memory region |
| 860 | // from /proc/self/maps. |
| 861 | class MinidumpLinuxMaps : public MinidumpObject { |
| 862 | public: |
| 863 | // The memory address of the base of the mapped region. |
| 864 | uint64_t GetBase() const { return valid_ ? region_.start : 0; } |
| 865 | // The size of the mapped region. |
| 866 | uint64_t GetSize() const { return valid_ ? region_.end - region_.start : 0; } |
| 867 |
| 868 | // The permissions of the mapped region. |
| 869 | bool IsReadable() const { |
| 870 | return valid_ ? region_.permissions & MappedMemoryRegion::READ : false; |
| 871 | } |
| 872 | bool IsWriteable() const { |
| 873 | return valid_ ? region_.permissions & MappedMemoryRegion::WRITE : false; |
| 874 | } |
| 875 | bool IsExecutable() const { |
| 876 | return valid_ ? region_.permissions & MappedMemoryRegion::EXECUTE : false; |
| 877 | } |
| 878 | bool IsPrivate() const { |
| 879 | return valid_ ? region_.permissions & MappedMemoryRegion::PRIVATE : false; |
| 880 | } |
| 881 |
| 882 | // The offset of the mapped region. |
| 883 | uint64_t GetOffset() const { return valid_ ? region_.offset : 0; } |
| 884 |
| 885 | // The major device number. |
| 886 | uint8_t GetMajorDevice() const { return valid_ ? region_.major_device : 0; } |
| 887 | // The minor device number. |
| 888 | uint8_t GetMinorDevice() const { return valid_ ? region_.minor_device : 0; } |
| 889 |
| 890 | // The inode of the mapped region. |
| 891 | uint64_t GetInode() const { return valid_ ? region_.inode : 0; } |
| 892 |
| 893 | // The pathname of the mapped region. |
| 894 | const string GetPathname() const { return valid_ ? region_.path : ""; } |
| 895 |
| 896 | // Print the contents of this mapping. |
| 897 | void Print() const; |
| 898 |
| 899 | private: |
| 900 | // These objects are managed by MinidumpLinuxMapsList. |
| 901 | friend class MinidumpLinuxMapsList; |
| 902 |
| 903 | // This caller owns the pointer. |
| 904 | explicit MinidumpLinuxMaps(Minidump *minidump); |
| 905 |
| 906 | // The memory region struct that this class wraps. |
| 907 | MappedMemoryRegion region_; |
| 908 |
| 909 | DISALLOW_COPY_AND_ASSIGN(MinidumpLinuxMaps); |
| 910 | }; |
| 911 |
| 912 | // MinidumpLinuxMapsList corresponds to the Linux-exclusive MD_LINUX_MAPS |
| 913 | // stream, which contains the contents of /prod/self/maps, which contains |
| 914 | // the mapped memory regions and their access permissions. |
| 915 | class MinidumpLinuxMapsList : public MinidumpStream { |
| 916 | public: |
| 917 | virtual ~MinidumpLinuxMapsList(); |
| 918 |
| 919 | // Get number of mappings. |
| 920 | unsigned int get_maps_count() const { return valid_ ? maps_count_ : 0; } |
| 921 |
| 922 | // Get mapping at the given memory address. The caller owns the pointer. |
| 923 | const MinidumpLinuxMaps *GetLinuxMapsForAddress(uint64_t address) const; |
| 924 | // Get mapping at the given index. The caller owns the pointer. |
| 925 | const MinidumpLinuxMaps *GetLinuxMapsAtIndex(unsigned int index) const; |
| 926 |
| 927 | // Print the contents of /proc/self/maps to stdout. |
| 928 | void Print() const; |
| 929 |
| 930 | private: |
| 931 | friend class Minidump; |
| 932 |
| 933 | typedef vector<MinidumpLinuxMaps *> MinidumpLinuxMappings; |
| 934 |
| 935 | static const uint32_t kStreamType = MD_LINUX_MAPS; |
| 936 |
| 937 | // The caller owns the pointer. |
| 938 | explicit MinidumpLinuxMapsList(Minidump *minidump); |
| 939 |
| 940 | // Read and load the contents of the process mapping data. |
| 941 | // The stream should have data in the form of /proc/self/maps. |
| 942 | // This method returns whether the stream was read successfully. |
| 943 | bool Read(uint32_t expected_size); |
| 944 |
| 945 | // The list of individual mappings. |
| 946 | MinidumpLinuxMappings *maps_; |
| 947 | // The number of mappings. |
| 948 | uint32_t maps_count_; |
| 949 |
| 950 | DISALLOW_COPY_AND_ASSIGN(MinidumpLinuxMapsList); |
| 951 | }; |
| 952 |
| 953 | // Minidump is the user's interface to a minidump file. It wraps MDRawHeader |
| 954 | // and provides access to the minidump's top-level stream directory. |
| 955 | class Minidump { |
| 956 | public: |
| 957 | // path is the pathname of a file containing the minidump. |
| 958 | explicit Minidump(const string& path); |
| 959 | // input is an istream wrapping minidump data. Minidump holds a |
| 960 | // weak pointer to input, and the caller must ensure that the stream |
| 961 | // is valid as long as the Minidump object is. |
| 962 | explicit Minidump(std::istream& input); |
| 963 |
| 964 | virtual ~Minidump(); |
| 965 |
| 966 | // path may be empty if the minidump was not opened from a file |
| 967 | virtual string path() const { |
| 968 | return path_; |
| 969 | } |
| 970 | static void set_max_streams(uint32_t max_streams) { |
| 971 | max_streams_ = max_streams; |
| 972 | } |
| 973 | static uint32_t max_streams() { return max_streams_; } |
| 974 |
| 975 | static void set_max_string_length(uint32_t max_string_length) { |
| 976 | max_string_length_ = max_string_length; |
| 977 | } |
| 978 | static uint32_t max_string_length() { return max_string_length_; } |
| 979 |
| 980 | virtual const MDRawHeader* header() const { return valid_ ? &header_ : NULL; } |
| 981 |
| 982 | // Reads the CPU information from the system info stream and generates the |
| 983 | // appropriate CPU flags. The returned context_cpu_flags are the same as |
| 984 | // if the CPU type bits were set in the context_flags of a context record. |
| 985 | // On success, context_cpu_flags will have the flags that identify the CPU. |
| 986 | // If a system info stream is missing, context_cpu_flags will be 0. |
| 987 | // Returns true if the current position in the stream was not changed. |
| 988 | // Returns false when the current location in the stream was changed and the |
| 989 | // attempt to restore the original position failed. |
| 990 | bool GetContextCPUFlagsFromSystemInfo(uint32_t* context_cpu_flags); |
| 991 |
| 992 | // Reads the minidump file's header and top-level stream directory. |
| 993 | // The minidump is expected to be positioned at the beginning of the |
| 994 | // header. Read() sets up the stream list and map, and validates the |
| 995 | // Minidump object. |
| 996 | virtual bool Read(); |
| 997 |
| 998 | // The next set of methods are stubs that call GetStream. They exist to |
| 999 | // force code generation of the templatized API within the module, and |
| 1000 | // to avoid exposing an ugly API (GetStream needs to accept a garbage |
| 1001 | // parameter). |
| 1002 | virtual MinidumpThreadList* GetThreadList(); |
| 1003 | virtual MinidumpModuleList* GetModuleList(); |
| 1004 | virtual MinidumpMemoryList* GetMemoryList(); |
| 1005 | virtual MinidumpException* GetException(); |
| 1006 | virtual MinidumpAssertion* GetAssertion(); |
| 1007 | virtual MinidumpSystemInfo* GetSystemInfo(); |
| 1008 | virtual MinidumpMiscInfo* GetMiscInfo(); |
| 1009 | virtual MinidumpBreakpadInfo* GetBreakpadInfo(); |
| 1010 | virtual MinidumpMemoryInfoList* GetMemoryInfoList(); |
| 1011 |
| 1012 | // The next method also calls GetStream, but is exclusive for Linux dumps. |
| 1013 | virtual MinidumpLinuxMapsList *GetLinuxMapsList(); |
| 1014 |
| 1015 | // The next set of methods are provided for users who wish to access |
| 1016 | // data in minidump files directly, while leveraging the rest of |
| 1017 | // this class and related classes to handle the basic minidump |
| 1018 | // structure and known stream types. |
| 1019 |
| 1020 | unsigned int GetDirectoryEntryCount() const { |
| 1021 | return valid_ ? header_.stream_count : 0; |
| 1022 | } |
| 1023 | const MDRawDirectory* GetDirectoryEntryAtIndex(unsigned int index) const; |
| 1024 |
| 1025 | // The next 2 methods are lower-level I/O routines. They use fd_. |
| 1026 |
| 1027 | // Reads count bytes from the minidump at the current position into |
| 1028 | // the storage area pointed to by bytes. bytes must be of sufficient |
| 1029 | // size. After the read, the file position is advanced by count. |
| 1030 | bool ReadBytes(void* bytes, size_t count); |
| 1031 |
| 1032 | // Sets the position of the minidump file to offset. |
| 1033 | bool SeekSet(off_t offset); |
| 1034 |
| 1035 | // Returns the current position of the minidump file. |
| 1036 | off_t Tell(); |
| 1037 |
| 1038 | // The next 2 methods are medium-level I/O routines. |
| 1039 |
| 1040 | // ReadString returns a string which is owned by the caller! offset |
| 1041 | // specifies the offset that a length-encoded string is stored at in the |
| 1042 | // minidump file. |
| 1043 | string* ReadString(off_t offset); |
| 1044 |
| 1045 | // SeekToStreamType positions the file at the beginning of a stream |
| 1046 | // identified by stream_type, and informs the caller of the stream's |
| 1047 | // length by setting *stream_length. Because stream_map maps each stream |
| 1048 | // type to only one stream in the file, this might mislead the user into |
| 1049 | // thinking that the stream that this seeks to is the only stream with |
| 1050 | // type stream_type. That can't happen for streams that these classes |
| 1051 | // deal with directly, because they're only supposed to be present in the |
| 1052 | // file singly, and that's verified when stream_map_ is built. Users who |
| 1053 | // are looking for other stream types should be aware of this |
| 1054 | // possibility, and consider using GetDirectoryEntryAtIndex (possibly |
| 1055 | // with GetDirectoryEntryCount) if expecting multiple streams of the same |
| 1056 | // type in a single minidump file. |
| 1057 | bool SeekToStreamType(uint32_t stream_type, uint32_t* stream_length); |
| 1058 |
| 1059 | bool swap() const { return valid_ ? swap_ : false; } |
| 1060 |
| 1061 | // Print a human-readable representation of the object to stdout. |
| 1062 | void Print(); |
| 1063 |
| 1064 | private: |
| 1065 | // MinidumpStreamInfo is used in the MinidumpStreamMap. It lets |
| 1066 | // the Minidump object locate interesting streams quickly, and |
| 1067 | // provides a convenient place to stash MinidumpStream objects. |
| 1068 | struct MinidumpStreamInfo { |
| 1069 | MinidumpStreamInfo() : stream_index(0), stream(NULL) {} |
| 1070 | ~MinidumpStreamInfo() { delete stream; } |
| 1071 |
| 1072 | // Index into the MinidumpDirectoryEntries vector |
| 1073 | unsigned int stream_index; |
| 1074 |
| 1075 | // Pointer to the stream if cached, or NULL if not yet populated |
| 1076 | MinidumpStream* stream; |
| 1077 | }; |
| 1078 |
| 1079 | typedef vector<MDRawDirectory> MinidumpDirectoryEntries; |
| 1080 | typedef map<uint32_t, MinidumpStreamInfo> MinidumpStreamMap; |
| 1081 |
| 1082 | template<typename T> T* GetStream(T** stream); |
| 1083 |
| 1084 | // Opens the minidump file, or if already open, seeks to the beginning. |
| 1085 | bool Open(); |
| 1086 |
| 1087 | // The largest number of top-level streams that will be read from a minidump. |
| 1088 | // Note that streams are only read (and only consume memory) as needed, |
| 1089 | // when directed by the caller. The default is 128. |
| 1090 | static uint32_t max_streams_; |
| 1091 |
| 1092 | // The maximum length of a UTF-16 string that will be read from a minidump |
| 1093 | // in 16-bit words. The default is 1024. UTF-16 strings are converted |
| 1094 | // to UTF-8 when stored in memory, and each UTF-16 word will be represented |
| 1095 | // by as many as 3 bytes in UTF-8. |
| 1096 | static unsigned int max_string_length_; |
| 1097 |
| 1098 | MDRawHeader header_; |
| 1099 |
| 1100 | // The list of streams. |
| 1101 | MinidumpDirectoryEntries* directory_; |
| 1102 |
| 1103 | // Access to streams using the stream type as the key. |
| 1104 | MinidumpStreamMap* stream_map_; |
| 1105 |
| 1106 | // The pathname of the minidump file to process, set in the constructor. |
| 1107 | // This may be empty if the minidump was opened directly from a stream. |
| 1108 | const string path_; |
| 1109 |
| 1110 | // The stream for all file I/O. Used by ReadBytes and SeekSet. |
| 1111 | // Set based on the path in Open, or directly in the constructor. |
| 1112 | std::istream* stream_; |
| 1113 |
| 1114 | // swap_ is true if the minidump file should be byte-swapped. If the |
| 1115 | // minidump was produced by a CPU that is other-endian than the CPU |
| 1116 | // processing the minidump, this will be true. If the two CPUs are |
| 1117 | // same-endian, this will be false. |
| 1118 | bool swap_; |
| 1119 |
| 1120 | // Validity of the Minidump structure, false immediately after |
| 1121 | // construction or after a failed Read(); true following a successful |
| 1122 | // Read(). |
| 1123 | bool valid_; |
| 1124 | }; |
| 1125 |
| 1126 |
| 1127 | } // namespace google_breakpad |
| 1128 |
| 1129 |
| 1130 | #endif // GOOGLE_BREAKPAD_PROCESSOR_MINIDUMP_H__ |
| 1131 |