Skip to content

Commit 6d94911

Browse files
committed
addressed feedback
1 parent a8dfb26 commit 6d94911

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/doc/unstable-book/src/compiler-flags/source-based-code-coverage.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ When running a coverage-instrumented program, the counter values are written to
2626

2727
## Enable coverage profiling in the Rust compiler
2828

29-
*IMPORTANT:* Rust's coverage profiling features may not be enabled, by default. To enable them, you may need to build a version of the Rust compiler with the `profiler` feature enabled.
29+
Rust's source-based code coverage requires the Rust "profiler runtime". Without it, compiling with `-Zinstrument-coverage` generates an error that the profiler runtime is missing.
3030

31-
First, edit the `config.toml` file, and find the `profiler` feature entry. Uncomment it and set it to `true`:
31+
The Rust `nightly` distribution channel should include the profiler runtime, by default.
32+
33+
*IMPORTANT:* If you are building the Rust compiler from the source distribution, the profiler runtime is *not* enabled in the default `config.toml.example`, and may not be enabled in your `config.toml`. Edit the `config.toml` file, and find the `profiler` feature entry. Uncomment it and set it to `true`:
3234

3335
```toml
3436
# Build the profiler runtime (required when compiling with options that depend
@@ -40,7 +42,15 @@ Then rebuild the Rust compiler (see [rustc-dev-guide-how-to-build-and-run]).
4042

4143
### Building the demangler
4244

43-
LLVM coverage reporting tools generate results that can include function names and other symbol references, and the raw coverage results report symbols using the compiler's "mangled" version of the symbol names, which can be difficult to interpret. To work around this issue, LLVM coverage tools also support a user-specified symbol name demangler. Rust's symbol name demangler can be built with:
45+
LLVM coverage reporting tools generate results that can include function names and other symbol references, and the raw coverage results report symbols using the compiler's "mangled" version of the symbol names, which can be difficult to interpret. To work around this issue, LLVM coverage tools also support a user-specified symbol name demangler.
46+
47+
One option for a Rust demangler is [`rustfilt`](https://crates.io/crates/rustfilt), which can be installed with:
48+
49+
```shell
50+
cargo install rustfilt
51+
```
52+
53+
Another option, if you are building from the Rust compiler source distribution, is to use the `rust-demangler` tool included in the Rust source distribution, which can be built with:
4454

4555
```shell
4656
$ ./x.py build rust-demangler
@@ -102,25 +112,24 @@ If `LLVM_PROFILE_FILE` contains a path to a non-existent directory, the missing
102112

103113
## Creating coverage reports
104114

105-
LLVM's tools to process coverage data and coverage maps have some version dependencies. If you encounter a version mismatch, try updating your LLVM tools, or use the LLVM tools bundled with the same Rust distrubition used to rebuild the Rust compiler (as shown in the following examples).
115+
LLVM's tools to process coverage data and coverage maps have some version dependencies. If you encounter a version mismatch, try updating your LLVM tools.
116+
117+
If you are building the Rust compiler from source, you can optionally use the bundled LLVM tools, built from source. Those tool binaries can typically be found in your build platform directory at something like: `rust/build/x86_64-unknown-linux-gnu/llvm/bin/llvm-*`. (Look for `llvm-profdata` and `llvm-cov`.)
106118

107119
Raw profiles have to be indexed before they can be used to generate coverage reports. This is done using [`llvm-profdata merge`] (which can combine multiple raw profiles and index them at the same time):
108120

109121
```shell
110-
$ $HOME/rust/build/x86_64-unknown-linux-gnu/llvm/bin/llvm-profdata merge \
111-
-sparse formatjson5.profraw -o formatjson5.profdata
122+
$ llvm-profdata merge -sparse formatjson5.profraw -o formatjson5.profdata
112123
```
113124

114125
Finally, the `.profdata` file is used, in combination with the coverage map (from the program binary) to generate coverage reports using [`llvm-cov report`]--for a coverage summaries--and [`llvm-cov show`]--to see detailed coverage of lines and regions (character ranges), overlaid on the original source code.
115126

116127
These commands have several display and filtering options. For example:
117128

118129
```shell
119-
$ $HOME/rust/build/x86_64-unknown-linux-gnu/llvm/bin/llvm-cov show \
130+
$ llvm-cov show -Xdemangler=rustfilt target/debug/examples/formatjson5 \
120131
-instr-profile=formatjson5.profdata \
121-
target/debug/examples/formatjson5 \
122132
-show-line-counts-or-regions \
123-
-Xdemangler=$HOME/rust/build/x86_64-unknown-linux-gnu/stage0-tools-bin/rust-demangler \
124133
-show-instantiations \
125134
-name=add_quoted_string
126135
```
@@ -131,9 +140,9 @@ $ $HOME/rust/build/x86_64-unknown-linux-gnu/llvm/bin/llvm-cov show \
131140

132141
Some of the more notable options in this example include:
133142

134-
* `--instr-profile=<path-to-file>.profdata` - the location of the `.profdata` file created by `llvm-profdata merge`
135-
* `target/debug/examples/formatjson5` - the binary that generated the coverage profiling data (originally as a `.profraw` file)
136-
* `--Xdemangler=<path-to>/rust-demangler` - the location of the `rust-demangler` tool
143+
* `--Xdemangler=rustfilt` - the command name or path used to demangle Rust symbols (`rustfilt` in the example, but this could also be a path to the `rust-demangler` tool)
144+
* `target/debug/examples/formatjson5` - the instrumented binary (from which to extract the coverage map)
145+
* `--instr-profile=<path-to-file>.profdata` - the location of the `.profdata` file created by `llvm-profdata merge` (from the `.profraw` file generated by the instrumented binary)
137146
* `--name=<exact-function-name>` - to show coverage for a specific function (or, consider using another filter option, such as `--name-regex=<pattern>`)
138147

139148
## Interpreting reports
@@ -155,6 +164,6 @@ Rust's implementation and workflow for source-based code coverage is based on th
155164
[`llvm.instrprof.increment`]: https://llvm.org/docs/LangRef.html#llvm-instrprof-increment-intrinsic
156165
[LLVM Code Coverage Mapping Format]: https://llvm.org/docs/CoverageMappingFormat.html
157166
[rustc-dev-guide-how-to-build-and-run]: https://rustc-dev-guide.rust-lang.org/building/how-to-build-and-run.html
158-
[`llvm-profdata merge`]: https://llvm.org/docs/CommandGuide/llvm-profdata.html#profdata-merge
159-
[`llvm-cov report`]: https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-report
160-
[`llvm-cov show`]: https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-show
167+
[`llvm-profdata merge`]: https://llvm.org/docs/CommandGuide/llvm-profdata.html#profdata-merge
168+
[`llvm-cov report`]: https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-report
169+
[`llvm-cov show`]: https://llvm.org/docs/CommandGuide/llvm-cov.html#llvm-cov-show

0 commit comments

Comments
 (0)