| 1 |
use super::*; |
| 2 |
use format::{MiscInfoFlags, MINIDUMP_MISC_INFO_2 as MDRawMiscInfo}; |
| 3 |
use std::time::Duration; |
| 4 |
|
| 5 |
/// From <usr/include/mach/time_value.h> |
| 6 |
#[repr(C)] |
| 7 |
#[derive(Copy, Clone)] |
| 8 |
struct TimeValue { |
| 9 |
seconds: i32, |
| 10 |
microseconds: i32, |
| 11 |
} |
| 12 |
|
| 13 |
impl From<TimeValue> for Duration { |
| 14 |
fn from(tv: TimeValue) -> Self { |
| 15 |
let mut seconds = tv.seconds as u64; |
| 16 |
let mut microseconds = tv.microseconds as u32; |
| 17 |
// This _probably_ will never happen, but this will avoid a panic in |
| 18 |
// Duration::new() if it does |
| 19 |
if tv.microseconds >= 1000000 { |
| 20 |
seconds += 1; |
| 21 |
microseconds -= 1000000; |
| 22 |
} |
| 23 |
|
| 24 |
Duration::new(seconds, microseconds * 1000) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
/// From <usr/include/mach/task_info.h>, this includes basic information about |
| 29 |
/// a task. |
| 30 |
#[repr(C, packed(4))] |
| 31 |
struct MachTaskBasicInfo { |
| 32 |
/// Virtual memory size in bytes |
| 33 |
virtual_size: u64, |
| 34 |
/// Resident memory size in bytes |
| 35 |
resident_size: u64, |
| 36 |
/// Maximum resident memory size in bytes |
| 37 |
resident_size_max: u64, |
| 38 |
/// Total user run time for terminated threads |
| 39 |
user_time: TimeValue, |
| 40 |
/// Total system run time for terminated threads |
| 41 |
system_time: TimeValue, |
| 42 |
/// Default policy for new threads |
| 43 |
policy: i32, |
| 44 |
/// Suspend count for task |
| 45 |
suspend_count: i32, |
| 46 |
} |
| 47 |
|
| 48 |
impl mach::TaskInfo for MachTaskBasicInfo { |
| 49 |
const FLAVOR: u32 = mach::task_info::MACH_TASK_BASIC_INFO; |
| 50 |
} |
| 51 |
|
| 52 |
/// From <usr/include/mach/task_info.h>, this includes times for currently |
| 53 |
/// live threads in the task. |
| 54 |
#[repr(C, packed(4))] |
| 55 |
struct TaskThreadsTimeInfo { |
| 56 |
/// Total user run time for live threads |
| 57 |
user_time: TimeValue, |
| 58 |
/// total system run time for live threads |
| 59 |
system_time: TimeValue, |
| 60 |
} |
| 61 |
|
| 62 |
impl mach::TaskInfo for TaskThreadsTimeInfo { |
| 63 |
const FLAVOR: u32 = mach::task_info::TASK_THREAD_TIMES_INFO; |
| 64 |
} |
| 65 |
|
| 66 |
impl MinidumpWriter { |
| 67 |
/// Writes the [`MDStreamType::MiscInfoStream`] stream. |
| 68 |
/// |
| 69 |
/// On MacOS, we write a [`minidump_common::format::MINIDUMP_MISC_INFO_2`] |
| 70 |
/// to this stream, which includes the start time of the process at second |
| 71 |
/// granularity, and the (approximate) amount of time spent in user and |
| 72 |
/// system (kernel) time for the lifetime of the task. We attempt to also |
| 73 |
/// retrieve power ie CPU usage statistics, though this information is only |
| 74 |
/// currently available on x86_64, not aarch64 at the moment. |
| 75 |
pub(crate) fn write_misc_info( |
| 76 |
&mut self, |
| 77 |
buffer: &mut DumpBuf, |
| 78 |
dumper: &TaskDumper, |
| 79 |
) -> Result<MDRawDirectory, WriterError> { |
| 80 |
let mut info_section = MemoryWriter::<MDRawMiscInfo>::alloc(buffer)?; |
| 81 |
let dirent = MDRawDirectory { |
| 82 |
stream_type: MDStreamType::MiscInfoStream as u32, |
| 83 |
location: info_section.location(), |
| 84 |
}; |
| 85 |
|
| 86 |
let pid = dumper.pid_for_task()?; |
| 87 |
|
| 88 |
let mut misc_info = MDRawMiscInfo { |
| 89 |
size_of_info: std::mem::size_of::<MDRawMiscInfo>() as u32, |
| 90 |
flags1: MiscInfoFlags::MINIDUMP_MISC1_PROCESS_ID.bits() |
| 91 |
| MiscInfoFlags::MINIDUMP_MISC1_PROCESS_TIMES.bits() |
| 92 |
| MiscInfoFlags::MINIDUMP_MISC1_PROCESSOR_POWER_INFO.bits(), |
| 93 |
process_id: pid as u32, |
| 94 |
process_create_time: 0, |
| 95 |
process_user_time: 0, |
| 96 |
process_kernel_time: 0, |
| 97 |
processor_max_mhz: 0, |
| 98 |
processor_current_mhz: 0, |
| 99 |
processor_mhz_limit: 0, |
| 100 |
processor_max_idle_state: 0, |
| 101 |
processor_current_idle_state: 0, |
| 102 |
}; |
| 103 |
|
| 104 |
// Note that both Breakpad and Crashpad use `sysctl CTL_KERN, KERN_PROC, KERN_PROC_PID` |
| 105 |
// to retrieve the process start time, but none of the structures that |
| 106 |
// are filled in by that call are in libc at the moment, and `proc_pidinfo` |
| 107 |
// seems to work just fine, so using that instead. |
| 108 |
// |
| 109 |
// SAFETY: syscall |
| 110 |
misc_info.process_create_time = unsafe { |
| 111 |
// Breakpad was using an old method to retrieve this, let's try the |
| 112 |
// BSD method instead which is already implemented in libc |
| 113 |
let mut proc_info = std::mem::MaybeUninit::<libc::proc_bsdinfo>::uninit(); |
| 114 |
let size = std::mem::size_of::<libc::proc_bsdinfo>() as i32; |
| 115 |
if libc::proc_pidinfo( |
| 116 |
pid, |
| 117 |
libc::PROC_PIDTBSDINFO, |
| 118 |
0, |
| 119 |
proc_info.as_mut_ptr().cast(), |
| 120 |
size, |
| 121 |
) == size |
| 122 |
{ |
| 123 |
let proc_info = proc_info.assume_init(); |
| 124 |
|
| 125 |
proc_info.pbi_start_tvsec as u32 |
| 126 |
} else { |
| 127 |
0 |
| 128 |
} |
| 129 |
}; |
| 130 |
|
| 131 |
// Note that Breakpad is using `getrusage` to retrieve this information, |
| 132 |
// however that is wrong, as it can only retrieve the process usage information |
| 133 |
// for the current or children processes, not an external process, so |
| 134 |
// we use the Crashpad method, which is itself based off of the XNU |
| 135 |
// method of retrieving the process times |
| 136 |
// https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/kern/kern_resource.c#L1215 |
| 137 |
|
| 138 |
// The basic task info keeps the timings for all of the terminated threads |
| 139 |
let basic_info = dumper.task_info::<MachTaskBasicInfo>().ok(); |
| 140 |
|
| 141 |
// THe thread times info keeps the timings for all of the living threads |
| 142 |
let thread_times_info = dumper.task_info::<TaskThreadsTimeInfo>().ok(); |
| 143 |
|
| 144 |
let user_time = basic_info |
| 145 |
.as_ref() |
| 146 |
.map(|bi| Duration::from(bi.user_time)) |
| 147 |
.unwrap_or_default() |
| 148 |
+ thread_times_info |
| 149 |
.as_ref() |
| 150 |
.map(|tt| Duration::from(tt.user_time)) |
| 151 |
.unwrap_or_default(); |
| 152 |
let system_time = basic_info |
| 153 |
.as_ref() |
| 154 |
.map(|bi| Duration::from(bi.system_time)) |
| 155 |
.unwrap_or_default() |
| 156 |
+ thread_times_info |
| 157 |
.as_ref() |
| 158 |
.map(|tt| Duration::from(tt.system_time)) |
| 159 |
.unwrap_or_default(); |
| 160 |
|
| 161 |
misc_info.process_user_time = user_time.as_secs() as u32; |
| 162 |
misc_info.process_kernel_time = system_time.as_secs() as u32; |
| 163 |
|
| 164 |
// Note that neither of these two keys are present on aarch64, at least atm |
| 165 |
let max: u64 = mach::sysctl_by_name(b"hw.cpufrequency_max\0"); |
| 166 |
let freq: u64 = mach::sysctl_by_name(b"hw.cpufrequency\0"); |
| 167 |
|
| 168 |
let max = (max / 1000 * 1000) as u32; |
| 169 |
let current = (freq / 1000 * 1000) as u32; |
| 170 |
|
| 171 |
misc_info.processor_max_mhz = max; |
| 172 |
misc_info.processor_mhz_limit = max; |
| 173 |
misc_info.processor_current_mhz = current; |
| 174 |
|
| 175 |
info_section.set_value(buffer, misc_info)?; |
| 176 |
|
| 177 |
Ok(dirent) |
| 178 |
} |
| 179 |
} |