1 /* Gcov.c: prepend line execution counts and branch probabilities to a
3 Copyright (C) 1990-2018 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. */
34 #define INCLUDE_ALGORITHM
35 #define INCLUDE_VECTOR
36 #define INCLUDE_STRING
40 #include "coretypes.h"
43 #include "diagnostic.h"
46 #include "color-macros.h"
58 /* The gcno file is generated by -ftest-coverage option. The gcda file is
59 generated by a program compiled with -fprofile-arcs. Their formats
60 are documented in gcov-io.h. */
62 /* The functions in this file for creating and solution program flow graphs
63 are very similar to functions in the gcc source file profile.c. In
64 some places we make use of the knowledge of how profile.c works to
65 select particular algorithms here. */
67 /* The code validates that the profile information read in corresponds
68 to the code currently being compiled. Rather than checking for
69 identical files, the code below compares a checksum on the CFG
70 (based on the order of basic blocks and the arcs in the CFG). If
71 the CFG checksum in the gcda file match the CFG checksum in the
72 gcno file, the profile data will be used. */
74 /* This is the size of the buffer used to read in source file lines. */
80 /* Describes an arc between two basic blocks. */
84 /* source and destination blocks. */
85 struct block_info
*src
;
86 struct block_info
*dst
;
88 /* transition counts. */
90 /* used in cycle search, so that we do not clobber original counts. */
93 unsigned int count_valid
: 1;
94 unsigned int on_tree
: 1;
95 unsigned int fake
: 1;
96 unsigned int fall_through
: 1;
98 /* Arc to a catch handler. */
99 unsigned int is_throw
: 1;
101 /* Arc is for a function that abnormally returns. */
102 unsigned int is_call_non_return
: 1;
104 /* Arc is for catch/setjmp. */
105 unsigned int is_nonlocal_return
: 1;
107 /* Is an unconditional branch. */
108 unsigned int is_unconditional
: 1;
110 /* Loop making arc. */
111 unsigned int cycle
: 1;
113 /* Links to next arc on src and dst lists. */
114 struct arc_info
*succ_next
;
115 struct arc_info
*pred_next
;
118 /* Describes which locations (lines and files) are associated with
121 struct block_location_info
123 block_location_info (unsigned _source_file_idx
):
124 source_file_idx (_source_file_idx
)
127 unsigned source_file_idx
;
128 vector
<unsigned> lines
;
131 /* Describes a basic block. Contains lists of arcs to successor and
132 predecessor blocks. */
139 /* Chain of exit and entry arcs. */
143 /* Number of unprocessed exit and entry arcs. */
149 /* Block execution count. */
151 unsigned count_valid
: 1;
152 unsigned valid_chain
: 1;
153 unsigned invalid_chain
: 1;
154 unsigned exceptional
: 1;
156 /* Block is a call instrumenting site. */
157 unsigned is_call_site
: 1; /* Does the call. */
158 unsigned is_call_return
: 1; /* Is the return. */
160 /* Block is a landing pad for longjmp or throw. */
161 unsigned is_nonlocal_return
: 1;
163 vector
<block_location_info
> locations
;
167 /* Single line graph cycle workspace. Used for all-blocks
171 } cycle
; /* Used in all-blocks mode, after blocks are linked onto
174 /* Temporary chain for solving graph, and for chaining blocks on one
176 struct block_info
*chain
;
180 block_info::block_info (): succ (NULL
), pred (NULL
), num_succ (0), num_pred (0),
181 id (0), count (0), count_valid (0), valid_chain (0), invalid_chain (0),
182 exceptional (0), is_call_site (0), is_call_return (0), is_nonlocal_return (0),
183 locations (), chain (NULL
)
188 /* Describes a single line of source. Contains a chain of basic blocks
193 /* Default constructor. */
196 /* Return true when NEEDLE is one of basic blocks the line belongs to. */
197 bool has_block (block_info
*needle
);
199 /* Execution count. */
202 /* Branches from blocks that end on this line. */
203 vector
<arc_info
*> branches
;
205 /* blocks which start on this line. Used in all-blocks mode. */
206 vector
<block_info
*> blocks
;
209 unsigned unexceptional
: 1;
210 unsigned has_unexecuted_block
: 1;
213 line_info::line_info (): count (0), branches (), blocks (), exists (false),
214 unexceptional (0), has_unexecuted_block (0)
219 line_info::has_block (block_info
*needle
)
221 return std::find (blocks
.begin (), blocks
.end (), needle
) != blocks
.end ();
224 /* Describes a single function. Contains an array of basic blocks. */
231 /* Return true when line N belongs to the function in source file SRC_IDX.
232 The line must be defined in body of the function, can't be inlined. */
233 bool group_line_p (unsigned n
, unsigned src_idx
);
235 /* Function filter based on function_info::artificial variable. */
238 is_artificial (function_info
*fn
)
240 return fn
->artificial
;
243 /* Name of function. */
245 char *demangled_name
;
247 unsigned lineno_checksum
;
248 unsigned cfg_checksum
;
250 /* The graph contains at least one fake incoming edge. */
251 unsigned has_catch
: 1;
253 /* True when the function is artificial and does not exist
255 unsigned artificial
: 1;
257 /* True when multiple functions start at a line in a source file. */
258 unsigned is_group
: 1;
260 /* Array of basic blocks. Like in GCC, the entry block is
261 at blocks[0] and the exit block is at blocks[1]. */
262 #define ENTRY_BLOCK (0)
263 #define EXIT_BLOCK (1)
264 vector
<block_info
> blocks
;
265 unsigned blocks_executed
;
267 /* Raw arc coverage counts. */
268 vector
<gcov_type
> counts
;
270 /* First line number. */
273 /* First line column. */
274 unsigned start_column
;
276 /* Last line number. */
279 /* Index of source file where the function is defined. */
282 /* Vector of line information. */
283 vector
<line_info
> lines
;
286 struct function_info
*next
;
289 /* Function info comparer that will sort functions according to starting
292 struct function_line_start_cmp
294 inline bool operator() (const function_info
*lhs
,
295 const function_info
*rhs
)
297 return (lhs
->start_line
== rhs
->start_line
298 ? lhs
->start_column
< rhs
->start_column
299 : lhs
->start_line
< rhs
->start_line
);
303 /* Describes coverage of a file or function. */
311 int branches_executed
;
320 /* Describes a file mentioned in the block graph. Contains an array
325 /* Default constructor. */
328 vector
<function_info
*> get_functions_at_location (unsigned line_num
) const;
330 /* Index of the source_info in sources vector. */
333 /* Canonical name of source file. */
337 /* Vector of line information. */
338 vector
<line_info
> lines
;
340 coverage_info coverage
;
342 /* Maximum line count in the source file. */
343 unsigned int maximum_count
;
345 /* Functions in this source file. These are in ascending line
347 vector
<function_info
*> functions
;
350 source_info::source_info (): index (0), name (NULL
), file_time (),
351 lines (), coverage (), maximum_count (0), functions ()
355 vector
<function_info
*>
356 source_info::get_functions_at_location (unsigned line_num
) const
358 vector
<function_info
*> r
;
360 for (vector
<function_info
*>::const_iterator it
= functions
.begin ();
361 it
!= functions
.end (); it
++)
363 if ((*it
)->start_line
== line_num
&& (*it
)->src
== index
)
367 std::sort (r
.begin (), r
.end (), function_line_start_cmp ());
379 name_map (char *_name
, unsigned _src
): name (_name
), src (_src
)
383 bool operator== (const name_map
&rhs
) const
385 #if HAVE_DOS_BASED_FILE_SYSTEM
386 return strcasecmp (this->name
, rhs
.name
) == 0;
388 return strcmp (this->name
, rhs
.name
) == 0;
392 bool operator< (const name_map
&rhs
) const
394 #if HAVE_DOS_BASED_FILE_SYSTEM
395 return strcasecmp (this->name
, rhs
.name
) < 0;
397 return strcmp (this->name
, rhs
.name
) < 0;
401 const char *name
; /* Source file name */
402 unsigned src
; /* Source file */
405 /* Vector of all functions. */
406 static vector
<function_info
*> functions
;
408 /* Vector of source files. */
409 static vector
<source_info
> sources
;
411 /* Mapping of file names to sources */
412 static vector
<name_map
> names
;
414 /* Record all processed files in order to warn about
415 a file being read multiple times. */
416 static vector
<char *> processed_files
;
418 /* This holds data summary information. */
420 static unsigned object_runs
;
422 static unsigned total_lines
;
423 static unsigned total_executed
;
425 /* Modification time of graph file. */
427 static time_t bbg_file_time
;
429 /* Name of the notes (gcno) output file. The "bbg" prefix is for
430 historical reasons, when the notes file contained only the
431 basic block graph notes. */
433 static char *bbg_file_name
;
435 /* Stamp of the bbg file */
436 static unsigned bbg_stamp
;
438 /* Supports has_unexecuted_blocks functionality. */
439 static unsigned bbg_supports_has_unexecuted_blocks
;
441 /* Working directory in which a TU was compiled. */
442 static const char *bbg_cwd
;
444 /* Name and file pointer of the input file for the count data (gcda). */
446 static char *da_file_name
;
448 /* Data file is missing. */
450 static int no_data_file
;
452 /* If there is several input files, compute and display results after
453 reading all data files. This way if two or more gcda file refer to
454 the same source file (eg inline subprograms in a .h file), the
457 static int multiple_files
= 0;
459 /* Output branch probabilities. */
461 static int flag_branches
= 0;
463 /* Show unconditional branches too. */
464 static int flag_unconditional
= 0;
466 /* Output a gcov file if this is true. This is on by default, and can
467 be turned off by the -n option. */
469 static int flag_gcov_file
= 1;
471 /* Output to stdout instead to a gcov file. */
473 static int flag_use_stdout
= 0;
475 /* Output progress indication if this is true. This is off by default
476 and can be turned on by the -d option. */
478 static int flag_display_progress
= 0;
480 /* Output *.gcov file in intermediate format used by 'lcov'. */
482 static int flag_intermediate_format
= 0;
484 /* Output demangled function names. */
486 static int flag_demangled_names
= 0;
488 /* For included files, make the gcov output file name include the name
489 of the input source file. For example, if x.h is included in a.c,
490 then the output file name is a.c##x.h.gcov instead of x.h.gcov. */
492 static int flag_long_names
= 0;
494 /* For situations when a long name can potentially hit filesystem path limit,
495 let's calculate md5sum of the path and append it to a file name. */
497 static int flag_hash_filenames
= 0;
499 /* Print verbose informations. */
501 static int flag_verbose
= 0;
503 /* Print colored output. */
505 static int flag_use_colors
= 0;
507 /* Use perf-like colors to indicate hot lines. */
509 static int flag_use_hotness_colors
= 0;
511 /* Output count information for every basic block, not merely those
512 that contain line number information. */
514 static int flag_all_blocks
= 0;
516 /* Output human readable numbers. */
518 static int flag_human_readable_numbers
= 0;
520 /* Output summary info for each function. */
522 static int flag_function_summary
= 0;
524 /* Object directory file prefix. This is the directory/file where the
525 graph and data files are looked for, if nonzero. */
527 static char *object_directory
= 0;
529 /* Source directory prefix. This is removed from source pathnames
530 that match, when generating the output file name. */
532 static char *source_prefix
= 0;
533 static size_t source_length
= 0;
535 /* Only show data for sources with relative pathnames. Absolute ones
536 usually indicate a system header file, which although it may
537 contain inline functions, is usually uninteresting. */
538 static int flag_relative_only
= 0;
540 /* Preserve all pathname components. Needed when object files and
541 source files are in subdirectories. '/' is mangled as '#', '.' is
542 elided and '..' mangled to '^'. */
544 static int flag_preserve_paths
= 0;
546 /* Output the number of times a branch was taken as opposed to the percentage
547 of times it was taken. */
549 static int flag_counts
= 0;
551 /* Forward declarations. */
552 static int process_args (int, char **);
553 static void print_usage (int) ATTRIBUTE_NORETURN
;
554 static void print_version (void) ATTRIBUTE_NORETURN
;
555 static void process_file (const char *);
556 static void process_all_functions (void);
557 static void generate_results (const char *);
558 static void create_file_names (const char *);
559 static char *canonicalize_name (const char *);
560 static unsigned find_source (const char *);
561 static void read_graph_file (void);
562 static int read_count_file (void);
563 static void solve_flow_graph (function_info
*);
564 static void find_exception_blocks (function_info
*);
565 static void add_branch_counts (coverage_info
*, const arc_info
*);
566 static void add_line_counts (coverage_info
*, function_info
*);
567 static void executed_summary (unsigned, unsigned);
568 static void function_summary (const coverage_info
*, const char *);
569 static const char *format_gcov (gcov_type
, gcov_type
, int);
570 static void accumulate_line_counts (source_info
*);
571 static void output_gcov_file (const char *, source_info
*);
572 static int output_branch_count (FILE *, int, const arc_info
*);
573 static void output_lines (FILE *, const source_info
*);
574 static char *make_gcov_file_name (const char *, const char *);
575 static char *mangle_name (const char *, char *);
576 static void release_structures (void);
577 extern int main (int, char **);
579 function_info::function_info (): name (NULL
), demangled_name (NULL
),
580 ident (0), lineno_checksum (0), cfg_checksum (0), has_catch (0),
581 artificial (0), is_group (0),
582 blocks (), blocks_executed (0), counts (),
583 start_line (0), start_column (), end_line (0), src (0), lines (), next (NULL
)
587 function_info::~function_info ()
589 for (int i
= blocks
.size () - 1; i
>= 0; i
--)
591 arc_info
*arc
, *arc_n
;
593 for (arc
= blocks
[i
].succ
; arc
; arc
= arc_n
)
595 arc_n
= arc
->succ_next
;
599 if (flag_demangled_names
&& demangled_name
!= name
)
600 free (demangled_name
);
604 bool function_info::group_line_p (unsigned n
, unsigned src_idx
)
606 return is_group
&& src
== src_idx
&& start_line
<= n
&& n
<= end_line
;
610 There are a bajillion algorithms that do this. Boost's function is named
611 hawick_cycles, so I used the algorithm by K. A. Hawick and H. A. James in
612 "Enumerating Circuits and Loops in Graphs with Self-Arcs and Multiple-Arcs"
613 (url at <http://complexity.massey.ac.nz/cstn/013/cstn-013.pdf>).
615 The basic algorithm is simple: effectively, we're finding all simple paths
616 in a subgraph (that shrinks every iteration). Duplicates are filtered by
617 "blocking" a path when a node is added to the path (this also prevents non-
618 simple paths)--the node is unblocked only when it participates in a cycle.
621 typedef vector
<arc_info
*> arc_vector_t
;
622 typedef vector
<const block_info
*> block_vector_t
;
624 /* Enum with types of loop in CFG. */
633 /* Loop_type operator that merges two values: A and B. */
635 inline loop_type
& operator |= (loop_type
& a
, loop_type b
)
637 return a
= static_cast<loop_type
> (a
| b
);
640 /* Handle cycle identified by EDGES, where the function finds minimum cs_count
641 and subtract the value from all counts. The subtracted value is added
642 to COUNT. Returns type of loop. */
645 handle_cycle (const arc_vector_t
&edges
, int64_t &count
)
647 /* Find the minimum edge of the cycle, and reduce all nodes in the cycle by
649 int64_t cycle_count
= INTTYPE_MAXIMUM (int64_t);
650 for (unsigned i
= 0; i
< edges
.size (); i
++)
652 int64_t ecount
= edges
[i
]->cs_count
;
653 if (cycle_count
> ecount
)
654 cycle_count
= ecount
;
656 count
+= cycle_count
;
657 for (unsigned i
= 0; i
< edges
.size (); i
++)
658 edges
[i
]->cs_count
-= cycle_count
;
660 return cycle_count
< 0 ? NEGATIVE_LOOP
: LOOP
;
663 /* Unblock a block U from BLOCKED. Apart from that, iterate all blocks
664 blocked by U in BLOCK_LISTS. */
667 unblock (const block_info
*u
, block_vector_t
&blocked
,
668 vector
<block_vector_t
> &block_lists
)
670 block_vector_t::iterator it
= find (blocked
.begin (), blocked
.end (), u
);
671 if (it
== blocked
.end ())
674 unsigned index
= it
- blocked
.begin ();
677 block_vector_t
to_unblock (block_lists
[index
]);
679 block_lists
.erase (block_lists
.begin () + index
);
681 for (block_vector_t::iterator it
= to_unblock
.begin ();
682 it
!= to_unblock
.end (); it
++)
683 unblock (*it
, blocked
, block_lists
);
686 /* Find circuit going to block V, PATH is provisional seen cycle.
687 BLOCKED is vector of blocked vertices, BLOCK_LISTS contains vertices
688 blocked by a block. COUNT is accumulated count of the current LINE.
689 Returns what type of loop it contains. */
692 circuit (block_info
*v
, arc_vector_t
&path
, block_info
*start
,
693 block_vector_t
&blocked
, vector
<block_vector_t
> &block_lists
,
694 line_info
&linfo
, int64_t &count
)
696 loop_type result
= NO_LOOP
;
698 /* Add v to the block list. */
699 gcc_assert (find (blocked
.begin (), blocked
.end (), v
) == blocked
.end ());
700 blocked
.push_back (v
);
701 block_lists
.push_back (block_vector_t ());
703 for (arc_info
*arc
= v
->succ
; arc
; arc
= arc
->succ_next
)
705 block_info
*w
= arc
->dst
;
706 if (w
< start
|| !linfo
.has_block (w
))
709 path
.push_back (arc
);
711 /* Cycle has been found. */
712 result
|= handle_cycle (path
, count
);
713 else if (find (blocked
.begin (), blocked
.end (), w
) == blocked
.end ())
714 result
|= circuit (w
, path
, start
, blocked
, block_lists
, linfo
, count
);
719 if (result
!= NO_LOOP
)
720 unblock (v
, blocked
, block_lists
);
722 for (arc_info
*arc
= v
->succ
; arc
; arc
= arc
->succ_next
)
724 block_info
*w
= arc
->dst
;
725 if (w
< start
|| !linfo
.has_block (w
))
729 = find (blocked
.begin (), blocked
.end (), w
) - blocked
.begin ();
730 gcc_assert (index
< blocked
.size ());
731 block_vector_t
&list
= block_lists
[index
];
732 if (find (list
.begin (), list
.end (), v
) == list
.end ())
739 /* Find cycles for a LINFO. If HANDLE_NEGATIVE_CYCLES is set and the line
740 contains a negative loop, then perform the same function once again. */
743 get_cycles_count (line_info
&linfo
, bool handle_negative_cycles
= true)
745 /* Note that this algorithm works even if blocks aren't in sorted order.
746 Each iteration of the circuit detection is completely independent
747 (except for reducing counts, but that shouldn't matter anyways).
748 Therefore, operating on a permuted order (i.e., non-sorted) only
749 has the effect of permuting the output cycles. */
751 loop_type result
= NO_LOOP
;
753 for (vector
<block_info
*>::iterator it
= linfo
.blocks
.begin ();
754 it
!= linfo
.blocks
.end (); it
++)
757 block_vector_t blocked
;
758 vector
<block_vector_t
> block_lists
;
759 result
|= circuit (*it
, path
, *it
, blocked
, block_lists
, linfo
,
763 /* If we have a negative cycle, repeat the find_cycles routine. */
764 if (result
== NEGATIVE_LOOP
&& handle_negative_cycles
)
765 count
+= get_cycles_count (linfo
, false);
771 main (int argc
, char **argv
)
777 p
= argv
[0] + strlen (argv
[0]);
778 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
782 xmalloc_set_program_name (progname
);
784 /* Unlock the stdio streams. */
785 unlock_std_streams ();
789 diagnostic_initialize (global_dc
, 0);
791 /* Handle response files. */
792 expandargv (&argc
, &argv
);
794 argno
= process_args (argc
, argv
);
798 if (argc
- argno
> 1)
803 for (; argno
!= argc
; argno
++)
805 if (flag_display_progress
)
806 printf ("Processing file %d out of %d\n", argno
- first_arg
+ 1,
808 process_file (argv
[argno
]);
810 if (flag_intermediate_format
|| argno
== argc
- 1)
812 process_all_functions ();
813 generate_results (argv
[argno
]);
814 release_structures ();
821 /* Print a usage message and exit. If ERROR_P is nonzero, this is an error,
822 otherwise the output of --help. */
825 print_usage (int error_p
)
827 FILE *file
= error_p
? stderr
: stdout
;
828 int status
= error_p
? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
;
830 fnotice (file
, "Usage: gcov [OPTION...] SOURCE|OBJ...\n\n");
831 fnotice (file
, "Print code coverage information.\n\n");
832 fnotice (file
, " -a, --all-blocks Show information for every basic block\n");
833 fnotice (file
, " -b, --branch-probabilities Include branch probabilities in output\n");
834 fnotice (file
, " -c, --branch-counts Output counts of branches taken\n\
835 rather than percentages\n");
836 fnotice (file
, " -d, --display-progress Display progress information\n");
837 fnotice (file
, " -f, --function-summaries Output summaries for each function\n");
838 fnotice (file
, " -h, --help Print this help, then exit\n");
839 fnotice (file
, " -i, --intermediate-format Output .gcov file in intermediate text format\n");
840 fnotice (file
, " -j, --human-readable Output human readable numbers\n");
841 fnotice (file
, " -k, --use-colors Emit colored output\n");
842 fnotice (file
, " -l, --long-file-names Use long output file names for included\n\
844 fnotice (file
, " -m, --demangled-names Output demangled function names\n");
845 fnotice (file
, " -n, --no-output Do not create an output file\n");
846 fnotice (file
, " -o, --object-directory DIR|FILE Search for object files in DIR or called FILE\n");
847 fnotice (file
, " -p, --preserve-paths Preserve all pathname components\n");
848 fnotice (file
, " -q, --use-hotness-colors Emit perf-like colored output for hot lines\n");
849 fnotice (file
, " -r, --relative-only Only show data for relative sources\n");
850 fnotice (file
, " -s, --source-prefix DIR Source prefix to elide\n");
851 fnotice (file
, " -t, --stdout Output to stdout instead of a file\n");
852 fnotice (file
, " -u, --unconditional-branches Show unconditional branch counts too\n");
853 fnotice (file
, " -v, --version Print version number, then exit\n");
854 fnotice (file
, " -w, --verbose Print verbose informations\n");
855 fnotice (file
, " -x, --hash-filenames Hash long pathnames\n");
856 fnotice (file
, "\nFor bug reporting instructions, please see:\n%s.\n",
861 /* Print version information and exit. */
866 fnotice (stdout
, "gcov %s%s\n", pkgversion_string
, version_string
);
867 fprintf (stdout
, "Copyright %s 2018 Free Software Foundation, Inc.\n",
870 _("This is free software; see the source for copying conditions.\n"
871 "There is NO warranty; not even for MERCHANTABILITY or \n"
872 "FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
873 exit (SUCCESS_EXIT_CODE
);
876 static const struct option options
[] =
878 { "help", no_argument
, NULL
, 'h' },
879 { "version", no_argument
, NULL
, 'v' },
880 { "verbose", no_argument
, NULL
, 'w' },
881 { "all-blocks", no_argument
, NULL
, 'a' },
882 { "branch-probabilities", no_argument
, NULL
, 'b' },
883 { "branch-counts", no_argument
, NULL
, 'c' },
884 { "intermediate-format", no_argument
, NULL
, 'i' },
885 { "human-readable", no_argument
, NULL
, 'j' },
886 { "no-output", no_argument
, NULL
, 'n' },
887 { "long-file-names", no_argument
, NULL
, 'l' },
888 { "function-summaries", no_argument
, NULL
, 'f' },
889 { "demangled-names", no_argument
, NULL
, 'm' },
890 { "preserve-paths", no_argument
, NULL
, 'p' },
891 { "relative-only", no_argument
, NULL
, 'r' },
892 { "object-directory", required_argument
, NULL
, 'o' },
893 { "object-file", required_argument
, NULL
, 'o' },
894 { "source-prefix", required_argument
, NULL
, 's' },
895 { "stdout", no_argument
, NULL
, 't' },
896 { "unconditional-branches", no_argument
, NULL
, 'u' },
897 { "display-progress", no_argument
, NULL
, 'd' },
898 { "hash-filenames", no_argument
, NULL
, 'x' },
899 { "use-colors", no_argument
, NULL
, 'k' },
900 { "use-hotness-colors", no_argument
, NULL
, 'q' },
904 /* Process args, return index to first non-arg. */
907 process_args (int argc
, char **argv
)
911 const char *opts
= "abcdfhijklmno:pqrs:tuvwx";
912 while ((opt
= getopt_long (argc
, argv
, opts
, options
, NULL
)) != -1)
926 flag_function_summary
= 1;
930 /* print_usage will exit. */
935 flag_human_readable_numbers
= 1;
941 flag_use_hotness_colors
= 1;
944 flag_demangled_names
= 1;
950 object_directory
= optarg
;
953 source_prefix
= optarg
;
954 source_length
= strlen (source_prefix
);
957 flag_relative_only
= 1;
960 flag_preserve_paths
= 1;
963 flag_unconditional
= 1;
966 flag_intermediate_format
= 1;
970 flag_display_progress
= 1;
973 flag_hash_filenames
= 1;
983 /* print_version will exit. */
986 /* print_usage will exit. */
993 /* Output intermediate LINE sitting on LINE_NUM to output file F. */
996 output_intermediate_line (FILE *f
, line_info
*line
, unsigned line_num
)
1001 fprintf (f
, "lcount:%u,%s,%d\n", line_num
,
1002 format_gcov (line
->count
, 0, -1),
1003 line
->has_unexecuted_block
);
1005 vector
<arc_info
*>::const_iterator it
;
1007 for (it
= line
->branches
.begin (); it
!= line
->branches
.end ();
1010 if (!(*it
)->is_unconditional
&& !(*it
)->is_call_non_return
)
1012 const char *branch_type
;
1013 /* branch:<line_num>,<branch_coverage_infoype>
1014 branch_coverage_infoype
1015 : notexec (Branch not executed)
1016 : taken (Branch executed and taken)
1017 : nottaken (Branch executed, but not taken)
1019 if ((*it
)->src
->count
)
1021 = ((*it
)->count
> 0) ? "taken" : "nottaken";
1023 branch_type
= "notexec";
1024 fprintf (f
, "branch:%d,%s\n", line_num
, branch_type
);
1029 /* Get the name of the gcov file. The return value must be free'd.
1031 It appends the '.gcov' extension to the *basename* of the file.
1032 The resulting file name will be in PWD.
1035 input: foo.da, output: foo.da.gcov
1036 input: a/b/foo.cc, output: foo.cc.gcov */
1039 get_gcov_intermediate_filename (const char *file_name
)
1041 const char *gcov
= ".gcov";
1045 /* Find the 'basename'. */
1046 cptr
= lbasename (file_name
);
1048 result
= XNEWVEC (char, strlen (cptr
) + strlen (gcov
) + 1);
1049 sprintf (result
, "%s%s", cptr
, gcov
);
1054 /* Output the result in intermediate format used by 'lcov'.
1056 The intermediate format contains a single file named 'foo.cc.gcov',
1057 with no source code included.
1059 The default gcov outputs multiple files: 'foo.cc.gcov',
1060 'iostream.gcov', 'ios_base.h.gcov', etc. with source code
1061 included. Instead the intermediate format here outputs only a single
1062 file 'foo.cc.gcov' similar to the above example. */
1065 output_intermediate_file (FILE *gcov_file
, source_info
*src
)
1067 fprintf (gcov_file
, "version:%s\n", version_string
);
1068 fprintf (gcov_file
, "file:%s\n", src
->name
); /* source file name */
1069 fprintf (gcov_file
, "cwd:%s\n", bbg_cwd
);
1071 std::sort (src
->functions
.begin (), src
->functions
.end (),
1072 function_line_start_cmp ());
1073 for (vector
<function_info
*>::iterator it
= src
->functions
.begin ();
1074 it
!= src
->functions
.end (); it
++)
1076 /* function:<name>,<line_number>,<execution_count> */
1077 fprintf (gcov_file
, "function:%d,%d,%s,%s\n", (*it
)->start_line
,
1078 (*it
)->end_line
, format_gcov ((*it
)->blocks
[0].count
, 0, -1),
1079 flag_demangled_names
? (*it
)->demangled_name
: (*it
)->name
);
1082 for (unsigned line_num
= 1; line_num
<= src
->lines
.size (); line_num
++)
1084 vector
<function_info
*> fns
= src
->get_functions_at_location (line_num
);
1086 /* Print first group functions that begin on the line. */
1087 for (vector
<function_info
*>::iterator it2
= fns
.begin ();
1088 it2
!= fns
.end (); it2
++)
1090 vector
<line_info
> &lines
= (*it2
)->lines
;
1091 for (unsigned i
= 0; i
< lines
.size (); i
++)
1093 line_info
*line
= &lines
[i
];
1094 output_intermediate_line (gcov_file
, line
, line_num
+ i
);
1098 /* Follow with lines associated with the source file. */
1099 if (line_num
< src
->lines
.size ())
1100 output_intermediate_line (gcov_file
, &src
->lines
[line_num
], line_num
);
1104 /* Function start pair. */
1105 struct function_start
1107 unsigned source_file_idx
;
1108 unsigned start_line
;
1111 /* Traits class for function start hash maps below. */
1113 struct function_start_pair_hash
: typed_noop_remove
<function_start
>
1115 typedef function_start value_type
;
1116 typedef function_start compare_type
;
1119 hash (const function_start
&ref
)
1121 inchash::hash
hstate (0);
1122 hstate
.add_int (ref
.source_file_idx
);
1123 hstate
.add_int (ref
.start_line
);
1124 return hstate
.end ();
1128 equal (const function_start
&ref1
, const function_start
&ref2
)
1130 return (ref1
.source_file_idx
== ref2
.source_file_idx
1131 && ref1
.start_line
== ref2
.start_line
);
1135 mark_deleted (function_start
&ref
)
1137 ref
.start_line
= ~1U;
1141 mark_empty (function_start
&ref
)
1143 ref
.start_line
= ~2U;
1147 is_deleted (const function_start
&ref
)
1149 return ref
.start_line
== ~1U;
1153 is_empty (const function_start
&ref
)
1155 return ref
.start_line
== ~2U;
1159 /* Process a single input file. */
1162 process_file (const char *file_name
)
1164 create_file_names (file_name
);
1166 for (unsigned i
= 0; i
< processed_files
.size (); i
++)
1167 if (strcmp (da_file_name
, processed_files
[i
]) == 0)
1169 fnotice (stderr
, "'%s' file is already processed\n",
1174 processed_files
.push_back (xstrdup (da_file_name
));
1180 /* Process all functions in all files. */
1183 process_all_functions (void)
1185 hash_map
<function_start_pair_hash
, function_info
*> fn_map
;
1187 /* Identify group functions. */
1188 for (vector
<function_info
*>::iterator it
= functions
.begin ();
1189 it
!= functions
.end (); it
++)
1190 if (!(*it
)->artificial
)
1192 function_start needle
;
1193 needle
.source_file_idx
= (*it
)->src
;
1194 needle
.start_line
= (*it
)->start_line
;
1196 function_info
**slot
= fn_map
.get (needle
);
1199 (*slot
)->is_group
= 1;
1200 (*it
)->is_group
= 1;
1203 fn_map
.put (needle
, *it
);
1206 /* Remove all artificial function. */
1207 functions
.erase (remove_if (functions
.begin (), functions
.end (),
1208 function_info::is_artificial
), functions
.end ());
1210 for (vector
<function_info
*>::iterator it
= functions
.begin ();
1211 it
!= functions
.end (); it
++)
1213 function_info
*fn
= *it
;
1214 unsigned src
= fn
->src
;
1216 if (!fn
->counts
.empty () || no_data_file
)
1218 source_info
*s
= &sources
[src
];
1219 s
->functions
.push_back (fn
);
1221 /* Mark last line in files touched by function. */
1222 for (unsigned block_no
= 0; block_no
!= fn
->blocks
.size ();
1225 block_info
*block
= &fn
->blocks
[block_no
];
1226 for (unsigned i
= 0; i
< block
->locations
.size (); i
++)
1228 /* Sort lines of locations. */
1229 sort (block
->locations
[i
].lines
.begin (),
1230 block
->locations
[i
].lines
.end ());
1232 if (!block
->locations
[i
].lines
.empty ())
1234 s
= &sources
[block
->locations
[i
].source_file_idx
];
1236 = block
->locations
[i
].lines
.back ();
1238 /* Record new lines for the function. */
1239 if (last_line
>= s
->lines
.size ())
1241 s
= &sources
[block
->locations
[i
].source_file_idx
];
1243 = block
->locations
[i
].lines
.back ();
1245 /* Record new lines for the function. */
1246 if (last_line
>= s
->lines
.size ())
1248 /* Record new lines for a source file. */
1249 s
->lines
.resize (last_line
+ 1);
1256 /* Allocate lines for group function, following start_line
1257 and end_line information of the function. */
1259 fn
->lines
.resize (fn
->end_line
- fn
->start_line
+ 1);
1261 solve_flow_graph (fn
);
1263 find_exception_blocks (fn
);
1267 /* The function was not in the executable -- some other
1268 instance must have been selected. */
1274 output_gcov_file (const char *file_name
, source_info
*src
)
1276 char *gcov_file_name
= make_gcov_file_name (file_name
, src
->coverage
.name
);
1278 if (src
->coverage
.lines
)
1280 FILE *gcov_file
= fopen (gcov_file_name
, "w");
1283 fnotice (stdout
, "Creating '%s'\n", gcov_file_name
);
1284 output_lines (gcov_file
, src
);
1285 if (ferror (gcov_file
))
1286 fnotice (stderr
, "Error writing output file '%s'\n",
1291 fnotice (stderr
, "Could not open output file '%s'\n", gcov_file_name
);
1295 unlink (gcov_file_name
);
1296 fnotice (stdout
, "Removing '%s'\n", gcov_file_name
);
1298 free (gcov_file_name
);
1302 generate_results (const char *file_name
)
1304 FILE *gcov_intermediate_file
= NULL
;
1305 char *gcov_intermediate_filename
= NULL
;
1307 for (vector
<function_info
*>::iterator it
= functions
.begin ();
1308 it
!= functions
.end (); it
++)
1310 function_info
*fn
= *it
;
1311 coverage_info coverage
;
1313 memset (&coverage
, 0, sizeof (coverage
));
1314 coverage
.name
= flag_demangled_names
? fn
->demangled_name
: fn
->name
;
1315 add_line_counts (flag_function_summary
? &coverage
: NULL
, fn
);
1316 if (flag_function_summary
)
1318 function_summary (&coverage
, "Function");
1319 fnotice (stdout
, "\n");
1327 needle
.name
= file_name
;
1328 vector
<name_map
>::iterator it
= std::find (names
.begin (), names
.end (),
1330 if (it
!= names
.end ())
1331 file_name
= sources
[it
->src
].coverage
.name
;
1333 file_name
= canonicalize_name (file_name
);
1336 if (flag_gcov_file
&& flag_intermediate_format
&& !flag_use_stdout
)
1338 /* Open the intermediate file. */
1339 gcov_intermediate_filename
= get_gcov_intermediate_filename (file_name
);
1340 gcov_intermediate_file
= fopen (gcov_intermediate_filename
, "w");
1341 if (!gcov_intermediate_file
)
1343 fnotice (stderr
, "Cannot open intermediate output file %s\n",
1344 gcov_intermediate_filename
);
1349 for (vector
<source_info
>::iterator it
= sources
.begin ();
1350 it
!= sources
.end (); it
++)
1352 source_info
*src
= &(*it
);
1353 if (flag_relative_only
)
1355 /* Ignore this source, if it is an absolute path (after
1356 source prefix removal). */
1357 char first
= src
->coverage
.name
[0];
1359 #if HAVE_DOS_BASED_FILE_SYSTEM
1360 if (first
&& src
->coverage
.name
[1] == ':')
1361 first
= src
->coverage
.name
[2];
1363 if (IS_DIR_SEPARATOR (first
))
1367 accumulate_line_counts (src
);
1369 if (!flag_use_stdout
)
1370 function_summary (&src
->coverage
, "File");
1371 total_lines
+= src
->coverage
.lines
;
1372 total_executed
+= src
->coverage
.lines_executed
;
1375 if (flag_intermediate_format
)
1376 /* Output the intermediate format without requiring source
1377 files. This outputs a section to a *single* file. */
1378 output_intermediate_file ((flag_use_stdout
1379 ? stdout
: gcov_intermediate_file
), src
);
1382 if (flag_use_stdout
)
1384 if (src
->coverage
.lines
)
1385 output_lines (stdout
, src
);
1389 output_gcov_file (file_name
, src
);
1390 fnotice (stdout
, "\n");
1396 if (flag_gcov_file
&& flag_intermediate_format
&& !flag_use_stdout
)
1398 /* Now we've finished writing the intermediate file. */
1399 fclose (gcov_intermediate_file
);
1400 XDELETEVEC (gcov_intermediate_filename
);
1404 executed_summary (total_lines
, total_executed
);
1407 /* Release all memory used. */
1410 release_structures (void)
1412 for (vector
<function_info
*>::iterator it
= functions
.begin ();
1413 it
!= functions
.end (); it
++)
1418 functions
.resize (0);
1421 /* Generate the names of the graph and data files. If OBJECT_DIRECTORY
1422 is not specified, these are named from FILE_NAME sans extension. If
1423 OBJECT_DIRECTORY is specified and is a directory, the files are in that
1424 directory, but named from the basename of the FILE_NAME, sans extension.
1425 Otherwise OBJECT_DIRECTORY is taken to be the name of the object *file*
1426 and the data files are named from that. */
1429 create_file_names (const char *file_name
)
1433 int length
= strlen (file_name
);
1436 /* Free previous file names. */
1437 free (bbg_file_name
);
1438 free (da_file_name
);
1439 da_file_name
= bbg_file_name
= NULL
;
1443 if (object_directory
&& object_directory
[0])
1447 length
+= strlen (object_directory
) + 2;
1448 name
= XNEWVEC (char, length
);
1451 base
= !stat (object_directory
, &status
) && S_ISDIR (status
.st_mode
);
1452 strcat (name
, object_directory
);
1453 if (base
&& (!IS_DIR_SEPARATOR (name
[strlen (name
) - 1])))
1458 name
= XNEWVEC (char, length
+ 1);
1459 strcpy (name
, file_name
);
1465 /* Append source file name. */
1466 const char *cptr
= lbasename (file_name
);
1467 strcat (name
, cptr
? cptr
: file_name
);
1470 /* Remove the extension. */
1471 cptr
= strrchr (CONST_CAST (char *, lbasename (name
)), '.');
1475 length
= strlen (name
);
1477 bbg_file_name
= XNEWVEC (char, length
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
1478 strcpy (bbg_file_name
, name
);
1479 strcpy (bbg_file_name
+ length
, GCOV_NOTE_SUFFIX
);
1481 da_file_name
= XNEWVEC (char, length
+ strlen (GCOV_DATA_SUFFIX
) + 1);
1482 strcpy (da_file_name
, name
);
1483 strcpy (da_file_name
+ length
, GCOV_DATA_SUFFIX
);
1489 /* Find or create a source file structure for FILE_NAME. Copies
1490 FILE_NAME on creation */
1493 find_source (const char *file_name
)
1500 file_name
= "<unknown>";
1503 needle
.name
= file_name
;
1505 vector
<name_map
>::iterator it
= std::find (names
.begin (), names
.end (),
1507 if (it
!= names
.end ())
1513 /* Not found, try the canonical name. */
1514 canon
= canonicalize_name (file_name
);
1515 needle
.name
= canon
;
1516 it
= std::find (names
.begin (), names
.end (), needle
);
1517 if (it
== names
.end ())
1519 /* Not found with canonical name, create a new source. */
1522 idx
= sources
.size ();
1523 needle
= name_map (canon
, idx
);
1524 names
.push_back (needle
);
1526 sources
.push_back (source_info ());
1527 src
= &sources
.back ();
1529 src
->coverage
.name
= src
->name
;
1532 #if HAVE_DOS_BASED_FILE_SYSTEM
1533 /* You lose if separators don't match exactly in the
1535 && !strncasecmp (source_prefix
, src
->coverage
.name
, source_length
)
1537 && !strncmp (source_prefix
, src
->coverage
.name
, source_length
)
1539 && IS_DIR_SEPARATOR (src
->coverage
.name
[source_length
]))
1540 src
->coverage
.name
+= source_length
+ 1;
1541 if (!stat (src
->name
, &status
))
1542 src
->file_time
= status
.st_mtime
;
1547 needle
.name
= file_name
;
1548 if (std::find (names
.begin (), names
.end (), needle
) == names
.end ())
1550 /* Append the non-canonical name. */
1551 names
.push_back (name_map (xstrdup (file_name
), idx
));
1554 /* Resort the name map. */
1555 std::sort (names
.begin (), names
.end ());
1558 if (sources
[idx
].file_time
> bbg_file_time
)
1560 static int info_emitted
;
1562 fnotice (stderr
, "%s:source file is newer than notes file '%s'\n",
1563 file_name
, bbg_file_name
);
1567 "(the message is displayed only once per source file)\n");
1570 sources
[idx
].file_time
= 0;
1576 /* Read the notes file. Save functions to FUNCTIONS global vector. */
1579 read_graph_file (void)
1582 unsigned current_tag
= 0;
1585 if (!gcov_open (bbg_file_name
, 1))
1587 fnotice (stderr
, "%s:cannot open notes file\n", bbg_file_name
);
1590 bbg_file_time
= gcov_time ();
1591 if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC
))
1593 fnotice (stderr
, "%s:not a gcov notes file\n", bbg_file_name
);
1598 version
= gcov_read_unsigned ();
1599 if (version
!= GCOV_VERSION
)
1603 GCOV_UNSIGNED2STRING (v
, version
);
1604 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1606 fnotice (stderr
, "%s:version '%.4s', prefer '%.4s'\n",
1607 bbg_file_name
, v
, e
);
1609 bbg_stamp
= gcov_read_unsigned ();
1610 bbg_cwd
= xstrdup (gcov_read_string ());
1611 bbg_supports_has_unexecuted_blocks
= gcov_read_unsigned ();
1613 function_info
*fn
= NULL
;
1614 while ((tag
= gcov_read_unsigned ()))
1616 unsigned length
= gcov_read_unsigned ();
1617 gcov_position_t base
= gcov_position ();
1619 if (tag
== GCOV_TAG_FUNCTION
)
1621 char *function_name
;
1623 unsigned lineno_checksum
, cfg_checksum
;
1625 ident
= gcov_read_unsigned ();
1626 lineno_checksum
= gcov_read_unsigned ();
1627 cfg_checksum
= gcov_read_unsigned ();
1628 function_name
= xstrdup (gcov_read_string ());
1629 unsigned artificial
= gcov_read_unsigned ();
1630 unsigned src_idx
= find_source (gcov_read_string ());
1631 unsigned start_line
= gcov_read_unsigned ();
1632 unsigned start_column
= gcov_read_unsigned ();
1633 unsigned end_line
= gcov_read_unsigned ();
1635 fn
= new function_info ();
1636 functions
.push_back (fn
);
1637 fn
->name
= function_name
;
1638 if (flag_demangled_names
)
1640 fn
->demangled_name
= cplus_demangle (fn
->name
, DMGL_PARAMS
);
1641 if (!fn
->demangled_name
)
1642 fn
->demangled_name
= fn
->name
;
1645 fn
->lineno_checksum
= lineno_checksum
;
1646 fn
->cfg_checksum
= cfg_checksum
;
1648 fn
->start_line
= start_line
;
1649 fn
->start_column
= start_column
;
1650 fn
->end_line
= end_line
;
1651 fn
->artificial
= artificial
;
1655 else if (fn
&& tag
== GCOV_TAG_BLOCKS
)
1657 if (!fn
->blocks
.empty ())
1658 fnotice (stderr
, "%s:already seen blocks for '%s'\n",
1659 bbg_file_name
, fn
->name
);
1661 fn
->blocks
.resize (gcov_read_unsigned ());
1663 else if (fn
&& tag
== GCOV_TAG_ARCS
)
1665 unsigned src
= gcov_read_unsigned ();
1666 fn
->blocks
[src
].id
= src
;
1667 unsigned num_dests
= GCOV_TAG_ARCS_NUM (length
);
1668 block_info
*src_blk
= &fn
->blocks
[src
];
1669 unsigned mark_catches
= 0;
1670 struct arc_info
*arc
;
1672 if (src
>= fn
->blocks
.size () || fn
->blocks
[src
].succ
)
1677 unsigned dest
= gcov_read_unsigned ();
1678 unsigned flags
= gcov_read_unsigned ();
1680 if (dest
>= fn
->blocks
.size ())
1682 arc
= XCNEW (arc_info
);
1684 arc
->dst
= &fn
->blocks
[dest
];
1688 arc
->count_valid
= 0;
1689 arc
->on_tree
= !!(flags
& GCOV_ARC_ON_TREE
);
1690 arc
->fake
= !!(flags
& GCOV_ARC_FAKE
);
1691 arc
->fall_through
= !!(flags
& GCOV_ARC_FALLTHROUGH
);
1693 arc
->succ_next
= src_blk
->succ
;
1694 src_blk
->succ
= arc
;
1695 src_blk
->num_succ
++;
1697 arc
->pred_next
= fn
->blocks
[dest
].pred
;
1698 fn
->blocks
[dest
].pred
= arc
;
1699 fn
->blocks
[dest
].num_pred
++;
1705 /* Exceptional exit from this function, the
1706 source block must be a call. */
1707 fn
->blocks
[src
].is_call_site
= 1;
1708 arc
->is_call_non_return
= 1;
1713 /* Non-local return from a callee of this
1714 function. The destination block is a setjmp. */
1715 arc
->is_nonlocal_return
= 1;
1716 fn
->blocks
[dest
].is_nonlocal_return
= 1;
1721 fn
->counts
.push_back (0);
1726 /* We have a fake exit from this block. The other
1727 non-fall through exits must be to catch handlers.
1728 Mark them as catch arcs. */
1730 for (arc
= src_blk
->succ
; arc
; arc
= arc
->succ_next
)
1731 if (!arc
->fake
&& !arc
->fall_through
)
1738 else if (fn
&& tag
== GCOV_TAG_LINES
)
1740 unsigned blockno
= gcov_read_unsigned ();
1741 block_info
*block
= &fn
->blocks
[blockno
];
1743 if (blockno
>= fn
->blocks
.size ())
1748 unsigned lineno
= gcov_read_unsigned ();
1751 block
->locations
.back ().lines
.push_back (lineno
);
1754 const char *file_name
= gcov_read_string ();
1758 block
->locations
.push_back (block_location_info
1759 (find_source (file_name
)));
1763 else if (current_tag
&& !GCOV_TAG_IS_SUBTAG (current_tag
, tag
))
1768 gcov_sync (base
, length
);
1769 if (gcov_is_error ())
1772 fnotice (stderr
, "%s:corrupted\n", bbg_file_name
);
1778 if (functions
.empty ())
1779 fnotice (stderr
, "%s:no functions found\n", bbg_file_name
);
1782 /* Reads profiles from the count file and attach to each
1783 function. Return nonzero if fatal error. */
1786 read_count_file (void)
1791 function_info
*fn
= NULL
;
1794 if (!gcov_open (da_file_name
, 1))
1796 fnotice (stderr
, "%s:cannot open data file, assuming not executed\n",
1801 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
1803 fnotice (stderr
, "%s:not a gcov data file\n", da_file_name
);
1808 version
= gcov_read_unsigned ();
1809 if (version
!= GCOV_VERSION
)
1813 GCOV_UNSIGNED2STRING (v
, version
);
1814 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1816 fnotice (stderr
, "%s:version '%.4s', prefer version '%.4s'\n",
1817 da_file_name
, v
, e
);
1819 tag
= gcov_read_unsigned ();
1820 if (tag
!= bbg_stamp
)
1822 fnotice (stderr
, "%s:stamp mismatch with notes file\n", da_file_name
);
1826 while ((tag
= gcov_read_unsigned ()))
1828 unsigned length
= gcov_read_unsigned ();
1829 unsigned long base
= gcov_position ();
1831 if (tag
== GCOV_TAG_OBJECT_SUMMARY
)
1833 struct gcov_summary summary
;
1834 gcov_read_summary (&summary
);
1835 object_runs
= summary
.runs
;
1837 else if (tag
== GCOV_TAG_FUNCTION
&& !length
)
1839 else if (tag
== GCOV_TAG_FUNCTION
&& length
== GCOV_TAG_FUNCTION_LENGTH
)
1843 /* Try to find the function in the list. To speed up the
1844 search, first start from the last function found. */
1845 ident
= gcov_read_unsigned ();
1848 for (vector
<function_info
*>::reverse_iterator it
1849 = functions
.rbegin (); it
!= functions
.rend (); it
++)
1851 if ((*it
)->ident
== ident
)
1860 else if (gcov_read_unsigned () != fn
->lineno_checksum
1861 || gcov_read_unsigned () != fn
->cfg_checksum
)
1864 fnotice (stderr
, "%s:profile mismatch for '%s'\n",
1865 da_file_name
, fn
->name
);
1869 else if (tag
== GCOV_TAG_FOR_COUNTER (GCOV_COUNTER_ARCS
) && fn
)
1871 if (length
!= GCOV_TAG_COUNTER_LENGTH (fn
->counts
.size ()))
1874 for (ix
= 0; ix
!= fn
->counts
.size (); ix
++)
1875 fn
->counts
[ix
] += gcov_read_counter ();
1877 gcov_sync (base
, length
);
1878 if ((error
= gcov_is_error ()))
1882 ? N_("%s:overflowed\n")
1883 : N_("%s:corrupted\n"),
1893 /* Solve the flow graph. Propagate counts from the instrumented arcs
1894 to the blocks and the uninstrumented arcs. */
1897 solve_flow_graph (function_info
*fn
)
1901 gcov_type
*count_ptr
= &fn
->counts
.front ();
1903 block_info
*valid_blocks
= NULL
; /* valid, but unpropagated blocks. */
1904 block_info
*invalid_blocks
= NULL
; /* invalid, but inferable blocks. */
1906 /* The arcs were built in reverse order. Fix that now. */
1907 for (ix
= fn
->blocks
.size (); ix
--;)
1909 arc_info
*arc_p
, *arc_n
;
1911 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].succ
; arc
;
1912 arc_p
= arc
, arc
= arc_n
)
1914 arc_n
= arc
->succ_next
;
1915 arc
->succ_next
= arc_p
;
1917 fn
->blocks
[ix
].succ
= arc_p
;
1919 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].pred
; arc
;
1920 arc_p
= arc
, arc
= arc_n
)
1922 arc_n
= arc
->pred_next
;
1923 arc
->pred_next
= arc_p
;
1925 fn
->blocks
[ix
].pred
= arc_p
;
1928 if (fn
->blocks
.size () < 2)
1929 fnotice (stderr
, "%s:'%s' lacks entry and/or exit blocks\n",
1930 bbg_file_name
, fn
->name
);
1933 if (fn
->blocks
[ENTRY_BLOCK
].num_pred
)
1934 fnotice (stderr
, "%s:'%s' has arcs to entry block\n",
1935 bbg_file_name
, fn
->name
);
1937 /* We can't deduce the entry block counts from the lack of
1939 fn
->blocks
[ENTRY_BLOCK
].num_pred
= ~(unsigned)0;
1941 if (fn
->blocks
[EXIT_BLOCK
].num_succ
)
1942 fnotice (stderr
, "%s:'%s' has arcs from exit block\n",
1943 bbg_file_name
, fn
->name
);
1945 /* Likewise, we can't deduce exit block counts from the lack
1946 of its successors. */
1947 fn
->blocks
[EXIT_BLOCK
].num_succ
= ~(unsigned)0;
1950 /* Propagate the measured counts, this must be done in the same
1951 order as the code in profile.c */
1952 for (unsigned i
= 0; i
< fn
->blocks
.size (); i
++)
1954 blk
= &fn
->blocks
[i
];
1955 block_info
const *prev_dst
= NULL
;
1956 int out_of_order
= 0;
1957 int non_fake_succ
= 0;
1959 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1967 arc
->count
= *count_ptr
++;
1968 arc
->count_valid
= 1;
1970 arc
->dst
->num_pred
--;
1972 if (prev_dst
&& prev_dst
> arc
->dst
)
1974 prev_dst
= arc
->dst
;
1976 if (non_fake_succ
== 1)
1978 /* If there is only one non-fake exit, it is an
1979 unconditional branch. */
1980 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1983 arc
->is_unconditional
= 1;
1984 /* If this block is instrumenting a call, it might be
1985 an artificial block. It is not artificial if it has
1986 a non-fallthrough exit, or the destination of this
1987 arc has more than one entry. Mark the destination
1988 block as a return site, if none of those conditions
1990 if (blk
->is_call_site
&& arc
->fall_through
1991 && arc
->dst
->pred
== arc
&& !arc
->pred_next
)
1992 arc
->dst
->is_call_return
= 1;
1996 /* Sort the successor arcs into ascending dst order. profile.c
1997 normally produces arcs in the right order, but sometimes with
1998 one or two out of order. We're not using a particularly
2002 arc_info
*start
= blk
->succ
;
2003 unsigned changes
= 1;
2007 arc_info
*arc
, *arc_p
, *arc_n
;
2010 for (arc_p
= NULL
, arc
= start
; (arc_n
= arc
->succ_next
);)
2012 if (arc
->dst
> arc_n
->dst
)
2016 arc_p
->succ_next
= arc_n
;
2019 arc
->succ_next
= arc_n
->succ_next
;
2020 arc_n
->succ_next
= arc
;
2033 /* Place it on the invalid chain, it will be ignored if that's
2035 blk
->invalid_chain
= 1;
2036 blk
->chain
= invalid_blocks
;
2037 invalid_blocks
= blk
;
2040 while (invalid_blocks
|| valid_blocks
)
2042 while ((blk
= invalid_blocks
))
2044 gcov_type total
= 0;
2045 const arc_info
*arc
;
2047 invalid_blocks
= blk
->chain
;
2048 blk
->invalid_chain
= 0;
2050 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
2051 total
+= arc
->count
;
2052 else if (!blk
->num_pred
)
2053 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
2054 total
+= arc
->count
;
2059 blk
->count_valid
= 1;
2060 blk
->chain
= valid_blocks
;
2061 blk
->valid_chain
= 1;
2064 while ((blk
= valid_blocks
))
2067 arc_info
*arc
, *inv_arc
;
2069 valid_blocks
= blk
->chain
;
2070 blk
->valid_chain
= 0;
2071 if (blk
->num_succ
== 1)
2077 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
2079 total
-= arc
->count
;
2080 if (!arc
->count_valid
)
2084 inv_arc
->count_valid
= 1;
2085 inv_arc
->count
= total
;
2088 if (dst
->count_valid
)
2090 if (dst
->num_pred
== 1 && !dst
->valid_chain
)
2092 dst
->chain
= valid_blocks
;
2093 dst
->valid_chain
= 1;
2099 if (!dst
->num_pred
&& !dst
->invalid_chain
)
2101 dst
->chain
= invalid_blocks
;
2102 dst
->invalid_chain
= 1;
2103 invalid_blocks
= dst
;
2107 if (blk
->num_pred
== 1)
2113 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
2115 total
-= arc
->count
;
2116 if (!arc
->count_valid
)
2120 inv_arc
->count_valid
= 1;
2121 inv_arc
->count
= total
;
2124 if (src
->count_valid
)
2126 if (src
->num_succ
== 1 && !src
->valid_chain
)
2128 src
->chain
= valid_blocks
;
2129 src
->valid_chain
= 1;
2135 if (!src
->num_succ
&& !src
->invalid_chain
)
2137 src
->chain
= invalid_blocks
;
2138 src
->invalid_chain
= 1;
2139 invalid_blocks
= src
;
2146 /* If the graph has been correctly solved, every block will have a
2148 for (unsigned i
= 0; ix
< fn
->blocks
.size (); i
++)
2149 if (!fn
->blocks
[i
].count_valid
)
2151 fnotice (stderr
, "%s:graph is unsolvable for '%s'\n",
2152 bbg_file_name
, fn
->name
);
2157 /* Mark all the blocks only reachable via an incoming catch. */
2160 find_exception_blocks (function_info
*fn
)
2163 block_info
**queue
= XALLOCAVEC (block_info
*, fn
->blocks
.size ());
2165 /* First mark all blocks as exceptional. */
2166 for (ix
= fn
->blocks
.size (); ix
--;)
2167 fn
->blocks
[ix
].exceptional
= 1;
2169 /* Now mark all the blocks reachable via non-fake edges */
2170 queue
[0] = &fn
->blocks
[0];
2171 queue
[0]->exceptional
= 0;
2174 block_info
*block
= queue
[--ix
];
2175 const arc_info
*arc
;
2177 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2178 if (!arc
->fake
&& !arc
->is_throw
&& arc
->dst
->exceptional
)
2180 arc
->dst
->exceptional
= 0;
2181 queue
[ix
++] = arc
->dst
;
2187 /* Increment totals in COVERAGE according to arc ARC. */
2190 add_branch_counts (coverage_info
*coverage
, const arc_info
*arc
)
2192 if (arc
->is_call_non_return
)
2195 if (arc
->src
->count
)
2196 coverage
->calls_executed
++;
2198 else if (!arc
->is_unconditional
)
2200 coverage
->branches
++;
2201 if (arc
->src
->count
)
2202 coverage
->branches_executed
++;
2204 coverage
->branches_taken
++;
2208 /* Format COUNT, if flag_human_readable_numbers is set, return it human
2212 format_count (gcov_type count
)
2214 static char buffer
[64];
2215 const char *units
= " kMGTPEZY";
2217 if (count
< 1000 || !flag_human_readable_numbers
)
2219 sprintf (buffer
, "%" PRId64
, count
);
2224 gcov_type divisor
= 1;
2225 for (i
= 0; units
[i
+1]; i
++, divisor
*= 1000)
2227 if (count
+ divisor
/ 2 < 1000 * divisor
)
2230 float r
= 1.0f
* count
/ divisor
;
2231 sprintf (buffer
, "%.1f%c", r
, units
[i
]);
2235 /* Format a GCOV_TYPE integer as either a percent ratio, or absolute
2236 count. If DECIMAL_PLACES >= 0, format TOP/BOTTOM * 100 to DECIMAL_PLACES.
2237 If DECIMAL_PLACES is zero, no decimal point is printed. Only print 100% when
2238 TOP==BOTTOM and only print 0% when TOP=0. If DECIMAL_PLACES < 0, then simply
2239 format TOP. Return pointer to a static string. */
2242 format_gcov (gcov_type top
, gcov_type bottom
, int decimal_places
)
2244 static char buffer
[20];
2246 if (decimal_places
>= 0)
2248 float ratio
= bottom
? 100.0f
* top
/ bottom
: 0;
2250 /* Round up to 1% if there's a small non-zero value. */
2251 if (ratio
> 0.0f
&& ratio
< 0.5f
&& decimal_places
== 0)
2253 sprintf (buffer
, "%.*f%%", decimal_places
, ratio
);
2256 return format_count (top
);
2261 /* Summary of execution */
2264 executed_summary (unsigned lines
, unsigned executed
)
2267 fnotice (stdout
, "Lines executed:%s of %d\n",
2268 format_gcov (executed
, lines
, 2), lines
);
2270 fnotice (stdout
, "No executable lines\n");
2273 /* Output summary info for a function or file. */
2276 function_summary (const coverage_info
*coverage
, const char *title
)
2278 fnotice (stdout
, "%s '%s'\n", title
, coverage
->name
);
2279 executed_summary (coverage
->lines
, coverage
->lines_executed
);
2283 if (coverage
->branches
)
2285 fnotice (stdout
, "Branches executed:%s of %d\n",
2286 format_gcov (coverage
->branches_executed
,
2287 coverage
->branches
, 2),
2288 coverage
->branches
);
2289 fnotice (stdout
, "Taken at least once:%s of %d\n",
2290 format_gcov (coverage
->branches_taken
,
2291 coverage
->branches
, 2),
2292 coverage
->branches
);
2295 fnotice (stdout
, "No branches\n");
2296 if (coverage
->calls
)
2297 fnotice (stdout
, "Calls executed:%s of %d\n",
2298 format_gcov (coverage
->calls_executed
, coverage
->calls
, 2),
2301 fnotice (stdout
, "No calls\n");
2305 /* Canonicalize the filename NAME by canonicalizing directory
2306 separators, eliding . components and resolving .. components
2307 appropriately. Always returns a unique string. */
2310 canonicalize_name (const char *name
)
2312 /* The canonical name cannot be longer than the incoming name. */
2313 char *result
= XNEWVEC (char, strlen (name
) + 1);
2314 const char *base
= name
, *probe
;
2319 #if HAVE_DOS_BASED_FILE_SYSTEM
2320 if (base
[0] && base
[1] == ':')
2322 result
[0] = base
[0];
2328 for (dd_base
= ptr
; *base
; base
= probe
)
2332 for (probe
= base
; *probe
; probe
++)
2333 if (IS_DIR_SEPARATOR (*probe
))
2337 if (len
== 1 && base
[0] == '.')
2338 /* Elide a '.' directory */
2340 else if (len
== 2 && base
[0] == '.' && base
[1] == '.')
2342 /* '..', we can only elide it and the previous directory, if
2343 we're not a symlink. */
2344 struct stat ATTRIBUTE_UNUSED buf
;
2348 #if defined (S_ISLNK)
2349 /* S_ISLNK is not POSIX.1-1996. */
2350 || stat (result
, &buf
) || S_ISLNK (buf
.st_mode
)
2354 /* Cannot elide, or unreadable or a symlink. */
2355 dd_base
= ptr
+ 2 + slash
;
2358 while (ptr
!= dd_base
&& *ptr
!= '/')
2360 slash
= ptr
!= result
;
2365 /* Regular pathname component. */
2368 memcpy (ptr
, base
, len
);
2373 for (; IS_DIR_SEPARATOR (*probe
); probe
++)
2381 /* Print hex representation of 16 bytes from SUM and write it to BUFFER. */
2384 md5sum_to_hex (const char *sum
, char *buffer
)
2386 for (unsigned i
= 0; i
< 16; i
++)
2387 sprintf (buffer
+ (2 * i
), "%02x", (unsigned char)sum
[i
]);
2390 /* Generate an output file name. INPUT_NAME is the canonicalized main
2391 input file and SRC_NAME is the canonicalized file name.
2392 LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
2393 long_output_names we prepend the processed name of the input file
2394 to each output name (except when the current source file is the
2395 input file, so you don't get a double concatenation). The two
2396 components are separated by '##'. With preserve_paths we create a
2397 filename from all path components of the source file, replacing '/'
2398 with '#', and .. with '^', without it we simply take the basename
2399 component. (Remember, the canonicalized name will already have
2400 elided '.' components and converted \\ separators.) */
2403 make_gcov_file_name (const char *input_name
, const char *src_name
)
2408 if (flag_long_names
&& input_name
&& strcmp (src_name
, input_name
))
2410 /* Generate the input filename part. */
2411 result
= XNEWVEC (char, strlen (input_name
) + strlen (src_name
) + 10);
2414 ptr
= mangle_name (input_name
, ptr
);
2415 ptr
[0] = ptr
[1] = '#';
2420 result
= XNEWVEC (char, strlen (src_name
) + 10);
2424 ptr
= mangle_name (src_name
, ptr
);
2425 strcpy (ptr
, ".gcov");
2427 /* When hashing filenames, we shorten them by only using the filename
2428 component and appending a hash of the full (mangled) pathname. */
2429 if (flag_hash_filenames
)
2433 char md5sum_hex
[33];
2435 md5_init_ctx (&ctx
);
2436 md5_process_bytes (src_name
, strlen (src_name
), &ctx
);
2437 md5_finish_ctx (&ctx
, md5sum
);
2438 md5sum_to_hex (md5sum
, md5sum_hex
);
2441 result
= XNEWVEC (char, strlen (src_name
) + 50);
2443 ptr
= mangle_name (src_name
, ptr
);
2444 ptr
[0] = ptr
[1] = '#';
2446 memcpy (ptr
, md5sum_hex
, 32);
2448 strcpy (ptr
, ".gcov");
2455 mangle_name (char const *base
, char *ptr
)
2459 /* Generate the source filename part. */
2460 if (!flag_preserve_paths
)
2462 base
= lbasename (base
);
2463 len
= strlen (base
);
2464 memcpy (ptr
, base
, len
);
2468 ptr
= mangle_path (base
);
2473 /* Scan through the bb_data for each line in the block, increment
2474 the line number execution count indicated by the execution count of
2475 the appropriate basic block. */
2478 add_line_counts (coverage_info
*coverage
, function_info
*fn
)
2480 bool has_any_line
= false;
2481 /* Scan each basic block. */
2482 for (unsigned ix
= 0; ix
!= fn
->blocks
.size (); ix
++)
2484 line_info
*line
= NULL
;
2485 block_info
*block
= &fn
->blocks
[ix
];
2486 if (block
->count
&& ix
&& ix
+ 1 != fn
->blocks
.size ())
2487 fn
->blocks_executed
++;
2488 for (unsigned i
= 0; i
< block
->locations
.size (); i
++)
2490 unsigned src_idx
= block
->locations
[i
].source_file_idx
;
2491 vector
<unsigned> &lines
= block
->locations
[i
].lines
;
2493 block
->cycle
.arc
= NULL
;
2494 block
->cycle
.ident
= ~0U;
2496 for (unsigned j
= 0; j
< lines
.size (); j
++)
2498 unsigned ln
= lines
[j
];
2500 /* Line belongs to a function that is in a group. */
2501 if (fn
->group_line_p (ln
, src_idx
))
2503 gcc_assert (lines
[j
] - fn
->start_line
< fn
->lines
.size ());
2504 line
= &(fn
->lines
[lines
[j
] - fn
->start_line
]);
2506 if (!block
->exceptional
)
2508 line
->unexceptional
= 1;
2509 if (block
->count
== 0)
2510 line
->has_unexecuted_block
= 1;
2512 line
->count
+= block
->count
;
2516 gcc_assert (ln
< sources
[src_idx
].lines
.size ());
2517 line
= &(sources
[src_idx
].lines
[ln
]);
2522 if (!line
->count
&& block
->count
)
2523 coverage
->lines_executed
++;
2526 if (!block
->exceptional
)
2528 line
->unexceptional
= 1;
2529 if (block
->count
== 0)
2530 line
->has_unexecuted_block
= 1;
2532 line
->count
+= block
->count
;
2536 has_any_line
= true;
2538 if (!ix
|| ix
+ 1 == fn
->blocks
.size ())
2539 /* Entry or exit block. */;
2540 else if (line
!= NULL
)
2542 line
->blocks
.push_back (block
);
2548 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2549 line
->branches
.push_back (arc
);
2556 fnotice (stderr
, "%s:no lines for '%s'\n", bbg_file_name
, fn
->name
);
2559 /* Accumulate info for LINE that belongs to SRC source file. If ADD_COVERAGE
2560 is set to true, update source file summary. */
2562 static void accumulate_line_info (line_info
*line
, source_info
*src
,
2566 for (vector
<arc_info
*>::iterator it
= line
->branches
.begin ();
2567 it
!= line
->branches
.end (); it
++)
2568 add_branch_counts (&src
->coverage
, *it
);
2570 if (!line
->blocks
.empty ())
2572 /* The user expects the line count to be the number of times
2573 a line has been executed. Simply summing the block count
2574 will give an artificially high number. The Right Thing
2575 is to sum the entry counts to the graph of blocks on this
2576 line, then find the elementary cycles of the local graph
2577 and add the transition counts of those cycles. */
2578 gcov_type count
= 0;
2580 /* Cycle detection. */
2581 for (vector
<block_info
*>::iterator it
= line
->blocks
.begin ();
2582 it
!= line
->blocks
.end (); it
++)
2584 for (arc_info
*arc
= (*it
)->pred
; arc
; arc
= arc
->pred_next
)
2585 if (!line
->has_block (arc
->src
))
2586 count
+= arc
->count
;
2587 for (arc_info
*arc
= (*it
)->succ
; arc
; arc
= arc
->succ_next
)
2588 arc
->cs_count
= arc
->count
;
2591 /* Now, add the count of loops entirely on this line. */
2592 count
+= get_cycles_count (*line
);
2593 line
->count
= count
;
2595 if (line
->count
> src
->maximum_count
)
2596 src
->maximum_count
= line
->count
;
2599 if (line
->exists
&& add_coverage
)
2601 src
->coverage
.lines
++;
2603 src
->coverage
.lines_executed
++;
2607 /* Accumulate the line counts of a file. */
2610 accumulate_line_counts (source_info
*src
)
2612 /* First work on group functions. */
2613 for (vector
<function_info
*>::iterator it
= src
->functions
.begin ();
2614 it
!= src
->functions
.end (); it
++)
2616 function_info
*fn
= *it
;
2618 if (fn
->src
!= src
->index
|| !fn
->is_group
)
2621 for (vector
<line_info
>::iterator it2
= fn
->lines
.begin ();
2622 it2
!= fn
->lines
.end (); it2
++)
2624 line_info
*line
= &(*it2
);
2625 accumulate_line_info (line
, src
, false);
2629 /* Work on global lines that line in source file SRC. */
2630 for (vector
<line_info
>::iterator it
= src
->lines
.begin ();
2631 it
!= src
->lines
.end (); it
++)
2632 accumulate_line_info (&(*it
), src
, true);
2634 /* If not using intermediate mode, sum lines of group functions and
2635 add them to lines that live in a source file. */
2636 if (!flag_intermediate_format
)
2637 for (vector
<function_info
*>::iterator it
= src
->functions
.begin ();
2638 it
!= src
->functions
.end (); it
++)
2640 function_info
*fn
= *it
;
2642 if (fn
->src
!= src
->index
|| !fn
->is_group
)
2645 for (unsigned i
= 0; i
< fn
->lines
.size (); i
++)
2647 line_info
*fn_line
= &fn
->lines
[i
];
2648 if (fn_line
->exists
)
2650 unsigned ln
= fn
->start_line
+ i
;
2651 line_info
*src_line
= &src
->lines
[ln
];
2653 if (!src_line
->exists
)
2654 src
->coverage
.lines
++;
2655 if (!src_line
->count
&& fn_line
->count
)
2656 src
->coverage
.lines_executed
++;
2658 src_line
->count
+= fn_line
->count
;
2659 src_line
->exists
= 1;
2661 if (fn_line
->has_unexecuted_block
)
2662 src_line
->has_unexecuted_block
= 1;
2664 if (fn_line
->unexceptional
)
2665 src_line
->unexceptional
= 1;
2671 /* Output information about ARC number IX. Returns nonzero if
2672 anything is output. */
2675 output_branch_count (FILE *gcov_file
, int ix
, const arc_info
*arc
)
2677 if (arc
->is_call_non_return
)
2679 if (arc
->src
->count
)
2681 fnotice (gcov_file
, "call %2d returned %s\n", ix
,
2682 format_gcov (arc
->src
->count
- arc
->count
,
2683 arc
->src
->count
, -flag_counts
));
2686 fnotice (gcov_file
, "call %2d never executed\n", ix
);
2688 else if (!arc
->is_unconditional
)
2690 if (arc
->src
->count
)
2691 fnotice (gcov_file
, "branch %2d taken %s%s", ix
,
2692 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
),
2693 arc
->fall_through
? " (fallthrough)"
2694 : arc
->is_throw
? " (throw)" : "");
2696 fnotice (gcov_file
, "branch %2d never executed", ix
);
2699 fnotice (gcov_file
, " (BB %d)", arc
->dst
->id
);
2701 fnotice (gcov_file
, "\n");
2703 else if (flag_unconditional
&& !arc
->dst
->is_call_return
)
2705 if (arc
->src
->count
)
2706 fnotice (gcov_file
, "unconditional %2d taken %s\n", ix
,
2707 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
));
2709 fnotice (gcov_file
, "unconditional %2d never executed\n", ix
);
2717 read_line (FILE *file
)
2719 static char *string
;
2720 static size_t string_len
;
2727 string
= XNEWVEC (char, string_len
);
2730 while ((ptr
= fgets (string
+ pos
, string_len
- pos
, file
)))
2732 size_t len
= strlen (string
+ pos
);
2734 if (len
&& string
[pos
+ len
- 1] == '\n')
2736 string
[pos
+ len
- 1] = 0;
2740 /* If the file contains NUL characters or an incomplete
2741 last line, which can happen more than once in one run,
2742 we have to avoid doubling the STRING_LEN unnecessarily. */
2743 if (pos
> string_len
/ 2)
2746 string
= XRESIZEVEC (char, string
, string_len
);
2750 return pos
? string
: NULL
;
2753 /* Pad string S with spaces from left to have total width equal to 9. */
2756 pad_count_string (string
&s
)
2759 s
.insert (0, 9 - s
.size (), ' ');
2762 /* Print GCOV line beginning to F stream. If EXISTS is set to true, the
2763 line exists in source file. UNEXCEPTIONAL indicated that it's not in
2764 an exceptional statement. The output is printed for LINE_NUM of given
2765 COUNT of executions. EXCEPTIONAL_STRING and UNEXCEPTIONAL_STRING are
2766 used to indicate non-executed blocks. */
2769 output_line_beginning (FILE *f
, bool exists
, bool unexceptional
,
2770 bool has_unexecuted_block
,
2771 gcov_type count
, unsigned line_num
,
2772 const char *exceptional_string
,
2773 const char *unexceptional_string
,
2774 unsigned int maximum_count
)
2781 s
= format_gcov (count
, 0, -1);
2782 if (has_unexecuted_block
2783 && bbg_supports_has_unexecuted_blocks
)
2785 if (flag_use_colors
)
2787 pad_count_string (s
);
2788 s
.insert (0, SGR_SEQ (COLOR_BG_MAGENTA
2789 COLOR_SEPARATOR COLOR_FG_WHITE
));
2795 pad_count_string (s
);
2799 if (flag_use_colors
)
2802 pad_count_string (s
);
2804 s
.insert (0, SGR_SEQ (COLOR_BG_RED
2805 COLOR_SEPARATOR COLOR_FG_WHITE
));
2807 s
.insert (0, SGR_SEQ (COLOR_BG_CYAN
2808 COLOR_SEPARATOR COLOR_FG_WHITE
));
2813 s
= unexceptional
? unexceptional_string
: exceptional_string
;
2814 pad_count_string (s
);
2821 pad_count_string (s
);
2824 /* Format line number in output. */
2826 sprintf (buffer
, "%5u", line_num
);
2827 string
linestr (buffer
);
2829 if (flag_use_hotness_colors
&& maximum_count
)
2831 if (count
* 2 > maximum_count
) /* > 50%. */
2832 linestr
.insert (0, SGR_SEQ (COLOR_BG_RED
));
2833 else if (count
* 5 > maximum_count
) /* > 20%. */
2834 linestr
.insert (0, SGR_SEQ (COLOR_BG_YELLOW
));
2835 else if (count
* 10 > maximum_count
) /* > 10%. */
2836 linestr
.insert (0, SGR_SEQ (COLOR_BG_GREEN
));
2837 linestr
+= SGR_RESET
;
2840 fprintf (f
, "%s:%s", s
.c_str (), linestr
.c_str ());
2844 print_source_line (FILE *f
, const vector
<const char *> &source_lines
,
2847 gcc_assert (line
>= 1);
2848 gcc_assert (line
<= source_lines
.size ());
2850 fprintf (f
, ":%s\n", source_lines
[line
- 1]);
2853 /* Output line details for LINE and print it to F file. LINE lives on
2857 output_line_details (FILE *f
, const line_info
*line
, unsigned line_num
)
2859 if (flag_all_blocks
)
2865 for (vector
<block_info
*>::const_iterator it
= line
->blocks
.begin ();
2866 it
!= line
->blocks
.end (); it
++)
2868 if (!(*it
)->is_call_return
)
2870 output_line_beginning (f
, line
->exists
,
2871 (*it
)->exceptional
, false,
2872 (*it
)->count
, line_num
,
2873 "%%%%%", "$$$$$", 0);
2874 fprintf (f
, "-block %2d", ix
++);
2876 fprintf (f
, " (BB %u)", (*it
)->id
);
2880 for (arc
= (*it
)->succ
; arc
; arc
= arc
->succ_next
)
2881 jx
+= output_branch_count (f
, jx
, arc
);
2884 else if (flag_branches
)
2889 for (vector
<arc_info
*>::const_iterator it
= line
->branches
.begin ();
2890 it
!= line
->branches
.end (); it
++)
2891 ix
+= output_branch_count (f
, ix
, (*it
));
2895 /* Output detail statistics about function FN to file F. */
2898 output_function_details (FILE *f
, const function_info
*fn
)
2903 arc_info
*arc
= fn
->blocks
[EXIT_BLOCK
].pred
;
2904 gcov_type return_count
= fn
->blocks
[EXIT_BLOCK
].count
;
2905 gcov_type called_count
= fn
->blocks
[ENTRY_BLOCK
].count
;
2907 for (; arc
; arc
= arc
->pred_next
)
2909 return_count
-= arc
->count
;
2911 fprintf (f
, "function %s",
2912 flag_demangled_names
? fn
->demangled_name
: fn
->name
);
2913 fprintf (f
, " called %s",
2914 format_gcov (called_count
, 0, -1));
2915 fprintf (f
, " returned %s",
2916 format_gcov (return_count
, called_count
, 0));
2917 fprintf (f
, " blocks executed %s",
2918 format_gcov (fn
->blocks_executed
, fn
->blocks
.size () - 2,
2923 /* Read in the source file one line at a time, and output that line to
2924 the gcov file preceded by its execution count and other
2928 output_lines (FILE *gcov_file
, const source_info
*src
)
2930 #define DEFAULT_LINE_START " -: 0:"
2931 #define FN_SEPARATOR "------------------\n"
2936 /* Print colorization legend. */
2937 if (flag_use_colors
)
2938 fprintf (gcov_file
, "%s",
2939 DEFAULT_LINE_START
"Colorization: profile count: " \
2940 SGR_SEQ (COLOR_BG_CYAN
) "zero coverage (exceptional)" SGR_RESET \
2942 SGR_SEQ (COLOR_BG_RED
) "zero coverage (unexceptional)" SGR_RESET \
2944 SGR_SEQ (COLOR_BG_MAGENTA
) "unexecuted block" SGR_RESET
"\n");
2946 if (flag_use_hotness_colors
)
2947 fprintf (gcov_file
, "%s",
2948 DEFAULT_LINE_START
"Colorization: line numbers: hotness: " \
2949 SGR_SEQ (COLOR_BG_RED
) "> 50%" SGR_RESET
" " \
2950 SGR_SEQ (COLOR_BG_YELLOW
) "> 20%" SGR_RESET
" " \
2951 SGR_SEQ (COLOR_BG_GREEN
) "> 10%" SGR_RESET
"\n");
2953 fprintf (gcov_file
, DEFAULT_LINE_START
"Source:%s\n", src
->coverage
.name
);
2955 fprintf (gcov_file
, DEFAULT_LINE_START
"Source:%s\n", src
->coverage
.name
);
2956 if (!multiple_files
)
2958 fprintf (gcov_file
, DEFAULT_LINE_START
"Graph:%s\n", bbg_file_name
);
2959 fprintf (gcov_file
, DEFAULT_LINE_START
"Data:%s\n",
2960 no_data_file
? "-" : da_file_name
);
2961 fprintf (gcov_file
, DEFAULT_LINE_START
"Runs:%u\n", object_runs
);
2964 source_file
= fopen (src
->name
, "r");
2966 fnotice (stderr
, "Cannot open source file %s\n", src
->name
);
2967 else if (src
->file_time
== 0)
2968 fprintf (gcov_file
, DEFAULT_LINE_START
"Source is newer than graph\n");
2970 vector
<const char *> source_lines
;
2972 while ((retval
= read_line (source_file
)) != NULL
)
2973 source_lines
.push_back (xstrdup (retval
));
2975 unsigned line_start_group
= 0;
2976 vector
<function_info
*> fns
;
2978 for (unsigned line_num
= 1; line_num
<= source_lines
.size (); line_num
++)
2980 if (line_num
>= src
->lines
.size ())
2982 fprintf (gcov_file
, "%9s:%5u", "-", line_num
);
2983 print_source_line (gcov_file
, source_lines
, line_num
);
2987 const line_info
*line
= &src
->lines
[line_num
];
2989 if (line_start_group
== 0)
2991 fns
= src
->get_functions_at_location (line_num
);
2992 if (fns
.size () > 1)
2994 /* It's possible to have functions that partially overlap,
2995 thus take the maximum end_line of functions starting
2997 for (unsigned i
= 0; i
< fns
.size (); i
++)
2998 if (fns
[i
]->end_line
> line_start_group
)
2999 line_start_group
= fns
[i
]->end_line
;
3001 else if (fns
.size () == 1)
3003 function_info
*fn
= fns
[0];
3004 output_function_details (gcov_file
, fn
);
3008 /* For lines which don't exist in the .bb file, print '-' before
3009 the source line. For lines which exist but were never
3010 executed, print '#####' or '=====' before the source line.
3011 Otherwise, print the execution count before the source line.
3012 There are 16 spaces of indentation added before the source
3013 line so that tabs won't be messed up. */
3014 output_line_beginning (gcov_file
, line
->exists
, line
->unexceptional
,
3015 line
->has_unexecuted_block
, line
->count
,
3016 line_num
, "=====", "#####", src
->maximum_count
);
3018 print_source_line (gcov_file
, source_lines
, line_num
);
3019 output_line_details (gcov_file
, line
, line_num
);
3021 if (line_start_group
== line_num
)
3023 for (vector
<function_info
*>::iterator it
= fns
.begin ();
3024 it
!= fns
.end (); it
++)
3026 function_info
*fn
= *it
;
3027 vector
<line_info
> &lines
= fn
->lines
;
3029 fprintf (gcov_file
, FN_SEPARATOR
);
3032 = flag_demangled_names
? fn
->demangled_name
: fn
->name
;
3034 if (flag_use_colors
)
3036 fn_name
.insert (0, SGR_SEQ (COLOR_FG_CYAN
));
3037 fn_name
+= SGR_RESET
;
3040 fprintf (gcov_file
, "%s:\n", fn_name
.c_str ());
3042 output_function_details (gcov_file
, fn
);
3044 /* Print all lines covered by the function. */
3045 for (unsigned i
= 0; i
< lines
.size (); i
++)
3047 line_info
*line
= &lines
[i
];
3048 unsigned l
= fn
->start_line
+ i
;
3050 /* For lines which don't exist in the .bb file, print '-'
3051 before the source line. For lines which exist but
3052 were never executed, print '#####' or '=====' before
3053 the source line. Otherwise, print the execution count
3054 before the source line.
3055 There are 16 spaces of indentation added before the source
3056 line so that tabs won't be messed up. */
3057 output_line_beginning (gcov_file
, line
->exists
,
3058 line
->unexceptional
,
3059 line
->has_unexecuted_block
,
3061 l
, "=====", "#####",
3062 src
->maximum_count
);
3064 print_source_line (gcov_file
, source_lines
, l
);
3065 output_line_details (gcov_file
, line
, l
);
3069 fprintf (gcov_file
, FN_SEPARATOR
);
3070 line_start_group
= 0;
3075 fclose (source_file
);