1 @c Copyright (C) 1996-2020 Free Software Foundation, Inc.
2 @c This is part of the GCC manual.
3 @c For copying conditions, see the file gcc.texi.
7 Copyright @copyright{} 1996-2020 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:
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.
27 @c Set file name and title for the man page.
29 @settitle coverage testing tool
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.
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.
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
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:
65 how often each line of code executes
68 what lines of code are actually executed
71 how much computing time each section of code uses
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
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
105 @command{gcov} works only on code compiled with GCC@. It is not
106 compatible with any other profiling or test coverage mechanism.
111 @section Invoking @command{gcov}
114 gcov @r{[}@var{options}@r{]} @var{files}
117 @command{gcov} accepts the following options:
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{-j}|@option{--json-format}]
128 [@option{-H}|@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}]
144 gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}.
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
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.
166 @itemx --branch-counts
167 Write branch frequencies as the number of branches taken, rather than
168 the percentage of branches taken.
171 @itemx --display-progress
172 Display the progress on the standard output.
175 @itemx --function-summaries
176 Output summaries for each function in addition to the file level summary.
180 Display help about using @command{gcov} (on the standard output), and
181 exit without doing any further processing.
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:
194 "current_working_directory": @var{current_working_directory},
195 "data_file": @var{data_file},
196 "format_version": @var{format_version},
197 "gcc_version": @var{gcc_version}
198 "files": [@var{file}]
202 Fields of the root element have following semantics:
206 @var{current_working_directory}: working directory where
207 a compilation unit was compiled
210 @var{data_file}: name of the data file (GCDA)
213 @var{format_version}: semantic version of the format
216 @var{gcc_version}: version of the GCC compiler
219 Each @var{file} has the following form:
223 "file": @var{file_name},
224 "functions": [@var{function}],
225 "lines": [@var{line}]
229 Fields of the @var{file} element have following semantics:
233 @var{file_name}: name of the source file
236 Each @var{function} has the following form:
240 "blocks": @var{blocks},
241 "blocks_executed": @var{blocks_executed},
242 "demangled_name": "@var{demangled_name},
243 "end_column": @var{end_column},
244 "end_line": @var{end_line},
245 "execution_count": @var{execution_count},
247 "start_column": @var{start_column}
248 "start_line": @var{start_line}
252 Fields of the @var{function} element have following semantics:
256 @var{blocks}: number of blocks that are in the function
259 @var{blocks_executed}: number of executed blocks of the function
262 @var{demangled_name}: demangled name of the function
265 @var{end_column}: column in the source file where the function ends
268 @var{end_line}: line in the source file where the function ends
271 @var{execution_count}: number of executions of the function
274 @var{name}: name of the function
277 @var{start_column}: column in the source file where the function begins
280 @var{start_line}: line in the source file where the function begins
283 Note that line numbers and column numbers number from 1. In the current
284 implementation, @var{start_line} and @var{start_column} do not include
285 any template parameters and the leading return type but that
286 this is likely to be fixed in the future.
288 Each @var{line} has the following form:
292 "branches": [@var{branch}],
293 "count": @var{count},
294 "line_number": @var{line_number},
295 "unexecuted_block": @var{unexecuted_block}
296 "function_name": @var{function_name},
300 Branches are present only with @var{-b} option.
301 Fields of the @var{line} element have following semantics:
305 @var{count}: number of executions of the line
308 @var{line_number}: line number
311 @var{unexecuted_block}: flag whether the line contains an unexecuted block
312 (not all statements on the line are executed)
315 @var{function_name}: a name of a function this @var{line} belongs to
316 (for a line with an inlined statements can be not set)
319 Each @var{branch} has the following form:
323 "count": @var{count},
324 "fallthrough": @var{fallthrough},
329 Fields of the @var{branch} element have following semantics:
333 @var{count}: number of executions of the branch
336 @var{fallthrough}: true when the branch is a fall through branch
339 @var{throw}: true when the branch is an exceptional branch
343 @itemx --human-readable
344 Write counts in human readable format (like 24.6k).
349 Use colors for lines of code that have zero coverage. We use red color for
350 non-exceptional lines and cyan for exceptional. Same colors are used for
351 basic blocks with @option{-a} option.
354 @itemx --long-file-names
355 Create long file names for included source files. For example, if the
356 header file @file{x.h} contains code, and was included in the file
357 @file{a.c}, then running @command{gcov} on the file @file{a.c} will
358 produce an output file called @file{a.c##x.h.gcov} instead of
359 @file{x.h.gcov}. This can be useful if @file{x.h} is included in
360 multiple source files and you want to see the individual
361 contributions. If you use the @samp{-p} option, both the including
362 and included file names will be complete path names.
365 @itemx --demangled-names
366 Display demangled function names in output. The default is to show
367 mangled function names.
371 Do not create the @command{gcov} output file.
373 @item -o @var{directory|file}
374 @itemx --object-directory @var{directory}
375 @itemx --object-file @var{file}
376 Specify either the directory containing the gcov data files, or the
377 object path name. The @file{.gcno}, and
378 @file{.gcda} data files are searched for using this option. If a directory
379 is specified, the data files are in that directory and named after the
380 input file name, without its extension. If a file is specified here,
381 the data files are named after that file, without its extension.
384 @itemx --preserve-paths
385 Preserve complete path information in the names of generated
386 @file{.gcov} files. Without this option, just the filename component is
387 used. With this option, all directories are used, with @samp{/} characters
388 translated to @samp{#} characters, @file{.} directory components
389 removed and unremoveable @file{..}
390 components renamed to @samp{^}. This is useful if sourcefiles are in several
391 different directories.
394 @itemx --use-hotness-colors
396 Emit perf-like colored output for hot lines. Legend of the color scale
397 is printed at the very beginning of the output file.
400 @itemx --relative-only
401 Only output information about source files with a relative pathname
402 (after source prefix elision). Absolute paths are usually system
403 header files and coverage of any inline functions therein is normally
406 @item -s @var{directory}
407 @itemx --source-prefix @var{directory}
408 A prefix for source file names to remove when generating the output
409 coverage files. This option is useful when building in a separate
410 directory, and the pathname to the source directory is not wanted when
411 determining the output file names. Note that this prefix detection is
412 applied before determining whether the source file is absolute.
416 Output to standard output instead of output files.
419 @itemx --unconditional-branches
420 When branch probabilities are given, include those of unconditional branches.
421 Unconditional branches are normally not interesting.
425 Display the @command{gcov} version number (on the standard output),
426 and exit without doing any further processing.
430 Print verbose informations related to basic blocks and arcs.
433 @itemx --hash-filenames
434 When using @var{--preserve-paths},
435 gcov uses the full pathname of the source files to create
436 an output filename. This can lead to long filenames that can overflow
437 filesystem limits. This option creates names of the form
438 @file{@var{source-file}##@var{md5}.gcov},
439 where the @var{source-file} component is the final filename part and
440 the @var{md5} component is calculated from the full mangled name that
441 would have been used otherwise. The option is an alternative
442 to the @var{--preserve-paths} on systems which have a filesystem limit.
446 @command{gcov} should be run with the current directory the same as that
447 when you invoked the compiler. Otherwise it will not be able to locate
448 the source files. @command{gcov} produces files called
449 @file{@var{mangledname}.gcov} in the current directory. These contain
450 the coverage information of the source file they correspond to.
451 One @file{.gcov} file is produced for each source (or header) file
453 which was compiled to produce the data files. The @var{mangledname} part
454 of the output file name is usually simply the source file name, but can
455 be something more complicated if the @samp{-l} or @samp{-p} options are
456 given. Refer to those options for details.
458 If you invoke @command{gcov} with multiple input files, the
459 contributions from each input file are summed. Typically you would
460 invoke it with the same list of files as the final link of your executable.
462 The @file{.gcov} files contain the @samp{:} separated fields along with
463 program source code. The format is
466 @var{execution_count}:@var{line_number}:@var{source line text}
469 Additional block information may succeed each line, when requested by
470 command line option. The @var{execution_count} is @samp{-} for lines
471 containing no code. Unexecuted lines are marked @samp{#####} or
472 @samp{=====}, depending on whether they are reachable by
473 non-exceptional paths or only exceptional paths such as C++ exception
474 handlers, respectively. Given the @samp{-a} option, unexecuted blocks are
475 marked @samp{$$$$$} or @samp{%%%%%}, depending on whether a basic block
476 is reachable via non-exceptional or exceptional paths.
477 Executed basic blocks having a statement with zero @var{execution_count}
478 end with @samp{*} character and are colored with magenta color with
479 the @option{-k} option. This functionality is not supported in Ada.
481 Note that GCC can completely remove the bodies of functions that are
482 not needed -- for instance if they are inlined everywhere. Such functions
483 are marked with @samp{-}, which can be confusing.
484 Use the @option{-fkeep-inline-functions} and @option{-fkeep-static-functions}
485 options to retain these functions and
486 allow gcov to properly show their @var{execution_count}.
488 Some lines of information at the start have @var{line_number} of zero.
489 These preamble lines are of the form
492 -:0:@var{tag}:@var{value}
495 The ordering and number of these preamble lines will be augmented as
496 @command{gcov} development progresses --- do not rely on them remaining
497 unchanged. Use @var{tag} to locate a particular preamble line.
499 The additional block information is of the form
502 @var{tag} @var{information}
505 The @var{information} is human readable, but designed to be simple
506 enough for machine parsing too.
508 When printing percentages, 0% and 100% are only printed when the values
509 are @emph{exactly} 0% and 100% respectively. Other values which would
510 conventionally be rounded to 0% or 100% are instead printed as the
511 nearest non-boundary value.
513 When using @command{gcov}, you must first compile your program
514 with a special GCC option @samp{--coverage}.
515 This tells the compiler to generate additional information needed by
516 gcov (basically a flow graph of the program) and also includes
517 additional code in the object files for generating the extra profiling
518 information needed by gcov. These additional files are placed in the
519 directory where the object file is located.
521 Running the program will cause profile output to be generated. For each
522 source file compiled with @option{-fprofile-arcs}, an accompanying
523 @file{.gcda} file will be placed in the object file directory.
525 Running @command{gcov} with your program's source file names as arguments
526 will now produce a listing of the code along with frequency of execution
527 for each line. For example, if your program is called @file{tmp.cpp}, this
528 is what you see when you use the basic @command{gcov} facility:
531 $ g++ --coverage tmp.cpp -c
532 $ g++ --coverage tmp.o
536 Lines executed:92.86% of 14
537 Creating 'tmp.cpp.gcov'
540 The file @file{tmp.cpp.gcov} contains output from @command{gcov}.
545 -: 0:Working directory:/home/gcc/testcase
550 -: 1:#include <stdio.h>
552 -: 3:template<class T>
556 1*: 7: Foo(): b (1000) @{@}
559 #####: 7: Foo(): b (1000) @{@}
562 1: 7: Foo(): b (1000) @{@}
564 2*: 8: void inc () @{ b++; @}
567 #####: 8: void inc () @{ b++; @}
570 2: 8: void inc () @{ b++; @}
577 -: 14:template class Foo<int>;
578 -: 15:template class Foo<char>;
584 1: 21: Foo<int> counter;
586 1: 23: counter.inc();
587 1: 24: counter.inc();
590 11: 27: for (i = 0; i < 10; i++)
593 1*: 30: int v = total > 100 ? 1 : 2;
595 1: 32: if (total != 45)
596 #####: 33: printf ("Failure\n");
598 1: 35: printf ("Success\n");
603 Note that line 7 is shown in the report multiple times. First occurrence
604 presents total number of execution of the line and the next two belong
605 to instances of class Foo constructors. As you can also see, line 30 contains
606 some unexecuted basic blocks and thus execution count has asterisk symbol.
608 When you use the @option{-a} option, you will get individual block
609 counts, and the output looks like this:
613 -: 0:Working directory:/home/gcc/testcase
618 -: 1:#include <stdio.h>
620 -: 3:template<class T>
624 1*: 7: Foo(): b (1000) @{@}
627 #####: 7: Foo(): b (1000) @{@}
630 1: 7: Foo(): b (1000) @{@}
632 2*: 8: void inc () @{ b++; @}
635 #####: 8: void inc () @{ b++; @}
638 2: 8: void inc () @{ b++; @}
645 -: 14:template class Foo<int>;
646 -: 15:template class Foo<char>;
652 1: 21: Foo<int> counter;
655 1: 23: counter.inc();
657 1: 24: counter.inc();
661 11: 27: for (i = 0; i < 10; i++)
667 1*: 30: int v = total > 100 ? 1 : 2;
672 1: 32: if (total != 45)
674 #####: 33: printf ("Failure\n");
677 1: 35: printf ("Success\n");
684 In this mode, each basic block is only shown on one line -- the last
685 line of the block. A multi-line block will only contribute to the
686 execution count of that last line, and other lines will not be shown
687 to contain code, unless previous blocks end on those lines.
688 The total execution count of a line is shown and subsequent lines show
689 the execution counts for individual blocks that end on that line. After each
690 block, the branch and call counts of the block will be shown, if the
691 @option{-b} option is given.
693 Because of the way GCC instruments calls, a call count can be shown
694 after a line with no individual blocks.
695 As you can see, line 33 contains a basic block that was not executed.
698 When you use the @option{-b} option, your output looks like this:
702 -: 0:Working directory:/home/gcc/testcase
707 -: 1:#include <stdio.h>
709 -: 3:template<class T>
713 1*: 7: Foo(): b (1000) @{@}
716 function Foo<char>::Foo() called 0 returned 0% blocks executed 0%
717 #####: 7: Foo(): b (1000) @{@}
720 function Foo<int>::Foo() called 1 returned 100% blocks executed 100%
721 1: 7: Foo(): b (1000) @{@}
723 2*: 8: void inc () @{ b++; @}
726 function Foo<char>::inc() called 0 returned 0% blocks executed 0%
727 #####: 8: void inc () @{ b++; @}
730 function Foo<int>::inc() called 2 returned 100% blocks executed 100%
731 2: 8: void inc () @{ b++; @}
738 -: 14:template class Foo<int>;
739 -: 15:template class Foo<char>;
742 function main called 1 returned 100% blocks executed 81%
746 1: 21: Foo<int> counter;
748 branch 1 taken 100% (fallthrough)
749 branch 2 taken 0% (throw)
751 1: 23: counter.inc();
753 branch 1 taken 100% (fallthrough)
754 branch 2 taken 0% (throw)
755 1: 24: counter.inc();
757 branch 1 taken 100% (fallthrough)
758 branch 2 taken 0% (throw)
761 11: 27: for (i = 0; i < 10; i++)
762 branch 0 taken 91% (fallthrough)
766 1*: 30: int v = total > 100 ? 1 : 2;
767 branch 0 taken 0% (fallthrough)
770 1: 32: if (total != 45)
771 branch 0 taken 0% (fallthrough)
773 #####: 33: printf ("Failure\n");
774 call 0 never executed
775 branch 1 never executed
776 branch 2 never executed
778 1: 35: printf ("Success\n");
780 branch 1 taken 100% (fallthrough)
781 branch 2 taken 0% (throw)
786 For each function, a line is printed showing how many times the function
787 is called, how many times it returns and what percentage of the
788 function's blocks were executed.
790 For each basic block, a line is printed after the last line of the basic
791 block describing the branch or call that ends the basic block. There can
792 be multiple branches and calls listed for a single source line if there
793 are multiple basic blocks that end on that line. In this case, the
794 branches and calls are each given a number. There is no simple way to map
795 these branches and calls back to source constructs. In general, though,
796 the lowest numbered branch or call will correspond to the leftmost construct
799 For a branch, if it was executed at least once, then a percentage
800 indicating the number of times the branch was taken divided by the
801 number of times the branch was executed will be printed. Otherwise, the
802 message ``never executed'' is printed.
804 For a call, if it was executed at least once, then a percentage
805 indicating the number of times the call returned divided by the number
806 of times the call was executed will be printed. This will usually be
807 100%, but may be less for functions that call @code{exit} or @code{longjmp},
808 and thus may not return every time they are called.
810 The execution counts are cumulative. If the example program were
811 executed again without removing the @file{.gcda} file, the count for the
812 number of times each line in the source was executed would be added to
813 the results of the previous run(s). This is potentially useful in
814 several ways. For example, it could be used to accumulate data over a
815 number of program runs as part of a test verification suite, or to
816 provide more accurate long-term information over a large number of
819 The data in the @file{.gcda} files is saved immediately before the program
820 exits. For each source file compiled with @option{-fprofile-arcs}, the
821 profiling code first attempts to read in an existing @file{.gcda} file; if
822 the file doesn't match the executable (differing number of basic block
823 counts) it will ignore the contents of the file. It then adds in the
824 new execution counts and finally writes the data to the file.
826 @node Gcov and Optimization
827 @section Using @command{gcov} with GCC Optimization
829 If you plan to use @command{gcov} to help optimize your code, you must
830 first compile your program with a special GCC option
831 @samp{--coverage}. Aside from that, you can use any
832 other GCC options; but if you want to prove that every single line
833 in your program was executed, you should not compile with optimization
834 at the same time. On some machines the optimizer can eliminate some
835 simple code lines by combining them with other lines. For example, code
846 can be compiled into one instruction on some machines. In this case,
847 there is no way for @command{gcov} to calculate separate execution counts
848 for each line because there isn't separate code for each line. Hence
849 the @command{gcov} output looks like this if you compiled the program with
859 The output shows that this block of code, combined by optimization,
860 executed 100 times. In one sense this result is correct, because there
861 was only one instruction representing all four of these lines. However,
862 the output does not indicate how many times the result was 0 and how
863 many times the result was 1.
865 Inlineable functions can create unexpected line counts. Line counts are
866 shown for the source code of the inlineable function, but what is shown
867 depends on where the function is inlined, or if it is not inlined at all.
869 If the function is not inlined, the compiler must emit an out of line
870 copy of the function, in any object file that needs it. If
871 @file{fileA.o} and @file{fileB.o} both contain out of line bodies of a
872 particular inlineable function, they will also both contain coverage
873 counts for that function. When @file{fileA.o} and @file{fileB.o} are
874 linked together, the linker will, on many systems, select one of those
875 out of line bodies for all calls to that function, and remove or ignore
876 the other. Unfortunately, it will not remove the coverage counters for
877 the unused function body. Hence when instrumented, all but one use of
878 that function will show zero counts.
880 If the function is inlined in several places, the block structure in
881 each location might not be the same. For instance, a condition might
882 now be calculable at compile time in some instances. Because the
883 coverage of all the uses of the inline function will be shown for the
884 same source lines, the line counts themselves might seem inconsistent.
886 Long-running applications can use the @code{__gcov_reset} and @code{__gcov_dump}
887 facilities to restrict profile collection to the program region of
888 interest. Calling @code{__gcov_reset(void)} will clear all profile counters
889 to zero, and calling @code{__gcov_dump(void)} will cause the profile information
890 collected at that point to be dumped to @file{.gcda} output files.
891 Instrumented applications use a static destructor with priority 99
892 to invoke the @code{__gcov_dump} function. Thus @code{__gcov_dump}
893 is executed after all user defined static destructors,
894 as well as handlers registered with @code{atexit}.
895 If an executable loads a dynamic shared object via dlopen functionality,
896 @option{-Wl,--dynamic-list-data} is needed to dump all profile data.
898 Profiling run-time library reports various errors related to profile
899 manipulation and profile saving. Errors are printed into standard error output
900 or @samp{GCOV_ERROR_FILE} file, if environment variable is used.
901 In order to terminate immediately after an errors occurs
902 set @samp{GCOV_EXIT_AT_ERROR} environment variable.
903 That can help users to find profile clashing which leads
904 to a misleading profile.
908 @node Gcov Data Files
909 @section Brief Description of @command{gcov} Data Files
911 @command{gcov} uses two files for profiling. The names of these files
912 are derived from the original @emph{object} file by substituting the
913 file suffix with either @file{.gcno}, or @file{.gcda}. The files
914 contain coverage and profile data stored in a platform-independent format.
915 The @file{.gcno} files are placed in the same directory as the object
916 file. By default, the @file{.gcda} files are also stored in the same
917 directory as the object file, but the GCC @option{-fprofile-dir} option
918 may be used to store the @file{.gcda} files in a separate directory.
920 The @file{.gcno} notes file is generated when the source file is compiled
921 with the GCC @option{-ftest-coverage} option. It contains information to
922 reconstruct the basic block graphs and assign source line numbers to
925 The @file{.gcda} count data file is generated when a program containing
926 object files built with the GCC @option{-fprofile-arcs} option is executed.
927 A separate @file{.gcda} file is created for each object file compiled with
928 this option. It contains arc transition counts, value profile counts, and
929 some summary information.
931 It is not recommended to access the coverage files directly.
932 Consumers should use the intermediate format that is provided
933 by @command{gcov} tool via @option{--json-format} option.
935 @node Cross-profiling
936 @section Data File Relocation to Support Cross-Profiling
938 Running the program will cause profile output to be generated. For each
939 source file compiled with @option{-fprofile-arcs}, an accompanying @file{.gcda}
940 file will be placed in the object file directory. That implicitly requires
941 running the program on the same system as it was built or having the same
942 absolute directory structure on the target system. The program will try
943 to create the needed directory structure, if it is not already present.
945 To support cross-profiling, a program compiled with @option{-fprofile-arcs}
946 can relocate the data files based on two environment variables:
950 GCOV_PREFIX contains the prefix to add to the absolute paths
951 in the object file. Prefix can be absolute, or relative. The
952 default is no prefix.
955 GCOV_PREFIX_STRIP indicates the how many initial directory names to strip off
956 the hardwired absolute paths. Default value is 0.
958 @emph{Note:} If GCOV_PREFIX_STRIP is set without GCOV_PREFIX is undefined,
959 then a relative path is made out of the hardwired absolute paths.
962 For example, if the object file @file{/user/build/foo.o} was built with
963 @option{-fprofile-arcs}, the final executable will try to create the data file
964 @file{/user/build/foo.gcda} when running on the target system. This will
965 fail if the corresponding directory does not exist and it is unable to create
966 it. This can be overcome by, for example, setting the environment as
967 @samp{GCOV_PREFIX=/target/run} and @samp{GCOV_PREFIX_STRIP=1}. Such a
968 setting will name the data file @file{/target/run/build/foo.gcda}.
970 You must move the data files to the expected directory tree in order to
971 use them for profile directed optimizations (@option{-fprofile-use}), or to
972 use the @command{gcov} tool.