bhm: Add backtrace demangling (#37819)

This PR adds backtrace demangling to background hang monitor, thereby
removing a `TODO` in the component source.
The required `rustc-demangle` crate dependency is gated behind
`#[cfg(feature = "sampler")]` as it is my understanding that the
surrounding code is only called when `sampler` is enabled.

Testing: testing was done manually - I suppose a substring match for a
known demangled string could be used for adding a proper test case, and
checking `RUSTFLAGS` to discern whether `v0` demangling should be
expected? Open to suggestions. Manual testing was done by printf
debugging (not part of the PR):

```diff
diff --git a/components/background_hang_monitor/tests/hang_monitor_tests.rs b/components/background_hang_monitor/tests/hang_monitor_tests.rs
index a35ef7bfd..1ebd7d001 100644
--- a/components/background_hang_monitor/tests/hang_monitor_tests.rs
+++ b/components/background_hang_monitor/tests/hang_monitor_tests.rs
@@ -61,6 +61,7 @@ fn test_hang_monitoring() {
     // Check for a permanent hang alert.
     match background_hang_monitor_receiver.recv().unwrap() {
         HangMonitorAlert::Hang(HangAlert::Permanent(component_id, _annotation, _profile)) => {
+            println!("{:?}", _profile);
             let expected = MonitoredComponentId(TEST_PIPELINE_ID, MonitoredComponentType::Script);
             assert_eq!(expected, component_id);
         },
```

here is the output of running the test cases on my Linux (pop!_OS 2024)
system with both legacy mangling (the default) and "v0":


```

❯ cargo test --features background_hang_monitor/sampler --package background_hang_monitor  -- --nocapture                                         
   Compiling background_hang_monitor v0.0.1 (/tmp/aec.25_06_30_20_04_42.P8A/servo/components/background_hang_monitor)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 1.07s
     Running tests/hang_monitor_tests.rs (/tmp/cargo/debug/deps/hang_monitor_tests-2ef5768739c27dd6)

running 6 tests
Some(HangProfile backtrace:
                         - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h2649f14f1b1c150d
                        at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/alloc/src/boxed.rs:1993
                         - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hb2ade58cb34e1bc4
                        at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/alloc/src/boxed.rs:1993
                         - std::sys::pal::unix:🧵:Thread:🆕:thread_start::ha07e360225fa3528
                        at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/sys/pal/unix/thread.rs:106
                         - core::ops::function::FnOnce::call_once{{vtable.shim}}::hfed2d99b0f690670
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250
                         - std::panicking::try::h72c6a91b66ed940c
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:547
                         - std::panic::catch_unwind::h977d45286c990a5c
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:358
                         - std:🧵:Builder::spawn_unchecked_::{{closure}}::h3e66aedadec0e66b
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/mod.rs:562
                         - __rust_try
                         - std::panicking::try::do_call::h638036fc728c36b1
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:584
                         - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::hff781dc648b37712
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:272
                         - std:🧵:Builder::spawn_unchecked_::{{closure}}::{{closure}}::h676d8813fe1265ec
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/mod.rs:564
                         - std::sys::backtrace::__rust_begin_short_backtrace::h3225fcf5f71669b6
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/backtrace.rs:152
                         - background_hang_monitor::background_hang_monitor::HangMonitorRegister::init::{{closure}}::hfdbd05eb9a4d2c1f
                        at /tmp/aec.25_06_30_20_04_42.P8A/servo/components/background_hang_monitor/background_hang_monitor.rs:61
                         - background_hang_monitor::background_hang_monitor::BackgroundHangMonitorWorker::run::h33cc7072a6114a19
                        at /tmp/aec.25_06_30_20_04_42.P8A/servo/components/background_hang_monitor/background_hang_monitor.rs:480
                         - background_hang_monitor::background_hang_monitor::BackgroundHangMonitorWorker::perform_a_hang_monitor_checkpoint::hda50a6aa6d103807
                        at /tmp/aec.25_06_30_20_04_42.P8A/servo/components/background_hang_monitor/background_hang_monitor.rs:562
                         - <background_hang_monitor::sampler_linux::LinuxSampler as background_hang_monitor::sampler::Sampler>::suspend_and_sample_thread::h0bd3d9753717c480
                        at /tmp/aec.25_06_30_20_04_42.P8A/servo/components/background_hang_monitor/sampler_linux.rs:172
                         - backtrace::backtrace::libunwind::trace::h4b1b880a458591ce
                        at /home/ace/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/backtrace-0.3.75/src/backtrace/libunwind.rs:117
                         - backtrace::backtrace::trace_unsynchronized::hfdd102bb42227ed3
                        at /home/ace/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/backtrace-0.3.75/src/backtrace/mod.rs:66)
test test_hang_monitoring ... ok
test test_hang_monitoring_exit_signal3 ... ok
test test_hang_monitoring_unregister ... ok
test test_hang_monitoring_exit_signal1 ... ok
test test_hang_monitoring_exit_signal4 ... ok
test test_hang_monitoring_exit_signal2 ... ok

test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.14s
```

```
❯ RUSTFLAGS="-Csymbol-mangling-version=v0" cargo test --features background_hang_monitor/sampler --package background_hang_monitor  -- --nocapture
   Compiling background_hang_monitor v0.0.1 (/tmp/aec.25_06_30_20_04_42.P8A/servo/components/background_hang_monitor)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 1.64s
     Running tests/hang_monitor_tests.rs (/tmp/cargo/debug/deps/hang_monitor_tests-9a8a75f69169c1ff)

running 6 tests
Some(HangProfile backtrace:
                         - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h2649f14f1b1c150d
                        at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/alloc/src/boxed.rs:1993
                         - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hb2ade58cb34e1bc4
                        at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/alloc/src/boxed.rs:1993
                         - std::sys::pal::unix:🧵:Thread:🆕:thread_start::ha07e360225fa3528
                        at /rustc/4d91de4e48198da2e33413efdcd9cd2cc0c46688/library/std/src/sys/pal/unix/thread.rs:106
                         - <<std[cba35227022cdb8c]:🧵:Builder>::spawn_unchecked_<<background_hang_monitor[5f27e502ead78af0]::background_hang_monitor::HangMonitorRegister>::init::{closure#0}, ()>::{closure#1} as core[3b140760090fa97d]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250
                         - std[cba35227022cdb8c]::panicking::try::<(), core[3b140760090fa97d]::panic::unwind_safe::AssertUnwindSafe<<std[cba35227022cdb8c]:🧵:Builder>::spawn_unchecked_<<background_hang_monitor[5f27e502ead78af0]::background_hang_monitor::HangMonitorRegister>::init::{closure#0}, ()>::{closure#1}::{closure#0}>>
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:547
                         - std[cba35227022cdb8c]::panic::catch_unwind::<core[3b140760090fa97d]::panic::unwind_safe::AssertUnwindSafe<<std[cba35227022cdb8c]:🧵:Builder>::spawn_unchecked_<<background_hang_monitor[5f27e502ead78af0]::background_hang_monitor::HangMonitorRegister>::init::{closure#0}, ()>::{closure#1}::{closure#0}>, ()>
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panic.rs:358
                         - <std[cba35227022cdb8c]:🧵:Builder>::spawn_unchecked_::<<background_hang_monitor[5f27e502ead78af0]::background_hang_monitor::HangMonitorRegister>::init::{closure#0}, ()>::{closure#1}
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/mod.rs:562
                         - __rust_try
                         - std[cba35227022cdb8c]::panicking::try::do_call::<core[3b140760090fa97d]::panic::unwind_safe::AssertUnwindSafe<<std[cba35227022cdb8c]:🧵:Builder>::spawn_unchecked_<<background_hang_monitor[5f27e502ead78af0]::background_hang_monitor::HangMonitorRegister>::init::{closure#0}, ()>::{closure#1}::{closure#0}>, ()>
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/panicking.rs:584
                         - <core[3b140760090fa97d]::panic::unwind_safe::AssertUnwindSafe<<std[cba35227022cdb8c]:🧵:Builder>::spawn_unchecked_<<background_hang_monitor[5f27e502ead78af0]::background_hang_monitor::HangMonitorRegister>::init::{closure#0}, ()>::{closure#1}::{closure#0}> as core[3b140760090fa97d]::ops::function::FnOnce<()>>::call_once
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/panic/unwind_safe.rs:272
                         - <std[cba35227022cdb8c]:🧵:Builder>::spawn_unchecked_::<<background_hang_monitor[5f27e502ead78af0]::background_hang_monitor::HangMonitorRegister>::init::{closure#0}, ()>::{closure#1}::{closure#0}
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/mod.rs:564
                         - std[cba35227022cdb8c]::sys::backtrace::__rust_begin_short_backtrace::<<background_hang_monitor[5f27e502ead78af0]::background_hang_monitor::HangMonitorRegister>::init::{closure#0}, ()>
                        at /home/ace/.rustup/toolchains/1.85.0-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/backtrace.rs:152
                         - <background_hang_monitor[5f27e502ead78af0]::background_hang_monitor::HangMonitorRegister>::init::{closure#0}
                        at /tmp/aec.25_06_30_20_04_42.P8A/servo/components/background_hang_monitor/background_hang_monitor.rs:61
                         - <background_hang_monitor[5f27e502ead78af0]::background_hang_monitor::BackgroundHangMonitorWorker>::run
                        at /tmp/aec.25_06_30_20_04_42.P8A/servo/components/background_hang_monitor/background_hang_monitor.rs:480
                         - <background_hang_monitor[5f27e502ead78af0]::background_hang_monitor::BackgroundHangMonitorWorker>::perform_a_hang_monitor_checkpoint
                        at /tmp/aec.25_06_30_20_04_42.P8A/servo/components/background_hang_monitor/background_hang_monitor.rs:562
                         - <background_hang_monitor[5f27e502ead78af0]::sampler_linux::LinuxSampler as background_hang_monitor[5f27e502ead78af0]::sampler::Sampler>::suspend_and_sample_thread
                        at /tmp/aec.25_06_30_20_04_42.P8A/servo/components/background_hang_monitor/sampler_linux.rs:172
                         - backtrace[ecf2c71ec589035a]::backtrace::libunwind::trace
                        at /home/ace/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/backtrace-0.3.75/src/backtrace/libunwind.rs:117
                         - backtrace[ecf2c71ec589035a]::backtrace::trace_unsynchronized::<<background_hang_monitor[5f27e502ead78af0]::sampler_linux::LinuxSampler as background_hang_monitor[5f27e502ead78af0]::sampler::Sampler>::suspend_and_sample_thread::{closure#0}>
                        at /home/ace/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/backtrace-0.3.75/src/backtrace/mod.rs:66)
test test_hang_monitoring ... ok
test test_hang_monitoring_exit_signal1 ... ok
test test_hang_monitoring_exit_signal4 ... ok
test test_hang_monitoring_exit_signal3 ... ok
test test_hang_monitoring_exit_signal2 ... ok
test test_hang_monitoring_unregister ... ok

test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.13s
```

---------

Signed-off-by: Anatol Ulrich <e+github@mail.taugt.net>
Co-authored-by: Anatol Ulrich <e+github@mail.taugt.net>
This commit is contained in:
Anatol Ulrich 2025-07-02 13:39:36 +00:00 committed by GitHub
parent 647122d0f6
commit 5f0bbce9d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 2 deletions

1
Cargo.lock generated
View file

@ -703,6 +703,7 @@ dependencies = [
"log",
"mach2",
"nix",
"rustc-demangle",
"serde_json",
]

View file

@ -21,6 +21,7 @@ crossbeam-channel = { workspace = true }
ipc-channel = { workspace = true }
libc = { workspace = true }
log = { workspace = true }
rustc-demangle = { version = "0.1", optional = true }
serde_json = { workspace = true }
[target.'cfg(target_os = "macos")'.dependencies]
@ -33,4 +34,4 @@ nix = { workspace = true, features = ["signal"], optional = true }
nix = { workspace = true, features = ["signal"], optional = true }
[features]
sampler = ["mach2", "nix"]
sampler = ["mach2", "nix", "rustc-demangle"]

View file

@ -86,10 +86,15 @@ impl NativeStack {
continue;
}
backtrace::resolve(*ip, |symbol| {
// TODO: use the demangled or C++ demangled symbols if available.
let name = symbol
.name()
.map(|n| String::from_utf8_lossy(n.as_bytes()).to_string());
// demangle if possible -
// the `rustc_demangle` crate transparently supports both
// "legacy" (C++ style) and "v0" mangling formats.
#[cfg(feature = "sampler")]
let name = name.map(|n| rustc_demangle::demangle(&n).to_string());
let filename = symbol.filename().map(|n| n.to_string_lossy().to_string());
let lineno = symbol.lineno();
profile.backtrace.push(HangProfileSymbol {