1@c Copyright (C) 1996, 1997 Free Software Foundation, Inc. 2@c This is part of the GCC manual. 3@c For copying conditions, see the file gcc.texi. 4 5@node Gcov 6@chapter @code{gcov}: a Test Coverage Program 7 8@code{gcov} is a tool you can use in conjunction with @sc{gnu} CC to 9test code coverage in your programs. 10 11This chapter describes version 1.5 of @code{gcov}. 12 13@menu 14* Gcov Intro:: Introduction to gcov. 15* Invoking Gcov:: How to use gcov. 16* Gcov and Optimization:: Using gcov with GCC optimization. 17* Gcov Data Files:: The files used by gcov. 18@end menu 19 20@node Gcov Intro 21@section Introduction to @code{gcov} 22 23@code{gcov} is a test coverage program. Use it in concert with @sc{gnu} 24CC to analyze your programs to help create more efficient, faster 25running code. You can use @code{gcov} as a profiling tool to help 26discover where your optimization efforts will best affect your code. You 27can also use @code{gcov} along with the other profiling tool, 28@code{gprof}, to assess which parts of your code use the greatest amount 29of computing time. 30 31Profiling tools help you analyze your code's performance. Using a 32profiler such as @code{gcov} or @code{gprof}, you can find out some 33basic performance statistics, such as: 34 35@itemize @bullet 36@item 37how often each line of code executes 38 39@item 40what lines of code are actually executed 41 42@item 43how much computing time each section of code uses 44@end itemize 45 46Once you know these things about how your code works when compiled, you 47can look at each module to see which modules should be optimized. 48@code{gcov} helps you determine where to work on optimization. 49 50Software developers also use coverage testing in concert with 51testsuites, to make sure software is actually good enough for a release. 52Testsuites can verify that a program works as expected; a coverage 53program tests to see how much of the program is exercised by the 54testsuite. Developers can then determine what kinds of test cases need 55to be added to the testsuites to create both better testing and a better 56final product. 57 58You should compile your code without optimization if you plan to use 59@code{gcov} because the optimization, by combining some lines of code 60into one function, may not give you as much information as you need to 61look for `hot spots' where the code is using a great deal of computer 62time. Likewise, because @code{gcov} accumulates statistics by line (at 63the lowest resolution), it works best with a programming style that 64places only one statement on each line. If you use complicated macros 65that expand to loops or to other control structures, the statistics are 66less helpful---they only report on the line where the macro call 67appears. If your complex macros behave like functions, you can replace 68them with inline functions to solve this problem. 69 70@code{gcov} creates a logfile called @file{@var{sourcefile}.gcov} which 71indicates how many times each line of a source file @file{@var{sourcefile}.c} 72has executed. You can use these logfiles along with @code{gprof} to aid 73in fine-tuning the performance of your programs. @code{gprof} gives 74timing information you can use along with the information you get from 75@code{gcov}. 76 77@code{gcov} works only on code compiled with @sc{gnu} CC. It is not 78compatible with any other profiling or test coverage mechanism. 79 80@node Invoking Gcov 81@section Invoking gcov 82 83@smallexample 84gcov [-b] [-v] [-n] [-l] [-f] [-o directory] @var{sourcefile} 85@end smallexample 86 87@table @code 88@item -b 89Write branch frequencies to the output file, and write branch summary 90info to the standard output. This option allows you to see how often 91each branch in your program was taken. 92 93@item -v 94Display the @code{gcov} version number (on the standard error stream). 95 96@item -n 97Do not create the @code{gcov} output file. 98 99@item -l 100Create long file names for included source files. For example, if the 101header file @samp{x.h} contains code, and was included in the file 102@samp{a.c}, then running @code{gcov} on the file @samp{a.c} will produce 103an output file called @samp{a.c.x.h.gcov} instead of @samp{x.h.gcov}. 104This can be useful if @samp{x.h} is included in multiple source files. 105 106@item -f 107Output summaries for each function in addition to the file level summary. 108 109@item -o 110The directory where the object files live. Gcov will search for @code{.bb}, 111@code{.bbg}, and @code{.da} files in this directory. 112@end table 113 114@need 3000 115When using @code{gcov}, you must first compile your program with two 116special @sc{gnu} CC options: @samp{-fprofile-arcs -ftest-coverage}. 117This tells the compiler to generate additional information needed by 118gcov (basically a flow graph of the program) and also includes 119additional code in the object files for generating the extra profiling 120information needed by gcov. These additional files are placed in the 121directory where the source code is located. 122 123Running the program will cause profile output to be generated. For each 124source file compiled with -fprofile-arcs, an accompanying @code{.da} 125file will be placed in the source directory. 126 127Running @code{gcov} with your program's source file names as arguments 128will now produce a listing of the code along with frequency of execution 129for each line. For example, if your program is called @samp{tmp.c}, this 130is what you see when you use the basic @code{gcov} facility: 131 132@smallexample 133$ gcc -fprofile-arcs -ftest-coverage tmp.c 134$ a.out 135$ gcov tmp.c 136 87.50% of 8 source lines executed in file tmp.c 137Creating tmp.c.gcov. 138@end smallexample 139 140The file @file{tmp.c.gcov} contains output from @code{gcov}. 141Here is a sample: 142 143@smallexample 144 main() 145 @{ 146 1 int i, total; 147 148 1 total = 0; 149 150 11 for (i = 0; i < 10; i++) 151 10 total += i; 152 153 1 if (total != 45) 154 ###### printf ("Failure\n"); 155 else 156 1 printf ("Success\n"); 157 1 @} 158@end smallexample 159 160@need 450 161When you use the @samp{-b} option, your output looks like this: 162 163@smallexample 164$ gcov -b tmp.c 165 87.50% of 8 source lines executed in file tmp.c 166 80.00% of 5 branches executed in file tmp.c 167 80.00% of 5 branches taken at least once in file tmp.c 168 50.00% of 2 calls executed in file tmp.c 169Creating tmp.c.gcov. 170@end smallexample 171 172Here is a sample of a resulting @file{tmp.c.gcov} file: 173 174@smallexample 175 main() 176 @{ 177 1 int i, total; 178 179 1 total = 0; 180 181 11 for (i = 0; i < 10; i++) 182branch 0 taken = 91% 183branch 1 taken = 100% 184branch 2 taken = 100% 185 10 total += i; 186 187 1 if (total != 45) 188branch 0 taken = 100% 189 ###### printf ("Failure\n"); 190call 0 never executed 191branch 1 never executed 192 else 193 1 printf ("Success\n"); 194call 0 returns = 100% 195 1 @} 196@end smallexample 197 198For each basic block, a line is printed after the last line of the basic 199block describing the branch or call that ends the basic block. There can 200be multiple branches and calls listed for a single source line if there 201are multiple basic blocks that end on that line. In this case, the 202branches and calls are each given a number. There is no simple way to map 203these branches and calls back to source constructs. In general, though, 204the lowest numbered branch or call will correspond to the leftmost construct 205on the source line. 206 207For a branch, if it was executed at least once, then a percentage 208indicating the number of times the branch was taken divided by the 209number of times the branch was executed will be printed. Otherwise, the 210message ``never executed'' is printed. 211 212For a call, if it was executed at least once, then a percentage 213indicating the number of times the call returned divided by the number 214of times the call was executed will be printed. This will usually be 215100%, but may be less for functions call @code{exit} or @code{longjmp}, 216and thus may not return everytime they are called. 217 218The execution counts are cumulative. If the example program were 219executed again without removing the @code{.da} file, the count for the 220number of times each line in the source was executed would be added to 221the results of the previous run(s). This is potentially useful in 222several ways. For example, it could be used to accumulate data over a 223number of program runs as part of a test verification suite, or to 224provide more accurate long-term information over a large number of 225program runs. 226 227The data in the @code{.da} files is saved immediately before the program 228exits. For each source file compiled with -fprofile-arcs, the profiling 229code first attempts to read in an existing @code{.da} file; if the file 230doesn't match the executable (differing number of basic block counts) it 231will ignore the contents of the file. It then adds in the new execution 232counts and finally writes the data to the file. 233 234@node Gcov and Optimization 235@section Using @code{gcov} with GCC Optimization 236 237If you plan to use @code{gcov} to help optimize your code, you must 238first compile your program with two special @sc{gnu} CC options: 239@samp{-fprofile-arcs -ftest-coverage}. Aside from that, you can use any 240other @sc{gnu} CC options; but if you want to prove that every single line 241in your program was executed, you should not compile with optimization 242at the same time. On some machines the optimizer can eliminate some 243simple code lines by combining them with other lines. For example, code 244like this: 245 246@smallexample 247if (a != b) 248 c = 1; 249else 250 c = 0; 251@end smallexample 252 253@noindent 254can be compiled into one instruction on some machines. In this case, 255there is no way for @code{gcov} to calculate separate execution counts 256for each line because there isn't separate code for each line. Hence 257the @code{gcov} output looks like this if you compiled the program with 258optimization: 259 260@smallexample 261 100 if (a != b) 262 100 c = 1; 263 100 else 264 100 c = 0; 265@end smallexample 266 267The output shows that this block of code, combined by optimization, 268executed 100 times. In one sense this result is correct, because there 269was only one instruction representing all four of these lines. However, 270the output does not indicate how many times the result was 0 and how 271many times the result was 1. 272 273@node Gcov Data Files 274@section Brief description of @code{gcov} data files 275 276@code{gcov} uses three files for doing profiling. The names of these 277files are derived from the original @emph{source} file by substituting 278the file suffix with either @code{.bb}, @code{.bbg}, or @code{.da}. All 279of these files are placed in the same directory as the source file, and 280contain data stored in a platform-independent method. 281 282The @code{.bb} and @code{.bbg} files are generated when the source file 283is compiled with the @sc{gnu} CC @samp{-ftest-coverage} option. The 284@code{.bb} file contains a list of source files (including headers), 285functions within those files, and line numbers corresponding to each 286basic block in the source file. 287 288The @code{.bb} file format consists of several lists of 4-byte integers 289which correspond to the line numbers of each basic block in the 290file. Each list is terminated by a line number of 0. A line number of -1 291is used to designate that the source file name (padded to a 4-byte 292boundary and followed by another -1) follows. In addition, a line number 293of -2 is used to designate that the name of a function (also padded to a 2944-byte boundary and followed by a -2) follows. 295 296The @code{.bbg} file is used to reconstruct the program flow graph for 297the source file. It contains a list of the program flow arcs (possible 298branches taken from one basic block to another) for each function which, 299in combination with the @code{.bb} file, enables gcov to reconstruct the 300program flow. 301 302In the @code{.bbg} file, the format is: 303@smallexample 304 number of basic blocks for function #0 (4-byte number) 305 total number of arcs for function #0 (4-byte number) 306 count of arcs in basic block #0 (4-byte number) 307 destination basic block of arc #0 (4-byte number) 308 flag bits (4-byte number) 309 destination basic block of arc #1 (4-byte number) 310 flag bits (4-byte number) 311 ... 312 destination basic block of arc #N (4-byte number) 313 flag bits (4-byte number) 314 count of arcs in basic block #1 (4-byte number) 315 destination basic block of arc #0 (4-byte number) 316 flag bits (4-byte number) 317 ... 318@end smallexample 319 320A -1 (stored as a 4-byte number) is used to separate each function's 321list of basic blocks, and to verify that the file has been read 322correctly. 323 324The @code{.da} file is generated when a program containing object files 325built with the @sc{gnu} CC @samp{-fprofile-arcs} option is executed. A 326separate @code{.da} file is created for each source file compiled with 327this option, and the name of the @code{.da} file is stored as an 328absolute pathname in the resulting object file. This path name is 329derived from the source file name by substituting a @code{.da} suffix. 330 331The format of the @code{.da} file is fairly simple. The first 8-byte 332number is the number of counts in the file, followed by the counts 333(stored as 8-byte numbers). Each count corresponds to the number of 334times each arc in the program is executed. The counts are cumulative; 335each time the program is executed, it attemps to combine the existing 336@code{.da} files with the new counts for this invocation of the 337program. It ignores the contents of any @code{.da} files whose number of 338arcs doesn't correspond to the current program, and merely overwrites 339them instead. 340 341All three of these files use the functions in @code{gcov-io.h} to store 342integers; the functions in this header provide a machine-independent 343mechanism for storing and retrieving data from a stream. 344 345