Skip to main content

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