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
]);
789 generate_results (multiple_files
? NULL
: argv
[argc
- 1]);
791 release_structures ();
796 /* Print a usage message and exit. If ERROR_P is nonzero, this is an error,
797 otherwise the output of --help. */
800 print_usage (int error_p
)
802 FILE *file
= error_p
? stderr
: stdout
;
803 int status
= error_p
? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
;
805 fnotice (file
, "Usage: gcov [OPTION...] SOURCE|OBJ...\n\n");
806 fnotice (file
, "Print code coverage information.\n\n");
807 fnotice (file
, " -a, --all-blocks Show information for every basic block\n");
808 fnotice (file
, " -b, --branch-probabilities Include branch probabilities in output\n");
809 fnotice (file
, " -c, --branch-counts Output counts of branches taken\n\
810 rather than percentages\n");
811 fnotice (file
, " -d, --display-progress Display progress information\n");
812 fnotice (file
, " -f, --function-summaries Output summaries for each function\n");
813 fnotice (file
, " -h, --help Print this help, then exit\n");
814 fnotice (file
, " -i, --intermediate-format Output .gcov file in intermediate text format\n");
815 fnotice (file
, " -j, --human-readable Output human readable numbers\n");
816 fnotice (file
, " -k, --use-colors Emit colored output\n");
817 fnotice (file
, " -l, --long-file-names Use long output file names for included\n\
819 fnotice (file
, " -m, --demangled-names Output demangled function names\n");
820 fnotice (file
, " -n, --no-output Do not create an output file\n");
821 fnotice (file
, " -o, --object-directory DIR|FILE Search for object files in DIR or called FILE\n");
822 fnotice (file
, " -p, --preserve-paths Preserve all pathname components\n");
823 fnotice (file
, " -r, --relative-only Only show data for relative sources\n");
824 fnotice (file
, " -s, --source-prefix DIR Source prefix to elide\n");
825 fnotice (file
, " -u, --unconditional-branches Show unconditional branch counts too\n");
826 fnotice (file
, " -v, --version Print version number, then exit\n");
827 fnotice (file
, " -w, --verbose Print verbose informations\n");
828 fnotice (file
, " -x, --hash-filenames Hash long pathnames\n");
829 fnotice (file
, "\nFor bug reporting instructions, please see:\n%s.\n",
834 /* Print version information and exit. */
839 fnotice (stdout
, "gcov %s%s\n", pkgversion_string
, version_string
);
840 fprintf (stdout
, "Copyright %s 2017 Free Software Foundation, Inc.\n",
843 _("This is free software; see the source for copying conditions.\n"
844 "There is NO warranty; not even for MERCHANTABILITY or \n"
845 "FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
846 exit (SUCCESS_EXIT_CODE
);
849 static const struct option options
[] =
851 { "help", no_argument
, NULL
, 'h' },
852 { "version", no_argument
, NULL
, 'v' },
853 { "verbose", no_argument
, NULL
, 'w' },
854 { "all-blocks", no_argument
, NULL
, 'a' },
855 { "branch-probabilities", no_argument
, NULL
, 'b' },
856 { "branch-counts", no_argument
, NULL
, 'c' },
857 { "intermediate-format", no_argument
, NULL
, 'i' },
858 { "human-readable", no_argument
, NULL
, 'j' },
859 { "no-output", no_argument
, NULL
, 'n' },
860 { "long-file-names", no_argument
, NULL
, 'l' },
861 { "function-summaries", no_argument
, NULL
, 'f' },
862 { "demangled-names", no_argument
, NULL
, 'm' },
863 { "preserve-paths", no_argument
, NULL
, 'p' },
864 { "relative-only", no_argument
, NULL
, 'r' },
865 { "object-directory", required_argument
, NULL
, 'o' },
866 { "object-file", required_argument
, NULL
, 'o' },
867 { "source-prefix", required_argument
, NULL
, 's' },
868 { "unconditional-branches", no_argument
, NULL
, 'u' },
869 { "display-progress", no_argument
, NULL
, 'd' },
870 { "hash-filenames", no_argument
, NULL
, 'x' },
871 { "use-colors", no_argument
, NULL
, 'k' },
875 /* Process args, return index to first non-arg. */
878 process_args (int argc
, char **argv
)
882 const char *opts
= "abcdfhijklmno:prs:uvwx";
883 while ((opt
= getopt_long (argc
, argv
, opts
, options
, NULL
)) != -1)
897 flag_function_summary
= 1;
901 /* print_usage will exit. */
906 flag_human_readable_numbers
= 1;
912 flag_demangled_names
= 1;
918 object_directory
= optarg
;
921 source_prefix
= optarg
;
922 source_length
= strlen (source_prefix
);
925 flag_relative_only
= 1;
928 flag_preserve_paths
= 1;
931 flag_unconditional
= 1;
934 flag_intermediate_format
= 1;
938 flag_display_progress
= 1;
941 flag_hash_filenames
= 1;
948 /* print_version will exit. */
951 /* print_usage will exit. */
958 /* Output intermediate LINE sitting on LINE_NUM to output file F. */
961 output_intermediate_line (FILE *f
, line_info
*line
, unsigned line_num
)
966 fprintf (f
, "lcount:%u,%s,%d\n", line_num
,
967 format_gcov (line
->count
, 0, -1),
968 line
->has_unexecuted_block
);
970 vector
<arc_t
*>::const_iterator it
;
972 for (it
= line
->branches
.begin (); it
!= line
->branches
.end ();
975 if (!(*it
)->is_unconditional
&& !(*it
)->is_call_non_return
)
977 const char *branch_type
;
978 /* branch:<line_num>,<branch_coverage_type>
980 : notexec (Branch not executed)
981 : taken (Branch executed and taken)
982 : nottaken (Branch executed, but not taken)
984 if ((*it
)->src
->count
)
986 = ((*it
)->count
> 0) ? "taken" : "nottaken";
988 branch_type
= "notexec";
989 fprintf (f
, "branch:%d,%s\n", line_num
, branch_type
);
994 /* Output the result in intermediate format used by 'lcov'.
996 The intermediate format contains a single file named 'foo.cc.gcov',
997 with no source code included.
999 The default gcov outputs multiple files: 'foo.cc.gcov',
1000 'iostream.gcov', 'ios_base.h.gcov', etc. with source code
1001 included. Instead the intermediate format here outputs only a single
1002 file 'foo.cc.gcov' similar to the above example. */
1005 output_intermediate_file (FILE *gcov_file
, source_info
*src
)
1007 fprintf (gcov_file
, "file:%s\n", src
->name
); /* source file name */
1009 std::sort (src
->functions
.begin (), src
->functions
.end (),
1010 function_line_start_cmp ());
1011 for (vector
<function_t
*>::iterator it
= src
->functions
.begin ();
1012 it
!= src
->functions
.end (); it
++)
1014 /* function:<name>,<line_number>,<execution_count> */
1015 fprintf (gcov_file
, "function:%d,%d,%s,%s\n", (*it
)->start_line
,
1016 (*it
)->end_line
, format_gcov ((*it
)->blocks
[0].count
, 0, -1),
1017 flag_demangled_names
? (*it
)->demangled_name
: (*it
)->name
);
1020 for (unsigned line_num
= 0; line_num
<= src
->lines
.size (); line_num
++)
1022 vector
<function_t
*> fns
= src
->get_functions_at_location (line_num
);
1024 /* Print first group functions that begin on the line. */
1025 for (vector
<function_t
*>::iterator it2
= fns
.begin ();
1026 it2
!= fns
.end (); it2
++)
1028 vector
<line_info
> &lines
= (*it2
)->lines
;
1029 for (unsigned i
= 0; i
< lines
.size (); i
++)
1031 line_info
*line
= &lines
[i
];
1032 output_intermediate_line (gcov_file
, line
, line_num
+ i
);
1036 /* Follow with lines associated with the source file. */
1037 output_intermediate_line (gcov_file
, &src
->lines
[line_num
], line_num
);
1041 /* Function start pair. */
1042 struct function_start
1044 unsigned source_file_idx
;
1045 unsigned start_line
;
1048 /* Traits class for function start hash maps below. */
1050 struct function_start_pair_hash
: typed_noop_remove
<function_start
>
1052 typedef function_start value_type
;
1053 typedef function_start compare_type
;
1056 hash (const function_start
&ref
)
1058 inchash::hash
hstate (0);
1059 hstate
.add_int (ref
.source_file_idx
);
1060 hstate
.add_int (ref
.start_line
);
1061 return hstate
.end ();
1065 equal (const function_start
&ref1
, const function_start
&ref2
)
1067 return (ref1
.source_file_idx
== ref2
.source_file_idx
1068 && ref1
.start_line
== ref2
.start_line
);
1072 mark_deleted (function_start
&ref
)
1074 ref
.start_line
= ~1U;
1078 mark_empty (function_start
&ref
)
1080 ref
.start_line
= ~2U;
1084 is_deleted (const function_start
&ref
)
1086 return ref
.start_line
== ~1U;
1090 is_empty (const function_start
&ref
)
1092 return ref
.start_line
== ~2U;
1096 /* Process a single input file. */
1099 process_file (const char *file_name
)
1103 create_file_names (file_name
);
1104 fns
= read_graph_file ();
1108 read_count_file (fns
);
1110 hash_map
<function_start_pair_hash
, function_t
*> fn_map
;
1112 /* Identify group functions. */
1113 for (function_t
*f
= fns
; f
; f
= f
->next
)
1116 function_start needle
;
1117 needle
.source_file_idx
= f
->src
;
1118 needle
.start_line
= f
->start_line
;
1120 function_t
**slot
= fn_map
.get (needle
);
1123 gcc_assert ((*slot
)->end_line
== f
->end_line
);
1124 (*slot
)->is_group
= 1;
1128 fn_map
.put (needle
, f
);
1133 function_t
*fn
= fns
;
1137 if (fn
->counts
|| no_data_file
)
1139 unsigned src
= fn
->src
;
1142 /* Process only non-artificial functions. */
1143 if (!fn
->artificial
)
1145 source_info
*s
= &sources
[src
];
1146 s
->functions
.push_back (fn
);
1148 /* Mark last line in files touched by function. */
1149 for (block_no
= 0; block_no
!= fn
->blocks
.size (); block_no
++)
1151 block_t
*block
= &fn
->blocks
[block_no
];
1152 for (unsigned i
= 0; i
< block
->locations
.size (); i
++)
1154 /* Sort lines of locations. */
1155 sort (block
->locations
[i
].lines
.begin (),
1156 block
->locations
[i
].lines
.end ());
1158 if (!block
->locations
[i
].lines
.empty ())
1160 s
= &sources
[block
->locations
[i
].source_file_idx
];
1162 = block
->locations
[i
].lines
.back ();
1164 /* Record new lines for the function. */
1165 if (last_line
>= s
->lines
.size ())
1167 /* Record new lines for a source file. */
1168 s
->lines
.resize (last_line
+ 1);
1173 /* Allocate lines for group function, following start_line
1174 and end_line information of the function. */
1176 fn
->lines
.resize (fn
->end_line
- fn
->start_line
+ 1);
1179 solve_flow_graph (fn
);
1181 find_exception_blocks (fn
);
1188 /* The function was not in the executable -- some other
1189 instance must have been selected. */
1195 output_gcov_file (const char *file_name
, source_info
*src
)
1197 char *gcov_file_name
= make_gcov_file_name (file_name
, src
->coverage
.name
);
1199 if (src
->coverage
.lines
)
1201 FILE *gcov_file
= fopen (gcov_file_name
, "w");
1204 fnotice (stdout
, "Creating '%s'\n", gcov_file_name
);
1206 if (flag_intermediate_format
)
1207 output_intermediate_file (gcov_file
, src
);
1209 output_lines (gcov_file
, src
);
1210 if (ferror (gcov_file
))
1211 fnotice (stderr
, "Error writing output file '%s'\n", gcov_file_name
);
1215 fnotice (stderr
, "Could not open output file '%s'\n", gcov_file_name
);
1219 unlink (gcov_file_name
);
1220 fnotice (stdout
, "Removing '%s'\n", gcov_file_name
);
1222 free (gcov_file_name
);
1226 generate_results (const char *file_name
)
1230 for (fn
= functions
; fn
; fn
= fn
->next
)
1232 coverage_t coverage
;
1236 memset (&coverage
, 0, sizeof (coverage
));
1237 coverage
.name
= flag_demangled_names
? fn
->demangled_name
: fn
->name
;
1238 add_line_counts (flag_function_summary
? &coverage
: NULL
, fn
);
1239 if (flag_function_summary
)
1241 function_summary (&coverage
, "Function");
1242 fnotice (stdout
, "\n");
1250 needle
.name
= file_name
;
1251 vector
<name_map
>::iterator it
= std::find (names
.begin (), names
.end (),
1253 if (it
!= names
.end ())
1254 file_name
= sources
[it
->src
].coverage
.name
;
1256 file_name
= canonicalize_name (file_name
);
1259 for (vector
<source_info
>::iterator it
= sources
.begin ();
1260 it
!= sources
.end (); it
++)
1262 source_info
*src
= &(*it
);
1263 if (flag_relative_only
)
1265 /* Ignore this source, if it is an absolute path (after
1266 source prefix removal). */
1267 char first
= src
->coverage
.name
[0];
1269 #if HAVE_DOS_BASED_FILE_SYSTEM
1270 if (first
&& src
->coverage
.name
[1] == ':')
1271 first
= src
->coverage
.name
[2];
1273 if (IS_DIR_SEPARATOR (first
))
1277 accumulate_line_counts (src
);
1278 function_summary (&src
->coverage
, "File");
1279 total_lines
+= src
->coverage
.lines
;
1280 total_executed
+= src
->coverage
.lines_executed
;
1283 output_gcov_file (file_name
, src
);
1284 fnotice (stdout
, "\n");
1289 executed_summary (total_lines
, total_executed
);
1292 /* Release all memory used. */
1295 release_structures (void)
1299 while ((fn
= functions
))
1301 functions
= fn
->next
;
1306 /* Generate the names of the graph and data files. If OBJECT_DIRECTORY
1307 is not specified, these are named from FILE_NAME sans extension. If
1308 OBJECT_DIRECTORY is specified and is a directory, the files are in that
1309 directory, but named from the basename of the FILE_NAME, sans extension.
1310 Otherwise OBJECT_DIRECTORY is taken to be the name of the object *file*
1311 and the data files are named from that. */
1314 create_file_names (const char *file_name
)
1318 int length
= strlen (file_name
);
1321 /* Free previous file names. */
1322 free (bbg_file_name
);
1323 free (da_file_name
);
1324 da_file_name
= bbg_file_name
= NULL
;
1328 if (object_directory
&& object_directory
[0])
1332 length
+= strlen (object_directory
) + 2;
1333 name
= XNEWVEC (char, length
);
1336 base
= !stat (object_directory
, &status
) && S_ISDIR (status
.st_mode
);
1337 strcat (name
, object_directory
);
1338 if (base
&& (!IS_DIR_SEPARATOR (name
[strlen (name
) - 1])))
1343 name
= XNEWVEC (char, length
+ 1);
1344 strcpy (name
, file_name
);
1350 /* Append source file name. */
1351 const char *cptr
= lbasename (file_name
);
1352 strcat (name
, cptr
? cptr
: file_name
);
1355 /* Remove the extension. */
1356 cptr
= strrchr (CONST_CAST (char *, lbasename (name
)), '.');
1360 length
= strlen (name
);
1362 bbg_file_name
= XNEWVEC (char, length
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
1363 strcpy (bbg_file_name
, name
);
1364 strcpy (bbg_file_name
+ length
, GCOV_NOTE_SUFFIX
);
1366 da_file_name
= XNEWVEC (char, length
+ strlen (GCOV_DATA_SUFFIX
) + 1);
1367 strcpy (da_file_name
, name
);
1368 strcpy (da_file_name
+ length
, GCOV_DATA_SUFFIX
);
1374 /* Find or create a source file structure for FILE_NAME. Copies
1375 FILE_NAME on creation */
1378 find_source (const char *file_name
)
1385 file_name
= "<unknown>";
1388 needle
.name
= file_name
;
1390 vector
<name_map
>::iterator it
= std::find (names
.begin (), names
.end (),
1392 if (it
!= names
.end ())
1398 /* Not found, try the canonical name. */
1399 canon
= canonicalize_name (file_name
);
1400 needle
.name
= canon
;
1401 it
= std::find (names
.begin (), names
.end (), needle
);
1402 if (it
== names
.end ())
1404 /* Not found with canonical name, create a new source. */
1407 idx
= sources
.size ();
1408 needle
= name_map (canon
, idx
);
1409 names
.push_back (needle
);
1411 sources
.push_back (source_info ());
1412 src
= &sources
.back ();
1414 src
->coverage
.name
= src
->name
;
1417 #if HAVE_DOS_BASED_FILE_SYSTEM
1418 /* You lose if separators don't match exactly in the
1420 && !strncasecmp (source_prefix
, src
->coverage
.name
, source_length
)
1422 && !strncmp (source_prefix
, src
->coverage
.name
, source_length
)
1424 && IS_DIR_SEPARATOR (src
->coverage
.name
[source_length
]))
1425 src
->coverage
.name
+= source_length
+ 1;
1426 if (!stat (src
->name
, &status
))
1427 src
->file_time
= status
.st_mtime
;
1432 needle
.name
= file_name
;
1433 if (std::find (names
.begin (), names
.end (), needle
) == names
.end ())
1435 /* Append the non-canonical name. */
1436 names
.push_back (name_map (xstrdup (file_name
), idx
));
1439 /* Resort the name map. */
1440 std::sort (names
.begin (), names
.end ());
1443 if (sources
[idx
].file_time
> bbg_file_time
)
1445 static int info_emitted
;
1447 fnotice (stderr
, "%s:source file is newer than notes file '%s'\n",
1448 file_name
, bbg_file_name
);
1452 "(the message is displayed only once per source file)\n");
1455 sources
[idx
].file_time
= 0;
1461 /* Read the notes file. Return list of functions read -- in reverse order. */
1464 read_graph_file (void)
1467 unsigned current_tag
= 0;
1468 function_t
*fn
= NULL
;
1469 function_t
*fns
= NULL
;
1470 function_t
**fns_end
= &fns
;
1473 if (!gcov_open (bbg_file_name
, 1))
1475 fnotice (stderr
, "%s:cannot open notes file\n", bbg_file_name
);
1478 bbg_file_time
= gcov_time ();
1479 if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC
))
1481 fnotice (stderr
, "%s:not a gcov notes file\n", bbg_file_name
);
1486 version
= gcov_read_unsigned ();
1487 if (version
!= GCOV_VERSION
)
1491 GCOV_UNSIGNED2STRING (v
, version
);
1492 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1494 fnotice (stderr
, "%s:version '%.4s', prefer '%.4s'\n",
1495 bbg_file_name
, v
, e
);
1497 bbg_stamp
= gcov_read_unsigned ();
1498 bbg_supports_has_unexecuted_blocks
= gcov_read_unsigned ();
1500 while ((tag
= gcov_read_unsigned ()))
1502 unsigned length
= gcov_read_unsigned ();
1503 gcov_position_t base
= gcov_position ();
1505 if (tag
== GCOV_TAG_FUNCTION
)
1507 char *function_name
;
1509 unsigned lineno_checksum
, cfg_checksum
;
1511 ident
= gcov_read_unsigned ();
1512 lineno_checksum
= gcov_read_unsigned ();
1513 cfg_checksum
= gcov_read_unsigned ();
1514 function_name
= xstrdup (gcov_read_string ());
1515 unsigned artificial
= gcov_read_unsigned ();
1516 unsigned src_idx
= find_source (gcov_read_string ());
1517 unsigned start_line
= gcov_read_unsigned ();
1518 unsigned start_column
= gcov_read_unsigned ();
1519 unsigned end_line
= gcov_read_unsigned ();
1521 fn
= new function_t
;
1522 fn
->name
= function_name
;
1523 if (flag_demangled_names
)
1525 fn
->demangled_name
= cplus_demangle (fn
->name
, DMGL_PARAMS
);
1526 if (!fn
->demangled_name
)
1527 fn
->demangled_name
= fn
->name
;
1530 fn
->lineno_checksum
= lineno_checksum
;
1531 fn
->cfg_checksum
= cfg_checksum
;
1533 fn
->start_line
= start_line
;
1534 fn
->start_column
= start_column
;
1535 fn
->end_line
= end_line
;
1536 fn
->artificial
= artificial
;
1540 fns_end
= &fn
->next
;
1543 else if (fn
&& tag
== GCOV_TAG_BLOCKS
)
1545 if (!fn
->blocks
.empty ())
1546 fnotice (stderr
, "%s:already seen blocks for '%s'\n",
1547 bbg_file_name
, fn
->name
);
1549 fn
->blocks
.resize (gcov_read_unsigned ());
1551 else if (fn
&& tag
== GCOV_TAG_ARCS
)
1553 unsigned src
= gcov_read_unsigned ();
1554 fn
->blocks
[src
].id
= src
;
1555 unsigned num_dests
= GCOV_TAG_ARCS_NUM (length
);
1556 block_t
*src_blk
= &fn
->blocks
[src
];
1557 unsigned mark_catches
= 0;
1558 struct arc_info
*arc
;
1560 if (src
>= fn
->blocks
.size () || fn
->blocks
[src
].succ
)
1565 unsigned dest
= gcov_read_unsigned ();
1566 unsigned flags
= gcov_read_unsigned ();
1568 if (dest
>= fn
->blocks
.size ())
1570 arc
= XCNEW (arc_t
);
1572 arc
->dst
= &fn
->blocks
[dest
];
1576 arc
->count_valid
= 0;
1577 arc
->on_tree
= !!(flags
& GCOV_ARC_ON_TREE
);
1578 arc
->fake
= !!(flags
& GCOV_ARC_FAKE
);
1579 arc
->fall_through
= !!(flags
& GCOV_ARC_FALLTHROUGH
);
1581 arc
->succ_next
= src_blk
->succ
;
1582 src_blk
->succ
= arc
;
1583 src_blk
->num_succ
++;
1585 arc
->pred_next
= fn
->blocks
[dest
].pred
;
1586 fn
->blocks
[dest
].pred
= arc
;
1587 fn
->blocks
[dest
].num_pred
++;
1593 /* Exceptional exit from this function, the
1594 source block must be a call. */
1595 fn
->blocks
[src
].is_call_site
= 1;
1596 arc
->is_call_non_return
= 1;
1601 /* Non-local return from a callee of this
1602 function. The destination block is a setjmp. */
1603 arc
->is_nonlocal_return
= 1;
1604 fn
->blocks
[dest
].is_nonlocal_return
= 1;
1614 /* We have a fake exit from this block. The other
1615 non-fall through exits must be to catch handlers.
1616 Mark them as catch arcs. */
1618 for (arc
= src_blk
->succ
; arc
; arc
= arc
->succ_next
)
1619 if (!arc
->fake
&& !arc
->fall_through
)
1626 else if (fn
&& tag
== GCOV_TAG_LINES
)
1628 unsigned blockno
= gcov_read_unsigned ();
1629 block_t
*block
= &fn
->blocks
[blockno
];
1631 if (blockno
>= fn
->blocks
.size ())
1636 unsigned lineno
= gcov_read_unsigned ();
1639 block
->locations
.back ().lines
.push_back (lineno
);
1642 const char *file_name
= gcov_read_string ();
1646 block
->locations
.push_back (block_location_info
1647 (find_source (file_name
)));
1651 else if (current_tag
&& !GCOV_TAG_IS_SUBTAG (current_tag
, tag
))
1656 gcov_sync (base
, length
);
1657 if (gcov_is_error ())
1660 fnotice (stderr
, "%s:corrupted\n", bbg_file_name
);
1667 fnotice (stderr
, "%s:no functions found\n", bbg_file_name
);
1672 /* Reads profiles from the count file and attach to each
1673 function. Return nonzero if fatal error. */
1676 read_count_file (function_t
*fns
)
1681 function_t
*fn
= NULL
;
1684 if (!gcov_open (da_file_name
, 1))
1686 fnotice (stderr
, "%s:cannot open data file, assuming not executed\n",
1691 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
1693 fnotice (stderr
, "%s:not a gcov data file\n", da_file_name
);
1698 version
= gcov_read_unsigned ();
1699 if (version
!= GCOV_VERSION
)
1703 GCOV_UNSIGNED2STRING (v
, version
);
1704 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
1706 fnotice (stderr
, "%s:version '%.4s', prefer version '%.4s'\n",
1707 da_file_name
, v
, e
);
1709 tag
= gcov_read_unsigned ();
1710 if (tag
!= bbg_stamp
)
1712 fnotice (stderr
, "%s:stamp mismatch with notes file\n", da_file_name
);
1716 while ((tag
= gcov_read_unsigned ()))
1718 unsigned length
= gcov_read_unsigned ();
1719 unsigned long base
= gcov_position ();
1721 if (tag
== GCOV_TAG_PROGRAM_SUMMARY
)
1723 struct gcov_summary summary
;
1724 gcov_read_summary (&summary
);
1725 object_runs
+= summary
.ctrs
[GCOV_COUNTER_ARCS
].runs
;
1728 else if (tag
== GCOV_TAG_FUNCTION
&& !length
)
1730 else if (tag
== GCOV_TAG_FUNCTION
&& length
== GCOV_TAG_FUNCTION_LENGTH
)
1733 struct function_info
*fn_n
;
1735 /* Try to find the function in the list. To speed up the
1736 search, first start from the last function found. */
1737 ident
= gcov_read_unsigned ();
1739 for (fn
= fn
? fn
->next
: NULL
; ; fn
= fn
->next
)
1743 else if ((fn
= fn_n
))
1747 fnotice (stderr
, "%s:unknown function '%u'\n",
1748 da_file_name
, ident
);
1751 if (fn
->ident
== ident
)
1757 else if (gcov_read_unsigned () != fn
->lineno_checksum
1758 || gcov_read_unsigned () != fn
->cfg_checksum
)
1761 fnotice (stderr
, "%s:profile mismatch for '%s'\n",
1762 da_file_name
, fn
->name
);
1766 else if (tag
== GCOV_TAG_FOR_COUNTER (GCOV_COUNTER_ARCS
) && fn
)
1768 if (length
!= GCOV_TAG_COUNTER_LENGTH (fn
->num_counts
))
1772 fn
->counts
= XCNEWVEC (gcov_type
, fn
->num_counts
);
1774 for (ix
= 0; ix
!= fn
->num_counts
; ix
++)
1775 fn
->counts
[ix
] += gcov_read_counter ();
1777 gcov_sync (base
, length
);
1778 if ((error
= gcov_is_error ()))
1782 ? N_("%s:overflowed\n")
1783 : N_("%s:corrupted\n"),
1793 /* Solve the flow graph. Propagate counts from the instrumented arcs
1794 to the blocks and the uninstrumented arcs. */
1797 solve_flow_graph (function_t
*fn
)
1801 gcov_type
*count_ptr
= fn
->counts
;
1803 block_t
*valid_blocks
= NULL
; /* valid, but unpropagated blocks. */
1804 block_t
*invalid_blocks
= NULL
; /* invalid, but inferable blocks. */
1806 /* The arcs were built in reverse order. Fix that now. */
1807 for (ix
= fn
->blocks
.size (); ix
--;)
1809 arc_t
*arc_p
, *arc_n
;
1811 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].succ
; arc
;
1812 arc_p
= arc
, arc
= arc_n
)
1814 arc_n
= arc
->succ_next
;
1815 arc
->succ_next
= arc_p
;
1817 fn
->blocks
[ix
].succ
= arc_p
;
1819 for (arc_p
= NULL
, arc
= fn
->blocks
[ix
].pred
; arc
;
1820 arc_p
= arc
, arc
= arc_n
)
1822 arc_n
= arc
->pred_next
;
1823 arc
->pred_next
= arc_p
;
1825 fn
->blocks
[ix
].pred
= arc_p
;
1828 if (fn
->blocks
.size () < 2)
1829 fnotice (stderr
, "%s:'%s' lacks entry and/or exit blocks\n",
1830 bbg_file_name
, fn
->name
);
1833 if (fn
->blocks
[ENTRY_BLOCK
].num_pred
)
1834 fnotice (stderr
, "%s:'%s' has arcs to entry block\n",
1835 bbg_file_name
, fn
->name
);
1837 /* We can't deduce the entry block counts from the lack of
1839 fn
->blocks
[ENTRY_BLOCK
].num_pred
= ~(unsigned)0;
1841 if (fn
->blocks
[EXIT_BLOCK
].num_succ
)
1842 fnotice (stderr
, "%s:'%s' has arcs from exit block\n",
1843 bbg_file_name
, fn
->name
);
1845 /* Likewise, we can't deduce exit block counts from the lack
1846 of its successors. */
1847 fn
->blocks
[EXIT_BLOCK
].num_succ
= ~(unsigned)0;
1850 /* Propagate the measured counts, this must be done in the same
1851 order as the code in profile.c */
1852 for (unsigned i
= 0; i
< fn
->blocks
.size (); i
++)
1854 blk
= &fn
->blocks
[i
];
1855 block_t
const *prev_dst
= NULL
;
1856 int out_of_order
= 0;
1857 int non_fake_succ
= 0;
1859 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1867 arc
->count
= *count_ptr
++;
1868 arc
->count_valid
= 1;
1870 arc
->dst
->num_pred
--;
1872 if (prev_dst
&& prev_dst
> arc
->dst
)
1874 prev_dst
= arc
->dst
;
1876 if (non_fake_succ
== 1)
1878 /* If there is only one non-fake exit, it is an
1879 unconditional branch. */
1880 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1883 arc
->is_unconditional
= 1;
1884 /* If this block is instrumenting a call, it might be
1885 an artificial block. It is not artificial if it has
1886 a non-fallthrough exit, or the destination of this
1887 arc has more than one entry. Mark the destination
1888 block as a return site, if none of those conditions
1890 if (blk
->is_call_site
&& arc
->fall_through
1891 && arc
->dst
->pred
== arc
&& !arc
->pred_next
)
1892 arc
->dst
->is_call_return
= 1;
1896 /* Sort the successor arcs into ascending dst order. profile.c
1897 normally produces arcs in the right order, but sometimes with
1898 one or two out of order. We're not using a particularly
1902 arc_t
*start
= blk
->succ
;
1903 unsigned changes
= 1;
1907 arc_t
*arc
, *arc_p
, *arc_n
;
1910 for (arc_p
= NULL
, arc
= start
; (arc_n
= arc
->succ_next
);)
1912 if (arc
->dst
> arc_n
->dst
)
1916 arc_p
->succ_next
= arc_n
;
1919 arc
->succ_next
= arc_n
->succ_next
;
1920 arc_n
->succ_next
= arc
;
1933 /* Place it on the invalid chain, it will be ignored if that's
1935 blk
->invalid_chain
= 1;
1936 blk
->chain
= invalid_blocks
;
1937 invalid_blocks
= blk
;
1940 while (invalid_blocks
|| valid_blocks
)
1942 while ((blk
= invalid_blocks
))
1944 gcov_type total
= 0;
1947 invalid_blocks
= blk
->chain
;
1948 blk
->invalid_chain
= 0;
1950 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1951 total
+= arc
->count
;
1952 else if (!blk
->num_pred
)
1953 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
1954 total
+= arc
->count
;
1959 blk
->count_valid
= 1;
1960 blk
->chain
= valid_blocks
;
1961 blk
->valid_chain
= 1;
1964 while ((blk
= valid_blocks
))
1967 arc_t
*arc
, *inv_arc
;
1969 valid_blocks
= blk
->chain
;
1970 blk
->valid_chain
= 0;
1971 if (blk
->num_succ
== 1)
1977 for (arc
= blk
->succ
; arc
; arc
= arc
->succ_next
)
1979 total
-= arc
->count
;
1980 if (!arc
->count_valid
)
1984 inv_arc
->count_valid
= 1;
1985 inv_arc
->count
= total
;
1988 if (dst
->count_valid
)
1990 if (dst
->num_pred
== 1 && !dst
->valid_chain
)
1992 dst
->chain
= valid_blocks
;
1993 dst
->valid_chain
= 1;
1999 if (!dst
->num_pred
&& !dst
->invalid_chain
)
2001 dst
->chain
= invalid_blocks
;
2002 dst
->invalid_chain
= 1;
2003 invalid_blocks
= dst
;
2007 if (blk
->num_pred
== 1)
2013 for (arc
= blk
->pred
; arc
; arc
= arc
->pred_next
)
2015 total
-= arc
->count
;
2016 if (!arc
->count_valid
)
2020 inv_arc
->count_valid
= 1;
2021 inv_arc
->count
= total
;
2024 if (src
->count_valid
)
2026 if (src
->num_succ
== 1 && !src
->valid_chain
)
2028 src
->chain
= valid_blocks
;
2029 src
->valid_chain
= 1;
2035 if (!src
->num_succ
&& !src
->invalid_chain
)
2037 src
->chain
= invalid_blocks
;
2038 src
->invalid_chain
= 1;
2039 invalid_blocks
= src
;
2046 /* If the graph has been correctly solved, every block will have a
2048 for (unsigned i
= 0; ix
< fn
->blocks
.size (); i
++)
2049 if (!fn
->blocks
[i
].count_valid
)
2051 fnotice (stderr
, "%s:graph is unsolvable for '%s'\n",
2052 bbg_file_name
, fn
->name
);
2057 /* Mark all the blocks only reachable via an incoming catch. */
2060 find_exception_blocks (function_t
*fn
)
2063 block_t
**queue
= XALLOCAVEC (block_t
*, fn
->blocks
.size ());
2065 /* First mark all blocks as exceptional. */
2066 for (ix
= fn
->blocks
.size (); ix
--;)
2067 fn
->blocks
[ix
].exceptional
= 1;
2069 /* Now mark all the blocks reachable via non-fake edges */
2070 queue
[0] = &fn
->blocks
[0];
2071 queue
[0]->exceptional
= 0;
2074 block_t
*block
= queue
[--ix
];
2077 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2078 if (!arc
->fake
&& !arc
->is_throw
&& arc
->dst
->exceptional
)
2080 arc
->dst
->exceptional
= 0;
2081 queue
[ix
++] = arc
->dst
;
2087 /* Increment totals in COVERAGE according to arc ARC. */
2090 add_branch_counts (coverage_t
*coverage
, const arc_t
*arc
)
2092 if (arc
->is_call_non_return
)
2095 if (arc
->src
->count
)
2096 coverage
->calls_executed
++;
2098 else if (!arc
->is_unconditional
)
2100 coverage
->branches
++;
2101 if (arc
->src
->count
)
2102 coverage
->branches_executed
++;
2104 coverage
->branches_taken
++;
2108 /* Format COUNT, if flag_human_readable_numbers is set, return it human
2112 format_count (gcov_type count
)
2114 static char buffer
[64];
2115 const char *units
= " kMGTPEZY";
2117 if (count
< 1000 || !flag_human_readable_numbers
)
2119 sprintf (buffer
, "%" PRId64
, count
);
2124 gcov_type divisor
= 1;
2125 for (i
= 0; units
[i
+1]; i
++, divisor
*= 1000)
2127 if (count
+ divisor
/ 2 < 1000 * divisor
)
2130 gcov_type r
= (count
+ divisor
/ 2) / divisor
;
2131 sprintf (buffer
, "%" PRId64
"%c", r
, units
[i
]);
2135 /* Format a GCOV_TYPE integer as either a percent ratio, or absolute
2136 count. If dp >= 0, format TOP/BOTTOM * 100 to DP decimal places.
2137 If DP is zero, no decimal point is printed. Only print 100% when
2138 TOP==BOTTOM and only print 0% when TOP=0. If dp < 0, then simply
2139 format TOP. Return pointer to a static string. */
2142 format_gcov (gcov_type top
, gcov_type bottom
, int dp
)
2144 static char buffer
[20];
2146 /* Handle invalid values that would result in a misleading value. */
2147 if (bottom
!= 0 && top
> bottom
&& dp
>= 0)
2149 sprintf (buffer
, "NAN %%");
2155 float ratio
= bottom
? (float)top
/ bottom
: 0;
2157 unsigned limit
= 100;
2160 for (ix
= dp
; ix
--; )
2163 percent
= (unsigned) (ratio
* limit
+ (float)0.5);
2164 if (percent
<= 0 && top
)
2166 else if (percent
>= limit
&& top
!= bottom
)
2167 percent
= limit
- 1;
2168 ix
= sprintf (buffer
, "%.*u%%", dp
+ 1, percent
);
2174 buffer
[ix
+1] = buffer
[ix
];
2178 buffer
[ix
+ 1] = '.';
2182 return format_count (top
);
2187 /* Summary of execution */
2190 executed_summary (unsigned lines
, unsigned executed
)
2193 fnotice (stdout
, "Lines executed:%s of %d\n",
2194 format_gcov (executed
, lines
, 2), lines
);
2196 fnotice (stdout
, "No executable lines\n");
2199 /* Output summary info for a function or file. */
2202 function_summary (const coverage_t
*coverage
, const char *title
)
2204 fnotice (stdout
, "%s '%s'\n", title
, coverage
->name
);
2205 executed_summary (coverage
->lines
, coverage
->lines_executed
);
2209 if (coverage
->branches
)
2211 fnotice (stdout
, "Branches executed:%s of %d\n",
2212 format_gcov (coverage
->branches_executed
,
2213 coverage
->branches
, 2),
2214 coverage
->branches
);
2215 fnotice (stdout
, "Taken at least once:%s of %d\n",
2216 format_gcov (coverage
->branches_taken
,
2217 coverage
->branches
, 2),
2218 coverage
->branches
);
2221 fnotice (stdout
, "No branches\n");
2222 if (coverage
->calls
)
2223 fnotice (stdout
, "Calls executed:%s of %d\n",
2224 format_gcov (coverage
->calls_executed
, coverage
->calls
, 2),
2227 fnotice (stdout
, "No calls\n");
2231 /* Canonicalize the filename NAME by canonicalizing directory
2232 separators, eliding . components and resolving .. components
2233 appropriately. Always returns a unique string. */
2236 canonicalize_name (const char *name
)
2238 /* The canonical name cannot be longer than the incoming name. */
2239 char *result
= XNEWVEC (char, strlen (name
) + 1);
2240 const char *base
= name
, *probe
;
2245 #if HAVE_DOS_BASED_FILE_SYSTEM
2246 if (base
[0] && base
[1] == ':')
2248 result
[0] = base
[0];
2254 for (dd_base
= ptr
; *base
; base
= probe
)
2258 for (probe
= base
; *probe
; probe
++)
2259 if (IS_DIR_SEPARATOR (*probe
))
2263 if (len
== 1 && base
[0] == '.')
2264 /* Elide a '.' directory */
2266 else if (len
== 2 && base
[0] == '.' && base
[1] == '.')
2268 /* '..', we can only elide it and the previous directory, if
2269 we're not a symlink. */
2270 struct stat ATTRIBUTE_UNUSED buf
;
2274 #if defined (S_ISLNK)
2275 /* S_ISLNK is not POSIX.1-1996. */
2276 || stat (result
, &buf
) || S_ISLNK (buf
.st_mode
)
2280 /* Cannot elide, or unreadable or a symlink. */
2281 dd_base
= ptr
+ 2 + slash
;
2284 while (ptr
!= dd_base
&& *ptr
!= '/')
2286 slash
= ptr
!= result
;
2291 /* Regular pathname component. */
2294 memcpy (ptr
, base
, len
);
2299 for (; IS_DIR_SEPARATOR (*probe
); probe
++)
2307 /* Print hex representation of 16 bytes from SUM and write it to BUFFER. */
2310 md5sum_to_hex (const char *sum
, char *buffer
)
2312 for (unsigned i
= 0; i
< 16; i
++)
2313 sprintf (buffer
+ (2 * i
), "%02x", (unsigned char)sum
[i
]);
2316 /* Generate an output file name. INPUT_NAME is the canonicalized main
2317 input file and SRC_NAME is the canonicalized file name.
2318 LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
2319 long_output_names we prepend the processed name of the input file
2320 to each output name (except when the current source file is the
2321 input file, so you don't get a double concatenation). The two
2322 components are separated by '##'. With preserve_paths we create a
2323 filename from all path components of the source file, replacing '/'
2324 with '#', and .. with '^', without it we simply take the basename
2325 component. (Remember, the canonicalized name will already have
2326 elided '.' components and converted \\ separators.) */
2329 make_gcov_file_name (const char *input_name
, const char *src_name
)
2334 if (flag_long_names
&& input_name
&& strcmp (src_name
, input_name
))
2336 /* Generate the input filename part. */
2337 result
= XNEWVEC (char, strlen (input_name
) + strlen (src_name
) + 10);
2340 ptr
= mangle_name (input_name
, ptr
);
2341 ptr
[0] = ptr
[1] = '#';
2346 result
= XNEWVEC (char, strlen (src_name
) + 10);
2350 ptr
= mangle_name (src_name
, ptr
);
2351 strcpy (ptr
, ".gcov");
2353 /* When hashing filenames, we shorten them by only using the filename
2354 component and appending a hash of the full (mangled) pathname. */
2355 if (flag_hash_filenames
)
2359 char md5sum_hex
[33];
2361 md5_init_ctx (&ctx
);
2362 md5_process_bytes (src_name
, strlen (src_name
), &ctx
);
2363 md5_finish_ctx (&ctx
, md5sum
);
2364 md5sum_to_hex (md5sum
, md5sum_hex
);
2367 result
= XNEWVEC (char, strlen (src_name
) + 50);
2369 ptr
= mangle_name (src_name
, ptr
);
2370 ptr
[0] = ptr
[1] = '#';
2372 memcpy (ptr
, md5sum_hex
, 32);
2374 strcpy (ptr
, ".gcov");
2381 mangle_name (char const *base
, char *ptr
)
2385 /* Generate the source filename part. */
2386 if (!flag_preserve_paths
)
2388 base
= lbasename (base
);
2389 len
= strlen (base
);
2390 memcpy (ptr
, base
, len
);
2395 /* Convert '/' to '#', convert '..' to '^',
2396 convert ':' to '~' on DOS based file system. */
2399 #if HAVE_DOS_BASED_FILE_SYSTEM
2400 if (base
[0] && base
[1] == ':')
2408 for (; *base
; base
= probe
)
2412 for (probe
= base
; *probe
; probe
++)
2416 if (len
== 2 && base
[0] == '.' && base
[1] == '.')
2420 memcpy (ptr
, base
, len
);
2434 /* Scan through the bb_data for each line in the block, increment
2435 the line number execution count indicated by the execution count of
2436 the appropriate basic block. */
2439 add_line_counts (coverage_t
*coverage
, function_t
*fn
)
2441 bool has_any_line
= false;
2442 /* Scan each basic block. */
2443 for (unsigned ix
= 0; ix
!= fn
->blocks
.size (); ix
++)
2445 line_info
*line
= NULL
;
2446 block_t
*block
= &fn
->blocks
[ix
];
2447 if (block
->count
&& ix
&& ix
+ 1 != fn
->blocks
.size ())
2448 fn
->blocks_executed
++;
2449 for (unsigned i
= 0; i
< block
->locations
.size (); i
++)
2451 unsigned src_idx
= block
->locations
[i
].source_file_idx
;
2452 vector
<unsigned> &lines
= block
->locations
[i
].lines
;
2454 block
->cycle
.arc
= NULL
;
2455 block
->cycle
.ident
= ~0U;
2457 for (unsigned j
= 0; j
< lines
.size (); j
++)
2459 unsigned ln
= lines
[j
];
2461 /* Line belongs to a function that is in a group. */
2462 if (fn
->group_line_p (ln
, src_idx
))
2464 gcc_assert (lines
[j
] - fn
->start_line
< fn
->lines
.size ());
2465 line
= &(fn
->lines
[lines
[j
] - fn
->start_line
]);
2467 if (!block
->exceptional
)
2469 line
->unexceptional
= 1;
2470 if (block
->count
== 0)
2471 line
->has_unexecuted_block
= 1;
2473 line
->count
+= block
->count
;
2477 gcc_assert (ln
< sources
[src_idx
].lines
.size ());
2478 line
= &(sources
[src_idx
].lines
[ln
]);
2483 if (!line
->count
&& block
->count
)
2484 coverage
->lines_executed
++;
2487 if (!block
->exceptional
)
2489 line
->unexceptional
= 1;
2490 if (block
->count
== 0)
2491 line
->has_unexecuted_block
= 1;
2493 line
->count
+= block
->count
;
2497 has_any_line
= true;
2499 if (!ix
|| ix
+ 1 == fn
->blocks
.size ())
2500 /* Entry or exit block. */;
2501 else if (line
!= NULL
)
2503 line
->blocks
.push_back (block
);
2509 for (arc
= block
->succ
; arc
; arc
= arc
->succ_next
)
2510 line
->branches
.push_back (arc
);
2517 fnotice (stderr
, "%s:no lines for '%s'\n", bbg_file_name
, fn
->name
);
2520 /* Accumulate info for LINE that belongs to SRC source file. If ADD_COVERAGE
2521 is set to true, update source file summary. */
2523 static void accumulate_line_info (line_info
*line
, source_info
*src
,
2527 for (vector
<arc_info
*>::iterator it
= line
->branches
.begin ();
2528 it
!= line
->branches
.end (); it
++)
2529 add_branch_counts (&src
->coverage
, *it
);
2531 if (!line
->blocks
.empty ())
2533 /* The user expects the line count to be the number of times
2534 a line has been executed. Simply summing the block count
2535 will give an artificially high number. The Right Thing
2536 is to sum the entry counts to the graph of blocks on this
2537 line, then find the elementary cycles of the local graph
2538 and add the transition counts of those cycles. */
2539 gcov_type count
= 0;
2541 /* Cycle detection. */
2542 for (vector
<block_t
*>::iterator it
= line
->blocks
.begin ();
2543 it
!= line
->blocks
.end (); it
++)
2545 for (arc_t
*arc
= (*it
)->pred
; arc
; arc
= arc
->pred_next
)
2546 if (!line
->has_block (arc
->src
))
2547 count
+= arc
->count
;
2548 for (arc_t
*arc
= (*it
)->succ
; arc
; arc
= arc
->succ_next
)
2549 arc
->cs_count
= arc
->count
;
2552 /* Now, add the count of loops entirely on this line. */
2553 count
+= get_cycles_count (*line
);
2554 line
->count
= count
;
2557 if (line
->exists
&& add_coverage
)
2559 src
->coverage
.lines
++;
2561 src
->coverage
.lines_executed
++;
2565 /* Accumulate the line counts of a file. */
2568 accumulate_line_counts (source_info
*src
)
2570 /* First work on group functions. */
2571 for (vector
<function_t
*>::iterator it
= src
->functions
.begin ();
2572 it
!= src
->functions
.end (); it
++)
2574 function_info
*fn
= *it
;
2576 if (fn
->src
!= src
->index
|| !fn
->is_group
)
2579 for (vector
<line_info
>::iterator it2
= fn
->lines
.begin ();
2580 it2
!= fn
->lines
.end (); it2
++)
2582 line_info
*line
= &(*it2
);
2583 accumulate_line_info (line
, src
, false);
2587 /* Work on global lines that line in source file SRC. */
2588 for (vector
<line_info
>::iterator it
= src
->lines
.begin ();
2589 it
!= src
->lines
.end (); it
++)
2590 accumulate_line_info (&(*it
), src
, true);
2592 /* If not using intermediate mode, sum lines of group functions and
2593 add them to lines that live in a source file. */
2594 if (!flag_intermediate_format
)
2595 for (vector
<function_t
*>::iterator it
= src
->functions
.begin ();
2596 it
!= src
->functions
.end (); it
++)
2598 function_info
*fn
= *it
;
2600 if (fn
->src
!= src
->index
|| !fn
->is_group
)
2603 for (unsigned i
= 0; i
< fn
->lines
.size (); i
++)
2605 line_info
*fn_line
= &fn
->lines
[i
];
2606 if (fn_line
->exists
)
2608 unsigned ln
= fn
->start_line
+ i
;
2609 line_info
*src_line
= &src
->lines
[ln
];
2611 if (!src_line
->exists
)
2612 src
->coverage
.lines
++;
2613 if (!src_line
->count
&& fn_line
->count
)
2614 src
->coverage
.lines_executed
++;
2616 src_line
->count
+= fn_line
->count
;
2617 src_line
->exists
= 1;
2619 if (fn_line
->has_unexecuted_block
)
2620 src_line
->has_unexecuted_block
= 1;
2622 if (fn_line
->unexceptional
)
2623 src_line
->unexceptional
= 1;
2629 /* Output information about ARC number IX. Returns nonzero if
2630 anything is output. */
2633 output_branch_count (FILE *gcov_file
, int ix
, const arc_t
*arc
)
2635 if (arc
->is_call_non_return
)
2637 if (arc
->src
->count
)
2639 fnotice (gcov_file
, "call %2d returned %s\n", ix
,
2640 format_gcov (arc
->src
->count
- arc
->count
,
2641 arc
->src
->count
, -flag_counts
));
2644 fnotice (gcov_file
, "call %2d never executed\n", ix
);
2646 else if (!arc
->is_unconditional
)
2648 if (arc
->src
->count
)
2649 fnotice (gcov_file
, "branch %2d taken %s%s", ix
,
2650 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
),
2651 arc
->fall_through
? " (fallthrough)"
2652 : arc
->is_throw
? " (throw)" : "");
2654 fnotice (gcov_file
, "branch %2d never executed", ix
);
2657 fnotice (gcov_file
, " (BB %d)", arc
->dst
->id
);
2659 fnotice (gcov_file
, "\n");
2661 else if (flag_unconditional
&& !arc
->dst
->is_call_return
)
2663 if (arc
->src
->count
)
2664 fnotice (gcov_file
, "unconditional %2d taken %s\n", ix
,
2665 format_gcov (arc
->count
, arc
->src
->count
, -flag_counts
));
2667 fnotice (gcov_file
, "unconditional %2d never executed\n", ix
);
2675 read_line (FILE *file
)
2677 static char *string
;
2678 static size_t string_len
;
2685 string
= XNEWVEC (char, string_len
);
2688 while ((ptr
= fgets (string
+ pos
, string_len
- pos
, file
)))
2690 size_t len
= strlen (string
+ pos
);
2692 if (len
&& string
[pos
+ len
- 1] == '\n')
2694 string
[pos
+ len
- 1] = 0;
2698 /* If the file contains NUL characters or an incomplete
2699 last line, which can happen more than once in one run,
2700 we have to avoid doubling the STRING_LEN unnecessarily. */
2701 if (pos
> string_len
/ 2)
2704 string
= XRESIZEVEC (char, string
, string_len
);
2708 return pos
? string
: NULL
;
2711 /* Pad string S with spaces from left to have total width equal to 9. */
2714 pad_count_string (string
&s
)
2717 s
.insert (0, 9 - s
.size (), ' ');
2720 /* Print GCOV line beginning to F stream. If EXISTS is set to true, the
2721 line exists in source file. UNEXCEPTIONAL indicated that it's not in
2722 an exceptional statement. The output is printed for LINE_NUM of given
2723 COUNT of executions. EXCEPTIONAL_STRING and UNEXCEPTIONAL_STRING are
2724 used to indicate non-executed blocks. */
2727 output_line_beginning (FILE *f
, bool exists
, bool unexceptional
,
2728 bool has_unexecuted_block
,
2729 gcov_type count
, unsigned line_num
,
2730 const char *exceptional_string
,
2731 const char *unexceptional_string
)
2738 s
= format_gcov (count
, 0, -1);
2739 if (has_unexecuted_block
2740 && bbg_supports_has_unexecuted_blocks
)
2742 if (flag_use_colors
)
2744 pad_count_string (s
);
2745 s
.insert (0, SGR_SEQ (COLOR_BG_MAGENTA
2746 COLOR_SEPARATOR COLOR_FG_WHITE
));
2752 pad_count_string (s
);
2756 if (flag_use_colors
)
2759 pad_count_string (s
);
2761 s
.insert (0, SGR_SEQ (COLOR_BG_RED
2762 COLOR_SEPARATOR COLOR_FG_WHITE
));
2764 s
.insert (0, SGR_SEQ (COLOR_BG_CYAN
2765 COLOR_SEPARATOR COLOR_FG_WHITE
));
2770 s
= unexceptional
? unexceptional_string
: exceptional_string
;
2771 pad_count_string (s
);
2778 pad_count_string (s
);
2781 fprintf (f
, "%s:%5u", s
.c_str (), line_num
);
2785 print_source_line (FILE *f
, const vector
<const char *> &source_lines
,
2788 gcc_assert (line
>= 1);
2789 gcc_assert (line
<= source_lines
.size ());
2791 fprintf (f
, ":%s\n", source_lines
[line
- 1]);
2794 /* Output line details for LINE and print it to F file. LINE lives on
2798 output_line_details (FILE *f
, const line_info
*line
, unsigned line_num
)
2800 if (flag_all_blocks
)
2806 for (vector
<block_t
*>::const_iterator it
= line
->blocks
.begin ();
2807 it
!= line
->blocks
.end (); it
++)
2809 if (!(*it
)->is_call_return
)
2811 output_line_beginning (f
, line
->exists
,
2812 (*it
)->exceptional
, false,
2813 (*it
)->count
, line_num
,
2815 fprintf (f
, "-block %2d", ix
++);
2817 fprintf (f
, " (BB %u)", (*it
)->id
);
2821 for (arc
= (*it
)->succ
; arc
; arc
= arc
->succ_next
)
2822 jx
+= output_branch_count (f
, jx
, arc
);
2825 else if (flag_branches
)
2830 for (vector
<arc_t
*>::const_iterator it
= line
->branches
.begin ();
2831 it
!= line
->branches
.end (); it
++)
2832 ix
+= output_branch_count (f
, ix
, (*it
));
2836 /* Output detail statistics about function FN to file F. */
2839 output_function_details (FILE *f
, const function_info
*fn
)
2844 arc_t
*arc
= fn
->blocks
[EXIT_BLOCK
].pred
;
2845 gcov_type return_count
= fn
->blocks
[EXIT_BLOCK
].count
;
2846 gcov_type called_count
= fn
->blocks
[ENTRY_BLOCK
].count
;
2848 for (; arc
; arc
= arc
->pred_next
)
2850 return_count
-= arc
->count
;
2852 fprintf (f
, "function %s",
2853 flag_demangled_names
? fn
->demangled_name
: fn
->name
);
2854 fprintf (f
, " called %s",
2855 format_gcov (called_count
, 0, -1));
2856 fprintf (f
, " returned %s",
2857 format_gcov (return_count
, called_count
, 0));
2858 fprintf (f
, " blocks executed %s",
2859 format_gcov (fn
->blocks_executed
, fn
->blocks
.size () - 2,
2864 /* Read in the source file one line at a time, and output that line to
2865 the gcov file preceded by its execution count and other
2869 output_lines (FILE *gcov_file
, const source_info
*src
)
2871 #define DEFAULT_LINE_START " -: 0:"
2872 #define FN_SEPARATOR "------------------\n"
2877 fprintf (gcov_file
, DEFAULT_LINE_START
"Source:%s\n", src
->coverage
.name
);
2878 if (!multiple_files
)
2880 fprintf (gcov_file
, DEFAULT_LINE_START
"Graph:%s\n", bbg_file_name
);
2881 fprintf (gcov_file
, DEFAULT_LINE_START
"Data:%s\n",
2882 no_data_file
? "-" : da_file_name
);
2883 fprintf (gcov_file
, DEFAULT_LINE_START
"Runs:%u\n", object_runs
);
2885 fprintf (gcov_file
, DEFAULT_LINE_START
"Programs:%u\n", program_count
);
2887 source_file
= fopen (src
->name
, "r");
2889 fnotice (stderr
, "Cannot open source file %s\n", src
->name
);
2890 else if (src
->file_time
== 0)
2891 fprintf (gcov_file
, DEFAULT_LINE_START
"Source is newer than graph\n");
2893 vector
<const char *> source_lines
;
2895 while ((retval
= read_line (source_file
)) != NULL
)
2896 source_lines
.push_back (xstrdup (retval
));
2898 unsigned line_start_group
= 0;
2899 vector
<function_t
*> fns
;
2901 for (unsigned line_num
= 1; line_num
<= source_lines
.size (); line_num
++)
2903 if (line_num
>= src
->lines
.size ())
2905 fprintf (gcov_file
, "%9s:%5u", "-", line_num
);
2906 print_source_line (gcov_file
, source_lines
, line_num
);
2910 const line_info
*line
= &src
->lines
[line_num
];
2912 if (line_start_group
== 0)
2914 fns
= src
->get_functions_at_location (line_num
);
2915 if (fns
.size () > 1)
2916 line_start_group
= fns
[0]->end_line
;
2917 else if (fns
.size () == 1)
2919 function_t
*fn
= fns
[0];
2920 output_function_details (gcov_file
, fn
);
2924 /* For lines which don't exist in the .bb file, print '-' before
2925 the source line. For lines which exist but were never
2926 executed, print '#####' or '=====' before the source line.
2927 Otherwise, print the execution count before the source line.
2928 There are 16 spaces of indentation added before the source
2929 line so that tabs won't be messed up. */
2930 output_line_beginning (gcov_file
, line
->exists
, line
->unexceptional
,
2931 line
->has_unexecuted_block
, line
->count
,
2932 line_num
, "=====", "#####");
2934 print_source_line (gcov_file
, source_lines
, line_num
);
2935 output_line_details (gcov_file
, line
, line_num
);
2937 if (line_start_group
== line_num
)
2939 for (vector
<function_t
*>::iterator it
= fns
.begin ();
2940 it
!= fns
.end (); it
++)
2942 function_info
*fn
= *it
;
2943 vector
<line_info
> &lines
= fn
->lines
;
2945 fprintf (gcov_file
, FN_SEPARATOR
);
2948 = flag_demangled_names
? fn
->demangled_name
: fn
->name
;
2950 if (flag_use_colors
)
2952 fn_name
.insert (0, SGR_SEQ (COLOR_FG_CYAN
));
2953 fn_name
+= SGR_RESET
;
2956 fprintf (gcov_file
, "%s:\n", fn_name
.c_str ());
2958 output_function_details (gcov_file
, fn
);
2960 /* Print all lines covered by the function. */
2961 for (unsigned i
= 0; i
< lines
.size (); i
++)
2963 line_info
*line
= &lines
[i
];
2964 unsigned l
= fn
->start_line
+ i
;
2966 /* For lines which don't exist in the .bb file, print '-'
2967 before the source line. For lines which exist but
2968 were never executed, print '#####' or '=====' before
2969 the source line. Otherwise, print the execution count
2970 before the source line.
2971 There are 16 spaces of indentation added before the source
2972 line so that tabs won't be messed up. */
2973 output_line_beginning (gcov_file
, line
->exists
,
2974 line
->unexceptional
,
2975 line
->has_unexecuted_block
,
2977 l
, "=====", "#####");
2979 print_source_line (gcov_file
, source_lines
, l
);
2980 output_line_details (gcov_file
, line
, l
);
2984 fprintf (gcov_file
, FN_SEPARATOR
);
2985 line_start_group
= 0;
2990 fclose (source_file
);