Is there a way to merge two .gcda files into one?
Asked Answered
G

3

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.

Grisham answered 28/6, 2011 at 22:1 Comment(0)
G
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).

Grafting answered 28/6, 2011 at 22:18 Comment(0)
C
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.

Conard answered 7/7, 2011 at 12:48 Comment(0)
T
1

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.

Toffic answered 7/1, 2022 at 7:48 Comment(4)
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?Furfural
@Furfural yes, exactly: files with same names must be in same relative locations inside combined directories.Toffic
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).Furfural
@Furfural very nice!Toffic

© 2022 - 2024 — McMap. All rights reserved.