This crate provides code coverage support for no_std
and embedded programs.
This is done through a modified version of the LLVM profiling runtime (normally part of compiler-rt) from which all dependencies on libc have been removed.
All types of instrumentation using the LLVM profiling runtime are supported:
- Rust code coverage with
-Zinstrument-coverage
. - Rust profile-guided optimization with
-Cprofile-generate
. - Clang code coverage with
-fprofile-instr-generate -fcoverage-mapping
. - Clang profile-guided optimization with
-fprofile-instr-generate
. - Clang LLVM IR profile-guided optimization with
-fprofile-generate
.
Note that to profile both C and Rust code at the same time you must use Clang with the same LLVM version as the LLVM used by rustc.
Usage
Note: This crate requires a recent nightly compiler.
- Ensure that the following environment variables are set up:
Note that these flags also apply to build-dependencies and proc macros by default. This can be worked around by explicitly specifying a target when invoking cargo:
# Applies RUSTFLAGS to everything
# Doesn't apply RUSTFLAGS to build dependencies and proc macros
- Add the
minicov
crate as a dependency to your program:
[]
= "0.2"
- Before your program exits, call
minicov::capture_coverage
which returns aVec<u8>
and dump its contents to a file with the.profraw
extension:
fn main() {
// ...
let coverage = minicov::capture_coverage();
std::fs::write("output.profraw", coverage).unwrap();
}
If your program is running on a different system than your build system then you will need to transfer this file back to your build system.
- Use a tool such as grcov or llvm-cov to generate a human-readable coverage report: