10

I have several unit tests for an application, each of which is capable of generating .gcda files. I would like to be able to generate unified .gcda files which represent the coverage of my test suite as a whole. There doesn't appear to be an easy way to do this, but I could be wrong, and that's why I'm asking.

With gcov, is it possible to merge to .gcda files? was previously asked and the solution was to convert to lcov .info files and merge that way. If possible, I would like the output of the merge operation to still be a single .gcda file so that I'm not forced to use lcov.

Community
  • 1
  • 1
Kanak
  • 101
  • 1
  • 3

3 Answers3

4

There is code to merge .gcda files but unfortunately it's buried in libgcov.a which builds as part of gcc. If you exit (or in any way invoke __gcov_flush()) a program compiled with coverage it will actually detect pre-existing .gcda files, load them, aggregate their data with the running program's data, and save it back out. I don't know of any tool which provides that functionality at the command line. Even with libgcov.a you probably have no useful hooks to do what you want and would have to take the source from the gcc-core distribution and modify it.

What I have done in the past is just extract all the data to annotated source (.gcov) and aggregate at that level. The .gcda format is capable of storing a lot more than line coverage information (eg branch counts) and the libgcov.a aggregation knows how to combine those (for some it's not as simple as summation).

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
2

Gcov package comes with gcov-tool executable which is capable of merging gcda files:

gcov-tool merge dir1 dir2

(position of .gcda files within dirN must be identical). Resulting .gcda will be stored in the merged_profile directory (can be overriden with -o flag).

Gcov-tool is mainly meant to be used with series of .gcda files generated with GCOV_PREFIX or -fprofile-dir=XXX.%p.

Gcov-tool can merge only two profiles at a time but you can use a wrapper gcov-tool-many to circumvent this.

yugr
  • 19,769
  • 3
  • 51
  • 96
  • Thanks for the pointer! What does "position must be identical" mean? If I happen to have a directory structure with the `.gcda` distributed across it, it expects the files with the same names to be at the same location in that tree? – thoni56 Feb 20 '22 at 08:00
  • 1
    @thoni56 yes, exactly: files with same names must be in same relative locations inside combined directories. – yugr Feb 20 '22 at 08:38
  • 2
    It isn't documented so it might be risk to rely on, but it seems that it is possible to incrementally merge new sets into the original, like `gcov-tool merge dir1 dir -o dir ; gcov-tool merge dir2 dir -o dir; gcov-tool merge dir3 dir -o dir` simplifying this quite a bit. (Not to take away any merits from `gcov-tool-many`). – thoni56 Feb 20 '22 at 14:27
  • @thoni56 very nice! – yugr Feb 20 '22 at 17:54
1

I have just created a test project which shows, AFAIKT, that when a single test application is run successively with different parameters, then the gcda files are updated between runs and running gcov once (I use gcovr.py) produces aggregate coverage information.

amon
  • 57,091
  • 2
  • 89
  • 149
quamrana
  • 37,849
  • 12
  • 53
  • 71