Skip to content

Commit 6e25418

Browse files
authored
Rollup merge of #75143 - oli-obk:tracing, r=RalfJung
Use `tracing` spans to trace the entire MIR interp stack r? @RalfJung While being very verbose, this allows really good tracking of what's going on. While I considered schemes like the previous indenter that we had (which we could get by using the `tracing-tree` crate), this will break down horribly with things like multithreaded rustc. Instead, we can now use `RUSTC_LOG` to restrict the things being traced. You could specify a filter in a way that only shows the logging of a specific frame. ![screenshot of command line output of the new formatting](https://user-images.githubusercontent.com/332036/89291343-aa40de00-d65a-11ea-9f6c-ea06c1806327.png) If we lower the span's level to `debug`, then in `info` level logging we'd not see the frames, but in `debug` level we would see them. The filtering rules in `tracing` are super powerful, but I'm not sure if we can specify a filter so we do see `debug` level events, but *not* the `frame` spans. The documentation at https://docs.rs/tracing-subscriber/0.2.10/tracing_subscriber/struct.EnvFilter.html makes me think that we can only turn on things, not turn off things at a more precise level. cc @hawkw
2 parents bad9ad0 + 8f9472c commit 6e25418

File tree

6 files changed

+75
-41
lines changed

6 files changed

+75
-41
lines changed

Cargo.lock

+3-12
Original file line numberDiff line numberDiff line change
@@ -1726,15 +1726,6 @@ dependencies = [
17261726
"cfg-if",
17271727
]
17281728

1729-
[[package]]
1730-
name = "log_settings"
1731-
version = "0.1.2"
1732-
source = "registry+https://github.com/rust-lang/crates.io-index"
1733-
checksum = "19af41f0565d7c19b2058153ad0b42d4d5ce89ec4dbf06ed6741114a8b63e7cd"
1734-
dependencies = [
1735-
"lazy_static",
1736-
]
1737-
17381729
[[package]]
17391730
name = "lsp-codec"
17401731
version = "0.1.2"
@@ -3523,6 +3514,7 @@ dependencies = [
35233514
"rustc_target",
35243515
"tracing",
35253516
"tracing-subscriber",
3517+
"tracing-tree",
35263518
"winapi 0.3.9",
35273519
]
35283520

