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