Skip to content

implement PanicTracker to track t panics #140636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use tracing::{instrument, span};
pub use utils::change_tracker::{
CONFIG_CHANGE_HISTORY, find_recent_config_change_ids, human_readable_changes,
};
pub use utils::helpers::PanicTracker;

use crate::core::build_steps::vendor::VENDOR_DIR;

Expand Down
30 changes: 25 additions & 5 deletions src/bootstrap/src/utils/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::OnceLock;
use std::thread::panicking;
use std::time::{Instant, SystemTime, UNIX_EPOCH};
use std::{env, fs, io, str};
use std::{env, fs, io, panic, str};

use build_helper::util::fail;
use object::read::archive::ArchiveFile;
Expand All @@ -22,6 +23,23 @@ pub use crate::utils::shared_helpers::{dylib_path, dylib_path_var};
#[cfg(test)]
mod tests;

/// A wrapper around `std::panic::Location` used to track the location of panics
/// triggered by `t` macro usage.
pub struct PanicTracker<'a>(pub &'a panic::Location<'a>);

impl Drop for PanicTracker<'_> {
fn drop(&mut self) {
if panicking() {
eprintln!(
"Panic was initiated from {}:{}:{}",
self.0.file(),
self.0.line(),
self.0.column()
);
}
}
}

/// A helper macro to `unwrap` a result except also print out details like:
///
/// * The file/line of the panic
Expand All @@ -32,19 +50,21 @@ mod tests;
/// using a `Result` with `try!`, but this may change one day...
#[macro_export]
macro_rules! t {
($e:expr) => {
($e:expr) => {{
let _panic_guard = $crate::PanicTracker(std::panic::Location::caller());
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {}", stringify!($e), e),
}
};
}};
// it can show extra info in the second parameter
($e:expr, $extra:expr) => {
($e:expr, $extra:expr) => {{
let _panic_guard = $crate::PanicTracker(std::panic::Location::caller());
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {} ({:?})", stringify!($e), e, $extra),
}
};
}};
}

pub use t;
Expand Down
Loading