The Rust compiler includes two code coverage implementations:
A GCC-compatible, gcov-based coverage implementation, enabled with -Z profile, which derives coverage data based on DebugInfo.
A source-based code coverage implementation, enabled with -C instrument-coverage, which uses LLVM's native, efficient coverage instrumentation to generate very precise coverage data.
instrument-coverage environment
grcov has a bug in Windows, please run the command line with administrator
First of all, install grcov
cargo install grcov
Second, install the llvm-tools Rust component (llvm-tools-preview
for now, it might become llvm-tools
soon):
rustup component add llvm-tools-preview
Project is enables source-based coverage
# Export the flags needed to instrument the program to collect code coverage.
export RUSTFLAGS="-Zinstrument-coverage"
export LLVM_PROFILE_FILE="spdm-rs-%p%m.profraw"
# Build the program
cargo build -p spdm-responder-emu -p spdm-requester-emu
# Run the program
cargo run -p spdm-responder-emu &
cargo run -p spdm-requester-emu
# Generate a HTML report in the ./target/debug/gcov_coverage/ directory.
grcov . -s . --binary-path ./target/debug/ -t html --branch --ignore-not-existing -o ./target/debug/source_coverage/
Project is disables gcov-based coverage
# Export the flags needed to instrument the program to collect code coverage.
export CARGO_INCREMENTAL=0
export RUSTDOCFLAGS="-Cpanic=abort"
export RUSTFLAGS="-Zprofile -Ccodegen-units=1 -Copt-level=0 -Clink-dead-code -Coverflow-checks=off -Zpanic_abort_tests -Cpanic=abort"
# Build the program
cargo build -p spdm-responder-emu -p spdm-requester-emu
# Run the program
cargo run -p spdm-responder-emu &
cargo run -p spdm-requester-emu
# Generate a HTML report in the ./target/debug/gcov_coverage/ directory.
grcov . -s . --binary-path ./target/debug/ -t html --branch --ignore-not-existing -o ./target/debug/gcov_coverage/
- RUSTFLAG set by source-based coverage and gcov-based coverage are different.
- source-based coverage has no branch data.
- Our project gcov-based coverage can't be run under Windows, and a library fails to build.
Reference: