| 1 |
use super::*; |
| 2 |
|
| 3 |
impl MinidumpWriter { |
| 4 |
/// Writes the [`MDStreamType::ThreadNamesStream`] which is an array of |
| 5 |
/// [`miniduimp_common::format::MINIDUMP_THREAD`] |
| 6 |
pub(crate) fn write_thread_names( |
| 7 |
&mut self, |
| 8 |
buffer: &mut DumpBuf, |
| 9 |
dumper: &TaskDumper, |
| 10 |
) -> Result<MDRawDirectory, WriterError> { |
| 11 |
let threads = self.threads(dumper); |
| 12 |
|
| 13 |
let list_header = MemoryWriter::<u32>::alloc_with_val(buffer, threads.len() as u32)?; |
| 14 |
|
| 15 |
let mut dirent = MDRawDirectory { |
| 16 |
stream_type: MDStreamType::ThreadNamesStream as u32, |
| 17 |
location: list_header.location(), |
| 18 |
}; |
| 19 |
|
| 20 |
let mut names = MemoryArrayWriter::<MDRawThreadName>::alloc_array(buffer, threads.len())?; |
| 21 |
dirent.location.data_size += names.location().data_size; |
| 22 |
|
| 23 |
for (i, tid) in threads.enumerate() { |
| 24 |
// It's unfortunate if we can't grab a thread name, but it's also |
| 25 |
// not a critical failure |
| 26 |
let name_loc = match Self::write_thread_name(buffer, dumper, tid) { |
| 27 |
Ok(loc) => loc, |
| 28 |
Err(err) => { |
| 29 |
log::warn!("failed to write thread name for thread {tid}: {err}"); |
| 30 |
write_string_to_location(buffer, "")? |
| 31 |
} |
| 32 |
}; |
| 33 |
|
| 34 |
let thread = MDRawThreadName { |
| 35 |
thread_id: tid, |
| 36 |
thread_name_rva: name_loc.rva.into(), |
| 37 |
}; |
| 38 |
|
| 39 |
names.set_value_at(buffer, thread, i)?; |
| 40 |
} |
| 41 |
|
| 42 |
Ok(dirent) |
| 43 |
} |
| 44 |
|
| 45 |
/// Attempts to retrieve and write the threadname, returning the threa names |
| 46 |
/// location if successful |
| 47 |
fn write_thread_name( |
| 48 |
buffer: &mut Buffer, |
| 49 |
dumper: &TaskDumper, |
| 50 |
tid: u32, |
| 51 |
) -> Result<MDLocationDescriptor, WriterError> { |
| 52 |
// As noted in usr/include/mach/thread_info.h, the THREAD_EXTENDED_INFO |
| 53 |
// return is exactly the same as proc_pidinfo(..., proc_threadinfo) |
| 54 |
impl mach::ThreadInfo for libc::proc_threadinfo { |
| 55 |
const FLAVOR: u32 = 5; // THREAD_EXTENDED_INFO |
| 56 |
} |
| 57 |
|
| 58 |
let thread_info: libc::proc_threadinfo = dumper.thread_info(tid)?; |
| 59 |
|
| 60 |
let name = std::str::from_utf8( |
| 61 |
// SAFETY: This is an initialized block of static size |
| 62 |
unsafe { |
| 63 |
std::slice::from_raw_parts( |
| 64 |
thread_info.pth_name.as_ptr().cast(), |
| 65 |
thread_info.pth_name.len(), |
| 66 |
) |
| 67 |
}, |
| 68 |
) |
| 69 |
.unwrap_or_default(); |
| 70 |
|
| 71 |
// Ignore the null terminator |
| 72 |
let tname = match name.find('\0') { |
| 73 |
Some(i) => &name[..i], |
| 74 |
None => name, |
| 75 |
}; |
| 76 |
|
| 77 |
Ok(write_string_to_location(buffer, tname)?) |
| 78 |
} |
| 79 |
} |