gcno file: do not stream block flags (PR gcov-profile/80031).
[official-gcc.git] / gcc / gcov.c
blob63f6a75f1af2069b92a4a18d493dc031f2bbac16
1 /* Gcov.c: prepend line execution counts and branch probabilities to a
2 source file.
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)
11 any later version.
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. */
33 #include "config.h"
34 #define INCLUDE_ALGORITHM
35 #define INCLUDE_VECTOR
36 #include "system.h"
37 #include "coretypes.h"
38 #include "tm.h"
39 #include "intl.h"
40 #include "diagnostic.h"
41 #include "version.h"
42 #include "demangle.h"
44 #include <getopt.h>
46 #include "md5.h"
48 using namespace std;
50 #define IN_GCOV 1
51 #include "gcov-io.h"
52 #include "gcov-io.c"
54 /* The gcno file is generated by -ftest-coverage option. The gcda file is
55 generated by a program compiled with -fprofile-arcs. Their formats
56 are documented in gcov-io.h. */
58 /* The functions in this file for creating and solution program flow graphs
59 are very similar to functions in the gcc source file profile.c. In
60 some places we make use of the knowledge of how profile.c works to
61 select particular algorithms here. */
63 /* The code validates that the profile information read in corresponds
64 to the code currently being compiled. Rather than checking for
65 identical files, the code below compares a checksum on the CFG
66 (based on the order of basic blocks and the arcs in the CFG). If
67 the CFG checksum in the gcda file match the CFG checksum in the
68 gcno file, the profile data will be used. */
70 /* This is the size of the buffer used to read in source file lines. */
72 struct function_info;
73 struct block_info;
74 struct source_info;
76 /* Describes an arc between two basic blocks. */
78 typedef struct arc_info
80 /* source and destination blocks. */
81 struct block_info *src;
82 struct block_info *dst;
84 /* transition counts. */
85 gcov_type count;
86 /* used in cycle search, so that we do not clobber original counts. */
87 gcov_type cs_count;
89 unsigned int count_valid : 1;
90 unsigned int on_tree : 1;
91 unsigned int fake : 1;
92 unsigned int fall_through : 1;
94 /* Arc to a catch handler. */
95 unsigned int is_throw : 1;
97 /* Arc is for a function that abnormally returns. */
98 unsigned int is_call_non_return : 1;
100 /* Arc is for catch/setjmp. */
101 unsigned int is_nonlocal_return : 1;
103 /* Is an unconditional branch. */
104 unsigned int is_unconditional : 1;
106 /* Loop making arc. */
107 unsigned int cycle : 1;
109 /* Next branch on line. */
110 struct arc_info *line_next;
112 /* Links to next arc on src and dst lists. */
113 struct arc_info *succ_next;
114 struct arc_info *pred_next;
115 } arc_t;
117 /* Describes a basic block. Contains lists of arcs to successor and
118 predecessor blocks. */
120 typedef struct block_info
122 /* Chain of exit and entry arcs. */
123 arc_t *succ;
124 arc_t *pred;
126 /* Number of unprocessed exit and entry arcs. */
127 gcov_type num_succ;
128 gcov_type num_pred;
130 /* Block execution count. */
131 gcov_type count;
132 unsigned count_valid : 1;
133 unsigned valid_chain : 1;
134 unsigned invalid_chain : 1;
135 unsigned exceptional : 1;
137 /* Block is a call instrumenting site. */
138 unsigned is_call_site : 1; /* Does the call. */
139 unsigned is_call_return : 1; /* Is the return. */
141 /* Block is a landing pad for longjmp or throw. */
142 unsigned is_nonlocal_return : 1;
144 union
146 struct
148 /* Array of line numbers and source files. source files are
149 introduced by a linenumber of zero, the next 'line number' is
150 the number of the source file. Always starts with a source
151 file. */
152 unsigned *encoding;
153 unsigned num;
154 } line; /* Valid until blocks are linked onto lines */
155 struct
157 /* Single line graph cycle workspace. Used for all-blocks
158 mode. */
159 arc_t *arc;
160 unsigned ident;
161 } cycle; /* Used in all-blocks mode, after blocks are linked onto
162 lines. */
163 } u;
165 /* Temporary chain for solving graph, and for chaining blocks on one
166 line. */
167 struct block_info *chain;
169 } block_t;
171 /* Describes a single function. Contains an array of basic blocks. */
173 typedef struct function_info
175 /* Name of function. */
176 char *name;
177 char *demangled_name;
178 unsigned ident;
179 unsigned lineno_checksum;
180 unsigned cfg_checksum;
182 /* The graph contains at least one fake incoming edge. */
183 unsigned has_catch : 1;
185 /* Array of basic blocks. Like in GCC, the entry block is
186 at blocks[0] and the exit block is at blocks[1]. */
187 #define ENTRY_BLOCK (0)
188 #define EXIT_BLOCK (1)
189 block_t *blocks;
190 unsigned num_blocks;
191 unsigned blocks_executed;
193 /* Raw arc coverage counts. */
194 gcov_type *counts;
195 unsigned num_counts;
197 /* First line number & file. */
198 unsigned line;
199 unsigned src;
201 /* Next function in same source file. */
202 struct function_info *next_file_fn;
204 /* Next function. */
205 struct function_info *next;
206 } function_t;
208 /* Describes coverage of a file or function. */
210 typedef struct coverage_info
212 int lines;
213 int lines_executed;
215 int branches;
216 int branches_executed;
217 int branches_taken;
219 int calls;
220 int calls_executed;
222 char *name;
223 } coverage_t;
225 /* Describes a single line of source. Contains a chain of basic blocks
226 with code on it. */
228 typedef struct line_info
230 /* Return true when NEEDLE is one of basic blocks the line belongs to. */
231 bool has_block (block_t *needle);
233 gcov_type count; /* execution count */
234 union
236 arc_t *branches; /* branches from blocks that end on this
237 line. Used for branch-counts when not
238 all-blocks mode. */
239 block_t *blocks; /* blocks which start on this line. Used
240 in all-blocks mode. */
241 } u;
242 unsigned exists : 1;
243 unsigned unexceptional : 1;
244 } line_t;
246 bool
247 line_t::has_block (block_t *needle)
249 for (block_t *n = u.blocks; n; n = n->chain)
250 if (n == needle)
251 return true;
253 return false;
256 /* Describes a file mentioned in the block graph. Contains an array
257 of line info. */
259 typedef struct source_info
261 /* Canonical name of source file. */
262 char *name;
263 time_t file_time;
265 /* Array of line information. */
266 line_t *lines;
267 unsigned num_lines;
269 coverage_t coverage;
271 /* Functions in this source file. These are in ascending line
272 number order. */
273 function_t *functions;
274 } source_t;
276 typedef struct name_map
278 char *name; /* Source file name */
279 unsigned src; /* Source file */
280 } name_map_t;
282 /* Holds a list of function basic block graphs. */
284 static function_t *functions;
285 static function_t **fn_end = &functions;
287 static source_t *sources; /* Array of source files */
288 static unsigned n_sources; /* Number of sources */
289 static unsigned a_sources; /* Allocated sources */
291 static name_map_t *names; /* Mapping of file names to sources */
292 static unsigned n_names; /* Number of names */
293 static unsigned a_names; /* Allocated names */
295 /* This holds data summary information. */
297 static unsigned object_runs;
298 static unsigned program_count;
300 static unsigned total_lines;
301 static unsigned total_executed;
303 /* Modification time of graph file. */
305 static time_t bbg_file_time;
307 /* Name of the notes (gcno) output file. The "bbg" prefix is for
308 historical reasons, when the notes file contained only the
309 basic block graph notes. */
311 static char *bbg_file_name;
313 /* Stamp of the bbg file */
314 static unsigned bbg_stamp;
316 /* Name and file pointer of the input file for the count data (gcda). */
318 static char *da_file_name;
320 /* Data file is missing. */
322 static int no_data_file;
324 /* If there is several input files, compute and display results after
325 reading all data files. This way if two or more gcda file refer to
326 the same source file (eg inline subprograms in a .h file), the
327 counts are added. */
329 static int multiple_files = 0;
331 /* Output branch probabilities. */
333 static int flag_branches = 0;
335 /* Show unconditional branches too. */
336 static int flag_unconditional = 0;
338 /* Output a gcov file if this is true. This is on by default, and can
339 be turned off by the -n option. */
341 static int flag_gcov_file = 1;
343 /* Output progress indication if this is true. This is off by default
344 and can be turned on by the -d option. */
346 static int flag_display_progress = 0;
348 /* Output *.gcov file in intermediate format used by 'lcov'. */
350 static int flag_intermediate_format = 0;
352 /* Output demangled function names. */
354 static int flag_demangled_names = 0;
356 /* For included files, make the gcov output file name include the name
357 of the input source file. For example, if x.h is included in a.c,
358 then the output file name is a.c##x.h.gcov instead of x.h.gcov. */
360 static int flag_long_names = 0;
362 /* For situations when a long name can potentially hit filesystem path limit,
363 let's calculate md5sum of the path and append it to a file name. */
365 static int flag_hash_filenames = 0;
367 /* Output count information for every basic block, not merely those
368 that contain line number information. */
370 static int flag_all_blocks = 0;
372 /* Output summary info for each function. */
374 static int flag_function_summary = 0;
376 /* Object directory file prefix. This is the directory/file where the
377 graph and data files are looked for, if nonzero. */
379 static char *object_directory = 0;
381 /* Source directory prefix. This is removed from source pathnames
382 that match, when generating the output file name. */
384 static char *source_prefix = 0;
385 static size_t source_length = 0;
387 /* Only show data for sources with relative pathnames. Absolute ones
388 usually indicate a system header file, which although it may
389 contain inline functions, is usually uninteresting. */
390 static int flag_relative_only = 0;
392 /* Preserve all pathname components. Needed when object files and
393 source files are in subdirectories. '/' is mangled as '#', '.' is
394 elided and '..' mangled to '^'. */
396 static int flag_preserve_paths = 0;
398 /* Output the number of times a branch was taken as opposed to the percentage
399 of times it was taken. */
401 static int flag_counts = 0;
403 /* Forward declarations. */
404 static int process_args (int, char **);
405 static void print_usage (int) ATTRIBUTE_NORETURN;
406 static void print_version (void) ATTRIBUTE_NORETURN;
407 static void process_file (const char *);
408 static void generate_results (const char *);
409 static void create_file_names (const char *);
410 static int name_search (const void *, const void *);
411 static int name_sort (const void *, const void *);
412 static char *canonicalize_name (const char *);
413 static unsigned find_source (const char *);
414 static function_t *read_graph_file (void);
415 static int read_count_file (function_t *);
416 static void solve_flow_graph (function_t *);
417 static void find_exception_blocks (function_t *);
418 static void add_branch_counts (coverage_t *, const arc_t *);
419 static void add_line_counts (coverage_t *, function_t *);
420 static void executed_summary (unsigned, unsigned);
421 static void function_summary (const coverage_t *, const char *);
422 static const char *format_gcov (gcov_type, gcov_type, int);
423 static void accumulate_line_counts (source_t *);
424 static void output_gcov_file (const char *, source_t *);
425 static int output_branch_count (FILE *, int, const arc_t *);
426 static void output_lines (FILE *, const source_t *);
427 static char *make_gcov_file_name (const char *, const char *);
428 static char *mangle_name (const char *, char *);
429 static void release_structures (void);
430 static void release_function (function_t *);
431 extern int main (int, char **);
433 /* Cycle detection!
434 There are a bajillion algorithms that do this. Boost's function is named
435 hawick_cycles, so I used the algorithm by K. A. Hawick and H. A. James in
436 "Enumerating Circuits and Loops in Graphs with Self-Arcs and Multiple-Arcs"
437 (url at <http://complexity.massey.ac.nz/cstn/013/cstn-013.pdf>).
439 The basic algorithm is simple: effectively, we're finding all simple paths
440 in a subgraph (that shrinks every iteration). Duplicates are filtered by
441 "blocking" a path when a node is added to the path (this also prevents non-
442 simple paths)--the node is unblocked only when it participates in a cycle.
445 typedef vector<arc_t *> arc_vector_t;
446 typedef vector<const block_t *> block_vector_t;
448 /* Enum with types of loop in CFG. */
450 enum loop_type
452 NO_LOOP = 0,
453 LOOP = 1,
454 NEGATIVE_LOOP = 3
457 /* Loop_type operator that merges two values: A and B. */
459 inline loop_type& operator |= (loop_type& a, loop_type b)
461 return a = static_cast<loop_type> (a | b);
464 /* Handle cycle identified by EDGES, where the function finds minimum cs_count
465 and subtract the value from all counts. The subtracted value is added
466 to COUNT. Returns type of loop. */
468 static loop_type
469 handle_cycle (const arc_vector_t &edges, int64_t &count)
471 /* Find the minimum edge of the cycle, and reduce all nodes in the cycle by
472 that amount. */
473 int64_t cycle_count = INTTYPE_MAXIMUM (int64_t);
474 for (unsigned i = 0; i < edges.size (); i++)
476 int64_t ecount = edges[i]->cs_count;
477 if (cycle_count > ecount)
478 cycle_count = ecount;
480 count += cycle_count;
481 for (unsigned i = 0; i < edges.size (); i++)
482 edges[i]->cs_count -= cycle_count;
484 return cycle_count < 0 ? NEGATIVE_LOOP : LOOP;
487 /* Unblock a block U from BLOCKED. Apart from that, iterate all blocks
488 blocked by U in BLOCK_LISTS. */
490 static void
491 unblock (const block_t *u, block_vector_t &blocked,
492 vector<block_vector_t > &block_lists)
494 block_vector_t::iterator it = find (blocked.begin (), blocked.end (), u);
495 if (it == blocked.end ())
496 return;
498 unsigned index = it - blocked.begin ();
499 blocked.erase (it);
501 for (block_vector_t::iterator it2 = block_lists[index].begin ();
502 it2 != block_lists[index].end (); it2++)
503 unblock (*it2, blocked, block_lists);
504 for (unsigned j = 0; j < block_lists[index].size (); j++)
505 unblock (u, blocked, block_lists);
507 block_lists.erase (block_lists.begin () + index);
510 /* Find circuit going to block V, PATH is provisional seen cycle.
511 BLOCKED is vector of blocked vertices, BLOCK_LISTS contains vertices
512 blocked by a block. COUNT is accumulated count of the current LINE.
513 Returns what type of loop it contains. */
515 static loop_type
516 circuit (block_t *v, arc_vector_t &path, block_t *start,
517 block_vector_t &blocked, vector<block_vector_t> &block_lists,
518 line_t &linfo, int64_t &count)
520 loop_type result = NO_LOOP;
522 /* Add v to the block list. */
523 gcc_assert (find (blocked.begin (), blocked.end (), v) == blocked.end ());
524 blocked.push_back (v);
525 block_lists.push_back (block_vector_t ());
527 for (arc_t *arc = v->succ; arc; arc = arc->succ_next)
529 block_t *w = arc->dst;
530 if (w < start || !linfo.has_block (w))
531 continue;
533 path.push_back (arc);
534 if (w == start)
535 /* Cycle has been found. */
536 result |= handle_cycle (path, count);
537 else if (find (blocked.begin (), blocked.end (), w) == blocked.end ())
538 result |= circuit (w, path, start, blocked, block_lists, linfo, count);
540 path.pop_back ();
543 if (result != NO_LOOP)
544 unblock (v, blocked, block_lists);
545 else
546 for (arc_t *arc = v->succ; arc; arc = arc->succ_next)
548 block_t *w = arc->dst;
549 if (w < start || !linfo.has_block (w))
550 continue;
552 size_t index
553 = find (blocked.begin (), blocked.end (), w) - blocked.begin ();
554 gcc_assert (index < blocked.size ());
555 block_vector_t &list = block_lists[index];
556 if (find (list.begin (), list.end (), v) == list.end ())
557 list.push_back (v);
560 return result;
563 /* Find cycles for a LINFO. If HANDLE_NEGATIVE_CYCLES is set and the line
564 contains a negative loop, then perform the same function once again. */
566 static gcov_type
567 get_cycles_count (line_t &linfo, bool handle_negative_cycles = true)
569 /* Note that this algorithm works even if blocks aren't in sorted order.
570 Each iteration of the circuit detection is completely independent
571 (except for reducing counts, but that shouldn't matter anyways).
572 Therefore, operating on a permuted order (i.e., non-sorted) only
573 has the effect of permuting the output cycles. */
575 loop_type result = NO_LOOP;
576 gcov_type count = 0;
577 for (block_t *block = linfo.u.blocks; block; block = block->chain)
579 arc_vector_t path;
580 block_vector_t blocked;
581 vector<block_vector_t > block_lists;
582 result |= circuit (block, path, block, blocked, block_lists, linfo,
583 count);
586 /* If we have a negative cycle, repeat the find_cycles routine. */
587 if (result == NEGATIVE_LOOP && handle_negative_cycles)
588 count += get_cycles_count (linfo, false);
590 return count;
594 main (int argc, char **argv)
596 int argno;
597 int first_arg;
598 const char *p;
600 p = argv[0] + strlen (argv[0]);
601 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
602 --p;
603 progname = p;
605 xmalloc_set_program_name (progname);
607 /* Unlock the stdio streams. */
608 unlock_std_streams ();
610 gcc_init_libintl ();
612 diagnostic_initialize (global_dc, 0);
614 /* Handle response files. */
615 expandargv (&argc, &argv);
617 a_names = 10;
618 names = XNEWVEC (name_map_t, a_names);
619 a_sources = 10;
620 sources = XNEWVEC (source_t, a_sources);
622 argno = process_args (argc, argv);
623 if (optind == argc)
624 print_usage (true);
626 if (argc - argno > 1)
627 multiple_files = 1;
629 first_arg = argno;
631 for (; argno != argc; argno++)
633 if (flag_display_progress)
634 printf ("Processing file %d out of %d\n", argno - first_arg + 1,
635 argc - first_arg);
636 process_file (argv[argno]);
639 generate_results (multiple_files ? NULL : argv[argc - 1]);
641 release_structures ();
643 return 0;
646 /* Print a usage message and exit. If ERROR_P is nonzero, this is an error,
647 otherwise the output of --help. */
649 static void
650 print_usage (int error_p)
652 FILE *file = error_p ? stderr : stdout;
653 int status = error_p ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE;
655 fnotice (file, "Usage: gcov [OPTION...] SOURCE|OBJ...\n\n");
656 fnotice (file, "Print code coverage information.\n\n");
657 fnotice (file, " -h, --help Print this help, then exit\n");
658 fnotice (file, " -a, --all-blocks Show information for every basic block\n");
659 fnotice (file, " -b, --branch-probabilities Include branch probabilities in output\n");
660 fnotice (file, " -c, --branch-counts Output counts of branches taken\n\
661 rather than percentages\n");
662 fnotice (file, " -d, --display-progress Display progress information\n");
663 fnotice (file, " -f, --function-summaries Output summaries for each function\n");
664 fnotice (file, " -i, --intermediate-format Output .gcov file in intermediate text format\n");
665 fnotice (file, " -l, --long-file-names Use long output file names for included\n\
666 source files\n");
667 fnotice (file, " -m, --demangled-names Output demangled function names\n");
668 fnotice (file, " -n, --no-output Do not create an output file\n");
669 fnotice (file, " -o, --object-directory DIR|FILE Search for object files in DIR or called FILE\n");
670 fnotice (file, " -p, --preserve-paths Preserve all pathname components\n");
671 fnotice (file, " -r, --relative-only Only show data for relative sources\n");
672 fnotice (file, " -s, --source-prefix DIR Source prefix to elide\n");
673 fnotice (file, " -u, --unconditional-branches Show unconditional branch counts too\n");
674 fnotice (file, " -v, --version Print version number, then exit\n");
675 fnotice (file, " -x, --hash-filenames Hash long pathnames\n");
676 fnotice (file, "\nFor bug reporting instructions, please see:\n%s.\n",
677 bug_report_url);
678 exit (status);
681 /* Print version information and exit. */
683 static void
684 print_version (void)
686 fnotice (stdout, "gcov %s%s\n", pkgversion_string, version_string);
687 fprintf (stdout, "Copyright %s 2017 Free Software Foundation, Inc.\n",
688 _("(C)"));
689 fnotice (stdout,
690 _("This is free software; see the source for copying conditions.\n"
691 "There is NO warranty; not even for MERCHANTABILITY or \n"
692 "FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
693 exit (SUCCESS_EXIT_CODE);
696 static const struct option options[] =
698 { "help", no_argument, NULL, 'h' },
699 { "version", no_argument, NULL, 'v' },
700 { "all-blocks", no_argument, NULL, 'a' },
701 { "branch-probabilities", no_argument, NULL, 'b' },
702 { "branch-counts", no_argument, NULL, 'c' },
703 { "intermediate-format", no_argument, NULL, 'i' },
704 { "no-output", no_argument, NULL, 'n' },
705 { "long-file-names", no_argument, NULL, 'l' },
706 { "function-summaries", no_argument, NULL, 'f' },
707 { "demangled-names", no_argument, NULL, 'm' },
708 { "preserve-paths", no_argument, NULL, 'p' },
709 { "relative-only", no_argument, NULL, 'r' },
710 { "object-directory", required_argument, NULL, 'o' },
711 { "object-file", required_argument, NULL, 'o' },
712 { "source-prefix", required_argument, NULL, 's' },
713 { "unconditional-branches", no_argument, NULL, 'u' },
714 { "display-progress", no_argument, NULL, 'd' },
715 { "hash-filenames", no_argument, NULL, 'x' },
716 { 0, 0, 0, 0 }
719 /* Process args, return index to first non-arg. */
721 static int
722 process_args (int argc, char **argv)
724 int opt;
726 const char *opts = "abcdfhilmno:prs:uvx";
727 while ((opt = getopt_long (argc, argv, opts, options, NULL)) != -1)
729 switch (opt)
731 case 'a':
732 flag_all_blocks = 1;
733 break;
734 case 'b':
735 flag_branches = 1;
736 break;
737 case 'c':
738 flag_counts = 1;
739 break;
740 case 'f':
741 flag_function_summary = 1;
742 break;
743 case 'h':
744 print_usage (false);
745 /* print_usage will exit. */
746 case 'l':
747 flag_long_names = 1;
748 break;
749 case 'm':
750 flag_demangled_names = 1;
751 break;
752 case 'n':
753 flag_gcov_file = 0;
754 break;
755 case 'o':
756 object_directory = optarg;
757 break;
758 case 's':
759 source_prefix = optarg;
760 source_length = strlen (source_prefix);
761 break;
762 case 'r':
763 flag_relative_only = 1;
764 break;
765 case 'p':
766 flag_preserve_paths = 1;
767 break;
768 case 'u':
769 flag_unconditional = 1;
770 break;
771 case 'i':
772 flag_intermediate_format = 1;
773 flag_gcov_file = 1;
774 break;
775 case 'd':
776 flag_display_progress = 1;
777 break;
778 case 'x':
779 flag_hash_filenames = 1;
780 break;
781 case 'v':
782 print_version ();
783 /* print_version will exit. */
784 default:
785 print_usage (true);
786 /* print_usage will exit. */
790 return optind;
793 /* Output the result in intermediate format used by 'lcov'.
795 The intermediate format contains a single file named 'foo.cc.gcov',
796 with no source code included. A sample output is
798 file:foo.cc
799 function:5,1,_Z3foov
800 function:13,1,main
801 function:19,1,_GLOBAL__sub_I__Z3foov
802 function:19,1,_Z41__static_initialization_and_destruction_0ii
803 lcount:5,1
804 lcount:7,9
805 lcount:9,8
806 lcount:11,1
807 file:/.../iostream
808 lcount:74,1
809 file:/.../basic_ios.h
810 file:/.../ostream
811 file:/.../ios_base.h
812 function:157,0,_ZStorSt12_Ios_IostateS_
813 lcount:157,0
814 file:/.../char_traits.h
815 function:258,0,_ZNSt11char_traitsIcE6lengthEPKc
816 lcount:258,0
819 The default gcov outputs multiple files: 'foo.cc.gcov',
820 'iostream.gcov', 'ios_base.h.gcov', etc. with source code
821 included. Instead the intermediate format here outputs only a single
822 file 'foo.cc.gcov' similar to the above example. */
824 static void
825 output_intermediate_file (FILE *gcov_file, source_t *src)
827 unsigned line_num; /* current line number. */
828 const line_t *line; /* current line info ptr. */
829 function_t *fn; /* current function info ptr. */
831 fprintf (gcov_file, "file:%s\n", src->name); /* source file name */
833 for (fn = src->functions; fn; fn = fn->next_file_fn)
835 /* function:<name>,<line_number>,<execution_count> */
836 fprintf (gcov_file, "function:%d,%s,%s\n", fn->line,
837 format_gcov (fn->blocks[0].count, 0, -1),
838 flag_demangled_names ? fn->demangled_name : fn->name);
841 for (line_num = 1, line = &src->lines[line_num];
842 line_num < src->num_lines;
843 line_num++, line++)
845 arc_t *arc;
846 if (line->exists)
847 fprintf (gcov_file, "lcount:%u,%s\n", line_num,
848 format_gcov (line->count, 0, -1));
849 if (flag_branches)
850 for (arc = line->u.branches; arc; arc = arc->line_next)
852 if (!arc->is_unconditional && !arc->is_call_non_return)
854 const char *branch_type;
855 /* branch:<line_num>,<branch_coverage_type>
856 branch_coverage_type
857 : notexec (Branch not executed)
858 : taken (Branch executed and taken)
859 : nottaken (Branch executed, but not taken)
861 if (arc->src->count)
862 branch_type = (arc->count > 0) ? "taken" : "nottaken";
863 else
864 branch_type = "notexec";
865 fprintf (gcov_file, "branch:%d,%s\n", line_num, branch_type);
871 /* Process a single input file. */
873 static void
874 process_file (const char *file_name)
876 function_t *fns;
878 create_file_names (file_name);
879 fns = read_graph_file ();
880 if (!fns)
881 return;
883 read_count_file (fns);
884 while (fns)
886 function_t *fn = fns;
888 fns = fn->next;
889 fn->next = NULL;
890 if (fn->counts || no_data_file)
892 unsigned src = fn->src;
893 unsigned line = fn->line;
894 unsigned block_no;
895 function_t *probe, **prev;
897 /* Now insert it into the source file's list of
898 functions. Normally functions will be encountered in
899 ascending order, so a simple scan is quick. Note we're
900 building this list in reverse order. */
901 for (prev = &sources[src].functions;
902 (probe = *prev); prev = &probe->next_file_fn)
903 if (probe->line <= line)
904 break;
905 fn->next_file_fn = probe;
906 *prev = fn;
908 /* Mark last line in files touched by function. */
909 for (block_no = 0; block_no != fn->num_blocks; block_no++)
911 unsigned *enc = fn->blocks[block_no].u.line.encoding;
912 unsigned num = fn->blocks[block_no].u.line.num;
914 for (; num--; enc++)
915 if (!*enc)
917 if (enc[1] != src)
919 if (line >= sources[src].num_lines)
920 sources[src].num_lines = line + 1;
921 line = 0;
922 src = enc[1];
924 enc++;
925 num--;
927 else if (*enc > line)
928 line = *enc;
930 if (line >= sources[src].num_lines)
931 sources[src].num_lines = line + 1;
933 solve_flow_graph (fn);
934 if (fn->has_catch)
935 find_exception_blocks (fn);
936 *fn_end = fn;
937 fn_end = &fn->next;
939 else
940 /* The function was not in the executable -- some other
941 instance must have been selected. */
942 release_function (fn);
946 static void
947 output_gcov_file (const char *file_name, source_t *src)
949 char *gcov_file_name = make_gcov_file_name (file_name, src->coverage.name);
951 if (src->coverage.lines)
953 FILE *gcov_file = fopen (gcov_file_name, "w");
954 if (gcov_file)
956 fnotice (stdout, "Creating '%s'\n", gcov_file_name);
958 if (flag_intermediate_format)
959 output_intermediate_file (gcov_file, src);
960 else
961 output_lines (gcov_file, src);
962 if (ferror (gcov_file))
963 fnotice (stderr, "Error writing output file '%s'\n", gcov_file_name);
964 fclose (gcov_file);
966 else
967 fnotice (stderr, "Could not open output file '%s'\n", gcov_file_name);
969 else
971 unlink (gcov_file_name);
972 fnotice (stdout, "Removing '%s'\n", gcov_file_name);
974 free (gcov_file_name);
977 static void
978 generate_results (const char *file_name)
980 unsigned ix;
981 source_t *src;
982 function_t *fn;
984 for (ix = n_sources, src = sources; ix--; src++)
985 if (src->num_lines)
986 src->lines = XCNEWVEC (line_t, src->num_lines);
988 for (fn = functions; fn; fn = fn->next)
990 coverage_t coverage;
992 memset (&coverage, 0, sizeof (coverage));
993 coverage.name = flag_demangled_names ? fn->demangled_name : fn->name;
994 add_line_counts (flag_function_summary ? &coverage : NULL, fn);
995 if (flag_function_summary)
997 function_summary (&coverage, "Function");
998 fnotice (stdout, "\n");
1002 if (file_name)
1004 name_map_t *name_map = (name_map_t *)bsearch
1005 (file_name, names, n_names, sizeof (*names), name_search);
1006 if (name_map)
1007 file_name = sources[name_map->src].coverage.name;
1008 else
1009 file_name = canonicalize_name (file_name);
1012 for (ix = n_sources, src = sources; ix--; src++)
1014 if (flag_relative_only)
1016 /* Ignore this source, if it is an absolute path (after
1017 source prefix removal). */
1018 char first = src->coverage.name[0];
1020 #if HAVE_DOS_BASED_FILE_SYSTEM
1021 if (first && src->coverage.name[1] == ':')
1022 first = src->coverage.name[2];
1023 #endif
1024 if (IS_DIR_SEPARATOR (first))
1025 continue;
1028 accumulate_line_counts (src);
1029 function_summary (&src->coverage, "File");
1030 total_lines += src->coverage.lines;
1031 total_executed += src->coverage.lines_executed;
1032 if (flag_gcov_file)
1034 output_gcov_file (file_name, src);
1035 fnotice (stdout, "\n");
1039 if (!file_name)
1040 executed_summary (total_lines, total_executed);
1043 /* Release a function structure */
1045 static void
1046 release_function (function_t *fn)
1048 unsigned ix;
1049 block_t *block;
1051 for (ix = fn->num_blocks, block = fn->blocks; ix--; block++)
1053 arc_t *arc, *arc_n;
1055 for (arc = block->succ; arc; arc = arc_n)
1057 arc_n = arc->succ_next;
1058 free (arc);
1061 free (fn->blocks);
1062 free (fn->counts);
1063 if (flag_demangled_names && fn->demangled_name != fn->name)
1064 free (fn->demangled_name);
1065 free (fn->name);
1068 /* Release all memory used. */
1070 static void
1071 release_structures (void)
1073 unsigned ix;
1074 function_t *fn;
1076 for (ix = n_sources; ix--;)
1077 free (sources[ix].lines);
1078 free (sources);
1080 for (ix = n_names; ix--;)
1081 free (names[ix].name);
1082 free (names);
1084 while ((fn = functions))
1086 functions = fn->next;
1087 release_function (fn);
1091 /* Generate the names of the graph and data files. If OBJECT_DIRECTORY
1092 is not specified, these are named from FILE_NAME sans extension. If
1093 OBJECT_DIRECTORY is specified and is a directory, the files are in that
1094 directory, but named from the basename of the FILE_NAME, sans extension.
1095 Otherwise OBJECT_DIRECTORY is taken to be the name of the object *file*
1096 and the data files are named from that. */
1098 static void
1099 create_file_names (const char *file_name)
1101 char *cptr;
1102 char *name;
1103 int length = strlen (file_name);
1104 int base;
1106 /* Free previous file names. */
1107 free (bbg_file_name);
1108 free (da_file_name);
1109 da_file_name = bbg_file_name = NULL;
1110 bbg_file_time = 0;
1111 bbg_stamp = 0;
1113 if (object_directory && object_directory[0])
1115 struct stat status;
1117 length += strlen (object_directory) + 2;
1118 name = XNEWVEC (char, length);
1119 name[0] = 0;
1121 base = !stat (object_directory, &status) && S_ISDIR (status.st_mode);
1122 strcat (name, object_directory);
1123 if (base && (!IS_DIR_SEPARATOR (name[strlen (name) - 1])))
1124 strcat (name, "/");
1126 else
1128 name = XNEWVEC (char, length + 1);
1129 strcpy (name, file_name);
1130 base = 0;
1133 if (base)
1135 /* Append source file name. */
1136 const char *cptr = lbasename (file_name);
1137 strcat (name, cptr ? cptr : file_name);
1140 /* Remove the extension. */
1141 cptr = strrchr (CONST_CAST (char *, lbasename (name)), '.');
1142 if (cptr)
1143 *cptr = 0;
1145 length = strlen (name);
1147 bbg_file_name = XNEWVEC (char, length + strlen (GCOV_NOTE_SUFFIX) + 1);
1148 strcpy (bbg_file_name, name);
1149 strcpy (bbg_file_name + length, GCOV_NOTE_SUFFIX);
1151 da_file_name = XNEWVEC (char, length + strlen (GCOV_DATA_SUFFIX) + 1);
1152 strcpy (da_file_name, name);
1153 strcpy (da_file_name + length, GCOV_DATA_SUFFIX);
1155 free (name);
1156 return;
1159 /* A is a string and B is a pointer to name_map_t. Compare for file
1160 name orderability. */
1162 static int
1163 name_search (const void *a_, const void *b_)
1165 const char *a = (const char *)a_;
1166 const name_map_t *b = (const name_map_t *)b_;
1168 #if HAVE_DOS_BASED_FILE_SYSTEM
1169 return strcasecmp (a, b->name);
1170 #else
1171 return strcmp (a, b->name);
1172 #endif
1175 /* A and B are a pointer to name_map_t. Compare for file name
1176 orderability. */
1178 static int
1179 name_sort (const void *a_, const void *b_)
1181 const name_map_t *a = (const name_map_t *)a_;
1182 return name_search (a->name, b_);
1185 /* Find or create a source file structure for FILE_NAME. Copies
1186 FILE_NAME on creation */
1188 static unsigned
1189 find_source (const char *file_name)
1191 name_map_t *name_map;
1192 char *canon;
1193 unsigned idx;
1194 struct stat status;
1196 if (!file_name)
1197 file_name = "<unknown>";
1198 name_map = (name_map_t *)bsearch
1199 (file_name, names, n_names, sizeof (*names), name_search);
1200 if (name_map)
1202 idx = name_map->src;
1203 goto check_date;
1206 if (n_names + 2 > a_names)
1208 /* Extend the name map array -- we'll be inserting one or two
1209 entries. */
1210 a_names *= 2;
1211 name_map = XNEWVEC (name_map_t, a_names);
1212 memcpy (name_map, names, n_names * sizeof (*names));
1213 free (names);
1214 names = name_map;
1217 /* Not found, try the canonical name. */
1218 canon = canonicalize_name (file_name);
1219 name_map = (name_map_t *) bsearch (canon, names, n_names, sizeof (*names),
1220 name_search);
1221 if (!name_map)
1223 /* Not found with canonical name, create a new source. */
1224 source_t *src;
1226 if (n_sources == a_sources)
1228 a_sources *= 2;
1229 src = XNEWVEC (source_t, a_sources);
1230 memcpy (src, sources, n_sources * sizeof (*sources));
1231 free (sources);
1232 sources = src;
1235 idx = n_sources;
1237 name_map = &names[n_names++];
1238 name_map->name = canon;
1239 name_map->src = idx;
1241 src = &sources[n_sources++];
1242 memset (src, 0, sizeof (*src));
1243 src->name = canon;
1244 src->coverage.name = src->name;
1245 if (source_length
1246 #if HAVE_DOS_BASED_FILE_SYSTEM
1247 /* You lose if separators don't match exactly in the
1248 prefix. */
1249 && !strncasecmp (source_prefix, src->coverage.name, source_length)
1250 #else
1251 && !strncmp (source_prefix, src->coverage.name, source_length)
1252 #endif
1253 && IS_DIR_SEPARATOR (src->coverage.name[source_length]))
1254 src->coverage.name += source_length + 1;
1255 if (!stat (src->name, &status))
1256 src->file_time = status.st_mtime;
1258 else
1259 idx = name_map->src;
1261 if (name_search (file_name, name_map))
1263 /* Append the non-canonical name. */
1264 name_map = &names[n_names++];
1265 name_map->name = xstrdup (file_name);
1266 name_map->src = idx;
1269 /* Resort the name map. */
1270 qsort (names, n_names, sizeof (*names), name_sort);
1272 check_date:
1273 if (sources[idx].file_time > bbg_file_time)
1275 static int info_emitted;
1277 fnotice (stderr, "%s:source file is newer than notes file '%s'\n",
1278 file_name, bbg_file_name);
1279 if (!info_emitted)
1281 fnotice (stderr,
1282 "(the message is displayed only once per source file)\n");
1283 info_emitted = 1;
1285 sources[idx].file_time = 0;
1288 return idx;
1291 /* Read the notes file. Return list of functions read -- in reverse order. */
1293 static function_t *
1294 read_graph_file (void)
1296 unsigned version;
1297 unsigned current_tag = 0;
1298 function_t *fn = NULL;
1299 function_t *fns = NULL;
1300 function_t **fns_end = &fns;
1301 unsigned src_idx = 0;
1302 unsigned ix;
1303 unsigned tag;
1305 if (!gcov_open (bbg_file_name, 1))
1307 fnotice (stderr, "%s:cannot open notes file\n", bbg_file_name);
1308 return fns;
1310 bbg_file_time = gcov_time ();
1311 if (!gcov_magic (gcov_read_unsigned (), GCOV_NOTE_MAGIC))
1313 fnotice (stderr, "%s:not a gcov notes file\n", bbg_file_name);
1314 gcov_close ();
1315 return fns;
1318 version = gcov_read_unsigned ();
1319 if (version != GCOV_VERSION)
1321 char v[4], e[4];
1323 GCOV_UNSIGNED2STRING (v, version);
1324 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
1326 fnotice (stderr, "%s:version '%.4s', prefer '%.4s'\n",
1327 bbg_file_name, v, e);
1329 bbg_stamp = gcov_read_unsigned ();
1331 while ((tag = gcov_read_unsigned ()))
1333 unsigned length = gcov_read_unsigned ();
1334 gcov_position_t base = gcov_position ();
1336 if (tag == GCOV_TAG_FUNCTION)
1338 char *function_name;
1339 unsigned ident, lineno;
1340 unsigned lineno_checksum, cfg_checksum;
1342 ident = gcov_read_unsigned ();
1343 lineno_checksum = gcov_read_unsigned ();
1344 cfg_checksum = gcov_read_unsigned ();
1345 function_name = xstrdup (gcov_read_string ());
1346 src_idx = find_source (gcov_read_string ());
1347 lineno = gcov_read_unsigned ();
1349 fn = XCNEW (function_t);
1350 fn->name = function_name;
1351 if (flag_demangled_names)
1353 fn->demangled_name = cplus_demangle (fn->name, DMGL_PARAMS);
1354 if (!fn->demangled_name)
1355 fn->demangled_name = fn->name;
1357 fn->ident = ident;
1358 fn->lineno_checksum = lineno_checksum;
1359 fn->cfg_checksum = cfg_checksum;
1360 fn->src = src_idx;
1361 fn->line = lineno;
1363 fn->next_file_fn = NULL;
1364 fn->next = NULL;
1365 *fns_end = fn;
1366 fns_end = &fn->next;
1367 current_tag = tag;
1369 else if (fn && tag == GCOV_TAG_BLOCKS)
1371 if (fn->blocks)
1372 fnotice (stderr, "%s:already seen blocks for '%s'\n",
1373 bbg_file_name, fn->name);
1374 else
1376 fn->num_blocks = gcov_read_unsigned ();
1377 fn->blocks = XCNEWVEC (block_t, fn->num_blocks);
1380 else if (fn && tag == GCOV_TAG_ARCS)
1382 unsigned src = gcov_read_unsigned ();
1383 unsigned num_dests = GCOV_TAG_ARCS_NUM (length);
1384 block_t *src_blk = &fn->blocks[src];
1385 unsigned mark_catches = 0;
1386 struct arc_info *arc;
1388 if (src >= fn->num_blocks || fn->blocks[src].succ)
1389 goto corrupt;
1391 while (num_dests--)
1393 unsigned dest = gcov_read_unsigned ();
1394 unsigned flags = gcov_read_unsigned ();
1396 if (dest >= fn->num_blocks)
1397 goto corrupt;
1398 arc = XCNEW (arc_t);
1400 arc->dst = &fn->blocks[dest];
1401 arc->src = src_blk;
1403 arc->count = 0;
1404 arc->count_valid = 0;
1405 arc->on_tree = !!(flags & GCOV_ARC_ON_TREE);
1406 arc->fake = !!(flags & GCOV_ARC_FAKE);
1407 arc->fall_through = !!(flags & GCOV_ARC_FALLTHROUGH);
1409 arc->succ_next = src_blk->succ;
1410 src_blk->succ = arc;
1411 src_blk->num_succ++;
1413 arc->pred_next = fn->blocks[dest].pred;
1414 fn->blocks[dest].pred = arc;
1415 fn->blocks[dest].num_pred++;
1417 if (arc->fake)
1419 if (src)
1421 /* Exceptional exit from this function, the
1422 source block must be a call. */
1423 fn->blocks[src].is_call_site = 1;
1424 arc->is_call_non_return = 1;
1425 mark_catches = 1;
1427 else
1429 /* Non-local return from a callee of this
1430 function. The destination block is a setjmp. */
1431 arc->is_nonlocal_return = 1;
1432 fn->blocks[dest].is_nonlocal_return = 1;
1436 if (!arc->on_tree)
1437 fn->num_counts++;
1440 if (mark_catches)
1442 /* We have a fake exit from this block. The other
1443 non-fall through exits must be to catch handlers.
1444 Mark them as catch arcs. */
1446 for (arc = src_blk->succ; arc; arc = arc->succ_next)
1447 if (!arc->fake && !arc->fall_through)
1449 arc->is_throw = 1;
1450 fn->has_catch = 1;
1454 else if (fn && tag == GCOV_TAG_LINES)
1456 unsigned blockno = gcov_read_unsigned ();
1457 unsigned *line_nos = XCNEWVEC (unsigned, length - 1);
1459 if (blockno >= fn->num_blocks || fn->blocks[blockno].u.line.encoding)
1460 goto corrupt;
1462 for (ix = 0; ; )
1464 unsigned lineno = gcov_read_unsigned ();
1466 if (lineno)
1468 if (!ix)
1470 line_nos[ix++] = 0;
1471 line_nos[ix++] = src_idx;
1473 line_nos[ix++] = lineno;
1475 else
1477 const char *file_name = gcov_read_string ();
1479 if (!file_name)
1480 break;
1481 src_idx = find_source (file_name);
1482 line_nos[ix++] = 0;
1483 line_nos[ix++] = src_idx;
1487 fn->blocks[blockno].u.line.encoding = line_nos;
1488 fn->blocks[blockno].u.line.num = ix;
1490 else if (current_tag && !GCOV_TAG_IS_SUBTAG (current_tag, tag))
1492 fn = NULL;
1493 current_tag = 0;
1495 gcov_sync (base, length);
1496 if (gcov_is_error ())
1498 corrupt:;
1499 fnotice (stderr, "%s:corrupted\n", bbg_file_name);
1500 break;
1503 gcov_close ();
1505 if (!fns)
1506 fnotice (stderr, "%s:no functions found\n", bbg_file_name);
1508 return fns;
1511 /* Reads profiles from the count file and attach to each
1512 function. Return nonzero if fatal error. */
1514 static int
1515 read_count_file (function_t *fns)
1517 unsigned ix;
1518 unsigned version;
1519 unsigned tag;
1520 function_t *fn = NULL;
1521 int error = 0;
1523 if (!gcov_open (da_file_name, 1))
1525 fnotice (stderr, "%s:cannot open data file, assuming not executed\n",
1526 da_file_name);
1527 no_data_file = 1;
1528 return 0;
1530 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
1532 fnotice (stderr, "%s:not a gcov data file\n", da_file_name);
1533 cleanup:;
1534 gcov_close ();
1535 return 1;
1537 version = gcov_read_unsigned ();
1538 if (version != GCOV_VERSION)
1540 char v[4], e[4];
1542 GCOV_UNSIGNED2STRING (v, version);
1543 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
1545 fnotice (stderr, "%s:version '%.4s', prefer version '%.4s'\n",
1546 da_file_name, v, e);
1548 tag = gcov_read_unsigned ();
1549 if (tag != bbg_stamp)
1551 fnotice (stderr, "%s:stamp mismatch with notes file\n", da_file_name);
1552 goto cleanup;
1555 while ((tag = gcov_read_unsigned ()))
1557 unsigned length = gcov_read_unsigned ();
1558 unsigned long base = gcov_position ();
1560 if (tag == GCOV_TAG_PROGRAM_SUMMARY)
1562 struct gcov_summary summary;
1563 gcov_read_summary (&summary);
1564 object_runs += summary.ctrs[GCOV_COUNTER_ARCS].runs;
1565 program_count++;
1567 else if (tag == GCOV_TAG_FUNCTION && !length)
1568 ; /* placeholder */
1569 else if (tag == GCOV_TAG_FUNCTION && length == GCOV_TAG_FUNCTION_LENGTH)
1571 unsigned ident;
1572 struct function_info *fn_n;
1574 /* Try to find the function in the list. To speed up the
1575 search, first start from the last function found. */
1576 ident = gcov_read_unsigned ();
1577 fn_n = fns;
1578 for (fn = fn ? fn->next : NULL; ; fn = fn->next)
1580 if (fn)
1582 else if ((fn = fn_n))
1583 fn_n = NULL;
1584 else
1586 fnotice (stderr, "%s:unknown function '%u'\n",
1587 da_file_name, ident);
1588 break;
1590 if (fn->ident == ident)
1591 break;
1594 if (!fn)
1596 else if (gcov_read_unsigned () != fn->lineno_checksum
1597 || gcov_read_unsigned () != fn->cfg_checksum)
1599 mismatch:;
1600 fnotice (stderr, "%s:profile mismatch for '%s'\n",
1601 da_file_name, fn->name);
1602 goto cleanup;
1605 else if (tag == GCOV_TAG_FOR_COUNTER (GCOV_COUNTER_ARCS) && fn)
1607 if (length != GCOV_TAG_COUNTER_LENGTH (fn->num_counts))
1608 goto mismatch;
1610 if (!fn->counts)
1611 fn->counts = XCNEWVEC (gcov_type, fn->num_counts);
1613 for (ix = 0; ix != fn->num_counts; ix++)
1614 fn->counts[ix] += gcov_read_counter ();
1616 gcov_sync (base, length);
1617 if ((error = gcov_is_error ()))
1619 fnotice (stderr,
1620 error < 0
1621 ? N_("%s:overflowed\n")
1622 : N_("%s:corrupted\n"),
1623 da_file_name);
1624 goto cleanup;
1628 gcov_close ();
1629 return 0;
1632 /* Solve the flow graph. Propagate counts from the instrumented arcs
1633 to the blocks and the uninstrumented arcs. */
1635 static void
1636 solve_flow_graph (function_t *fn)
1638 unsigned ix;
1639 arc_t *arc;
1640 gcov_type *count_ptr = fn->counts;
1641 block_t *blk;
1642 block_t *valid_blocks = NULL; /* valid, but unpropagated blocks. */
1643 block_t *invalid_blocks = NULL; /* invalid, but inferable blocks. */
1645 /* The arcs were built in reverse order. Fix that now. */
1646 for (ix = fn->num_blocks; ix--;)
1648 arc_t *arc_p, *arc_n;
1650 for (arc_p = NULL, arc = fn->blocks[ix].succ; arc;
1651 arc_p = arc, arc = arc_n)
1653 arc_n = arc->succ_next;
1654 arc->succ_next = arc_p;
1656 fn->blocks[ix].succ = arc_p;
1658 for (arc_p = NULL, arc = fn->blocks[ix].pred; arc;
1659 arc_p = arc, arc = arc_n)
1661 arc_n = arc->pred_next;
1662 arc->pred_next = arc_p;
1664 fn->blocks[ix].pred = arc_p;
1667 if (fn->num_blocks < 2)
1668 fnotice (stderr, "%s:'%s' lacks entry and/or exit blocks\n",
1669 bbg_file_name, fn->name);
1670 else
1672 if (fn->blocks[ENTRY_BLOCK].num_pred)
1673 fnotice (stderr, "%s:'%s' has arcs to entry block\n",
1674 bbg_file_name, fn->name);
1675 else
1676 /* We can't deduce the entry block counts from the lack of
1677 predecessors. */
1678 fn->blocks[ENTRY_BLOCK].num_pred = ~(unsigned)0;
1680 if (fn->blocks[EXIT_BLOCK].num_succ)
1681 fnotice (stderr, "%s:'%s' has arcs from exit block\n",
1682 bbg_file_name, fn->name);
1683 else
1684 /* Likewise, we can't deduce exit block counts from the lack
1685 of its successors. */
1686 fn->blocks[EXIT_BLOCK].num_succ = ~(unsigned)0;
1689 /* Propagate the measured counts, this must be done in the same
1690 order as the code in profile.c */
1691 for (ix = 0, blk = fn->blocks; ix != fn->num_blocks; ix++, blk++)
1693 block_t const *prev_dst = NULL;
1694 int out_of_order = 0;
1695 int non_fake_succ = 0;
1697 for (arc = blk->succ; arc; arc = arc->succ_next)
1699 if (!arc->fake)
1700 non_fake_succ++;
1702 if (!arc->on_tree)
1704 if (count_ptr)
1705 arc->count = *count_ptr++;
1706 arc->count_valid = 1;
1707 blk->num_succ--;
1708 arc->dst->num_pred--;
1710 if (prev_dst && prev_dst > arc->dst)
1711 out_of_order = 1;
1712 prev_dst = arc->dst;
1714 if (non_fake_succ == 1)
1716 /* If there is only one non-fake exit, it is an
1717 unconditional branch. */
1718 for (arc = blk->succ; arc; arc = arc->succ_next)
1719 if (!arc->fake)
1721 arc->is_unconditional = 1;
1722 /* If this block is instrumenting a call, it might be
1723 an artificial block. It is not artificial if it has
1724 a non-fallthrough exit, or the destination of this
1725 arc has more than one entry. Mark the destination
1726 block as a return site, if none of those conditions
1727 hold. */
1728 if (blk->is_call_site && arc->fall_through
1729 && arc->dst->pred == arc && !arc->pred_next)
1730 arc->dst->is_call_return = 1;
1734 /* Sort the successor arcs into ascending dst order. profile.c
1735 normally produces arcs in the right order, but sometimes with
1736 one or two out of order. We're not using a particularly
1737 smart sort. */
1738 if (out_of_order)
1740 arc_t *start = blk->succ;
1741 unsigned changes = 1;
1743 while (changes)
1745 arc_t *arc, *arc_p, *arc_n;
1747 changes = 0;
1748 for (arc_p = NULL, arc = start; (arc_n = arc->succ_next);)
1750 if (arc->dst > arc_n->dst)
1752 changes = 1;
1753 if (arc_p)
1754 arc_p->succ_next = arc_n;
1755 else
1756 start = arc_n;
1757 arc->succ_next = arc_n->succ_next;
1758 arc_n->succ_next = arc;
1759 arc_p = arc_n;
1761 else
1763 arc_p = arc;
1764 arc = arc_n;
1768 blk->succ = start;
1771 /* Place it on the invalid chain, it will be ignored if that's
1772 wrong. */
1773 blk->invalid_chain = 1;
1774 blk->chain = invalid_blocks;
1775 invalid_blocks = blk;
1778 while (invalid_blocks || valid_blocks)
1780 while ((blk = invalid_blocks))
1782 gcov_type total = 0;
1783 const arc_t *arc;
1785 invalid_blocks = blk->chain;
1786 blk->invalid_chain = 0;
1787 if (!blk->num_succ)
1788 for (arc = blk->succ; arc; arc = arc->succ_next)
1789 total += arc->count;
1790 else if (!blk->num_pred)
1791 for (arc = blk->pred; arc; arc = arc->pred_next)
1792 total += arc->count;
1793 else
1794 continue;
1796 blk->count = total;
1797 blk->count_valid = 1;
1798 blk->chain = valid_blocks;
1799 blk->valid_chain = 1;
1800 valid_blocks = blk;
1802 while ((blk = valid_blocks))
1804 gcov_type total;
1805 arc_t *arc, *inv_arc;
1807 valid_blocks = blk->chain;
1808 blk->valid_chain = 0;
1809 if (blk->num_succ == 1)
1811 block_t *dst;
1813 total = blk->count;
1814 inv_arc = NULL;
1815 for (arc = blk->succ; arc; arc = arc->succ_next)
1817 total -= arc->count;
1818 if (!arc->count_valid)
1819 inv_arc = arc;
1821 dst = inv_arc->dst;
1822 inv_arc->count_valid = 1;
1823 inv_arc->count = total;
1824 blk->num_succ--;
1825 dst->num_pred--;
1826 if (dst->count_valid)
1828 if (dst->num_pred == 1 && !dst->valid_chain)
1830 dst->chain = valid_blocks;
1831 dst->valid_chain = 1;
1832 valid_blocks = dst;
1835 else
1837 if (!dst->num_pred && !dst->invalid_chain)
1839 dst->chain = invalid_blocks;
1840 dst->invalid_chain = 1;
1841 invalid_blocks = dst;
1845 if (blk->num_pred == 1)
1847 block_t *src;
1849 total = blk->count;
1850 inv_arc = NULL;
1851 for (arc = blk->pred; arc; arc = arc->pred_next)
1853 total -= arc->count;
1854 if (!arc->count_valid)
1855 inv_arc = arc;
1857 src = inv_arc->src;
1858 inv_arc->count_valid = 1;
1859 inv_arc->count = total;
1860 blk->num_pred--;
1861 src->num_succ--;
1862 if (src->count_valid)
1864 if (src->num_succ == 1 && !src->valid_chain)
1866 src->chain = valid_blocks;
1867 src->valid_chain = 1;
1868 valid_blocks = src;
1871 else
1873 if (!src->num_succ && !src->invalid_chain)
1875 src->chain = invalid_blocks;
1876 src->invalid_chain = 1;
1877 invalid_blocks = src;
1884 /* If the graph has been correctly solved, every block will have a
1885 valid count. */
1886 for (ix = 0; ix < fn->num_blocks; ix++)
1887 if (!fn->blocks[ix].count_valid)
1889 fnotice (stderr, "%s:graph is unsolvable for '%s'\n",
1890 bbg_file_name, fn->name);
1891 break;
1895 /* Mark all the blocks only reachable via an incoming catch. */
1897 static void
1898 find_exception_blocks (function_t *fn)
1900 unsigned ix;
1901 block_t **queue = XALLOCAVEC (block_t *, fn->num_blocks);
1903 /* First mark all blocks as exceptional. */
1904 for (ix = fn->num_blocks; ix--;)
1905 fn->blocks[ix].exceptional = 1;
1907 /* Now mark all the blocks reachable via non-fake edges */
1908 queue[0] = fn->blocks;
1909 queue[0]->exceptional = 0;
1910 for (ix = 1; ix;)
1912 block_t *block = queue[--ix];
1913 const arc_t *arc;
1915 for (arc = block->succ; arc; arc = arc->succ_next)
1916 if (!arc->fake && !arc->is_throw && arc->dst->exceptional)
1918 arc->dst->exceptional = 0;
1919 queue[ix++] = arc->dst;
1925 /* Increment totals in COVERAGE according to arc ARC. */
1927 static void
1928 add_branch_counts (coverage_t *coverage, const arc_t *arc)
1930 if (arc->is_call_non_return)
1932 coverage->calls++;
1933 if (arc->src->count)
1934 coverage->calls_executed++;
1936 else if (!arc->is_unconditional)
1938 coverage->branches++;
1939 if (arc->src->count)
1940 coverage->branches_executed++;
1941 if (arc->count)
1942 coverage->branches_taken++;
1946 /* Format a GCOV_TYPE integer as either a percent ratio, or absolute
1947 count. If dp >= 0, format TOP/BOTTOM * 100 to DP decimal places.
1948 If DP is zero, no decimal point is printed. Only print 100% when
1949 TOP==BOTTOM and only print 0% when TOP=0. If dp < 0, then simply
1950 format TOP. Return pointer to a static string. */
1952 static char const *
1953 format_gcov (gcov_type top, gcov_type bottom, int dp)
1955 static char buffer[20];
1957 if (dp >= 0)
1959 float ratio = bottom ? (float)top / bottom : 0;
1960 int ix;
1961 unsigned limit = 100;
1962 unsigned percent;
1964 for (ix = dp; ix--; )
1965 limit *= 10;
1967 percent = (unsigned) (ratio * limit + (float)0.5);
1968 if (percent <= 0 && top)
1969 percent = 1;
1970 else if (percent >= limit && top != bottom)
1971 percent = limit - 1;
1972 ix = sprintf (buffer, "%.*u%%", dp + 1, percent);
1973 if (dp)
1975 dp++;
1978 buffer[ix+1] = buffer[ix];
1979 ix--;
1981 while (dp--);
1982 buffer[ix + 1] = '.';
1985 else
1986 sprintf (buffer, "%" PRId64, (int64_t)top);
1988 return buffer;
1991 /* Summary of execution */
1993 static void
1994 executed_summary (unsigned lines, unsigned executed)
1996 if (lines)
1997 fnotice (stdout, "Lines executed:%s of %d\n",
1998 format_gcov (executed, lines, 2), lines);
1999 else
2000 fnotice (stdout, "No executable lines\n");
2003 /* Output summary info for a function or file. */
2005 static void
2006 function_summary (const coverage_t *coverage, const char *title)
2008 fnotice (stdout, "%s '%s'\n", title, coverage->name);
2009 executed_summary (coverage->lines, coverage->lines_executed);
2011 if (flag_branches)
2013 if (coverage->branches)
2015 fnotice (stdout, "Branches executed:%s of %d\n",
2016 format_gcov (coverage->branches_executed,
2017 coverage->branches, 2),
2018 coverage->branches);
2019 fnotice (stdout, "Taken at least once:%s of %d\n",
2020 format_gcov (coverage->branches_taken,
2021 coverage->branches, 2),
2022 coverage->branches);
2024 else
2025 fnotice (stdout, "No branches\n");
2026 if (coverage->calls)
2027 fnotice (stdout, "Calls executed:%s of %d\n",
2028 format_gcov (coverage->calls_executed, coverage->calls, 2),
2029 coverage->calls);
2030 else
2031 fnotice (stdout, "No calls\n");
2035 /* Canonicalize the filename NAME by canonicalizing directory
2036 separators, eliding . components and resolving .. components
2037 appropriately. Always returns a unique string. */
2039 static char *
2040 canonicalize_name (const char *name)
2042 /* The canonical name cannot be longer than the incoming name. */
2043 char *result = XNEWVEC (char, strlen (name) + 1);
2044 const char *base = name, *probe;
2045 char *ptr = result;
2046 char *dd_base;
2047 int slash = 0;
2049 #if HAVE_DOS_BASED_FILE_SYSTEM
2050 if (base[0] && base[1] == ':')
2052 result[0] = base[0];
2053 result[1] = ':';
2054 base += 2;
2055 ptr += 2;
2057 #endif
2058 for (dd_base = ptr; *base; base = probe)
2060 size_t len;
2062 for (probe = base; *probe; probe++)
2063 if (IS_DIR_SEPARATOR (*probe))
2064 break;
2066 len = probe - base;
2067 if (len == 1 && base[0] == '.')
2068 /* Elide a '.' directory */
2070 else if (len == 2 && base[0] == '.' && base[1] == '.')
2072 /* '..', we can only elide it and the previous directory, if
2073 we're not a symlink. */
2074 struct stat ATTRIBUTE_UNUSED buf;
2076 *ptr = 0;
2077 if (dd_base == ptr
2078 #if defined (S_ISLNK)
2079 /* S_ISLNK is not POSIX.1-1996. */
2080 || stat (result, &buf) || S_ISLNK (buf.st_mode)
2081 #endif
2084 /* Cannot elide, or unreadable or a symlink. */
2085 dd_base = ptr + 2 + slash;
2086 goto regular;
2088 while (ptr != dd_base && *ptr != '/')
2089 ptr--;
2090 slash = ptr != result;
2092 else
2094 regular:
2095 /* Regular pathname component. */
2096 if (slash)
2097 *ptr++ = '/';
2098 memcpy (ptr, base, len);
2099 ptr += len;
2100 slash = 1;
2103 for (; IS_DIR_SEPARATOR (*probe); probe++)
2104 continue;
2106 *ptr = 0;
2108 return result;
2111 /* Print hex representation of 16 bytes from SUM and write it to BUFFER. */
2113 static void
2114 md5sum_to_hex (const char *sum, char *buffer)
2116 for (unsigned i = 0; i < 16; i++)
2117 sprintf (buffer + (2 * i), "%02x", (unsigned char)sum[i]);
2120 /* Generate an output file name. INPUT_NAME is the canonicalized main
2121 input file and SRC_NAME is the canonicalized file name.
2122 LONG_OUTPUT_NAMES and PRESERVE_PATHS affect name generation. With
2123 long_output_names we prepend the processed name of the input file
2124 to each output name (except when the current source file is the
2125 input file, so you don't get a double concatenation). The two
2126 components are separated by '##'. With preserve_paths we create a
2127 filename from all path components of the source file, replacing '/'
2128 with '#', and .. with '^', without it we simply take the basename
2129 component. (Remember, the canonicalized name will already have
2130 elided '.' components and converted \\ separators.) */
2132 static char *
2133 make_gcov_file_name (const char *input_name, const char *src_name)
2135 char *ptr;
2136 char *result;
2138 if (flag_long_names && input_name && strcmp (src_name, input_name))
2140 /* Generate the input filename part. */
2141 result = XNEWVEC (char, strlen (input_name) + strlen (src_name) + 10);
2143 ptr = result;
2144 ptr = mangle_name (input_name, ptr);
2145 ptr[0] = ptr[1] = '#';
2146 ptr += 2;
2148 else
2150 result = XNEWVEC (char, strlen (src_name) + 10);
2151 ptr = result;
2154 ptr = mangle_name (src_name, ptr);
2155 strcpy (ptr, ".gcov");
2157 /* When hashing filenames, we shorten them by only using the filename
2158 component and appending a hash of the full (mangled) pathname. */
2159 if (flag_hash_filenames)
2161 md5_ctx ctx;
2162 char md5sum[16];
2163 char md5sum_hex[33];
2165 md5_init_ctx (&ctx);
2166 md5_process_bytes (src_name, strlen (src_name), &ctx);
2167 md5_finish_ctx (&ctx, md5sum);
2168 md5sum_to_hex (md5sum, md5sum_hex);
2169 free (result);
2171 result = XNEWVEC (char, strlen (src_name) + 50);
2172 ptr = result;
2173 ptr = mangle_name (src_name, ptr);
2174 ptr[0] = ptr[1] = '#';
2175 ptr += 2;
2176 memcpy (ptr, md5sum_hex, 32);
2177 ptr += 32;
2178 strcpy (ptr, ".gcov");
2181 return result;
2184 static char *
2185 mangle_name (char const *base, char *ptr)
2187 size_t len;
2189 /* Generate the source filename part. */
2190 if (!flag_preserve_paths)
2192 base = lbasename (base);
2193 len = strlen (base);
2194 memcpy (ptr, base, len);
2195 ptr += len;
2197 else
2199 /* Convert '/' to '#', convert '..' to '^',
2200 convert ':' to '~' on DOS based file system. */
2201 const char *probe;
2203 #if HAVE_DOS_BASED_FILE_SYSTEM
2204 if (base[0] && base[1] == ':')
2206 ptr[0] = base[0];
2207 ptr[1] = '~';
2208 ptr += 2;
2209 base += 2;
2211 #endif
2212 for (; *base; base = probe)
2214 size_t len;
2216 for (probe = base; *probe; probe++)
2217 if (*probe == '/')
2218 break;
2219 len = probe - base;
2220 if (len == 2 && base[0] == '.' && base[1] == '.')
2221 *ptr++ = '^';
2222 else
2224 memcpy (ptr, base, len);
2225 ptr += len;
2227 if (*probe)
2229 *ptr++ = '#';
2230 probe++;
2235 return ptr;
2238 /* Scan through the bb_data for each line in the block, increment
2239 the line number execution count indicated by the execution count of
2240 the appropriate basic block. */
2242 static void
2243 add_line_counts (coverage_t *coverage, function_t *fn)
2245 unsigned ix;
2246 line_t *line = NULL; /* This is propagated from one iteration to the
2247 next. */
2249 /* Scan each basic block. */
2250 for (ix = 0; ix != fn->num_blocks; ix++)
2252 block_t *block = &fn->blocks[ix];
2253 unsigned *encoding;
2254 const source_t *src = NULL;
2255 unsigned jx;
2257 if (block->count && ix && ix + 1 != fn->num_blocks)
2258 fn->blocks_executed++;
2259 for (jx = 0, encoding = block->u.line.encoding;
2260 jx != block->u.line.num; jx++, encoding++)
2261 if (!*encoding)
2263 src = &sources[*++encoding];
2264 jx++;
2266 else
2268 line = &src->lines[*encoding];
2270 if (coverage)
2272 if (!line->exists)
2273 coverage->lines++;
2274 if (!line->count && block->count)
2275 coverage->lines_executed++;
2277 line->exists = 1;
2278 if (!block->exceptional)
2279 line->unexceptional = 1;
2280 line->count += block->count;
2282 free (block->u.line.encoding);
2283 block->u.cycle.arc = NULL;
2284 block->u.cycle.ident = ~0U;
2286 if (!ix || ix + 1 == fn->num_blocks)
2287 /* Entry or exit block */;
2288 else if (flag_all_blocks)
2290 line_t *block_line = line;
2292 if (!block_line)
2293 block_line = &sources[fn->src].lines[fn->line];
2295 block->chain = block_line->u.blocks;
2296 block_line->u.blocks = block;
2298 else if (flag_branches)
2300 arc_t *arc;
2302 for (arc = block->succ; arc; arc = arc->succ_next)
2304 arc->line_next = line->u.branches;
2305 line->u.branches = arc;
2306 if (coverage && !arc->is_unconditional)
2307 add_branch_counts (coverage, arc);
2311 if (!line)
2312 fnotice (stderr, "%s:no lines for '%s'\n", bbg_file_name, fn->name);
2315 /* Accumulate the line counts of a file. */
2317 static void
2318 accumulate_line_counts (source_t *src)
2320 line_t *line;
2321 function_t *fn, *fn_p, *fn_n;
2322 unsigned ix;
2324 /* Reverse the function order. */
2325 for (fn = src->functions, fn_p = NULL; fn; fn_p = fn, fn = fn_n)
2327 fn_n = fn->next_file_fn;
2328 fn->next_file_fn = fn_p;
2330 src->functions = fn_p;
2332 for (ix = src->num_lines, line = src->lines; ix--; line++)
2334 if (!flag_all_blocks)
2336 arc_t *arc, *arc_p, *arc_n;
2338 /* Total and reverse the branch information. */
2339 for (arc = line->u.branches, arc_p = NULL; arc;
2340 arc_p = arc, arc = arc_n)
2342 arc_n = arc->line_next;
2343 arc->line_next = arc_p;
2345 add_branch_counts (&src->coverage, arc);
2347 line->u.branches = arc_p;
2349 else if (line->u.blocks)
2351 /* The user expects the line count to be the number of times
2352 a line has been executed. Simply summing the block count
2353 will give an artificially high number. The Right Thing
2354 is to sum the entry counts to the graph of blocks on this
2355 line, then find the elementary cycles of the local graph
2356 and add the transition counts of those cycles. */
2357 block_t *block, *block_p, *block_n;
2358 gcov_type count = 0;
2360 /* Reverse the block information. */
2361 for (block = line->u.blocks, block_p = NULL; block;
2362 block_p = block, block = block_n)
2364 block_n = block->chain;
2365 block->chain = block_p;
2366 block->u.cycle.ident = ix;
2368 line->u.blocks = block_p;
2370 /* Sum the entry arcs. */
2371 for (block = line->u.blocks; block; block = block->chain)
2373 arc_t *arc;
2375 for (arc = block->pred; arc; arc = arc->pred_next)
2376 if (flag_branches)
2377 add_branch_counts (&src->coverage, arc);
2380 /* Cycle detection. */
2381 for (block = line->u.blocks; block; block = block->chain)
2383 for (arc_t *arc = block->pred; arc; arc = arc->pred_next)
2384 if (!line->has_block (arc->src))
2385 count += arc->count;
2386 for (arc_t *arc = block->succ; arc; arc = arc->succ_next)
2387 arc->cs_count = arc->count;
2390 /* Now, add the count of loops entirely on this line. */
2391 count += get_cycles_count (*line);
2392 line->count = count;
2395 if (line->exists)
2397 src->coverage.lines++;
2398 if (line->count)
2399 src->coverage.lines_executed++;
2404 /* Output information about ARC number IX. Returns nonzero if
2405 anything is output. */
2407 static int
2408 output_branch_count (FILE *gcov_file, int ix, const arc_t *arc)
2410 if (arc->is_call_non_return)
2412 if (arc->src->count)
2414 fnotice (gcov_file, "call %2d returned %s\n", ix,
2415 format_gcov (arc->src->count - arc->count,
2416 arc->src->count, -flag_counts));
2418 else
2419 fnotice (gcov_file, "call %2d never executed\n", ix);
2421 else if (!arc->is_unconditional)
2423 if (arc->src->count)
2424 fnotice (gcov_file, "branch %2d taken %s%s\n", ix,
2425 format_gcov (arc->count, arc->src->count, -flag_counts),
2426 arc->fall_through ? " (fallthrough)"
2427 : arc->is_throw ? " (throw)" : "");
2428 else
2429 fnotice (gcov_file, "branch %2d never executed\n", ix);
2431 else if (flag_unconditional && !arc->dst->is_call_return)
2433 if (arc->src->count)
2434 fnotice (gcov_file, "unconditional %2d taken %s\n", ix,
2435 format_gcov (arc->count, arc->src->count, -flag_counts));
2436 else
2437 fnotice (gcov_file, "unconditional %2d never executed\n", ix);
2439 else
2440 return 0;
2441 return 1;
2444 static const char *
2445 read_line (FILE *file)
2447 static char *string;
2448 static size_t string_len;
2449 size_t pos = 0;
2450 char *ptr;
2452 if (!string_len)
2454 string_len = 200;
2455 string = XNEWVEC (char, string_len);
2458 while ((ptr = fgets (string + pos, string_len - pos, file)))
2460 size_t len = strlen (string + pos);
2462 if (len && string[pos + len - 1] == '\n')
2464 string[pos + len - 1] = 0;
2465 return string;
2467 pos += len;
2468 /* If the file contains NUL characters or an incomplete
2469 last line, which can happen more than once in one run,
2470 we have to avoid doubling the STRING_LEN unnecessarily. */
2471 if (pos > string_len / 2)
2473 string_len *= 2;
2474 string = XRESIZEVEC (char, string, string_len);
2478 return pos ? string : NULL;
2481 /* Read in the source file one line at a time, and output that line to
2482 the gcov file preceded by its execution count and other
2483 information. */
2485 static void
2486 output_lines (FILE *gcov_file, const source_t *src)
2488 FILE *source_file;
2489 unsigned line_num; /* current line number. */
2490 const line_t *line; /* current line info ptr. */
2491 const char *retval = ""; /* status of source file reading. */
2492 function_t *fn = NULL;
2494 fprintf (gcov_file, "%9s:%5d:Source:%s\n", "-", 0, src->coverage.name);
2495 if (!multiple_files)
2497 fprintf (gcov_file, "%9s:%5d:Graph:%s\n", "-", 0, bbg_file_name);
2498 fprintf (gcov_file, "%9s:%5d:Data:%s\n", "-", 0,
2499 no_data_file ? "-" : da_file_name);
2500 fprintf (gcov_file, "%9s:%5d:Runs:%u\n", "-", 0, object_runs);
2502 fprintf (gcov_file, "%9s:%5d:Programs:%u\n", "-", 0, program_count);
2504 source_file = fopen (src->name, "r");
2505 if (!source_file)
2507 fnotice (stderr, "Cannot open source file %s\n", src->name);
2508 retval = NULL;
2510 else if (src->file_time == 0)
2511 fprintf (gcov_file, "%9s:%5d:Source is newer than graph\n", "-", 0);
2513 if (flag_branches)
2514 fn = src->functions;
2516 for (line_num = 1, line = &src->lines[line_num];
2517 line_num < src->num_lines; line_num++, line++)
2519 for (; fn && fn->line == line_num; fn = fn->next_file_fn)
2521 arc_t *arc = fn->blocks[EXIT_BLOCK].pred;
2522 gcov_type return_count = fn->blocks[EXIT_BLOCK].count;
2523 gcov_type called_count = fn->blocks[ENTRY_BLOCK].count;
2525 for (; arc; arc = arc->pred_next)
2526 if (arc->fake)
2527 return_count -= arc->count;
2529 fprintf (gcov_file, "function %s", flag_demangled_names ?
2530 fn->demangled_name : fn->name);
2531 fprintf (gcov_file, " called %s",
2532 format_gcov (called_count, 0, -1));
2533 fprintf (gcov_file, " returned %s",
2534 format_gcov (return_count, called_count, 0));
2535 fprintf (gcov_file, " blocks executed %s",
2536 format_gcov (fn->blocks_executed, fn->num_blocks - 2, 0));
2537 fprintf (gcov_file, "\n");
2540 if (retval)
2541 retval = read_line (source_file);
2543 /* For lines which don't exist in the .bb file, print '-' before
2544 the source line. For lines which exist but were never
2545 executed, print '#####' or '=====' before the source line.
2546 Otherwise, print the execution count before the source line.
2547 There are 16 spaces of indentation added before the source
2548 line so that tabs won't be messed up. */
2549 fprintf (gcov_file, "%9s:%5u:%s\n",
2550 !line->exists ? "-" : line->count
2551 ? format_gcov (line->count, 0, -1)
2552 : line->unexceptional ? "#####" : "=====", line_num,
2553 retval ? retval : "/*EOF*/");
2555 if (flag_all_blocks)
2557 block_t *block;
2558 arc_t *arc;
2559 int ix, jx;
2561 for (ix = jx = 0, block = line->u.blocks; block;
2562 block = block->chain)
2564 if (!block->is_call_return)
2565 fprintf (gcov_file, "%9s:%5u-block %2d\n",
2566 !line->exists ? "-" : block->count
2567 ? format_gcov (block->count, 0, -1)
2568 : block->exceptional ? "%%%%%" : "$$$$$",
2569 line_num, ix++);
2570 if (flag_branches)
2571 for (arc = block->succ; arc; arc = arc->succ_next)
2572 jx += output_branch_count (gcov_file, jx, arc);
2575 else if (flag_branches)
2577 int ix;
2578 arc_t *arc;
2580 for (ix = 0, arc = line->u.branches; arc; arc = arc->line_next)
2581 ix += output_branch_count (gcov_file, ix, arc);
2585 /* Handle all remaining source lines. There may be lines after the
2586 last line of code. */
2587 if (retval)
2589 for (; (retval = read_line (source_file)); line_num++)
2590 fprintf (gcov_file, "%9s:%5u:%s\n", "-", line_num, retval);
2593 if (source_file)
2594 fclose (source_file);