如何为rustc编译器自身收集代码覆盖率信息?
为rustc编译器收集代码覆盖率的问题及解决思路
问题背景
尝试为rustc编译器自身收集代码覆盖率信息,目的是了解编译过程中执行的代码部分,确保关键区域测试覆盖全面。但直接设置RUSTFLAGS="-C instrument-coverage"后编译出错,不确定是否需要修改构建脚本/配置,也想知道适用的覆盖率工具或流程。
已做配置
config.toml修改内容:
assertions = true profiler = true prefix = "/path/to/install" sysconfdir = "etc" llvm-config = "/path/to/self-compile-llvm-config" llvm-filecheck = "/path/to/self-compile-FileCheck"
- 设置环境变量:
export RUSTFLAGS="-C instrument-coverage"
- 执行构建命令:
./x build
遇到的错误
Compiling cc v1.0.99 Compiling std v0.0.0 (/path/to/rust/library/std) warning: -C instrument-coverage requires symbol mangling version `v0`, but `-C symbol-mangling-version=legacy` was specified Compiling profiler_builtins v0.0.0 (/path/to/rust/library/profiler_builtins) error: `profiler_builtins` crate (required by compiler options) is not compatible with crate attribute `#![no_core]` error[E0463]: can't find crate for `profiler_builtins` | = note: the compiler may have been built without the profiler runtime For more information about this error, try `rustc --explain E0463`. warning: `core` (lib) generated 1 warning error: could not compile `core` (lib) due to 2 previous errors; 1 warning emitted warning: build failed, waiting for other jobs to finish... warning: profiler_builtins@0.0.0: In file included from /path/to/rust/src/llvm-project/compiler-rt/lib/profile/InstrProfiling.h:12, warning: profiler_builtins@0.0.0: from /path/to/rust/src/llvm-project/compiler-rt/lib/profile/InstrProfilingFile.c:34: warning: profiler_builtins@0.0.0: /path/to/rust/src/llvm-project/compiler-rt/lib/profile/InstrProfilingFile.c: In function ‘doProfileMerging’: warning: profiler_builtins@0.0.0: /path/to/rust/src/llvm-project/compiler-rt/lib/profile/InstrProfilingPort.h:33:37: warning: ignoring return value of ‘ftruncate’ declared with attribute ‘warn_unused_result’ [-Wunused-result] warning: profiler_builtins@0.0.0: 33 | #define COMPILER_RT_FTRUNCATE(f, l) ftruncate(fileno(f), l) warning: profiler_builtins@0.0.0: | ^~~~~~~~~~~~~~~~~~~~~~~ warning: profiler_builtins@0.0.0: /path/to/rust/src/llvm-project/compiler-rt/lib/profile/InstrProfilingFile.c:445:9: note: in expansion of macro ‘COMPILER_RT_FTRUNCATE’ warning: profiler_builtins@0.0.0: 445 | (void)COMPILER_RT_FTRUNCATE(ProfileFile, warning: profiler_builtins@0.0.0: | ^~~~~~~~~~~~~~~~~~~~~ Build completed unsuccessfully in 0:00:31
解决思路和步骤
1. 错误根源分析
- 覆盖率插桩要求符号 mangling 版本为
v0,但rustc默认构建使用legacy版本 profiler_builtinscrate 带有#![no_core]属性,与覆盖率插桩依赖的组件冲突,且编译器未正确加载profiler运行时
2. 调整构建配置与参数
不要全局设置RUSTFLAGS,避免影响core、profiler_builtins等底层核心库的编译,改用rustc构建系统的针对性参数:
修改config.toml
添加或调整以下配置项:
[rust] # 适配覆盖率要求,启用v0符号mangling symbol-mangling-version = "v0" # 明确启用profiler和coverage组件 profiler = true coverage = true
针对性传递覆盖率编译选项
仅对最终的stage2 rustc添加覆盖率插桩,执行构建命令:
./x build --stage 2 --rustc-extra-flags="-C instrument-coverage"
3. 收集覆盖率数据并生成报告
编译完成后,运行rustc编译目标代码或执行测试套件,会生成.profraw格式的原始覆盖率文件,后续通过以下步骤生成报告:
合并原始覆盖率数据
llvm-profdata merge -output=rustc.profdata *.profraw
生成lcov格式报告
llvm-cov export -format=lcov -instr-profile=rustc.profdata ./build/x86_64-unknown-linux-gnu/stage2/bin/rustc > rustc_coverage.lcov
生成HTML可视化报告
llvm-cov show -format=html -instr-profile=rustc.profdata ./build/x86_64-unknown-linux-gnu/stage2/bin/rustc -output-dir coverage_report
4. 注意事项
- 必须使用rustc构建时配套的
llvm-profdata和llvm-cov工具,避免版本不兼容问题 - 若要收集rustc测试套件的覆盖率,可执行
./x test --stage 2 --rustc-extra-flags="-C instrument-coverage",再处理生成的.profraw文件 - 不要对stage0、stage1的编译器添加覆盖率插桩,会导致底层依赖库编译冲突
内容的提问来源于stack exchange,提问作者Surfer
相关产品推荐
相关产品推荐

