starnix_logging/
core_dump_list.rs

1// Copyright 2023 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::log_debug;
6use fuchsia_inspect::Node;
7use fuchsia_inspect_contrib::nodes::BoundedListNode;
8
9use starnix_sync::Mutex;
10
11/// The maximum number of failed tasks to record.
12///
13/// This number is arbitrary and we may want to make it configurable in the future.
14const MAX_NUM_COREDUMPS: usize = 64;
15
16/// The maximum length of an argv string to record.
17///
18/// This number is arbitrary and we may wand to make it configurable in the future.
19const MAX_ARGV_LENGTH: usize = 128;
20
21/// A list of recently coredumped tasks in Inspect.
22pub struct CoreDumpList {
23    list: Mutex<BoundedListNode>,
24}
25
26#[derive(Debug)]
27pub struct CoreDumpInfo {
28    pub process_koid: zx::Koid,
29    pub thread_koid: zx::Koid,
30    pub linux_pid: i64,
31    pub argv: Vec<String>,
32    pub uptime: i64,
33    pub thread_name: String,
34    pub signal: String,
35}
36
37impl CoreDumpList {
38    pub fn new(node: Node) -> Self {
39        Self { list: Mutex::new(BoundedListNode::new(node, MAX_NUM_COREDUMPS)) }
40    }
41
42    pub fn record_core_dump(&self, core_dump_info: CoreDumpInfo) {
43        let mut argv = core_dump_info.argv.join(" ");
44        let original_len = argv.len();
45        argv.truncate(MAX_ARGV_LENGTH - 3);
46        if argv.len() < original_len {
47            argv.push_str("...");
48        }
49        let mut list = self.list.lock();
50        list.add_entry(|crash_node| {
51            log_debug!(linux_pid = core_dump_info.linux_pid, argv:%; "Recording task with a coredump.");
52            crash_node.record_uint("thread_koid", core_dump_info.thread_koid.raw_koid());
53            crash_node.record_uint("process_koid", core_dump_info.process_koid.raw_koid());
54            crash_node.record_int("pid", core_dump_info.linux_pid);
55            crash_node.record_int("uptime", core_dump_info.uptime);
56            crash_node.record_string("argv", argv);
57            crash_node.record_string("thread_name", &core_dump_info.thread_name);
58            crash_node.record_string("signal", &core_dump_info.signal);
59        });
60    }
61}