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
-
5
1
# Debugging the compiler
6
2
[ debugging ] : #debugging
7
3
@@ -134,17 +130,20 @@ $ # Cool, now I have a backtrace for the error
134
130
# # Getting logging output
135
131
[getting-logging-output]: # getting-logging-output
136
132
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
+
137
138
The compiler has a lot of ` debug! ` calls, which print out logging information
138
139
at many points. These are very useful to at least narrow down the location of
139
140
a bug if not to find it entirely, or just to orient yourself as to why the
140
141
compiler is doing a particular thing.
141
142
142
143
To see the logs, you need to set the ` RUST_LOG` environment variable to
143
144
your 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.
148
147
149
148
Note that unless you use a very strict filter, the logger will emit a * lot*
150
149
of 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
176
175
$ RUST_LOG=rustc_trans=info rustc +local my-file.rs
177
176
` ` `
178
177
178
+ # ## How to keep or remove logging calls from the resulting binary
179
+
179
180
While 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
183
189
if you run the compiler with `RUST_LOG=rustc rustc some.rs` and only see
184
190
`INFO` logs, make sure that `debug-assertions=yes` is turned on in your
185
191
config.toml.
@@ -190,58 +196,27 @@ want to call `x.py clean` to force one.
190
196
191
197
### Logging etiquette
192
198
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
196
201
helped you pinning down a bug, they will probably help someone else with a
197
202
different one.
198
203
199
204
However, there are still a few concerns that you might care about:
200
205
201
206
### Expensive operations in logs
202
207
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
206
209
207
210
```Rust
208
211
debug!("{:?}", random_operation(tcx));
209
212
```
210
213
211
214
Then 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.
214
216
215
217
This means that you should not put anything too expensive or likely
216
218
to 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.
245
220
246
221
## Formatting Graphviz output (.dot files)
247
222
[formatting-graphviz-output]: #formatting-graphviz-output
@@ -382,7 +357,7 @@ create a minimal working example with Godbolt. Go to
382
357
5. Once you have a godbolt link demonstrating the issue, it is pretty easy to
383
358
fill in an LLVM bug.
384
359
385
-
360
+ [log]: https://docs.rs/log/0.4.6/log/index.html
386
361
[env-logger]: https://docs.rs/env_logger/0.4.3/env_logger/
387
362
388
363
## Narrowing (Bisecting) Regressions
0 commit comments