2.5.1 Using Gcov to Analyze Code Coverage

Gcov is an open-source code-coverage tool.

To use Gcov, perform the following steps:

  1. Compile the code with the -fprofile-arcs and -ftest-coverage flags, for example:

    $ gcc -fprofile-arcs -ftest-coverage test.c

    The -ftest-coverage flag causes gcc to add instrumentation codes to the binary.

  2. Run the instrumented binary and perform functional testing.

    Running the binary generates profile output. For each source file that you compiled with -fprofile-arcs, a .gcda profile output file is created in the object file directory.

  3. Generate a report file based on the data that is stored in the profile output files:

    $ gcov test.c
    56.0% of 110 source lines executed in file test.c
    Creating test.c.gcov.

For more information about using gcov, see the gcov(1) manual page.