GCOV(1)
โจ GCOV(1) โ GNU Manual โจ
Okay bestie, this is the GNU manual page for GCOV(1) ๐ Let's get into it ๐
๐ NAME ๐
gcov โ coverage testing tool ๐ฆ
โจ SYNOPSIS โจ
Here's every flag gcov takes, all lined up nice and neat ๐ธ
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 ๐ง
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 find the parts of your program nobody has tested yet ๐ซถ You can also use gcov as a profiling tool to figure out where your optimization efforts will actually matter. And you can pair it with the other profiling tool, gprof, to see which parts of your code eat the most computing time ๐
Profiling tools help you analyze how your code performs ๐ With 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 compiled, you can look at each module and see which ones need optimizing โจ gcov helps you figure out where to start the optimization work.
Software developers also pair coverage testing with testsuites, to make sure software is actually good enough to release ๐ A testsuite checks that a program works as expected, while a coverage program checks how much of the program the testsuite actually exercises ๐ Then developers can see what kinds of test cases to add to the testsuites, for better testing and a better final product.
Compile your code without optimization if you plan to use gcov ๐
Optimization can combine several lines of code into one function, which may not give you enough info to spot โhot spotsโ where the code is using a ton of computer time. Also, because gcov keeps stats by line (at the lowest resolution), it works best with a style that puts only one statement on each line ๐ If you use complicated macros that expand into loops or other control structures, the stats get less helpfulโthey only report on the line where the macro call appears. If your complex macros act like functions, you can swap them for inline functions to fix this ๐ฆ
gcov creates a logfile called sourcefile.gcov which shows how many times each line of a source file sourcefile.c has executed ๐ You can pair these logfiles with gprof to fine-tune your program's performance. gprof gives timing info you can use alongside what gcov tells you.
gcov only works on code compiled with GCC ๐ซ It is not compatible with any other profiling or test coverage mechanism.
๐ OPTIONS ๐
-a,--all-blocksโ Spits out execution counts for every single basic block ๐ข Normallygcovonly shows counts for the main blocks of a line. With this one, you can spot blocks within a single line that aren't getting executed.-b,--branch-probabilitiesโ Writes branch frequencies to the output file and branch summary info to standard output ๐ฟ This lets you see how often each branch in your program got taken. Unconditional branches won't show up unless you also pass-u.-c,--branch-countsโ Shows 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 standard output ๐ง This lets you check whether each condition in your boolean expressions had an independent effect on the outcome at least once (modified condition/decision coverage). You have to compile the source with-fcondition-coveragefor this one.-e,--prime-pathsโ Writes path coverage to the output file and path summary info to standard output ๐ค๏ธ This shows how many prime paths got 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's prime if it's a simple path of maximal length. For the regular output, this option only includes the number of paths covered. For more fine-grained info, use--prime-paths-linesor--prime-paths-source. With--json-format, all path details get included in the output. You have to compile the source with-fpath-coveragefor this one.--prime-paths-lines [=type]โ Writes path coverage to the output file and path summary info to standard output ๐ This shows how many prime paths got 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 can 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 bothcoveredanduncovered.
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
So to cover path 2, you have to run lines 8, 11, 13, 14, and 17, evaluating the decision at 8 as false and the decisions at 11 and 13 to "false". Bestie, that's the whole plan ๐บ๏ธ
--prime-paths-source [=type]โ Writes path coverage to the output file and path summary info to standard output ๐ This shows how many prime paths got taken at least once, plus a detailed report on the uncovered paths and how to cover them. This mode is super useful for understanding paths and how different sections of your program interact ๐งฉ type can 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 bothcoveredanduncovered.
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. 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 standard output ๐-f,--function-summariesโ Outputs summaries for each function, in addition to the file-level summary ๐งฉ--include regexโ Includes functions matching regex. This makesgcovonly 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 defaultgcovreports on all functions, but if--includeis used, only functions matching the include will be reported ๐--exclude regexโ Excludes functions matching regex. This makesgcovskip 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 defaultgcovreports on all functions, and if--excludeis used, functions matching it get omitted ๐-h,--helpโ Displays help about usinggcov(on 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 source code to generate ๐ง The JSON file is compressed with the gzip compression algorithm, and the files have the.gcov.json.gzextension.
Here's the structure of the JSON, bestie ๐
{
"current_working_directory": "foo/bar",
"data_file": "a.out",
"format_version": "2",
"gcc_version": "11.1.1 20210510"
"files": ["$file"]
}
Here's what the root element fields mean:
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.
- Changes in version 2:
gcc_versionโ The version of the GCC compiler.
Each file has the following form:
{
"file": "a.c",
"functions": ["$function"],
"lines": ["$line"]
}
Here's what the file element fields mean:
file_nameโ The name of the source file.
Each function has the following form:
okay so here's what a function element looks like, bestie ๐ โจ
{
"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
}
The function element fields are giving main character energy ๐ฆ
blocksโ how many blocks are in the function ๐งฑblocks_executedโ how many of those blocks actually ran ๐โโ๏ธdemangled_nameโ the demangled name of the function ๐end_columnโ the column in the source file where the function endsend_lineโ the line in the source file where the function endsexecution_countโ how many times the function got executed ๐ฅnameโ the name of the function ๐ธstart_columnโ the column in the source file where the function beginsstart_lineโ the line in the source file where the function begins
Quick heads up, bestie: line numbers and column numbers start counting at 1 ๐ Also, right now start_line and start_column do NOT include any template parameters or the leading return type. That's likely to get fixed later, so don't get too attached to it yet ๐
Each line element 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 when you use the -b option ๐ฆ Here's what the line fields mean:
block_idsโ IDs of the basic blocks that belong to this linecountโ how many times the line was executedline_numberโ the line number ๐ขunexecuted_blockโ a flag for whether the line has an unexecuted block (aka not every statement on the line ran) ๐ โโ๏ธfunction_nameโ the name of the function this line belongs to. For a line with inlined statements, this one can be left unset ๐ง
Each branch looks like this:
{
"count": 11,
"destination_block_id": 17,
"fallthrough": true,
"source_block_id": 13,
"throw": false
}
Here's the branch tea โ
countโ how many times the branch ranfallthroughโtruewhen the branch is a fall through branchthrowโtruewhen the branch is an exceptional branch ๐ฅisource_block_idโ the ID of the basic block where this branch happensdestination_block_idโ the ID of the basic block this branch jumps to ๐ฆ
Each call looks like this:
{
"destination_block_id": 1,
"returned": 11,
"source_block_id": 13
}
And the call fields:
returnedโ how many times the function call returned (the call count is equal toline::count) ๐isource_block_idโ the ID of the basic block where this call happensdestination_block_idโ the ID of the basic block this call continues after returning to ๐
Each condition looks like this:
{
"count": 4,
"covered": 2,
"not_covered_false": [],
"not_covered_true": [0, 1],
}
And finally the condition fields, which are a whole mood ๐ฅน
countโ how many condition outcomes are in this expressioncoveredโ how many of those condition outcomes are covered โจnot_covered_trueโ the terms, by index, that were never seen as true in this expressionnot_covered_falseโ the terms, by index, that were never seen as false in this expression
-H,--human-readableโ shows the counts in a human friendly way, like24.6k๐-k,--use-colorsโ colours the lines of code that have zero coverage. We use red for non-exceptional lines and cyan for exceptional ones. The same colours are used for basic blocks when you use-a๐ธ-l,--long-file-namesโ makes long file names for included source files. For example, if the header filex.hhas code and gets included ina.c, running gcov ona.cwill make an output file calleda.c##x.h.gcovinstead ofx.h.gcov. This is super useful ifx.his included in multiple source files and you want to see each one's contribution ๐งฉ If you also use-p, both the including and included file names will be complete path names ๐บ๏ธ-m,--demangled-namesโ shows demangled function names in the output. By default, you get the mangled names ๐ฎ-M,--filter-on-demangledโ makes--includeand--excludematch demangled names. This ONLY affects the matching and does NOT turn on--demangled-names, but you can safely combine it with that one ๐-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 as a directory or as the object path name. It searches for the.gcnoand.gcdadata files using this option. If you give a directory, the data files live in that directory and are named after the input file name, without its extension. If you give a file, the data files are named after that file, without its extension ๐-p,--preserve-pathsโ keeps the complete path info in the names of the generated.gcovfiles. Without it, only the filename part gets used. With it, all directories get used,/characters become#characters,.directory components get removed, and..components that can't be removed get renamed to^. This is a lifesaver if your source files are spread across several different directories ๐ซถ-q,--use-hotness-colorsโ emits perf-like coloured output for hot lines ๐ฅ A legend for the colour scale gets printed at the very beginning of the output file ๐-r,--relative-onlyโ only outputs info about source files with a relative pathname (after source prefix elision). Absolute paths are usually system header files, and the coverage of any inline functions in them is normally not very interesting ๐-s directory,--source-prefix directoryโ a prefix for source file names to strip when generating the output coverage files. This is handy when you're building in a separate directory and don't want the path to the source directory showing up in your output file names. Note that this prefix detection happens before gcov decides whether the source file is absolute ๐งน-t,--stdoutโ outputs to standard output instead of output files ๐ป-u,--unconditional-branchesโ when branch probabilities are given, this also includes those of unconditional branches. Unconditional branches are normally not that interesting, so they're skipped by default ๐ด-v,--versionโ displays the gcov version number (on standard output) and exits without doing any further processing ๐ท๏ธ-w,--verboseโ prints verbose information about basic blocks and arcs ๐ฃ-x,--hash-filenamesโ when using--preserve-paths, gcov normally uses the full pathname of the source files to make an output filename, which can get long enough to overflow filesystem limits. This option makes names of the formsource-file##md5.gcov, wheresource-fileis the final filename part and themd5part is calculated from the full mangled name that would have been used otherwise. Use it instead of--preserve-pathson 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 vibe-kill ๐ญ gcov makes files called mangledname.gcov in the current directory. These hold the coverage info for the source file they match up with. One .gcov file gets produced for each source (or header) file that has code and was compiled to make 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 use the -l or -p options. Check those options for the details ๐
If you run gcov with multiple input files, the contributions from each input file get summed up together. Usually you'd run it with the same list of files you used in the final link of your executable ๐
The .gcov files have :-separated fields along with the program's source code. The format is:
<execution_count>:<line_number>:<source line text>
Extra block information might follow each line if you asked for it with a command line option. The execution_count is - for lines with no code ๐ซฅ Unexecuted lines are marked ##### or =====. Which one you get depends on whether they're reachable by non-exceptional paths (#####) or only by exceptional paths, like C++ exception handlers (=====) ๐ฅ If you use 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, so Ada folks, sorry bestie ๐
Quick note, GCC can completely remove the bodies of functions that aren't needed, for example if they're inlined everywhere. Those functions get marked with -, which can be super 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 of information 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 progresses, so do NOT rely on them staying the same! Use tag to find 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 gcov prints percentages, 0% and 100% only show up when the values are EXACTLY 0% and 100%. Any other values that would normally get rounded to 0% or 100% are instead printed as the nearest non-boundary value ๐ฏ
Okay so, before gcov can do anything, you have to compile your program with the special GCC option --coverage ๐. This 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 record the profiling data. Those extra files get dropped into whatever directory the object file lives in ๐.
Running the program is what produces the profile output ๐ฆ. For every source file compiled with -fprofile-arcs, you'll get a matching .gcda file in the object file directory ๐.
Now, when you run gcov with your program's source file names as arguments, you get a listing of your code with how often each line ran ๐ฅ. Say your program is called tmp.cpp. Here's what the basic gcov vibe looks like:
$ 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 gcov's output, and here's a sample ๐
:
-: 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:}
Note that line 7 shows up in the report multiple times ๐. The first occurrence gives the total number of executions for the line, and the next two belong to the instances of the Foo class constructors ๐. Also, line 30 has some unexecuted basic blocks, which is why its execution count has an asterisk (*) ๐.
When you use the -a option, you get individual block counts, and the 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():
#####: 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 shows up on one line, which is the last line of the block ๐. A multi-line block only contributes to the execution count of that last line, and the other lines won't be shown as containing code, unless previous 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 of that block are shown, if the -b option is given.
Because of the way GCC instruments calls, a call count can be shown after a line with no individual blocks ๐ค. As you can see, line 33 contains a basic block that was not 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, a line gets printed showing how many times the function is called, how many times it returns, and what percentage of the function's blocks were executed ๐.
For each basic block, gcov prints a line right after the block's last line, describing the branch or call that ends it ๐งฉ. Multiple branches and calls can land on one source line if several basic blocks end there, and that gets messy ngl ๐. In that case each one gets a number. There's no simple way to map those numbers back to source constructs, BUT the lowest-numbered branch or call is usually the leftmost construct on the line ๐.
For a branch that ran at least once, you get a percentage: the number of times it was taken divided by the number of times it was executed ๐ฆ. If it never ran, gcov says never executed ๐.
For a call that ran at least once, you get a percentage: the number of times it returned divided by the number of 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 time they're called ๐ญ.
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:}
For every condition, gcov prints how many outcomes were taken and how many there are in total ๐. If any outcomes are uncovered, you get a line for each condition showing the uncovered outcome in parentheses. Conditions are identified by index, and index 0 is the left-most condition. 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 the example below, the decision evaluates to true and a is evaluated, but a is still not covered. That's because a can't change the decision 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:}
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 useful for figuring out roughly where coverage is missing, and for testing how different inputs change the coverage ๐. The --prime-paths-source is also a 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 details on 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 final columns are the line number and the line itself. This is great for understanding paths, especially the ones that are hard to cover or even unreachable ๐ต. Lines may repeat, like the for loop, if the same line is part of multiple basic blocks. This mode is made for humans, and it's good at showing what code gets exercised by testing or by given inputs. It's quite verbose, though, 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, which 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 is expanded using the lines and decisions like --prime-paths-source, but it's 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 ๐ง.
The execution counts are cumulative ๐. If the example program runs again without removing the .gcda file, the count for each source line gets added to the results of the previous run(s). This is useful in a few ways. For example, you could 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 program runs ๐.
The data in the .gcda files gets saved immediately before the program exits ๐พ. For each source file compiled with -fprofile-arcs, the profiling code first tries to read an existing .gcda file. If the file doesn't match the executable (meaning a different number of basic block counts), it ignores the contents of the file ๐
. Then it adds in the new execution counts and finally writes the data back to the file.
You can report on a subset of functions with --include and --exclude ๐. This is super handy combined with --stdout. You can run a test, look at the gcov output for one particular function, try another input, and then run gcov again to see how the behaviour and coverage change ๐.
$ 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++; }
------------------
gcov matches on mangled names by default, but you can change that with the -M flag ๐. Matching and reporting are independent, so you can match on mangled names while printing demangled names, and vice versa. To report on the "int" instantiation of "Foo" while matching on both mangled and demangled names:
$ 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 filters are extended regular expressions, just like grep -E ๐, which means the pattern in.? matches both inc and main โจ. If you add -M, then every int instantiation of Foo gets matched too ๐. You can use --include and --exclude as many times as you want, and if a name matches more than one filter, the last one to match takes the crown ๐. Here's a look at grabbing main and the int instantiation of inc while leaving out 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
Okay 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 to use alongside it, but if you want to prove that every single line in your program actually ran, don't compile with optimization at the same time ๐. Some machines' optimizers can squish simple code lines together with other lines, so code like this:
if (a != b)
c = 1;
else
c = 0;
can get compiled into one instruction on some machines ๐ง. In that case 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 of code, squished together by the optimizer, ran 100 times. Technically that's correct, since there was only one instruction covering all four lines ๐ค. BUT it doesn't tell you how many of those times the result was 0 and how many times it was 1 ๐ญ.
Inlineable functions can also create unexpected line counts ๐. Line counts get shown for the source code of the inlineable function, but what you see depends on where the function gets inlined, or whether it gets 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 contain out of line bodies of the same inlineable function, they'll both contain coverage counts for it too ๐ฆ. When you link fileA.o and fileB.o together, the linker will, on many systems, pick one of those out of line bodies for all calls to that function and remove or ignore the other one. The annoying part is that it won't remove the coverage counters for the unused body ๐ค. So when instrumented, all but one use of that function will show zero counts.
If the function is inlined in several places, the block structure in each location might not be the same. For instance, a condition might be calculable at compile time in some spots but not others ๐. Since the coverage of all the uses of the inline function gets shown for the same source lines, the line counts themselves might seem inconsistent. Not a bug, just vibes ๐ .
Long-running applications can use the __gcov_reset and __gcov_dump facilities to limit profile collection to the part of the program you actually care about โจ. Calling __gcov_reset(void) clears all run-time profile counters back to zero, and calling __gcov_dump(void) dumps the profile information collected up to that point into .gcda output files ๐. Instrumented applications use a static destructor with priority 99 to invoke __gcov_dump, so __gcov_dump runs after all user defined static destructors, as well as any handlers registered with atexit ๐.
If an executable loads a dynamic shared object through dlopen functionality, you need -Wl,--dynamic-list-data to dump all of 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 file named by the GCOV_ERROR_FILE environment variable if you set it. If you want execution to stop right away when an error happens, set the GCOV_EXIT_AT_ERROR environment variable ๐. That can help you track down profile clashing, which leads to a misleading profile ๐ต๏ธโโ๏ธ.
โจ SEE ALSO โจ
gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for gcc ๐.
๐ COPYRIGHT ๐
Copyright (c) 1996-2026 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."
That's the whole page, bestie! ๐ซถ It's the gcc-16 GCOV(1) page, dated 2026-03-22, so now you're basically a gcov expert ๐โจ
๐ฆ where this page comes from
- ubuntu resolute: gcc-16-powerpc64-linux-gnu 16-20260322-1ubuntu1cross1
- ๐ girlified by claude-haiku-5-5 on : a language model's rewrite of the original, under its terms