1 @c Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2 @c 2008, 2010 Free Software Foundation, Inc.
3 @c This is part of the GCC manual.
4 @c For copying conditions, see the file gcc.texi.
8 Copyright @copyright{} 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004,
9 2005, 2008, 2010 Free Software Foundation, Inc.
11 Permission is granted to copy, distribute and/or modify this document
12 under the terms of the GNU Free Documentation License, Version 1.3 or
13 any later version published by the Free Software Foundation; with the
14 Invariant Sections being ``GNU General Public License'' and ``Funding
15 Free Software'', the Front-Cover texts being (a) (see below), and with
16 the Back-Cover Texts being (b) (see below). A copy of the license is
17 included in the gfdl(7) man page.
19 (a) The FSF's Front-Cover Text is:
23 (b) The FSF's Back-Cover Text is:
25 You have freedom to copy and modify this GNU Manual, like GNU
26 software. Copies published by the Free Software Foundation raise
27 funds for GNU development.
29 @c Set file name and title for the man page.
31 @settitle coverage testing tool
35 @chapter @command{gcov}---a Test Coverage Program
37 @command{gcov} is a tool you can use in conjunction with GCC to
38 test code coverage in your programs.
41 * Gcov Intro:: Introduction to gcov.
42 * Invoking Gcov:: How to use gcov.
43 * Gcov and Optimization:: Using gcov with GCC optimization.
44 * Gcov Data Files:: The files used by gcov.
45 * Cross-profiling:: Data file relocation.
49 @section Introduction to @command{gcov}
50 @c man begin DESCRIPTION
52 @command{gcov} is a test coverage program. Use it in concert with GCC
53 to analyze your programs to help create more efficient, faster running
54 code and to discover untested parts of your program. You can use
55 @command{gcov} as a profiling tool to help discover where your
56 optimization efforts will best affect your code. You can also use
57 @command{gcov} along with the other profiling tool, @command{gprof}, to
58 assess which parts of your code use the greatest amount of computing
61 Profiling tools help you analyze your code's performance. Using a
62 profiler such as @command{gcov} or @command{gprof}, you can find out some
63 basic performance statistics, such as:
67 how often each line of code executes
70 what lines of code are actually executed
73 how much computing time each section of code uses
76 Once you know these things about how your code works when compiled, you
77 can look at each module to see which modules should be optimized.
78 @command{gcov} helps you determine where to work on optimization.
80 Software developers also use coverage testing in concert with
81 testsuites, to make sure software is actually good enough for a release.
82 Testsuites can verify that a program works as expected; a coverage
83 program tests to see how much of the program is exercised by the
84 testsuite. Developers can then determine what kinds of test cases need
85 to be added to the testsuites to create both better testing and a better
88 You should compile your code without optimization if you plan to use
89 @command{gcov} because the optimization, by combining some lines of code
90 into one function, may not give you as much information as you need to
91 look for `hot spots' where the code is using a great deal of computer
92 time. Likewise, because @command{gcov} accumulates statistics by line (at
93 the lowest resolution), it works best with a programming style that
94 places only one statement on each line. If you use complicated macros
95 that expand to loops or to other control structures, the statistics are
96 less helpful---they only report on the line where the macro call
97 appears. If your complex macros behave like functions, you can replace
98 them with inline functions to solve this problem.
100 @command{gcov} creates a logfile called @file{@var{sourcefile}.gcov} which
101 indicates how many times each line of a source file @file{@var{sourcefile}.c}
102 has executed. You can use these logfiles along with @command{gprof} to aid
103 in fine-tuning the performance of your programs. @command{gprof} gives
104 timing information you can use along with the information you get from
107 @command{gcov} works only on code compiled with GCC@. It is not
108 compatible with any other profiling or test coverage mechanism.
113 @section Invoking @command{gcov}
116 gcov @r{[}@var{options}@r{]} @var{sourcefiles}
119 @command{gcov} accepts the following options:
122 @c man begin SYNOPSIS
123 gcov [@option{-v}|@option{--version}] [@option{-h}|@option{--help}]
124 [@option{-a}|@option{--all-blocks}]
125 [@option{-b}|@option{--branch-probabilities}]
126 [@option{-c}|@option{--branch-counts}]
127 [@option{-n}|@option{--no-output}]
128 [@option{-l}|@option{--long-file-names}]
129 [@option{-p}|@option{--preserve-paths}]
130 [@option{-f}|@option{--function-summaries}]
131 [@option{-o}|@option{--object-directory} @var{directory|file}] @var{sourcefiles}
132 [@option{-u}|@option{--unconditional-branches}]
133 [@option{-d}|@option{--display-progress}]
136 gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}.
144 Display help about using @command{gcov} (on the standard output), and
145 exit without doing any further processing.
149 Display the @command{gcov} version number (on the standard output),
150 and exit without doing any further processing.
154 Write individual execution counts for every basic block. Normally gcov
155 outputs execution counts only for the main blocks of a line. With this
156 option you can determine if blocks within a single line are not being
160 @itemx --branch-probabilities
161 Write branch frequencies to the output file, and write branch summary
162 info to the standard output. This option allows you to see how often
163 each branch in your program was taken. Unconditional branches will not
164 be shown, unless the @option{-u} option is given.
167 @itemx --branch-counts
168 Write branch frequencies as the number of branches taken, rather than
169 the percentage of branches taken.
173 Do not create the @command{gcov} output file.
176 @itemx --long-file-names
177 Create long file names for included source files. For example, if the
178 header file @file{x.h} contains code, and was included in the file
179 @file{a.c}, then running @command{gcov} on the file @file{a.c} will produce
180 an output file called @file{a.c##x.h.gcov} instead of @file{x.h.gcov}.
181 This can be useful if @file{x.h} is included in multiple source
182 files. If you use the @samp{-p} option, both the including and
183 included file names will be complete path names.
186 @itemx --preserve-paths
187 Preserve complete path information in the names of generated
188 @file{.gcov} files. Without this option, just the filename component is
189 used. With this option, all directories are used, with @samp{/} characters
190 translated to @samp{#} characters, @file{.} directory components
191 removed and @file{..}
192 components renamed to @samp{^}. This is useful if sourcefiles are in several
193 different directories. It also affects the @samp{-l} option.
196 @itemx --function-summaries
197 Output summaries for each function in addition to the file level summary.
199 @item -o @var{directory|file}
200 @itemx --object-directory @var{directory}
201 @itemx --object-file @var{file}
202 Specify either the directory containing the gcov data files, or the
203 object path name. The @file{.gcno}, and
204 @file{.gcda} data files are searched for using this option. If a directory
205 is specified, the data files are in that directory and named after the
206 source file name, without its extension. If a file is specified here,
207 the data files are named after that file, without its extension. If this
208 option is not supplied, it defaults to the current directory.
211 @itemx --unconditional-branches
212 When branch probabilities are given, include those of unconditional branches.
213 Unconditional branches are normally not interesting.
216 @itemx --display-progress
217 Display the progress on the standard output.
221 @command{gcov} should be run with the current directory the same as that
222 when you invoked the compiler. Otherwise it will not be able to locate
223 the source files. @command{gcov} produces files called
224 @file{@var{mangledname}.gcov} in the current directory. These contain
225 the coverage information of the source file they correspond to.
226 One @file{.gcov} file is produced for each source file containing code,
227 which was compiled to produce the data files. The @var{mangledname} part
228 of the output file name is usually simply the source file name, but can
229 be something more complicated if the @samp{-l} or @samp{-p} options are
230 given. Refer to those options for details.
232 The @file{.gcov} files contain the @samp{:} separated fields along with
233 program source code. The format is
236 @var{execution_count}:@var{line_number}:@var{source line text}
239 Additional block information may succeed each line, when requested by
240 command line option. The @var{execution_count} is @samp{-} for lines
241 containing no code and @samp{#####} for lines which were never executed.
242 Some lines of information at the start have @var{line_number} of zero.
244 The preamble lines are of the form
247 -:0:@var{tag}:@var{value}
250 The ordering and number of these preamble lines will be augmented as
251 @command{gcov} development progresses --- do not rely on them remaining
252 unchanged. Use @var{tag} to locate a particular preamble line.
254 The additional block information is of the form
257 @var{tag} @var{information}
260 The @var{information} is human readable, but designed to be simple
261 enough for machine parsing too.
263 When printing percentages, 0% and 100% are only printed when the values
264 are @emph{exactly} 0% and 100% respectively. Other values which would
265 conventionally be rounded to 0% or 100% are instead printed as the
266 nearest non-boundary value.
268 When using @command{gcov}, you must first compile your program with two
269 special GCC options: @samp{-fprofile-arcs -ftest-coverage}.
270 This tells the compiler to generate additional information needed by
271 gcov (basically a flow graph of the program) and also includes
272 additional code in the object files for generating the extra profiling
273 information needed by gcov. These additional files are placed in the
274 directory where the object file is located.
276 Running the program will cause profile output to be generated. For each
277 source file compiled with @option{-fprofile-arcs}, an accompanying
278 @file{.gcda} file will be placed in the object file directory.
280 Running @command{gcov} with your program's source file names as arguments
281 will now produce a listing of the code along with frequency of execution
282 for each line. For example, if your program is called @file{tmp.c}, this
283 is what you see when you use the basic @command{gcov} facility:
286 $ gcc -fprofile-arcs -ftest-coverage tmp.c
289 90.00% of 10 source lines executed in file tmp.c
293 The file @file{tmp.c.gcov} contains output from @command{gcov}.
302 -: 1:#include <stdio.h>
310 11: 9: for (i = 0; i < 10; i++)
313 1: 12: if (total != 45)
314 #####: 13: printf ("Failure\n");
316 1: 15: printf ("Success\n");
321 When you use the @option{-a} option, you will get individual block
322 counts, and the output looks like this:
330 -: 1:#include <stdio.h>
339 11: 9: for (i = 0; i < 10; i++)
344 1: 12: if (total != 45)
346 #####: 13: printf ("Failure\n");
349 1: 15: printf ("Success\n");
356 In this mode, each basic block is only shown on one line -- the last
357 line of the block. A multi-line block will only contribute to the
358 execution count of that last line, and other lines will not be shown
359 to contain code, unless previous blocks end on those lines.
360 The total execution count of a line is shown and subsequent lines show
361 the execution counts for individual blocks that end on that line. After each
362 block, the branch and call counts of the block will be shown, if the
363 @option{-b} option is given.
365 Because of the way GCC instruments calls, a call count can be shown
366 after a line with no individual blocks.
367 As you can see, line 13 contains a basic block that was not executed.
370 When you use the @option{-b} option, your output looks like this:
374 90.00% of 10 source lines executed in file tmp.c
375 80.00% of 5 branches executed in file tmp.c
376 80.00% of 5 branches taken at least once in file tmp.c
377 50.00% of 2 calls executed in file tmp.c
381 Here is a sample of a resulting @file{tmp.c.gcov} file:
389 -: 1:#include <stdio.h>
392 function main called 1 returned 1 blocks executed 75%
398 11: 9: for (i = 0; i < 10; i++)
399 branch 0 taken 91% (fallthrough)
403 1: 12: if (total != 45)
404 branch 0 taken 0% (fallthrough)
406 #####: 13: printf ("Failure\n");
407 call 0 never executed
409 1: 15: printf ("Success\n");
410 call 0 called 1 returned 100%
415 For each function, a line is printed showing how many times the function
416 is called, how many times it returns and what percentage of the
417 function's blocks were executed.
419 For each basic block, a line is printed after the last line of the basic
420 block describing the branch or call that ends the basic block. There can
421 be multiple branches and calls listed for a single source line if there
422 are multiple basic blocks that end on that line. In this case, the
423 branches and calls are each given a number. There is no simple way to map
424 these branches and calls back to source constructs. In general, though,
425 the lowest numbered branch or call will correspond to the leftmost construct
428 For a branch, if it was executed at least once, then a percentage
429 indicating the number of times the branch was taken divided by the
430 number of times the branch was executed will be printed. Otherwise, the
431 message ``never executed'' is printed.
433 For a call, if it was executed at least once, then a percentage
434 indicating the number of times the call returned divided by the number
435 of times the call was executed will be printed. This will usually be
436 100%, but may be less for functions that call @code{exit} or @code{longjmp},
437 and thus may not return every time they are called.
439 The execution counts are cumulative. If the example program were
440 executed again without removing the @file{.gcda} file, the count for the
441 number of times each line in the source was executed would be added to
442 the results of the previous run(s). This is potentially useful in
443 several ways. For example, it could be used to accumulate data over a
444 number of program runs as part of a test verification suite, or to
445 provide more accurate long-term information over a large number of
448 The data in the @file{.gcda} files is saved immediately before the program
449 exits. For each source file compiled with @option{-fprofile-arcs}, the
450 profiling code first attempts to read in an existing @file{.gcda} file; if
451 the file doesn't match the executable (differing number of basic block
452 counts) it will ignore the contents of the file. It then adds in the
453 new execution counts and finally writes the data to the file.
455 @node Gcov and Optimization
456 @section Using @command{gcov} with GCC Optimization
458 If you plan to use @command{gcov} to help optimize your code, you must
459 first compile your program with two special GCC options:
460 @samp{-fprofile-arcs -ftest-coverage}. Aside from that, you can use any
461 other GCC options; but if you want to prove that every single line
462 in your program was executed, you should not compile with optimization
463 at the same time. On some machines the optimizer can eliminate some
464 simple code lines by combining them with other lines. For example, code
475 can be compiled into one instruction on some machines. In this case,
476 there is no way for @command{gcov} to calculate separate execution counts
477 for each line because there isn't separate code for each line. Hence
478 the @command{gcov} output looks like this if you compiled the program with
488 The output shows that this block of code, combined by optimization,
489 executed 100 times. In one sense this result is correct, because there
490 was only one instruction representing all four of these lines. However,
491 the output does not indicate how many times the result was 0 and how
492 many times the result was 1.
494 Inlineable functions can create unexpected line counts. Line counts are
495 shown for the source code of the inlineable function, but what is shown
496 depends on where the function is inlined, or if it is not inlined at all.
498 If the function is not inlined, the compiler must emit an out of line
499 copy of the function, in any object file that needs it. If
500 @file{fileA.o} and @file{fileB.o} both contain out of line bodies of a
501 particular inlineable function, they will also both contain coverage
502 counts for that function. When @file{fileA.o} and @file{fileB.o} are
503 linked together, the linker will, on many systems, select one of those
504 out of line bodies for all calls to that function, and remove or ignore
505 the other. Unfortunately, it will not remove the coverage counters for
506 the unused function body. Hence when instrumented, all but one use of
507 that function will show zero counts.
509 If the function is inlined in several places, the block structure in
510 each location might not be the same. For instance, a condition might
511 now be calculable at compile time in some instances. Because the
512 coverage of all the uses of the inline function will be shown for the
513 same source lines, the line counts themselves might seem inconsistent.
517 @node Gcov Data Files
518 @section Brief description of @command{gcov} data files
520 @command{gcov} uses two files for profiling. The names of these files
521 are derived from the original @emph{object} file by substituting the
522 file suffix with either @file{.gcno}, or @file{.gcda}. All of these files
523 are placed in the same directory as the object file, and contain data
524 stored in a platform-independent format.
526 The @file{.gcno} file is generated when the source file is compiled with
527 the GCC @option{-ftest-coverage} option. It contains information to
528 reconstruct the basic block graphs and assign source line numbers to
531 The @file{.gcda} file is generated when a program containing object files
532 built with the GCC @option{-fprofile-arcs} option is executed. A
533 separate @file{.gcda} file is created for each object file compiled with
534 this option. It contains arc transition counts, and some summary
537 The full details of the file format is specified in @file{gcov-io.h},
538 and functions provided in that header file should be used to access the
541 @node Cross-profiling
542 @section Data file relocation to support cross-profiling
544 Running the program will cause profile output to be generated. For each
545 source file compiled with @option{-fprofile-arcs}, an accompanying @file{.gcda}
546 file will be placed in the object file directory. That implicitly requires
547 running the program on the same system as it was built or having the same
548 absolute directory structure on the target system. The program will try
549 to create the needed directory structure, if it is not already present.
551 To support cross-profiling, a program compiled with @option{-fprofile-arcs}
552 can relocate the data files based on two environment variables:
556 GCOV_PREFIX contains the prefix to add to the absolute paths
557 in the object file. Prefix can be absolute, or relative. The
558 default is no prefix.
561 GCOV_PREFIX_STRIP indicates the how many initial directory names to strip off
562 the hardwired absolute paths. Default value is 0.
564 @emph{Note:} If GCOV_PREFIX_STRIP is set without GCOV_PREFIX is undefined,
565 then a relative path is made out of the hardwired absolute paths.
568 For example, if the object file @file{/user/build/foo.o} was built with
569 @option{-fprofile-arcs}, the final executable will try to create the data file
570 @file{/user/build/foo.gcda} when running on the target system. This will
571 fail if the corresponding directory does not exist and it is unable to create
572 it. This can be overcome by, for example, setting the environment as
573 @samp{GCOV_PREFIX=/target/run} and @samp{GCOV_PREFIX_STRIP=1}. Such a
574 setting will name the data file @file{/target/run/build/foo.gcda}.
576 You must move the data files to the expected directory tree in order to
577 use them for profile directed optimizations (@option{--use-profile}), or to
578 use the @command{gcov} tool.