2013-05-30 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / doc / gcov.texi
blob9db8e8fd8ebaa78d849599063707e0278b02c8f9
1 @c Copyright (C) 1996-2013 Free Software Foundation, Inc.
2 @c This is part of the GCC manual.
3 @c For copying conditions, see the file gcc.texi.
5 @ignore
6 @c man begin COPYRIGHT
7 Copyright @copyright{} 1996-2013 Free Software Foundation, Inc.
9 Permission is granted to copy, distribute and/or modify this document
10 under the terms of the GNU Free Documentation License, Version 1.3 or
11 any later version published by the Free Software Foundation; with the
12 Invariant Sections being ``GNU General Public License'' and ``Funding
13 Free Software'', the Front-Cover texts being (a) (see below), and with
14 the Back-Cover Texts being (b) (see below).  A copy of the license is
15 included in the gfdl(7) man page.
17 (a) The FSF's Front-Cover Text is:
19      A GNU Manual
21 (b) The FSF's Back-Cover Text is:
23      You have freedom to copy and modify this GNU Manual, like GNU
24      software.  Copies published by the Free Software Foundation raise
25      funds for GNU development.
26 @c man end
27 @c Set file name and title for the man page.
28 @setfilename gcov
29 @settitle coverage testing tool
30 @end ignore
32 @node Gcov
33 @chapter @command{gcov}---a Test Coverage Program
35 @command{gcov} is a tool you can use in conjunction with GCC to
36 test code coverage in your programs.
38 @menu
39 * Gcov Intro::                  Introduction to gcov.
40 * Invoking Gcov::               How to use gcov.
41 * Gcov and Optimization::       Using gcov with GCC optimization.
42 * Gcov Data Files::             The files used by gcov.
43 * Cross-profiling::             Data file relocation.
44 @end menu
46 @node Gcov Intro
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
57 time.
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:
63 @itemize @bullet
64 @item
65 how often each line of code executes
67 @item
68 what lines of code are actually executed
70 @item
71 how much computing time each section of code uses
72 @end itemize
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
84 final product.
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
103 @command{gcov}.
105 @command{gcov} works only on code compiled with GCC@.  It is not
106 compatible with any other profiling or test coverage mechanism.
108 @c man end
110 @node Invoking Gcov
111 @section Invoking @command{gcov}
113 @smallexample
114 gcov @r{[}@var{options}@r{]} @var{files}
115 @end smallexample
117 @command{gcov} accepts the following options:
119 @ignore
120 @c man begin SYNOPSIS
121 gcov [@option{-v}|@option{--version}] [@option{-h}|@option{--help}]
122      [@option{-a}|@option{--all-blocks}]
123      [@option{-b}|@option{--branch-probabilities}]
124      [@option{-c}|@option{--branch-counts}]
125      [@option{-u}|@option{--unconditional-branches}]
126      [@option{-n}|@option{--no-output}]
127      [@option{-l}|@option{--long-file-names}]
128      [@option{-p}|@option{--preserve-paths}]
129      [@option{-r}|@option{--relative-only}]
130      [@option{-f}|@option{--function-summaries}]
131      [@option{-o}|@option{--object-directory} @var{directory|file}]
132      [@option{-s}|@option{--source-prefix} @var{directory}]
133      [@option{-d}|@option{--display-progress}]
134      @var{files}
135 @c man end
136 @c man begin SEEALSO
137 gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}.
138 @c man end
139 @end ignore
141 @c man begin OPTIONS
142 @table @gcctabopt
143 @item -h
144 @itemx --help
145 Display help about using @command{gcov} (on the standard output), and
146 exit without doing any further processing.
148 @item -v
149 @itemx --version
150 Display the @command{gcov} version number (on the standard output),
151 and exit without doing any further processing.
153 @item -a
154 @itemx --all-blocks
155 Write individual execution counts for every basic block.  Normally gcov
156 outputs execution counts only for the main blocks of a line.  With this
157 option you can determine if blocks within a single line are not being
158 executed.
160 @item -b
161 @itemx --branch-probabilities
162 Write branch frequencies to the output file, and write branch summary
163 info to the standard output.  This option allows you to see how often
164 each branch in your program was taken.  Unconditional branches will not
165 be shown, unless the @option{-u} option is given.
167 @item -c
168 @itemx --branch-counts
169 Write branch frequencies as the number of branches taken, rather than
170 the percentage of branches taken.
172 @item -n
173 @itemx --no-output
174 Do not create the @command{gcov} output file.
176 @item -l
177 @itemx --long-file-names
178 Create long file names for included source files.  For example, if the
179 header file @file{x.h} contains code, and was included in the file
180 @file{a.c}, then running @command{gcov} on the file @file{a.c} will
181 produce an output file called @file{a.c##x.h.gcov} instead of
182 @file{x.h.gcov}.  This can be useful if @file{x.h} is included in
183 multiple source files and you want to see the individual
184 contributions.  If you use the @samp{-p} option, both the including
185 and included file names will be complete path names.
187 @item -p
188 @itemx --preserve-paths
189 Preserve complete path information in the names of generated
190 @file{.gcov} files.  Without this option, just the filename component is
191 used.  With this option, all directories are used, with @samp{/} characters
192 translated to @samp{#} characters, @file{.} directory components
193 removed and unremoveable @file{..}
194 components renamed to @samp{^}.  This is useful if sourcefiles are in several
195 different directories.
197 @item -r
198 @itemx --relative-only
199 Only output information about source files with a relative pathname
200 (after source prefix elision).  Absolute paths are usually system
201 header files and coverage of any inline functions therein is normally
202 uninteresting.
204 @item -f
205 @itemx --function-summaries
206 Output summaries for each function in addition to the file level summary.
208 @item -o @var{directory|file}
209 @itemx --object-directory @var{directory}
210 @itemx --object-file @var{file}
211 Specify either the directory containing the gcov data files, or the
212 object path name.  The @file{.gcno}, and
213 @file{.gcda} data files are searched for using this option.  If a directory
214 is specified, the data files are in that directory and named after the
215 input file name, without its extension.  If a file is specified here,
216 the data files are named after that file, without its extension.
218 @item -s @var{directory}
219 @itemx --source-prefix @var{directory}
220 A prefix for source file names to remove when generating the output
221 coverage files.  This option is useful when building in a separate
222 directory, and the pathname to the source directory is not wanted when
223 determining the output file names.  Note that this prefix detection is
224 applied before determining whether the source file is absolute.
226 @item -u
227 @itemx --unconditional-branches
228 When branch probabilities are given, include those of unconditional branches.
229 Unconditional branches are normally not interesting.
231 @item -d
232 @itemx --display-progress
233 Display the progress on the standard output.
235 @end table
237 @command{gcov} should be run with the current directory the same as that
238 when you invoked the compiler.  Otherwise it will not be able to locate
239 the source files.  @command{gcov} produces files called
240 @file{@var{mangledname}.gcov} in the current directory.  These contain
241 the coverage information of the source file they correspond to.
242 One @file{.gcov} file is produced for each source (or header) file
243 containing code,
244 which was compiled to produce the data files.  The @var{mangledname} part
245 of the output file name is usually simply the source file name, but can
246 be something more complicated if the @samp{-l} or @samp{-p} options are
247 given.  Refer to those options for details.
249 If you invoke @command{gcov} with multiple input files, the
250 contributions from each input file are summed.  Typically you would
251 invoke it with the same list of files as the final link of your executable.
253 The @file{.gcov} files contain the @samp{:} separated fields along with
254 program source code.  The format is
256 @smallexample
257 @var{execution_count}:@var{line_number}:@var{source line text}
258 @end smallexample
260 Additional block information may succeed each line, when requested by
261 command line option.  The @var{execution_count} is @samp{-} for lines
262 containing no code.  Unexecuted lines are marked @samp{#####} or
263 @samp{====}, depending on whether they are reachable by
264 non-exceptional paths or only exceptional paths such as C++ exception
265 handlers, respectively.
267 Some lines of information at the start have @var{line_number} of zero.
268 These preamble lines are of the form
270 @smallexample
271 -:0:@var{tag}:@var{value}
272 @end smallexample
274 The ordering and number of these preamble lines will be augmented as
275 @command{gcov} development progresses --- do not rely on them remaining
276 unchanged.  Use @var{tag} to locate a particular preamble line.
278 The additional block information is of the form
280 @smallexample
281 @var{tag} @var{information}
282 @end smallexample
284 The @var{information} is human readable, but designed to be simple
285 enough for machine parsing too.
287 When printing percentages, 0% and 100% are only printed when the values
288 are @emph{exactly} 0% and 100% respectively.  Other values which would
289 conventionally be rounded to 0% or 100% are instead printed as the
290 nearest non-boundary value.
292 When using @command{gcov}, you must first compile your program with two
293 special GCC options: @samp{-fprofile-arcs -ftest-coverage}.
294 This tells the compiler to generate additional information needed by
295 gcov (basically a flow graph of the program) and also includes
296 additional code in the object files for generating the extra profiling
297 information needed by gcov.  These additional files are placed in the
298 directory where the object file is located.
300 Running the program will cause profile output to be generated.  For each
301 source file compiled with @option{-fprofile-arcs}, an accompanying
302 @file{.gcda} file will be placed in the object file directory.
304 Running @command{gcov} with your program's source file names as arguments
305 will now produce a listing of the code along with frequency of execution
306 for each line.  For example, if your program is called @file{tmp.c}, this
307 is what you see when you use the basic @command{gcov} facility:
309 @smallexample
310 $ gcc -fprofile-arcs -ftest-coverage tmp.c
311 $ a.out
312 $ gcov tmp.c
313 90.00% of 10 source lines executed in file tmp.c
314 Creating tmp.c.gcov.
315 @end smallexample
317 The file @file{tmp.c.gcov} contains output from @command{gcov}.
318 Here is a sample:
320 @smallexample
321         -:    0:Source:tmp.c
322         -:    0:Graph:tmp.gcno
323         -:    0:Data:tmp.gcda
324         -:    0:Runs:1
325         -:    0:Programs:1
326         -:    1:#include <stdio.h>
327         -:    2:
328         -:    3:int main (void)
329         1:    4:@{
330         1:    5:  int i, total;
331         -:    6:
332         1:    7:  total = 0;
333         -:    8:
334        11:    9:  for (i = 0; i < 10; i++)
335        10:   10:    total += i;
336         -:   11:
337         1:   12:  if (total != 45)
338     #####:   13:    printf ("Failure\n");
339         -:   14:  else
340         1:   15:    printf ("Success\n");
341         1:   16:  return 0;
342         -:   17:@}
343 @end smallexample
345 When you use the @option{-a} option, you will get individual block
346 counts, and the output looks like this:
348 @smallexample
349         -:    0:Source:tmp.c
350         -:    0:Graph:tmp.gcno
351         -:    0:Data:tmp.gcda
352         -:    0:Runs:1
353         -:    0:Programs:1
354         -:    1:#include <stdio.h>
355         -:    2:
356         -:    3:int main (void)
357         1:    4:@{
358         1:    4-block  0
359         1:    5:  int i, total;
360         -:    6:
361         1:    7:  total = 0;
362         -:    8:
363        11:    9:  for (i = 0; i < 10; i++)
364        11:    9-block  0
365        10:   10:    total += i;
366        10:   10-block  0
367         -:   11:
368         1:   12:  if (total != 45)
369         1:   12-block  0
370     #####:   13:    printf ("Failure\n");
371     $$$$$:   13-block  0
372         -:   14:  else
373         1:   15:    printf ("Success\n");
374         1:   15-block  0
375         1:   16:  return 0;
376         1:   16-block  0
377         -:   17:@}
378 @end smallexample
380 In this mode, each basic block is only shown on one line -- the last
381 line of the block.  A multi-line block will only contribute to the
382 execution count of that last line, and other lines will not be shown
383 to contain code, unless previous blocks end on those lines.
384 The total execution count of a line is shown and subsequent lines show
385 the execution counts for individual blocks that end on that line.  After each
386 block, the branch and call counts of the block will be shown, if the
387 @option{-b} option is given.
389 Because of the way GCC instruments calls, a call count can be shown
390 after a line with no individual blocks.
391 As you can see, line 13 contains a basic block that was not executed.
393 @need 450
394 When you use the @option{-b} option, your output looks like this:
396 @smallexample
397 $ gcov -b tmp.c
398 90.00% of 10 source lines executed in file tmp.c
399 80.00% of 5 branches executed in file tmp.c
400 80.00% of 5 branches taken at least once in file tmp.c
401 50.00% of 2 calls executed in file tmp.c
402 Creating tmp.c.gcov.
403 @end smallexample
405 Here is a sample of a resulting @file{tmp.c.gcov} file:
407 @smallexample
408         -:    0:Source:tmp.c
409         -:    0:Graph:tmp.gcno
410         -:    0:Data:tmp.gcda
411         -:    0:Runs:1
412         -:    0:Programs:1
413         -:    1:#include <stdio.h>
414         -:    2:
415         -:    3:int main (void)
416 function main called 1 returned 1 blocks executed 75%
417         1:    4:@{
418         1:    5:  int i, total;
419         -:    6:
420         1:    7:  total = 0;
421         -:    8:
422        11:    9:  for (i = 0; i < 10; i++)
423 branch  0 taken 91% (fallthrough)
424 branch  1 taken 9%
425        10:   10:    total += i;
426         -:   11:
427         1:   12:  if (total != 45)
428 branch  0 taken 0% (fallthrough)
429 branch  1 taken 100%
430     #####:   13:    printf ("Failure\n");
431 call    0 never executed
432         -:   14:  else
433         1:   15:    printf ("Success\n");
434 call    0 called 1 returned 100%
435         1:   16:  return 0;
436         -:   17:@}
437 @end smallexample
439 For each function, a line is printed showing how many times the function
440 is called, how many times it returns and what percentage of the
441 function's blocks were executed.
443 For each basic block, a line is printed after the last line of the basic
444 block describing the branch or call that ends the basic block.  There can
445 be multiple branches and calls listed for a single source line if there
446 are multiple basic blocks that end on that line.  In this case, the
447 branches and calls are each given a number.  There is no simple way to map
448 these branches and calls back to source constructs.  In general, though,
449 the lowest numbered branch or call will correspond to the leftmost construct
450 on the source line.
452 For a branch, if it was executed at least once, then a percentage
453 indicating the number of times the branch was taken divided by the
454 number of times the branch was executed will be printed.  Otherwise, the
455 message ``never executed'' is printed.
457 For a call, if it was executed at least once, then a percentage
458 indicating the number of times the call returned divided by the number
459 of times the call was executed will be printed.  This will usually be
460 100%, but may be less for functions that call @code{exit} or @code{longjmp},
461 and thus may not return every time they are called.
463 The execution counts are cumulative.  If the example program were
464 executed again without removing the @file{.gcda} file, the count for the
465 number of times each line in the source was executed would be added to
466 the results of the previous run(s).  This is potentially useful in
467 several ways.  For example, it could be used to accumulate data over a
468 number of program runs as part of a test verification suite, or to
469 provide more accurate long-term information over a large number of
470 program runs.
472 The data in the @file{.gcda} files is saved immediately before the program
473 exits.  For each source file compiled with @option{-fprofile-arcs}, the
474 profiling code first attempts to read in an existing @file{.gcda} file; if
475 the file doesn't match the executable (differing number of basic block
476 counts) it will ignore the contents of the file.  It then adds in the
477 new execution counts and finally writes the data to the file.
479 @node Gcov and Optimization
480 @section Using @command{gcov} with GCC Optimization
482 If you plan to use @command{gcov} to help optimize your code, you must
483 first compile your program with two special GCC options:
484 @samp{-fprofile-arcs -ftest-coverage}.  Aside from that, you can use any
485 other GCC options; but if you want to prove that every single line
486 in your program was executed, you should not compile with optimization
487 at the same time.  On some machines the optimizer can eliminate some
488 simple code lines by combining them with other lines.  For example, code
489 like this:
491 @smallexample
492 if (a != b)
493   c = 1;
494 else
495   c = 0;
496 @end smallexample
498 @noindent
499 can be compiled into one instruction on some machines.  In this case,
500 there is no way for @command{gcov} to calculate separate execution counts
501 for each line because there isn't separate code for each line.  Hence
502 the @command{gcov} output looks like this if you compiled the program with
503 optimization:
505 @smallexample
506       100:   12:if (a != b)
507       100:   13:  c = 1;
508       100:   14:else
509       100:   15:  c = 0;
510 @end smallexample
512 The output shows that this block of code, combined by optimization,
513 executed 100 times.  In one sense this result is correct, because there
514 was only one instruction representing all four of these lines.  However,
515 the output does not indicate how many times the result was 0 and how
516 many times the result was 1.
518 Inlineable functions can create unexpected line counts.  Line counts are
519 shown for the source code of the inlineable function, but what is shown
520 depends on where the function is inlined, or if it is not inlined at all.
522 If the function is not inlined, the compiler must emit an out of line
523 copy of the function, in any object file that needs it.  If
524 @file{fileA.o} and @file{fileB.o} both contain out of line bodies of a
525 particular inlineable function, they will also both contain coverage
526 counts for that function.  When @file{fileA.o} and @file{fileB.o} are
527 linked together, the linker will, on many systems, select one of those
528 out of line bodies for all calls to that function, and remove or ignore
529 the other.  Unfortunately, it will not remove the coverage counters for
530 the unused function body.  Hence when instrumented, all but one use of
531 that function will show zero counts.
533 If the function is inlined in several places, the block structure in
534 each location might not be the same.  For instance, a condition might
535 now be calculable at compile time in some instances.  Because the
536 coverage of all the uses of the inline function will be shown for the
537 same source lines, the line counts themselves might seem inconsistent.
539 Long-running applications can use the @code{_gcov_reset} and @code{_gcov_dump}
540 facilities to restrict profile collection to the program region of
541 interest. Calling @code{_gcov_reset(void)} will clear all profile counters
542 to zero, and calling @code{_gcov_dump(void)} will cause the profile information
543 collected at that point to be dumped to @file{.gcda} output files.
545 @c man end
547 @node Gcov Data Files
548 @section Brief description of @command{gcov} data files
550 @command{gcov} uses two files for profiling.  The names of these files
551 are derived from the original @emph{object} file by substituting the
552 file suffix with either @file{.gcno}, or @file{.gcda}.  The files
553 contain coverage and profile data stored in a platform-independent format.
554 The @file{.gcno} files are placed in the same directory as the object
555 file.  By default, the @file{.gcda} files are also stored in the same
556 directory as the object file, but the GCC @option{-fprofile-dir} option
557 may be used to store the @file{.gcda} files in a separate directory.
559 The @file{.gcno} notes file is generated when the source file is compiled
560 with the GCC @option{-ftest-coverage} option.  It contains information to
561 reconstruct the basic block graphs and assign source line numbers to
562 blocks.
564 The @file{.gcda} count data file is generated when a program containing
565 object files built with the GCC @option{-fprofile-arcs} option is executed.
566 A separate @file{.gcda} file is created for each object file compiled with
567 this option.  It contains arc transition counts, value profile counts, and
568 some summary information.
570 The full details of the file format is specified in @file{gcov-io.h},
571 and functions provided in that header file should be used to access the
572 coverage files.
574 @node Cross-profiling
575 @section Data file relocation to support cross-profiling
577 Running the program will cause profile output to be generated.  For each
578 source file compiled with @option{-fprofile-arcs}, an accompanying @file{.gcda}
579 file will be placed in the object file directory. That implicitly requires
580 running the program on the same system as it was built or having the same
581 absolute directory structure on the target system. The program will try
582 to create the needed directory structure, if it is not already present.
584 To support cross-profiling, a program compiled with @option{-fprofile-arcs}
585 can relocate the data files based on two environment variables:
587 @itemize @bullet
588 @item
589 GCOV_PREFIX contains the prefix to add to the absolute paths
590 in the object file. Prefix can be absolute, or relative.  The
591 default is no prefix.
593 @item
594 GCOV_PREFIX_STRIP indicates the how many initial directory names to strip off
595 the hardwired absolute paths. Default value is 0.
597 @emph{Note:} If GCOV_PREFIX_STRIP is set without GCOV_PREFIX is undefined,
598  then a relative path is made out of the hardwired absolute paths.
599 @end itemize
601 For example, if the object file @file{/user/build/foo.o} was built with
602 @option{-fprofile-arcs}, the final executable will try to create the data file
603 @file{/user/build/foo.gcda} when running on the target system.  This will
604 fail if the corresponding directory does not exist and it is unable to create
605 it.  This can be overcome by, for example, setting the environment as
606 @samp{GCOV_PREFIX=/target/run} and @samp{GCOV_PREFIX_STRIP=1}.  Such a
607 setting will name the data file @file{/target/run/build/foo.gcda}.
609 You must move the data files to the expected directory tree in order to
610 use them for profile directed optimizations (@option{--use-profile}), or to
611 use the @command{gcov} tool.