1use sentry_core::protocol::{Frame, Stacktrace};
2
3use crate::utils::function_starts_with;
4
5const WELL_KNOWN_SYS_MODULES: &[&str] = &[
6 "std::",
7 "core::",
8 "alloc::",
9 "backtrace::",
10 "sentry::",
11 "sentry_core::",
12 "sentry_types::",
13 "__rust_",
15 "___rust_",
16 "anyhow::",
18 "log::",
19 "tokio::",
20 "tracing_core::",
21];
22
23const WELL_KNOWN_BORDER_FRAMES: &[&str] = &[
24 "std::panicking::begin_panic",
25 "core::panicking::panic",
26 "anyhow::",
28 "<sentry_log::Logger as log::Log>::log",
29 "tracing_core::",
30];
31
32pub fn trim_stacktrace<F>(stacktrace: &mut Stacktrace, f: F)
34where
35 F: Fn(&Frame, &Stacktrace) -> bool,
36{
37 let known_cutoff = stacktrace
38 .frames
39 .iter()
40 .rev()
41 .position(|frame| match frame.function {
42 Some(ref func) => is_well_known(func) || f(frame, stacktrace),
43 None => false,
44 });
45
46 if let Some(cutoff) = known_cutoff {
47 let trunc = stacktrace.frames.len() - cutoff - 1;
48 stacktrace.frames.truncate(trunc);
49 }
50}
51
52pub fn is_sys_function(func: &str) -> bool {
54 WELL_KNOWN_SYS_MODULES
55 .iter()
56 .any(|m| function_starts_with(func, m))
57}
58
59fn is_well_known(func: &str) -> bool {
61 WELL_KNOWN_BORDER_FRAMES
62 .iter()
63 .any(|m| function_starts_with(func, m))
64}