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