xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/doc/gcov.texi (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
1*8feb0f0bSmrg@c Copyright (C) 1996-2020 Free Software Foundation, Inc.
21debfc3dSmrg@c This is part of the GCC manual.
31debfc3dSmrg@c For copying conditions, see the file gcc.texi.
41debfc3dSmrg
51debfc3dSmrg@ignore
61debfc3dSmrg@c man begin COPYRIGHT
7*8feb0f0bSmrgCopyright @copyright{} 1996-2020 Free Software Foundation, Inc.
81debfc3dSmrg
91debfc3dSmrgPermission is granted to copy, distribute and/or modify this document
101debfc3dSmrgunder the terms of the GNU Free Documentation License, Version 1.3 or
111debfc3dSmrgany later version published by the Free Software Foundation; with the
121debfc3dSmrgInvariant Sections being ``GNU General Public License'' and ``Funding
131debfc3dSmrgFree Software'', the Front-Cover texts being (a) (see below), and with
141debfc3dSmrgthe Back-Cover Texts being (b) (see below).  A copy of the license is
151debfc3dSmrgincluded in the gfdl(7) man page.
161debfc3dSmrg
171debfc3dSmrg(a) The FSF's Front-Cover Text is:
181debfc3dSmrg
191debfc3dSmrg     A GNU Manual
201debfc3dSmrg
211debfc3dSmrg(b) The FSF's Back-Cover Text is:
221debfc3dSmrg
231debfc3dSmrg     You have freedom to copy and modify this GNU Manual, like GNU
241debfc3dSmrg     software.  Copies published by the Free Software Foundation raise
251debfc3dSmrg     funds for GNU development.
261debfc3dSmrg@c man end
271debfc3dSmrg@c Set file name and title for the man page.
281debfc3dSmrg@setfilename gcov
291debfc3dSmrg@settitle coverage testing tool
301debfc3dSmrg@end ignore
311debfc3dSmrg
321debfc3dSmrg@node Gcov
331debfc3dSmrg@chapter @command{gcov}---a Test Coverage Program
341debfc3dSmrg
351debfc3dSmrg@command{gcov} is a tool you can use in conjunction with GCC to
361debfc3dSmrgtest code coverage in your programs.
371debfc3dSmrg
381debfc3dSmrg@menu
391debfc3dSmrg* Gcov Intro::                  Introduction to gcov.
401debfc3dSmrg* Invoking Gcov::               How to use gcov.
411debfc3dSmrg* Gcov and Optimization::       Using gcov with GCC optimization.
421debfc3dSmrg* Gcov Data Files::             The files used by gcov.
431debfc3dSmrg* Cross-profiling::             Data file relocation.
441debfc3dSmrg@end menu
451debfc3dSmrg
461debfc3dSmrg@node Gcov Intro
471debfc3dSmrg@section Introduction to @command{gcov}
481debfc3dSmrg@c man begin DESCRIPTION
491debfc3dSmrg
501debfc3dSmrg@command{gcov} is a test coverage program.  Use it in concert with GCC
511debfc3dSmrgto analyze your programs to help create more efficient, faster running
521debfc3dSmrgcode and to discover untested parts of your program.  You can use
531debfc3dSmrg@command{gcov} as a profiling tool to help discover where your
541debfc3dSmrgoptimization efforts will best affect your code.  You can also use
551debfc3dSmrg@command{gcov} along with the other profiling tool, @command{gprof}, to
561debfc3dSmrgassess which parts of your code use the greatest amount of computing
571debfc3dSmrgtime.
581debfc3dSmrg
591debfc3dSmrgProfiling tools help you analyze your code's performance.  Using a
601debfc3dSmrgprofiler such as @command{gcov} or @command{gprof}, you can find out some
611debfc3dSmrgbasic performance statistics, such as:
621debfc3dSmrg
631debfc3dSmrg@itemize @bullet
641debfc3dSmrg@item
651debfc3dSmrghow often each line of code executes
661debfc3dSmrg
671debfc3dSmrg@item
681debfc3dSmrgwhat lines of code are actually executed
691debfc3dSmrg
701debfc3dSmrg@item
711debfc3dSmrghow much computing time each section of code uses
721debfc3dSmrg@end itemize
731debfc3dSmrg
741debfc3dSmrgOnce you know these things about how your code works when compiled, you
751debfc3dSmrgcan look at each module to see which modules should be optimized.
761debfc3dSmrg@command{gcov} helps you determine where to work on optimization.
771debfc3dSmrg
781debfc3dSmrgSoftware developers also use coverage testing in concert with
791debfc3dSmrgtestsuites, to make sure software is actually good enough for a release.
801debfc3dSmrgTestsuites can verify that a program works as expected; a coverage
811debfc3dSmrgprogram tests to see how much of the program is exercised by the
821debfc3dSmrgtestsuite.  Developers can then determine what kinds of test cases need
831debfc3dSmrgto be added to the testsuites to create both better testing and a better
841debfc3dSmrgfinal product.
851debfc3dSmrg
861debfc3dSmrgYou should compile your code without optimization if you plan to use
871debfc3dSmrg@command{gcov} because the optimization, by combining some lines of code
881debfc3dSmrginto one function, may not give you as much information as you need to
891debfc3dSmrglook for `hot spots' where the code is using a great deal of computer
901debfc3dSmrgtime.  Likewise, because @command{gcov} accumulates statistics by line (at
911debfc3dSmrgthe lowest resolution), it works best with a programming style that
921debfc3dSmrgplaces only one statement on each line.  If you use complicated macros
931debfc3dSmrgthat expand to loops or to other control structures, the statistics are
941debfc3dSmrgless helpful---they only report on the line where the macro call
951debfc3dSmrgappears.  If your complex macros behave like functions, you can replace
961debfc3dSmrgthem with inline functions to solve this problem.
971debfc3dSmrg
981debfc3dSmrg@command{gcov} creates a logfile called @file{@var{sourcefile}.gcov} which
991debfc3dSmrgindicates how many times each line of a source file @file{@var{sourcefile}.c}
1001debfc3dSmrghas executed.  You can use these logfiles along with @command{gprof} to aid
1011debfc3dSmrgin fine-tuning the performance of your programs.  @command{gprof} gives
1021debfc3dSmrgtiming information you can use along with the information you get from
1031debfc3dSmrg@command{gcov}.
1041debfc3dSmrg
1051debfc3dSmrg@command{gcov} works only on code compiled with GCC@.  It is not
1061debfc3dSmrgcompatible with any other profiling or test coverage mechanism.
1071debfc3dSmrg
1081debfc3dSmrg@c man end
1091debfc3dSmrg
1101debfc3dSmrg@node Invoking Gcov
1111debfc3dSmrg@section Invoking @command{gcov}
1121debfc3dSmrg
1131debfc3dSmrg@smallexample
1141debfc3dSmrggcov @r{[}@var{options}@r{]} @var{files}
1151debfc3dSmrg@end smallexample
1161debfc3dSmrg
1171debfc3dSmrg@command{gcov} accepts the following options:
1181debfc3dSmrg
1191debfc3dSmrg@ignore
1201debfc3dSmrg@c man begin SYNOPSIS
1211debfc3dSmrggcov [@option{-v}|@option{--version}] [@option{-h}|@option{--help}]
1221debfc3dSmrg     [@option{-a}|@option{--all-blocks}]
1231debfc3dSmrg     [@option{-b}|@option{--branch-probabilities}]
1241debfc3dSmrg     [@option{-c}|@option{--branch-counts}]
1251debfc3dSmrg     [@option{-d}|@option{--display-progress}]
1261debfc3dSmrg     [@option{-f}|@option{--function-summaries}]
127c0a68be4Smrg     [@option{-i}|@option{--json-format}]
128a2dc1f3fSmrg     [@option{-j}|@option{--human-readable}]
129a2dc1f3fSmrg     [@option{-k}|@option{--use-colors}]
1301debfc3dSmrg     [@option{-l}|@option{--long-file-names}]
1311debfc3dSmrg     [@option{-m}|@option{--demangled-names}]
1321debfc3dSmrg     [@option{-n}|@option{--no-output}]
1331debfc3dSmrg     [@option{-o}|@option{--object-directory} @var{directory|file}]
1341debfc3dSmrg     [@option{-p}|@option{--preserve-paths}]
135c0a68be4Smrg     [@option{-q}|@option{--use-hotness-colors}]
1361debfc3dSmrg     [@option{-r}|@option{--relative-only}]
1371debfc3dSmrg     [@option{-s}|@option{--source-prefix} @var{directory}]
138c0a68be4Smrg     [@option{-t}|@option{--stdout}]
1391debfc3dSmrg     [@option{-u}|@option{--unconditional-branches}]
1401debfc3dSmrg     [@option{-x}|@option{--hash-filenames}]
1411debfc3dSmrg     @var{files}
1421debfc3dSmrg@c man end
1431debfc3dSmrg@c man begin SEEALSO
1441debfc3dSmrggpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}.
1451debfc3dSmrg@c man end
1461debfc3dSmrg@end ignore
1471debfc3dSmrg
1481debfc3dSmrg@c man begin OPTIONS
1491debfc3dSmrg@table @gcctabopt
1501debfc3dSmrg
1511debfc3dSmrg@item -a
1521debfc3dSmrg@itemx --all-blocks
1531debfc3dSmrgWrite individual execution counts for every basic block.  Normally gcov
1541debfc3dSmrgoutputs execution counts only for the main blocks of a line.  With this
1551debfc3dSmrgoption you can determine if blocks within a single line are not being
1561debfc3dSmrgexecuted.
1571debfc3dSmrg
1581debfc3dSmrg@item -b
1591debfc3dSmrg@itemx --branch-probabilities
1601debfc3dSmrgWrite branch frequencies to the output file, and write branch summary
1611debfc3dSmrginfo to the standard output.  This option allows you to see how often
1621debfc3dSmrgeach branch in your program was taken.  Unconditional branches will not
1631debfc3dSmrgbe shown, unless the @option{-u} option is given.
1641debfc3dSmrg
1651debfc3dSmrg@item -c
1661debfc3dSmrg@itemx --branch-counts
1671debfc3dSmrgWrite branch frequencies as the number of branches taken, rather than
1681debfc3dSmrgthe percentage of branches taken.
1691debfc3dSmrg
1701debfc3dSmrg@item -d
1711debfc3dSmrg@itemx --display-progress
1721debfc3dSmrgDisplay the progress on the standard output.
1731debfc3dSmrg
1741debfc3dSmrg@item -f
1751debfc3dSmrg@itemx --function-summaries
1761debfc3dSmrgOutput summaries for each function in addition to the file level summary.
1771debfc3dSmrg
1781debfc3dSmrg@item -h
1791debfc3dSmrg@itemx --help
1801debfc3dSmrgDisplay help about using @command{gcov} (on the standard output), and
1811debfc3dSmrgexit without doing any further processing.
1821debfc3dSmrg
1831debfc3dSmrg@item -i
184c0a68be4Smrg@itemx --json-format
185c0a68be4SmrgOutput gcov file in an easy-to-parse JSON intermediate format
186c0a68be4Smrgwhich does not require source code for generation.  The JSON
187c0a68be4Smrgfile is compressed with gzip compression algorithm
188c0a68be4Smrgand the files have @file{.gcov.json.gz} extension.
1891debfc3dSmrg
190c0a68be4SmrgStructure of the JSON is following:
1911debfc3dSmrg
1921debfc3dSmrg@smallexample
193c0a68be4Smrg@{
194c0a68be4Smrg  "current_working_directory": @var{current_working_directory},
195c0a68be4Smrg  "data_file": @var{data_file},
196c0a68be4Smrg  "format_version": @var{format_version},
197c0a68be4Smrg  "gcc_version": @var{gcc_version}
198c0a68be4Smrg  "files": [@var{file}]
199c0a68be4Smrg@}
200a2dc1f3fSmrg@end smallexample
2011debfc3dSmrg
202c0a68be4SmrgFields of the root element have following semantics:
2031debfc3dSmrg
204c0a68be4Smrg@itemize @bullet
205c0a68be4Smrg@item
206c0a68be4Smrg@var{current_working_directory}: working directory where
207c0a68be4Smrga compilation unit was compiled
208c0a68be4Smrg
209c0a68be4Smrg@item
210c0a68be4Smrg@var{data_file}: name of the data file (GCDA)
211c0a68be4Smrg
212c0a68be4Smrg@item
213c0a68be4Smrg@var{format_version}: semantic version of the format
214c0a68be4Smrg
215c0a68be4Smrg@item
216c0a68be4Smrg@var{gcc_version}: version of the GCC compiler
217c0a68be4Smrg@end itemize
218c0a68be4Smrg
219c0a68be4SmrgEach @var{file} has the following form:
2201debfc3dSmrg
2211debfc3dSmrg@smallexample
222c0a68be4Smrg@{
223c0a68be4Smrg  "file": @var{file_name},
224c0a68be4Smrg  "functions": [@var{function}],
225c0a68be4Smrg  "lines": [@var{line}]
226c0a68be4Smrg@}
2271debfc3dSmrg@end smallexample
2281debfc3dSmrg
229c0a68be4SmrgFields of the @var{file} element have following semantics:
230c0a68be4Smrg
231c0a68be4Smrg@itemize @bullet
232c0a68be4Smrg@item
233c0a68be4Smrg@var{file_name}: name of the source file
234c0a68be4Smrg@end itemize
235c0a68be4Smrg
236c0a68be4SmrgEach @var{function} has the following form:
237c0a68be4Smrg
238c0a68be4Smrg@smallexample
239c0a68be4Smrg@{
240c0a68be4Smrg  "blocks": @var{blocks},
241c0a68be4Smrg  "blocks_executed": @var{blocks_executed},
242c0a68be4Smrg  "demangled_name": "@var{demangled_name},
243c0a68be4Smrg  "end_column": @var{end_column},
244c0a68be4Smrg  "end_line": @var{end_line},
245c0a68be4Smrg  "execution_count": @var{execution_count},
246c0a68be4Smrg  "name": @var{name},
247c0a68be4Smrg  "start_column": @var{start_column}
248c0a68be4Smrg  "start_line": @var{start_line}
249c0a68be4Smrg@}
250c0a68be4Smrg@end smallexample
251c0a68be4Smrg
252c0a68be4SmrgFields of the @var{function} element have following semantics:
253c0a68be4Smrg
254c0a68be4Smrg@itemize @bullet
255c0a68be4Smrg@item
256c0a68be4Smrg@var{blocks}: number of blocks that are in the function
257c0a68be4Smrg
258c0a68be4Smrg@item
259c0a68be4Smrg@var{blocks_executed}: number of executed blocks of the function
260c0a68be4Smrg
261c0a68be4Smrg@item
262c0a68be4Smrg@var{demangled_name}: demangled name of the function
263c0a68be4Smrg
264c0a68be4Smrg@item
265c0a68be4Smrg@var{end_column}: column in the source file where the function ends
266c0a68be4Smrg
267c0a68be4Smrg@item
268c0a68be4Smrg@var{end_line}: line in the source file where the function ends
269c0a68be4Smrg
270c0a68be4Smrg@item
271c0a68be4Smrg@var{execution_count}: number of executions of the function
272c0a68be4Smrg
273c0a68be4Smrg@item
274c0a68be4Smrg@var{name}: name of the function
275c0a68be4Smrg
276c0a68be4Smrg@item
277c0a68be4Smrg@var{start_column}: column in the source file where the function begins
278c0a68be4Smrg
279c0a68be4Smrg@item
280c0a68be4Smrg@var{start_line}: line in the source file where the function begins
281c0a68be4Smrg@end itemize
282c0a68be4Smrg
283c0a68be4SmrgNote that line numbers and column numbers number from 1.  In the current
284c0a68be4Smrgimplementation, @var{start_line} and @var{start_column} do not include
285c0a68be4Smrgany template parameters and the leading return type but that
286c0a68be4Smrgthis is likely to be fixed in the future.
287c0a68be4Smrg
288c0a68be4SmrgEach @var{line} has the following form:
289c0a68be4Smrg
290c0a68be4Smrg@smallexample
291c0a68be4Smrg@{
292c0a68be4Smrg  "branches": [@var{branch}],
293c0a68be4Smrg  "count": @var{count},
294c0a68be4Smrg  "line_number": @var{line_number},
295c0a68be4Smrg  "unexecuted_block": @var{unexecuted_block}
296c0a68be4Smrg  "function_name": @var{function_name},
297c0a68be4Smrg@}
298c0a68be4Smrg@end smallexample
299c0a68be4Smrg
300c0a68be4SmrgBranches are present only with @var{-b} option.
301c0a68be4SmrgFields of the @var{line} element have following semantics:
302c0a68be4Smrg
303c0a68be4Smrg@itemize @bullet
304c0a68be4Smrg@item
305c0a68be4Smrg@var{count}: number of executions of the line
306c0a68be4Smrg
307c0a68be4Smrg@item
308c0a68be4Smrg@var{line_number}: line number
309c0a68be4Smrg
310c0a68be4Smrg@item
311c0a68be4Smrg@var{unexecuted_block}: flag whether the line contains an unexecuted block
312c0a68be4Smrg(not all statements on the line are executed)
313c0a68be4Smrg
314c0a68be4Smrg@item
315c0a68be4Smrg@var{function_name}: a name of a function this @var{line} belongs to
316c0a68be4Smrg(for a line with an inlined statements can be not set)
317c0a68be4Smrg@end itemize
318c0a68be4Smrg
319c0a68be4SmrgEach @var{branch} has the following form:
320c0a68be4Smrg
321c0a68be4Smrg@smallexample
322c0a68be4Smrg@{
323c0a68be4Smrg  "count": @var{count},
324c0a68be4Smrg  "fallthrough": @var{fallthrough},
325c0a68be4Smrg  "throw": @var{throw}
326c0a68be4Smrg@}
327c0a68be4Smrg@end smallexample
328c0a68be4Smrg
329c0a68be4SmrgFields of the @var{branch} element have following semantics:
330c0a68be4Smrg
331c0a68be4Smrg@itemize @bullet
332c0a68be4Smrg@item
333c0a68be4Smrg@var{count}: number of executions of the branch
334c0a68be4Smrg
335c0a68be4Smrg@item
336c0a68be4Smrg@var{fallthrough}: true when the branch is a fall through branch
337c0a68be4Smrg
338c0a68be4Smrg@item
339c0a68be4Smrg@var{throw}: true when the branch is an exceptional branch
340c0a68be4Smrg@end itemize
341c0a68be4Smrg
342a2dc1f3fSmrg@item -j
343a2dc1f3fSmrg@itemx --human-readable
344c0a68be4SmrgWrite counts in human readable format (like 24.6k).
345a2dc1f3fSmrg
346a2dc1f3fSmrg@item -k
347a2dc1f3fSmrg@itemx --use-colors
348a2dc1f3fSmrg
349a2dc1f3fSmrgUse colors for lines of code that have zero coverage.  We use red color for
350a2dc1f3fSmrgnon-exceptional lines and cyan for exceptional.  Same colors are used for
351a2dc1f3fSmrgbasic blocks with @option{-a} option.
352a2dc1f3fSmrg
3531debfc3dSmrg@item -l
3541debfc3dSmrg@itemx --long-file-names
3551debfc3dSmrgCreate long file names for included source files.  For example, if the
3561debfc3dSmrgheader file @file{x.h} contains code, and was included in the file
3571debfc3dSmrg@file{a.c}, then running @command{gcov} on the file @file{a.c} will
3581debfc3dSmrgproduce an output file called @file{a.c##x.h.gcov} instead of
3591debfc3dSmrg@file{x.h.gcov}.  This can be useful if @file{x.h} is included in
3601debfc3dSmrgmultiple source files and you want to see the individual
3611debfc3dSmrgcontributions.  If you use the @samp{-p} option, both the including
3621debfc3dSmrgand included file names will be complete path names.
3631debfc3dSmrg
3641debfc3dSmrg@item -m
3651debfc3dSmrg@itemx --demangled-names
3661debfc3dSmrgDisplay demangled function names in output. The default is to show
3671debfc3dSmrgmangled function names.
3681debfc3dSmrg
3691debfc3dSmrg@item -n
3701debfc3dSmrg@itemx --no-output
3711debfc3dSmrgDo not create the @command{gcov} output file.
3721debfc3dSmrg
3731debfc3dSmrg@item -o @var{directory|file}
3741debfc3dSmrg@itemx --object-directory @var{directory}
3751debfc3dSmrg@itemx --object-file @var{file}
3761debfc3dSmrgSpecify either the directory containing the gcov data files, or the
3771debfc3dSmrgobject path name.  The @file{.gcno}, and
3781debfc3dSmrg@file{.gcda} data files are searched for using this option.  If a directory
3791debfc3dSmrgis specified, the data files are in that directory and named after the
3801debfc3dSmrginput file name, without its extension.  If a file is specified here,
3811debfc3dSmrgthe data files are named after that file, without its extension.
3821debfc3dSmrg
3831debfc3dSmrg@item -p
3841debfc3dSmrg@itemx --preserve-paths
3851debfc3dSmrgPreserve complete path information in the names of generated
3861debfc3dSmrg@file{.gcov} files.  Without this option, just the filename component is
3871debfc3dSmrgused.  With this option, all directories are used, with @samp{/} characters
3881debfc3dSmrgtranslated to @samp{#} characters, @file{.} directory components
3891debfc3dSmrgremoved and unremoveable @file{..}
3901debfc3dSmrgcomponents renamed to @samp{^}.  This is useful if sourcefiles are in several
3911debfc3dSmrgdifferent directories.
3921debfc3dSmrg
393c0a68be4Smrg@item -q
394c0a68be4Smrg@itemx --use-hotness-colors
395c0a68be4Smrg
396c0a68be4SmrgEmit perf-like colored output for hot lines.  Legend of the color scale
397c0a68be4Smrgis printed at the very beginning of the output file.
398c0a68be4Smrg
3991debfc3dSmrg@item -r
4001debfc3dSmrg@itemx --relative-only
4011debfc3dSmrgOnly output information about source files with a relative pathname
4021debfc3dSmrg(after source prefix elision).  Absolute paths are usually system
4031debfc3dSmrgheader files and coverage of any inline functions therein is normally
4041debfc3dSmrguninteresting.
4051debfc3dSmrg
4061debfc3dSmrg@item -s @var{directory}
4071debfc3dSmrg@itemx --source-prefix @var{directory}
4081debfc3dSmrgA prefix for source file names to remove when generating the output
4091debfc3dSmrgcoverage files.  This option is useful when building in a separate
4101debfc3dSmrgdirectory, and the pathname to the source directory is not wanted when
4111debfc3dSmrgdetermining the output file names.  Note that this prefix detection is
4121debfc3dSmrgapplied before determining whether the source file is absolute.
4131debfc3dSmrg
414c0a68be4Smrg@item -t
415c0a68be4Smrg@itemx --stdout
416c0a68be4SmrgOutput to standard output instead of output files.
417c0a68be4Smrg
4181debfc3dSmrg@item -u
4191debfc3dSmrg@itemx --unconditional-branches
4201debfc3dSmrgWhen branch probabilities are given, include those of unconditional branches.
4211debfc3dSmrgUnconditional branches are normally not interesting.
4221debfc3dSmrg
4231debfc3dSmrg@item -v
4241debfc3dSmrg@itemx --version
4251debfc3dSmrgDisplay the @command{gcov} version number (on the standard output),
4261debfc3dSmrgand exit without doing any further processing.
4271debfc3dSmrg
4281debfc3dSmrg@item -w
4291debfc3dSmrg@itemx --verbose
4301debfc3dSmrgPrint verbose informations related to basic blocks and arcs.
4311debfc3dSmrg
4321debfc3dSmrg@item -x
4331debfc3dSmrg@itemx --hash-filenames
434c0a68be4SmrgWhen using @var{--preserve-paths},
435c0a68be4Smrggcov uses the full pathname of the source files to create
4361debfc3dSmrgan output filename.  This can lead to long filenames that can overflow
4371debfc3dSmrgfilesystem limits.  This option creates names of the form
4381debfc3dSmrg@file{@var{source-file}##@var{md5}.gcov},
4391debfc3dSmrgwhere the @var{source-file} component is the final filename part and
4401debfc3dSmrgthe @var{md5} component is calculated from the full mangled name that
441c0a68be4Smrgwould have been used otherwise.  The option is an alternative
442c0a68be4Smrgto the @var{--preserve-paths} on systems which have a filesystem limit.
4431debfc3dSmrg
4441debfc3dSmrg@end table
4451debfc3dSmrg
4461debfc3dSmrg@command{gcov} should be run with the current directory the same as that
4471debfc3dSmrgwhen you invoked the compiler.  Otherwise it will not be able to locate
4481debfc3dSmrgthe source files.  @command{gcov} produces files called
4491debfc3dSmrg@file{@var{mangledname}.gcov} in the current directory.  These contain
4501debfc3dSmrgthe coverage information of the source file they correspond to.
4511debfc3dSmrgOne @file{.gcov} file is produced for each source (or header) file
4521debfc3dSmrgcontaining code,
4531debfc3dSmrgwhich was compiled to produce the data files.  The @var{mangledname} part
4541debfc3dSmrgof the output file name is usually simply the source file name, but can
4551debfc3dSmrgbe something more complicated if the @samp{-l} or @samp{-p} options are
4561debfc3dSmrggiven.  Refer to those options for details.
4571debfc3dSmrg
4581debfc3dSmrgIf you invoke @command{gcov} with multiple input files, the
4591debfc3dSmrgcontributions from each input file are summed.  Typically you would
4601debfc3dSmrginvoke it with the same list of files as the final link of your executable.
4611debfc3dSmrg
4621debfc3dSmrgThe @file{.gcov} files contain the @samp{:} separated fields along with
4631debfc3dSmrgprogram source code.  The format is
4641debfc3dSmrg
4651debfc3dSmrg@smallexample
4661debfc3dSmrg@var{execution_count}:@var{line_number}:@var{source line text}
4671debfc3dSmrg@end smallexample
4681debfc3dSmrg
4691debfc3dSmrgAdditional block information may succeed each line, when requested by
4701debfc3dSmrgcommand line option.  The @var{execution_count} is @samp{-} for lines
4711debfc3dSmrgcontaining no code.  Unexecuted lines are marked @samp{#####} or
4721debfc3dSmrg@samp{=====}, depending on whether they are reachable by
4731debfc3dSmrgnon-exceptional paths or only exceptional paths such as C++ exception
474c0a68be4Smrghandlers, respectively. Given the @samp{-a} option, unexecuted blocks are
4751debfc3dSmrgmarked @samp{$$$$$} or @samp{%%%%%}, depending on whether a basic block
4761debfc3dSmrgis reachable via non-exceptional or exceptional paths.
477a2dc1f3fSmrgExecuted basic blocks having a statement with zero @var{execution_count}
478c0a68be4Smrgend with @samp{*} character and are colored with magenta color with
479c0a68be4Smrgthe @option{-k} option.  This functionality is not supported in Ada.
4801debfc3dSmrg
4811debfc3dSmrgNote that GCC can completely remove the bodies of functions that are
4821debfc3dSmrgnot needed -- for instance if they are inlined everywhere.  Such functions
4831debfc3dSmrgare marked with @samp{-}, which can be confusing.
4841debfc3dSmrgUse the @option{-fkeep-inline-functions} and @option{-fkeep-static-functions}
4851debfc3dSmrgoptions to retain these functions and
4861debfc3dSmrgallow gcov to properly show their @var{execution_count}.
4871debfc3dSmrg
4881debfc3dSmrgSome lines of information at the start have @var{line_number} of zero.
4891debfc3dSmrgThese preamble lines are of the form
4901debfc3dSmrg
4911debfc3dSmrg@smallexample
4921debfc3dSmrg-:0:@var{tag}:@var{value}
4931debfc3dSmrg@end smallexample
4941debfc3dSmrg
4951debfc3dSmrgThe ordering and number of these preamble lines will be augmented as
4961debfc3dSmrg@command{gcov} development progresses --- do not rely on them remaining
4971debfc3dSmrgunchanged.  Use @var{tag} to locate a particular preamble line.
4981debfc3dSmrg
4991debfc3dSmrgThe additional block information is of the form
5001debfc3dSmrg
5011debfc3dSmrg@smallexample
5021debfc3dSmrg@var{tag} @var{information}
5031debfc3dSmrg@end smallexample
5041debfc3dSmrg
5051debfc3dSmrgThe @var{information} is human readable, but designed to be simple
5061debfc3dSmrgenough for machine parsing too.
5071debfc3dSmrg
5081debfc3dSmrgWhen printing percentages, 0% and 100% are only printed when the values
5091debfc3dSmrgare @emph{exactly} 0% and 100% respectively.  Other values which would
5101debfc3dSmrgconventionally be rounded to 0% or 100% are instead printed as the
5111debfc3dSmrgnearest non-boundary value.
5121debfc3dSmrg
513c0a68be4SmrgWhen using @command{gcov}, you must first compile your program
514c0a68be4Smrgwith a special GCC option @samp{--coverage}.
5151debfc3dSmrgThis tells the compiler to generate additional information needed by
5161debfc3dSmrggcov (basically a flow graph of the program) and also includes
5171debfc3dSmrgadditional code in the object files for generating the extra profiling
5181debfc3dSmrginformation needed by gcov.  These additional files are placed in the
5191debfc3dSmrgdirectory where the object file is located.
5201debfc3dSmrg
5211debfc3dSmrgRunning the program will cause profile output to be generated.  For each
5221debfc3dSmrgsource file compiled with @option{-fprofile-arcs}, an accompanying
5231debfc3dSmrg@file{.gcda} file will be placed in the object file directory.
5241debfc3dSmrg
5251debfc3dSmrgRunning @command{gcov} with your program's source file names as arguments
5261debfc3dSmrgwill now produce a listing of the code along with frequency of execution
527a2dc1f3fSmrgfor each line.  For example, if your program is called @file{tmp.cpp}, this
5281debfc3dSmrgis what you see when you use the basic @command{gcov} facility:
5291debfc3dSmrg
5301debfc3dSmrg@smallexample
531c0a68be4Smrg$ g++ --coverage tmp.cpp
5321debfc3dSmrg$ a.out
533a2dc1f3fSmrg$ gcov tmp.cpp -m
534a2dc1f3fSmrgFile 'tmp.cpp'
535a2dc1f3fSmrgLines executed:92.86% of 14
536a2dc1f3fSmrgCreating 'tmp.cpp.gcov'
5371debfc3dSmrg@end smallexample
5381debfc3dSmrg
539a2dc1f3fSmrgThe file @file{tmp.cpp.gcov} contains output from @command{gcov}.
5401debfc3dSmrgHere is a sample:
5411debfc3dSmrg
5421debfc3dSmrg@smallexample
543a2dc1f3fSmrg        -:    0:Source:tmp.cpp
544c0a68be4Smrg        -:    0:Working directory:/home/gcc/testcase
5451debfc3dSmrg        -:    0:Graph:tmp.gcno
5461debfc3dSmrg        -:    0:Data:tmp.gcda
5471debfc3dSmrg        -:    0:Runs:1
5481debfc3dSmrg        -:    0:Programs:1
5491debfc3dSmrg        -:    1:#include <stdio.h>
5501debfc3dSmrg        -:    2:
551a2dc1f3fSmrg        -:    3:template<class T>
552a2dc1f3fSmrg        -:    4:class Foo
553a2dc1f3fSmrg        -:    5:@{
554a2dc1f3fSmrg        -:    6:  public:
555a2dc1f3fSmrg       1*:    7:  Foo(): b (1000) @{@}
556a2dc1f3fSmrg------------------
557a2dc1f3fSmrgFoo<char>::Foo():
558a2dc1f3fSmrg    #####:    7:  Foo(): b (1000) @{@}
559a2dc1f3fSmrg------------------
560a2dc1f3fSmrgFoo<int>::Foo():
561a2dc1f3fSmrg        1:    7:  Foo(): b (1000) @{@}
562a2dc1f3fSmrg------------------
563a2dc1f3fSmrg       2*:    8:  void inc () @{ b++; @}
564a2dc1f3fSmrg------------------
565a2dc1f3fSmrgFoo<char>::inc():
566a2dc1f3fSmrg    #####:    8:  void inc () @{ b++; @}
567a2dc1f3fSmrg------------------
568a2dc1f3fSmrgFoo<int>::inc():
569a2dc1f3fSmrg        2:    8:  void inc () @{ b++; @}
570a2dc1f3fSmrg------------------
571a2dc1f3fSmrg        -:    9:
572a2dc1f3fSmrg        -:   10:  private:
573a2dc1f3fSmrg        -:   11:  int b;
574a2dc1f3fSmrg        -:   12:@};
575a2dc1f3fSmrg        -:   13:
576a2dc1f3fSmrg        -:   14:template class Foo<int>;
577a2dc1f3fSmrg        -:   15:template class Foo<char>;
578a2dc1f3fSmrg        -:   16:
579a2dc1f3fSmrg        -:   17:int
580a2dc1f3fSmrg        1:   18:main (void)
581a2dc1f3fSmrg        -:   19:@{
582a2dc1f3fSmrg        -:   20:  int i, total;
583a2dc1f3fSmrg        1:   21:  Foo<int> counter;
584a2dc1f3fSmrg        -:   22:
585a2dc1f3fSmrg        1:   23:  counter.inc();
586a2dc1f3fSmrg        1:   24:  counter.inc();
587a2dc1f3fSmrg        1:   25:  total = 0;
588a2dc1f3fSmrg        -:   26:
589a2dc1f3fSmrg       11:   27:  for (i = 0; i < 10; i++)
590a2dc1f3fSmrg       10:   28:    total += i;
591a2dc1f3fSmrg        -:   29:
592a2dc1f3fSmrg       1*:   30:  int v = total > 100 ? 1 : 2;
593a2dc1f3fSmrg        -:   31:
594a2dc1f3fSmrg        1:   32:  if (total != 45)
595a2dc1f3fSmrg    #####:   33:    printf ("Failure\n");
596a2dc1f3fSmrg        -:   34:  else
597a2dc1f3fSmrg        1:   35:    printf ("Success\n");
598a2dc1f3fSmrg        1:   36:  return 0;
599a2dc1f3fSmrg        -:   37:@}
6001debfc3dSmrg@end smallexample
6011debfc3dSmrg
602a2dc1f3fSmrgNote that line 7 is shown in the report multiple times.  First occurrence
603a2dc1f3fSmrgpresents total number of execution of the line and the next two belong
604a2dc1f3fSmrgto instances of class Foo constructors.  As you can also see, line 30 contains
605a2dc1f3fSmrgsome unexecuted basic blocks and thus execution count has asterisk symbol.
606a2dc1f3fSmrg
6071debfc3dSmrgWhen you use the @option{-a} option, you will get individual block
6081debfc3dSmrgcounts, and the output looks like this:
6091debfc3dSmrg
6101debfc3dSmrg@smallexample
611a2dc1f3fSmrg        -:    0:Source:tmp.cpp
612c0a68be4Smrg        -:    0:Working directory:/home/gcc/testcase
6131debfc3dSmrg        -:    0:Graph:tmp.gcno
6141debfc3dSmrg        -:    0:Data:tmp.gcda
6151debfc3dSmrg        -:    0:Runs:1
6161debfc3dSmrg        -:    0:Programs:1
6171debfc3dSmrg        -:    1:#include <stdio.h>
6181debfc3dSmrg        -:    2:
619a2dc1f3fSmrg        -:    3:template<class T>
620a2dc1f3fSmrg        -:    4:class Foo
621a2dc1f3fSmrg        -:    5:@{
622a2dc1f3fSmrg        -:    6:  public:
623a2dc1f3fSmrg       1*:    7:  Foo(): b (1000) @{@}
624a2dc1f3fSmrg------------------
625a2dc1f3fSmrgFoo<char>::Foo():
626a2dc1f3fSmrg    #####:    7:  Foo(): b (1000) @{@}
627a2dc1f3fSmrg------------------
628a2dc1f3fSmrgFoo<int>::Foo():
629a2dc1f3fSmrg        1:    7:  Foo(): b (1000) @{@}
630a2dc1f3fSmrg------------------
631a2dc1f3fSmrg       2*:    8:  void inc () @{ b++; @}
632a2dc1f3fSmrg------------------
633a2dc1f3fSmrgFoo<char>::inc():
634a2dc1f3fSmrg    #####:    8:  void inc () @{ b++; @}
635a2dc1f3fSmrg------------------
636a2dc1f3fSmrgFoo<int>::inc():
637a2dc1f3fSmrg        2:    8:  void inc () @{ b++; @}
638a2dc1f3fSmrg------------------
639a2dc1f3fSmrg        -:    9:
640a2dc1f3fSmrg        -:   10:  private:
641a2dc1f3fSmrg        -:   11:  int b;
642a2dc1f3fSmrg        -:   12:@};
643a2dc1f3fSmrg        -:   13:
644a2dc1f3fSmrg        -:   14:template class Foo<int>;
645a2dc1f3fSmrg        -:   15:template class Foo<char>;
646a2dc1f3fSmrg        -:   16:
647a2dc1f3fSmrg        -:   17:int
648a2dc1f3fSmrg        1:   18:main (void)
649a2dc1f3fSmrg        -:   19:@{
650a2dc1f3fSmrg        -:   20:  int i, total;
651a2dc1f3fSmrg        1:   21:  Foo<int> counter;
652a2dc1f3fSmrg        1:   21-block  0
653a2dc1f3fSmrg        -:   22:
654a2dc1f3fSmrg        1:   23:  counter.inc();
655a2dc1f3fSmrg        1:   23-block  0
656a2dc1f3fSmrg        1:   24:  counter.inc();
657a2dc1f3fSmrg        1:   24-block  0
658a2dc1f3fSmrg        1:   25:  total = 0;
659a2dc1f3fSmrg        -:   26:
660a2dc1f3fSmrg       11:   27:  for (i = 0; i < 10; i++)
661a2dc1f3fSmrg        1:   27-block  0
662a2dc1f3fSmrg       11:   27-block  1
663a2dc1f3fSmrg       10:   28:    total += i;
664a2dc1f3fSmrg       10:   28-block  0
665a2dc1f3fSmrg        -:   29:
666a2dc1f3fSmrg       1*:   30:  int v = total > 100 ? 1 : 2;
667a2dc1f3fSmrg        1:   30-block  0
668a2dc1f3fSmrg    %%%%%:   30-block  1
669a2dc1f3fSmrg        1:   30-block  2
670a2dc1f3fSmrg        -:   31:
671a2dc1f3fSmrg        1:   32:  if (total != 45)
672a2dc1f3fSmrg        1:   32-block  0
673a2dc1f3fSmrg    #####:   33:    printf ("Failure\n");
674a2dc1f3fSmrg    %%%%%:   33-block  0
675a2dc1f3fSmrg        -:   34:  else
676a2dc1f3fSmrg        1:   35:    printf ("Success\n");
677a2dc1f3fSmrg        1:   35-block  0
678a2dc1f3fSmrg        1:   36:  return 0;
679a2dc1f3fSmrg        1:   36-block  0
680a2dc1f3fSmrg        -:   37:@}
6811debfc3dSmrg@end smallexample
6821debfc3dSmrg
6831debfc3dSmrgIn this mode, each basic block is only shown on one line -- the last
6841debfc3dSmrgline of the block.  A multi-line block will only contribute to the
6851debfc3dSmrgexecution count of that last line, and other lines will not be shown
6861debfc3dSmrgto contain code, unless previous blocks end on those lines.
6871debfc3dSmrgThe total execution count of a line is shown and subsequent lines show
6881debfc3dSmrgthe execution counts for individual blocks that end on that line.  After each
6891debfc3dSmrgblock, the branch and call counts of the block will be shown, if the
6901debfc3dSmrg@option{-b} option is given.
6911debfc3dSmrg
6921debfc3dSmrgBecause of the way GCC instruments calls, a call count can be shown
6931debfc3dSmrgafter a line with no individual blocks.
694a2dc1f3fSmrgAs you can see, line 33 contains a basic block that was not executed.
6951debfc3dSmrg
6961debfc3dSmrg@need 450
6971debfc3dSmrgWhen you use the @option{-b} option, your output looks like this:
6981debfc3dSmrg
6991debfc3dSmrg@smallexample
700a2dc1f3fSmrg        -:    0:Source:tmp.cpp
701c0a68be4Smrg        -:    0:Working directory:/home/gcc/testcase
7021debfc3dSmrg        -:    0:Graph:tmp.gcno
7031debfc3dSmrg        -:    0:Data:tmp.gcda
7041debfc3dSmrg        -:    0:Runs:1
7051debfc3dSmrg        -:    0:Programs:1
7061debfc3dSmrg        -:    1:#include <stdio.h>
7071debfc3dSmrg        -:    2:
708a2dc1f3fSmrg        -:    3:template<class T>
709a2dc1f3fSmrg        -:    4:class Foo
710a2dc1f3fSmrg        -:    5:@{
711a2dc1f3fSmrg        -:    6:  public:
712a2dc1f3fSmrg       1*:    7:  Foo(): b (1000) @{@}
713a2dc1f3fSmrg------------------
714a2dc1f3fSmrgFoo<char>::Foo():
715a2dc1f3fSmrgfunction Foo<char>::Foo() called 0 returned 0% blocks executed 0%
716a2dc1f3fSmrg    #####:    7:  Foo(): b (1000) @{@}
717a2dc1f3fSmrg------------------
718a2dc1f3fSmrgFoo<int>::Foo():
719a2dc1f3fSmrgfunction Foo<int>::Foo() called 1 returned 100% blocks executed 100%
720a2dc1f3fSmrg        1:    7:  Foo(): b (1000) @{@}
721a2dc1f3fSmrg------------------
722a2dc1f3fSmrg       2*:    8:  void inc () @{ b++; @}
723a2dc1f3fSmrg------------------
724a2dc1f3fSmrgFoo<char>::inc():
725a2dc1f3fSmrgfunction Foo<char>::inc() called 0 returned 0% blocks executed 0%
726a2dc1f3fSmrg    #####:    8:  void inc () @{ b++; @}
727a2dc1f3fSmrg------------------
728a2dc1f3fSmrgFoo<int>::inc():
729a2dc1f3fSmrgfunction Foo<int>::inc() called 2 returned 100% blocks executed 100%
730a2dc1f3fSmrg        2:    8:  void inc () @{ b++; @}
731a2dc1f3fSmrg------------------
732a2dc1f3fSmrg        -:    9:
733a2dc1f3fSmrg        -:   10:  private:
734a2dc1f3fSmrg        -:   11:  int b;
735a2dc1f3fSmrg        -:   12:@};
736a2dc1f3fSmrg        -:   13:
737a2dc1f3fSmrg        -:   14:template class Foo<int>;
738a2dc1f3fSmrg        -:   15:template class Foo<char>;
739a2dc1f3fSmrg        -:   16:
740a2dc1f3fSmrg        -:   17:int
741a2dc1f3fSmrgfunction main called 1 returned 100% blocks executed 81%
742a2dc1f3fSmrg        1:   18:main (void)
743a2dc1f3fSmrg        -:   19:@{
744a2dc1f3fSmrg        -:   20:  int i, total;
745a2dc1f3fSmrg        1:   21:  Foo<int> counter;
746a2dc1f3fSmrgcall    0 returned 100%
747a2dc1f3fSmrgbranch  1 taken 100% (fallthrough)
748a2dc1f3fSmrgbranch  2 taken 0% (throw)
749a2dc1f3fSmrg        -:   22:
750a2dc1f3fSmrg        1:   23:  counter.inc();
751a2dc1f3fSmrgcall    0 returned 100%
752a2dc1f3fSmrgbranch  1 taken 100% (fallthrough)
753a2dc1f3fSmrgbranch  2 taken 0% (throw)
754a2dc1f3fSmrg        1:   24:  counter.inc();
755a2dc1f3fSmrgcall    0 returned 100%
756a2dc1f3fSmrgbranch  1 taken 100% (fallthrough)
757a2dc1f3fSmrgbranch  2 taken 0% (throw)
758a2dc1f3fSmrg        1:   25:  total = 0;
759a2dc1f3fSmrg        -:   26:
760a2dc1f3fSmrg       11:   27:  for (i = 0; i < 10; i++)
7611debfc3dSmrgbranch  0 taken 91% (fallthrough)
7621debfc3dSmrgbranch  1 taken 9%
763a2dc1f3fSmrg       10:   28:    total += i;
764a2dc1f3fSmrg        -:   29:
765a2dc1f3fSmrg       1*:   30:  int v = total > 100 ? 1 : 2;
7661debfc3dSmrgbranch  0 taken 0% (fallthrough)
7671debfc3dSmrgbranch  1 taken 100%
768a2dc1f3fSmrg        -:   31:
769a2dc1f3fSmrg        1:   32:  if (total != 45)
770a2dc1f3fSmrgbranch  0 taken 0% (fallthrough)
771a2dc1f3fSmrgbranch  1 taken 100%
772a2dc1f3fSmrg    #####:   33:    printf ("Failure\n");
7731debfc3dSmrgcall    0 never executed
774a2dc1f3fSmrgbranch  1 never executed
775a2dc1f3fSmrgbranch  2 never executed
776a2dc1f3fSmrg        -:   34:  else
777a2dc1f3fSmrg        1:   35:    printf ("Success\n");
778a2dc1f3fSmrgcall    0 returned 100%
779a2dc1f3fSmrgbranch  1 taken 100% (fallthrough)
780a2dc1f3fSmrgbranch  2 taken 0% (throw)
781a2dc1f3fSmrg        1:   36:  return 0;
782a2dc1f3fSmrg        -:   37:@}
7831debfc3dSmrg@end smallexample
7841debfc3dSmrg
7851debfc3dSmrgFor each function, a line is printed showing how many times the function
7861debfc3dSmrgis called, how many times it returns and what percentage of the
7871debfc3dSmrgfunction's blocks were executed.
7881debfc3dSmrg
7891debfc3dSmrgFor each basic block, a line is printed after the last line of the basic
7901debfc3dSmrgblock describing the branch or call that ends the basic block.  There can
7911debfc3dSmrgbe multiple branches and calls listed for a single source line if there
7921debfc3dSmrgare multiple basic blocks that end on that line.  In this case, the
7931debfc3dSmrgbranches and calls are each given a number.  There is no simple way to map
7941debfc3dSmrgthese branches and calls back to source constructs.  In general, though,
7951debfc3dSmrgthe lowest numbered branch or call will correspond to the leftmost construct
7961debfc3dSmrgon the source line.
7971debfc3dSmrg
7981debfc3dSmrgFor a branch, if it was executed at least once, then a percentage
7991debfc3dSmrgindicating the number of times the branch was taken divided by the
8001debfc3dSmrgnumber of times the branch was executed will be printed.  Otherwise, the
8011debfc3dSmrgmessage ``never executed'' is printed.
8021debfc3dSmrg
8031debfc3dSmrgFor a call, if it was executed at least once, then a percentage
8041debfc3dSmrgindicating the number of times the call returned divided by the number
8051debfc3dSmrgof times the call was executed will be printed.  This will usually be
8061debfc3dSmrg100%, but may be less for functions that call @code{exit} or @code{longjmp},
8071debfc3dSmrgand thus may not return every time they are called.
8081debfc3dSmrg
8091debfc3dSmrgThe execution counts are cumulative.  If the example program were
8101debfc3dSmrgexecuted again without removing the @file{.gcda} file, the count for the
8111debfc3dSmrgnumber of times each line in the source was executed would be added to
8121debfc3dSmrgthe results of the previous run(s).  This is potentially useful in
8131debfc3dSmrgseveral ways.  For example, it could be used to accumulate data over a
8141debfc3dSmrgnumber of program runs as part of a test verification suite, or to
8151debfc3dSmrgprovide more accurate long-term information over a large number of
8161debfc3dSmrgprogram runs.
8171debfc3dSmrg
8181debfc3dSmrgThe data in the @file{.gcda} files is saved immediately before the program
8191debfc3dSmrgexits.  For each source file compiled with @option{-fprofile-arcs}, the
8201debfc3dSmrgprofiling code first attempts to read in an existing @file{.gcda} file; if
8211debfc3dSmrgthe file doesn't match the executable (differing number of basic block
8221debfc3dSmrgcounts) it will ignore the contents of the file.  It then adds in the
8231debfc3dSmrgnew execution counts and finally writes the data to the file.
8241debfc3dSmrg
8251debfc3dSmrg@node Gcov and Optimization
8261debfc3dSmrg@section Using @command{gcov} with GCC Optimization
8271debfc3dSmrg
8281debfc3dSmrgIf you plan to use @command{gcov} to help optimize your code, you must
829c0a68be4Smrgfirst compile your program with a special GCC option
830c0a68be4Smrg@samp{--coverage}.  Aside from that, you can use any
8311debfc3dSmrgother GCC options; but if you want to prove that every single line
8321debfc3dSmrgin your program was executed, you should not compile with optimization
8331debfc3dSmrgat the same time.  On some machines the optimizer can eliminate some
8341debfc3dSmrgsimple code lines by combining them with other lines.  For example, code
8351debfc3dSmrglike this:
8361debfc3dSmrg
8371debfc3dSmrg@smallexample
8381debfc3dSmrgif (a != b)
8391debfc3dSmrg  c = 1;
8401debfc3dSmrgelse
8411debfc3dSmrg  c = 0;
8421debfc3dSmrg@end smallexample
8431debfc3dSmrg
8441debfc3dSmrg@noindent
8451debfc3dSmrgcan be compiled into one instruction on some machines.  In this case,
8461debfc3dSmrgthere is no way for @command{gcov} to calculate separate execution counts
8471debfc3dSmrgfor each line because there isn't separate code for each line.  Hence
8481debfc3dSmrgthe @command{gcov} output looks like this if you compiled the program with
8491debfc3dSmrgoptimization:
8501debfc3dSmrg
8511debfc3dSmrg@smallexample
8521debfc3dSmrg      100:   12:if (a != b)
8531debfc3dSmrg      100:   13:  c = 1;
8541debfc3dSmrg      100:   14:else
8551debfc3dSmrg      100:   15:  c = 0;
8561debfc3dSmrg@end smallexample
8571debfc3dSmrg
8581debfc3dSmrgThe output shows that this block of code, combined by optimization,
8591debfc3dSmrgexecuted 100 times.  In one sense this result is correct, because there
8601debfc3dSmrgwas only one instruction representing all four of these lines.  However,
8611debfc3dSmrgthe output does not indicate how many times the result was 0 and how
8621debfc3dSmrgmany times the result was 1.
8631debfc3dSmrg
8641debfc3dSmrgInlineable functions can create unexpected line counts.  Line counts are
8651debfc3dSmrgshown for the source code of the inlineable function, but what is shown
8661debfc3dSmrgdepends on where the function is inlined, or if it is not inlined at all.
8671debfc3dSmrg
8681debfc3dSmrgIf the function is not inlined, the compiler must emit an out of line
8691debfc3dSmrgcopy of the function, in any object file that needs it.  If
8701debfc3dSmrg@file{fileA.o} and @file{fileB.o} both contain out of line bodies of a
8711debfc3dSmrgparticular inlineable function, they will also both contain coverage
8721debfc3dSmrgcounts for that function.  When @file{fileA.o} and @file{fileB.o} are
8731debfc3dSmrglinked together, the linker will, on many systems, select one of those
8741debfc3dSmrgout of line bodies for all calls to that function, and remove or ignore
8751debfc3dSmrgthe other.  Unfortunately, it will not remove the coverage counters for
8761debfc3dSmrgthe unused function body.  Hence when instrumented, all but one use of
8771debfc3dSmrgthat function will show zero counts.
8781debfc3dSmrg
8791debfc3dSmrgIf the function is inlined in several places, the block structure in
8801debfc3dSmrgeach location might not be the same.  For instance, a condition might
8811debfc3dSmrgnow be calculable at compile time in some instances.  Because the
8821debfc3dSmrgcoverage of all the uses of the inline function will be shown for the
8831debfc3dSmrgsame source lines, the line counts themselves might seem inconsistent.
8841debfc3dSmrg
8851debfc3dSmrgLong-running applications can use the @code{__gcov_reset} and @code{__gcov_dump}
8861debfc3dSmrgfacilities to restrict profile collection to the program region of
8871debfc3dSmrginterest. Calling @code{__gcov_reset(void)} will clear all profile counters
8881debfc3dSmrgto zero, and calling @code{__gcov_dump(void)} will cause the profile information
8891debfc3dSmrgcollected at that point to be dumped to @file{.gcda} output files.
8901debfc3dSmrgInstrumented applications use a static destructor with priority 99
8911debfc3dSmrgto invoke the @code{__gcov_dump} function. Thus @code{__gcov_dump}
8921debfc3dSmrgis executed after all user defined static destructors,
8931debfc3dSmrgas well as handlers registered with @code{atexit}.
8941debfc3dSmrgIf an executable loads a dynamic shared object via dlopen functionality,
8951debfc3dSmrg@option{-Wl,--dynamic-list-data} is needed to dump all profile data.
8961debfc3dSmrg
897c0a68be4SmrgProfiling run-time library reports various errors related to profile
898c0a68be4Smrgmanipulation and profile saving.  Errors are printed into standard error output
899c0a68be4Smrgor @samp{GCOV_ERROR_FILE} file, if environment variable is used.
900c0a68be4SmrgIn order to terminate immediately after an errors occurs
901c0a68be4Smrgset @samp{GCOV_EXIT_AT_ERROR} environment variable.
902c0a68be4SmrgThat can help users to find profile clashing which leads
903c0a68be4Smrgto a misleading profile.
904c0a68be4Smrg
9051debfc3dSmrg@c man end
9061debfc3dSmrg
9071debfc3dSmrg@node Gcov Data Files
9081debfc3dSmrg@section Brief Description of @command{gcov} Data Files
9091debfc3dSmrg
9101debfc3dSmrg@command{gcov} uses two files for profiling.  The names of these files
9111debfc3dSmrgare derived from the original @emph{object} file by substituting the
9121debfc3dSmrgfile suffix with either @file{.gcno}, or @file{.gcda}.  The files
9131debfc3dSmrgcontain coverage and profile data stored in a platform-independent format.
9141debfc3dSmrgThe @file{.gcno} files are placed in the same directory as the object
9151debfc3dSmrgfile.  By default, the @file{.gcda} files are also stored in the same
9161debfc3dSmrgdirectory as the object file, but the GCC @option{-fprofile-dir} option
9171debfc3dSmrgmay be used to store the @file{.gcda} files in a separate directory.
9181debfc3dSmrg
9191debfc3dSmrgThe @file{.gcno} notes file is generated when the source file is compiled
9201debfc3dSmrgwith the GCC @option{-ftest-coverage} option.  It contains information to
9211debfc3dSmrgreconstruct the basic block graphs and assign source line numbers to
9221debfc3dSmrgblocks.
9231debfc3dSmrg
9241debfc3dSmrgThe @file{.gcda} count data file is generated when a program containing
9251debfc3dSmrgobject files built with the GCC @option{-fprofile-arcs} option is executed.
9261debfc3dSmrgA separate @file{.gcda} file is created for each object file compiled with
9271debfc3dSmrgthis option.  It contains arc transition counts, value profile counts, and
9281debfc3dSmrgsome summary information.
9291debfc3dSmrg
930a2dc1f3fSmrgIt is not recommended to access the coverage files directly.
931a2dc1f3fSmrgConsumers should use the intermediate format that is provided
932c0a68be4Smrgby @command{gcov} tool via @option{--json-format} option.
9331debfc3dSmrg
9341debfc3dSmrg@node Cross-profiling
9351debfc3dSmrg@section Data File Relocation to Support Cross-Profiling
9361debfc3dSmrg
9371debfc3dSmrgRunning the program will cause profile output to be generated.  For each
9381debfc3dSmrgsource file compiled with @option{-fprofile-arcs}, an accompanying @file{.gcda}
9391debfc3dSmrgfile will be placed in the object file directory. That implicitly requires
9401debfc3dSmrgrunning the program on the same system as it was built or having the same
9411debfc3dSmrgabsolute directory structure on the target system. The program will try
9421debfc3dSmrgto create the needed directory structure, if it is not already present.
9431debfc3dSmrg
9441debfc3dSmrgTo support cross-profiling, a program compiled with @option{-fprofile-arcs}
9451debfc3dSmrgcan relocate the data files based on two environment variables:
9461debfc3dSmrg
9471debfc3dSmrg@itemize @bullet
9481debfc3dSmrg@item
9491debfc3dSmrgGCOV_PREFIX contains the prefix to add to the absolute paths
9501debfc3dSmrgin the object file. Prefix can be absolute, or relative.  The
9511debfc3dSmrgdefault is no prefix.
9521debfc3dSmrg
9531debfc3dSmrg@item
9541debfc3dSmrgGCOV_PREFIX_STRIP indicates the how many initial directory names to strip off
9551debfc3dSmrgthe hardwired absolute paths. Default value is 0.
9561debfc3dSmrg
9571debfc3dSmrg@emph{Note:} If GCOV_PREFIX_STRIP is set without GCOV_PREFIX is undefined,
9581debfc3dSmrg then a relative path is made out of the hardwired absolute paths.
9591debfc3dSmrg@end itemize
9601debfc3dSmrg
9611debfc3dSmrgFor example, if the object file @file{/user/build/foo.o} was built with
9621debfc3dSmrg@option{-fprofile-arcs}, the final executable will try to create the data file
9631debfc3dSmrg@file{/user/build/foo.gcda} when running on the target system.  This will
9641debfc3dSmrgfail if the corresponding directory does not exist and it is unable to create
9651debfc3dSmrgit.  This can be overcome by, for example, setting the environment as
9661debfc3dSmrg@samp{GCOV_PREFIX=/target/run} and @samp{GCOV_PREFIX_STRIP=1}.  Such a
9671debfc3dSmrgsetting will name the data file @file{/target/run/build/foo.gcda}.
9681debfc3dSmrg
9691debfc3dSmrgYou must move the data files to the expected directory tree in order to
9701debfc3dSmrguse them for profile directed optimizations (@option{-fprofile-use}), or to
9711debfc3dSmrguse the @command{gcov} tool.
972