1 /* Gcov.c: prepend line execution counts and branch probabilities to a
3 Copyright (C) 1990-2016 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"
53 /* The gcno file is generated by -ftest-coverage option. The gcda file is
54 generated by a program compiled with -fprofile-arcs. Their formats
55 are documented in gcov-io.h. */
57 /* The functions in this file for creating and solution program flow graphs
58 are very similar to functions in the gcc source file profile.c. In
59 some places we make use of the knowledge of how profile.c works to
60 select particular algorithms here. */
62 /* The code validates that the profile information read in corresponds
63 to the code currently being compiled. Rather than checking for
64 identical files, the code below compares a checksum on the CFG
65 (based on the order of basic blocks and the arcs in the CFG). If
66 the CFG checksum in the gcda file match the CFG checksum in the
67 gcno file, the profile data will be used. */
69 /* This is the size of the buffer used to read in source file lines. */
75 /* Describes an arc between two basic blocks. */
77 typedef struct arc_info
79 /* source and destination blocks. */
80 struct block_info
*src
;
81 struct block_info
*dst
;
83 /* transition counts. */
85 /* used in cycle search, so that we do not clobber original counts. */
88 unsigned int count_valid
: 1;
89 unsigned int on_tree
: 1;
90 unsigned int fake
: 1;
91 unsigned int fall_through
: 1;
93 /* Arc to a catch handler. */
94 unsigned int is_throw
: 1;
96 /* Arc is for a function that abnormally returns. */
97 unsigned int is_call_non_return
: 1;
99 /* Arc is for catch/setjmp. */
100 unsigned int is_nonlocal_return
: 1;
102 /* Is an unconditional branch. */
103 unsigned int is_unconditional
: 1;
105 /* Loop making arc. */
106 unsigned int cycle
: 1;
108 /* Next branch on line. */
109 struct arc_info
*line_next
;
111 /* Links to next arc on src and dst lists. */
112 struct arc_info
*succ_next
;
113 struct arc_info
*pred_next
;
116 /* Describes a basic block. Contains lists of arcs to successor and
117 predecessor blocks. */
119 typedef struct block_info
121 /* Chain of exit and entry arcs. */
125 /* Number of unprocessed exit and entry arcs. */
129 /* Block execution count. */
132 unsigned count_valid
: 1;
133 unsigned valid_chain
: 1;
134 unsigned invalid_chain
: 1;
135 unsigned exceptional
: 1;
137 /* Block is a call instrumenting site. */
138 unsigned is_call_site
: 1; /* Does the call. */
139 unsigned is_call_return
: 1; /* Is the return. */
141 /* Block is a landing pad for longjmp or throw. */
142 unsigned is_nonlocal_return
: 1;
148 /* Array of line numbers and source files. source files are
149 introduced by a linenumber of zero, the next 'line number' is
150 the number of the source file. Always starts with a source
154 } line
; /* Valid until blocks are linked onto lines */
157 /* Single line graph cycle workspace. Used for all-blocks
161 } cycle
; /* Used in all-blocks mode, after blocks are linked onto
165 /* Temporary chain for solving graph, and for chaining blocks on one
167 struct block_info
*chain
;
171 /* Describes a single function. Contains an array of basic blocks. */
173 typedef struct function_info
175 /* Name of function. */
177 char *demangled_name
;
179 unsigned lineno_checksum
;
180 unsigned cfg_checksum
;
182 /* The graph contains at least one fake incoming edge. */
183 unsigned has_catch
: 1;
185 /* Array of basic blocks. Like in GCC, the entry block is
186 at blocks[0] and the exit block is at blocks[1]. */
187 #define ENTRY_BLOCK (0)
188 #define EXIT_BLOCK (1)
191 unsigned blocks_executed
;
193 /* Raw arc coverage counts. */
197 /* First line number & file. */
201 /* Next function in same source file. */
202 struct function_info
*next_file_fn
;
205 struct function_info
*next
;
208 /* Describes coverage of a file or function. */
210 typedef struct coverage_info
216 int branches_executed
;
225 /* Describes a single line of source. Contains a chain of basic blocks
228 typedef struct line_info
230 /* Return true when NEEDLE is one of basic blocks the line belongs to. */
231 bool has_block (block_t
*needle
);
233 gcov_type count
; /* execution count */
236 arc_t
*branches
; /* branches from blocks that end on this
237 line. Used for branch-counts when not
239 block_t
*blocks
; /* blocks which start on this line. Used
240 in all-blocks mode. */
243 unsigned unexceptional
: 1;
247 line_t::has_block (block_t
*needle
)
249 for (block_t
*n
= u
.blocks
; n
; n
= n
->chain
)
256 /* Describes a file mentioned in the block graph. Contains an array
259 typedef struct source_info
261 /* Canonical name of source file. */
265 /* Array of line information. */
271 /* Functions in this source file. These are in ascending line
273 function_t
*functions
;
276 typedef struct name_map
278 char *name
; /* Source file name */
279 unsigned src
; /* Source file */
282 /* Holds a list of function basic block graphs. */
284 static function_t
*functions
;
285 static function_t
**fn_end
= &functions
;
287 static source_t
*sources
; /* Array of source files */
288 static unsigned n_sources
; /* Number of sources */
289 static unsigned a_sources
; /* Allocated sources */
291 static name_map_t
*names
; /* Mapping of file names to sources */
292 static unsigned n_names
; /* Number of names */
293 static unsigned a_names
; /* Allocated names */
295 /* This holds data summary information. */
297 static unsigned object_runs
;
298 static unsigned program_count
;
300 static unsigned total_lines
;
301 static unsigned total_executed
;
303 /* Modification time of graph file. */
305 static time_t bbg_file_time
;
307 /* Name of the notes (gcno) output file. The "bbg" prefix is for
308 historical reasons, when the notes file contained only the
309 basic block graph notes. */
311 static char *bbg_file_name
;
313 /* Stamp of the bbg file */
314 static unsigned bbg_stamp
;
316 /* Name and file pointer of the input file for the count data (gcda). */
318 static char *da_file_name
;
320 /* Data file is missing. */
322 static int no_data_file
;
324 /* If there is several input files, compute and display results after
325 reading all data files. This way if two or more gcda file refer to
326 the same source file (eg inline subprograms in a .h file), the
329 static int multiple_files
= 0;
331 /* Output branch probabilities. */
333 static int flag_branches
= 0;
335 /* Show unconditional branches too. */
336 static int flag_unconditional
= 0;
338 /* Output a gcov file if this is true. This is on by default, and can
339 be turned off by the -n option. */
341 static int flag_gcov_file
= 1;
343 /* Output progress indication if this is true. This is off by default
344 and can be turned on by the -d option. */
346 static int flag_display_progress
= 0;
348 /* Output *.gcov file in intermediate format used by 'lcov'. */
350 static int flag_intermediate_format
= 0;
352 /* Output demangled function names. */
354 static int flag_demangled_names
= 0;
356 /* For included files, make the gcov output file name include the name
357 of the input source file. For example, if x.h is included in a.c,
358 then the output file name is a.c##x.h.gcov instead of x.h.gcov. */
360 static int flag_long_names
= 0;
362 /* Output count information for every basic block, not merely those
363 that contain line number information. */
365 static int flag_all_blocks
= 0;
367 /* Output summary info for each function. */
369 static int flag_function_summary
= 0;
371 /* Object directory file prefix. This is the directory/file where the
372 graph and data files are looked for, if nonzero. */
374 static char *object_directory
= 0;
376 /* Source directory prefix. This is removed from source pathnames
377 that match, when generating the output file name. */
379 static char *source_prefix
= 0;
380 static size_t source_length
= 0;
382 /* Only show data for sources with relative pathnames. Absolute ones
383 usually indicate a system header file, which although it may
384 contain inline functions, is usually uninteresting. */
385 static int flag_relative_only
= 0;
387 /* Preserve all pathname components. Needed when object files and
388 source files are in subdirectories. '/' is mangled as '#', '.' is
389 elided and '..' mangled to '^'. */
391 static int flag_preserve_paths
= 0;
393 /* Output the number of times a branch was taken as opposed to the percentage
394 of times it was taken. */
396 static int flag_counts
= 0;
398 /* Forward declarations. */
399 static int process_args (int, char **);
400 static void print_usage (int) ATTRIBUTE_NORETURN
;
401 static void print_version (void) ATTRIBUTE_NORETURN
;
402 static void process_file (const char *);
403 static void generate_results (const char *);
404 static void create_file_names (const char *);
405 static int name_search (const void *, const void *);
406 static int name_sort (const void *, const void *);
407 static char *canonicalize_name (const char *);
408 static unsigned find_source (const char *);
409 static function_t
*read_graph_file (void);
410 static int read_count_file (function_t
*);
411 static void solve_flow_graph (function_t
*);
412 static void find_exception_blocks (function_t
*);
413 static void add_branch_counts (coverage_t
*, const arc_t
*);
414 static void add_line_counts (coverage_t
*, function_t
*);
415 static void executed_summary (unsigned, unsigned);
416 static void function_summary (const coverage_t
*, const char *);
417 static const char *format_gcov (gcov_type
, gcov_type
, int);
418 static void accumulate_line_counts (source_t
*);
419 static void output_gcov_file (const char *, source_t
*);
420 static int output_branch_count (FILE *, int, const arc_t
*);
421 static void output_lines (FILE *, const source_t
*);
422 static char *make_gcov_file_name (const char *, const char *);
423 static char *mangle_name (const char *, char *);
424 static void release_structures (void);
425 static void release_function (function_t
*);
426 extern int main (int, char **);
429 There are a bajillion algorithms that do this. Boost's function is named
430 hawick_cycles, so I used the algorithm by K. A. Hawick and H. A. James in
431 "Enumerating Circuits and Loops in Graphs with Self-Arcs and Multiple-Arcs"
432 (url at <http://complexity.massey.ac.nz/cstn/013/cstn-013.pdf>).
434 The basic algorithm is simple: effectively, we're finding all simple paths
435 in a subgraph (that shrinks every iteration). Duplicates are filtered by
436 "blocking" a path when a node is added to the path (this also prevents non-
437 simple paths)--the node is unblocked only when it participates in a cycle.
440 typedef vector
<arc_t
*> arc_vector_t
;
441 typedef vector
<const block_t
*> block_vector_t
;
443 /* Enum with types of loop in CFG. */
452 /* Loop_type operator that merges two values: A and B. */
454 inline loop_type
& operator |= (loop_type
& a
, loop_type b
)
456 return a
= static_cast<loop_type
> (a
| b
);
459 /* Handle cycle identified by EDGES, where the function finds minimum cs_count
460 and subtract the value from all counts. The subtracted value is added
461 to COUNT. Returns type of loop. */
464 handle_cycle (const arc_vector_t
&edges
, int64_t &count
)
466 /* Find the minimum edge of the cycle, and reduce all nodes in the cycle by
468 int64_t cycle_count
= INTTYPE_MAXIMUM (int64_t);
469 for (unsigned i
= 0; i
< edges
.size (); i
++)
471 int64_t ecount
= edges
[i
]->cs_count
;
472 if (cycle_count
> ecount
)
473 cycle_count
= ecount
;
475 count
+= cycle_count
;
476 for (unsigned i
= 0; i
< edges
.size (); i
++)
477 edges
[i
]->cs_count
-= cycle_count
;
479 return cycle_count
< 0 ? NEGATIVE_LOOP
: LOOP
;
482 /* Unblock a block U from BLOCKED. Apart from that, iterate all blocks
483 blocked by U in BLOCK_LISTS. */
486 unblock (const block_t
*u
, block_vector_t
&blocked
,
487 vector
<block_vector_t
> &block_lists
)
489 block_vector_t::iterator it
= find (blocked
.begin (), blocked
.end (), u
);
490 if (it
== blocked
.end ())
493 unsigned index
= it
- blocked
.begin ();
496 for (block_vector_t::iterator it2
= block_lists
[index
].begin ();
497 it2
!= block_lists
[index
].end (); it2
++)
498 unblock (*it2
, blocked
, block_lists
);
499 for (unsigned j
= 0; j
< block_lists
[index
].size (); j
++)
500 unblock (u
, blocked
, block_lists
);
502 block_lists
.erase (block_lists
.begin () + index
);
505 /* Find circuit going to block V, PATH is provisional seen cycle.
506 BLOCKED is vector of blocked vertices, BLOCK_LISTS contains vertices
507 blocked by a block. COUNT is accumulated count of the current LINE.
508 Returns what type of loop it contains. */
511 circuit (block_t
*v
, arc_vector_t
&path
, block_t
*start
,
512 block_vector_t
&blocked
, vector
<block_vector_t
> &block_lists
,
513 line_t
&linfo
, int64_t &count
)
515 loop_type result
= NO_LOOP
;
517 /* Add v to the block list. */
518 gcc_assert (find (blocked
.begin (), blocked
.end (), v
) == blocked
.end ());
519 blocked
.push_back (v
);
520 block_lists
.push_back (block_vector_t ());
522 for (arc_t
*arc
= v
->succ
; arc
; arc
= arc
->succ_next
)
524 block_t
*w
= arc
->dst
;
525 if (w
< start
|| !linfo
.has_block (w
))
528 path
.push_back (arc
);
530 /* Cycle has been found. */
531 result
|= handle_cycle (path
, count
);
532 else if (find (blocked
.begin (), blocked
.end (), w
) == blocked
.end ())
533 result
|= circuit (w
, path
, start
, blocked
, block_lists
, linfo
, count
);
538 if (result
!= NO_LOOP
)
539 unblock (v
, blocked
, block_lists
);
541 for (arc_t
*arc
= v
->succ
; arc
; arc
= arc
->succ_next
)
543 block_t
*w
= arc
->dst
;
544 if (w
< start
|| !linfo
.has_block (w
))
548 = find (blocked
.begin (), blocked
.end (), w
) - blocked
.begin ();
549 gcc_assert (index
< blocked
.size ());
550 block_vector_t
&list
= block_lists
[index
];
551 if (find (list
.begin (), list
.end (), v
) == list
.end ())
558 /* Find cycles for a LINFO. If HANDLE_NEGATIVE_CYCLES is set and the line
559 contains a negative loop, then perform the same function once again. */
562 get_cycles_count (line_t
&linfo
, bool handle_negative_cycles
= true)
564 /* Note that this algorithm works even if blocks aren't in sorted order.
565 Each iteration of the circuit detection is completely independent
566 (except for reducing counts, but that shouldn't matter anyways).
567 Therefore, operating on a permuted order (i.e., non-sorted) only
568 has the effect of permuting the output cycles. */
570 loop_type result
= NO_LOOP
;
572 for (block_t
*block
= linfo
.u
.blocks
; block
; block
= block
->chain
)
575 block_vector_t blocked
;
576 vector
<block_vector_t
> block_lists
;
577 result
|= circuit (block
, path
, block
, blocked
, block_lists
, linfo
,
581 /* If we have a negative cycle, repeat the find_cycles routine. */
582 if (result
== NEGATIVE_LOOP
&& handle_negative_cycles
)
583 count
+= get_cycles_count (linfo
, false);
589 main (int argc
, char **argv
)
595 p
= argv
[0] + strlen (argv
[0]);
596 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
600 xmalloc_set_program_name (progname
);
602 /* Unlock the stdio streams. */
603 unlock_std_streams ();
607 diagnostic_initialize (global_dc
, 0);
609 /* Handle response files. */
610 expandargv (&argc
, &argv
);
613 names
= XNEWVEC (name_map_t
, a_names
);
615 sources
= XNEWVEC (source_t
, a_sources
);
617 argno
= process_args (argc
, argv
);
621 if (argc
- argno
> 1)
626 for (; argno
!= argc
; argno
++)
628 if (flag_display_progress
)
629 printf ("Processing file %d out of %d\n", argno
- first_arg
+ 1,
631 process_file (argv
[argno
]);
634 generate_results (multiple_files
? NULL
: argv
[argc
- 1]);
636 release_structures ();
641 /* Print a usage message and exit. If ERROR_P is nonzero, this is an error,
642 otherwise the output of --help. */
645 print_usage (int error_p
)
647 FILE *file
= error_p
? stderr
: stdout
;
648 int status
= error_p
? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
;
650 fnotice (file
, "Usage: gcov [OPTION]... SOURCE|OBJ...\n\n");
651 fnotice (file
, "Print code coverage information.\n\n");
652 fnotice (file
, " -h, --help Print this help, then exit\n");
653 fnotice (file
, " -a, --all-blocks Show information for every basic block\n");
654 fnotice (file
, " -b, --branch-probabilities Include branch probabilities in output\n");
655 fnotice (file
, " -c, --branch-counts Output counts of branches taken\n\
656 rather than percentages\n");
657 fnotice (file
, " -d, --display-progress Display progress information\n");
658 fnotice (file
, " -f, --function-summaries Output summaries for each function\n");
659 fnotice (file
, " -i, --intermediate-format Output .gcov file in intermediate text format\n");
660 fnotice (file
, " -l, --long-file-names Use long output file names for included\n\
662 fnotice (file
, " -m, --demangled-names Output demangled function names\n");
663 fnotice (file
, " -n, --no-output Do not create an output file\n");
664 fnotice (file
, " -o, --object-directory DIR|FILE Search for object files in DIR or called FILE\n");
665 fnotice (file
, " -p, --preserve-paths Preserve all pathname components\n");
666 fnotice (file
, " -r, --relative-only Only show data for relative sources\n");
667 fnotice (file
, " -s, --source-prefix DIR Source prefix to elide\n");
668 fnotice (file
, " -u, --unconditional-branches Show unconditional branch counts too\n");
669 fnotice (file
, " -v, --version Print version number, then exit\n");
670 fnotice (file
, "\nFor bug reporting instructions, please see:\n%s.\n",
675 /* Print version information and exit. */
680 fnotice (stdout
, "gcov %s%s\n", pkgversion_string
, version_string
);
681 fprintf (stdout
, "Copyright %s 2016 Free Software Foundation, Inc.\n",
684 _("This is free software; see the source for copying conditions.\n"
685 "There is NO warranty; not even for MERCHANTABILITY or \n"
686 "FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
687 exit (SUCCESS_EXIT_CODE
);
690 static const struct option options
[] =
692 { "help", no_argument
, NULL
, 'h' },
693 { "version", no_argument
, NULL
, 'v' },
694 { "all-blocks", no_argument
, NULL
, 'a' },
695 { "branch-probabilities", no_argument
, NULL
, 'b' },
696 { "branch-counts", no_argument
, NULL
, 'c' },
697 { "intermediate-format", no_argument
, NULL
, 'i' },
698 { "no-output", no_argument
, NULL
, 'n' },
699 { "long-file-names", no_argument
, NULL
, 'l' },
700 { "function-summaries", no_argument
, NULL
, 'f' },
701 { "demangled-names", no_argument
, NULL
, 'm' },
702 { "preserve-paths", no_argument
, NULL
, 'p' },
703 { "relative-only", no_argument
, NULL
, 'r' },
704 { "object-directory", required_argument
, NULL
, 'o' },
705 { "object-file", required_argument
, NULL
, 'o' },
706 { "source-prefix", required_argument
, NULL
, 's' },
707 { "unconditional-branches", no_argument
, NULL
, 'u' },
708 { "display-progress", no_argument
, NULL
, 'd' },
712 /* Process args, return index to first non-arg. */
715 process_args (int argc
, char **argv
)
719 while ((opt
= getopt_long (argc
, argv
, "abcdfhilmno:s:pruv", options
, NULL
)) !=
734 flag_function_summary
= 1;
738 /* print_usage will exit. */
743 flag_demangled_names
= 1;
749 object_directory
= optarg
;
752 source_prefix
= optarg
;
753 source_length
= strlen (source_prefix
);
756 flag_relative_only
= 1;
759 flag_preserve_paths
= 1;
762 flag_unconditional
= 1;
765 flag_intermediate_format
= 1;
769 flag_display_progress
= 1;
773 /* print_version will exit. */
776 /* print_usage will exit. */
783 /* Get the name of the gcov file. The return value must be free'd.
785 It appends the '.gcov' extension to the *basename* of the file.
786 The resulting file name will be in PWD.
789 input: foo.da, output: foo.da.gcov
790 input: a/b/foo.cc, output: foo.cc.gcov */
793 get_gcov_intermediate_filename (const char *file_name
)
795 const char *gcov
= ".gcov";
799 /* Find the 'basename'. */
800 cptr
= lbasename (file_name
);
802 result
= XNEWVEC (char, strlen (cptr
) + strlen (gcov
) + 1);
803 sprintf (result
, "%s%s", cptr
, gcov
);
808 /* Output the result in intermediate format used by 'lcov'.
810 The intermediate format contains a single file named 'foo.cc.gcov',
811 with no source code included. A sample output is
816 function:19,1,_GLOBAL__sub_I__Z3foov
817 function:19,1,_Z41__static_initialization_and_destruction_0ii
824 file:/.../basic_ios.h
827 function:157,0,_ZStorSt12_Ios_IostateS_
829 file:/.../char_traits.h
830 function:258,0,_ZNSt11char_traitsIcE6lengthEPKc
834 The default gcov outputs multiple files: 'foo.cc.gcov',
835 'iostream.gcov', 'ios_base.h.gcov', etc. with source code
836 included. Instead the intermediate format here outputs only a single
837 file 'foo.cc.gcov' similar to the above example. */
840 output_intermediate_file (FILE *gcov_file
, source_t
*src
)
842 unsigned line_num
; /* current line number. */
843 const line_t
*line
; /* current line info ptr. */
844 function_t
*fn
; /* current function info ptr. */
846 fprintf (gcov_file
, "file:%s\n", src
->name
); /* source file name */
848 for (fn
= src
->functions
; fn
; fn
= fn
->next_file_fn
)
850 /* function:<name>,<line_number>,<execution_count> */
851 fprintf (gcov_file
, "function:%d,%s,%s\n", fn
->line
,
852 format_gcov (fn
->blocks
[0].count
, 0, -1),
853 flag_demangled_names
? fn
->demangled_name
: fn
->name
);
856 for (line_num
= 1, line
= &src
->lines
[line_num
];
857 line_num
< src
->num_lines
;
862 fprintf (gcov_file
, "lcount:%u,%s\n", line_num
,
863 format_gcov (line
->count
, 0, -1));
865 for (arc
= line
->u
.branches
; arc
; arc
= arc
->line_next
)
867 if (!arc
->is_unconditional
&& !arc
->is_call_non_return
)
869 const char *branch_type
;
870 /* branch:<line_num>,<branch_coverage_type>
872 : notexec (Branch not executed)
873 : taken (Branch executed and taken)
874 : nottaken (Branch executed, but not taken)
877 branch_type
= (arc
->count
> 0) ? "taken" : "nottaken";
879 branch_type
= "notexec";
880 fprintf (gcov_file
, "branch:%d,%s\n", line_num
, branch_type
);
886 /* Process a single input file. */
889 process_file (const char *file_name
)
893 create_file_names (file_name
);
894 fns
= read_graph_file ();
898 read_count_file (fns
);
901 function_t
*fn
= fns
;
907 unsigned src
= fn
->src
;
908 unsigned line
= fn
->line
;
910 function_t
*probe
, **prev
;
912 /* Now insert it into the source file's list of
913 functions. Normally functions will be encountered in
914 ascending order, so a simple scan is quick. Note we're
915 building this list in reverse order. */
916 for (prev
= &sources
[src
].functions
;
917 (probe
= *prev
); prev
= &probe
->next_file_fn
)
918 if (probe
->line
<= line
)
920 fn
->next_file_fn
= probe
;
923 /* Mark last line in files touched by function. */
924 for (block_no
= 0; block_no
!= fn
->num_blocks
; block_no
++)
926 unsigned *enc
= fn
->blocks
[block_no
].u
.line
.encoding
;
927 unsigned num
= fn
->blocks
[block_no
].u
.line
.num
;
934 if (line
>= sources
[src
].num_lines
)
935 sources
[src
].num_lines
= line
+ 1;
942 else if (*enc
> line
)
945 if (line
>= sources
[src
].num_lines
)
946 sources
[src
].num_lines
= line
+ 1;
948 solve_flow_graph (fn
);
950 find_exception_blocks (fn
);
955 /* The function was not in the executable -- some other
956 instance must have been selected. */
957 release_function (fn
);
962 output_gcov_file (const char *file_name
, source_t
*src
)
964 char *gcov_file_name
= make_gcov_file_name (file_name
, src
->coverage
.name
);
966 if (src
->coverage
.lines
)
968 FILE *gcov_file
= fopen (gcov_file_name
, "w");
971 fnotice (stdout
, "Creating '%s'\n", gcov_file_name
);
972 output_lines (gcov_file
, src
);
973 if (ferror (gcov_file
))
974 fnotice (stderr
, "Error writing output file '%s'\n", gcov_file_name
);
978 fnotice (stderr
, "Could not open output file '%s'\n", gcov_file_name
);
982 unlink (gcov_file_name
);
983 fnotice (stdout
, "Removing '%s'\n", gcov_file_name
);
985 free (gcov_file_name
);
989 generate_results (const char *file_name
)
994 FILE *gcov_intermediate_file
= NULL
;
995 char *gcov_intermediate_filename
= NULL
;
997 for (ix
= n_sources
, src
= sources
; ix
--; src
++)
999 src
->lines
= XCNEWVEC (line_t
, src
->num_lines
);
1001 for (fn
= functions
; fn
; fn
= fn
->next
)
1003 coverage_t coverage
;
1005 memset (&coverage
, 0, sizeof (coverage
));
1006 coverage
.name
= flag_demangled_names
? fn
->demangled_name
: fn
->name
;
1007 add_line_counts (flag_function_summary
? &coverage
: NULL
, fn
);
1008 if (flag_function_summary
)
1010 function_summary (&coverage
, "Function");
1011 fnotice (stdout
, "\n");
1017 name_map_t
*name_map
= (name_map_t
*)bsearch
1018 (file_name
, names
, n_names
, sizeof (*names
), name_search
);
1020 file_name
= sources
[name_map
->src
].coverage
.name
;
1022 file_name
= canonicalize_name (file_name
);
1025 if (flag_gcov_file
&& flag_intermediate_format
)
1027 /* Open the intermediate file. */
1028 gcov_intermediate_filename
= get_gcov_intermediate_filename (file_name
);
1029 gcov_intermediate_file
= fopen (gcov_intermediate_filename
, "w");
1030 if (!gcov_intermediate_file
)
1032 fnotice (stderr
, "Cannot open intermediate output file %s\n",
1033 gcov_intermediate_filename
);
1038 for (ix
= n_sources
, src
= sources
; ix
--; src
++)
1040 if (flag_relative_only
)
1042 /* Ignore this source, if it is an absolute path (after
1043 source prefix removal). */
1044 char first
= src
->coverage
.name
[0];
1046 #if HAVE_DOS_BASED_FILE_SYSTEM
1047 if (first
&& src
->coverage
.name
[1] == ':')
1048 first
= src
->coverage
.name
[2];
1050 if (IS_DIR_SEPARATOR (first
))
1054 accumulate_line_counts (src
);
1055 function_summary (&src
->coverage
, "File");
1056 total_lines
+= src
->coverage
.lines
;
1057 total_executed
+= src
->coverage
.lines_executed
;
1060 if (flag_intermediate_format
)
1061 /* Output the intermediate format without requiring source
1062 files. This outputs a section to a *single* file. */
1063 output_intermediate_file (gcov_intermediate_file
, src
);
1065 output_gcov_file (file_name
, src
);
1066 fnotice (stdout
, "\n");
1070 if (flag_gcov_file
&& flag_intermediate_format
)
1072 /* Now we've finished writing the intermediate file. */
1073 fclose (gcov_intermediate_file
);
1074 XDELETEVEC (gcov_intermediate_filename
);
1078 executed_summary (total_lines
, total_executed
);
1081 /* Release a function structure */
1084 release_function (function_t
*fn
)
1089 for (ix
= fn
->num_blocks
, block
= fn
->blocks
; ix
--; block
++)
1093 for (arc
= block
->succ
; arc
; arc
= arc_n
)
1095 arc_n
= arc
->succ_next
;
1101 if (flag_demangled_names
&& fn
->demangled_name
!= fn
->name
)
1102 free (fn
->demangled_name
);
1106 /* Release all memory used. */
1109 release_structures (void)
1114 for (ix
= n_sources
; ix
--;)
1115 free (sources
[ix
].lines
);
1118 for (ix
= n_names
; ix
--;)
1119 free (names
[ix
].name
);
1122 while ((fn
= functions
))
1124 functions
= fn
->next
;
1125 release_function (fn
);
1129 /* Generate the names of the graph and data files. If OBJECT_DIRECTORY
1130 is not specified, these are named from FILE_NAME sans extension. If
1131 OBJECT_DIRECTORY is specified and is a directory, the files are in that
1132 directory, but named from the basename of the FILE_NAME, sans extension.
1133 Otherwise OBJECT_DIRECTORY is taken to be the name of the object *file*
1134 and the data files are named from that. */
1137 create_file_names (const char *file_name
)
1141 int length
= strlen (file_name
);
1144 /* Free previous file names. */
1145 free (bbg_file_name
);
1146 free (da_file_name
);
1147 da_file_name
= bbg_file_name
= NULL
;
1151 if (object_directory
&& object_directory
[0])
1155 length
+= strlen (object_directory
) + 2;
1156 name
= XNEWVEC (char, length
);
1159 base
= !stat (object_directory
, &status
) && S_ISDIR (status
.st_mode
);
1160 strcat (name
, object_directory
);
1161 if (base
&& (!IS_DIR_SEPARATOR (name
[strlen (name
) - 1])))
1166 name
= XNEWVEC (char, length
+ 1);
1167 strcpy (name
, file_name
);
1173 /* Append source file name. */
1174 const char *cptr
= lbasename (file_name
);
1175 strcat (name
, cptr
? cptr
: file_name
);
1178 /* Remove the extension. */
1179 cptr
= strrchr (CONST_CAST (char *, lbasename (name
)), '.');
1183 length
= strlen (name
);
1185 bbg_file_name
= XNEWVEC (char, length
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
1186 strcpy (bbg_file_name
, name
);
1187 strcpy (bbg_file_name
+ length
, GCOV_NOTE_SUFFIX
);
1189 da_file_name
= XNEWVEC (char, length
+ strlen (GCOV_DATA_SUFFIX
) + 1);
1190 strcpy (da_file_name
, name
);
1191 strcpy (da_file_name
+ length
, GCOV_DATA_SUFFIX
);
1197 /* A is a string and B is a pointer to name_map_t. Compare for file
1198 name orderability. */
1201 name_search (const void *a_
, const void *b_
)
1203 const char *a
= (const char *)a_
;
1204 const name_map_t
*b
= (const name_map_t
*)b_
;
1206 #if HAVE_DOS_BASED_FILE_SYSTEM
1207 return strcasecmp (a
, b
->name
);
1209 return strcmp (a
, b
->name
);
1213 /* A and B are a pointer to name_map_t. Compare for file name
1217 name_sort (const void *a_
, const void *b_
)
1219 const name_map_t
*a
= (const name_map_t
*)a_
;
1220 return name_search (a
->name
, b_
);
1223 /* Find or create a source file structure for FILE_NAME. Copies
1224 FILE_NAME on creation */
1227 find_source (const char *file_name
)
1229 name_map_t
*name_map
;
1235 file_name
= "<unknown>";
1236 name_map
= (name_map_t
*)bsearch
1237 (file_name
, names
, n_names
, sizeof (*names
), name_search
);
1240 idx
= name_map
->src
;
1244 if (n_names
+ 2 > a_names
)
1246 /* Extend the name map array -- we'll be inserting one or two
1249 name_map
= XNEWVEC (name_map_t
, a_names
);
1250 memcpy (name_map
, names
, n_names
* sizeof (*names
));
1255 /* Not found, try the canonical name. */
1256 canon
= canonicalize_name (file_name
);
1257 name_map
= (name_map_t
*) bsearch (canon
, names
, n_names
, sizeof (*names
),
1261 /* Not found with canonical name, create a new source. */
1264 if (n_sources
== a_sources
)
1267 src
= XNEWVEC (source_t
, a_sources
);
1268 memcpy (src
, sources
, n_sources
* sizeof (*sources
));
1275 name_map
= &names
[n_names
++];
1276 name_map
->name
= canon
;
1277 name_map
->src
= idx
;
1279 src
= &sources
[n_sources
++];
1280 memset (src
, 0, sizeof (*src
));
1282 src
->coverage
.name
= src
->name
;
1284 #if HAVE_DOS_BASED_FILE_SYSTEM
1285 /* You lose if separators don't match exactly in the
1287 && !strncasecmp (source_prefix
, src
->coverage
.name
, source_length
)
1289 && !strncmp (source_prefix
, src
->coverage
.name
, source_length
)
1291 && IS_DIR_SEPARATOR (src
->coverage
.name
[source_length
]))
1292 src
->coverage
.name
+= source_length
+ 1;
1293 if (!stat (src
->name
, &status
))
1294 src
->file_time
= status
.st_mtime
;
1297 idx
= name_map
->src
;
1299 if (name_search (file_name
, name_map
))
1301 /* Append the non-canonical name. */
1302 name_map
= &names
[n_names
++];
1303 name_map
->name
= xstrdup (file_name
);
1304 name_map
->src
= idx
;
1307 /* Resort the name map. */
1308 qsort (names
, n_names
, sizeof (*names
), name_sort
);
1311 if (sources
[idx
].file_time
> bbg_file_time
)
1313 static int info_emitted
;
1315 fnotice (stderr
, "%s:source file is newer than notes file '%s'\n",
1316 file_name
, bbg_file_name
);
1320 "(the message is displayed only once per source file)\n");
1323 sources
[idx
].file_time
= 0;
1329 /* Read the notes file. Return list of functions read -- in reverse order. */
1332 read_graph_file (void)
1335 unsigned current_tag
= 0;
1336 function_t
*fn
= NULL
;
1337 function_t
*fns
= NULL
;
1338 function_t
**fns_end
= &fns
;
1339 unsigned src_idx
= 0;
1343 if (!gcov_open (bbg_file_name
, 1))
1345 fnotice (stderr
, "%s:cannot open notes file\n", bbg_file_name
);
1348 bbg_file_time
= gcov_time ();
1349 if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC
))
1351 fnotice (stderr
, "%s:not a gcov notes file\n", bbg_file_name
);
1356 version
= gcov_read_unsigned ();
1357 if (version
!= GCOV_VERSION
)
1361 GCOV_UNSIGNED2STRING (v
, version
);
1362 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1364 fnotice (stderr
, "%s:version '%.4s', prefer '%.4s'\n",
1365 bbg_file_name
, v
, e
);
1367 bbg_stamp
= gcov_read_unsigned ();
1369 while ((tag
= gcov_read_unsigned ()))
1371 unsigned length
= gcov_read_unsigned ();
1372 gcov_position_t base
= gcov_position ();
1374 if (tag
== GCOV_TAG_FUNCTION
)
1376 char *function_name
;
1377 unsigned ident
, lineno
;
1378 unsigned lineno_checksum
, cfg_checksum
;
1380 ident
= gcov_read_unsigned ();
1381 lineno_checksum
= gcov_read_unsigned ();
1382 cfg_checksum
= gcov_read_unsigned ();
1383 function_name
= xstrdup (gcov_read_string ());
1384 src_idx
= find_source (gcov_read_string ());
1385 lineno
= gcov_read_unsigned ();
1387 fn
= XCNEW (function_t
);
1388 fn
->name
= function_name
;
1389 if (flag_demangled_names
)
1391 fn
->demangled_name
= cplus_demangle (fn
->name
, DMGL_PARAMS
);
1392 if (!fn
->demangled_name
)
1393 fn
->demangled_name
= fn
->name
;
1396 fn
->lineno_checksum
= lineno_checksum
;
1397 fn
->cfg_checksum
= cfg_checksum
;
1401 fn
->next_file_fn
= NULL
;
1404 fns_end
= &fn
->next
;
1407 else if (fn
&& tag
== GCOV_TAG_BLOCKS
)
1410 fnotice (stderr
, "%s:already seen blocks for '%s'\n",
1411 bbg_file_name
, fn
->name
);
1414 unsigned ix
, num_blocks
= GCOV_TAG_BLOCKS_NUM (length
);
1415 fn
->num_blocks
= num_blocks
;
1417 fn
->blocks
= XCNEWVEC (block_t
, fn
->num_blocks
);
1418 for (ix
= 0; ix
!= num_blocks
; ix
++)
1419 fn
->blocks
[ix
].flags
= gcov_read_unsigned ();
1422 else if (fn
&& tag
== GCOV_TAG_ARCS
)
1424 unsigned src
= gcov_read_unsigned ();
1425 unsigned num_dests
= GCOV_TAG_ARCS_NUM (length
);
1426 block_t
*src_blk
= &fn
->blocks
[src
];
1427 unsigned mark_catches
= 0;
1428 struct arc_info
*arc
;
1430 if (src
>= fn
->num_blocks
|| fn
->blocks
[src
].succ
)
1435 unsigned dest
= gcov_read_unsigned ();
1436 unsigned flags
= gcov_read_unsigned ();
1438 if (dest
>= fn
->num_blocks
)
1440 arc
= XCNEW (arc_t
);
1442 arc
->dst
= &fn
->blocks
[dest
];
1446 arc
->count_valid
= 0;
1447 arc
->on_tree
= !!(flags
& GCOV_ARC_ON_TREE
);
1448 arc
->fake
= !!(flags
& GCOV_ARC_FAKE
);
1449 arc
->fall_through
= !!(flags
& GCOV_ARC_FALLTHROUGH
);
1451 arc
->succ_next
= src_blk
->succ
;
1452 src_blk
->succ
= arc
;
1453 src_blk
->num_succ
++;
1455 arc
->pred_next
= fn
->blocks
[dest
].pred
;
1456 fn
->blocks
[dest
].pred
= arc
;
1457 fn
->blocks
[dest
].num_pred
++;
1463 /* Exceptional exit from this function, the
1464 source block must be a call. */
1465 fn
->blocks
[src
].is_call_site
= 1;
1466 arc
->is_call_non_return
= 1;
1471 /* Non-local return from a callee of this
1472 function. The destination block is a setjmp. */
1473 arc
->is_nonlocal_return
= 1;
1474 fn
->blocks
[dest
].is_nonlocal_return
= 1;
1484 /* We have a fake exit from this block. The other
1485 non-fall through exits must be to catch handlers.
1486 Mark them as catch arcs. */
1488 for (arc
= src_blk
->succ
; arc
; arc
= arc
->succ_next
)
1489 if (!arc
->fake
&& !arc
->fall_through
)
1496 else if (fn
&& tag
== GCOV_TAG_LINES
)
1498 unsigned blockno
= gcov_read_unsigned ();
1499 unsigned *line_nos
= XCNEWVEC (unsigned, length
- 1);
1501 if (blockno
>= fn
->num_blocks
|| fn
->blocks
[blockno
].u
.line
.encoding
)
1506 unsigned lineno
= gcov_read_unsigned ();
1513 line_nos
[ix
++] = src_idx
;
1515 line_nos
[ix
++] = lineno
;
1519 const char *file_name
= gcov_read_string ();
1523 src_idx
= find_source (file_name
);
1525 line_nos
[ix
++] = src_idx
;
1529 fn
->blocks
[blockno
].u
.line
.encoding
= line_nos
;
1530 fn
->blocks
[blockno
].u
.line
.num
= ix
;
1532 else if (current_tag
&& !GCOV_TAG_IS_SUBTAG (current_tag
, tag
))
1537 gcov_sync (base
, length
);
1538 if (gcov_is_error ())
1541 fnotice (stderr
, "%s:corrupted\n", bbg_file_name
);
1548 fnotice (stderr
, "%s:no functions found\n", bbg_file_name
);
1553 /* Reads profiles from the count file and attach to each
1554 function. Return nonzero if fatal error. */
1557 read_count_file (function_t
*fns
)
1562 function_t
*fn
= NULL
;
1565 if (!gcov_open (da_file_name
, 1))
1567 fnotice (stderr
, "%s:cannot open data file, assuming not executed\n",
1572 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
1574 fnotice (stderr
, "%s:not a gcov data file\n", da_file_name
);
1579 version
= gcov_read_unsigned ();
1580 if (version
!= GCOV_VERSION
)
1584 GCOV_UNSIGNED2STRING (v
, version
);
1585 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1587 fnotice (stderr
, "%s:version '%.4s', prefer version '%.4s'\n",
1588 da_file_name
, v
, e
);
1590 tag
= gcov_read_unsigned ();
1591 if (tag
!= bbg_stamp
)
1593 fnotice (stderr
, "%s:stamp mismatch with notes file\n", da_file_name
);
1597 while ((tag
= gcov_read_unsigned ()))
1599 unsigned length
= gcov_read_unsigned ();
1600 unsigned long base
= gcov_position ();
1602 if (tag
== GCOV_TAG_PROGRAM_SUMMARY
)
1604 struct gcov_summary summary
;
1605 gcov_read_summary (&summary
);
1606 object_runs
+= summary
.ctrs
[GCOV_COUNTER_ARCS
].runs
;
1609 else if (tag
== GCOV_TAG_FUNCTION
&& !length
)
1611 else if (tag
== GCOV_TAG_FUNCTION
&& length
== GCOV_TAG_FUNCTION_LENGTH
)
1614 struct function_info
*fn_n
;
1616 /* Try to find the function in the list. To speed up the
1617 search, first start from the last function found. */
1618 ident
= gcov_read_unsigned ();
1620 for (fn
= fn
? fn
->next
: NULL
; ; fn
= fn
->next
)
1624 else if ((fn
= fn_n
))
1628 fnotice (stderr
, "%s:unknown function '%u'\n",
1629 da_file_name
, ident
);
1632 if (fn
->ident
== ident
)
1638 else if (gcov_read_unsigned () != fn
->lineno_checksum
1639 || gcov_read_unsigned () != fn
->cfg_checksum
)
1642 fnotice (stderr
, "%s:profile mismatch for '%s'\n",
1643 da_file_name
, fn
->name
);
1647 else if (tag
== GCOV_TAG_FOR_COUNTER (GCOV_COUNTER_ARCS
) && fn
)
1649 if (length
!= GCOV_TAG_COUNTER_LENGTH (fn
->num_counts
))
1653 fn
->counts
= XCNEWVEC (gcov_type
, fn
->num_counts
);
1655 for (ix
= 0; ix
!= fn
->num_counts
; ix
++)
1656 fn
->counts
[ix
] += gcov_read_counter ();
1658 gcov_sync (base
, length
);
1659 if ((error
= gcov_is_error ()))
1661 fnotice (stderr
, error
< 0 ? "%s:overflowed\n" : "%s:corrupted\n",
1671 /* Solve the flow graph. Propagate counts from the instrumented arcs
1672 to the blocks and the uninstrumented arcs. */
1675 solve_flow_graph (function_t
*fn
)
1679 gcov_type
*count_ptr
= fn
->counts
;
1681 block_t
*valid_blocks
= NULL
; /* valid, but unpropagated blocks. */
1682 block_t
*invalid_blocks
= NULL
; /* invalid, but inferable blocks. */
1684 /* The arcs were built in reverse order. Fix that now. */
1685 for (ix
= fn
->num_blocks
; ix
--;)
1687 arc_t
*arc_p
, *arc_n
;
1689 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].succ
; arc
;
1690 arc_p
= arc
, arc
= arc_n
)
1692 arc_n
= arc
->succ_next
;
1693 arc
->succ_next
= arc_p
;
1695 fn
->blocks
[ix
].succ
= arc_p
;
1697 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].pred
; arc
;
1698 arc_p
= arc
, arc
= arc_n
)
1700 arc_n
= arc
->pred_next
;
1701 arc
->pred_next
= arc_p
;
1703 fn
->blocks
[ix
].pred
= arc_p
;
1706 if (fn
->num_blocks
< 2)
1707 fnotice (stderr
, "%s:'%s' lacks entry and/or exit blocks\n",
1708 bbg_file_name
, fn
->name
);
1711 if (fn
->blocks
[ENTRY_BLOCK
].num_pred
)
1712 fnotice (stderr
, "%s:'%s' has arcs to entry block\n",
1713 bbg_file_name
, fn
->name
);
1715 /* We can't deduce the entry block counts from the lack of
1717 fn
->blocks
[ENTRY_BLOCK
].num_pred
= ~(unsigned)0;
1719 if (fn
->blocks
[EXIT_BLOCK
].num_succ
)
1720 fnotice (stderr
, "%s:'%s' has arcs from exit block\n",
1721 bbg_file_name
, fn
->name
);
1723 /* Likewise, we can't deduce exit block counts from the lack
1724 of its successors. */
1725 fn
->blocks
[EXIT_BLOCK
].num_succ
= ~(unsigned)0;
1728 /* Propagate the measured counts, this must be done in the same
1729 order as the code in profile.c */
1730 for (ix
= 0, blk
= fn
->blocks
; ix
!= fn
->num_blocks
; ix
++, blk
++)
1732 block_t
const *prev_dst
= NULL
;
1733 int out_of_order
= 0;
1734 int non_fake_succ
= 0;
1736 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1744 arc
->count
= *count_ptr
++;
1745 arc
->count_valid
= 1;
1747 arc
->dst
->num_pred
--;
1749 if (prev_dst
&& prev_dst
> arc
->dst
)
1751 prev_dst
= arc
->dst
;
1753 if (non_fake_succ
== 1)
1755 /* If there is only one non-fake exit, it is an
1756 unconditional branch. */
1757 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1760 arc
->is_unconditional
= 1;
1761 /* If this block is instrumenting a call, it might be
1762 an artificial block. It is not artificial if it has
1763 a non-fallthrough exit, or the destination of this
1764 arc has more than one entry. Mark the destination
1765 block as a return site, if none of those conditions
1767 if (blk
->is_call_site
&& arc
->fall_through
1768 && arc
->dst
->pred
== arc
&& !arc
->pred_next
)
1769 arc
->dst
->is_call_return
= 1;
1773 /* Sort the successor arcs into ascending dst order. profile.c
1774 normally produces arcs in the right order, but sometimes with
1775 one or two out of order. We're not using a particularly
1779 arc_t
*start
= blk
->succ
;
1780 unsigned changes
= 1;
1784 arc_t
*arc
, *arc_p
, *arc_n
;
1787 for (arc_p
= NULL
, arc
= start
; (arc_n
= arc
->succ_next
);)
1789 if (arc
->dst
> arc_n
->dst
)
1793 arc_p
->succ_next
= arc_n
;
1796 arc
->succ_next
= arc_n
->succ_next
;
1797 arc_n
->succ_next
= arc
;
1810 /* Place it on the invalid chain, it will be ignored if that's
1812 blk
->invalid_chain
= 1;
1813 blk
->chain
= invalid_blocks
;
1814 invalid_blocks
= blk
;
1817 while (invalid_blocks
|| valid_blocks
)
1819 while ((blk
= invalid_blocks
))
1821 gcov_type total
= 0;
1824 invalid_blocks
= blk
->chain
;
1825 blk
->invalid_chain
= 0;
1827 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1828 total
+= arc
->count
;
1829 else if (!blk
->num_pred
)
1830 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
1831 total
+= arc
->count
;
1836 blk
->count_valid
= 1;
1837 blk
->chain
= valid_blocks
;
1838 blk
->valid_chain
= 1;
1841 while ((blk
= valid_blocks
))
1844 arc_t
*arc
, *inv_arc
;
1846 valid_blocks
= blk
->chain
;
1847 blk
->valid_chain
= 0;
1848 if (blk
->num_succ
== 1)
1854 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1856 total
-= arc
->count
;
1857 if (!arc
->count_valid
)
1861 inv_arc
->count_valid
= 1;
1862 inv_arc
->count
= total
;
1865 if (dst
->count_valid
)
1867 if (dst
->num_pred
== 1 && !dst
->valid_chain
)
1869 dst
->chain
= valid_blocks
;
1870 dst
->valid_chain
= 1;
1876 if (!dst
->num_pred
&& !dst
->invalid_chain
)
1878 dst
->chain
= invalid_blocks
;
1879 dst
->invalid_chain
= 1;
1880 invalid_blocks
= dst
;
1884 if (blk
->num_pred
== 1)
1890 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
1892 total
-= arc
->count
;
1893 if (!arc
->count_valid
)
1897 inv_arc
->count_valid
= 1;
1898 inv_arc
->count
= total
;
1901 if (src
->count_valid
)
1903 if (src
->num_succ
== 1 && !src
->valid_chain
)
1905 src
->chain
= valid_blocks
;
1906 src
->valid_chain
= 1;
1912 if (!src
->num_succ
&& !src
->invalid_chain
)
1914 src
->chain
= invalid_blocks
;
1915 src
->invalid_chain
= 1;
1916 invalid_blocks
= src
;
1923 /* If the graph has been correctly solved, every block will have a
1925 for (ix
= 0; ix
< fn
->num_blocks
; ix
++)
1926 if (!fn
->blocks
[ix
].count_valid
)
1928 fnotice (stderr
, "%s:graph is unsolvable for '%s'\n",
1929 bbg_file_name
, fn
->name
);
1934 /* Mark all the blocks only reachable via an incoming catch. */
1937 find_exception_blocks (function_t
*fn
)
1940 block_t
**queue
= XALLOCAVEC (block_t
*, fn
->num_blocks
);
1942 /* First mark all blocks as exceptional. */
1943 for (ix
= fn
->num_blocks
; ix
--;)
1944 fn
->blocks
[ix
].exceptional
= 1;
1946 /* Now mark all the blocks reachable via non-fake edges */
1947 queue
[0] = fn
->blocks
;
1948 queue
[0]->exceptional
= 0;
1951 block_t
*block
= queue
[--ix
];
1954 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
1955 if (!arc
->fake
&& !arc
->is_throw
&& arc
->dst
->exceptional
)
1957 arc
->dst
->exceptional
= 0;
1958 queue
[ix
++] = arc
->dst
;
1964 /* Increment totals in COVERAGE according to arc ARC. */
1967 add_branch_counts (coverage_t
*coverage
, const arc_t
*arc
)
1969 if (arc
->is_call_non_return
)
1972 if (arc
->src
->count
)
1973 coverage
->calls_executed
++;
1975 else if (!arc
->is_unconditional
)
1977 coverage
->branches
++;
1978 if (arc
->src
->count
)
1979 coverage
->branches_executed
++;
1981 coverage
->branches_taken
++;
1985 /* Format a GCOV_TYPE integer as either a percent ratio, or absolute
1986 count. If dp >= 0, format TOP/BOTTOM * 100 to DP decimal places.
1987 If DP is zero, no decimal point is printed. Only print 100% when
1988 TOP==BOTTOM and only print 0% when TOP=0. If dp < 0, then simply
1989 format TOP. Return pointer to a static string. */
1992 format_gcov (gcov_type top
, gcov_type bottom
, int dp
)
1994 static char buffer
[20];
1998 float ratio
= bottom
? (float)top
/ bottom
: 0;
2000 unsigned limit
= 100;
2003 for (ix
= dp
; ix
--; )
2006 percent
= (unsigned) (ratio
* limit
+ (float)0.5);
2007 if (percent
<= 0 && top
)
2009 else if (percent
>= limit
&& top
!= bottom
)
2010 percent
= limit
- 1;
2011 ix
= sprintf (buffer
, "%.*u%%", dp
+ 1, percent
);
2017 buffer
[ix
+1] = buffer
[ix
];
2021 buffer
[ix
+ 1] = '.';
2025 sprintf (buffer
, "%" PRId64
, (int64_t)top
);
2030 /* Summary of execution */
2033 executed_summary (unsigned lines
, unsigned executed
)
2036 fnotice (stdout
, "Lines executed:%s of %d\n",
2037 format_gcov (executed
, lines
, 2), lines
);
2039 fnotice (stdout
, "No executable lines\n");
2042 /* Output summary info for a function or file. */
2045 function_summary (const coverage_t
*coverage
, const char *title
)
2047 fnotice (stdout
, "%s '%s'\n", title
, coverage
->name
);
2048 executed_summary (coverage
->lines
, coverage
->lines_executed
);
2052 if (coverage
->branches
)
2054 fnotice (stdout
, "Branches executed:%s of %d\n",
2055 format_gcov (coverage
->branches_executed
,
2056 coverage
->branches
, 2),
2057 coverage
->branches
);
2058 fnotice (stdout
, "Taken at least once:%s of %d\n",
2059 format_gcov (coverage
->branches_taken
,
2060 coverage
->branches
, 2),
2061 coverage
->branches
);
2064 fnotice (stdout
, "No branches\n");
2065 if (coverage
->calls
)
2066 fnotice (stdout
, "Calls executed:%s of %d\n",
2067 format_gcov (coverage
->calls_executed
, coverage
->calls
, 2),
2070 fnotice (stdout
, "No calls\n");
2074 /* Canonicalize the filename NAME by canonicalizing directory
2075 separators, eliding . components and resolving .. components
2076 appropriately. Always returns a unique string. */
2079 canonicalize_name (const char *name
)
2081 /* The canonical name cannot be longer than the incoming name. */
2082 char *result
= XNEWVEC (char, strlen (name
) + 1);
2083 const char *base
= name
, *probe
;
2088 #if HAVE_DOS_BASED_FILE_SYSTEM
2089 if (base
[0] && base
[1] == ':')
2091 result
[0] = base
[0];
2097 for (dd_base
= ptr
; *base
; base
= probe
)
2101 for (probe
= base
; *probe
; probe
++)
2102 if (IS_DIR_SEPARATOR (*probe
))
2106 if (len
== 1 && base
[0] == '.')
2107 /* Elide a '.' directory */
2109 else if (len
== 2 && base
[0] == '.' && base
[1] == '.')
2111 /* '..', we can only elide it and the previous directory, if
2112 we're not a symlink. */
2113 struct stat ATTRIBUTE_UNUSED buf
;
2117 #if defined (S_ISLNK)
2118 /* S_ISLNK is not POSIX.1-1996. */
2119 || stat (result
, &buf
) || S_ISLNK (buf
.st_mode
)
2123 /* Cannot elide, or unreadable or a symlink. */
2124 dd_base
= ptr
+ 2 + slash
;
2127 while (ptr
!= dd_base
&& *ptr
!= '/')
2129 slash
= ptr
!= result
;
2134 /* Regular pathname component. */
2137 memcpy (ptr
, base
, len
);
2142 for (; IS_DIR_SEPARATOR (*probe
); probe
++)
2150 /* Generate an output file name. INPUT_NAME is the canonicalized main
2151 input file and SRC_NAME is the canonicalized file name.
2152 LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
2153 long_output_names we prepend the processed name of the input file
2154 to each output name (except when the current source file is the
2155 input file, so you don't get a double concatenation). The two
2156 components are separated by '##'. With preserve_paths we create a
2157 filename from all path components of the source file, replacing '/'
2158 with '#', and .. with '^', without it we simply take the basename
2159 component. (Remember, the canonicalized name will already have
2160 elided '.' components and converted \\ separators.) */
2163 make_gcov_file_name (const char *input_name
, const char *src_name
)
2168 if (flag_long_names
&& input_name
&& strcmp (src_name
, input_name
))
2170 /* Generate the input filename part. */
2171 result
= XNEWVEC (char, strlen (input_name
) + strlen (src_name
) + 10);
2174 ptr
= mangle_name (input_name
, ptr
);
2175 ptr
[0] = ptr
[1] = '#';
2180 result
= XNEWVEC (char, strlen (src_name
) + 10);
2184 ptr
= mangle_name (src_name
, ptr
);
2185 strcpy (ptr
, ".gcov");
2191 mangle_name (char const *base
, char *ptr
)
2195 /* Generate the source filename part. */
2196 if (!flag_preserve_paths
)
2198 base
= lbasename (base
);
2199 len
= strlen (base
);
2200 memcpy (ptr
, base
, len
);
2205 /* Convert '/' to '#', convert '..' to '^',
2206 convert ':' to '~' on DOS based file system. */
2209 #if HAVE_DOS_BASED_FILE_SYSTEM
2210 if (base
[0] && base
[1] == ':')
2218 for (; *base
; base
= probe
)
2222 for (probe
= base
; *probe
; probe
++)
2226 if (len
== 2 && base
[0] == '.' && base
[1] == '.')
2230 memcpy (ptr
, base
, len
);
2244 /* Scan through the bb_data for each line in the block, increment
2245 the line number execution count indicated by the execution count of
2246 the appropriate basic block. */
2249 add_line_counts (coverage_t
*coverage
, function_t
*fn
)
2252 line_t
*line
= NULL
; /* This is propagated from one iteration to the
2255 /* Scan each basic block. */
2256 for (ix
= 0; ix
!= fn
->num_blocks
; ix
++)
2258 block_t
*block
= &fn
->blocks
[ix
];
2260 const source_t
*src
= NULL
;
2263 if (block
->count
&& ix
&& ix
+ 1 != fn
->num_blocks
)
2264 fn
->blocks_executed
++;
2265 for (jx
= 0, encoding
= block
->u
.line
.encoding
;
2266 jx
!= block
->u
.line
.num
; jx
++, encoding
++)
2269 src
= &sources
[*++encoding
];
2274 line
= &src
->lines
[*encoding
];
2280 if (!line
->count
&& block
->count
)
2281 coverage
->lines_executed
++;
2284 if (!block
->exceptional
)
2285 line
->unexceptional
= 1;
2286 line
->count
+= block
->count
;
2288 free (block
->u
.line
.encoding
);
2289 block
->u
.cycle
.arc
= NULL
;
2290 block
->u
.cycle
.ident
= ~0U;
2292 if (!ix
|| ix
+ 1 == fn
->num_blocks
)
2293 /* Entry or exit block */;
2294 else if (flag_all_blocks
)
2296 line_t
*block_line
= line
;
2299 block_line
= &sources
[fn
->src
].lines
[fn
->line
];
2301 block
->chain
= block_line
->u
.blocks
;
2302 block_line
->u
.blocks
= block
;
2304 else if (flag_branches
)
2308 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2310 arc
->line_next
= line
->u
.branches
;
2311 line
->u
.branches
= arc
;
2312 if (coverage
&& !arc
->is_unconditional
)
2313 add_branch_counts (coverage
, arc
);
2318 fnotice (stderr
, "%s:no lines for '%s'\n", bbg_file_name
, fn
->name
);
2321 /* Accumulate the line counts of a file. */
2324 accumulate_line_counts (source_t
*src
)
2327 function_t
*fn
, *fn_p
, *fn_n
;
2330 /* Reverse the function order. */
2331 for (fn
= src
->functions
, fn_p
= NULL
; fn
; fn_p
= fn
, fn
= fn_n
)
2333 fn_n
= fn
->next_file_fn
;
2334 fn
->next_file_fn
= fn_p
;
2336 src
->functions
= fn_p
;
2338 for (ix
= src
->num_lines
, line
= src
->lines
; ix
--; line
++)
2340 if (!flag_all_blocks
)
2342 arc_t
*arc
, *arc_p
, *arc_n
;
2344 /* Total and reverse the branch information. */
2345 for (arc
= line
->u
.branches
, arc_p
= NULL
; arc
;
2346 arc_p
= arc
, arc
= arc_n
)
2348 arc_n
= arc
->line_next
;
2349 arc
->line_next
= arc_p
;
2351 add_branch_counts (&src
->coverage
, arc
);
2353 line
->u
.branches
= arc_p
;
2355 else if (line
->u
.blocks
)
2357 /* The user expects the line count to be the number of times
2358 a line has been executed. Simply summing the block count
2359 will give an artificially high number. The Right Thing
2360 is to sum the entry counts to the graph of blocks on this
2361 line, then find the elementary cycles of the local graph
2362 and add the transition counts of those cycles. */
2363 block_t
*block
, *block_p
, *block_n
;
2364 gcov_type count
= 0;
2366 /* Reverse the block information. */
2367 for (block
= line
->u
.blocks
, block_p
= NULL
; block
;
2368 block_p
= block
, block
= block_n
)
2370 block_n
= block
->chain
;
2371 block
->chain
= block_p
;
2372 block
->u
.cycle
.ident
= ix
;
2374 line
->u
.blocks
= block_p
;
2376 /* Sum the entry arcs. */
2377 for (block
= line
->u
.blocks
; block
; block
= block
->chain
)
2381 for (arc
= block
->pred
; arc
; arc
= arc
->pred_next
)
2383 add_branch_counts (&src
->coverage
, arc
);
2386 /* Cycle detection. */
2387 for (block
= line
->u
.blocks
; block
; block
= block
->chain
)
2389 for (arc_t
*arc
= block
->pred
; arc
; arc
= arc
->pred_next
)
2390 if (!line
->has_block (arc
->src
))
2391 count
+= arc
->count
;
2392 for (arc_t
*arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2393 arc
->cs_count
= arc
->count
;
2396 /* Now, add the count of loops entirely on this line. */
2397 count
+= get_cycles_count (*line
);
2398 line
->count
= count
;
2403 src
->coverage
.lines
++;
2405 src
->coverage
.lines_executed
++;
2410 /* Output information about ARC number IX. Returns nonzero if
2411 anything is output. */
2414 output_branch_count (FILE *gcov_file
, int ix
, const arc_t
*arc
)
2416 if (arc
->is_call_non_return
)
2418 if (arc
->src
->count
)
2420 fnotice (gcov_file
, "call %2d returned %s\n", ix
,
2421 format_gcov (arc
->src
->count
- arc
->count
,
2422 arc
->src
->count
, -flag_counts
));
2425 fnotice (gcov_file
, "call %2d never executed\n", ix
);
2427 else if (!arc
->is_unconditional
)
2429 if (arc
->src
->count
)
2430 fnotice (gcov_file
, "branch %2d taken %s%s\n", ix
,
2431 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
),
2432 arc
->fall_through
? " (fallthrough)"
2433 : arc
->is_throw
? " (throw)" : "");
2435 fnotice (gcov_file
, "branch %2d never executed\n", ix
);
2437 else if (flag_unconditional
&& !arc
->dst
->is_call_return
)
2439 if (arc
->src
->count
)
2440 fnotice (gcov_file
, "unconditional %2d taken %s\n", ix
,
2441 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
));
2443 fnotice (gcov_file
, "unconditional %2d never executed\n", ix
);
2451 read_line (FILE *file
)
2453 static char *string
;
2454 static size_t string_len
;
2461 string
= XNEWVEC (char, string_len
);
2464 while ((ptr
= fgets (string
+ pos
, string_len
- pos
, file
)))
2466 size_t len
= strlen (string
+ pos
);
2468 if (string
[pos
+ len
- 1] == '\n')
2470 string
[pos
+ len
- 1] = 0;
2474 string
= XRESIZEVEC (char, string
, string_len
* 2);
2478 return pos
? string
: NULL
;
2481 /* Read in the source file one line at a time, and output that line to
2482 the gcov file preceded by its execution count and other
2486 output_lines (FILE *gcov_file
, const source_t
*src
)
2489 unsigned line_num
; /* current line number. */
2490 const line_t
*line
; /* current line info ptr. */
2491 const char *retval
= ""; /* status of source file reading. */
2492 function_t
*fn
= NULL
;
2494 fprintf (gcov_file
, "%9s:%5d:Source:%s\n", "-", 0, src
->coverage
.name
);
2495 if (!multiple_files
)
2497 fprintf (gcov_file
, "%9s:%5d:Graph:%s\n", "-", 0, bbg_file_name
);
2498 fprintf (gcov_file
, "%9s:%5d:Data:%s\n", "-", 0,
2499 no_data_file
? "-" : da_file_name
);
2500 fprintf (gcov_file
, "%9s:%5d:Runs:%u\n", "-", 0, object_runs
);
2502 fprintf (gcov_file
, "%9s:%5d:Programs:%u\n", "-", 0, program_count
);
2504 source_file
= fopen (src
->name
, "r");
2507 fnotice (stderr
, "Cannot open source file %s\n", src
->name
);
2510 else if (src
->file_time
== 0)
2511 fprintf (gcov_file
, "%9s:%5d:Source is newer than graph\n", "-", 0);
2514 fn
= src
->functions
;
2516 for (line_num
= 1, line
= &src
->lines
[line_num
];
2517 line_num
< src
->num_lines
; line_num
++, line
++)
2519 for (; fn
&& fn
->line
== line_num
; fn
= fn
->next_file_fn
)
2521 arc_t
*arc
= fn
->blocks
[EXIT_BLOCK
].pred
;
2522 gcov_type return_count
= fn
->blocks
[EXIT_BLOCK
].count
;
2523 gcov_type called_count
= fn
->blocks
[ENTRY_BLOCK
].count
;
2525 for (; arc
; arc
= arc
->pred_next
)
2527 return_count
-= arc
->count
;
2529 fprintf (gcov_file
, "function %s", flag_demangled_names
?
2530 fn
->demangled_name
: fn
->name
);
2531 fprintf (gcov_file
, " called %s",
2532 format_gcov (called_count
, 0, -1));
2533 fprintf (gcov_file
, " returned %s",
2534 format_gcov (return_count
, called_count
, 0));
2535 fprintf (gcov_file
, " blocks executed %s",
2536 format_gcov (fn
->blocks_executed
, fn
->num_blocks
- 2, 0));
2537 fprintf (gcov_file
, "\n");
2541 retval
= read_line (source_file
);
2543 /* For lines which don't exist in the .bb file, print '-' before
2544 the source line. For lines which exist but were never
2545 executed, print '#####' or '=====' before the source line.
2546 Otherwise, print the execution count before the source line.
2547 There are 16 spaces of indentation added before the source
2548 line so that tabs won't be messed up. */
2549 fprintf (gcov_file
, "%9s:%5u:%s\n",
2550 !line
->exists
? "-" : line
->count
2551 ? format_gcov (line
->count
, 0, -1)
2552 : line
->unexceptional
? "#####" : "=====", line_num
,
2553 retval
? retval
: "/*EOF*/");
2555 if (flag_all_blocks
)
2561 for (ix
= jx
= 0, block
= line
->u
.blocks
; block
;
2562 block
= block
->chain
)
2564 if (!block
->is_call_return
)
2565 fprintf (gcov_file
, "%9s:%5u-block %2d\n",
2566 !line
->exists
? "-" : block
->count
2567 ? format_gcov (block
->count
, 0, -1)
2568 : block
->exceptional
? "%%%%%" : "$$$$$",
2571 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2572 jx
+= output_branch_count (gcov_file
, jx
, arc
);
2575 else if (flag_branches
)
2580 for (ix
= 0, arc
= line
->u
.branches
; arc
; arc
= arc
->line_next
)
2581 ix
+= output_branch_count (gcov_file
, ix
, arc
);
2585 /* Handle all remaining source lines. There may be lines after the
2586 last line of code. */
2589 for (; (retval
= read_line (source_file
)); line_num
++)
2590 fprintf (gcov_file
, "%9s:%5u:%s\n", "-", line_num
, retval
);
2594 fclose (source_file
);