OSDN Git Service

* Makefile.in (start.encap): Do not depend on LIBGCC1.
[pf3gnuchains/gcc-fork.git] / gcc / gcov.texi
1 @c Copyright (C) 1996, 1997 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 @node Gcov
6 @chapter @code{gcov}: a Test Coverage Program
7
8 @code{gcov} is a tool you can use in conjunction with @sc{gnu} CC to
9 test code coverage in your programs.
10
11 This chapter describes version 1.5 of @code{gcov}.
12
13 @menu
14 * Gcov Intro::                  Introduction to gcov.
15 * Invoking Gcov::               How to use gcov.
16 * Gcov and Optimization::       Using gcov with GCC optimization.
17 * Gcov Data Files::             The files used by gcov.
18 @end menu
19
20 @node Gcov Intro
21 @section Introduction to @code{gcov}
22
23 @code{gcov} is a test coverage program.  Use it in concert with @sc{gnu}
24 CC to analyze your programs to help create more efficient, faster
25 running code.  You can use @code{gcov} as a profiling tool to help
26 discover where your optimization efforts will best affect your code.  You
27 can also use @code{gcov} along with the other profiling tool,
28 @code{gprof}, to assess which parts of your code use the greatest amount
29 of computing time.
30
31 Profiling tools help you analyze your code's performance.  Using a
32 profiler such as @code{gcov} or @code{gprof}, you can find out some
33 basic performance statistics, such as:
34
35 @itemize @bullet
36 @item
37 how often each line of code executes
38
39 @item
40 what lines of code are actually executed
41
42 @item
43 how much computing time each section of code uses
44 @end itemize
45
46 Once you know these things about how your code works when compiled, you
47 can look at each module to see which modules should be optimized.
48 @code{gcov} helps you determine where to work on optimization.
49
50 Software developers also use coverage testing in concert with
51 testsuites, to make sure software is actually good enough for a release.
52 Testsuites can verify that a program works as expected; a coverage
53 program tests to see how much of the program is exercised by the
54 testsuite.  Developers can then determine what kinds of test cases need
55 to be added to the testsuites to create both better testing and a better
56 final product.
57
58 You should compile your code without optimization if you plan to use
59 @code{gcov} because the optimization, by combining some lines of code
60 into one function, may not give you as much information as you need to
61 look for `hot spots' where the code is using a great deal of computer
62 time.  Likewise, because @code{gcov} accumulates statistics by line (at
63 the lowest resolution), it works best with a programming style that
64 places only one statement on each line.  If you use complicated macros
65 that expand to loops or to other control structures, the statistics are
66 less helpful---they only report on the line where the macro call
67 appears.  If your complex macros behave like functions, you can replace
68 them with inline functions to solve this problem.
69
70 @code{gcov} creates a logfile called @file{@var{sourcefile}.gcov} which
71 indicates how many times each line of a source file @file{@var{sourcefile}.c}
72 has executed.  You can use these logfiles along with @code{gprof} to aid
73 in fine-tuning the performance of your programs.  @code{gprof} gives
74 timing information you can use along with the information you get from
75 @code{gcov}.
76
77 @code{gcov} works only on code compiled with @sc{gnu} CC.  It is not
78 compatible with any other profiling or test coverage mechanism.
79
80 @node Invoking Gcov
81 @section Invoking gcov
82
83 @smallexample
84 gcov [-b] [-c] [-v] [-n] [-l] [-f] [-o directory] @var{sourcefile}
85 @end smallexample
86
87 @table @code
88 @item -b 
89 Write branch frequencies to the output file, and write branch summary
90 info to the standard output.  This option allows you to see how often
91 each branch in your program was taken.
92
93 @item -c
94 Write branch frequencies as the number of branches taken, rather than 
95 the percentage of branches taken.
96
97 @item -v
98 Display the @code{gcov} version number (on the standard error stream).
99
100 @item -n
101 Do not create the @code{gcov} output file.
102
103 @item -l
104 Create long file names for included source files.  For example, if the
105 header file @samp{x.h} contains code, and was included in the file
106 @samp{a.c}, then running @code{gcov} on the file @samp{a.c} will produce
107 an output file called @samp{a.c.x.h.gcov} instead of @samp{x.h.gcov}.
108 This can be useful if @samp{x.h} is included in multiple source files.
109
110 @item -f
111 Output summaries for each function in addition to the file level summary.
112
113 @item -o
114 The directory where the object files live.  Gcov will search for @code{.bb},
115 @code{.bbg}, and @code{.da} files in this directory.
116 @end table
117
118 @need 3000
119 When using @code{gcov}, you must first compile your program with two
120 special @sc{gnu} CC options: @samp{-fprofile-arcs -ftest-coverage}.
121 This tells the compiler to generate additional information needed by
122 gcov (basically a flow graph of the program) and also includes
123 additional code in the object files for generating the extra profiling
124 information needed by gcov.  These additional files are placed in the
125 directory where the source code is located.
126
127 Running the program will cause profile output to be generated.  For each
128 source file compiled with -fprofile-arcs, an accompanying @code{.da}
129 file will be placed in the source directory.
130
131 Running @code{gcov} with your program's source file names as arguments
132 will now produce a listing of the code along with frequency of execution
133 for each line.  For example, if your program is called @samp{tmp.c}, this
134 is what you see when you use the basic @code{gcov} facility:
135
136 @smallexample
137 $ gcc -fprofile-arcs -ftest-coverage tmp.c
138 $ a.out
139 $ gcov tmp.c
140  87.50% of 8 source lines executed in file tmp.c
141 Creating tmp.c.gcov.
142 @end smallexample
143
144 The file @file{tmp.c.gcov} contains output from @code{gcov}. 
145 Here is a sample:
146
147 @smallexample
148                 main()
149                 @{
150            1      int i, total;
151                 
152            1      total = 0;
153                 
154           11      for (i = 0; i < 10; i++)
155           10        total += i;
156                 
157            1      if (total != 45)
158       ######        printf ("Failure\n");
159                   else
160            1        printf ("Success\n");
161            1    @}
162 @end smallexample
163
164 @need 450
165 When you use the @samp{-b} option, your output looks like this:
166
167 @smallexample
168 $ gcov -b tmp.c
169  87.50% of 8 source lines executed in file tmp.c
170  80.00% of 5 branches executed in file tmp.c
171  80.00% of 5 branches taken at least once in file tmp.c
172  50.00% of 2 calls executed in file tmp.c
173 Creating tmp.c.gcov.
174 @end smallexample
175
176 Here is a sample of a resulting @file{tmp.c.gcov} file:
177
178 @smallexample
179                 main()
180                 @{
181            1      int i, total;
182                 
183            1      total = 0;
184                 
185           11      for (i = 0; i < 10; i++)
186 branch 0 taken = 91%
187 branch 1 taken = 100%
188 branch 2 taken = 100%
189           10        total += i;
190                 
191            1      if (total != 45)
192 branch 0 taken = 100%
193       ######        printf ("Failure\n");
194 call 0 never executed
195 branch 1 never executed
196                   else
197            1        printf ("Success\n");
198 call 0 returns = 100%
199            1    @}
200 @end smallexample
201
202 For each basic block, a line is printed after the last line of the basic
203 block describing the branch or call that ends the basic block.  There can
204 be multiple branches and calls listed for a single source line if there
205 are multiple basic blocks that end on that line.  In this case, the
206 branches and calls are each given a number.  There is no simple way to map
207 these branches and calls back to source constructs.  In general, though,
208 the lowest numbered branch or call will correspond to the leftmost construct
209 on the source line.
210
211 For a branch, if it was executed at least once, then a percentage
212 indicating the number of times the branch was taken divided by the
213 number of times the branch was executed will be printed.  Otherwise, the
214 message ``never executed'' is printed.
215
216 For a call, if it was executed at least once, then a percentage
217 indicating the number of times the call returned divided by the number
218 of times the call was executed will be printed.  This will usually be
219 100%, but may be less for functions call @code{exit} or @code{longjmp},
220 and thus may not return everytime they are called.
221
222 The execution counts are cumulative.  If the example program were
223 executed again without removing the @code{.da} file, the count for the
224 number of times each line in the source was executed would be added to
225 the results of the previous run(s).  This is potentially useful in
226 several ways.  For example, it could be used to accumulate data over a
227 number of program runs as part of a test verification suite, or to
228 provide more accurate long-term information over a large number of
229 program runs.
230
231 The data in the @code{.da} files is saved immediately before the program
232 exits.  For each source file compiled with -fprofile-arcs, the profiling
233 code first attempts to read in an existing @code{.da} file; if the file
234 doesn't match the executable (differing number of basic block counts) it
235 will ignore the contents of the file.  It then adds in the new execution
236 counts and finally writes the data to the file.
237
238 @node Gcov and Optimization
239 @section Using @code{gcov} with GCC Optimization
240
241 If you plan to use @code{gcov} to help optimize your code, you must
242 first compile your program with two special @sc{gnu} CC options:
243 @samp{-fprofile-arcs -ftest-coverage}.  Aside from that, you can use any
244 other @sc{gnu} CC options; but if you want to prove that every single line
245 in your program was executed, you should not compile with optimization
246 at the same time.  On some machines the optimizer can eliminate some
247 simple code lines by combining them with other lines.  For example, code
248 like this:
249
250 @smallexample
251 if (a != b)
252   c = 1;
253 else
254   c = 0;
255 @end smallexample
256
257 @noindent
258 can be compiled into one instruction on some machines.  In this case,
259 there is no way for @code{gcov} to calculate separate execution counts
260 for each line because there isn't separate code for each line.  Hence
261 the @code{gcov} output looks like this if you compiled the program with
262 optimization:
263
264 @smallexample
265       100  if (a != b)
266       100    c = 1;
267       100  else
268       100    c = 0;
269 @end smallexample
270
271 The output shows that this block of code, combined by optimization,
272 executed 100 times.  In one sense this result is correct, because there
273 was only one instruction representing all four of these lines.  However,
274 the output does not indicate how many times the result was 0 and how
275 many times the result was 1.
276
277 @node Gcov Data Files
278 @section Brief description of @code{gcov} data files
279
280 @code{gcov} uses three files for doing profiling.  The names of these
281 files are derived from the original @emph{source} file by substituting
282 the file suffix with either @code{.bb}, @code{.bbg}, or @code{.da}.  All
283 of these files are placed in the same directory as the source file, and
284 contain data stored in a platform-independent method.
285
286 The @code{.bb} and @code{.bbg} files are generated when the source file
287 is compiled with the @sc{gnu} CC @samp{-ftest-coverage} option.  The
288 @code{.bb} file contains a list of source files (including headers),
289 functions within those files, and line numbers corresponding to each
290 basic block in the source file.
291
292 The @code{.bb} file format consists of several lists of 4-byte integers
293 which correspond to the line numbers of each basic block in the
294 file.  Each list is terminated by a line number of 0.  A line number of -1
295 is used to designate that the source file name (padded to a 4-byte
296 boundary and followed by another -1) follows.  In addition, a line number
297 of -2 is used to designate that the name of a function (also padded to a
298 4-byte boundary and followed by a -2) follows.
299
300 The @code{.bbg} file is used to reconstruct the program flow graph for
301 the source file.  It contains a list of the program flow arcs (possible
302 branches taken from one basic block to another) for each function which,
303 in combination with the @code{.bb} file, enables gcov to reconstruct the
304 program flow.
305
306 In the @code{.bbg} file, the format is:
307 @smallexample
308         number of basic blocks for function #0 (4-byte number)
309         total number of arcs for function #0 (4-byte number)
310         count of arcs in basic block #0 (4-byte number)
311         destination basic block of arc #0 (4-byte number)
312         flag bits (4-byte number)
313         destination basic block of arc #1 (4-byte number)
314         flag bits (4-byte number)
315         ...
316         destination basic block of arc #N (4-byte number)
317         flag bits (4-byte number)
318         count of arcs in basic block #1 (4-byte number)
319         destination basic block of arc #0 (4-byte number)
320         flag bits (4-byte number)
321         ...
322 @end smallexample
323
324 A -1 (stored as a 4-byte number) is used to separate each function's
325 list of basic blocks, and to verify that the file has been read
326 correctly.
327
328 The @code{.da} file is generated when a program containing object files
329 built with the @sc{gnu} CC @samp{-fprofile-arcs} option is executed.  A
330 separate @code{.da} file is created for each source file compiled with
331 this option, and the name of the @code{.da} file is stored as an
332 absolute pathname in the resulting object file.  This path name is
333 derived from the source file name by substituting a @code{.da} suffix.
334
335 The format of the @code{.da} file is fairly simple.  The first 8-byte
336 number is the number of counts in the file, followed by the counts
337 (stored as 8-byte numbers).  Each count corresponds to the number of
338 times each arc in the program is executed.  The counts are cumulative;
339 each time the program is executed, it attemps to combine the existing
340 @code{.da} files with the new counts for this invocation of the
341 program.  It ignores the contents of any @code{.da} files whose number of
342 arcs doesn't correspond to the current program, and merely overwrites
343 them instead.
344
345 All three of these files use the functions in @code{gcov-io.h} to store
346 integers; the functions in this header provide a machine-independent
347 mechanism for storing and retrieving data from a stream.
348