@@ -3810,7 +3802,6 @@ version = "0.0.0"
38103802
dependencies = [
38113803
"either",
38123804
"itertools 0.9.0",
3813-
"log_settings",
38143805
"polonius-engine",
38153806
"regex",
38163807
"rustc_apfloat",
@@ -5105,9 +5096,9 @@ dependencies = [
51055096

51065097
[[package]]
51075098
name = "tracing-tree"
5108-
version = "0.1.5"
5099+
version = "0.1.6"
51095100
source = "registry+https://github.com/rust-lang/crates.io-index"
5110-
checksum = "e1a3dc4774db3a6b2d66a4f8d8de670e874ec3ed55615860c994927419b32c5f"
5101+
checksum = "43aac8afb493b08e1e1904956f7407c1e671b9c83b26a17e1bd83d6a3520e350"
51115102
dependencies = [
51125103
"ansi_term 0.12.1",
51135104
"atty",

compiler/rustc_driver/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ crate-type = ["dylib"]
1111
libc = "0.2"
1212
tracing = { version = "0.1.18" }
1313
tracing-subscriber = { version = "0.2.10", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"] }
14+
tracing-tree = "0.1.6"
1415
rustc_middle = { path = "../rustc_middle" }
1516
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1617
rustc_target = { path = "../rustc_target" }

compiler/rustc_driver/src/lib.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -1251,11 +1251,21 @@ pub fn init_env_logger(env: &str) {
12511251
Ok(s) if s.is_empty() => return,
12521252
Ok(_) => {}
12531253
}
1254-
let builder = tracing_subscriber::FmtSubscriber::builder();
1255-
1256-
let builder = builder.with_env_filter(tracing_subscriber::EnvFilter::from_env(env));
1257-
1258-
builder.init()
1254+
let filter = tracing_subscriber::EnvFilter::from_env(env);
1255+
let layer = tracing_tree::HierarchicalLayer::default()
1256+
.with_indent_lines(true)
1257+
.with_ansi(true)
1258+
.with_targets(true)
1259+
.with_thread_ids(true)
1260+
.with_thread_names(true)
1261+
.with_wraparound(10)
1262+
.with_verbose_exit(true)
1263+
.with_verbose_entry(true)
1264+
.with_indent_amount(2);
1265+
1266+
use tracing_subscriber::layer::SubscriberExt;
1267+
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
1268+
tracing::subscriber::set_global_default(subscriber).unwrap();
12591269
}
12601270

12611271
pub fn main() -> ! {

compiler/rustc_mir/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ either = "1.5.0"
1212
rustc_graphviz = { path = "../rustc_graphviz" }
1313
itertools = "0.9"
1414
tracing = "0.1"
15-
log_settings = "0.1.1"
1615
polonius-engine = "0.12.0"
1716
regex = "1"
1817
rustc_middle = { path = "../rustc_middle" }

compiler/rustc_mir/src/interpret/eval_context.rs

+56-22
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,41 @@ pub struct InterpCx<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
4848
FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), Pointer<M::PointerTag>>,
4949
}
5050

51+
// The Phantomdata exists to prevent this type from being `Send`. If it were sent across a thread
52+
// boundary and dropped in the other thread, it would exit the span in the other thread.
53+
struct SpanGuard(tracing::Span, std::marker::PhantomData<*const u8>);
54+
55+
impl SpanGuard {
56+
/// By default a `SpanGuard` does nothing.
57+
fn new() -> Self {
58+
Self(tracing::Span::none(), std::marker::PhantomData)
59+
}
60+
61+
/// If a span is entered, we exit the previous span (if any, normally none) and enter the
62+
/// new span. This is mainly so we don't have to use `Option` for the `tracing_span` field of
63+
/// `Frame` by creating a dummy span to being with and then entering it once the frame has
64+
/// been pushed.
65+
fn enter(&mut self, span: tracing::Span) {
66+
// This executes the destructor on the previous instance of `SpanGuard`, ensuring that
67+
// we never enter or exit more spans than vice versa. Unless you `mem::leak`, then we
68+
// can't protect the tracing stack, but that'll just lead to weird logging, no actual
69+
// problems.
70+
*self = Self(span, std::marker::PhantomData);
71+
self.0.with_subscriber(|(id, dispatch)| {
72+
dispatch.enter(id);
73+
});
74+
}
75+
}
76+
77+
impl Drop for SpanGuard {
78+
fn drop(&mut self) {
79+
self.0.with_subscriber(|(id, dispatch)| {
80+
dispatch.exit(id);
81+
});
82+
}
83+
}
84+
5185
/// A stack frame.
52-
#[derive(Clone)]
5386
pub struct Frame<'mir, 'tcx, Tag = (), Extra = ()> {
5487
////////////////////////////////////////////////////////////////////////////////
5588
// Function and callsite information
@@ -80,6 +113,11 @@ pub struct Frame<'mir, 'tcx, Tag = (), Extra = ()> {
80113
/// can either directly contain `Scalar` or refer to some part of an `Allocation`.
81114
pub locals: IndexVec<mir::Local, LocalState<'tcx, Tag>>,
82115

116+
/// The span of the `tracing` crate is stored here.
117+
/// When the guard is dropped, the span is exited. This gives us
118+
/// a full stack trace on all tracing statements.
119+
tracing_span: SpanGuard,
120+
83121
////////////////////////////////////////////////////////////////////////////////
84122
// Current position within the function
85123
////////////////////////////////////////////////////////////////////////////////
@@ -184,6 +222,7 @@ impl<'mir, 'tcx, Tag> Frame<'mir, 'tcx, Tag> {
184222
locals: self.locals,
185223
loc: self.loc,
186224
extra,
225+
tracing_span: self.tracing_span,
187226
}
188227
}
189228
}
@@ -637,11 +676,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
637676
return_place: Option<PlaceTy<'tcx, M::PointerTag>>,
638677
return_to_block: StackPopCleanup,
639678
) -> InterpResult<'tcx> {
640-
if !self.stack().is_empty() {
641-
info!("PAUSING({}) {}", self.frame_idx(), self.frame().instance);
642-
}
643-
::log_settings::settings().indentation += 1;
644-
645679
// first push a stack frame so we have access to the local substs
646680
let pre_frame = Frame {
647681
body,
@@ -652,6 +686,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
652686
// all methods actually know about the frame
653687
locals: IndexVec::new(),
654688
instance,
689+
tracing_span: SpanGuard::new(),
655690
extra: (),
656691
};
657692
let frame = M::init_frame_extra(self, pre_frame)?;
@@ -696,7 +731,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
696731
self.frame_mut().locals = locals;
697732
M::after_stack_push(self)?;
698733
self.frame_mut().loc = Ok(mir::Location::START);
699-
info!("ENTERING({}) {}", self.frame_idx(), self.frame().instance);
734+
735+
let span = info_span!("frame", "{}", instance);
736+
self.frame_mut().tracing_span.enter(span);
700737

701738
Ok(())
702739
}
@@ -747,10 +784,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
747784
/// cause us to continue unwinding.
748785
pub(super) fn pop_stack_frame(&mut self, unwinding: bool) -> InterpResult<'tcx> {
749786
info!(
750-
"LEAVING({}) {} (unwinding = {})",
751-
self.frame_idx(),
752-
self.frame().instance,
753-
unwinding
787+
"popping stack frame ({})",
788+
if unwinding { "during unwinding" } else { "returning from function" }
754789
);
755790

756791
// Sanity check `unwinding`.
@@ -766,7 +801,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
766801
throw_ub_format!("unwinding past the topmost frame of the stack");
767802
}
768803

769-
::log_settings::settings().indentation -= 1;
770804
let frame =
771805
self.stack_mut().pop().expect("tried to pop a stack frame, but there were none");
772806

@@ -823,15 +857,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
823857
}
824858
}
825859

826-
if !self.stack().is_empty() {
827-
info!(
828-
"CONTINUING({}) {} (unwinding = {})",
829-
self.frame_idx(),
830-
self.frame().instance,
831-
unwinding
832-
);
833-
}
834-
835860
Ok(())
836861
}
837862

@@ -995,7 +1020,16 @@ where
9951020
{
9961021
fn hash_stable(&self, hcx: &mut StableHashingContext<'ctx>, hasher: &mut StableHasher) {
9971022
// Exhaustive match on fields to make sure we forget no field.
998-
let Frame { body, instance, return_to_block, return_place, locals, loc, extra } = self;
1023+
let Frame {
1024+
body,
1025+
instance,
1026+
return_to_block,
1027+
return_place,
1028+
locals,
1029+
loc,
1030+
extra,
1031+
tracing_span: _,
1032+
} = self;
9991033
body.hash_stable(hcx, hasher);
10001034
instance.hash_stable(hcx, hasher);
10011035
return_to_block.hash_stable(hcx, hasher);

src/tools/tidy/src/deps.rs

-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
116116
"libz-sys",
117117
"lock_api",
118118
"log",
119-
"log_settings",
120119
"maybe-uninit",
121120
"md-5",
122121
"measureme",

0 commit comments

Comments
 (0)