1 /* Gcov.c: prepend line execution counts and branch probabilities to a
3 Copyright (C) 1990-2015 Free Software Foundation, Inc.
4 Contributed by James E. Wilson of Cygnus Support.
5 Mangled by Bob Manson of Cygnus Support.
6 Mangled further by Nathan Sidwell <nathan@codesourcery.com>
8 Gcov is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 Gcov is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Gcov; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* ??? Print a list of the ten blocks with the highest execution counts,
23 and list the line numbers corresponding to those blocks. Also, perhaps
24 list the line numbers with the highest execution counts, only printing
25 the first if there are several which are all listed in the same block. */
27 /* ??? Should have an option to print the number of basic blocks, and the
28 percent of them that are covered. */
30 /* Need an option to show individual block counts, and show
31 probabilities of fall through arcs. */
35 #include "coretypes.h"
38 #include "diagnostic.h"
48 /* The gcno file is generated by -ftest-coverage option. The gcda file is
49 generated by a program compiled with -fprofile-arcs. Their formats
50 are documented in gcov-io.h. */
52 /* The functions in this file for creating and solution program flow graphs
53 are very similar to functions in the gcc source file profile.c. In
54 some places we make use of the knowledge of how profile.c works to
55 select particular algorithms here. */
57 /* The code validates that the profile information read in corresponds
58 to the code currently being compiled. Rather than checking for
59 identical files, the code below compares a checksum on the CFG
60 (based on the order of basic blocks and the arcs in the CFG). If
61 the CFG checksum in the gcda file match the CFG checksum in the
62 gcno file, the profile data will be used. */
64 /* This is the size of the buffer used to read in source file lines. */
70 /* Describes an arc between two basic blocks. */
72 typedef struct arc_info
74 /* source and destination blocks. */
75 struct block_info
*src
;
76 struct block_info
*dst
;
78 /* transition counts. */
80 /* used in cycle search, so that we do not clobber original counts. */
83 unsigned int count_valid
: 1;
84 unsigned int on_tree
: 1;
85 unsigned int fake
: 1;
86 unsigned int fall_through
: 1;
88 /* Arc to a catch handler. */
89 unsigned int is_throw
: 1;
91 /* Arc is for a function that abnormally returns. */
92 unsigned int is_call_non_return
: 1;
94 /* Arc is for catch/setjmp. */
95 unsigned int is_nonlocal_return
: 1;
97 /* Is an unconditional branch. */
98 unsigned int is_unconditional
: 1;
100 /* Loop making arc. */
101 unsigned int cycle
: 1;
103 /* Next branch on line. */
104 struct arc_info
*line_next
;
106 /* Links to next arc on src and dst lists. */
107 struct arc_info
*succ_next
;
108 struct arc_info
*pred_next
;
111 /* Describes a basic block. Contains lists of arcs to successor and
112 predecessor blocks. */
114 typedef struct block_info
116 /* Chain of exit and entry arcs. */
120 /* Number of unprocessed exit and entry arcs. */
124 /* Block execution count. */
127 unsigned count_valid
: 1;
128 unsigned valid_chain
: 1;
129 unsigned invalid_chain
: 1;
130 unsigned exceptional
: 1;
132 /* Block is a call instrumenting site. */
133 unsigned is_call_site
: 1; /* Does the call. */
134 unsigned is_call_return
: 1; /* Is the return. */
136 /* Block is a landing pad for longjmp or throw. */
137 unsigned is_nonlocal_return
: 1;
143 /* Array of line numbers and source files. source files are
144 introduced by a linenumber of zero, the next 'line number' is
145 the number of the source file. Always starts with a source
149 } line
; /* Valid until blocks are linked onto lines */
152 /* Single line graph cycle workspace. Used for all-blocks
156 } cycle
; /* Used in all-blocks mode, after blocks are linked onto
160 /* Temporary chain for solving graph, and for chaining blocks on one
162 struct block_info
*chain
;
166 /* Describes a single function. Contains an array of basic blocks. */
168 typedef struct function_info
170 /* Name of function. */
172 char *demangled_name
;
174 unsigned lineno_checksum
;
175 unsigned cfg_checksum
;
177 /* The graph contains at least one fake incoming edge. */
178 unsigned has_catch
: 1;
180 /* Array of basic blocks. Like in GCC, the entry block is
181 at blocks[0] and the exit block is at blocks[1]. */
182 #define ENTRY_BLOCK (0)
183 #define EXIT_BLOCK (1)
186 unsigned blocks_executed
;
188 /* Raw arc coverage counts. */
192 /* First line number & file. */
196 /* Next function in same source file. */
197 struct function_info
*line_next
;
200 struct function_info
*next
;
203 /* Describes coverage of a file or function. */
205 typedef struct coverage_info
211 int branches_executed
;
220 /* Describes a single line of source. Contains a chain of basic blocks
223 typedef struct line_info
225 gcov_type count
; /* execution count */
228 arc_t
*branches
; /* branches from blocks that end on this
229 line. Used for branch-counts when not
231 block_t
*blocks
; /* blocks which start on this line. Used
232 in all-blocks mode. */
235 unsigned unexceptional
: 1;
238 /* Describes a file mentioned in the block graph. Contains an array
241 typedef struct source_info
243 /* Canonical name of source file. */
247 /* Array of line information. */
253 /* Functions in this source file. These are in ascending line
255 function_t
*functions
;
258 typedef struct name_map
260 char *name
; /* Source file name */
261 unsigned src
; /* Source file */
264 /* Holds a list of function basic block graphs. */
266 static function_t
*functions
;
267 static function_t
**fn_end
= &functions
;
269 static source_t
*sources
; /* Array of source files */
270 static unsigned n_sources
; /* Number of sources */
271 static unsigned a_sources
; /* Allocated sources */
273 static name_map_t
*names
; /* Mapping of file names to sources */
274 static unsigned n_names
; /* Number of names */
275 static unsigned a_names
; /* Allocated names */
277 /* This holds data summary information. */
279 static unsigned object_runs
;
280 static unsigned program_count
;
282 static unsigned total_lines
;
283 static unsigned total_executed
;
285 /* Modification time of graph file. */
287 static time_t bbg_file_time
;
289 /* Name of the notes (gcno) output file. The "bbg" prefix is for
290 historical reasons, when the notes file contained only the
291 basic block graph notes. */
293 static char *bbg_file_name
;
295 /* Stamp of the bbg file */
296 static unsigned bbg_stamp
;
298 /* Name and file pointer of the input file for the count data (gcda). */
300 static char *da_file_name
;
302 /* Data file is missing. */
304 static int no_data_file
;
306 /* If there is several input files, compute and display results after
307 reading all data files. This way if two or more gcda file refer to
308 the same source file (eg inline subprograms in a .h file), the
311 static int multiple_files
= 0;
313 /* Output branch probabilities. */
315 static int flag_branches
= 0;
317 /* Show unconditional branches too. */
318 static int flag_unconditional
= 0;
320 /* Output a gcov file if this is true. This is on by default, and can
321 be turned off by the -n option. */
323 static int flag_gcov_file
= 1;
325 /* Output progress indication if this is true. This is off by default
326 and can be turned on by the -d option. */
328 static int flag_display_progress
= 0;
330 /* Output *.gcov file in intermediate format used by 'lcov'. */
332 static int flag_intermediate_format
= 0;
334 /* Output demangled function names. */
336 static int flag_demangled_names
= 0;
338 /* For included files, make the gcov output file name include the name
339 of the input source file. For example, if x.h is included in a.c,
340 then the output file name is a.c##x.h.gcov instead of x.h.gcov. */
342 static int flag_long_names
= 0;
344 /* Output count information for every basic block, not merely those
345 that contain line number information. */
347 static int flag_all_blocks
= 0;
349 /* Output summary info for each function. */
351 static int flag_function_summary
= 0;
353 /* Object directory file prefix. This is the directory/file where the
354 graph and data files are looked for, if nonzero. */
356 static char *object_directory
= 0;
358 /* Source directory prefix. This is removed from source pathnames
359 that match, when generating the output file name. */
361 static char *source_prefix
= 0;
362 static size_t source_length
= 0;
364 /* Only show data for sources with relative pathnames. Absolute ones
365 usually indicate a system header file, which although it may
366 contain inline functions, is usually uninteresting. */
367 static int flag_relative_only
= 0;
369 /* Preserve all pathname components. Needed when object files and
370 source files are in subdirectories. '/' is mangled as '#', '.' is
371 elided and '..' mangled to '^'. */
373 static int flag_preserve_paths
= 0;
375 /* Output the number of times a branch was taken as opposed to the percentage
376 of times it was taken. */
378 static int flag_counts
= 0;
380 /* Forward declarations. */
381 static int process_args (int, char **);
382 static void print_usage (int) ATTRIBUTE_NORETURN
;
383 static void print_version (void) ATTRIBUTE_NORETURN
;
384 static void process_file (const char *);
385 static void generate_results (const char *);
386 static void create_file_names (const char *);
387 static int name_search (const void *, const void *);
388 static int name_sort (const void *, const void *);
389 static char *canonicalize_name (const char *);
390 static unsigned find_source (const char *);
391 static function_t
*read_graph_file (void);
392 static int read_count_file (function_t
*);
393 static void solve_flow_graph (function_t
*);
394 static void find_exception_blocks (function_t
*);
395 static void add_branch_counts (coverage_t
*, const arc_t
*);
396 static void add_line_counts (coverage_t
*, function_t
*);
397 static void executed_summary (unsigned, unsigned);
398 static void function_summary (const coverage_t
*, const char *);
399 static const char *format_gcov (gcov_type
, gcov_type
, int);
400 static void accumulate_line_counts (source_t
*);
401 static void output_gcov_file (const char *, source_t
*);
402 static int output_branch_count (FILE *, int, const arc_t
*);
403 static void output_lines (FILE *, const source_t
*);
404 static char *make_gcov_file_name (const char *, const char *);
405 static char *mangle_name (const char *, char *);
406 static void release_structures (void);
407 static void release_function (function_t
*);
408 extern int main (int, char **);
411 main (int argc
, char **argv
)
417 p
= argv
[0] + strlen (argv
[0]);
418 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
422 xmalloc_set_program_name (progname
);
424 /* Unlock the stdio streams. */
425 unlock_std_streams ();
429 diagnostic_initialize (global_dc
, 0);
431 /* Handle response files. */
432 expandargv (&argc
, &argv
);
435 names
= XNEWVEC (name_map_t
, a_names
);
437 sources
= XNEWVEC (source_t
, a_sources
);
439 argno
= process_args (argc
, argv
);
443 if (argc
- argno
> 1)
448 for (; argno
!= argc
; argno
++)
450 if (flag_display_progress
)
451 printf ("Processing file %d out of %d\n",
452 argno
- first_arg
+ 1, argc
- first_arg
);
453 process_file (argv
[argno
]);
456 generate_results (multiple_files
? NULL
: argv
[argc
- 1]);
458 release_structures ();
463 /* Print a usage message and exit. If ERROR_P is nonzero, this is an error,
464 otherwise the output of --help. */
467 print_usage (int error_p
)
469 FILE *file
= error_p
? stderr
: stdout
;
470 int status
= error_p
? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
;
472 fnotice (file
, "Usage: gcov [OPTION]... SOURCE|OBJ...\n\n");
473 fnotice (file
, "Print code coverage information.\n\n");
474 fnotice (file
, " -h, --help Print this help, then exit\n");
475 fnotice (file
, " -a, --all-blocks Show information for every basic block\n");
476 fnotice (file
, " -b, --branch-probabilities Include branch probabilities in output\n");
477 fnotice (file
, " -c, --branch-counts Output counts of branches taken\n\
478 rather than percentages\n");
479 fnotice (file
, " -d, --display-progress Display progress information\n");
480 fnotice (file
, " -f, --function-summaries Output summaries for each function\n");
481 fnotice (file
, " -i, --intermediate-format Output .gcov file in intermediate text format\n");
482 fnotice (file
, " -l, --long-file-names Use long output file names for included\n\
484 fnotice (file
, " -m, --demangled-names Output demangled function names\n");
485 fnotice (file
, " -n, --no-output Do not create an output file\n");
486 fnotice (file
, " -o, --object-directory DIR|FILE Search for object files in DIR or called FILE\n");
487 fnotice (file
, " -p, --preserve-paths Preserve all pathname components\n");
488 fnotice (file
, " -r, --relative-only Only show data for relative sources\n");
489 fnotice (file
, " -s, --source-prefix DIR Source prefix to elide\n");
490 fnotice (file
, " -u, --unconditional-branches Show unconditional branch counts too\n");
491 fnotice (file
, " -v, --version Print version number, then exit\n");
492 fnotice (file
, "\nFor bug reporting instructions, please see:\n%s.\n",
497 /* Print version information and exit. */
502 fnotice (stdout
, "gcov %s%s\n", pkgversion_string
, version_string
);
503 fprintf (stdout
, "Copyright %s 2015 Free Software Foundation, Inc.\n",
506 _("This is free software; see the source for copying conditions.\n"
507 "There is NO warranty; not even for MERCHANTABILITY or \n"
508 "FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
509 exit (SUCCESS_EXIT_CODE
);
512 static const struct option options
[] =
514 { "help", no_argument
, NULL
, 'h' },
515 { "version", no_argument
, NULL
, 'v' },
516 { "all-blocks", no_argument
, NULL
, 'a' },
517 { "branch-probabilities", no_argument
, NULL
, 'b' },
518 { "branch-counts", no_argument
, NULL
, 'c' },
519 { "intermediate-format", no_argument
, NULL
, 'i' },
520 { "no-output", no_argument
, NULL
, 'n' },
521 { "long-file-names", no_argument
, NULL
, 'l' },
522 { "function-summaries", no_argument
, NULL
, 'f' },
523 { "demangled-names", no_argument
, NULL
, 'm' },
524 { "preserve-paths", no_argument
, NULL
, 'p' },
525 { "relative-only", no_argument
, NULL
, 'r' },
526 { "object-directory", required_argument
, NULL
, 'o' },
527 { "object-file", required_argument
, NULL
, 'o' },
528 { "source-prefix", required_argument
, NULL
, 's' },
529 { "unconditional-branches", no_argument
, NULL
, 'u' },
530 { "display-progress", no_argument
, NULL
, 'd' },
534 /* Process args, return index to first non-arg. */
537 process_args (int argc
, char **argv
)
541 while ((opt
= getopt_long (argc
, argv
, "abcdfhilmno:s:pruv", options
, NULL
)) !=
556 flag_function_summary
= 1;
560 /* print_usage will exit. */
565 flag_demangled_names
= 1;
571 object_directory
= optarg
;
574 source_prefix
= optarg
;
575 source_length
= strlen (source_prefix
);
578 flag_relative_only
= 1;
581 flag_preserve_paths
= 1;
584 flag_unconditional
= 1;
587 flag_intermediate_format
= 1;
591 flag_display_progress
= 1;
595 /* print_version will exit. */
598 /* print_usage will exit. */
605 /* Get the name of the gcov file. The return value must be free'd.
607 It appends the '.gcov' extension to the *basename* of the file.
608 The resulting file name will be in PWD.
611 input: foo.da, output: foo.da.gcov
612 input: a/b/foo.cc, output: foo.cc.gcov */
615 get_gcov_intermediate_filename (const char *file_name
)
617 const char *gcov
= ".gcov";
621 /* Find the 'basename'. */
622 cptr
= lbasename (file_name
);
624 result
= XNEWVEC (char, strlen (cptr
) + strlen (gcov
) + 1);
625 sprintf (result
, "%s%s", cptr
, gcov
);
630 /* Output the result in intermediate format used by 'lcov'.
632 The intermediate format contains a single file named 'foo.cc.gcov',
633 with no source code included. A sample output is
638 function:19,1,_GLOBAL__sub_I__Z3foov
639 function:19,1,_Z41__static_initialization_and_destruction_0ii
646 file:/.../basic_ios.h
649 function:157,0,_ZStorSt12_Ios_IostateS_
651 file:/.../char_traits.h
652 function:258,0,_ZNSt11char_traitsIcE6lengthEPKc
656 The default gcov outputs multiple files: 'foo.cc.gcov',
657 'iostream.gcov', 'ios_base.h.gcov', etc. with source code
658 included. Instead the intermediate format here outputs only a single
659 file 'foo.cc.gcov' similar to the above example. */
662 output_intermediate_file (FILE *gcov_file
, source_t
*src
)
664 unsigned line_num
; /* current line number. */
665 const line_t
*line
; /* current line info ptr. */
666 function_t
*fn
; /* current function info ptr. */
668 fprintf (gcov_file
, "file:%s\n", src
->name
); /* source file name */
670 for (fn
= src
->functions
; fn
; fn
= fn
->line_next
)
672 /* function:<name>,<line_number>,<execution_count> */
673 fprintf (gcov_file
, "function:%d,%s,%s\n", fn
->line
,
674 format_gcov (fn
->blocks
[0].count
, 0, -1),
675 flag_demangled_names
? fn
->demangled_name
: fn
->name
);
678 for (line_num
= 1, line
= &src
->lines
[line_num
];
679 line_num
< src
->num_lines
;
684 fprintf (gcov_file
, "lcount:%u,%s\n", line_num
,
685 format_gcov (line
->count
, 0, -1));
687 for (arc
= line
->u
.branches
; arc
; arc
= arc
->line_next
)
689 if (!arc
->is_unconditional
&& !arc
->is_call_non_return
)
691 const char *branch_type
;
692 /* branch:<line_num>,<branch_coverage_type>
694 : notexec (Branch not executed)
695 : taken (Branch executed and taken)
696 : nottaken (Branch executed, but not taken)
699 branch_type
= (arc
->count
> 0) ? "taken" : "nottaken";
701 branch_type
= "notexec";
702 fprintf (gcov_file
, "branch:%d,%s\n", line_num
, branch_type
);
709 /* Process a single input file. */
712 process_file (const char *file_name
)
716 create_file_names (file_name
);
717 fns
= read_graph_file ();
721 read_count_file (fns
);
724 function_t
*fn
= fns
;
730 unsigned src
= fn
->src
;
731 unsigned line
= fn
->line
;
733 function_t
*probe
, **prev
;
735 /* Now insert it into the source file's list of
736 functions. Normally functions will be encountered in
737 ascending order, so a simple scan is quick. Note we're
738 building this list in reverse order. */
739 for (prev
= &sources
[src
].functions
;
740 (probe
= *prev
); prev
= &probe
->line_next
)
741 if (probe
->line
<= line
)
743 fn
->line_next
= probe
;
746 /* Mark last line in files touched by function. */
747 for (block_no
= 0; block_no
!= fn
->num_blocks
; block_no
++)
749 unsigned *enc
= fn
->blocks
[block_no
].u
.line
.encoding
;
750 unsigned num
= fn
->blocks
[block_no
].u
.line
.num
;
757 if (line
>= sources
[src
].num_lines
)
758 sources
[src
].num_lines
= line
+ 1;
765 else if (*enc
> line
)
768 if (line
>= sources
[src
].num_lines
)
769 sources
[src
].num_lines
= line
+ 1;
771 solve_flow_graph (fn
);
773 find_exception_blocks (fn
);
778 /* The function was not in the executable -- some other
779 instance must have been selected. */
780 release_function (fn
);
785 output_gcov_file (const char *file_name
, source_t
*src
)
787 char *gcov_file_name
= make_gcov_file_name (file_name
, src
->coverage
.name
);
789 if (src
->coverage
.lines
)
791 FILE *gcov_file
= fopen (gcov_file_name
, "w");
794 fnotice (stdout
, "Creating '%s'\n", gcov_file_name
);
795 output_lines (gcov_file
, src
);
796 if (ferror (gcov_file
))
797 fnotice (stderr
, "Error writing output file '%s'\n", gcov_file_name
);
801 fnotice (stderr
, "Could not open output file '%s'\n", gcov_file_name
);
805 unlink (gcov_file_name
);
806 fnotice (stdout
, "Removing '%s'\n", gcov_file_name
);
808 free (gcov_file_name
);
812 generate_results (const char *file_name
)
817 FILE *gcov_intermediate_file
= NULL
;
818 char *gcov_intermediate_filename
= NULL
;
820 for (ix
= n_sources
, src
= sources
; ix
--; src
++)
822 src
->lines
= XCNEWVEC (line_t
, src
->num_lines
);
824 for (fn
= functions
; fn
; fn
= fn
->next
)
828 memset (&coverage
, 0, sizeof (coverage
));
829 coverage
.name
= flag_demangled_names
? fn
->demangled_name
: fn
->name
;
830 add_line_counts (flag_function_summary
? &coverage
: NULL
, fn
);
831 if (flag_function_summary
)
833 function_summary (&coverage
, "Function");
834 fnotice (stdout
, "\n");
840 name_map_t
*name_map
= (name_map_t
*)bsearch
841 (file_name
, names
, n_names
, sizeof (*names
), name_search
);
843 file_name
= sources
[name_map
->src
].coverage
.name
;
845 file_name
= canonicalize_name (file_name
);
848 if (flag_gcov_file
&& flag_intermediate_format
)
850 /* Open the intermediate file. */
851 gcov_intermediate_filename
=
852 get_gcov_intermediate_filename (file_name
);
853 gcov_intermediate_file
= fopen (gcov_intermediate_filename
, "w");
854 if (!gcov_intermediate_file
)
856 fnotice (stderr
, "Cannot open intermediate output file %s\n",
857 gcov_intermediate_filename
);
862 for (ix
= n_sources
, src
= sources
; ix
--; src
++)
864 if (flag_relative_only
)
866 /* Ignore this source, if it is an absolute path (after
867 source prefix removal). */
868 char first
= src
->coverage
.name
[0];
870 #if HAVE_DOS_BASED_FILE_SYSTEM
871 if (first
&& src
->coverage
.name
[1] == ':')
872 first
= src
->coverage
.name
[2];
874 if (IS_DIR_SEPARATOR (first
))
878 accumulate_line_counts (src
);
879 function_summary (&src
->coverage
, "File");
880 total_lines
+= src
->coverage
.lines
;
881 total_executed
+= src
->coverage
.lines_executed
;
884 if (flag_intermediate_format
)
885 /* Output the intermediate format without requiring source
886 files. This outputs a section to a *single* file. */
887 output_intermediate_file (gcov_intermediate_file
, src
);
889 output_gcov_file (file_name
, src
);
890 fnotice (stdout
, "\n");
894 if (flag_gcov_file
&& flag_intermediate_format
)
896 /* Now we've finished writing the intermediate file. */
897 fclose (gcov_intermediate_file
);
898 XDELETEVEC (gcov_intermediate_filename
);
902 executed_summary (total_lines
, total_executed
);
905 /* Release a function structure */
908 release_function (function_t
*fn
)
913 for (ix
= fn
->num_blocks
, block
= fn
->blocks
; ix
--; block
++)
917 for (arc
= block
->succ
; arc
; arc
= arc_n
)
919 arc_n
= arc
->succ_next
;
925 if (flag_demangled_names
&& fn
->demangled_name
!= fn
->name
)
926 free (fn
->demangled_name
);
930 /* Release all memory used. */
933 release_structures (void)
938 for (ix
= n_sources
; ix
--;)
939 free (sources
[ix
].lines
);
942 for (ix
= n_names
; ix
--;)
943 free (names
[ix
].name
);
946 while ((fn
= functions
))
948 functions
= fn
->next
;
949 release_function (fn
);
953 /* Generate the names of the graph and data files. If OBJECT_DIRECTORY
954 is not specified, these are named from FILE_NAME sans extension. If
955 OBJECT_DIRECTORY is specified and is a directory, the files are in that
956 directory, but named from the basename of the FILE_NAME, sans extension.
957 Otherwise OBJECT_DIRECTORY is taken to be the name of the object *file*
958 and the data files are named from that. */
961 create_file_names (const char *file_name
)
965 int length
= strlen (file_name
);
968 /* Free previous file names. */
969 free (bbg_file_name
);
971 da_file_name
= bbg_file_name
= NULL
;
975 if (object_directory
&& object_directory
[0])
979 length
+= strlen (object_directory
) + 2;
980 name
= XNEWVEC (char, length
);
983 base
= !stat (object_directory
, &status
) && S_ISDIR (status
.st_mode
);
984 strcat (name
, object_directory
);
985 if (base
&& (! IS_DIR_SEPARATOR (name
[strlen (name
) - 1])))
990 name
= XNEWVEC (char, length
+ 1);
991 strcpy (name
, file_name
);
997 /* Append source file name. */
998 const char *cptr
= lbasename (file_name
);
999 strcat (name
, cptr
? cptr
: file_name
);
1002 /* Remove the extension. */
1003 cptr
= strrchr (CONST_CAST (char *, lbasename (name
)), '.');
1007 length
= strlen (name
);
1009 bbg_file_name
= XNEWVEC (char, length
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
1010 strcpy (bbg_file_name
, name
);
1011 strcpy (bbg_file_name
+ length
, GCOV_NOTE_SUFFIX
);
1013 da_file_name
= XNEWVEC (char, length
+ strlen (GCOV_DATA_SUFFIX
) + 1);
1014 strcpy (da_file_name
, name
);
1015 strcpy (da_file_name
+ length
, GCOV_DATA_SUFFIX
);
1021 /* A is a string and B is a pointer to name_map_t. Compare for file
1022 name orderability. */
1025 name_search (const void *a_
, const void *b_
)
1027 const char *a
= (const char *)a_
;
1028 const name_map_t
*b
= (const name_map_t
*)b_
;
1030 #if HAVE_DOS_BASED_FILE_SYSTEM
1031 return strcasecmp (a
, b
->name
);
1033 return strcmp (a
, b
->name
);
1037 /* A and B are a pointer to name_map_t. Compare for file name
1041 name_sort (const void *a_
, const void *b_
)
1043 const name_map_t
*a
= (const name_map_t
*)a_
;
1044 return name_search (a
->name
, b_
);
1047 /* Find or create a source file structure for FILE_NAME. Copies
1048 FILE_NAME on creation */
1051 find_source (const char *file_name
)
1053 name_map_t
*name_map
;
1059 file_name
= "<unknown>";
1060 name_map
= (name_map_t
*)bsearch
1061 (file_name
, names
, n_names
, sizeof (*names
), name_search
);
1064 idx
= name_map
->src
;
1068 if (n_names
+ 2 > a_names
)
1070 /* Extend the name map array -- we'll be inserting one or two
1073 name_map
= XNEWVEC (name_map_t
, a_names
);
1074 memcpy (name_map
, names
, n_names
* sizeof (*names
));
1079 /* Not found, try the canonical name. */
1080 canon
= canonicalize_name (file_name
);
1081 name_map
= (name_map_t
*)bsearch
1082 (canon
, names
, n_names
, sizeof (*names
), name_search
);
1085 /* Not found with canonical name, create a new source. */
1088 if (n_sources
== a_sources
)
1091 src
= XNEWVEC (source_t
, a_sources
);
1092 memcpy (src
, sources
, n_sources
* sizeof (*sources
));
1099 name_map
= &names
[n_names
++];
1100 name_map
->name
= canon
;
1101 name_map
->src
= idx
;
1103 src
= &sources
[n_sources
++];
1104 memset (src
, 0, sizeof (*src
));
1106 src
->coverage
.name
= src
->name
;
1108 #if HAVE_DOS_BASED_FILE_SYSTEM
1109 /* You lose if separators don't match exactly in the
1111 && !strncasecmp (source_prefix
, src
->coverage
.name
, source_length
)
1113 && !strncmp (source_prefix
, src
->coverage
.name
, source_length
)
1115 && IS_DIR_SEPARATOR (src
->coverage
.name
[source_length
]))
1116 src
->coverage
.name
+= source_length
+ 1;
1117 if (!stat (src
->name
, &status
))
1118 src
->file_time
= status
.st_mtime
;
1121 idx
= name_map
->src
;
1123 if (name_search (file_name
, name_map
))
1125 /* Append the non-canonical name. */
1126 name_map
= &names
[n_names
++];
1127 name_map
->name
= xstrdup (file_name
);
1128 name_map
->src
= idx
;
1131 /* Resort the name map. */
1132 qsort (names
, n_names
, sizeof (*names
), name_sort
);
1135 if (sources
[idx
].file_time
> bbg_file_time
)
1137 static int info_emitted
;
1139 fnotice (stderr
, "%s:source file is newer than notes file '%s'\n",
1140 file_name
, bbg_file_name
);
1144 "(the message is displayed only once per source file)\n");
1147 sources
[idx
].file_time
= 0;
1153 /* Read the notes file. Return list of functions read -- in reverse order. */
1156 read_graph_file (void)
1159 unsigned current_tag
= 0;
1160 function_t
*fn
= NULL
;
1161 function_t
*fns
= NULL
;
1162 function_t
**fns_end
= &fns
;
1163 unsigned src_idx
= 0;
1167 if (!gcov_open (bbg_file_name
, 1))
1169 fnotice (stderr
, "%s:cannot open notes file\n", bbg_file_name
);
1172 bbg_file_time
= gcov_time ();
1173 if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC
))
1175 fnotice (stderr
, "%s:not a gcov notes file\n", bbg_file_name
);
1180 version
= gcov_read_unsigned ();
1181 if (version
!= GCOV_VERSION
)
1185 GCOV_UNSIGNED2STRING (v
, version
);
1186 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1188 fnotice (stderr
, "%s:version '%.4s', prefer '%.4s'\n",
1189 bbg_file_name
, v
, e
);
1191 bbg_stamp
= gcov_read_unsigned ();
1193 while ((tag
= gcov_read_unsigned ()))
1195 unsigned length
= gcov_read_unsigned ();
1196 gcov_position_t base
= gcov_position ();
1198 if (tag
== GCOV_TAG_FUNCTION
)
1200 char *function_name
;
1201 unsigned ident
, lineno
;
1202 unsigned lineno_checksum
, cfg_checksum
;
1204 ident
= gcov_read_unsigned ();
1205 lineno_checksum
= gcov_read_unsigned ();
1206 cfg_checksum
= gcov_read_unsigned ();
1207 function_name
= xstrdup (gcov_read_string ());
1208 src_idx
= find_source (gcov_read_string ());
1209 lineno
= gcov_read_unsigned ();
1211 fn
= XCNEW (function_t
);
1212 fn
->name
= function_name
;
1213 if (flag_demangled_names
)
1215 fn
->demangled_name
= cplus_demangle (fn
->name
, DMGL_PARAMS
);
1216 if (!fn
->demangled_name
)
1217 fn
->demangled_name
= fn
->name
;
1220 fn
->lineno_checksum
= lineno_checksum
;
1221 fn
->cfg_checksum
= cfg_checksum
;
1225 fn
->line_next
= NULL
;
1228 fns_end
= &fn
->next
;
1231 else if (fn
&& tag
== GCOV_TAG_BLOCKS
)
1234 fnotice (stderr
, "%s:already seen blocks for '%s'\n",
1235 bbg_file_name
, fn
->name
);
1238 unsigned ix
, num_blocks
= GCOV_TAG_BLOCKS_NUM (length
);
1239 fn
->num_blocks
= num_blocks
;
1241 fn
->blocks
= XCNEWVEC (block_t
, fn
->num_blocks
);
1242 for (ix
= 0; ix
!= num_blocks
; ix
++)
1243 fn
->blocks
[ix
].flags
= gcov_read_unsigned ();
1246 else if (fn
&& tag
== GCOV_TAG_ARCS
)
1248 unsigned src
= gcov_read_unsigned ();
1249 unsigned num_dests
= GCOV_TAG_ARCS_NUM (length
);
1250 block_t
*src_blk
= &fn
->blocks
[src
];
1251 unsigned mark_catches
= 0;
1252 struct arc_info
*arc
;
1254 if (src
>= fn
->num_blocks
|| fn
->blocks
[src
].succ
)
1259 unsigned dest
= gcov_read_unsigned ();
1260 unsigned flags
= gcov_read_unsigned ();
1262 if (dest
>= fn
->num_blocks
)
1264 arc
= XCNEW (arc_t
);
1266 arc
->dst
= &fn
->blocks
[dest
];
1270 arc
->count_valid
= 0;
1271 arc
->on_tree
= !!(flags
& GCOV_ARC_ON_TREE
);
1272 arc
->fake
= !!(flags
& GCOV_ARC_FAKE
);
1273 arc
->fall_through
= !!(flags
& GCOV_ARC_FALLTHROUGH
);
1275 arc
->succ_next
= src_blk
->succ
;
1276 src_blk
->succ
= arc
;
1277 src_blk
->num_succ
++;
1279 arc
->pred_next
= fn
->blocks
[dest
].pred
;
1280 fn
->blocks
[dest
].pred
= arc
;
1281 fn
->blocks
[dest
].num_pred
++;
1287 /* Exceptional exit from this function, the
1288 source block must be a call. */
1289 fn
->blocks
[src
].is_call_site
= 1;
1290 arc
->is_call_non_return
= 1;
1295 /* Non-local return from a callee of this
1296 function. The destination block is a setjmp. */
1297 arc
->is_nonlocal_return
= 1;
1298 fn
->blocks
[dest
].is_nonlocal_return
= 1;
1308 /* We have a fake exit from this block. The other
1309 non-fall through exits must be to catch handlers.
1310 Mark them as catch arcs. */
1312 for (arc
= src_blk
->succ
; arc
; arc
= arc
->succ_next
)
1313 if (!arc
->fake
&& !arc
->fall_through
)
1320 else if (fn
&& tag
== GCOV_TAG_LINES
)
1322 unsigned blockno
= gcov_read_unsigned ();
1323 unsigned *line_nos
= XCNEWVEC (unsigned, length
- 1);
1325 if (blockno
>= fn
->num_blocks
|| fn
->blocks
[blockno
].u
.line
.encoding
)
1330 unsigned lineno
= gcov_read_unsigned ();
1337 line_nos
[ix
++] = src_idx
;
1339 line_nos
[ix
++] = lineno
;
1343 const char *file_name
= gcov_read_string ();
1347 src_idx
= find_source (file_name
);
1349 line_nos
[ix
++] = src_idx
;
1353 fn
->blocks
[blockno
].u
.line
.encoding
= line_nos
;
1354 fn
->blocks
[blockno
].u
.line
.num
= ix
;
1356 else if (current_tag
&& !GCOV_TAG_IS_SUBTAG (current_tag
, tag
))
1361 gcov_sync (base
, length
);
1362 if (gcov_is_error ())
1365 fnotice (stderr
, "%s:corrupted\n", bbg_file_name
);
1372 fnotice (stderr
, "%s:no functions found\n", bbg_file_name
);
1377 /* Reads profiles from the count file and attach to each
1378 function. Return nonzero if fatal error. */
1381 read_count_file (function_t
*fns
)
1386 function_t
*fn
= NULL
;
1389 if (!gcov_open (da_file_name
, 1))
1391 fnotice (stderr
, "%s:cannot open data file, assuming not executed\n",
1396 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
1398 fnotice (stderr
, "%s:not a gcov data file\n", da_file_name
);
1403 version
= gcov_read_unsigned ();
1404 if (version
!= GCOV_VERSION
)
1408 GCOV_UNSIGNED2STRING (v
, version
);
1409 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1411 fnotice (stderr
, "%s:version '%.4s', prefer version '%.4s'\n",
1412 da_file_name
, v
, e
);
1414 tag
= gcov_read_unsigned ();
1415 if (tag
!= bbg_stamp
)
1417 fnotice (stderr
, "%s:stamp mismatch with notes file\n", da_file_name
);
1421 while ((tag
= gcov_read_unsigned ()))
1423 unsigned length
= gcov_read_unsigned ();
1424 unsigned long base
= gcov_position ();
1426 if (tag
== GCOV_TAG_PROGRAM_SUMMARY
)
1428 struct gcov_summary summary
;
1429 gcov_read_summary (&summary
);
1430 object_runs
+= summary
.ctrs
[GCOV_COUNTER_ARCS
].runs
;
1433 else if (tag
== GCOV_TAG_FUNCTION
&& !length
)
1435 else if (tag
== GCOV_TAG_FUNCTION
&& length
== GCOV_TAG_FUNCTION_LENGTH
)
1438 struct function_info
*fn_n
;
1440 /* Try to find the function in the list. To speed up the
1441 search, first start from the last function found. */
1442 ident
= gcov_read_unsigned ();
1444 for (fn
= fn
? fn
->next
: NULL
; ; fn
= fn
->next
)
1448 else if ((fn
= fn_n
))
1452 fnotice (stderr
, "%s:unknown function '%u'\n",
1453 da_file_name
, ident
);
1456 if (fn
->ident
== ident
)
1462 else if (gcov_read_unsigned () != fn
->lineno_checksum
1463 || gcov_read_unsigned () != fn
->cfg_checksum
)
1466 fnotice (stderr
, "%s:profile mismatch for '%s'\n",
1467 da_file_name
, fn
->name
);
1471 else if (tag
== GCOV_TAG_FOR_COUNTER (GCOV_COUNTER_ARCS
) && fn
)
1473 if (length
!= GCOV_TAG_COUNTER_LENGTH (fn
->num_counts
))
1477 fn
->counts
= XCNEWVEC (gcov_type
, fn
->num_counts
);
1479 for (ix
= 0; ix
!= fn
->num_counts
; ix
++)
1480 fn
->counts
[ix
] += gcov_read_counter ();
1482 gcov_sync (base
, length
);
1483 if ((error
= gcov_is_error ()))
1485 fnotice (stderr
, error
< 0 ? "%s:overflowed\n" : "%s:corrupted\n",
1495 /* Solve the flow graph. Propagate counts from the instrumented arcs
1496 to the blocks and the uninstrumented arcs. */
1499 solve_flow_graph (function_t
*fn
)
1503 gcov_type
*count_ptr
= fn
->counts
;
1505 block_t
*valid_blocks
= NULL
; /* valid, but unpropagated blocks. */
1506 block_t
*invalid_blocks
= NULL
; /* invalid, but inferable blocks. */
1508 /* The arcs were built in reverse order. Fix that now. */
1509 for (ix
= fn
->num_blocks
; ix
--;)
1511 arc_t
*arc_p
, *arc_n
;
1513 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].succ
; arc
;
1514 arc_p
= arc
, arc
= arc_n
)
1516 arc_n
= arc
->succ_next
;
1517 arc
->succ_next
= arc_p
;
1519 fn
->blocks
[ix
].succ
= arc_p
;
1521 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].pred
; arc
;
1522 arc_p
= arc
, arc
= arc_n
)
1524 arc_n
= arc
->pred_next
;
1525 arc
->pred_next
= arc_p
;
1527 fn
->blocks
[ix
].pred
= arc_p
;
1530 if (fn
->num_blocks
< 2)
1531 fnotice (stderr
, "%s:'%s' lacks entry and/or exit blocks\n",
1532 bbg_file_name
, fn
->name
);
1535 if (fn
->blocks
[ENTRY_BLOCK
].num_pred
)
1536 fnotice (stderr
, "%s:'%s' has arcs to entry block\n",
1537 bbg_file_name
, fn
->name
);
1539 /* We can't deduce the entry block counts from the lack of
1541 fn
->blocks
[ENTRY_BLOCK
].num_pred
= ~(unsigned)0;
1543 if (fn
->blocks
[EXIT_BLOCK
].num_succ
)
1544 fnotice (stderr
, "%s:'%s' has arcs from exit block\n",
1545 bbg_file_name
, fn
->name
);
1547 /* Likewise, we can't deduce exit block counts from the lack
1548 of its successors. */
1549 fn
->blocks
[EXIT_BLOCK
].num_succ
= ~(unsigned)0;
1552 /* Propagate the measured counts, this must be done in the same
1553 order as the code in profile.c */
1554 for (ix
= 0, blk
= fn
->blocks
; ix
!= fn
->num_blocks
; ix
++, blk
++)
1556 block_t
const *prev_dst
= NULL
;
1557 int out_of_order
= 0;
1558 int non_fake_succ
= 0;
1560 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1568 arc
->count
= *count_ptr
++;
1569 arc
->count_valid
= 1;
1571 arc
->dst
->num_pred
--;
1573 if (prev_dst
&& prev_dst
> arc
->dst
)
1575 prev_dst
= arc
->dst
;
1577 if (non_fake_succ
== 1)
1579 /* If there is only one non-fake exit, it is an
1580 unconditional branch. */
1581 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1584 arc
->is_unconditional
= 1;
1585 /* If this block is instrumenting a call, it might be
1586 an artificial block. It is not artificial if it has
1587 a non-fallthrough exit, or the destination of this
1588 arc has more than one entry. Mark the destination
1589 block as a return site, if none of those conditions
1591 if (blk
->is_call_site
&& arc
->fall_through
1592 && arc
->dst
->pred
== arc
&& !arc
->pred_next
)
1593 arc
->dst
->is_call_return
= 1;
1597 /* Sort the successor arcs into ascending dst order. profile.c
1598 normally produces arcs in the right order, but sometimes with
1599 one or two out of order. We're not using a particularly
1603 arc_t
*start
= blk
->succ
;
1604 unsigned changes
= 1;
1608 arc_t
*arc
, *arc_p
, *arc_n
;
1611 for (arc_p
= NULL
, arc
= start
; (arc_n
= arc
->succ_next
);)
1613 if (arc
->dst
> arc_n
->dst
)
1617 arc_p
->succ_next
= arc_n
;
1620 arc
->succ_next
= arc_n
->succ_next
;
1621 arc_n
->succ_next
= arc
;
1634 /* Place it on the invalid chain, it will be ignored if that's
1636 blk
->invalid_chain
= 1;
1637 blk
->chain
= invalid_blocks
;
1638 invalid_blocks
= blk
;
1641 while (invalid_blocks
|| valid_blocks
)
1643 while ((blk
= invalid_blocks
))
1645 gcov_type total
= 0;
1648 invalid_blocks
= blk
->chain
;
1649 blk
->invalid_chain
= 0;
1651 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1652 total
+= arc
->count
;
1653 else if (!blk
->num_pred
)
1654 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
1655 total
+= arc
->count
;
1660 blk
->count_valid
= 1;
1661 blk
->chain
= valid_blocks
;
1662 blk
->valid_chain
= 1;
1665 while ((blk
= valid_blocks
))
1668 arc_t
*arc
, *inv_arc
;
1670 valid_blocks
= blk
->chain
;
1671 blk
->valid_chain
= 0;
1672 if (blk
->num_succ
== 1)
1678 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1680 total
-= arc
->count
;
1681 if (!arc
->count_valid
)
1685 inv_arc
->count_valid
= 1;
1686 inv_arc
->count
= total
;
1689 if (dst
->count_valid
)
1691 if (dst
->num_pred
== 1 && !dst
->valid_chain
)
1693 dst
->chain
= valid_blocks
;
1694 dst
->valid_chain
= 1;
1700 if (!dst
->num_pred
&& !dst
->invalid_chain
)
1702 dst
->chain
= invalid_blocks
;
1703 dst
->invalid_chain
= 1;
1704 invalid_blocks
= dst
;
1708 if (blk
->num_pred
== 1)
1714 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
1716 total
-= arc
->count
;
1717 if (!arc
->count_valid
)
1721 inv_arc
->count_valid
= 1;
1722 inv_arc
->count
= total
;
1725 if (src
->count_valid
)
1727 if (src
->num_succ
== 1 && !src
->valid_chain
)
1729 src
->chain
= valid_blocks
;
1730 src
->valid_chain
= 1;
1736 if (!src
->num_succ
&& !src
->invalid_chain
)
1738 src
->chain
= invalid_blocks
;
1739 src
->invalid_chain
= 1;
1740 invalid_blocks
= src
;
1747 /* If the graph has been correctly solved, every block will have a
1749 for (ix
= 0; ix
< fn
->num_blocks
; ix
++)
1750 if (!fn
->blocks
[ix
].count_valid
)
1752 fnotice (stderr
, "%s:graph is unsolvable for '%s'\n",
1753 bbg_file_name
, fn
->name
);
1758 /* Mark all the blocks only reachable via an incoming catch. */
1761 find_exception_blocks (function_t
*fn
)
1764 block_t
**queue
= XALLOCAVEC (block_t
*, fn
->num_blocks
);
1766 /* First mark all blocks as exceptional. */
1767 for (ix
= fn
->num_blocks
; ix
--;)
1768 fn
->blocks
[ix
].exceptional
= 1;
1770 /* Now mark all the blocks reachable via non-fake edges */
1771 queue
[0] = fn
->blocks
;
1772 queue
[0]->exceptional
= 0;
1775 block_t
*block
= queue
[--ix
];
1778 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
1779 if (!arc
->fake
&& !arc
->is_throw
&& arc
->dst
->exceptional
)
1781 arc
->dst
->exceptional
= 0;
1782 queue
[ix
++] = arc
->dst
;
1788 /* Increment totals in COVERAGE according to arc ARC. */
1791 add_branch_counts (coverage_t
*coverage
, const arc_t
*arc
)
1793 if (arc
->is_call_non_return
)
1796 if (arc
->src
->count
)
1797 coverage
->calls_executed
++;
1799 else if (!arc
->is_unconditional
)
1801 coverage
->branches
++;
1802 if (arc
->src
->count
)
1803 coverage
->branches_executed
++;
1805 coverage
->branches_taken
++;
1809 /* Format a GCOV_TYPE integer as either a percent ratio, or absolute
1810 count. If dp >= 0, format TOP/BOTTOM * 100 to DP decimal places.
1811 If DP is zero, no decimal point is printed. Only print 100% when
1812 TOP==BOTTOM and only print 0% when TOP=0. If dp < 0, then simply
1813 format TOP. Return pointer to a static string. */
1816 format_gcov (gcov_type top
, gcov_type bottom
, int dp
)
1818 static char buffer
[20];
1822 float ratio
= bottom
? (float)top
/ bottom
: 0;
1824 unsigned limit
= 100;
1827 for (ix
= dp
; ix
--; )
1830 percent
= (unsigned) (ratio
* limit
+ (float)0.5);
1831 if (percent
<= 0 && top
)
1833 else if (percent
>= limit
&& top
!= bottom
)
1834 percent
= limit
- 1;
1835 ix
= sprintf (buffer
, "%.*u%%", dp
+ 1, percent
);
1841 buffer
[ix
+1] = buffer
[ix
];
1845 buffer
[ix
+ 1] = '.';
1849 sprintf (buffer
, "%"PRId64
, (int64_t)top
);
1854 /* Summary of execution */
1857 executed_summary (unsigned lines
, unsigned executed
)
1860 fnotice (stdout
, "Lines executed:%s of %d\n",
1861 format_gcov (executed
, lines
, 2), lines
);
1863 fnotice (stdout
, "No executable lines\n");
1866 /* Output summary info for a function or file. */
1869 function_summary (const coverage_t
*coverage
, const char *title
)
1871 fnotice (stdout
, "%s '%s'\n", title
, coverage
->name
);
1872 executed_summary (coverage
->lines
, coverage
->lines_executed
);
1876 if (coverage
->branches
)
1878 fnotice (stdout
, "Branches executed:%s of %d\n",
1879 format_gcov (coverage
->branches_executed
,
1880 coverage
->branches
, 2),
1881 coverage
->branches
);
1882 fnotice (stdout
, "Taken at least once:%s of %d\n",
1883 format_gcov (coverage
->branches_taken
,
1884 coverage
->branches
, 2),
1885 coverage
->branches
);
1888 fnotice (stdout
, "No branches\n");
1889 if (coverage
->calls
)
1890 fnotice (stdout
, "Calls executed:%s of %d\n",
1891 format_gcov (coverage
->calls_executed
, coverage
->calls
, 2),
1894 fnotice (stdout
, "No calls\n");
1898 /* Canonicalize the filename NAME by canonicalizing directory
1899 separators, eliding . components and resolving .. components
1900 appropriately. Always returns a unique string. */
1903 canonicalize_name (const char *name
)
1905 /* The canonical name cannot be longer than the incoming name. */
1906 char *result
= XNEWVEC (char, strlen (name
) + 1);
1907 const char *base
= name
, *probe
;
1912 #if HAVE_DOS_BASED_FILE_SYSTEM
1913 if (base
[0] && base
[1] == ':')
1915 result
[0] = base
[0];
1921 for (dd_base
= ptr
; *base
; base
= probe
)
1925 for (probe
= base
; *probe
; probe
++)
1926 if (IS_DIR_SEPARATOR (*probe
))
1930 if (len
== 1 && base
[0] == '.')
1931 /* Elide a '.' directory */
1933 else if (len
== 2 && base
[0] == '.' && base
[1] == '.')
1935 /* '..', we can only elide it and the previous directory, if
1936 we're not a symlink. */
1937 struct stat ATTRIBUTE_UNUSED buf
;
1941 #if defined (S_ISLNK)
1942 /* S_ISLNK is not POSIX.1-1996. */
1943 || stat (result
, &buf
) || S_ISLNK (buf
.st_mode
)
1947 /* Cannot elide, or unreadable or a symlink. */
1948 dd_base
= ptr
+ 2 + slash
;
1951 while (ptr
!= dd_base
&& *ptr
!= '/')
1953 slash
= ptr
!= result
;
1958 /* Regular pathname component. */
1961 memcpy (ptr
, base
, len
);
1966 for (; IS_DIR_SEPARATOR (*probe
); probe
++)
1974 /* Generate an output file name. INPUT_NAME is the canonicalized main
1975 input file and SRC_NAME is the canonicalized file name.
1976 LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
1977 long_output_names we prepend the processed name of the input file
1978 to each output name (except when the current source file is the
1979 input file, so you don't get a double concatenation). The two
1980 components are separated by '##'. With preserve_paths we create a
1981 filename from all path components of the source file, replacing '/'
1982 with '#', and .. with '^', without it we simply take the basename
1983 component. (Remember, the canonicalized name will already have
1984 elided '.' components and converted \\ separators.) */
1987 make_gcov_file_name (const char *input_name
, const char *src_name
)
1992 if (flag_long_names
&& input_name
&& strcmp (src_name
, input_name
))
1994 /* Generate the input filename part. */
1995 result
= XNEWVEC (char, strlen (input_name
) + strlen (src_name
) + 10);
1998 ptr
= mangle_name (input_name
, ptr
);
1999 ptr
[0] = ptr
[1] = '#';
2004 result
= XNEWVEC (char, strlen (src_name
) + 10);
2008 ptr
= mangle_name (src_name
, ptr
);
2009 strcpy (ptr
, ".gcov");
2015 mangle_name (char const *base
, char *ptr
)
2019 /* Generate the source filename part. */
2020 if (!flag_preserve_paths
)
2022 base
= lbasename (base
);
2023 len
= strlen (base
);
2024 memcpy (ptr
, base
, len
);
2029 /* Convert '/' to '#', convert '..' to '^',
2030 convert ':' to '~' on DOS based file system. */
2033 #if HAVE_DOS_BASED_FILE_SYSTEM
2034 if (base
[0] && base
[1] == ':')
2042 for (; *base
; base
= probe
)
2046 for (probe
= base
; *probe
; probe
++)
2050 if (len
== 2 && base
[0] == '.' && base
[1] == '.')
2054 memcpy (ptr
, base
, len
);
2068 /* Scan through the bb_data for each line in the block, increment
2069 the line number execution count indicated by the execution count of
2070 the appropriate basic block. */
2073 add_line_counts (coverage_t
*coverage
, function_t
*fn
)
2076 line_t
*line
= NULL
; /* This is propagated from one iteration to the
2079 /* Scan each basic block. */
2080 for (ix
= 0; ix
!= fn
->num_blocks
; ix
++)
2082 block_t
*block
= &fn
->blocks
[ix
];
2084 const source_t
*src
= NULL
;
2087 if (block
->count
&& ix
&& ix
+ 1 != fn
->num_blocks
)
2088 fn
->blocks_executed
++;
2089 for (jx
= 0, encoding
= block
->u
.line
.encoding
;
2090 jx
!= block
->u
.line
.num
; jx
++, encoding
++)
2093 src
= &sources
[*++encoding
];
2098 line
= &src
->lines
[*encoding
];
2104 if (!line
->count
&& block
->count
)
2105 coverage
->lines_executed
++;
2108 if (!block
->exceptional
)
2109 line
->unexceptional
= 1;
2110 line
->count
+= block
->count
;
2112 free (block
->u
.line
.encoding
);
2113 block
->u
.cycle
.arc
= NULL
;
2114 block
->u
.cycle
.ident
= ~0U;
2116 if (!ix
|| ix
+ 1 == fn
->num_blocks
)
2117 /* Entry or exit block */;
2118 else if (flag_all_blocks
)
2120 line_t
*block_line
= line
;
2123 block_line
= &sources
[fn
->src
].lines
[fn
->line
];
2125 block
->chain
= block_line
->u
.blocks
;
2126 block_line
->u
.blocks
= block
;
2128 else if (flag_branches
)
2132 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2134 arc
->line_next
= line
->u
.branches
;
2135 line
->u
.branches
= arc
;
2136 if (coverage
&& !arc
->is_unconditional
)
2137 add_branch_counts (coverage
, arc
);
2142 fnotice (stderr
, "%s:no lines for '%s'\n", bbg_file_name
, fn
->name
);
2145 /* Accumulate the line counts of a file. */
2148 accumulate_line_counts (source_t
*src
)
2151 function_t
*fn
, *fn_p
, *fn_n
;
2154 /* Reverse the function order. */
2155 for (fn
= src
->functions
, fn_p
= NULL
; fn
;
2156 fn_p
= fn
, fn
= fn_n
)
2158 fn_n
= fn
->line_next
;
2159 fn
->line_next
= fn_p
;
2161 src
->functions
= fn_p
;
2163 for (ix
= src
->num_lines
, line
= src
->lines
; ix
--; line
++)
2165 if (!flag_all_blocks
)
2167 arc_t
*arc
, *arc_p
, *arc_n
;
2169 /* Total and reverse the branch information. */
2170 for (arc
= line
->u
.branches
, arc_p
= NULL
; arc
;
2171 arc_p
= arc
, arc
= arc_n
)
2173 arc_n
= arc
->line_next
;
2174 arc
->line_next
= arc_p
;
2176 add_branch_counts (&src
->coverage
, arc
);
2178 line
->u
.branches
= arc_p
;
2180 else if (line
->u
.blocks
)
2182 /* The user expects the line count to be the number of times
2183 a line has been executed. Simply summing the block count
2184 will give an artificially high number. The Right Thing
2185 is to sum the entry counts to the graph of blocks on this
2186 line, then find the elementary cycles of the local graph
2187 and add the transition counts of those cycles. */
2188 block_t
*block
, *block_p
, *block_n
;
2189 gcov_type count
= 0;
2191 /* Reverse the block information. */
2192 for (block
= line
->u
.blocks
, block_p
= NULL
; block
;
2193 block_p
= block
, block
= block_n
)
2195 block_n
= block
->chain
;
2196 block
->chain
= block_p
;
2197 block
->u
.cycle
.ident
= ix
;
2199 line
->u
.blocks
= block_p
;
2201 /* Sum the entry arcs. */
2202 for (block
= line
->u
.blocks
; block
; block
= block
->chain
)
2206 for (arc
= block
->pred
; arc
; arc
= arc
->pred_next
)
2208 if (arc
->src
->u
.cycle
.ident
!= ix
)
2209 count
+= arc
->count
;
2211 add_branch_counts (&src
->coverage
, arc
);
2214 /* Initialize the cs_count. */
2215 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2216 arc
->cs_count
= arc
->count
;
2219 /* Find the loops. This uses the algorithm described in
2220 Tiernan 'An Efficient Search Algorithm to Find the
2221 Elementary Circuits of a Graph', CACM Dec 1970. We hold
2222 the P array by having each block point to the arc that
2223 connects to the previous block. The H array is implicitly
2224 held because of the arc ordering, and the block's
2225 previous arc pointer.
2227 Although the algorithm is O(N^3) for highly connected
2228 graphs, at worst we'll have O(N^2), as most blocks have
2229 only one or two exits. Most graphs will be small.
2231 For each loop we find, locate the arc with the smallest
2232 transition count, and add that to the cumulative
2233 count. Decrease flow over the cycle and remove the arc
2234 from consideration. */
2235 for (block
= line
->u
.blocks
; block
; block
= block
->chain
)
2237 block_t
*head
= block
;
2245 block_t
*dst
= arc
->dst
;
2246 if (/* Already used that arc. */
2248 /* Not to same graph, or before first vertex. */
2249 || dst
->u
.cycle
.ident
!= ix
2250 /* Already in path. */
2251 || dst
->u
.cycle
.arc
)
2253 arc
= arc
->succ_next
;
2259 /* Found a closing arc. */
2260 gcov_type cycle_count
= arc
->cs_count
;
2261 arc_t
*cycle_arc
= arc
;
2264 /* Locate the smallest arc count of the loop. */
2265 for (dst
= head
; (probe_arc
= dst
->u
.cycle
.arc
);
2266 dst
= probe_arc
->src
)
2267 if (cycle_count
> probe_arc
->cs_count
)
2269 cycle_count
= probe_arc
->cs_count
;
2270 cycle_arc
= probe_arc
;
2273 count
+= cycle_count
;
2274 cycle_arc
->cycle
= 1;
2276 /* Remove the flow from the cycle. */
2277 arc
->cs_count
-= cycle_count
;
2278 for (dst
= head
; (probe_arc
= dst
->u
.cycle
.arc
);
2279 dst
= probe_arc
->src
)
2280 probe_arc
->cs_count
-= cycle_count
;
2282 /* Unwind to the cyclic arc. */
2283 while (head
!= cycle_arc
->src
)
2285 arc
= head
->u
.cycle
.arc
;
2286 head
->u
.cycle
.arc
= NULL
;
2290 arc
= arc
->succ_next
;
2294 /* Add new block to chain. */
2295 dst
->u
.cycle
.arc
= arc
;
2299 /* We could not add another vertex to the path. Remove
2300 the last vertex from the list. */
2301 arc
= head
->u
.cycle
.arc
;
2304 /* It was not the first vertex. Move onto next arc. */
2305 head
->u
.cycle
.arc
= NULL
;
2307 arc
= arc
->succ_next
;
2308 goto current_vertex
;
2310 /* Mark this block as unusable. */
2311 block
->u
.cycle
.ident
= ~0U;
2314 line
->count
= count
;
2319 src
->coverage
.lines
++;
2321 src
->coverage
.lines_executed
++;
2326 /* Output information about ARC number IX. Returns nonzero if
2327 anything is output. */
2330 output_branch_count (FILE *gcov_file
, int ix
, const arc_t
*arc
)
2332 if (arc
->is_call_non_return
)
2334 if (arc
->src
->count
)
2336 fnotice (gcov_file
, "call %2d returned %s\n", ix
,
2337 format_gcov (arc
->src
->count
- arc
->count
,
2338 arc
->src
->count
, -flag_counts
));
2341 fnotice (gcov_file
, "call %2d never executed\n", ix
);
2343 else if (!arc
->is_unconditional
)
2345 if (arc
->src
->count
)
2346 fnotice (gcov_file
, "branch %2d taken %s%s\n", ix
,
2347 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
),
2348 arc
->fall_through
? " (fallthrough)"
2349 : arc
->is_throw
? " (throw)" : "");
2351 fnotice (gcov_file
, "branch %2d never executed\n", ix
);
2353 else if (flag_unconditional
&& !arc
->dst
->is_call_return
)
2355 if (arc
->src
->count
)
2356 fnotice (gcov_file
, "unconditional %2d taken %s\n", ix
,
2357 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
));
2359 fnotice (gcov_file
, "unconditional %2d never executed\n", ix
);
2368 read_line (FILE *file
)
2370 static char *string
;
2371 static size_t string_len
;
2378 string
= XNEWVEC (char, string_len
);
2381 while ((ptr
= fgets (string
+ pos
, string_len
- pos
, file
)))
2383 size_t len
= strlen (string
+ pos
);
2385 if (string
[pos
+ len
- 1] == '\n')
2387 string
[pos
+ len
- 1] = 0;
2391 string
= XRESIZEVEC (char, string
, string_len
* 2);
2395 return pos
? string
: NULL
;
2398 /* Read in the source file one line at a time, and output that line to
2399 the gcov file preceded by its execution count and other
2403 output_lines (FILE *gcov_file
, const source_t
*src
)
2406 unsigned line_num
; /* current line number. */
2407 const line_t
*line
; /* current line info ptr. */
2408 const char *retval
= ""; /* status of source file reading. */
2409 function_t
*fn
= NULL
;
2411 fprintf (gcov_file
, "%9s:%5d:Source:%s\n", "-", 0, src
->coverage
.name
);
2412 if (!multiple_files
)
2414 fprintf (gcov_file
, "%9s:%5d:Graph:%s\n", "-", 0, bbg_file_name
);
2415 fprintf (gcov_file
, "%9s:%5d:Data:%s\n", "-", 0,
2416 no_data_file
? "-" : da_file_name
);
2417 fprintf (gcov_file
, "%9s:%5d:Runs:%u\n", "-", 0, object_runs
);
2419 fprintf (gcov_file
, "%9s:%5d:Programs:%u\n", "-", 0, program_count
);
2421 source_file
= fopen (src
->name
, "r");
2424 fnotice (stderr
, "Cannot open source file %s\n", src
->name
);
2427 else if (src
->file_time
== 0)
2428 fprintf (gcov_file
, "%9s:%5d:Source is newer than graph\n", "-", 0);
2431 fn
= src
->functions
;
2433 for (line_num
= 1, line
= &src
->lines
[line_num
];
2434 line_num
< src
->num_lines
; line_num
++, line
++)
2436 for (; fn
&& fn
->line
== line_num
; fn
= fn
->line_next
)
2438 arc_t
*arc
= fn
->blocks
[EXIT_BLOCK
].pred
;
2439 gcov_type return_count
= fn
->blocks
[EXIT_BLOCK
].count
;
2440 gcov_type called_count
= fn
->blocks
[ENTRY_BLOCK
].count
;
2442 for (; arc
; arc
= arc
->pred_next
)
2444 return_count
-= arc
->count
;
2446 fprintf (gcov_file
, "function %s", flag_demangled_names
?
2447 fn
->demangled_name
: fn
->name
);
2448 fprintf (gcov_file
, " called %s",
2449 format_gcov (called_count
, 0, -1));
2450 fprintf (gcov_file
, " returned %s",
2451 format_gcov (return_count
, called_count
, 0));
2452 fprintf (gcov_file
, " blocks executed %s",
2453 format_gcov (fn
->blocks_executed
, fn
->num_blocks
- 2, 0));
2454 fprintf (gcov_file
, "\n");
2458 retval
= read_line (source_file
);
2460 /* For lines which don't exist in the .bb file, print '-' before
2461 the source line. For lines which exist but were never
2462 executed, print '#####' or '=====' before the source line.
2463 Otherwise, print the execution count before the source line.
2464 There are 16 spaces of indentation added before the source
2465 line so that tabs won't be messed up. */
2466 fprintf (gcov_file
, "%9s:%5u:%s\n",
2467 !line
->exists
? "-" : line
->count
2468 ? format_gcov (line
->count
, 0, -1)
2469 : line
->unexceptional
? "#####" : "=====", line_num
,
2470 retval
? retval
: "/*EOF*/");
2472 if (flag_all_blocks
)
2478 for (ix
= jx
= 0, block
= line
->u
.blocks
; block
;
2479 block
= block
->chain
)
2481 if (!block
->is_call_return
)
2482 fprintf (gcov_file
, "%9s:%5u-block %2d\n",
2483 !line
->exists
? "-" : block
->count
2484 ? format_gcov (block
->count
, 0, -1)
2485 : block
->exceptional
? "%%%%%" : "$$$$$",
2488 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2489 jx
+= output_branch_count (gcov_file
, jx
, arc
);
2492 else if (flag_branches
)
2497 for (ix
= 0, arc
= line
->u
.branches
; arc
; arc
= arc
->line_next
)
2498 ix
+= output_branch_count (gcov_file
, ix
, arc
);
2502 /* Handle all remaining source lines. There may be lines after the
2503 last line of code. */
2506 for (; (retval
= read_line (source_file
)); line_num
++)
2507 fprintf (gcov_file
, "%9s:%5u:%s\n", "-", line_num
, retval
);
2511 fclose (source_file
);