sentry_backtrace/
trim.rs

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    // these are not modules but things like __rust_maybe_catch_panic
14    "__rust_",
15    "___rust_",
16    // these are well-known library frames
17    "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    // well-known library frames
27    "anyhow::",
28    "<sentry_log::Logger as log::Log>::log",
29    "tracing_core::",
30];
31
32/// A helper function to trim a stacktrace.
33pub 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
52/// Checks if a function is considered to be not in-app
53pub fn is_sys_function(func: &str) -> bool {
54    WELL_KNOWN_SYS_MODULES
55        .iter()
56        .any(|m| function_starts_with(func, m))
57}
58
59/// Checks if a function is a well-known system function
60fn is_well_known(func: &str) -> bool {
61    WELL_KNOWN_BORDER_FRAMES
62        .iter()
63        .any(|m| function_starts_with(func, m))
64}