1 /* Gcov.c: prepend line execution counts and branch probabilities to a
3 Copyright (C) 1990-2019 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"
47 #include "pretty-print.h"
61 /* The gcno file is generated by -ftest-coverage option. The gcda file is
62 generated by a program compiled with -fprofile-arcs. Their formats
63 are documented in gcov-io.h. */
65 /* The functions in this file for creating and solution program flow graphs
66 are very similar to functions in the gcc source file profile.c. In
67 some places we make use of the knowledge of how profile.c works to
68 select particular algorithms here. */
70 /* The code validates that the profile information read in corresponds
71 to the code currently being compiled. Rather than checking for
72 identical files, the code below compares a checksum on the CFG
73 (based on the order of basic blocks and the arcs in the CFG). If
74 the CFG checksum in the gcda file match the CFG checksum in the
75 gcno file, the profile data will be used. */
77 /* This is the size of the buffer used to read in source file lines. */
83 /* Describes an arc between two basic blocks. */
87 /* source and destination blocks. */
88 struct block_info
*src
;
89 struct block_info
*dst
;
91 /* transition counts. */
93 /* used in cycle search, so that we do not clobber original counts. */
96 unsigned int count_valid
: 1;
97 unsigned int on_tree
: 1;
98 unsigned int fake
: 1;
99 unsigned int fall_through
: 1;
101 /* Arc to a catch handler. */
102 unsigned int is_throw
: 1;
104 /* Arc is for a function that abnormally returns. */
105 unsigned int is_call_non_return
: 1;
107 /* Arc is for catch/setjmp. */
108 unsigned int is_nonlocal_return
: 1;
110 /* Is an unconditional branch. */
111 unsigned int is_unconditional
: 1;
113 /* Loop making arc. */
114 unsigned int cycle
: 1;
116 /* Links to next arc on src and dst lists. */
117 struct arc_info
*succ_next
;
118 struct arc_info
*pred_next
;
121 /* Describes which locations (lines and files) are associated with
124 struct block_location_info
126 block_location_info (unsigned _source_file_idx
):
127 source_file_idx (_source_file_idx
)
130 unsigned source_file_idx
;
131 vector
<unsigned> lines
;
134 /* Describes a basic block. Contains lists of arcs to successor and
135 predecessor blocks. */
142 /* Chain of exit and entry arcs. */
146 /* Number of unprocessed exit and entry arcs. */
152 /* Block execution count. */
154 unsigned count_valid
: 1;
155 unsigned valid_chain
: 1;
156 unsigned invalid_chain
: 1;
157 unsigned exceptional
: 1;
159 /* Block is a call instrumenting site. */
160 unsigned is_call_site
: 1; /* Does the call. */
161 unsigned is_call_return
: 1; /* Is the return. */
163 /* Block is a landing pad for longjmp or throw. */
164 unsigned is_nonlocal_return
: 1;
166 vector
<block_location_info
> locations
;
170 /* Single line graph cycle workspace. Used for all-blocks
174 } cycle
; /* Used in all-blocks mode, after blocks are linked onto
177 /* Temporary chain for solving graph, and for chaining blocks on one
179 struct block_info
*chain
;
183 block_info::block_info (): succ (NULL
), pred (NULL
), num_succ (0), num_pred (0),
184 id (0), count (0), count_valid (0), valid_chain (0), invalid_chain (0),
185 exceptional (0), is_call_site (0), is_call_return (0), is_nonlocal_return (0),
186 locations (), chain (NULL
)
191 /* Describes a single line of source. Contains a chain of basic blocks
196 /* Default constructor. */
199 /* Return true when NEEDLE is one of basic blocks the line belongs to. */
200 bool has_block (block_info
*needle
);
202 /* Execution count. */
205 /* Branches from blocks that end on this line. */
206 vector
<arc_info
*> branches
;
208 /* blocks which start on this line. Used in all-blocks mode. */
209 vector
<block_info
*> blocks
;
212 unsigned unexceptional
: 1;
213 unsigned has_unexecuted_block
: 1;
216 line_info::line_info (): count (0), branches (), blocks (), exists (false),
217 unexceptional (0), has_unexecuted_block (0)
222 line_info::has_block (block_info
*needle
)
224 return std::find (blocks
.begin (), blocks
.end (), needle
) != blocks
.end ();
227 /* Output demangled function names. */
229 static int flag_demangled_names
= 0;
231 /* Describes a single function. Contains an array of basic blocks. */
238 /* Return true when line N belongs to the function in source file SRC_IDX.
239 The line must be defined in body of the function, can't be inlined. */
240 bool group_line_p (unsigned n
, unsigned src_idx
);
242 /* Function filter based on function_info::artificial variable. */
245 is_artificial (function_info
*fn
)
247 return fn
->artificial
;
250 /* Name of function. */
252 char *m_demangled_name
;
254 unsigned lineno_checksum
;
255 unsigned cfg_checksum
;
257 /* The graph contains at least one fake incoming edge. */
258 unsigned has_catch
: 1;
260 /* True when the function is artificial and does not exist
262 unsigned artificial
: 1;
264 /* True when multiple functions start at a line in a source file. */
265 unsigned is_group
: 1;
267 /* Array of basic blocks. Like in GCC, the entry block is
268 at blocks[0] and the exit block is at blocks[1]. */
269 #define ENTRY_BLOCK (0)
270 #define EXIT_BLOCK (1)
271 vector
<block_info
> blocks
;
272 unsigned blocks_executed
;
274 /* Raw arc coverage counts. */
275 vector
<gcov_type
> counts
;
277 /* First line number. */
280 /* First line column. */
281 unsigned start_column
;
283 /* Last line number. */
286 /* Last line column. */
289 /* Index of source file where the function is defined. */
292 /* Vector of line information. */
293 vector
<line_info
> lines
;
296 struct function_info
*next
;
298 /* Get demangled name of a function. The demangled name
299 is converted when it is used for the first time. */
300 char *get_demangled_name ()
302 if (m_demangled_name
== NULL
)
304 m_demangled_name
= cplus_demangle (m_name
, DMGL_PARAMS
);
305 if (!m_demangled_name
)
306 m_demangled_name
= m_name
;
309 return m_demangled_name
;
312 /* Get name of the function based on flag_demangled_names. */
315 return flag_demangled_names
? get_demangled_name () : m_name
;
318 /* Return number of basic blocks (without entry and exit block). */
319 unsigned get_block_count ()
321 return blocks
.size () - 2;
325 /* Function info comparer that will sort functions according to starting
328 struct function_line_start_cmp
330 inline bool operator() (const function_info
*lhs
,
331 const function_info
*rhs
)
333 return (lhs
->start_line
== rhs
->start_line
334 ? lhs
->start_column
< rhs
->start_column
335 : lhs
->start_line
< rhs
->start_line
);
339 /* Describes coverage of a file or function. */
347 int branches_executed
;
356 /* Describes a file mentioned in the block graph. Contains an array
361 /* Default constructor. */
364 vector
<function_info
*> *get_functions_at_location (unsigned line_num
) const;
366 /* Register a new function. */
367 void add_function (function_info
*fn
);
369 /* Index of the source_info in sources vector. */
372 /* Canonical name of source file. */
376 /* Vector of line information. */
377 vector
<line_info
> lines
;
379 coverage_info coverage
;
381 /* Maximum line count in the source file. */
382 unsigned int maximum_count
;
384 /* Functions in this source file. These are in ascending line
386 vector
<function_info
*> functions
;
388 /* Line number to functions map. */
389 vector
<vector
<function_info
*> *> line_to_function_map
;
392 source_info::source_info (): index (0), name (NULL
), file_time (),
393 lines (), coverage (), maximum_count (0), functions ()
397 /* Register a new function. */
399 source_info::add_function (function_info
*fn
)
401 functions
.push_back (fn
);
403 if (fn
->start_line
>= line_to_function_map
.size ())
404 line_to_function_map
.resize (fn
->start_line
+ 1);
406 vector
<function_info
*> **slot
= &line_to_function_map
[fn
->start_line
];
408 *slot
= new vector
<function_info
*> ();
410 (*slot
)->push_back (fn
);
413 vector
<function_info
*> *
414 source_info::get_functions_at_location (unsigned line_num
) const
416 if (line_num
>= line_to_function_map
.size ())
419 vector
<function_info
*> *slot
= line_to_function_map
[line_num
];
421 std::sort (slot
->begin (), slot
->end (), function_line_start_cmp ());
433 name_map (char *_name
, unsigned _src
): name (_name
), src (_src
)
437 bool operator== (const name_map
&rhs
) const
439 #if HAVE_DOS_BASED_FILE_SYSTEM
440 return strcasecmp (this->name
, rhs
.name
) == 0;
442 return strcmp (this->name
, rhs
.name
) == 0;
446 bool operator< (const name_map
&rhs
) const
448 #if HAVE_DOS_BASED_FILE_SYSTEM
449 return strcasecmp (this->name
, rhs
.name
) < 0;
451 return strcmp (this->name
, rhs
.name
) < 0;
455 const char *name
; /* Source file name */
456 unsigned src
; /* Source file */
459 /* Vector of all functions. */
460 static vector
<function_info
*> functions
;
462 /* Function ident to function_info * map. */
463 static map
<unsigned, function_info
*> ident_to_fn
;
465 /* Vector of source files. */
466 static vector
<source_info
> sources
;
468 /* Mapping of file names to sources */
469 static vector
<name_map
> names
;
471 /* Record all processed files in order to warn about
472 a file being read multiple times. */
473 static vector
<char *> processed_files
;
475 /* This holds data summary information. */
477 static unsigned object_runs
;
479 static unsigned total_lines
;
480 static unsigned total_executed
;
482 /* Modification time of graph file. */
484 static time_t bbg_file_time
;
486 /* Name of the notes (gcno) output file. The "bbg" prefix is for
487 historical reasons, when the notes file contained only the
488 basic block graph notes. */
490 static char *bbg_file_name
;
492 /* Stamp of the bbg file */
493 static unsigned bbg_stamp
;
495 /* Supports has_unexecuted_blocks functionality. */
496 static unsigned bbg_supports_has_unexecuted_blocks
;
498 /* Working directory in which a TU was compiled. */
499 static const char *bbg_cwd
;
501 /* Name and file pointer of the input file for the count data (gcda). */
503 static char *da_file_name
;
505 /* Data file is missing. */
507 static int no_data_file
;
509 /* If there is several input files, compute and display results after
510 reading all data files. This way if two or more gcda file refer to
511 the same source file (eg inline subprograms in a .h file), the
514 static int multiple_files
= 0;
516 /* Output branch probabilities. */
518 static int flag_branches
= 0;
520 /* Show unconditional branches too. */
521 static int flag_unconditional
= 0;
523 /* Output a gcov file if this is true. This is on by default, and can
524 be turned off by the -n option. */
526 static int flag_gcov_file
= 1;
528 /* Output to stdout instead to a gcov file. */
530 static int flag_use_stdout
= 0;
532 /* Output progress indication if this is true. This is off by default
533 and can be turned on by the -d option. */
535 static int flag_display_progress
= 0;
537 /* Output *.gcov file in JSON intermediate format used by consumers. */
539 static int flag_json_format
= 0;
541 /* For included files, make the gcov output file name include the name
542 of the input source file. For example, if x.h is included in a.c,
543 then the output file name is a.c##x.h.gcov instead of x.h.gcov. */
545 static int flag_long_names
= 0;
547 /* For situations when a long name can potentially hit filesystem path limit,
548 let's calculate md5sum of the path and append it to a file name. */
550 static int flag_hash_filenames
= 0;
552 /* Print verbose informations. */
554 static int flag_verbose
= 0;
556 /* Print colored output. */
558 static int flag_use_colors
= 0;
560 /* Use perf-like colors to indicate hot lines. */
562 static int flag_use_hotness_colors
= 0;
564 /* Output count information for every basic block, not merely those
565 that contain line number information. */
567 static int flag_all_blocks
= 0;
569 /* Output human readable numbers. */
571 static int flag_human_readable_numbers
= 0;
573 /* Output summary info for each function. */
575 static int flag_function_summary
= 0;
577 /* Object directory file prefix. This is the directory/file where the
578 graph and data files are looked for, if nonzero. */
580 static char *object_directory
= 0;
582 /* Source directory prefix. This is removed from source pathnames
583 that match, when generating the output file name. */
585 static char *source_prefix
= 0;
586 static size_t source_length
= 0;
588 /* Only show data for sources with relative pathnames. Absolute ones
589 usually indicate a system header file, which although it may
590 contain inline functions, is usually uninteresting. */
591 static int flag_relative_only
= 0;
593 /* Preserve all pathname components. Needed when object files and
594 source files are in subdirectories. '/' is mangled as '#', '.' is
595 elided and '..' mangled to '^'. */
597 static int flag_preserve_paths
= 0;
599 /* Output the number of times a branch was taken as opposed to the percentage
600 of times it was taken. */
602 static int flag_counts
= 0;
604 /* Forward declarations. */
605 static int process_args (int, char **);
606 static void print_usage (int) ATTRIBUTE_NORETURN
;
607 static void print_version (void) ATTRIBUTE_NORETURN
;
608 static void process_file (const char *);
609 static void process_all_functions (void);
610 static void generate_results (const char *);
611 static void create_file_names (const char *);
612 static char *canonicalize_name (const char *);
613 static unsigned find_source (const char *);
614 static void read_graph_file (void);
615 static int read_count_file (void);
616 static void solve_flow_graph (function_info
*);
617 static void find_exception_blocks (function_info
*);
618 static void add_branch_counts (coverage_info
*, const arc_info
*);
619 static void add_line_counts (coverage_info
*, function_info
*);
620 static void executed_summary (unsigned, unsigned);
621 static void function_summary (const coverage_info
*);
622 static void file_summary (const coverage_info
*);
623 static const char *format_gcov (gcov_type
, gcov_type
, int);
624 static void accumulate_line_counts (source_info
*);
625 static void output_gcov_file (const char *, source_info
*);
626 static int output_branch_count (FILE *, int, const arc_info
*);
627 static void output_lines (FILE *, const source_info
*);
628 static char *make_gcov_file_name (const char *, const char *);
629 static char *mangle_name (const char *, char *);
630 static void release_structures (void);
631 extern int main (int, char **);
633 function_info::function_info (): m_name (NULL
), m_demangled_name (NULL
),
634 ident (0), lineno_checksum (0), cfg_checksum (0), has_catch (0),
635 artificial (0), is_group (0),
636 blocks (), blocks_executed (0), counts (),
637 start_line (0), start_column (0), end_line (0), end_column (0),
638 src (0), lines (), next (NULL
)
642 function_info::~function_info ()
644 for (int i
= blocks
.size () - 1; i
>= 0; i
--)
646 arc_info
*arc
, *arc_n
;
648 for (arc
= blocks
[i
].succ
; arc
; arc
= arc_n
)
650 arc_n
= arc
->succ_next
;
654 if (m_demangled_name
!= m_name
)
655 free (m_demangled_name
);
659 bool function_info::group_line_p (unsigned n
, unsigned src_idx
)
661 return is_group
&& src
== src_idx
&& start_line
<= n
&& n
<= end_line
;
665 There are a bajillion algorithms that do this. Boost's function is named
666 hawick_cycles, so I used the algorithm by K. A. Hawick and H. A. James in
667 "Enumerating Circuits and Loops in Graphs with Self-Arcs and Multiple-Arcs"
668 (url at <http://complexity.massey.ac.nz/cstn/013/cstn-013.pdf>).
670 The basic algorithm is simple: effectively, we're finding all simple paths
671 in a subgraph (that shrinks every iteration). Duplicates are filtered by
672 "blocking" a path when a node is added to the path (this also prevents non-
673 simple paths)--the node is unblocked only when it participates in a cycle.
676 typedef vector
<arc_info
*> arc_vector_t
;
677 typedef vector
<const block_info
*> block_vector_t
;
679 /* Handle cycle identified by EDGES, where the function finds minimum cs_count
680 and subtract the value from all counts. The subtracted value is added
681 to COUNT. Returns type of loop. */
684 handle_cycle (const arc_vector_t
&edges
, int64_t &count
)
686 /* Find the minimum edge of the cycle, and reduce all nodes in the cycle by
688 int64_t cycle_count
= INTTYPE_MAXIMUM (int64_t);
689 for (unsigned i
= 0; i
< edges
.size (); i
++)
691 int64_t ecount
= edges
[i
]->cs_count
;
692 if (cycle_count
> ecount
)
693 cycle_count
= ecount
;
695 count
+= cycle_count
;
696 for (unsigned i
= 0; i
< edges
.size (); i
++)
697 edges
[i
]->cs_count
-= cycle_count
;
699 gcc_assert (cycle_count
> 0);
702 /* Unblock a block U from BLOCKED. Apart from that, iterate all blocks
703 blocked by U in BLOCK_LISTS. */
706 unblock (const block_info
*u
, block_vector_t
&blocked
,
707 vector
<block_vector_t
> &block_lists
)
709 block_vector_t::iterator it
= find (blocked
.begin (), blocked
.end (), u
);
710 if (it
== blocked
.end ())
713 unsigned index
= it
- blocked
.begin ();
716 block_vector_t
to_unblock (block_lists
[index
]);
718 block_lists
.erase (block_lists
.begin () + index
);
720 for (block_vector_t::iterator it
= to_unblock
.begin ();
721 it
!= to_unblock
.end (); it
++)
722 unblock (*it
, blocked
, block_lists
);
725 /* Return true when PATH contains a zero cycle arc count. */
728 path_contains_zero_cycle_arc (arc_vector_t
&path
)
730 for (unsigned i
= 0; i
< path
.size (); i
++)
731 if (path
[i
]->cs_count
== 0)
736 /* Find circuit going to block V, PATH is provisional seen cycle.
737 BLOCKED is vector of blocked vertices, BLOCK_LISTS contains vertices
738 blocked by a block. COUNT is accumulated count of the current LINE.
739 Returns what type of loop it contains. */
742 circuit (block_info
*v
, arc_vector_t
&path
, block_info
*start
,
743 block_vector_t
&blocked
, vector
<block_vector_t
> &block_lists
,
744 line_info
&linfo
, int64_t &count
)
746 bool loop_found
= false;
748 /* Add v to the block list. */
749 gcc_assert (find (blocked
.begin (), blocked
.end (), v
) == blocked
.end ());
750 blocked
.push_back (v
);
751 block_lists
.push_back (block_vector_t ());
753 for (arc_info
*arc
= v
->succ
; arc
; arc
= arc
->succ_next
)
755 block_info
*w
= arc
->dst
;
757 || arc
->cs_count
== 0
758 || !linfo
.has_block (w
))
761 path
.push_back (arc
);
764 /* Cycle has been found. */
765 handle_cycle (path
, count
);
768 else if (!path_contains_zero_cycle_arc (path
)
769 && find (blocked
.begin (), blocked
.end (), w
) == blocked
.end ())
770 loop_found
|= circuit (w
, path
, start
, blocked
, block_lists
, linfo
,
777 unblock (v
, blocked
, block_lists
);
779 for (arc_info
*arc
= v
->succ
; arc
; arc
= arc
->succ_next
)
781 block_info
*w
= arc
->dst
;
783 || arc
->cs_count
== 0
784 || !linfo
.has_block (w
))
788 = find (blocked
.begin (), blocked
.end (), w
) - blocked
.begin ();
789 gcc_assert (index
< blocked
.size ());
790 block_vector_t
&list
= block_lists
[index
];
791 if (find (list
.begin (), list
.end (), v
) == list
.end ())
798 /* Find cycles for a LINFO. */
801 get_cycles_count (line_info
&linfo
)
803 /* Note that this algorithm works even if blocks aren't in sorted order.
804 Each iteration of the circuit detection is completely independent
805 (except for reducing counts, but that shouldn't matter anyways).
806 Therefore, operating on a permuted order (i.e., non-sorted) only
807 has the effect of permuting the output cycles. */
809 bool loop_found
= false;
811 for (vector
<block_info
*>::iterator it
= linfo
.blocks
.begin ();
812 it
!= linfo
.blocks
.end (); it
++)
815 block_vector_t blocked
;
816 vector
<block_vector_t
> block_lists
;
817 loop_found
|= circuit (*it
, path
, *it
, blocked
, block_lists
, linfo
,
825 main (int argc
, char **argv
)
831 p
= argv
[0] + strlen (argv
[0]);
832 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
836 xmalloc_set_program_name (progname
);
838 /* Unlock the stdio streams. */
839 unlock_std_streams ();
843 diagnostic_initialize (global_dc
, 0);
845 /* Handle response files. */
846 expandargv (&argc
, &argv
);
848 argno
= process_args (argc
, argv
);
852 if (argc
- argno
> 1)
857 for (; argno
!= argc
; argno
++)
859 if (flag_display_progress
)
860 printf ("Processing file %d out of %d\n", argno
- first_arg
+ 1,
862 process_file (argv
[argno
]);
864 if (flag_json_format
|| argno
== argc
- 1)
866 process_all_functions ();
867 generate_results (argv
[argno
]);
868 release_structures ();
875 /* Print a usage message and exit. If ERROR_P is nonzero, this is an error,
876 otherwise the output of --help. */
879 print_usage (int error_p
)
881 FILE *file
= error_p
? stderr
: stdout
;
882 int status
= error_p
? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
;
884 fnotice (file
, "Usage: gcov [OPTION...] SOURCE|OBJ...\n\n");
885 fnotice (file
, "Print code coverage information.\n\n");
886 fnotice (file
, " -a, --all-blocks Show information for every basic block\n");
887 fnotice (file
, " -b, --branch-probabilities Include branch probabilities in output\n");
888 fnotice (file
, " -c, --branch-counts Output counts of branches taken\n\
889 rather than percentages\n");
890 fnotice (file
, " -d, --display-progress Display progress information\n");
891 fnotice (file
, " -f, --function-summaries Output summaries for each function\n");
892 fnotice (file
, " -h, --help Print this help, then exit\n");
893 fnotice (file
, " -i, --json-format Output JSON intermediate format into .gcov.json.gz file\n");
894 fnotice (file
, " -j, --human-readable Output human readable numbers\n");
895 fnotice (file
, " -k, --use-colors Emit colored output\n");
896 fnotice (file
, " -l, --long-file-names Use long output file names for included\n\
898 fnotice (file
, " -m, --demangled-names Output demangled function names\n");
899 fnotice (file
, " -n, --no-output Do not create an output file\n");
900 fnotice (file
, " -o, --object-directory DIR|FILE Search for object files in DIR or called FILE\n");
901 fnotice (file
, " -p, --preserve-paths Preserve all pathname components\n");
902 fnotice (file
, " -q, --use-hotness-colors Emit perf-like colored output for hot lines\n");
903 fnotice (file
, " -r, --relative-only Only show data for relative sources\n");
904 fnotice (file
, " -s, --source-prefix DIR Source prefix to elide\n");
905 fnotice (file
, " -t, --stdout Output to stdout instead of a file\n");
906 fnotice (file
, " -u, --unconditional-branches Show unconditional branch counts too\n");
907 fnotice (file
, " -v, --version Print version number, then exit\n");
908 fnotice (file
, " -w, --verbose Print verbose informations\n");
909 fnotice (file
, " -x, --hash-filenames Hash long pathnames\n");
910 fnotice (file
, "\nFor bug reporting instructions, please see:\n%s.\n",
915 /* Print version information and exit. */
920 fnotice (stdout
, "gcov %s%s\n", pkgversion_string
, version_string
);
921 fprintf (stdout
, "Copyright %s 2019 Free Software Foundation, Inc.\n",
924 _("This is free software; see the source for copying conditions.\n"
925 "There is NO warranty; not even for MERCHANTABILITY or \n"
926 "FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
927 exit (SUCCESS_EXIT_CODE
);
930 static const struct option options
[] =
932 { "help", no_argument
, NULL
, 'h' },
933 { "version", no_argument
, NULL
, 'v' },
934 { "verbose", no_argument
, NULL
, 'w' },
935 { "all-blocks", no_argument
, NULL
, 'a' },
936 { "branch-probabilities", no_argument
, NULL
, 'b' },
937 { "branch-counts", no_argument
, NULL
, 'c' },
938 { "json-format", no_argument
, NULL
, 'i' },
939 { "human-readable", no_argument
, NULL
, 'j' },
940 { "no-output", no_argument
, NULL
, 'n' },
941 { "long-file-names", no_argument
, NULL
, 'l' },
942 { "function-summaries", no_argument
, NULL
, 'f' },
943 { "demangled-names", no_argument
, NULL
, 'm' },
944 { "preserve-paths", no_argument
, NULL
, 'p' },
945 { "relative-only", no_argument
, NULL
, 'r' },
946 { "object-directory", required_argument
, NULL
, 'o' },
947 { "object-file", required_argument
, NULL
, 'o' },
948 { "source-prefix", required_argument
, NULL
, 's' },
949 { "stdout", no_argument
, NULL
, 't' },
950 { "unconditional-branches", no_argument
, NULL
, 'u' },
951 { "display-progress", no_argument
, NULL
, 'd' },
952 { "hash-filenames", no_argument
, NULL
, 'x' },
953 { "use-colors", no_argument
, NULL
, 'k' },
954 { "use-hotness-colors", no_argument
, NULL
, 'q' },
958 /* Process args, return index to first non-arg. */
961 process_args (int argc
, char **argv
)
965 const char *opts
= "abcdfhijklmno:pqrs:tuvwx";
966 while ((opt
= getopt_long (argc
, argv
, opts
, options
, NULL
)) != -1)
980 flag_function_summary
= 1;
984 /* print_usage will exit. */
989 flag_human_readable_numbers
= 1;
995 flag_use_hotness_colors
= 1;
998 flag_demangled_names
= 1;
1004 object_directory
= optarg
;
1007 source_prefix
= optarg
;
1008 source_length
= strlen (source_prefix
);
1011 flag_relative_only
= 1;
1014 flag_preserve_paths
= 1;
1017 flag_unconditional
= 1;
1020 flag_json_format
= 1;
1024 flag_display_progress
= 1;
1027 flag_hash_filenames
= 1;
1033 flag_use_stdout
= 1;
1037 /* print_version will exit. */
1040 /* print_usage will exit. */
1047 /* Output intermediate LINE sitting on LINE_NUM to JSON OBJECT.
1048 Add FUNCTION_NAME to the LINE. */
1051 output_intermediate_json_line (json::array
*object
,
1052 line_info
*line
, unsigned line_num
,
1053 const char *function_name
)
1058 json::object
*lineo
= new json::object ();
1059 lineo
->set ("line_number", new json::number (line_num
));
1060 if (function_name
!= NULL
)
1061 lineo
->set ("function_name", new json::string (function_name
));
1062 lineo
->set ("count", new json::number (line
->count
));
1063 lineo
->set ("unexecuted_block",
1064 new json::literal (line
->has_unexecuted_block
));
1066 json::array
*branches
= new json::array ();
1067 lineo
->set ("branches", branches
);
1069 vector
<arc_info
*>::const_iterator it
;
1071 for (it
= line
->branches
.begin (); it
!= line
->branches
.end ();
1074 if (!(*it
)->is_unconditional
&& !(*it
)->is_call_non_return
)
1076 json::object
*branch
= new json::object ();
1077 branch
->set ("count", new json::number ((*it
)->count
));
1078 branch
->set ("throw", new json::literal ((*it
)->is_throw
));
1079 branch
->set ("fallthrough",
1080 new json::literal ((*it
)->fall_through
));
1081 branches
->append (branch
);
1085 object
->append (lineo
);
1088 /* Get the name of the gcov file. The return value must be free'd.
1090 It appends the '.gcov' extension to the *basename* of the file.
1091 The resulting file name will be in PWD.
1094 input: foo.da, output: foo.da.gcov
1095 input: a/b/foo.cc, output: foo.cc.gcov */
1098 get_gcov_intermediate_filename (const char *file_name
)
1100 const char *gcov
= ".gcov.json.gz";
1104 /* Find the 'basename'. */
1105 cptr
= lbasename (file_name
);
1107 result
= XNEWVEC (char, strlen (cptr
) + strlen (gcov
) + 1);
1108 sprintf (result
, "%s%s", cptr
, gcov
);
1113 /* Output the result in JSON intermediate format.
1114 Source info SRC is dumped into JSON_FILES which is JSON array. */
1117 output_json_intermediate_file (json::array
*json_files
, source_info
*src
)
1119 json::object
*root
= new json::object ();
1120 json_files
->append (root
);
1122 root
->set ("file", new json::string (src
->name
));
1124 json::array
*functions
= new json::array ();
1125 root
->set ("functions", functions
);
1127 std::sort (src
->functions
.begin (), src
->functions
.end (),
1128 function_line_start_cmp ());
1129 for (vector
<function_info
*>::iterator it
= src
->functions
.begin ();
1130 it
!= src
->functions
.end (); it
++)
1132 json::object
*function
= new json::object ();
1133 function
->set ("name", new json::string ((*it
)->m_name
));
1134 function
->set ("demangled_name",
1135 new json::string ((*it
)->get_demangled_name ()));
1136 function
->set ("start_line", new json::number ((*it
)->start_line
));
1137 function
->set ("start_column", new json::number ((*it
)->start_column
));
1138 function
->set ("end_line", new json::number ((*it
)->end_line
));
1139 function
->set ("end_column", new json::number ((*it
)->end_column
));
1140 function
->set ("blocks",
1141 new json::number ((*it
)->get_block_count ()));
1142 function
->set ("blocks_executed",
1143 new json::number ((*it
)->blocks_executed
));
1144 function
->set ("execution_count",
1145 new json::number ((*it
)->blocks
[0].count
));
1147 functions
->append (function
);
1150 json::array
*lineso
= new json::array ();
1151 root
->set ("lines", lineso
);
1153 function_info
*last_non_group_fn
= NULL
;
1155 for (unsigned line_num
= 1; line_num
<= src
->lines
.size (); line_num
++)
1157 vector
<function_info
*> *fns
= src
->get_functions_at_location (line_num
);
1160 /* Print first group functions that begin on the line. */
1161 for (vector
<function_info
*>::iterator it2
= fns
->begin ();
1162 it2
!= fns
->end (); it2
++)
1164 if (!(*it2
)->is_group
)
1165 last_non_group_fn
= *it2
;
1167 vector
<line_info
> &lines
= (*it2
)->lines
;
1168 for (unsigned i
= 0; i
< lines
.size (); i
++)
1170 line_info
*line
= &lines
[i
];
1171 output_intermediate_json_line (lineso
, line
, line_num
+ i
,
1176 /* Follow with lines associated with the source file. */
1177 if (line_num
< src
->lines
.size ())
1178 output_intermediate_json_line (lineso
, &src
->lines
[line_num
], line_num
,
1179 (last_non_group_fn
!= NULL
1180 ? last_non_group_fn
->m_name
: NULL
));
1184 /* Function start pair. */
1185 struct function_start
1187 unsigned source_file_idx
;
1188 unsigned start_line
;
1191 /* Traits class for function start hash maps below. */
1193 struct function_start_pair_hash
: typed_noop_remove
<function_start
>
1195 typedef function_start value_type
;
1196 typedef function_start compare_type
;
1199 hash (const function_start
&ref
)
1201 inchash::hash
hstate (0);
1202 hstate
.add_int (ref
.source_file_idx
);
1203 hstate
.add_int (ref
.start_line
);
1204 return hstate
.end ();
1208 equal (const function_start
&ref1
, const function_start
&ref2
)
1210 return (ref1
.source_file_idx
== ref2
.source_file_idx
1211 && ref1
.start_line
== ref2
.start_line
);
1215 mark_deleted (function_start
&ref
)
1217 ref
.start_line
= ~1U;
1221 mark_empty (function_start
&ref
)
1223 ref
.start_line
= ~2U;
1227 is_deleted (const function_start
&ref
)
1229 return ref
.start_line
== ~1U;
1233 is_empty (const function_start
&ref
)
1235 return ref
.start_line
== ~2U;
1239 /* Process a single input file. */
1242 process_file (const char *file_name
)
1244 create_file_names (file_name
);
1246 for (unsigned i
= 0; i
< processed_files
.size (); i
++)
1247 if (strcmp (da_file_name
, processed_files
[i
]) == 0)
1249 fnotice (stderr
, "'%s' file is already processed\n",
1254 processed_files
.push_back (xstrdup (da_file_name
));
1260 /* Process all functions in all files. */
1263 process_all_functions (void)
1265 hash_map
<function_start_pair_hash
, function_info
*> fn_map
;
1267 /* Identify group functions. */
1268 for (vector
<function_info
*>::iterator it
= functions
.begin ();
1269 it
!= functions
.end (); it
++)
1270 if (!(*it
)->artificial
)
1272 function_start needle
;
1273 needle
.source_file_idx
= (*it
)->src
;
1274 needle
.start_line
= (*it
)->start_line
;
1276 function_info
**slot
= fn_map
.get (needle
);
1279 (*slot
)->is_group
= 1;
1280 (*it
)->is_group
= 1;
1283 fn_map
.put (needle
, *it
);
1286 /* Remove all artificial function. */
1287 functions
.erase (remove_if (functions
.begin (), functions
.end (),
1288 function_info::is_artificial
), functions
.end ());
1290 for (vector
<function_info
*>::iterator it
= functions
.begin ();
1291 it
!= functions
.end (); it
++)
1293 function_info
*fn
= *it
;
1294 unsigned src
= fn
->src
;
1296 if (!fn
->counts
.empty () || no_data_file
)
1298 source_info
*s
= &sources
[src
];
1299 s
->add_function (fn
);
1301 /* Mark last line in files touched by function. */
1302 for (unsigned block_no
= 0; block_no
!= fn
->blocks
.size ();
1305 block_info
*block
= &fn
->blocks
[block_no
];
1306 for (unsigned i
= 0; i
< block
->locations
.size (); i
++)
1308 /* Sort lines of locations. */
1309 sort (block
->locations
[i
].lines
.begin (),
1310 block
->locations
[i
].lines
.end ());
1312 if (!block
->locations
[i
].lines
.empty ())
1314 s
= &sources
[block
->locations
[i
].source_file_idx
];
1316 = block
->locations
[i
].lines
.back ();
1318 /* Record new lines for the function. */
1319 if (last_line
>= s
->lines
.size ())
1321 s
= &sources
[block
->locations
[i
].source_file_idx
];
1323 = block
->locations
[i
].lines
.back ();
1325 /* Record new lines for the function. */
1326 if (last_line
>= s
->lines
.size ())
1328 /* Record new lines for a source file. */
1329 s
->lines
.resize (last_line
+ 1);
1336 /* Allocate lines for group function, following start_line
1337 and end_line information of the function. */
1339 fn
->lines
.resize (fn
->end_line
- fn
->start_line
+ 1);
1341 solve_flow_graph (fn
);
1343 find_exception_blocks (fn
);
1347 /* The function was not in the executable -- some other
1348 instance must have been selected. */
1354 output_gcov_file (const char *file_name
, source_info
*src
)
1356 char *gcov_file_name
= make_gcov_file_name (file_name
, src
->coverage
.name
);
1358 if (src
->coverage
.lines
)
1360 FILE *gcov_file
= fopen (gcov_file_name
, "w");
1363 fnotice (stdout
, "Creating '%s'\n", gcov_file_name
);
1364 output_lines (gcov_file
, src
);
1365 if (ferror (gcov_file
))
1366 fnotice (stderr
, "Error writing output file '%s'\n",
1371 fnotice (stderr
, "Could not open output file '%s'\n", gcov_file_name
);
1375 unlink (gcov_file_name
);
1376 fnotice (stdout
, "Removing '%s'\n", gcov_file_name
);
1378 free (gcov_file_name
);
1382 generate_results (const char *file_name
)
1384 char *gcov_intermediate_filename
;
1386 for (vector
<function_info
*>::iterator it
= functions
.begin ();
1387 it
!= functions
.end (); it
++)
1389 function_info
*fn
= *it
;
1390 coverage_info coverage
;
1392 memset (&coverage
, 0, sizeof (coverage
));
1393 coverage
.name
= fn
->get_name ();
1394 add_line_counts (flag_function_summary
? &coverage
: NULL
, fn
);
1395 if (flag_function_summary
)
1397 function_summary (&coverage
);
1398 fnotice (stdout
, "\n");
1406 needle
.name
= file_name
;
1407 vector
<name_map
>::iterator it
= std::find (names
.begin (), names
.end (),
1409 if (it
!= names
.end ())
1410 file_name
= sources
[it
->src
].coverage
.name
;
1412 file_name
= canonicalize_name (file_name
);
1415 gcov_intermediate_filename
= get_gcov_intermediate_filename (file_name
);
1417 json::object
*root
= new json::object ();
1418 root
->set ("format_version", new json::string ("1"));
1419 root
->set ("gcc_version", new json::string (version_string
));
1421 if (bbg_cwd
!= NULL
)
1422 root
->set ("current_working_directory", new json::string (bbg_cwd
));
1423 root
->set ("data_file", new json::string (file_name
));
1425 json::array
*json_files
= new json::array ();
1426 root
->set ("files", json_files
);
1428 for (vector
<source_info
>::iterator it
= sources
.begin ();
1429 it
!= sources
.end (); it
++)
1431 source_info
*src
= &(*it
);
1432 if (flag_relative_only
)
1434 /* Ignore this source, if it is an absolute path (after
1435 source prefix removal). */
1436 char first
= src
->coverage
.name
[0];
1438 #if HAVE_DOS_BASED_FILE_SYSTEM
1439 if (first
&& src
->coverage
.name
[1] == ':')
1440 first
= src
->coverage
.name
[2];
1442 if (IS_DIR_SEPARATOR (first
))
1446 accumulate_line_counts (src
);
1448 if (!flag_use_stdout
)
1449 file_summary (&src
->coverage
);
1450 total_lines
+= src
->coverage
.lines
;
1451 total_executed
+= src
->coverage
.lines_executed
;
1454 if (flag_json_format
)
1455 output_json_intermediate_file (json_files
, src
);
1458 if (flag_use_stdout
)
1460 if (src
->coverage
.lines
)
1461 output_lines (stdout
, src
);
1465 output_gcov_file (file_name
, src
);
1466 fnotice (stdout
, "\n");
1472 if (flag_gcov_file
&& flag_json_format
)
1474 if (flag_use_stdout
)
1476 root
->dump (stdout
);
1483 pp_formatted_text (&pp
);
1485 gzFile output
= gzopen (gcov_intermediate_filename
, "w");
1488 fnotice (stderr
, "Cannot open JSON output file %s\n",
1489 gcov_intermediate_filename
);
1493 if (gzputs (output
, pp_formatted_text (&pp
)) == EOF
1494 || gzclose (output
))
1496 fnotice (stderr
, "Error writing JSON output file %s\n",
1497 gcov_intermediate_filename
);
1504 executed_summary (total_lines
, total_executed
);
1507 /* Release all memory used. */
1510 release_structures (void)
1512 for (vector
<function_info
*>::iterator it
= functions
.begin ();
1513 it
!= functions
.end (); it
++)
1518 functions
.resize (0);
1519 ident_to_fn
.clear ();
1522 /* Generate the names of the graph and data files. If OBJECT_DIRECTORY
1523 is not specified, these are named from FILE_NAME sans extension. If
1524 OBJECT_DIRECTORY is specified and is a directory, the files are in that
1525 directory, but named from the basename of the FILE_NAME, sans extension.
1526 Otherwise OBJECT_DIRECTORY is taken to be the name of the object *file*
1527 and the data files are named from that. */
1530 create_file_names (const char *file_name
)
1534 int length
= strlen (file_name
);
1537 /* Free previous file names. */
1538 free (bbg_file_name
);
1539 free (da_file_name
);
1540 da_file_name
= bbg_file_name
= NULL
;
1544 if (object_directory
&& object_directory
[0])
1548 length
+= strlen (object_directory
) + 2;
1549 name
= XNEWVEC (char, length
);
1552 base
= !stat (object_directory
, &status
) && S_ISDIR (status
.st_mode
);
1553 strcat (name
, object_directory
);
1554 if (base
&& (!IS_DIR_SEPARATOR (name
[strlen (name
) - 1])))
1559 name
= XNEWVEC (char, length
+ 1);
1560 strcpy (name
, file_name
);
1566 /* Append source file name. */
1567 const char *cptr
= lbasename (file_name
);
1568 strcat (name
, cptr
? cptr
: file_name
);
1571 /* Remove the extension. */
1572 cptr
= strrchr (CONST_CAST (char *, lbasename (name
)), '.');
1576 length
= strlen (name
);
1578 bbg_file_name
= XNEWVEC (char, length
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
1579 strcpy (bbg_file_name
, name
);
1580 strcpy (bbg_file_name
+ length
, GCOV_NOTE_SUFFIX
);
1582 da_file_name
= XNEWVEC (char, length
+ strlen (GCOV_DATA_SUFFIX
) + 1);
1583 strcpy (da_file_name
, name
);
1584 strcpy (da_file_name
+ length
, GCOV_DATA_SUFFIX
);
1590 /* Find or create a source file structure for FILE_NAME. Copies
1591 FILE_NAME on creation */
1594 find_source (const char *file_name
)
1601 file_name
= "<unknown>";
1604 needle
.name
= file_name
;
1606 vector
<name_map
>::iterator it
= std::find (names
.begin (), names
.end (),
1608 if (it
!= names
.end ())
1614 /* Not found, try the canonical name. */
1615 canon
= canonicalize_name (file_name
);
1616 needle
.name
= canon
;
1617 it
= std::find (names
.begin (), names
.end (), needle
);
1618 if (it
== names
.end ())
1620 /* Not found with canonical name, create a new source. */
1623 idx
= sources
.size ();
1624 needle
= name_map (canon
, idx
);
1625 names
.push_back (needle
);
1627 sources
.push_back (source_info ());
1628 src
= &sources
.back ();
1630 src
->coverage
.name
= src
->name
;
1633 #if HAVE_DOS_BASED_FILE_SYSTEM
1634 /* You lose if separators don't match exactly in the
1636 && !strncasecmp (source_prefix
, src
->coverage
.name
, source_length
)
1638 && !strncmp (source_prefix
, src
->coverage
.name
, source_length
)
1640 && IS_DIR_SEPARATOR (src
->coverage
.name
[source_length
]))
1641 src
->coverage
.name
+= source_length
+ 1;
1642 if (!stat (src
->name
, &status
))
1643 src
->file_time
= status
.st_mtime
;
1648 needle
.name
= file_name
;
1649 if (std::find (names
.begin (), names
.end (), needle
) == names
.end ())
1651 /* Append the non-canonical name. */
1652 names
.push_back (name_map (xstrdup (file_name
), idx
));
1655 /* Resort the name map. */
1656 std::sort (names
.begin (), names
.end ());
1659 if (sources
[idx
].file_time
> bbg_file_time
)
1661 static int info_emitted
;
1663 fnotice (stderr
, "%s:source file is newer than notes file '%s'\n",
1664 file_name
, bbg_file_name
);
1668 "(the message is displayed only once per source file)\n");
1671 sources
[idx
].file_time
= 0;
1677 /* Read the notes file. Save functions to FUNCTIONS global vector. */
1680 read_graph_file (void)
1683 unsigned current_tag
= 0;
1686 if (!gcov_open (bbg_file_name
, 1))
1688 fnotice (stderr
, "%s:cannot open notes file\n", bbg_file_name
);
1691 bbg_file_time
= gcov_time ();
1692 if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC
))
1694 fnotice (stderr
, "%s:not a gcov notes file\n", bbg_file_name
);
1699 version
= gcov_read_unsigned ();
1700 if (version
!= GCOV_VERSION
)
1704 GCOV_UNSIGNED2STRING (v
, version
);
1705 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1707 fnotice (stderr
, "%s:version '%.4s', prefer '%.4s'\n",
1708 bbg_file_name
, v
, e
);
1710 bbg_stamp
= gcov_read_unsigned ();
1711 bbg_cwd
= xstrdup (gcov_read_string ());
1712 bbg_supports_has_unexecuted_blocks
= gcov_read_unsigned ();
1714 function_info
*fn
= NULL
;
1715 while ((tag
= gcov_read_unsigned ()))
1717 unsigned length
= gcov_read_unsigned ();
1718 gcov_position_t base
= gcov_position ();
1720 if (tag
== GCOV_TAG_FUNCTION
)
1722 char *function_name
;
1724 unsigned lineno_checksum
, cfg_checksum
;
1726 ident
= gcov_read_unsigned ();
1727 lineno_checksum
= gcov_read_unsigned ();
1728 cfg_checksum
= gcov_read_unsigned ();
1729 function_name
= xstrdup (gcov_read_string ());
1730 unsigned artificial
= gcov_read_unsigned ();
1731 unsigned src_idx
= find_source (gcov_read_string ());
1732 unsigned start_line
= gcov_read_unsigned ();
1733 unsigned start_column
= gcov_read_unsigned ();
1734 unsigned end_line
= gcov_read_unsigned ();
1735 unsigned end_column
= gcov_read_unsigned ();
1737 fn
= new function_info ();
1738 functions
.push_back (fn
);
1739 ident_to_fn
[ident
] = fn
;
1741 fn
->m_name
= function_name
;
1743 fn
->lineno_checksum
= lineno_checksum
;
1744 fn
->cfg_checksum
= cfg_checksum
;
1746 fn
->start_line
= start_line
;
1747 fn
->start_column
= start_column
;
1748 fn
->end_line
= end_line
;
1749 fn
->end_column
= end_column
;
1750 fn
->artificial
= artificial
;
1754 else if (fn
&& tag
== GCOV_TAG_BLOCKS
)
1756 if (!fn
->blocks
.empty ())
1757 fnotice (stderr
, "%s:already seen blocks for '%s'\n",
1758 bbg_file_name
, fn
->get_name ());
1760 fn
->blocks
.resize (gcov_read_unsigned ());
1762 else if (fn
&& tag
== GCOV_TAG_ARCS
)
1764 unsigned src
= gcov_read_unsigned ();
1765 fn
->blocks
[src
].id
= src
;
1766 unsigned num_dests
= GCOV_TAG_ARCS_NUM (length
);
1767 block_info
*src_blk
= &fn
->blocks
[src
];
1768 unsigned mark_catches
= 0;
1769 struct arc_info
*arc
;
1771 if (src
>= fn
->blocks
.size () || fn
->blocks
[src
].succ
)
1776 unsigned dest
= gcov_read_unsigned ();
1777 unsigned flags
= gcov_read_unsigned ();
1779 if (dest
>= fn
->blocks
.size ())
1781 arc
= XCNEW (arc_info
);
1783 arc
->dst
= &fn
->blocks
[dest
];
1787 arc
->count_valid
= 0;
1788 arc
->on_tree
= !!(flags
& GCOV_ARC_ON_TREE
);
1789 arc
->fake
= !!(flags
& GCOV_ARC_FAKE
);
1790 arc
->fall_through
= !!(flags
& GCOV_ARC_FALLTHROUGH
);
1792 arc
->succ_next
= src_blk
->succ
;
1793 src_blk
->succ
= arc
;
1794 src_blk
->num_succ
++;
1796 arc
->pred_next
= fn
->blocks
[dest
].pred
;
1797 fn
->blocks
[dest
].pred
= arc
;
1798 fn
->blocks
[dest
].num_pred
++;
1804 /* Exceptional exit from this function, the
1805 source block must be a call. */
1806 fn
->blocks
[src
].is_call_site
= 1;
1807 arc
->is_call_non_return
= 1;
1812 /* Non-local return from a callee of this
1813 function. The destination block is a setjmp. */
1814 arc
->is_nonlocal_return
= 1;
1815 fn
->blocks
[dest
].is_nonlocal_return
= 1;
1820 fn
->counts
.push_back (0);
1825 /* We have a fake exit from this block. The other
1826 non-fall through exits must be to catch handlers.
1827 Mark them as catch arcs. */
1829 for (arc
= src_blk
->succ
; arc
; arc
= arc
->succ_next
)
1830 if (!arc
->fake
&& !arc
->fall_through
)
1837 else if (fn
&& tag
== GCOV_TAG_LINES
)
1839 unsigned blockno
= gcov_read_unsigned ();
1840 block_info
*block
= &fn
->blocks
[blockno
];
1842 if (blockno
>= fn
->blocks
.size ())
1847 unsigned lineno
= gcov_read_unsigned ();
1850 block
->locations
.back ().lines
.push_back (lineno
);
1853 const char *file_name
= gcov_read_string ();
1857 block
->locations
.push_back (block_location_info
1858 (find_source (file_name
)));
1862 else if (current_tag
&& !GCOV_TAG_IS_SUBTAG (current_tag
, tag
))
1867 gcov_sync (base
, length
);
1868 if (gcov_is_error ())
1871 fnotice (stderr
, "%s:corrupted\n", bbg_file_name
);
1877 if (functions
.empty ())
1878 fnotice (stderr
, "%s:no functions found\n", bbg_file_name
);
1881 /* Reads profiles from the count file and attach to each
1882 function. Return nonzero if fatal error. */
1885 read_count_file (void)
1890 function_info
*fn
= NULL
;
1892 map
<unsigned, function_info
*>::iterator it
;
1894 if (!gcov_open (da_file_name
, 1))
1896 fnotice (stderr
, "%s:cannot open data file, assuming not executed\n",
1901 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
1903 fnotice (stderr
, "%s:not a gcov data file\n", da_file_name
);
1908 version
= gcov_read_unsigned ();
1909 if (version
!= GCOV_VERSION
)
1913 GCOV_UNSIGNED2STRING (v
, version
);
1914 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1916 fnotice (stderr
, "%s:version '%.4s', prefer version '%.4s'\n",
1917 da_file_name
, v
, e
);
1919 tag
= gcov_read_unsigned ();
1920 if (tag
!= bbg_stamp
)
1922 fnotice (stderr
, "%s:stamp mismatch with notes file\n", da_file_name
);
1926 while ((tag
= gcov_read_unsigned ()))
1928 unsigned length
= gcov_read_unsigned ();
1929 unsigned long base
= gcov_position ();
1931 if (tag
== GCOV_TAG_OBJECT_SUMMARY
)
1933 struct gcov_summary summary
;
1934 gcov_read_summary (&summary
);
1935 object_runs
= summary
.runs
;
1937 else if (tag
== GCOV_TAG_FUNCTION
&& !length
)
1939 else if (tag
== GCOV_TAG_FUNCTION
&& length
== GCOV_TAG_FUNCTION_LENGTH
)
1942 ident
= gcov_read_unsigned ();
1944 it
= ident_to_fn
.find (ident
);
1945 if (it
!= ident_to_fn
.end ())
1950 else if (gcov_read_unsigned () != fn
->lineno_checksum
1951 || gcov_read_unsigned () != fn
->cfg_checksum
)
1954 fnotice (stderr
, "%s:profile mismatch for '%s'\n",
1955 da_file_name
, fn
->get_name ());
1959 else if (tag
== GCOV_TAG_FOR_COUNTER (GCOV_COUNTER_ARCS
) && fn
)
1961 if (length
!= GCOV_TAG_COUNTER_LENGTH (fn
->counts
.size ()))
1964 for (ix
= 0; ix
!= fn
->counts
.size (); ix
++)
1965 fn
->counts
[ix
] += gcov_read_counter ();
1967 gcov_sync (base
, length
);
1968 if ((error
= gcov_is_error ()))
1972 ? N_("%s:overflowed\n")
1973 : N_("%s:corrupted\n"),
1983 /* Solve the flow graph. Propagate counts from the instrumented arcs
1984 to the blocks and the uninstrumented arcs. */
1987 solve_flow_graph (function_info
*fn
)
1991 gcov_type
*count_ptr
= &fn
->counts
.front ();
1993 block_info
*valid_blocks
= NULL
; /* valid, but unpropagated blocks. */
1994 block_info
*invalid_blocks
= NULL
; /* invalid, but inferable blocks. */
1996 /* The arcs were built in reverse order. Fix that now. */
1997 for (ix
= fn
->blocks
.size (); ix
--;)
1999 arc_info
*arc_p
, *arc_n
;
2001 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].succ
; arc
;
2002 arc_p
= arc
, arc
= arc_n
)
2004 arc_n
= arc
->succ_next
;
2005 arc
->succ_next
= arc_p
;
2007 fn
->blocks
[ix
].succ
= arc_p
;
2009 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].pred
; arc
;
2010 arc_p
= arc
, arc
= arc_n
)
2012 arc_n
= arc
->pred_next
;
2013 arc
->pred_next
= arc_p
;
2015 fn
->blocks
[ix
].pred
= arc_p
;
2018 if (fn
->blocks
.size () < 2)
2019 fnotice (stderr
, "%s:'%s' lacks entry and/or exit blocks\n",
2020 bbg_file_name
, fn
->get_name ());
2023 if (fn
->blocks
[ENTRY_BLOCK
].num_pred
)
2024 fnotice (stderr
, "%s:'%s' has arcs to entry block\n",
2025 bbg_file_name
, fn
->get_name ());
2027 /* We can't deduce the entry block counts from the lack of
2029 fn
->blocks
[ENTRY_BLOCK
].num_pred
= ~(unsigned)0;
2031 if (fn
->blocks
[EXIT_BLOCK
].num_succ
)
2032 fnotice (stderr
, "%s:'%s' has arcs from exit block\n",
2033 bbg_file_name
, fn
->get_name ());
2035 /* Likewise, we can't deduce exit block counts from the lack
2036 of its successors. */
2037 fn
->blocks
[EXIT_BLOCK
].num_succ
= ~(unsigned)0;
2040 /* Propagate the measured counts, this must be done in the same
2041 order as the code in profile.c */
2042 for (unsigned i
= 0; i
< fn
->blocks
.size (); i
++)
2044 blk
= &fn
->blocks
[i
];
2045 block_info
const *prev_dst
= NULL
;
2046 int out_of_order
= 0;
2047 int non_fake_succ
= 0;
2049 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
2057 arc
->count
= *count_ptr
++;
2058 arc
->count_valid
= 1;
2060 arc
->dst
->num_pred
--;
2062 if (prev_dst
&& prev_dst
> arc
->dst
)
2064 prev_dst
= arc
->dst
;
2066 if (non_fake_succ
== 1)
2068 /* If there is only one non-fake exit, it is an
2069 unconditional branch. */
2070 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
2073 arc
->is_unconditional
= 1;
2074 /* If this block is instrumenting a call, it might be
2075 an artificial block. It is not artificial if it has
2076 a non-fallthrough exit, or the destination of this
2077 arc has more than one entry. Mark the destination
2078 block as a return site, if none of those conditions
2080 if (blk
->is_call_site
&& arc
->fall_through
2081 && arc
->dst
->pred
== arc
&& !arc
->pred_next
)
2082 arc
->dst
->is_call_return
= 1;
2086 /* Sort the successor arcs into ascending dst order. profile.c
2087 normally produces arcs in the right order, but sometimes with
2088 one or two out of order. We're not using a particularly
2092 arc_info
*start
= blk
->succ
;
2093 unsigned changes
= 1;
2097 arc_info
*arc
, *arc_p
, *arc_n
;
2100 for (arc_p
= NULL
, arc
= start
; (arc_n
= arc
->succ_next
);)
2102 if (arc
->dst
> arc_n
->dst
)
2106 arc_p
->succ_next
= arc_n
;
2109 arc
->succ_next
= arc_n
->succ_next
;
2110 arc_n
->succ_next
= arc
;
2123 /* Place it on the invalid chain, it will be ignored if that's
2125 blk
->invalid_chain
= 1;
2126 blk
->chain
= invalid_blocks
;
2127 invalid_blocks
= blk
;
2130 while (invalid_blocks
|| valid_blocks
)
2132 while ((blk
= invalid_blocks
))
2134 gcov_type total
= 0;
2135 const arc_info
*arc
;
2137 invalid_blocks
= blk
->chain
;
2138 blk
->invalid_chain
= 0;
2140 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
2141 total
+= arc
->count
;
2142 else if (!blk
->num_pred
)
2143 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
2144 total
+= arc
->count
;
2149 blk
->count_valid
= 1;
2150 blk
->chain
= valid_blocks
;
2151 blk
->valid_chain
= 1;
2154 while ((blk
= valid_blocks
))
2157 arc_info
*arc
, *inv_arc
;
2159 valid_blocks
= blk
->chain
;
2160 blk
->valid_chain
= 0;
2161 if (blk
->num_succ
== 1)
2167 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
2169 total
-= arc
->count
;
2170 if (!arc
->count_valid
)
2174 inv_arc
->count_valid
= 1;
2175 inv_arc
->count
= total
;
2178 if (dst
->count_valid
)
2180 if (dst
->num_pred
== 1 && !dst
->valid_chain
)
2182 dst
->chain
= valid_blocks
;
2183 dst
->valid_chain
= 1;
2189 if (!dst
->num_pred
&& !dst
->invalid_chain
)
2191 dst
->chain
= invalid_blocks
;
2192 dst
->invalid_chain
= 1;
2193 invalid_blocks
= dst
;
2197 if (blk
->num_pred
== 1)
2203 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
2205 total
-= arc
->count
;
2206 if (!arc
->count_valid
)
2210 inv_arc
->count_valid
= 1;
2211 inv_arc
->count
= total
;
2214 if (src
->count_valid
)
2216 if (src
->num_succ
== 1 && !src
->valid_chain
)
2218 src
->chain
= valid_blocks
;
2219 src
->valid_chain
= 1;
2225 if (!src
->num_succ
&& !src
->invalid_chain
)
2227 src
->chain
= invalid_blocks
;
2228 src
->invalid_chain
= 1;
2229 invalid_blocks
= src
;
2236 /* If the graph has been correctly solved, every block will have a
2238 for (unsigned i
= 0; ix
< fn
->blocks
.size (); i
++)
2239 if (!fn
->blocks
[i
].count_valid
)
2241 fnotice (stderr
, "%s:graph is unsolvable for '%s'\n",
2242 bbg_file_name
, fn
->get_name ());
2247 /* Mark all the blocks only reachable via an incoming catch. */
2250 find_exception_blocks (function_info
*fn
)
2253 block_info
**queue
= XALLOCAVEC (block_info
*, fn
->blocks
.size ());
2255 /* First mark all blocks as exceptional. */
2256 for (ix
= fn
->blocks
.size (); ix
--;)
2257 fn
->blocks
[ix
].exceptional
= 1;
2259 /* Now mark all the blocks reachable via non-fake edges */
2260 queue
[0] = &fn
->blocks
[0];
2261 queue
[0]->exceptional
= 0;
2264 block_info
*block
= queue
[--ix
];
2265 const arc_info
*arc
;
2267 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2268 if (!arc
->fake
&& !arc
->is_throw
&& arc
->dst
->exceptional
)
2270 arc
->dst
->exceptional
= 0;
2271 queue
[ix
++] = arc
->dst
;
2277 /* Increment totals in COVERAGE according to arc ARC. */
2280 add_branch_counts (coverage_info
*coverage
, const arc_info
*arc
)
2282 if (arc
->is_call_non_return
)
2285 if (arc
->src
->count
)
2286 coverage
->calls_executed
++;
2288 else if (!arc
->is_unconditional
)
2290 coverage
->branches
++;
2291 if (arc
->src
->count
)
2292 coverage
->branches_executed
++;
2294 coverage
->branches_taken
++;
2298 /* Format COUNT, if flag_human_readable_numbers is set, return it human
2302 format_count (gcov_type count
)
2304 static char buffer
[64];
2305 const char *units
= " kMGTPEZY";
2307 if (count
< 1000 || !flag_human_readable_numbers
)
2309 sprintf (buffer
, "%" PRId64
, count
);
2314 gcov_type divisor
= 1;
2315 for (i
= 0; units
[i
+1]; i
++, divisor
*= 1000)
2317 if (count
+ divisor
/ 2 < 1000 * divisor
)
2320 float r
= 1.0f
* count
/ divisor
;
2321 sprintf (buffer
, "%.1f%c", r
, units
[i
]);
2325 /* Format a GCOV_TYPE integer as either a percent ratio, or absolute
2326 count. If DECIMAL_PLACES >= 0, format TOP/BOTTOM * 100 to DECIMAL_PLACES.
2327 If DECIMAL_PLACES is zero, no decimal point is printed. Only print 100% when
2328 TOP==BOTTOM and only print 0% when TOP=0. If DECIMAL_PLACES < 0, then simply
2329 format TOP. Return pointer to a static string. */
2332 format_gcov (gcov_type top
, gcov_type bottom
, int decimal_places
)
2334 static char buffer
[20];
2336 if (decimal_places
>= 0)
2338 float ratio
= bottom
? 100.0f
* top
/ bottom
: 0;
2340 /* Round up to 1% if there's a small non-zero value. */
2341 if (ratio
> 0.0f
&& ratio
< 0.5f
&& decimal_places
== 0)
2343 sprintf (buffer
, "%.*f%%", decimal_places
, ratio
);
2346 return format_count (top
);
2351 /* Summary of execution */
2354 executed_summary (unsigned lines
, unsigned executed
)
2357 fnotice (stdout
, "Lines executed:%s of %d\n",
2358 format_gcov (executed
, lines
, 2), lines
);
2360 fnotice (stdout
, "No executable lines\n");
2363 /* Output summary info for a function. */
2366 function_summary (const coverage_info
*coverage
)
2368 fnotice (stdout
, "%s '%s'\n", "Function", coverage
->name
);
2369 executed_summary (coverage
->lines
, coverage
->lines_executed
);
2372 /* Output summary info for a file. */
2375 file_summary (const coverage_info
*coverage
)
2377 fnotice (stdout
, "%s '%s'\n", "File", coverage
->name
);
2378 executed_summary (coverage
->lines
, coverage
->lines_executed
);
2382 if (coverage
->branches
)
2384 fnotice (stdout
, "Branches executed:%s of %d\n",
2385 format_gcov (coverage
->branches_executed
,
2386 coverage
->branches
, 2),
2387 coverage
->branches
);
2388 fnotice (stdout
, "Taken at least once:%s of %d\n",
2389 format_gcov (coverage
->branches_taken
,
2390 coverage
->branches
, 2),
2391 coverage
->branches
);
2394 fnotice (stdout
, "No branches\n");
2395 if (coverage
->calls
)
2396 fnotice (stdout
, "Calls executed:%s of %d\n",
2397 format_gcov (coverage
->calls_executed
, coverage
->calls
, 2),
2400 fnotice (stdout
, "No calls\n");
2404 /* Canonicalize the filename NAME by canonicalizing directory
2405 separators, eliding . components and resolving .. components
2406 appropriately. Always returns a unique string. */
2409 canonicalize_name (const char *name
)
2411 /* The canonical name cannot be longer than the incoming name. */
2412 char *result
= XNEWVEC (char, strlen (name
) + 1);
2413 const char *base
= name
, *probe
;
2418 #if HAVE_DOS_BASED_FILE_SYSTEM
2419 if (base
[0] && base
[1] == ':')
2421 result
[0] = base
[0];
2427 for (dd_base
= ptr
; *base
; base
= probe
)
2431 for (probe
= base
; *probe
; probe
++)
2432 if (IS_DIR_SEPARATOR (*probe
))
2436 if (len
== 1 && base
[0] == '.')
2437 /* Elide a '.' directory */
2439 else if (len
== 2 && base
[0] == '.' && base
[1] == '.')
2441 /* '..', we can only elide it and the previous directory, if
2442 we're not a symlink. */
2443 struct stat ATTRIBUTE_UNUSED buf
;
2447 #if defined (S_ISLNK)
2448 /* S_ISLNK is not POSIX.1-1996. */
2449 || stat (result
, &buf
) || S_ISLNK (buf
.st_mode
)
2453 /* Cannot elide, or unreadable or a symlink. */
2454 dd_base
= ptr
+ 2 + slash
;
2457 while (ptr
!= dd_base
&& *ptr
!= '/')
2459 slash
= ptr
!= result
;
2464 /* Regular pathname component. */
2467 memcpy (ptr
, base
, len
);
2472 for (; IS_DIR_SEPARATOR (*probe
); probe
++)
2480 /* Print hex representation of 16 bytes from SUM and write it to BUFFER. */
2483 md5sum_to_hex (const char *sum
, char *buffer
)
2485 for (unsigned i
= 0; i
< 16; i
++)
2486 sprintf (buffer
+ (2 * i
), "%02x", (unsigned char)sum
[i
]);
2489 /* Generate an output file name. INPUT_NAME is the canonicalized main
2490 input file and SRC_NAME is the canonicalized file name.
2491 LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
2492 long_output_names we prepend the processed name of the input file
2493 to each output name (except when the current source file is the
2494 input file, so you don't get a double concatenation). The two
2495 components are separated by '##'. With preserve_paths we create a
2496 filename from all path components of the source file, replacing '/'
2497 with '#', and .. with '^', without it we simply take the basename
2498 component. (Remember, the canonicalized name will already have
2499 elided '.' components and converted \\ separators.) */
2502 make_gcov_file_name (const char *input_name
, const char *src_name
)
2507 if (flag_long_names
&& input_name
&& strcmp (src_name
, input_name
))
2509 /* Generate the input filename part. */
2510 result
= XNEWVEC (char, strlen (input_name
) + strlen (src_name
) + 10);
2513 ptr
= mangle_name (input_name
, ptr
);
2514 ptr
[0] = ptr
[1] = '#';
2519 result
= XNEWVEC (char, strlen (src_name
) + 10);
2523 ptr
= mangle_name (src_name
, ptr
);
2524 strcpy (ptr
, ".gcov");
2526 /* When hashing filenames, we shorten them by only using the filename
2527 component and appending a hash of the full (mangled) pathname. */
2528 if (flag_hash_filenames
)
2532 char md5sum_hex
[33];
2534 md5_init_ctx (&ctx
);
2535 md5_process_bytes (src_name
, strlen (src_name
), &ctx
);
2536 md5_finish_ctx (&ctx
, md5sum
);
2537 md5sum_to_hex (md5sum
, md5sum_hex
);
2540 result
= XNEWVEC (char, strlen (src_name
) + 50);
2542 ptr
= mangle_name (src_name
, ptr
);
2543 ptr
[0] = ptr
[1] = '#';
2545 memcpy (ptr
, md5sum_hex
, 32);
2547 strcpy (ptr
, ".gcov");
2553 /* Mangle BASE name, copy it at the beginning of PTR buffer and
2554 return address of the \0 character of the buffer. */
2557 mangle_name (char const *base
, char *ptr
)
2561 /* Generate the source filename part. */
2562 if (!flag_preserve_paths
)
2563 base
= lbasename (base
);
2565 base
= mangle_path (base
);
2567 len
= strlen (base
);
2568 memcpy (ptr
, base
, len
);
2574 /* Scan through the bb_data for each line in the block, increment
2575 the line number execution count indicated by the execution count of
2576 the appropriate basic block. */
2579 add_line_counts (coverage_info
*coverage
, function_info
*fn
)
2581 bool has_any_line
= false;
2582 /* Scan each basic block. */
2583 for (unsigned ix
= 0; ix
!= fn
->blocks
.size (); ix
++)
2585 line_info
*line
= NULL
;
2586 block_info
*block
= &fn
->blocks
[ix
];
2587 if (block
->count
&& ix
&& ix
+ 1 != fn
->blocks
.size ())
2588 fn
->blocks_executed
++;
2589 for (unsigned i
= 0; i
< block
->locations
.size (); i
++)
2591 unsigned src_idx
= block
->locations
[i
].source_file_idx
;
2592 vector
<unsigned> &lines
= block
->locations
[i
].lines
;
2594 block
->cycle
.arc
= NULL
;
2595 block
->cycle
.ident
= ~0U;
2597 for (unsigned j
= 0; j
< lines
.size (); j
++)
2599 unsigned ln
= lines
[j
];
2601 /* Line belongs to a function that is in a group. */
2602 if (fn
->group_line_p (ln
, src_idx
))
2604 gcc_assert (lines
[j
] - fn
->start_line
< fn
->lines
.size ());
2605 line
= &(fn
->lines
[lines
[j
] - fn
->start_line
]);
2607 if (!block
->exceptional
)
2609 line
->unexceptional
= 1;
2610 if (block
->count
== 0)
2611 line
->has_unexecuted_block
= 1;
2613 line
->count
+= block
->count
;
2617 gcc_assert (ln
< sources
[src_idx
].lines
.size ());
2618 line
= &(sources
[src_idx
].lines
[ln
]);
2623 if (!line
->count
&& block
->count
)
2624 coverage
->lines_executed
++;
2627 if (!block
->exceptional
)
2629 line
->unexceptional
= 1;
2630 if (block
->count
== 0)
2631 line
->has_unexecuted_block
= 1;
2633 line
->count
+= block
->count
;
2637 has_any_line
= true;
2639 if (!ix
|| ix
+ 1 == fn
->blocks
.size ())
2640 /* Entry or exit block. */;
2641 else if (line
!= NULL
)
2643 line
->blocks
.push_back (block
);
2649 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2650 line
->branches
.push_back (arc
);
2657 fnotice (stderr
, "%s:no lines for '%s'\n", bbg_file_name
,
2661 /* Accumulate info for LINE that belongs to SRC source file. If ADD_COVERAGE
2662 is set to true, update source file summary. */
2664 static void accumulate_line_info (line_info
*line
, source_info
*src
,
2668 for (vector
<arc_info
*>::iterator it
= line
->branches
.begin ();
2669 it
!= line
->branches
.end (); it
++)
2670 add_branch_counts (&src
->coverage
, *it
);
2672 if (!line
->blocks
.empty ())
2674 /* The user expects the line count to be the number of times
2675 a line has been executed. Simply summing the block count
2676 will give an artificially high number. The Right Thing
2677 is to sum the entry counts to the graph of blocks on this
2678 line, then find the elementary cycles of the local graph
2679 and add the transition counts of those cycles. */
2680 gcov_type count
= 0;
2682 /* Cycle detection. */
2683 for (vector
<block_info
*>::iterator it
= line
->blocks
.begin ();
2684 it
!= line
->blocks
.end (); it
++)
2686 for (arc_info
*arc
= (*it
)->pred
; arc
; arc
= arc
->pred_next
)
2687 if (!line
->has_block (arc
->src
))
2688 count
+= arc
->count
;
2689 for (arc_info
*arc
= (*it
)->succ
; arc
; arc
= arc
->succ_next
)
2690 arc
->cs_count
= arc
->count
;
2693 /* Now, add the count of loops entirely on this line. */
2694 count
+= get_cycles_count (*line
);
2695 line
->count
= count
;
2697 if (line
->count
> src
->maximum_count
)
2698 src
->maximum_count
= line
->count
;
2701 if (line
->exists
&& add_coverage
)
2703 src
->coverage
.lines
++;
2705 src
->coverage
.lines_executed
++;
2709 /* Accumulate the line counts of a file. */
2712 accumulate_line_counts (source_info
*src
)
2714 /* First work on group functions. */
2715 for (vector
<function_info
*>::iterator it
= src
->functions
.begin ();
2716 it
!= src
->functions
.end (); it
++)
2718 function_info
*fn
= *it
;
2720 if (fn
->src
!= src
->index
|| !fn
->is_group
)
2723 for (vector
<line_info
>::iterator it2
= fn
->lines
.begin ();
2724 it2
!= fn
->lines
.end (); it2
++)
2726 line_info
*line
= &(*it2
);
2727 accumulate_line_info (line
, src
, false);
2731 /* Work on global lines that line in source file SRC. */
2732 for (vector
<line_info
>::iterator it
= src
->lines
.begin ();
2733 it
!= src
->lines
.end (); it
++)
2734 accumulate_line_info (&(*it
), src
, true);
2736 /* If not using intermediate mode, sum lines of group functions and
2737 add them to lines that live in a source file. */
2738 if (!flag_json_format
)
2739 for (vector
<function_info
*>::iterator it
= src
->functions
.begin ();
2740 it
!= src
->functions
.end (); it
++)
2742 function_info
*fn
= *it
;
2744 if (fn
->src
!= src
->index
|| !fn
->is_group
)
2747 for (unsigned i
= 0; i
< fn
->lines
.size (); i
++)
2749 line_info
*fn_line
= &fn
->lines
[i
];
2750 if (fn_line
->exists
)
2752 unsigned ln
= fn
->start_line
+ i
;
2753 line_info
*src_line
= &src
->lines
[ln
];
2755 if (!src_line
->exists
)
2756 src
->coverage
.lines
++;
2757 if (!src_line
->count
&& fn_line
->count
)
2758 src
->coverage
.lines_executed
++;
2760 src_line
->count
+= fn_line
->count
;
2761 src_line
->exists
= 1;
2763 if (fn_line
->has_unexecuted_block
)
2764 src_line
->has_unexecuted_block
= 1;
2766 if (fn_line
->unexceptional
)
2767 src_line
->unexceptional
= 1;
2773 /* Output information about ARC number IX. Returns nonzero if
2774 anything is output. */
2777 output_branch_count (FILE *gcov_file
, int ix
, const arc_info
*arc
)
2779 if (arc
->is_call_non_return
)
2781 if (arc
->src
->count
)
2783 fnotice (gcov_file
, "call %2d returned %s\n", ix
,
2784 format_gcov (arc
->src
->count
- arc
->count
,
2785 arc
->src
->count
, -flag_counts
));
2788 fnotice (gcov_file
, "call %2d never executed\n", ix
);
2790 else if (!arc
->is_unconditional
)
2792 if (arc
->src
->count
)
2793 fnotice (gcov_file
, "branch %2d taken %s%s", ix
,
2794 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
),
2795 arc
->fall_through
? " (fallthrough)"
2796 : arc
->is_throw
? " (throw)" : "");
2798 fnotice (gcov_file
, "branch %2d never executed", ix
);
2801 fnotice (gcov_file
, " (BB %d)", arc
->dst
->id
);
2803 fnotice (gcov_file
, "\n");
2805 else if (flag_unconditional
&& !arc
->dst
->is_call_return
)
2807 if (arc
->src
->count
)
2808 fnotice (gcov_file
, "unconditional %2d taken %s\n", ix
,
2809 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
));
2811 fnotice (gcov_file
, "unconditional %2d never executed\n", ix
);
2819 read_line (FILE *file
)
2821 static char *string
;
2822 static size_t string_len
;
2829 string
= XNEWVEC (char, string_len
);
2832 while ((ptr
= fgets (string
+ pos
, string_len
- pos
, file
)))
2834 size_t len
= strlen (string
+ pos
);
2836 if (len
&& string
[pos
+ len
- 1] == '\n')
2838 string
[pos
+ len
- 1] = 0;
2842 /* If the file contains NUL characters or an incomplete
2843 last line, which can happen more than once in one run,
2844 we have to avoid doubling the STRING_LEN unnecessarily. */
2845 if (pos
> string_len
/ 2)
2848 string
= XRESIZEVEC (char, string
, string_len
);
2852 return pos
? string
: NULL
;
2855 /* Pad string S with spaces from left to have total width equal to 9. */
2858 pad_count_string (string
&s
)
2861 s
.insert (0, 9 - s
.size (), ' ');
2864 /* Print GCOV line beginning to F stream. If EXISTS is set to true, the
2865 line exists in source file. UNEXCEPTIONAL indicated that it's not in
2866 an exceptional statement. The output is printed for LINE_NUM of given
2867 COUNT of executions. EXCEPTIONAL_STRING and UNEXCEPTIONAL_STRING are
2868 used to indicate non-executed blocks. */
2871 output_line_beginning (FILE *f
, bool exists
, bool unexceptional
,
2872 bool has_unexecuted_block
,
2873 gcov_type count
, unsigned line_num
,
2874 const char *exceptional_string
,
2875 const char *unexceptional_string
,
2876 unsigned int maximum_count
)
2883 s
= format_gcov (count
, 0, -1);
2884 if (has_unexecuted_block
2885 && bbg_supports_has_unexecuted_blocks
)
2887 if (flag_use_colors
)
2889 pad_count_string (s
);
2890 s
.insert (0, SGR_SEQ (COLOR_BG_MAGENTA
2891 COLOR_SEPARATOR COLOR_FG_WHITE
));
2897 pad_count_string (s
);
2901 if (flag_use_colors
)
2904 pad_count_string (s
);
2906 s
.insert (0, SGR_SEQ (COLOR_BG_RED
2907 COLOR_SEPARATOR COLOR_FG_WHITE
));
2909 s
.insert (0, SGR_SEQ (COLOR_BG_CYAN
2910 COLOR_SEPARATOR COLOR_FG_WHITE
));
2915 s
= unexceptional
? unexceptional_string
: exceptional_string
;
2916 pad_count_string (s
);
2923 pad_count_string (s
);
2926 /* Format line number in output. */
2928 sprintf (buffer
, "%5u", line_num
);
2929 string
linestr (buffer
);
2931 if (flag_use_hotness_colors
&& maximum_count
)
2933 if (count
* 2 > maximum_count
) /* > 50%. */
2934 linestr
.insert (0, SGR_SEQ (COLOR_BG_RED
));
2935 else if (count
* 5 > maximum_count
) /* > 20%. */
2936 linestr
.insert (0, SGR_SEQ (COLOR_BG_YELLOW
));
2937 else if (count
* 10 > maximum_count
) /* > 10%. */
2938 linestr
.insert (0, SGR_SEQ (COLOR_BG_GREEN
));
2939 linestr
+= SGR_RESET
;
2942 fprintf (f
, "%s:%s", s
.c_str (), linestr
.c_str ());
2946 print_source_line (FILE *f
, const vector
<const char *> &source_lines
,
2949 gcc_assert (line
>= 1);
2950 gcc_assert (line
<= source_lines
.size ());
2952 fprintf (f
, ":%s\n", source_lines
[line
- 1]);
2955 /* Output line details for LINE and print it to F file. LINE lives on
2959 output_line_details (FILE *f
, const line_info
*line
, unsigned line_num
)
2961 if (flag_all_blocks
)
2967 for (vector
<block_info
*>::const_iterator it
= line
->blocks
.begin ();
2968 it
!= line
->blocks
.end (); it
++)
2970 if (!(*it
)->is_call_return
)
2972 output_line_beginning (f
, line
->exists
,
2973 (*it
)->exceptional
, false,
2974 (*it
)->count
, line_num
,
2975 "%%%%%", "$$$$$", 0);
2976 fprintf (f
, "-block %2d", ix
++);
2978 fprintf (f
, " (BB %u)", (*it
)->id
);
2982 for (arc
= (*it
)->succ
; arc
; arc
= arc
->succ_next
)
2983 jx
+= output_branch_count (f
, jx
, arc
);
2986 else if (flag_branches
)
2991 for (vector
<arc_info
*>::const_iterator it
= line
->branches
.begin ();
2992 it
!= line
->branches
.end (); it
++)
2993 ix
+= output_branch_count (f
, ix
, (*it
));
2997 /* Output detail statistics about function FN to file F. */
3000 output_function_details (FILE *f
, function_info
*fn
)
3005 arc_info
*arc
= fn
->blocks
[EXIT_BLOCK
].pred
;
3006 gcov_type return_count
= fn
->blocks
[EXIT_BLOCK
].count
;
3007 gcov_type called_count
= fn
->blocks
[ENTRY_BLOCK
].count
;
3009 for (; arc
; arc
= arc
->pred_next
)
3011 return_count
-= arc
->count
;
3013 fprintf (f
, "function %s", fn
->get_name ());
3014 fprintf (f
, " called %s",
3015 format_gcov (called_count
, 0, -1));
3016 fprintf (f
, " returned %s",
3017 format_gcov (return_count
, called_count
, 0));
3018 fprintf (f
, " blocks executed %s",
3019 format_gcov (fn
->blocks_executed
, fn
->get_block_count (), 0));
3023 /* Read in the source file one line at a time, and output that line to
3024 the gcov file preceded by its execution count and other
3028 output_lines (FILE *gcov_file
, const source_info
*src
)
3030 #define DEFAULT_LINE_START " -: 0:"
3031 #define FN_SEPARATOR "------------------\n"
3036 /* Print colorization legend. */
3037 if (flag_use_colors
)
3038 fprintf (gcov_file
, "%s",
3039 DEFAULT_LINE_START
"Colorization: profile count: " \
3040 SGR_SEQ (COLOR_BG_CYAN
) "zero coverage (exceptional)" SGR_RESET \
3042 SGR_SEQ (COLOR_BG_RED
) "zero coverage (unexceptional)" SGR_RESET \
3044 SGR_SEQ (COLOR_BG_MAGENTA
) "unexecuted block" SGR_RESET
"\n");
3046 if (flag_use_hotness_colors
)
3047 fprintf (gcov_file
, "%s",
3048 DEFAULT_LINE_START
"Colorization: line numbers: hotness: " \
3049 SGR_SEQ (COLOR_BG_RED
) "> 50%" SGR_RESET
" " \
3050 SGR_SEQ (COLOR_BG_YELLOW
) "> 20%" SGR_RESET
" " \
3051 SGR_SEQ (COLOR_BG_GREEN
) "> 10%" SGR_RESET
"\n");
3053 fprintf (gcov_file
, DEFAULT_LINE_START
"Source:%s\n", src
->coverage
.name
);
3054 if (!multiple_files
)
3056 fprintf (gcov_file
, DEFAULT_LINE_START
"Graph:%s\n", bbg_file_name
);
3057 fprintf (gcov_file
, DEFAULT_LINE_START
"Data:%s\n",
3058 no_data_file
? "-" : da_file_name
);
3059 fprintf (gcov_file
, DEFAULT_LINE_START
"Runs:%u\n", object_runs
);
3062 source_file
= fopen (src
->name
, "r");
3064 fnotice (stderr
, "Cannot open source file %s\n", src
->name
);
3065 else if (src
->file_time
== 0)
3066 fprintf (gcov_file
, DEFAULT_LINE_START
"Source is newer than graph\n");
3068 vector
<const char *> source_lines
;
3070 while ((retval
= read_line (source_file
)) != NULL
)
3071 source_lines
.push_back (xstrdup (retval
));
3073 unsigned line_start_group
= 0;
3074 vector
<function_info
*> *fns
;
3076 for (unsigned line_num
= 1; line_num
<= source_lines
.size (); line_num
++)
3078 if (line_num
>= src
->lines
.size ())
3080 fprintf (gcov_file
, "%9s:%5u", "-", line_num
);
3081 print_source_line (gcov_file
, source_lines
, line_num
);
3085 const line_info
*line
= &src
->lines
[line_num
];
3087 if (line_start_group
== 0)
3089 fns
= src
->get_functions_at_location (line_num
);
3090 if (fns
!= NULL
&& fns
->size () > 1)
3092 /* It's possible to have functions that partially overlap,
3093 thus take the maximum end_line of functions starting
3095 for (unsigned i
= 0; i
< fns
->size (); i
++)
3096 if ((*fns
)[i
]->end_line
> line_start_group
)
3097 line_start_group
= (*fns
)[i
]->end_line
;
3099 else if (fns
!= NULL
&& fns
->size () == 1)
3101 function_info
*fn
= (*fns
)[0];
3102 output_function_details (gcov_file
, fn
);
3106 /* For lines which don't exist in the .bb file, print '-' before
3107 the source line. For lines which exist but were never
3108 executed, print '#####' or '=====' before the source line.
3109 Otherwise, print the execution count before the source line.
3110 There are 16 spaces of indentation added before the source
3111 line so that tabs won't be messed up. */
3112 output_line_beginning (gcov_file
, line
->exists
, line
->unexceptional
,
3113 line
->has_unexecuted_block
, line
->count
,
3114 line_num
, "=====", "#####", src
->maximum_count
);
3116 print_source_line (gcov_file
, source_lines
, line_num
);
3117 output_line_details (gcov_file
, line
, line_num
);
3119 if (line_start_group
== line_num
)
3121 for (vector
<function_info
*>::iterator it
= fns
->begin ();
3122 it
!= fns
->end (); it
++)
3124 function_info
*fn
= *it
;
3125 vector
<line_info
> &lines
= fn
->lines
;
3127 fprintf (gcov_file
, FN_SEPARATOR
);
3129 string fn_name
= fn
->get_name ();
3130 if (flag_use_colors
)
3132 fn_name
.insert (0, SGR_SEQ (COLOR_FG_CYAN
));
3133 fn_name
+= SGR_RESET
;
3136 fprintf (gcov_file
, "%s:\n", fn_name
.c_str ());
3138 output_function_details (gcov_file
, fn
);
3140 /* Print all lines covered by the function. */
3141 for (unsigned i
= 0; i
< lines
.size (); i
++)
3143 line_info
*line
= &lines
[i
];
3144 unsigned l
= fn
->start_line
+ i
;
3146 /* For lines which don't exist in the .bb file, print '-'
3147 before the source line. For lines which exist but
3148 were never executed, print '#####' or '=====' before
3149 the source line. Otherwise, print the execution count
3150 before the source line.
3151 There are 16 spaces of indentation added before the source
3152 line so that tabs won't be messed up. */
3153 output_line_beginning (gcov_file
, line
->exists
,
3154 line
->unexceptional
,
3155 line
->has_unexecuted_block
,
3157 l
, "=====", "#####",
3158 src
->maximum_count
);
3160 print_source_line (gcov_file
, source_lines
, l
);
3161 output_line_details (gcov_file
, line
, l
);
3165 fprintf (gcov_file
, FN_SEPARATOR
);
3166 line_start_group
= 0;
3171 fclose (source_file
);