Gcov
   HOME

TheInfoList



OR:

Gcov is a source code coverage analysis and statement-by-statement profiling tool. Gcov generates exact counts of the number of times each statement in a program is executed and annotates
source code In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer. Since a computer, at base, only ...
to add instrumentation. Gcov comes as a standard utility with the
GNU Compiler Collection The GNU Compiler Collection (GCC) is a collection of compilers from the GNU Project that support various programming languages, Computer architecture, hardware architectures, and operating systems. The Free Software Foundation (FSF) distributes ...
(GCC) suite. The gcov utility gives information on how often a
program Program (American English; also Commonwealth English in terms of computer programming and related activities) or programme (Commonwealth English in all other meanings), programmer, or programming may refer to: Business and management * Program m ...
executes segments of code. It produces a copy of the source file, annotated with execution frequencies. The gcov utility does not produce any time-based data and works only on code compiled with the GCC suite. The manual claims it is not compatible with any other profiling or test coverage mechanism, but it works with
llvm LLVM, also called LLVM Core, is a target-independent optimizer and code generator. It can be used to develop a Compiler#Front end, frontend for any programming language and a Compiler#Back end, backend for any instruction set architecture. LLVM i ...
-generated files too.


Description

gcov produces a test coverage analysis of a specially instrumented
program Program (American English; also Commonwealth English in terms of computer programming and related activities) or programme (Commonwealth English in all other meanings), programmer, or programming may refer to: Business and management * Program m ...
. The options -fprofile-arcs -ftest-coverage should be used to compile the program for coverage analysis (first option to record branch statistics and second to save line execution count); -fprofile-arcs should also be used to link the program. After running such program will create several files with ".bb" ".bbg" and ".da" extensions (suffixes), which can be analysed by gcov. It takes source files as command-line arguments and produces an annotated source listing. Each line of source code is prefixed with the number of times it has been executed; lines that have not been executed are prefixed with "#####". gcov creates a logfile called ''sourcefile.gcov'' which indicates how many times each line of a source file ''sourcefile.c'' has executed. This annotated source file can be used with
gprof Gprof is a performance analysis tool for Unix applications. It used a hybrid of instrumentation and samplingSusan L. Graham, Peter B. Kessler, and Marshall K. Mckusick''gprof: a Call Graph Execution Profiler''// Proceedings of the SIGPLAN '82 Sy ...
, another profiling tool, to extract timing information about the program.


Example

The following program, written in C, loops over the integers 1 to 9 and tests their divisibility with the modulus (%) operator. #include int main (void) To enable coverage testing the program must be compiled with the following options: $ gcc -Wall -fprofile-arcs -ftest-coverage cov.c where cov.c is the name of the program file. This creates an instrumented
executable In computer science, executable code, an executable file, or an executable program, sometimes simply referred to as an executable or binary, causes a computer "to perform indicated tasks according to encoded instruction (computer science), in ...
which contains additional instructions that record the number of times each line of the program is executed. The option adds instructions for counting the number of times individual lines are executed, while incorporates instrumentation code for each branch of the program. Branch instrumentation records how frequently different paths are taken through ‘if’ statements and other conditionals. The
executable In computer science, executable code, an executable file, or an executable program, sometimes simply referred to as an executable or binary, causes a computer "to perform indicated tasks according to encoded instruction (computer science), in ...
can then be run to analyze the code and create the
coverage data A coverage is the digital representation of some spatio-temporal phenomenon. ISO 19123 provides the definition: * '' feature that acts as a function to return values from its range for any direct position within its spatial, temporal or spatiote ...
. $ ./a.out The data from the run is written to several ''coverage data files'' with the extensions ‘.bb’ ‘.bbg’ and ‘.da’ respectively in the current directory. If the program execution varies based on the input parameters or data, it can be run multiple times and the results will accumulate in the ''coverage data files'' for overall analysis. This data can be analyzed using the gcov command and the name of a source file: $ gcov cov.c 88.89% of 9 source lines executed in file cov.c Creating cov.c.gcov The ''gcov'' command produces an annotated version of the original
source file In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer. Since a computer, at base, onl ...
, with the file extension ‘.gcov’, containing counts of the number of times each line was executed: #include int main (void) The line counts can be seen in the first column of the output. Lines which were not executed are marked with hashes ‘######’.


Command-line options

