| 1 |
#![allow(unsafe_code)] |
| 2 |
|
| 3 |
use crate::windows::errors::Error; |
| 4 |
use crate::windows::ffi::{ |
| 5 |
capture_context, CloseHandle, GetCurrentProcess, GetCurrentThreadId, GetThreadContext, |
| 6 |
MiniDumpWriteDump, MinidumpType, OpenProcess, OpenThread, ResumeThread, SuspendThread, |
| 7 |
EXCEPTION_POINTERS, EXCEPTION_RECORD, FALSE, HANDLE, MINIDUMP_EXCEPTION_INFORMATION, |
| 8 |
MINIDUMP_USER_STREAM, MINIDUMP_USER_STREAM_INFORMATION, PROCESS_ALL_ACCESS, |
| 9 |
STATUS_NONCONTINUABLE_EXCEPTION, THREAD_GET_CONTEXT, THREAD_QUERY_INFORMATION, |
| 10 |
THREAD_SUSPEND_RESUME, |
| 11 |
}; |
| 12 |
use minidump_common::format::{BreakpadInfoValid, MINIDUMP_BREAKPAD_INFO, MINIDUMP_STREAM_TYPE}; |
| 13 |
use scroll::Pwrite; |
| 14 |
use std::os::windows::io::AsRawHandle; |
| 15 |
|
| 16 |
pub struct MinidumpWriter { |
| 17 |
|
| 18 |
exc_info: Option<MINIDUMP_EXCEPTION_INFORMATION>, |
| 19 |
|
| 20 |
crashing_process: HANDLE, |
| 21 |
|
| 22 |
pid: u32, |
| 23 |
|
| 24 |
tid: u32, |
| 25 |
|
| 26 |
#[allow(dead_code)] |
| 27 |
exception_code: i32, |
| 28 |
|
| 29 |
is_external_process: bool, |
| 30 |
} |
| 31 |
|
| 32 |
impl MinidumpWriter { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
pub fn dump_local_context( |
| 47 |
exception_code: Option<i32>, |
| 48 |
thread_id: Option<u32>, |
| 49 |
minidump_type: Option<MinidumpType>, |
| 50 |
destination: &mut std::fs::File, |
| 51 |
) -> Result<(), Error> { |
| 52 |
let exception_code = exception_code.unwrap_or(STATUS_NONCONTINUABLE_EXCEPTION); |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
unsafe { |
| 57 |
let mut exception_context = if let Some(tid) = thread_id { |
| 58 |
let mut ec = std::mem::MaybeUninit::uninit(); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
if tid == GetCurrentThreadId() { |
| 63 |
capture_context(ec.as_mut_ptr()); |
| 64 |
} else { |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
let thread_handle = OpenThread( |
| 70 |
THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION | THREAD_SUSPEND_RESUME, |
| 71 |
FALSE, |
| 72 |
tid, |
| 73 |
); |
| 74 |
|
| 75 |
if thread_handle == 0 { |
| 76 |
return Err(Error::ThreadOpen(std::io::Error::last_os_error())); |
| 77 |
} |
| 78 |
|
| 79 |
struct OwnedHandle(HANDLE); |
| 80 |
|
| 81 |
impl Drop for OwnedHandle { |
| 82 |
fn drop(&mut self) { |
| 83 |
|
| 84 |
unsafe { CloseHandle(self.0) }; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
let thread_handle = OwnedHandle(thread_handle); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
if SuspendThread(thread_handle.0) == u32::MAX { |
| 93 |
return Err(Error::ThreadSuspend(std::io::Error::last_os_error())); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
if GetThreadContext(thread_handle.0, ec.as_mut_ptr()) == 0 { |
| 98 |
|
| 99 |
ResumeThread(thread_handle.0); |
| 100 |
|
| 101 |
return Err(Error::ThreadContext(std::io::Error::last_os_error())); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
ResumeThread(thread_handle.0); |
| 108 |
} |
| 109 |
|
| 110 |
ec.assume_init() |
| 111 |
} else { |
| 112 |
let mut ec = std::mem::MaybeUninit::uninit(); |
| 113 |
capture_context(ec.as_mut_ptr()); |
| 114 |
ec.assume_init() |
| 115 |
}; |
| 116 |
|
| 117 |
let mut exception_record: EXCEPTION_RECORD = std::mem::zeroed(); |
| 118 |
|
| 119 |
let exception_ptrs = EXCEPTION_POINTERS { |
| 120 |
ExceptionRecord: &mut exception_record, |
| 121 |
ContextRecord: &mut exception_context, |
| 122 |
}; |
| 123 |
|
| 124 |
exception_record.ExceptionCode = exception_code; |
| 125 |
|
| 126 |
let cc = crash_context::CrashContext { |
| 127 |
exception_pointers: (&exception_ptrs as *const EXCEPTION_POINTERS).cast(), |
| 128 |
process_id: std::process::id(), |
| 129 |
thread_id: thread_id.unwrap_or_else(|| GetCurrentThreadId()), |
| 130 |
exception_code, |
| 131 |
}; |
| 132 |
|
| 133 |
Self::dump_crash_context(cc, minidump_type, destination) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
pub fn dump_crash_context( |
| 152 |
crash_context: crash_context::CrashContext, |
| 153 |
minidump_type: Option<MinidumpType>, |
| 154 |
destination: &mut std::fs::File, |
| 155 |
) -> Result<(), Error> { |
| 156 |
let pid = crash_context.process_id; |
| 157 |
|
| 158 |
|
| 159 |
let (crashing_process, is_external_process) = unsafe { |
| 160 |
if pid != std::process::id() { |
| 161 |
let proc = OpenProcess( |
| 162 |
PROCESS_ALL_ACCESS, |
| 163 |
FALSE, |
| 164 |
pid, |
| 165 |
); |
| 166 |
|
| 167 |
if proc == 0 { |
| 168 |
return Err(std::io::Error::last_os_error().into()); |
| 169 |
} |
| 170 |
|
| 171 |
(proc, true) |
| 172 |
} else { |
| 173 |
(GetCurrentProcess(), false) |
| 174 |
} |
| 175 |
}; |
| 176 |
|
| 177 |
let pid = crash_context.process_id; |
| 178 |
let tid = crash_context.thread_id; |
| 179 |
let exception_code = crash_context.exception_code; |
| 180 |
|
| 181 |
let exc_info = (!crash_context.exception_pointers.is_null()).then_some( |
| 182 |
|
| 183 |
MINIDUMP_EXCEPTION_INFORMATION { |
| 184 |
ThreadId: crash_context.thread_id, |
| 185 |
|
| 186 |
|
| 187 |
ExceptionPointers: crash_context.exception_pointers as *mut _, |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
ClientPointers: i32::from(is_external_process), |
| 196 |
}, |
| 197 |
); |
| 198 |
|
| 199 |
let mdw = Self { |
| 200 |
exc_info, |
| 201 |
crashing_process, |
| 202 |
pid, |
| 203 |
tid, |
| 204 |
exception_code, |
| 205 |
is_external_process, |
| 206 |
}; |
| 207 |
|
| 208 |
mdw.dump(minidump_type, destination) |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
fn dump( |
| 213 |
mut self, |
| 214 |
minidump_type: Option<MinidumpType>, |
| 215 |
destination: &mut std::fs::File, |
| 216 |
) -> Result<(), Error> { |
| 217 |
let exc_info = self.exc_info.take(); |
| 218 |
|
| 219 |
let mut user_streams = Vec::with_capacity(1); |
| 220 |
|
| 221 |
let mut breakpad_info = self.fill_breakpad_stream(); |
| 222 |
|
| 223 |
if let Some(bp_info) = &mut breakpad_info { |
| 224 |
user_streams.push(MINIDUMP_USER_STREAM { |
| 225 |
Type: MINIDUMP_STREAM_TYPE::BreakpadInfoStream as u32, |
| 226 |
BufferSize: bp_info.len() as u32, |
| 227 |
|
| 228 |
Buffer: bp_info.as_mut_ptr().cast(), |
| 229 |
}); |
| 230 |
} |
| 231 |
|
| 232 |
let user_stream_infos = MINIDUMP_USER_STREAM_INFORMATION { |
| 233 |
UserStreamCount: user_streams.len() as u32, |
| 234 |
UserStreamArray: user_streams.as_mut_ptr(), |
| 235 |
}; |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
let ret = unsafe { |
| 241 |
MiniDumpWriteDump( |
| 242 |
self.crashing_process, |
| 243 |
self.pid, |
| 244 |
destination.as_raw_handle() as HANDLE, |
| 245 |
minidump_type.unwrap_or(MinidumpType::Normal), |
| 246 |
exc_info |
| 247 |
.as_ref() |
| 248 |
.map_or(std::ptr::null(), |ei| ei as *const _), |
| 249 |
&user_stream_infos, |
| 250 |
std::ptr::null(), |
| 251 |
) |
| 252 |
}; |
| 253 |
|
| 254 |
if ret == 0 { |
| 255 |
Err(std::io::Error::last_os_error().into()) |
| 256 |
} else { |
| 257 |
Ok(()) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
fn fill_breakpad_stream(&self) -> Option<[u8; 12]> { |
| 270 |
if self.is_external_process { |
| 271 |
return None; |
| 272 |
} |
| 273 |
|
| 274 |
let mut breakpad_info = [0u8; 12]; |
| 275 |
|
| 276 |
let bp_info = MINIDUMP_BREAKPAD_INFO { |
| 277 |
validity: BreakpadInfoValid::DumpThreadId.bits() |
| 278 |
| BreakpadInfoValid::RequestingThreadId.bits(), |
| 279 |
dump_thread_id: self.tid, |
| 280 |
|
| 281 |
requesting_thread_id: unsafe { GetCurrentThreadId() }, |
| 282 |
}; |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
let mut offset = 0; |
| 287 |
breakpad_info.gwrite(bp_info.validity, &mut offset).ok()?; |
| 288 |
breakpad_info |
| 289 |
.gwrite(bp_info.dump_thread_id, &mut offset) |
| 290 |
.ok()?; |
| 291 |
breakpad_info |
| 292 |
.gwrite(bp_info.requesting_thread_id, &mut offset) |
| 293 |
.ok()?; |
| 294 |
|
| 295 |
Some(breakpad_info) |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
impl Drop for MinidumpWriter { |
| 300 |
fn drop(&mut self) { |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
unsafe { CloseHandle(self.crashing_process) }; |
| 308 |
} |
| 309 |
} |