6 gcov - coverage testing tool
20 .Fl -branch-probabilities Oc
29 .Fl -long-file-names Oc
32 .Fl -preserve-paths Oc
35 .Fl -function-summaries Oc
38 .Fl -object-directory Ar directory|file Oc
41 .Fl -unconditional-branches Oc
45 is a test coverage program.
46 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.
49 as a profiling tool to help discover where your optimization efforts will best affect your code.
52 along with the other profiling tool,
54 to assess which parts of your code use the greatest amount of computing time.
56 Profiling tools help you analyze your code's performance.
57 Using a profiler such as
61 you can find out some basic performance statistics, such as:
65 how often each line of code executes
68 what lines of code are actually executed
71 how much computing time each section of code uses
74 Once you know these things about how your code works when compiled, you can look at each module to see which modules should be optimized.
76 helps you determine where to work on optimization.
78 Software developers also use coverage testing in concert with testsuites, to make sure software is actually good enough for a release.
79 Testsuites can verify that a program works as expected; a coverage program tests to see how much of the program is exercised by the testsuite.
80 Developers can then determine what kinds of test cases need to be added to the testsuites to create both better testing and a better final product.
82 You should compile your code without optimization if you plan to use
84 because the optimization, by combining some lines of code into one function, may not give you as much information as you need to look for `hot spots' where the code is using a great deal of computer time.
87 accumulates statistics by line (at the lowest resolution), it works best with a programming style that places only one statement on each line.
88 If you use complicated macros that expand to loops or to other control structures, the statistics are less helpful---they only report on the line where the macro call appears.
89 If your complex macros behave like functions, you can replace them with inline functions to solve this problem.
92 creates a logfile called
94 which indicates how many times each line of a source file
97 You can use these logfiles along with
99 to aid in fine-tuning the performance of your programs.
101 gives timing information you can use along with the information you get from
105 works only on code compiled with GCC.
106 It is not compatible with any other profiling or test coverage mechanism.
111 Display help about using
113 (on the standard output), and exit without doing any further processing.
119 version number (on the standard output), and exit without doing any further processing.
123 Write individual execution counts for every basic block.
124 Normally gcov outputs execution counts only for the main blocks of a line.
125 With this option you can determine if blocks within a single line are not being executed.
128 .It Fl -branch-probabilities
129 Write branch frequencies to the output file, and write branch summary info to the standard output.
130 This option allows you to see how often each branch in your program was taken.
131 Unconditional branches will not be shown, unless the
136 .It Fl -branch-counts
137 Write branch frequencies as the number of branches taken, rather than the percentage of branches taken.
146 .It Fl -long-file-names
147 Create long file names for included source files.
148 For example, if the header file
150 contains code, and was included in the file
156 will produce an output file called
160 This can be useful if
162 is included in multiple source files.
165 option, both the including and included file names will be complete path names.
168 .It Fl -preserve-paths
169 Preserve complete path information in the names of generated
172 Without this option, just the filename component is used.
173 With this option, all directories are used, with
175 characters translated to
179 directory components removed and
181 components renamed to
183 This is useful if sourcefiles are in several different directories.
189 .It Fl -function-summaries
190 Output summaries for each function in addition to the file level summary.
192 .It Fl o Ar directory|file
193 .It Fl -object-directory Ar directory
194 .It Fl -object-file Ar file
195 Specify either the directory containing the gcov data files, or the object path name.
200 data files are searched for using this option.
201 If a directory is specified, the data files are in that directory and named after the source file name, without its extension.
202 If a file is specified here, the data files are named after that file, without its extension.
203 If this option is not supplied, it defaults to the current directory.
206 .It Fl -unconditional-branches
207 When branch probabilities are given, include those of unconditional branches.
208 Unconditional branches are normally not interesting.
212 should be run with the current directory the same as that when you invoked the compiler.
213 Otherwise it will not be able to locate the source files.
215 produces files called
217 in the current directory.
218 These contain the coverage information of the source file they correspond to.
221 file is produced for each source file containing code, which was compiled to produce the data files.
224 part of the output file name is usually simply the source file name, but can be something more complicated if the
229 Refer to those options for details.
235 separated fields along with program source code.
238 .Bd -literal -offset indent
239 \*[Lt]execution_count\*[Gt]:\*[Lt]line_number\*[Gt]:\*[Lt]source line text\*[Gt]
241 Additional block information may succeed each line, when requested by command line option.
246 for lines containing no code and
248 for lines which were never executed.
249 Some lines of information at the start have
253 The preamble lines are of the form
255 .Bd -literal -offset indent
256 -:0:\*[Lt]tag\*[Gt]:\*[Lt]value\*[Gt]
258 The ordering and number of these preamble lines will be augmented as
260 development progresses --- do not rely on them remaining unchanged.
263 to locate a particular preamble line.
265 The additional block information is of the form
267 .Bd -literal -offset indent
268 \*[Lt]tag\*[Gt] \*[Lt]information\*[Gt]
272 is human readable, but designed to be simple enough for machine parsing too.
274 When printing percentages, 0% and 100% are only printed when the values are
276 0% and 100% respectively.
277 Other values which would conventionally be rounded to 0% or 100% are instead printed as the nearest non-boundary value.
281 you must first compile your program with two special GCC options:
282 .Fl fprofile-arcs Fl ftest-coverage .
283 This tells the compiler to generate additional information needed by gcov (basically a flow graph of the program) and also includes additional code in the object files for generating the extra profiling information needed by gcov.
284 These additional files are placed in the directory where the object file is located.
286 Running the program will cause profile output to be generated.
287 For each source file compiled with
291 file will be placed in the object file directory.
295 with your program's source file names as arguments will now produce a listing of the code along with frequency of execution for each line.
296 For example, if your program is called
298 this is what you see when you use the basic
302 .Bd -literal -offset indent
303 $ gcc -fprofile-arcs -ftest-coverage tmp.c
306 90.00% of 10 source lines executed in file tmp.c
315 .Bd -literal -offset indent
321 -: 1:#include \*[Lt]stdio.h\*[Gt]
329 11: 9: for (i = 0; i \*[Lt] 10; i++)
332 1: 12: if (total != 45)
333 #####: 13: printf ("Failure\en");
335 1: 15: printf ("Success\en");
341 option, you will get individual block counts, and the output looks like this:
343 .Bd -literal -offset indent
349 -: 1:#include \*[Lt]stdio.h\*[Gt]
358 11: 9: for (i = 0; i \*[Lt] 10; i++)
363 1: 12: if (total != 45)
365 #####: 13: printf ("Failure\en");
368 1: 15: printf ("Success\en");
374 In this mode, each basic block is only shown on one line -- the last line of the block.
375 A multi-line block will only contribute to the execution count of that last line, and other lines will not be shown to contain code, unless previous blocks end on those lines.
376 The total execution count of a line is shown and subsequent lines show the execution counts for individual blocks that end on that line.
377 After each block, the branch and call counts of the block will be shown, if the
381 Because of the way GCC instruments calls, a call count can be shown after a line with no individual blocks.
382 As you can see, line 13 contains a basic block that was not executed.
386 option, your output looks like this:
388 .Bd -literal -offset indent
390 90.00% of 10 source lines executed in file tmp.c
391 80.00% of 5 branches executed in file tmp.c
392 80.00% of 5 branches taken at least once in file tmp.c
393 50.00% of 2 calls executed in file tmp.c
396 Here is a sample of a resulting
400 .Bd -literal -offset indent
406 -: 1:#include \*[Lt]stdio.h\*[Gt]
409 function main called 1 returned 1 blocks executed 75%
415 11: 9: for (i = 0; i \*[Lt] 10; i++)
416 branch 0 taken 91% (fallthrough)
420 1: 12: if (total != 45)
421 branch 0 taken 0% (fallthrough)
423 #####: 13: printf ("Failure\en");
424 call 0 never executed
426 1: 15: printf ("Success\en");
427 call 0 called 1 returned 100%
431 For each function, a line is printed showing how many times the function is called, how many times it returns and what percentage of the function's blocks were executed.
433 For each basic block, a line is printed after the last line of the basic block describing the branch or call that ends the basic block.
434 There can be multiple branches and calls listed for a single source line if there are multiple basic blocks that end on that line.
435 In this case, the branches and calls are each given a number.
436 There is no simple way to map these branches and calls back to source constructs.
437 In general, though, the lowest numbered branch or call will correspond to the leftmost construct on the source line.
439 For a branch, if it was executed at least once, then a percentage indicating the number of times the branch was taken divided by the number of times the branch was executed will be printed.
440 Otherwise, the message "never executed" is printed.
442 For a call, if it was executed at least once, then a percentage indicating the number of times the call returned divided by the number of times the call was executed will be printed.
443 This will usually be 100%, but may be less for functions that call
447 and thus may not return every time they are called.
449 The execution counts are cumulative.
450 If the example program were executed again without removing the
452 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).
453 This is potentially useful in several ways.
454 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.
458 files is saved immediately before the program exits.
459 For each source file compiled with
461 the profiling code first attempts to read in an existing
463 file; if the file doesn't match the executable (differing number of basic block counts) it will ignore the contents of the file.
464 It then adds in the new execution counts and finally writes the data to the file.
466 .Ss Using B<gcov> with GCC Optimization
469 to help optimize your code, you must first compile your program with two special GCC options:
470 .Fl fprofile-arcs Fl ftest-coverage .
471 Aside from that, you can use any other GCC options; but if you want to prove that every single line in your program was executed, you should not compile with optimization at the same time.
472 On some machines the optimizer can eliminate some simple code lines by combining them with other lines.
473 For example, code like this:
475 .Bd -literal -offset indent
481 can be compiled into one instruction on some machines.
482 In this case, there is no way for
484 to calculate separate execution counts for each line because there isn't separate code for each line.
487 output looks like this if you compiled the program with optimization:
489 .Bd -literal -offset indent
495 The output shows that this block of code, combined by optimization, executed 100 times.
496 In one sense this result is correct, because there was only one instruction representing all four of these lines.
497 However, the output does not indicate how many times the result was 0 and how many times the result was 1.
499 Inlineable functions can create unexpected line counts.
500 Line counts are shown for the source code of the inlineable function, but what is shown depends on where the function is inlined, or if it is not inlined at all.
502 If the function is not inlined, the compiler must emit an out of line copy of the function, in any object file that needs it.
507 both contain out of line bodies of a particular inlineable function, they will also both contain coverage counts for that function.
512 are linked together, the linker will, on many systems, select one of those out of line bodies for all calls to that function, and remove or ignore the other.
513 Unfortunately, it will not remove the coverage counters for the unused function body.
514 Hence when instrumented, all but one use of that function will show zero counts.
516 If the function is inlined in several places, the block structure in each location might not be the same.
517 For instance, a condition might now be calculable at compile time in some instances.
518 Because the coverage of all the uses of the inline function will be shown for the same source lines, the line counts themselves might seem inconsistent.
520 gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for
523 Copyright (c) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
525 Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with the Invariant Sections being "GNU General Public License" and "Funding Free Software", the Front-Cover texts being (a) (see below), and with the Back-Cover Texts being (b) (see below).
526 A copy of the license is included in the gfdl(7) man page.
528 (a) The FSF's Front-Cover Text is:
532 (b) The FSF's Back-Cover Text is:
534 You have freedom to copy and modify this GNU Manual, like GNU software.
535 Copies published by the Free Software Foundation raise funds for GNU development.