* decl2.c (min_vis_expr_r, expr_visibility): New.
[official-gcc.git] / gcc / doc / gcov.texi
blob8046fd78e358371a4f1d4b8fd67cf450b2f1554e
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{--json-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{-q}|@option{--use-hotness-colors}]
136      [@option{-r}|@option{--relative-only}]
137      [@option{-s}|@option{--source-prefix} @var{directory}]
138      [@option{-t}|@option{--stdout}]
139      [@option{-u}|@option{--unconditional-branches}]
140      [@option{-x}|@option{--hash-filenames}]
141      @var{files}
142 @c man end
143 @c man begin SEEALSO
144 gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}.
145 @c man end
146 @end ignore
148 @c man begin OPTIONS
149 @table @gcctabopt
151 @item -a
152 @itemx --all-blocks
153 Write individual execution counts for every basic block.  Normally gcov
154 outputs execution counts only for the main blocks of a line.  With this
155 option you can determine if blocks within a single line are not being
156 executed.
158 @item -b
159 @itemx --branch-probabilities
160 Write branch frequencies to the output file, and write branch summary
161 info to the standard output.  This option allows you to see how often
162 each branch in your program was taken.  Unconditional branches will not
163 be shown, unless the @option{-u} option is given.
165 @item -c
166 @itemx --branch-counts
167 Write branch frequencies as the number of branches taken, rather than
168 the percentage of branches taken.
170 @item -d
171 @itemx --display-progress
172 Display the progress on the standard output.
174 @item -f
175 @itemx --function-summaries
176 Output summaries for each function in addition to the file level summary.
178 @item -h
179 @itemx --help
180 Display help about using @command{gcov} (on the standard output), and
181 exit without doing any further processing.
183 @item -i
184 @itemx --json-format
185 Output gcov file in an easy-to-parse JSON intermediate format
186 which does not require source code for generation.  The JSON
187 file is compressed with gzip compression algorithm
188 and the files have @file{.gcov.json.gz} extension.
190 Structure of the JSON is following:
192 @smallexample
194   "current_working_directory": @var{current_working_directory},
195   "format_version": @var{format_version},
196   "gcc_version": @var{gcc_version}
197   "files": [@var{file}]
199 @end smallexample
201 Fields of the root element have following semantics:
203 @itemize @bullet
204 @item
205 @var{current_working_directory}: working directory where
206 a compilation unit was compiled
208 @item
209 @var{format_version}: semantic version of the format
211 @item
212 @var{gcc_version}: version of the GCC compiler
213 @end itemize
215 Each @var{file} has the following form:
217 @smallexample
219   "file": @var{file_name},
220   "functions": [@var{function}],
221   "lines": [@var{line}]
223 @end smallexample
225 Fields of the @var{file} element have following semantics:
227 @itemize @bullet
228 @item
229 @var{file_name}: name of the source file
230 @end itemize
232 Each @var{function} has the following form:
234 @smallexample
236   "blocks": @var{blocks},
237   "blocks_executed": @var{blocks_executed},
238   "demangled_name": "@var{demangled_name},
239   "end_line": @var{end_line},
240   "execution_count": @var{execution_count},
241   "name": @var{name},
242   "start_line": @var{start_line}
244 @end smallexample
246 Fields of the @var{function} element have following semantics:
248 @itemize @bullet
249 @item
250 @var{blocks}: number of blocks that are in the function
252 @item
253 @var{blocks_executed}: number of executed blocks of the function
255 @item
256 @var{demangled_name}: demangled name of the function
258 @item
259 @var{end_line}: line in the source file where the function ends
261 @item
262 @var{execution_count}: number of executions of the function
264 @item
265 @var{name}: name of the function
267 @item
268 @var{start_line}: line in the source file where the function begins
269 @end itemize
271 Each @var{line} has the following form:
273 @smallexample
275   "branches": [@var{branch}],
276   "count": @var{count},
277   "line_number": @var{line_number},
278   "unexecuted_block": @var{unexecuted_block}
280 @end smallexample
282 Branches are present only with @var{-b} option.
283 Fields of the @var{line} element have following semantics:
285 @itemize @bullet
286 @item
287 @var{count}: number of executions of the line
289 @item
290 @var{line_number}: line number
292 @item
293 @var{unexecuted_block}: flag whether the line contains an unexecuted block
294 (not all statements on the line are executed)
295 @end itemize
297 Each @var{branch} has the following form:
299 @smallexample
301   "count": @var{count},
302   "fallthrough": @var{fallthrough},
303   "throw": @var{throw}
305 @end smallexample
307 Fields of the @var{branch} element have following semantics:
309 @itemize @bullet
310 @item
311 @var{count}: number of executions of the branch
313 @item
314 @var{fallthrough}: true when the branch is a fall through branch
316 @item
317 @var{throw}: true when the branch is an exceptional branch
318 @end itemize
320 @item -j
321 @itemx --human-readable
322 Write counts in human readable format (like 24.6k).
324 @item -k
325 @itemx --use-colors
327 Use colors for lines of code that have zero coverage.  We use red color for
328 non-exceptional lines and cyan for exceptional.  Same colors are used for
329 basic blocks with @option{-a} option.
331 @item -l
332 @itemx --long-file-names
333 Create long file names for included source files.  For example, if the
334 header file @file{x.h} contains code, and was included in the file
335 @file{a.c}, then running @command{gcov} on the file @file{a.c} will
336 produce an output file called @file{a.c##x.h.gcov} instead of
337 @file{x.h.gcov}.  This can be useful if @file{x.h} is included in
338 multiple source files and you want to see the individual
339 contributions.  If you use the @samp{-p} option, both the including
340 and included file names will be complete path names.
342 @item -m
343 @itemx --demangled-names
344 Display demangled function names in output. The default is to show
345 mangled function names.
347 @item -n
348 @itemx --no-output
349 Do not create the @command{gcov} output file.
351 @item -o @var{directory|file}
352 @itemx --object-directory @var{directory}
353 @itemx --object-file @var{file}
354 Specify either the directory containing the gcov data files, or the
355 object path name.  The @file{.gcno}, and
356 @file{.gcda} data files are searched for using this option.  If a directory
357 is specified, the data files are in that directory and named after the
358 input file name, without its extension.  If a file is specified here,
359 the data files are named after that file, without its extension.
361 @item -p
362 @itemx --preserve-paths
363 Preserve complete path information in the names of generated
364 @file{.gcov} files.  Without this option, just the filename component is
365 used.  With this option, all directories are used, with @samp{/} characters
366 translated to @samp{#} characters, @file{.} directory components
367 removed and unremoveable @file{..}
368 components renamed to @samp{^}.  This is useful if sourcefiles are in several
369 different directories.
371 @item -q
372 @itemx --use-hotness-colors
374 Emit perf-like colored output for hot lines.  Legend of the color scale
375 is printed at the very beginning of the output file.
377 @item -r
378 @itemx --relative-only
379 Only output information about source files with a relative pathname
380 (after source prefix elision).  Absolute paths are usually system
381 header files and coverage of any inline functions therein is normally
382 uninteresting.
384 @item -s @var{directory}
385 @itemx --source-prefix @var{directory}
386 A prefix for source file names to remove when generating the output
387 coverage files.  This option is useful when building in a separate
388 directory, and the pathname to the source directory is not wanted when
389 determining the output file names.  Note that this prefix detection is
390 applied before determining whether the source file is absolute.
392 @item -t
393 @itemx --stdout
394 Output to standard output instead of output files.
396 @item -u
397 @itemx --unconditional-branches
398 When branch probabilities are given, include those of unconditional branches.
399 Unconditional branches are normally not interesting.
401 @item -v
402 @itemx --version
403 Display the @command{gcov} version number (on the standard output),
404 and exit without doing any further processing.
406 @item -w
407 @itemx --verbose
408 Print verbose informations related to basic blocks and arcs.
410 @item -x
411 @itemx --hash-filenames
412 By default, gcov uses the full pathname of the source files to create
413 an output filename.  This can lead to long filenames that can overflow
414 filesystem limits.  This option creates names of the form
415 @file{@var{source-file}##@var{md5}.gcov},
416 where the @var{source-file} component is the final filename part and
417 the @var{md5} component is calculated from the full mangled name that
418 would have been used otherwise.
420 @end table
422 @command{gcov} should be run with the current directory the same as that
423 when you invoked the compiler.  Otherwise it will not be able to locate
424 the source files.  @command{gcov} produces files called
425 @file{@var{mangledname}.gcov} in the current directory.  These contain
426 the coverage information of the source file they correspond to.
427 One @file{.gcov} file is produced for each source (or header) file
428 containing code,
429 which was compiled to produce the data files.  The @var{mangledname} part
430 of the output file name is usually simply the source file name, but can
431 be something more complicated if the @samp{-l} or @samp{-p} options are
432 given.  Refer to those options for details.
434 If you invoke @command{gcov} with multiple input files, the
435 contributions from each input file are summed.  Typically you would
436 invoke it with the same list of files as the final link of your executable.
438 The @file{.gcov} files contain the @samp{:} separated fields along with
439 program source code.  The format is
441 @smallexample
442 @var{execution_count}:@var{line_number}:@var{source line text}
443 @end smallexample
445 Additional block information may succeed each line, when requested by
446 command line option.  The @var{execution_count} is @samp{-} for lines
447 containing no code.  Unexecuted lines are marked @samp{#####} or
448 @samp{=====}, depending on whether they are reachable by
449 non-exceptional paths or only exceptional paths such as C++ exception
450 handlers, respectively. Given the @samp{-a} option, unexecuted blocks are
451 marked @samp{$$$$$} or @samp{%%%%%}, depending on whether a basic block
452 is reachable via non-exceptional or exceptional paths.
453 Executed basic blocks having a statement with zero @var{execution_count}
454 end with @samp{*} character and are colored with magenta color with
455 the @option{-k} option.  This functionality is not supported in Ada.
457 Note that GCC can completely remove the bodies of functions that are
458 not needed -- for instance if they are inlined everywhere.  Such functions
459 are marked with @samp{-}, which can be confusing.
460 Use the @option{-fkeep-inline-functions} and @option{-fkeep-static-functions}
461 options to retain these functions and
462 allow gcov to properly show their @var{execution_count}.
464 Some lines of information at the start have @var{line_number} of zero.
465 These preamble lines are of the form
467 @smallexample
468 -:0:@var{tag}:@var{value}
469 @end smallexample
471 The ordering and number of these preamble lines will be augmented as
472 @command{gcov} development progresses --- do not rely on them remaining
473 unchanged.  Use @var{tag} to locate a particular preamble line.
475 The additional block information is of the form
477 @smallexample
478 @var{tag} @var{information}
479 @end smallexample
481 The @var{information} is human readable, but designed to be simple
482 enough for machine parsing too.
484 When printing percentages, 0% and 100% are only printed when the values
485 are @emph{exactly} 0% and 100% respectively.  Other values which would
486 conventionally be rounded to 0% or 100% are instead printed as the
487 nearest non-boundary value.
489 When using @command{gcov}, you must first compile your program with two
490 special GCC options: @samp{-fprofile-arcs -ftest-coverage}.
491 This tells the compiler to generate additional information needed by
492 gcov (basically a flow graph of the program) and also includes
493 additional code in the object files for generating the extra profiling
494 information needed by gcov.  These additional files are placed in the
495 directory where the object file is located.
497 Running the program will cause profile output to be generated.  For each
498 source file compiled with @option{-fprofile-arcs}, an accompanying
499 @file{.gcda} file will be placed in the object file directory.
501 Running @command{gcov} with your program's source file names as arguments
502 will now produce a listing of the code along with frequency of execution
503 for each line.  For example, if your program is called @file{tmp.cpp}, this
504 is what you see when you use the basic @command{gcov} facility:
506 @smallexample
507 $ g++ -fprofile-arcs -ftest-coverage tmp.cpp
508 $ a.out
509 $ gcov tmp.cpp -m
510 File 'tmp.cpp'
511 Lines executed:92.86% of 14
512 Creating 'tmp.cpp.gcov'
513 @end smallexample
515 The file @file{tmp.cpp.gcov} contains output from @command{gcov}.
516 Here is a sample:
518 @smallexample
519         -:    0:Source:tmp.cpp
520         -:    0:Working directory:/home/gcc/testcase
521         -:    0:Graph:tmp.gcno
522         -:    0:Data:tmp.gcda
523         -:    0:Runs:1
524         -:    0:Programs:1
525         -:    1:#include <stdio.h>
526         -:    2:
527         -:    3:template<class T>
528         -:    4:class Foo
529         -:    5:@{
530         -:    6:  public:
531        1*:    7:  Foo(): b (1000) @{@}
532 ------------------
533 Foo<char>::Foo():
534     #####:    7:  Foo(): b (1000) @{@}
535 ------------------
536 Foo<int>::Foo():
537         1:    7:  Foo(): b (1000) @{@}
538 ------------------
539        2*:    8:  void inc () @{ b++; @}
540 ------------------
541 Foo<char>::inc():
542     #####:    8:  void inc () @{ b++; @}
543 ------------------
544 Foo<int>::inc():
545         2:    8:  void inc () @{ b++; @}
546 ------------------
547         -:    9:
548         -:   10:  private:
549         -:   11:  int b;
550         -:   12:@};
551         -:   13:
552         -:   14:template class Foo<int>;
553         -:   15:template class Foo<char>;
554         -:   16:
555         -:   17:int
556         1:   18:main (void)
557         -:   19:@{
558         -:   20:  int i, total;
559         1:   21:  Foo<int> counter;
560         -:   22:
561         1:   23:  counter.inc();
562         1:   24:  counter.inc();
563         1:   25:  total = 0;
564         -:   26:
565        11:   27:  for (i = 0; i < 10; i++)
566        10:   28:    total += i;
567         -:   29:
568        1*:   30:  int v = total > 100 ? 1 : 2;
569         -:   31:
570         1:   32:  if (total != 45)
571     #####:   33:    printf ("Failure\n");
572         -:   34:  else
573         1:   35:    printf ("Success\n");
574         1:   36:  return 0;
575         -:   37:@}
576 @end smallexample
578 Note that line 7 is shown in the report multiple times.  First occurrence
579 presents total number of execution of the line and the next two belong
580 to instances of class Foo constructors.  As you can also see, line 30 contains
581 some unexecuted basic blocks and thus execution count has asterisk symbol.
583 When you use the @option{-a} option, you will get individual block
584 counts, and the output looks like this:
586 @smallexample
587         -:    0:Source:tmp.cpp
588         -:    0:Working directory:/home/gcc/testcase
589         -:    0:Graph:tmp.gcno
590         -:    0:Data:tmp.gcda
591         -:    0:Runs:1
592         -:    0:Programs:1
593         -:    1:#include <stdio.h>
594         -:    2:
595         -:    3:template<class T>
596         -:    4:class Foo
597         -:    5:@{
598         -:    6:  public:
599        1*:    7:  Foo(): b (1000) @{@}
600 ------------------
601 Foo<char>::Foo():
602     #####:    7:  Foo(): b (1000) @{@}
603 ------------------
604 Foo<int>::Foo():
605         1:    7:  Foo(): b (1000) @{@}
606 ------------------
607        2*:    8:  void inc () @{ b++; @}
608 ------------------
609 Foo<char>::inc():
610     #####:    8:  void inc () @{ b++; @}
611 ------------------
612 Foo<int>::inc():
613         2:    8:  void inc () @{ b++; @}
614 ------------------
615         -:    9:
616         -:   10:  private:
617         -:   11:  int b;
618         -:   12:@};
619         -:   13:
620         -:   14:template class Foo<int>;
621         -:   15:template class Foo<char>;
622         -:   16:
623         -:   17:int
624         1:   18:main (void)
625         -:   19:@{
626         -:   20:  int i, total;
627         1:   21:  Foo<int> counter;
628         1:   21-block  0
629         -:   22:
630         1:   23:  counter.inc();
631         1:   23-block  0
632         1:   24:  counter.inc();
633         1:   24-block  0
634         1:   25:  total = 0;
635         -:   26:
636        11:   27:  for (i = 0; i < 10; i++)
637         1:   27-block  0
638        11:   27-block  1
639        10:   28:    total += i;
640        10:   28-block  0
641         -:   29:
642        1*:   30:  int v = total > 100 ? 1 : 2;
643         1:   30-block  0
644     %%%%%:   30-block  1
645         1:   30-block  2
646         -:   31:
647         1:   32:  if (total != 45)
648         1:   32-block  0
649     #####:   33:    printf ("Failure\n");
650     %%%%%:   33-block  0
651         -:   34:  else
652         1:   35:    printf ("Success\n");
653         1:   35-block  0
654         1:   36:  return 0;
655         1:   36-block  0
656         -:   37:@}
657 @end smallexample
659 In this mode, each basic block is only shown on one line -- the last
660 line of the block.  A multi-line block will only contribute to the
661 execution count of that last line, and other lines will not be shown
662 to contain code, unless previous blocks end on those lines.
663 The total execution count of a line is shown and subsequent lines show
664 the execution counts for individual blocks that end on that line.  After each
665 block, the branch and call counts of the block will be shown, if the
666 @option{-b} option is given.
668 Because of the way GCC instruments calls, a call count can be shown
669 after a line with no individual blocks.
670 As you can see, line 33 contains a basic block that was not executed.
672 @need 450
673 When you use the @option{-b} option, your output looks like this:
675 @smallexample
676         -:    0:Source:tmp.cpp
677         -:    0:Working directory:/home/gcc/testcase
678         -:    0:Graph:tmp.gcno
679         -:    0:Data:tmp.gcda
680         -:    0:Runs:1
681         -:    0:Programs:1
682         -:    1:#include <stdio.h>
683         -:    2:
684         -:    3:template<class T>
685         -:    4:class Foo
686         -:    5:@{
687         -:    6:  public:
688        1*:    7:  Foo(): b (1000) @{@}
689 ------------------
690 Foo<char>::Foo():
691 function Foo<char>::Foo() called 0 returned 0% blocks executed 0%
692     #####:    7:  Foo(): b (1000) @{@}
693 ------------------
694 Foo<int>::Foo():
695 function Foo<int>::Foo() called 1 returned 100% blocks executed 100%
696         1:    7:  Foo(): b (1000) @{@}
697 ------------------
698        2*:    8:  void inc () @{ b++; @}
699 ------------------
700 Foo<char>::inc():
701 function Foo<char>::inc() called 0 returned 0% blocks executed 0%
702     #####:    8:  void inc () @{ b++; @}
703 ------------------
704 Foo<int>::inc():
705 function Foo<int>::inc() called 2 returned 100% blocks executed 100%
706         2:    8:  void inc () @{ b++; @}
707 ------------------
708         -:    9:
709         -:   10:  private:
710         -:   11:  int b;
711         -:   12:@};
712         -:   13:
713         -:   14:template class Foo<int>;
714         -:   15:template class Foo<char>;
715         -:   16:
716         -:   17:int
717 function main called 1 returned 100% blocks executed 81%
718         1:   18:main (void)
719         -:   19:@{
720         -:   20:  int i, total;
721         1:   21:  Foo<int> counter;
722 call    0 returned 100%
723 branch  1 taken 100% (fallthrough)
724 branch  2 taken 0% (throw)
725         -:   22:
726         1:   23:  counter.inc();
727 call    0 returned 100%
728 branch  1 taken 100% (fallthrough)
729 branch  2 taken 0% (throw)
730         1:   24:  counter.inc();
731 call    0 returned 100%
732 branch  1 taken 100% (fallthrough)
733 branch  2 taken 0% (throw)
734         1:   25:  total = 0;
735         -:   26:
736        11:   27:  for (i = 0; i < 10; i++)
737 branch  0 taken 91% (fallthrough)
738 branch  1 taken 9%
739        10:   28:    total += i;
740         -:   29:
741        1*:   30:  int v = total > 100 ? 1 : 2;
742 branch  0 taken 0% (fallthrough)
743 branch  1 taken 100%
744         -:   31:
745         1:   32:  if (total != 45)
746 branch  0 taken 0% (fallthrough)
747 branch  1 taken 100%
748     #####:   33:    printf ("Failure\n");
749 call    0 never executed
750 branch  1 never executed
751 branch  2 never executed
752         -:   34:  else
753         1:   35:    printf ("Success\n");
754 call    0 returned 100%
755 branch  1 taken 100% (fallthrough)
756 branch  2 taken 0% (throw)
757         1:   36:  return 0;
758         -:   37:@}
759 @end smallexample
761 For each function, a line is printed showing how many times the function
762 is called, how many times it returns and what percentage of the
763 function's blocks were executed.
765 For each basic block, a line is printed after the last line of the basic
766 block describing the branch or call that ends the basic block.  There can
767 be multiple branches and calls listed for a single source line if there
768 are multiple basic blocks that end on that line.  In this case, the
769 branches and calls are each given a number.  There is no simple way to map
770 these branches and calls back to source constructs.  In general, though,
771 the lowest numbered branch or call will correspond to the leftmost construct
772 on the source line.
774 For a branch, if it was executed at least once, then a percentage
775 indicating the number of times the branch was taken divided by the
776 number of times the branch was executed will be printed.  Otherwise, the
777 message ``never executed'' is printed.
779 For a call, if it was executed at least once, then a percentage
780 indicating the number of times the call returned divided by the number
781 of times the call was executed will be printed.  This will usually be
782 100%, but may be less for functions that call @code{exit} or @code{longjmp},
783 and thus may not return every time they are called.
785 The execution counts are cumulative.  If the example program were
786 executed again without removing the @file{.gcda} file, the count for the
787 number of times each line in the source was executed would be added to
788 the results of the previous run(s).  This is potentially useful in
789 several ways.  For example, it could be used to accumulate data over a
790 number of program runs as part of a test verification suite, or to
791 provide more accurate long-term information over a large number of
792 program runs.
794 The data in the @file{.gcda} files is saved immediately before the program
795 exits.  For each source file compiled with @option{-fprofile-arcs}, the
796 profiling code first attempts to read in an existing @file{.gcda} file; if
797 the file doesn't match the executable (differing number of basic block
798 counts) it will ignore the contents of the file.  It then adds in the
799 new execution counts and finally writes the data to the file.
801 @node Gcov and Optimization
802 @section Using @command{gcov} with GCC Optimization
804 If you plan to use @command{gcov} to help optimize your code, you must
805 first compile your program with two special GCC options:
806 @samp{-fprofile-arcs -ftest-coverage}.  Aside from that, you can use any
807 other GCC options; but if you want to prove that every single line
808 in your program was executed, you should not compile with optimization
809 at the same time.  On some machines the optimizer can eliminate some
810 simple code lines by combining them with other lines.  For example, code
811 like this:
813 @smallexample
814 if (a != b)
815   c = 1;
816 else
817   c = 0;
818 @end smallexample
820 @noindent
821 can be compiled into one instruction on some machines.  In this case,
822 there is no way for @command{gcov} to calculate separate execution counts
823 for each line because there isn't separate code for each line.  Hence
824 the @command{gcov} output looks like this if you compiled the program with
825 optimization:
827 @smallexample
828       100:   12:if (a != b)
829       100:   13:  c = 1;
830       100:   14:else
831       100:   15:  c = 0;
832 @end smallexample
834 The output shows that this block of code, combined by optimization,
835 executed 100 times.  In one sense this result is correct, because there
836 was only one instruction representing all four of these lines.  However,
837 the output does not indicate how many times the result was 0 and how
838 many times the result was 1.
840 Inlineable functions can create unexpected line counts.  Line counts are
841 shown for the source code of the inlineable function, but what is shown
842 depends on where the function is inlined, or if it is not inlined at all.
844 If the function is not inlined, the compiler must emit an out of line
845 copy of the function, in any object file that needs it.  If
846 @file{fileA.o} and @file{fileB.o} both contain out of line bodies of a
847 particular inlineable function, they will also both contain coverage
848 counts for that function.  When @file{fileA.o} and @file{fileB.o} are
849 linked together, the linker will, on many systems, select one of those
850 out of line bodies for all calls to that function, and remove or ignore
851 the other.  Unfortunately, it will not remove the coverage counters for
852 the unused function body.  Hence when instrumented, all but one use of
853 that function will show zero counts.
855 If the function is inlined in several places, the block structure in
856 each location might not be the same.  For instance, a condition might
857 now be calculable at compile time in some instances.  Because the
858 coverage of all the uses of the inline function will be shown for the
859 same source lines, the line counts themselves might seem inconsistent.
861 Long-running applications can use the @code{__gcov_reset} and @code{__gcov_dump}
862 facilities to restrict profile collection to the program region of
863 interest. Calling @code{__gcov_reset(void)} will clear all profile counters
864 to zero, and calling @code{__gcov_dump(void)} will cause the profile information
865 collected at that point to be dumped to @file{.gcda} output files.
866 Instrumented applications use a static destructor with priority 99
867 to invoke the @code{__gcov_dump} function. Thus @code{__gcov_dump}
868 is executed after all user defined static destructors,
869 as well as handlers registered with @code{atexit}.
870 If an executable loads a dynamic shared object via dlopen functionality,
871 @option{-Wl,--dynamic-list-data} is needed to dump all profile data.
873 Profiling run-time library reports various errors related to profile
874 manipulation and profile saving.  Errors are printed into standard error output
875 or @samp{GCOV_ERROR_FILE} file, if environment variable is used.
876 In order to terminate immediately after an errors occurs
877 set @samp{GCOV_EXIT_AT_ERROR} environment variable.
878 That can help users to find profile clashing which leads
879 to a misleading profile.
881 @c man end
883 @node Gcov Data Files
884 @section Brief Description of @command{gcov} Data Files
886 @command{gcov} uses two files for profiling.  The names of these files
887 are derived from the original @emph{object} file by substituting the
888 file suffix with either @file{.gcno}, or @file{.gcda}.  The files
889 contain coverage and profile data stored in a platform-independent format.
890 The @file{.gcno} files are placed in the same directory as the object
891 file.  By default, the @file{.gcda} files are also stored in the same
892 directory as the object file, but the GCC @option{-fprofile-dir} option
893 may be used to store the @file{.gcda} files in a separate directory.
895 The @file{.gcno} notes file is generated when the source file is compiled
896 with the GCC @option{-ftest-coverage} option.  It contains information to
897 reconstruct the basic block graphs and assign source line numbers to
898 blocks.
900 The @file{.gcda} count data file is generated when a program containing
901 object files built with the GCC @option{-fprofile-arcs} option is executed.
902 A separate @file{.gcda} file is created for each object file compiled with
903 this option.  It contains arc transition counts, value profile counts, and
904 some summary information.
906 It is not recommended to access the coverage files directly.
907 Consumers should use the intermediate format that is provided
908 by @command{gcov} tool via @option{--json-format} option.
910 @node Cross-profiling
911 @section Data File Relocation to Support Cross-Profiling
913 Running the program will cause profile output to be generated.  For each
914 source file compiled with @option{-fprofile-arcs}, an accompanying @file{.gcda}
915 file will be placed in the object file directory. That implicitly requires
916 running the program on the same system as it was built or having the same
917 absolute directory structure on the target system. The program will try
918 to create the needed directory structure, if it is not already present.
920 To support cross-profiling, a program compiled with @option{-fprofile-arcs}
921 can relocate the data files based on two environment variables:
923 @itemize @bullet
924 @item
925 GCOV_PREFIX contains the prefix to add to the absolute paths
926 in the object file. Prefix can be absolute, or relative.  The
927 default is no prefix.
929 @item
930 GCOV_PREFIX_STRIP indicates the how many initial directory names to strip off
931 the hardwired absolute paths. Default value is 0.
933 @emph{Note:} If GCOV_PREFIX_STRIP is set without GCOV_PREFIX is undefined,
934  then a relative path is made out of the hardwired absolute paths.
935 @end itemize
937 For example, if the object file @file{/user/build/foo.o} was built with
938 @option{-fprofile-arcs}, the final executable will try to create the data file
939 @file{/user/build/foo.gcda} when running on the target system.  This will
940 fail if the corresponding directory does not exist and it is unable to create
941 it.  This can be overcome by, for example, setting the environment as
942 @samp{GCOV_PREFIX=/target/run} and @samp{GCOV_PREFIX_STRIP=1}.  Such a
943 setting will name the data file @file{/target/run/build/foo.gcda}.
945 You must move the data files to the expected directory tree in order to
946 use them for profile directed optimizations (@option{-fprofile-use}), or to
947 use the @command{gcov} tool.