# How to collect code coverage for the rustc compiler itself?

**URL:** <https://internals.rust-lang.org/t/how-to-collect-code-coverage-for-the-rustc-compiler-itself/21744>\
**Category:** compiler\
**Created:** [October 22, 2024, 8:44am UTC](https://internals.rust-lang.org/t/how-to-collect-code-coverage-for-the-rustc-compiler-itself/21744 "2024-10-22T08:44:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Surfer](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Surfer](https://internals.rust-lang.org/u/Surfer)\
**Post date:** [October 22, 2024, 8:44am UTC](https://internals.rust-lang.org/t/how-to-collect-code-coverage-for-the-rustc-compiler-itself/21744/1 "2024-10-22T08:44:10Z")

</div>

I am trying to collect coverage information for the `rustc` compiler itself. Here's what I've done so far:

- Modified `config.toml` with settings like `assertions = true`, `profiler = true`, and `llvm-config` paths.
- Set the environment variable: `export RUSTFLAGS="-C instrument-coverage"`.
- Ran `./x build` but encountered errors including:
  - `profiler_builtins` crate not compatible with `#![no_core]`
  - Symbol mangling warnings.

Has anyone successfully gathered coverage for `rustc`? Any recommended steps or configurations? The same question has been posted on Stack Overflow here: [https://stackoverflow.com/questions/79112200/how-to-collect-code-coverage-for-the-rustc-compiler-itself](https://stackoverflow.com/questions/79112200/how-to-collect-code-coverage-for-the-rustc-compiler-itself)

---

<div class="post-metadata">

**Author:** ![shao-hua-li](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/shao-hua-li/32/12650_2.png) [@shao-hua-li](https://internals.rust-lang.org/u/shao-hua-li)\
**Post date:** [October 22, 2024, 2:23pm UTC](https://internals.rust-lang.org/t/how-to-collect-code-coverage-for-the-rustc-compiler-itself/21744/2 "2024-10-22T14:23:55Z")

</div>

You can try to use this fork for collecting rustc's coverage:

> **[GitHub - rustsmith/rust: Empowering everyone to build reliable and...](https://github.com/rustsmith/rust)**
>
> Empowering everyone to build reliable and efficient software.

But I believe we definitely should have an official coverage collection patch for rustc.

---

<div class="post-metadata">

**Author:** ![Surfer](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Surfer](https://internals.rust-lang.org/u/Surfer)\
**Post date:** [October 24, 2024, 3:49pm UTC](https://internals.rust-lang.org/t/how-to-collect-code-coverage-for-the-rustc-compiler-itself/21744/3 "2024-10-24T15:49:11Z")

</div>

I’ve been attempting to measure code coverage in `rustc` using a methodology inspired by RustSmith. Below are the steps I’ve followed, along with the issue I’m encountering:

1. I modified the Rust source (commit `b8bb296`), specifically in `src/bootstrap/src/core/builder/cargo.rs` at line 551, by adding the following flags:

2. This allowed me to successfully run `./x build && ./x install`.

3. I deleted all `.profraw` files before testing.

4. I ran the `rustc` binary from the installation directory, which successfully generated a `.profraw` file.

5. I used the following command to merge the profile data:

6. Then, I used `llvm-cov` to generate a coverage report:

However, the generated coverage report only includes 36 lines corresponding to the `main` function in `rustc`, which is clearly incorrect. Even when I use `rustc` to compile a simple `.rs` file, the report still only shows 36 lines, with 100% coverage.

Am I using llvm-cov incorrectly?

---

<div class="post-metadata">

**Author:** ![Vorpal](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@Vorpal](https://internals.rust-lang.org/u/Vorpal)\
**Post date:** [October 24, 2024, 8:59pm UTC](https://internals.rust-lang.org/t/how-to-collect-code-coverage-for-the-rustc-compiler-itself/21744/4 "2024-10-24T20:59:18Z")

</div>

`Zprofile` is apparently broken (and might get removed): [Remove support for `-Zprofile` (gcov-style coverage instrumentation) by Zalathar · Pull Request #131829 · rust-lang/rust · GitHub](https://github.com/rust-lang/rust/pull/131829)

(I don't know how to get coverage to work though)

---

<div class="post-metadata">

**Author:** ![Surfer](https://avatars.discourse-cdn.com/v4/letter/s/d9b06d/32.png) [@Surfer](https://internals.rust-lang.org/u/Surfer)\
**Post date:** [October 26, 2024, 11:57am UTC](https://internals.rust-lang.org/t/how-to-collect-code-coverage-for-the-rustc-compiler-itself/21744/5 "2024-10-26T11:57:55Z")

</div>

Thank you, everyone. The issue has been resolved, and the detailed steps are in the previous Stack Overflow link. It would be great if an official method for coverage collection could be provided in the future, similar to GCC, where enabling a flag would be sufficient without needing to modify the code.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [April 19, 2026, 11:58am UTC](https://internals.rust-lang.org/t/how-to-collect-code-coverage-for-the-rustc-compiler-itself/21744/6 "2026-04-19T11:58:10Z")

</div>

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
