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