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