GCOV(1)

๐Ÿ“ฆ version 15.2.0, in ubuntu questing

โœจ NAME โœจ

Meet gcov, the coverage testing tool! ๐Ÿ’– It's a test coverage program that's basically your code's best friend.


๐Ÿ’… SYNOPSIS ๐Ÿ’…

gcov [-v|--version] [-h|--help]
     [-a|--all-blocks]
     [-b|--branch-probabilities]
     [-c|--branch-counts]
     [-g|--conditions]
     [-e|--prime-paths]
     [--prime-paths-lines[=type]]
     [--prime-paths-source[=type]]
     [-d|--display-progress]
     [-f|--function-summaries]
     [--include regex]
     [--exclude regex]
     [-j|--json-format]
     [-H|--human-readable]
     [-k|--use-colors]
     [-l|--long-file-names]
     [-m|--demangled-names]
     [-M|--filter-on-demangled]
     [-n|--no-output]
     [-o|--object-directory directory|file]
     [-p|--preserve-paths]
     [-q|--use-hotness-colors]
     [-r|--relative-only]
     [-s|--source-prefix directory]
     [-t|--stdout]
     [-u|--unconditional-branches]
     [-x|--hash-filenames]
     files

๐ŸŽ€ DESCRIPTION ๐ŸŽ€

okay so gcov is a test coverage program. ๐Ÿฆ‹ You use it alongside GCC to analyze your programs, so you can make more efficient, faster running code and discover the untested parts of your program. It's giving optimization queen energy. ๐Ÿ‘‘

You can also use gcov as a profiling tool to figure out where your optimization efforts will hit your code the hardest. And you can pair it with the other profiling tool, gprof, to see which parts of your code are using the greatest amount of computing time. โญ

Profiling tools help you analyze how your code performs. ๐ŸŒธ When you use a profiler like gcov or gprof, you can find out some basic performance stats, like:

  • how often each line of code executes
  • what lines of code are actually executed
  • how much computing time each section of code uses

Once you know these things about how your code behaves when it's compiled, you can look at each module and see which ones should be optimized. gcov helps you decide where to put your optimization energy. ๐Ÿ’œ

Software developers also use coverage testing together with testsuites, to make sure software is actually good enough for a release. ๐Ÿซถ Testsuites can verify that a program works as expected, while a coverage program checks how much of the program is exercised by the testsuite. Developers can then figure out what kinds of test cases need to be added to the testsuites, so you get both better testing and a better final product. โœจ

โš ๏ธ Heads up, bestie: you should compile your code without optimization if you plan to use gcov. Optimization can combine some lines of code into one function, which may not give you enough information to spot the "hot spots" where the code is using a great deal of computer time. ๐Ÿ˜ญ

Also, because gcov accumulates statistics by line (at the lowest resolution), it works best with a programming style that puts only one statement on each line. ๐Ÿ“ If you use complicated macros that expand into loops or other control structures, the statistics are less helpful, since they only report on the line where the macro call appears. ๐Ÿ™ˆ If your complex macros behave like functions, you can swap them out for inline functions to solve this problem.

gcov creates a logfile called sourcefile.gcov which shows how many times each line of a source file sourcefile.c has executed. ๐Ÿงš You can use these logfiles along with gprof to fine-tune the performance of your programs. gprof gives timing information you can combine with what you get from gcov. ๐Ÿ’–

gcov only works on code compiled with GCC. ๐Ÿ’€ It is not compatible with any other profiling or test coverage mechanism.


๐Ÿฆ‹ OPTIONS ๐Ÿฆ‹

  • -a, --all-blocks โ€” Writes individual execution counts for every basic block. โœจ Normally gcov only outputs execution counts for the main blocks of a line. With this option, you can tell whether blocks within a single line aren't being executed.
  • -b, --branch-probabilities โ€” Writes branch frequencies to the output file and branch summary info to the standard output. ๐ŸŒˆ This lets you see how often each branch in your program was taken. Unconditional branches won't be shown unless the -u option is given.
  • -c, --branch-counts โ€” Writes branch frequencies as the number of branches taken, instead of the percentage of branches taken. ๐Ÿ”ข
  • -g, --conditions โ€” Writes condition coverage to the output file and condition summary info to the standard output. ๐Ÿ”ฅ This lets you see whether the conditions in your program had an independent effect on the outcome of the boolean expression at least once (modified condition/decision coverage). This requires you to compile the source with -fcondition-coverage.
  • -e, --prime-paths โ€” Writes path coverage to the output file and path summary info to the standard output. ๐Ÿ›ค๏ธ This lets you see how many prime paths were taken at least once. A path is a sequence of basic blocks. A path is simple if it has no repeated blocks (no loops) except maybe the first and last block, and it is prime if it is a simple path of maximal length. For the regular output, this option only includes the number of paths covered. For more fine-grained info on paths, use --prime-paths-lines or --prime-paths-source. With --json-format, all path details are included in the output. This requires you to compile the source with -fpath-coverage.
  • --prime-paths-lines [=type] โ€” Writes path coverage to the output file and path summary info to the standard output. ๐Ÿ“‹ This lets you see how many prime paths were taken at least once, plus a dense report on the covered or uncovered paths and how to cover them. This mode is great for automated reporting and progress tracking. type may be left out, or be one of:
    • uncovered โ€” Includes the uncovered (not taken) paths. This is the default.
    • covered โ€” Includes the covered (taken) paths.
    • both โ€” Includes all paths. This is equivalent to using both covered and uncovered.

