OSDN Git Service

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