File size: 2,751 Bytes
d8435ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, JsonSchema, Debug)]
struct StackTraceSymbol {
    name: Option<String>,
    file: Option<String>,
    line: Option<u32>,
}

#[derive(Deserialize, Serialize, JsonSchema, Debug)]
struct StackTraceFrame {
    symbols: Vec<StackTraceSymbol>,
}

impl StackTraceFrame {
    pub fn render(&self) -> String {
        let mut result = String::new();
        for symbol in &self.symbols {
            let symbol_string = format!(
                "{}:{} - {} ",
                symbol.file.as_deref().unwrap_or_default(),
                symbol.line.unwrap_or_default(),
                symbol.name.as_deref().unwrap_or_default(),
            );
            result.push_str(&symbol_string);
        }
        result
    }
}

#[derive(Deserialize, Serialize, JsonSchema, Debug)]
pub struct ThreadStackTrace {
    id: u32,
    name: String,
    frames: Vec<String>,
}

#[derive(Deserialize, Serialize, JsonSchema, Debug)]
pub struct StackTrace {
    threads: Vec<ThreadStackTrace>,
}

pub fn get_stack_trace() -> StackTrace {
    #[cfg(not(all(target_os = "linux", feature = "stacktrace")))]
    {
        StackTrace { threads: vec![] }
    }

    #[cfg(all(target_os = "linux", feature = "stacktrace"))]
    {
        let exe = std::env::current_exe().unwrap();
        let trace =
            rstack_self::trace(std::process::Command::new(exe).arg("--stacktrace")).unwrap();
        StackTrace {
            threads: trace
                .threads()
                .iter()
                .map(|thread| ThreadStackTrace {
                    id: thread.id(),
                    name: thread.name().to_string(),
                    frames: thread
                        .frames()
                        .iter()
                        .map(|frame| {
                            let frame = StackTraceFrame {
                                symbols: frame
                                    .symbols()
                                    .iter()
                                    .map(|symbol| StackTraceSymbol {
                                        name: symbol.name().map(|name| name.to_string()),
                                        file: symbol.file().map(|file| {
                                            file.to_str().unwrap_or_default().to_string()
                                        }),
                                        line: symbol.line(),
                                    })
                                    .collect(),
                            };
                            frame.render()
                        })
                        .collect(),
                })
                .collect(),
        }
    }
}