OSDN Git Service

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