gcov.texi
来自「理解和实践操作系统的一本好书」· TEXI 代码 · 共 574 行 · 第 1/2 页
TEXI
574 行
@c Copyright (C) 1996, 1997, 1999, 2000, 2001,@c 2002, 2003, 2004, 2005 Free Software Foundation, Inc.@c This is part of the GCC manual.@c For copying conditions, see the file gcc.texi.@ignore@c man begin COPYRIGHTCopyright @copyright{} 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005Free Software Foundation, Inc.Permission is granted to copy, distribute and/or modify this documentunder the terms of the GNU Free Documentation License, Version 1.2 orany later version published by the Free Software Foundation; with theInvariant Sections being ``GNU General Public License'' and ``FundingFree Software'', the Front-Cover texts being (a) (see below), and withthe Back-Cover Texts being (b) (see below). A copy of the license isincluded in the gfdl(7) man page.(a) The FSF's Front-Cover Text is: A GNU Manual(b) The FSF's Back-Cover Text is: You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development.@c man end@c Set file name and title for the man page.@setfilename gcov@settitle coverage testing tool@end ignore@node Gcov@chapter @command{gcov}---a Test Coverage Program@command{gcov} is a tool you can use in conjunction with GCC totest code coverage in your programs.@menu* Gcov Intro:: Introduction to gcov.* Invoking Gcov:: How to use gcov.* Gcov and Optimization:: Using gcov with GCC optimization.* Gcov Data Files:: The files used by gcov.* Cross-profiling:: Data file relocation.@end menu@node Gcov Intro@section Introduction to @command{gcov}@c man begin DESCRIPTION@command{gcov} is a test coverage program. Use it in concert with GCCto analyze your programs to help create more efficient, faster runningcode and to discover untested parts of your program. You can use@command{gcov} as a profiling tool to help discover where youroptimization efforts will best affect your code. You can also use@command{gcov} along with the other profiling tool, @command{gprof}, toassess which parts of your code use the greatest amount of computingtime.Profiling tools help you analyze your code's performance. Using aprofiler such as @command{gcov} or @command{gprof}, you can find out somebasic performance statistics, such as:@itemize @bullet@itemhow often each line of code executes@itemwhat lines of code are actually executed@itemhow much computing time each section of code uses@end itemizeOnce you know these things about how your code works when compiled, youcan look at each module to see which modules should be optimized.@command{gcov} helps you determine where to work on optimization.Software developers also use coverage testing in concert withtestsuites, to make sure software is actually good enough for a release.Testsuites can verify that a program works as expected; a coverageprogram tests to see how much of the program is exercised by thetestsuite. Developers can then determine what kinds of test cases needto be added to the testsuites to create both better testing and a betterfinal product.You should compile your code without optimization if you plan to use@command{gcov} because the optimization, by combining some lines of codeinto one function, may not give you as much information as you need tolook for `hot spots' where the code is using a great deal of computertime. Likewise, because @command{gcov} accumulates statistics by line (atthe lowest resolution), it works best with a programming style thatplaces only one statement on each line. If you use complicated macrosthat expand to loops or to other control structures, the statistics areless helpful---they only report on the line where the macro callappears. If your complex macros behave like functions, you can replacethem with inline functions to solve this problem.@command{gcov} creates a logfile called @file{@var{sourcefile}.gcov} whichindicates how many times each line of a source file @file{@var{sourcefile}.c}has executed. You can use these logfiles along with @command{gprof} to aidin fine-tuning the performance of your programs. @command{gprof} givestiming information you can use along with the information you get from@command{gcov}.@command{gcov} works only on code compiled with GCC@. It is notcompatible with any other profiling or test coverage mechanism.@c man end@node Invoking Gcov@section Invoking @command{gcov}@smallexamplegcov @r{[}@var{options}@r{]} @var{sourcefiles}@end smallexample@command{gcov} accepts the following options:@ignore@c man begin SYNOPSISgcov [@option{-v}|@option{--version}] [@option{-h}|@option{--help}] [@option{-a}|@option{--all-blocks}] [@option{-b}|@option{--branch-probabilities}] [@option{-c}|@option{--branch-counts}] [@option{-n}|@option{--no-output}] [@option{-l}|@option{--long-file-names}] [@option{-p}|@option{--preserve-paths}] [@option{-f}|@option{--function-summaries}] [@option{-o}|@option{--object-directory} @var{directory|file}] @var{sourcefiles} [@option{-u}|@option{--unconditional-branches}]@c man end@c man begin SEEALSOgpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}.@c man end@end ignore@c man begin OPTIONS@table @gcctabopt@item -h@itemx --helpDisplay help about using @command{gcov} (on the standard output), andexit without doing any further processing.@item -v@itemx --versionDisplay the @command{gcov} version number (on the standard output),and exit without doing any further processing.@item -a@itemx --all-blocksWrite individual execution counts for every basic block. Normally gcovoutputs execution counts only for the main blocks of a line. With thisoption you can determine if blocks within a single line are not beingexecuted.@item -b@itemx --branch-probabilitiesWrite branch frequencies to the output file, and write branch summaryinfo to the standard output. This option allows you to see how ofteneach branch in your program was taken. Unconditional branches will notbe shown, unless the @option{-u} option is given.@item -c@itemx --branch-countsWrite branch frequencies as the number of branches taken, rather thanthe percentage of branches taken.@item -n@itemx --no-outputDo not create the @command{gcov} output file.@item -l@itemx --long-file-namesCreate long file names for included source files. For example, if theheader file @file{x.h} contains code, and was included in the file@file{a.c}, then running @command{gcov} on the file @file{a.c} will producean output file called @file{a.c##x.h.gcov} instead of @file{x.h.gcov}.This can be useful if @file{x.h} is included in multiple sourcefiles. If you use the @samp{-p} option, both the including andincluded file names will be complete path names.@item -p@itemx --preserve-pathsPreserve complete path information in the names of generated@file{.gcov} files. Without this option, just the filename component isused. With this option, all directories are used, with @samp{/} characterstranslated to @samp{#} characters, @file{.} directory componentsremoved and @file{..}components renamed to @samp{^}. This is useful if sourcefiles are in severaldifferent directories. It also affects the @samp{-l} option.@item -f@itemx --function-summariesOutput summaries for each function in addition to the file level summary.@item -o @var{directory|file}@itemx --object-directory @var{directory}@itemx --object-file @var{file}Specify either the directory containing the gcov data files, or theobject path name. The @file{.gcno}, and@file{.gcda} data files are searched for using this option. If a directoryis specified, the data files are in that directory and named after thesource file name, without its extension. If a file is specified here,the data files are named after that file, without its extension. If thisoption is not supplied, it defaults to the current directory.@item -u@itemx --unconditional-branchesWhen branch probabilities are given, include those of unconditional branches.Unconditional branches are normally not interesting.@end table@command{gcov} should be run with the current directory the same as thatwhen you invoked the compiler. Otherwise it will not be able to locatethe source files. @command{gcov} produces files called@file{@var{mangledname}.gcov} in the current directory. These containthe coverage information of the source file they correspond to.One @file{.gcov} file is produced for each source file containing code,which was compiled to produce the data files. The @var{mangledname} partof the output file name is usually simply the source file name, but canbe something more complicated if the @samp{-l} or @samp{-p} options aregiven. Refer to those options for details.The @file{.gcov} files contain the @samp{:} separated fields along withprogram source code. The format is@smallexample@var{execution_count}:@var{line_number}:@var{source line text}@end smallexampleAdditional block information may succeed each line, when requested bycommand line option. The @var{execution_count} is @samp{-} for linescontaining no code and @samp{#####} for lines which were never executed.Some lines of information at the start have @var{line_number} of zero.The preamble lines are of the form@smallexample-:0:@var{tag}:@var{value}@end smallexampleThe ordering and number of these preamble lines will be augmented as@command{gcov} development progresses --- do not rely on them remainingunchanged. Use @var{tag} to locate a particular preamble line.The additional block information is of the form@smallexample@var{tag} @var{information}@end smallexampleThe @var{information} is human readable, but designed to be simpleenough for machine parsing too.When printing percentages, 0% and 100% are only printed when the valuesare @emph{exactly} 0% and 100% respectively. Other values which wouldconventionally be rounded to 0% or 100% are instead printed as thenearest non-boundary value.When using @command{gcov}, you must first compile your program with twospecial GCC options: @samp{-fprofile-arcs -ftest-coverage}.This tells the compiler to generate additional information needed bygcov (basically a flow graph of the program) and also includesadditional code in the object files for generating the extra profilinginformation needed by gcov. These additional files are placed in thedirectory where the object file is located.Running the program will cause profile output to be generated. For eachsource file compiled with @option{-fprofile-arcs}, an accompanying@file{.gcda} file will be placed in the object file directory.Running @command{gcov} with your program's source file names as argumentswill now produce a listing of the code along with frequency of executionfor each line. For example, if your program is called @file{tmp.c}, thisis what you see when you use the basic @command{gcov} facility:@smallexample$ gcc -fprofile-arcs -ftest-coverage tmp.c$ a.out$ gcov tmp.c90.00% of 10 source lines executed in file tmp.cCreating tmp.c.gcov.@end smallexample
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?