| 1 |
use crate::{ |
| 2 |
dir_section::{DirSection, DumpBuf}, |
| 3 |
mac::{errors::WriterError, task_dumper::TaskDumper}, |
| 4 |
mem_writer::*, |
| 5 |
minidump_format::{self, MDMemoryDescriptor, MDRawDirectory, MDRawHeader}, |
| 6 |
}; |
| 7 |
use std::io::{Seek, Write}; |
| 8 |
|
| 9 |
pub use mach2::mach_types::{task_t, thread_t}; |
| 10 |
|
| 11 |
type Result<T> = std::result::Result<T, WriterError>; |
| 12 |
|
| 13 |
pub struct MinidumpWriter { |
| 14 |
/// The crash context as captured by an exception handler |
| 15 |
pub(crate) crash_context: Option<crash_context::CrashContext>, |
| 16 |
/// List of raw blocks of memory we've written into the stream. These are |
| 17 |
/// referenced by other streams (eg thread list) |
| 18 |
pub(crate) memory_blocks: Vec<MDMemoryDescriptor>, |
| 19 |
/// The task being dumped |
| 20 |
pub(crate) task: task_t, |
| 21 |
/// The handler thread, so it can be ignored/deprioritized |
| 22 |
pub(crate) handler_thread: thread_t, |
| 23 |
} |
| 24 |
|
| 25 |
impl MinidumpWriter { |
| 26 |
/// Creates a minidump writer for the specified mach task (process) and |
| 27 |
/// handler thread. If not specified, defaults to the current task and thread. |
| 28 |
/// |
| 29 |
/// ``` |
| 30 |
/// use minidump_writer::{minidump_writer::MinidumpWriter, mach2}; |
| 31 |
/// |
| 32 |
/// // Note that this is the same as specifying `None` for both the task and |
| 33 |
/// // handler thread, this is just meant to illustrate how you can setup |
| 34 |
/// // a MinidumpWriter manually instead of using a `CrashContext` |
| 35 |
/// // SAFETY: syscalls |
| 36 |
/// let mdw = unsafe { |
| 37 |
/// MinidumpWriter::new( |
| 38 |
/// Some(mach2::traps::mach_task_self()), |
| 39 |
/// Some(mach2::mach_init::mach_thread_self()), |
| 40 |
/// ) |
| 41 |
/// }; |
| 42 |
/// ``` |
| 43 |
pub fn new(task: Option<task_t>, handler_thread: Option<thread_t>) -> Self { |
| 44 |
Self { |
| 45 |
crash_context: None, |
| 46 |
memory_blocks: Vec::new(), |
| 47 |
task: task.unwrap_or_else(|| { |
| 48 |
// SAFETY: syscall |
| 49 |
unsafe { mach2::traps::mach_task_self() } |
| 50 |
}), |
| 51 |
handler_thread: handler_thread.unwrap_or_else(|| { |
| 52 |
// SAFETY: syscall |
| 53 |
unsafe { mach2::mach_init::mach_thread_self() } |
| 54 |
}), |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/// Creates a minidump writer with the specified crash context, presumably |
| 59 |
/// for another task |
| 60 |
pub fn with_crash_context(crash_context: crash_context::CrashContext) -> Self { |
| 61 |
let task = crash_context.task; |
| 62 |
let handler_thread = crash_context.handler_thread; |
| 63 |
|
| 64 |
Self { |
| 65 |
crash_context: Some(crash_context), |
| 66 |
memory_blocks: Vec::new(), |
| 67 |
task, |
| 68 |
handler_thread, |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
/// Writes a minidump to the specified destination, returning the raw minidump |
| 73 |
/// contents upon success |
| 74 |
pub fn dump(&mut self, destination: &mut (impl Write + Seek)) -> Result<Vec<u8>> { |
| 75 |
let writers = { |
| 76 |
#[allow(clippy::type_complexity)] |
| 77 |
let mut writers: Vec< |
| 78 |
Box<dyn FnMut(&mut Self, &mut DumpBuf, &TaskDumper) -> Result<MDRawDirectory>>, |
| 79 |
> = vec![ |
| 80 |
Box::new(|mw, buffer, dumper| mw.write_thread_list(buffer, dumper)), |
| 81 |
Box::new(|mw, buffer, dumper| mw.write_memory_list(buffer, dumper)), |
| 82 |
Box::new(|mw, buffer, dumper| mw.write_system_info(buffer, dumper)), |
| 83 |
Box::new(|mw, buffer, dumper| mw.write_module_list(buffer, dumper)), |
| 84 |
Box::new(|mw, buffer, dumper| mw.write_misc_info(buffer, dumper)), |
| 85 |
Box::new(|mw, buffer, dumper| mw.write_breakpad_info(buffer, dumper)), |
| 86 |
Box::new(|mw, buffer, dumper| mw.write_thread_names(buffer, dumper)), |
| 87 |
]; |
| 88 |
|
| 89 |
// Exception stream needs to be the last entry in this array as it may |
| 90 |
// be omitted in the case where the minidump is written without an |
| 91 |
// exception. |
| 92 |
if self |
| 93 |
.crash_context |
| 94 |
.as_ref() |
| 95 |
.and_then(|cc| cc.exception.as_ref()) |
| 96 |
.is_some() |
| 97 |
{ |
| 98 |
writers.push(Box::new(|mw, buffer, dumper| { |
| 99 |
mw.write_exception(buffer, dumper) |
| 100 |
})); |
| 101 |
} |
| 102 |
|
| 103 |
writers |
| 104 |
}; |
| 105 |
|
| 106 |
let num_writers = writers.len() as u32; |
| 107 |
let mut buffer = Buffer::with_capacity(0); |
| 108 |
|
| 109 |
let mut header_section = MemoryWriter::<MDRawHeader>::alloc(&mut buffer)?; |
| 110 |
let mut dir_section = DirSection::new(&mut buffer, num_writers, destination)?; |
| 111 |
|
| 112 |
let header = MDRawHeader { |
| 113 |
signature: minidump_format::MD_HEADER_SIGNATURE, |
| 114 |
version: minidump_format::MD_HEADER_VERSION, |
| 115 |
stream_count: num_writers, |
| 116 |
stream_directory_rva: dir_section.position(), |
| 117 |
checksum: 0, /* Can be 0. In fact, that's all that's |
| 118 |
* been found in minidump files. */ |
| 119 |
time_date_stamp: std::time::SystemTime::now() |
| 120 |
.duration_since(std::time::UNIX_EPOCH) |
| 121 |
.unwrap() |
| 122 |
.as_secs() as u32, // TODO: This is not Y2038 safe, but thats how its currently defined as |
| 123 |
flags: 0, |
| 124 |
}; |
| 125 |
header_section.set_value(&mut buffer, header)?; |
| 126 |
|
| 127 |
// Ensure the header gets flushed. If we crash somewhere below, |
| 128 |
// we should have a mostly-intact dump |
| 129 |
dir_section.write_to_file(&mut buffer, None)?; |
| 130 |
|
| 131 |
let dumper = super::task_dumper::TaskDumper::new(self.task); |
| 132 |
|
| 133 |
for mut writer in writers { |
| 134 |
let dirent = writer(self, &mut buffer, &dumper)?; |
| 135 |
dir_section.write_to_file(&mut buffer, Some(dirent))?; |
| 136 |
} |
| 137 |
|
| 138 |
Ok(buffer.into()) |
| 139 |
} |
| 140 |
|
| 141 |
/// Retrieves the list of active threads in the target process, except |
| 142 |
/// the handler thread if it is known, to simplify dump analysis |
| 143 |
#[inline] |
| 144 |
pub(crate) fn threads(&self, dumper: &TaskDumper) -> ActiveThreads { |
| 145 |
ActiveThreads { |
| 146 |
threads: dumper.read_threads().unwrap_or_default(), |
| 147 |
handler_thread: self.handler_thread, |
| 148 |
i: 0, |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
pub(crate) struct ActiveThreads { |
| 154 |
threads: &'static [u32], |
| 155 |
handler_thread: u32, |
| 156 |
i: usize, |
| 157 |
} |
| 158 |
|
| 159 |
impl ActiveThreads { |
| 160 |
#[inline] |
| 161 |
pub(crate) fn len(&self) -> usize { |
| 162 |
let mut len = self.threads.len(); |
| 163 |
|
| 164 |
if self.handler_thread != mach2::port::MACH_PORT_NULL { |
| 165 |
len -= 1; |
| 166 |
} |
| 167 |
|
| 168 |
len |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
impl Iterator for ActiveThreads { |
| 173 |
type Item = u32; |
| 174 |
|
| 175 |
fn next(&mut self) -> Option<Self::Item> { |
| 176 |
while self.i < self.threads.len() { |
| 177 |
let i = self.i; |
| 178 |
self.i += 1; |
| 179 |
|
| 180 |
if self.threads[i] != self.handler_thread { |
| 181 |
return Some(self.threads[i]); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
None |
| 186 |
} |
| 187 |
} |