1 @c Copyright (C) 1996, 1997, 1999, 2000, 2001,
2 @c 2002 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 Free Software Foundation, Inc.
10 Permission is granted to copy, distribute and/or modify this document
11 under the terms of the GNU Free Documentation License, Version 1.1 or
12 any later version published by the Free Software Foundation; with the
13 Invariant Sections being ``GNU General Public License'' and ``Funding
14 Free Software'', the Front-Cover texts being (a) (see below), and with
15 the Back-Cover Texts being (b) (see below). A copy of the license is
16 included in the gfdl(7) man page.
18 (a) The FSF's Front-Cover Text is:
22 (b) The FSF's Back-Cover Text is:
24 You have freedom to copy and modify this GNU Manual, like GNU
25 software. Copies published by the Free Software Foundation raise
26 funds for GNU development.
28 @c Set file name and title for the man page.
30 @settitle coverage testing tool
34 @chapter @command{gcov}: a Test Coverage Program
36 @command{gcov} is a tool you can use in conjunction with GCC to
37 test code coverage in your programs.
40 * Gcov Intro:: Introduction to gcov.
41 * Invoking Gcov:: How to use gcov.
42 * Gcov and Optimization:: Using gcov with GCC optimization.
43 * Gcov Data Files:: The files used by gcov.
47 @section Introduction to @command{gcov}
48 @c man begin DESCRIPTION
50 @command{gcov} is a test coverage program. Use it in concert with GCC
51 to analyze your programs to help create more efficient, faster running
52 code and to discover untested parts of your program. You can use
53 @command{gcov} as a profiling tool to help discover where your
54 optimization efforts will best affect your code. You can also use
55 @command{gcov} along with the other profiling tool, @command{gprof}, to
56 assess which parts of your code use the greatest amount of computing
59 Profiling tools help you analyze your code's performance. Using a
60 profiler such as @command{gcov} or @command{gprof}, you can find out some
61 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
75 can look at each module to see which modules should be optimized.
76 @command{gcov} helps you determine where to work on optimization.
78 Software developers also use coverage testing in concert with
79 testsuites, to make sure software is actually good enough for a release.
80 Testsuites can verify that a program works as expected; a coverage
81 program tests to see how much of the program is exercised by the
82 testsuite. Developers can then determine what kinds of test cases need
83 to be added to the testsuites to create both better testing and a better
86 You should compile your code without optimization if you plan to use
87 @command{gcov} because the optimization, by combining some lines of code
88 into one function, may not give you as much information as you need to
89 look for `hot spots' where the code is using a great deal of computer
90 time. Likewise, because @command{gcov} accumulates statistics by line (at
91 the lowest resolution), it works best with a programming style that
92 places only one statement on each line. If you use complicated macros
93 that expand to loops or to other control structures, the statistics are
94 less helpful---they only report on the line where the macro call
95 appears. If your complex macros behave like functions, you can replace
96 them with inline functions to solve this problem.
98 @command{gcov} creates a logfile called @file{@var{sourcefile}.gcov} which
99 indicates how many times each line of a source file @file{@var{sourcefile}.c}
100 has executed. You can use these logfiles along with @command{gprof} to aid
101 in fine-tuning the performance of your programs. @command{gprof} gives
102 timing information you can use along with the information you get from
105 @command{gcov} works only on code compiled with GCC@. It is not
106 compatible with any other profiling or test coverage mechanism.
111 @section Invoking gcov
114 gcov @r{[}@var{options}@r{]} @var{sourcefile}
117 @command{gcov} accepts the following options:
120 @c man begin SYNOPSIS
121 gcov [@option{-v}|@option{--version}] [@option{-h}|@option{--help}]
122 [@option{-b}|@option{--branch-probabilities}]
123 [@option{-c}|@option{--branch-counts}]
124 [@option{-n}|@option{--no-output}]
125 [@option{-l}|@option{--long-file-names}]
126 [@option{-p}|@option{--preserve-paths}]
127 [@option{-f}|@option{--function-summaries}]
128 [@option{-o}|@option{--object-directory} @var{directory|file}] @var{sourcefile}
131 gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}.
139 Display help about using @command{gcov} (on the standard output), and
140 exit without doing any further processing.
144 Display the @command{gcov} version number (on the standard output),
145 and exit without doing any further processing.
148 @itemx --branch-probabilities
149 Write branch frequencies to the output file, and write branch summary
150 info to the standard output. This option allows you to see how often
151 each branch in your program was taken.
154 @itemx --branch-counts
155 Write branch frequencies as the number of branches taken, rather than
156 the percentage of branches taken.
160 Do not create the @command{gcov} output file.
163 @itemx --long-file-names
164 Create long file names for included source files. For example, if the
165 header file @file{x.h} contains code, and was included in the file
166 @file{a.c}, then running @command{gcov} on the file @file{a.c} will produce
167 an output file called @file{a.c##x.h.gcov} instead of @file{x.h.gcov}.
168 This can be useful if @file{x.h} is included in multiple source files.
171 @itemx --preserve-paths
172 Preserve complete path information in the names of generated
173 @file{.gcov} files. Without this option, just the filename component is
174 used. With this option, all directories are used, with '/' characters
175 translated to '#' characters, '.' directory components removed and '..'
176 components renamed to '^'. This is useful if sourcefiles are in several
177 different directories. It also affects the @samp{-l} option.
180 @itemx --function-summaries
181 Output summaries for each function in addition to the file level summary.
183 @item -o @var{directory|file}
184 @itemx --object-directory @var{directory}
185 @itemx --object-file @var{file}
186 Specify either the directory containing the gcov data files, or the
187 object path name. The @file{.bb}, @file{.bbg}, and
188 @file{.da} data files are searched for using this option. If a directory
189 is specified, the data files are in that directory and named after the
190 source file name, without its extension. If a file is specified here,
191 the data files are named after that file, without its extension. If this
192 option is not supplied, it defaults to the current directory.
196 Gcov should be run with the current directory the same as that when you
197 invoked the compiler. Otherwise it will not be able to locate the source
198 files. Gcov produces files called @file{@var{mangledname}.gcov} in the
199 current directory. These contain the coverage information of the source
200 file they correspond to. One @file{.gcov} file is produced for each
201 source file containing code, which was compiled to produce the data
202 files. The @file{.gcov} files contain the ':' separated fields along
203 with program source code. The format is
206 @var{execution_count}:@var{line_number}:@var{source line text}
209 Additional block information may succeed each line, when requested by
210 command line option. The @var{execution_count} is @samp{-} for lines
211 containing no code and @samp{#####} for lines which were never
212 executed. Some lines of information at the start have @var{line_number}
215 When printing percentages, 0% and 100% are only printed when the values
216 are @emph{exactly} 0% and 100% respectively. Other values which would
217 conventionally be rounded to 0% or 100% are instead printed as the
218 nearest non-boundary value.
220 When using @command{gcov}, you must first compile your program with two
221 special GCC options: @samp{-fprofile-arcs -ftest-coverage}.
222 This tells the compiler to generate additional information needed by
223 gcov (basically a flow graph of the program) and also includes
224 additional code in the object files for generating the extra profiling
225 information needed by gcov. These additional files are placed in the
226 directory where the object file is located.
228 Running the program will cause profile output to be generated. For each
229 source file compiled with @option{-fprofile-arcs}, an accompanying @file{.da}
230 file will be placed in the object file directory.
232 Running @command{gcov} with your program's source file names as arguments
233 will now produce a listing of the code along with frequency of execution
234 for each line. For example, if your program is called @file{tmp.c}, this
235 is what you see when you use the basic @command{gcov} facility:
238 $ gcc -fprofile-arcs -ftest-coverage tmp.c
241 90.00% of 10 source lines executed in file tmp.c
245 The file @file{tmp.c.gcov} contains output from @command{gcov}.
251 -: 1:#include <stdio.h>
259 11: 9: for (i = 0; i < 10; i++)
262 1: 12: if (total != 45)
263 #####: 13: printf ("Failure\n");
265 1: 15: printf ("Success\n");
271 When you use the @option{-b} option, your output looks like this:
275 90.00% of 10 source lines executed in file tmp.c
276 80.00% of 5 branches executed in file tmp.c
277 80.00% of 5 branches taken at least once in file tmp.c
278 50.00% of 2 calls executed in file tmp.c
282 Here is a sample of a resulting @file{tmp.c.gcov} file:
287 -: 1:#include <stdio.h>
295 11: 9: for (i = 0; i < 10; i++)
301 1: 12: if (total != 45)
303 #####: 13: printf ("Failure\n");
304 call 0: never executed
305 branch 1: never executed
307 1: 15: printf ("Success\n");
313 For each basic block, a line is printed after the last line of the basic
314 block describing the branch or call that ends the basic block. There can
315 be multiple branches and calls listed for a single source line if there
316 are multiple basic blocks that end on that line. In this case, the
317 branches and calls are each given a number. There is no simple way to map
318 these branches and calls back to source constructs. In general, though,
319 the lowest numbered branch or call will correspond to the leftmost construct
322 For a branch, if it was executed at least once, then a percentage
323 indicating the number of times the branch was taken divided by the
324 number of times the branch was executed will be printed. Otherwise, the
325 message ``never executed'' is printed.
327 For a call, if it was executed at least once, then a percentage
328 indicating the number of times the call returned divided by the number
329 of times the call was executed will be printed. This will usually be
330 100%, but may be less for functions call @code{exit} or @code{longjmp},
331 and thus may not return every time they are called.
333 The execution counts are cumulative. If the example program were
334 executed again without removing the @file{.da} file, the count for the
335 number of times each line in the source was executed would be added to
336 the results of the previous run(s). This is potentially useful in
337 several ways. For example, it could be used to accumulate data over a
338 number of program runs as part of a test verification suite, or to
339 provide more accurate long-term information over a large number of
342 The data in the @file{.da} files is saved immediately before the program
343 exits. For each source file compiled with @option{-fprofile-arcs}, the
344 profiling code first attempts to read in an existing @file{.da} file; if
345 the file doesn't match the executable (differing number of basic block
346 counts) it will ignore the contents of the file. It then adds in the
347 new execution counts and finally writes the data to the file.
349 @node Gcov and Optimization
350 @section Using @command{gcov} with GCC Optimization
352 If you plan to use @command{gcov} to help optimize your code, you must
353 first compile your program with two special GCC options:
354 @samp{-fprofile-arcs -ftest-coverage}. Aside from that, you can use any
355 other GCC options; but if you want to prove that every single line
356 in your program was executed, you should not compile with optimization
357 at the same time. On some machines the optimizer can eliminate some
358 simple code lines by combining them with other lines. For example, code
369 can be compiled into one instruction on some machines. In this case,
370 there is no way for @command{gcov} to calculate separate execution counts
371 for each line because there isn't separate code for each line. Hence
372 the @command{gcov} output looks like this if you compiled the program with
382 The output shows that this block of code, combined by optimization,
383 executed 100 times. In one sense this result is correct, because there
384 was only one instruction representing all four of these lines. However,
385 the output does not indicate how many times the result was 0 and how
386 many times the result was 1.
389 @node Gcov Data Files
390 @section Brief description of @command{gcov} data files
392 @command{gcov} uses three files for doing profiling. The names of these
393 files are derived from the original @emph{source} file by substituting
394 the file suffix with either @file{.bb}, @file{.bbg}, or @file{.da}. All
395 of these files are placed in the same directory as the source file, and
396 contain data stored in a platform-independent method.
398 The @file{.bb} and @file{.bbg} files are generated when the source file
399 is compiled with the GCC @option{-ftest-coverage} option. The
400 @file{.bb} file contains a list of source files (including headers),
401 functions within those files, and line numbers corresponding to each
402 basic block in the source file.
404 The @file{.bb} file format consists of several lists of 4-byte integers
405 which correspond to the line numbers of each basic block in the file.
406 Each list is terminated by a line number of 0. A line number of
407 @minus{}1 is used to designate that the source file name (padded to a
408 4-byte boundary and followed by another @minus{}1) follows. In
409 addition, a line number of @minus{}2 is used to designate that the name
410 of a function (also padded to a 4-byte boundary and followed by a
413 The @file{.bbg} file is used to reconstruct the program flow graph for
414 the source file. It contains a list of the program flow arcs (possible
415 branches taken from one basic block to another) for each function which,
416 in combination with the @file{.bb} file, enables gcov to reconstruct the
419 In the @file{.bbg} file, the format is:
422 checksum of function #0
423 number of basic blocks for function #0 (4-byte number)
424 total number of arcs for function #0 (4-byte number)
425 count of arcs in basic block #0 (4-byte number)
426 destination basic block of arc #0 (4-byte number)
427 flag bits (4-byte number)
428 destination basic block of arc #1 (4-byte number)
429 flag bits (4-byte number)
431 destination basic block of arc #N (4-byte number)
432 flag bits (4-byte number)
433 count of arcs in basic block #1 (4-byte number)
434 destination basic block of arc #0 (4-byte number)
435 flag bits (4-byte number)
439 A @minus{}1 (stored as a 4-byte number) is used to separate each function's
440 list of basic blocks, and to verify that the file has been read
443 The function name is stored as a @minus{}1 (4 bytes), the length (4 bytes),
444 the name itself (padded to 4-byte boundary) followed by a @minus{}1 (4 bytes).
446 The flags are defined as follows:
449 On function spanning tree
455 Is the fall through edge from one block to its immediate successor.
462 The @file{.da} file is generated when a program containing object files
463 built with the GCC @option{-fprofile-arcs} option is executed. A
464 separate @file{.da} file is created for each source file compiled with
465 this option, and the name of the @file{.da} file is stored as an
466 absolute pathname in the resulting object file. This path name is
467 derived from the object file name by substituting a @file{.da} suffix.
469 The @file{.da} consists of one or more blocks with the following
472 "magic" number @minus{}123 (4-byte number)
473 number of functions (4-byte number)
474 length of the "extension block" in bytes
475 extension block (variable length)
476 name of function #0 (the same format as in .bbg file)
477 checksum of function #0
478 number of instrumented arcs (4-byte number)
479 count of arc #0 (8-byte number)
480 count of arc #1 (8-byte number)
482 count of arc #M_0 (8-byte number)
483 name of function #1 (the same format as in .bbg file)
484 checksum of function #1
487 Multiple program runs might merge data into a single block, or might
488 append a new block. The current structure of the extension block is as
491 number of instrumented arcs in whole program (4-byte number)
492 sum all of instrumented arcs in whole program (8-byte number)
493 maximal value of counter in whole program (8-byte number)
494 number of instrumented arcs in the object file (4-byte number)
495 sum all of instrumented arcs in the object file (8-byte number)
496 maximal value of counter in the object file (8-byte number)
499 All three of these files use the functions in @file{gcov-io.h} to store
500 integers; the functions in this header provide a machine-independent
501 mechanism for storing and retrieving data from a stream.