1- ** Note: This is copied from the
2- [ rust-forge] ( https://github.com/rust-lang-nursery/rust-forge ) . If anything needs
3- updating, please open an issue or make a PR on the github repo.**
4-
51# Debugging the compiler
62[ debugging ] : #debugging
73
@@ -134,17 +130,20 @@ $ # Cool, now I have a backtrace for the error
134130# # Getting logging output
135131[getting-logging-output]: # getting-logging-output
136132
133+ These crates are used in compiler for logging:
134+
135+ * [log]
136+ * [env-logger]: check the link to see the full ` RUST_LOG` syntax
137+
137138The compiler has a lot of ` debug! ` calls, which print out logging information
138139at many points. These are very useful to at least narrow down the location of
139140a bug if not to find it entirely, or just to orient yourself as to why the
140141compiler is doing a particular thing.
141142
142143To see the logs, you need to set the ` RUST_LOG` environment variable to
143144your log filter, e.g. to get the logs for a specific module, you can run the
144- compiler as ` RUST_LOG=module::path rustc my-file.rs` . The Rust logs are
145- powered by [env-logger], and you can look at the docs linked there to see
146- the full ` RUST_LOG` syntax. All ` debug! ` output will then appear in
147- standard error.
145+ compiler as ` RUST_LOG=module::path rustc my-file.rs` . All ` debug! ` output will
146+ then appear in standard error.
148147
149148Note that unless you use a very strict filter, the logger will emit a * lot*
150149of output - so it' s typically a good idea to pipe standard error to a file
@@ -176,8 +175,10 @@ $ RUST_LOG=debug rustc +local my-file.rs 2>all-log
176175$ RUST_LOG=rustc_trans=info rustc +local my-file.rs
177176` ` `
178177
179- While calls to ` info! ` are included in every build of the compiler,
180- calls to ` debug! ` are only included in the program if the
178+ # ## How to keep or remove `debug!` and `trace!` calls from the resulting binary
179+
180+ While calls to ` error! ` , ` warn! ` and ` info! ` are included in every build of the compiler,
181+ calls to ` debug! ` and ` trace! ` are only included in the program if
181182` debug-assertions=yes` is turned on in config.toml (it is
182183turned off by default), so if you don' t see `DEBUG` logs, especially
183184if you run the compiler with `RUST_LOG=rustc rustc some.rs` and only see
@@ -200,48 +201,18 @@ However, there are still a few concerns that you might care about:
200201
201202### Expensive operations in logs
202203
203- A note of caution: the expressions *within* the `debug!` call are run
204- whenever RUST_LOG is set, even if the filter would exclude the log. This means
205- that if in the module `rustc::foo` you have a statement
204+ If in the module `rustc::foo` you have a statement
206205
207206```Rust
208207debug!("{:?}", random_operation(tcx));
209208```
210209
211210Then if someone runs a debug `rustc` with `RUST_LOG=rustc::bar`, then
212- `random_operation()` will still run - even while it' s output will never be
213- needed!
211+ `random_operation()` will run.
214212
215213This means that you should not put anything too expensive or likely
216214to crash there - that would annoy anyone who wants to use logging for their own
217- module. Note that if ` RUST_LOG` is unset (the default), then the code will not
218- run - this means that if your logging code panics, then no-one will know it
219- until someone tries to use logging to find * another* bug.
220-
221- If you * need* to do an expensive operation in a log, be aware that while log
222- expressions are * evaluated* even if logging is not enabled in your module,
223- they are not * formatted* unless it * is* . This means you can put your
224- expensive/crashy operations inside an ` fmt::Debug` impl, and they will not be
225- run unless your log is enabled:
226-
227- ` ` ` Rust
228- use std::fmt;
229-
230- struct ExpensiveOperationContainer< ' a, ' gcx, ' tcx>
231- where ' tcx: ' gcx, ' a: ' tcx
232- {
233- tcx: TyCtxt<' a, ' gcx, ' tcx>
234- }
235-
236- impl< ' a, ' gcx, ' tcx> fmt::Debug for ExpensiveOperationContainer<' a, ' gcx, ' tcx> {
237- fn fmt(& self, fmt: & mut fmt::Formatter) -> fmt::Result {
238- let value = random_operation(tcx);
239- fmt::Debug::fmt(& value, fmt)
240- }
241- }
242-
243- debug! (" {:?}" , ExpensiveOperationContainer { tcx });
244- ` ` `
215+ module. No-one will know it until someone tries to use logging to find *another* bug.
245216
246217## Formatting Graphviz output (.dot files)
247218[formatting-graphviz-output]: #formatting-graphviz-output
@@ -382,7 +353,7 @@ create a minimal working example with Godbolt. Go to
3823535. Once you have a godbolt link demonstrating the issue, it is pretty easy to
383354 fill in an LLVM bug.
384355
385-
356+ [log]: https://docs.rs/log/0.4.6/log/index.html
386357[env-logger]: https://docs.rs/env_logger/0.4.3/env_logger/
387358
388359## Narrowing (Bisecting) Regressions
0 commit comments