summaryrefslogtreecommitdiffstats
path: root/third_party/rust/minidump-writer/src/linux/dumper_cpu_info.rs
blob: 72da20fe3003fbf8e6a61c476a6aa1066cf2a10a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
cfg_if::cfg_if! {
    if #[cfg(any(
        target_arch = "x86_64",
        target_arch = "x86",
        target_arch = "mips",
        target_arch = "mips64"
    ))]
    {
        pub mod x86_mips;
        pub use x86_mips as imp;
    } else if #[cfg(any(
        target_arch = "arm",
        target_arch = "aarch64",
    ))]
    {
        pub mod arm;
        pub use arm as imp;
    }
}

pub use imp::write_cpu_information;

use crate::minidump_format::PlatformId;
use nix::sys::utsname::uname;

/// Retrieves the [`MDOSPlatform`] and synthesized version information
pub fn os_information() -> (PlatformId, String) {
    let platform_id = if cfg!(target_os = "android") {
        PlatformId::Android
    } else {
        PlatformId::Linux
    };

    // This is quite unfortunate, but the primary reason that uname could fail
    // would be if it failed to fill out the nodename (hostname) field, even
    // though we don't care about that particular field at all
    let info = uname().map_or_else(
        |_e| {
            let os = if platform_id == PlatformId::Linux {
                "Linux"
            } else {
                "Android"
            };

            let machine = if cfg!(target_arch = "x86_64") {
                "x86_64"
            } else if cfg!(target_arch = "x86") {
                "x86"
            } else if cfg!(target_arch = "aarch64") {
                "aarch64"
            } else if cfg!(target_arch = "arm") {
                "arm"
            } else {
                "<unknown>"
            };

            // TODO: Fallback to other sources of information, eg /etc/os-release
            format!("{os} <unknown> <unknown> {machine}")
        },
        |info| {
            format!(
                "{} {} {} {}",
                info.sysname().to_str().unwrap_or("<unknown>"),
                info.release().to_str().unwrap_or("<unknown>"),
                info.version().to_str().unwrap_or("<unknown>"),
                info.machine().to_str().unwrap_or("<unknown>"),
            )
        },
    );

    (platform_id, info)
}