1*e4b17023SJohn Marino@c Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2*e4b17023SJohn Marino@c 2008, 2010 Free Software Foundation, Inc. 3*e4b17023SJohn Marino@c This is part of the GCC manual. 4*e4b17023SJohn Marino@c For copying conditions, see the file gcc.texi. 5*e4b17023SJohn Marino 6*e4b17023SJohn Marino@ignore 7*e4b17023SJohn Marino@c man begin COPYRIGHT 8*e4b17023SJohn MarinoCopyright @copyright{} 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 9*e4b17023SJohn Marino2005, 2008, 2010 Free Software Foundation, Inc. 10*e4b17023SJohn Marino 11*e4b17023SJohn MarinoPermission is granted to copy, distribute and/or modify this document 12*e4b17023SJohn Marinounder the terms of the GNU Free Documentation License, Version 1.3 or 13*e4b17023SJohn Marinoany later version published by the Free Software Foundation; with the 14*e4b17023SJohn MarinoInvariant Sections being ``GNU General Public License'' and ``Funding 15*e4b17023SJohn MarinoFree Software'', the Front-Cover texts being (a) (see below), and with 16*e4b17023SJohn Marinothe Back-Cover Texts being (b) (see below). A copy of the license is 17*e4b17023SJohn Marinoincluded in the gfdl(7) man page. 18*e4b17023SJohn Marino 19*e4b17023SJohn Marino(a) The FSF's Front-Cover Text is: 20*e4b17023SJohn Marino 21*e4b17023SJohn Marino A GNU Manual 22*e4b17023SJohn Marino 23*e4b17023SJohn Marino(b) The FSF's Back-Cover Text is: 24*e4b17023SJohn Marino 25*e4b17023SJohn Marino You have freedom to copy and modify this GNU Manual, like GNU 26*e4b17023SJohn Marino software. Copies published by the Free Software Foundation raise 27*e4b17023SJohn Marino funds for GNU development. 28*e4b17023SJohn Marino@c man end 29*e4b17023SJohn Marino@c Set file name and title for the man page. 30*e4b17023SJohn Marino@setfilename gcov 31*e4b17023SJohn Marino@settitle coverage testing tool 32*e4b17023SJohn Marino@end ignore 33*e4b17023SJohn Marino 34*e4b17023SJohn Marino@node Gcov 35*e4b17023SJohn Marino@chapter @command{gcov}---a Test Coverage Program 36*e4b17023SJohn Marino 37*e4b17023SJohn Marino@command{gcov} is a tool you can use in conjunction with GCC to 38*e4b17023SJohn Marinotest code coverage in your programs. 39*e4b17023SJohn Marino 40*e4b17023SJohn Marino@menu 41*e4b17023SJohn Marino* Gcov Intro:: Introduction to gcov. 42*e4b17023SJohn Marino* Invoking Gcov:: How to use gcov. 43*e4b17023SJohn Marino* Gcov and Optimization:: Using gcov with GCC optimization. 44*e4b17023SJohn Marino* Gcov Data Files:: The files used by gcov. 45*e4b17023SJohn Marino* Cross-profiling:: Data file relocation. 46*e4b17023SJohn Marino@end menu 47*e4b17023SJohn Marino 48*e4b17023SJohn Marino@node Gcov Intro 49*e4b17023SJohn Marino@section Introduction to @command{gcov} 50*e4b17023SJohn Marino@c man begin DESCRIPTION 51*e4b17023SJohn Marino 52*e4b17023SJohn Marino@command{gcov} is a test coverage program. Use it in concert with GCC 53*e4b17023SJohn Marinoto analyze your programs to help create more efficient, faster running 54*e4b17023SJohn Marinocode and to discover untested parts of your program. You can use 55*e4b17023SJohn Marino@command{gcov} as a profiling tool to help discover where your 56*e4b17023SJohn Marinooptimization efforts will best affect your code. You can also use 57*e4b17023SJohn Marino@command{gcov} along with the other profiling tool, @command{gprof}, to 58*e4b17023SJohn Marinoassess which parts of your code use the greatest amount of computing 59*e4b17023SJohn Marinotime. 60*e4b17023SJohn Marino 61*e4b17023SJohn MarinoProfiling tools help you analyze your code's performance. Using a 62*e4b17023SJohn Marinoprofiler such as @command{gcov} or @command{gprof}, you can find out some 63*e4b17023SJohn Marinobasic performance statistics, such as: 64*e4b17023SJohn Marino 65*e4b17023SJohn Marino@itemize @bullet 66*e4b17023SJohn Marino@item 67*e4b17023SJohn Marinohow often each line of code executes 68*e4b17023SJohn Marino 69*e4b17023SJohn Marino@item 70*e4b17023SJohn Marinowhat lines of code are actually executed 71*e4b17023SJohn Marino 72*e4b17023SJohn Marino@item 73*e4b17023SJohn Marinohow much computing time each section of code uses 74*e4b17023SJohn Marino@end itemize 75*e4b17023SJohn Marino 76*e4b17023SJohn MarinoOnce you know these things about how your code works when compiled, you 77*e4b17023SJohn Marinocan look at each module to see which modules should be optimized. 78*e4b17023SJohn Marino@command{gcov} helps you determine where to work on optimization. 79*e4b17023SJohn Marino 80*e4b17023SJohn MarinoSoftware developers also use coverage testing in concert with 81*e4b17023SJohn Marinotestsuites, to make sure software is actually good enough for a release. 82*e4b17023SJohn MarinoTestsuites can verify that a program works as expected; a coverage 83*e4b17023SJohn Marinoprogram tests to see how much of the program is exercised by the 84*e4b17023SJohn Marinotestsuite. Developers can then determine what kinds of test cases need 85*e4b17023SJohn Marinoto be added to the testsuites to create both better testing and a better 86*e4b17023SJohn Marinofinal product. 87*e4b17023SJohn Marino 88*e4b17023SJohn MarinoYou should compile your code without optimization if you plan to use 89*e4b17023SJohn Marino@command{gcov} because the optimization, by combining some lines of code 90*e4b17023SJohn Marinointo one function, may not give you as much information as you need to 91*e4b17023SJohn Marinolook for `hot spots' where the code is using a great deal of computer 92*e4b17023SJohn Marinotime. Likewise, because @command{gcov} accumulates statistics by line (at 93*e4b17023SJohn Marinothe lowest resolution), it works best with a programming style that 94*e4b17023SJohn Marinoplaces only one statement on each line. If you use complicated macros 95*e4b17023SJohn Marinothat expand to loops or to other control structures, the statistics are 96*e4b17023SJohn Marinoless helpful---they only report on the line where the macro call 97*e4b17023SJohn Marinoappears. If your complex macros behave like functions, you can replace 98*e4b17023SJohn Marinothem with inline functions to solve this problem. 99*e4b17023SJohn Marino 100*e4b17023SJohn Marino@command{gcov} creates a logfile called @file{@var{sourcefile}.gcov} which 101*e4b17023SJohn Marinoindicates how many times each line of a source file @file{@var{sourcefile}.c} 102*e4b17023SJohn Marinohas executed. You can use these logfiles along with @command{gprof} to aid 103*e4b17023SJohn Marinoin fine-tuning the performance of your programs. @command{gprof} gives 104*e4b17023SJohn Marinotiming information you can use along with the information you get from 105*e4b17023SJohn Marino@command{gcov}. 106*e4b17023SJohn Marino 107*e4b17023SJohn Marino@command{gcov} works only on code compiled with GCC@. It is not 108*e4b17023SJohn Marinocompatible with any other profiling or test coverage mechanism. 109*e4b17023SJohn Marino 110*e4b17023SJohn Marino@c man end 111*e4b17023SJohn Marino 112*e4b17023SJohn Marino@node Invoking Gcov 113*e4b17023SJohn Marino@section Invoking @command{gcov} 114*e4b17023SJohn Marino 115*e4b17023SJohn Marino@smallexample 116*e4b17023SJohn Marinogcov @r{[}@var{options}@r{]} @var{files} 117*e4b17023SJohn Marino@end smallexample 118*e4b17023SJohn Marino 119*e4b17023SJohn Marino@command{gcov} accepts the following options: 120*e4b17023SJohn Marino 121*e4b17023SJohn Marino@ignore 122*e4b17023SJohn Marino@c man begin SYNOPSIS 123*e4b17023SJohn Marinogcov [@option{-v}|@option{--version}] [@option{-h}|@option{--help}] 124*e4b17023SJohn Marino [@option{-a}|@option{--all-blocks}] 125*e4b17023SJohn Marino [@option{-b}|@option{--branch-probabilities}] 126*e4b17023SJohn Marino [@option{-c}|@option{--branch-counts}] 127*e4b17023SJohn Marino [@option{-u}|@option{--unconditional-branches}] 128*e4b17023SJohn Marino [@option{-n}|@option{--no-output}] 129*e4b17023SJohn Marino [@option{-l}|@option{--long-file-names}] 130*e4b17023SJohn Marino [@option{-p}|@option{--preserve-paths}] 131*e4b17023SJohn Marino [@option{-r}|@option{--relative-only}] 132*e4b17023SJohn Marino [@option{-f}|@option{--function-summaries}] 133*e4b17023SJohn Marino [@option{-o}|@option{--object-directory} @var{directory|file}] 134*e4b17023SJohn Marino [@option{-s}|@option{--source-prefix} @var{directory}] 135*e4b17023SJohn Marino [@option{-d}|@option{--display-progress}] 136*e4b17023SJohn Marino @var{files} 137*e4b17023SJohn Marino@c man end 138*e4b17023SJohn Marino@c man begin SEEALSO 139*e4b17023SJohn Marinogpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}. 140*e4b17023SJohn Marino@c man end 141*e4b17023SJohn Marino@end ignore 142*e4b17023SJohn Marino 143*e4b17023SJohn Marino@c man begin OPTIONS 144*e4b17023SJohn Marino@table @gcctabopt 145*e4b17023SJohn Marino@item -h 146*e4b17023SJohn Marino@itemx --help 147*e4b17023SJohn MarinoDisplay help about using @command{gcov} (on the standard output), and 148*e4b17023SJohn Marinoexit without doing any further processing. 149*e4b17023SJohn Marino 150*e4b17023SJohn Marino@item -v 151*e4b17023SJohn Marino@itemx --version 152*e4b17023SJohn MarinoDisplay the @command{gcov} version number (on the standard output), 153*e4b17023SJohn Marinoand exit without doing any further processing. 154*e4b17023SJohn Marino 155*e4b17023SJohn Marino@item -a 156*e4b17023SJohn Marino@itemx --all-blocks 157*e4b17023SJohn MarinoWrite individual execution counts for every basic block. Normally gcov 158*e4b17023SJohn Marinooutputs execution counts only for the main blocks of a line. With this 159*e4b17023SJohn Marinooption you can determine if blocks within a single line are not being 160*e4b17023SJohn Marinoexecuted. 161*e4b17023SJohn Marino 162*e4b17023SJohn Marino@item -b 163*e4b17023SJohn Marino@itemx --branch-probabilities 164*e4b17023SJohn MarinoWrite branch frequencies to the output file, and write branch summary 165*e4b17023SJohn Marinoinfo to the standard output. This option allows you to see how often 166*e4b17023SJohn Marinoeach branch in your program was taken. Unconditional branches will not 167*e4b17023SJohn Marinobe shown, unless the @option{-u} option is given. 168*e4b17023SJohn Marino 169*e4b17023SJohn Marino@item -c 170*e4b17023SJohn Marino@itemx --branch-counts 171*e4b17023SJohn MarinoWrite branch frequencies as the number of branches taken, rather than 172*e4b17023SJohn Marinothe percentage of branches taken. 173*e4b17023SJohn Marino 174*e4b17023SJohn Marino@item -n 175*e4b17023SJohn Marino@itemx --no-output 176*e4b17023SJohn MarinoDo not create the @command{gcov} output file. 177*e4b17023SJohn Marino 178*e4b17023SJohn Marino@item -l 179*e4b17023SJohn Marino@itemx --long-file-names 180*e4b17023SJohn MarinoCreate long file names for included source files. For example, if the 181*e4b17023SJohn Marinoheader file @file{x.h} contains code, and was included in the file 182*e4b17023SJohn Marino@file{a.c}, then running @command{gcov} on the file @file{a.c} will 183*e4b17023SJohn Marinoproduce an output file called @file{a.c##x.h.gcov} instead of 184*e4b17023SJohn Marino@file{x.h.gcov}. This can be useful if @file{x.h} is included in 185*e4b17023SJohn Marinomultiple source files and you want to see the individual 186*e4b17023SJohn Marinocontributions. If you use the @samp{-p} option, both the including 187*e4b17023SJohn Marinoand included file names will be complete path names. 188*e4b17023SJohn Marino 189*e4b17023SJohn Marino@item -p 190*e4b17023SJohn Marino@itemx --preserve-paths 191*e4b17023SJohn MarinoPreserve complete path information in the names of generated 192*e4b17023SJohn Marino@file{.gcov} files. Without this option, just the filename component is 193*e4b17023SJohn Marinoused. With this option, all directories are used, with @samp{/} characters 194*e4b17023SJohn Marinotranslated to @samp{#} characters, @file{.} directory components 195*e4b17023SJohn Marinoremoved and unremoveable @file{..} 196*e4b17023SJohn Marinocomponents renamed to @samp{^}. This is useful if sourcefiles are in several 197*e4b17023SJohn Marinodifferent directories. 198*e4b17023SJohn Marino 199*e4b17023SJohn Marino@item -r 200*e4b17023SJohn Marino@itemx --relative-only 201*e4b17023SJohn MarinoOnly output information about source files with a relative pathname 202*e4b17023SJohn Marino(after source prefix elision). Absolute paths are usually system 203*e4b17023SJohn Marinoheader files and coverage of any inline functions therein is normally 204*e4b17023SJohn Marinouninteresting. 205*e4b17023SJohn Marino 206*e4b17023SJohn Marino@item -f 207*e4b17023SJohn Marino@itemx --function-summaries 208*e4b17023SJohn MarinoOutput summaries for each function in addition to the file level summary. 209*e4b17023SJohn Marino 210*e4b17023SJohn Marino@item -o @var{directory|file} 211*e4b17023SJohn Marino@itemx --object-directory @var{directory} 212*e4b17023SJohn Marino@itemx --object-file @var{file} 213*e4b17023SJohn MarinoSpecify either the directory containing the gcov data files, or the 214*e4b17023SJohn Marinoobject path name. The @file{.gcno}, and 215*e4b17023SJohn Marino@file{.gcda} data files are searched for using this option. If a directory 216*e4b17023SJohn Marinois specified, the data files are in that directory and named after the 217*e4b17023SJohn Marinoinput file name, without its extension. If a file is specified here, 218*e4b17023SJohn Marinothe data files are named after that file, without its extension. 219*e4b17023SJohn Marino 220*e4b17023SJohn Marino@item -s @var{directory} 221*e4b17023SJohn Marino@itemx --source-prefix @var{directory} 222*e4b17023SJohn MarinoA prefix for source file names to remove when generating the output 223*e4b17023SJohn Marinocoverage files. This option is useful when building in a separate 224*e4b17023SJohn Marinodirectory, and the pathname to the source directory is not wanted when 225*e4b17023SJohn Marinodetermining the output file names. Note that this prefix detection is 226*e4b17023SJohn Marinoapplied before determining whether the source file is absolute. 227*e4b17023SJohn Marino 228*e4b17023SJohn Marino@item -u 229*e4b17023SJohn Marino@itemx --unconditional-branches 230*e4b17023SJohn MarinoWhen branch probabilities are given, include those of unconditional branches. 231*e4b17023SJohn MarinoUnconditional branches are normally not interesting. 232*e4b17023SJohn Marino 233*e4b17023SJohn Marino@item -d 234*e4b17023SJohn Marino@itemx --display-progress 235*e4b17023SJohn MarinoDisplay the progress on the standard output. 236*e4b17023SJohn Marino 237*e4b17023SJohn Marino@end table 238*e4b17023SJohn Marino 239*e4b17023SJohn Marino@command{gcov} should be run with the current directory the same as that 240*e4b17023SJohn Marinowhen you invoked the compiler. Otherwise it will not be able to locate 241*e4b17023SJohn Marinothe source files. @command{gcov} produces files called 242*e4b17023SJohn Marino@file{@var{mangledname}.gcov} in the current directory. These contain 243*e4b17023SJohn Marinothe coverage information of the source file they correspond to. 244*e4b17023SJohn MarinoOne @file{.gcov} file is produced for each source (or header) file 245*e4b17023SJohn Marinocontaining code, 246*e4b17023SJohn Marinowhich was compiled to produce the data files. The @var{mangledname} part 247*e4b17023SJohn Marinoof the output file name is usually simply the source file name, but can 248*e4b17023SJohn Marinobe something more complicated if the @samp{-l} or @samp{-p} options are 249*e4b17023SJohn Marinogiven. Refer to those options for details. 250*e4b17023SJohn Marino 251*e4b17023SJohn MarinoIf you invoke @command{gcov} with multiple input files, the 252*e4b17023SJohn Marinocontributions from each input file are summed. Typically you would 253*e4b17023SJohn Marinoinvoke it with the same list of files as the final link of your executable. 254*e4b17023SJohn Marino 255*e4b17023SJohn MarinoThe @file{.gcov} files contain the @samp{:} separated fields along with 256*e4b17023SJohn Marinoprogram source code. The format is 257*e4b17023SJohn Marino 258*e4b17023SJohn Marino@smallexample 259*e4b17023SJohn Marino@var{execution_count}:@var{line_number}:@var{source line text} 260*e4b17023SJohn Marino@end smallexample 261*e4b17023SJohn Marino 262*e4b17023SJohn MarinoAdditional block information may succeed each line, when requested by 263*e4b17023SJohn Marinocommand line option. The @var{execution_count} is @samp{-} for lines 264*e4b17023SJohn Marinocontaining no code. Unexecuted lines are marked @samp{#####} or 265*e4b17023SJohn Marino@samp{====}, depending on whether they are reachable by 266*e4b17023SJohn Marinonon-exceptional paths or only exceptional paths such as C++ exception 267*e4b17023SJohn Marinohandlers, respectively. 268*e4b17023SJohn Marino 269*e4b17023SJohn MarinoSome lines of information at the start have @var{line_number} of zero. 270*e4b17023SJohn MarinoThese preamble lines are of the form 271*e4b17023SJohn Marino 272*e4b17023SJohn Marino@smallexample 273*e4b17023SJohn Marino-:0:@var{tag}:@var{value} 274*e4b17023SJohn Marino@end smallexample 275*e4b17023SJohn Marino 276*e4b17023SJohn MarinoThe ordering and number of these preamble lines will be augmented as 277*e4b17023SJohn Marino@command{gcov} development progresses --- do not rely on them remaining 278*e4b17023SJohn Marinounchanged. Use @var{tag} to locate a particular preamble line. 279*e4b17023SJohn Marino 280*e4b17023SJohn MarinoThe additional block information is of the form 281*e4b17023SJohn Marino 282*e4b17023SJohn Marino@smallexample 283*e4b17023SJohn Marino@var{tag} @var{information} 284*e4b17023SJohn Marino@end smallexample 285*e4b17023SJohn Marino 286*e4b17023SJohn MarinoThe @var{information} is human readable, but designed to be simple 287*e4b17023SJohn Marinoenough for machine parsing too. 288*e4b17023SJohn Marino 289*e4b17023SJohn MarinoWhen printing percentages, 0% and 100% are only printed when the values 290*e4b17023SJohn Marinoare @emph{exactly} 0% and 100% respectively. Other values which would 291*e4b17023SJohn Marinoconventionally be rounded to 0% or 100% are instead printed as the 292*e4b17023SJohn Marinonearest non-boundary value. 293*e4b17023SJohn Marino 294*e4b17023SJohn MarinoWhen using @command{gcov}, you must first compile your program with two 295*e4b17023SJohn Marinospecial GCC options: @samp{-fprofile-arcs -ftest-coverage}. 296*e4b17023SJohn MarinoThis tells the compiler to generate additional information needed by 297*e4b17023SJohn Marinogcov (basically a flow graph of the program) and also includes 298*e4b17023SJohn Marinoadditional code in the object files for generating the extra profiling 299*e4b17023SJohn Marinoinformation needed by gcov. These additional files are placed in the 300*e4b17023SJohn Marinodirectory where the object file is located. 301*e4b17023SJohn Marino 302*e4b17023SJohn MarinoRunning the program will cause profile output to be generated. For each 303*e4b17023SJohn Marinosource file compiled with @option{-fprofile-arcs}, an accompanying 304*e4b17023SJohn Marino@file{.gcda} file will be placed in the object file directory. 305*e4b17023SJohn Marino 306*e4b17023SJohn MarinoRunning @command{gcov} with your program's source file names as arguments 307*e4b17023SJohn Marinowill now produce a listing of the code along with frequency of execution 308*e4b17023SJohn Marinofor each line. For example, if your program is called @file{tmp.c}, this 309*e4b17023SJohn Marinois what you see when you use the basic @command{gcov} facility: 310*e4b17023SJohn Marino 311*e4b17023SJohn Marino@smallexample 312*e4b17023SJohn Marino$ gcc -fprofile-arcs -ftest-coverage tmp.c 313*e4b17023SJohn Marino$ a.out 314*e4b17023SJohn Marino$ gcov tmp.c 315*e4b17023SJohn Marino90.00% of 10 source lines executed in file tmp.c 316*e4b17023SJohn MarinoCreating tmp.c.gcov. 317*e4b17023SJohn Marino@end smallexample 318*e4b17023SJohn Marino 319*e4b17023SJohn MarinoThe file @file{tmp.c.gcov} contains output from @command{gcov}. 320*e4b17023SJohn MarinoHere is a sample: 321*e4b17023SJohn Marino 322*e4b17023SJohn Marino@smallexample 323*e4b17023SJohn Marino -: 0:Source:tmp.c 324*e4b17023SJohn Marino -: 0:Graph:tmp.gcno 325*e4b17023SJohn Marino -: 0:Data:tmp.gcda 326*e4b17023SJohn Marino -: 0:Runs:1 327*e4b17023SJohn Marino -: 0:Programs:1 328*e4b17023SJohn Marino -: 1:#include <stdio.h> 329*e4b17023SJohn Marino -: 2: 330*e4b17023SJohn Marino -: 3:int main (void) 331*e4b17023SJohn Marino 1: 4:@{ 332*e4b17023SJohn Marino 1: 5: int i, total; 333*e4b17023SJohn Marino -: 6: 334*e4b17023SJohn Marino 1: 7: total = 0; 335*e4b17023SJohn Marino -: 8: 336*e4b17023SJohn Marino 11: 9: for (i = 0; i < 10; i++) 337*e4b17023SJohn Marino 10: 10: total += i; 338*e4b17023SJohn Marino -: 11: 339*e4b17023SJohn Marino 1: 12: if (total != 45) 340*e4b17023SJohn Marino #####: 13: printf ("Failure\n"); 341*e4b17023SJohn Marino -: 14: else 342*e4b17023SJohn Marino 1: 15: printf ("Success\n"); 343*e4b17023SJohn Marino 1: 16: return 0; 344*e4b17023SJohn Marino -: 17:@} 345*e4b17023SJohn Marino@end smallexample 346*e4b17023SJohn Marino 347*e4b17023SJohn MarinoWhen you use the @option{-a} option, you will get individual block 348*e4b17023SJohn Marinocounts, and the output looks like this: 349*e4b17023SJohn Marino 350*e4b17023SJohn Marino@smallexample 351*e4b17023SJohn Marino -: 0:Source:tmp.c 352*e4b17023SJohn Marino -: 0:Graph:tmp.gcno 353*e4b17023SJohn Marino -: 0:Data:tmp.gcda 354*e4b17023SJohn Marino -: 0:Runs:1 355*e4b17023SJohn Marino -: 0:Programs:1 356*e4b17023SJohn Marino -: 1:#include <stdio.h> 357*e4b17023SJohn Marino -: 2: 358*e4b17023SJohn Marino -: 3:int main (void) 359*e4b17023SJohn Marino 1: 4:@{ 360*e4b17023SJohn Marino 1: 4-block 0 361*e4b17023SJohn Marino 1: 5: int i, total; 362*e4b17023SJohn Marino -: 6: 363*e4b17023SJohn Marino 1: 7: total = 0; 364*e4b17023SJohn Marino -: 8: 365*e4b17023SJohn Marino 11: 9: for (i = 0; i < 10; i++) 366*e4b17023SJohn Marino 11: 9-block 0 367*e4b17023SJohn Marino 10: 10: total += i; 368*e4b17023SJohn Marino 10: 10-block 0 369*e4b17023SJohn Marino -: 11: 370*e4b17023SJohn Marino 1: 12: if (total != 45) 371*e4b17023SJohn Marino 1: 12-block 0 372*e4b17023SJohn Marino #####: 13: printf ("Failure\n"); 373*e4b17023SJohn Marino $$$$$: 13-block 0 374*e4b17023SJohn Marino -: 14: else 375*e4b17023SJohn Marino 1: 15: printf ("Success\n"); 376*e4b17023SJohn Marino 1: 15-block 0 377*e4b17023SJohn Marino 1: 16: return 0; 378*e4b17023SJohn Marino 1: 16-block 0 379*e4b17023SJohn Marino -: 17:@} 380*e4b17023SJohn Marino@end smallexample 381*e4b17023SJohn Marino 382*e4b17023SJohn MarinoIn this mode, each basic block is only shown on one line -- the last 383*e4b17023SJohn Marinoline of the block. A multi-line block will only contribute to the 384*e4b17023SJohn Marinoexecution count of that last line, and other lines will not be shown 385*e4b17023SJohn Marinoto contain code, unless previous blocks end on those lines. 386*e4b17023SJohn MarinoThe total execution count of a line is shown and subsequent lines show 387*e4b17023SJohn Marinothe execution counts for individual blocks that end on that line. After each 388*e4b17023SJohn Marinoblock, the branch and call counts of the block will be shown, if the 389*e4b17023SJohn Marino@option{-b} option is given. 390*e4b17023SJohn Marino 391*e4b17023SJohn MarinoBecause of the way GCC instruments calls, a call count can be shown 392*e4b17023SJohn Marinoafter a line with no individual blocks. 393*e4b17023SJohn MarinoAs you can see, line 13 contains a basic block that was not executed. 394*e4b17023SJohn Marino 395*e4b17023SJohn Marino@need 450 396*e4b17023SJohn MarinoWhen you use the @option{-b} option, your output looks like this: 397*e4b17023SJohn Marino 398*e4b17023SJohn Marino@smallexample 399*e4b17023SJohn Marino$ gcov -b tmp.c 400*e4b17023SJohn Marino90.00% of 10 source lines executed in file tmp.c 401*e4b17023SJohn Marino80.00% of 5 branches executed in file tmp.c 402*e4b17023SJohn Marino80.00% of 5 branches taken at least once in file tmp.c 403*e4b17023SJohn Marino50.00% of 2 calls executed in file tmp.c 404*e4b17023SJohn MarinoCreating tmp.c.gcov. 405*e4b17023SJohn Marino@end smallexample 406*e4b17023SJohn Marino 407*e4b17023SJohn MarinoHere is a sample of a resulting @file{tmp.c.gcov} file: 408*e4b17023SJohn Marino 409*e4b17023SJohn Marino@smallexample 410*e4b17023SJohn Marino -: 0:Source:tmp.c 411*e4b17023SJohn Marino -: 0:Graph:tmp.gcno 412*e4b17023SJohn Marino -: 0:Data:tmp.gcda 413*e4b17023SJohn Marino -: 0:Runs:1 414*e4b17023SJohn Marino -: 0:Programs:1 415*e4b17023SJohn Marino -: 1:#include <stdio.h> 416*e4b17023SJohn Marino -: 2: 417*e4b17023SJohn Marino -: 3:int main (void) 418*e4b17023SJohn Marinofunction main called 1 returned 1 blocks executed 75% 419*e4b17023SJohn Marino 1: 4:@{ 420*e4b17023SJohn Marino 1: 5: int i, total; 421*e4b17023SJohn Marino -: 6: 422*e4b17023SJohn Marino 1: 7: total = 0; 423*e4b17023SJohn Marino -: 8: 424*e4b17023SJohn Marino 11: 9: for (i = 0; i < 10; i++) 425*e4b17023SJohn Marinobranch 0 taken 91% (fallthrough) 426*e4b17023SJohn Marinobranch 1 taken 9% 427*e4b17023SJohn Marino 10: 10: total += i; 428*e4b17023SJohn Marino -: 11: 429*e4b17023SJohn Marino 1: 12: if (total != 45) 430*e4b17023SJohn Marinobranch 0 taken 0% (fallthrough) 431*e4b17023SJohn Marinobranch 1 taken 100% 432*e4b17023SJohn Marino #####: 13: printf ("Failure\n"); 433*e4b17023SJohn Marinocall 0 never executed 434*e4b17023SJohn Marino -: 14: else 435*e4b17023SJohn Marino 1: 15: printf ("Success\n"); 436*e4b17023SJohn Marinocall 0 called 1 returned 100% 437*e4b17023SJohn Marino 1: 16: return 0; 438*e4b17023SJohn Marino -: 17:@} 439*e4b17023SJohn Marino@end smallexample 440*e4b17023SJohn Marino 441*e4b17023SJohn MarinoFor each function, a line is printed showing how many times the function 442*e4b17023SJohn Marinois called, how many times it returns and what percentage of the 443*e4b17023SJohn Marinofunction's blocks were executed. 444*e4b17023SJohn Marino 445*e4b17023SJohn MarinoFor each basic block, a line is printed after the last line of the basic 446*e4b17023SJohn Marinoblock describing the branch or call that ends the basic block. There can 447*e4b17023SJohn Marinobe multiple branches and calls listed for a single source line if there 448*e4b17023SJohn Marinoare multiple basic blocks that end on that line. In this case, the 449*e4b17023SJohn Marinobranches and calls are each given a number. There is no simple way to map 450*e4b17023SJohn Marinothese branches and calls back to source constructs. In general, though, 451*e4b17023SJohn Marinothe lowest numbered branch or call will correspond to the leftmost construct 452*e4b17023SJohn Marinoon the source line. 453*e4b17023SJohn Marino 454*e4b17023SJohn MarinoFor a branch, if it was executed at least once, then a percentage 455*e4b17023SJohn Marinoindicating the number of times the branch was taken divided by the 456*e4b17023SJohn Marinonumber of times the branch was executed will be printed. Otherwise, the 457*e4b17023SJohn Marinomessage ``never executed'' is printed. 458*e4b17023SJohn Marino 459*e4b17023SJohn MarinoFor a call, if it was executed at least once, then a percentage 460*e4b17023SJohn Marinoindicating the number of times the call returned divided by the number 461*e4b17023SJohn Marinoof times the call was executed will be printed. This will usually be 462*e4b17023SJohn Marino100%, but may be less for functions that call @code{exit} or @code{longjmp}, 463*e4b17023SJohn Marinoand thus may not return every time they are called. 464*e4b17023SJohn Marino 465*e4b17023SJohn MarinoThe execution counts are cumulative. If the example program were 466*e4b17023SJohn Marinoexecuted again without removing the @file{.gcda} file, the count for the 467*e4b17023SJohn Marinonumber of times each line in the source was executed would be added to 468*e4b17023SJohn Marinothe results of the previous run(s). This is potentially useful in 469*e4b17023SJohn Marinoseveral ways. For example, it could be used to accumulate data over a 470*e4b17023SJohn Marinonumber of program runs as part of a test verification suite, or to 471*e4b17023SJohn Marinoprovide more accurate long-term information over a large number of 472*e4b17023SJohn Marinoprogram runs. 473*e4b17023SJohn Marino 474*e4b17023SJohn MarinoThe data in the @file{.gcda} files is saved immediately before the program 475*e4b17023SJohn Marinoexits. For each source file compiled with @option{-fprofile-arcs}, the 476*e4b17023SJohn Marinoprofiling code first attempts to read in an existing @file{.gcda} file; if 477*e4b17023SJohn Marinothe file doesn't match the executable (differing number of basic block 478*e4b17023SJohn Marinocounts) it will ignore the contents of the file. It then adds in the 479*e4b17023SJohn Marinonew execution counts and finally writes the data to the file. 480*e4b17023SJohn Marino 481*e4b17023SJohn Marino@node Gcov and Optimization 482*e4b17023SJohn Marino@section Using @command{gcov} with GCC Optimization 483*e4b17023SJohn Marino 484*e4b17023SJohn MarinoIf you plan to use @command{gcov} to help optimize your code, you must 485*e4b17023SJohn Marinofirst compile your program with two special GCC options: 486*e4b17023SJohn Marino@samp{-fprofile-arcs -ftest-coverage}. Aside from that, you can use any 487*e4b17023SJohn Marinoother GCC options; but if you want to prove that every single line 488*e4b17023SJohn Marinoin your program was executed, you should not compile with optimization 489*e4b17023SJohn Marinoat the same time. On some machines the optimizer can eliminate some 490*e4b17023SJohn Marinosimple code lines by combining them with other lines. For example, code 491*e4b17023SJohn Marinolike this: 492*e4b17023SJohn Marino 493*e4b17023SJohn Marino@smallexample 494*e4b17023SJohn Marinoif (a != b) 495*e4b17023SJohn Marino c = 1; 496*e4b17023SJohn Marinoelse 497*e4b17023SJohn Marino c = 0; 498*e4b17023SJohn Marino@end smallexample 499*e4b17023SJohn Marino 500*e4b17023SJohn Marino@noindent 501*e4b17023SJohn Marinocan be compiled into one instruction on some machines. In this case, 502*e4b17023SJohn Marinothere is no way for @command{gcov} to calculate separate execution counts 503*e4b17023SJohn Marinofor each line because there isn't separate code for each line. Hence 504*e4b17023SJohn Marinothe @command{gcov} output looks like this if you compiled the program with 505*e4b17023SJohn Marinooptimization: 506*e4b17023SJohn Marino 507*e4b17023SJohn Marino@smallexample 508*e4b17023SJohn Marino 100: 12:if (a != b) 509*e4b17023SJohn Marino 100: 13: c = 1; 510*e4b17023SJohn Marino 100: 14:else 511*e4b17023SJohn Marino 100: 15: c = 0; 512*e4b17023SJohn Marino@end smallexample 513*e4b17023SJohn Marino 514*e4b17023SJohn MarinoThe output shows that this block of code, combined by optimization, 515*e4b17023SJohn Marinoexecuted 100 times. In one sense this result is correct, because there 516*e4b17023SJohn Marinowas only one instruction representing all four of these lines. However, 517*e4b17023SJohn Marinothe output does not indicate how many times the result was 0 and how 518*e4b17023SJohn Marinomany times the result was 1. 519*e4b17023SJohn Marino 520*e4b17023SJohn MarinoInlineable functions can create unexpected line counts. Line counts are 521*e4b17023SJohn Marinoshown for the source code of the inlineable function, but what is shown 522*e4b17023SJohn Marinodepends on where the function is inlined, or if it is not inlined at all. 523*e4b17023SJohn Marino 524*e4b17023SJohn MarinoIf the function is not inlined, the compiler must emit an out of line 525*e4b17023SJohn Marinocopy of the function, in any object file that needs it. If 526*e4b17023SJohn Marino@file{fileA.o} and @file{fileB.o} both contain out of line bodies of a 527*e4b17023SJohn Marinoparticular inlineable function, they will also both contain coverage 528*e4b17023SJohn Marinocounts for that function. When @file{fileA.o} and @file{fileB.o} are 529*e4b17023SJohn Marinolinked together, the linker will, on many systems, select one of those 530*e4b17023SJohn Marinoout of line bodies for all calls to that function, and remove or ignore 531*e4b17023SJohn Marinothe other. Unfortunately, it will not remove the coverage counters for 532*e4b17023SJohn Marinothe unused function body. Hence when instrumented, all but one use of 533*e4b17023SJohn Marinothat function will show zero counts. 534*e4b17023SJohn Marino 535*e4b17023SJohn MarinoIf the function is inlined in several places, the block structure in 536*e4b17023SJohn Marinoeach location might not be the same. For instance, a condition might 537*e4b17023SJohn Marinonow be calculable at compile time in some instances. Because the 538*e4b17023SJohn Marinocoverage of all the uses of the inline function will be shown for the 539*e4b17023SJohn Marinosame source lines, the line counts themselves might seem inconsistent. 540*e4b17023SJohn Marino 541*e4b17023SJohn Marino@c man end 542*e4b17023SJohn Marino 543*e4b17023SJohn Marino@node Gcov Data Files 544*e4b17023SJohn Marino@section Brief description of @command{gcov} data files 545*e4b17023SJohn Marino 546*e4b17023SJohn Marino@command{gcov} uses two files for profiling. The names of these files 547*e4b17023SJohn Marinoare derived from the original @emph{object} file by substituting the 548*e4b17023SJohn Marinofile suffix with either @file{.gcno}, or @file{.gcda}. All of these files 549*e4b17023SJohn Marinoare placed in the same directory as the object file, and contain data 550*e4b17023SJohn Marinostored in a platform-independent format. 551*e4b17023SJohn Marino 552*e4b17023SJohn MarinoThe @file{.gcno} file is generated when the source file is compiled with 553*e4b17023SJohn Marinothe GCC @option{-ftest-coverage} option. It contains information to 554*e4b17023SJohn Marinoreconstruct the basic block graphs and assign source line numbers to 555*e4b17023SJohn Marinoblocks. 556*e4b17023SJohn Marino 557*e4b17023SJohn MarinoThe @file{.gcda} file is generated when a program containing object files 558*e4b17023SJohn Marinobuilt with the GCC @option{-fprofile-arcs} option is executed. A 559*e4b17023SJohn Marinoseparate @file{.gcda} file is created for each object file compiled with 560*e4b17023SJohn Marinothis option. It contains arc transition counts, and some summary 561*e4b17023SJohn Marinoinformation. 562*e4b17023SJohn Marino 563*e4b17023SJohn MarinoThe full details of the file format is specified in @file{gcov-io.h}, 564*e4b17023SJohn Marinoand functions provided in that header file should be used to access the 565*e4b17023SJohn Marinocoverage files. 566*e4b17023SJohn Marino 567*e4b17023SJohn Marino@node Cross-profiling 568*e4b17023SJohn Marino@section Data file relocation to support cross-profiling 569*e4b17023SJohn Marino 570*e4b17023SJohn MarinoRunning the program will cause profile output to be generated. For each 571*e4b17023SJohn Marinosource file compiled with @option{-fprofile-arcs}, an accompanying @file{.gcda} 572*e4b17023SJohn Marinofile will be placed in the object file directory. That implicitly requires 573*e4b17023SJohn Marinorunning the program on the same system as it was built or having the same 574*e4b17023SJohn Marinoabsolute directory structure on the target system. The program will try 575*e4b17023SJohn Marinoto create the needed directory structure, if it is not already present. 576*e4b17023SJohn Marino 577*e4b17023SJohn MarinoTo support cross-profiling, a program compiled with @option{-fprofile-arcs} 578*e4b17023SJohn Marinocan relocate the data files based on two environment variables: 579*e4b17023SJohn Marino 580*e4b17023SJohn Marino@itemize @bullet 581*e4b17023SJohn Marino@item 582*e4b17023SJohn MarinoGCOV_PREFIX contains the prefix to add to the absolute paths 583*e4b17023SJohn Marinoin the object file. Prefix can be absolute, or relative. The 584*e4b17023SJohn Marinodefault is no prefix. 585*e4b17023SJohn Marino 586*e4b17023SJohn Marino@item 587*e4b17023SJohn MarinoGCOV_PREFIX_STRIP indicates the how many initial directory names to strip off 588*e4b17023SJohn Marinothe hardwired absolute paths. Default value is 0. 589*e4b17023SJohn Marino 590*e4b17023SJohn Marino@emph{Note:} If GCOV_PREFIX_STRIP is set without GCOV_PREFIX is undefined, 591*e4b17023SJohn Marino then a relative path is made out of the hardwired absolute paths. 592*e4b17023SJohn Marino@end itemize 593*e4b17023SJohn Marino 594*e4b17023SJohn MarinoFor example, if the object file @file{/user/build/foo.o} was built with 595*e4b17023SJohn Marino@option{-fprofile-arcs}, the final executable will try to create the data file 596*e4b17023SJohn Marino@file{/user/build/foo.gcda} when running on the target system. This will 597*e4b17023SJohn Marinofail if the corresponding directory does not exist and it is unable to create 598*e4b17023SJohn Marinoit. This can be overcome by, for example, setting the environment as 599*e4b17023SJohn Marino@samp{GCOV_PREFIX=/target/run} and @samp{GCOV_PREFIX_STRIP=1}. Such a 600*e4b17023SJohn Marinosetting will name the data file @file{/target/run/build/foo.gcda}. 601*e4b17023SJohn Marino 602*e4b17023SJohn MarinoYou must move the data files to the expected directory tree in order to 603*e4b17023SJohn Marinouse them for profile directed optimizations (@option{--use-profile}), or to 604*e4b17023SJohn Marinouse the @command{gcov} tool. 605