Here's an example of --prime-paths-lines output:

        paths covered 12 of 15
        path  2 not covered: lines 8 8(false) 11(true) 11 13(true) 13(true) 14 17
        path  3 not covered: lines 8 8(false) 11(true) 11 13(true) 13(false) 16 17
        path  4 not covered: lines 8 8(false) 11(true) 11 13(false) 16 17

This means that to cover path 2, you must run lines 8, 11, 13, 14, and 17, evaluating the decision at 8 to "false" and the decisions at 11 and 13 to "false". ๐Ÿง 

  • --prime-paths-source [=type] โ€” Writes path coverage to the output file and path summary info to the standard output. ๐Ÿ” This lets you see how many prime paths were taken at least once, plus a detailed report on the uncovered paths and how to cover them. This mode is useful for understanding paths and the interactions between sections of your program. type may be left out, or be one of:
    • uncovered โ€” Includes the uncovered (not taken) paths. This is the default.
    • covered โ€” Includes the covered (taken) paths.
    • both โ€” Includes all paths. This is equivalent to using both covered and uncovered.

Here's an example of --prime-paths-source output:

        path 10 not covered:
        BB  3:           8:  for (i = 0; i < 10; i++)
        BB  3:           9:    total += i;
        BB  4: (false)   8:  for (i = 0; i < 10; i++)
        BB  5: (true)   11:  int v = total > 100 ? 1 : 2;
        BB  6:          11:  int v = total > 100 ? 1 : 2;
        BB  8: (false)  13:  if (total != 45 && v == 1)
        BB 11:          16:    printf ("Success\n");
        BB 12:          17:  return 0;

The first column (BB) is the sequence of basic blocks (see -w). ๐Ÿงฉ The middle column (true/false) is the decision for that line. The third column is the line number, and the fourth column is the line itself. These lines must be run in this order to cover path 10.

  • -d, --display-progress โ€” Displays the progress on the standard output. โณ
  • -f, --function-summaries โ€” Outputs summaries for each function, in addition to the file-level summary. ๐Ÿ’…
  • --include regex โ€” Includes functions matching regex. ๐ŸŽฏ This makes gcov only report on functions that match the extended regular expression regex. This flag can be combined with --exclude. If a function matches both includes and excludes, the last include/exclude applies. By default, gcov reports on all functions, but if --include is used, only functions matching the include will be reported.
  • --exclude regex โ€” Excludes functions matching regex. ๐Ÿ™…โ€โ™€๏ธ This makes gcov skip reporting on functions that match the extended regular expression regex. This flag can be combined with --include. If a function matches both includes and excludes, the last include/exclude applies. By default, gcov reports on all functions, and if --exclude is used, functions matching it will be omitted.
  • -h, --help โ€” Displays help about using gcov on the standard output, then exits without doing any further processing. ๐Ÿ™‹โ€โ™€๏ธ
  • -j, --json-format โ€” Outputs the gcov file in an easy-to-parse JSON intermediate format that doesn't need the source code to generate. ๐Ÿงƒ The JSON file is compressed with the gzip compression algorithm, and the files have the .gcov.json.gz extension.

The JSON is structured like this:

{
  "current_working_directory": "foo/bar",
  "data_file": "a.out",
  "format_version": "2",
  "gcc_version": "11.1.1 20210510"
  "files": ["$file"]
}

The fields of the root element mean the following:

  • current_working_directory โ€” the working directory where a compilation unit was compiled ๐Ÿ“‚
  • data_file โ€” the name of the data file (GCDA) ๐Ÿ—‚๏ธ
  • format_version โ€” the semantic version of the format

Changes in version 2:

  • calls โ€” information about function calls is added ๐Ÿ“ž
  • gcc_version โ€” the version of the GCC compiler ๐Ÿ› ๏ธ

Each file has the following form:

{
  "file": "a.c",
  "functions": ["$function"],
  "lines": ["$line"]
}

The fields of the file element mean the following:

  • file_name โ€” the name of the source file ๐Ÿ“„

Each function has the following form:

{
  "blocks": 2,
  "blocks_executed": 2,
  "demangled_name": "foo",
  "end_column": 1,
  "end_line": 4,
  "execution_count": 1,
  "name": "foo",
  "start_column": 5,
  "start_line": 1
}

okay so the function element is basically the whole vibe of a function in one little object ๐Ÿ’… Here's what each field is giving:

  • blocks: how many blocks live inside the function โœจ
  • blocks_executed: how many of those blocks actually ran ๐Ÿƒโ€โ™€๏ธ
  • demangled_name: the function's name, but make it readable ๐ŸŽ€
  • end_column: the column in the source file where the function ends
  • end_line: the line in the source file where the function ends
  • execution_count: how many times the function got called/run ๐Ÿ”ฅ
  • name: the function's name, mangled and all ๐Ÿฆ‹
  • start_column: the column in the source file where the function begins
  • start_line: the line in the source file where the function begins

Quick reminder, bestie: line numbers and column numbers both start counting from 1, not 0 ๐Ÿงš. Also, in the current implementation, start_line and start_column do not include any template parameters or the leading return type ๐Ÿ˜ญ. The devs say that's likely getting fixed later, so keep an eye out ๐Ÿ‘€.

Now for the line element. Each line looks like this:

{
  "block_ids": ["$block_id"],
  "branches": ["$branch"],
  "calls": ["$call"],
  "count": 2,
  "conditions": ["$condition"],
  "line_number": 15,
  "unexecuted_block": false,
  "function_name": "foo",
}

