⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gcov.texi

📁 GCC
💻 TEXI
📖 第 1 页 / 共 2 页
字号:
@c Copyright (C) 1996, 1997, 1999, 2000 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 COPYRIGHT
Copyright @copyright{} 1996, 1997, 1999, 2000 Free Software Foundation, Inc.

Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided also that the
entire resulting derived work is distributed under the terms of a
permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that this permission notice may be included in translations
approved by the Free Software Foundation instead of in the original
English.
@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 @sc{gnu} CC to
test code coverage in your programs.

This chapter describes version 1.5 of @command{gcov}.

@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.
@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 @sc{gnu}
CC to analyze your programs to help create more efficient, faster
running code.  You can use @command{gcov} as a profiling tool to help
discover where your optimization efforts will best affect your code.  You
can also use @command{gcov} along with the other profiling tool,
@command{gprof}, to assess which parts of your code use the greatest amount
of computing time.

Profiling tools help you analyze your code's performance.  Using a
profiler such as @command{gcov} or @command{gprof}, you can find out some
basic performance statistics, such as:

@itemize @bullet
@item
how often each line of code executes

@item
what lines of code are actually executed

@item
how much computing time each section of code uses
@end itemize

Once you know these things about how your code works when compiled, you
can 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 with
testsuites, to make sure software is actually good enough for a release.
Testsuites can verify that a program works as expected; a coverage
program tests to see how much of the program is exercised by the
testsuite.  Developers can then determine what kinds of test cases need
to be added to the testsuites to create both better testing and a better
final product.

You should compile your code without optimization if you plan to use
@command{gcov} because the optimization, by combining some lines of code
into one function, may not give you as much information as you need to
look for `hot spots' where the code is using a great deal of computer
time.  Likewise, because @command{gcov} accumulates statistics by line (at
the lowest resolution), it works best with a programming style that
places only one statement on each line.  If you use complicated macros
that expand to loops or to other control structures, the statistics are
less helpful---they only report on the line where the macro call
appears.  If your complex macros behave like functions, you can replace
them with inline functions to solve this problem.

@command{gcov} creates a logfile called @file{@var{sourcefile}.gcov} which
indicates 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 aid
in fine-tuning the performance of your programs.  @command{gprof} gives
timing information you can use along with the information you get from
@command{gcov}.

@command{gcov} works only on code compiled with @sc{gnu} CC.  It is not
compatible with any other profiling or test coverage mechanism.

@c man end

@node Invoking Gcov
@section Invoking gcov

@smallexample
gcov [-b] [-c] [-v] [-n] [-l] [-f] [-o directory] @var{sourcefile}
@end smallexample

@ignore
@c man begin SYNOPSIS
gcov [@option{-b}] [@option{-c}] [@option{-v}] [@option{-n}] [@option{-l}] [@option{-f}] [@option{-o} @var{directory}] @var{sourcefile}
@c man end
@c man begin SEEALSO
gcc(1) and the Info entry for @file{gcc}.
@c man end
@end ignore

@c man begin OPTIONS
@table @gcctabopt
@item -b
Write branch frequencies to the output file, and write branch summary
info to the standard output.  This option allows you to see how often
each branch in your program was taken.

@item -c
Write branch frequencies as the number of branches taken, rather than
the percentage of branches taken.

@item -v
Display the @command{gcov} version number (on the standard error stream).

@item -n
Do not create the @command{gcov} output file.

@item -l
Create long file names for included source files.  For example, if the
header 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 produce
an 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 source files.

@item -f
Output summaries for each function in addition to the file level summary.

@item -o
The directory where the object files live.  Gcov will search for @file{.bb},
@file{.bbg}, and @file{.da} files in this directory.
@end table

@need 3000
When using @command{gcov}, you must first compile your program with two
special @sc{gnu} CC options: @samp{-fprofile-arcs -ftest-coverage}.
This tells the compiler to generate additional information needed by
gcov (basically a flow graph of the program) and also includes
additional code in the object files for generating the extra profiling
information needed by gcov.  These additional files are placed in the
directory where the source code is located.

Running the program will cause profile output to be generated.  For each
source file compiled with @option{-fprofile-arcs}, an accompanying @file{.da}
file will be placed in the source directory.

Running @command{gcov} with your program's source file names as arguments
will now produce a listing of the code along with frequency of execution
for each line.  For example, if your program is called @file{tmp.c}, this
is what you see when you use the basic @command{gcov} facility:

@smallexample
$ gcc -fprofile-arcs -ftest-coverage tmp.c
$ a.out
$ gcov tmp.c
 87.50% of 8 source lines executed in file tmp.c
Creating tmp.c.gcov.
@end smallexample

The file @file{tmp.c.gcov} contains output from @command{gcov}.
Here is a sample:

@smallexample
                main()
                @{
           1      int i, total;

           1      total = 0;

          11      for (i = 0; i < 10; i++)
          10        total += i;

           1      if (total != 45)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -