Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / doc / gcov.texi
blob3278a4b5a97718195db05998341be9e4eb9f4af2
1 @c Copyright (C) 1996, 1997, 1999, 2000, 2001,
2 @c 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 @c This is part of the GCC manual.
4 @c For copying conditions, see the file gcc.texi.
6 @ignore
7 @c man begin COPYRIGHT
8 Copyright @copyright{} 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005
9 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.2 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:
21      A GNU Manual
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.
28 @c man end
29 @c Set file name and title for the man page.
30 @setfilename gcov
31 @settitle coverage testing tool
32 @end ignore
34 @node Gcov
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.
40 @menu
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 @end menu
47 @node Gcov Intro
48 @section Introduction to @command{gcov}
49 @c man begin DESCRIPTION
51 @command{gcov} is a test coverage program.  Use it in concert with GCC
52 to analyze your programs to help create more efficient, faster running
53 code and to discover untested parts of your program.  You can use
54 @command{gcov} as a profiling tool to help discover where your
55 optimization efforts will best affect your code.  You can also use
56 @command{gcov} along with the other profiling tool, @command{gprof}, to
57 assess which parts of your code use the greatest amount of computing
58 time.
60 Profiling tools help you analyze your code's performance.  Using a
61 profiler such as @command{gcov} or @command{gprof}, you can find out some
62 basic performance statistics, such as:
64 @itemize @bullet
65 @item
66 how often each line of code executes
68 @item
69 what lines of code are actually executed
71 @item
72 how much computing time each section of code uses
73 @end itemize
75 Once you know these things about how your code works when compiled, you
76 can look at each module to see which modules should be optimized.
77 @command{gcov} helps you determine where to work on optimization.
79 Software developers also use coverage testing in concert with
80 testsuites, to make sure software is actually good enough for a release.
81 Testsuites can verify that a program works as expected; a coverage
82 program tests to see how much of the program is exercised by the
83 testsuite.  Developers can then determine what kinds of test cases need
84 to be added to the testsuites to create both better testing and a better
85 final product.
87 You should compile your code without optimization if you plan to use
88 @command{gcov} because the optimization, by combining some lines of code
89 into one function, may not give you as much information as you need to
90 look for `hot spots' where the code is using a great deal of computer
91 time.  Likewise, because @command{gcov} accumulates statistics by line (at
92 the lowest resolution), it works best with a programming style that
93 places only one statement on each line.  If you use complicated macros
94 that expand to loops or to other control structures, the statistics are
95 less helpful---they only report on the line where the macro call
96 appears.  If your complex macros behave like functions, you can replace
97 them with inline functions to solve this problem.
99 @command{gcov} creates a logfile called @file{@var{sourcefile}.gcov} which
100 indicates how many times each line of a source file @file{@var{sourcefile}.c}
101 has executed.  You can use these logfiles along with @command{gprof} to aid
102 in fine-tuning the performance of your programs.  @command{gprof} gives
103 timing information you can use along with the information you get from
104 @command{gcov}.
106 @command{gcov} works only on code compiled with GCC@.  It is not
107 compatible with any other profiling or test coverage mechanism.
109 @c man end
111 @node Invoking Gcov
112 @section Invoking gcov
114 @smallexample
115 gcov @r{[}@var{options}@r{]} @var{sourcefile}
116 @end smallexample
118 @command{gcov} accepts the following options:
120 @ignore
121 @c man begin SYNOPSIS
122 gcov [@option{-v}|@option{--version}] [@option{-h}|@option{--help}]
123      [@option{-a}|@option{--all-blocks}]
124      [@option{-b}|@option{--branch-probabilities}]
125      [@option{-c}|@option{--branch-counts}]
126      [@option{-n}|@option{--no-output}]
127      [@option{-l}|@option{--long-file-names}]
128      [@option{-p}|@option{--preserve-paths}]
129      [@option{-f}|@option{--function-summaries}]
130      [@option{-o}|@option{--object-directory} @var{directory|file}] @var{sourcefile}
131      [@option{-u}|@option{--unconditional-branches}]
132 @c man end
133 @c man begin SEEALSO
134 gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}.
135 @c man end
136 @end ignore
138 @c man begin OPTIONS
139 @table @gcctabopt
140 @item -h
141 @itemx --help
142 Display help about using @command{gcov} (on the standard output), and
143 exit without doing any further processing.
145 @item -v
146 @itemx --version
147 Display the @command{gcov} version number (on the standard output),
148 and exit without doing any further processing.
150 @item -a
151 @itemx --all-blocks
152 Write individual execution counts for every basic block.  Normally gcov
153 outputs execution counts only for the main blocks of a line.  With this
154 option you can determine if blocks within a single line are not being
155 executed.
157 @item -b
158 @itemx --branch-probabilities
159 Write branch frequencies to the output file, and write branch summary
160 info to the standard output.  This option allows you to see how often
161 each branch in your program was taken.  Unconditional branches will not
162 be shown, unless the @option{-u} option is given.
164 @item -c
165 @itemx --branch-counts
166 Write branch frequencies as the number of branches taken, rather than
167 the percentage of branches taken.
169 @item -n
170 @itemx --no-output
171 Do not create the @command{gcov} output file.
173 @item -l
174 @itemx --long-file-names
175 Create long file names for included source files.  For example, if the
176 header file @file{x.h} contains code, and was included in the file
177 @file{a.c}, then running @command{gcov} on the file @file{a.c} will produce
178 an output file called @file{a.c##x.h.gcov} instead of @file{x.h.gcov}.
179 This can be useful if @file{x.h} is included in multiple source
180 files.  If you use the @samp{-p} option, both the including and
181 included file names will be complete path names.
183 @item -p
184 @itemx --preserve-paths
185 Preserve complete path information in the names of generated
186 @file{.gcov} files.  Without this option, just the filename component is
187 used.  With this option, all directories are used, with @samp{/} characters
188 translated to @samp{#} characters, @file{.} directory components
189 removed and @file{..}
190 components renamed to @samp{^}.  This is useful if sourcefiles are in several
191 different directories.  It also affects the @samp{-l} option.
193 @item -f
194 @itemx --function-summaries
195 Output summaries for each function in addition to the file level summary.
197 @item -o @var{directory|file}
198 @itemx --object-directory @var{directory}
199 @itemx --object-file @var{file}
200 Specify either the directory containing the gcov data files, or the
201 object path name.  The @file{.gcno}, and
202 @file{.gcda} data files are searched for using this option.  If a directory
203 is specified, the data files are in that directory and named after the
204 source file name, without its extension.  If a file is specified here,
205 the data files are named after that file, without its extension.  If this
206 option is not supplied, it defaults to the current directory.
208 @item -u
209 @itemx --unconditional-branches
210 When branch counts are given, include those of unconditional branches.
211 Unconditional branches are normally not interesting.
213 @end table
215 @command{gcov} should be run with the current directory the same as that
216 when you invoked the compiler.  Otherwise it will not be able to locate
217 the source files.  @command{gcov} produces files called
218 @file{@var{mangledname}.gcov} in the current directory.  These contain
219 the coverage information of the source file they correspond to.
220 One @file{.gcov} file is produced for each source file containing code,
221 which was compiled to produce the data files.  The @var{mangledname} part
222 of the output file name is usually simply the source file name, but can
223 be something more complicated if the @samp{-l} or @samp{-p} options are
224 given.  Refer to those options for details.
226 The @file{.gcov} files contain the @samp{:} separated fields along with
227 program source code.  The format is
229 @smallexample
230 @var{execution_count}:@var{line_number}:@var{source line text}
231 @end smallexample
233 Additional block information may succeed each line, when requested by
234 command line option.  The @var{execution_count} is @samp{-} for lines
235 containing no code and @samp{#####} for lines which were never
236 executed.  Some lines of information at the start have @var{line_number}
237 of zero.
239 When printing percentages, 0% and 100% are only printed when the values
240 are @emph{exactly} 0% and 100% respectively.  Other values which would
241 conventionally be rounded to 0% or 100% are instead printed as the
242 nearest non-boundary value.
244 When using @command{gcov}, you must first compile your program with two
245 special GCC options: @samp{-fprofile-arcs -ftest-coverage}.
246 This tells the compiler to generate additional information needed by
247 gcov (basically a flow graph of the program) and also includes
248 additional code in the object files for generating the extra profiling
249 information needed by gcov.  These additional files are placed in the
250 directory where the object file is located.
252 Running the program will cause profile output to be generated.  For each
253 source file compiled with @option{-fprofile-arcs}, an accompanying
254 @file{.gcda} file will be placed in the object file directory.
256 Running @command{gcov} with your program's source file names as arguments
257 will now produce a listing of the code along with frequency of execution
258 for each line.  For example, if your program is called @file{tmp.c}, this
259 is what you see when you use the basic @command{gcov} facility:
261 @smallexample
262 $ gcc -fprofile-arcs -ftest-coverage tmp.c
263 $ a.out
264 $ gcov tmp.c
265 90.00% of 10 source lines executed in file tmp.c
266 Creating tmp.c.gcov.
267 @end smallexample
269 The file @file{tmp.c.gcov} contains output from @command{gcov}.
270 Here is a sample:
272 @smallexample
273         -:    0:Source:tmp.c
274         -:    0:Graph:tmp.gcno
275         -:    0:Data:tmp.gcda
276         -:    0:Runs:1
277         -:    0:Programs:1
278         -:    1:#include <stdio.h>
279         -:    2:
280         -:    3:int main (void)
281 function main called 1 returned 1 blocks executed 75%
282         1:    4:@{
283         1:    5:  int i, total;
284         -:    6:
285         1:    7:  total = 0;
286         -:    8:
287        11:    9:  for (i = 0; i < 10; i++)
288        10:   10:    total += i;
289         -:   11:
290         1:   12:  if (total != 45)
291     #####:   13:    printf ("Failure\n");
292         -:   14:  else
293         1:   15:    printf ("Success\n");
294         1:   16:  return 0;
295         -:   17:@}
296 @end smallexample
298 When you use the @option{-a} option, you will get individual block
299 counts, and the output looks like this:
301 @smallexample
302         -:    0:Source:tmp.c
303         -:    0:Graph:tmp.gcno
304         -:    0:Data:tmp.gcda
305         -:    0:Runs:1
306         -:    0:Programs:1
307         -:    1:#include <stdio.h>
308         -:    2:
309         -:    3:int main (void)
310 function main called 1 returned 1 blocks executed 75%
311         1:    4:@{
312         1:    4-block  0
313         1:    5:  int i, total;
314         -:    6:
315         1:    7:  total = 0;
316         -:    8:
317        11:    9:  for (i = 0; i < 10; i++)
318        11:    9-block  0
319        10:   10:    total += i;
320        10:   10-block  0
321         -:   11:
322         1:   12:  if (total != 45)
323         1:   12-block  0
324     #####:   13:    printf ("Failure\n");
325     $$$$$:   13-block  0
326         -:   14:  else
327         1:   15:    printf ("Success\n");
328         1:   15-block  0
329         1:   16:  return 0;
330         1:   16-block  0
331         -:   17:@}
332 @end smallexample
334 In this mode, each basic block is only shown on one line -- the last
335 line of the block.  A multi-line block will only contribute to the
336 execution count of that last line, and other lines will not be shown
337 to contain code, unless previous blocks end on those lines.
338 The total execution count of a line is shown and subsequent lines show
339 the execution counts for individual blocks that end on that line.  After each
340 block, the branch and call counts of the block will be shown, if the
341 @option{-b} option is given.
343 Because of the way GCC instruments calls, a call count can be shown
344 after a line with no individual blocks.
345 As you can see, line 13 contains a basic block that was not executed.
347 @need 450
348 When you use the @option{-b} option, your output looks like this:
350 @smallexample
351 $ gcov -b tmp.c
352 90.00% of 10 source lines executed in file tmp.c
353 80.00% of 5 branches executed in file tmp.c
354 80.00% of 5 branches taken at least once in file tmp.c
355 50.00% of 2 calls executed in file tmp.c
356 Creating tmp.c.gcov.
357 @end smallexample
359 Here is a sample of a resulting @file{tmp.c.gcov} file:
361 @smallexample
362         -:    0:Source:tmp.c
363         -:    0:Graph:tmp.gcno
364         -:    0:Data:tmp.gcda
365         -:    0:Runs:1
366         -:    0:Programs:1
367         -:    1:#include <stdio.h>
368         -:    2:
369         -:    3:int main (void)
370 function main called 1 returned 1 blocks executed 75%
371         1:    4:@{
372         1:    5:  int i, total;
373         -:    6:
374         1:    7:  total = 0;
375         -:    8:
376        11:    9:  for (i = 0; i < 10; i++)
377 branch  0 taken 91% (fallthrough)
378 branch  1 taken 9%
379        10:   10:    total += i;
380         -:   11:
381         1:   12:  if (total != 45)
382 branch  0 taken 0% (fallthrough)
383 branch  1 taken 100%
384     #####:   13:    printf ("Failure\n");
385 call    0 never executed
386         -:   14:  else
387         1:   15:    printf ("Success\n");
388 call    0 called 1 returned 100%
389         1:   16:  return 0;
390         -:   17:@}
391 @end smallexample
393 For each basic block, a line is printed after the last line of the basic
394 block describing the branch or call that ends the basic block.  There can
395 be multiple branches and calls listed for a single source line if there
396 are multiple basic blocks that end on that line.  In this case, the
397 branches and calls are each given a number.  There is no simple way to map
398 these branches and calls back to source constructs.  In general, though,
399 the lowest numbered branch or call will correspond to the leftmost construct
400 on the source line.
402 For a branch, if it was executed at least once, then a percentage
403 indicating the number of times the branch was taken divided by the
404 number of times the branch was executed will be printed.  Otherwise, the
405 message ``never executed'' is printed.
407 For a call, if it was executed at least once, then a percentage
408 indicating the number of times the call returned divided by the number
409 of times the call was executed will be printed.  This will usually be
410 100%, but may be less for functions call @code{exit} or @code{longjmp},
411 and thus may not return every time they are called.
413 The execution counts are cumulative.  If the example program were
414 executed again without removing the @file{.gcda} file, the count for the
415 number of times each line in the source was executed would be added to
416 the results of the previous run(s).  This is potentially useful in
417 several ways.  For example, it could be used to accumulate data over a
418 number of program runs as part of a test verification suite, or to
419 provide more accurate long-term information over a large number of
420 program runs.
422 The data in the @file{.gcda} files is saved immediately before the program
423 exits.  For each source file compiled with @option{-fprofile-arcs}, the
424 profiling code first attempts to read in an existing @file{.gcda} file; if
425 the file doesn't match the executable (differing number of basic block
426 counts) it will ignore the contents of the file.  It then adds in the
427 new execution counts and finally writes the data to the file.
429 @node Gcov and Optimization
430 @section Using @command{gcov} with GCC Optimization
432 If you plan to use @command{gcov} to help optimize your code, you must
433 first compile your program with two special GCC options:
434 @samp{-fprofile-arcs -ftest-coverage}.  Aside from that, you can use any
435 other GCC options; but if you want to prove that every single line
436 in your program was executed, you should not compile with optimization
437 at the same time.  On some machines the optimizer can eliminate some
438 simple code lines by combining them with other lines.  For example, code
439 like this:
441 @smallexample
442 if (a != b)
443   c = 1;
444 else
445   c = 0;
446 @end smallexample
448 @noindent
449 can be compiled into one instruction on some machines.  In this case,
450 there is no way for @command{gcov} to calculate separate execution counts
451 for each line because there isn't separate code for each line.  Hence
452 the @command{gcov} output looks like this if you compiled the program with
453 optimization:
455 @smallexample
456       100:   12:if (a != b)
457       100:   13:  c = 1;
458       100:   14:else
459       100:   15:  c = 0;
460 @end smallexample
462 The output shows that this block of code, combined by optimization,
463 executed 100 times.  In one sense this result is correct, because there
464 was only one instruction representing all four of these lines.  However,
465 the output does not indicate how many times the result was 0 and how
466 many times the result was 1.
468 Inlineable functions can create unexpected line counts.  Line counts are
469 shown for the source code of the inlineable function, but what is shown
470 depends on where the function is inlined, or if it is not inlined at all.
472 If the function is not inlined, the compiler must emit an out of line
473 copy of the function, in any object file that needs it.  If
474 @file{fileA.o} and @file{fileB.o} both contain out of line bodies of a
475 particular inlineable function, they will also both contain coverage
476 counts for that function.  When @file{fileA.o} and @file{fileB.o} are
477 linked together, the linker will, on many systems, select one of those
478 out of line bodies for all calls to that function, and remove or ignore
479 the other.  Unfortunately, it will not remove the coverage counters for
480 the unused function body.  Hence when instrumented, all but one use of
481 that function will show zero counts.
483 If the function is inlined in several places, the block structure in
484 each location might not be the same.  For instance, a condition might
485 now be calculable at compile time in some instances.  Because the
486 coverage of all the uses of the inline function will be shown for the
487 same source lines, the line counts themselves might seem inconsistent.
489 @c man end
491 @node Gcov Data Files
492 @section Brief description of @command{gcov} data files
494 @command{gcov} uses two files for profiling.  The names of these files
495 are derived from the original @emph{object} file by substituting the
496 file suffix with either @file{.gcno}, or @file{.gcda}.  All of these files
497 are placed in the same directory as the object file, and contain data
498 stored in a platform-independent format.
500 The @file{.gcno} file is generated when the source file is compiled with
501 the GCC @option{-ftest-coverage} option.  It contains information to
502 reconstruct the basic block graphs and assign source line numbers to
503 blocks.
505 The @file{.gcda} file is generated when a program containing object files
506 built with the GCC @option{-fprofile-arcs} option is executed.  A
507 separate @file{.gcda} file is created for each object file compiled with
508 this option.  It contains arc transition counts, and some summary
509 information.
511 The full details of the file format is specified in @file{gcov-io.h},
512 and functions provided in that header file should be used to access the
513 coverage files.