branches and calls only show up if you pass the -b option ๐Ÿ’œ. The fields of the line element mean:

  • block_ids: the IDs of the basic blocks that belong to this line ๐Ÿงฉ
  • count: how many times the line was executed ๐Ÿ’–
  • line_number: the line number, obviously ๐ŸŒธ
  • unexecuted_block: a flag that says whether the line has an unexecuted block in it (aka not every statement on the line ran, lowkey a red flag ๐Ÿšฉ)
  • function_name: the name of the function this line belongs to. Heads up, for a line with inlined statements this one might not be set at all ๐Ÿ™ˆ

Next up, each branch looks like:

{
  "count": 11,
  "destination_block_id": 17,
  "fallthrough": true,
  "source_block_id": 13,
  "throw": false
}

The fields of the branch element:

  • count: how many times the branch was taken ๐Ÿ”
  • fallthrough: true when the branch is a fall through branch โฌ‡๏ธ
  • throw: true when the branch is an exceptional branch ๐Ÿ’ฅ
  • isource_block_id: the ID of the basic block where this branch happens (yes, the field name really is spelled isource_block_id, it's giving typo energy but we copy it exactly ๐Ÿซถ)
  • destination_block_id: the ID of the basic block this branch jumps to ๐ŸŽฏ

Each call has the form:

{
  "destination_block_id": 1,
  "returned": 11,
  "source_block_id": 13
}

The call element's fields:

  • returned: how many times the function call returned (the call count is equal to line::count) ๐Ÿ“ž
  • isource_block_id: the ID of the basic block where this call happens (same spelling, same vibes ๐Ÿ™ƒ)
  • destination_block_id: the ID of the basic block this call continues after it returns ๐Ÿ

And finally, each condition looks like:

{
  "count": 4,
  "covered": 2,
  "not_covered_false": [],
  "not_covered_true": [0, 1],
}

The condition element's fields:

  • count: how many condition outcomes are in this expression ๐Ÿงฎ
  • covered: how many of those condition outcomes are covered โœ…
  • not_covered_true: the terms, by index, that were never seen as true in this expression ๐Ÿฅ€
  • not_covered_false: the terms, by index, that were never seen as false in this expression ๐Ÿฅ€

  • -H, --human-readable โ€” the counts get written in a human readable format, like 24.6k ๐Ÿ“
  • -k, --use-colors โ€” colours the lines of code that have zero coverage ๐ŸŽจ. We go with red for non-exceptional lines and cyan for exceptional ones ๐Ÿ’…. The same colours show up for basic blocks when you use -a.
  • -l, --long-file-names โ€” makes long file names for included source files ๐Ÿ“‚. Say the header x.h has code in it and got included in a.c. Running gcov on a.c will spit out a.c##x.h.gcov instead of x.h.gcov ๐Ÿง. This is super useful if x.h is included in lots of source files and you want to see each one's individual contribution. If you also use -p, both the including and included file names end up as complete path names ๐Ÿ—บ๏ธ.
  • -m, --demangled-names โ€” shows demangled function names in the output โœจ. By default, you get the mangled ones ๐Ÿ™ƒ.
  • -M, --filter-on-demangled โ€” makes --include and --exclude match against demangled names ๐Ÿ”. This only affects the matching, it does not turn on --demangled-names by itself, but the two play nicely together and can be combined safely ๐Ÿซถ.
  • -n, --no-output โ€” doesn't create the gcov output file ๐Ÿ™…โ€โ™€๏ธ.
  • -o directory|file, --object-directory directory, --object-file file โ€” tells gcov where to find the gcov data files, either a directory or the object path name ๐Ÿ“. The .gcno and .gcda data files get searched for using this option. If you give it a directory, the data files live in that directory and are named after the input file name, minus its extension ๐Ÿช„. If you give it a file, the data files are named after that file, minus its extension ๐ŸŽ€.
  • -p, --preserve-paths โ€” keeps the complete path information in the names of the generated .gcov files ๐Ÿ›ฃ๏ธ. Without it, you only get the filename component. With it, all the directories get used: / characters become # characters, . directory components get removed, and the unremoveable .. components get renamed to ^ ๐Ÿฆ‹. This is handy when your source files are spread across several different directories ๐ŸŒˆ.
  • -q, --use-hotness-colors โ€” emits perf-like coloured output for hot lines ๐Ÿ”ฅ. The legend for the colour scale gets printed at the very beginning of the output file ๐Ÿ“œ.
  • -r, --relative-only โ€” only outputs info about source files that have a relative pathname (after source prefix elision) ๐Ÿงญ. Absolute paths are usually system header files, and coverage of any inline functions in those is normally not very interesting ๐Ÿ˜ด.
  • -s directory, --source-prefix directory โ€” a prefix to strip off source file names when generating the output coverage files โœ‚๏ธ. This is useful when you build in a separate directory and don't want the path to the source directory messing with the output file names ๐Ÿงน. Heads up: this prefix detection happens before gcov decides whether the source file is absolute ๐Ÿ’ก.
  • -t, --stdout โ€” outputs to standard output instead of to output files ๐Ÿ“บ.
  • -u, --unconditional-branches โ€” when branch probabilities are given, this also includes the ones for unconditional branches ๐ŸŒ™. Unconditional branches are normally not that interesting, so they're left out by default ๐Ÿฅฑ.
  • -v, --version โ€” displays the gcov version number on standard output, then exits right away without doing any more processing ๐Ÿท๏ธ.
  • -w, --verbose โ€” prints verbose information about basic blocks and arcs ๐Ÿ“.
  • -x, --hash-filenames โ€” when you're using --preserve-paths, gcov normally uses the full pathname of the source files to make an output filename ๐Ÿ›ค๏ธ. That can get super long and overflow filesystem limits ๐Ÿ˜ญ. This option makes names like source-file##md5.gcov, where source-file is the final filename part and md5 is calculated from the full mangled name that would've been used otherwise ๐Ÿ”. It's an alternative to --preserve-paths for systems that have a filesystem limit ๐Ÿ’œ.

gcov should be run from the same current directory you were in when you invoked the compiler ๐Ÿ“. Otherwise it won't be able to find the source files, and that's a whole mess ๐Ÿ’€. gcov produces files called mangledname.gcov in the current directory ๐Ÿ“„. They hold the coverage information for the source file they correspond to. You get one .gcov file for each source (or header) file that has code and was compiled to produce the data files ๐ŸŽ. The mangledname part of the output file name is usually just the source file name, but it can get more complicated if you pass the -l or -p options. Check out those options for the deets ๐Ÿ”.

If you invoke gcov with multiple input files, the contributions from each input file get summed up ๐Ÿงฎ. Typically you'd run it with the same list of files you used for the final link of your executable ๐Ÿ”—.

The .gcov files contain : separated fields along with the program's source code ๐Ÿ’…. The format is:

<execution_count>:<line_number>:<source line text>

Extra block information may follow each line, if you asked for it with a command line option โœจ. The execution_count is - for lines that have no code ๐Ÿซฅ. Unexecuted lines get marked with ##### or =====, depending on whether they're reachable by non-exceptional paths or only by exceptional paths, like C++ exception handlers ๐Ÿ’ฅ. With the -a option, unexecuted blocks get marked $$$$$ or %%%%%, depending on whether a basic block is reachable via non-exceptional or exceptional paths ๐Ÿงฑ. Executed basic blocks that have a statement with a zero execution_count end with a * character, and they're coloured magenta with the -k option ๐Ÿ’œ. This functionality is not supported in Ada ๐Ÿ™…โ€โ™€๏ธ.

Heads up, bestie: GCC can completely remove the bodies of functions that aren't needed, like when they're inlined everywhere ๐Ÿซ . Those functions get marked with -, which can be really confusing ๐Ÿ˜ญ. To keep these functions around and let gcov properly show their execution_count, use the -fkeep-inline-functions and -fkeep-static-functions options ๐Ÿ”ง.

Some lines at the start have a line_number of zero ๐Ÿงš. These preamble lines look like this:

-:0:<tag>:<value>

The ordering and number of these preamble lines will be augmented as gcov development moves along, so do not rely on them staying the same ๐Ÿ™ˆ. Use the tag to locate a particular preamble line instead ๐ŸŽฏ.

The additional block information looks like this:

<tag> <information>

The information is human readable, but it's designed to be simple enough for machines to parse too ๐Ÿค–๐Ÿ’–.

When printing percentages, 0% and 100% only get printed when the values are exactly 0% and 100% respectively ๐Ÿ’ฏ. Other values that would normally round to 0% or 100% are instead printed as the nearest non-boundary value ๐ŸŒธ.

When you're using gcov, bestie, you have to compile your program with the special GCC option --coverage first ๐ŸŽ€ That tells the compiler to generate the extra info gcov needs (basically a flow graph of your program) and to sneak extra code into the object files so they can collect the profiling data. All of these extra files get dropped into the same directory as the object file ๐Ÿ“

Then you run the program and profile output gets generated โœจ For every source file compiled with -fprofile-arcs, a matching .gcda file lands in the object file directory ๐Ÿฆ‹

Now, if you run gcov with your source file names as arguments, it spits out a listing of your code with how often each line ran ๐Ÿ’… Say your program is called tmp.cpp. This is what you get when you use the basic gcov facility:

$ g++ --coverage tmp.cpp -c
$ g++ --coverage tmp.o
$ a.out
$ gcov tmp.cpp -m
File 'tmp.cpp'
Lines executed:92.86% of 14
Creating 'tmp.cpp.gcov'

The file tmp.cpp.gcov holds the gcov output. Here's a sample, slay ๐Ÿ˜ญ

        -:    0:Source:tmp.cpp
        -:    0:Working directory:/home/gcc/testcase
        -:    0:Graph:tmp.gcno
        -:    0:Data:tmp.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:#include <stdio.h>
        -:    2:
        -:    3:template<class T>
        -:    4:class Foo
        -:    5:{
        -:    6:  public:
       1*:    7:  Foo(): b (1000) {}
------------------
Foo<char>::Foo():
    #####:    7:  Foo(): b (1000) {}
------------------
Foo<int>::Foo():
        1:    7:  Foo(): b (1000) {}
------------------
       2*:    8:  void inc () { b++; }
------------------
Foo<char>::inc():
    #####:    8:  void inc () { b++; }
------------------
Foo<int>::inc():
        2:    8:  void inc () { b++; }
------------------
        -:    9:
        -:   10:  private:
        -:   11:  int b;
        -:   12:};
        -:   13:
        -:   14:template class Foo<int>;
        -:   15:template class Foo<char>;
        -:   16:
        -:   17:int
        1:   18:main (void)
        -:   19:{
        -:   20:  int i, total;
        1:   21:  Foo<int> counter;
        -:   22:
        1:   23:  counter.inc();
        1:   24:  counter.inc();
        1:   25:  total = 0;
        -:   26:
       11:   27:  for (i = 0; i < 10; i++)
       10:   28:    total += i;
        -:   29:
       1*:   30:  int v = total > 100 ? 1 : 2;
        -:   31:
        1:   32:  if (total != 45)
    #####:   33:    printf ("Failure\n");
        -:   34:  else
        1:   35:    printf ("Success\n");
        1:   36:  return 0;
        -:   37:}

Look at line 7 popping up in the report more than once ๐Ÿ‘€ The first hit shows how many times the line ran in total, and the next two belong to the two instances of the Foo constructor. Also, line 30 has some unexecuted basic blocks, which is why its execution count has an asterisk symbol โญ

When you use the -a option, you get individual block counts instead, and it looks like this:

        -:    0:Source:tmp.cpp
        -:    0:Working directory:/home/gcc/testcase
        -:    0:Graph:tmp.gcno
        -:    0:Data:tmp.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:#include <stdio.h>
        -:    2:
        -:    3:template<class T>
        -:    4:class Foo
        -:    5:{
        -:    6:  public:
       1*:    7:  Foo(): b (1000) {}
------------------
Foo<char>::Foo():
    #####:    7:  Foo(): b (1000) {}
------------------
Foo<int>::Foo():
        1:    7:  Foo(): b (1000) {}
------------------
       2*:    8:  void inc () { b++; }
------------------
Foo<char>::inc():
    #####:    8:  void inc () { b++; }
------------------
Foo<int>::inc():
        2:    8:  void inc () { b++; }
------------------
        -:    9:
        -:   10:  private:
        -:   11:  int b;
        -:   12:};
        -:   13:
        -:   14:template class Foo<int>;
        -:   15:template class Foo<char>;
        -:   16:
        -:   17:int
        1:   18:main (void)
        -:   19:{
        -:   20:  int i, total;
        1:   21:  Foo<int> counter;
        1:   21-block  0
        -:   22:
        1:   23:  counter.inc();
        1:   23-block  0
        1:   24:  counter.inc();
        1:   24-block  0
        1:   25:  total = 0;
        -:   26:
       11:   27:  for (i = 0; i < 10; i++)
        1:   27-block  0
       11:   27-block  1
       10:   28:    total += i;
       10:   28-block  0
        -:   29:
       1*:   30:  int v = total > 100 ? 1 : 2;
        1:   30-block  0
    %%%%%:   30-block  1
        1:   30-block  2
        -:   31:
        1:   32:  if (total != 45)
        1:   32-block  0
    #####:   33:    printf ("Failure\n");
    %%%%%:   33-block  0
        -:   34:  else
        1:   35:    printf ("Success\n");
        1:   35-block  0
        1:   36:  return 0;
        1:   36-block  0
        -:   37:}

In this mode, each basic block only gets shown on one line, the last line of that block ๐Ÿง  A multi-line block only adds to the execution count of that last line, and the other lines won't show as containing code, unless earlier blocks end on those lines. The total execution count of a line is shown, and the lines after it show the execution counts for the individual blocks that end on that line. After each block, the branch and call counts for that block get shown too, but only if you pass the -b option.

Because of the way GCC instruments calls, you can get a call count showing up after a line that has no individual blocks, so don't panic ๐Ÿ™ˆ As you can see, line 33 has a basic block that never got executed.

When you use the -b option, your output looks like this:

        -:    0:Source:tmp.cpp
        -:    0:Working directory:/home/gcc/testcase
        -:    0:Graph:tmp.gcno
        -:    0:Data:tmp.gcda
        -:    0:Runs:1
        -:    0:Programs:1
        -:    1:#include <stdio.h>
        -:    2:
        -:    3:template<class T>
        -:    4:class Foo
        -:    5:{
        -:    6:  public:
       1*:    7:  Foo(): b (1000) {}
------------------
Foo<char>::Foo():
function Foo<char>::Foo() called 0 returned 0% blocks executed 0%
    #####:    7:  Foo(): b (1000) {}
------------------
Foo<int>::Foo():
function Foo<int>::Foo() called 1 returned 100% blocks executed 100%
        1:    7:  Foo(): b (1000) {}
------------------
       2*:    8:  void inc () { b++; }
------------------
Foo<char>::inc():
function Foo<char>::inc() called 0 returned 0% blocks executed 0%
    #####:    8:  void inc () { b++; }
------------------
Foo<int>::inc():
function Foo<int>::inc() called 2 returned 100% blocks executed 100%
        2:    8:  void inc () { b++; }
------------------
        -:    9:
        -:   10:  private:
        -:   11:  int b;
        -:   12:};
        -:   13:
        -:   14:template class Foo<int>;
        -:   15:template class Foo<char>;
        -:   16:
        -:   17:int
function main called 1 returned 100% blocks executed 81%
        1:   18:main (void)
        -:   19:{
        -:   20:  int i, total;
        1:   21:  Foo<int> counter;
call    0 returned 100%
branch  1 taken 100% (fallthrough)
branch  2 taken 0% (throw)
        -:   22:
        1:   23:  counter.inc();
call    0 returned 100%
branch  1 taken 100% (fallthrough)
branch  2 taken 0% (throw)
        1:   24:  counter.inc();
call    0 returned 100%
branch  1 taken 100% (fallthrough)
branch  2 taken 0% (throw)
        1:   25:  total = 0;
        -:   26:
       11:   27:  for (i = 0; i < 10; i++)
branch  0 taken 91% (fallthrough)
branch  1 taken 9%
       10:   28:    total += i;
        -:   29:
       1*:   30:  int v = total > 100 ? 1 : 2;
branch  0 taken 0% (fallthrough)
branch  1 taken 100%
        -:   31:
        1:   32:  if (total != 45)
branch  0 taken 0% (fallthrough)
branch  1 taken 100%
    #####:   33:    printf ("Failure\n");
call    0 never executed
branch  1 never executed
branch  2 never executed
        -:   34:  else
        1:   35:    printf ("Success\n");
call    0 returned 100%
branch  1 taken 100% (fallthrough)
branch  2 taken 0% (throw)
        1:   36:  return 0;
        -:   37:}

For each function, gcov prints a line telling you how many times the function got called, how many times it returned, and what percentage of the function's blocks were executed ๐ŸŒˆ

okay so when a source line has more than one basic block ending on it, gcov prints a line right after the last line of that block describing the branch or call that ends it ๐Ÿง โœจ. If several branches or calls end on the same line, each one gets a number ๐Ÿ”ข. Honestly there's no simple way to map those numbers back to specific source constructs ๐Ÿ˜ญ. Generally though, the lowest-numbered branch or call is the one for the leftmost construct on that line ๐Ÿ‘€.

For a branch ๐ŸŒฟ, if it ran at least once, you get a percentage showing how many times it was taken divided by how many times it was executed. If it never ran, gcov just says "never executed" ๐Ÿ’€.

For a call ๐Ÿ“ž, if it ran at least once, you get a percentage of how many times it returned divided by how many times it was called. This is usually 100%, but it can be lower for functions that call exit or longjmp, since those might not return every single time they're called ๐Ÿ™ˆ.


๐ŸŽ€ Using -g

When you use the -g option, your output looks like this ๐Ÿ’–:

$ gcov -t -m -g tmp
        -:    0:Source:tmp.cpp
        -:    0:Graph:tmp.gcno
        -:    0:Data:tmp.gcda
        -:    0:Runs:1
        -:    1:#include <stdio.h>
        -:    2:
        -:    3:int
        1:    4:main (void)
        -:    5:{
        -:    6:  int i, total;
        1:    7:  total = 0;
        -:    8:
       11:    9:  for (i = 0; i < 10; i++)
condition outcomes covered 2/2
       10:   10:    total += i;
        -:   11:
       1*:   12:  int v = total > 100 ? 1 : 2;
condition outcomes covered 1/2
condition  0 not covered (true)
        -:   13:
       1*:   14:  if (total != 45 && v == 1)
condition outcomes covered 1/4
condition  0 not covered (true)
condition  1 not covered (true false)
    #####:   15:    printf ("Failure\n");
        -:   16:  else
        1:   17:    printf ("Success\n");
        1:   18:  return 0;
        -:   19:}

Every condition gets its taken and total outcomes printed ๐Ÿ“Š. If any outcomes are uncovered, you get a line for each condition showing the uncovered outcome in parentheses ๐Ÿซถ. Conditions are numbered by their index, and index 0 is the left-most condition ๐Ÿ‘‘. So in a || (b && c), a is condition 0, b is condition 1, and c is condition 2 ๐Ÿงš.

An outcome counts as covered if it has an independent effect on the decision ๐Ÿ’…. This is also called masking MC/DC (Modified Condition/Decision Coverage) ๐Ÿค“. In this example, the decision comes out true and a gets evaluated, but a is still not covered. That's because a can't affect the decision all on its own. Both a and b have to change value for the decision to flip ๐Ÿ˜ญโœจ.

$ gcov -t -m -g tmp
        -:    0:Source:tmp.c
        -:    0:Graph:tmp.gcno
        -:    0:Data:tmp.gcda
        -:    0:Runs:1
        -:    1:#include <stdio.h>
        -:    2:
        1:    3:int main()
        -:    4:{
        1:    5:  int a = 1;
        1:    6:  int b = 0;
        -:    7:
        1:    8:  if (a && b)
condition outcomes covered 1/4
condition  0 not covered (true false)
condition  1 not covered (true)
    #####:    9:    printf ("Success!\n");
        -:   10:  else
        1:   11:    printf ("Failure!\n");
        -:   12:}

๐Ÿ›ค๏ธ Paths

When you compile with --coverage -fpath-coverage and use the option -e, your output looks like this ๐ŸŒˆ:

$ gcov -t -e tmp
        -:    0:Source:tmp.cpp
        -:    0:Graph:tmp.gcno
        -:    0:Data:tmp.gcda
        -:    0:Runs:1
        -:    1:#include <stdio.h>
        -:    2:
paths covered 4 of 15
        1:    3:int main ()
        -:    4:{
        -:    5:  int i, total;
        1:    6:  total = 0;
        -:    7:
       11:    8:  for (i = 0; i < 10; i++)
       10:    9:    total += i;
        -:   10:
       1*:   11:  int v = total > 100 ? 1 : 2;
        -:   12:
       1*:   13:  if (total != 45 && v == 1)
    #####:   14:    printf ("Failure\n");
        -:   15:  else
        1:   16:    printf ("Success\n");
        1:   17:  return 0;
        -:   18:}

This output is super handy for figuring out roughly where coverage is missing ๐Ÿ”, and for testing how different inputs change the coverage ๐Ÿฆ‹. Also, the --prime-paths-source option is a really useful tool for understanding paths ๐Ÿง ๐Ÿ’œ.

$ gcov -t --prime-paths-source tmp
        -:    0:Source:tmp.cpp
        -:    0:Graph:tmp.gcno
        -:    0:Data:tmp.gcda
        -:    0:Runs:1
        -:    1:#include <stdio.h>
        -:    2:
paths covered 4 of 15
path 1:
BB  2:           3:int main ()
BB  2:           6:  total = 0;
BB  2:           8:  for (i = 0; i < 10; i++)
BB  4: (false)   8:  for (i = 0; i < 10; i++)
BB  5: (true)   11:  int v = total > 100 ? 1 : 2;
BB  6:          11:  int v = total > 100 ? 1 : 2;
BB  8: (true)   13:  if (total != 45 && v == 1)
BB  9: (true)   13:  if (total != 45 && v == 1)
BB 10:          14:    printf ("Failure\n");
BB 12:          17:  return 0;

In this mode, gcov prints the details of the missing paths ๐Ÿ•ต๏ธโ€โ™€๏ธ. The first column lists the sequence of basic blocks (BB) ๐Ÿ“‹. The second column is the decision to take at that line, if there is one ๐Ÿ”€. The last columns are the line number and the line itself ๐Ÿ“. This is great for understanding the paths, especially the ones that are hard to cover or might even be unreachable ๐Ÿ™ˆ. Lines can show up more than once, like the for loop, if that same line is part of multiple basic blocks ๐Ÿ”. This mode is meant for humans and is really good at showing what code gets exercised by testing or by given inputs ๐Ÿซถ. The output is pretty verbose ๐Ÿ“œ, so if you want to focus on specific functions, you can combine it with the filters --include and --exclude ๐ŸŽฏ.

A denser output is available with --prime-paths-lines ๐Ÿ“, and it looks like this:

        -:    0:Source:tmp.cpp
        -:    0:Graph:tmp.gcno
        -:    0:Data:tmp.gcda
        -:    0:Runs:1
        -:    1:#include <stdio.h>
        -:    2:
paths covered 4 of 15
path  1 not covered: lines 8 8(false) 11(true) 11 13(true) 13(true) 14 17
path  2 not covered: lines 8 8(false) 11(true) 11 13(true) 13(false) 16 17
path  3 not covered: lines 8 8(false) 11(true) 11 13(false) 16 17
path  4 not covered: lines 8 8(false) 11(false) 11 13(true) 13(true) 14 17
path  5 not covered: lines 8 8(false) 11(false) 11 13(true) 13(false) 16 17
path  6 not covered: lines 8 8(false) 11(false) 11 13(false) 16 17
path  8 not covered: lines 9 8(false) 11(true) 11 13(true) 13(true) 14 17
path  9 not covered: lines 9 8(false) 11(true) 11 13(true) 13(false) 16 17
path 10 not covered: lines 9 8(false) 11(true) 11 13(false) 16 17
path 11 not covered: lines 9 8(false) 11(false) 11 13(true) 13(true) 14 17
path 12 not covered: lines 9 8(false) 11(false) 11 13(true) 13(false) 16 17
        1:    3:int main ()
        -:    4:{

In this mode, every missing path gets expanded using the lines and decisions from --prime-paths-source, but it's all printed on a single line ๐Ÿช„. You get a nice overview of the paths, and it's great for tracking how different tests and inputs exercise the code ๐Ÿ“ˆโœจ.


๐Ÿ” Cumulative Counts

The execution counts are cumulative ๐Ÿ’œ. If you run the example program again without deleting the .gcda file, the count for how many times each source line ran gets added to the results from the previous run(s) ๐Ÿง. This is really useful in a few ways. For example, you can accumulate data over a bunch of program runs as part of a test verification suite, or get more accurate long-term info over a large number of runs ๐ŸŒธ.

The data in the .gcda files is saved right before the program exits ๐Ÿ’พ. For each source file compiled with -fprofile-arcs, the profiling code first tries to read an existing .gcda file. If that file doesn't match the executable (meaning the number of basic block counts is different), it ignores the file's contents ๐Ÿ™…โ€โ™€๏ธ. Then it adds in the new execution counts, and finally writes the data back to the file ๐Ÿ“.


๐Ÿ” Filtering Functions

You can report on a subset of functions by using --include and --exclude ๐ŸŽฏ. This is super useful when you pair it with --stdout ๐Ÿ’…. Like, you can run a test, look at the gcov output for one particular function, try another input, and run gcov again to see how that function's behavior and coverage changed ๐Ÿซ .

$ gcov -m --stdout --include inc tmp
        -:    0:Source:tmp.cpp
        -:    0:Graph:tmp.gcno
        -:    0:Data:tmp.gcda
        -:    0:Runs:1
       2*:    8:  void inc () { b++; }
------------------
Foo<char>::inc():
    #####:    8:  void inc () { b++; }
------------------
Foo<int>::inc():
        2:    8:  void inc () { b++; }
------------------

By default, gcov matches on mangled names ๐Ÿงฉ, and you can change that with the -M flag ๐Ÿ”€. Matching and reporting are independent of each other, so you can match on mangled names while printing demangled names, and the other way around too ๐ŸŒˆ. To report on the "int" instantiation of "Foo", matching on both mangled and demangled names, do this ๐Ÿ‘‘:

$ gcov -t -m -M tmp --include 'Foo<int>'
        -:    0:Source:tmp.cpp
        -:    0:Graph:tmp.gcno
        -:    0:Data:tmp.gcda
        -:    0:Runs:1
        1:    7:  Foo(): b (1000) {}
        2:    8:  void inc () { b++; }

$ gcov -t -m tmp --include 'FooIi'
        -:    0:Source:tmp.cpp
        -:    0:Graph:tmp.gcno
        -:    0:Data:tmp.gcda
        -:    0:Runs:1
        1:    7:  Foo(): b (1000) {}
        2:    8:  void inc () { b++; }

Okay so the --include and --exclude args are extended regular expressions, like grep -E, which means the pattern "in.?" matches both "inc" and "main" ๐Ÿ’… If you use -M, then all the int instantiations of "Foo" get matched too ๐Ÿฆ‹ You can use --include and --exclude as many times as you want. If a name matches more than one filter, the last one that matches wins ๐Ÿ‘‘

Here's a cute example. We want "main" and the int instantiation of "inc", but we want to drop the "Foo" constructor ๐ŸŽ€

$ gcov -t -m -M --include in --exclude Foo --include '<int>::inc' tmp
        -:    0:Source:tmp.cpp
        -:    0:Graph:tmp.gcno
        -:    0:Data:tmp.gcda
        -:    0:Runs:1
        2:    8:  void inc () { b++; }
        1:   18:main (void)
        -:   19:{
        -:   20:  int i, total;
        1:   21:  Foo<int> counter;
        -:   22:
        1:   23:  counter.inc();
        1:   24:  counter.inc();
        1:   25:  total = 0;
        -:   26:
       11:   27:  for (i = 0; i < 10; i++)
       10:   28:    total += i;
        -:   29:
       1*:   30:  int v = total > 100 ? 1 : 2;
        -:   31:
        1:   32:  if (total != 45)
    #####:   33:    printf ("Failure\n");
        -:   34:  else
        1:   35:    printf ("Success\n");
        1:   36:  return 0;

โœจ Using gcov with GCC Optimization โœจ

Bestie, if you want to use gcov to help optimize your code, you have to compile your program with the special GCC option --coverage first ๐Ÿงš Other GCC options are totally fine too ๐Ÿ’– BUT if you want to prove that every single line in your program ran, you should not compile with optimization at the same time ๐Ÿ˜ญ On some machines the optimizer can wipe out simple lines by merging them with other lines. Like, code that looks like this:

if (a != b)
  c = 1;
else
  c = 0;

can get squashed into one instruction on some machines ๐Ÿ’€ When that happens, gcov has no way to work out separate execution counts for each line, because there isn't separate code for each line. So if you compiled with optimization, the gcov output ends up looking like this:

      100:   12:if (a != b)
      100:   13:  c = 1;
      100:   14:else
      100:   15:  c = 0;

That output says this block, merged by the optimizer, ran 100 times. Technically that's correct, because there was only one instruction covering all four lines โœจ But it doesn't tell you how many times the result was 0 and how many times it was 1, so it's kinda useless for that ๐Ÿ™ˆ

Inlineable functions can also give you some unexpected line counts ๐ŸŒธ The counts show up for the source of the inlineable function, but what you see depends on where the function got inlined, or whether it got inlined at all.

If the function is not inlined, the compiler has to emit an out of line copy of it in every object file that needs it. So if fileA.o and fileB.o both have out of line bodies for the same inlineable function, they'll both have coverage counts for it ๐Ÿซถ When fileA.o and fileB.o get linked together, the linker on lots of systems picks one of those out of line bodies for every call to that function and removes or ignores the other one. The annoying part is that it does not remove the coverage counters for the unused body. So when you instrument it, every use except one will show zero counts ๐Ÿ˜ค

If the function gets inlined in several places, the block structure in each spot might not be the same. Like, a condition might be calculable at compile time in some instances ๐Ÿง  All the uses of the inline function get shown against the same source lines, so the line counts can look inconsistent. That's not a bug, it's just vibes ๐Ÿ’…

Long-running applications can use the "__gcov_reset" and "__gcov_dump" facilities to keep profile collection limited to the part of the program you actually care about โœจ Calling "__gcov_reset(void)" clears all run-time profile counters back to zero ๐Ÿงน Calling "__gcov_dump(void)" dumps the profile info collected up to that point into .gcda output files ๐Ÿ“ Instrumented apps use a static destructor with priority 99 to call "__gcov_dump", so "__gcov_dump" runs after all user defined static destructors, plus any handlers registered with "atexit" ๐Ÿ‘‘

If an executable loads a dynamic shared object through dlopen, you need -Wl,--dynamic-list-data to dump all the profile data ๐Ÿšจ

The profiling run-time library reports various errors related to profile manipulation and profile saving ๐Ÿ˜ฌ Errors get printed to standard error output, or to the GCOV_ERROR_FILE file if you've set that environment variable. If you want it to terminate immediately when an error happens, set the GCOV_EXIT_AT_ERROR environment variable ๐Ÿ›‘ That can help you catch profile clashing, which is what leads to a misleading profile ๐Ÿ”

๐Ÿ”— SEE ALSO ๐Ÿ”—

gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for gcc ๐Ÿ’œ

Copyright (c) 1996-2025 Free Software Foundation, Inc. ๐ŸŒˆ

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation ๐ŸŽ€ The Invariant Sections are "GNU General Public License" and "Funding Free Software". The Front-Cover texts are (a) (see below), and the Back-Cover Texts are (b) (see below). A copy of the license is included in the gfdl(7) man page ๐Ÿ“–

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development. ๐Ÿซถ

Bye bestie, that's the whole page ๐Ÿ’– Shoutout to gcc-15.2.0, last updated 2025-08-08, and the GCOV(1) page, which literally ate ๐Ÿ”ฅ๐Ÿ’…

๐Ÿ“ฆ where this page comes from