Skip to content

Commit 94e1724

Browse files
authored
Rollup merge of #135391 - jieyouxu:conditional-tracing, r=onur-ozkan
bootstrap: Implement conditional `tracing` infra Add a conditional `tracing` setup that is gated behind `BOOTSTRAP_TRACING` env var. This `tracing` infra is implemented by: - Introducing an optional `tracing` cargo feature in bootstrap. - Added optional `tracing*` dependencies which are gated behind the `tracing` cargo feature. - When `BOOTSTRAP_TRACING` is set, `bootstrap.py` will build bootstrap with `--features=tracing`. There is a small trick here to share `BOOTSTRAP_TRACING` env var without having to add a separate env var: - `BOOTSTRAP_TRACING=1` is not a registered `tracing` filter target, so that can be used to enable the `tracing` cargo feature yet not actually enable any tracing logs (useful for editor r-a setups without actually outputting any tracing logs). - `BOOTSTRAP_TRACING=TRACE` and such are actually valid `tracing` filters, but that sets `BOOTSTRAP_TRACING` anyway. Example usage: rust-lang/rust#135299 (that experimental PR is not conditionally gated) This PR is intentionally kept minimal to focus on the infra itself. To get actual mileage, instrumentations will need to be added to individual `Step`s and such. r? `@onur-ozkan` (or reroll)
2 parents 6dd2245 + a58b85a commit 94e1724

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
- [Prologue](./building/bootstrapping/intro.md)
7676
- [What Bootstrapping does](./building/bootstrapping/what-bootstrapping-does.md)
7777
- [How Bootstrap does it](./building/bootstrapping/how-bootstrap-does-it.md)
78+
- [Debugging bootstrap](./building/bootstrapping/debugging-bootstrap.md)
7879

7980
# High-level Compiler Architecture
8081

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Debugging bootstrap
2+
3+
> FIXME: this page could be expanded
4+
5+
## `tracing` in bootstrap
6+
7+
Bootstrap has conditional [`tracing`][tracing] setup to provide structured logging.
8+
9+
[tracing]: https://docs.rs/tracing/0.1.41/tracing/index.html
10+
11+
### Enabling `tracing` output
12+
13+
Bootstrap will conditionally enable `tracing` output if the `BOOTSTRAP_TRACING` env var is set.
14+
15+
Example usage:
16+
17+
```bash
18+
$ BOOTSTRAP_TRACING=TRACE ./x build library --stage 1
19+
```
20+
21+
Example output[^experimental]:
22+
23+
![Example bootstrap tracing output](./debugging-bootstrap/tracing-output-example.png)
24+
25+
[^experimental]: This shows what's *possible* with the infra in an experimental implementation.
26+
27+
The env var `BOOTSTRAP_TRACING` accepts a [`tracing` env-filter][tracing-env-filter]. The `TRACE` filter will enable *all* `trace` level or less verbose level tracing output.
28+
29+
[tracing-env-filter]: https://docs.rs/tracing-subscriber/0.3.19/tracing_subscriber/filter/struct.EnvFilter.html
30+
31+
### Using `tracing` in bootstrap
32+
33+
Both `tracing::*` macros and the `tracing::instrument` proc-macro attribute need to be gated behind `tracing` feature. Examples:
34+
35+
```rs
36+
#[cfg(feature = "tracing")]
37+
use tracing::{instrument, trace};
38+
39+
struct Foo;
40+
41+
impl Step for Foo {
42+
type Output = ();
43+
44+
#[cfg_attr(feature = "tracing", instrument(level = "trace", name = "Foo::should_run", skip_all))]
45+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
46+
#[cfg(feature = "tracing")]
47+
trace!(?run, "entered Foo::should_run");
48+
49+
todo!()
50+
}
51+
52+
#[cfg_attr(
53+
feature = "tracing",
54+
instrument(
55+
level = "trace",
56+
name = "Foo::run",
57+
skip_all,
58+
fields(compiler = ?builder.compiler),
59+
),
60+
)]
61+
fn run(self, builder: &Builder<'_>) -> Self::Output {
62+
#[cfg(feature = "tracing")]
63+
trace!(?run, "entered Foo::run");
64+
65+
todo!()
66+
}
67+
}
68+
```
69+
70+
For `#[instrument]`, it's recommended to:
71+
72+
- Gate it behind `trace` level for fine-granularity, possibly `debug` level for core functions.
73+
- Explicitly pick an instrumentation name via `name = ".."` to distinguish between e.g. `run` of different steps.
74+
- Take care to not cause diverging behavior via tracing, e.g. building extra things only when tracing infra is enabled.
75+
76+
### Enabling `tracing` bootstrap feature in rust-analyzer
77+
78+
You can adjust your `settings.json`'s `rust-analyzer.check.overrideCommand` and `rust-analyzer.cargo.buildScripts.overrideCommand` if you want to also enable `logging` cargo feature by default in your editor. This is mostly useful if you want proper r-a completions and such when working on bootstrap itself.
79+
80+
```json
81+
"rust-analyzer.check.overrideCommand": [
82+
"BOOTSTRAP_TRACING=1", // <- BOOTSTRAP_TRACING=1 won't enable tracing filter, but it will activate bootstrap's `tracing` feature
83+
"python3",
84+
"x.py",
85+
"check",
86+
"--json-output",
87+
"--build-dir=build-rust-analyzer"
88+
],
89+
```
90+
91+
```json
92+
"rust-analyzer.cargo.buildScripts.overrideCommand": [
93+
"BOOTSTRAP_TRACING=1", // <- note this
94+
"python3",
95+
"x.py",
96+
"check",
97+
"--json-output",
98+
"--build-dir=build-rust-analyzer"
99+
],
100+
```
Loading

src/building/bootstrapping/intro.md

+3
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ In this section, we give a high-level overview of
1717
[what Bootstrap does](./what-bootstrapping-does.md), followed by a high-level
1818
introduction to [how Bootstrap does it](./how-bootstrap-does-it.md).
1919

20+
Additionally, see [debugging bootstrap](./debugging-bootstrap.md) to learn
21+
about debugging methods.
22+
2023
[boot]: https://en.wikipedia.org/wiki/Bootstrapping_(compilers)
2124
[ocaml-compiler]: https://github.com/rust-lang/rust/tree/ef75860a0a72f79f97216f8aaa5b388d98da6480/src/boot

0 commit comments

Comments
 (0)