| 1 |
// Copyright 2015 Ted Mielczarek. See the COPYRIGHT |
| 2 |
// file at the top-level directory of this distribution. |
| 3 |
|
| 4 |
//! A parser for the minidump file format. |
| 5 |
//! |
| 6 |
//! The `minidump` module provides a parser for the |
| 7 |
//! [minidump][minidump] file format as produced by Microsoft's |
| 8 |
//! [`MinidumpWriteDump`][minidumpwritedump] API and the |
| 9 |
//! [Google Breakpad][breakpad] library. |
| 10 |
//! |
| 11 |
//! |
| 12 |
//! |
| 13 |
//! # Usage |
| 14 |
//! |
| 15 |
//! The primary API for this library is the [`Minidump`][] struct, which can be |
| 16 |
//! instantiated by calling the [`Minidump::read`][] or [`Minidump::read_path`][] |
| 17 |
//! methods. |
| 18 |
//! |
| 19 |
//! Successfully parsing a Minidump struct means the minidump has a minimally valid |
| 20 |
//! header and stream directory. Individual streams are only parsed when they're |
| 21 |
//! requested. |
| 22 |
//! |
| 23 |
//! Although you may enumerate the streams in a minidump with methods like |
| 24 |
//! [`Minidump::all_streams`][], this is only really useful for debugging. Instead |
| 25 |
//! you should statically request streams with [`Minidump::get_stream`][]. |
| 26 |
//! Depending on what analysis you're trying to perform, you may: |
| 27 |
//! |
| 28 |
//! * Consider it an error for a stream to be missing (using `?` or `unwrap`) |
| 29 |
//! * Branch on the presence of stream to conditionally refine your analysis |
| 30 |
//! * Use a stream's `Default` implementation to get an "empty" instance |
| 31 |
//! (with `unwrap_or_default`) |
| 32 |
//! |
| 33 |
//! ``` |
| 34 |
//! use minidump::*; |
| 35 |
//! |
| 36 |
//! fn main() -> Result<(), Error> { |
| 37 |
//! // Read the minidump from a file |
| 38 |
//! let mut dump = minidump::Minidump::read_path("../testdata/test.dmp")?; |
| 39 |
//! |
| 40 |
//! // Statically request (and require) several streams we care about: |
| 41 |
//! let system_info = dump.get_stream::<MinidumpSystemInfo>()?; |
| 42 |
//! let exception = dump.get_stream::<MinidumpException>()?; |
| 43 |
//! |
| 44 |
//! // Combine the contents of the streams to perform more refined analysis |
| 45 |
//! let crash_reason = exception.get_crash_reason(system_info.os, system_info.cpu); |
| 46 |
//! |
| 47 |
//! // Conditionally analyze a stream |
| 48 |
//! if let Ok(threads) = dump.get_stream::<MinidumpThreadList>() { |
| 49 |
//! // Use `Default` to try to make progress when a stream is missing. |
| 50 |
//! // This is especially natural for MinidumpMemoryList because |
| 51 |
//! // everything needs to handle memory lookups failing anyway. |
| 52 |
//! let mem = dump.get_memory().unwrap_or_default(); |
| 53 |
//! |
| 54 |
//! for thread in &threads.threads { |
| 55 |
//! let stack = thread.stack_memory(&mem); |
| 56 |
//! // ... |
| 57 |
//! } |
| 58 |
//! } |
| 59 |
//! |
| 60 |
//! Ok(()) |
| 61 |
//! } |
| 62 |
//! ``` |
| 63 |
//! |
| 64 |
//! Generally speaking, there isn't any reason to distinguish between a stream being |
| 65 |
//! absent and it being corrupt. Just ask for what you want and we'll do our best |
| 66 |
//! to give it to you. |
| 67 |
//! |
| 68 |
//! Everything else you would want to do with a Minidump is specific to the |
| 69 |
//! individual streams: |
| 70 |
//! |
| 71 |
//! * [`MinidumpAssertion`][] |
| 72 |
//! * [`MinidumpBreakpadInfo`][] |
| 73 |
//! * [`MinidumpCrashpadInfo`][] |
| 74 |
//! * [`MinidumpException`][] |
| 75 |
//! * [`MinidumpLinuxCpuInfo`][] |
| 76 |
//! * [`MinidumpLinuxEnviron`][] |
| 77 |
//! * [`MinidumpLinuxLsbRelease`][] |
| 78 |
//! * [`MinidumpLinuxMaps`][] |
| 79 |
//! * [`MinidumpLinuxProcStatus`][] |
| 80 |
//! * [`MinidumpMacCrashInfo`][] |
| 81 |
//! * [`MinidumpMacBootargs`][] |
| 82 |
//! * [`MinidumpMemoryList`][] |
| 83 |
//! * [`MinidumpMemoryInfoList`][] |
| 84 |
//! * [`MinidumpMiscInfo`][] |
| 85 |
//! * [`MinidumpModuleList`][] |
| 86 |
//! * [`MinidumpSystemInfo`][] |
| 87 |
//! * [`MinidumpThreadList`][] |
| 88 |
//! * [`MinidumpThreadNames`][] |
| 89 |
//! * [`MinidumpUnloadedModuleList`][] |
| 90 |
//! * [`MinidumpLinuxProcLimits`][] |
| 91 |
//! |
| 92 |
//! |
| 93 |
//! |
| 94 |
//! |
| 95 |
//! # Notable Streams |
| 96 |
//! |
| 97 |
//! There's a lot of different Minidump Streams, but some are especially |
| 98 |
//! notable/fundamental: |
| 99 |
//! |
| 100 |
//! [`MinidumpSystemInfo`][] includes details about the hardware and operating |
| 101 |
//! system that the crash occured on. This information is often required to |
| 102 |
//! properly interpret the other streams of the minidump, as they contain |
| 103 |
//! platform-specific values. |
| 104 |
//! |
| 105 |
//! [`MinidumpException`][] includes actual details about where and why the crash |
| 106 |
//! occured. |
| 107 |
//! |
| 108 |
//! [`MinidumpThreadList`][] includes the registers and stack memory of every thread |
| 109 |
//! in the program at the time of the crash. This enables generating backtraces for |
| 110 |
//! every thread. |
| 111 |
//! |
| 112 |
//! [`MinidumpMemoryList`][] maps the crashing program's runtime addresses (such as |
| 113 |
//! `$rsp`) to ranges of memory in the Minidump. |
| 114 |
//! |
| 115 |
//! [`MinidumpModuleList`][] includes info on all the modules (libraries) that were |
| 116 |
//! linked into the crashing program. This enables symbolication, as you can map |
| 117 |
//! instruction addresses back to offsets in a specific library's binary. |
| 118 |
//! |
| 119 |
//! |
| 120 |
//! |
| 121 |
//! |
| 122 |
//! # What is a Minidump? |
| 123 |
//! |
| 124 |
//! Minidumps capture the state of a crashing process (threads, stack memory, |
| 125 |
//! registers, dlls), why it crashed (crashing thread, error codes, error |
| 126 |
//! messages), and details about the system the program was running on (os, cpu). |
| 127 |
//! |
| 128 |
//! The information in a minidump is divided up into a series of |
| 129 |
//! independent "streams". If you want a specific piece of information, you must |
| 130 |
//! know the stream that contains it, and then look up that stream in the |
| 131 |
//! minidump's directory. Most streams are pretty straight-forward -- you can guess |
| 132 |
//! what you might find in [`MinidumpThreadList`][] or [`MinidumpSystemInfo`][] |
| 133 |
//! -- but others -- like [`MinidumpMiscInfo`][] -- are a bit more random. |
| 134 |
//! |
| 135 |
//! This [format][minidump] was initially defined by Microsoft, as Windows has long |
| 136 |
//! included [system apis to generate minidumps][minidumpwritedump]. But lots of |
| 137 |
//! software gets made for operating systems other than Windows, where no such |
| 138 |
//! native support for minidumps is present. [google-breakpad][breakpad] was |
| 139 |
//! created to extend Microsoft's minidump format to other platforms, and defines |
| 140 |
//! minidump generators for things like Linux and MacOS. |
| 141 |
//! |
| 142 |
//! I do not believe that Microsoft and Breakpad officially collaborate on the |
| 143 |
//! format, it's just designed to be very extensible, so it's easy to add random |
| 144 |
//! stuff to a minidump in ways that don't break old tools and likely won't |
| 145 |
//! interfere with future versions. That said, Microsoft does now develop |
| 146 |
//! cross-platform products that make use of Breakpad, such as VSCode, so at very |
| 147 |
//! least their crash reporting infra deals with Breakpad minidumps. |
| 148 |
//! |
| 149 |
//! The rust-minidump crates are specifically designed to support Breakpad's |
| 150 |
//! extended minidump format (and native Windows minidumps, which should in theory |
| 151 |
//! just be a subset). That said, rust-minidump doesn't yet (and probably won't |
| 152 |
//! ever) support *everything*. There's a lot of random stuff that either Microsoft |
| 153 |
//! or Breakpad have defined over the years that we just, do not have any use for |
| 154 |
//! at the moment. Not a lot of demand for handling minidumps for PlayStation 3, |
| 155 |
//! SPARC, or Windows CE these days. |
| 156 |
//! |
| 157 |
//! |
| 158 |
//! |
| 159 |
//! |
| 160 |
//! |
| 161 |
//! # The Minidump Format |
| 162 |
//! |
| 163 |
//! This section is dedicated to describing how to parse minidumps, for anyone |
| 164 |
//! wanting to maintain this code or write their own parser. |
| 165 |
//! |
| 166 |
//! Minidumps are a binary format. This format is simultaneously very simple and |
| 167 |
//! very complicated. |
| 168 |
//! |
| 169 |
//! The simple part of a minidump is that it's basically just an array of pointers |
| 170 |
//! to different typed "Streams" (system info, exception info, threads, memory |
| 171 |
//! mappings, etc.). So if you want to lookup the system info, you just search the |
| 172 |
//! array for a system info stream and interpret that range of memory as that |
| 173 |
//! stream. |
| 174 |
//! |
| 175 |
//! The complicated part of a minidump is the fact that every stream contains |
| 176 |
//! totally different information in totally different formats. Sure, there are |
| 177 |
//! families of streams that have the same general structure, but you've still got |
| 178 |
//! to write custom code to interpret the values meaningfully and figure out what |
| 179 |
//! on earth that information is useful for. |
| 180 |
//! |
| 181 |
//! Sometimes the answer to "what is it useful for?" is "I don't know but maybe |
| 182 |
//! we'll find a use for it later". This is genuinely useful because it allows us |
| 183 |
//! to add new analyses long after a crash occurs and gain new insights that the |
| 184 |
//! minidump format wasn't explicitly designed to provide. |
| 185 |
//! |
| 186 |
//! This is all to say that, beyond the basic layout of the minidump header and |
| 187 |
//! directory, it's basically just a big ball of random formats with independent |
| 188 |
//! formats and layout -- and everyone is technically free to come up with their |
| 189 |
//! own custom Streams that they can just toss in there, so trying to cover |
| 190 |
//! everything is kind of impossible? Lets see how far we get! |
| 191 |
//! |
| 192 |
//! |
| 193 |
//! |
| 194 |
//! ## The Minidump Header and Directory |
| 195 |
//! |
| 196 |
//! The first thing in a Minidump is the [`MINIDUMP_HEADER`][format::MINIDUMP_HEADER], which has the |
| 197 |
//! following layout: |
| 198 |
//! |
| 199 |
//! ``` |
| 200 |
//! pub struct MINIDUMP_HEADER { |
| 201 |
//! pub signature: u32, |
| 202 |
//! pub version: u32, |
| 203 |
//! pub stream_count: u32, |
| 204 |
//! pub stream_directory_rva: RVA, |
| 205 |
//! pub checksum: u32, |
| 206 |
//! pub time_date_stamp: u32, |
| 207 |
//! pub flags: u64, |
| 208 |
//! } |
| 209 |
//! |
| 210 |
//! /// Offset into the minidump |
| 211 |
//! pub type RVA = u32; |
| 212 |
//! ``` |
| 213 |
//! |
| 214 |
//! The `signature` is always [`MINIDUMP_SIGNATURE`][format::MINIDUMP_SIGNATURE] = `0x504d444d` |
| 215 |
//! ("MDMP" in ascii). You can use this to detect whether the minidump is little-endian or |
| 216 |
//! big-endian (minidumps always have the endianess of platform they were generated |
| 217 |
//! on, since they contain lots of raw memory from the process, but at this point |
| 218 |
//! we don't know what that platform is). |
| 219 |
//! |
| 220 |
//! The lower 16 bits of `version` are always |
| 221 |
//! [`MINIDUMP_VERSION`][format::MINIDUMP_VERSION] = 42899. |
| 222 |
//! (The high bits contain implementation-specific values that you should just |
| 223 |
//! ignore). |
| 224 |
//! |
| 225 |
//! `stream_directory_rva` and `stream_count` are the location (offset from the |
| 226 |
//! start of the file, in bytes) and size of the stream directory, respectively. |
| 227 |
//! |
| 228 |
//! `checksum` is some kind of checksum of the minidump itself (which may be null), |
| 229 |
//! but the algorithm isn't specified, and rust-minidump doesn't check it. |
| 230 |
//! |
| 231 |
//! `time_date_stamp` is a Windows `time_t` of when the miniump was generated. |
| 232 |
//! |
| 233 |
//! `flags` are a [`MINIDUMP_TYPE`][MINIDUMP_TYPE] which largely just specify what you can expect |
| 234 |
//! to find in the minidump. This is unused by rust-minidump since this information |
| 235 |
//! is generally redundant with the stream directory and flags within the streams |
| 236 |
//! that we need to check anyway. (e.g. instead of checking that this is a |
| 237 |
//! `MiniDumpWithUnloadedModules`, you can just check the directory for the |
| 238 |
//! [`MinidumpUnloadedModuleList`][] stream.) |
| 239 |
//! |
| 240 |
//! At `stream_directory_rva` (typically immediately after the header) you will find |
| 241 |
//! an array of `stream_count` [`MINIDUMP_DIRECTORY`][format::MINIDUMP_DIRECTORY] entries, |
| 242 |
//! with the following layout: |
| 243 |
//! |
| 244 |
//! ``` |
| 245 |
//! pub struct MINIDUMP_DIRECTORY { |
| 246 |
//! /// The type of the stream |
| 247 |
//! pub stream_type: u32, |
| 248 |
//! /// The location of the stream contents within the dump. |
| 249 |
//! pub location: MINIDUMP_LOCATION_DESCRIPTOR, |
| 250 |
//! } |
| 251 |
//! |
| 252 |
//! /// A "slice" of the minidump |
| 253 |
//! pub struct MINIDUMP_LOCATION_DESCRIPTOR { |
| 254 |
//! /// The size of this data (in bytes) |
| 255 |
//! pub data_size: u32, |
| 256 |
//! /// The offset to this data within the minidump file. |
| 257 |
//! pub rva: RVA, |
| 258 |
//! } |
| 259 |
//! |
| 260 |
//! /// Offset into the minidump |
| 261 |
//! pub type RVA = u32; |
| 262 |
//! ``` |
| 263 |
//! |
| 264 |
//! Known `stream_type` values are defined in |
| 265 |
//! [`MINIDUMP_STREAM_TYPE`][format::MINIDUMP_STREAM_TYPE], but users |
| 266 |
//! are allowed to define their own stream types, so it's normal to see unknown |
| 267 |
//! types (this is the primary mechanism breakpad uses to extend the format without |
| 268 |
//! causing upstream problems). |
| 269 |
//! |
| 270 |
//! And that's it! Everything else in a minidump is just all the different types of |
| 271 |
//! stream. As of this writing, rust-minidump is aware of 51 different types of |
| 272 |
//! stream, and implements 18 of them (there's a long tail of platform-specific and |
| 273 |
//! domain-specific streams, so that isn't as bad as it sounds). |
| 274 |
//! |
| 275 |
//! |
| 276 |
//! |
| 277 |
//! |
| 278 |
//! ## Stream Format Families |
| 279 |
//! |
| 280 |
//! Although every stream can do whatever it wants, there's a lot of streams that |
| 281 |
//! are basically "a struct" or "a list of structs", so the same header formats and |
| 282 |
//! layouts are used in several places. (This is descriptive, so these aren't |
| 283 |
//! necessarily official terms/concepts.) |
| 284 |
//! |
| 285 |
//! |
| 286 |
//! |
| 287 |
//! ### Plain Old Struct Streams |
| 288 |
//! |
| 289 |
//! A stream that's just a struct. |
| 290 |
//! |
| 291 |
//! That's it. Just read the struct out of the stream. Although it might contain |
| 292 |
//! RVAs to other data, which may or may not be relative to the start of the stream |
| 293 |
//! or the start of the file (annoyingly inconsistent between streams). |
| 294 |
//! |
| 295 |
//! Known members of this family: |
| 296 |
//! |
| 297 |
//! * [`MinidumpAssertion`][] (contains [`MINIDUMP_ASSERTION_INFO`][format::MINIDUMP_ASSERTION_INFO]) |
| 298 |
//! * [`MinidumpBreakpadInfo`][] (contains [`MINIDUMP_BREAKPAD_INFO`][format::MINIDUMP_BREAKPAD_INFO]) |
| 299 |
//! * [`MinidumpCrashpadInfo`][] (contains [`MINIDUMP_CRASHPAD_INFO`][format::MINIDUMP_CRASHPAD_INFO]) |
| 300 |
//! * [`MinidumpException`][] (contains [`MINIDUMP_EXCEPTION_STREAM`][format::MINIDUMP_EXCEPTION_STREAM]) |
| 301 |
//! * [`MinidumpSystemInfo`][] (contains [`MINIDUMP_SYSTEM_INFO`][format::MINIDUMP_SYSTEM_INFO]) |
| 302 |
//! |
| 303 |
//! |
| 304 |
//! |
| 305 |
//! ### List Streams |
| 306 |
//! |
| 307 |
//! A list of some entry type. |
| 308 |
//! |
| 309 |
//! A `u32` count of entries followed by an array of entries. There may be padding |
| 310 |
//! between the count and the entries. The array should be "right-justified" in the |
| 311 |
//! stream (the stream ends exactly where the array does), so you can use the |
| 312 |
//! difference between the array's expected size and the rest of the stream's size |
| 313 |
//! to determine the padding. |
| 314 |
//! |
| 315 |
//! This format is used by a lot of the oldest (and therefore most important) |
| 316 |
//! minidump streams. |
| 317 |
//! |
| 318 |
//! Known members of this family: |
| 319 |
//! |
| 320 |
//! * [`MinidumpMemoryList`] (entries are [`MINIDUMP_MEMORY_DESCRIPTOR`][format::MINIDUMP_MEMORY_DESCRIPTOR]) |
| 321 |
//! * [`MinidumpModuleList`] (entries are [`MINIDUMP_MODULE`][format::MINIDUMP_MODULE]) |
| 322 |
//! * [`MinidumpThreadList`] (entries are [`MINIDUMP_THREAD`][format::MINIDUMP_THREAD]) |
| 323 |
//! * [`MinidumpThreadNames`] (entries are [`MINIDUMP_THREAD_NAME`][format::MINIDUMP_THREAD_NAME]) |
| 324 |
//! * `MINIDUMP_THREAD_EX_LIST` (yes, the stream with "EX_LIST" in the name isn't an |
| 325 |
//! EX list, names are hard.) |
| 326 |
//! |
| 327 |
//! The stream [`MinidumpMemory64List`] is a variant of list stream. It starts with |
| 328 |
//! a `u64` count of entries, a 64-bit shared RVA for all entries, then followed by |
| 329 |
//! an array of entires [`MINIDUMP_MEMORY_DESCRIPTOR64`][format::MINIDUMP_MEMORY_DESCRIPTOR64]. |
| 330 |
//! |
| 331 |
//! |
| 332 |
//! ### EX List Streams |
| 333 |
//! |
| 334 |
//! A newer and more flexible version of list streams. (so EXtreme!!!) |
| 335 |
//! |
| 336 |
//! EX list streams start with this header: |
| 337 |
//! |
| 338 |
//! ``` |
| 339 |
//! struct EX_LIST_HEADER { |
| 340 |
//! /// Size (in bytes) of this header (array starts immediately after) |
| 341 |
//! pub size_of_header: u32, |
| 342 |
//! /// Size (in bytes) of an entry in the array |
| 343 |
//! pub size_of_entry: u32, |
| 344 |
//! /// The number of entries in the array |
| 345 |
//! pub number_of_entries: u32, |
| 346 |
//! } |
| 347 |
//! ``` |
| 348 |
//! |
| 349 |
//! This design allows newer versions of the stream to be introduced, and for fields |
| 350 |
//! to be added to the end of an entry type. I am not aware of an instance where |
| 351 |
//! this flexibility has been used yet, but in theory you could identify "versions" |
| 352 |
//! of the stream format by size, and older versions don't need to worry about |
| 353 |
//! unknown future revisions, because they can just ignore the trailing bytes of |
| 354 |
//! each entry. |
| 355 |
//! |
| 356 |
//! Known members of this family: |
| 357 |
//! |
| 358 |
//! * [`MinidumpMemoryInfoList`][] (entries are [`MINIDUMP_MEMORY_INFO`][format::MINIDUMP_MEMORY_INFO]) |
| 359 |
//! * [`MinidumpUnloadedModuleList`][] (entries are [`MINIDUMP_UNLOADED_MODULE`][format::MINIDUMP_UNLOADED_MODULE]) |
| 360 |
//! * [`MinidumpHandleDataStream`][] is a slight variation of this format with different |
| 361 |
//! filed names and a trailing `u32` member reserved for future use (entries |
| 362 |
//! are [`MINIDUMP_HANDLE_DESCRIPTOR`][format::MINIDUMP_HANDLE_DESCRIPTOR] and |
| 363 |
//! [`MINIDUMP_HANDLE_DESCRIPTOR_2`][format::MINIDUMP_HANDLE_DESCRIPTOR_2]) |
| 364 |
//! * [`MinidumpThreadInfoList`][] (entries are [`MINIDUMP_THREAD_INFO`][format::MINIDUMP_THREAD_INFO]) |
| 365 |
//! |
| 366 |
//! |
| 367 |
//! |
| 368 |
//! ### Linux List Streams |
| 369 |
//! |
| 370 |
//! A dump of a special linux file like `/proc/cpuinfo`. |
| 371 |
//! |
| 372 |
//! These streams are plain text ([`strings::LinuxOsString`][]) files containing |
| 373 |
//! line-delimited key-value pairs, like: |
| 374 |
//! |
| 375 |
//! ```text |
| 376 |
//! processor : 0 |
| 377 |
//! vendor_id : GenuineIntel |
| 378 |
//! cpu family : 6 |
| 379 |
//! model : 45 |
| 380 |
//! model name : Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz |
| 381 |
//! ``` |
| 382 |
//! |
| 383 |
//! Whitespace and separators vary from stream to stream. |
| 384 |
//! |
| 385 |
//! Known members of this family: |
| 386 |
//! |
| 387 |
//! * [`MinidumpLinuxCpuInfo`][] (separator is `:`) |
| 388 |
//! * [`MinidumpLinuxEnviron`][] (separator is `=`) |
| 389 |
//! * [`MinidumpLinuxLsbRelease`][] (separator is `=`) |
| 390 |
//! * [`MinidumpLinuxProcStatus`][] (separator is `:`) |
| 391 |
//! * [`MinidumpLinuxProcLimits`][] (separator is ` `) |
| 392 |
//! |
| 393 |
//! |
| 394 |
//! |
| 395 |
//! [MINIDUMP_TYPE]: https://docs.microsoft.com/en-us/windows/win32/api/minidumpapiset/ne-minidumpapiset-minidump_type |
| 396 |
//! [minidump]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680369%28v=vs.85%29.aspx |
| 397 |
//! [minidumpwritedump]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680360%28v=vs.85%29.aspx |
| 398 |
//! [breakpad]: https://chromium.googlesource.com/breakpad/breakpad/+/master/ |
| 399 |
|
| 400 |
#![warn(missing_debug_implementations)] |
| 401 |
|
| 402 |
#[cfg(doctest)] |
| 403 |
doc_comment::doctest!("../README.md"); |
| 404 |
|
| 405 |
pub use scroll::Endian; |
| 406 |
|
| 407 |
mod context; |
| 408 |
mod iostuff; |
| 409 |
mod minidump; |
| 410 |
|
| 411 |
pub use minidump_common::format; |
| 412 |
pub use minidump_common::traits::Module; |
| 413 |
|
| 414 |
pub use crate::iostuff::Readable; |
| 415 |
pub use crate::minidump::*; |
| 416 |
|
| 417 |
pub mod strings; |
| 418 |
pub mod system_info; |