OSDN Git Service

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