]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/gcov.texi
GCOV: add -j argument (human readable format).
[thirdparty/gcc.git] / gcc / doc / gcov.texi
1 @c Copyright (C) 1996-2017 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-2017 Free Software Foundation, Inc.
8
9 Permission is granted to copy, distribute and/or modify this document
10 under the terms of the GNU Free Documentation License, Version 1.3 or
11 any later version published by the Free Software Foundation; with the
12 Invariant Sections being ``GNU General Public License'' and ``Funding
13 Free Software'', the Front-Cover texts being (a) (see below), and with
14 the Back-Cover Texts being (b) (see below). A copy of the license is
15 included in the gfdl(7) man page.
16
17 (a) The FSF's Front-Cover Text is:
18
19 A GNU Manual
20
21 (b) The FSF's Back-Cover Text is:
22
23 You have freedom to copy and modify this GNU Manual, like GNU
24 software. Copies published by the Free Software Foundation raise
25 funds for GNU development.
26 @c man end
27 @c Set file name and title for the man page.
28 @setfilename gcov
29 @settitle coverage testing tool
30 @end ignore
31
32 @node Gcov
33 @chapter @command{gcov}---a Test Coverage Program
34
35 @command{gcov} is a tool you can use in conjunction with GCC to
36 test code coverage in your programs.
37
38 @menu
39 * Gcov Intro:: Introduction to gcov.
40 * Invoking Gcov:: How to use gcov.
41 * Gcov and Optimization:: Using gcov with GCC optimization.
42 * Gcov Data Files:: The files used by gcov.
43 * Cross-profiling:: Data file relocation.
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 @command{gcov}
112
113 @smallexample
114 gcov @r{[}@var{options}@r{]} @var{files}
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{-a}|@option{--all-blocks}]
123 [@option{-b}|@option{--branch-probabilities}]
124 [@option{-c}|@option{--branch-counts}]
125 [@option{-d}|@option{--display-progress}]
126 [@option{-f}|@option{--function-summaries}]
127 [@option{-i}|@option{--intermediate-format}]
128 [@option{-j}|@option{--human-readable}]
129 [@option{-k}|@option{--use-colors}]
130 [@option{-l}|@option{--long-file-names}]
131 [@option{-m}|@option{--demangled-names}]
132 [@option{-n}|@option{--no-output}]
133 [@option{-o}|@option{--object-directory} @var{directory|file}]
134 [@option{-p}|@option{--preserve-paths}]
135 [@option{-r}|@option{--relative-only}]
136 [@option{-s}|@option{--source-prefix} @var{directory}]
137 [@option{-u}|@option{--unconditional-branches}]
138 [@option{-x}|@option{--hash-filenames}]
139 @var{files}
140 @c man end
141 @c man begin SEEALSO
142 gpl(7), gfdl(7), fsf-funding(7), gcc(1) and the Info entry for @file{gcc}.
143 @c man end
144 @end ignore
145
146 @c man begin OPTIONS
147 @table @gcctabopt
148
149 @item -a
150 @itemx --all-blocks
151 Write individual execution counts for every basic block. Normally gcov
152 outputs execution counts only for the main blocks of a line. With this
153 option you can determine if blocks within a single line are not being
154 executed.
155
156 @item -b
157 @itemx --branch-probabilities
158 Write branch frequencies to the output file, and write branch summary
159 info to the standard output. This option allows you to see how often
160 each branch in your program was taken. Unconditional branches will not
161 be shown, unless the @option{-u} option is given.
162
163 @item -c
164 @itemx --branch-counts
165 Write branch frequencies as the number of branches taken, rather than
166 the percentage of branches taken.
167
168 @item -d
169 @itemx --display-progress
170 Display the progress on the standard output.
171
172 @item -f
173 @itemx --function-summaries
174 Output summaries for each function in addition to the file level summary.
175
176 @item -h
177 @itemx --help
178 Display help about using @command{gcov} (on the standard output), and
179 exit without doing any further processing.
180
181 @item -i
182 @itemx --intermediate-format
183 Output gcov file in an easy-to-parse intermediate text format that can
184 be used by @command{lcov} or other tools. The output is a single
185 @file{.gcov} file per @file{.gcda} file. No source code is required.
186
187 The format of the intermediate @file{.gcov} file is plain text with
188 one entry per line
189
190 @item -j
191 @itemx --human-readable
192 Write counts in human readable format (like 24k).
193
194 @smallexample
195 file:@var{source_file_name}
196 function:@var{line_number},@var{execution_count},@var{function_name}
197 lcount:@var{line number},@var{execution_count},@var{has_unexecuted_block}
198 branch:@var{line_number},@var{branch_coverage_type}
199
200 Where the @var{branch_coverage_type} is
201 notexec (Branch not executed)
202 taken (Branch executed and taken)
203 nottaken (Branch executed, but not taken)
204
205 There can be multiple @var{file} entries in an intermediate gcov
206 file. All entries following a @var{file} pertain to that source file
207 until the next @var{file} entry.
208 @end smallexample
209
210 Here is a sample when @option{-i} is used in conjunction with @option{-b} option:
211
212 @smallexample
213 file:array.cc
214 function:11,1,_Z3sumRKSt6vectorIPiSaIS0_EE
215 function:22,1,main
216 lcount:11,1,0
217 lcount:12,1,0
218 lcount:14,1,0
219 branch:14,taken
220 lcount:26,1,0
221 branch:28,nottaken
222 @end smallexample
223
224 @item -k
225 @itemx --use-colors
226
227 Use colors for lines of code that have zero coverage. We use red color for
228 non-exceptional lines and cyan for exceptional. Same colors are used for
229 basic blocks with @option{-a} option.
230
231
232 @item -l
233 @itemx --long-file-names
234 Create long file names for included source files. For example, if the
235 header file @file{x.h} contains code, and was included in the file
236 @file{a.c}, then running @command{gcov} on the file @file{a.c} will
237 produce an output file called @file{a.c##x.h.gcov} instead of
238 @file{x.h.gcov}. This can be useful if @file{x.h} is included in
239 multiple source files and you want to see the individual
240 contributions. If you use the @samp{-p} option, both the including
241 and included file names will be complete path names.
242
243 @item -m
244 @itemx --demangled-names
245 Display demangled function names in output. The default is to show
246 mangled function names.
247
248 @item -n
249 @itemx --no-output
250 Do not create the @command{gcov} output file.
251
252 @item -o @var{directory|file}
253 @itemx --object-directory @var{directory}
254 @itemx --object-file @var{file}
255 Specify either the directory containing the gcov data files, or the
256 object path name. The @file{.gcno}, and
257 @file{.gcda} data files are searched for using this option. If a directory
258 is specified, the data files are in that directory and named after the
259 input file name, without its extension. If a file is specified here,
260 the data files are named after that file, without its extension.
261
262 @item -p
263 @itemx --preserve-paths
264 Preserve complete path information in the names of generated
265 @file{.gcov} files. Without this option, just the filename component is
266 used. With this option, all directories are used, with @samp{/} characters
267 translated to @samp{#} characters, @file{.} directory components
268 removed and unremoveable @file{..}
269 components renamed to @samp{^}. This is useful if sourcefiles are in several
270 different directories.
271
272 @item -r
273 @itemx --relative-only
274 Only output information about source files with a relative pathname
275 (after source prefix elision). Absolute paths are usually system
276 header files and coverage of any inline functions therein is normally
277 uninteresting.
278
279 @item -s @var{directory}
280 @itemx --source-prefix @var{directory}
281 A prefix for source file names to remove when generating the output
282 coverage files. This option is useful when building in a separate
283 directory, and the pathname to the source directory is not wanted when
284 determining the output file names. Note that this prefix detection is
285 applied before determining whether the source file is absolute.
286
287 @item -u
288 @itemx --unconditional-branches
289 When branch probabilities are given, include those of unconditional branches.
290 Unconditional branches are normally not interesting.
291
292 @item -v
293 @itemx --version
294 Display the @command{gcov} version number (on the standard output),
295 and exit without doing any further processing.
296
297 @item -w
298 @itemx --verbose
299 Print verbose informations related to basic blocks and arcs.
300
301 @item -x
302 @itemx --hash-filenames
303 By default, gcov uses the full pathname of the source files to to create
304 an output filename. This can lead to long filenames that can overflow
305 filesystem limits. This option creates names of the form
306 @file{@var{source-file}##@var{md5}.gcov},
307 where the @var{source-file} component is the final filename part and
308 the @var{md5} component is calculated from the full mangled name that
309 would have been used otherwise.
310
311 @end table
312
313 @command{gcov} should be run with the current directory the same as that
314 when you invoked the compiler. Otherwise it will not be able to locate
315 the source files. @command{gcov} produces files called
316 @file{@var{mangledname}.gcov} in the current directory. These contain
317 the coverage information of the source file they correspond to.
318 One @file{.gcov} file is produced for each source (or header) file
319 containing code,
320 which was compiled to produce the data files. The @var{mangledname} part
321 of the output file name is usually simply the source file name, but can
322 be something more complicated if the @samp{-l} or @samp{-p} options are
323 given. Refer to those options for details.
324
325 If you invoke @command{gcov} with multiple input files, the
326 contributions from each input file are summed. Typically you would
327 invoke it with the same list of files as the final link of your executable.
328
329 The @file{.gcov} files contain the @samp{:} separated fields along with
330 program source code. The format is
331
332 @smallexample
333 @var{execution_count}:@var{line_number}:@var{source line text}
334 @end smallexample
335
336 Additional block information may succeed each line, when requested by
337 command line option. The @var{execution_count} is @samp{-} for lines
338 containing no code. Unexecuted lines are marked @samp{#####} or
339 @samp{====}, depending on whether they are reachable by
340 non-exceptional paths or only exceptional paths such as C++ exception
341 handlers, respectively. Given @samp{-a} option, unexecuted blocks are
342 marked @samp{$$$$$} or @samp{%%%%%}, depending on whether a basic block
343 is reachable via non-exceptional or exceptional paths.
344 Executed basic blocks having a statement with zero @var{execution_count}
345 end with @samp{*} character and are colored with magenta color with @option{-k}
346 option.
347
348 Note that GCC can completely remove the bodies of functions that are
349 not needed -- for instance if they are inlined everywhere. Such functions
350 are marked with @samp{-}, which can be confusing.
351 Use the @option{-fkeep-inline-functions} and @option{-fkeep-static-functions}
352 options to retain these functions and
353 allow gcov to properly show their @var{execution_count}.
354
355 Some lines of information at the start have @var{line_number} of zero.
356 These preamble lines are of the form
357
358 @smallexample
359 -:0:@var{tag}:@var{value}
360 @end smallexample
361
362 The ordering and number of these preamble lines will be augmented as
363 @command{gcov} development progresses --- do not rely on them remaining
364 unchanged. Use @var{tag} to locate a particular preamble line.
365
366 The additional block information is of the form
367
368 @smallexample
369 @var{tag} @var{information}
370 @end smallexample
371
372 The @var{information} is human readable, but designed to be simple
373 enough for machine parsing too.
374
375 When printing percentages, 0% and 100% are only printed when the values
376 are @emph{exactly} 0% and 100% respectively. Other values which would
377 conventionally be rounded to 0% or 100% are instead printed as the
378 nearest non-boundary value.
379
380 When using @command{gcov}, you must first compile your program with two
381 special GCC options: @samp{-fprofile-arcs -ftest-coverage}.
382 This tells the compiler to generate additional information needed by
383 gcov (basically a flow graph of the program) and also includes
384 additional code in the object files for generating the extra profiling
385 information needed by gcov. These additional files are placed in the
386 directory where the object file is located.
387
388 Running the program will cause profile output to be generated. For each
389 source file compiled with @option{-fprofile-arcs}, an accompanying
390 @file{.gcda} file will be placed in the object file directory.
391
392 Running @command{gcov} with your program's source file names as arguments
393 will now produce a listing of the code along with frequency of execution
394 for each line. For example, if your program is called @file{tmp.c}, this
395 is what you see when you use the basic @command{gcov} facility:
396
397 @smallexample
398 $ gcc -fprofile-arcs -ftest-coverage tmp.c
399 $ a.out
400 $ gcov tmp.c
401 File 'tmp.c'
402 Lines executed:90.00% of 10
403 Creating 'tmp.c.gcov'
404 @end smallexample
405
406 The file @file{tmp.c.gcov} contains output from @command{gcov}.
407 Here is a sample:
408
409 @smallexample
410 -: 0:Source:tmp.c
411 -: 0:Graph:tmp.gcno
412 -: 0:Data:tmp.gcda
413 -: 0:Runs:1
414 -: 0:Programs:1
415 -: 1:#include <stdio.h>
416 -: 2:
417 -: 3:int main (void)
418 1: 4:@{
419 1: 5: int i, total;
420 -: 6:
421 1: 7: total = 0;
422 -: 8:
423 11: 9: for (i = 0; i < 10; i++)
424 10: 10: total += i;
425 -: 11:
426 1: 12: if (total != 45)
427 #####: 13: printf ("Failure\n");
428 -: 14: else
429 1: 15: printf ("Success\n");
430 1: 16: return 0;
431 -: 17:@}
432 @end smallexample
433
434 When you use the @option{-a} option, you will get individual block
435 counts, and the output looks like this:
436
437 @smallexample
438 -: 0:Source:tmp.c
439 -: 0:Graph:tmp.gcno
440 -: 0:Data:tmp.gcda
441 -: 0:Runs:1
442 -: 0:Programs:1
443 -: 1:#include <stdio.h>
444 -: 2:
445 -: 3:int main (void)
446 1: 4:@{
447 1: 4-block 0
448 1: 5: int i, total;
449 -: 6:
450 1: 7: total = 0;
451 -: 8:
452 11: 9: for (i = 0; i < 10; i++)
453 11: 9-block 0
454 10: 10: total += i;
455 10: 10-block 0
456 -: 11:
457 1: 12: if (total != 45)
458 1: 12-block 0
459 #####: 13: printf ("Failure\n");
460 $$$$$: 13-block 0
461 -: 14: else
462 1: 15: printf ("Success\n");
463 1: 15-block 0
464 1: 16: return 0;
465 1: 16-block 0
466 -: 17:@}
467 @end smallexample
468
469 In this mode, each basic block is only shown on one line -- the last
470 line of the block. A multi-line block will only contribute to the
471 execution count of that last line, and other lines will not be shown
472 to contain code, unless previous blocks end on those lines.
473 The total execution count of a line is shown and subsequent lines show
474 the execution counts for individual blocks that end on that line. After each
475 block, the branch and call counts of the block will be shown, if the
476 @option{-b} option is given.
477
478 Because of the way GCC instruments calls, a call count can be shown
479 after a line with no individual blocks.
480 As you can see, line 13 contains a basic block that was not executed.
481
482 @need 450
483 When you use the @option{-b} option, your output looks like this:
484
485 @smallexample
486 $ gcov -b tmp.c
487 File 'tmp.c'
488 Lines executed:90.00% of 10
489 Branches executed:80.00% of 5
490 Taken at least once:80.00% of 5
491 Calls executed:50.00% of 2
492 Creating 'tmp.c.gcov'
493 @end smallexample
494
495 Here is a sample of a resulting @file{tmp.c.gcov} file:
496
497 @smallexample
498 -: 0:Source:tmp.c
499 -: 0:Graph:tmp.gcno
500 -: 0:Data:tmp.gcda
501 -: 0:Runs:1
502 -: 0:Programs:1
503 -: 1:#include <stdio.h>
504 -: 2:
505 -: 3:int main (void)
506 function main called 1 returned 1 blocks executed 75%
507 1: 4:@{
508 1: 5: int i, total;
509 -: 6:
510 1: 7: total = 0;
511 -: 8:
512 11: 9: for (i = 0; i < 10; i++)
513 branch 0 taken 91% (fallthrough)
514 branch 1 taken 9%
515 10: 10: total += i;
516 -: 11:
517 1: 12: if (total != 45)
518 branch 0 taken 0% (fallthrough)
519 branch 1 taken 100%
520 #####: 13: printf ("Failure\n");
521 call 0 never executed
522 -: 14: else
523 1: 15: printf ("Success\n");
524 call 0 called 1 returned 100%
525 1: 16: return 0;
526 -: 17:@}
527 @end smallexample
528
529 For each function, a line is printed showing how many times the function
530 is called, how many times it returns and what percentage of the
531 function's blocks were executed.
532
533 For each basic block, a line is printed after the last line of the basic
534 block describing the branch or call that ends the basic block. There can
535 be multiple branches and calls listed for a single source line if there
536 are multiple basic blocks that end on that line. In this case, the
537 branches and calls are each given a number. There is no simple way to map
538 these branches and calls back to source constructs. In general, though,
539 the lowest numbered branch or call will correspond to the leftmost construct
540 on the source line.
541
542 For a branch, if it was executed at least once, then a percentage
543 indicating the number of times the branch was taken divided by the
544 number of times the branch was executed will be printed. Otherwise, the
545 message ``never executed'' is printed.
546
547 For a call, if it was executed at least once, then a percentage
548 indicating the number of times the call returned divided by the number
549 of times the call was executed will be printed. This will usually be
550 100%, but may be less for functions that call @code{exit} or @code{longjmp},
551 and thus may not return every time they are called.
552
553 The execution counts are cumulative. If the example program were
554 executed again without removing the @file{.gcda} file, the count for the
555 number of times each line in the source was executed would be added to
556 the results of the previous run(s). This is potentially useful in
557 several ways. For example, it could be used to accumulate data over a
558 number of program runs as part of a test verification suite, or to
559 provide more accurate long-term information over a large number of
560 program runs.
561
562 The data in the @file{.gcda} files is saved immediately before the program
563 exits. For each source file compiled with @option{-fprofile-arcs}, the
564 profiling code first attempts to read in an existing @file{.gcda} file; if
565 the file doesn't match the executable (differing number of basic block
566 counts) it will ignore the contents of the file. It then adds in the
567 new execution counts and finally writes the data to the file.
568
569 @node Gcov and Optimization
570 @section Using @command{gcov} with GCC Optimization
571
572 If you plan to use @command{gcov} to help optimize your code, you must
573 first compile your program with two special GCC options:
574 @samp{-fprofile-arcs -ftest-coverage}. Aside from that, you can use any
575 other GCC options; but if you want to prove that every single line
576 in your program was executed, you should not compile with optimization
577 at the same time. On some machines the optimizer can eliminate some
578 simple code lines by combining them with other lines. For example, code
579 like this:
580
581 @smallexample
582 if (a != b)
583 c = 1;
584 else
585 c = 0;
586 @end smallexample
587
588 @noindent
589 can be compiled into one instruction on some machines. In this case,
590 there is no way for @command{gcov} to calculate separate execution counts
591 for each line because there isn't separate code for each line. Hence
592 the @command{gcov} output looks like this if you compiled the program with
593 optimization:
594
595 @smallexample
596 100: 12:if (a != b)
597 100: 13: c = 1;
598 100: 14:else
599 100: 15: c = 0;
600 @end smallexample
601
602 The output shows that this block of code, combined by optimization,
603 executed 100 times. In one sense this result is correct, because there
604 was only one instruction representing all four of these lines. However,
605 the output does not indicate how many times the result was 0 and how
606 many times the result was 1.
607
608 Inlineable functions can create unexpected line counts. Line counts are
609 shown for the source code of the inlineable function, but what is shown
610 depends on where the function is inlined, or if it is not inlined at all.
611
612 If the function is not inlined, the compiler must emit an out of line
613 copy of the function, in any object file that needs it. If
614 @file{fileA.o} and @file{fileB.o} both contain out of line bodies of a
615 particular inlineable function, they will also both contain coverage
616 counts for that function. When @file{fileA.o} and @file{fileB.o} are
617 linked together, the linker will, on many systems, select one of those
618 out of line bodies for all calls to that function, and remove or ignore
619 the other. Unfortunately, it will not remove the coverage counters for
620 the unused function body. Hence when instrumented, all but one use of
621 that function will show zero counts.
622
623 If the function is inlined in several places, the block structure in
624 each location might not be the same. For instance, a condition might
625 now be calculable at compile time in some instances. Because the
626 coverage of all the uses of the inline function will be shown for the
627 same source lines, the line counts themselves might seem inconsistent.
628
629 Long-running applications can use the @code{__gcov_reset} and @code{__gcov_dump}
630 facilities to restrict profile collection to the program region of
631 interest. Calling @code{__gcov_reset(void)} will clear all profile counters
632 to zero, and calling @code{__gcov_dump(void)} will cause the profile information
633 collected at that point to be dumped to @file{.gcda} output files.
634 Instrumented applications use a static destructor with priority 99
635 to invoke the @code{__gcov_dump} function. Thus @code{__gcov_dump}
636 is executed after all user defined static destructors,
637 as well as handlers registered with @code{atexit}.
638
639 @c man end
640
641 @node Gcov Data Files
642 @section Brief Description of @command{gcov} Data Files
643
644 @command{gcov} uses two files for profiling. The names of these files
645 are derived from the original @emph{object} file by substituting the
646 file suffix with either @file{.gcno}, or @file{.gcda}. The files
647 contain coverage and profile data stored in a platform-independent format.
648 The @file{.gcno} files are placed in the same directory as the object
649 file. By default, the @file{.gcda} files are also stored in the same
650 directory as the object file, but the GCC @option{-fprofile-dir} option
651 may be used to store the @file{.gcda} files in a separate directory.
652
653 The @file{.gcno} notes file is generated when the source file is compiled
654 with the GCC @option{-ftest-coverage} option. It contains information to
655 reconstruct the basic block graphs and assign source line numbers to
656 blocks.
657
658 The @file{.gcda} count data file is generated when a program containing
659 object files built with the GCC @option{-fprofile-arcs} option is executed.
660 A separate @file{.gcda} file is created for each object file compiled with
661 this option. It contains arc transition counts, value profile counts, and
662 some summary information.
663
664 The full details of the file format is specified in @file{gcov-io.h},
665 and functions provided in that header file should be used to access the
666 coverage files.
667
668 @node Cross-profiling
669 @section Data File Relocation to Support Cross-Profiling
670
671 Running the program will cause profile output to be generated. For each
672 source file compiled with @option{-fprofile-arcs}, an accompanying @file{.gcda}
673 file will be placed in the object file directory. That implicitly requires
674 running the program on the same system as it was built or having the same
675 absolute directory structure on the target system. The program will try
676 to create the needed directory structure, if it is not already present.
677
678 To support cross-profiling, a program compiled with @option{-fprofile-arcs}
679 can relocate the data files based on two environment variables:
680
681 @itemize @bullet
682 @item
683 GCOV_PREFIX contains the prefix to add to the absolute paths
684 in the object file. Prefix can be absolute, or relative. The
685 default is no prefix.
686
687 @item
688 GCOV_PREFIX_STRIP indicates the how many initial directory names to strip off
689 the hardwired absolute paths. Default value is 0.
690
691 @emph{Note:} If GCOV_PREFIX_STRIP is set without GCOV_PREFIX is undefined,
692 then a relative path is made out of the hardwired absolute paths.
693 @end itemize
694
695 For example, if the object file @file{/user/build/foo.o} was built with
696 @option{-fprofile-arcs}, the final executable will try to create the data file
697 @file{/user/build/foo.gcda} when running on the target system. This will
698 fail if the corresponding directory does not exist and it is unable to create
699 it. This can be overcome by, for example, setting the environment as
700 @samp{GCOV_PREFIX=/target/run} and @samp{GCOV_PREFIX_STRIP=1}. Such a
701 setting will name the data file @file{/target/run/build/foo.gcda}.
702
703 You must move the data files to the expected directory tree in order to
704 use them for profile directed optimizations (@option{-fprofile-use}), or to
705 use the @command{gcov} tool.