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 Free Software Foundation, Inc.
4 Contributed by James E. Wilson, UC Berkeley/Cygnus Support;
5 based on some ideas from Dain Samples of UC Berkeley.
6 Further mangling by Bob Manson, Cygnus Support.
7 Further mangled by Nathan Sidwell, CodeSourcery
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
31 #include "coretypes.h"
45 #include "langhooks.h"
52 struct function_list *next; /* next function */
53 const char *name; /* function name */
54 unsigned checksum; /* function checksum */
55 unsigned n_ctrs[GCOV_COUNTERS];/* number of counters. */
58 /* Counts information for a function. */
59 typedef struct counts_entry
68 struct gcov_ctr_summary summary;
71 struct counts_entry *chain;
75 static struct function_list *functions_head = 0;
76 static struct function_list **functions_tail = &functions_head;
78 /* Cumulative counter information for whole program. */
79 static unsigned prg_ctr_mask; /* Mask of counter types generated. */
80 static unsigned prg_n_ctrs[GCOV_COUNTERS];
82 /* Counter information for current function. */
83 static unsigned fn_ctr_mask;
84 static unsigned fn_n_ctrs[GCOV_COUNTERS];
86 /* Name of the output file for coverage output file. */
87 static char *bbg_file_name;
88 static unsigned bbg_file_opened;
89 static int bbg_function_announced;
91 /* Name of the count data file. */
92 static char *da_file_name;
94 /* Hash table of count data. */
95 static htab_t counts_hash = NULL;
97 /* The names of the counter tables. */
98 static GTY(()) rtx ctr_labels[GCOV_COUNTERS];
100 /* Forward declarations. */
101 static hashval_t htab_counts_entry_hash PARAMS ((const void *));
102 static int htab_counts_entry_eq PARAMS ((const void *, const void *));
103 static void htab_counts_entry_del PARAMS ((void *));
104 static void read_counts_file PARAMS ((void));
105 static unsigned compute_checksum PARAMS ((void));
106 static unsigned checksum_string PARAMS ((unsigned, const char *));
107 static tree build_fn_info_type PARAMS ((unsigned));
108 static tree build_fn_info_value PARAMS ((const struct function_list *, tree));
109 static tree build_ctr_info_type PARAMS ((void));
110 static tree build_ctr_info_value PARAMS ((unsigned, tree));
111 static tree build_gcov_info PARAMS ((void));
112 static void create_coverage PARAMS ((void));
116 htab_counts_entry_hash (of)
119 const counts_entry_t *entry = of;
121 return htab_hash_string (entry->function_name) ^ entry->ctr;
125 htab_counts_entry_eq (of1, of2)
129 const counts_entry_t *entry1 = of1;
130 const counts_entry_t *entry2 = of2;
132 return !strcmp (entry1->function_name, entry2->function_name)
133 && entry1->ctr == entry2->ctr;
137 htab_counts_entry_del (of)
140 counts_entry_t *entry = of;
142 free (entry->function_name);
143 free (entry->counts);
147 /* Read in the counts file, if available. */
152 char *function_name_buffer = NULL;
153 unsigned version, ix, checksum = -1;
154 counts_entry_t *summaried = NULL;
155 unsigned seen_summary = 0;
157 if (!gcov_open (da_file_name, 1))
160 if (gcov_read_unsigned () != GCOV_DATA_MAGIC)
162 warning ("`%s' is not a gcov data file", da_file_name);
166 else if ((version = gcov_read_unsigned ()) != GCOV_VERSION)
169 unsigned required = GCOV_VERSION;
171 for (ix = 4; ix--; required >>= 8, version >>= 8)
176 warning ("`%s' is version `%.4s', expected version `%.4s'",
182 counts_hash = htab_create (10,
183 htab_counts_entry_hash, htab_counts_entry_eq,
184 htab_counts_entry_del);
185 while (!gcov_is_eof ())
187 unsigned tag, length;
188 unsigned long offset;
191 tag = gcov_read_unsigned ();
192 length = gcov_read_unsigned ();
193 offset = gcov_position ();
194 if (tag == GCOV_TAG_FUNCTION)
196 const char *string = gcov_read_string ();
197 free (function_name_buffer);
198 function_name_buffer = string ? xstrdup (string) : NULL;
199 checksum = gcov_read_unsigned ();
202 /* We have already seen a summary, this means that this
203 new function begins a new set of program runs. We
204 must unlink the summaried chain. */
205 counts_entry_t *entry, *chain;
207 for (entry = summaried; entry; entry = chain)
209 chain = entry->chain;
216 else if (tag == GCOV_TAG_PROGRAM_SUMMARY)
218 counts_entry_t *entry;
219 struct gcov_summary summary;
221 gcov_read_summary (&summary);
223 for (entry = summaried; entry; entry = entry->chain)
225 struct gcov_ctr_summary *csum = &summary.ctrs[entry->ctr];
227 entry->summary.runs += csum->runs;
228 entry->summary.sum_all += csum->sum_all;
229 if (entry->summary.run_max < csum->run_max)
230 entry->summary.run_max = csum->run_max;
231 entry->summary.sum_max += csum->sum_max;
234 else if (GCOV_TAG_IS_COUNTER (tag) && function_name_buffer)
236 counts_entry_t **slot, *entry, elt;
237 unsigned n_counts = length / 8;
240 elt.function_name = function_name_buffer;
241 elt.ctr = GCOV_COUNTER_FOR_TAG (tag);
243 slot = (counts_entry_t **) htab_find_slot
244 (counts_hash, &elt, INSERT);
248 *slot = entry = xcalloc (1, sizeof (counts_entry_t));
249 entry->function_name = xstrdup (elt.function_name);
250 entry->ctr = elt.ctr;
251 entry->checksum = checksum;
252 entry->summary.num = n_counts;
253 entry->counts = xcalloc (n_counts, sizeof (gcov_type));
255 else if (entry->checksum != checksum
256 || entry->summary.num != n_counts)
258 warning ("profile mismatch for `%s'", function_name_buffer);
259 htab_delete (counts_hash);
263 /* This should always be true for a just allocated entry,
264 and always false for an existing one. Check this way, in
265 case the gcov file is corrupt. */
266 if (!entry->chain || summaried != entry)
268 entry->chain = summaried;
271 for (ix = 0; ix != n_counts; ix++)
272 entry->counts[ix] += gcov_read_counter ();
274 gcov_seek (offset, length);
275 if ((error = gcov_is_error ()))
277 warning (error < 0 ? "`%s' has overflowed" : "`%s' is corrupted",
279 htab_delete (counts_hash);
284 free (function_name_buffer);
288 /* Returns the counters for a particular tag. */
291 get_coverage_counts (unsigned counter, unsigned expected,
292 const struct gcov_ctr_summary **summary)
294 counts_entry_t *entry, elt;
296 /* No hash table, no counts. */
299 static int warned = 0;
302 warning ("file %s not found, execution counts assumed to be zero",
308 = (char *) IDENTIFIER_POINTER
309 (DECL_ASSEMBLER_NAME (current_function_decl));
311 entry = htab_find (counts_hash, &elt);
314 warning ("No profile for function '%s' found.", elt.function_name);
318 if (expected != entry->summary.num
319 || compute_checksum () != entry->checksum)
321 warning ("profile mismatch for `%s'", elt.function_name);
326 *summary = &entry->summary;
328 return entry->counts;
331 /* Generate a MEM rtl to access COUNTER NO . */
334 coverage_counter_ref (unsigned counter, unsigned no)
336 enum machine_mode mode = mode_for_size (GCOV_TYPE_SIZE, MODE_INT, 0);
339 if (!ctr_labels[counter])
341 /* Generate and save a copy of this so it can be shared. */
344 ASM_GENERATE_INTERNAL_LABEL (buf, "LPBX", counter + 1);
345 ctr_labels[counter] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (buf));
347 if (no + 1 > fn_n_ctrs[counter])
349 fn_n_ctrs[counter] = no + 1;
350 fn_ctr_mask |= 1 << counter;
353 no += prg_n_ctrs[counter];
354 ref = plus_constant (ctr_labels[counter],
355 GCOV_TYPE_SIZE / BITS_PER_UNIT * no);
356 ref = gen_rtx_MEM (mode, ref);
357 set_mem_alias_set (ref, new_alias_set ());
362 /* Generate a checksum for a string. CHKSUM is the current
366 checksum_string (unsigned chksum, const char *string)
370 unsigned value = *string << 24;
373 for (ix = 8; ix--; value <<= 1)
377 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
387 /* Compute checksum for the current function. We generate a CRC32. */
392 unsigned chksum = DECL_SOURCE_LINE (current_function_decl);
394 chksum = checksum_string (chksum, DECL_SOURCE_FILE (current_function_decl));
395 chksum = checksum_string
396 (chksum, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)));
401 /* Begin output to the graph file for the current function.
402 Opens the output file, if not already done. Writes the
403 function header, if not already done. Returns non-zero if data
407 coverage_begin_output ()
409 if (!bbg_function_announced)
411 const char *file = DECL_SOURCE_FILE (current_function_decl);
412 unsigned line = DECL_SOURCE_LINE (current_function_decl);
413 unsigned long offset;
415 if (!bbg_file_opened)
417 if (!gcov_open (bbg_file_name, -1))
418 error ("cannot open %s", bbg_file_name);
421 gcov_write_unsigned (GCOV_GRAPH_MAGIC);
422 gcov_write_unsigned (GCOV_VERSION);
427 /* Announce function */
428 offset = gcov_write_tag (GCOV_TAG_FUNCTION);
429 gcov_write_string (IDENTIFIER_POINTER
430 (DECL_ASSEMBLER_NAME (current_function_decl)));
431 gcov_write_unsigned (compute_checksum ());
432 gcov_write_string (file);
433 gcov_write_unsigned (line);
434 gcov_write_length (offset);
436 bbg_function_announced = 1;
438 return !gcov_is_error ();
441 /* Finish coverage data for the current function. Verify no output
442 error has occurred. Save function coverage counts. */
445 coverage_end_function ()
449 if (bbg_file_opened > 1 && gcov_is_error ())
451 warning ("error writing `%s'", bbg_file_name);
452 bbg_file_opened = -1;
457 struct function_list *item;
459 /* ??? Probably should re-use the existing struct function. */
460 item = xmalloc (sizeof (struct function_list));
462 *functions_tail = item;
463 functions_tail = &item->next;
466 item->name = xstrdup (IDENTIFIER_POINTER
467 (DECL_ASSEMBLER_NAME (current_function_decl)));
468 item->checksum = compute_checksum ();
469 for (i = 0; i != GCOV_COUNTERS; i++)
471 item->n_ctrs[i] = fn_n_ctrs[i];
472 prg_n_ctrs[i] += fn_n_ctrs[i];
475 prg_ctr_mask |= fn_ctr_mask;
478 bbg_function_announced = 0;
481 /* Creates the gcov_fn_info RECORD_TYPE. */
484 build_fn_info_type (counters)
487 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
490 build_pointer_type (build_qualified_type (char_type_node,
495 fields = build_decl (FIELD_DECL, NULL_TREE, string_type);
498 field = build_decl (FIELD_DECL, NULL_TREE, unsigned_type_node);
499 TREE_CHAIN (field) = fields;
502 array_type = build_index_type (build_int_2 (counters - 1, 0));
503 array_type = build_array_type (unsigned_type_node, array_type);
506 field = build_decl (FIELD_DECL, NULL_TREE, array_type);
507 TREE_CHAIN (field) = fields;
510 finish_builtin_struct (type, "__gcov_fn_info", fields, NULL_TREE);
515 /* Creates a CONSTRUCTOR for a gcov_fn_info. FUNCTION is
516 the function being processed and TYPE is the gcov_fn_info
520 build_fn_info_value (function, type)
521 const struct function_list *function;
524 tree value = NULL_TREE;
525 tree fields = TYPE_FIELDS (type);
526 size_t name_len = strlen (function->name);
527 tree fname = build_string (name_len + 1, function->name);
529 build_pointer_type (build_qualified_type (char_type_node,
532 tree array_value = NULL_TREE;
536 build_array_type (char_type_node,
537 build_index_type (build_int_2 (name_len, 0)));
538 value = tree_cons (fields,
539 build1 (ADDR_EXPR, string_type, fname),
541 fields = TREE_CHAIN (fields);
544 value = tree_cons (fields,
545 convert (unsigned_type_node,
546 build_int_2 (function->checksum, 0)),
548 fields = TREE_CHAIN (fields);
551 for (ix = 0; ix != GCOV_COUNTERS; ix++)
552 if (prg_ctr_mask & (1 << ix))
554 tree counters = convert (unsigned_type_node,
555 build_int_2 (function->n_ctrs[ix], 0));
557 array_value = tree_cons (NULL_TREE, counters, array_value);
560 array_value = build_constructor (TREE_TYPE (fields), nreverse (array_value));
561 value = tree_cons (fields, array_value, value);
563 value = build_constructor (type, nreverse (value));
568 /* Creates the gcov_ctr_info RECORD_TYPE. */
571 build_ctr_info_type ()
573 tree type = (*lang_hooks.types.make_type) (RECORD_TYPE);
574 tree field, fields = NULL_TREE;
577 field = build_decl (FIELD_DECL, NULL_TREE, unsigned_type_node);
578 TREE_CHAIN (field) = fields;
582 field = build_decl (FIELD_DECL, NULL_TREE,
583 build_pointer_type (make_signed_type (GCOV_TYPE_SIZE)));
584 TREE_CHAIN (field) = fields;
587 finish_builtin_struct (type, "__gcov_ctr_info", fields, NULL_TREE);
592 /* Creates a CONSTRUCTOR for a gcov_ctr_info. COUNTER is
593 the counter being processed and TYPE is the gcov_ctr_info
597 build_ctr_info_value (counter, type)
601 tree value = NULL_TREE;
602 tree fields = TYPE_FIELDS (type);
605 value = tree_cons (fields,
606 convert (unsigned_type_node,
607 build_int_2 (prg_n_ctrs[counter], 0)),
609 fields = TREE_CHAIN (fields);
611 if (prg_n_ctrs[counter])
613 tree array_type, array;
615 array_type = build_index_type (build_int_2 (prg_n_ctrs[counter] - 1, 0));
616 array_type = build_array_type (TREE_TYPE (TREE_TYPE (fields)),
619 array = build (VAR_DECL, array_type, NULL_TREE, NULL_TREE);
620 TREE_STATIC (array) = 1;
621 DECL_NAME (array) = get_identifier (XSTR (ctr_labels[counter], 0));
622 assemble_variable (array, 0, 0, 0);
624 value = tree_cons (fields,
625 build1 (ADDR_EXPR, TREE_TYPE (fields), array),
629 value = tree_cons (fields, null_pointer_node, value);
631 value = build_constructor (type, nreverse (value));
636 /* Creates the gcov_info RECORD_TYPE and initializer for it. Returns a
642 unsigned n_ctr_types, ix;
643 tree type, const_type;
644 tree fn_info_type, fn_info_value = NULL_TREE;
645 tree fn_info_ptr_type;
646 tree ctr_info_type, ctr_info_ary_type, ctr_info_value = NULL_TREE;
647 tree field, fields = NULL_TREE;
648 tree value = NULL_TREE;
649 tree filename_string;
653 const struct function_list *fn;
656 /* Count the number of active counters. */
657 for (n_ctr_types = 0, ix = 0; ix != GCOV_COUNTERS; ix++)
658 if (prg_ctr_mask & (1 << ix))
661 type = (*lang_hooks.types.make_type) (RECORD_TYPE);
662 const_type = build_qualified_type (type, TYPE_QUAL_CONST);
665 field = build_decl (FIELD_DECL, NULL_TREE, long_unsigned_type_node);
666 TREE_CHAIN (field) = fields;
668 value = tree_cons (field, convert (long_unsigned_type_node,
669 build_int_2 (GCOV_VERSION, 0)),
673 field = build_decl (FIELD_DECL, NULL_TREE, build_pointer_type (const_type));
674 TREE_CHAIN (field) = fields;
676 value = tree_cons (field, null_pointer_node, value);
679 string_type = build_pointer_type (build_qualified_type (char_type_node,
681 field = build_decl (FIELD_DECL, NULL_TREE, string_type);
682 TREE_CHAIN (field) = fields;
684 filename = getpwd ();
685 filename = (filename && da_file_name[0] != '/'
686 ? concat (filename, "/", da_file_name, NULL)
688 filename_len = strlen (filename);
689 filename_string = build_string (filename_len + 1, filename);
690 if (filename != da_file_name)
692 TREE_TYPE (filename_string) =
693 build_array_type (char_type_node,
694 build_index_type (build_int_2 (filename_len, 0)));
695 value = tree_cons (field, build1 (ADDR_EXPR, string_type, filename_string),
698 /* Build the fn_info type and initializer. */
699 fn_info_type = build_fn_info_type (n_ctr_types);
700 fn_info_ptr_type = build_pointer_type (build_qualified_type
701 (fn_info_type, TYPE_QUAL_CONST));
702 for (fn = functions_head, n_fns = 0; fn; fn = fn->next, n_fns++)
703 fn_info_value = tree_cons (NULL_TREE,
704 build_fn_info_value (fn, fn_info_type),
710 array_type = build_index_type (build_int_2 (n_fns - 1, 0));
711 array_type = build_array_type (fn_info_type, array_type);
713 fn_info_value = build_constructor (array_type, nreverse (fn_info_value));
714 fn_info_value = build1 (ADDR_EXPR, fn_info_ptr_type, fn_info_value);
717 fn_info_value = null_pointer_node;
719 /* number of functions */
720 field = build_decl (FIELD_DECL, NULL_TREE, unsigned_type_node);
721 TREE_CHAIN (field) = fields;
723 value = tree_cons (field,
724 convert (unsigned_type_node, build_int_2 (n_fns, 0)),
728 field = build_decl (FIELD_DECL, NULL_TREE, fn_info_ptr_type);
729 TREE_CHAIN (field) = fields;
731 value = tree_cons (field, fn_info_value, value);
734 field = build_decl (FIELD_DECL, NULL_TREE, unsigned_type_node);
735 TREE_CHAIN (field) = fields;
737 value = tree_cons (field,
738 convert (unsigned_type_node,
739 build_int_2 (prg_ctr_mask, 0)),
743 ctr_info_type = build_ctr_info_type ();
744 ctr_info_ary_type = build_index_type (build_int_2 (n_ctr_types, 0));
745 ctr_info_ary_type = build_array_type (ctr_info_type, ctr_info_ary_type);
746 for (ix = 0; ix != GCOV_COUNTERS; ix++)
747 if (prg_ctr_mask & (1 << ix))
748 ctr_info_value = tree_cons (NULL_TREE,
749 build_ctr_info_value (ix, ctr_info_type),
751 ctr_info_value = build_constructor (ctr_info_ary_type,
752 nreverse (ctr_info_value));
754 field = build_decl (FIELD_DECL, NULL_TREE, ctr_info_ary_type);
755 TREE_CHAIN (field) = fields;
757 value = tree_cons (field, ctr_info_value, value);
759 finish_builtin_struct (type, "__gcov_info", fields, NULL_TREE);
761 value = build_constructor (type, nreverse (value));
766 /* Write out the structure which libgcov uses to locate all the
767 counters. The structures used here must match those defined in
768 gcov-io.h. Write out the constructor to call __gcov_init. */
773 tree gcov_info, gcov_info_value;
777 rtx gcov_info_address;
778 int save_flag_inline_functions = flag_inline_functions;
783 gcov_info_value = build_gcov_info ();
785 gcov_info = build (VAR_DECL, TREE_TYPE (gcov_info_value),
786 NULL_TREE, NULL_TREE);
787 DECL_INITIAL (gcov_info) = gcov_info_value;
789 TREE_STATIC (gcov_info) = 1;
790 ASM_GENERATE_INTERNAL_LABEL (name, "LPBX", 0);
791 DECL_NAME (gcov_info) = get_identifier (name);
793 /* Build structure. */
794 assemble_variable (gcov_info, 0, 0, 0);
796 /* Build the constructor function to invoke __gcov_init. */
797 ctor_name = concat (IDENTIFIER_POINTER (get_file_function_name ('I')),
799 ctor = build_decl (FUNCTION_DECL, get_identifier (ctor_name),
800 build_function_type (void_type_node, NULL_TREE));
802 DECL_EXTERNAL (ctor) = 0;
804 /* It can be a static function as long as collect2 does not have
805 to scan the object file to find its ctor/dtor routine. */
806 TREE_PUBLIC (ctor) = ! targetm.have_ctors_dtors;
807 TREE_USED (ctor) = 1;
808 DECL_RESULT (ctor) = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
810 ctor = (*lang_hooks.decls.pushdecl) (ctor);
811 rest_of_decl_compilation (ctor, 0, 1, 0);
812 announce_function (ctor);
813 current_function_decl = ctor;
814 DECL_INITIAL (ctor) = error_mark_node;
815 make_decl_rtl (ctor, NULL);
816 init_function_start (ctor, input_filename, lineno);
817 (*lang_hooks.decls.pushlevel) (0);
818 expand_function_start (ctor, 0);
819 cfun->arc_profile = 0;
821 /* Actually generate the code to call __gcov_init. */
822 gcov_info_address = force_reg (Pmode, XEXP (DECL_RTL (gcov_info), 0));
823 emit_library_call (gcov_init_libfunc, LCT_NORMAL, VOIDmode, 1,
824 gcov_info_address, Pmode);
826 expand_function_end (input_filename, lineno, 0);
827 (*lang_hooks.decls.poplevel) (1, 0, 1);
829 /* Since ctor isn't in the list of globals, it would never be emitted
830 when it's considered to be 'safe' for inlining, so turn off
831 flag_inline_functions. */
832 flag_inline_functions = 0;
834 rest_of_compilation (ctor);
836 /* Reset flag_inline_functions to its original value. */
837 flag_inline_functions = save_flag_inline_functions;
840 fflush (asm_out_file);
841 current_function_decl = NULL_TREE;
843 if (targetm.have_ctors_dtors)
844 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (ctor), 0),
845 DEFAULT_INIT_PRIORITY);
848 /* Perform file-level initialization. Read in data file, generate name
852 coverage_init (filename)
853 const char *filename;
855 int len = strlen (filename);
857 da_file_name = (char *) xmalloc (len + strlen (GCOV_DATA_SUFFIX) + 1);
858 strcpy (da_file_name, filename);
859 strcat (da_file_name, GCOV_DATA_SUFFIX);
863 /* Open the bbg output file. */
864 bbg_file_name = (char *) xmalloc (len + strlen (GCOV_GRAPH_SUFFIX) + 1);
865 strcpy (bbg_file_name, filename);
866 strcat (bbg_file_name, GCOV_GRAPH_SUFFIX);
869 /* Performs file-level cleanup. Close graph file, generate coverage
870 variables and constructor. */
878 int error = gcov_close ();
881 unlink (bbg_file_name);
883 /* If the compiler is instrumented, we should not
884 unconditionally remove the counts file, because we might be
885 recompiling ourselves. The .da files are all removed during
886 copying the stage1 files. */
889 unlink (da_file_name);
893 #include "gt-coverage.h"