1@c Copyright (C) 1996, 1997, 1999, 2000, 2001, 2@c 2002, 2003 Free Software Foundation, Inc. 3@c This is part of the GCC manual. 4@c For copying conditions, see the file gcc.texi. 5 6@ignore 7@c man begin COPYRIGHT 8Copyright @copyright{} 1996, 1997, 1999, 2000, 2001, 2002, 2003 9Free Software Foundation, Inc. 10 11Permission is granted to copy, distribute and/or modify this document 12under the terms of the GNU Free Documentation License, Version 1.2 or 13any later version published by the Free Software Foundation; with the 14Invariant Sections being ``GNU General Public License'' and ``Funding 15Free Software'', the Front-Cover texts being (a) (see below), and with 16the Back-Cover Texts being (b) (see below). A copy of the license is 17included in the gfdl(7) man page. 18 19(a) The FSF's Front-Cover Text is: 20 21 A GNU Manual 22 23(b) The FSF's Back-Cover Text is: 24 25 You have freedom to copy and modify this GNU Manual, like GNU 26 software. Copies published by the Free Software Foundation raise 27 funds for GNU development. 28@c man end 29@c Set file name and title for the man page. 30@setfilename gcov 31@settitle coverage testing tool 32@end ignore 33 34@node Gcov 35@chapter @command{gcov}---a Test Coverage Program 36 37@command{gcov} is a tool you can use in conjunction with GCC to 38test code coverage in your programs. 39 40@menu 41* Gcov Intro:: Introduction to gcov. 42* Invoking Gcov:: How to use gcov. 43* Gcov and Optimization:: Using gcov with GCC optimization. 44* Gcov Data Files:: The files used by gcov. 45@end menu 46 47@node Gcov Intro 48@section Introduction to @command{gcov} 49@c man begin DESCRIPTION 50 51@command{gcov} is a test coverage program. Use it in concert with GCC 52to analyze your programs to help create more efficient, faster running 53code and to discover untested parts of your program. You can use 54@command{gcov} as a profiling tool to help discover where your 55optimization efforts will best affect your code. You can also use 56@command{gcov} along with the other profiling tool, @command{gprof}, to 57assess which parts of your code use the greatest amount of computing 58time. 59 60Profiling tools help you analyze your code's performance. Using a 61profiler such as @command{gcov} or @command{gprof}, you can find out some 62basic performance statistics, such as: 63 64@itemize @bullet 65@item 66how often each line of code executes 67 68@item 69what lines of code are actually executed 70 71@item 72how much computing time each section of code uses 73@end itemize 74 75Once you know these things about how your code works when compiled, you 76can look at each module to see which modules should be optimized. 77@command{gcov} helps you determine where to work on optimization. 78 79Software developers also use coverage testing in concert with 80testsuites, to make sure software is actually good enough for a release. 81Testsuites can verify that a program works as expected; a coverage 82program tests to see how much of the program is exercised by the 83testsuite. Developers can then determine what kinds of test cases need 84to be added to the testsuites to create both better testing and a better 85final product. 86 87You should compile your code without optimization if you plan to use 88@command{gcov} because the optimization, by combining some lines of code 89into one function, may not give you as much information as you need to 90look for `hot spots' where the code is using a great deal of computer 91time. Likewise, because @command{gcov} accumulates statistics by line (at 92the lowest resolution), it works best with a programming style that 93places only one statement on each line. If you use complicated macros 94that expand to loops or to other control structures, the statistics are 95less helpful---they only report on the line where the macro call 96appears. If your complex macros behave like functions, you can replace 97them with inline functions to solve this problem. 98 99@command{gcov} creates a logfile called @file{@var{sourcefile}.gcov} which 100indicates how many times each line of a source file @file{@var{sourcefile}.c} 101has executed. You can use these logfiles along with @command{gprof} to aid 102in fine-tuning the performance of your programs. @command{gprof} gives 103timing information you can use along with the information you get from 104@command{gcov}. 105 106@command{gcov} works only on code compiled with GCC@. It is not 107compatible with any other profiling or test coverage mechanism. 108 109@c man end 110 111@node Invoking Gcov 112@section Invoking gcov 113 114@smallexample 115gcov @r{[}@var{options}@r{]} @var{sourcefile} 116@end smallexample 117 118@command{gcov} accepts the following options: 119 120@ignore 121@c man begin SYNOPSIS 122gcov [@option{-v}|@option{--version}] [@option{-h}|@option{--help}] 123 [@option{-b}|@option{--branch-probabilities}] 124 [@option{-c}|@option{--branch-counts}] 125 [@option{-n}|@option{--no-output}] 126 [@option{-l}|@option{--long-file-names}] 127 [@option{-p}|@option{--preserve-paths}] 128 [@option{-f}|@option{--function-summaries}] 129 [@option{-o}|@option{--object-directory} @var{directory|file}] @var{sourcefile} 130@c man end 131@c man begin SEEALSO 132gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}. 133@c man end 134@end ignore 135 136@c man begin OPTIONS 137@table @gcctabopt 138@item -h 139@itemx --help 140Display help about using @command{gcov} (on the standard output), and 141exit without doing any further processing. 142 143@item -v 144@itemx --version 145Display the @command{gcov} version number (on the standard output), 146and exit without doing any further processing. 147 148@item -b 149@itemx --branch-probabilities 150Write branch frequencies to the output file, and write branch summary 151info to the standard output. This option allows you to see how often 152each branch in your program was taken. 153 154@item -c 155@itemx --branch-counts 156Write branch frequencies as the number of branches taken, rather than 157the percentage of branches taken. 158 159@item -n 160@itemx --no-output 161Do not create the @command{gcov} output file. 162 163@item -l 164@itemx --long-file-names 165Create long file names for included source files. For example, if the 166header file @file{x.h} contains code, and was included in the file 167@file{a.c}, then running @command{gcov} on the file @file{a.c} will produce 168an output file called @file{a.c##x.h.gcov} instead of @file{x.h.gcov}. 169This can be useful if @file{x.h} is included in multiple source files. 170 171@item -p 172@itemx --preserve-paths 173Preserve complete path information in the names of generated 174@file{.gcov} files. Without this option, just the filename component is 175used. With this option, all directories are used, with '/' characters 176translated to '#' characters, '.' directory components removed and '..' 177components renamed to '^'. This is useful if sourcefiles are in several 178different directories. It also affects the @samp{-l} option. 179 180@item -f 181@itemx --function-summaries 182Output summaries for each function in addition to the file level summary. 183 184@item -o @var{directory|file} 185@itemx --object-directory @var{directory} 186@itemx --object-file @var{file} 187Specify either the directory containing the gcov data files, or the 188object path name. The @file{.bb}, @file{.bbg}, and 189@file{.da} data files are searched for using this option. If a directory 190is specified, the data files are in that directory and named after the 191source file name, without its extension. If a file is specified here, 192the data files are named after that file, without its extension. If this 193option is not supplied, it defaults to the current directory. 194 195@end table 196 197@command{gcov} should be run with the current directory the same as that 198when you invoked the compiler. Otherwise it will not be able to locate 199the source files. @command{gcov} produces files called 200@file{@var{mangledname}.gcov} in the current directory. These contain 201the coverage information of the source file they correspond to. 202One @file{.gcov} file is produced for each source file containing code, 203which was compiled to produce the data files. The @file{.gcov} files 204contain the ':' separated fields along with program source code. The 205format is 206 207@smallexample 208@var{execution_count}:@var{line_number}:@var{source line text} 209@end smallexample 210 211Additional block information may succeed each line, when requested by 212command line option. The @var{execution_count} is @samp{-} for lines 213containing no code and @samp{#####} for lines which were never 214executed. Some lines of information at the start have @var{line_number} 215of zero. 216 217When printing percentages, 0% and 100% are only printed when the values 218are @emph{exactly} 0% and 100% respectively. Other values which would 219conventionally be rounded to 0% or 100% are instead printed as the 220nearest non-boundary value. 221 222When using @command{gcov}, you must first compile your program with two 223special GCC options: @samp{-fprofile-arcs -ftest-coverage}. 224This tells the compiler to generate additional information needed by 225gcov (basically a flow graph of the program) and also includes 226additional code in the object files for generating the extra profiling 227information needed by gcov. These additional files are placed in the 228directory where the object file is located. 229 230Running the program will cause profile output to be generated. For each 231source file compiled with @option{-fprofile-arcs}, an accompanying @file{.da} 232file will be placed in the object file directory. 233 234Running @command{gcov} with your program's source file names as arguments 235will now produce a listing of the code along with frequency of execution 236for each line. For example, if your program is called @file{tmp.c}, this 237is what you see when you use the basic @command{gcov} facility: 238 239@smallexample 240$ gcc -fprofile-arcs -ftest-coverage tmp.c 241$ a.out 242$ gcov tmp.c 24390.00% of 10 source lines executed in file tmp.c 244Creating tmp.c.gcov. 245@end smallexample 246 247The file @file{tmp.c.gcov} contains output from @command{gcov}. 248Here is a sample: 249 250@smallexample 251 -: 0:Source:tmp.c 252 -: 0:Object:tmp.bb 253 -: 1:#include <stdio.h> 254 -: 2: 255 -: 3:int main (void) 256 1: 4:@{ 257 1: 5: int i, total; 258 -: 6: 259 1: 7: total = 0; 260 -: 8: 261 11: 9: for (i = 0; i < 10; i++) 262 10: 10: total += i; 263 -: 11: 264 1: 12: if (total != 45) 265 #####: 13: printf ("Failure\n"); 266 -: 14: else 267 1: 15: printf ("Success\n"); 268 1: 16: return 0; 269 1: 17:@} 270@end smallexample 271 272@need 450 273When you use the @option{-b} option, your output looks like this: 274 275@smallexample 276$ gcov -b tmp.c 27790.00% of 10 source lines executed in file tmp.c 27880.00% of 5 branches executed in file tmp.c 27980.00% of 5 branches taken at least once in file tmp.c 28050.00% of 2 calls executed in file tmp.c 281Creating tmp.c.gcov. 282@end smallexample 283 284Here is a sample of a resulting @file{tmp.c.gcov} file: 285 286@smallexample 287 -: 0:Source:tmp.c 288 -: 0:Object:tmp.bb 289 -: 1:#include <stdio.h> 290 -: 2: 291 -: 3:int main (void) 292 1: 4:@{ 293 1: 5: int i, total; 294 -: 6: 295 1: 7: total = 0; 296 -: 8: 297 11: 9: for (i = 0; i < 10; i++) 298branch 0: taken 90% 299branch 1: taken 100% 300branch 2: taken 100% 301 10: 10: total += i; 302 -: 11: 303 1: 12: if (total != 45) 304branch 0: taken 100% 305 #####: 13: printf ("Failure\n"); 306call 0: never executed 307branch 1: never executed 308 -: 14: else 309 1: 15: printf ("Success\n"); 310call 0: returns 100% 311 1: 16: return 0; 312 1: 17:@} 313@end smallexample 314 315For each basic block, a line is printed after the last line of the basic 316block describing the branch or call that ends the basic block. There can 317be multiple branches and calls listed for a single source line if there 318are multiple basic blocks that end on that line. In this case, the 319branches and calls are each given a number. There is no simple way to map 320these branches and calls back to source constructs. In general, though, 321the lowest numbered branch or call will correspond to the leftmost construct 322on the source line. 323 324For a branch, if it was executed at least once, then a percentage 325indicating the number of times the branch was taken divided by the 326number of times the branch was executed will be printed. Otherwise, the 327message ``never executed'' is printed. 328 329For a call, if it was executed at least once, then a percentage 330indicating the number of times the call returned divided by the number 331of times the call was executed will be printed. This will usually be 332100%, but may be less for functions call @code{exit} or @code{longjmp}, 333and thus may not return every time they are called. 334 335The execution counts are cumulative. If the example program were 336executed again without removing the @file{.da} file, the count for the 337number of times each line in the source was executed would be added to 338the results of the previous run(s). This is potentially useful in 339several ways. For example, it could be used to accumulate data over a 340number of program runs as part of a test verification suite, or to 341provide more accurate long-term information over a large number of 342program runs. 343 344The data in the @file{.da} files is saved immediately before the program 345exits. For each source file compiled with @option{-fprofile-arcs}, the 346profiling code first attempts to read in an existing @file{.da} file; if 347the file doesn't match the executable (differing number of basic block 348counts) it will ignore the contents of the file. It then adds in the 349new execution counts and finally writes the data to the file. 350 351@node Gcov and Optimization 352@section Using @command{gcov} with GCC Optimization 353 354If you plan to use @command{gcov} to help optimize your code, you must 355first compile your program with two special GCC options: 356@samp{-fprofile-arcs -ftest-coverage}. Aside from that, you can use any 357other GCC options; but if you want to prove that every single line 358in your program was executed, you should not compile with optimization 359at the same time. On some machines the optimizer can eliminate some 360simple code lines by combining them with other lines. For example, code 361like this: 362 363@smallexample 364if (a != b) 365 c = 1; 366else 367 c = 0; 368@end smallexample 369 370@noindent 371can be compiled into one instruction on some machines. In this case, 372there is no way for @command{gcov} to calculate separate execution counts 373for each line because there isn't separate code for each line. Hence 374the @command{gcov} output looks like this if you compiled the program with 375optimization: 376 377@smallexample 378 100: 12:if (a != b) 379 100: 13: c = 1; 380 100: 14:else 381 100: 15: c = 0; 382@end smallexample 383 384The output shows that this block of code, combined by optimization, 385executed 100 times. In one sense this result is correct, because there 386was only one instruction representing all four of these lines. However, 387the output does not indicate how many times the result was 0 and how 388many times the result was 1. 389@c man end 390 391@node Gcov Data Files 392@section Brief description of @command{gcov} data files 393 394@command{gcov} uses three files for doing profiling. The names of these 395files are derived from the original @emph{source} file by substituting 396the file suffix with either @file{.bb}, @file{.bbg}, or @file{.da}. All 397of these files are placed in the same directory as the source file, and 398contain data stored in a platform-independent method. 399 400The @file{.bb} and @file{.bbg} files are generated when the source file 401is compiled with the GCC @option{-ftest-coverage} option. The 402@file{.bb} file contains a list of source files (including headers), 403functions within those files, and line numbers corresponding to each 404basic block in the source file. 405 406The @file{.bb} file format consists of several lists of 4-byte integers 407which correspond to the line numbers of each basic block in the file. 408Each list is terminated by a line number of 0. A line number of 409@minus{}1 is used to designate that the source file name (padded to a 4104-byte boundary and followed by another @minus{}1) follows. In 411addition, a line number of @minus{}2 is used to designate that the name 412of a function (also padded to a 4-byte boundary and followed by a 413@minus{}2) follows. 414 415The @file{.bbg} file is used to reconstruct the program flow graph for 416the source file. It contains a list of the program flow arcs (possible 417branches taken from one basic block to another) for each function which, 418in combination with the @file{.bb} file, enables gcov to reconstruct the 419program flow. 420 421In the @file{.bbg} file, the format is: 422@smallexample 423 name of function #0 424 checksum of function #0 425 number of basic blocks for function #0 (4-byte number) 426 total number of arcs for function #0 (4-byte number) 427 count of arcs in basic block #0 (4-byte number) 428 destination basic block of arc #0 (4-byte number) 429 flag bits (4-byte number) 430 destination basic block of arc #1 (4-byte number) 431 flag bits (4-byte number) 432 @dots{} 433 destination basic block of arc #N (4-byte number) 434 flag bits (4-byte number) 435 count of arcs in basic block #1 (4-byte number) 436 destination basic block of arc #0 (4-byte number) 437 flag bits (4-byte number) 438 @dots{} 439@end smallexample 440 441A @minus{}1 (stored as a 4-byte number) is used to separate each function's 442list of basic blocks, and to verify that the file has been read 443correctly. 444 445The function name is stored as a @minus{}1 (4 bytes), the length (4 bytes), 446the name itself (padded to 4-byte boundary) followed by a @minus{}1 (4 bytes). 447 448The flags are defined as follows: 449@itemize 450@item bit0 451On function spanning tree 452 453@item bit1 454Is a fake edge 455 456@item bit2 457Is the fall through edge from one block to its immediate successor. 458 459@item bit3-bit31 460For future expansion 461 462@end itemize 463 464The @file{.da} file is generated when a program containing object files 465built with the GCC @option{-fprofile-arcs} option is executed. A 466separate @file{.da} file is created for each source file compiled with 467this option, and the name of the @file{.da} file is stored as an 468absolute pathname in the resulting object file. This path name is 469derived from the object file name by substituting a @file{.da} suffix. 470 471The @file{.da} consists of one or more blocks with the following 472structure: 473@smallexample 474 "magic" number @minus{}123 (4-byte number) 475 number of functions (4-byte number) 476 length of the "extension block" in bytes 477 extension block (variable length) 478 name of function #0 (the same format as in .bbg file) 479 checksum of function #0 480 number of instrumented arcs (4-byte number) 481 count of arc #0 (8-byte number) 482 count of arc #1 (8-byte number) 483 @dots{} 484 count of arc #M_0 (8-byte number) 485 name of function #1 (the same format as in .bbg file) 486 checksum of function #1 487 @dots{} 488@end smallexample 489Multiple program runs might merge data into a single block, or might 490append a new block. The current structure of the extension block is as 491follows: 492@smallexample 493 number of instrumented arcs in whole program (4-byte number) 494 sum all of instrumented arcs in whole program (8-byte number) 495 maximal value of counter in whole program (8-byte number) 496 number of instrumented arcs in the object file (4-byte number) 497 sum all of instrumented arcs in the object file (8-byte number) 498 maximal value of counter in the object file (8-byte number) 499@end smallexample 500 501All three of these files use the functions in @file{gcov-io.h} to store 502integers; the functions in this header provide a machine-independent 503mechanism for storing and retrieving data from a stream. 504 505