GCOV(1)
πΈ GCOV(1) πΈ
okay bestie, welcome to the manual page for GCOV(1) β yep, the GNU one π
this is the gcov page, she's long, she's gorgeous, and she's coming at you in parts. part 1, let's go π¦
π NAME π
gcov β coverage testing tool π
β¨ 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 run her side by side with GCC, and together you get to analyze your programs β make them more efficient, make them faster, and (the fun part) find the parts of your code that nobody has ever actually tested π
you can use gcov as a profiling tool to figure out where your optimization energy is gonna hit hardest. and you can also pair her up with the other profiling tool, gprof, to see which parts of your code are eating the most computing time π
profiling tools help you analyze your code's performance. with a profiler like gcov or gprof, you can get 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 go module by module and see which ones should be optimized. gcov helps you figure out where to actually do the optimization work β¨
software devs also use coverage testing alongside testsuites, to make sure the software is genuinely good enough to ship. testsuites verify a program works as expected; a coverage program checks how much of the program the testsuite actually exercises. then devs can figure out what kinds of test cases need to be added to the testsuites β better testing, better final product, periodt π
if you're planning to use gcov, you should compile your code without optimization, because optimization can combine some lines of code into one function, and that might not give you as much info as you need to hunt for hot spots where the code is burning a ton of computer time. same vibe: because gcov accumulates statistics by line (that's the lowest resolution she works at), she 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 β they only report on the line where the macro call appears π if your complex macros behave like functions, you can swap them for inline functions to fix that.
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 your programs' performance β gprof gives you timing info you can use together with what gcov hands you π«Ά
gcov works only on code compiled with GCC. she is not compatible with any other profiling or test coverage mechanism. that's the deal, no cap.
π OPTIONS π
-a,--all-blocksβ write individual execution counts for every basic block. normallygcovonly spits out execution counts for the main blocks of a line, but with this you can clock whether blocks inside a single line aren't being executed π-b,--branch-probabilitiesβ write branch frequencies to the output file, and write branch summary info to standard output. this lets you see how often each branch in your program got taken. unconditional branches stay hidden unless you also pass-u.-c,--branch-countsβ write branch frequencies as the number of branches taken, instead of the percentage of branches taken π’-g,--conditionsβ write condition coverage to the output file, and write condition summary info to standard output. this lets you see whether the conditions in your program at least once had an independent effect on the outcome of the boolean expression (that's modified condition/decision coverage, iykyk). heads up: this requires you to compile the source with-fcondition-coverage.-e,--prime-pathsβ write path coverage to the output file, and write path summary info to 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 prime if it's a simple path of maximal length. for regular output this option only includes the number of paths covered. for finer-grained path info you can use--prime-paths-linesor--prime-paths-source. with--json-format, all path details get included in the output. this requires you to compile the source with-fpath-coverage.--prime-paths-lines [=type]β write path coverage to the output file, and write path summary info to standard output. this shows you 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 omitted, or one of:uncoveredβ include the uncovered (not taken) paths. this is the default.coveredβ include the covered (taken) paths.bothβ include 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 this means: to cover path 2 you must run lines 8, 11, 13, 14, and 17, evaluating the decision at 8 as false and the decisions at 11 and 13 as "false". slay π«‘
--prime-paths-source [=type]β write path coverage to the output file, and write path summary info to standard output. this shows you 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 great for understanding paths and the interactions between sections of your program. type may be omitted, or one of:uncoveredβ include the uncovered (not taken) paths. this is the default.coveredβ include the covered (taken) paths.bothβ include 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 (peep -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β display the progress on standard output.-f,--function-summariesβ output summaries for each function, in addition to the file-level summary.--include regexβ include 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 a--includeis used then only functions matching the include get reported.--exclude regexβ exclude functions matching regex. this makesgcovnot report 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 then functions matching it get left out π-h,--helpβ display help about usinggcov(on standard output), and exit without doing any further processing. the "ok bye" button π-j,--json-formatβ output thegcovfile in an easy-to-parse JSON intermediate format which does not require source code for generation. the JSON file is compressed with the gzip compression algorithm and the files have the.gcov.json.gzextension.
the structure of the JSON is like this:
{
"current_working_directory": "foo/bar",
"data_file": "a.out",
"format_version": "2",
"gcc_version": "11.1.1 20210510"
"files": ["$file"]
}
fields of the root element have the following semantics:
current_working_directory: working directory where a compilation unit was compileddata_file: name of the data file (GCDA)format_version: semantic version of the format
changes in version 2:
calls: information about function calls is added
gcc_version: version of the GCC compiler
each file has the following form:
{
"file": "a.c",
"functions": ["$function"],
"lines": ["$line"]
}
fields of the file element have the following semantics:
file_name: 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 fields in that function element mean this π
- **`blocks`** β how many blocks live inside 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 ends
- **`end_line`** β the line in the source file where the function ends
- **`execution_count`** β how many times the function got executed
- **`name`** β name of the function
- **`start_column`** β the column in the source file where the function begins
- **`start_line`** β the line in the source file where the function begins
<pink>heads up bestie:</pink> line numbers and column numbers start counting from **1**. And in the current implementation, `start_line` and `start_column` do <red>not</red> include any template parameters or the leading return type β but that's probably getting fixed later, so like, don't hardcode your whole life around it π
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 when you pass `-b`. And the fields of the line element mean:
- **`block_ids`** β IDs of the basic blocks that belong to this line
- **`count`** β how many times the line got executed
- **`line_number`** β the line number
- **`unexecuted_block`** β a flag for whether the line contains an unexecuted block (so like, not every statement on that line ran)
- **`function_name`** β the name of the function this line belongs to (for a line that has inlined statements, it's allowed to be unset)
Each branch is giving:
{ "count": 11, "destination_block_id": 17, "fallthrough": true, "source_block_id": 13, "throw": false }
Fields of the branch element:
- **`count`** β how many times the branch got executed
- **`fallthrough`** β `true` when the branch is a fall through branch
- **`throw`** β `true` when the branch is an exceptional branch
- **`isource_block_id`** β ID of the basic block where this branch happens
- **`destination_block_id`** β ID of the basic block this branch jumps to
Each call goes:
{ "destination_block_id": 1, "returned": 11, "source_block_id": 13 }
Fields of the call element:
- **`returned`** β number of times a function call returned (the call count is equal to `line::count`, no cap)
- **`isource_block_id`** β ID of the basic block where this call happens
- **`destination_block_id`** β ID of the basic block this call continues to after it returns
Each condition looks like:
{ "count": 4, "covered": 2, "not_covered_false": [], "not_covered_true": [0, 1], }
Fields of the condition element:
- **`count`** β number of condition outcomes in this expression
- **`covered`** β number of *covered* condition outcomes in this expression
- **`not_covered_true`** β terms, by index, that were never seen as true in this expression
- **`not_covered_false`** β terms, by index, that were never seen as false in this expression
---
- **`-H`, `--human-readable`** β write those counts in a human readable format, like `24.6k` π
so much easier on the eyes
- **`-k`, `--use-colors`** β colors for lines of code that have <red>zero</red> coverage. Red is for non-exceptional lines, cyan is for exceptional ones. Same colors get used for basic blocks when you pass `-a` β¨
- **`-l`, `--long-file-names`** β makes long file names for included source files. Example: if the header file `x.h` contains code and it got included in `a.c`, then running gcov on `a.c` produces an output file called `a.c##x.h.gcov` instead of just `x.h.gcov`. Iconic when `x.h` is included in a bunch of source files and you wanna see each individual contribution π«Ά. And if you also use `-p`, then *both* the including and the included file names become complete path names.
- **`-m`, `--demangled-names`** β show demangled function names in the output. Default is to show the mangled ones, so this is the glow-up β¨
- **`-M`, `--filter-on-demangled`** β makes `--include` and `--exclude` match against demangled names. It only affects the matching, and it does **not** imply `--demangled-names` β but you can safely combine them, they're besties fr fr π
- **`-n`, `--no-output`** β don't create the gcov output file. Simple as that π
- **`-o` *directory*|*file***, **`--object-directory` *directory***, **`--object-file` *file*** β tell it either the directory that holds the gcov data files, or the object path name. The `.gcno` and `.gcda` data files get searched for using this option. If you hand it a directory, the data files are in that directory and named after the input file name, minus its extension. If you hand 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 this, only the filename component gets used. With it, all the directories are used, with `/` characters translated to `#` characters, `.` directory components removed, and unremovable `..` components renamed to `^`. Super useful when your source files live in a bunch of different directories π
- **`-q`, `--use-hotness-colors`** β emit perf-like colored output for the hot lines π₯. A legend of the color scale gets printed right 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 there is normally uninteresting, so like, skip π§
- **`-s` *directory***, **`--source-prefix` *directory*** β a prefix for source file names to remove when it generates the output coverage files. Great when you build in a separate directory and you don't want the pathname to the source directory mucking up your output file names. <yellow>note:</yellow> this prefix detection happens *before* it decides whether the source file is absolute.
- **`-t`, `--stdout`** β output goes to standard output instead of into output files πΊ
- **`-u`, `--unconditional-branches`** β when branch probabilities are given, also include the unconditional branches. Unconditional branches are normally not interesting, just so you know.
- **`-v`, `--version`** β show the gcov version number (on the standard output) and exit without doing any further processing, periodt π
- **`-w`, `--verbose`** β print verbose information all about the basic blocks and arcs.
- **`-x`, `--hash-filenames`** β when you use `--preserve-paths`, gcov uses the full pathname of the source files to create an output filename. That can lead to long filenames that can overflow filesystem limits π. This option creates names of the form `source-file##md5.gcov`, where the `source-file` component is the final filename part and the `md5` component is calculated from the full mangled name that would have been used otherwise. It's an alternative to `--preserve-paths` on systems that have a filesystem limit.
---
okay important vibe check π: you should run gcov with the current directory the same as the one you were in when you invoked the compiler. Otherwise it will not be able to locate the source files. Fr fr.
gcov produces files called `mangledname.gcov` in the current directory. These contain the coverage information of the source file they correspond to. One `.gcov` file is produced for each source (or header) file containing code that 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 be something more complicated if you gave the `-l` or `-p` options β go peek at those for the deets.
If you invoke gcov with multiple input files, the contributions from each input file get summed. Typically you'd invoke it with the same list of files as the final link of your executable. Slay.
The `.gcov` files contain `:` separated fields along with the program source code. The format is:
<execution_count>:<line_number>:<source line text>
Additional block information may follow each line, when requested by a command line option. The `execution_count` is `-` for lines containing no code. Unexecuted lines are marked `#####` or `=====`, depending on whether they're reachable by non-exceptional paths or only exceptional paths (like C++ exception handlers), respectively. Given the `-a` option, unexecuted blocks are marked `$$$$$` or `%%%%%`, depending on whether a basic block is reachable via non-exceptional or exceptional paths. Executed basic blocks that have a statement with zero `execution_count` end with a `*` character and get colored magenta with the `-k` option π. <red>heads up:</red> this functionality is not supported in Ada.
Also, GCC can completely remove the bodies of functions that aren't needed β for instance if they got inlined everywhere. Those functions get marked with `-`, which can be confusing ngl. Use the `-fkeep-inline-functions` and `-fkeep-static-functions` options to retain these functions and let gcov properly show their `execution_count` π
Some lines of information at the start have a `line_number` of zero. These preamble lines look like:
-:0:<tag>:<value>
The ordering and number of these preamble lines will be augmented as gcov development progresses β <red>do not rely on them remaining unchanged</red>, bestie. Use `tag` to locate a particular preamble line π
The additional block information is of the form:
<tag> <information>
The information is human readable, but designed to be simple enough for machine parsing too β it's giving bilingual β¨
When printing percentages, `0%` and `100%` are only printed when the values are *exactly* `0%` and `100%` respectively. Other values that would conventionally be rounded to `0%` or `100%` instead get printed as the nearest non-boundary value. No rounding drama here π
# β¨ INVOKING GCOV β¨
okay bestie, so here's the prerequisite, no skipping it π
β before you can do **anything** with gcov, you have to compile your program using a special GCC option called `--coverage`.
What does that lil guy actually do? Two things, and I need you to lock in π:
- it tells the compiler to generate the extra info that gcov needs β basically <purple>a flow graph of the program</purple>, that's the whole vibe πΈ
- it also slides additional code into the object files, because that's what lets the extra profiling information gcov wants actually get generated
Those extra files? They get placed in <mint>the directory where the object file is located</mint>. π β¨
Then you just... run the program, and profile output happens. For every single source file compiled with `-fprofile-arcs`, an accompanying `.gcda` file shows up in the object file directory. Every. Single. One. π
And now the main character moment: run gcov with your program's source file names as arguments, and it gives you a listing of the code along with the frequency of execution for each line. π
So say your program is called `tmp.cpp` β here's what you get from 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` contains the output from gcov. Here's a sample, feast your eyes π:
-: 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:}
okay so real talk, notice that **line 7 shows up in the report multiple times** β and that's not a glitch, bestie. π The first occurrence presents the total number of executions of that line, and the next two belong to the instances of the class `Foo` constructors. Also, clock line 30: it contains some unexecuted basic blocks, which is why its execution count has an <yellow>asterisk symbol</yellow> on it. β
---
When you use the `-a` option, you get individual block counts instead, 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 is only shown on <hotpink>one line</hotpink> β specifically, the last line of the block. Here's the tea on how that plays out:
- a multi-line block will only contribute to the execution count of that last line
- the other lines will **not** be shown to contain code, unless previous blocks end on those lines
- the total execution count of a line is shown, and then the subsequent lines show the execution counts for the individual blocks that end on that line
- after each block, the branch and call counts of that block get shown β but only if the `-b` option is given
And because of the way GCC instruments calls, a call count can show up after a line with no individual blocks. Which is exactly what's happening with line 33 up there β it 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 you three lil facts, and they're all <cyan>iconic</cyan>:
1. how many times the function is called
2. how many times it returns
3. what percentage of the function's blocks were executed
periodt. π«Ά
okay so for every basic block, a line gets printed right after the last line of that basic block, and itβs spilling the tea on the branch or call that ends it. β¨
If there are <pink>multiple basic blocks</pink> that end on a single source line, you can get multiple branches and calls listed for that one line. In that case, each branch and call gets its own number. And ngl, thereβs no simple way to map these branches and calls back to source constructs. In general, though, the lowest numbered branch or call will correspond to the leftmost construct on the source line. π
For a branch: if it was executed at least once, you get a percentage showing the number of times the branch was taken divided by the number of times the branch was executed. Otherwise, the message `"never executed"` is printed. π
For a call: if it was executed at least once, you get a percentage showing the number of times the call returned divided by the number of times the call was executed. This will usually be `100%`, but may be less for functions that call `"exit"` or `"longjmp"`, and thus may not return every time they are 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 the number of taken and total outcomes are printed, and if there are uncovered outcomes a line will be printed for each condition showing the uncovered outcome in parentheses. Conditions are identified by their index -- index 0 is the left-most condition. In `"a || (b && c)"`, `a` is condition 0, `b` condition 1, and `c` condition 2. π
An outcome is considered covered if it has an independent effect on the decision, also known as masking MC/DC (Modified Condition/Decision Coverage). In this example the decision evaluates to true and `a` is evaluated, but not covered. This is because `a` cannot affect the decision independently -- both `a` and `b` must change value for the decision to change. π§
$ 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 useful to figure out roughly where coverage is missing and testing how different inputs change the coverage. The `--prime-paths-source` is 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` will print 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 useful for understanding the paths, in particular those that are hard to cover or even unreachable. Lines may be repeated, for example the `"for"` loop, if the same line is a part of multiple basic blocks. This mode is intended for humans and good at understanding what code is exercised under testing or for given inputs. This output is quite verbose, and for focusing on specific functions it can be combined 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 printed on a single line. This mode provides a good overview over the paths and for tracking how different tests and inputs exercises the code. β¨
The execution counts are cumulative. If the example program were executed again without removing the `.gcda` file, the count for the number of times each line in the source was executed would be added to the results of the previous run(s). This is potentially useful in several ways. For example, it could be used to accumulate data over a number of program runs as part of a test verification suite, or to provide more accurate long-term information over a large number of program runs. π
The data in the `.gcda` files is saved immediately before the program exits. For each source file compiled with `-fprofile-arcs`, the profiling code first attempts to read in an existing `.gcda` file; if the file doesnβt match the executable (differing number of basic block counts) it will ignore the contents of the file. It then adds in the new execution counts and finally writes the data to the file. πΎ
You can report on a subset of functions by using `--include` and `--exclude`. This is very useful when combined with `--stdout` trying to understand behavior and coverage for a particular function by running a test, looking at `gcov` output, testing another input, and running `gcov` again. π―
$ 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` will match on mangled names by default, which you can control with the `-M` flag. Note that 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"` matching on 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 bestie, the args you hand to `--include` and `--exclude` are **extended regular expressions** (literally the same flavour as `grep -E`), which means a pattern like `in.?` matches <cyan>"inc"</cyan> AND <cyan>"main"</cyan> in one go. β¨ And if you're rocking it with `-M`? Then all the <purple>"int"</purple> instantiations of <purple>"Foo"</purple> would match too. You can use `--include` and `--exclude` as many times as you want, and if a name matches more than one filter, <hotpink>the last filter to match wins</hotpink>. π
So for example, to match <cyan>"main"</cyan> and the <cyan>"int"</cyan> instantiation of <cyan>"inc"</cyan>, while ditching the <cyan>"Foo"</cyan> 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 π¦
So if your whole plan is to use `gcov` to help you **optimize** your code, you have to first compile your program with a special GCC option: `--coverage`. <mint>No cap, that one's mandatory.</mint> Other than that you can use literally any other GCC options you want β BUT. π«Ά And it's a big but. If you want to <rainbow>prove</rainbow> that every single line of your program actually ran, you should **not** compile with optimization at the same time. On some machines the optimizer will just eliminate simple code lines by merging them into other lines. π
Like, code that looks like this:
if (a != b) c = 1; else c = 0;
can get compiled into a <italic>single</italic> instruction on certain machines. And in that case there is genuinely **no way** for `gcov` to compute separate execution counts per line, because there isn't separate code per line. π So the `gcov` output ends up looking like this if you compiled with optimization on:
100: 12:if (a != b) 100: 13: c = 1; 100: 14:else 100: 15: c = 0;
See how it says this whole block, combined by optimization, executed 100 times? In one sense that's <green>correct</green> β there was only one instruction representing all four of these lines. But <red>the output does not tell you how many times the result was 0 and how many times it was 1.</red> Info lost, bestie. Gone. π
### π Inlineable functions, the chaos era
Inlineable functions can create some genuinely unexpected line counts. Line counts get shown for the source code of the inlineable function, but <hotpink>what exactly is shown depends on where the function gets inlined β or if it doesn't get inlined at all.</hotpink>
- If the function is **not** inlined, the compiler has to emit an out of line copy of it, in whatever object file needs it. And if `fileA.o` and `fileB.o` both contain out of line bodies of a particular inlineable function, they'll both contain coverage counts for that function too. When `fileA.o` and `fileB.o` get linked 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. <red>Unfortunately it will not remove the coverage counters for the unused function body.</red> So when it's instrumented, all but one use of that function will show **zero counts**. π
- If the function gets inlined in several places, the block structure at each location might not be the same. Like, a condition might become calculable at compile time in some instances. And because the coverage of *all* the uses of the inline function is shown for the *same* source lines, the line counts themselves might look inconsistent. π€·ββοΈ
### π Long-running apps, this one's for you
Long-running applications can use the `__gcov_reset` and `__gcov_dump` facilities to restrict profile collection to just the program region of interest. Periodt. π
- Calling `__gcov_reset(void)` clears **all** run-time profile counters to zero.
- Calling `__gcov_dump(void)` causes the profile info collected at that exact point to be dumped out to `.gcda` output files.
Instrumented applications use a static destructor with priority `99` to invoke the `__gcov_dump` function. Which means `__gcov_dump` gets executed **after** all user defined static destructors, and also after any handlers registered with `atexit`. π
> Also important: if an executable loads a dynamic shared object via `dlopen` functionality, `-Wl,--dynamic-list-data` is needed to dump all profile data. Don't skip it, bestie. π«Ά
### π Errors, because of course
The profiling run-time library reports various errors related to profile manipulation and profile saving. Those errors get printed to <cyan>standard error output</cyan>, or to the `GCOV_ERROR_FILE` file if that environment variable is used. And if you want to **terminate immediately** after an error occurs, set the `GCOV_EXIT_AT_ERROR` environment variable. β That one can help users find profile clashing, which is what leads to a misleading profile. iykyk. π₯
---
# π SEE ALSO π
`gpl(7)`, `gfdl(7)`, `fsf-funding(7)`, `gcc(1)` and the Info entry for `gcc`.
---
# π COPYRIGHT π
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; with the Invariant Sections being <purple>"GNU General Public License"</purple> and <purple>"Funding Free Software"</purple>, the Front-Cover texts being (a) (see below), and with the Back-Cover Texts being (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.
---
aaaand that's a wrap bestie!! π¦β¨ this lil masterpiece is `gcc-15.3.0`, dated `2026-06-12`, and it's the `GCOV(1)` girlies' page. go forth and count those lines, you ate. π
π
π¦ where this page comes from
- ubuntu stonking: gcc-15-powerpc-linux-gnu 15.3.0-4ubuntu1cross1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms