Skip to content

Commit 6b51eee

Browse files
committed
Add enter_trace_span! that checks #[cfg("tracing")]
Includes a custom syntax shortand to enter_trace_span! with NAME::SUBNAME. MaybeEnteredTraceSpan is `pub use`d in lib.rs to make it available also in bin/, just in case.
1 parent a96e73f commit 6b51eee

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/tools/miri/src/helpers.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,3 +1431,44 @@ impl ToU64 for usize {
14311431
self.try_into().unwrap()
14321432
}
14331433
}
1434+
1435+
/// This struct is needed to enforce `#[must_use]` on values produced by [enter_trace_span] even
1436+
/// when the "tracing" feature is not enabled.
1437+
#[must_use]
1438+
pub struct MaybeEnteredTraceSpan {
1439+
#[cfg(feature = "tracing")]
1440+
pub _entered_span: tracing::span::EnteredSpan,
1441+
}
1442+
1443+
/// Enters a [tracing::info_span] only if the "tracing" feature is enabled, otherwise does nothing.
1444+
/// This is like [rustc_const_eval::enter_trace_span] except that it does not depend on the
1445+
/// [Machine] trait to check if tracing is enabled, because from the Miri codebase we can directly
1446+
/// check whether the "tracing" feature is enabled, unlike from the rustc_const_eval codebase.
1447+
///
1448+
/// In addition to the syntax accepted by [tracing::span!], this macro optionally allows passing
1449+
/// the span name (i.e. the first macro argument) in the form `NAME::SUBNAME` (without quotes) to
1450+
/// indicate that the span has name "NAME" (usually the name of the component) and has an additional
1451+
/// more specific name "SUBNAME" (usually the function name). The latter is passed to the [tracing]
1452+
/// infrastructure as a span field with the name "NAME". This allows not being distracted by
1453+
/// subnames when looking at the trace in <https://ui.perfetto.dev>, but when deeper introspection
1454+
/// is needed within a component, it's still possible to view the subnames directly in the UI by
1455+
/// selecting a span, clicking on the "NAME" argument on the right, and clicking on "Visualize
1456+
/// argument values".
1457+
/// ```rust
1458+
/// // for example, the first will expand to the second
1459+
/// enter_trace_span!(borrow_tracker::on_stack_pop, /* ... */)
1460+
/// enter_trace_span!("borrow_tracker", borrow_tracker = "on_stack_pop", /* ... */)
1461+
/// ```
1462+
#[macro_export]
1463+
macro_rules! enter_trace_span {
1464+
($name:ident :: $subname:ident $($tt:tt)*) => {{
1465+
enter_trace_span!(stringify!($name), $name = %stringify!(subname) $($tt)*)
1466+
}};
1467+
1468+
($($tt:tt)*) => {
1469+
$crate::MaybeEnteredTraceSpan {
1470+
#[cfg(feature = "tracing")]
1471+
_entered_span: tracing::info_span!($($tt)*).entered()
1472+
}
1473+
};
1474+
}

src/tools/miri/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ pub use crate::eval::{
139139
AlignmentCheck, BacktraceStyle, IsolatedOp, MiriConfig, MiriEntryFnType, RejectOpWith,
140140
ValidationMode, create_ecx, eval_entry,
141141
};
142-
pub use crate::helpers::{AccessKind, EvalContextExt as _, ToU64 as _, ToUsize as _};
142+
pub use crate::helpers::{
143+
AccessKind, EvalContextExt as _, MaybeEnteredTraceSpan, ToU64 as _, ToUsize as _,
144+
};
143145
pub use crate::intrinsics::EvalContextExt as _;
144146
pub use crate::machine::{
145147
AllocExtra, DynMachineCallback, FrameExtra, MachineCallback, MemoryKind, MiriInterpCx,

0 commit comments

Comments
 (0)