| 1 |
use super::*; |
| 2 |
|
| 3 |
impl MinidumpWriter { |
| 4 |
/// Writes the [`MDStreamType::MemoryListStream`]. The memory blocks that are |
| 5 |
/// written into this stream are the raw thread contexts that were retrieved |
| 6 |
/// and added by [`Self::write_thread_list`] |
| 7 |
pub(crate) fn write_memory_list( |
| 8 |
&mut self, |
| 9 |
buffer: &mut DumpBuf, |
| 10 |
dumper: &TaskDumper, |
| 11 |
) -> Result<MDRawDirectory, WriterError> { |
| 12 |
// Include some memory around the instruction pointer if the crash was |
| 13 |
// due to an exception |
| 14 |
if let Some(cc) = &self.crash_context { |
| 15 |
if cc.exception.is_some() { |
| 16 |
const IP_MEM_SIZE: u64 = 256; |
| 17 |
|
| 18 |
let get_ip_block = |tid| -> Option<std::ops::Range<u64>> { |
| 19 |
let thread_state = dumper.read_thread_state(tid).ok()?; |
| 20 |
|
| 21 |
let ip = thread_state.pc(); |
| 22 |
|
| 23 |
// Bound it to the upper and lower bounds of the region |
| 24 |
// it's contained within. If it's not in a known memory region, |
| 25 |
// don't bother trying to write it. |
| 26 |
let region = dumper.get_vm_region(ip).ok()?; |
| 27 |
|
| 28 |
if ip < region.range.start || ip > region.range.end { |
| 29 |
return None; |
| 30 |
} |
| 31 |
|
| 32 |
// Try to get IP_MEM_SIZE / 2 bytes before and after the IP, but |
| 33 |
// settle for whatever's available. |
| 34 |
let start = std::cmp::max(region.range.start, ip - IP_MEM_SIZE / 2); |
| 35 |
let end = std::cmp::min(ip + IP_MEM_SIZE / 2, region.range.end); |
| 36 |
|
| 37 |
Some(start..end) |
| 38 |
}; |
| 39 |
|
| 40 |
if let Some(ip_range) = get_ip_block(cc.thread) { |
| 41 |
let size = ip_range.end - ip_range.start; |
| 42 |
let stack_buffer = |
| 43 |
dumper.read_task_memory(ip_range.start as _, size as usize)?; |
| 44 |
let ip_location = MDLocationDescriptor { |
| 45 |
data_size: size as u32, |
| 46 |
rva: buffer.position() as u32, |
| 47 |
}; |
| 48 |
buffer.write_all(&stack_buffer); |
| 49 |
|
| 50 |
self.memory_blocks.push(MDMemoryDescriptor { |
| 51 |
start_of_memory_range: ip_range.start, |
| 52 |
memory: ip_location, |
| 53 |
}); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
let list_header = |
| 59 |
MemoryWriter::<u32>::alloc_with_val(buffer, self.memory_blocks.len() as u32)?; |
| 60 |
|
| 61 |
let mut dirent = MDRawDirectory { |
| 62 |
stream_type: MDStreamType::MemoryListStream as u32, |
| 63 |
location: list_header.location(), |
| 64 |
}; |
| 65 |
|
| 66 |
let block_list = |
| 67 |
MemoryArrayWriter::<MDMemoryDescriptor>::alloc_from_array(buffer, &self.memory_blocks)?; |
| 68 |
|
| 69 |
dirent.location.data_size += block_list.location().data_size; |
| 70 |
Ok(dirent) |
| 71 |
} |
| 72 |
} |