4

I understand two compiler flags: -ftest-coverage -fprofile-arcs needs to be passed for getting code coverage in GCC. My question is, what is the reasoning for having 2 compiler flags for getting coverage. Also, what can we get if we use them independently?.

I tried compiling a c program with only -fprofile-arcs flag. I didnt notice any differences. Was able to generate .gcno .gcda and gcov files

MSK
  • 175
  • 1
  • 10
  • 2
    Have you [read the documentation](https://gcc.gnu.org/onlinedocs/gcc-6.2.0/gcc/Instrumentation-Options.html#Instrumentation-Options) about the options, to see what they actually do? – Some programmer dude Aug 31 '16 at 06:04
  • Yes, i had gone thru the docs.. But not clear on why there are 2 flags, when one flag (-fprofile-arcs) serves the purpose – MSK Aug 31 '16 at 07:54

2 Answers2

5

If you want to use the Gcov tool to get code coverage in GCC, refer to this documentation which explicitly states:

When using gcov, you must first compile your program with two special GCC options: ‘-fprofile-arcs -ftest-coverage’. This tells the compiler to generate additional information needed by gcov (basically a flow graph of the program) and also includes additional code in the object files for generating the extra profiling information needed by gcov.

More precisely, by referring to the Instrumentation Options, my understanding about the two flags is the following:

  • -fprofile-arcs generates the information indicating how many times each branch of your program is taken; in other words, this makes your program generate extra data relative to its execution. The information is stored into .gcda files.
  • -ftest-coverage uses the information produced by -fprofile-arcs, and generates .gcno files containing control flow information, which can be used by Gcov to produce human readable .gcov files. Without the test coverage data obtained with -fprofile-arcs, you will not get anything meaningful.

The -fprofile-arcs can also be combined with other flags, such as -fbranch-probabilities (more information here). Typically when using this flag, the compiler performs optimisations to improve branch prediction based on the profiling information contained in the .gcda files. -fprofile-arcs and -fbranch-probabilities are automatically enabled when using -fprofile-generate and -fprofile-use (this is the standard way for performing Profile-Guided Optimisation with GCC).

Hope this helps!

Pyves
  • 6,333
  • 7
  • 41
  • 59
  • When `-fprofile-arcs` and `-ftest-coverage` are used for `gcov`, is it obligated to use `g++` with `-O0` (no optimization) flag? This [page](http://www.qnx.com/developers/docs/6.5.0/index.jsp?topic=%2Fcom.qnx.doc.ide.userguide%2Ftopic%2Fcoverage_Enabling_Code_Coverage_For_Make_Projects.html) uses `-O0` for this case; but I am not sure what is different if `-O3` is used instead? – Steven Lee May 27 '21 at 04:19
  • Sorry I found that [this article](https://stackoverflow.com/questions/36930207/code-coverage-with-optimization) suggest to use `-O0` for coverage test. – Steven Lee May 27 '21 at 04:33
2

If you check the documentation for -fprofile-arcs you will see that the data it generates can be used for two different things depending on other options: -ftest-coverage and -fbranch-probabilities.

So the -fprofile-arcs is to generate code that does instrumentation and saves data. Then you use either -ftest-coverage or -fbranch-probabilities to specialize the data depending on what analysis you want to perform.

It doesn't say anywhere, but from your experience it seems that GCC defaults to -ftest-coverage if none of the specialization flags are provided.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621