starnix_core/execution/
crash_reporter.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::signals::SignalInfo;
6use crate::task::CurrentTask;
7use crash_throttling::{CrashThrottler, PendingCrashReport};
8use fidl_fuchsia_feedback::{
9    Annotation, CrashReport, CrashReporterProxy, MAX_ANNOTATION_VALUE_LENGTH,
10    MAX_CRASH_SIGNATURE_LENGTH, NativeCrashReport, SpecificCrashReport,
11};
12use fuchsia_inspect::Node;
13use starnix_logging::{
14    CATEGORY_STARNIX, CoreDumpInfo, CoreDumpList, TraceScope, log_error, log_info, log_warn,
15    trace_instant,
16};
17
18pub struct CrashReporter {
19    /// Diagnostics information about crashed tasks.
20    core_dumps: CoreDumpList,
21
22    /// Throttles crash reports to avoid spamming the system.
23    throttler: CrashThrottler,
24
25    /// Connection to the feedback stack for reporting crashes.
26    proxy: Option<CrashReporterProxy>,
27}
28
29impl CrashReporter {
30    pub fn new(
31        inspect_node: &Node,
32        proxy: Option<CrashReporterProxy>,
33        crash_loop_age_out: zx::MonotonicDuration,
34        enable_throttling: bool,
35    ) -> Self {
36        Self {
37            core_dumps: CoreDumpList::new(inspect_node.create_child("coredumps")),
38            throttler: CrashThrottler::new(inspect_node, crash_loop_age_out, enable_throttling),
39            proxy,
40        }
41    }
42
43    /// Returns a PendingCrashReport if the crash report should be reported. Otherwise, returns
44    /// None.
45    pub fn begin_crash_report(&self, current_task: &CurrentTask) -> Option<PendingCrashReport> {
46        let argv = current_task
47            .read_argv(MAX_ANNOTATION_VALUE_LENGTH as usize)
48            .unwrap_or_else(|_| vec!["<unknown>".into()])
49            .into_iter()
50            .map(|a| a.to_string())
51            .collect::<Vec<_>>();
52        let argv0 = argv.get(0).map(AsRef::as_ref).unwrap_or_else(|| "<unknown>");
53
54        // Get the filename.
55        let argv0 = argv0.rsplit_once("/").unwrap_or(("", &argv0)).1.to_string();
56
57        self.throttler.should_report(argv, argv0, zx::MonotonicInstant::get())
58    }
59
60    /// Callers should first check whether the crash should be reported via begin_crash_report.
61    pub fn handle_core_dump(
62        &self,
63        current_task: &CurrentTask,
64        signal_info: &SignalInfo,
65        pending_crash_report: PendingCrashReport,
66    ) {
67        trace_instant!(CATEGORY_STARNIX, "RecordCoreDump", TraceScope::Process);
68
69        let argv = pending_crash_report.argv;
70        let argv0 = pending_crash_report.argv0;
71        let process_koid = current_task
72            .thread_group()
73            .process
74            .koid()
75            .expect("handles for processes with crashing threads are still valid");
76        let thread_koid = current_task
77            .thread
78            .read()
79            .as_ref()
80            .expect("coredumps occur in tasks with associated threads")
81            .koid()
82            .expect("handles for crashing threads are still valid");
83        let linux_pid = current_task.thread_group().leader as i64;
84        let thread_name = current_task.command().to_string();
85
86        // TODO(https://fxbug.dev/356912301) use boot time
87        let uptime = zx::MonotonicInstant::get() - current_task.thread_group().start_time;
88
89        let dump_info = CoreDumpInfo {
90            process_koid,
91            thread_koid,
92            linux_pid,
93            uptime: uptime.into_nanos(),
94            argv: argv.clone(),
95            thread_name: thread_name.clone(),
96            signal: signal_info.signal.to_string(),
97        };
98        self.core_dumps.record_core_dump(dump_info);
99
100        let mut argv_joined = argv.join(" ");
101        truncate_with_ellipsis(&mut argv_joined, MAX_ANNOTATION_VALUE_LENGTH as usize);
102
103        let mut env_joined = current_task
104            .read_env(MAX_ANNOTATION_VALUE_LENGTH as usize)
105            .unwrap_or_else(|_| vec![])
106            .into_iter()
107            .map(|a| a.to_string())
108            .collect::<Vec<_>>()
109            .join(" ");
110        truncate_with_ellipsis(&mut env_joined, MAX_ANNOTATION_VALUE_LENGTH as usize);
111
112        let signal_str = signal_info.signal.to_string();
113
114        // Truncate program name to fit in crash signature with a space and signal string added.
115        let max_signature_prefix_len = MAX_CRASH_SIGNATURE_LENGTH as usize - (signal_str.len() + 1);
116        let mut crash_signature = argv0.clone();
117        truncate_with_ellipsis(&mut crash_signature, max_signature_prefix_len);
118        crash_signature.push(' ');
119        crash_signature.push_str(&signal_str);
120
121        let crash_report = CrashReport {
122            crash_signature: Some(crash_signature),
123            program_name: Some(argv0.clone()),
124            program_uptime: Some(uptime.into_nanos()),
125            specific_report: Some(SpecificCrashReport::Native(NativeCrashReport {
126                process_koid: Some(process_koid.raw_koid()),
127                process_name: Some(argv0),
128                thread_koid: Some(thread_koid.raw_koid()),
129                thread_name: Some(thread_name),
130                ..Default::default()
131            })),
132            annotations: Some(vec![
133                // Note that this pid will be different from the Zircon process koid that's visible
134                // to the rest of Fuchsia. We want to include both so that this can be correlated
135                // against debugging artifacts produced by Android code.
136                Annotation { key: "linux.pid".to_string(), value: linux_pid.to_string() },
137                Annotation { key: "linux.argv".to_string(), value: argv_joined },
138                Annotation { key: "linux.env".to_string(), value: env_joined },
139                Annotation { key: "linux.signal".to_string(), value: signal_str },
140            ]),
141            is_fatal: Some(true),
142            weight: Some(pending_crash_report.weight),
143            ..Default::default()
144        };
145
146        if let Some(reporter) = &self.proxy {
147            let reporter = reporter.clone();
148            // Do the actual report in the background since they can take a while to file.
149            current_task.kernel().kthreads.spawn_future(
150                move || async move {
151                    match reporter.file_report(crash_report).await {
152                        Ok(Ok(_)) => (),
153                        Ok(Err(filing_error)) => {
154                            log_error!(filing_error:?; "Couldn't file crash report.");
155                        }
156                        Err(fidl_error) => log_warn!(
157                            fidl_error:?;
158                            "Couldn't file crash report due to error on underlying channel."
159                        ),
160                    };
161                },
162                "crash-filing",
163            );
164        } else {
165            log_info!(crash_report:?; "no crash reporter available for crash");
166        }
167    }
168}
169
170fn truncate_with_ellipsis(s: &mut String, max_len: usize) {
171    if s.len() <= max_len {
172        return;
173    }
174
175    // 3 bytes for ellipsis.
176    let max_content_len = max_len - 3;
177
178    // String::truncate panics if the new max length is in the middle of a character, so we need to
179    // find an appropriate byte boundary.
180    let mut new_len = 0;
181    let mut iter = s.char_indices();
182    while let Some((offset, _)) = iter.next() {
183        if offset > max_content_len {
184            break;
185        }
186        new_len = offset;
187    }
188
189    s.truncate(new_len);
190    s.push_str("...");
191}
192
193#[cfg(test)]
194mod tests {
195    use super::*;
196
197    #[test]
198    fn truncate_noop_on_max_length_string() {
199        let mut s = String::from("1234567890");
200        let before = s.clone();
201        truncate_with_ellipsis(&mut s, 10);
202        assert_eq!(s, before);
203    }
204
205    #[test]
206    fn truncate_adds_ellipsis() {
207        let mut s = String::from("1234567890");
208        truncate_with_ellipsis(&mut s, 9);
209        assert_eq!(s.len(), 9);
210        assert_eq!(s, "123456...", "truncate must add ellipsis and still fit under max len");
211    }
212
213    #[test]
214    fn truncate_is_sensible_in_middle_of_multibyte_chars() {
215        let mut s = String::from("æææææææææ");
216        // æ is 2 bytes, so any odd byte length should be in the middle of a character. Truncate
217        // adds 3 bytes for the ellipsis so we actually need an even max length to hit the middle
218        // of a character.
219        truncate_with_ellipsis(&mut s, 8);
220        assert_eq!(s.len(), 7, "may end up shorter than provided max length w/ multi-byte chars");
221        assert_eq!(s, "ææ...", "truncate must remove whole characters and add ellipsis");
222    }
223}