Skip to content

build: RFC Coverage build type #31047

Open

Description

@fanquake

Porting this issue from: hebasto#341.

It's not clear if using our Coverage build type works with Clang, or not. In the linked thread there are simultaneous claims that it "works", but also that it does not work. It seems from the discussion that even using GCC with the Coverage build type is flaky.

There's been suggestions to add a Coverage mode for Clang: hebasto#233, however adding a second way of doing things, when the current way may not work properly, and (likely?) currently isn't being used by anyone, doesn't seem like a good approach, and adds even more complication to the build system. Note that devs are already using Clang for coverage regardless of if it currently exists in the build system (cc @dergoegge, @marcofleon, @vasild).

If using Clangs native coverage mode is what is preferred/is "better", then it'd seem better for us to replace the current, more GCC focussed implementation, with one that is geared towards LLVM/Clang, if the idea is to provide something that works out-of-the-box, and is generally used by the developers working on the project.

Activity

  1. dergoegge commented on Oct 7, 2024

    @dergoegge
    Member

    I haven't tried after the transition to CMake but I always had trouble with the build in coverage build mode (e.g. make cov). Usually there was problem with path prefixes or a lcov version mismatch. The coverage reports themselves were always hard to read and I could not find any documentation on how to read them, for example:

    Screenshot from 2024-10-07 12-18-00

    How is branch coverage supposed to be interpreted from this?

    I've switched all my coverage reports to using LLVM's native coverage tooling: https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#id3. I don't use our build in command to produce coverage reports but I think it's a useful thing to have, so devs can easily produce their own coverage reports. I can recommend switching to LLVM's native coverage reporting, as that has been far less of a struggle in my experience.

  2. vasild commented on Oct 7, 2024

    @vasild
    Contributor

    For me it seems best to use each compiler's native workflow. That is: gcov+lcov if using GCC and the source based coverage if using Clang.

    Would be nice if that is integrated into the build system so it boils down to just cmake --i-want-coverage, but if not, then some good instructions in doc/ would be fine as well.

  3. fanquake commented on Oct 7, 2024

    @fanquake
    MemberAuthor

    For me it seems best to use each compiler's native workflow.

    Sure, anyone can still use whatever they like. However our build system doesn't need to (and shouldn't have to) handle all possible combinations of compiler + tooling. Providing a single convenience build type that is the same as what is used by the majority of the developers seems fine, however I don't think we need to natively support anything outside of that, given it's unlikely to be tested/used, or work properly, and just futher complicates our build system.

  4. vasild commented on Oct 7, 2024

    @vasild
    Contributor

    our build system doesn't need to (and shouldn't have to) handle all possible combinations of compiler + tooling

    It is just two:

    1. gcc compiler + gcov/lcov and
    2. clang compiler + llvm-cov (source based).

    (I think it is a waste of time and we should ignore a third option where we try to use the gcov/lcov workflow with clang).

    Providing a single convenience build type that is the same as what is used by the majority of the developers seems fine

    I do not object this, but then if only 1. or only 2. is supported then the coverage build should produce an error if the compiler is the other one.

  5. fanquake commented on Oct 7, 2024

    @fanquake
    MemberAuthor

    It is just two:

    It's not though, because gcc compiler + gcov/lcov already encompasses more than 1 thing. i.e supporting different versions of gcov/lcov across releases.

  6. marcofleon commented on Oct 7, 2024

    @marcofleon
    Contributor

    I don't have a strong preference for which one is supported by the build system. I happened to learn about llvm-cov before coming upon the coverage section in the docs, so that's what I've been using on both macOS and Linux. It's been working well for me so far.

  7. fanquake commented on Oct 10, 2024

    @fanquake
    MemberAuthor

    cc @maflcko. Do you have an opinion here?

  8. maflcko commented on Oct 10, 2024

    @maflcko
    Member

    I liked the summary stats per folder produced by the current approach, but if others like other stuff better, I don't mind.

  9. dergoegge commented on Oct 10, 2024

    @dergoegge
    Member

    I liked the summary stats per folder produced by the current approach, but if others like other stuff better, I don't mind.

    The LLVM approach also supports that:

    Screenshot from 2024-10-10 13-52-58

    edit: this is possible with the -show-directory-coverage option for llvm-cov show.

  10. vasild commented on Oct 10, 2024

    @vasild
    Contributor

    I liked the summary stats per folder produced by the current approach, but if others like other stuff better, I don't mind.

    Maybe I misunderstand, but somehow the above implies that we have to commit to one and give up on the other method. But one can create gcc or clang coverages regardless of what is integrated into the cmake build system. That integration would be just a convenience and we could have no integration, or just gcc (the current master), or just clang or both gcc and clang.

  11. maflcko commented on Oct 10, 2024

    @maflcko
    Member

    Yeah. I guess I don't really mind either way. As long as there is at least one documented, tested and working solution.

  12. maflcko commented on Oct 28, 2024

    @maflcko
    Member
  13. fanquake commented on Nov 19, 2024

    @fanquake
    MemberAuthor

    Apparently code coverage on oss-fuzz has been broken since CMake. See https://issues.oss-fuzz.com/issues/379122777. Full logs (https://oss-fuzz-build-logs.storage.googleapis.com/log-6b12c5ff-d6c4-441c-a10f-2dffff96e82c.txt):

    Step #7: �[0m�[0;31merror: /workspace/out/libfuzzer-coverage-x86_64/src/bitcoin-core/build_fuzz/src/wallet/wallet/walletdb.h: No such file or directory
    Step #7: �[0m�[0;31mwarning: The file '/src/bitcoin-core/build_fuzz/src/wallet/wallet/walletdb.h' isn't covered.
    Step #7: �[0m�[0;31merror: /workspace/out/libfuzzer-coverage-x86_64/src/bitcoin-core/build_fuzz/src/wallet/wallet/rpc/wallet.cpp: No such file or directory
    Step #7: �[0m�[0;31mwarning: The file '/src/bitcoin-core/build_fuzz/src/wallet/wallet/rpc/wallet.cpp' isn't covered.
    Step #7: �[0m�[0;31merror: /workspace/out/libfuzzer-coverage-x86_64/src/bitcoin-core/build_fuzz/src/wallet/util/task_runner.h: No such file or directory
    Step #7: �[0m�[0;31mwarning: The file '/src/bitcoin-core/build_fuzz/src/wallet/util/task_runner.h' isn't covered.
    Step #7: �[0mwarning: 60 functions have mismatched data
    Step #7: [2024-11-17 06:58:59,574 DEBUG] Finished generating per-file code coverage summary.
    Step #7: [2024-11-17 06:58:59,574 DEBUG] Generating file view html index file as: "/workspace/out/libfuzzer-coverage-x86_64/report/linux/file_view_index.html".
    Step #7: Traceback (most recent call last):
    Step #7:   File "/opt/code_coverage/coverage_utils.py", line 829, in <module>
    Step #7:     sys.exit(Main())
    Step #7:   File "/opt/code_coverage/coverage_utils.py", line 823, in Main
    Step #7:     return _CmdPostProcess(args)
    Step #7:   File "/opt/code_coverage/coverage_utils.py", line 780, in _CmdPostProcess
    Step #7:     processor.PrepareHtmlReport()
    Step #7:   File "/opt/code_coverage/coverage_utils.py", line 577, in PrepareHtmlReport
    Step #7:     self.GenerateFileViewHtmlIndexFile(per_file_coverage_summary,
    Step #7:   File "/opt/code_coverage/coverage_utils.py", line 450, in GenerateFileViewHtmlIndexFile
    Step #7:     self.GetCoverageHtmlReportPathForFile(file_path),
    Step #7:   File "/opt/code_coverage/coverage_utils.py", line 422, in GetCoverageHtmlReportPathForFile
    Step #7:     assert os.path.isfile(
    Step #7: AssertionError: "/src/bitcoin-core/build_fuzz/crc32c/include/crc32c/crc32c.h" is not a file.
    Step #7: ********************************************************************************
    Step #7: Code coverage report generation failed.
    Step #7: To reproduce, run:
    Step #7: python infra/helper.py build_image bitcoin-core
    Step #7: python infra/helper.py build_fuzzers --sanitizer coverage bitcoin-core
    Step #7: python infra/helper.py coverage bitcoin-core
    Step #7: ********************************************************************************
    Finished Step #7
    ERROR
  14. 8 remaining items

  15. fanquake commented on Feb 14, 2025

    @fanquake
    MemberAuthor

    Looking at Corecheck, does it use our Coverage.cmake? I can't find it being used anywhere there, seems like only calls to gcovr directly.

  16. maflcko commented on Feb 17, 2025

    @maflcko
    Member

    It is using the coverage build type, which this issue is about. In any case, all mentioned projects are open-source, and I am happy to review pull requests, if there are any. Removing the cmake scripts may be fine, but I don't see it as a release blocker.

  17. removed this from the 29.0 milestone on Feb 21, 2025
  18. maflcko commented on Feb 21, 2025

    @maflcko
    Member

    Removing the milestone for now. I am happy to review pull requests or suggestions, if there are any.

  19. hebasto commented on Feb 21, 2025

    @hebasto
    Member

    From the OP:

    It's not clear if using our Coverage build type works with Clang, or not.

    There are a few points to take into consideration:

    1. Modern LLVM's coverage tool emulates GCC's gcov version 4.2.0, which is obviously outdated.
    2. Recent versions of the lcov tool no longer support GCC/gcov versions older than 9.x. For example:
    lcov: WARNING: (unsupported) Function begin/end line exclusions not supported with this version of GCC/gcov; require gcc/9 or newer: attempting to derive function end lines - see lcovrc man entry for 'derive_function_end_line'.
    

    Thus, I must conclude that the answer is "No."

    The current "Coverage" build type should be considered and documented as GCC-only.

  20. maflcko commented on Jun 5, 2025

    @maflcko
    Member

    Can this be closed as fixed by 31933?

  21. fanquake commented on Jun 6, 2025

    @fanquake
    MemberAuthor

    I don't think so. If anything, it's even more of an indication that we should swap what has been implemented for GCC (and still not documented as such, leading to i.e #32571) for LLVM / Clang, which is clearly what (the majority of) project contributors are using, or wanting to use.

  22. vasild commented on Jun 12, 2025

    @vasild
    Contributor

    My impression is that a non-trivial part of the developers use Linux with its native compiler GCC. The current GCC-oriented method we have does work with GCC, contrary to what the OP suggests "the current way may not work properly".

    So, to me it looks like that removing the current GCC-oriented way could affect some developers in a negative way. I could be wrong though. Maybe none of the Linux people generate code coverage? 👀 Or they bother to install Clang additionally and use that?

    Lets see if a quick poll on IRC would help bring some clarity to this: https://www.erisian.com.au/bitcoin-core-dev/log-2025-06-12.html#l-69

  23. pinned this issue on Jun 12, 2025
  24. unpinned this issue on Jun 12, 2025
  25. vasild commented on Jun 13, 2025

    @vasild
    Contributor

    If the people that attended the IRC meeting is a representative sample, then I guess it might be ok to ditch the current GCC-oriented coverage "integration" into CMake and replace it with Clang's (which is already documented at doc/developer-notes.md / Using LLVM/Clang toolchain), instead of having to maintain both.

    I guess there will be more feedback once there is a PR which does that.

  26. maflcko commented on Jun 13, 2025

    @maflcko
    Member

    I guess there will be more feedback once there is a PR which does that.

    There already is (well not a replacement, but close enough), see #31394 (comment)

  27. nkaretnikov commented on May 29, 2026

    @nkaretnikov

    I tried getting lcov working using Coverage.cmake yesterday on de92545 and I couldn't get it to work.
    Both gcc and clang built the project fine, but the lcov step was generating errors.

    shell.nix env

    gcc is implicitly part of this environment, so I didn't need to add it.
    The exported vars should not be a problem because I explicitly set compilers later.

    with import <nixpkgs> {};
    
    mkShell {
      packages = [
        cmake
        llvmPackages_latest.clang
        llvmPackages_latest.lld
        llvmPackages_latest.llvm
        sqlite
        capnproto
        boost
        pkg-config
        libevent
        python3
        lcov
      ];
    
      shellHook = ''
        export CC=clang
        export CXX=clang++
      '';
    }
    gcc/clang + lcov The build for clang was similar, you just need to adjust the C/CXX compiler variables, but I wouldn't expect it to work with lcov if gcc doesn't, so I'm including the gcc version.
    .PHONY: clean build shell test cov
    
    NUM_JOBS=1
    
    # To run:
    # nix-shell --pure --run 'make clean build && exec $SHELL'
    
    shell:
    	nix-shell --pure
    
    # https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#using-lcov
    build:
    	cmake -B build \
    		-DCMAKE_BUILD_TYPE=Coverage \
    		-DCMAKE_C_COMPILER=gcc \
    		-DCMAKE_CXX_COMPILER=g++
    	cmake --build build -j ${NUM_JOBS}
    
    test:
    	cmake -DJOBS="${NUM_JOBS}" -P build/Coverage.cmake
    	@printf '%s\n' "Coverage report: build/total.coverage/index.html"
    
    clean:
    	rm -rf build
    clang + llvm-cov
    .PHONY: clean build shell test cov
    
    NUM_JOBS=1
    
    # To run:
    # nix-shell --pure --run 'make clean build && exec $SHELL'
    
    shell:
    	nix-shell --pure
    
    # https://github.com/bitcoin/bitcoin/blob/master/doc/developer-notes.md#using-llvmclang-toolchain
    build:
    	cmake -B build -DCMAKE_C_COMPILER="clang" \
    	-DCMAKE_CXX_COMPILER="clang++" \
    	-DAPPEND_CFLAGS="-fprofile-instr-generate -fcoverage-mapping" \
    	-DAPPEND_CXXFLAGS="-fprofile-instr-generate -fcoverage-mapping" \
    	-DAPPEND_LDFLAGS="-fprofile-instr-generate -fcoverage-mapping"
    	cmake --build build -j ${NUM_JOBS}
    
    test:
    	mkdir -p build/raw_profile_data
    	LLVM_PROFILE_FILE="${CURDIR}/build/raw_profile_data/%m_%p.profraw" ctest --test-dir build -j ${NUM_JOBS}
    	LLVM_PROFILE_FILE="${CURDIR}/build/raw_profile_data/%m_%p.profraw" build/test/functional/test_runner.py -j ${NUM_JOBS}
    	find build/raw_profile_data -name "*.profraw" > build/raw_profile_data_files.txt
    	llvm-profdata merge -f build/raw_profile_data_files.txt -o build/coverage.profdata
    
    cov:
    	llvm-cov show \
    	    --object=build/bin/test_bitcoin \
    	    --object=build/bin/bitcoind \
    	    -Xdemangler=llvm-cxxfilt \
    	    --instr-profile=build/coverage.profdata \
    	    --ignore-filename-regex="src/crc32c/|src/leveldb/|src/minisketch/|src/secp256k1/|src/test/" \
    	    --format=html \
    	    --show-instantiation-summary \
    	    --show-line-counts-or-regions \
    	    --show-expansions \
    	    --output-dir=build/coverage_report \
    	    --project-title="Bitcoin Core Coverage Report"
    
    clean:
    	rm -rf build

    Based on this experience, I recommend switching to clang + llvm-cov in Coverage.cmake. This configuration also has an advantage since clang and coverage are often used in fuzzing, with libFuzzer and sanitizers.

  28. maflcko commented on Sep 1, 2026

    @maflcko
    Member

    Not sure what the current status here is. Looks like there was an IRC meeting that concluded it would be fine to remove the GCC coverage cmake type. Given that there are docs for llmv, it seems fine to fully remove it without replacement, but some places need to be updated first, see #31047 (comment) for a list.

  29. maflcko commented on Sep 1, 2026

    @maflcko
    Member

    Only slightly related: Both gcov and llvm-cov seem filled with bugs, see the thread in bitcoin-core/qa-assets#292 listing a few bugs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions