OSDN Git Service

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