Gcov Explained

gcov
Genre:Code coverage
Operating System:Unix-like
License:GNU General Public License and other free software licenses

Gcov is a source code coverage analysis and statement-by-statement profiling tool. Gcov generates exact counts of the number of times each statement in a program is executed and annotates source code to add instrumentation. Gcov comes as a standard utility with the GNU Compiler Collection (GCC) suite.[1]

The gcov utility gives information on how often a program executes segments of code. It produces a copy of the source file, annotated with execution frequencies. The gcov utility does not produce any time-based data and works only on code compiled with the GCC suite. The manual claims it is not compatible with any other profiling or test coverage mechanism,[2] but it works with llvm-generated files too.[3]

Description

gcov produces a test coverage analysis of a specially instrumented program. The options -fprofile-arcs -ftest-coverage should be used to compile the program for coverage analysis (first option to record branch statistics and second to save line execution count); -fprofile-arcs should also be used to link the program. After running such program will create several files with ".bb" ".bbg" and ".da" extensions (suffixes), which can be analysed by gcov. It takes source files as command-line arguments and produces an annotated source listing. Each line of source code is prefixed with the number of times it has been executed; lines that have not been executed are prefixed with "#####".[4]

gcov creates a logfile called sourcefile.gcov which indicates how many times each line of a source file sourcefile.c has executed. This annotated source file can be used with gprof, another profiling tool, to extract timing information about the program.

Example

The following program, written in C, loops over the integers 1 to 9 and tests their divisibility with the modulus (%) operator.

  1. include

intmain (void)

To enable coverage testing the program must be compiled with the following options:$ gcc -Wall -fprofile-arcs -ftest-coverage cov.cwhere cov.c is the name of the program file. This creates an instrumented executable which contains additional instructions that record the number of times each line of the program is executed. The option adds instructions for counting the number of times individual lines are executed, while incorporates instrumentation code for each branch of the program. Branch instrumentation records how frequently different paths are taken through ‘if’ statements and other conditionals.

The executable can then be run to analyze the code and create the coverage data.

$ ./a.out

The data from the run is written to several coverage data files with the extensions ‘.bb’ ‘.bbg’ and ‘.da’ respectively in the current directory.

If the program execution varies based on the input parameters or data, it can be run multiple times and the results will accumulate in the coverage data files for overall analysis.

This data can be analyzed using the gcov command and the name of a source file:$ gcov cov.c 88.89% of 9 source lines executed in file cov.cCreating cov.c.gcov

The gcov command produces an annotated version of the original source file, with the file extension ‘.gcov’, containing counts of the number of times each line was executed: #include

int main (void) The line counts can be seen in the first column of the output. Lines which were not executed are marked with hashes ‘######’.

Command-line options

Gcov command line utility supports following options while generating annotated files from profile data:[5] [6]

Coverage summaries

Lcov is a graphical front-end for gcov. It collects gcov data for multiple source files and creates HTML pages containing the source code annotated with coverage information. It also adds overview pages for easy navigation within the file structure. Lcov supports statement, function, and branch coverage measurement.[7] There is also a Windows version.

Gcovr provides a utility for managing the use of gcov and generating summarized code coverage results. This command is inspired by the Python coverage.py package, which provides a similar utility in Python. Gcovr produces either compact human-readable summary reports, machine readable XML reports or a graphical HTML summary. The XML reports generated by gcovr can be used by Jenkins to provide graphical code coverage summaries. Gcovr supports statement and branch coverage measurement[8]

SCov is a utility that processes the intermediate text format generated by gcov (using gcov -i) to generate reports on code coverage. These reports can be a simple text report, or HTML pages with more detailed reports.[9]

See also

Notes and References

  1. How Gcov works-tool part of GCC . February 12, 2012 . dead . https://web.archive.org/web/20140409083331/http://xview.net/papers/gcov/code_coverage_gcov.pdf . April 9, 2014 .
  2. Web site: gcov man page. February 12, 2012.
  3. Web site: gcov llvm. November 12, 2022.
  4. Book: Brian J. Gough. An Introduction to GCC - for the GNU compilers gcc and g++ - Coverage testing with gcov. February 12, 2012.
  5. Web site: gnu.org. Gcov Command line options. Feb 11, 2012.
  6. Web site: linux commands. Gcov Command line options. Feb 12, 2012. dead. https://web.archive.org/web/20120523233637/http://linuxcommand.org/man_pages/gcov1.html. 2012-05-23.
  7. Web site: Lcov.
  8. Web site: Gcovr.
  9. Web site: SCov.