7 .Nd coverage testing tool
10 .Op Fl v | Fl -version
12 .Op Fl a | Fl -all-blocks
13 .Op Fl b | Fl -branch-probabilities
14 .Op Fl c | Fl -branch-counts
15 .Op Fl n | Fl -no-output
16 .Op Fl l | Fl -long-file-names
17 .Op Fl p | Fl -preserve-paths
18 .Op Fl f | Fl -function-summaries
19 .Op Fl o | Fl -object-directory Ar directory|file
20 .Op Fl u | Fl -unconditional-branches
24 is a test coverage program.
25 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.
28 as a profiling tool to help discover where your optimization efforts will best affect your code.
31 along with the other profiling tool,
33 to assess which parts of your code use the greatest amount of computing time.
35 Profiling tools help you analyze your code's performance.
36 Using a profiler such as
40 you can find out some basic performance statistics, such as:
44 how often each line of code executes
47 what lines of code are actually executed
50 how much computing time each section of code uses
53 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.
55 helps you determine where to work on optimization.
57 Software developers also use coverage testing in concert with testsuites, to make sure software is actually good enough for a release.
58 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.
59 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.
61 You should compile your code without optimization if you plan to use
63 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.
66 accumulates statistics by line (at the lowest resolution), it works best with a programming style that places only one statement on each line.
67 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.
68 If your complex macros behave like functions, you can replace them with inline functions to solve this problem.
71 creates a logfile called
73 which indicates how many times each line of a source file
76 You can use these logfiles along with
78 to aid in fine-tuning the performance of your programs.
80 gives timing information you can use along with the information you get from
84 works only on code compiled with GCC.
85 It is not compatible with any other profiling or test coverage mechanism.
87 .Bl -tag -width xx -compact
90 Display help about using
92 (on the standard output), and exit without doing any further processing.
98 version number (on the standard output), and exit without doing any further processing.
102 Write individual execution counts for every basic block.
103 Normally gcov outputs execution counts only for the main blocks of a line.
104 With this option you can determine if blocks within a single line are not being executed.
107 .It Fl -branch-probabilities
108 Write branch frequencies to the output file, and write branch summary info to the standard output.
109 This option allows you to see how often each branch in your program was taken.
110 Unconditional branches will not be shown, unless the
115 .It Fl -branch-counts
116 Write branch frequencies as the number of branches taken, rather than the percentage of branches taken.
125 .It Fl -long-file-names
126 Create long file names for included source files.
127 For example, if the header file
129 contains code, and was included in the file
135 will produce an output file called
139 This can be useful if
141 is included in multiple source files.
144 option, both the including and included file names will be complete path names.
147 .It Fl -preserve-paths
148 Preserve complete path information in the names of generated
151 Without this option, just the filename component is used.
152 With this option, all directories are used, with
154 characters translated to
158 directory components removed and
160 components renamed to
162 This is useful if sourcefiles are in several different directories.
168 .It Fl -function-summaries
169 Output summaries for each function in addition to the file level summary.
171 .It Fl o Ar directory|file
172 .It Fl -object-directory Ar directory
173 .It Fl -object-file Ar file
174 Specify either the directory containing the gcov data files, or the object path name.
179 data files are searched for using this option.
180 If a directory is specified, the data files are in that directory and named after the source file name, without its extension.
181 If a file is specified here, the data files are named after that file, without its extension.
182 If this option is not supplied, it defaults to the current directory.
185 .It Fl -unconditional-branches
186 When branch probabilities are given, include those of unconditional branches.
187 Unconditional branches are normally not interesting.
191 should be run with the current directory the same as that when you invoked the compiler.
192 Otherwise it will not be able to locate the source files.
194 produces files called
196 in the current directory.
197 These contain the coverage information of the source file they correspond to.
200 file is produced for each source file containing code, which was compiled to produce the data files.
203 part of the output file name is usually simply the source file name, but can be something more complicated if the
208 Refer to those options for details.
214 separated fields along with program source code.
217 .Bd -literal -offset indent
218 \*[Lt]execution_count\*[Gt]:\*[Lt]line_number\*[Gt]:\*[Lt]source line text\*[Gt]
220 Additional block information may succeed each line, when requested by command line option.
225 for lines containing no code and
227 for lines which were never executed.
228 Some lines of information at the start have
232 The preamble lines are of the form
234 .Bd -literal -offset indent
235 -:0:\*[Lt]tag\*[Gt]:\*[Lt]value\*[Gt]
237 The ordering and number of these preamble lines will be augmented as
239 development progresses --- do not rely on them remaining unchanged.
242 to locate a particular preamble line.
244 The additional block information is of the form
246 .Bd -literal -offset indent
247 \*[Lt]tag\*[Gt] \*[Lt]information\*[Gt]
251 is human readable, but designed to be simple enough for machine parsing too.
253 When printing percentages, 0% and 100% are only printed when the values are
255 0% and 100% respectively.
256 Other values which would conventionally be rounded to 0% or 100% are instead printed as the nearest non-boundary value.
260 you must first compile your program with two special GCC options:
261 .Fl fprofile-arcs Fl ftest-coverage .
262 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.
263 These additional files are placed in the directory where the object file is located.
265 Running the program will cause profile output to be generated.
266 For each source file compiled with
270 file will be placed in the object file directory.
274 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.
275 For example, if your program is called
277 this is what you see when you use the basic
281 .Bd -literal -offset indent
282 $ gcc -fprofile-arcs -ftest-coverage tmp.c
285 90.00% of 10 source lines executed in file tmp.c
294 .Bd -literal -offset indent
300 -: 1:#include \*[Lt]stdio.h\*[Gt]
308 11: 9: for (i = 0; i \*[Lt] 10; i++)
311 1: 12: if (total != 45)
312 #####: 13: printf ("Failure\en");
314 1: 15: printf ("Success\en");
320 option, you will get individual block counts, and the output looks like this:
322 .Bd -literal -offset indent
328 -: 1:#include \*[Lt]stdio.h\*[Gt]
337 11: 9: for (i = 0; i \*[Lt] 10; i++)
342 1: 12: if (total != 45)
344 #####: 13: printf ("Failure\en");
347 1: 15: printf ("Success\en");
353 In this mode, each basic block is only shown on one line -- the last line of the block.
354 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.
355 The total execution count of a line is shown and subsequent lines show the execution counts for individual blocks that end on that line.
356 After each block, the branch and call counts of the block will be shown, if the
360 Because of the way GCC instruments calls, a call count can be shown after a line with no individual blocks.
361 As you can see, line 13 contains a basic block that was not executed.
365 option, your output looks like this:
367 .Bd -literal -offset indent
369 90.00% of 10 source lines executed in file tmp.c
370 80.00% of 5 branches executed in file tmp.c
371 80.00% of 5 branches taken at least once in file tmp.c
372 50.00% of 2 calls executed in file tmp.c
375 Here is a sample of a resulting
379 .Bd -literal -offset indent
385 -: 1:#include \*[Lt]stdio.h\*[Gt]
388 function main called 1 returned 1 blocks executed 75%
394 11: 9: for (i = 0; i \*[Lt] 10; i++)
395 branch 0 taken 91% (fallthrough)
399 1: 12: if (total != 45)
400 branch 0 taken 0% (fallthrough)
402 #####: 13: printf ("Failure\en");
403 call 0 never executed
405 1: 15: printf ("Success\en");
406 call 0 called 1 returned 100%
410 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.
412 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.
413 There can be multiple branches and calls listed for a single source line if there are multiple basic blocks that end on that line.
414 In this case, the branches and calls are each given a number.
415 There is no simple way to map these branches and calls back to source constructs.
416 In general, though, the lowest numbered branch or call will correspond to the leftmost construct on the source line.
418 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.
419 Otherwise, the message "never executed" is printed.
421 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.
422 This will usually be 100%, but may be less for functions that call
426 and thus may not return every time they are called.
428 The execution counts are cumulative.
429 If the example program were executed again without removing the
431 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).
432 This is potentially useful in several ways.
433 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.
437 files is saved immediately before the program exits.
438 For each source file compiled with
440 the profiling code first attempts to read in an existing
442 file; if the file doesn't match the executable (differing number of basic block counts) it will ignore the contents of the file.
443 It then adds in the new execution counts and finally writes the data to the file.
445 .Ss Using B<gcov> with GCC Optimization
448 to help optimize your code, you must first compile your program with two special GCC options:
449 .Fl fprofile-arcs Fl ftest-coverage .
450 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.
451 On some machines the optimizer can eliminate some simple code lines by combining them with other lines.
452 For example, code like this:
454 .Bd -literal -offset indent
460 can be compiled into one instruction on some machines.
461 In this case, there is no way for
463 to calculate separate execution counts for each line because there isn't separate code for each line.
466 output looks like this if you compiled the program with optimization:
468 .Bd -literal -offset indent
474 The output shows that this block of code, combined by optimization, executed 100 times.
475 In one sense this result is correct, because there was only one instruction representing all four of these lines.
476 However, the output does not indicate how many times the result was 0 and how many times the result was 1.
478 Inlineable functions can create unexpected line counts.
479 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.
481 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.
486 both contain out of line bodies of a particular inlineable function, they will also both contain coverage counts for that function.
491 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.
492 Unfortunately, it will not remove the coverage counters for the unused function body.
493 Hence when instrumented, all but one use of that function will show zero counts.
495 If the function is inlined in several places, the block structure in each location might not be the same.
496 For instance, a condition might now be calculable at compile time in some instances.
497 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.
499 gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for
502 Copyright (c) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
504 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).
505 A copy of the license is included in the gfdl(7) man page.
507 (a) The FSF's Front-Cover Text is:
511 (b) The FSF's Back-Cover Text is:
513 You have freedom to copy and modify this GNU Manual, like GNU software.
514 Copies published by the Free Software Foundation raise funds for GNU development.