1@c Copyright (C) 1996-2015 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@ignore 6@c man begin COPYRIGHT 7Copyright @copyright{} 1996-2015 Free Software Foundation, Inc. 8 9Permission is granted to copy, distribute and/or modify this document 10under the terms of the GNU Free Documentation License, Version 1.3 or 11any later version published by the Free Software Foundation; with the 12Invariant Sections being ``GNU General Public License'' and ``Funding 13Free Software'', the Front-Cover texts being (a) (see below), and with 14the Back-Cover Texts being (b) (see below). A copy of the license is 15included in the gfdl(7) man page. 16 17(a) The FSF's Front-Cover Text is: 18 19 A GNU Manual 20 21(b) The FSF's Back-Cover Text is: 22 23 You have freedom to copy and modify this GNU Manual, like GNU 24 software. Copies published by the Free Software Foundation raise 25 funds for GNU development. 26@c man end 27@c Set file name and title for the man page. 28@setfilename gcov 29@settitle coverage testing tool 30@end ignore 31 32@node Gcov 33@chapter @command{gcov}---a Test Coverage Program 34 35@command{gcov} is a tool you can use in conjunction with GCC to 36test code coverage in your programs. 37 38@menu 39* Gcov Intro:: Introduction to gcov. 40* Invoking Gcov:: How to use gcov. 41* Gcov and Optimization:: Using gcov with GCC optimization. 42* Gcov Data Files:: The files used by gcov. 43* Cross-profiling:: Data file relocation. 44@end menu 45 46@node Gcov Intro 47@section Introduction to @command{gcov} 48@c man begin DESCRIPTION 49 50@command{gcov} is a test coverage program. Use it in concert with GCC 51to analyze your programs to help create more efficient, faster running 52code and to discover untested parts of your program. You can use 53@command{gcov} as a profiling tool to help discover where your 54optimization efforts will best affect your code. You can also use 55@command{gcov} along with the other profiling tool, @command{gprof}, to 56assess which parts of your code use the greatest amount of computing 57time. 58 59Profiling tools help you analyze your code's performance. Using a 60profiler such as @command{gcov} or @command{gprof}, you can find out some 61basic performance statistics, such as: 62 63@itemize @bullet 64@item 65how often each line of code executes 66 67@item 68what lines of code are actually executed 69 70@item 71how much computing time each section of code uses 72@end itemize 73 74Once you know these things about how your code works when compiled, you 75can look at each module to see which modules should be optimized. 76@command{gcov} helps you determine where to work on optimization. 77 78Software developers also use coverage testing in concert with 79testsuites, to make sure software is actually good enough for a release. 80Testsuites can verify that a program works as expected; a coverage 81program tests to see how much of the program is exercised by the 82testsuite. Developers can then determine what kinds of test cases need 83to be added to the testsuites to create both better testing and a better 84final product. 85 86You should compile your code without optimization if you plan to use 87@command{gcov} because the optimization, by combining some lines of code 88into one function, may not give you as much information as you need to 89look for `hot spots' where the code is using a great deal of computer 90time. Likewise, because @command{gcov} accumulates statistics by line (at 91the lowest resolution), it works best with a programming style that 92places only one statement on each line. If you use complicated macros 93that expand to loops or to other control structures, the statistics are 94less helpful---they only report on the line where the macro call 95appears. If your complex macros behave like functions, you can replace 96them with inline functions to solve this problem. 97 98@command{gcov} creates a logfile called @file{@var{sourcefile}.gcov} which 99indicates how many times each line of a source file @file{@var{sourcefile}.c} 100has executed. You can use these logfiles along with @command{gprof} to aid 101in fine-tuning the performance of your programs. @command{gprof} gives 102timing information you can use along with the information you get from 103@command{gcov}. 104 105@command{gcov} works only on code compiled with GCC@. It is not 106compatible with any other profiling or test coverage mechanism. 107 108@c man end 109 110@node Invoking Gcov 111@section Invoking @command{gcov} 112 113@smallexample 114gcov @r{[}@var{options}@r{]} @var{files} 115@end smallexample 116 117@command{gcov} accepts the following options: 118 119@ignore 120@c man begin SYNOPSIS 121gcov [@option{-v}|@option{--version}] [@option{-h}|@option{--help}] 122 [@option{-a}|@option{--all-blocks}] 123 [@option{-b}|@option{--branch-probabilities}] 124 [@option{-c}|@option{--branch-counts}] 125 [@option{-d}|@option{--display-progress}] 126 [@option{-f}|@option{--function-summaries}] 127 [@option{-i}|@option{--intermediate-format}] 128 [@option{-l}|@option{--long-file-names}] 129 [@option{-m}|@option{--demangled-names}] 130 [@option{-n}|@option{--no-output}] 131 [@option{-o}|@option{--object-directory} @var{directory|file}] 132 [@option{-p}|@option{--preserve-paths}] 133 [@option{-r}|@option{--relative-only}] 134 [@option{-s}|@option{--source-prefix} @var{directory}] 135 [@option{-u}|@option{--unconditional-branches}] 136 @var{files} 137@c man end 138@c man begin SEEALSO 139gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}. 140@c man end 141@end ignore 142 143@c man begin OPTIONS 144@table @gcctabopt 145 146@item -a 147@itemx --all-blocks 148Write individual execution counts for every basic block. Normally gcov 149outputs execution counts only for the main blocks of a line. With this 150option you can determine if blocks within a single line are not being 151executed. 152 153@item -b 154@itemx --branch-probabilities 155Write branch frequencies to the output file, and write branch summary 156info to the standard output. This option allows you to see how often 157each branch in your program was taken. Unconditional branches will not 158be shown, unless the @option{-u} option is given. 159 160@item -c 161@itemx --branch-counts 162Write branch frequencies as the number of branches taken, rather than 163the percentage of branches taken. 164 165@item -d 166@itemx --display-progress 167Display the progress on the standard output. 168 169@item -f 170@itemx --function-summaries 171Output summaries for each function in addition to the file level summary. 172 173@item -h 174@itemx --help 175Display help about using @command{gcov} (on the standard output), and 176exit without doing any further processing. 177 178@item -i 179@itemx --intermediate-format 180Output gcov file in an easy-to-parse intermediate text format that can 181be used by @command{lcov} or other tools. The output is a single 182@file{.gcov} file per @file{.gcda} file. No source code is required. 183 184The format of the intermediate @file{.gcov} file is plain text with 185one entry per line 186 187@smallexample 188file:@var{source_file_name} 189function:@var{line_number},@var{execution_count},@var{function_name} 190lcount:@var{line number},@var{execution_count} 191branch:@var{line_number},@var{branch_coverage_type} 192 193Where the @var{branch_coverage_type} is 194 notexec (Branch not executed) 195 taken (Branch executed and taken) 196 nottaken (Branch executed, but not taken) 197 198There can be multiple @var{file} entries in an intermediate gcov 199file. All entries following a @var{file} pertain to that source file 200until the next @var{file} entry. 201@end smallexample 202 203Here is a sample when @option{-i} is used in conjunction with @option{-b} option: 204 205@smallexample 206file:array.cc 207function:11,1,_Z3sumRKSt6vectorIPiSaIS0_EE 208function:22,1,main 209lcount:11,1 210lcount:12,1 211lcount:14,1 212branch:14,taken 213lcount:26,1 214branch:28,nottaken 215@end smallexample 216 217@item -l 218@itemx --long-file-names 219Create long file names for included source files. For example, if the 220header file @file{x.h} contains code, and was included in the file 221@file{a.c}, then running @command{gcov} on the file @file{a.c} will 222produce an output file called @file{a.c##x.h.gcov} instead of 223@file{x.h.gcov}. This can be useful if @file{x.h} is included in 224multiple source files and you want to see the individual 225contributions. If you use the @samp{-p} option, both the including 226and included file names will be complete path names. 227 228@item -m 229@itemx --demangled-names 230Display demangled function names in output. The default is to show 231mangled function names. 232 233@item -n 234@itemx --no-output 235Do not create the @command{gcov} output file. 236 237@item -o @var{directory|file} 238@itemx --object-directory @var{directory} 239@itemx --object-file @var{file} 240Specify either the directory containing the gcov data files, or the 241object path name. The @file{.gcno}, and 242@file{.gcda} data files are searched for using this option. If a directory 243is specified, the data files are in that directory and named after the 244input file name, without its extension. If a file is specified here, 245the data files are named after that file, without its extension. 246 247@item -p 248@itemx --preserve-paths 249Preserve complete path information in the names of generated 250@file{.gcov} files. Without this option, just the filename component is 251used. With this option, all directories are used, with @samp{/} characters 252translated to @samp{#} characters, @file{.} directory components 253removed and unremoveable @file{..} 254components renamed to @samp{^}. This is useful if sourcefiles are in several 255different directories. 256 257@item -r 258@itemx --relative-only 259Only output information about source files with a relative pathname 260(after source prefix elision). Absolute paths are usually system 261header files and coverage of any inline functions therein is normally 262uninteresting. 263 264@item -s @var{directory} 265@itemx --source-prefix @var{directory} 266A prefix for source file names to remove when generating the output 267coverage files. This option is useful when building in a separate 268directory, and the pathname to the source directory is not wanted when 269determining the output file names. Note that this prefix detection is 270applied before determining whether the source file is absolute. 271 272@item -u 273@itemx --unconditional-branches 274When branch probabilities are given, include those of unconditional branches. 275Unconditional branches are normally not interesting. 276 277@item -v 278@itemx --version 279Display the @command{gcov} version number (on the standard output), 280and exit without doing any further processing. 281 282@item -w 283@itemx --verbose 284Print verbose informations related to basic blocks and arcs. 285 286@item -x 287@itemx --hash-filenames 288By default, gcov uses the full pathname of the source files to to create 289an output filename. This can lead to long filenames that can overflow 290filesystem limits. This option creates names of the form 291@file{@var{source-file}##@var{md5}.gcov}, 292where the @var{source-file} component is the final filename part and 293the @var{md5} component is calculated from the full mangled name that 294would have been used otherwise. 295 296@end table 297 298@command{gcov} should be run with the current directory the same as that 299when you invoked the compiler. Otherwise it will not be able to locate 300the source files. @command{gcov} produces files called 301@file{@var{mangledname}.gcov} in the current directory. These contain 302the coverage information of the source file they correspond to. 303One @file{.gcov} file is produced for each source (or header) file 304containing code, 305which was compiled to produce the data files. The @var{mangledname} part 306of the output file name is usually simply the source file name, but can 307be something more complicated if the @samp{-l} or @samp{-p} options are 308given. Refer to those options for details. 309 310If you invoke @command{gcov} with multiple input files, the 311contributions from each input file are summed. Typically you would 312invoke it with the same list of files as the final link of your executable. 313 314The @file{.gcov} files contain the @samp{:} separated fields along with 315program source code. The format is 316 317@smallexample 318@var{execution_count}:@var{line_number}:@var{source line text} 319@end smallexample 320 321Additional block information may succeed each line, when requested by 322command line option. The @var{execution_count} is @samp{-} for lines 323containing no code. Unexecuted lines are marked @samp{#####} or 324@samp{====}, depending on whether they are reachable by 325non-exceptional paths or only exceptional paths such as C++ exception 326handlers, respectively. Given @samp{-a} option, unexecuted blocks are 327marked @samp{$$$$$} or @samp{%%%%%}, depending on whether a basic block 328is reachable via non-exceptional or exceptional paths. 329 330Some lines of information at the start have @var{line_number} of zero. 331These preamble lines are of the form 332 333@smallexample 334-:0:@var{tag}:@var{value} 335@end smallexample 336 337The ordering and number of these preamble lines will be augmented as 338@command{gcov} development progresses --- do not rely on them remaining 339unchanged. Use @var{tag} to locate a particular preamble line. 340 341The additional block information is of the form 342 343@smallexample 344@var{tag} @var{information} 345@end smallexample 346 347The @var{information} is human readable, but designed to be simple 348enough for machine parsing too. 349 350When printing percentages, 0% and 100% are only printed when the values 351are @emph{exactly} 0% and 100% respectively. Other values which would 352conventionally be rounded to 0% or 100% are instead printed as the 353nearest non-boundary value. 354 355When using @command{gcov}, you must first compile your program with two 356special GCC options: @samp{-fprofile-arcs -ftest-coverage}. 357This tells the compiler to generate additional information needed by 358gcov (basically a flow graph of the program) and also includes 359additional code in the object files for generating the extra profiling 360information needed by gcov. These additional files are placed in the 361directory where the object file is located. 362 363Running the program will cause profile output to be generated. For each 364source file compiled with @option{-fprofile-arcs}, an accompanying 365@file{.gcda} file will be placed in the object file directory. 366 367Running @command{gcov} with your program's source file names as arguments 368will now produce a listing of the code along with frequency of execution 369for each line. For example, if your program is called @file{tmp.c}, this 370is what you see when you use the basic @command{gcov} facility: 371 372@smallexample 373$ gcc -fprofile-arcs -ftest-coverage tmp.c 374$ a.out 375$ gcov tmp.c 37690.00% of 10 source lines executed in file tmp.c 377Creating tmp.c.gcov. 378@end smallexample 379 380The file @file{tmp.c.gcov} contains output from @command{gcov}. 381Here is a sample: 382 383@smallexample 384 -: 0:Source:tmp.c 385 -: 0:Graph:tmp.gcno 386 -: 0:Data:tmp.gcda 387 -: 0:Runs:1 388 -: 0:Programs:1 389 -: 1:#include <stdio.h> 390 -: 2: 391 -: 3:int main (void) 392 1: 4:@{ 393 1: 5: int i, total; 394 -: 6: 395 1: 7: total = 0; 396 -: 8: 397 11: 9: for (i = 0; i < 10; i++) 398 10: 10: total += i; 399 -: 11: 400 1: 12: if (total != 45) 401 #####: 13: printf ("Failure\n"); 402 -: 14: else 403 1: 15: printf ("Success\n"); 404 1: 16: return 0; 405 -: 17:@} 406@end smallexample 407 408When you use the @option{-a} option, you will get individual block 409counts, and the output looks like this: 410 411@smallexample 412 -: 0:Source:tmp.c 413 -: 0:Graph:tmp.gcno 414 -: 0:Data:tmp.gcda 415 -: 0:Runs:1 416 -: 0:Programs:1 417 -: 1:#include <stdio.h> 418 -: 2: 419 -: 3:int main (void) 420 1: 4:@{ 421 1: 4-block 0 422 1: 5: int i, total; 423 -: 6: 424 1: 7: total = 0; 425 -: 8: 426 11: 9: for (i = 0; i < 10; i++) 427 11: 9-block 0 428 10: 10: total += i; 429 10: 10-block 0 430 -: 11: 431 1: 12: if (total != 45) 432 1: 12-block 0 433 #####: 13: printf ("Failure\n"); 434 $$$$$: 13-block 0 435 -: 14: else 436 1: 15: printf ("Success\n"); 437 1: 15-block 0 438 1: 16: return 0; 439 1: 16-block 0 440 -: 17:@} 441@end smallexample 442 443In this mode, each basic block is only shown on one line -- the last 444line of the block. A multi-line block will only contribute to the 445execution count of that last line, and other lines will not be shown 446to contain code, unless previous blocks end on those lines. 447The total execution count of a line is shown and subsequent lines show 448the execution counts for individual blocks that end on that line. After each 449block, the branch and call counts of the block will be shown, if the 450@option{-b} option is given. 451 452Because of the way GCC instruments calls, a call count can be shown 453after a line with no individual blocks. 454As you can see, line 13 contains a basic block that was not executed. 455 456@need 450 457When you use the @option{-b} option, your output looks like this: 458 459@smallexample 460$ gcov -b tmp.c 46190.00% of 10 source lines executed in file tmp.c 46280.00% of 5 branches executed in file tmp.c 46380.00% of 5 branches taken at least once in file tmp.c 46450.00% of 2 calls executed in file tmp.c 465Creating tmp.c.gcov. 466@end smallexample 467 468Here is a sample of a resulting @file{tmp.c.gcov} file: 469 470@smallexample 471 -: 0:Source:tmp.c 472 -: 0:Graph:tmp.gcno 473 -: 0:Data:tmp.gcda 474 -: 0:Runs:1 475 -: 0:Programs:1 476 -: 1:#include <stdio.h> 477 -: 2: 478 -: 3:int main (void) 479function main called 1 returned 1 blocks executed 75% 480 1: 4:@{ 481 1: 5: int i, total; 482 -: 6: 483 1: 7: total = 0; 484 -: 8: 485 11: 9: for (i = 0; i < 10; i++) 486branch 0 taken 91% (fallthrough) 487branch 1 taken 9% 488 10: 10: total += i; 489 -: 11: 490 1: 12: if (total != 45) 491branch 0 taken 0% (fallthrough) 492branch 1 taken 100% 493 #####: 13: printf ("Failure\n"); 494call 0 never executed 495 -: 14: else 496 1: 15: printf ("Success\n"); 497call 0 called 1 returned 100% 498 1: 16: return 0; 499 -: 17:@} 500@end smallexample 501 502For each function, a line is printed showing how many times the function 503is called, how many times it returns and what percentage of the 504function's blocks were executed. 505 506For each basic block, a line is printed after the last line of the basic 507block describing the branch or call that ends the basic block. There can 508be multiple branches and calls listed for a single source line if there 509are multiple basic blocks that end on that line. In this case, the 510branches and calls are each given a number. There is no simple way to map 511these branches and calls back to source constructs. In general, though, 512the lowest numbered branch or call will correspond to the leftmost construct 513on the source line. 514 515For a branch, if it was executed at least once, then a percentage 516indicating the number of times the branch was taken divided by the 517number of times the branch was executed will be printed. Otherwise, the 518message ``never executed'' is printed. 519 520For a call, if it was executed at least once, then a percentage 521indicating the number of times the call returned divided by the number 522of times the call was executed will be printed. This will usually be 523100%, but may be less for functions that call @code{exit} or @code{longjmp}, 524and thus may not return every time they are called. 525 526The execution counts are cumulative. If the example program were 527executed again without removing the @file{.gcda} file, the count for the 528number of times each line in the source was executed would be added to 529the results of the previous run(s). This is potentially useful in 530several ways. For example, it could be used to accumulate data over a 531number of program runs as part of a test verification suite, or to 532provide more accurate long-term information over a large number of 533program runs. 534 535The data in the @file{.gcda} files is saved immediately before the program 536exits. For each source file compiled with @option{-fprofile-arcs}, the 537profiling code first attempts to read in an existing @file{.gcda} file; if 538the file doesn't match the executable (differing number of basic block 539counts) it will ignore the contents of the file. It then adds in the 540new execution counts and finally writes the data to the file. 541 542@node Gcov and Optimization 543@section Using @command{gcov} with GCC Optimization 544 545If you plan to use @command{gcov} to help optimize your code, you must 546first compile your program with two special GCC options: 547@samp{-fprofile-arcs -ftest-coverage}. Aside from that, you can use any 548other GCC options; but if you want to prove that every single line 549in your program was executed, you should not compile with optimization 550at the same time. On some machines the optimizer can eliminate some 551simple code lines by combining them with other lines. For example, code 552like this: 553 554@smallexample 555if (a != b) 556 c = 1; 557else 558 c = 0; 559@end smallexample 560 561@noindent 562can be compiled into one instruction on some machines. In this case, 563there is no way for @command{gcov} to calculate separate execution counts 564for each line because there isn't separate code for each line. Hence 565the @command{gcov} output looks like this if you compiled the program with 566optimization: 567 568@smallexample 569 100: 12:if (a != b) 570 100: 13: c = 1; 571 100: 14:else 572 100: 15: c = 0; 573@end smallexample 574 575The output shows that this block of code, combined by optimization, 576executed 100 times. In one sense this result is correct, because there 577was only one instruction representing all four of these lines. However, 578the output does not indicate how many times the result was 0 and how 579many times the result was 1. 580 581Inlineable functions can create unexpected line counts. Line counts are 582shown for the source code of the inlineable function, but what is shown 583depends on where the function is inlined, or if it is not inlined at all. 584 585If the function is not inlined, the compiler must emit an out of line 586copy of the function, in any object file that needs it. If 587@file{fileA.o} and @file{fileB.o} both contain out of line bodies of a 588particular inlineable function, they will also both contain coverage 589counts for that function. When @file{fileA.o} and @file{fileB.o} are 590linked together, the linker will, on many systems, select one of those 591out of line bodies for all calls to that function, and remove or ignore 592the other. Unfortunately, it will not remove the coverage counters for 593the unused function body. Hence when instrumented, all but one use of 594that function will show zero counts. 595 596If the function is inlined in several places, the block structure in 597each location might not be the same. For instance, a condition might 598now be calculable at compile time in some instances. Because the 599coverage of all the uses of the inline function will be shown for the 600same source lines, the line counts themselves might seem inconsistent. 601 602Long-running applications can use the @code{_gcov_reset} and @code{_gcov_dump} 603facilities to restrict profile collection to the program region of 604interest. Calling @code{_gcov_reset(void)} will clear all profile counters 605to zero, and calling @code{_gcov_dump(void)} will cause the profile information 606collected at that point to be dumped to @file{.gcda} output files. 607 608@c man end 609 610@node Gcov Data Files 611@section Brief Description of @command{gcov} Data Files 612 613@command{gcov} uses two files for profiling. The names of these files 614are derived from the original @emph{object} file by substituting the 615file suffix with either @file{.gcno}, or @file{.gcda}. The files 616contain coverage and profile data stored in a platform-independent format. 617The @file{.gcno} files are placed in the same directory as the object 618file. By default, the @file{.gcda} files are also stored in the same 619directory as the object file, but the GCC @option{-fprofile-dir} option 620may be used to store the @file{.gcda} files in a separate directory. 621 622The @file{.gcno} notes file is generated when the source file is compiled 623with the GCC @option{-ftest-coverage} option. It contains information to 624reconstruct the basic block graphs and assign source line numbers to 625blocks. 626 627The @file{.gcda} count data file is generated when a program containing 628object files built with the GCC @option{-fprofile-arcs} option is executed. 629A separate @file{.gcda} file is created for each object file compiled with 630this option. It contains arc transition counts, value profile counts, and 631some summary information. 632 633The full details of the file format is specified in @file{gcov-io.h}, 634and functions provided in that header file should be used to access the 635coverage files. 636 637@node Cross-profiling 638@section Data File Relocation to Support Cross-Profiling 639 640Running the program will cause profile output to be generated. For each 641source file compiled with @option{-fprofile-arcs}, an accompanying @file{.gcda} 642file will be placed in the object file directory. That implicitly requires 643running the program on the same system as it was built or having the same 644absolute directory structure on the target system. The program will try 645to create the needed directory structure, if it is not already present. 646 647To support cross-profiling, a program compiled with @option{-fprofile-arcs} 648can relocate the data files based on two environment variables: 649 650@itemize @bullet 651@item 652GCOV_PREFIX contains the prefix to add to the absolute paths 653in the object file. Prefix can be absolute, or relative. The 654default is no prefix. 655 656@item 657GCOV_PREFIX_STRIP indicates the how many initial directory names to strip off 658the hardwired absolute paths. Default value is 0. 659 660@emph{Note:} If GCOV_PREFIX_STRIP is set without GCOV_PREFIX is undefined, 661 then a relative path is made out of the hardwired absolute paths. 662@end itemize 663 664For example, if the object file @file{/user/build/foo.o} was built with 665@option{-fprofile-arcs}, the final executable will try to create the data file 666@file{/user/build/foo.gcda} when running on the target system. This will 667fail if the corresponding directory does not exist and it is unable to create 668it. This can be overcome by, for example, setting the environment as 669@samp{GCOV_PREFIX=/target/run} and @samp{GCOV_PREFIX_STRIP=1}. Such a 670setting will name the data file @file{/target/run/build/foo.gcda}. 671 672You must move the data files to the expected directory tree in order to 673use them for profile directed optimizations (@option{-fprofile-use}), or to 674use the @command{gcov} tool. 675