Debian Codegraph @

third_party/rust/minidump/src/system_info.rs

raw
1 // Copyright 2015 Ted Mielczarek. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3
4 //! Information about the system that produced a `Minidump`.
5
6 use num_traits::FromPrimitive;
7 use std::borrow::Cow;
8 use std::fmt;
9
10 use minidump_common::format as md;
11 use minidump_common::format::PlatformId;
12 use minidump_common::format::ProcessorArchitecture::*;
13
14 /// Known operating systems
15 ///
16 /// This is a slightly nicer layer over the `PlatformId` enum defined in the minidump-common crate.
17 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
18 pub enum Os {
19 Windows,
20 MacOs,
21 Ios,
22 Linux,
23 Solaris,
24 Android,
25 Ps3,
26 NaCl,
27 Unknown(u32),
28 }
29
30 impl Os {
31 /// Get an `Os` value matching the `platform_id` value from `MINIDUMP_SYSTEM_INFO`
32 pub fn from_platform_id(id: u32) -> Os {
33 match PlatformId::from_u32(id) {
34 Some(PlatformId::VER_PLATFORM_WIN32_WINDOWS)
35 | Some(PlatformId::VER_PLATFORM_WIN32_NT) => Os::Windows,
36 Some(PlatformId::MacOs) => Os::MacOs,
37 Some(PlatformId::Ios) => Os::Ios,
38 Some(PlatformId::Linux) => Os::Linux,
39 Some(PlatformId::Solaris) => Os::Solaris,
40 Some(PlatformId::Android) => Os::Android,
41 Some(PlatformId::Ps3) => Os::Ps3,
42 Some(PlatformId::NaCl) => Os::NaCl,
43 _ => Os::Unknown(id),
44 }
45 }
46
47 /// Get a human-readable friendly name for an `Os`
48 pub fn long_name(&self) -> Cow<'_, str> {
49 match *self {
50 Os::Windows => Cow::Borrowed("Windows NT"),
51 Os::MacOs => Cow::Borrowed("Mac OS X"),
52 Os::Ios => Cow::Borrowed("iOS"),
53 Os::Linux => Cow::Borrowed("Linux"),
54 Os::Solaris => Cow::Borrowed("Solaris"),
55 Os::Android => Cow::Borrowed("Android"),
56 Os::Ps3 => Cow::Borrowed("PS3"),
57 Os::NaCl => Cow::Borrowed("NaCl"),
58 Os::Unknown(val) => Cow::Owned(format!("0x{val:#08x}")),
59 }
60 }
61 }
62
63 impl fmt::Display for Os {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 write!(
66 f,
67 "{}",
68 match *self {
69 Os::Windows => "windows",
70 Os::MacOs => "mac",
71 Os::Ios => "ios",
72 Os::Linux => "linux",
73 Os::Solaris => "solaris",
74 Os::Android => "android",
75 Os::Ps3 => "ps3",
76 Os::NaCl => "nacl",
77 Os::Unknown(_) => "unknown",
78 }
79 )
80 }
81 }
82
83 /// Known CPU types
84 ///
85 /// This is a slightly nicer layer over the `ProcessorArchitecture` enum defined in
86 /// the minidump-common crate.
87 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
88 #[non_exhaustive]
89 pub enum Cpu {
90 X86,
91 X86_64,
92 Ppc,
93 Ppc64,
94 Sparc,
95 Arm,
96 Arm64,
97 Mips,
98 Mips64,
99 Unknown(u16),
100 }
101
102 /// Supported CPU pointer widths
103 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
104 pub enum PointerWidth {
105 Bits32,
106 Bits64,
107 Unknown,
108 }
109
110 impl Cpu {
111 /// Get a `Cpu` value matching the `processor_architecture` value from `MINIDUMP_SYSTEM_INFO`
112 pub fn from_processor_architecture(arch: u16) -> Cpu {
113 match md::ProcessorArchitecture::from_u16(arch) {
114 Some(PROCESSOR_ARCHITECTURE_INTEL) | Some(PROCESSOR_ARCHITECTURE_IA32_ON_WIN64) => {
115 Cpu::X86
116 }
117 Some(PROCESSOR_ARCHITECTURE_AMD64) => Cpu::X86_64,
118 Some(PROCESSOR_ARCHITECTURE_PPC) => Cpu::Ppc,
119 Some(PROCESSOR_ARCHITECTURE_PPC64) => Cpu::Ppc64,
120 Some(PROCESSOR_ARCHITECTURE_SPARC) => Cpu::Sparc,
121 Some(PROCESSOR_ARCHITECTURE_ARM) => Cpu::Arm,
122 Some(PROCESSOR_ARCHITECTURE_ARM64) | Some(PROCESSOR_ARCHITECTURE_ARM64_OLD) => {
123 Cpu::Arm64
124 }
125 Some(PROCESSOR_ARCHITECTURE_MIPS) => Cpu::Mips,
126 Some(PROCESSOR_ARCHITECTURE_MIPS64) => Cpu::Mips64,
127 _ => Cpu::Unknown(arch),
128 }
129 }
130
131 /// The native pointer width of this platform
132 pub fn pointer_width(&self) -> PointerWidth {
133 match self {
134 Cpu::X86 | Cpu::Ppc | Cpu::Sparc | Cpu::Arm | Cpu::Mips => PointerWidth::Bits32,
135 Cpu::X86_64 | Cpu::Ppc64 | Cpu::Arm64 | Cpu::Mips64 => PointerWidth::Bits64,
136 Cpu::Unknown(_) => PointerWidth::Unknown,
137 }
138 }
139 }
140
141 impl fmt::Display for Cpu {
142 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143 write!(
144 f,
145 "{}",
146 match *self {
147 Cpu::X86 => "x86",
148 Cpu::X86_64 => "amd64",
149 Cpu::Ppc => "ppc",
150 Cpu::Ppc64 => "ppc64",
151 Cpu::Sparc => "sparc",
152 Cpu::Arm => "arm",
153 Cpu::Arm64 => "arm64",
154 Cpu::Mips => "mips",
155 Cpu::Mips64 => "mips64",
156 Cpu::Unknown(_) => "unknown",
157 }
158 )
159 }
160 }
161
162 impl PointerWidth {
163 pub fn size_in_bytes(self) -> Option<u8> {
164 match self {
165 Self::Bits32 => Some(4),
166 Self::Bits64 => Some(8),
167 Self::Unknown => None,
168 }
169 }
170 }