487 lines
26 KiB
HTML
487 lines
26 KiB
HTML
<HTML>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<!-- Created on March, 17 2001 by texi2html 1.64 -->
|
|
<!--
|
|
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
|
|
Karl Berry <karl@freefriends.org>
|
|
Olaf Bachmann <obachman@mathematik.uni-kl.de>
|
|
and many others.
|
|
Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de>
|
|
Send bugs and suggestions to <texi2html@mathematik.uni-kl.de>
|
|
|
|
-->
|
|
<HEAD>
|
|
<TITLE>Using and Porting the GNU Compiler Collection (GCC): Gcov</TITLE>
|
|
|
|
<META NAME="description" CONTENT="Using and Porting the GNU Compiler Collection (GCC): Gcov">
|
|
<META NAME="keywords" CONTENT="Using and Porting the GNU Compiler Collection (GCC): Gcov">
|
|
<META NAME="resource-type" CONTENT="document">
|
|
<META NAME="distribution" CONTENT="global">
|
|
<META NAME="Generator" CONTENT="texi2html 1.64">
|
|
|
|
</HEAD>
|
|
|
|
<BODY LANG="" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">
|
|
|
|
<A NAME="SEC113"></A>
|
|
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
|
|
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_5.html#SEC112" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_5.html#SEC112"> < </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC114" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC114"> > </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_7.html#SEC118" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_7.html#SEC118"> << </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc.html#SEC_Top" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc.html#SEC_Top"> Up </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_7.html#SEC118" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_7.html#SEC118"> >> </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc.html#SEC_Top" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc.html#SEC_Top">Top</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_toc.html#SEC_Contents" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_toc.html#SEC_Contents">Contents</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_24.html#SEC261" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_24.html#SEC261">Index</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_abt.html#SEC_About" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_abt.html#SEC_About"> ? </A>]</TD>
|
|
</TR></TABLE>
|
|
<H1> 6. <CODE>gcov</CODE>: a Test Coverage Program </H1>
|
|
<!--docid::SEC113::-->
|
|
<P>
|
|
|
|
<CODE>gcov</CODE> is a tool you can use in conjunction with GNU CC to
|
|
test code coverage in your programs.
|
|
</P><P>
|
|
|
|
This chapter describes version 1.5 of <CODE>gcov</CODE>.
|
|
</P><P>
|
|
|
|
<BLOCKQUOTE><TABLE BORDER=0 CELLSPACING=0>
|
|
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="gcc_6.html#SEC114" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC114">6.1 Introduction to <CODE>gcov</CODE></A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Introduction to gcov.</TD></TR>
|
|
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="gcc_6.html#SEC115" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC115">6.2 Invoking gcov</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">How to use gcov.</TD></TR>
|
|
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="gcc_6.html#SEC116" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC116">6.3 Using <CODE>gcov</CODE> with GCC Optimization</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Using gcov with GCC optimization.</TD></TR>
|
|
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="gcc_6.html#SEC117" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC117">6.4 Brief description of <CODE>gcov</CODE> data files</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">The files used by gcov.</TD></TR>
|
|
</TABLE></BLOCKQUOTE>
|
|
<P>
|
|
|
|
<A NAME="Gcov Intro"></A>
|
|
<HR SIZE="6">
|
|
<A NAME="SEC114"></A>
|
|
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
|
|
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC113" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC113"> < </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC115" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC115"> > </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC113" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC113"> << </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC113" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC113"> Up </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_7.html#SEC118" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_7.html#SEC118"> >> </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc.html#SEC_Top" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc.html#SEC_Top">Top</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_toc.html#SEC_Contents" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_toc.html#SEC_Contents">Contents</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_24.html#SEC261" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_24.html#SEC261">Index</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_abt.html#SEC_About" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_abt.html#SEC_About"> ? </A>]</TD>
|
|
</TR></TABLE>
|
|
<H2> 6.1 Introduction to <CODE>gcov</CODE> </H2>
|
|
<!--docid::SEC114::-->
|
|
<P>
|
|
|
|
<CODE>gcov</CODE> is a test coverage program. Use it in concert with GNU
|
|
CC to analyze your programs to help create more efficient, faster
|
|
running code. You can use <CODE>gcov</CODE> as a profiling tool to help
|
|
discover where your optimization efforts will best affect your code. You
|
|
can also use <CODE>gcov</CODE> along with the other profiling tool,
|
|
<CODE>gprof</CODE>, to assess which parts of your code use the greatest amount
|
|
of computing time.
|
|
</P><P>
|
|
|
|
Profiling tools help you analyze your code's performance. Using a
|
|
profiler such as <CODE>gcov</CODE> or <CODE>gprof</CODE>, you can find out some
|
|
basic performance statistics, such as:
|
|
</P><P>
|
|
|
|
<UL>
|
|
<LI>
|
|
how often each line of code executes
|
|
<P>
|
|
|
|
<LI>
|
|
what lines of code are actually executed
|
|
<P>
|
|
|
|
<LI>
|
|
how much computing time each section of code uses
|
|
</UL>
|
|
<P>
|
|
|
|
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.
|
|
<CODE>gcov</CODE> helps you determine where to work on optimization.
|
|
</P><P>
|
|
|
|
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.
|
|
</P><P>
|
|
|
|
You should compile your code without optimization if you plan to use
|
|
<CODE>gcov</CODE> 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 <CODE>gcov</CODE> 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.
|
|
</P><P>
|
|
|
|
<CODE>gcov</CODE> creates a logfile called <TT>`<VAR>sourcefile</VAR>.gcov'</TT> which
|
|
indicates how many times each line of a source file <TT>`<VAR>sourcefile</VAR>.c'</TT>
|
|
has executed. You can use these logfiles along with <CODE>gprof</CODE> to aid
|
|
in fine-tuning the performance of your programs. <CODE>gprof</CODE> gives
|
|
timing information you can use along with the information you get from
|
|
<CODE>gcov</CODE>.
|
|
</P><P>
|
|
|
|
<CODE>gcov</CODE> works only on code compiled with GNU CC. It is not
|
|
compatible with any other profiling or test coverage mechanism.
|
|
</P><P>
|
|
|
|
<A NAME="Invoking Gcov"></A>
|
|
<HR SIZE="6">
|
|
<A NAME="SEC115"></A>
|
|
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
|
|
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC114" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC114"> < </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC116" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC116"> > </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC116" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC116"> << </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC113" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC113"> Up </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_7.html#SEC118" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_7.html#SEC118"> >> </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc.html#SEC_Top" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc.html#SEC_Top">Top</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_toc.html#SEC_Contents" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_toc.html#SEC_Contents">Contents</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_24.html#SEC261" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_24.html#SEC261">Index</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_abt.html#SEC_About" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_abt.html#SEC_About"> ? </A>]</TD>
|
|
</TR></TABLE>
|
|
<H2> 6.2 Invoking gcov </H2>
|
|
<!--docid::SEC115::-->
|
|
<P>
|
|
|
|
<TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>gcov [-b] [-v] [-n] [-l] [-f] [-o directory] <VAR>sourcefile</VAR>
|
|
</FONT></pre></td></tr></table></P><P>
|
|
|
|
<DL COMPACT>
|
|
<DT><CODE>-b</CODE>
|
|
<DD>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.
|
|
<P>
|
|
|
|
<DT><CODE>-v</CODE>
|
|
<DD>Display the <CODE>gcov</CODE> version number (on the standard error stream).
|
|
<P>
|
|
|
|
<DT><CODE>-n</CODE>
|
|
<DD>Do not create the <CODE>gcov</CODE> output file.
|
|
<P>
|
|
|
|
<DT><CODE>-l</CODE>
|
|
<DD>Create long file names for included source files. For example, if the
|
|
header file <SAMP>`x.h'</SAMP> contains code, and was included in the file
|
|
<SAMP>`a.c'</SAMP>, then running <CODE>gcov</CODE> on the file <SAMP>`a.c'</SAMP> will produce
|
|
an output file called <SAMP>`a.c.x.h.gcov'</SAMP> instead of <SAMP>`x.h.gcov'</SAMP>.
|
|
This can be useful if <SAMP>`x.h'</SAMP> is included in multiple source files.
|
|
<P>
|
|
|
|
<DT><CODE>-f</CODE>
|
|
<DD>Output summaries for each function in addition to the file level summary.
|
|
<P>
|
|
|
|
<DT><CODE>-o</CODE>
|
|
<DD>The directory where the object files live. Gcov will search for <CODE>.bb</CODE>,
|
|
<CODE>.bbg</CODE>, and <CODE>.da</CODE> files in this directory.
|
|
</DL>
|
|
<P>
|
|
|
|
When using <CODE>gcov</CODE>, you must first compile your program with two
|
|
special GNU CC options: <SAMP>`-fprofile-arcs -ftest-coverage'</SAMP>.
|
|
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.
|
|
</P><P>
|
|
|
|
Running the program will cause profile output to be generated. For each
|
|
source file compiled with -fprofile-arcs, an accompanying <CODE>.da</CODE>
|
|
file will be placed in the source directory.
|
|
</P><P>
|
|
|
|
Running <CODE>gcov</CODE> 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 <SAMP>`tmp.c'</SAMP>, this
|
|
is what you see when you use the basic <CODE>gcov</CODE> facility:
|
|
</P><P>
|
|
|
|
<TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>$ 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.
|
|
</FONT></pre></td></tr></table></P><P>
|
|
|
|
The file <TT>`tmp.c.gcov'</TT> contains output from <CODE>gcov</CODE>.
|
|
Here is a sample:
|
|
</P><P>
|
|
|
|
<TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre> main()
|
|
{
|
|
1 int i, total;
|
|
|
|
1 total = 0;
|
|
|
|
11 for (i = 0; i < 10; i++)
|
|
10 total += i;
|
|
|
|
1 if (total != 45)
|
|
###### printf ("Failure\n");
|
|
else
|
|
1 printf ("Success\n");
|
|
1 }
|
|
</FONT></pre></td></tr></table></P><P>
|
|
|
|
When you use the <SAMP>`-b'</SAMP> option, your output looks like this:
|
|
</P><P>
|
|
|
|
<TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>$ gcov -b tmp.c
|
|
87.50% of 8 source lines executed in file tmp.c
|
|
80.00% of 5 branches executed in file tmp.c
|
|
80.00% of 5 branches taken at least once in file tmp.c
|
|
50.00% of 2 calls executed in file tmp.c
|
|
Creating tmp.c.gcov.
|
|
</FONT></pre></td></tr></table></P><P>
|
|
|
|
Here is a sample of a resulting <TT>`tmp.c.gcov'</TT> file:
|
|
</P><P>
|
|
|
|
<TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre> main()
|
|
{
|
|
1 int i, total;
|
|
|
|
1 total = 0;
|
|
|
|
11 for (i = 0; i < 10; i++)
|
|
branch 0 taken = 91%
|
|
branch 1 taken = 100%
|
|
branch 2 taken = 100%
|
|
10 total += i;
|
|
|
|
1 if (total != 45)
|
|
branch 0 taken = 100%
|
|
###### printf ("Failure\n");
|
|
call 0 never executed
|
|
branch 1 never executed
|
|
else
|
|
1 printf ("Success\n");
|
|
call 0 returns = 100%
|
|
1 }
|
|
</FONT></pre></td></tr></table></P><P>
|
|
|
|
For each basic block, a line is printed after the last line of the basic
|
|
block describing the branch or call that ends the basic block. There can
|
|
be multiple branches and calls listed for a single source line if there
|
|
are multiple basic blocks that end on that line. In this case, the
|
|
branches and calls are each given a number. There is no simple way to map
|
|
these branches and calls back to source constructs. In general, though,
|
|
the lowest numbered branch or call will correspond to the leftmost construct
|
|
on the source line.
|
|
</P><P>
|
|
|
|
For a branch, if it was executed at least once, then a percentage
|
|
indicating the number of times the branch was taken divided by the
|
|
number of times the branch was executed will be printed. Otherwise, the
|
|
message "never executed" is printed.
|
|
</P><P>
|
|
|
|
For a call, if it was executed at least once, then a percentage
|
|
indicating the number of times the call returned divided by the number
|
|
of times the call was executed will be printed. This will usually be
|
|
100%, but may be less for functions call <CODE>exit</CODE> or <CODE>longjmp</CODE>,
|
|
and thus may not return everytime they are called.
|
|
</P><P>
|
|
|
|
The execution counts are cumulative. If the example program were
|
|
executed again without removing the <CODE>.da</CODE> file, the count for the
|
|
number of times each line in the source was executed would be added to
|
|
the results of the previous run(s). This is potentially useful in
|
|
several ways. For example, it could be used to accumulate data over a
|
|
number of program runs as part of a test verification suite, or to
|
|
provide more accurate long-term information over a large number of
|
|
program runs.
|
|
</P><P>
|
|
|
|
The data in the <CODE>.da</CODE> files is saved immediately before the program
|
|
exits. For each source file compiled with -fprofile-arcs, the profiling
|
|
code first attempts to read in an existing <CODE>.da</CODE> file; if the file
|
|
doesn't match the executable (differing number of basic block counts) it
|
|
will ignore the contents of the file. It then adds in the new execution
|
|
counts and finally writes the data to the file.
|
|
</P><P>
|
|
|
|
<A NAME="Gcov and Optimization"></A>
|
|
<HR SIZE="6">
|
|
<A NAME="SEC116"></A>
|
|
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
|
|
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC115" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC115"> < </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC117" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC117"> > </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC117" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC117"> << </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC113" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC113"> Up </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_7.html#SEC118" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_7.html#SEC118"> >> </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc.html#SEC_Top" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc.html#SEC_Top">Top</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_toc.html#SEC_Contents" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_toc.html#SEC_Contents">Contents</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_24.html#SEC261" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_24.html#SEC261">Index</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_abt.html#SEC_About" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_abt.html#SEC_About"> ? </A>]</TD>
|
|
</TR></TABLE>
|
|
<H2> 6.3 Using <CODE>gcov</CODE> with GCC Optimization </H2>
|
|
<!--docid::SEC116::-->
|
|
<P>
|
|
|
|
If you plan to use <CODE>gcov</CODE> to help optimize your code, you must
|
|
first compile your program with two special GNU CC options:
|
|
<SAMP>`-fprofile-arcs -ftest-coverage'</SAMP>. Aside from that, you can use any
|
|
other GNU CC options; but if you want to prove that every single line
|
|
in your program was executed, you should not compile with optimization
|
|
at the same time. On some machines the optimizer can eliminate some
|
|
simple code lines by combining them with other lines. For example, code
|
|
like this:
|
|
</P><P>
|
|
|
|
<TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre>if (a != b)
|
|
c = 1;
|
|
else
|
|
c = 0;
|
|
</FONT></pre></td></tr></table></P><P>
|
|
|
|
can be compiled into one instruction on some machines. In this case,
|
|
there is no way for <CODE>gcov</CODE> to calculate separate execution counts
|
|
for each line because there isn't separate code for each line. Hence
|
|
the <CODE>gcov</CODE> output looks like this if you compiled the program with
|
|
optimization:
|
|
</P><P>
|
|
|
|
<TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre> 100 if (a != b)
|
|
100 c = 1;
|
|
100 else
|
|
100 c = 0;
|
|
</FONT></pre></td></tr></table></P><P>
|
|
|
|
The output shows that this block of code, combined by optimization,
|
|
executed 100 times. In one sense this result is correct, because there
|
|
was only one instruction representing all four of these lines. However,
|
|
the output does not indicate how many times the result was 0 and how
|
|
many times the result was 1.
|
|
</P><P>
|
|
|
|
<A NAME="Gcov Data Files"></A>
|
|
<HR SIZE="6">
|
|
<A NAME="SEC117"></A>
|
|
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
|
|
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC116" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC116"> < </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_7.html#SEC118" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_7.html#SEC118"> > </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC113" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC113"> << </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC113" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC113"> Up </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_7.html#SEC118" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_7.html#SEC118"> >> </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc.html#SEC_Top" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc.html#SEC_Top">Top</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_toc.html#SEC_Contents" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_toc.html#SEC_Contents">Contents</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_24.html#SEC261" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_24.html#SEC261">Index</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_abt.html#SEC_About" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_abt.html#SEC_About"> ? </A>]</TD>
|
|
</TR></TABLE>
|
|
<H2> 6.4 Brief description of <CODE>gcov</CODE> data files </H2>
|
|
<!--docid::SEC117::-->
|
|
<P>
|
|
|
|
<CODE>gcov</CODE> uses three files for doing profiling. The names of these
|
|
files are derived from the original <EM>source</EM> file by substituting
|
|
the file suffix with either <CODE>.bb</CODE>, <CODE>.bbg</CODE>, or <CODE>.da</CODE>. All
|
|
of these files are placed in the same directory as the source file, and
|
|
contain data stored in a platform-independent method.
|
|
</P><P>
|
|
|
|
The <CODE>.bb</CODE> and <CODE>.bbg</CODE> files are generated when the source file
|
|
is compiled with the GNU CC <SAMP>`-ftest-coverage'</SAMP> option. The
|
|
<CODE>.bb</CODE> file contains a list of source files (including headers),
|
|
functions within those files, and line numbers corresponding to each
|
|
basic block in the source file.
|
|
</P><P>
|
|
|
|
The <CODE>.bb</CODE> file format consists of several lists of 4-byte integers
|
|
which correspond to the line numbers of each basic block in the
|
|
file. Each list is terminated by a line number of 0. A line number of -1
|
|
is used to designate that the source file name (padded to a 4-byte
|
|
boundary and followed by another -1) follows. In addition, a line number
|
|
of -2 is used to designate that the name of a function (also padded to a
|
|
4-byte boundary and followed by a -2) follows.
|
|
</P><P>
|
|
|
|
The <CODE>.bbg</CODE> file is used to reconstruct the program flow graph for
|
|
the source file. It contains a list of the program flow arcs (possible
|
|
branches taken from one basic block to another) for each function which,
|
|
in combination with the <CODE>.bb</CODE> file, enables gcov to reconstruct the
|
|
program flow.
|
|
</P><P>
|
|
|
|
In the <CODE>.bbg</CODE> file, the format is:
|
|
<TABLE><tr><td> </td><td class=smallexample><FONT SIZE=-1><pre> number of basic blocks for function #0 (4-byte number)
|
|
total number of arcs for function #0 (4-byte number)
|
|
count of arcs in basic block #0 (4-byte number)
|
|
destination basic block of arc #0 (4-byte number)
|
|
flag bits (4-byte number)
|
|
destination basic block of arc #1 (4-byte number)
|
|
flag bits (4-byte number)
|
|
...
|
|
destination basic block of arc #N (4-byte number)
|
|
flag bits (4-byte number)
|
|
count of arcs in basic block #1 (4-byte number)
|
|
destination basic block of arc #0 (4-byte number)
|
|
flag bits (4-byte number)
|
|
...
|
|
</FONT></pre></td></tr></table></P><P>
|
|
|
|
A -1 (stored as a 4-byte number) is used to separate each function's
|
|
list of basic blocks, and to verify that the file has been read
|
|
correctly.
|
|
</P><P>
|
|
|
|
The <CODE>.da</CODE> file is generated when a program containing object files
|
|
built with the GNU CC <SAMP>`-fprofile-arcs'</SAMP> option is executed. A
|
|
separate <CODE>.da</CODE> file is created for each source file compiled with
|
|
this option, and the name of the <CODE>.da</CODE> file is stored as an
|
|
absolute pathname in the resulting object file. This path name is
|
|
derived from the source file name by substituting a <CODE>.da</CODE> suffix.
|
|
</P><P>
|
|
|
|
The format of the <CODE>.da</CODE> file is fairly simple. The first 8-byte
|
|
number is the number of counts in the file, followed by the counts
|
|
(stored as 8-byte numbers). Each count corresponds to the number of
|
|
times each arc in the program is executed. The counts are cumulative;
|
|
each time the program is executed, it attemps to combine the existing
|
|
<CODE>.da</CODE> files with the new counts for this invocation of the
|
|
program. It ignores the contents of any <CODE>.da</CODE> files whose number of
|
|
arcs doesn't correspond to the current program, and merely overwrites
|
|
them instead.
|
|
</P><P>
|
|
|
|
All three of these files use the functions in <CODE>gcov-io.h</CODE> to store
|
|
integers; the functions in this header provide a machine-independent
|
|
mechanism for storing and retrieving data from a stream.
|
|
</P><P>
|
|
|
|
<A NAME="Trouble"></A>
|
|
<HR SIZE="6">
|
|
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
|
|
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_6.html#SEC113" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_6.html#SEC113"> << </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_7.html#SEC118" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_7.html#SEC118"> >> </A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc.html#SEC_Top" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc.html#SEC_Top">Top</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_toc.html#SEC_Contents" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_toc.html#SEC_Contents">Contents</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_24.html#SEC261" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_24.html#SEC261">Index</A>]</TD>
|
|
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="gcc_abt.html#SEC_About" tppabs="http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_abt.html#SEC_About"> ? </A>]</TD>
|
|
</TR></TABLE>
|
|
<BR>
|
|
<FONT SIZE="-1">
|
|
This document was generated
|
|
by <I>GCC Administrator</I> on <I>March, 17 2001</I>
|
|
using <A HREF="tppmsgs/msgs0.htm#1" tppabs="http://www.mathematik.uni-kl.de/~obachman/Texi2html"><I>texi2html</I></A>
|
|
|
|
</BODY>
|
|
</HTML>
|