Skip to content

Latest commit

 

History

History
88 lines (54 loc) · 3.05 KB

coverage.md

File metadata and controls

88 lines (54 loc) · 3.05 KB

coverage

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.

profile environment

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

bug issues

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

source-based coverage

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/

gcov-based 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/

The difference between source-based coverage and gcov-based coverage

  1. RUSTFLAG set by source-based coverage and gcov-based coverage are different.
  2. source-based coverage has no branch data.
  3. Our project gcov-based coverage can't be run under Windows, and a library fails to build.

image

Reference:

rust-code-coverage-sample

source_based_code_coverage

grcov