1 /* Gcov.c: prepend line execution counts and branch probabilities to a
3 Copyright (C) 1990-2017 Free Software Foundation, Inc.
4 Contributed by James E. Wilson of Cygnus Support.
5 Mangled by Bob Manson of Cygnus Support.
6 Mangled further by Nathan Sidwell <nathan@codesourcery.com>
8 Gcov is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 Gcov is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Gcov; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* ??? Print a list of the ten blocks with the highest execution counts,
23 and list the line numbers corresponding to those blocks. Also, perhaps
24 list the line numbers with the highest execution counts, only printing
25 the first if there are several which are all listed in the same block. */
27 /* ??? Should have an option to print the number of basic blocks, and the
28 percent of them that are covered. */
30 /* Need an option to show individual block counts, and show
31 probabilities of fall through arcs. */
34 #define INCLUDE_ALGORITHM
35 #define INCLUDE_VECTOR
36 #define INCLUDE_STRING
40 #include "coretypes.h"
43 #include "diagnostic.h"
46 #include "color-macros.h"
58 /* The gcno file is generated by -ftest-coverage option. The gcda file is
59 generated by a program compiled with -fprofile-arcs. Their formats
60 are documented in gcov-io.h. */
62 /* The functions in this file for creating and solution program flow graphs
63 are very similar to functions in the gcc source file profile.c. In
64 some places we make use of the knowledge of how profile.c works to
65 select particular algorithms here. */
67 /* The code validates that the profile information read in corresponds
68 to the code currently being compiled. Rather than checking for
69 identical files, the code below compares a checksum on the CFG
70 (based on the order of basic blocks and the arcs in the CFG). If
71 the CFG checksum in the gcda file match the CFG checksum in the
72 gcno file, the profile data will be used. */
74 /* This is the size of the buffer used to read in source file lines. */
80 /* Describes an arc between two basic blocks. */
82 typedef struct arc_info
84 /* source and destination blocks. */
85 struct block_info
*src
;
86 struct block_info
*dst
;
88 /* transition counts. */
90 /* used in cycle search, so that we do not clobber original counts. */
93 unsigned int count_valid
: 1;
94 unsigned int on_tree
: 1;
95 unsigned int fake
: 1;
96 unsigned int fall_through
: 1;
98 /* Arc to a catch handler. */
99 unsigned int is_throw
: 1;
101 /* Arc is for a function that abnormally returns. */
102 unsigned int is_call_non_return
: 1;
104 /* Arc is for catch/setjmp. */
105 unsigned int is_nonlocal_return
: 1;
107 /* Is an unconditional branch. */
108 unsigned int is_unconditional
: 1;
110 /* Loop making arc. */
111 unsigned int cycle
: 1;
113 /* Links to next arc on src and dst lists. */
114 struct arc_info
*succ_next
;
115 struct arc_info
*pred_next
;
118 /* Describes which locations (lines and files) are associated with
121 struct block_location_info
123 block_location_info (unsigned _source_file_idx
):
124 source_file_idx (_source_file_idx
)
127 unsigned source_file_idx
;
128 vector
<unsigned> lines
;
131 /* Describes a basic block. Contains lists of arcs to successor and
132 predecessor blocks. */
134 typedef struct block_info
139 /* Chain of exit and entry arcs. */
143 /* Number of unprocessed exit and entry arcs. */
149 /* Block execution count. */
151 unsigned count_valid
: 1;
152 unsigned valid_chain
: 1;
153 unsigned invalid_chain
: 1;
154 unsigned exceptional
: 1;
156 /* Block is a call instrumenting site. */
157 unsigned is_call_site
: 1; /* Does the call. */
158 unsigned is_call_return
: 1; /* Is the return. */
160 /* Block is a landing pad for longjmp or throw. */
161 unsigned is_nonlocal_return
: 1;
163 vector
<block_location_info
> locations
;
167 /* Single line graph cycle workspace. Used for all-blocks
171 } cycle
; /* Used in all-blocks mode, after blocks are linked onto
174 /* Temporary chain for solving graph, and for chaining blocks on one
176 struct block_info
*chain
;
180 block_info::block_info (): succ (NULL
), pred (NULL
), num_succ (0), num_pred (0),
181 id (0), count (0), count_valid (0), valid_chain (0), invalid_chain (0),
182 exceptional (0), is_call_site (0), is_call_return (0), is_nonlocal_return (0),
183 locations (), chain (NULL
)
188 /* Describes a single line of source. Contains a chain of basic blocks
193 /* Default constructor. */
196 /* Return true when NEEDLE is one of basic blocks the line belongs to. */
197 bool has_block (block_t
*needle
);
199 /* Execution count. */
202 /* Branches from blocks that end on this line. */
203 vector
<arc_t
*> branches
;
205 /* blocks which start on this line. Used in all-blocks mode. */
206 vector
<block_t
*> blocks
;
209 unsigned unexceptional
: 1;
210 unsigned has_unexecuted_block
: 1;
213 line_info::line_info (): count (0), branches (), blocks (), exists (false),
214 unexceptional (0), has_unexecuted_block (0)
219 line_info::has_block (block_t
*needle
)
221 return std::find (blocks
.begin (), blocks
.end (), needle
) != blocks
.end ();
224 /* Describes a single function. Contains an array of basic blocks. */
226 typedef struct function_info
231 /* Return true when line N belongs to the function in source file SRC_IDX.
232 The line must be defined in body of the function, can't be inlined. */
233 bool group_line_p (unsigned n
, unsigned src_idx
);
235 /* Name of function. */
237 char *demangled_name
;
239 unsigned lineno_checksum
;
240 unsigned cfg_checksum
;
242 /* The graph contains at least one fake incoming edge. */
243 unsigned has_catch
: 1;
245 /* True when the function is artificial and does not exist
247 unsigned artificial
: 1;
249 /* True when multiple functions start at a line in a source file. */
250 unsigned is_group
: 1;
252 /* Array of basic blocks. Like in GCC, the entry block is
253 at blocks[0] and the exit block is at blocks[1]. */
254 #define ENTRY_BLOCK (0)
255 #define EXIT_BLOCK (1)
256 vector
<block_t
> blocks
;
257 unsigned blocks_executed
;
259 /* Raw arc coverage counts. */
263 /* First line number. */
266 /* First line column. */
267 unsigned start_column
;
269 /* Last line number. */
272 /* Index of source file where the function is defined. */
275 /* Vector of line information. */
276 vector
<line_info
> lines
;
279 struct function_info
*next
;
282 /* Function info comparer that will sort functions according to starting
285 struct function_line_start_cmp
287 inline bool operator() (const function_info
*lhs
,
288 const function_info
*rhs
)
290 return (lhs
->start_line
== rhs
->start_line
291 ? lhs
->start_column
< rhs
->start_column
292 : lhs
->start_line
< rhs
->start_line
);
296 /* Describes coverage of a file or function. */
298 typedef struct coverage_info
304 int branches_executed
;
313 /* Describes a file mentioned in the block graph. Contains an array
318 /* Default constructor. */
321 vector
<function_t
*> get_functions_at_location (unsigned line_num
) const;
323 /* Index of the source_info in sources vector. */
326 /* Canonical name of source file. */
330 /* Vector of line information. */
331 vector
<line_info
> lines
;
335 /* Functions in this source file. These are in ascending line
337 vector
<function_t
*> functions
;
340 source_info::source_info (): index (0), name (NULL
), file_time (),
341 lines (), coverage (), functions ()
346 source_info::get_functions_at_location (unsigned line_num
) const
348 vector
<function_t
*> r
;
350 for (vector
<function_t
*>::const_iterator it
= functions
.begin ();
351 it
!= functions
.end (); it
++)
353 if ((*it
)->start_line
== line_num
&& (*it
)->src
== index
)
357 std::sort (r
.begin (), r
.end (), function_line_start_cmp ());
369 name_map (char *_name
, unsigned _src
): name (_name
), src (_src
)
373 bool operator== (const name_map
&rhs
) const
375 #if HAVE_DOS_BASED_FILE_SYSTEM
376 return strcasecmp (this->name
, rhs
.name
) == 0;
378 return strcmp (this->name
, rhs
.name
) == 0;
382 bool operator< (const name_map
&rhs
) const
384 #if HAVE_DOS_BASED_FILE_SYSTEM
385 return strcasecmp (this->name
, rhs
.name
) < 0;
387 return strcmp (this->name
, rhs
.name
) < 0;
391 const char *name
; /* Source file name */
392 unsigned src
; /* Source file */
395 /* Holds a list of function basic block graphs. */
397 static function_t
*functions
;
398 static function_t
**fn_end
= &functions
;
400 /* Vector of source files. */
401 static vector
<source_info
> sources
;
403 /* Mapping of file names to sources */
404 static vector
<name_map
> names
;
406 /* This holds data summary information. */
408 static unsigned object_runs
;
409 static unsigned program_count
;
411 static unsigned total_lines
;
412 static unsigned total_executed
;
414 /* Modification time of graph file. */
416 static time_t bbg_file_time
;
418 /* Name of the notes (gcno) output file. The "bbg" prefix is for
419 historical reasons, when the notes file contained only the
420 basic block graph notes. */
422 static char *bbg_file_name
;
424 /* Stamp of the bbg file */
425 static unsigned bbg_stamp
;
427 /* Supports has_unexecuted_blocks functionality. */
428 static unsigned bbg_supports_has_unexecuted_blocks
;
430 /* Name and file pointer of the input file for the count data (gcda). */
432 static char *da_file_name
;
434 /* Data file is missing. */
436 static int no_data_file
;
438 /* If there is several input files, compute and display results after
439 reading all data files. This way if two or more gcda file refer to
440 the same source file (eg inline subprograms in a .h file), the
443 static int multiple_files
= 0;
445 /* Output branch probabilities. */
447 static int flag_branches
= 0;
449 /* Show unconditional branches too. */
450 static int flag_unconditional
= 0;
452 /* Output a gcov file if this is true. This is on by default, and can
453 be turned off by the -n option. */
455 static int flag_gcov_file
= 1;
457 /* Output progress indication if this is true. This is off by default
458 and can be turned on by the -d option. */
460 static int flag_display_progress
= 0;
462 /* Output *.gcov file in intermediate format used by 'lcov'. */
464 static int flag_intermediate_format
= 0;
466 /* Output demangled function names. */
468 static int flag_demangled_names
= 0;
470 /* For included files, make the gcov output file name include the name
471 of the input source file. For example, if x.h is included in a.c,
472 then the output file name is a.c##x.h.gcov instead of x.h.gcov. */
474 static int flag_long_names
= 0;
476 /* For situations when a long name can potentially hit filesystem path limit,
477 let's calculate md5sum of the path and append it to a file name. */
479 static int flag_hash_filenames
= 0;
481 /* Print verbose informations. */
483 static int flag_verbose
= 0;
485 /* Print colored output. */
487 static int flag_use_colors
= 0;
489 /* Output count information for every basic block, not merely those
490 that contain line number information. */
492 static int flag_all_blocks
= 0;
494 /* Output human readable numbers. */
496 static int flag_human_readable_numbers
= 0;
498 /* Output summary info for each function. */
500 static int flag_function_summary
= 0;
502 /* Object directory file prefix. This is the directory/file where the
503 graph and data files are looked for, if nonzero. */
505 static char *object_directory
= 0;
507 /* Source directory prefix. This is removed from source pathnames
508 that match, when generating the output file name. */
510 static char *source_prefix
= 0;
511 static size_t source_length
= 0;
513 /* Only show data for sources with relative pathnames. Absolute ones
514 usually indicate a system header file, which although it may
515 contain inline functions, is usually uninteresting. */
516 static int flag_relative_only
= 0;
518 /* Preserve all pathname components. Needed when object files and
519 source files are in subdirectories. '/' is mangled as '#', '.' is
520 elided and '..' mangled to '^'. */
522 static int flag_preserve_paths
= 0;
524 /* Output the number of times a branch was taken as opposed to the percentage
525 of times it was taken. */
527 static int flag_counts
= 0;
529 /* Forward declarations. */
530 static int process_args (int, char **);
531 static void print_usage (int) ATTRIBUTE_NORETURN
;
532 static void print_version (void) ATTRIBUTE_NORETURN
;
533 static void process_file (const char *);
534 static void generate_results (const char *);
535 static void create_file_names (const char *);
536 static char *canonicalize_name (const char *);
537 static unsigned find_source (const char *);
538 static function_t
*read_graph_file (void);
539 static int read_count_file (function_t
*);
540 static void solve_flow_graph (function_t
*);
541 static void find_exception_blocks (function_t
*);
542 static void add_branch_counts (coverage_t
*, const arc_t
*);
543 static void add_line_counts (coverage_t
*, function_t
*);
544 static void executed_summary (unsigned, unsigned);
545 static void function_summary (const coverage_t
*, const char *);
546 static const char *format_gcov (gcov_type
, gcov_type
, int);
547 static void accumulate_line_counts (source_info
*);
548 static void output_gcov_file (const char *, source_info
*);
549 static int output_branch_count (FILE *, int, const arc_t
*);
550 static void output_lines (FILE *, const source_info
*);
551 static char *make_gcov_file_name (const char *, const char *);
552 static char *mangle_name (const char *, char *);
553 static void release_structures (void);
554 extern int main (int, char **);
556 function_info::function_info (): name (NULL
), demangled_name (NULL
),
557 ident (0), lineno_checksum (0), cfg_checksum (0), has_catch (0),
558 artificial (0), is_group (0),
559 blocks (), blocks_executed (0), counts (NULL
), num_counts (0),
560 start_line (0), start_column (0), end_line (0), src (0), lines (), next (NULL
)
564 function_info::~function_info ()
566 for (int i
= blocks
.size () - 1; i
>= 0; i
--)
570 for (arc
= blocks
[i
].succ
; arc
; arc
= arc_n
)
572 arc_n
= arc
->succ_next
;
577 if (flag_demangled_names
&& demangled_name
!= name
)
578 free (demangled_name
);
582 bool function_info::group_line_p (unsigned n
, unsigned src_idx
)
584 return is_group
&& src
== src_idx
&& start_line
<= n
&& n
<= end_line
;
588 There are a bajillion algorithms that do this. Boost's function is named
589 hawick_cycles, so I used the algorithm by K. A. Hawick and H. A. James in
590 "Enumerating Circuits and Loops in Graphs with Self-Arcs and Multiple-Arcs"
591 (url at <http://complexity.massey.ac.nz/cstn/013/cstn-013.pdf>).
593 The basic algorithm is simple: effectively, we're finding all simple paths
594 in a subgraph (that shrinks every iteration). Duplicates are filtered by
595 "blocking" a path when a node is added to the path (this also prevents non-
596 simple paths)--the node is unblocked only when it participates in a cycle.
599 typedef vector
<arc_t
*> arc_vector_t
;
600 typedef vector
<const block_t
*> block_vector_t
;
602 /* Enum with types of loop in CFG. */
611 /* Loop_type operator that merges two values: A and B. */
613 inline loop_type
& operator |= (loop_type
& a
, loop_type b
)
615 return a
= static_cast<loop_type
> (a
| b
);
618 /* Handle cycle identified by EDGES, where the function finds minimum cs_count
619 and subtract the value from all counts. The subtracted value is added
620 to COUNT. Returns type of loop. */
623 handle_cycle (const arc_vector_t
&edges
, int64_t &count
)
625 /* Find the minimum edge of the cycle, and reduce all nodes in the cycle by
627 int64_t cycle_count
= INTTYPE_MAXIMUM (int64_t);
628 for (unsigned i
= 0; i
< edges
.size (); i
++)
630 int64_t ecount
= edges
[i
]->cs_count
;
631 if (cycle_count
> ecount
)
632 cycle_count
= ecount
;
634 count
+= cycle_count
;
635 for (unsigned i
= 0; i
< edges
.size (); i
++)
636 edges
[i
]->cs_count
-= cycle_count
;
638 return cycle_count
< 0 ? NEGATIVE_LOOP
: LOOP
;
641 /* Unblock a block U from BLOCKED. Apart from that, iterate all blocks
642 blocked by U in BLOCK_LISTS. */
645 unblock (const block_t
*u
, block_vector_t
&blocked
,
646 vector
<block_vector_t
> &block_lists
)
648 block_vector_t::iterator it
= find (blocked
.begin (), blocked
.end (), u
);
649 if (it
== blocked
.end ())
652 unsigned index
= it
- blocked
.begin ();
655 block_vector_t
to_unblock (block_lists
[index
]);
657 block_lists
.erase (block_lists
.begin () + index
);
659 for (block_vector_t::iterator it
= to_unblock
.begin ();
660 it
!= to_unblock
.end (); it
++)
661 unblock (*it
, blocked
, block_lists
);
664 /* Find circuit going to block V, PATH is provisional seen cycle.
665 BLOCKED is vector of blocked vertices, BLOCK_LISTS contains vertices
666 blocked by a block. COUNT is accumulated count of the current LINE.
667 Returns what type of loop it contains. */
670 circuit (block_t
*v
, arc_vector_t
&path
, block_t
*start
,
671 block_vector_t
&blocked
, vector
<block_vector_t
> &block_lists
,
672 line_info
&linfo
, int64_t &count
)
674 loop_type result
= NO_LOOP
;
676 /* Add v to the block list. */
677 gcc_assert (find (blocked
.begin (), blocked
.end (), v
) == blocked
.end ());
678 blocked
.push_back (v
);
679 block_lists
.push_back (block_vector_t ());
681 for (arc_t
*arc
= v
->succ
; arc
; arc
= arc
->succ_next
)
683 block_t
*w
= arc
->dst
;
684 if (w
< start
|| !linfo
.has_block (w
))
687 path
.push_back (arc
);
689 /* Cycle has been found. */
690 result
|= handle_cycle (path
, count
);
691 else if (find (blocked
.begin (), blocked
.end (), w
) == blocked
.end ())
692 result
|= circuit (w
, path
, start
, blocked
, block_lists
, linfo
, count
);
697 if (result
!= NO_LOOP
)
698 unblock (v
, blocked
, block_lists
);
700 for (arc_t
*arc
= v
->succ
; arc
; arc
= arc
->succ_next
)
702 block_t
*w
= arc
->dst
;
703 if (w
< start
|| !linfo
.has_block (w
))
707 = find (blocked
.begin (), blocked
.end (), w
) - blocked
.begin ();
708 gcc_assert (index
< blocked
.size ());
709 block_vector_t
&list
= block_lists
[index
];
710 if (find (list
.begin (), list
.end (), v
) == list
.end ())
717 /* Find cycles for a LINFO. If HANDLE_NEGATIVE_CYCLES is set and the line
718 contains a negative loop, then perform the same function once again. */
721 get_cycles_count (line_info
&linfo
, bool handle_negative_cycles
= true)
723 /* Note that this algorithm works even if blocks aren't in sorted order.
724 Each iteration of the circuit detection is completely independent
725 (except for reducing counts, but that shouldn't matter anyways).
726 Therefore, operating on a permuted order (i.e., non-sorted) only
727 has the effect of permuting the output cycles. */
729 loop_type result
= NO_LOOP
;
731 for (vector
<block_t
*>::iterator it
= linfo
.blocks
.begin ();
732 it
!= linfo
.blocks
.end (); it
++)
735 block_vector_t blocked
;
736 vector
<block_vector_t
> block_lists
;
737 result
|= circuit (*it
, path
, *it
, blocked
, block_lists
, linfo
,
741 /* If we have a negative cycle, repeat the find_cycles routine. */
742 if (result
== NEGATIVE_LOOP
&& handle_negative_cycles
)
743 count
+= get_cycles_count (linfo
, false);
749 main (int argc
, char **argv
)
755 p
= argv
[0] + strlen (argv
[0]);
756 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
760 xmalloc_set_program_name (progname
);
762 /* Unlock the stdio streams. */
763 unlock_std_streams ();
767 diagnostic_initialize (global_dc
, 0);
769 /* Handle response files. */
770 expandargv (&argc
, &argv
);
772 argno
= process_args (argc
, argv
);
776 if (argc
- argno
> 1)
781 for (; argno
!= argc
; argno
++)
783 if (flag_display_progress
)
784 printf ("Processing file %d out of %d\n", argno
- first_arg
+ 1,
786 process_file (argv
[argno
]);
788 if (flag_intermediate_format
|| argno
== argc
- 1)
790 generate_results (argv
[argno
]);
791 release_structures ();
798 /* Print a usage message and exit. If ERROR_P is nonzero, this is an error,
799 otherwise the output of --help. */
802 print_usage (int error_p
)
804 FILE *file
= error_p
? stderr
: stdout
;
805 int status
= error_p
? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
;
807 fnotice (file
, "Usage: gcov [OPTION...] SOURCE|OBJ...\n\n");
808 fnotice (file
, "Print code coverage information.\n\n");
809 fnotice (file
, " -a, --all-blocks Show information for every basic block\n");
810 fnotice (file
, " -b, --branch-probabilities Include branch probabilities in output\n");
811 fnotice (file
, " -c, --branch-counts Output counts of branches taken\n\
812 rather than percentages\n");
813 fnotice (file
, " -d, --display-progress Display progress information\n");
814 fnotice (file
, " -f, --function-summaries Output summaries for each function\n");
815 fnotice (file
, " -h, --help Print this help, then exit\n");
816 fnotice (file
, " -i, --intermediate-format Output .gcov file in intermediate text format\n");
817 fnotice (file
, " -j, --human-readable Output human readable numbers\n");
818 fnotice (file
, " -k, --use-colors Emit colored output\n");
819 fnotice (file
, " -l, --long-file-names Use long output file names for included\n\
821 fnotice (file
, " -m, --demangled-names Output demangled function names\n");
822 fnotice (file
, " -n, --no-output Do not create an output file\n");
823 fnotice (file
, " -o, --object-directory DIR|FILE Search for object files in DIR or called FILE\n");
824 fnotice (file
, " -p, --preserve-paths Preserve all pathname components\n");
825 fnotice (file
, " -r, --relative-only Only show data for relative sources\n");
826 fnotice (file
, " -s, --source-prefix DIR Source prefix to elide\n");
827 fnotice (file
, " -u, --unconditional-branches Show unconditional branch counts too\n");
828 fnotice (file
, " -v, --version Print version number, then exit\n");
829 fnotice (file
, " -w, --verbose Print verbose informations\n");
830 fnotice (file
, " -x, --hash-filenames Hash long pathnames\n");
831 fnotice (file
, "\nFor bug reporting instructions, please see:\n%s.\n",
836 /* Print version information and exit. */
841 fnotice (stdout
, "gcov %s%s\n", pkgversion_string
, version_string
);
842 fprintf (stdout
, "Copyright %s 2017 Free Software Foundation, Inc.\n",
845 _("This is free software; see the source for copying conditions.\n"
846 "There is NO warranty; not even for MERCHANTABILITY or \n"
847 "FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
848 exit (SUCCESS_EXIT_CODE
);
851 static const struct option options
[] =
853 { "help", no_argument
, NULL
, 'h' },
854 { "version", no_argument
, NULL
, 'v' },
855 { "verbose", no_argument
, NULL
, 'w' },
856 { "all-blocks", no_argument
, NULL
, 'a' },
857 { "branch-probabilities", no_argument
, NULL
, 'b' },
858 { "branch-counts", no_argument
, NULL
, 'c' },
859 { "intermediate-format", no_argument
, NULL
, 'i' },
860 { "human-readable", no_argument
, NULL
, 'j' },
861 { "no-output", no_argument
, NULL
, 'n' },
862 { "long-file-names", no_argument
, NULL
, 'l' },
863 { "function-summaries", no_argument
, NULL
, 'f' },
864 { "demangled-names", no_argument
, NULL
, 'm' },
865 { "preserve-paths", no_argument
, NULL
, 'p' },
866 { "relative-only", no_argument
, NULL
, 'r' },
867 { "object-directory", required_argument
, NULL
, 'o' },
868 { "object-file", required_argument
, NULL
, 'o' },
869 { "source-prefix", required_argument
, NULL
, 's' },
870 { "unconditional-branches", no_argument
, NULL
, 'u' },
871 { "display-progress", no_argument
, NULL
, 'd' },
872 { "hash-filenames", no_argument
, NULL
, 'x' },
873 { "use-colors", no_argument
, NULL
, 'k' },
877 /* Process args, return index to first non-arg. */
880 process_args (int argc
, char **argv
)
884 const char *opts
= "abcdfhijklmno:prs:uvwx";
885 while ((opt
= getopt_long (argc
, argv
, opts
, options
, NULL
)) != -1)
899 flag_function_summary
= 1;
903 /* print_usage will exit. */
908 flag_human_readable_numbers
= 1;
914 flag_demangled_names
= 1;
920 object_directory
= optarg
;
923 source_prefix
= optarg
;
924 source_length
= strlen (source_prefix
);
927 flag_relative_only
= 1;
930 flag_preserve_paths
= 1;
933 flag_unconditional
= 1;
936 flag_intermediate_format
= 1;
940 flag_display_progress
= 1;
943 flag_hash_filenames
= 1;
950 /* print_version will exit. */
953 /* print_usage will exit. */
960 /* Output intermediate LINE sitting on LINE_NUM to output file F. */
963 output_intermediate_line (FILE *f
, line_info
*line
, unsigned line_num
)
968 fprintf (f
, "lcount:%u,%s,%d\n", line_num
,
969 format_gcov (line
->count
, 0, -1),
970 line
->has_unexecuted_block
);
972 vector
<arc_t
*>::const_iterator it
;
974 for (it
= line
->branches
.begin (); it
!= line
->branches
.end ();
977 if (!(*it
)->is_unconditional
&& !(*it
)->is_call_non_return
)
979 const char *branch_type
;
980 /* branch:<line_num>,<branch_coverage_type>
982 : notexec (Branch not executed)
983 : taken (Branch executed and taken)
984 : nottaken (Branch executed, but not taken)
986 if ((*it
)->src
->count
)
988 = ((*it
)->count
> 0) ? "taken" : "nottaken";
990 branch_type
= "notexec";
991 fprintf (f
, "branch:%d,%s\n", line_num
, branch_type
);
996 /* Get the name of the gcov file. The return value must be free'd.
998 It appends the '.gcov' extension to the *basename* of the file.
999 The resulting file name will be in PWD.
1002 input: foo.da, output: foo.da.gcov
1003 input: a/b/foo.cc, output: foo.cc.gcov */
1006 get_gcov_intermediate_filename (const char *file_name
)
1008 const char *gcov
= ".gcov";
1012 /* Find the 'basename'. */
1013 cptr
= lbasename (file_name
);
1015 result
= XNEWVEC (char, strlen (cptr
) + strlen (gcov
) + 1);
1016 sprintf (result
, "%s%s", cptr
, gcov
);
1021 /* Output the result in intermediate format used by 'lcov'.
1023 The intermediate format contains a single file named 'foo.cc.gcov',
1024 with no source code included.
1026 The default gcov outputs multiple files: 'foo.cc.gcov',
1027 'iostream.gcov', 'ios_base.h.gcov', etc. with source code
1028 included. Instead the intermediate format here outputs only a single
1029 file 'foo.cc.gcov' similar to the above example. */
1032 output_intermediate_file (FILE *gcov_file
, source_info
*src
)
1034 fprintf (gcov_file
, "file:%s\n", src
->name
); /* source file name */
1036 std::sort (src
->functions
.begin (), src
->functions
.end (),
1037 function_line_start_cmp ());
1038 for (vector
<function_t
*>::iterator it
= src
->functions
.begin ();
1039 it
!= src
->functions
.end (); it
++)
1041 /* function:<name>,<line_number>,<execution_count> */
1042 fprintf (gcov_file
, "function:%d,%d,%s,%s\n", (*it
)->start_line
,
1043 (*it
)->end_line
, format_gcov ((*it
)->blocks
[0].count
, 0, -1),
1044 flag_demangled_names
? (*it
)->demangled_name
: (*it
)->name
);
1047 for (unsigned line_num
= 1; line_num
<= src
->lines
.size (); line_num
++)
1049 vector
<function_t
*> fns
= src
->get_functions_at_location (line_num
);
1051 /* Print first group functions that begin on the line. */
1052 for (vector
<function_t
*>::iterator it2
= fns
.begin ();
1053 it2
!= fns
.end (); it2
++)
1055 vector
<line_info
> &lines
= (*it2
)->lines
;
1056 for (unsigned i
= 0; i
< lines
.size (); i
++)
1058 line_info
*line
= &lines
[i
];
1059 output_intermediate_line (gcov_file
, line
, line_num
+ i
);
1063 /* Follow with lines associated with the source file. */
1064 output_intermediate_line (gcov_file
, &src
->lines
[line_num
], line_num
);
1068 /* Function start pair. */
1069 struct function_start
1071 unsigned source_file_idx
;
1072 unsigned start_line
;
1075 /* Traits class for function start hash maps below. */
1077 struct function_start_pair_hash
: typed_noop_remove
<function_start
>
1079 typedef function_start value_type
;
1080 typedef function_start compare_type
;
1083 hash (const function_start
&ref
)
1085 inchash::hash
hstate (0);
1086 hstate
.add_int (ref
.source_file_idx
);
1087 hstate
.add_int (ref
.start_line
);
1088 return hstate
.end ();
1092 equal (const function_start
&ref1
, const function_start
&ref2
)
1094 return (ref1
.source_file_idx
== ref2
.source_file_idx
1095 && ref1
.start_line
== ref2
.start_line
);
1099 mark_deleted (function_start
&ref
)
1101 ref
.start_line
= ~1U;
1105 mark_empty (function_start
&ref
)
1107 ref
.start_line
= ~2U;
1111 is_deleted (const function_start
&ref
)
1113 return ref
.start_line
== ~1U;
1117 is_empty (const function_start
&ref
)
1119 return ref
.start_line
== ~2U;
1123 /* Process a single input file. */
1126 process_file (const char *file_name
)
1130 create_file_names (file_name
);
1131 fns
= read_graph_file ();
1135 read_count_file (fns
);
1137 hash_map
<function_start_pair_hash
, function_t
*> fn_map
;
1139 /* Identify group functions. */
1140 for (function_t
*f
= fns
; f
; f
= f
->next
)
1143 function_start needle
;
1144 needle
.source_file_idx
= f
->src
;
1145 needle
.start_line
= f
->start_line
;
1147 function_t
**slot
= fn_map
.get (needle
);
1150 gcc_assert ((*slot
)->end_line
== f
->end_line
);
1151 (*slot
)->is_group
= 1;
1155 fn_map
.put (needle
, f
);
1160 function_t
*fn
= fns
;
1164 if (fn
->counts
|| no_data_file
)
1166 unsigned src
= fn
->src
;
1169 /* Process only non-artificial functions. */
1170 if (!fn
->artificial
)
1172 source_info
*s
= &sources
[src
];
1173 s
->functions
.push_back (fn
);
1175 /* Mark last line in files touched by function. */
1176 for (block_no
= 0; block_no
!= fn
->blocks
.size (); block_no
++)
1178 block_t
*block
= &fn
->blocks
[block_no
];
1179 for (unsigned i
= 0; i
< block
->locations
.size (); i
++)
1181 /* Sort lines of locations. */
1182 sort (block
->locations
[i
].lines
.begin (),
1183 block
->locations
[i
].lines
.end ());
1185 if (!block
->locations
[i
].lines
.empty ())
1187 s
= &sources
[block
->locations
[i
].source_file_idx
];
1189 = block
->locations
[i
].lines
.back ();
1191 /* Record new lines for the function. */
1192 if (last_line
>= s
->lines
.size ())
1194 /* Record new lines for a source file. */
1195 s
->lines
.resize (last_line
+ 1);
1200 /* Allocate lines for group function, following start_line
1201 and end_line information of the function. */
1203 fn
->lines
.resize (fn
->end_line
- fn
->start_line
+ 1);
1206 solve_flow_graph (fn
);
1208 find_exception_blocks (fn
);
1215 /* The function was not in the executable -- some other
1216 instance must have been selected. */
1222 output_gcov_file (const char *file_name
, source_info
*src
)
1224 char *gcov_file_name
= make_gcov_file_name (file_name
, src
->coverage
.name
);
1226 if (src
->coverage
.lines
)
1228 FILE *gcov_file
= fopen (gcov_file_name
, "w");
1231 fnotice (stdout
, "Creating '%s'\n", gcov_file_name
);
1232 output_lines (gcov_file
, src
);
1233 if (ferror (gcov_file
))
1234 fnotice (stderr
, "Error writing output file '%s'\n",
1239 fnotice (stderr
, "Could not open output file '%s'\n", gcov_file_name
);
1243 unlink (gcov_file_name
);
1244 fnotice (stdout
, "Removing '%s'\n", gcov_file_name
);
1246 free (gcov_file_name
);
1250 generate_results (const char *file_name
)
1253 FILE *gcov_intermediate_file
= NULL
;
1254 char *gcov_intermediate_filename
= NULL
;
1256 for (fn
= functions
; fn
; fn
= fn
->next
)
1258 coverage_t coverage
;
1262 memset (&coverage
, 0, sizeof (coverage
));
1263 coverage
.name
= flag_demangled_names
? fn
->demangled_name
: fn
->name
;
1264 add_line_counts (flag_function_summary
? &coverage
: NULL
, fn
);
1265 if (flag_function_summary
)
1267 function_summary (&coverage
, "Function");
1268 fnotice (stdout
, "\n");
1276 needle
.name
= file_name
;
1277 vector
<name_map
>::iterator it
= std::find (names
.begin (), names
.end (),
1279 if (it
!= names
.end ())
1280 file_name
= sources
[it
->src
].coverage
.name
;
1282 file_name
= canonicalize_name (file_name
);
1285 if (flag_gcov_file
&& flag_intermediate_format
)
1287 /* Open the intermediate file. */
1288 gcov_intermediate_filename
= get_gcov_intermediate_filename (file_name
);
1289 gcov_intermediate_file
= fopen (gcov_intermediate_filename
, "w");
1290 if (!gcov_intermediate_file
)
1292 fnotice (stderr
, "Cannot open intermediate output file %s\n",
1293 gcov_intermediate_filename
);
1298 for (vector
<source_info
>::iterator it
= sources
.begin ();
1299 it
!= sources
.end (); it
++)
1301 source_info
*src
= &(*it
);
1302 if (flag_relative_only
)
1304 /* Ignore this source, if it is an absolute path (after
1305 source prefix removal). */
1306 char first
= src
->coverage
.name
[0];
1308 #if HAVE_DOS_BASED_FILE_SYSTEM
1309 if (first
&& src
->coverage
.name
[1] == ':')
1310 first
= src
->coverage
.name
[2];
1312 if (IS_DIR_SEPARATOR (first
))
1316 accumulate_line_counts (src
);
1317 function_summary (&src
->coverage
, "File");
1318 total_lines
+= src
->coverage
.lines
;
1319 total_executed
+= src
->coverage
.lines_executed
;
1322 if (flag_intermediate_format
)
1323 /* Output the intermediate format without requiring source
1324 files. This outputs a section to a *single* file. */
1325 output_intermediate_file (gcov_intermediate_file
, src
);
1327 output_gcov_file (file_name
, src
);
1328 fnotice (stdout
, "\n");
1332 if (flag_gcov_file
&& flag_intermediate_format
)
1334 /* Now we've finished writing the intermediate file. */
1335 fclose (gcov_intermediate_file
);
1336 XDELETEVEC (gcov_intermediate_filename
);
1340 executed_summary (total_lines
, total_executed
);
1343 /* Release all memory used. */
1346 release_structures (void)
1353 while ((fn
= functions
))
1355 functions
= fn
->next
;
1359 fn_end
= &functions
;
1362 /* Generate the names of the graph and data files. If OBJECT_DIRECTORY
1363 is not specified, these are named from FILE_NAME sans extension. If
1364 OBJECT_DIRECTORY is specified and is a directory, the files are in that
1365 directory, but named from the basename of the FILE_NAME, sans extension.
1366 Otherwise OBJECT_DIRECTORY is taken to be the name of the object *file*
1367 and the data files are named from that. */
1370 create_file_names (const char *file_name
)
1374 int length
= strlen (file_name
);
1377 /* Free previous file names. */
1378 free (bbg_file_name
);
1379 free (da_file_name
);
1380 da_file_name
= bbg_file_name
= NULL
;
1384 if (object_directory
&& object_directory
[0])
1388 length
+= strlen (object_directory
) + 2;
1389 name
= XNEWVEC (char, length
);
1392 base
= !stat (object_directory
, &status
) && S_ISDIR (status
.st_mode
);
1393 strcat (name
, object_directory
);
1394 if (base
&& (!IS_DIR_SEPARATOR (name
[strlen (name
) - 1])))
1399 name
= XNEWVEC (char, length
+ 1);
1400 strcpy (name
, file_name
);
1406 /* Append source file name. */
1407 const char *cptr
= lbasename (file_name
);
1408 strcat (name
, cptr
? cptr
: file_name
);
1411 /* Remove the extension. */
1412 cptr
= strrchr (CONST_CAST (char *, lbasename (name
)), '.');
1416 length
= strlen (name
);
1418 bbg_file_name
= XNEWVEC (char, length
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
1419 strcpy (bbg_file_name
, name
);
1420 strcpy (bbg_file_name
+ length
, GCOV_NOTE_SUFFIX
);
1422 da_file_name
= XNEWVEC (char, length
+ strlen (GCOV_DATA_SUFFIX
) + 1);
1423 strcpy (da_file_name
, name
);
1424 strcpy (da_file_name
+ length
, GCOV_DATA_SUFFIX
);
1430 /* Find or create a source file structure for FILE_NAME. Copies
1431 FILE_NAME on creation */
1434 find_source (const char *file_name
)
1441 file_name
= "<unknown>";
1444 needle
.name
= file_name
;
1446 vector
<name_map
>::iterator it
= std::find (names
.begin (), names
.end (),
1448 if (it
!= names
.end ())
1454 /* Not found, try the canonical name. */
1455 canon
= canonicalize_name (file_name
);
1456 needle
.name
= canon
;
1457 it
= std::find (names
.begin (), names
.end (), needle
);
1458 if (it
== names
.end ())
1460 /* Not found with canonical name, create a new source. */
1463 idx
= sources
.size ();
1464 needle
= name_map (canon
, idx
);
1465 names
.push_back (needle
);
1467 sources
.push_back (source_info ());
1468 src
= &sources
.back ();
1470 src
->coverage
.name
= src
->name
;
1473 #if HAVE_DOS_BASED_FILE_SYSTEM
1474 /* You lose if separators don't match exactly in the
1476 && !strncasecmp (source_prefix
, src
->coverage
.name
, source_length
)
1478 && !strncmp (source_prefix
, src
->coverage
.name
, source_length
)
1480 && IS_DIR_SEPARATOR (src
->coverage
.name
[source_length
]))
1481 src
->coverage
.name
+= source_length
+ 1;
1482 if (!stat (src
->name
, &status
))
1483 src
->file_time
= status
.st_mtime
;
1488 needle
.name
= file_name
;
1489 if (std::find (names
.begin (), names
.end (), needle
) == names
.end ())
1491 /* Append the non-canonical name. */
1492 names
.push_back (name_map (xstrdup (file_name
), idx
));
1495 /* Resort the name map. */
1496 std::sort (names
.begin (), names
.end ());
1499 if (sources
[idx
].file_time
> bbg_file_time
)
1501 static int info_emitted
;
1503 fnotice (stderr
, "%s:source file is newer than notes file '%s'\n",
1504 file_name
, bbg_file_name
);
1508 "(the message is displayed only once per source file)\n");
1511 sources
[idx
].file_time
= 0;
1517 /* Read the notes file. Return list of functions read -- in reverse order. */
1520 read_graph_file (void)
1523 unsigned current_tag
= 0;
1524 function_t
*fn
= NULL
;
1525 function_t
*fns
= NULL
;
1526 function_t
**fns_end
= &fns
;
1529 if (!gcov_open (bbg_file_name
, 1))
1531 fnotice (stderr
, "%s:cannot open notes file\n", bbg_file_name
);
1534 bbg_file_time
= gcov_time ();
1535 if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC
))
1537 fnotice (stderr
, "%s:not a gcov notes file\n", bbg_file_name
);
1542 version
= gcov_read_unsigned ();
1543 if (version
!= GCOV_VERSION
)
1547 GCOV_UNSIGNED2STRING (v
, version
);
1548 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1550 fnotice (stderr
, "%s:version '%.4s', prefer '%.4s'\n",
1551 bbg_file_name
, v
, e
);
1553 bbg_stamp
= gcov_read_unsigned ();
1554 bbg_supports_has_unexecuted_blocks
= gcov_read_unsigned ();
1556 while ((tag
= gcov_read_unsigned ()))
1558 unsigned length
= gcov_read_unsigned ();
1559 gcov_position_t base
= gcov_position ();
1561 if (tag
== GCOV_TAG_FUNCTION
)
1563 char *function_name
;
1565 unsigned lineno_checksum
, cfg_checksum
;
1567 ident
= gcov_read_unsigned ();
1568 lineno_checksum
= gcov_read_unsigned ();
1569 cfg_checksum
= gcov_read_unsigned ();
1570 function_name
= xstrdup (gcov_read_string ());
1571 unsigned artificial
= gcov_read_unsigned ();
1572 unsigned src_idx
= find_source (gcov_read_string ());
1573 unsigned start_line
= gcov_read_unsigned ();
1574 unsigned start_column
= gcov_read_unsigned ();
1575 unsigned end_line
= gcov_read_unsigned ();
1577 fn
= new function_t
;
1578 fn
->name
= function_name
;
1579 if (flag_demangled_names
)
1581 fn
->demangled_name
= cplus_demangle (fn
->name
, DMGL_PARAMS
);
1582 if (!fn
->demangled_name
)
1583 fn
->demangled_name
= fn
->name
;
1586 fn
->lineno_checksum
= lineno_checksum
;
1587 fn
->cfg_checksum
= cfg_checksum
;
1589 fn
->start_line
= start_line
;
1590 fn
->start_column
= start_column
;
1591 fn
->end_line
= end_line
;
1592 fn
->artificial
= artificial
;
1596 fns_end
= &fn
->next
;
1599 else if (fn
&& tag
== GCOV_TAG_BLOCKS
)
1601 if (!fn
->blocks
.empty ())
1602 fnotice (stderr
, "%s:already seen blocks for '%s'\n",
1603 bbg_file_name
, fn
->name
);
1605 fn
->blocks
.resize (gcov_read_unsigned ());
1607 else if (fn
&& tag
== GCOV_TAG_ARCS
)
1609 unsigned src
= gcov_read_unsigned ();
1610 fn
->blocks
[src
].id
= src
;
1611 unsigned num_dests
= GCOV_TAG_ARCS_NUM (length
);
1612 block_t
*src_blk
= &fn
->blocks
[src
];
1613 unsigned mark_catches
= 0;
1614 struct arc_info
*arc
;
1616 if (src
>= fn
->blocks
.size () || fn
->blocks
[src
].succ
)
1621 unsigned dest
= gcov_read_unsigned ();
1622 unsigned flags
= gcov_read_unsigned ();
1624 if (dest
>= fn
->blocks
.size ())
1626 arc
= XCNEW (arc_t
);
1628 arc
->dst
= &fn
->blocks
[dest
];
1632 arc
->count_valid
= 0;
1633 arc
->on_tree
= !!(flags
& GCOV_ARC_ON_TREE
);
1634 arc
->fake
= !!(flags
& GCOV_ARC_FAKE
);
1635 arc
->fall_through
= !!(flags
& GCOV_ARC_FALLTHROUGH
);
1637 arc
->succ_next
= src_blk
->succ
;
1638 src_blk
->succ
= arc
;
1639 src_blk
->num_succ
++;
1641 arc
->pred_next
= fn
->blocks
[dest
].pred
;
1642 fn
->blocks
[dest
].pred
= arc
;
1643 fn
->blocks
[dest
].num_pred
++;
1649 /* Exceptional exit from this function, the
1650 source block must be a call. */
1651 fn
->blocks
[src
].is_call_site
= 1;
1652 arc
->is_call_non_return
= 1;
1657 /* Non-local return from a callee of this
1658 function. The destination block is a setjmp. */
1659 arc
->is_nonlocal_return
= 1;
1660 fn
->blocks
[dest
].is_nonlocal_return
= 1;
1670 /* We have a fake exit from this block. The other
1671 non-fall through exits must be to catch handlers.
1672 Mark them as catch arcs. */
1674 for (arc
= src_blk
->succ
; arc
; arc
= arc
->succ_next
)
1675 if (!arc
->fake
&& !arc
->fall_through
)
1682 else if (fn
&& tag
== GCOV_TAG_LINES
)
1684 unsigned blockno
= gcov_read_unsigned ();
1685 block_t
*block
= &fn
->blocks
[blockno
];
1687 if (blockno
>= fn
->blocks
.size ())
1692 unsigned lineno
= gcov_read_unsigned ();
1695 block
->locations
.back ().lines
.push_back (lineno
);
1698 const char *file_name
= gcov_read_string ();
1702 block
->locations
.push_back (block_location_info
1703 (find_source (file_name
)));
1707 else if (current_tag
&& !GCOV_TAG_IS_SUBTAG (current_tag
, tag
))
1712 gcov_sync (base
, length
);
1713 if (gcov_is_error ())
1716 fnotice (stderr
, "%s:corrupted\n", bbg_file_name
);
1723 fnotice (stderr
, "%s:no functions found\n", bbg_file_name
);
1728 /* Reads profiles from the count file and attach to each
1729 function. Return nonzero if fatal error. */
1732 read_count_file (function_t
*fns
)
1737 function_t
*fn
= NULL
;
1740 if (!gcov_open (da_file_name
, 1))
1742 fnotice (stderr
, "%s:cannot open data file, assuming not executed\n",
1747 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
1749 fnotice (stderr
, "%s:not a gcov data file\n", da_file_name
);
1754 version
= gcov_read_unsigned ();
1755 if (version
!= GCOV_VERSION
)
1759 GCOV_UNSIGNED2STRING (v
, version
);
1760 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1762 fnotice (stderr
, "%s:version '%.4s', prefer version '%.4s'\n",
1763 da_file_name
, v
, e
);
1765 tag
= gcov_read_unsigned ();
1766 if (tag
!= bbg_stamp
)
1768 fnotice (stderr
, "%s:stamp mismatch with notes file\n", da_file_name
);
1772 while ((tag
= gcov_read_unsigned ()))
1774 unsigned length
= gcov_read_unsigned ();
1775 unsigned long base
= gcov_position ();
1777 if (tag
== GCOV_TAG_PROGRAM_SUMMARY
)
1779 struct gcov_summary summary
;
1780 gcov_read_summary (&summary
);
1781 object_runs
+= summary
.ctrs
[GCOV_COUNTER_ARCS
].runs
;
1784 else if (tag
== GCOV_TAG_FUNCTION
&& !length
)
1786 else if (tag
== GCOV_TAG_FUNCTION
&& length
== GCOV_TAG_FUNCTION_LENGTH
)
1789 struct function_info
*fn_n
;
1791 /* Try to find the function in the list. To speed up the
1792 search, first start from the last function found. */
1793 ident
= gcov_read_unsigned ();
1795 for (fn
= fn
? fn
->next
: NULL
; ; fn
= fn
->next
)
1799 else if ((fn
= fn_n
))
1803 fnotice (stderr
, "%s:unknown function '%u'\n",
1804 da_file_name
, ident
);
1807 if (fn
->ident
== ident
)
1813 else if (gcov_read_unsigned () != fn
->lineno_checksum
1814 || gcov_read_unsigned () != fn
->cfg_checksum
)
1817 fnotice (stderr
, "%s:profile mismatch for '%s'\n",
1818 da_file_name
, fn
->name
);
1822 else if (tag
== GCOV_TAG_FOR_COUNTER (GCOV_COUNTER_ARCS
) && fn
)
1824 if (length
!= GCOV_TAG_COUNTER_LENGTH (fn
->num_counts
))
1828 fn
->counts
= XCNEWVEC (gcov_type
, fn
->num_counts
);
1830 for (ix
= 0; ix
!= fn
->num_counts
; ix
++)
1831 fn
->counts
[ix
] += gcov_read_counter ();
1833 gcov_sync (base
, length
);
1834 if ((error
= gcov_is_error ()))
1838 ? N_("%s:overflowed\n")
1839 : N_("%s:corrupted\n"),
1849 /* Solve the flow graph. Propagate counts from the instrumented arcs
1850 to the blocks and the uninstrumented arcs. */
1853 solve_flow_graph (function_t
*fn
)
1857 gcov_type
*count_ptr
= fn
->counts
;
1859 block_t
*valid_blocks
= NULL
; /* valid, but unpropagated blocks. */
1860 block_t
*invalid_blocks
= NULL
; /* invalid, but inferable blocks. */
1862 /* The arcs were built in reverse order. Fix that now. */
1863 for (ix
= fn
->blocks
.size (); ix
--;)
1865 arc_t
*arc_p
, *arc_n
;
1867 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].succ
; arc
;
1868 arc_p
= arc
, arc
= arc_n
)
1870 arc_n
= arc
->succ_next
;
1871 arc
->succ_next
= arc_p
;
1873 fn
->blocks
[ix
].succ
= arc_p
;
1875 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].pred
; arc
;
1876 arc_p
= arc
, arc
= arc_n
)
1878 arc_n
= arc
->pred_next
;
1879 arc
->pred_next
= arc_p
;
1881 fn
->blocks
[ix
].pred
= arc_p
;
1884 if (fn
->blocks
.size () < 2)
1885 fnotice (stderr
, "%s:'%s' lacks entry and/or exit blocks\n",
1886 bbg_file_name
, fn
->name
);
1889 if (fn
->blocks
[ENTRY_BLOCK
].num_pred
)
1890 fnotice (stderr
, "%s:'%s' has arcs to entry block\n",
1891 bbg_file_name
, fn
->name
);
1893 /* We can't deduce the entry block counts from the lack of
1895 fn
->blocks
[ENTRY_BLOCK
].num_pred
= ~(unsigned)0;
1897 if (fn
->blocks
[EXIT_BLOCK
].num_succ
)
1898 fnotice (stderr
, "%s:'%s' has arcs from exit block\n",
1899 bbg_file_name
, fn
->name
);
1901 /* Likewise, we can't deduce exit block counts from the lack
1902 of its successors. */
1903 fn
->blocks
[EXIT_BLOCK
].num_succ
= ~(unsigned)0;
1906 /* Propagate the measured counts, this must be done in the same
1907 order as the code in profile.c */
1908 for (unsigned i
= 0; i
< fn
->blocks
.size (); i
++)
1910 blk
= &fn
->blocks
[i
];
1911 block_t
const *prev_dst
= NULL
;
1912 int out_of_order
= 0;
1913 int non_fake_succ
= 0;
1915 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1923 arc
->count
= *count_ptr
++;
1924 arc
->count_valid
= 1;
1926 arc
->dst
->num_pred
--;
1928 if (prev_dst
&& prev_dst
> arc
->dst
)
1930 prev_dst
= arc
->dst
;
1932 if (non_fake_succ
== 1)
1934 /* If there is only one non-fake exit, it is an
1935 unconditional branch. */
1936 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1939 arc
->is_unconditional
= 1;
1940 /* If this block is instrumenting a call, it might be
1941 an artificial block. It is not artificial if it has
1942 a non-fallthrough exit, or the destination of this
1943 arc has more than one entry. Mark the destination
1944 block as a return site, if none of those conditions
1946 if (blk
->is_call_site
&& arc
->fall_through
1947 && arc
->dst
->pred
== arc
&& !arc
->pred_next
)
1948 arc
->dst
->is_call_return
= 1;
1952 /* Sort the successor arcs into ascending dst order. profile.c
1953 normally produces arcs in the right order, but sometimes with
1954 one or two out of order. We're not using a particularly
1958 arc_t
*start
= blk
->succ
;
1959 unsigned changes
= 1;
1963 arc_t
*arc
, *arc_p
, *arc_n
;
1966 for (arc_p
= NULL
, arc
= start
; (arc_n
= arc
->succ_next
);)
1968 if (arc
->dst
> arc_n
->dst
)
1972 arc_p
->succ_next
= arc_n
;
1975 arc
->succ_next
= arc_n
->succ_next
;
1976 arc_n
->succ_next
= arc
;
1989 /* Place it on the invalid chain, it will be ignored if that's
1991 blk
->invalid_chain
= 1;
1992 blk
->chain
= invalid_blocks
;
1993 invalid_blocks
= blk
;
1996 while (invalid_blocks
|| valid_blocks
)
1998 while ((blk
= invalid_blocks
))
2000 gcov_type total
= 0;
2003 invalid_blocks
= blk
->chain
;
2004 blk
->invalid_chain
= 0;
2006 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
2007 total
+= arc
->count
;
2008 else if (!blk
->num_pred
)
2009 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
2010 total
+= arc
->count
;
2015 blk
->count_valid
= 1;
2016 blk
->chain
= valid_blocks
;
2017 blk
->valid_chain
= 1;
2020 while ((blk
= valid_blocks
))
2023 arc_t
*arc
, *inv_arc
;
2025 valid_blocks
= blk
->chain
;
2026 blk
->valid_chain
= 0;
2027 if (blk
->num_succ
== 1)
2033 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
2035 total
-= arc
->count
;
2036 if (!arc
->count_valid
)
2040 inv_arc
->count_valid
= 1;
2041 inv_arc
->count
= total
;
2044 if (dst
->count_valid
)
2046 if (dst
->num_pred
== 1 && !dst
->valid_chain
)
2048 dst
->chain
= valid_blocks
;
2049 dst
->valid_chain
= 1;
2055 if (!dst
->num_pred
&& !dst
->invalid_chain
)
2057 dst
->chain
= invalid_blocks
;
2058 dst
->invalid_chain
= 1;
2059 invalid_blocks
= dst
;
2063 if (blk
->num_pred
== 1)
2069 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
2071 total
-= arc
->count
;
2072 if (!arc
->count_valid
)
2076 inv_arc
->count_valid
= 1;
2077 inv_arc
->count
= total
;
2080 if (src
->count_valid
)
2082 if (src
->num_succ
== 1 && !src
->valid_chain
)
2084 src
->chain
= valid_blocks
;
2085 src
->valid_chain
= 1;
2091 if (!src
->num_succ
&& !src
->invalid_chain
)
2093 src
->chain
= invalid_blocks
;
2094 src
->invalid_chain
= 1;
2095 invalid_blocks
= src
;
2102 /* If the graph has been correctly solved, every block will have a
2104 for (unsigned i
= 0; ix
< fn
->blocks
.size (); i
++)
2105 if (!fn
->blocks
[i
].count_valid
)
2107 fnotice (stderr
, "%s:graph is unsolvable for '%s'\n",
2108 bbg_file_name
, fn
->name
);
2113 /* Mark all the blocks only reachable via an incoming catch. */
2116 find_exception_blocks (function_t
*fn
)
2119 block_t
**queue
= XALLOCAVEC (block_t
*, fn
->blocks
.size ());
2121 /* First mark all blocks as exceptional. */
2122 for (ix
= fn
->blocks
.size (); ix
--;)
2123 fn
->blocks
[ix
].exceptional
= 1;
2125 /* Now mark all the blocks reachable via non-fake edges */
2126 queue
[0] = &fn
->blocks
[0];
2127 queue
[0]->exceptional
= 0;
2130 block_t
*block
= queue
[--ix
];
2133 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2134 if (!arc
->fake
&& !arc
->is_throw
&& arc
->dst
->exceptional
)
2136 arc
->dst
->exceptional
= 0;
2137 queue
[ix
++] = arc
->dst
;
2143 /* Increment totals in COVERAGE according to arc ARC. */
2146 add_branch_counts (coverage_t
*coverage
, const arc_t
*arc
)
2148 if (arc
->is_call_non_return
)
2151 if (arc
->src
->count
)
2152 coverage
->calls_executed
++;
2154 else if (!arc
->is_unconditional
)
2156 coverage
->branches
++;
2157 if (arc
->src
->count
)
2158 coverage
->branches_executed
++;
2160 coverage
->branches_taken
++;
2164 /* Format COUNT, if flag_human_readable_numbers is set, return it human
2168 format_count (gcov_type count
)
2170 static char buffer
[64];
2171 const char *units
= " kMGTPEZY";
2173 if (count
< 1000 || !flag_human_readable_numbers
)
2175 sprintf (buffer
, "%" PRId64
, count
);
2180 gcov_type divisor
= 1;
2181 for (i
= 0; units
[i
+1]; i
++, divisor
*= 1000)
2183 if (count
+ divisor
/ 2 < 1000 * divisor
)
2186 gcov_type r
= (count
+ divisor
/ 2) / divisor
;
2187 sprintf (buffer
, "%" PRId64
"%c", r
, units
[i
]);
2191 /* Format a GCOV_TYPE integer as either a percent ratio, or absolute
2192 count. If dp >= 0, format TOP/BOTTOM * 100 to DP decimal places.
2193 If DP is zero, no decimal point is printed. Only print 100% when
2194 TOP==BOTTOM and only print 0% when TOP=0. If dp < 0, then simply
2195 format TOP. Return pointer to a static string. */
2198 format_gcov (gcov_type top
, gcov_type bottom
, int dp
)
2200 static char buffer
[20];
2202 /* Handle invalid values that would result in a misleading value. */
2203 if (bottom
!= 0 && top
> bottom
&& dp
>= 0)
2205 sprintf (buffer
, "NAN %%");
2211 float ratio
= bottom
? (float)top
/ bottom
: 0;
2213 unsigned limit
= 100;
2216 for (ix
= dp
; ix
--; )
2219 percent
= (unsigned) (ratio
* limit
+ (float)0.5);
2220 if (percent
<= 0 && top
)
2222 else if (percent
>= limit
&& top
!= bottom
)
2223 percent
= limit
- 1;
2224 ix
= sprintf (buffer
, "%.*u%%", dp
+ 1, percent
);
2230 buffer
[ix
+1] = buffer
[ix
];
2234 buffer
[ix
+ 1] = '.';
2238 return format_count (top
);
2243 /* Summary of execution */
2246 executed_summary (unsigned lines
, unsigned executed
)
2249 fnotice (stdout
, "Lines executed:%s of %d\n",
2250 format_gcov (executed
, lines
, 2), lines
);
2252 fnotice (stdout
, "No executable lines\n");
2255 /* Output summary info for a function or file. */
2258 function_summary (const coverage_t
*coverage
, const char *title
)
2260 fnotice (stdout
, "%s '%s'\n", title
, coverage
->name
);
2261 executed_summary (coverage
->lines
, coverage
->lines_executed
);
2265 if (coverage
->branches
)
2267 fnotice (stdout
, "Branches executed:%s of %d\n",
2268 format_gcov (coverage
->branches_executed
,
2269 coverage
->branches
, 2),
2270 coverage
->branches
);
2271 fnotice (stdout
, "Taken at least once:%s of %d\n",
2272 format_gcov (coverage
->branches_taken
,
2273 coverage
->branches
, 2),
2274 coverage
->branches
);
2277 fnotice (stdout
, "No branches\n");
2278 if (coverage
->calls
)
2279 fnotice (stdout
, "Calls executed:%s of %d\n",
2280 format_gcov (coverage
->calls_executed
, coverage
->calls
, 2),
2283 fnotice (stdout
, "No calls\n");
2287 /* Canonicalize the filename NAME by canonicalizing directory
2288 separators, eliding . components and resolving .. components
2289 appropriately. Always returns a unique string. */
2292 canonicalize_name (const char *name
)
2294 /* The canonical name cannot be longer than the incoming name. */
2295 char *result
= XNEWVEC (char, strlen (name
) + 1);
2296 const char *base
= name
, *probe
;
2301 #if HAVE_DOS_BASED_FILE_SYSTEM
2302 if (base
[0] && base
[1] == ':')
2304 result
[0] = base
[0];
2310 for (dd_base
= ptr
; *base
; base
= probe
)
2314 for (probe
= base
; *probe
; probe
++)
2315 if (IS_DIR_SEPARATOR (*probe
))
2319 if (len
== 1 && base
[0] == '.')
2320 /* Elide a '.' directory */
2322 else if (len
== 2 && base
[0] == '.' && base
[1] == '.')
2324 /* '..', we can only elide it and the previous directory, if
2325 we're not a symlink. */
2326 struct stat ATTRIBUTE_UNUSED buf
;
2330 #if defined (S_ISLNK)
2331 /* S_ISLNK is not POSIX.1-1996. */
2332 || stat (result
, &buf
) || S_ISLNK (buf
.st_mode
)
2336 /* Cannot elide, or unreadable or a symlink. */
2337 dd_base
= ptr
+ 2 + slash
;
2340 while (ptr
!= dd_base
&& *ptr
!= '/')
2342 slash
= ptr
!= result
;
2347 /* Regular pathname component. */
2350 memcpy (ptr
, base
, len
);
2355 for (; IS_DIR_SEPARATOR (*probe
); probe
++)
2363 /* Print hex representation of 16 bytes from SUM and write it to BUFFER. */
2366 md5sum_to_hex (const char *sum
, char *buffer
)
2368 for (unsigned i
= 0; i
< 16; i
++)
2369 sprintf (buffer
+ (2 * i
), "%02x", (unsigned char)sum
[i
]);
2372 /* Generate an output file name. INPUT_NAME is the canonicalized main
2373 input file and SRC_NAME is the canonicalized file name.
2374 LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
2375 long_output_names we prepend the processed name of the input file
2376 to each output name (except when the current source file is the
2377 input file, so you don't get a double concatenation). The two
2378 components are separated by '##'. With preserve_paths we create a
2379 filename from all path components of the source file, replacing '/'
2380 with '#', and .. with '^', without it we simply take the basename
2381 component. (Remember, the canonicalized name will already have
2382 elided '.' components and converted \\ separators.) */
2385 make_gcov_file_name (const char *input_name
, const char *src_name
)
2390 if (flag_long_names
&& input_name
&& strcmp (src_name
, input_name
))
2392 /* Generate the input filename part. */
2393 result
= XNEWVEC (char, strlen (input_name
) + strlen (src_name
) + 10);
2396 ptr
= mangle_name (input_name
, ptr
);
2397 ptr
[0] = ptr
[1] = '#';
2402 result
= XNEWVEC (char, strlen (src_name
) + 10);
2406 ptr
= mangle_name (src_name
, ptr
);
2407 strcpy (ptr
, ".gcov");
2409 /* When hashing filenames, we shorten them by only using the filename
2410 component and appending a hash of the full (mangled) pathname. */
2411 if (flag_hash_filenames
)
2415 char md5sum_hex
[33];
2417 md5_init_ctx (&ctx
);
2418 md5_process_bytes (src_name
, strlen (src_name
), &ctx
);
2419 md5_finish_ctx (&ctx
, md5sum
);
2420 md5sum_to_hex (md5sum
, md5sum_hex
);
2423 result
= XNEWVEC (char, strlen (src_name
) + 50);
2425 ptr
= mangle_name (src_name
, ptr
);
2426 ptr
[0] = ptr
[1] = '#';
2428 memcpy (ptr
, md5sum_hex
, 32);
2430 strcpy (ptr
, ".gcov");
2437 mangle_name (char const *base
, char *ptr
)
2441 /* Generate the source filename part. */
2442 if (!flag_preserve_paths
)
2444 base
= lbasename (base
);
2445 len
= strlen (base
);
2446 memcpy (ptr
, base
, len
);
2451 /* Convert '/' to '#', convert '..' to '^',
2452 convert ':' to '~' on DOS based file system. */
2455 #if HAVE_DOS_BASED_FILE_SYSTEM
2456 if (base
[0] && base
[1] == ':')
2464 for (; *base
; base
= probe
)
2468 for (probe
= base
; *probe
; probe
++)
2472 if (len
== 2 && base
[0] == '.' && base
[1] == '.')
2476 memcpy (ptr
, base
, len
);
2490 /* Scan through the bb_data for each line in the block, increment
2491 the line number execution count indicated by the execution count of
2492 the appropriate basic block. */
2495 add_line_counts (coverage_t
*coverage
, function_t
*fn
)
2497 bool has_any_line
= false;
2498 /* Scan each basic block. */
2499 for (unsigned ix
= 0; ix
!= fn
->blocks
.size (); ix
++)
2501 line_info
*line
= NULL
;
2502 block_t
*block
= &fn
->blocks
[ix
];
2503 if (block
->count
&& ix
&& ix
+ 1 != fn
->blocks
.size ())
2504 fn
->blocks_executed
++;
2505 for (unsigned i
= 0; i
< block
->locations
.size (); i
++)
2507 unsigned src_idx
= block
->locations
[i
].source_file_idx
;
2508 vector
<unsigned> &lines
= block
->locations
[i
].lines
;
2510 block
->cycle
.arc
= NULL
;
2511 block
->cycle
.ident
= ~0U;
2513 for (unsigned j
= 0; j
< lines
.size (); j
++)
2515 unsigned ln
= lines
[j
];
2517 /* Line belongs to a function that is in a group. */
2518 if (fn
->group_line_p (ln
, src_idx
))
2520 gcc_assert (lines
[j
] - fn
->start_line
< fn
->lines
.size ());
2521 line
= &(fn
->lines
[lines
[j
] - fn
->start_line
]);
2523 if (!block
->exceptional
)
2525 line
->unexceptional
= 1;
2526 if (block
->count
== 0)
2527 line
->has_unexecuted_block
= 1;
2529 line
->count
+= block
->count
;
2533 gcc_assert (ln
< sources
[src_idx
].lines
.size ());
2534 line
= &(sources
[src_idx
].lines
[ln
]);
2539 if (!line
->count
&& block
->count
)
2540 coverage
->lines_executed
++;
2543 if (!block
->exceptional
)
2545 line
->unexceptional
= 1;
2546 if (block
->count
== 0)
2547 line
->has_unexecuted_block
= 1;
2549 line
->count
+= block
->count
;
2553 has_any_line
= true;
2555 if (!ix
|| ix
+ 1 == fn
->blocks
.size ())
2556 /* Entry or exit block. */;
2557 else if (line
!= NULL
)
2559 line
->blocks
.push_back (block
);
2565 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2566 line
->branches
.push_back (arc
);
2573 fnotice (stderr
, "%s:no lines for '%s'\n", bbg_file_name
, fn
->name
);
2576 /* Accumulate info for LINE that belongs to SRC source file. If ADD_COVERAGE
2577 is set to true, update source file summary. */
2579 static void accumulate_line_info (line_info
*line
, source_info
*src
,
2583 for (vector
<arc_info
*>::iterator it
= line
->branches
.begin ();
2584 it
!= line
->branches
.end (); it
++)
2585 add_branch_counts (&src
->coverage
, *it
);
2587 if (!line
->blocks
.empty ())
2589 /* The user expects the line count to be the number of times
2590 a line has been executed. Simply summing the block count
2591 will give an artificially high number. The Right Thing
2592 is to sum the entry counts to the graph of blocks on this
2593 line, then find the elementary cycles of the local graph
2594 and add the transition counts of those cycles. */
2595 gcov_type count
= 0;
2597 /* Cycle detection. */
2598 for (vector
<block_t
*>::iterator it
= line
->blocks
.begin ();
2599 it
!= line
->blocks
.end (); it
++)
2601 for (arc_t
*arc
= (*it
)->pred
; arc
; arc
= arc
->pred_next
)
2602 if (!line
->has_block (arc
->src
))
2603 count
+= arc
->count
;
2604 for (arc_t
*arc
= (*it
)->succ
; arc
; arc
= arc
->succ_next
)
2605 arc
->cs_count
= arc
->count
;
2608 /* Now, add the count of loops entirely on this line. */
2609 count
+= get_cycles_count (*line
);
2610 line
->count
= count
;
2613 if (line
->exists
&& add_coverage
)
2615 src
->coverage
.lines
++;
2617 src
->coverage
.lines_executed
++;
2621 /* Accumulate the line counts of a file. */
2624 accumulate_line_counts (source_info
*src
)
2626 /* First work on group functions. */
2627 for (vector
<function_t
*>::iterator it
= src
->functions
.begin ();
2628 it
!= src
->functions
.end (); it
++)
2630 function_info
*fn
= *it
;
2632 if (fn
->src
!= src
->index
|| !fn
->is_group
)
2635 for (vector
<line_info
>::iterator it2
= fn
->lines
.begin ();
2636 it2
!= fn
->lines
.end (); it2
++)
2638 line_info
*line
= &(*it2
);
2639 accumulate_line_info (line
, src
, false);
2643 /* Work on global lines that line in source file SRC. */
2644 for (vector
<line_info
>::iterator it
= src
->lines
.begin ();
2645 it
!= src
->lines
.end (); it
++)
2646 accumulate_line_info (&(*it
), src
, true);
2648 /* If not using intermediate mode, sum lines of group functions and
2649 add them to lines that live in a source file. */
2650 if (!flag_intermediate_format
)
2651 for (vector
<function_t
*>::iterator it
= src
->functions
.begin ();
2652 it
!= src
->functions
.end (); it
++)
2654 function_info
*fn
= *it
;
2656 if (fn
->src
!= src
->index
|| !fn
->is_group
)
2659 for (unsigned i
= 0; i
< fn
->lines
.size (); i
++)
2661 line_info
*fn_line
= &fn
->lines
[i
];
2662 if (fn_line
->exists
)
2664 unsigned ln
= fn
->start_line
+ i
;
2665 line_info
*src_line
= &src
->lines
[ln
];
2667 if (!src_line
->exists
)
2668 src
->coverage
.lines
++;
2669 if (!src_line
->count
&& fn_line
->count
)
2670 src
->coverage
.lines_executed
++;
2672 src_line
->count
+= fn_line
->count
;
2673 src_line
->exists
= 1;
2675 if (fn_line
->has_unexecuted_block
)
2676 src_line
->has_unexecuted_block
= 1;
2678 if (fn_line
->unexceptional
)
2679 src_line
->unexceptional
= 1;
2685 /* Output information about ARC number IX. Returns nonzero if
2686 anything is output. */
2689 output_branch_count (FILE *gcov_file
, int ix
, const arc_t
*arc
)
2691 if (arc
->is_call_non_return
)
2693 if (arc
->src
->count
)
2695 fnotice (gcov_file
, "call %2d returned %s\n", ix
,
2696 format_gcov (arc
->src
->count
- arc
->count
,
2697 arc
->src
->count
, -flag_counts
));
2700 fnotice (gcov_file
, "call %2d never executed\n", ix
);
2702 else if (!arc
->is_unconditional
)
2704 if (arc
->src
->count
)
2705 fnotice (gcov_file
, "branch %2d taken %s%s", ix
,
2706 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
),
2707 arc
->fall_through
? " (fallthrough)"
2708 : arc
->is_throw
? " (throw)" : "");
2710 fnotice (gcov_file
, "branch %2d never executed", ix
);
2713 fnotice (gcov_file
, " (BB %d)", arc
->dst
->id
);
2715 fnotice (gcov_file
, "\n");
2717 else if (flag_unconditional
&& !arc
->dst
->is_call_return
)
2719 if (arc
->src
->count
)
2720 fnotice (gcov_file
, "unconditional %2d taken %s\n", ix
,
2721 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
));
2723 fnotice (gcov_file
, "unconditional %2d never executed\n", ix
);
2731 read_line (FILE *file
)
2733 static char *string
;
2734 static size_t string_len
;
2741 string
= XNEWVEC (char, string_len
);
2744 while ((ptr
= fgets (string
+ pos
, string_len
- pos
, file
)))
2746 size_t len
= strlen (string
+ pos
);
2748 if (len
&& string
[pos
+ len
- 1] == '\n')
2750 string
[pos
+ len
- 1] = 0;
2754 /* If the file contains NUL characters or an incomplete
2755 last line, which can happen more than once in one run,
2756 we have to avoid doubling the STRING_LEN unnecessarily. */
2757 if (pos
> string_len
/ 2)
2760 string
= XRESIZEVEC (char, string
, string_len
);
2764 return pos
? string
: NULL
;
2767 /* Pad string S with spaces from left to have total width equal to 9. */
2770 pad_count_string (string
&s
)
2773 s
.insert (0, 9 - s
.size (), ' ');
2776 /* Print GCOV line beginning to F stream. If EXISTS is set to true, the
2777 line exists in source file. UNEXCEPTIONAL indicated that it's not in
2778 an exceptional statement. The output is printed for LINE_NUM of given
2779 COUNT of executions. EXCEPTIONAL_STRING and UNEXCEPTIONAL_STRING are
2780 used to indicate non-executed blocks. */
2783 output_line_beginning (FILE *f
, bool exists
, bool unexceptional
,
2784 bool has_unexecuted_block
,
2785 gcov_type count
, unsigned line_num
,
2786 const char *exceptional_string
,
2787 const char *unexceptional_string
)
2794 s
= format_gcov (count
, 0, -1);
2795 if (has_unexecuted_block
2796 && bbg_supports_has_unexecuted_blocks
)
2798 if (flag_use_colors
)
2800 pad_count_string (s
);
2801 s
.insert (0, SGR_SEQ (COLOR_BG_MAGENTA
2802 COLOR_SEPARATOR COLOR_FG_WHITE
));
2808 pad_count_string (s
);
2812 if (flag_use_colors
)
2815 pad_count_string (s
);
2817 s
.insert (0, SGR_SEQ (COLOR_BG_RED
2818 COLOR_SEPARATOR COLOR_FG_WHITE
));
2820 s
.insert (0, SGR_SEQ (COLOR_BG_CYAN
2821 COLOR_SEPARATOR COLOR_FG_WHITE
));
2826 s
= unexceptional
? unexceptional_string
: exceptional_string
;
2827 pad_count_string (s
);
2834 pad_count_string (s
);
2837 fprintf (f
, "%s:%5u", s
.c_str (), line_num
);
2841 print_source_line (FILE *f
, const vector
<const char *> &source_lines
,
2844 gcc_assert (line
>= 1);
2845 gcc_assert (line
<= source_lines
.size ());
2847 fprintf (f
, ":%s\n", source_lines
[line
- 1]);
2850 /* Output line details for LINE and print it to F file. LINE lives on
2854 output_line_details (FILE *f
, const line_info
*line
, unsigned line_num
)
2856 if (flag_all_blocks
)
2862 for (vector
<block_t
*>::const_iterator it
= line
->blocks
.begin ();
2863 it
!= line
->blocks
.end (); it
++)
2865 if (!(*it
)->is_call_return
)
2867 output_line_beginning (f
, line
->exists
,
2868 (*it
)->exceptional
, false,
2869 (*it
)->count
, line_num
,
2871 fprintf (f
, "-block %2d", ix
++);
2873 fprintf (f
, " (BB %u)", (*it
)->id
);
2877 for (arc
= (*it
)->succ
; arc
; arc
= arc
->succ_next
)
2878 jx
+= output_branch_count (f
, jx
, arc
);
2881 else if (flag_branches
)
2886 for (vector
<arc_t
*>::const_iterator it
= line
->branches
.begin ();
2887 it
!= line
->branches
.end (); it
++)
2888 ix
+= output_branch_count (f
, ix
, (*it
));
2892 /* Output detail statistics about function FN to file F. */
2895 output_function_details (FILE *f
, const function_info
*fn
)
2900 arc_t
*arc
= fn
->blocks
[EXIT_BLOCK
].pred
;
2901 gcov_type return_count
= fn
->blocks
[EXIT_BLOCK
].count
;
2902 gcov_type called_count
= fn
->blocks
[ENTRY_BLOCK
].count
;
2904 for (; arc
; arc
= arc
->pred_next
)
2906 return_count
-= arc
->count
;
2908 fprintf (f
, "function %s",
2909 flag_demangled_names
? fn
->demangled_name
: fn
->name
);
2910 fprintf (f
, " called %s",
2911 format_gcov (called_count
, 0, -1));
2912 fprintf (f
, " returned %s",
2913 format_gcov (return_count
, called_count
, 0));
2914 fprintf (f
, " blocks executed %s",
2915 format_gcov (fn
->blocks_executed
, fn
->blocks
.size () - 2,
2920 /* Read in the source file one line at a time, and output that line to
2921 the gcov file preceded by its execution count and other
2925 output_lines (FILE *gcov_file
, const source_info
*src
)
2927 #define DEFAULT_LINE_START " -: 0:"
2928 #define FN_SEPARATOR "------------------\n"
2933 fprintf (gcov_file
, DEFAULT_LINE_START
"Source:%s\n", src
->coverage
.name
);
2934 if (!multiple_files
)
2936 fprintf (gcov_file
, DEFAULT_LINE_START
"Graph:%s\n", bbg_file_name
);
2937 fprintf (gcov_file
, DEFAULT_LINE_START
"Data:%s\n",
2938 no_data_file
? "-" : da_file_name
);
2939 fprintf (gcov_file
, DEFAULT_LINE_START
"Runs:%u\n", object_runs
);
2941 fprintf (gcov_file
, DEFAULT_LINE_START
"Programs:%u\n", program_count
);
2943 source_file
= fopen (src
->name
, "r");
2945 fnotice (stderr
, "Cannot open source file %s\n", src
->name
);
2946 else if (src
->file_time
== 0)
2947 fprintf (gcov_file
, DEFAULT_LINE_START
"Source is newer than graph\n");
2949 vector
<const char *> source_lines
;
2951 while ((retval
= read_line (source_file
)) != NULL
)
2952 source_lines
.push_back (xstrdup (retval
));
2954 unsigned line_start_group
= 0;
2955 vector
<function_t
*> fns
;
2957 for (unsigned line_num
= 1; line_num
<= source_lines
.size (); line_num
++)
2959 if (line_num
>= src
->lines
.size ())
2961 fprintf (gcov_file
, "%9s:%5u", "-", line_num
);
2962 print_source_line (gcov_file
, source_lines
, line_num
);
2966 const line_info
*line
= &src
->lines
[line_num
];
2968 if (line_start_group
== 0)
2970 fns
= src
->get_functions_at_location (line_num
);
2971 if (fns
.size () > 1)
2972 line_start_group
= fns
[0]->end_line
;
2973 else if (fns
.size () == 1)
2975 function_t
*fn
= fns
[0];
2976 output_function_details (gcov_file
, fn
);
2980 /* For lines which don't exist in the .bb file, print '-' before
2981 the source line. For lines which exist but were never
2982 executed, print '#####' or '=====' before the source line.
2983 Otherwise, print the execution count before the source line.
2984 There are 16 spaces of indentation added before the source
2985 line so that tabs won't be messed up. */
2986 output_line_beginning (gcov_file
, line
->exists
, line
->unexceptional
,
2987 line
->has_unexecuted_block
, line
->count
,
2988 line_num
, "=====", "#####");
2990 print_source_line (gcov_file
, source_lines
, line_num
);
2991 output_line_details (gcov_file
, line
, line_num
);
2993 if (line_start_group
== line_num
)
2995 for (vector
<function_t
*>::iterator it
= fns
.begin ();
2996 it
!= fns
.end (); it
++)
2998 function_info
*fn
= *it
;
2999 vector
<line_info
> &lines
= fn
->lines
;
3001 fprintf (gcov_file
, FN_SEPARATOR
);
3004 = flag_demangled_names
? fn
->demangled_name
: fn
->name
;
3006 if (flag_use_colors
)
3008 fn_name
.insert (0, SGR_SEQ (COLOR_FG_CYAN
));
3009 fn_name
+= SGR_RESET
;
3012 fprintf (gcov_file
, "%s:\n", fn_name
.c_str ());
3014 output_function_details (gcov_file
, fn
);
3016 /* Print all lines covered by the function. */
3017 for (unsigned i
= 0; i
< lines
.size (); i
++)
3019 line_info
*line
= &lines
[i
];
3020 unsigned l
= fn
->start_line
+ i
;
3022 /* For lines which don't exist in the .bb file, print '-'
3023 before the source line. For lines which exist but
3024 were never executed, print '#####' or '=====' before
3025 the source line. Otherwise, print the execution count
3026 before the source line.
3027 There are 16 spaces of indentation added before the source
3028 line so that tabs won't be messed up. */
3029 output_line_beginning (gcov_file
, line
->exists
,
3030 line
->unexceptional
,
3031 line
->has_unexecuted_block
,
3033 l
, "=====", "#####");
3035 print_source_line (gcov_file
, source_lines
, l
);
3036 output_line_details (gcov_file
, line
, l
);
3040 fprintf (gcov_file
, FN_SEPARATOR
);
3041 line_start_group
= 0;
3046 fclose (source_file
);