1 /* Read and write coverage files, and associated functionality.
2 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998, 1999,
3 2000, 2001, 2003, 2004, 2005, 2007, 2008 Free Software Foundation,
5 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
6 based on some ideas from Dain Samples of UC Berkeley.
7 Further mangling by Bob Manson, Cygnus Support.
8 Further mangled by Nathan Sidwell, CodeSourcery
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free
14 Software Foundation; either version 3, or (at your option) any later
17 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
18 WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
31 #include "coretypes.h"
44 #include "langhooks.h"
46 #include "tree-iterator.h"
48 #include "tree-pass.h"
49 #include "diagnostic.h"
56 struct function_list *next; /* next function */
57 unsigned ident; /* function ident */
58 unsigned checksum; /* function checksum */
59 unsigned n_ctrs[GCOV_COUNTERS];/* number of counters. */
62 /* Counts information for a function. */
63 typedef struct counts_entry
72 struct gcov_ctr_summary summary;
75 struct counts_entry *chain;
79 static struct function_list *functions_head = 0;
80 static struct function_list **functions_tail = &functions_head;
81 static unsigned no_coverage = 0;
83 /* Cumulative counter information for whole program. */
84 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
85 static unsigned prg_n_ctrs[GCOV_COUNTERS]; /* Total counters allocated. */
87 /* Counter information for current function. */
88 static unsigned fn_ctr_mask; /* Mask of counters used. */
89 static unsigned fn_n_ctrs[GCOV_COUNTERS]; /* Counters allocated. */
90 static unsigned fn_b_ctrs[GCOV_COUNTERS]; /* Allocation base. */
92 /* Name of the output file for coverage output file. */
93 static char *bbg_file_name;
94 static unsigned bbg_file_opened;
95 static int bbg_function_announced;
97 /* Name of the count data file. */
98 static char *da_file_name;
100 /* Hash table of count data. */
101 static htab_t counts_hash = NULL;
103 /* Trees representing the counter table arrays. */
104 static GTY(()) tree tree_ctr_tables[GCOV_COUNTERS];
106 /* The names of the counter tables. Not used if we're
107 generating counters at tree level. */
108 static GTY(()) rtx ctr_labels[GCOV_COUNTERS];
110 /* The names of merge functions for counters. */
111 static const char *const ctr_merge_functions[GCOV_COUNTERS] = GCOV_MERGE_FUNCTIONS;
112 static const char *const ctr_names[GCOV_COUNTERS] = GCOV_COUNTER_NAMES;
114 /* Forward declarations. */
115 static hashval_t htab_counts_entry_hash (const void *);
116 static int htab_counts_entry_eq (const void *, const void *);
117 static void htab_counts_entry_del (void *);
118 static void read_counts_file (void);
119 static unsigned compute_checksum (void);
120 static unsigned coverage_checksum_string (unsigned, const char *);
121 static tree build_fn_info_type (unsigned);
122 static tree build_fn_info_value (const struct function_list *, tree);
123 static tree build_ctr_info_type (void);
124 static tree build_ctr_info_value (unsigned, tree);
125 static tree build_gcov_info (void);
126 static void create_coverage (void);
128 /* Return the type node for gcov_type. */
133 return lang_hooks.types.type_for_size (GCOV_TYPE_SIZE, false);
136 /* Return the type node for gcov_unsigned_t. */
139 get_gcov_unsigned_t (void)
141 return lang_hooks.types.type_for_size (32, true);
145 htab_counts_entry_hash (const void *of)
147 const counts_entry_t *const entry = (const counts_entry_t *) of;
149 return entry->ident * GCOV_COUNTERS + entry->ctr;
153 htab_counts_entry_eq (const void *of1, const void *of2)
155 const counts_entry_t *const entry1 = (const counts_entry_t *) of1;
156 const counts_entry_t *const entry2 = (const counts_entry_t *) of2;
158 return entry1->ident == entry2->ident && entry1->ctr == entry2->ctr;
162 htab_counts_entry_del (void *of)
164 counts_entry_t *const entry = (counts_entry_t *) of;
166 free (entry->counts);
170 /* Read in the counts file, if available. */
173 read_counts_file (void)
175 gcov_unsigned_t fn_ident = 0;
176 gcov_unsigned_t checksum = -1;
177 counts_entry_t *summaried = NULL;
178 unsigned seen_summary = 0;
182 if (!gcov_open (da_file_name, 1))
185 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC))
187 warning (0, "%qs is not a gcov data file", da_file_name);
191 else if ((tag = gcov_read_unsigned ()) != GCOV_VERSION)
195 GCOV_UNSIGNED2STRING (v, tag);
196 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
198 warning (0, "%qs is version %q.*s, expected version %q.*s",
199 da_file_name, 4, v, 4, e);
204 /* Read and discard the stamp. */
205 gcov_read_unsigned ();
207 counts_hash = htab_create (10,
208 htab_counts_entry_hash, htab_counts_entry_eq,
209 htab_counts_entry_del);
210 while ((tag = gcov_read_unsigned ()))
212 gcov_unsigned_t length;
213 gcov_position_t offset;
215 length = gcov_read_unsigned ();
216 offset = gcov_position ();
217 if (tag == GCOV_TAG_FUNCTION)
219 fn_ident = gcov_read_unsigned ();
220 checksum = gcov_read_unsigned ();
223 /* We have already seen a summary, this means that this
224 new function begins a new set of program runs. We
225 must unlink the summaried chain. */
226 counts_entry_t *entry, *chain;
228 for (entry = summaried; entry; entry = chain)
230 chain = entry->chain;
237 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
239 counts_entry_t *entry;
240 struct gcov_summary summary;
242 gcov_read_summary (&summary);
244 for (entry = summaried; entry; entry = entry->chain)
246 struct gcov_ctr_summary *csum = &summary.ctrs[entry->ctr];
248 entry->summary.runs += csum->runs;
249 entry->summary.sum_all += csum->sum_all;
250 if (entry->summary.run_max < csum->run_max)
251 entry->summary.run_max = csum->run_max;
252 entry->summary.sum_max += csum->sum_max;
255 else if (GCOV_TAG_IS_COUNTER (tag) && fn_ident)
257 counts_entry_t **slot, *entry, elt;
258 unsigned n_counts = GCOV_TAG_COUNTER_NUM (length);
261 elt.ident = fn_ident;
262 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
264 slot = (counts_entry_t **) htab_find_slot
265 (counts_hash, &elt, INSERT);
269 *slot = entry = XCNEW (counts_entry_t);
270 entry->ident = elt.ident;
271 entry->ctr = elt.ctr;
272 entry->checksum = checksum;
273 entry->summary.num = n_counts;
274 entry->counts = XCNEWVEC (gcov_type, n_counts);
276 else if (entry->checksum != checksum)
278 error ("coverage mismatch for function %u while reading execution counters",
280 error ("checksum is %x instead of %x", entry->checksum, checksum);
281 htab_delete (counts_hash);
284 else if (entry->summary.num != n_counts)
286 error ("coverage mismatch for function %u while reading execution counters",
288 error ("number of counters is %d instead of %d", entry->summary.num, n_counts);
289 htab_delete (counts_hash);
292 else if (elt.ctr >= GCOV_COUNTERS_SUMMABLE)
294 error ("cannot merge separate %s counters for function %u",
295 ctr_names[elt.ctr], fn_ident);
299 if (elt.ctr < GCOV_COUNTERS_SUMMABLE
300 /* This should always be true for a just allocated entry,
301 and always false for an existing one. Check this way, in
302 case the gcov file is corrupt. */
303 && (!entry->chain || summaried != entry))
305 entry->chain = summaried;
308 for (ix = 0; ix != n_counts; ix++)
309 entry->counts[ix] += gcov_read_counter ();
312 gcov_sync (offset, length);
313 if ((is_error = gcov_is_error ()))
315 error (is_error < 0 ? "%qs has overflowed" : "%qs is corrupted",
317 htab_delete (counts_hash);
325 /* Returns the counters for a particular tag. */
328 get_coverage_counts (unsigned counter, unsigned expected,
329 const struct gcov_ctr_summary **summary)
331 counts_entry_t *entry, elt;
332 gcov_unsigned_t checksum = -1;
334 /* No hash table, no counts. */
337 static int warned = 0;
340 inform (input_location, (flag_guess_branch_prob
341 ? "file %s not found, execution counts estimated"
342 : "file %s not found, execution counts assumed to be zero"),
347 elt.ident = current_function_funcdef_no + 1;
349 entry = (counts_entry_t *) htab_find (counts_hash, &elt);
352 warning (0, "no coverage for function %qE found",
353 DECL_ASSEMBLER_NAME (current_function_decl));
357 checksum = compute_checksum ();
358 if (entry->checksum != checksum
359 || entry->summary.num != expected)
361 static int warned = 0;
362 bool warning_printed = false;
363 tree id = DECL_ASSEMBLER_NAME (current_function_decl);
366 warning_at (input_location, OPT_Wcoverage_mismatch,
367 "coverage mismatch for function "
368 "%qE while reading counter %qs", id, ctr_names[counter]);
371 if (entry->checksum != checksum)
372 inform (input_location, "checksum is %x instead of %x",
373 entry->checksum, checksum);
375 inform (input_location, "number of counters is %d instead of %d",
376 entry->summary.num, expected);
378 if (!(errorcount || sorrycount)
381 inform (input_location, "coverage mismatch ignored");
382 inform (input_location, flag_guess_branch_prob
383 ? G_("execution counts estimated")
384 : G_("execution counts assumed to be zero"));
385 if (!flag_guess_branch_prob)
386 inform (input_location,
387 "this can result in poorly optimized code");
395 *summary = &entry->summary;
397 return entry->counts;
400 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
401 allocation succeeded. */
404 coverage_counter_alloc (unsigned counter, unsigned num)
412 if (!tree_ctr_tables[counter])
414 /* Generate and save a copy of this so it can be shared. Leave
415 the index type unspecified for now; it will be set after all
416 functions have been compiled. */
418 tree gcov_type_node = get_gcov_type ();
419 tree gcov_type_array_type
420 = build_array_type (gcov_type_node, NULL_TREE);
421 tree_ctr_tables[counter]
422 = build_decl (BUILTINS_LOCATION,
423 VAR_DECL, NULL_TREE, gcov_type_array_type);
424 TREE_STATIC (tree_ctr_tables[counter]) = 1;
425 ASM_GENERATE_INTERNAL_LABEL (buf, "LPBX", counter + 1);
426 DECL_NAME (tree_ctr_tables[counter]) = get_identifier (buf);
427 DECL_ALIGN (tree_ctr_tables[counter]) = TYPE_ALIGN (gcov_type_node);
430 fprintf (dump_file, "Using data file %s\n", da_file_name);
432 fn_b_ctrs[counter] = fn_n_ctrs[counter];
433 fn_n_ctrs[counter] += num;
434 fn_ctr_mask |= 1 << counter;
438 /* Generate a tree to access COUNTER NO. */
441 tree_coverage_counter_ref (unsigned counter, unsigned no)
443 tree gcov_type_node = get_gcov_type ();
445 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
446 no += prg_n_ctrs[counter] + fn_b_ctrs[counter];
448 /* "no" here is an array index, scaled to bytes later. */
449 return build4 (ARRAY_REF, gcov_type_node, tree_ctr_tables[counter],
450 build_int_cst (NULL_TREE, no), NULL, NULL);
453 /* Generate a tree to access the address of COUNTER NO. */
456 tree_coverage_counter_addr (unsigned counter, unsigned no)
458 tree gcov_type_node = get_gcov_type ();
460 gcc_assert (no < fn_n_ctrs[counter] - fn_b_ctrs[counter]);
461 no += prg_n_ctrs[counter] + fn_b_ctrs[counter];
463 TREE_ADDRESSABLE (tree_ctr_tables[counter]) = 1;
465 /* "no" here is an array index, scaled to bytes later. */
466 return build_fold_addr_expr (build4 (ARRAY_REF, gcov_type_node,
467 tree_ctr_tables[counter],
468 build_int_cst (NULL_TREE, no),
472 /* Generate a checksum for a string. CHKSUM is the current
476 coverage_checksum_string (unsigned chksum, const char *string)
481 /* Look for everything that looks if it were produced by
482 get_file_function_name and zero out the second part
483 that may result from flag_random_seed. This is not critical
484 as the checksums are used only for sanity checking. */
485 for (i = 0; string[i]; i++)
488 if (!strncmp (string + i, "_GLOBAL__N_", 11))
490 if (!strncmp (string + i, "_GLOBAL__", 9))
493 /* C++ namespaces do have scheme:
494 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
495 since filename might contain extra underscores there seems
496 to be no better chance then walk all possible offsets looking
500 for (i = i + offset; string[i]; i++)
505 for (y = 1; y < 9; y++)
506 if (!(string[i + y] >= '0' && string[i + y] <= '9')
507 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
509 if (y != 9 || string[i + 9] != '_')
511 for (y = 10; y < 18; y++)
512 if (!(string[i + y] >= '0' && string[i + y] <= '9')
513 && !(string[i + y] >= 'A' && string[i + y] <= 'F'))
518 string = dup = xstrdup (string);
519 for (y = 10; y < 18; y++)
526 chksum = crc32_string (chksum, string);
533 /* Compute checksum for the current function. We generate a CRC32. */
536 compute_checksum (void)
538 expanded_location xloc
539 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
540 unsigned chksum = xloc.line;
542 chksum = coverage_checksum_string (chksum, xloc.file);
543 chksum = coverage_checksum_string
544 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
549 /* Begin output to the graph file for the current function.
550 Opens the output file, if not already done. Writes the
551 function header, if not already done. Returns nonzero if data
555 coverage_begin_output (void)
557 /* We don't need to output .gcno file unless we're under -ftest-coverage
558 (e.g. -fprofile-arcs/generate/use don't need .gcno to work). */
559 if (no_coverage || !flag_test_coverage || flag_compare_debug)
562 if (!bbg_function_announced)
564 expanded_location xloc
565 = expand_location (DECL_SOURCE_LOCATION (current_function_decl));
566 unsigned long offset;
568 if (!bbg_file_opened)
570 if (!gcov_open (bbg_file_name, -1))
571 error ("cannot open %s", bbg_file_name);
574 gcov_write_unsigned (GCOV_NOTE_MAGIC);
575 gcov_write_unsigned (GCOV_VERSION);
576 gcov_write_unsigned (local_tick);
581 /* Announce function */
582 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
583 gcov_write_unsigned (current_function_funcdef_no + 1);
584 gcov_write_unsigned (compute_checksum ());
585 gcov_write_string (IDENTIFIER_POINTER
586 (DECL_ASSEMBLER_NAME (current_function_decl)));
587 gcov_write_string (xloc.file);
588 gcov_write_unsigned (xloc.line);
589 gcov_write_length (offset);
591 bbg_function_announced = 1;
593 return !gcov_is_error ();
596 /* Finish coverage data for the current function. Verify no output
597 error has occurred. Save function coverage counts. */
600 coverage_end_function (void)
604 if (bbg_file_opened > 1 && gcov_is_error ())
606 warning (0, "error writing %qs", bbg_file_name);
607 bbg_file_opened = -1;
612 struct function_list *item;
614 item = XNEW (struct function_list);
616 *functions_tail = item;
617 functions_tail = &item->next;
620 item->ident = current_function_funcdef_no + 1;
621 item->checksum = compute_checksum ();
622 for (i = 0; i != GCOV_COUNTERS; i++)
624 item->n_ctrs[i] = fn_n_ctrs[i];
625 prg_n_ctrs[i] += fn_n_ctrs[i];
626 fn_n_ctrs[i] = fn_b_ctrs[i] = 0;
628 prg_ctr_mask |= fn_ctr_mask;
631 bbg_function_announced = 0;
634 /* Creates the gcov_fn_info RECORD_TYPE. */
637 build_fn_info_type (unsigned int counters)
639 tree type = lang_hooks.types.make_type (RECORD_TYPE);
644 fields = build_decl (BUILTINS_LOCATION,
645 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
648 field = build_decl (BUILTINS_LOCATION,
649 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
650 TREE_CHAIN (field) = fields;
653 array_type = build_int_cst (NULL_TREE, counters - 1);
654 array_type = build_index_type (array_type);
655 array_type = build_array_type (get_gcov_unsigned_t (), array_type);
658 field = build_decl (BUILTINS_LOCATION,
659 FIELD_DECL, NULL_TREE, array_type);
660 TREE_CHAIN (field) = fields;
663 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
668 /* Creates a CONSTRUCTOR for a gcov_fn_info. FUNCTION is
669 the function being processed and TYPE is the gcov_fn_info
673 build_fn_info_value (const struct function_list *function, tree type)
675 tree value = NULL_TREE;
676 tree fields = TYPE_FIELDS (type);
678 tree array_value = NULL_TREE;
681 value = tree_cons (fields, build_int_cstu (get_gcov_unsigned_t (),
682 function->ident), value);
683 fields = TREE_CHAIN (fields);
686 value = tree_cons (fields, build_int_cstu (get_gcov_unsigned_t (),
687 function->checksum), value);
688 fields = TREE_CHAIN (fields);
691 for (ix = 0; ix != GCOV_COUNTERS; ix++)
692 if (prg_ctr_mask & (1 << ix))
694 tree counters = build_int_cstu (get_gcov_unsigned_t (),
695 function->n_ctrs[ix]);
697 array_value = tree_cons (NULL_TREE, counters, array_value);
700 /* FIXME: use build_constructor directly. */
701 array_value = build_constructor_from_list (TREE_TYPE (fields),
702 nreverse (array_value));
703 value = tree_cons (fields, array_value, value);
705 /* FIXME: use build_constructor directly. */
706 value = build_constructor_from_list (type, nreverse (value));
711 /* Creates the gcov_ctr_info RECORD_TYPE. */
714 build_ctr_info_type (void)
716 tree type = lang_hooks.types.make_type (RECORD_TYPE);
717 tree field, fields = NULL_TREE;
718 tree gcov_ptr_type = build_pointer_type (get_gcov_type ());
719 tree gcov_merge_fn_type;
722 field = build_decl (BUILTINS_LOCATION,
723 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
724 TREE_CHAIN (field) = fields;
728 field = build_decl (BUILTINS_LOCATION,
729 FIELD_DECL, NULL_TREE, gcov_ptr_type);
730 TREE_CHAIN (field) = fields;
735 build_function_type_list (void_type_node,
736 gcov_ptr_type, get_gcov_unsigned_t (),
738 field = build_decl (BUILTINS_LOCATION,
739 FIELD_DECL, NULL_TREE,
740 build_pointer_type (gcov_merge_fn_type));
741 TREE_CHAIN (field) = fields;
744 finish_builtin_struct (type, "__gcov_ctr_info", fields, NULL_TREE);
749 /* Creates a CONSTRUCTOR for a gcov_ctr_info. COUNTER is
750 the counter being processed and TYPE is the gcov_ctr_info
754 build_ctr_info_value (unsigned int counter, tree type)
756 tree value = NULL_TREE;
757 tree fields = TYPE_FIELDS (type);
761 value = tree_cons (fields,
762 build_int_cstu (get_gcov_unsigned_t (),
763 prg_n_ctrs[counter]),
765 fields = TREE_CHAIN (fields);
767 if (prg_n_ctrs[counter])
771 array_type = build_int_cstu (get_gcov_unsigned_t (),
772 prg_n_ctrs[counter] - 1);
773 array_type = build_index_type (array_type);
774 array_type = build_array_type (TREE_TYPE (TREE_TYPE (fields)),
777 TREE_TYPE (tree_ctr_tables[counter]) = array_type;
778 DECL_SIZE (tree_ctr_tables[counter]) = TYPE_SIZE (array_type);
779 DECL_SIZE_UNIT (tree_ctr_tables[counter]) = TYPE_SIZE_UNIT (array_type);
780 varpool_finalize_decl (tree_ctr_tables[counter]);
782 value = tree_cons (fields,
783 build1 (ADDR_EXPR, TREE_TYPE (fields),
784 tree_ctr_tables[counter]),
788 value = tree_cons (fields, null_pointer_node, value);
789 fields = TREE_CHAIN (fields);
791 fn = build_decl (BUILTINS_LOCATION,
793 get_identifier (ctr_merge_functions[counter]),
794 TREE_TYPE (TREE_TYPE (fields)));
795 DECL_EXTERNAL (fn) = 1;
796 TREE_PUBLIC (fn) = 1;
797 DECL_ARTIFICIAL (fn) = 1;
798 TREE_NOTHROW (fn) = 1;
799 DECL_ASSEMBLER_NAME (fn); /* Initialize assembler name so we can stream out. */
800 value = tree_cons (fields,
801 build1 (ADDR_EXPR, TREE_TYPE (fields), fn),
804 /* FIXME: use build_constructor directly. */
805 value = build_constructor_from_list (type, nreverse (value));
810 /* Creates the gcov_info RECORD_TYPE and initializer for it. Returns a
814 build_gcov_info (void)
816 unsigned n_ctr_types, ix;
817 tree type, const_type;
818 tree fn_info_type, fn_info_value = NULL_TREE;
819 tree fn_info_ptr_type;
820 tree ctr_info_type, ctr_info_ary_type, ctr_info_value = NULL_TREE;
821 tree field, fields = NULL_TREE;
822 tree value = NULL_TREE;
823 tree filename_string;
824 int da_file_name_len;
826 const struct function_list *fn;
829 /* Count the number of active counters. */
830 for (n_ctr_types = 0, ix = 0; ix != GCOV_COUNTERS; ix++)
831 if (prg_ctr_mask & (1 << ix))
834 type = lang_hooks.types.make_type (RECORD_TYPE);
835 const_type = build_qualified_type (type, TYPE_QUAL_CONST);
838 field = build_decl (BUILTINS_LOCATION,
839 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
840 TREE_CHAIN (field) = fields;
842 value = tree_cons (field, build_int_cstu (TREE_TYPE (field), GCOV_VERSION),
846 field = build_decl (BUILTINS_LOCATION,
847 FIELD_DECL, NULL_TREE, build_pointer_type (const_type));
848 TREE_CHAIN (field) = fields;
850 value = tree_cons (field, null_pointer_node, value);
853 field = build_decl (BUILTINS_LOCATION,
854 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
855 TREE_CHAIN (field) = fields;
857 value = tree_cons (field, build_int_cstu (TREE_TYPE (field), local_tick),
861 string_type = build_pointer_type (build_qualified_type (char_type_node,
863 field = build_decl (BUILTINS_LOCATION,
864 FIELD_DECL, NULL_TREE, string_type);
865 TREE_CHAIN (field) = fields;
867 da_file_name_len = strlen (da_file_name);
868 filename_string = build_string (da_file_name_len + 1, da_file_name);
869 TREE_TYPE (filename_string) = build_array_type
870 (char_type_node, build_index_type
871 (build_int_cst (NULL_TREE, da_file_name_len)));
872 value = tree_cons (field, build1 (ADDR_EXPR, string_type, filename_string),
875 /* Build the fn_info type and initializer. */
876 fn_info_type = build_fn_info_type (n_ctr_types);
877 fn_info_ptr_type = build_pointer_type (build_qualified_type
878 (fn_info_type, TYPE_QUAL_CONST));
879 for (fn = functions_head, n_fns = 0; fn; fn = fn->next, n_fns++)
880 fn_info_value = tree_cons (NULL_TREE,
881 build_fn_info_value (fn, fn_info_type),
887 array_type = build_index_type (build_int_cst (NULL_TREE, n_fns - 1));
888 array_type = build_array_type (fn_info_type, array_type);
890 /* FIXME: use build_constructor directly. */
891 fn_info_value = build_constructor_from_list (array_type,
892 nreverse (fn_info_value));
893 fn_info_value = build1 (ADDR_EXPR, fn_info_ptr_type, fn_info_value);
896 fn_info_value = null_pointer_node;
898 /* number of functions */
899 field = build_decl (BUILTINS_LOCATION,
900 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
901 TREE_CHAIN (field) = fields;
903 value = tree_cons (field,
904 build_int_cstu (get_gcov_unsigned_t (), n_fns),
908 field = build_decl (BUILTINS_LOCATION,
909 FIELD_DECL, NULL_TREE, fn_info_ptr_type);
910 TREE_CHAIN (field) = fields;
912 value = tree_cons (field, fn_info_value, value);
915 field = build_decl (BUILTINS_LOCATION,
916 FIELD_DECL, NULL_TREE, get_gcov_unsigned_t ());
917 TREE_CHAIN (field) = fields;
919 value = tree_cons (field,
920 build_int_cstu (get_gcov_unsigned_t (), prg_ctr_mask),
924 ctr_info_type = build_ctr_info_type ();
925 ctr_info_ary_type = build_index_type (build_int_cst (NULL_TREE,
927 ctr_info_ary_type = build_array_type (ctr_info_type, ctr_info_ary_type);
928 for (ix = 0; ix != GCOV_COUNTERS; ix++)
929 if (prg_ctr_mask & (1 << ix))
930 ctr_info_value = tree_cons (NULL_TREE,
931 build_ctr_info_value (ix, ctr_info_type),
933 /* FIXME: use build_constructor directly. */
934 ctr_info_value = build_constructor_from_list (ctr_info_ary_type,
935 nreverse (ctr_info_value));
937 field = build_decl (BUILTINS_LOCATION,
938 FIELD_DECL, NULL_TREE, ctr_info_ary_type);
939 TREE_CHAIN (field) = fields;
941 value = tree_cons (field, ctr_info_value, value);
943 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
945 /* FIXME: use build_constructor directly. */
946 value = build_constructor_from_list (type, nreverse (value));
951 /* Write out the structure which libgcov uses to locate all the
952 counters. The structures used here must match those defined in
953 gcov-io.h. Write out the constructor to call __gcov_init. */
956 create_coverage (void)
958 tree gcov_info, gcov_init, body, t;
961 no_coverage = 1; /* Disable any further coverage. */
966 t = build_gcov_info ();
968 gcov_info = build_decl (BUILTINS_LOCATION,
969 VAR_DECL, NULL_TREE, TREE_TYPE (t));
970 TREE_STATIC (gcov_info) = 1;
971 ASM_GENERATE_INTERNAL_LABEL (name_buf, "LPBX", 0);
972 DECL_NAME (gcov_info) = get_identifier (name_buf);
973 DECL_INITIAL (gcov_info) = t;
975 /* Build structure. */
976 varpool_finalize_decl (gcov_info);
978 /* Build a decl for __gcov_init. */
979 t = build_pointer_type (TREE_TYPE (gcov_info));
980 t = build_function_type_list (void_type_node, t, NULL);
981 t = build_decl (BUILTINS_LOCATION,
982 FUNCTION_DECL, get_identifier ("__gcov_init"), t);
984 DECL_EXTERNAL (t) = 1;
985 DECL_ASSEMBLER_NAME (t); /* Initialize assembler name so we can stream out. */
988 /* Generate a call to __gcov_init(&gcov_info). */
990 t = build_fold_addr_expr (gcov_info);
991 t = build_call_expr (gcov_init, 1, t);
992 append_to_statement_list (t, &body);
994 /* Generate a constructor to run it. */
995 cgraph_build_static_cdtor ('I', body, DEFAULT_INIT_PRIORITY);
998 /* Perform file-level initialization. Read in data file, generate name
1002 coverage_init (const char *filename)
1004 int len = strlen (filename);
1005 /* + 1 for extra '/', in case prefix doesn't end with /. */
1008 if (profile_data_prefix == 0 && filename[0] != '/')
1009 profile_data_prefix = getpwd ();
1011 prefix_len = (profile_data_prefix) ? strlen (profile_data_prefix) + 1 : 0;
1013 /* Name of da file. */
1014 da_file_name = XNEWVEC (char, len + strlen (GCOV_DATA_SUFFIX)
1017 if (profile_data_prefix)
1019 strcpy (da_file_name, profile_data_prefix);
1020 da_file_name[prefix_len - 1] = '/';
1021 da_file_name[prefix_len] = 0;
1024 da_file_name[0] = 0;
1025 strcat (da_file_name, filename);
1026 strcat (da_file_name, GCOV_DATA_SUFFIX);
1028 /* Name of bbg file. */
1029 bbg_file_name = XNEWVEC (char, len + strlen (GCOV_NOTE_SUFFIX) + 1);
1030 strcpy (bbg_file_name, filename);
1031 strcat (bbg_file_name, GCOV_NOTE_SUFFIX);
1033 if (flag_profile_use)
1034 read_counts_file ();
1037 /* Performs file-level cleanup. Close graph file, generate coverage
1038 variables and constructor. */
1041 coverage_finish (void)
1044 if (bbg_file_opened)
1046 int error = gcov_close ();
1049 unlink (bbg_file_name);
1051 /* Only remove the da file, if we cannot stamp it. If we can
1052 stamp it, libgcov will DTRT. */
1053 unlink (da_file_name);
1057 #include "gt-coverage.h"