Gcov command line utility supports following options while generating annotated files from profile data: *''-h'' (''--help''): Display help about using gcov (on the standard output), and exit without doing any further processing. *''-v'' (''--version''): Display the gcov version number (on the standard output), and exit without doing any further processing. *''-a'' (''--all-blocks''): Write individual execution counts for every
basic block In compiler construction, a basic block is a straight-line code sequence with no branches in except to the entry and no branches out except at the exit. This restricted form makes a basic block highly amenable to analysis. Compilers usually decom ...
. Normally gcov outputs execution counts only for the main blocks of a line. With this option you can determine if blocks within a single line are not being executed. *''-b'' (''--branch-probabilities''): Write branch frequencies to the output file, and write branch summary info to the standard output. This option allows you to see how often each branch in your program was taken. Unconditional branches will not be shown, unless the -u option is given. *''-c'' (''--branch-counts''): Write branch frequencies as the number of branches taken, rather than the percentage of branches taken. *''-n'' (''--no-output''): Do not create the gcov output file. *''-l'' (''--long-file-names''): Create long file names for included source files. For example, if the
header file An include directive instructs a text file processor to replace the directive text with the content of a specified file. The act of including may be logical in nature. The processor may simply process the include file content at the location of ...
x.h contains code, and was included in the file 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 be useful 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. *''-p'' (''--preserve-paths''): Preserve complete path information in the names of generated .gcov files. Without this option, just the filename component is used. With this option, all directories are used, with `/' characters translated to `#' characters, . directory components removed and unremoveable .. components renamed to `^'. This is useful if sourcefiles are in several different directories. *''-r'' (''--relative-only''): Only output information about source files with a relative pathname (after source prefix elision). Absolute paths are usually system header files and coverage of any inline functions therein is normally uninteresting. *''-f'' (''--function-summaries''): Output summaries for each function in addition to the file level summary. *''-o directory, file'' (''--object-directory directory'' or ''--object-file file''): Specify either the directory containing the gcov data files, or the object path name. The .gcno, and .gcda data files are searched for using this option. If a directory is specified, the data files are in that directory and named after the input file name, without its extension. If a file is specified here, the data files are named after that file, without its extension. *''-s directory'' (''--source-prefix directory''): A prefix for source file names to remove when generating the output coverage files. This option is useful when building in a separate directory, and the pathname to the source directory is not wanted when determining the output file names. Note that this prefix detection is applied before determining whether the source file is absolute. *''-u'' (''--unconditional-branches''): When branch probabilities are given, include those of unconditional branches. Unconditional branches are normally not interesting. *''-d'' (''--display-progress''): Display the progress on the standard output.


Coverage summaries

Lcov is a graphical front-end for gcov. It collects gcov data for multiple source files and creates
HTML Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
pages containing the source code annotated with coverage information. It also adds overview pages for easy navigation within the file structure. Lcov supports statement, function, and branch coverage measurement. There is also a
Windows version.
Gcovr provides a utility for managing the use of gcov and generating summarized code coverage results. This command is inspired by the Python coverage.py package, which provides a similar utility in Python. Gcovr produces either compact human-readable summary reports, machine readable XML reports or a graphical HTML summary. The XML reports generated by gcovr can be used by
Jenkins Jenkins may refer to: People * Jenkins (name), history of the surname * List of people with surname Jenkins * The Jenkins, country music group Places United States * Jenkins, Illinois *Jenkins, Kentucky * Jenkins, Minnesota * Jenkins, Missour ...
to provide graphical code coverage summaries. Gcovr supports statement and branch coverage measurement SCov is a utility that processes the intermediate text format generated by gcov (using gcov -i) to generate reports on code coverage. These reports can be a simple text report, or
HTML Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
pages with more detailed reports.


See also

*
Tcov Tcov is a source code coverage analysis and statement-by-statement profiling tool for software written in Fortran, C and C++. Tcov generates exact counts of the number of times each statement in a program is executed and annotates source cod ...
– code coverage tool for Solaris provided in Sun Studio suite *
Trucov Trucov is an open source code coverage analysis tool for GNU Compiler Collection, GCC versions 4.0 and later that aims to be a gcov replacement. Trucov improves upon gcov by providing more granular and machine readable output, such as DOT Language, ...
- intended to improve on Gcov with machine readable output


References

{{reflist Software metrics Software testing tools