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 /* Enum with types of loop in CFG. */
688 /* Loop_type operator that merges two values: A and B. */
690 inline loop_type
& operator |= (loop_type
& a
, loop_type b
)
692 return a
= static_cast<loop_type
> (a
| b
);
695 /* Handle cycle identified by EDGES, where the function finds minimum cs_count
696 and subtract the value from all counts. The subtracted value is added
697 to COUNT. Returns type of loop. */
700 handle_cycle (const arc_vector_t
&edges
, int64_t &count
)
702 /* Find the minimum edge of the cycle, and reduce all nodes in the cycle by
704 int64_t cycle_count
= INTTYPE_MAXIMUM (int64_t);
705 for (unsigned i
= 0; i
< edges
.size (); i
++)
707 int64_t ecount
= edges
[i
]->cs_count
;
708 if (cycle_count
> ecount
)
709 cycle_count
= ecount
;
711 count
+= cycle_count
;
712 for (unsigned i
= 0; i
< edges
.size (); i
++)
713 edges
[i
]->cs_count
-= cycle_count
;
715 return cycle_count
< 0 ? NEGATIVE_LOOP
: LOOP
;
718 /* Unblock a block U from BLOCKED. Apart from that, iterate all blocks
719 blocked by U in BLOCK_LISTS. */
722 unblock (const block_info
*u
, block_vector_t
&blocked
,
723 vector
<block_vector_t
> &block_lists
)
725 block_vector_t::iterator it
= find (blocked
.begin (), blocked
.end (), u
);
726 if (it
== blocked
.end ())
729 unsigned index
= it
- blocked
.begin ();
732 block_vector_t
to_unblock (block_lists
[index
]);
734 block_lists
.erase (block_lists
.begin () + index
);
736 for (block_vector_t::iterator it
= to_unblock
.begin ();
737 it
!= to_unblock
.end (); it
++)
738 unblock (*it
, blocked
, block_lists
);
741 /* Find circuit going to block V, PATH is provisional seen cycle.
742 BLOCKED is vector of blocked vertices, BLOCK_LISTS contains vertices
743 blocked by a block. COUNT is accumulated count of the current LINE.
744 Returns what type of loop it contains. */
747 circuit (block_info
*v
, arc_vector_t
&path
, block_info
*start
,
748 block_vector_t
&blocked
, vector
<block_vector_t
> &block_lists
,
749 line_info
&linfo
, int64_t &count
)
751 loop_type result
= NO_LOOP
;
753 /* Add v to the block list. */
754 gcc_assert (find (blocked
.begin (), blocked
.end (), v
) == blocked
.end ());
755 blocked
.push_back (v
);
756 block_lists
.push_back (block_vector_t ());
758 for (arc_info
*arc
= v
->succ
; arc
; arc
= arc
->succ_next
)
760 block_info
*w
= arc
->dst
;
761 if (w
< start
|| !linfo
.has_block (w
))
764 path
.push_back (arc
);
766 /* Cycle has been found. */
767 result
|= handle_cycle (path
, count
);
768 else if (find (blocked
.begin (), blocked
.end (), w
) == blocked
.end ())
769 result
|= circuit (w
, path
, start
, blocked
, block_lists
, linfo
, count
);
774 if (result
!= NO_LOOP
)
775 unblock (v
, blocked
, block_lists
);
777 for (arc_info
*arc
= v
->succ
; arc
; arc
= arc
->succ_next
)
779 block_info
*w
= arc
->dst
;
780 if (w
< start
|| !linfo
.has_block (w
))
784 = find (blocked
.begin (), blocked
.end (), w
) - blocked
.begin ();
785 gcc_assert (index
< blocked
.size ());
786 block_vector_t
&list
= block_lists
[index
];
787 if (find (list
.begin (), list
.end (), v
) == list
.end ())
794 /* Find cycles for a LINFO. If HANDLE_NEGATIVE_CYCLES is set and the line
795 contains a negative loop, then perform the same function once again. */
798 get_cycles_count (line_info
&linfo
, bool handle_negative_cycles
= true)
800 /* Note that this algorithm works even if blocks aren't in sorted order.
801 Each iteration of the circuit detection is completely independent
802 (except for reducing counts, but that shouldn't matter anyways).
803 Therefore, operating on a permuted order (i.e., non-sorted) only
804 has the effect of permuting the output cycles. */
806 loop_type result
= NO_LOOP
;
808 for (vector
<block_info
*>::iterator it
= linfo
.blocks
.begin ();
809 it
!= linfo
.blocks
.end (); it
++)
812 block_vector_t blocked
;
813 vector
<block_vector_t
> block_lists
;
814 result
|= circuit (*it
, path
, *it
, blocked
, block_lists
, linfo
,
818 /* If we have a negative cycle, repeat the find_cycles routine. */
819 if (result
== NEGATIVE_LOOP
&& handle_negative_cycles
)
820 count
+= get_cycles_count (linfo
, false);
826 main (int argc
, char **argv
)
832 p
= argv
[0] + strlen (argv
[0]);
833 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
837 xmalloc_set_program_name (progname
);
839 /* Unlock the stdio streams. */
840 unlock_std_streams ();
844 diagnostic_initialize (global_dc
, 0);
846 /* Handle response files. */
847 expandargv (&argc
, &argv
);
849 argno
= process_args (argc
, argv
);
853 if (argc
- argno
> 1)
858 for (; argno
!= argc
; argno
++)
860 if (flag_display_progress
)
861 printf ("Processing file %d out of %d\n", argno
- first_arg
+ 1,
863 process_file (argv
[argno
]);
865 if (flag_json_format
|| argno
== argc
- 1)
867 process_all_functions ();
868 generate_results (argv
[argno
]);
869 release_structures ();
876 /* Print a usage message and exit. If ERROR_P is nonzero, this is an error,
877 otherwise the output of --help. */
880 print_usage (int error_p
)
882 FILE *file
= error_p
? stderr
: stdout
;
883 int status
= error_p
? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
;
885 fnotice (file
, "Usage: gcov [OPTION...] SOURCE|OBJ...\n\n");
886 fnotice (file
, "Print code coverage information.\n\n");
887 fnotice (file
, " -a, --all-blocks Show information for every basic block\n");
888 fnotice (file
, " -b, --branch-probabilities Include branch probabilities in output\n");
889 fnotice (file
, " -c, --branch-counts Output counts of branches taken\n\
890 rather than percentages\n");
891 fnotice (file
, " -d, --display-progress Display progress information\n");
892 fnotice (file
, " -f, --function-summaries Output summaries for each function\n");
893 fnotice (file
, " -h, --help Print this help, then exit\n");
894 fnotice (file
, " -i, --json-format Output JSON intermediate format into .gcov.json.gz file\n");
895 fnotice (file
, " -j, --human-readable Output human readable numbers\n");
896 fnotice (file
, " -k, --use-colors Emit colored output\n");
897 fnotice (file
, " -l, --long-file-names Use long output file names for included\n\
899 fnotice (file
, " -m, --demangled-names Output demangled function names\n");
900 fnotice (file
, " -n, --no-output Do not create an output file\n");
901 fnotice (file
, " -o, --object-directory DIR|FILE Search for object files in DIR or called FILE\n");
902 fnotice (file
, " -p, --preserve-paths Preserve all pathname components\n");
903 fnotice (file
, " -q, --use-hotness-colors Emit perf-like colored output for hot lines\n");
904 fnotice (file
, " -r, --relative-only Only show data for relative sources\n");
905 fnotice (file
, " -s, --source-prefix DIR Source prefix to elide\n");
906 fnotice (file
, " -t, --stdout Output to stdout instead of a file\n");
907 fnotice (file
, " -u, --unconditional-branches Show unconditional branch counts too\n");
908 fnotice (file
, " -v, --version Print version number, then exit\n");
909 fnotice (file
, " -w, --verbose Print verbose informations\n");
910 fnotice (file
, " -x, --hash-filenames Hash long pathnames\n");
911 fnotice (file
, "\nFor bug reporting instructions, please see:\n%s.\n",
916 /* Print version information and exit. */
921 fnotice (stdout
, "gcov %s%s\n", pkgversion_string
, version_string
);
922 fprintf (stdout
, "Copyright %s 2019 Free Software Foundation, Inc.\n",
925 _("This is free software; see the source for copying conditions.\n"
926 "There is NO warranty; not even for MERCHANTABILITY or \n"
927 "FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
928 exit (SUCCESS_EXIT_CODE
);
931 static const struct option options
[] =
933 { "help", no_argument
, NULL
, 'h' },
934 { "version", no_argument
, NULL
, 'v' },
935 { "verbose", no_argument
, NULL
, 'w' },
936 { "all-blocks", no_argument
, NULL
, 'a' },
937 { "branch-probabilities", no_argument
, NULL
, 'b' },
938 { "branch-counts", no_argument
, NULL
, 'c' },
939 { "json-format", no_argument
, NULL
, 'i' },
940 { "human-readable", no_argument
, NULL
, 'j' },
941 { "no-output", no_argument
, NULL
, 'n' },
942 { "long-file-names", no_argument
, NULL
, 'l' },
943 { "function-summaries", no_argument
, NULL
, 'f' },
944 { "demangled-names", no_argument
, NULL
, 'm' },
945 { "preserve-paths", no_argument
, NULL
, 'p' },
946 { "relative-only", no_argument
, NULL
, 'r' },
947 { "object-directory", required_argument
, NULL
, 'o' },
948 { "object-file", required_argument
, NULL
, 'o' },
949 { "source-prefix", required_argument
, NULL
, 's' },
950 { "stdout", no_argument
, NULL
, 't' },
951 { "unconditional-branches", no_argument
, NULL
, 'u' },
952 { "display-progress", no_argument
, NULL
, 'd' },
953 { "hash-filenames", no_argument
, NULL
, 'x' },
954 { "use-colors", no_argument
, NULL
, 'k' },
955 { "use-hotness-colors", no_argument
, NULL
, 'q' },
959 /* Process args, return index to first non-arg. */
962 process_args (int argc
, char **argv
)
966 const char *opts
= "abcdfhijklmno:pqrs:tuvwx";
967 while ((opt
= getopt_long (argc
, argv
, opts
, options
, NULL
)) != -1)
981 flag_function_summary
= 1;
985 /* print_usage will exit. */
990 flag_human_readable_numbers
= 1;
996 flag_use_hotness_colors
= 1;
999 flag_demangled_names
= 1;
1005 object_directory
= optarg
;
1008 source_prefix
= optarg
;
1009 source_length
= strlen (source_prefix
);
1012 flag_relative_only
= 1;
1015 flag_preserve_paths
= 1;
1018 flag_unconditional
= 1;
1021 flag_json_format
= 1;
1025 flag_display_progress
= 1;
1028 flag_hash_filenames
= 1;
1034 flag_use_stdout
= 1;
1038 /* print_version will exit. */
1041 /* print_usage will exit. */
1048 /* Output intermediate LINE sitting on LINE_NUM to JSON OBJECT.
1049 Add FUNCTION_NAME to the LINE. */
1052 output_intermediate_json_line (json::array
*object
,
1053 line_info
*line
, unsigned line_num
,
1054 const char *function_name
)
1059 json::object
*lineo
= new json::object ();
1060 lineo
->set ("line_number", new json::number (line_num
));
1061 if (function_name
!= NULL
)
1062 lineo
->set ("function_name", new json::string (function_name
));
1063 lineo
->set ("count", new json::number (line
->count
));
1064 lineo
->set ("unexecuted_block",
1065 new json::literal (line
->has_unexecuted_block
));
1067 json::array
*branches
= new json::array ();
1068 lineo
->set ("branches", branches
);
1070 vector
<arc_info
*>::const_iterator it
;
1072 for (it
= line
->branches
.begin (); it
!= line
->branches
.end ();
1075 if (!(*it
)->is_unconditional
&& !(*it
)->is_call_non_return
)
1077 json::object
*branch
= new json::object ();
1078 branch
->set ("count", new json::number ((*it
)->count
));
1079 branch
->set ("throw", new json::literal ((*it
)->is_throw
));
1080 branch
->set ("fallthrough",
1081 new json::literal ((*it
)->fall_through
));
1082 branches
->append (branch
);
1086 object
->append (lineo
);
1089 /* Get the name of the gcov file. The return value must be free'd.
1091 It appends the '.gcov' extension to the *basename* of the file.
1092 The resulting file name will be in PWD.
1095 input: foo.da, output: foo.da.gcov
1096 input: a/b/foo.cc, output: foo.cc.gcov */
1099 get_gcov_intermediate_filename (const char *file_name
)
1101 const char *gcov
= ".gcov.json.gz";
1105 /* Find the 'basename'. */
1106 cptr
= lbasename (file_name
);
1108 result
= XNEWVEC (char, strlen (cptr
) + strlen (gcov
) + 1);
1109 sprintf (result
, "%s%s", cptr
, gcov
);
1114 /* Output the result in JSON intermediate format.
1115 Source info SRC is dumped into JSON_FILES which is JSON array. */
1118 output_json_intermediate_file (json::array
*json_files
, source_info
*src
)
1120 json::object
*root
= new json::object ();
1121 json_files
->append (root
);
1123 root
->set ("file", new json::string (src
->name
));
1125 json::array
*functions
= new json::array ();
1126 root
->set ("functions", functions
);
1128 std::sort (src
->functions
.begin (), src
->functions
.end (),
1129 function_line_start_cmp ());
1130 for (vector
<function_info
*>::iterator it
= src
->functions
.begin ();
1131 it
!= src
->functions
.end (); it
++)
1133 json::object
*function
= new json::object ();
1134 function
->set ("name", new json::string ((*it
)->m_name
));
1135 function
->set ("demangled_name",
1136 new json::string ((*it
)->get_demangled_name ()));
1137 function
->set ("start_line", new json::number ((*it
)->start_line
));
1138 function
->set ("start_column", new json::number ((*it
)->start_column
));
1139 function
->set ("end_line", new json::number ((*it
)->end_line
));
1140 function
->set ("end_column", new json::number ((*it
)->end_column
));
1141 function
->set ("blocks",
1142 new json::number ((*it
)->get_block_count ()));
1143 function
->set ("blocks_executed",
1144 new json::number ((*it
)->blocks_executed
));
1145 function
->set ("execution_count",
1146 new json::number ((*it
)->blocks
[0].count
));
1148 functions
->append (function
);
1151 json::array
*lineso
= new json::array ();
1152 root
->set ("lines", lineso
);
1154 function_info
*last_non_group_fn
= NULL
;
1156 for (unsigned line_num
= 1; line_num
<= src
->lines
.size (); line_num
++)
1158 vector
<function_info
*> *fns
= src
->get_functions_at_location (line_num
);
1161 /* Print first group functions that begin on the line. */
1162 for (vector
<function_info
*>::iterator it2
= fns
->begin ();
1163 it2
!= fns
->end (); it2
++)
1165 if (!(*it2
)->is_group
)
1166 last_non_group_fn
= *it2
;
1168 vector
<line_info
> &lines
= (*it2
)->lines
;
1169 for (unsigned i
= 0; i
< lines
.size (); i
++)
1171 line_info
*line
= &lines
[i
];
1172 output_intermediate_json_line (lineso
, line
, line_num
+ i
,
1177 /* Follow with lines associated with the source file. */
1178 if (line_num
< src
->lines
.size ())
1179 output_intermediate_json_line (lineso
, &src
->lines
[line_num
], line_num
,
1180 (last_non_group_fn
!= NULL
1181 ? last_non_group_fn
->m_name
: NULL
));
1185 /* Function start pair. */
1186 struct function_start
1188 unsigned source_file_idx
;
1189 unsigned start_line
;
1192 /* Traits class for function start hash maps below. */
1194 struct function_start_pair_hash
: typed_noop_remove
<function_start
>
1196 typedef function_start value_type
;
1197 typedef function_start compare_type
;
1200 hash (const function_start
&ref
)
1202 inchash::hash
hstate (0);
1203 hstate
.add_int (ref
.source_file_idx
);
1204 hstate
.add_int (ref
.start_line
);
1205 return hstate
.end ();
1209 equal (const function_start
&ref1
, const function_start
&ref2
)
1211 return (ref1
.source_file_idx
== ref2
.source_file_idx
1212 && ref1
.start_line
== ref2
.start_line
);
1216 mark_deleted (function_start
&ref
)
1218 ref
.start_line
= ~1U;
1222 mark_empty (function_start
&ref
)
1224 ref
.start_line
= ~2U;
1228 is_deleted (const function_start
&ref
)
1230 return ref
.start_line
== ~1U;
1234 is_empty (const function_start
&ref
)
1236 return ref
.start_line
== ~2U;
1240 /* Process a single input file. */
1243 process_file (const char *file_name
)
1245 create_file_names (file_name
);
1247 for (unsigned i
= 0; i
< processed_files
.size (); i
++)
1248 if (strcmp (da_file_name
, processed_files
[i
]) == 0)
1250 fnotice (stderr
, "'%s' file is already processed\n",
1255 processed_files
.push_back (xstrdup (da_file_name
));
1261 /* Process all functions in all files. */
1264 process_all_functions (void)
1266 hash_map
<function_start_pair_hash
, function_info
*> fn_map
;
1268 /* Identify group functions. */
1269 for (vector
<function_info
*>::iterator it
= functions
.begin ();
1270 it
!= functions
.end (); it
++)
1271 if (!(*it
)->artificial
)
1273 function_start needle
;
1274 needle
.source_file_idx
= (*it
)->src
;
1275 needle
.start_line
= (*it
)->start_line
;
1277 function_info
**slot
= fn_map
.get (needle
);
1280 (*slot
)->is_group
= 1;
1281 (*it
)->is_group
= 1;
1284 fn_map
.put (needle
, *it
);
1287 /* Remove all artificial function. */
1288 functions
.erase (remove_if (functions
.begin (), functions
.end (),
1289 function_info::is_artificial
), functions
.end ());
1291 for (vector
<function_info
*>::iterator it
= functions
.begin ();
1292 it
!= functions
.end (); it
++)
1294 function_info
*fn
= *it
;
1295 unsigned src
= fn
->src
;
1297 if (!fn
->counts
.empty () || no_data_file
)
1299 source_info
*s
= &sources
[src
];
1300 s
->add_function (fn
);
1302 /* Mark last line in files touched by function. */
1303 for (unsigned block_no
= 0; block_no
!= fn
->blocks
.size ();
1306 block_info
*block
= &fn
->blocks
[block_no
];
1307 for (unsigned i
= 0; i
< block
->locations
.size (); i
++)
1309 /* Sort lines of locations. */
1310 sort (block
->locations
[i
].lines
.begin (),
1311 block
->locations
[i
].lines
.end ());
1313 if (!block
->locations
[i
].lines
.empty ())
1315 s
= &sources
[block
->locations
[i
].source_file_idx
];
1317 = block
->locations
[i
].lines
.back ();
1319 /* Record new lines for the function. */
1320 if (last_line
>= s
->lines
.size ())
1322 s
= &sources
[block
->locations
[i
].source_file_idx
];
1324 = block
->locations
[i
].lines
.back ();
1326 /* Record new lines for the function. */
1327 if (last_line
>= s
->lines
.size ())
1329 /* Record new lines for a source file. */
1330 s
->lines
.resize (last_line
+ 1);
1337 /* Allocate lines for group function, following start_line
1338 and end_line information of the function. */
1340 fn
->lines
.resize (fn
->end_line
- fn
->start_line
+ 1);
1342 solve_flow_graph (fn
);
1344 find_exception_blocks (fn
);
1348 /* The function was not in the executable -- some other
1349 instance must have been selected. */
1355 output_gcov_file (const char *file_name
, source_info
*src
)
1357 char *gcov_file_name
= make_gcov_file_name (file_name
, src
->coverage
.name
);
1359 if (src
->coverage
.lines
)
1361 FILE *gcov_file
= fopen (gcov_file_name
, "w");
1364 fnotice (stdout
, "Creating '%s'\n", gcov_file_name
);
1365 output_lines (gcov_file
, src
);
1366 if (ferror (gcov_file
))
1367 fnotice (stderr
, "Error writing output file '%s'\n",
1372 fnotice (stderr
, "Could not open output file '%s'\n", gcov_file_name
);
1376 unlink (gcov_file_name
);
1377 fnotice (stdout
, "Removing '%s'\n", gcov_file_name
);
1379 free (gcov_file_name
);
1383 generate_results (const char *file_name
)
1385 char *gcov_intermediate_filename
;
1387 for (vector
<function_info
*>::iterator it
= functions
.begin ();
1388 it
!= functions
.end (); it
++)
1390 function_info
*fn
= *it
;
1391 coverage_info coverage
;
1393 memset (&coverage
, 0, sizeof (coverage
));
1394 coverage
.name
= fn
->get_name ();
1395 add_line_counts (flag_function_summary
? &coverage
: NULL
, fn
);
1396 if (flag_function_summary
)
1398 function_summary (&coverage
);
1399 fnotice (stdout
, "\n");
1407 needle
.name
= file_name
;
1408 vector
<name_map
>::iterator it
= std::find (names
.begin (), names
.end (),
1410 if (it
!= names
.end ())
1411 file_name
= sources
[it
->src
].coverage
.name
;
1413 file_name
= canonicalize_name (file_name
);
1416 gcov_intermediate_filename
= get_gcov_intermediate_filename (file_name
);
1418 json::object
*root
= new json::object ();
1419 root
->set ("format_version", new json::string ("1"));
1420 root
->set ("gcc_version", new json::string (version_string
));
1422 if (bbg_cwd
!= NULL
)
1423 root
->set ("current_working_directory", new json::string (bbg_cwd
));
1424 root
->set ("data_file", new json::string (file_name
));
1426 json::array
*json_files
= new json::array ();
1427 root
->set ("files", json_files
);
1429 for (vector
<source_info
>::iterator it
= sources
.begin ();
1430 it
!= sources
.end (); it
++)
1432 source_info
*src
= &(*it
);
1433 if (flag_relative_only
)
1435 /* Ignore this source, if it is an absolute path (after
1436 source prefix removal). */
1437 char first
= src
->coverage
.name
[0];
1439 #if HAVE_DOS_BASED_FILE_SYSTEM
1440 if (first
&& src
->coverage
.name
[1] == ':')
1441 first
= src
->coverage
.name
[2];
1443 if (IS_DIR_SEPARATOR (first
))
1447 accumulate_line_counts (src
);
1449 if (!flag_use_stdout
)
1450 file_summary (&src
->coverage
);
1451 total_lines
+= src
->coverage
.lines
;
1452 total_executed
+= src
->coverage
.lines_executed
;
1455 if (flag_json_format
)
1456 output_json_intermediate_file (json_files
, src
);
1459 if (flag_use_stdout
)
1461 if (src
->coverage
.lines
)
1462 output_lines (stdout
, src
);
1466 output_gcov_file (file_name
, src
);
1467 fnotice (stdout
, "\n");
1473 if (flag_gcov_file
&& flag_json_format
)
1475 if (flag_use_stdout
)
1477 root
->dump (stdout
);
1484 pp_formatted_text (&pp
);
1486 gzFile output
= gzopen (gcov_intermediate_filename
, "w");
1489 fnotice (stderr
, "Cannot open JSON output file %s\n",
1490 gcov_intermediate_filename
);
1494 if (gzputs (output
, pp_formatted_text (&pp
)) == EOF
1495 || gzclose (output
))
1497 fnotice (stderr
, "Error writing JSON output file %s\n",
1498 gcov_intermediate_filename
);
1505 executed_summary (total_lines
, total_executed
);
1508 /* Release all memory used. */
1511 release_structures (void)
1513 for (vector
<function_info
*>::iterator it
= functions
.begin ();
1514 it
!= functions
.end (); it
++)
1519 functions
.resize (0);
1520 ident_to_fn
.clear ();
1523 /* Generate the names of the graph and data files. If OBJECT_DIRECTORY
1524 is not specified, these are named from FILE_NAME sans extension. If
1525 OBJECT_DIRECTORY is specified and is a directory, the files are in that
1526 directory, but named from the basename of the FILE_NAME, sans extension.
1527 Otherwise OBJECT_DIRECTORY is taken to be the name of the object *file*
1528 and the data files are named from that. */
1531 create_file_names (const char *file_name
)
1535 int length
= strlen (file_name
);
1538 /* Free previous file names. */
1539 free (bbg_file_name
);
1540 free (da_file_name
);
1541 da_file_name
= bbg_file_name
= NULL
;
1545 if (object_directory
&& object_directory
[0])
1549 length
+= strlen (object_directory
) + 2;
1550 name
= XNEWVEC (char, length
);
1553 base
= !stat (object_directory
, &status
) && S_ISDIR (status
.st_mode
);
1554 strcat (name
, object_directory
);
1555 if (base
&& (!IS_DIR_SEPARATOR (name
[strlen (name
) - 1])))
1560 name
= XNEWVEC (char, length
+ 1);
1561 strcpy (name
, file_name
);
1567 /* Append source file name. */
1568 const char *cptr
= lbasename (file_name
);
1569 strcat (name
, cptr
? cptr
: file_name
);
1572 /* Remove the extension. */
1573 cptr
= strrchr (CONST_CAST (char *, lbasename (name
)), '.');
1577 length
= strlen (name
);
1579 bbg_file_name
= XNEWVEC (char, length
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
1580 strcpy (bbg_file_name
, name
);
1581 strcpy (bbg_file_name
+ length
, GCOV_NOTE_SUFFIX
);
1583 da_file_name
= XNEWVEC (char, length
+ strlen (GCOV_DATA_SUFFIX
) + 1);
1584 strcpy (da_file_name
, name
);
1585 strcpy (da_file_name
+ length
, GCOV_DATA_SUFFIX
);
1591 /* Find or create a source file structure for FILE_NAME. Copies
1592 FILE_NAME on creation */
1595 find_source (const char *file_name
)
1602 file_name
= "<unknown>";
1605 needle
.name
= file_name
;
1607 vector
<name_map
>::iterator it
= std::find (names
.begin (), names
.end (),
1609 if (it
!= names
.end ())
1615 /* Not found, try the canonical name. */
1616 canon
= canonicalize_name (file_name
);
1617 needle
.name
= canon
;
1618 it
= std::find (names
.begin (), names
.end (), needle
);
1619 if (it
== names
.end ())
1621 /* Not found with canonical name, create a new source. */
1624 idx
= sources
.size ();
1625 needle
= name_map (canon
, idx
);
1626 names
.push_back (needle
);
1628 sources
.push_back (source_info ());
1629 src
= &sources
.back ();
1631 src
->coverage
.name
= src
->name
;
1634 #if HAVE_DOS_BASED_FILE_SYSTEM
1635 /* You lose if separators don't match exactly in the
1637 && !strncasecmp (source_prefix
, src
->coverage
.name
, source_length
)
1639 && !strncmp (source_prefix
, src
->coverage
.name
, source_length
)
1641 && IS_DIR_SEPARATOR (src
->coverage
.name
[source_length
]))
1642 src
->coverage
.name
+= source_length
+ 1;
1643 if (!stat (src
->name
, &status
))
1644 src
->file_time
= status
.st_mtime
;
1649 needle
.name
= file_name
;
1650 if (std::find (names
.begin (), names
.end (), needle
) == names
.end ())
1652 /* Append the non-canonical name. */
1653 names
.push_back (name_map (xstrdup (file_name
), idx
));
1656 /* Resort the name map. */
1657 std::sort (names
.begin (), names
.end ());
1660 if (sources
[idx
].file_time
> bbg_file_time
)
1662 static int info_emitted
;
1664 fnotice (stderr
, "%s:source file is newer than notes file '%s'\n",
1665 file_name
, bbg_file_name
);
1669 "(the message is displayed only once per source file)\n");
1672 sources
[idx
].file_time
= 0;
1678 /* Read the notes file. Save functions to FUNCTIONS global vector. */
1681 read_graph_file (void)
1684 unsigned current_tag
= 0;
1687 if (!gcov_open (bbg_file_name
, 1))
1689 fnotice (stderr
, "%s:cannot open notes file\n", bbg_file_name
);
1692 bbg_file_time
= gcov_time ();
1693 if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC
))
1695 fnotice (stderr
, "%s:not a gcov notes file\n", bbg_file_name
);
1700 version
= gcov_read_unsigned ();
1701 if (version
!= GCOV_VERSION
)
1705 GCOV_UNSIGNED2STRING (v
, version
);
1706 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1708 fnotice (stderr
, "%s:version '%.4s', prefer '%.4s'\n",
1709 bbg_file_name
, v
, e
);
1711 bbg_stamp
= gcov_read_unsigned ();
1712 bbg_cwd
= xstrdup (gcov_read_string ());
1713 bbg_supports_has_unexecuted_blocks
= gcov_read_unsigned ();
1715 function_info
*fn
= NULL
;
1716 while ((tag
= gcov_read_unsigned ()))
1718 unsigned length
= gcov_read_unsigned ();
1719 gcov_position_t base
= gcov_position ();
1721 if (tag
== GCOV_TAG_FUNCTION
)
1723 char *function_name
;
1725 unsigned lineno_checksum
, cfg_checksum
;
1727 ident
= gcov_read_unsigned ();
1728 lineno_checksum
= gcov_read_unsigned ();
1729 cfg_checksum
= gcov_read_unsigned ();
1730 function_name
= xstrdup (gcov_read_string ());
1731 unsigned artificial
= gcov_read_unsigned ();
1732 unsigned src_idx
= find_source (gcov_read_string ());
1733 unsigned start_line
= gcov_read_unsigned ();
1734 unsigned start_column
= gcov_read_unsigned ();
1735 unsigned end_line
= gcov_read_unsigned ();
1736 unsigned end_column
= gcov_read_unsigned ();
1738 fn
= new function_info ();
1739 functions
.push_back (fn
);
1740 ident_to_fn
[ident
] = fn
;
1742 fn
->m_name
= function_name
;
1744 fn
->lineno_checksum
= lineno_checksum
;
1745 fn
->cfg_checksum
= cfg_checksum
;
1747 fn
->start_line
= start_line
;
1748 fn
->start_column
= start_column
;
1749 fn
->end_line
= end_line
;
1750 fn
->end_column
= end_column
;
1751 fn
->artificial
= artificial
;
1755 else if (fn
&& tag
== GCOV_TAG_BLOCKS
)
1757 if (!fn
->blocks
.empty ())
1758 fnotice (stderr
, "%s:already seen blocks for '%s'\n",
1759 bbg_file_name
, fn
->get_name ());
1761 fn
->blocks
.resize (gcov_read_unsigned ());
1763 else if (fn
&& tag
== GCOV_TAG_ARCS
)
1765 unsigned src
= gcov_read_unsigned ();
1766 fn
->blocks
[src
].id
= src
;
1767 unsigned num_dests
= GCOV_TAG_ARCS_NUM (length
);
1768 block_info
*src_blk
= &fn
->blocks
[src
];
1769 unsigned mark_catches
= 0;
1770 struct arc_info
*arc
;
1772 if (src
>= fn
->blocks
.size () || fn
->blocks
[src
].succ
)
1777 unsigned dest
= gcov_read_unsigned ();
1778 unsigned flags
= gcov_read_unsigned ();
1780 if (dest
>= fn
->blocks
.size ())
1782 arc
= XCNEW (arc_info
);
1784 arc
->dst
= &fn
->blocks
[dest
];
1788 arc
->count_valid
= 0;
1789 arc
->on_tree
= !!(flags
& GCOV_ARC_ON_TREE
);
1790 arc
->fake
= !!(flags
& GCOV_ARC_FAKE
);
1791 arc
->fall_through
= !!(flags
& GCOV_ARC_FALLTHROUGH
);
1793 arc
->succ_next
= src_blk
->succ
;
1794 src_blk
->succ
= arc
;
1795 src_blk
->num_succ
++;
1797 arc
->pred_next
= fn
->blocks
[dest
].pred
;
1798 fn
->blocks
[dest
].pred
= arc
;
1799 fn
->blocks
[dest
].num_pred
++;
1805 /* Exceptional exit from this function, the
1806 source block must be a call. */
1807 fn
->blocks
[src
].is_call_site
= 1;
1808 arc
->is_call_non_return
= 1;
1813 /* Non-local return from a callee of this
1814 function. The destination block is a setjmp. */
1815 arc
->is_nonlocal_return
= 1;
1816 fn
->blocks
[dest
].is_nonlocal_return
= 1;
1821 fn
->counts
.push_back (0);
1826 /* We have a fake exit from this block. The other
1827 non-fall through exits must be to catch handlers.
1828 Mark them as catch arcs. */
1830 for (arc
= src_blk
->succ
; arc
; arc
= arc
->succ_next
)
1831 if (!arc
->fake
&& !arc
->fall_through
)
1838 else if (fn
&& tag
== GCOV_TAG_LINES
)
1840 unsigned blockno
= gcov_read_unsigned ();
1841 block_info
*block
= &fn
->blocks
[blockno
];
1843 if (blockno
>= fn
->blocks
.size ())
1848 unsigned lineno
= gcov_read_unsigned ();
1851 block
->locations
.back ().lines
.push_back (lineno
);
1854 const char *file_name
= gcov_read_string ();
1858 block
->locations
.push_back (block_location_info
1859 (find_source (file_name
)));
1863 else if (current_tag
&& !GCOV_TAG_IS_SUBTAG (current_tag
, tag
))
1868 gcov_sync (base
, length
);
1869 if (gcov_is_error ())
1872 fnotice (stderr
, "%s:corrupted\n", bbg_file_name
);
1878 if (functions
.empty ())
1879 fnotice (stderr
, "%s:no functions found\n", bbg_file_name
);
1882 /* Reads profiles from the count file and attach to each
1883 function. Return nonzero if fatal error. */
1886 read_count_file (void)
1891 function_info
*fn
= NULL
;
1893 map
<unsigned, function_info
*>::iterator it
;
1895 if (!gcov_open (da_file_name
, 1))
1897 fnotice (stderr
, "%s:cannot open data file, assuming not executed\n",
1902 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
1904 fnotice (stderr
, "%s:not a gcov data file\n", da_file_name
);
1909 version
= gcov_read_unsigned ();
1910 if (version
!= GCOV_VERSION
)
1914 GCOV_UNSIGNED2STRING (v
, version
);
1915 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1917 fnotice (stderr
, "%s:version '%.4s', prefer version '%.4s'\n",
1918 da_file_name
, v
, e
);
1920 tag
= gcov_read_unsigned ();
1921 if (tag
!= bbg_stamp
)
1923 fnotice (stderr
, "%s:stamp mismatch with notes file\n", da_file_name
);
1927 while ((tag
= gcov_read_unsigned ()))
1929 unsigned length
= gcov_read_unsigned ();
1930 unsigned long base
= gcov_position ();
1932 if (tag
== GCOV_TAG_OBJECT_SUMMARY
)
1934 struct gcov_summary summary
;
1935 gcov_read_summary (&summary
);
1936 object_runs
= summary
.runs
;
1938 else if (tag
== GCOV_TAG_FUNCTION
&& !length
)
1940 else if (tag
== GCOV_TAG_FUNCTION
&& length
== GCOV_TAG_FUNCTION_LENGTH
)
1943 ident
= gcov_read_unsigned ();
1945 it
= ident_to_fn
.find (ident
);
1946 if (it
!= ident_to_fn
.end ())
1951 else if (gcov_read_unsigned () != fn
->lineno_checksum
1952 || gcov_read_unsigned () != fn
->cfg_checksum
)
1955 fnotice (stderr
, "%s:profile mismatch for '%s'\n",
1956 da_file_name
, fn
->get_name ());
1960 else if (tag
== GCOV_TAG_FOR_COUNTER (GCOV_COUNTER_ARCS
) && fn
)
1962 if (length
!= GCOV_TAG_COUNTER_LENGTH (fn
->counts
.size ()))
1965 for (ix
= 0; ix
!= fn
->counts
.size (); ix
++)
1966 fn
->counts
[ix
] += gcov_read_counter ();
1968 gcov_sync (base
, length
);
1969 if ((error
= gcov_is_error ()))
1973 ? N_("%s:overflowed\n")
1974 : N_("%s:corrupted\n"),
1984 /* Solve the flow graph. Propagate counts from the instrumented arcs
1985 to the blocks and the uninstrumented arcs. */
1988 solve_flow_graph (function_info
*fn
)
1992 gcov_type
*count_ptr
= &fn
->counts
.front ();
1994 block_info
*valid_blocks
= NULL
; /* valid, but unpropagated blocks. */
1995 block_info
*invalid_blocks
= NULL
; /* invalid, but inferable blocks. */
1997 /* The arcs were built in reverse order. Fix that now. */
1998 for (ix
= fn
->blocks
.size (); ix
--;)
2000 arc_info
*arc_p
, *arc_n
;
2002 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].succ
; arc
;
2003 arc_p
= arc
, arc
= arc_n
)
2005 arc_n
= arc
->succ_next
;
2006 arc
->succ_next
= arc_p
;
2008 fn
->blocks
[ix
].succ
= arc_p
;
2010 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].pred
; arc
;
2011 arc_p
= arc
, arc
= arc_n
)
2013 arc_n
= arc
->pred_next
;
2014 arc
->pred_next
= arc_p
;
2016 fn
->blocks
[ix
].pred
= arc_p
;
2019 if (fn
->blocks
.size () < 2)
2020 fnotice (stderr
, "%s:'%s' lacks entry and/or exit blocks\n",
2021 bbg_file_name
, fn
->get_name ());
2024 if (fn
->blocks
[ENTRY_BLOCK
].num_pred
)
2025 fnotice (stderr
, "%s:'%s' has arcs to entry block\n",
2026 bbg_file_name
, fn
->get_name ());
2028 /* We can't deduce the entry block counts from the lack of
2030 fn
->blocks
[ENTRY_BLOCK
].num_pred
= ~(unsigned)0;
2032 if (fn
->blocks
[EXIT_BLOCK
].num_succ
)
2033 fnotice (stderr
, "%s:'%s' has arcs from exit block\n",
2034 bbg_file_name
, fn
->get_name ());
2036 /* Likewise, we can't deduce exit block counts from the lack
2037 of its successors. */
2038 fn
->blocks
[EXIT_BLOCK
].num_succ
= ~(unsigned)0;
2041 /* Propagate the measured counts, this must be done in the same
2042 order as the code in profile.c */
2043 for (unsigned i
= 0; i
< fn
->blocks
.size (); i
++)
2045 blk
= &fn
->blocks
[i
];
2046 block_info
const *prev_dst
= NULL
;
2047 int out_of_order
= 0;
2048 int non_fake_succ
= 0;
2050 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
2058 arc
->count
= *count_ptr
++;
2059 arc
->count_valid
= 1;
2061 arc
->dst
->num_pred
--;
2063 if (prev_dst
&& prev_dst
> arc
->dst
)
2065 prev_dst
= arc
->dst
;
2067 if (non_fake_succ
== 1)
2069 /* If there is only one non-fake exit, it is an
2070 unconditional branch. */
2071 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
2074 arc
->is_unconditional
= 1;
2075 /* If this block is instrumenting a call, it might be
2076 an artificial block. It is not artificial if it has
2077 a non-fallthrough exit, or the destination of this
2078 arc has more than one entry. Mark the destination
2079 block as a return site, if none of those conditions
2081 if (blk
->is_call_site
&& arc
->fall_through
2082 && arc
->dst
->pred
== arc
&& !arc
->pred_next
)
2083 arc
->dst
->is_call_return
= 1;
2087 /* Sort the successor arcs into ascending dst order. profile.c
2088 normally produces arcs in the right order, but sometimes with
2089 one or two out of order. We're not using a particularly
2093 arc_info
*start
= blk
->succ
;
2094 unsigned changes
= 1;
2098 arc_info
*arc
, *arc_p
, *arc_n
;
2101 for (arc_p
= NULL
, arc
= start
; (arc_n
= arc
->succ_next
);)
2103 if (arc
->dst
> arc_n
->dst
)
2107 arc_p
->succ_next
= arc_n
;
2110 arc
->succ_next
= arc_n
->succ_next
;
2111 arc_n
->succ_next
= arc
;
2124 /* Place it on the invalid chain, it will be ignored if that's
2126 blk
->invalid_chain
= 1;
2127 blk
->chain
= invalid_blocks
;
2128 invalid_blocks
= blk
;
2131 while (invalid_blocks
|| valid_blocks
)
2133 while ((blk
= invalid_blocks
))
2135 gcov_type total
= 0;
2136 const arc_info
*arc
;
2138 invalid_blocks
= blk
->chain
;
2139 blk
->invalid_chain
= 0;
2141 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
2142 total
+= arc
->count
;
2143 else if (!blk
->num_pred
)
2144 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
2145 total
+= arc
->count
;
2150 blk
->count_valid
= 1;
2151 blk
->chain
= valid_blocks
;
2152 blk
->valid_chain
= 1;
2155 while ((blk
= valid_blocks
))
2158 arc_info
*arc
, *inv_arc
;
2160 valid_blocks
= blk
->chain
;
2161 blk
->valid_chain
= 0;
2162 if (blk
->num_succ
== 1)
2168 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
2170 total
-= arc
->count
;
2171 if (!arc
->count_valid
)
2175 inv_arc
->count_valid
= 1;
2176 inv_arc
->count
= total
;
2179 if (dst
->count_valid
)
2181 if (dst
->num_pred
== 1 && !dst
->valid_chain
)
2183 dst
->chain
= valid_blocks
;
2184 dst
->valid_chain
= 1;
2190 if (!dst
->num_pred
&& !dst
->invalid_chain
)
2192 dst
->chain
= invalid_blocks
;
2193 dst
->invalid_chain
= 1;
2194 invalid_blocks
= dst
;
2198 if (blk
->num_pred
== 1)
2204 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
2206 total
-= arc
->count
;
2207 if (!arc
->count_valid
)
2211 inv_arc
->count_valid
= 1;
2212 inv_arc
->count
= total
;
2215 if (src
->count_valid
)
2217 if (src
->num_succ
== 1 && !src
->valid_chain
)
2219 src
->chain
= valid_blocks
;
2220 src
->valid_chain
= 1;
2226 if (!src
->num_succ
&& !src
->invalid_chain
)
2228 src
->chain
= invalid_blocks
;
2229 src
->invalid_chain
= 1;
2230 invalid_blocks
= src
;
2237 /* If the graph has been correctly solved, every block will have a
2239 for (unsigned i
= 0; ix
< fn
->blocks
.size (); i
++)
2240 if (!fn
->blocks
[i
].count_valid
)
2242 fnotice (stderr
, "%s:graph is unsolvable for '%s'\n",
2243 bbg_file_name
, fn
->get_name ());
2248 /* Mark all the blocks only reachable via an incoming catch. */
2251 find_exception_blocks (function_info
*fn
)
2254 block_info
**queue
= XALLOCAVEC (block_info
*, fn
->blocks
.size ());
2256 /* First mark all blocks as exceptional. */
2257 for (ix
= fn
->blocks
.size (); ix
--;)
2258 fn
->blocks
[ix
].exceptional
= 1;
2260 /* Now mark all the blocks reachable via non-fake edges */
2261 queue
[0] = &fn
->blocks
[0];
2262 queue
[0]->exceptional
= 0;
2265 block_info
*block
= queue
[--ix
];
2266 const arc_info
*arc
;
2268 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2269 if (!arc
->fake
&& !arc
->is_throw
&& arc
->dst
->exceptional
)
2271 arc
->dst
->exceptional
= 0;
2272 queue
[ix
++] = arc
->dst
;
2278 /* Increment totals in COVERAGE according to arc ARC. */
2281 add_branch_counts (coverage_info
*coverage
, const arc_info
*arc
)
2283 if (arc
->is_call_non_return
)
2286 if (arc
->src
->count
)
2287 coverage
->calls_executed
++;
2289 else if (!arc
->is_unconditional
)
2291 coverage
->branches
++;
2292 if (arc
->src
->count
)
2293 coverage
->branches_executed
++;
2295 coverage
->branches_taken
++;
2299 /* Format COUNT, if flag_human_readable_numbers is set, return it human
2303 format_count (gcov_type count
)
2305 static char buffer
[64];
2306 const char *units
= " kMGTPEZY";
2308 if (count
< 1000 || !flag_human_readable_numbers
)
2310 sprintf (buffer
, "%" PRId64
, count
);
2315 gcov_type divisor
= 1;
2316 for (i
= 0; units
[i
+1]; i
++, divisor
*= 1000)
2318 if (count
+ divisor
/ 2 < 1000 * divisor
)
2321 float r
= 1.0f
* count
/ divisor
;
2322 sprintf (buffer
, "%.1f%c", r
, units
[i
]);
2326 /* Format a GCOV_TYPE integer as either a percent ratio, or absolute
2327 count. If DECIMAL_PLACES >= 0, format TOP/BOTTOM * 100 to DECIMAL_PLACES.
2328 If DECIMAL_PLACES is zero, no decimal point is printed. Only print 100% when
2329 TOP==BOTTOM and only print 0% when TOP=0. If DECIMAL_PLACES < 0, then simply
2330 format TOP. Return pointer to a static string. */
2333 format_gcov (gcov_type top
, gcov_type bottom
, int decimal_places
)
2335 static char buffer
[20];
2337 if (decimal_places
>= 0)
2339 float ratio
= bottom
? 100.0f
* top
/ bottom
: 0;
2341 /* Round up to 1% if there's a small non-zero value. */
2342 if (ratio
> 0.0f
&& ratio
< 0.5f
&& decimal_places
== 0)
2344 sprintf (buffer
, "%.*f%%", decimal_places
, ratio
);
2347 return format_count (top
);
2352 /* Summary of execution */
2355 executed_summary (unsigned lines
, unsigned executed
)
2358 fnotice (stdout
, "Lines executed:%s of %d\n",
2359 format_gcov (executed
, lines
, 2), lines
);
2361 fnotice (stdout
, "No executable lines\n");
2364 /* Output summary info for a function. */
2367 function_summary (const coverage_info
*coverage
)
2369 fnotice (stdout
, "%s '%s'\n", "Function", coverage
->name
);
2370 executed_summary (coverage
->lines
, coverage
->lines_executed
);
2373 /* Output summary info for a file. */
2376 file_summary (const coverage_info
*coverage
)
2378 fnotice (stdout
, "%s '%s'\n", "File", coverage
->name
);
2379 executed_summary (coverage
->lines
, coverage
->lines_executed
);
2383 if (coverage
->branches
)
2385 fnotice (stdout
, "Branches executed:%s of %d\n",
2386 format_gcov (coverage
->branches_executed
,
2387 coverage
->branches
, 2),
2388 coverage
->branches
);
2389 fnotice (stdout
, "Taken at least once:%s of %d\n",
2390 format_gcov (coverage
->branches_taken
,
2391 coverage
->branches
, 2),
2392 coverage
->branches
);
2395 fnotice (stdout
, "No branches\n");
2396 if (coverage
->calls
)
2397 fnotice (stdout
, "Calls executed:%s of %d\n",
2398 format_gcov (coverage
->calls_executed
, coverage
->calls
, 2),
2401 fnotice (stdout
, "No calls\n");
2405 /* Canonicalize the filename NAME by canonicalizing directory
2406 separators, eliding . components and resolving .. components
2407 appropriately. Always returns a unique string. */
2410 canonicalize_name (const char *name
)
2412 /* The canonical name cannot be longer than the incoming name. */
2413 char *result
= XNEWVEC (char, strlen (name
) + 1);
2414 const char *base
= name
, *probe
;
2419 #if HAVE_DOS_BASED_FILE_SYSTEM
2420 if (base
[0] && base
[1] == ':')
2422 result
[0] = base
[0];
2428 for (dd_base
= ptr
; *base
; base
= probe
)
2432 for (probe
= base
; *probe
; probe
++)
2433 if (IS_DIR_SEPARATOR (*probe
))
2437 if (len
== 1 && base
[0] == '.')
2438 /* Elide a '.' directory */
2440 else if (len
== 2 && base
[0] == '.' && base
[1] == '.')
2442 /* '..', we can only elide it and the previous directory, if
2443 we're not a symlink. */
2444 struct stat ATTRIBUTE_UNUSED buf
;
2448 #if defined (S_ISLNK)
2449 /* S_ISLNK is not POSIX.1-1996. */
2450 || stat (result
, &buf
) || S_ISLNK (buf
.st_mode
)
2454 /* Cannot elide, or unreadable or a symlink. */
2455 dd_base
= ptr
+ 2 + slash
;
2458 while (ptr
!= dd_base
&& *ptr
!= '/')
2460 slash
= ptr
!= result
;
2465 /* Regular pathname component. */
2468 memcpy (ptr
, base
, len
);
2473 for (; IS_DIR_SEPARATOR (*probe
); probe
++)
2481 /* Print hex representation of 16 bytes from SUM and write it to BUFFER. */
2484 md5sum_to_hex (const char *sum
, char *buffer
)
2486 for (unsigned i
= 0; i
< 16; i
++)
2487 sprintf (buffer
+ (2 * i
), "%02x", (unsigned char)sum
[i
]);
2490 /* Generate an output file name. INPUT_NAME is the canonicalized main
2491 input file and SRC_NAME is the canonicalized file name.
2492 LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
2493 long_output_names we prepend the processed name of the input file
2494 to each output name (except when the current source file is the
2495 input file, so you don't get a double concatenation). The two
2496 components are separated by '##'. With preserve_paths we create a
2497 filename from all path components of the source file, replacing '/'
2498 with '#', and .. with '^', without it we simply take the basename
2499 component. (Remember, the canonicalized name will already have
2500 elided '.' components and converted \\ separators.) */
2503 make_gcov_file_name (const char *input_name
, const char *src_name
)
2508 if (flag_long_names
&& input_name
&& strcmp (src_name
, input_name
))
2510 /* Generate the input filename part. */
2511 result
= XNEWVEC (char, strlen (input_name
) + strlen (src_name
) + 10);
2514 ptr
= mangle_name (input_name
, ptr
);
2515 ptr
[0] = ptr
[1] = '#';
2520 result
= XNEWVEC (char, strlen (src_name
) + 10);
2524 ptr
= mangle_name (src_name
, ptr
);
2525 strcpy (ptr
, ".gcov");
2527 /* When hashing filenames, we shorten them by only using the filename
2528 component and appending a hash of the full (mangled) pathname. */
2529 if (flag_hash_filenames
)
2533 char md5sum_hex
[33];
2535 md5_init_ctx (&ctx
);
2536 md5_process_bytes (src_name
, strlen (src_name
), &ctx
);
2537 md5_finish_ctx (&ctx
, md5sum
);
2538 md5sum_to_hex (md5sum
, md5sum_hex
);
2541 result
= XNEWVEC (char, strlen (src_name
) + 50);
2543 ptr
= mangle_name (src_name
, ptr
);
2544 ptr
[0] = ptr
[1] = '#';
2546 memcpy (ptr
, md5sum_hex
, 32);
2548 strcpy (ptr
, ".gcov");
2554 /* Mangle BASE name, copy it at the beginning of PTR buffer and
2555 return address of the \0 character of the buffer. */
2558 mangle_name (char const *base
, char *ptr
)
2562 /* Generate the source filename part. */
2563 if (!flag_preserve_paths
)
2564 base
= lbasename (base
);
2566 base
= mangle_path (base
);
2568 len
= strlen (base
);
2569 memcpy (ptr
, base
, len
);
2575 /* Scan through the bb_data for each line in the block, increment
2576 the line number execution count indicated by the execution count of
2577 the appropriate basic block. */
2580 add_line_counts (coverage_info
*coverage
, function_info
*fn
)
2582 bool has_any_line
= false;
2583 /* Scan each basic block. */
2584 for (unsigned ix
= 0; ix
!= fn
->blocks
.size (); ix
++)
2586 line_info
*line
= NULL
;
2587 block_info
*block
= &fn
->blocks
[ix
];
2588 if (block
->count
&& ix
&& ix
+ 1 != fn
->blocks
.size ())
2589 fn
->blocks_executed
++;
2590 for (unsigned i
= 0; i
< block
->locations
.size (); i
++)
2592 unsigned src_idx
= block
->locations
[i
].source_file_idx
;
2593 vector
<unsigned> &lines
= block
->locations
[i
].lines
;
2595 block
->cycle
.arc
= NULL
;
2596 block
->cycle
.ident
= ~0U;
2598 for (unsigned j
= 0; j
< lines
.size (); j
++)
2600 unsigned ln
= lines
[j
];
2602 /* Line belongs to a function that is in a group. */
2603 if (fn
->group_line_p (ln
, src_idx
))
2605 gcc_assert (lines
[j
] - fn
->start_line
< fn
->lines
.size ());
2606 line
= &(fn
->lines
[lines
[j
] - fn
->start_line
]);
2608 if (!block
->exceptional
)
2610 line
->unexceptional
= 1;
2611 if (block
->count
== 0)
2612 line
->has_unexecuted_block
= 1;
2614 line
->count
+= block
->count
;
2618 gcc_assert (ln
< sources
[src_idx
].lines
.size ());
2619 line
= &(sources
[src_idx
].lines
[ln
]);
2624 if (!line
->count
&& block
->count
)
2625 coverage
->lines_executed
++;
2628 if (!block
->exceptional
)
2630 line
->unexceptional
= 1;
2631 if (block
->count
== 0)
2632 line
->has_unexecuted_block
= 1;
2634 line
->count
+= block
->count
;
2638 has_any_line
= true;
2640 if (!ix
|| ix
+ 1 == fn
->blocks
.size ())
2641 /* Entry or exit block. */;
2642 else if (line
!= NULL
)
2644 line
->blocks
.push_back (block
);
2650 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2651 line
->branches
.push_back (arc
);
2658 fnotice (stderr
, "%s:no lines for '%s'\n", bbg_file_name
,
2662 /* Accumulate info for LINE that belongs to SRC source file. If ADD_COVERAGE
2663 is set to true, update source file summary. */
2665 static void accumulate_line_info (line_info
*line
, source_info
*src
,
2669 for (vector
<arc_info
*>::iterator it
= line
->branches
.begin ();
2670 it
!= line
->branches
.end (); it
++)
2671 add_branch_counts (&src
->coverage
, *it
);
2673 if (!line
->blocks
.empty ())
2675 /* The user expects the line count to be the number of times
2676 a line has been executed. Simply summing the block count
2677 will give an artificially high number. The Right Thing
2678 is to sum the entry counts to the graph of blocks on this
2679 line, then find the elementary cycles of the local graph
2680 and add the transition counts of those cycles. */
2681 gcov_type count
= 0;
2683 /* Cycle detection. */
2684 for (vector
<block_info
*>::iterator it
= line
->blocks
.begin ();
2685 it
!= line
->blocks
.end (); it
++)
2687 for (arc_info
*arc
= (*it
)->pred
; arc
; arc
= arc
->pred_next
)
2688 if (!line
->has_block (arc
->src
))
2689 count
+= arc
->count
;
2690 for (arc_info
*arc
= (*it
)->succ
; arc
; arc
= arc
->succ_next
)
2691 arc
->cs_count
= arc
->count
;
2694 /* Now, add the count of loops entirely on this line. */
2695 count
+= get_cycles_count (*line
);
2696 line
->count
= count
;
2698 if (line
->count
> src
->maximum_count
)
2699 src
->maximum_count
= line
->count
;
2702 if (line
->exists
&& add_coverage
)
2704 src
->coverage
.lines
++;
2706 src
->coverage
.lines_executed
++;
2710 /* Accumulate the line counts of a file. */
2713 accumulate_line_counts (source_info
*src
)
2715 /* First work on group functions. */
2716 for (vector
<function_info
*>::iterator it
= src
->functions
.begin ();
2717 it
!= src
->functions
.end (); it
++)
2719 function_info
*fn
= *it
;
2721 if (fn
->src
!= src
->index
|| !fn
->is_group
)
2724 for (vector
<line_info
>::iterator it2
= fn
->lines
.begin ();
2725 it2
!= fn
->lines
.end (); it2
++)
2727 line_info
*line
= &(*it2
);
2728 accumulate_line_info (line
, src
, false);
2732 /* Work on global lines that line in source file SRC. */
2733 for (vector
<line_info
>::iterator it
= src
->lines
.begin ();
2734 it
!= src
->lines
.end (); it
++)
2735 accumulate_line_info (&(*it
), src
, true);
2737 /* If not using intermediate mode, sum lines of group functions and
2738 add them to lines that live in a source file. */
2739 if (!flag_json_format
)
2740 for (vector
<function_info
*>::iterator it
= src
->functions
.begin ();
2741 it
!= src
->functions
.end (); it
++)
2743 function_info
*fn
= *it
;
2745 if (fn
->src
!= src
->index
|| !fn
->is_group
)
2748 for (unsigned i
= 0; i
< fn
->lines
.size (); i
++)
2750 line_info
*fn_line
= &fn
->lines
[i
];
2751 if (fn_line
->exists
)
2753 unsigned ln
= fn
->start_line
+ i
;
2754 line_info
*src_line
= &src
->lines
[ln
];
2756 if (!src_line
->exists
)
2757 src
->coverage
.lines
++;
2758 if (!src_line
->count
&& fn_line
->count
)
2759 src
->coverage
.lines_executed
++;
2761 src_line
->count
+= fn_line
->count
;
2762 src_line
->exists
= 1;
2764 if (fn_line
->has_unexecuted_block
)
2765 src_line
->has_unexecuted_block
= 1;
2767 if (fn_line
->unexceptional
)
2768 src_line
->unexceptional
= 1;
2774 /* Output information about ARC number IX. Returns nonzero if
2775 anything is output. */
2778 output_branch_count (FILE *gcov_file
, int ix
, const arc_info
*arc
)
2780 if (arc
->is_call_non_return
)
2782 if (arc
->src
->count
)
2784 fnotice (gcov_file
, "call %2d returned %s\n", ix
,
2785 format_gcov (arc
->src
->count
- arc
->count
,
2786 arc
->src
->count
, -flag_counts
));
2789 fnotice (gcov_file
, "call %2d never executed\n", ix
);
2791 else if (!arc
->is_unconditional
)
2793 if (arc
->src
->count
)
2794 fnotice (gcov_file
, "branch %2d taken %s%s", ix
,
2795 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
),
2796 arc
->fall_through
? " (fallthrough)"
2797 : arc
->is_throw
? " (throw)" : "");
2799 fnotice (gcov_file
, "branch %2d never executed", ix
);
2802 fnotice (gcov_file
, " (BB %d)", arc
->dst
->id
);
2804 fnotice (gcov_file
, "\n");
2806 else if (flag_unconditional
&& !arc
->dst
->is_call_return
)
2808 if (arc
->src
->count
)
2809 fnotice (gcov_file
, "unconditional %2d taken %s\n", ix
,
2810 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
));
2812 fnotice (gcov_file
, "unconditional %2d never executed\n", ix
);
2820 read_line (FILE *file
)
2822 static char *string
;
2823 static size_t string_len
;
2830 string
= XNEWVEC (char, string_len
);
2833 while ((ptr
= fgets (string
+ pos
, string_len
- pos
, file
)))
2835 size_t len
= strlen (string
+ pos
);
2837 if (len
&& string
[pos
+ len
- 1] == '\n')
2839 string
[pos
+ len
- 1] = 0;
2843 /* If the file contains NUL characters or an incomplete
2844 last line, which can happen more than once in one run,
2845 we have to avoid doubling the STRING_LEN unnecessarily. */
2846 if (pos
> string_len
/ 2)
2849 string
= XRESIZEVEC (char, string
, string_len
);
2853 return pos
? string
: NULL
;
2856 /* Pad string S with spaces from left to have total width equal to 9. */
2859 pad_count_string (string
&s
)
2862 s
.insert (0, 9 - s
.size (), ' ');
2865 /* Print GCOV line beginning to F stream. If EXISTS is set to true, the
2866 line exists in source file. UNEXCEPTIONAL indicated that it's not in
2867 an exceptional statement. The output is printed for LINE_NUM of given
2868 COUNT of executions. EXCEPTIONAL_STRING and UNEXCEPTIONAL_STRING are
2869 used to indicate non-executed blocks. */
2872 output_line_beginning (FILE *f
, bool exists
, bool unexceptional
,
2873 bool has_unexecuted_block
,
2874 gcov_type count
, unsigned line_num
,
2875 const char *exceptional_string
,
2876 const char *unexceptional_string
,
2877 unsigned int maximum_count
)
2884 s
= format_gcov (count
, 0, -1);
2885 if (has_unexecuted_block
2886 && bbg_supports_has_unexecuted_blocks
)
2888 if (flag_use_colors
)
2890 pad_count_string (s
);
2891 s
.insert (0, SGR_SEQ (COLOR_BG_MAGENTA
2892 COLOR_SEPARATOR COLOR_FG_WHITE
));
2898 pad_count_string (s
);
2902 if (flag_use_colors
)
2905 pad_count_string (s
);
2907 s
.insert (0, SGR_SEQ (COLOR_BG_RED
2908 COLOR_SEPARATOR COLOR_FG_WHITE
));
2910 s
.insert (0, SGR_SEQ (COLOR_BG_CYAN
2911 COLOR_SEPARATOR COLOR_FG_WHITE
));
2916 s
= unexceptional
? unexceptional_string
: exceptional_string
;
2917 pad_count_string (s
);
2924 pad_count_string (s
);
2927 /* Format line number in output. */
2929 sprintf (buffer
, "%5u", line_num
);
2930 string
linestr (buffer
);
2932 if (flag_use_hotness_colors
&& maximum_count
)
2934 if (count
* 2 > maximum_count
) /* > 50%. */
2935 linestr
.insert (0, SGR_SEQ (COLOR_BG_RED
));
2936 else if (count
* 5 > maximum_count
) /* > 20%. */
2937 linestr
.insert (0, SGR_SEQ (COLOR_BG_YELLOW
));
2938 else if (count
* 10 > maximum_count
) /* > 10%. */
2939 linestr
.insert (0, SGR_SEQ (COLOR_BG_GREEN
));
2940 linestr
+= SGR_RESET
;
2943 fprintf (f
, "%s:%s", s
.c_str (), linestr
.c_str ());
2947 print_source_line (FILE *f
, const vector
<const char *> &source_lines
,
2950 gcc_assert (line
>= 1);
2951 gcc_assert (line
<= source_lines
.size ());
2953 fprintf (f
, ":%s\n", source_lines
[line
- 1]);
2956 /* Output line details for LINE and print it to F file. LINE lives on
2960 output_line_details (FILE *f
, const line_info
*line
, unsigned line_num
)
2962 if (flag_all_blocks
)
2968 for (vector
<block_info
*>::const_iterator it
= line
->blocks
.begin ();
2969 it
!= line
->blocks
.end (); it
++)
2971 if (!(*it
)->is_call_return
)
2973 output_line_beginning (f
, line
->exists
,
2974 (*it
)->exceptional
, false,
2975 (*it
)->count
, line_num
,
2976 "%%%%%", "$$$$$", 0);
2977 fprintf (f
, "-block %2d", ix
++);
2979 fprintf (f
, " (BB %u)", (*it
)->id
);
2983 for (arc
= (*it
)->succ
; arc
; arc
= arc
->succ_next
)
2984 jx
+= output_branch_count (f
, jx
, arc
);
2987 else if (flag_branches
)
2992 for (vector
<arc_info
*>::const_iterator it
= line
->branches
.begin ();
2993 it
!= line
->branches
.end (); it
++)
2994 ix
+= output_branch_count (f
, ix
, (*it
));
2998 /* Output detail statistics about function FN to file F. */
3001 output_function_details (FILE *f
, function_info
*fn
)
3006 arc_info
*arc
= fn
->blocks
[EXIT_BLOCK
].pred
;
3007 gcov_type return_count
= fn
->blocks
[EXIT_BLOCK
].count
;
3008 gcov_type called_count
= fn
->blocks
[ENTRY_BLOCK
].count
;
3010 for (; arc
; arc
= arc
->pred_next
)
3012 return_count
-= arc
->count
;
3014 fprintf (f
, "function %s", fn
->get_name ());
3015 fprintf (f
, " called %s",
3016 format_gcov (called_count
, 0, -1));
3017 fprintf (f
, " returned %s",
3018 format_gcov (return_count
, called_count
, 0));
3019 fprintf (f
, " blocks executed %s",
3020 format_gcov (fn
->blocks_executed
, fn
->get_block_count (), 0));
3024 /* Read in the source file one line at a time, and output that line to
3025 the gcov file preceded by its execution count and other
3029 output_lines (FILE *gcov_file
, const source_info
*src
)
3031 #define DEFAULT_LINE_START " -: 0:"
3032 #define FN_SEPARATOR "------------------\n"
3037 /* Print colorization legend. */
3038 if (flag_use_colors
)
3039 fprintf (gcov_file
, "%s",
3040 DEFAULT_LINE_START
"Colorization: profile count: " \
3041 SGR_SEQ (COLOR_BG_CYAN
) "zero coverage (exceptional)" SGR_RESET \
3043 SGR_SEQ (COLOR_BG_RED
) "zero coverage (unexceptional)" SGR_RESET \
3045 SGR_SEQ (COLOR_BG_MAGENTA
) "unexecuted block" SGR_RESET
"\n");
3047 if (flag_use_hotness_colors
)
3048 fprintf (gcov_file
, "%s",
3049 DEFAULT_LINE_START
"Colorization: line numbers: hotness: " \
3050 SGR_SEQ (COLOR_BG_RED
) "> 50%" SGR_RESET
" " \
3051 SGR_SEQ (COLOR_BG_YELLOW
) "> 20%" SGR_RESET
" " \
3052 SGR_SEQ (COLOR_BG_GREEN
) "> 10%" SGR_RESET
"\n");
3054 fprintf (gcov_file
, DEFAULT_LINE_START
"Source:%s\n", src
->coverage
.name
);
3055 if (!multiple_files
)
3057 fprintf (gcov_file
, DEFAULT_LINE_START
"Graph:%s\n", bbg_file_name
);
3058 fprintf (gcov_file
, DEFAULT_LINE_START
"Data:%s\n",
3059 no_data_file
? "-" : da_file_name
);
3060 fprintf (gcov_file
, DEFAULT_LINE_START
"Runs:%u\n", object_runs
);
3063 source_file
= fopen (src
->name
, "r");
3065 fnotice (stderr
, "Cannot open source file %s\n", src
->name
);
3066 else if (src
->file_time
== 0)
3067 fprintf (gcov_file
, DEFAULT_LINE_START
"Source is newer than graph\n");
3069 vector
<const char *> source_lines
;
3071 while ((retval
= read_line (source_file
)) != NULL
)
3072 source_lines
.push_back (xstrdup (retval
));
3074 unsigned line_start_group
= 0;
3075 vector
<function_info
*> *fns
;
3077 for (unsigned line_num
= 1; line_num
<= source_lines
.size (); line_num
++)
3079 if (line_num
>= src
->lines
.size ())
3081 fprintf (gcov_file
, "%9s:%5u", "-", line_num
);
3082 print_source_line (gcov_file
, source_lines
, line_num
);
3086 const line_info
*line
= &src
->lines
[line_num
];
3088 if (line_start_group
== 0)
3090 fns
= src
->get_functions_at_location (line_num
);
3091 if (fns
!= NULL
&& fns
->size () > 1)
3093 /* It's possible to have functions that partially overlap,
3094 thus take the maximum end_line of functions starting
3096 for (unsigned i
= 0; i
< fns
->size (); i
++)
3097 if ((*fns
)[i
]->end_line
> line_start_group
)
3098 line_start_group
= (*fns
)[i
]->end_line
;
3100 else if (fns
!= NULL
&& fns
->size () == 1)
3102 function_info
*fn
= (*fns
)[0];
3103 output_function_details (gcov_file
, fn
);
3107 /* For lines which don't exist in the .bb file, print '-' before
3108 the source line. For lines which exist but were never
3109 executed, print '#####' or '=====' before the source line.
3110 Otherwise, print the execution count before the source line.
3111 There are 16 spaces of indentation added before the source
3112 line so that tabs won't be messed up. */
3113 output_line_beginning (gcov_file
, line
->exists
, line
->unexceptional
,
3114 line
->has_unexecuted_block
, line
->count
,
3115 line_num
, "=====", "#####", src
->maximum_count
);
3117 print_source_line (gcov_file
, source_lines
, line_num
);
3118 output_line_details (gcov_file
, line
, line_num
);
3120 if (line_start_group
== line_num
)
3122 for (vector
<function_info
*>::iterator it
= fns
->begin ();
3123 it
!= fns
->end (); it
++)
3125 function_info
*fn
= *it
;
3126 vector
<line_info
> &lines
= fn
->lines
;
3128 fprintf (gcov_file
, FN_SEPARATOR
);
3130 string fn_name
= fn
->get_name ();
3131 if (flag_use_colors
)
3133 fn_name
.insert (0, SGR_SEQ (COLOR_FG_CYAN
));
3134 fn_name
+= SGR_RESET
;
3137 fprintf (gcov_file
, "%s:\n", fn_name
.c_str ());
3139 output_function_details (gcov_file
, fn
);
3141 /* Print all lines covered by the function. */
3142 for (unsigned i
= 0; i
< lines
.size (); i
++)
3144 line_info
*line
= &lines
[i
];
3145 unsigned l
= fn
->start_line
+ i
;
3147 /* For lines which don't exist in the .bb file, print '-'
3148 before the source line. For lines which exist but
3149 were never executed, print '#####' or '=====' before
3150 the source line. Otherwise, print the execution count
3151 before the source line.
3152 There are 16 spaces of indentation added before the source
3153 line so that tabs won't be messed up. */
3154 output_line_beginning (gcov_file
, line
->exists
,
3155 line
->unexceptional
,
3156 line
->has_unexecuted_block
,
3158 l
, "=====", "#####",
3159 src
->maximum_count
);
3161 print_source_line (gcov_file
, source_lines
, l
);
3162 output_line_details (gcov_file
, line
, l
);
3166 fprintf (gcov_file
, FN_SEPARATOR
);
3167 line_start_group
= 0;
3172 fclose (source_file
);