minidumper_child/
lib.rs

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use std::{
    path::{Path, PathBuf},
    process,
    sync::Arc,
};

pub mod client;
pub mod server;

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error(transparent)]
    IO(#[from] std::io::Error),
    #[error(transparent)]
    CrashHandler(#[from] crash_handler::Error),
    #[error(transparent)]
    Minidumper(#[from] minidumper::Error),
}

pub struct ClientHandle {
    client: Arc<minidumper::Client>,
    _handler: crash_handler::CrashHandler,
    _child: process::Child,
}

impl ClientHandle {
    pub fn send_message(&self, kind: u32, buf: impl AsRef<[u8]>) -> Result<(), Error> {
        self.client.send_message(kind, buf).map_err(Error::from)
    }
}

pub type OnMinidump = Box<dyn Fn(Vec<u8>, &Path) + Send + Sync + 'static>;
pub type OnMessage = Box<dyn Fn(u32, Vec<u8>) + Send + Sync + 'static>;

pub struct MinidumperChild {
    crashes_dir: PathBuf,
    server_stale_timeout: u64,
    client_connect_timeout: u64,
    server_arg: String,
    on_minidump: Option<OnMinidump>,
    on_message: Option<OnMessage>,
}

impl Default for MinidumperChild {
    fn default() -> Self {
        Self {
            crashes_dir: std::env::temp_dir().join("Crashes"),
            server_stale_timeout: 5000,
            client_connect_timeout: 3000,
            server_arg: "--crash-reporter-server".to_string(),
            on_minidump: None,
            on_message: None,
        }
    }
}

impl MinidumperChild {
    #[must_use = "You should call spawn() or the crash reporter won't be enabled"]
    pub fn new() -> Self {
        Self::default()
    }

    pub fn is_crash_reporter_process(&self) -> bool {
        std::env::args().any(|arg| arg.starts_with(&self.server_arg))
    }

    #[must_use = "You should call spawn() or the crash reporter won't be enabled"]
    pub fn on_minidump<F>(mut self, on_minidump: F) -> Self
    where
        F: Fn(Vec<u8>, &Path) + Send + Sync + 'static,
    {
        self.on_minidump = Some(Box::new(on_minidump));
        self
    }

    #[must_use = "You should call spawn() or the crash reporter won't be enabled"]
    pub fn on_message<F>(mut self, on_message: F) -> Self
    where
        F: Fn(u32, Vec<u8>) + Send + Sync + 'static,
    {
        self.on_message = Some(Box::new(on_message));
        self
    }

    #[must_use = "You should call spawn() or the crash reporter won't be enabled"]
    pub fn with_crashes_dir(mut self, crashes_dir: PathBuf) -> Self {
        self.crashes_dir = crashes_dir;
        self
    }

    #[must_use = "You should call spawn() or the crash reporter won't be enabled"]
    pub fn with_server_stale_timeout(mut self, server_stale_timeout: u64) -> Self {
        self.server_stale_timeout = server_stale_timeout;
        self
    }

    #[must_use = "You should call spawn() or the crash reporter won't be enabled"]
    pub fn with_client_connect_timeout(mut self, client_connect_timeout: u64) -> Self {
        self.client_connect_timeout = client_connect_timeout;
        self
    }

    #[must_use = "You should call spawn() or the crash reporter won't be enabled"]
    pub fn with_server_arg(mut self, server_arg: String) -> Self {
        self.server_arg = server_arg;
        self
    }

    #[must_use = "The return value of spawn() should not be dropped until the program exits"]
    pub fn spawn(self) -> Result<ClientHandle, Error> {
        if self.on_minidump.is_none() && self.on_message.is_none() {
            panic!("You should set one of 'on_minidump' or 'on_message'");
        }

        let server_socket = std::env::args()
            .find(|arg| arg.starts_with(&self.server_arg))
            .and_then(|arg| arg.split('=').last().map(|arg| arg.to_string()));

        if let Some(socket_name) = server_socket {
            server::start(
                &socket_name,
                self.crashes_dir,
                self.server_stale_timeout,
                self.on_minidump,
                self.on_message,
            )?;

            // We force exit so that the app code after here does not run in the
            // crash reporter process.
            std::process::exit(0);
        } else {
            // We use a unique socket name because we don't share the crash reporter
            // processes between different instances of the app.
            let socket_name = make_socket_name(uuid::Uuid::new_v4());

            std::env::current_exe()
                .and_then(|current_exe| {
                    process::Command::new(current_exe)
                        .arg(format!("{}={}", &self.server_arg, socket_name))
                        .spawn()
                })
                .map_err(Error::from)
                .and_then(|server_process| {
                    client::start(
                        &socket_name,
                        self.client_connect_timeout,
                        server_process.id(),
                        self.server_stale_timeout / 2,
                    )
                    .map(|(client, handler)| ClientHandle {
                        client,
                        _handler: handler,
                        _child: server_process,
                    })
                })
        }
    }
}

pub fn make_socket_name(session_id: uuid::Uuid) -> String {
    if cfg!(any(target_os = "linux", target_os = "android")) {
        format!("temp-socket-{}", session_id.simple())
    } else {
        // For platforms without abstract uds, put the pipe in the
        // temporary directory so that the OS can clean it up, rather than
        // polluting the cwd due to annoying file deletion problems,
        // particularly on Windows
        let mut td = std::env::temp_dir();
        td.push(format!("temp-socket-{}", session_id.simple()));
        td.to_string_lossy().to_string()
    }
}