OSDN Git Service

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