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