Linux "gcov" Command Line Options and Examples
coverage testing tool

gcov is a test coverage program. Use it in concert with GCC to analyze your programs to help create more efficient, faster running code and to discover untested parts of your program. You can use gcov as a profiling tool to help discover where your optimization efforts will best affect your code.


gcov [-v|--version] [-h|--help]
[-o|--object-directory directory|file]
[-s|--source-prefix directory]

Command Line Options:

Write individual execution counts for every basic block. Normally gcov outputs execution counts only for the main blocks of aline. With this option you can determine if blocks within a single line are not being executed.
gcov --all-blocks ...
Write branch frequencies to the output file, and write branch summary info to the standard output. This option allows you to seehow often each branch in your program was taken. Unconditional branches will not be shown, unless the -u option is given.
gcov --branch-probabilities ...
Write branch frequencies as the number of branches taken, rather than the percentage of branches taken.
gcov --branch-counts ...
Display the progress on the standard output.
gcov --display-progress ...
Output summaries for each function in addition to the file level summary.
gcov --function-summaries ...
Display help about using gcov (on the standard output), and exit without doing any further processing.
gcov --help ...
Output gcov file in an easy-to-parse intermediate text format that can be used by lcov or other tools. The output is a single.gcov file per .gcda file. No source code is required.The format of the intermediate .gcov file is plain text with one entry per linefile:<source_file_name>function:<line_number>,<execution_count>,<function_name>lcount:<line number>,<execution_count>branch:<line_number>,<branch_coverage_type>Where the <branch_coverage_type> isnotexec (Branch not executed)taken (Branch executed and taken)nottaken (Branch executed, but not taken)There can be multiple <file> entries in an intermediate gcovfile. All entries following a <file> pertain to that source fileuntil the next <file> entry.Here is a sample when -i is used in conjunction with -b option:file:array.ccfunction:11,1,_Z3sumRKSt6vectorIPiSaIS0_EEfunction:22,1,mainlcount:11,1lcount:12,1lcount:14,1branch:14,takenlcount:26,1branch:28,nottaken
gcov --intermediate-format ...
Create long file names for included source files. For example, if the header file x.h contains code, and was included in thefile a.c, then running gcov on the file a.c will produce an output file called a.c##x.h.gcov instead of x.h.gcov. This can beuseful if x.h is included in multiple source files and you want to see the individual contributions. If you use the -p option,both the including and included file names will be complete path names.
gcov --long-file-names ...
Display demangled function names in output. The default is to show mangled function names.
gcov --demangled-names ...
Do not create the gcov output file.
gcov --no-output ...
Specify either the directory containing the gcov data files, or the object path name. The .gcno, and .gcda data files aresearched for using this option. If a directory is specified, the data files are in that directory and named after the input filename, without its extension. If a file is specified here, the data files are named after that file, without its extension.
gcov --object-file ...
Preserve complete path information in the names of generated .gcov files. Without this option, just the filename component isused. With this option, all directories are used, with / characters translated to # characters, . directory components removedand unremoveable .. components renamed to ^. This is useful if sourcefiles are in several different directories.
gcov --preserve-paths ...
Only output information about source files with a relative pathname (after source prefix elision). Absolute paths are usuallysystem header files and coverage of any inline functions therein is normally uninteresting.
gcov --relative-only ...
A prefix for source file names to remove when generating the output coverage files. This option is useful when building in aseparate directory, and the pathname to the source directory is not wanted when determining the output file names. Note thatthis prefix detection is applied before determining whether the source file is absolute.
gcov --source-prefix ...
When branch probabilities are given, include those of unconditional branches. Unconditional branches are normally notinteresting.
gcov --unconditional-branches ...
Display the gcov version number (on the standard output), and exit without doing any further processing.
gcov --version ...
Print verbose informations related to basic blocks and arcs.
gcov --verbose ...
By default, gcov uses the full pathname of the source files to to create an output filename. This can lead to long filenamesthat can overflow filesystem limits. This option creates names of the form source-file##md5.gcov, where the source-filecomponent is the final filename part and the md5 component is calculated from the full mangled name that would have been usedotherwise.gcov should be run with the current directory the same as that when you invoked the compiler. Otherwise it will not be able tolocate the source files. gcov produces files called mangledname.gcov in the current directory. These contain the coverageinformation of the source file they correspond to. One .gcov file is produced for each source (or header) file containing code,which was compiled to produce the data files. The mangledname part of the output file name is usually simply the source file name,but can be something more complicated if the -l or -p options are given. Refer to those options for details.If you invoke gcov with multiple input files, the contributions from each input file are summed. Typically you would invoke it withthe same list of files as the final link of your executable.The .gcov files contain the : separated fields along with program source code. The format is<execution_count>:<line_number>:<source line text>Additional block information may succeed each line, when requested by command line option. The execution_count is - for linescontaining no code. Unexecuted lines are marked ##### or =====, depending on whether they are reachable by non-exceptional paths oronly exceptional paths such as C++ exception handlers, respectively. Given -a option, unexecuted blocks are marked $$$$$ or %%%%%,depending on whether a basic block is reachable via non-exceptional or exceptional paths.Note that GCC can completely remove the bodies of functions that are not needed -- for instance if they are inlined everywhere. Suchfunctions are marked with -, which can be confusing. Use the -fkeep-inline-functions and -fkeep-static-functions options to retainthese functions and allow gcov to properly show their execution_count.Some lines of information at the start have line_number of zero. These preamble lines are of the form
gcov --hash-filenames ...
The ordering and number of these preamble lines will be augmented as gcov development progresses --- do not rely on them remainingunchanged. 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.When printing percentages, 0% and 100% are only printed when the values are exactly 0% and 100% respectively. Other values whichwould conventionally be rounded to 0% or 100% are instead printed as the nearest non-boundary value.When using gcov, you must first compile your program with two special GCC options: -fprofile-arcs -ftest-coverage. This tells thecompiler to generate additional information needed by gcov (basically a flow graph of the program) and also includes additional codein the object files for generating the extra profiling information needed by gcov. These additional files are placed in thedirectory where the object file is located.Running the program will cause profile output to be generated. For each source file compiled with -fprofile-arcs, an accompanying.gcda file will be placed in the object file directory.Running gcov with your program's source file names as arguments will now produce a listing of the code along with frequency ofexecution for each line. For example, if your program is called tmp.c, this is what you see when you use the basic gcov facility:$ gcc -fprofile-arcs -ftest-coverage tmp.c$ a.out$ gcov tmp.cFile 'tmp.c'Lines executed:90.00% of 10Creating 'tmp.c.gcov'The file tmp.c.gcov contains output from gcov. Here is a sample:
gcov -:0:<tag>:<value> ...
gcov -: ...
--dynamic-list-data is needed to dump all profile data.
gcov -Wl ...