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] and you can 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,10 +175,17 @@ $ RUST_LOG=debug rustc +local my-file.rs 2>all-log
176175$ RUST_LOG=rustc_trans=info rustc +local my-file.rs
177176` ` `
178177
178+ # ## How to keep or remove logging calls from the resulting binary
179+
179180While calls to ` info! ` are included in every build of the compiler,
180- calls to ` debug! ` are only included in the program if the
181- ` debug-assertions=yes` is turned on in config.toml (it is
182- turned off by default), so if you don' t see `DEBUG` logs, especially
181+ calls to ` debug! ` and ` trace! ` can be removed and not present in the resulting binary:
182+
183+ * in relase mode, they are removed by default
184+ * in debug mode, they are kept by default
185+
186+ ` debug-assertions` can be used to change the default behavior. e.g. if ` debug-assertions=yes`
187+ is turned on in config.toml (it is turned off by default), calls to ` debug! ` and ` trace! `
188+ will take effect even in relese mode. And if you don' t see `DEBUG` logs, especially
183189if you run the compiler with `RUST_LOG=rustc rustc some.rs` and only see
184190`INFO` logs, make sure that `debug-assertions=yes` is turned on in your
185191config.toml.
@@ -190,58 +196,27 @@ want to call `x.py clean` to force one.
190196
191197### Logging etiquette
192198
193- Because calls to `debug!` are removed by default, in most cases, don' t worry
194- about adding " unnecessary" calls to ` debug! ` and leaving them in code you
195- commit - they won' t slow down the performance of what we ship, and if they
199+ In most cases, don' t worry about adding " unnecessary" calls to ` debug! ` and leaving them in code you commit
200+ - they won' t slow down the performance of what we ship, and if they
196201helped you pinning down a bug, they will probably help someone else with a
197202different one.
198203
199204However, there are still a few concerns that you might care about:
200205
201206### Expensive operations in logs
202207
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
208+ If in the module `rustc::foo` you have a statement
206209
207210```Rust
208211debug!("{:?}", random_operation(tcx));
209212```
210213
211214Then 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!
215+ `random_operation()` will run.
214216
215217This means that you should not put anything too expensive or likely
216218to 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- ` ` `
219+ module. No-one will know it until someone tries to use logging to find *another* bug.
245220
246221## Formatting Graphviz output (.dot files)
247222[formatting-graphviz-output]: #formatting-graphviz-output
@@ -382,7 +357,7 @@ create a minimal working example with Godbolt. Go to
3823575. Once you have a godbolt link demonstrating the issue, it is pretty easy to
383358 fill in an LLVM bug.
384359
385-
360+ [log]: https://docs.rs/log/0.4.6/log/index.html
386361[env-logger]: https://docs.rs/env_logger/0.4.3/env_logger/
387362
388363## Narrowing (Bisecting) Regressions
0 commit comments