| 1 |
use { |
| 2 |
crate::{errors::CpuInfoError, minidump_format::*}, |
| 3 |
failspot::failspot, |
| 4 |
std::{ |
| 5 |
io::{BufRead, BufReader}, |
| 6 |
path, |
| 7 |
}, |
| 8 |
}; |
| 9 |
|
| 10 |
type Result<T> = std::result::Result<T, CpuInfoError>; |
| 11 |
|
| 12 |
struct CpuInfoEntry { |
| 13 |
info_name: &'static str, |
| 14 |
value: i32, |
| 15 |
found: bool, |
| 16 |
} |
| 17 |
|
| 18 |
impl CpuInfoEntry { |
| 19 |
fn new(info_name: &'static str, value: i32, found: bool) -> Self { |
| 20 |
CpuInfoEntry { |
| 21 |
info_name, |
| 22 |
value, |
| 23 |
found, |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
pub fn write_cpu_information(sys_info: &mut MDRawSystemInfo) -> Result<()> { |
| 29 |
let vendor_id_name = "vendor_id"; |
| 30 |
let mut cpu_info_table = [ |
| 31 |
CpuInfoEntry::new("processor", -1, false), |
| 32 |
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] |
| 33 |
CpuInfoEntry::new("model", 0, false), |
| 34 |
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] |
| 35 |
CpuInfoEntry::new("stepping", 0, false), |
| 36 |
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] |
| 37 |
CpuInfoEntry::new("cpu family", 0, false), |
| 38 |
]; |
| 39 |
|
| 40 |
// processor_architecture should always be set, do this first |
| 41 |
sys_info.processor_architecture = if cfg!(target_arch = "mips") { |
| 42 |
MDCPUArchitecture::PROCESSOR_ARCHITECTURE_MIPS |
| 43 |
} else if cfg!(target_arch = "mips64") { |
| 44 |
MDCPUArchitecture::PROCESSOR_ARCHITECTURE_MIPS64 |
| 45 |
} else if cfg!(target_arch = "x86") { |
| 46 |
MDCPUArchitecture::PROCESSOR_ARCHITECTURE_INTEL |
| 47 |
} else { |
| 48 |
MDCPUArchitecture::PROCESSOR_ARCHITECTURE_AMD64 |
| 49 |
} as u16; |
| 50 |
|
| 51 |
failspot!( |
| 52 |
CpuInfoFileOpen |
| 53 |
bail(std::io::Error::other("test requested cpuinfo file failure")) |
| 54 |
); |
| 55 |
|
| 56 |
let cpuinfo_file = std::fs::File::open(path::PathBuf::from("/proc/cpuinfo"))?; |
| 57 |
|
| 58 |
let mut vendor_id = String::new(); |
| 59 |
for line in BufReader::new(cpuinfo_file).lines() { |
| 60 |
let line = line?; |
| 61 |
// Expected format: <field-name> <space>+ ':' <space> <value> |
| 62 |
// Note that: |
| 63 |
// - empty lines happen. |
| 64 |
// - <field-name> can contain spaces. |
| 65 |
// - some fields have an empty <value> |
| 66 |
if line.trim().is_empty() { |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
let mut liter = line.split(':').map(|x| x.trim()); |
| 71 |
let field = liter.next().unwrap(); // guaranteed to have at least one item |
| 72 |
let value = if let Some(val) = liter.next() { |
| 73 |
val |
| 74 |
} else { |
| 75 |
continue; |
| 76 |
}; |
| 77 |
|
| 78 |
let mut is_first_entry = true; |
| 79 |
for entry in cpu_info_table.iter_mut() { |
| 80 |
if !is_first_entry && entry.found { |
| 81 |
// except for the 'processor' field, ignore repeated values. |
| 82 |
continue; |
| 83 |
} |
| 84 |
is_first_entry = false; |
| 85 |
if field == entry.info_name { |
| 86 |
if let Ok(v) = value.parse() { |
| 87 |
entry.value = v; |
| 88 |
entry.found = true; |
| 89 |
} else { |
| 90 |
continue; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// special case for vendor_id |
| 95 |
if field == vendor_id_name && !value.is_empty() { |
| 96 |
vendor_id = value.into(); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
// make sure we got everything we wanted |
| 101 |
if !cpu_info_table.iter().all(|x| x.found) { |
| 102 |
return Err(CpuInfoError::NotAllProcEntriesFound); |
| 103 |
} |
| 104 |
// cpu_info_table[0] holds the last cpu id listed in /proc/cpuinfo, |
| 105 |
// assuming this is the highest id, change it to the number of CPUs |
| 106 |
// by adding one. |
| 107 |
cpu_info_table[0].value += 1; |
| 108 |
|
| 109 |
sys_info.number_of_processors = cpu_info_table[0].value as u8; // TODO: might not work on special machines with LOTS of CPUs |
| 110 |
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))] |
| 111 |
{ |
| 112 |
sys_info.processor_level = cpu_info_table[3].value as u16; |
| 113 |
sys_info.processor_revision = |
| 114 |
(cpu_info_table[1].value << 8 | cpu_info_table[2].value) as u16; |
| 115 |
} |
| 116 |
if !vendor_id.is_empty() { |
| 117 |
let vendor_id = vendor_id.as_bytes(); |
| 118 |
// The vendor_id is the first 12 (3 * size_of::<u32>()) bytes |
| 119 |
let vendor_len = std::cmp::min(3 * std::mem::size_of::<u32>(), vendor_id.len()); |
| 120 |
sys_info.cpu.data[..vendor_len].copy_from_slice(&vendor_id[..vendor_len]); |
| 121 |
} |
| 122 |
|
| 123 |
Ok(()) |
| 124 |
} |