Skip to content

Commit 48af505

Browse files
committed
Add custom syntax shortand to enter_trace_span!
1 parent 17b06e6 commit 48af505

File tree

2 files changed

+34
-49
lines changed

2 files changed

+34
-49
lines changed

src/borrow_tracker/mod.rs

Lines changed: 14 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,7 @@ impl GlobalStateInner {
260260
kind: MemoryKind,
261261
machine: &MiriMachine<'_>,
262262
) -> AllocState {
263-
let _span = enter_trace_span!(
264-
"borrow_tracker",
265-
borrow_tracker_fun = %"new_allocation",
266-
?id,
267-
?alloc_size,
268-
?kind,
269-
);
263+
let _span = enter_trace_span!(borrow_tracker::new_allocation, ?id, ?alloc_size, ?kind);
270264
match self.borrow_tracker_method {
271265
BorrowTrackerMethod::StackedBorrows =>
272266
AllocState::StackedBorrows(Box::new(RefCell::new(Stacks::new_allocation(
@@ -287,12 +281,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
287281
kind: RetagKind,
288282
val: &ImmTy<'tcx>,
289283
) -> InterpResult<'tcx, ImmTy<'tcx>> {
290-
let _span = enter_trace_span!(
291-
"borrow_tracker",
292-
borrow_tracker_fun = %"retag_ptr_value",
293-
?kind,
294-
?val.layout,
295-
);
284+
let _span = enter_trace_span!(borrow_tracker::retag_ptr_value, ?kind, ?val.layout);
296285
let this = self.eval_context_mut();
297286
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
298287
match method {
@@ -306,12 +295,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
306295
kind: RetagKind,
307296
place: &PlaceTy<'tcx>,
308297
) -> InterpResult<'tcx> {
309-
let _span = enter_trace_span!(
310-
"borrow_tracker",
311-
borrow_tracker_fun = %"retag_place_contents",
312-
?kind,
313-
?place,
314-
);
298+
let _span = enter_trace_span!(borrow_tracker::retag_place_contents, ?kind, ?place);
315299
let this = self.eval_context_mut();
316300
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
317301
match method {
@@ -321,8 +305,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
321305
}
322306

323307
fn protect_place(&mut self, place: &MPlaceTy<'tcx>) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
324-
let _span =
325-
enter_trace_span!("borrow_tracker", borrow_tracker_fun = %"protect_place", ?place);
308+
let _span = enter_trace_span!(borrow_tracker::protect_place, ?place);
326309
let this = self.eval_context_mut();
327310
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
328311
match method {
@@ -332,12 +315,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
332315
}
333316

334317
fn expose_tag(&self, alloc_id: AllocId, tag: BorTag) -> InterpResult<'tcx> {
335-
let _span = enter_trace_span!(
336-
"borrow_tracker",
337-
borrow_tracker_fun = %"expose_tag",
338-
alloc_id = alloc_id.0,
339-
tag = tag.0,
340-
);
318+
let _span: MaybeEnteredTraceSpan =
319+
enter_trace_span!(borrow_tracker::expose_tag, alloc_id = alloc_id.0, tag = tag.0);
341320
let this = self.eval_context_ref();
342321
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
343322
match method {
@@ -381,7 +360,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
381360
&self,
382361
frame: &Frame<'tcx, Provenance, FrameExtra<'tcx>>,
383362
) -> InterpResult<'tcx> {
384-
let _span = enter_trace_span!("borrow_tracker", borrow_tracker_fun = %"on_stack_pop");
363+
let _span = enter_trace_span!(borrow_tracker::on_stack_pop);
385364
let this = self.eval_context_ref();
386365
let borrow_tracker = this.machine.borrow_tracker.as_ref().unwrap();
387366
// The body of this loop needs `borrow_tracker` immutably
@@ -459,11 +438,7 @@ impl AllocState {
459438
range: AllocRange,
460439
machine: &MiriMachine<'tcx>,
461440
) -> InterpResult<'tcx> {
462-
let _span = enter_trace_span!(
463-
"borrow_tracker",
464-
borrow_tracker_fun = %"before_memory_read",
465-
alloc_id = alloc_id.0
466-
);
441+
let _span = enter_trace_span!(borrow_tracker::before_memory_read, alloc_id = alloc_id.0);
467442
match self {
468443
AllocState::StackedBorrows(sb) =>
469444
sb.borrow_mut().before_memory_read(alloc_id, prov_extra, range, machine),
@@ -485,11 +460,7 @@ impl AllocState {
485460
range: AllocRange,
486461
machine: &MiriMachine<'tcx>,
487462
) -> InterpResult<'tcx> {
488-
let _span = enter_trace_span!(
489-
"borrow_tracker",
490-
borrow_tracker_fun = %"before_memory_write",
491-
alloc_id = alloc_id.0
492-
);
463+
let _span = enter_trace_span!(borrow_tracker::before_memory_write, alloc_id = alloc_id.0);
493464
match self {
494465
AllocState::StackedBorrows(sb) =>
495466
sb.get_mut().before_memory_write(alloc_id, prov_extra, range, machine),
@@ -511,11 +482,8 @@ impl AllocState {
511482
size: Size,
512483
machine: &MiriMachine<'tcx>,
513484
) -> InterpResult<'tcx> {
514-
let _span = enter_trace_span!(
515-
"borrow_tracker",
516-
borrow_tracker_fun = %"before_memory_deallocation",
517-
alloc_id = alloc_id.0
518-
);
485+
let _span =
486+
enter_trace_span!(borrow_tracker::before_memory_deallocation, alloc_id = alloc_id.0);
519487
match self {
520488
AllocState::StackedBorrows(sb) =>
521489
sb.get_mut().before_memory_deallocation(alloc_id, prov_extra, size, machine),
@@ -525,8 +493,7 @@ impl AllocState {
525493
}
526494

527495
pub fn remove_unreachable_tags(&self, tags: &FxHashSet<BorTag>) {
528-
let _span =
529-
enter_trace_span!("borrow_tracker", borrow_tracker_fun = %"remove_unreachable_tags");
496+
let _span = enter_trace_span!(borrow_tracker::remove_unreachable_tags);
530497
match self {
531498
AllocState::StackedBorrows(sb) => sb.borrow_mut().remove_unreachable_tags(tags),
532499
AllocState::TreeBorrows(tb) => tb.borrow_mut().remove_unreachable_tags(tags),
@@ -542,8 +509,7 @@ impl AllocState {
542509
alloc_id: AllocId, // diagnostics
543510
) -> InterpResult<'tcx> {
544511
let _span = enter_trace_span!(
545-
"borrow_tracker",
546-
borrow_tracker_fun = %"release_protector",
512+
borrow_tracker::release_protector,
547513
alloc_id = alloc_id.0,
548514
tag = tag.0
549515
);
@@ -557,7 +523,7 @@ impl AllocState {
557523

558524
impl VisitProvenance for AllocState {
559525
fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
560-
let _span = enter_trace_span!("borrow_tracker", borrow_tracker_fun = %"visit_provenance");
526+
let _span = enter_trace_span!(borrow_tracker::visit_provenance);
561527
match self {
562528
AllocState::StackedBorrows(sb) => sb.visit_provenance(visit),
563529
AllocState::TreeBorrows(tb) => tb.visit_provenance(visit),

src/helpers.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1444,12 +1444,31 @@ pub struct MaybeEnteredTraceSpan {
14441444
/// This is like [rustc_const_eval::enter_trace_span] except that it does not depend on the
14451445
/// [Machine] trait to check if tracing is enabled, because from the Miri codebase we can directly
14461446
/// 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 is
1454+
/// 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+
/// ```
14471462
#[macro_export]
14481463
macro_rules! enter_trace_span {
1464+
($name:ident :: $subname:ident $($tt:tt)*) => {{
1465+
enter_trace_span!(stringify!($name), $name = %stringify!(subname) $($tt)*)
1466+
}};
1467+
14491468
($($tt:tt)*) => {
14501469
$crate::MaybeEnteredTraceSpan {
14511470
#[cfg(feature = "tracing")]
14521471
_entered_span: tracing::info_span!($($tt)*).entered()
14531472
}
1454-
}
1473+
};
14551474
}

0 commit comments

Comments
 (0)