1@c Copyright (C) 1996-2020 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-2020 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{--json-format}] 128 [@option{-j}|@option{--human-readable}] 129 [@option{-k}|@option{--use-colors}] 130 [@option{-l}|@option{--long-file-names}] 131 [@option{-m}|@option{--demangled-names}] 132 [@option{-n}|@option{--no-output}] 133 [@option{-o}|@option{--object-directory} @var{directory|file}] 134 [@option{-p}|@option{--preserve-paths}] 135 [@option{-q}|@option{--use-hotness-colors}] 136 [@option{-r}|@option{--relative-only}] 137 [@option{-s}|@option{--source-prefix} @var{directory}] 138 [@option{-t}|@option{--stdout}] 139 [@option{-u}|@option{--unconditional-branches}] 140 [@option{-x}|@option{--hash-filenames}] 141 @var{files} 142@c man end 143@c man begin SEEALSO 144gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}. 145@c man end 146@end ignore 147 148@c man begin OPTIONS 149@table @gcctabopt 150 151@item -a 152@itemx --all-blocks 153Write individual execution counts for every basic block. Normally gcov 154outputs execution counts only for the main blocks of a line. With this 155option you can determine if blocks within a single line are not being 156executed. 157 158@item -b 159@itemx --branch-probabilities 160Write branch frequencies to the output file, and write branch summary 161info to the standard output. This option allows you to see how often 162each branch in your program was taken. Unconditional branches will not 163be shown, unless the @option{-u} option is given. 164 165@item -c 166@itemx --branch-counts 167Write branch frequencies as the number of branches taken, rather than 168the percentage of branches taken. 169 170@item -d 171@itemx --display-progress 172Display the progress on the standard output. 173 174@item -f 175@itemx --function-summaries 176Output summaries for each function in addition to the file level summary. 177 178@item -h 179@itemx --help 180Display help about using @command{gcov} (on the standard output), and 181exit without doing any further processing. 182 183@item -i 184@itemx --json-format 185Output gcov file in an easy-to-parse JSON intermediate format 186which does not require source code for generation. The JSON 187file is compressed with gzip compression algorithm 188and the files have @file{.gcov.json.gz} extension. 189 190Structure of the JSON is following: 191 192@smallexample 193@{ 194 "current_working_directory": @var{current_working_directory}, 195 "data_file": @var{data_file}, 196 "format_version": @var{format_version}, 197 "gcc_version": @var{gcc_version} 198 "files": [@var{file}] 199@} 200@end smallexample 201 202Fields of the root element have following semantics: 203 204@itemize @bullet 205@item 206@var{current_working_directory}: working directory where 207a compilation unit was compiled 208 209@item 210@var{data_file}: name of the data file (GCDA) 211 212@item 213@var{format_version}: semantic version of the format 214 215@item 216@var{gcc_version}: version of the GCC compiler 217@end itemize 218 219Each @var{file} has the following form: 220 221@smallexample 222@{ 223 "file": @var{file_name}, 224 "functions": [@var{function}], 225 "lines": [@var{line}] 226@} 227@end smallexample 228 229Fields of the @var{file} element have following semantics: 230 231@itemize @bullet 232@item 233@var{file_name}: name of the source file 234@end itemize 235 236Each @var{function} has the following form: 237 238@smallexample 239@{ 240 "blocks": @var{blocks}, 241 "blocks_executed": @var{blocks_executed}, 242 "demangled_name": "@var{demangled_name}, 243 "end_column": @var{end_column}, 244 "end_line": @var{end_line}, 245 "execution_count": @var{execution_count}, 246 "name": @var{name}, 247 "start_column": @var{start_column} 248 "start_line": @var{start_line} 249@} 250@end smallexample 251 252Fields of the @var{function} element have following semantics: 253 254@itemize @bullet 255@item 256@var{blocks}: number of blocks that are in the function 257 258@item 259@var{blocks_executed}: number of executed blocks of the function 260 261@item 262@var{demangled_name}: demangled name of the function 263 264@item 265@var{end_column}: column in the source file where the function ends 266 267@item 268@var{end_line}: line in the source file where the function ends 269 270@item 271@var{execution_count}: number of executions of the function 272 273@item 274@var{name}: name of the function 275 276@item 277@var{start_column}: column in the source file where the function begins 278 279@item 280@var{start_line}: line in the source file where the function begins 281@end itemize 282 283Note that line numbers and column numbers number from 1. In the current 284implementation, @var{start_line} and @var{start_column} do not include 285any template parameters and the leading return type but that 286this is likely to be fixed in the future. 287 288Each @var{line} has the following form: 289 290@smallexample 291@{ 292 "branches": [@var{branch}], 293 "count": @var{count}, 294 "line_number": @var{line_number}, 295 "unexecuted_block": @var{unexecuted_block} 296 "function_name": @var{function_name}, 297@} 298@end smallexample 299 300Branches are present only with @var{-b} option. 301Fields of the @var{line} element have following semantics: 302 303@itemize @bullet 304@item 305@var{count}: number of executions of the line 306 307@item 308@var{line_number}: line number 309 310@item 311@var{unexecuted_block}: flag whether the line contains an unexecuted block 312(not all statements on the line are executed) 313 314@item 315@var{function_name}: a name of a function this @var{line} belongs to 316(for a line with an inlined statements can be not set) 317@end itemize 318 319Each @var{branch} has the following form: 320 321@smallexample 322@{ 323 "count": @var{count}, 324 "fallthrough": @var{fallthrough}, 325 "throw": @var{throw} 326@} 327@end smallexample 328 329Fields of the @var{branch} element have following semantics: 330 331@itemize @bullet 332@item 333@var{count}: number of executions of the branch 334 335@item 336@var{fallthrough}: true when the branch is a fall through branch 337 338@item 339@var{throw}: true when the branch is an exceptional branch 340@end itemize 341 342@item -j 343@itemx --human-readable 344Write counts in human readable format (like 24.6k). 345 346@item -k 347@itemx --use-colors 348 349Use colors for lines of code that have zero coverage. We use red color for 350non-exceptional lines and cyan for exceptional. Same colors are used for 351basic blocks with @option{-a} option. 352 353@item -l 354@itemx --long-file-names 355Create long file names for included source files. For example, if the 356header file @file{x.h} contains code, and was included in the file 357@file{a.c}, then running @command{gcov} on the file @file{a.c} will 358produce an output file called @file{a.c##x.h.gcov} instead of 359@file{x.h.gcov}. This can be useful if @file{x.h} is included in 360multiple source files and you want to see the individual 361contributions. If you use the @samp{-p} option, both the including 362and included file names will be complete path names. 363 364@item -m 365@itemx --demangled-names 366Display demangled function names in output. The default is to show 367mangled function names. 368 369@item -n 370@itemx --no-output 371Do not create the @command{gcov} output file. 372 373@item -o @var{directory|file} 374@itemx --object-directory @var{directory} 375@itemx --object-file @var{file} 376Specify either the directory containing the gcov data files, or the 377object path name. The @file{.gcno}, and 378@file{.gcda} data files are searched for using this option. If a directory 379is specified, the data files are in that directory and named after the 380input file name, without its extension. If a file is specified here, 381the data files are named after that file, without its extension. 382 383@item -p 384@itemx --preserve-paths 385Preserve complete path information in the names of generated 386@file{.gcov} files. Without this option, just the filename component is 387used. With this option, all directories are used, with @samp{/} characters 388translated to @samp{#} characters, @file{.} directory components 389removed and unremoveable @file{..} 390components renamed to @samp{^}. This is useful if sourcefiles are in several 391different directories. 392 393@item -q 394@itemx --use-hotness-colors 395 396Emit perf-like colored output for hot lines. Legend of the color scale 397is printed at the very beginning of the output file. 398 399@item -r 400@itemx --relative-only 401Only output information about source files with a relative pathname 402(after source prefix elision). Absolute paths are usually system 403header files and coverage of any inline functions therein is normally 404uninteresting. 405 406@item -s @var{directory} 407@itemx --source-prefix @var{directory} 408A prefix for source file names to remove when generating the output 409coverage files. This option is useful when building in a separate 410directory, and the pathname to the source directory is not wanted when 411determining the output file names. Note that this prefix detection is 412applied before determining whether the source file is absolute. 413 414@item -t 415@itemx --stdout 416Output to standard output instead of output files. 417 418@item -u 419@itemx --unconditional-branches 420When branch probabilities are given, include those of unconditional branches. 421Unconditional branches are normally not interesting. 422 423@item -v 424@itemx --version 425Display the @command{gcov} version number (on the standard output), 426and exit without doing any further processing. 427 428@item -w 429@itemx --verbose 430Print verbose informations related to basic blocks and arcs. 431 432@item -x 433@itemx --hash-filenames 434When using @var{--preserve-paths}, 435gcov uses the full pathname of the source files to create 436an output filename. This can lead to long filenames that can overflow 437filesystem limits. This option creates names of the form 438@file{@var{source-file}##@var{md5}.gcov}, 439where the @var{source-file} component is the final filename part and 440the @var{md5} component is calculated from the full mangled name that 441would have been used otherwise. The option is an alternative 442to the @var{--preserve-paths} on systems which have a filesystem limit. 443 444@end table 445 446@command{gcov} should be run with the current directory the same as that 447when you invoked the compiler. Otherwise it will not be able to locate 448the source files. @command{gcov} produces files called 449@file{@var{mangledname}.gcov} in the current directory. These contain 450the coverage information of the source file they correspond to. 451One @file{.gcov} file is produced for each source (or header) file 452containing code, 453which was compiled to produce the data files. The @var{mangledname} part 454of the output file name is usually simply the source file name, but can 455be something more complicated if the @samp{-l} or @samp{-p} options are 456given. Refer to those options for details. 457 458If you invoke @command{gcov} with multiple input files, the 459contributions from each input file are summed. Typically you would 460invoke it with the same list of files as the final link of your executable. 461 462The @file{.gcov} files contain the @samp{:} separated fields along with 463program source code. The format is 464 465@smallexample 466@var{execution_count}:@var{line_number}:@var{source line text} 467@end smallexample 468 469Additional block information may succeed each line, when requested by 470command line option. The @var{execution_count} is @samp{-} for lines 471containing no code. Unexecuted lines are marked @samp{#####} or 472@samp{=====}, depending on whether they are reachable by 473non-exceptional paths or only exceptional paths such as C++ exception 474handlers, respectively. Given the @samp{-a} option, unexecuted blocks are 475marked @samp{$$$$$} or @samp{%%%%%}, depending on whether a basic block 476is reachable via non-exceptional or exceptional paths. 477Executed basic blocks having a statement with zero @var{execution_count} 478end with @samp{*} character and are colored with magenta color with 479the @option{-k} option. This functionality is not supported in Ada. 480 481Note that GCC can completely remove the bodies of functions that are 482not needed -- for instance if they are inlined everywhere. Such functions 483are marked with @samp{-}, which can be confusing. 484Use the @option{-fkeep-inline-functions} and @option{-fkeep-static-functions} 485options to retain these functions and 486allow gcov to properly show their @var{execution_count}. 487 488Some lines of information at the start have @var{line_number} of zero. 489These preamble lines are of the form 490 491@smallexample 492-:0:@var{tag}:@var{value} 493@end smallexample 494 495The ordering and number of these preamble lines will be augmented as 496@command{gcov} development progresses --- do not rely on them remaining 497unchanged. Use @var{tag} to locate a particular preamble line. 498 499The additional block information is of the form 500 501@smallexample 502@var{tag} @var{information} 503@end smallexample 504 505The @var{information} is human readable, but designed to be simple 506enough for machine parsing too. 507 508When printing percentages, 0% and 100% are only printed when the values 509are @emph{exactly} 0% and 100% respectively. Other values which would 510conventionally be rounded to 0% or 100% are instead printed as the 511nearest non-boundary value. 512 513When using @command{gcov}, you must first compile your program 514with a special GCC option @samp{--coverage}. 515This tells the compiler to generate additional information needed by 516gcov (basically a flow graph of the program) and also includes 517additional code in the object files for generating the extra profiling 518information needed by gcov. These additional files are placed in the 519directory where the object file is located. 520 521Running the program will cause profile output to be generated. For each 522source file compiled with @option{-fprofile-arcs}, an accompanying 523@file{.gcda} file will be placed in the object file directory. 524 525Running @command{gcov} with your program's source file names as arguments 526will now produce a listing of the code along with frequency of execution 527for each line. For example, if your program is called @file{tmp.cpp}, this 528is what you see when you use the basic @command{gcov} facility: 529 530@smallexample 531$ g++ --coverage tmp.cpp 532$ a.out 533$ gcov tmp.cpp -m 534File 'tmp.cpp' 535Lines executed:92.86% of 14 536Creating 'tmp.cpp.gcov' 537@end smallexample 538 539The file @file{tmp.cpp.gcov} contains output from @command{gcov}. 540Here is a sample: 541 542@smallexample 543 -: 0:Source:tmp.cpp 544 -: 0:Working directory:/home/gcc/testcase 545 -: 0:Graph:tmp.gcno 546 -: 0:Data:tmp.gcda 547 -: 0:Runs:1 548 -: 0:Programs:1 549 -: 1:#include <stdio.h> 550 -: 2: 551 -: 3:template<class T> 552 -: 4:class Foo 553 -: 5:@{ 554 -: 6: public: 555 1*: 7: Foo(): b (1000) @{@} 556------------------ 557Foo<char>::Foo(): 558 #####: 7: Foo(): b (1000) @{@} 559------------------ 560Foo<int>::Foo(): 561 1: 7: Foo(): b (1000) @{@} 562------------------ 563 2*: 8: void inc () @{ b++; @} 564------------------ 565Foo<char>::inc(): 566 #####: 8: void inc () @{ b++; @} 567------------------ 568Foo<int>::inc(): 569 2: 8: void inc () @{ b++; @} 570------------------ 571 -: 9: 572 -: 10: private: 573 -: 11: int b; 574 -: 12:@}; 575 -: 13: 576 -: 14:template class Foo<int>; 577 -: 15:template class Foo<char>; 578 -: 16: 579 -: 17:int 580 1: 18:main (void) 581 -: 19:@{ 582 -: 20: int i, total; 583 1: 21: Foo<int> counter; 584 -: 22: 585 1: 23: counter.inc(); 586 1: 24: counter.inc(); 587 1: 25: total = 0; 588 -: 26: 589 11: 27: for (i = 0; i < 10; i++) 590 10: 28: total += i; 591 -: 29: 592 1*: 30: int v = total > 100 ? 1 : 2; 593 -: 31: 594 1: 32: if (total != 45) 595 #####: 33: printf ("Failure\n"); 596 -: 34: else 597 1: 35: printf ("Success\n"); 598 1: 36: return 0; 599 -: 37:@} 600@end smallexample 601 602Note that line 7 is shown in the report multiple times. First occurrence 603presents total number of execution of the line and the next two belong 604to instances of class Foo constructors. As you can also see, line 30 contains 605some unexecuted basic blocks and thus execution count has asterisk symbol. 606 607When you use the @option{-a} option, you will get individual block 608counts, and the output looks like this: 609 610@smallexample 611 -: 0:Source:tmp.cpp 612 -: 0:Working directory:/home/gcc/testcase 613 -: 0:Graph:tmp.gcno 614 -: 0:Data:tmp.gcda 615 -: 0:Runs:1 616 -: 0:Programs:1 617 -: 1:#include <stdio.h> 618 -: 2: 619 -: 3:template<class T> 620 -: 4:class Foo 621 -: 5:@{ 622 -: 6: public: 623 1*: 7: Foo(): b (1000) @{@} 624------------------ 625Foo<char>::Foo(): 626 #####: 7: Foo(): b (1000) @{@} 627------------------ 628Foo<int>::Foo(): 629 1: 7: Foo(): b (1000) @{@} 630------------------ 631 2*: 8: void inc () @{ b++; @} 632------------------ 633Foo<char>::inc(): 634 #####: 8: void inc () @{ b++; @} 635------------------ 636Foo<int>::inc(): 637 2: 8: void inc () @{ b++; @} 638------------------ 639 -: 9: 640 -: 10: private: 641 -: 11: int b; 642 -: 12:@}; 643 -: 13: 644 -: 14:template class Foo<int>; 645 -: 15:template class Foo<char>; 646 -: 16: 647 -: 17:int 648 1: 18:main (void) 649 -: 19:@{ 650 -: 20: int i, total; 651 1: 21: Foo<int> counter; 652 1: 21-block 0 653 -: 22: 654 1: 23: counter.inc(); 655 1: 23-block 0 656 1: 24: counter.inc(); 657 1: 24-block 0 658 1: 25: total = 0; 659 -: 26: 660 11: 27: for (i = 0; i < 10; i++) 661 1: 27-block 0 662 11: 27-block 1 663 10: 28: total += i; 664 10: 28-block 0 665 -: 29: 666 1*: 30: int v = total > 100 ? 1 : 2; 667 1: 30-block 0 668 %%%%%: 30-block 1 669 1: 30-block 2 670 -: 31: 671 1: 32: if (total != 45) 672 1: 32-block 0 673 #####: 33: printf ("Failure\n"); 674 %%%%%: 33-block 0 675 -: 34: else 676 1: 35: printf ("Success\n"); 677 1: 35-block 0 678 1: 36: return 0; 679 1: 36-block 0 680 -: 37:@} 681@end smallexample 682 683In this mode, each basic block is only shown on one line -- the last 684line of the block. A multi-line block will only contribute to the 685execution count of that last line, and other lines will not be shown 686to contain code, unless previous blocks end on those lines. 687The total execution count of a line is shown and subsequent lines show 688the execution counts for individual blocks that end on that line. After each 689block, the branch and call counts of the block will be shown, if the 690@option{-b} option is given. 691 692Because of the way GCC instruments calls, a call count can be shown 693after a line with no individual blocks. 694As you can see, line 33 contains a basic block that was not executed. 695 696@need 450 697When you use the @option{-b} option, your output looks like this: 698 699@smallexample 700 -: 0:Source:tmp.cpp 701 -: 0:Working directory:/home/gcc/testcase 702 -: 0:Graph:tmp.gcno 703 -: 0:Data:tmp.gcda 704 -: 0:Runs:1 705 -: 0:Programs:1 706 -: 1:#include <stdio.h> 707 -: 2: 708 -: 3:template<class T> 709 -: 4:class Foo 710 -: 5:@{ 711 -: 6: public: 712 1*: 7: Foo(): b (1000) @{@} 713------------------ 714Foo<char>::Foo(): 715function Foo<char>::Foo() called 0 returned 0% blocks executed 0% 716 #####: 7: Foo(): b (1000) @{@} 717------------------ 718Foo<int>::Foo(): 719function Foo<int>::Foo() called 1 returned 100% blocks executed 100% 720 1: 7: Foo(): b (1000) @{@} 721------------------ 722 2*: 8: void inc () @{ b++; @} 723------------------ 724Foo<char>::inc(): 725function Foo<char>::inc() called 0 returned 0% blocks executed 0% 726 #####: 8: void inc () @{ b++; @} 727------------------ 728Foo<int>::inc(): 729function Foo<int>::inc() called 2 returned 100% blocks executed 100% 730 2: 8: void inc () @{ b++; @} 731------------------ 732 -: 9: 733 -: 10: private: 734 -: 11: int b; 735 -: 12:@}; 736 -: 13: 737 -: 14:template class Foo<int>; 738 -: 15:template class Foo<char>; 739 -: 16: 740 -: 17:int 741function main called 1 returned 100% blocks executed 81% 742 1: 18:main (void) 743 -: 19:@{ 744 -: 20: int i, total; 745 1: 21: Foo<int> counter; 746call 0 returned 100% 747branch 1 taken 100% (fallthrough) 748branch 2 taken 0% (throw) 749 -: 22: 750 1: 23: counter.inc(); 751call 0 returned 100% 752branch 1 taken 100% (fallthrough) 753branch 2 taken 0% (throw) 754 1: 24: counter.inc(); 755call 0 returned 100% 756branch 1 taken 100% (fallthrough) 757branch 2 taken 0% (throw) 758 1: 25: total = 0; 759 -: 26: 760 11: 27: for (i = 0; i < 10; i++) 761branch 0 taken 91% (fallthrough) 762branch 1 taken 9% 763 10: 28: total += i; 764 -: 29: 765 1*: 30: int v = total > 100 ? 1 : 2; 766branch 0 taken 0% (fallthrough) 767branch 1 taken 100% 768 -: 31: 769 1: 32: if (total != 45) 770branch 0 taken 0% (fallthrough) 771branch 1 taken 100% 772 #####: 33: printf ("Failure\n"); 773call 0 never executed 774branch 1 never executed 775branch 2 never executed 776 -: 34: else 777 1: 35: printf ("Success\n"); 778call 0 returned 100% 779branch 1 taken 100% (fallthrough) 780branch 2 taken 0% (throw) 781 1: 36: return 0; 782 -: 37:@} 783@end smallexample 784 785For each function, a line is printed showing how many times the function 786is called, how many times it returns and what percentage of the 787function's blocks were executed. 788 789For each basic block, a line is printed after the last line of the basic 790block describing the branch or call that ends the basic block. There can 791be multiple branches and calls listed for a single source line if there 792are multiple basic blocks that end on that line. In this case, the 793branches and calls are each given a number. There is no simple way to map 794these branches and calls back to source constructs. In general, though, 795the lowest numbered branch or call will correspond to the leftmost construct 796on the source line. 797 798For a branch, if it was executed at least once, then a percentage 799indicating the number of times the branch was taken divided by the 800number of times the branch was executed will be printed. Otherwise, the 801message ``never executed'' is printed. 802 803For a call, if it was executed at least once, then a percentage 804indicating the number of times the call returned divided by the number 805of times the call was executed will be printed. This will usually be 806100%, but may be less for functions that call @code{exit} or @code{longjmp}, 807and thus may not return every time they are called. 808 809The execution counts are cumulative. If the example program were 810executed again without removing the @file{.gcda} file, the count for the 811number of times each line in the source was executed would be added to 812the results of the previous run(s). This is potentially useful in 813several ways. For example, it could be used to accumulate data over a 814number of program runs as part of a test verification suite, or to 815provide more accurate long-term information over a large number of 816program runs. 817 818The data in the @file{.gcda} files is saved immediately before the program 819exits. For each source file compiled with @option{-fprofile-arcs}, the 820profiling code first attempts to read in an existing @file{.gcda} file; if 821the file doesn't match the executable (differing number of basic block 822counts) it will ignore the contents of the file. It then adds in the 823new execution counts and finally writes the data to the file. 824 825@node Gcov and Optimization 826@section Using @command{gcov} with GCC Optimization 827 828If you plan to use @command{gcov} to help optimize your code, you must 829first compile your program with a special GCC option 830@samp{--coverage}. Aside from that, you can use any 831other GCC options; but if you want to prove that every single line 832in your program was executed, you should not compile with optimization 833at the same time. On some machines the optimizer can eliminate some 834simple code lines by combining them with other lines. For example, code 835like this: 836 837@smallexample 838if (a != b) 839 c = 1; 840else 841 c = 0; 842@end smallexample 843 844@noindent 845can be compiled into one instruction on some machines. In this case, 846there is no way for @command{gcov} to calculate separate execution counts 847for each line because there isn't separate code for each line. Hence 848the @command{gcov} output looks like this if you compiled the program with 849optimization: 850 851@smallexample 852 100: 12:if (a != b) 853 100: 13: c = 1; 854 100: 14:else 855 100: 15: c = 0; 856@end smallexample 857 858The output shows that this block of code, combined by optimization, 859executed 100 times. In one sense this result is correct, because there 860was only one instruction representing all four of these lines. However, 861the output does not indicate how many times the result was 0 and how 862many times the result was 1. 863 864Inlineable functions can create unexpected line counts. Line counts are 865shown for the source code of the inlineable function, but what is shown 866depends on where the function is inlined, or if it is not inlined at all. 867 868If the function is not inlined, the compiler must emit an out of line 869copy of the function, in any object file that needs it. If 870@file{fileA.o} and @file{fileB.o} both contain out of line bodies of a 871particular inlineable function, they will also both contain coverage 872counts for that function. When @file{fileA.o} and @file{fileB.o} are 873linked together, the linker will, on many systems, select one of those 874out of line bodies for all calls to that function, and remove or ignore 875the other. Unfortunately, it will not remove the coverage counters for 876the unused function body. Hence when instrumented, all but one use of 877that function will show zero counts. 878 879If the function is inlined in several places, the block structure in 880each location might not be the same. For instance, a condition might 881now be calculable at compile time in some instances. Because the 882coverage of all the uses of the inline function will be shown for the 883same source lines, the line counts themselves might seem inconsistent. 884 885Long-running applications can use the @code{__gcov_reset} and @code{__gcov_dump} 886facilities to restrict profile collection to the program region of 887interest. Calling @code{__gcov_reset(void)} will clear all profile counters 888to zero, and calling @code{__gcov_dump(void)} will cause the profile information 889collected at that point to be dumped to @file{.gcda} output files. 890Instrumented applications use a static destructor with priority 99 891to invoke the @code{__gcov_dump} function. Thus @code{__gcov_dump} 892is executed after all user defined static destructors, 893as well as handlers registered with @code{atexit}. 894If an executable loads a dynamic shared object via dlopen functionality, 895@option{-Wl,--dynamic-list-data} is needed to dump all profile data. 896 897Profiling run-time library reports various errors related to profile 898manipulation and profile saving. Errors are printed into standard error output 899or @samp{GCOV_ERROR_FILE} file, if environment variable is used. 900In order to terminate immediately after an errors occurs 901set @samp{GCOV_EXIT_AT_ERROR} environment variable. 902That can help users to find profile clashing which leads 903to a misleading profile. 904 905@c man end 906 907@node Gcov Data Files 908@section Brief Description of @command{gcov} Data Files 909 910@command{gcov} uses two files for profiling. The names of these files 911are derived from the original @emph{object} file by substituting the 912file suffix with either @file{.gcno}, or @file{.gcda}. The files 913contain coverage and profile data stored in a platform-independent format. 914The @file{.gcno} files are placed in the same directory as the object 915file. By default, the @file{.gcda} files are also stored in the same 916directory as the object file, but the GCC @option{-fprofile-dir} option 917may be used to store the @file{.gcda} files in a separate directory. 918 919The @file{.gcno} notes file is generated when the source file is compiled 920with the GCC @option{-ftest-coverage} option. It contains information to 921reconstruct the basic block graphs and assign source line numbers to 922blocks. 923 924The @file{.gcda} count data file is generated when a program containing 925object files built with the GCC @option{-fprofile-arcs} option is executed. 926A separate @file{.gcda} file is created for each object file compiled with 927this option. It contains arc transition counts, value profile counts, and 928some summary information. 929 930It is not recommended to access the coverage files directly. 931Consumers should use the intermediate format that is provided 932by @command{gcov} tool via @option{--json-format} option. 933 934@node Cross-profiling 935@section Data File Relocation to Support Cross-Profiling 936 937Running the program will cause profile output to be generated. For each 938source file compiled with @option{-fprofile-arcs}, an accompanying @file{.gcda} 939file will be placed in the object file directory. That implicitly requires 940running the program on the same system as it was built or having the same 941absolute directory structure on the target system. The program will try 942to create the needed directory structure, if it is not already present. 943 944To support cross-profiling, a program compiled with @option{-fprofile-arcs} 945can relocate the data files based on two environment variables: 946 947@itemize @bullet 948@item 949GCOV_PREFIX contains the prefix to add to the absolute paths 950in the object file. Prefix can be absolute, or relative. The 951default is no prefix. 952 953@item 954GCOV_PREFIX_STRIP indicates the how many initial directory names to strip off 955the hardwired absolute paths. Default value is 0. 956 957@emph{Note:} If GCOV_PREFIX_STRIP is set without GCOV_PREFIX is undefined, 958 then a relative path is made out of the hardwired absolute paths. 959@end itemize 960 961For example, if the object file @file{/user/build/foo.o} was built with 962@option{-fprofile-arcs}, the final executable will try to create the data file 963@file{/user/build/foo.gcda} when running on the target system. This will 964fail if the corresponding directory does not exist and it is unable to create 965it. This can be overcome by, for example, setting the environment as 966@samp{GCOV_PREFIX=/target/run} and @samp{GCOV_PREFIX_STRIP=1}. Such a 967setting will name the data file @file{/target/run/build/foo.gcda}. 968 969You must move the data files to the expected directory tree in order to 970use them for profile directed optimizations (@option{-fprofile-use}), or to 971use the @command{gcov} tool. 972