xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/doc/gcov.texi (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1@c Copyright (C) 1996-2018 Free Software Foundation, Inc.
2@c This is part of the GCC manual.
3@c For copying conditions, see the file gcc.texi.
4
5@ignore
6@c man begin COPYRIGHT
7Copyright @copyright{} 1996-2018 Free Software Foundation, Inc.
8
9Permission is granted to copy, distribute and/or modify this document
10under the terms of the GNU Free Documentation License, Version 1.3 or
11any later version published by the Free Software Foundation; with the
12Invariant Sections being ``GNU General Public License'' and ``Funding
13Free Software'', the Front-Cover texts being (a) (see below), and with
14the Back-Cover Texts being (b) (see below).  A copy of the license is
15included in the gfdl(7) man page.
16
17(a) The FSF's Front-Cover Text is:
18
19     A GNU Manual
20
21(b) The FSF's Back-Cover Text is:
22
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
31
32@node Gcov
33@chapter @command{gcov}---a Test Coverage Program
34
35@command{gcov} is a tool you can use in conjunction with GCC to
36test code coverage in your programs.
37
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
45
46@node Gcov Intro
47@section Introduction to @command{gcov}
48@c man begin DESCRIPTION
49
50@command{gcov} is a test coverage program.  Use it in concert with GCC
51to analyze your programs to help create more efficient, faster running
52code and to discover untested parts of your program.  You can use
53@command{gcov} as a profiling tool to help discover where your
54optimization efforts will best affect your code.  You can also use
55@command{gcov} along with the other profiling tool, @command{gprof}, to
56assess which parts of your code use the greatest amount of computing
57time.
58
59Profiling tools help you analyze your code's performance.  Using a
60profiler such as @command{gcov} or @command{gprof}, you can find out some
61basic performance statistics, such as:
62
63@itemize @bullet
64@item
65how often each line of code executes
66
67@item
68what lines of code are actually executed
69
70@item
71how much computing time each section of code uses
72@end itemize
73
74Once you know these things about how your code works when compiled, you
75can look at each module to see which modules should be optimized.
76@command{gcov} helps you determine where to work on optimization.
77
78Software developers also use coverage testing in concert with
79testsuites, to make sure software is actually good enough for a release.
80Testsuites can verify that a program works as expected; a coverage
81program tests to see how much of the program is exercised by the
82testsuite.  Developers can then determine what kinds of test cases need
83to be added to the testsuites to create both better testing and a better
84final product.
85
86You should compile your code without optimization if you plan to use
87@command{gcov} because the optimization, by combining some lines of code
88into one function, may not give you as much information as you need to
89look for `hot spots' where the code is using a great deal of computer
90time.  Likewise, because @command{gcov} accumulates statistics by line (at
91the lowest resolution), it works best with a programming style that
92places only one statement on each line.  If you use complicated macros
93that expand to loops or to other control structures, the statistics are
94less helpful---they only report on the line where the macro call
95appears.  If your complex macros behave like functions, you can replace
96them with inline functions to solve this problem.
97
98@command{gcov} creates a logfile called @file{@var{sourcefile}.gcov} which
99indicates how many times each line of a source file @file{@var{sourcefile}.c}
100has executed.  You can use these logfiles along with @command{gprof} to aid
101in fine-tuning the performance of your programs.  @command{gprof} gives
102timing information you can use along with the information you get from
103@command{gcov}.
104
105@command{gcov} works only on code compiled with GCC@.  It is not
106compatible with any other profiling or test coverage mechanism.
107
108@c man end
109
110@node Invoking Gcov
111@section Invoking @command{gcov}
112
113@smallexample
114gcov @r{[}@var{options}@r{]} @var{files}
115@end smallexample
116
117@command{gcov} accepts the following options:
118
119@ignore
120@c man begin SYNOPSIS
121gcov [@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{-d}|@option{--display-progress}]
126     [@option{-f}|@option{--function-summaries}]
127     [@option{-i}|@option{--intermediate-format}]
128     [@option{-j}|@option{--human-readable}]
129     [@option{-k}|@option{--use-colors}]
130     [@option{-l}|@option{--long-file-names}]
131     [@option{-m}|@option{--demangled-names}]
132     [@option{-n}|@option{--no-output}]
133     [@option{-o}|@option{--object-directory} @var{directory|file}]
134     [@option{-p}|@option{--preserve-paths}]
135     [@option{-r}|@option{--relative-only}]
136     [@option{-s}|@option{--source-prefix} @var{directory}]
137     [@option{-u}|@option{--unconditional-branches}]
138     [@option{-x}|@option{--hash-filenames}]
139     @var{files}
140@c man end
141@c man begin SEEALSO
142gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}.
143@c man end
144@end ignore
145
146@c man begin OPTIONS
147@table @gcctabopt
148
149@item -a
150@itemx --all-blocks
151Write individual execution counts for every basic block.  Normally gcov
152outputs execution counts only for the main blocks of a line.  With this
153option you can determine if blocks within a single line are not being
154executed.
155
156@item -b
157@itemx --branch-probabilities
158Write branch frequencies to the output file, and write branch summary
159info to the standard output.  This option allows you to see how often
160each branch in your program was taken.  Unconditional branches will not
161be shown, unless the @option{-u} option is given.
162
163@item -c
164@itemx --branch-counts
165Write branch frequencies as the number of branches taken, rather than
166the percentage of branches taken.
167
168@item -d
169@itemx --display-progress
170Display the progress on the standard output.
171
172@item -f
173@itemx --function-summaries
174Output summaries for each function in addition to the file level summary.
175
176@item -h
177@itemx --help
178Display help about using @command{gcov} (on the standard output), and
179exit without doing any further processing.
180
181@item -i
182@itemx --intermediate-format
183Output gcov file in an easy-to-parse intermediate text format that can
184be used by @command{lcov} or other tools. The output is a single
185@file{.gcov} file per @file{.gcda} file. No source code is required.
186
187The format of the intermediate @file{.gcov} file is plain text with
188one entry per line
189
190@smallexample
191version:@var{gcc_version}
192file:@var{source_file_name}
193function:@var{start_line_number},@var{end_line_number},@var{execution_count},@var{function_name}
194lcount:@var{line number},@var{execution_count},@var{has_unexecuted_block}
195branch:@var{line_number},@var{branch_coverage_type}
196
197Where the @var{branch_coverage_type} is
198   notexec (Branch not executed)
199   taken (Branch executed and taken)
200   nottaken (Branch executed, but not taken)
201@end smallexample
202
203There can be multiple @var{file} entries in an intermediate gcov
204file. All entries following a @var{file} pertain to that source file
205until the next @var{file} entry.  If there are multiple functions that
206start on a single line, then corresponding lcount is repeated multiple
207times.
208
209Here is a sample when @option{-i} is used in conjunction with @option{-b} option:
210
211@smallexample
212version: 8.1.0 20180103
213file:tmp.cpp
214function:7,7,0,_ZN3FooIcEC2Ev
215function:7,7,1,_ZN3FooIiEC2Ev
216function:8,8,0,_ZN3FooIcE3incEv
217function:8,8,2,_ZN3FooIiE3incEv
218function:18,37,1,main
219lcount:7,0,1
220lcount:7,1,0
221lcount:8,0,1
222lcount:8,2,0
223lcount:18,1,0
224lcount:21,1,0
225branch:21,taken
226branch:21,nottaken
227lcount:23,1,0
228branch:23,taken
229branch:23,nottaken
230lcount:24,1,0
231branch:24,taken
232branch:24,nottaken
233lcount:25,1,0
234lcount:27,11,0
235branch:27,taken
236branch:27,taken
237lcount:28,10,0
238lcount:30,1,1
239branch:30,nottaken
240branch:30,taken
241lcount:32,1,0
242branch:32,nottaken
243branch:32,taken
244lcount:33,0,1
245branch:33,notexec
246branch:33,notexec
247lcount:35,1,0
248branch:35,taken
249branch:35,nottaken
250lcount:36,1,0
251@end smallexample
252
253@item -j
254@itemx --human-readable
255Write counts in human readable format (like 24k).
256
257@item -k
258@itemx --use-colors
259
260Use colors for lines of code that have zero coverage.  We use red color for
261non-exceptional lines and cyan for exceptional.  Same colors are used for
262basic blocks with @option{-a} option.
263
264
265@item -l
266@itemx --long-file-names
267Create long file names for included source files.  For example, if the
268header file @file{x.h} contains code, and was included in the file
269@file{a.c}, then running @command{gcov} on the file @file{a.c} will
270produce an output file called @file{a.c##x.h.gcov} instead of
271@file{x.h.gcov}.  This can be useful if @file{x.h} is included in
272multiple source files and you want to see the individual
273contributions.  If you use the @samp{-p} option, both the including
274and included file names will be complete path names.
275
276@item -m
277@itemx --demangled-names
278Display demangled function names in output. The default is to show
279mangled function names.
280
281@item -n
282@itemx --no-output
283Do not create the @command{gcov} output file.
284
285@item -o @var{directory|file}
286@itemx --object-directory @var{directory}
287@itemx --object-file @var{file}
288Specify either the directory containing the gcov data files, or the
289object path name.  The @file{.gcno}, and
290@file{.gcda} data files are searched for using this option.  If a directory
291is specified, the data files are in that directory and named after the
292input file name, without its extension.  If a file is specified here,
293the data files are named after that file, without its extension.
294
295@item -p
296@itemx --preserve-paths
297Preserve complete path information in the names of generated
298@file{.gcov} files.  Without this option, just the filename component is
299used.  With this option, all directories are used, with @samp{/} characters
300translated to @samp{#} characters, @file{.} directory components
301removed and unremoveable @file{..}
302components renamed to @samp{^}.  This is useful if sourcefiles are in several
303different directories.
304
305@item -r
306@itemx --relative-only
307Only output information about source files with a relative pathname
308(after source prefix elision).  Absolute paths are usually system
309header files and coverage of any inline functions therein is normally
310uninteresting.
311
312@item -s @var{directory}
313@itemx --source-prefix @var{directory}
314A prefix for source file names to remove when generating the output
315coverage files.  This option is useful when building in a separate
316directory, and the pathname to the source directory is not wanted when
317determining the output file names.  Note that this prefix detection is
318applied before determining whether the source file is absolute.
319
320@item -u
321@itemx --unconditional-branches
322When branch probabilities are given, include those of unconditional branches.
323Unconditional branches are normally not interesting.
324
325@item -v
326@itemx --version
327Display the @command{gcov} version number (on the standard output),
328and exit without doing any further processing.
329
330@item -w
331@itemx --verbose
332Print verbose informations related to basic blocks and arcs.
333
334@item -x
335@itemx --hash-filenames
336By default, gcov uses the full pathname of the source files to create
337an output filename.  This can lead to long filenames that can overflow
338filesystem limits.  This option creates names of the form
339@file{@var{source-file}##@var{md5}.gcov},
340where the @var{source-file} component is the final filename part and
341the @var{md5} component is calculated from the full mangled name that
342would have been used otherwise.
343
344@end table
345
346@command{gcov} should be run with the current directory the same as that
347when you invoked the compiler.  Otherwise it will not be able to locate
348the source files.  @command{gcov} produces files called
349@file{@var{mangledname}.gcov} in the current directory.  These contain
350the coverage information of the source file they correspond to.
351One @file{.gcov} file is produced for each source (or header) file
352containing code,
353which was compiled to produce the data files.  The @var{mangledname} part
354of the output file name is usually simply the source file name, but can
355be something more complicated if the @samp{-l} or @samp{-p} options are
356given.  Refer to those options for details.
357
358If you invoke @command{gcov} with multiple input files, the
359contributions from each input file are summed.  Typically you would
360invoke it with the same list of files as the final link of your executable.
361
362The @file{.gcov} files contain the @samp{:} separated fields along with
363program source code.  The format is
364
365@smallexample
366@var{execution_count}:@var{line_number}:@var{source line text}
367@end smallexample
368
369Additional block information may succeed each line, when requested by
370command line option.  The @var{execution_count} is @samp{-} for lines
371containing no code.  Unexecuted lines are marked @samp{#####} or
372@samp{=====}, depending on whether they are reachable by
373non-exceptional paths or only exceptional paths such as C++ exception
374handlers, respectively. Given @samp{-a} option, unexecuted blocks are
375marked @samp{$$$$$} or @samp{%%%%%}, depending on whether a basic block
376is reachable via non-exceptional or exceptional paths.
377Executed basic blocks having a statement with zero @var{execution_count}
378end with @samp{*} character and are colored with magenta color with @option{-k}
379option.  The functionality is not supported in Ada.
380
381Note that GCC can completely remove the bodies of functions that are
382not needed -- for instance if they are inlined everywhere.  Such functions
383are marked with @samp{-}, which can be confusing.
384Use the @option{-fkeep-inline-functions} and @option{-fkeep-static-functions}
385options to retain these functions and
386allow gcov to properly show their @var{execution_count}.
387
388Some lines of information at the start have @var{line_number} of zero.
389These preamble lines are of the form
390
391@smallexample
392-:0:@var{tag}:@var{value}
393@end smallexample
394
395The ordering and number of these preamble lines will be augmented as
396@command{gcov} development progresses --- do not rely on them remaining
397unchanged.  Use @var{tag} to locate a particular preamble line.
398
399The additional block information is of the form
400
401@smallexample
402@var{tag} @var{information}
403@end smallexample
404
405The @var{information} is human readable, but designed to be simple
406enough for machine parsing too.
407
408When printing percentages, 0% and 100% are only printed when the values
409are @emph{exactly} 0% and 100% respectively.  Other values which would
410conventionally be rounded to 0% or 100% are instead printed as the
411nearest non-boundary value.
412
413When using @command{gcov}, you must first compile your program with two
414special GCC options: @samp{-fprofile-arcs -ftest-coverage}.
415This tells the compiler to generate additional information needed by
416gcov (basically a flow graph of the program) and also includes
417additional code in the object files for generating the extra profiling
418information needed by gcov.  These additional files are placed in the
419directory where the object file is located.
420
421Running the program will cause profile output to be generated.  For each
422source file compiled with @option{-fprofile-arcs}, an accompanying
423@file{.gcda} file will be placed in the object file directory.
424
425Running @command{gcov} with your program's source file names as arguments
426will now produce a listing of the code along with frequency of execution
427for each line.  For example, if your program is called @file{tmp.cpp}, this
428is what you see when you use the basic @command{gcov} facility:
429
430@smallexample
431$ g++ -fprofile-arcs -ftest-coverage tmp.cpp
432$ a.out
433$ gcov tmp.cpp -m
434File 'tmp.cpp'
435Lines executed:92.86% of 14
436Creating 'tmp.cpp.gcov'
437@end smallexample
438
439The file @file{tmp.cpp.gcov} contains output from @command{gcov}.
440Here is a sample:
441
442@smallexample
443        -:    0:Source:tmp.cpp
444        -:    0:Graph:tmp.gcno
445        -:    0:Data:tmp.gcda
446        -:    0:Runs:1
447        -:    0:Programs:1
448        -:    1:#include <stdio.h>
449        -:    2:
450        -:    3:template<class T>
451        -:    4:class Foo
452        -:    5:@{
453        -:    6:  public:
454       1*:    7:  Foo(): b (1000) @{@}
455------------------
456Foo<char>::Foo():
457    #####:    7:  Foo(): b (1000) @{@}
458------------------
459Foo<int>::Foo():
460        1:    7:  Foo(): b (1000) @{@}
461------------------
462       2*:    8:  void inc () @{ b++; @}
463------------------
464Foo<char>::inc():
465    #####:    8:  void inc () @{ b++; @}
466------------------
467Foo<int>::inc():
468        2:    8:  void inc () @{ b++; @}
469------------------
470        -:    9:
471        -:   10:  private:
472        -:   11:  int b;
473        -:   12:@};
474        -:   13:
475        -:   14:template class Foo<int>;
476        -:   15:template class Foo<char>;
477        -:   16:
478        -:   17:int
479        1:   18:main (void)
480        -:   19:@{
481        -:   20:  int i, total;
482        1:   21:  Foo<int> counter;
483        -:   22:
484        1:   23:  counter.inc();
485        1:   24:  counter.inc();
486        1:   25:  total = 0;
487        -:   26:
488       11:   27:  for (i = 0; i < 10; i++)
489       10:   28:    total += i;
490        -:   29:
491       1*:   30:  int v = total > 100 ? 1 : 2;
492        -:   31:
493        1:   32:  if (total != 45)
494    #####:   33:    printf ("Failure\n");
495        -:   34:  else
496        1:   35:    printf ("Success\n");
497        1:   36:  return 0;
498        -:   37:@}
499@end smallexample
500
501Note that line 7 is shown in the report multiple times.  First occurrence
502presents total number of execution of the line and the next two belong
503to instances of class Foo constructors.  As you can also see, line 30 contains
504some unexecuted basic blocks and thus execution count has asterisk symbol.
505
506When you use the @option{-a} option, you will get individual block
507counts, and the output looks like this:
508
509@smallexample
510        -:    0:Source:tmp.cpp
511        -:    0:Graph:tmp.gcno
512        -:    0:Data:tmp.gcda
513        -:    0:Runs:1
514        -:    0:Programs:1
515        -:    1:#include <stdio.h>
516        -:    2:
517        -:    3:template<class T>
518        -:    4:class Foo
519        -:    5:@{
520        -:    6:  public:
521       1*:    7:  Foo(): b (1000) @{@}
522------------------
523Foo<char>::Foo():
524    #####:    7:  Foo(): b (1000) @{@}
525------------------
526Foo<int>::Foo():
527        1:    7:  Foo(): b (1000) @{@}
528------------------
529       2*:    8:  void inc () @{ b++; @}
530------------------
531Foo<char>::inc():
532    #####:    8:  void inc () @{ b++; @}
533------------------
534Foo<int>::inc():
535        2:    8:  void inc () @{ b++; @}
536------------------
537        -:    9:
538        -:   10:  private:
539        -:   11:  int b;
540        -:   12:@};
541        -:   13:
542        -:   14:template class Foo<int>;
543        -:   15:template class Foo<char>;
544        -:   16:
545        -:   17:int
546        1:   18:main (void)
547        -:   19:@{
548        -:   20:  int i, total;
549        1:   21:  Foo<int> counter;
550        1:   21-block  0
551        -:   22:
552        1:   23:  counter.inc();
553        1:   23-block  0
554        1:   24:  counter.inc();
555        1:   24-block  0
556        1:   25:  total = 0;
557        -:   26:
558       11:   27:  for (i = 0; i < 10; i++)
559        1:   27-block  0
560       11:   27-block  1
561       10:   28:    total += i;
562       10:   28-block  0
563        -:   29:
564       1*:   30:  int v = total > 100 ? 1 : 2;
565        1:   30-block  0
566    %%%%%:   30-block  1
567        1:   30-block  2
568        -:   31:
569        1:   32:  if (total != 45)
570        1:   32-block  0
571    #####:   33:    printf ("Failure\n");
572    %%%%%:   33-block  0
573        -:   34:  else
574        1:   35:    printf ("Success\n");
575        1:   35-block  0
576        1:   36:  return 0;
577        1:   36-block  0
578        -:   37:@}
579@end smallexample
580
581In this mode, each basic block is only shown on one line -- the last
582line of the block.  A multi-line block will only contribute to the
583execution count of that last line, and other lines will not be shown
584to contain code, unless previous blocks end on those lines.
585The total execution count of a line is shown and subsequent lines show
586the execution counts for individual blocks that end on that line.  After each
587block, the branch and call counts of the block will be shown, if the
588@option{-b} option is given.
589
590Because of the way GCC instruments calls, a call count can be shown
591after a line with no individual blocks.
592As you can see, line 33 contains a basic block that was not executed.
593
594@need 450
595When you use the @option{-b} option, your output looks like this:
596
597@smallexample
598        -:    0:Source:tmp.cpp
599        -:    0:Graph:tmp.gcno
600        -:    0:Data:tmp.gcda
601        -:    0:Runs:1
602        -:    0:Programs:1
603        -:    1:#include <stdio.h>
604        -:    2:
605        -:    3:template<class T>
606        -:    4:class Foo
607        -:    5:@{
608        -:    6:  public:
609       1*:    7:  Foo(): b (1000) @{@}
610------------------
611Foo<char>::Foo():
612function Foo<char>::Foo() called 0 returned 0% blocks executed 0%
613    #####:    7:  Foo(): b (1000) @{@}
614------------------
615Foo<int>::Foo():
616function Foo<int>::Foo() called 1 returned 100% blocks executed 100%
617        1:    7:  Foo(): b (1000) @{@}
618------------------
619       2*:    8:  void inc () @{ b++; @}
620------------------
621Foo<char>::inc():
622function Foo<char>::inc() called 0 returned 0% blocks executed 0%
623    #####:    8:  void inc () @{ b++; @}
624------------------
625Foo<int>::inc():
626function Foo<int>::inc() called 2 returned 100% blocks executed 100%
627        2:    8:  void inc () @{ b++; @}
628------------------
629        -:    9:
630        -:   10:  private:
631        -:   11:  int b;
632        -:   12:@};
633        -:   13:
634        -:   14:template class Foo<int>;
635        -:   15:template class Foo<char>;
636        -:   16:
637        -:   17:int
638function main called 1 returned 100% blocks executed 81%
639        1:   18:main (void)
640        -:   19:@{
641        -:   20:  int i, total;
642        1:   21:  Foo<int> counter;
643call    0 returned 100%
644branch  1 taken 100% (fallthrough)
645branch  2 taken 0% (throw)
646        -:   22:
647        1:   23:  counter.inc();
648call    0 returned 100%
649branch  1 taken 100% (fallthrough)
650branch  2 taken 0% (throw)
651        1:   24:  counter.inc();
652call    0 returned 100%
653branch  1 taken 100% (fallthrough)
654branch  2 taken 0% (throw)
655        1:   25:  total = 0;
656        -:   26:
657       11:   27:  for (i = 0; i < 10; i++)
658branch  0 taken 91% (fallthrough)
659branch  1 taken 9%
660       10:   28:    total += i;
661        -:   29:
662       1*:   30:  int v = total > 100 ? 1 : 2;
663branch  0 taken 0% (fallthrough)
664branch  1 taken 100%
665        -:   31:
666        1:   32:  if (total != 45)
667branch  0 taken 0% (fallthrough)
668branch  1 taken 100%
669    #####:   33:    printf ("Failure\n");
670call    0 never executed
671branch  1 never executed
672branch  2 never executed
673        -:   34:  else
674        1:   35:    printf ("Success\n");
675call    0 returned 100%
676branch  1 taken 100% (fallthrough)
677branch  2 taken 0% (throw)
678        1:   36:  return 0;
679        -:   37:@}
680@end smallexample
681
682For each function, a line is printed showing how many times the function
683is called, how many times it returns and what percentage of the
684function's blocks were executed.
685
686For each basic block, a line is printed after the last line of the basic
687block describing the branch or call that ends the basic block.  There can
688be multiple branches and calls listed for a single source line if there
689are multiple basic blocks that end on that line.  In this case, the
690branches and calls are each given a number.  There is no simple way to map
691these branches and calls back to source constructs.  In general, though,
692the lowest numbered branch or call will correspond to the leftmost construct
693on the source line.
694
695For a branch, if it was executed at least once, then a percentage
696indicating the number of times the branch was taken divided by the
697number of times the branch was executed will be printed.  Otherwise, the
698message ``never executed'' is printed.
699
700For a call, if it was executed at least once, then a percentage
701indicating the number of times the call returned divided by the number
702of times the call was executed will be printed.  This will usually be
703100%, but may be less for functions that call @code{exit} or @code{longjmp},
704and thus may not return every time they are called.
705
706The execution counts are cumulative.  If the example program were
707executed again without removing the @file{.gcda} file, the count for the
708number of times each line in the source was executed would be added to
709the results of the previous run(s).  This is potentially useful in
710several ways.  For example, it could be used to accumulate data over a
711number of program runs as part of a test verification suite, or to
712provide more accurate long-term information over a large number of
713program runs.
714
715The data in the @file{.gcda} files is saved immediately before the program
716exits.  For each source file compiled with @option{-fprofile-arcs}, the
717profiling code first attempts to read in an existing @file{.gcda} file; if
718the file doesn't match the executable (differing number of basic block
719counts) it will ignore the contents of the file.  It then adds in the
720new execution counts and finally writes the data to the file.
721
722@node Gcov and Optimization
723@section Using @command{gcov} with GCC Optimization
724
725If you plan to use @command{gcov} to help optimize your code, you must
726first compile your program with two special GCC options:
727@samp{-fprofile-arcs -ftest-coverage}.  Aside from that, you can use any
728other GCC options; but if you want to prove that every single line
729in your program was executed, you should not compile with optimization
730at the same time.  On some machines the optimizer can eliminate some
731simple code lines by combining them with other lines.  For example, code
732like this:
733
734@smallexample
735if (a != b)
736  c = 1;
737else
738  c = 0;
739@end smallexample
740
741@noindent
742can be compiled into one instruction on some machines.  In this case,
743there is no way for @command{gcov} to calculate separate execution counts
744for each line because there isn't separate code for each line.  Hence
745the @command{gcov} output looks like this if you compiled the program with
746optimization:
747
748@smallexample
749      100:   12:if (a != b)
750      100:   13:  c = 1;
751      100:   14:else
752      100:   15:  c = 0;
753@end smallexample
754
755The output shows that this block of code, combined by optimization,
756executed 100 times.  In one sense this result is correct, because there
757was only one instruction representing all four of these lines.  However,
758the output does not indicate how many times the result was 0 and how
759many times the result was 1.
760
761Inlineable functions can create unexpected line counts.  Line counts are
762shown for the source code of the inlineable function, but what is shown
763depends on where the function is inlined, or if it is not inlined at all.
764
765If the function is not inlined, the compiler must emit an out of line
766copy of the function, in any object file that needs it.  If
767@file{fileA.o} and @file{fileB.o} both contain out of line bodies of a
768particular inlineable function, they will also both contain coverage
769counts for that function.  When @file{fileA.o} and @file{fileB.o} are
770linked together, the linker will, on many systems, select one of those
771out of line bodies for all calls to that function, and remove or ignore
772the other.  Unfortunately, it will not remove the coverage counters for
773the unused function body.  Hence when instrumented, all but one use of
774that function will show zero counts.
775
776If the function is inlined in several places, the block structure in
777each location might not be the same.  For instance, a condition might
778now be calculable at compile time in some instances.  Because the
779coverage of all the uses of the inline function will be shown for the
780same source lines, the line counts themselves might seem inconsistent.
781
782Long-running applications can use the @code{__gcov_reset} and @code{__gcov_dump}
783facilities to restrict profile collection to the program region of
784interest. Calling @code{__gcov_reset(void)} will clear all profile counters
785to zero, and calling @code{__gcov_dump(void)} will cause the profile information
786collected at that point to be dumped to @file{.gcda} output files.
787Instrumented applications use a static destructor with priority 99
788to invoke the @code{__gcov_dump} function. Thus @code{__gcov_dump}
789is executed after all user defined static destructors,
790as well as handlers registered with @code{atexit}.
791If an executable loads a dynamic shared object via dlopen functionality,
792@option{-Wl,--dynamic-list-data} is needed to dump all profile data.
793
794@c man end
795
796@node Gcov Data Files
797@section Brief Description of @command{gcov} Data Files
798
799@command{gcov} uses two files for profiling.  The names of these files
800are derived from the original @emph{object} file by substituting the
801file suffix with either @file{.gcno}, or @file{.gcda}.  The files
802contain coverage and profile data stored in a platform-independent format.
803The @file{.gcno} files are placed in the same directory as the object
804file.  By default, the @file{.gcda} files are also stored in the same
805directory as the object file, but the GCC @option{-fprofile-dir} option
806may be used to store the @file{.gcda} files in a separate directory.
807
808The @file{.gcno} notes file is generated when the source file is compiled
809with the GCC @option{-ftest-coverage} option.  It contains information to
810reconstruct the basic block graphs and assign source line numbers to
811blocks.
812
813The @file{.gcda} count data file is generated when a program containing
814object files built with the GCC @option{-fprofile-arcs} option is executed.
815A separate @file{.gcda} file is created for each object file compiled with
816this option.  It contains arc transition counts, value profile counts, and
817some summary information.
818
819It is not recommended to access the coverage files directly.
820Consumers should use the intermediate format that is provided
821by @command{gcov} tool via @option{--intermediate-format} option.
822
823@node Cross-profiling
824@section Data File Relocation to Support Cross-Profiling
825
826Running the program will cause profile output to be generated.  For each
827source file compiled with @option{-fprofile-arcs}, an accompanying @file{.gcda}
828file will be placed in the object file directory. That implicitly requires
829running the program on the same system as it was built or having the same
830absolute directory structure on the target system. The program will try
831to create the needed directory structure, if it is not already present.
832
833To support cross-profiling, a program compiled with @option{-fprofile-arcs}
834can relocate the data files based on two environment variables:
835
836@itemize @bullet
837@item
838GCOV_PREFIX contains the prefix to add to the absolute paths
839in the object file. Prefix can be absolute, or relative.  The
840default is no prefix.
841
842@item
843GCOV_PREFIX_STRIP indicates the how many initial directory names to strip off
844the hardwired absolute paths. Default value is 0.
845
846@emph{Note:} If GCOV_PREFIX_STRIP is set without GCOV_PREFIX is undefined,
847 then a relative path is made out of the hardwired absolute paths.
848@end itemize
849
850For example, if the object file @file{/user/build/foo.o} was built with
851@option{-fprofile-arcs}, the final executable will try to create the data file
852@file{/user/build/foo.gcda} when running on the target system.  This will
853fail if the corresponding directory does not exist and it is unable to create
854it.  This can be overcome by, for example, setting the environment as
855@samp{GCOV_PREFIX=/target/run} and @samp{GCOV_PREFIX_STRIP=1}.  Such a
856setting will name the data file @file{/target/run/build/foo.gcda}.
857
858You must move the data files to the expected directory tree in order to
859use them for profile directed optimizations (@option{-fprofile-use}), or to
860use the @command{gcov} tool.
861