Skip to content

Commit cc00936

Browse files
authoredJan 13, 2025··
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: #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 abdd40d + 3ae724e commit cc00936

File tree

8 files changed

+341
-6
lines changed

8 files changed

+341
-6
lines changed
 

‎src/bootstrap/Cargo.lock

Lines changed: 191 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ dependencies = [
5858
"tar",
5959
"termcolor",
6060
"toml",
61+
"tracing",
62+
"tracing-subscriber",
63+
"tracing-tree",
6164
"walkdir",
6265
"windows 0.52.0",
6366
"xz2",
@@ -70,7 +73,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
7073
checksum = "40723b8fb387abc38f4f4a37c09073622e41dd12327033091ef8950659e6dc0c"
7174
dependencies = [
7275
"memchr",
73-
"regex-automata",
76+
"regex-automata 0.4.9",
7477
"serde",
7578
]
7679

@@ -271,8 +274,8 @@ dependencies = [
271274
"aho-corasick",
272275
"bstr",
273276
"log",
274-
"regex-automata",
275-
"regex-syntax",
277+
"regex-automata 0.4.9",
278+
"regex-syntax 0.8.5",
276279
]
277280

278281
[[package]]
@@ -300,7 +303,7 @@ dependencies = [
300303
"globset",
301304
"log",
302305
"memchr",
303-
"regex-automata",
306+
"regex-automata 0.4.9",
304307
"same-file",
305308
"walkdir",
306309
"winapi-util",
@@ -322,6 +325,12 @@ dependencies = [
322325
"windows-sys 0.52.0",
323326
]
324327

328+
[[package]]
329+
name = "lazy_static"
330+
version = "1.5.0"
331+
source = "registry+https://github.com/rust-lang/crates.io-index"
332+
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
333+
325334
[[package]]
326335
name = "libc"
327336
version = "0.2.167"
@@ -362,6 +371,15 @@ dependencies = [
362371
"pkg-config",
363372
]
364373

374+
[[package]]
375+
name = "matchers"
376+
version = "0.1.0"
377+
source = "registry+https://github.com/rust-lang/crates.io-index"
378+
checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558"
379+
dependencies = [
380+
"regex-automata 0.1.10",
381+
]
382+
365383
[[package]]
366384
name = "memchr"
367385
version = "2.7.4"
@@ -377,6 +395,25 @@ dependencies = [
377395
"winapi",
378396
]
379397

398+
[[package]]
399+
name = "nu-ansi-term"
400+
version = "0.46.0"
401+
source = "registry+https://github.com/rust-lang/crates.io-index"
402+
checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84"
403+
dependencies = [
404+
"overload",
405+
"winapi",
406+
]
407+
408+
[[package]]
409+
name = "nu-ansi-term"
410+
version = "0.50.1"
411+
source = "registry+https://github.com/rust-lang/crates.io-index"
412+
checksum = "d4a28e057d01f97e61255210fcff094d74ed0466038633e95017f5beb68e4399"
413+
dependencies = [
414+
"windows-sys 0.52.0",
415+
]
416+
380417
[[package]]
381418
name = "object"
382419
version = "0.36.5"
@@ -386,6 +423,12 @@ dependencies = [
386423
"memchr",
387424
]
388425

426+
[[package]]
427+
name = "once_cell"
428+
version = "1.20.2"
429+
source = "registry+https://github.com/rust-lang/crates.io-index"
430+
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
431+
389432
[[package]]
390433
name = "opener"
391434
version = "0.5.2"
@@ -396,6 +439,18 @@ dependencies = [
396439
"winapi",
397440
]
398441

442+
[[package]]
443+
name = "overload"
444+
version = "0.1.1"
445+
source = "registry+https://github.com/rust-lang/crates.io-index"
446+
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
447+
448+
[[package]]
449+
name = "pin-project-lite"
450+
version = "0.2.16"
451+
source = "registry+https://github.com/rust-lang/crates.io-index"
452+
checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b"
453+
399454
[[package]]
400455
name = "pkg-config"
401456
version = "0.3.31"
@@ -439,6 +494,27 @@ dependencies = [
439494
"bitflags",
440495
]
441496

497+
[[package]]
498+
name = "regex"
499+
version = "1.11.1"
500+
source = "registry+https://github.com/rust-lang/crates.io-index"
501+
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
502+
dependencies = [
503+
"aho-corasick",
504+
"memchr",
505+
"regex-automata 0.4.9",
506+
"regex-syntax 0.8.5",
507+
]
508+
509+
[[package]]
510+
name = "regex-automata"
511+
version = "0.1.10"
512+
source = "registry+https://github.com/rust-lang/crates.io-index"
513+
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
514+
dependencies = [
515+
"regex-syntax 0.6.29",
516+
]
517+
442518
[[package]]
443519
name = "regex-automata"
444520
version = "0.4.9"
@@ -447,9 +523,15 @@ checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
447523
dependencies = [
448524
"aho-corasick",
449525
"memchr",
450-
"regex-syntax",
526+
"regex-syntax 0.8.5",
451527
]
452528

529+
[[package]]
530+
name = "regex-syntax"
531+
version = "0.6.29"
532+
source = "registry+https://github.com/rust-lang/crates.io-index"
533+
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
534+
453535
[[package]]
454536
name = "regex-syntax"
455537
version = "0.8.5"
@@ -539,12 +621,27 @@ dependencies = [
539621
"digest",
540622
]
541623

624+
[[package]]
625+
name = "sharded-slab"
626+
version = "0.1.7"
627+
source = "registry+https://github.com/rust-lang/crates.io-index"
628+
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
629+
dependencies = [
630+
"lazy_static",
631+
]
632+
542633
[[package]]
543634
name = "shlex"
544635
version = "1.3.0"
545636
source = "registry+https://github.com/rust-lang/crates.io-index"
546637
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
547638

639+
[[package]]
640+
name = "smallvec"
641+
version = "1.13.2"
642+
source = "registry+https://github.com/rust-lang/crates.io-index"
643+
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
644+
548645
[[package]]
549646
name = "syn"
550647
version = "2.0.87"
@@ -589,6 +686,16 @@ dependencies = [
589686
"winapi-util",
590687
]
591688

689+
[[package]]
690+
name = "thread_local"
691+
version = "1.1.8"
692+
source = "registry+https://github.com/rust-lang/crates.io-index"
693+
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
694+
dependencies = [
695+
"cfg-if",
696+
"once_cell",
697+
]
698+
592699
[[package]]
593700
name = "toml"
594701
version = "0.5.11"
@@ -598,6 +705,79 @@ dependencies = [
598705
"serde",
599706
]
600707

708+
[[package]]
709+
name = "tracing"
710+
version = "0.1.41"
711+
source = "registry+https://github.com/rust-lang/crates.io-index"
712+
checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0"
713+
dependencies = [
714+
"pin-project-lite",
715+
"tracing-attributes",
716+
"tracing-core",
717+
]
718+
719+
[[package]]
720+
name = "tracing-attributes"
721+
version = "0.1.28"
722+
source = "registry+https://github.com/rust-lang/crates.io-index"
723+
checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d"
724+
dependencies = [
725+
"proc-macro2",
726+
"quote",
727+
"syn",
728+
]
729+
730+
[[package]]
731+
name = "tracing-core"
732+
version = "0.1.33"
733+
source = "registry+https://github.com/rust-lang/crates.io-index"
734+
checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c"
735+
dependencies = [
736+
"once_cell",
737+
"valuable",
738+
]
739+
740+
[[package]]
741+
name = "tracing-log"
742+
version = "0.2.0"
743+
source = "registry+https://github.com/rust-lang/crates.io-index"
744+
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
745+
dependencies = [
746+
"log",
747+
"once_cell",
748+
"tracing-core",
749+
]
750+
751+
[[package]]
752+
name = "tracing-subscriber"
753+
version = "0.3.19"
754+
source = "registry+https://github.com/rust-lang/crates.io-index"
755+
checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008"
756+
dependencies = [
757+
"matchers",
758+
"nu-ansi-term 0.46.0",
759+
"once_cell",
760+
"regex",
761+
"sharded-slab",
762+
"smallvec",
763+
"thread_local",
764+
"tracing",
765+
"tracing-core",
766+
"tracing-log",
767+
]
768+
769+
[[package]]
770+
name = "tracing-tree"
771+
version = "0.4.0"
772+
source = "registry+https://github.com/rust-lang/crates.io-index"
773+
checksum = "f459ca79f1b0d5f71c54ddfde6debfc59c8b6eeb46808ae492077f739dc7b49c"
774+
dependencies = [
775+
"nu-ansi-term 0.50.1",
776+
"tracing-core",
777+
"tracing-log",
778+
"tracing-subscriber",
779+
]
780+
601781
[[package]]
602782
name = "typenum"
603783
version = "1.17.0"
@@ -610,6 +790,12 @@ version = "1.0.13"
610790
source = "registry+https://github.com/rust-lang/crates.io-index"
611791
checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe"
612792

793+
[[package]]
794+
name = "valuable"
795+
version = "0.1.0"
796+
source = "registry+https://github.com/rust-lang/crates.io-index"
797+
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
798+
613799
[[package]]
614800
name = "version_check"
615801
version = "0.9.5"

‎src/bootstrap/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ default-run = "bootstrap"
77

88
[features]
99
build-metrics = ["sysinfo"]
10+
tracing = ["dep:tracing", "dep:tracing-subscriber", "dep:tracing-tree"]
1011

1112
[lib]
1213
path = "src/lib.rs"
@@ -64,6 +65,11 @@ xz2 = "0.1"
6465
# Dependencies needed by the build-metrics feature
6566
sysinfo = { version = "0.33.0", default-features = false, optional = true, features = ["system"] }
6667

68+
# Dependencies needed by the `logging` feature
69+
tracing = { version = "0.1", optional = true, features = ["attributes"] }
70+
tracing-subscriber = { version = "0.3", optional = true, features = ["env-filter", "fmt", "registry", "std"] }
71+
tracing-tree = { version = "0.4.0", optional = true }
72+
6773
[target.'cfg(windows)'.dependencies.junction]
6874
version = "1.0.0"
6975

‎src/bootstrap/bootstrap.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,10 @@ def build_bootstrap_cmd(self, env):
11291129
"-Zroot-dir=" + self.rust_root,
11301130
]
11311131
args.extend("--verbose" for _ in range(self.verbose))
1132+
1133+
if "BOOTSTRAP_TRACING" in env:
1134+
args.append("--features=tracing")
1135+
11321136
if self.use_locked_deps:
11331137
args.append("--locked")
11341138
if self.use_vendored_sources:

‎src/bootstrap/src/bin/main.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@ use bootstrap::{
1515
human_readable_changes, t,
1616
};
1717
use build_helper::ci::CiEnv;
18-
18+
#[cfg(feature = "tracing")]
19+
use tracing::*;
20+
#[cfg(feature = "tracing")]
21+
use tracing_subscriber::EnvFilter;
22+
#[cfg(feature = "tracing")]
23+
use tracing_subscriber::prelude::*;
24+
25+
#[cfg_attr(feature = "tracing", instrument(level = "trace", name = "main"))]
1926
fn main() {
27+
#[cfg(feature = "tracing")]
28+
setup_tracing();
29+
2030
let args = env::args().skip(1).collect::<Vec<_>>();
2131

2232
if Flags::try_parse_verbose_help(&args) {
@@ -187,3 +197,28 @@ fn check_version(config: &Config) -> Option<String> {
187197

188198
Some(msg)
189199
}
200+
201+
// # Note on `tracing` usage in bootstrap
202+
//
203+
// Due to the conditional compilation via the `tracing` cargo feature, this means that `tracing`
204+
// usages in bootstrap need to be also gated behind the `tracing` feature:
205+
//
206+
// - `tracing` macros (like `trace!`) and anything from `tracing`, `tracing_subscriber` and
207+
// `tracing-tree` will need to be gated by `#[cfg(feature = "tracing")]`.
208+
// - `tracing`'s `#[instrument(..)]` macro will need to be gated like `#![cfg_attr(feature =
209+
// "tracing", instrument(..))]`.
210+
#[cfg(feature = "tracing")]
211+
fn setup_tracing() {
212+
let filter = EnvFilter::from_env("BOOTSTRAP_TRACING");
213+
let layer = tracing_tree::HierarchicalLayer::default()
214+
.with_writer(std::io::stderr)
215+
.with_ansi(true)
216+
.with_targets(true)
217+
.with_bracketed_fields(true)
218+
.with_indent_amount(2)
219+
.with_indent_lines(true);
220+
let subscriber = tracing_subscriber::registry().with(filter).with(layer);
221+
222+
tracing::subscriber::set_global_default(subscriber).unwrap();
223+
trace!("tracing subscriber setup");
224+
}

‎src/doc/rustc-dev-guide/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 100 additions & 0 deletions
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+
```

‎src/doc/rustc-dev-guide/src/building/bootstrapping/intro.md

Lines changed: 3 additions & 0 deletions
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)
Please sign in to comment.