You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/doc/unstable-book/src/compiler-flags/source-based-code-coverage.md
+24-15Lines changed: 24 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -26,9 +26,11 @@ When running a coverage-instrumented program, the counter values are written to
26
26
27
27
## Enable coverage profiling in the Rust compiler
28
28
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.
30
30
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`:
32
34
33
35
```toml
34
36
# 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]).
40
42
41
43
### Building the demangler
42
44
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:
44
54
45
55
```shell
46
56
$ ./x.py build rust-demangler
@@ -102,25 +112,24 @@ If `LLVM_PROFILE_FILE` contains a path to a non-existent directory, the missing
102
112
103
113
## Creating coverage reports
104
114
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`.)
106
118
107
119
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):
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.
115
126
116
127
These commands have several display and filtering options. For example:
117
128
118
129
```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 \
@@ -131,9 +140,9 @@ $ $HOME/rust/build/x86_64-unknown-linux-gnu/llvm/bin/llvm-cov show \
131
140
132
141
Some of the more notable options in this example include:
133
142
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)
137
146
*`--name=<exact-function-name>` - to show coverage for a specific function (or, consider using another filter option, such as `--name-regex=<pattern>`)
138
147
139
148
## Interpreting reports
@@ -155,6 +164,6 @@ Rust's implementation and workflow for source-based code coverage is based on th
0 commit comments