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 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, 51 Franklin Street, Fifth Floor, Boston, MA
31 #include "coretypes.h"
43 #include "langhooks.h"
45 #include "tree-iterator.h"
52 struct function_list
*next
; /* next function */
53 unsigned ident
; /* function ident */
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
;
77 static unsigned no_coverage
= 0;
79 /* Cumulative counter information for whole program. */
80 static unsigned prg_ctr_mask
; /* Mask of counter types generated. */
81 static unsigned prg_n_ctrs
[GCOV_COUNTERS
]; /* Total counters allocated. */
83 /* Counter information for current function. */
84 static unsigned fn_ctr_mask
; /* Mask of counters used. */
85 static unsigned fn_n_ctrs
[GCOV_COUNTERS
]; /* Counters allocated. */
86 static unsigned fn_b_ctrs
[GCOV_COUNTERS
]; /* Allocation base. */
88 /* Name of the output file for coverage output file. */
89 static char *bbg_file_name
;
90 static unsigned bbg_file_opened
;
91 static int bbg_function_announced
;
93 /* Name of the count data file. */
94 static char *da_file_name
;
96 /* Hash table of count data. */
97 static htab_t counts_hash
= NULL
;
99 /* Trees representing the counter table arrays. */
100 static GTY(()) tree tree_ctr_tables
[GCOV_COUNTERS
];
102 /* The names of the counter tables. Not used if we're
103 generating counters at tree level. */
104 static GTY(()) rtx ctr_labels
[GCOV_COUNTERS
];
106 /* The names of merge functions for counters. */
107 static const char *const ctr_merge_functions
[GCOV_COUNTERS
] = GCOV_MERGE_FUNCTIONS
;
108 static const char *const ctr_names
[GCOV_COUNTERS
] = GCOV_COUNTER_NAMES
;
110 /* Forward declarations. */
111 static hashval_t
htab_counts_entry_hash (const void *);
112 static int htab_counts_entry_eq (const void *, const void *);
113 static void htab_counts_entry_del (void *);
114 static void read_counts_file (void);
115 static unsigned compute_checksum (void);
116 static unsigned coverage_checksum_string (unsigned, const char *);
117 static tree
build_fn_info_type (unsigned);
118 static tree
build_fn_info_value (const struct function_list
*, tree
);
119 static tree
build_ctr_info_type (void);
120 static tree
build_ctr_info_value (unsigned, tree
);
121 static tree
build_gcov_info (void);
122 static void create_coverage (void);
124 /* Return the type node for gcov_type. */
129 return lang_hooks
.types
.type_for_size (GCOV_TYPE_SIZE
, false);
132 /* Return the type node for gcov_unsigned_t. */
135 get_gcov_unsigned_t (void)
137 return lang_hooks
.types
.type_for_size (32, true);
141 htab_counts_entry_hash (const void *of
)
143 const counts_entry_t
*entry
= of
;
145 return entry
->ident
* GCOV_COUNTERS
+ entry
->ctr
;
149 htab_counts_entry_eq (const void *of1
, const void *of2
)
151 const counts_entry_t
*entry1
= of1
;
152 const counts_entry_t
*entry2
= of2
;
154 return entry1
->ident
== entry2
->ident
&& entry1
->ctr
== entry2
->ctr
;
158 htab_counts_entry_del (void *of
)
160 counts_entry_t
*entry
= of
;
162 free (entry
->counts
);
166 /* Read in the counts file, if available. */
169 read_counts_file (void)
171 gcov_unsigned_t fn_ident
= 0;
172 gcov_unsigned_t checksum
= -1;
173 counts_entry_t
*summaried
= NULL
;
174 unsigned seen_summary
= 0;
178 if (!gcov_open (da_file_name
, 1))
181 if (!gcov_magic (gcov_read_unsigned (), GCOV_DATA_MAGIC
))
183 warning (0, "%qs is not a gcov data file", da_file_name
);
187 else if ((tag
= gcov_read_unsigned ()) != GCOV_VERSION
)
191 GCOV_UNSIGNED2STRING (v
, tag
);
192 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
194 warning (0, "%qs is version %q.*s, expected version %q.*s",
195 da_file_name
, 4, v
, 4, e
);
200 /* Read and discard the stamp. */
201 gcov_read_unsigned ();
203 counts_hash
= htab_create (10,
204 htab_counts_entry_hash
, htab_counts_entry_eq
,
205 htab_counts_entry_del
);
206 while ((tag
= gcov_read_unsigned ()))
208 gcov_unsigned_t length
;
209 gcov_position_t offset
;
211 length
= gcov_read_unsigned ();
212 offset
= gcov_position ();
213 if (tag
== GCOV_TAG_FUNCTION
)
215 fn_ident
= gcov_read_unsigned ();
216 checksum
= gcov_read_unsigned ();
219 /* We have already seen a summary, this means that this
220 new function begins a new set of program runs. We
221 must unlink the summaried chain. */
222 counts_entry_t
*entry
, *chain
;
224 for (entry
= summaried
; entry
; entry
= chain
)
226 chain
= entry
->chain
;
233 else if (tag
== GCOV_TAG_PROGRAM_SUMMARY
)
235 counts_entry_t
*entry
;
236 struct gcov_summary summary
;
238 gcov_read_summary (&summary
);
240 for (entry
= summaried
; entry
; entry
= entry
->chain
)
242 struct gcov_ctr_summary
*csum
= &summary
.ctrs
[entry
->ctr
];
244 entry
->summary
.runs
+= csum
->runs
;
245 entry
->summary
.sum_all
+= csum
->sum_all
;
246 if (entry
->summary
.run_max
< csum
->run_max
)
247 entry
->summary
.run_max
= csum
->run_max
;
248 entry
->summary
.sum_max
+= csum
->sum_max
;
251 else if (GCOV_TAG_IS_COUNTER (tag
) && fn_ident
)
253 counts_entry_t
**slot
, *entry
, elt
;
254 unsigned n_counts
= GCOV_TAG_COUNTER_NUM (length
);
257 elt
.ident
= fn_ident
;
258 elt
.ctr
= GCOV_COUNTER_FOR_TAG (tag
);
260 slot
= (counts_entry_t
**) htab_find_slot
261 (counts_hash
, &elt
, INSERT
);
265 *slot
= entry
= XCNEW (counts_entry_t
);
266 entry
->ident
= elt
.ident
;
267 entry
->ctr
= elt
.ctr
;
268 entry
->checksum
= checksum
;
269 entry
->summary
.num
= n_counts
;
270 entry
->counts
= XCNEWVEC (gcov_type
, n_counts
);
272 else if (entry
->checksum
!= checksum
)
274 error ("coverage mismatch for function %u while reading execution counters",
276 error ("checksum is %x instead of %x", entry
->checksum
, checksum
);
277 htab_delete (counts_hash
);
280 else if (entry
->summary
.num
!= n_counts
)
282 error ("coverage mismatch for function %u while reading execution counters",
284 error ("number of counters is %d instead of %d", entry
->summary
.num
, n_counts
);
285 htab_delete (counts_hash
);
288 else if (elt
.ctr
>= GCOV_COUNTERS_SUMMABLE
)
290 error ("cannot merge separate %s counters for function %u",
291 ctr_names
[elt
.ctr
], fn_ident
);
295 if (elt
.ctr
< GCOV_COUNTERS_SUMMABLE
296 /* This should always be true for a just allocated entry,
297 and always false for an existing one. Check this way, in
298 case the gcov file is corrupt. */
299 && (!entry
->chain
|| summaried
!= entry
))
301 entry
->chain
= summaried
;
304 for (ix
= 0; ix
!= n_counts
; ix
++)
305 entry
->counts
[ix
] += gcov_read_counter ();
308 gcov_sync (offset
, length
);
309 if ((is_error
= gcov_is_error ()))
311 error (is_error
< 0 ? "%qs has overflowed" : "%qs is corrupted",
313 htab_delete (counts_hash
);
321 /* Returns the counters for a particular tag. */
324 get_coverage_counts (unsigned counter
, unsigned expected
,
325 const struct gcov_ctr_summary
**summary
)
327 counts_entry_t
*entry
, elt
;
328 gcov_unsigned_t checksum
= -1;
330 /* No hash table, no counts. */
333 static int warned
= 0;
336 inform ((flag_guess_branch_prob
337 ? "file %s not found, execution counts estimated"
338 : "file %s not found, execution counts assumed to be zero"),
343 elt
.ident
= current_function_funcdef_no
+ 1;
345 entry
= htab_find (counts_hash
, &elt
);
348 warning (0, "no coverage for function %qs found", IDENTIFIER_POINTER
349 (DECL_ASSEMBLER_NAME (current_function_decl
)));
353 checksum
= compute_checksum ();
354 if (entry
->checksum
!= checksum
)
356 error ("coverage mismatch for function %qs while reading counter %qs",
357 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl
)),
359 error ("checksum is %x instead of %x", entry
->checksum
, checksum
);
362 else if (entry
->summary
.num
!= expected
)
364 error ("coverage mismatch for function %qs while reading counter %qs",
365 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl
)),
367 error ("number of counters is %d instead of %d", entry
->summary
.num
, expected
);
372 *summary
= &entry
->summary
;
374 return entry
->counts
;
377 /* Allocate NUM counters of type COUNTER. Returns nonzero if the
378 allocation succeeded. */
381 coverage_counter_alloc (unsigned counter
, unsigned num
)
389 if (!tree_ctr_tables
[counter
])
391 /* Generate and save a copy of this so it can be shared. Leave
392 the index type unspecified for now; it will be set after all
393 functions have been compiled. */
395 tree gcov_type_node
= get_gcov_type ();
396 tree gcov_type_array_type
397 = build_array_type (gcov_type_node
, NULL_TREE
);
398 tree_ctr_tables
[counter
]
399 = build_decl (VAR_DECL
, NULL_TREE
, gcov_type_array_type
);
400 TREE_STATIC (tree_ctr_tables
[counter
]) = 1;
401 ASM_GENERATE_INTERNAL_LABEL (buf
, "LPBX", counter
+ 1);
402 DECL_NAME (tree_ctr_tables
[counter
]) = get_identifier (buf
);
403 DECL_ALIGN (tree_ctr_tables
[counter
]) = TYPE_ALIGN (gcov_type_node
);
405 fn_b_ctrs
[counter
] = fn_n_ctrs
[counter
];
406 fn_n_ctrs
[counter
] += num
;
407 fn_ctr_mask
|= 1 << counter
;
411 /* Generate a tree to access COUNTER NO. */
414 tree_coverage_counter_ref (unsigned counter
, unsigned no
)
416 tree gcov_type_node
= get_gcov_type ();
418 gcc_assert (no
< fn_n_ctrs
[counter
] - fn_b_ctrs
[counter
]);
419 no
+= prg_n_ctrs
[counter
] + fn_b_ctrs
[counter
];
421 /* "no" here is an array index, scaled to bytes later. */
422 return build4 (ARRAY_REF
, gcov_type_node
, tree_ctr_tables
[counter
],
423 build_int_cst (NULL_TREE
, no
), NULL
, NULL
);
426 /* Generate a checksum for a string. CHKSUM is the current
430 coverage_checksum_string (unsigned chksum
, const char *string
)
435 /* Look for everything that looks if it were produced by
436 get_file_function_name_long and zero out the second part
437 that may result from flag_random_seed. This is not critical
438 as the checksums are used only for sanity checking. */
439 for (i
= 0; string
[i
]; i
++)
442 if (!strncmp (string
+ i
, "_GLOBAL__N_", 11))
444 if (!strncmp (string
+ i
, "_GLOBAL__", 9))
447 /* C++ namespaces do have scheme:
448 _GLOBAL__N_<filename>_<wrongmagicnumber>_<magicnumber>functionname
449 since filename might contain extra underscores there seems
450 to be no better chance then walk all possible offsets looking
454 for (i
= i
+ offset
; string
[i
]; i
++)
459 for (y
= 1; y
< 9; y
++)
460 if (!(string
[i
+ y
] >= '0' && string
[i
+ y
] <= '9')
461 && !(string
[i
+ y
] >= 'A' && string
[i
+ y
] <= 'F'))
463 if (y
!= 9 || string
[i
+ 9] != '_')
465 for (y
= 10; y
< 18; y
++)
466 if (!(string
[i
+ y
] >= '0' && string
[i
+ y
] <= '9')
467 && !(string
[i
+ y
] >= 'A' && string
[i
+ y
] <= 'F'))
472 string
= dup
= xstrdup (string
);
473 for (y
= 10; y
< 18; y
++)
480 chksum
= crc32_string (chksum
, string
);
487 /* Compute checksum for the current function. We generate a CRC32. */
490 compute_checksum (void)
492 expanded_location xloc
493 = expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
494 unsigned chksum
= xloc
.line
;
496 chksum
= coverage_checksum_string (chksum
, xloc
.file
);
497 chksum
= coverage_checksum_string
498 (chksum
, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl
)));
503 /* Begin output to the graph file for the current function.
504 Opens the output file, if not already done. Writes the
505 function header, if not already done. Returns nonzero if data
509 coverage_begin_output (void)
514 if (!bbg_function_announced
)
516 expanded_location xloc
517 = expand_location (DECL_SOURCE_LOCATION (current_function_decl
));
518 unsigned long offset
;
520 if (!bbg_file_opened
)
522 if (!gcov_open (bbg_file_name
, -1))
523 error ("cannot open %s", bbg_file_name
);
526 gcov_write_unsigned (GCOV_NOTE_MAGIC
);
527 gcov_write_unsigned (GCOV_VERSION
);
528 gcov_write_unsigned (local_tick
);
533 /* Announce function */
534 offset
= gcov_write_tag (GCOV_TAG_FUNCTION
);
535 gcov_write_unsigned (current_function_funcdef_no
+ 1);
536 gcov_write_unsigned (compute_checksum ());
537 gcov_write_string (IDENTIFIER_POINTER
538 (DECL_ASSEMBLER_NAME (current_function_decl
)));
539 gcov_write_string (xloc
.file
);
540 gcov_write_unsigned (xloc
.line
);
541 gcov_write_length (offset
);
543 bbg_function_announced
= 1;
545 return !gcov_is_error ();
548 /* Finish coverage data for the current function. Verify no output
549 error has occurred. Save function coverage counts. */
552 coverage_end_function (void)
556 if (bbg_file_opened
> 1 && gcov_is_error ())
558 warning (0, "error writing %qs", bbg_file_name
);
559 bbg_file_opened
= -1;
564 struct function_list
*item
;
566 item
= XNEW (struct function_list
);
568 *functions_tail
= item
;
569 functions_tail
= &item
->next
;
572 item
->ident
= current_function_funcdef_no
+ 1;
573 item
->checksum
= compute_checksum ();
574 for (i
= 0; i
!= GCOV_COUNTERS
; i
++)
576 item
->n_ctrs
[i
] = fn_n_ctrs
[i
];
577 prg_n_ctrs
[i
] += fn_n_ctrs
[i
];
578 fn_n_ctrs
[i
] = fn_b_ctrs
[i
] = 0;
580 prg_ctr_mask
|= fn_ctr_mask
;
583 bbg_function_announced
= 0;
586 /* Creates the gcov_fn_info RECORD_TYPE. */
589 build_fn_info_type (unsigned int counters
)
591 tree type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
596 fields
= build_decl (FIELD_DECL
, NULL_TREE
, get_gcov_unsigned_t ());
599 field
= build_decl (FIELD_DECL
, NULL_TREE
, get_gcov_unsigned_t ());
600 TREE_CHAIN (field
) = fields
;
603 array_type
= build_int_cst (NULL_TREE
, counters
- 1);
604 array_type
= build_index_type (array_type
);
605 array_type
= build_array_type (get_gcov_unsigned_t (), array_type
);
608 field
= build_decl (FIELD_DECL
, NULL_TREE
, array_type
);
609 TREE_CHAIN (field
) = fields
;
612 finish_builtin_struct (type
, "__gcov_fn_info", fields
, NULL_TREE
);
617 /* Creates a CONSTRUCTOR for a gcov_fn_info. FUNCTION is
618 the function being processed and TYPE is the gcov_fn_info
622 build_fn_info_value (const struct function_list
*function
, tree type
)
624 tree value
= NULL_TREE
;
625 tree fields
= TYPE_FIELDS (type
);
627 tree array_value
= NULL_TREE
;
630 value
= tree_cons (fields
, build_int_cstu (get_gcov_unsigned_t (),
631 function
->ident
), value
);
632 fields
= TREE_CHAIN (fields
);
635 value
= tree_cons (fields
, build_int_cstu (get_gcov_unsigned_t (),
636 function
->checksum
), value
);
637 fields
= TREE_CHAIN (fields
);
640 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
641 if (prg_ctr_mask
& (1 << ix
))
643 tree counters
= build_int_cstu (get_gcov_unsigned_t (),
644 function
->n_ctrs
[ix
]);
646 array_value
= tree_cons (NULL_TREE
, counters
, array_value
);
649 /* FIXME: use build_constructor directly. */
650 array_value
= build_constructor_from_list (TREE_TYPE (fields
),
651 nreverse (array_value
));
652 value
= tree_cons (fields
, array_value
, value
);
654 /* FIXME: use build_constructor directly. */
655 value
= build_constructor_from_list (type
, nreverse (value
));
660 /* Creates the gcov_ctr_info RECORD_TYPE. */
663 build_ctr_info_type (void)
665 tree type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
666 tree field
, fields
= NULL_TREE
;
667 tree gcov_ptr_type
= build_pointer_type (get_gcov_type ());
668 tree gcov_merge_fn_type
;
671 field
= build_decl (FIELD_DECL
, NULL_TREE
, get_gcov_unsigned_t ());
672 TREE_CHAIN (field
) = fields
;
676 field
= build_decl (FIELD_DECL
, NULL_TREE
, gcov_ptr_type
);
677 TREE_CHAIN (field
) = fields
;
682 build_function_type_list (void_type_node
,
683 gcov_ptr_type
, get_gcov_unsigned_t (),
685 field
= build_decl (FIELD_DECL
, NULL_TREE
,
686 build_pointer_type (gcov_merge_fn_type
));
687 TREE_CHAIN (field
) = fields
;
690 finish_builtin_struct (type
, "__gcov_ctr_info", fields
, NULL_TREE
);
695 /* Creates a CONSTRUCTOR for a gcov_ctr_info. COUNTER is
696 the counter being processed and TYPE is the gcov_ctr_info
700 build_ctr_info_value (unsigned int counter
, tree type
)
702 tree value
= NULL_TREE
;
703 tree fields
= TYPE_FIELDS (type
);
707 value
= tree_cons (fields
,
708 build_int_cstu (get_gcov_unsigned_t (),
709 prg_n_ctrs
[counter
]),
711 fields
= TREE_CHAIN (fields
);
713 if (prg_n_ctrs
[counter
])
717 array_type
= build_int_cstu (get_gcov_unsigned_t (),
718 prg_n_ctrs
[counter
] - 1);
719 array_type
= build_index_type (array_type
);
720 array_type
= build_array_type (TREE_TYPE (TREE_TYPE (fields
)),
723 TREE_TYPE (tree_ctr_tables
[counter
]) = array_type
;
724 DECL_SIZE (tree_ctr_tables
[counter
]) = TYPE_SIZE (array_type
);
725 DECL_SIZE_UNIT (tree_ctr_tables
[counter
]) = TYPE_SIZE_UNIT (array_type
);
726 assemble_variable (tree_ctr_tables
[counter
], 0, 0, 0);
728 value
= tree_cons (fields
,
729 build1 (ADDR_EXPR
, TREE_TYPE (fields
),
730 tree_ctr_tables
[counter
]),
734 value
= tree_cons (fields
, null_pointer_node
, value
);
735 fields
= TREE_CHAIN (fields
);
737 fn
= build_decl (FUNCTION_DECL
,
738 get_identifier (ctr_merge_functions
[counter
]),
739 TREE_TYPE (TREE_TYPE (fields
)));
740 DECL_EXTERNAL (fn
) = 1;
741 TREE_PUBLIC (fn
) = 1;
742 DECL_ARTIFICIAL (fn
) = 1;
743 TREE_NOTHROW (fn
) = 1;
744 value
= tree_cons (fields
,
745 build1 (ADDR_EXPR
, TREE_TYPE (fields
), fn
),
748 /* FIXME: use build_constructor directly. */
749 value
= build_constructor_from_list (type
, nreverse (value
));
754 /* Creates the gcov_info RECORD_TYPE and initializer for it. Returns a
758 build_gcov_info (void)
760 unsigned n_ctr_types
, ix
;
761 tree type
, const_type
;
762 tree fn_info_type
, fn_info_value
= NULL_TREE
;
763 tree fn_info_ptr_type
;
764 tree ctr_info_type
, ctr_info_ary_type
, ctr_info_value
= NULL_TREE
;
765 tree field
, fields
= NULL_TREE
;
766 tree value
= NULL_TREE
;
767 tree filename_string
;
771 const struct function_list
*fn
;
774 /* Count the number of active counters. */
775 for (n_ctr_types
= 0, ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
776 if (prg_ctr_mask
& (1 << ix
))
779 type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
780 const_type
= build_qualified_type (type
, TYPE_QUAL_CONST
);
783 field
= build_decl (FIELD_DECL
, NULL_TREE
, get_gcov_unsigned_t ());
784 TREE_CHAIN (field
) = fields
;
786 value
= tree_cons (field
, build_int_cstu (TREE_TYPE (field
), GCOV_VERSION
),
790 field
= build_decl (FIELD_DECL
, NULL_TREE
, build_pointer_type (const_type
));
791 TREE_CHAIN (field
) = fields
;
793 value
= tree_cons (field
, null_pointer_node
, value
);
796 field
= build_decl (FIELD_DECL
, NULL_TREE
, get_gcov_unsigned_t ());
797 TREE_CHAIN (field
) = fields
;
799 value
= tree_cons (field
, build_int_cstu (TREE_TYPE (field
), local_tick
),
803 string_type
= build_pointer_type (build_qualified_type (char_type_node
,
805 field
= build_decl (FIELD_DECL
, NULL_TREE
, string_type
);
806 TREE_CHAIN (field
) = fields
;
808 filename
= getpwd ();
809 filename
= (filename
&& da_file_name
[0] != '/'
810 ? concat (filename
, "/", da_file_name
, NULL
)
812 filename_len
= strlen (filename
);
813 filename_string
= build_string (filename_len
+ 1, filename
);
814 if (filename
!= da_file_name
)
816 TREE_TYPE (filename_string
) = build_array_type
817 (char_type_node
, build_index_type
818 (build_int_cst (NULL_TREE
, filename_len
)));
819 value
= tree_cons (field
, build1 (ADDR_EXPR
, string_type
, filename_string
),
822 /* Build the fn_info type and initializer. */
823 fn_info_type
= build_fn_info_type (n_ctr_types
);
824 fn_info_ptr_type
= build_pointer_type (build_qualified_type
825 (fn_info_type
, TYPE_QUAL_CONST
));
826 for (fn
= functions_head
, n_fns
= 0; fn
; fn
= fn
->next
, n_fns
++)
827 fn_info_value
= tree_cons (NULL_TREE
,
828 build_fn_info_value (fn
, fn_info_type
),
834 array_type
= build_index_type (build_int_cst (NULL_TREE
, n_fns
- 1));
835 array_type
= build_array_type (fn_info_type
, array_type
);
837 /* FIXME: use build_constructor directly. */
838 fn_info_value
= build_constructor_from_list (array_type
,
839 nreverse (fn_info_value
));
840 fn_info_value
= build1 (ADDR_EXPR
, fn_info_ptr_type
, fn_info_value
);
843 fn_info_value
= null_pointer_node
;
845 /* number of functions */
846 field
= build_decl (FIELD_DECL
, NULL_TREE
, get_gcov_unsigned_t ());
847 TREE_CHAIN (field
) = fields
;
849 value
= tree_cons (field
,
850 build_int_cstu (get_gcov_unsigned_t (), n_fns
),
854 field
= build_decl (FIELD_DECL
, NULL_TREE
, fn_info_ptr_type
);
855 TREE_CHAIN (field
) = fields
;
857 value
= tree_cons (field
, fn_info_value
, value
);
860 field
= build_decl (FIELD_DECL
, NULL_TREE
, get_gcov_unsigned_t ());
861 TREE_CHAIN (field
) = fields
;
863 value
= tree_cons (field
,
864 build_int_cstu (get_gcov_unsigned_t (), prg_ctr_mask
),
868 ctr_info_type
= build_ctr_info_type ();
869 ctr_info_ary_type
= build_index_type (build_int_cst (NULL_TREE
,
871 ctr_info_ary_type
= build_array_type (ctr_info_type
, ctr_info_ary_type
);
872 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
873 if (prg_ctr_mask
& (1 << ix
))
874 ctr_info_value
= tree_cons (NULL_TREE
,
875 build_ctr_info_value (ix
, ctr_info_type
),
877 /* FIXME: use build_constructor directly. */
878 ctr_info_value
= build_constructor_from_list (ctr_info_ary_type
,
879 nreverse (ctr_info_value
));
881 field
= build_decl (FIELD_DECL
, NULL_TREE
, ctr_info_ary_type
);
882 TREE_CHAIN (field
) = fields
;
884 value
= tree_cons (field
, ctr_info_value
, value
);
886 finish_builtin_struct (type
, "__gcov_info", fields
, NULL_TREE
);
888 /* FIXME: use build_constructor directly. */
889 value
= build_constructor_from_list (type
, nreverse (value
));
894 /* Write out the structure which libgcov uses to locate all the
895 counters. The structures used here must match those defined in
896 gcov-io.h. Write out the constructor to call __gcov_init. */
899 create_coverage (void)
901 tree gcov_info
, gcov_init
, body
, t
;
904 no_coverage
= 1; /* Disable any further coverage. */
909 t
= build_gcov_info ();
911 gcov_info
= build_decl (VAR_DECL
, NULL_TREE
, TREE_TYPE (t
));
912 TREE_STATIC (gcov_info
) = 1;
913 ASM_GENERATE_INTERNAL_LABEL (name_buf
, "LPBX", 0);
914 DECL_NAME (gcov_info
) = get_identifier (name_buf
);
915 DECL_INITIAL (gcov_info
) = t
;
917 /* Build structure. */
918 assemble_variable (gcov_info
, 0, 0, 0);
920 /* Build a decl for __gcov_init. */
921 t
= build_pointer_type (TREE_TYPE (gcov_info
));
922 t
= build_function_type_list (void_type_node
, t
, NULL
);
923 t
= build_decl (FUNCTION_DECL
, get_identifier ("__gcov_init"), t
);
925 DECL_EXTERNAL (t
) = 1;
928 /* Generate a call to __gcov_init(&gcov_info). */
930 t
= build_fold_addr_expr (gcov_info
);
931 t
= tree_cons (NULL
, t
, NULL
);
932 t
= build_function_call_expr (gcov_init
, t
);
933 append_to_statement_list (t
, &body
);
935 /* Generate a constructor to run it. */
936 cgraph_build_static_cdtor ('I', body
, DEFAULT_INIT_PRIORITY
);
939 /* Perform file-level initialization. Read in data file, generate name
943 coverage_init (const char *filename
)
945 int len
= strlen (filename
);
947 /* Name of da file. */
948 da_file_name
= XNEWVEC (char, len
+ strlen (GCOV_DATA_SUFFIX
) + 1);
949 strcpy (da_file_name
, filename
);
950 strcat (da_file_name
, GCOV_DATA_SUFFIX
);
952 /* Name of bbg file. */
953 bbg_file_name
= XNEWVEC (char, len
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
954 strcpy (bbg_file_name
, filename
);
955 strcat (bbg_file_name
, GCOV_NOTE_SUFFIX
);
960 /* Performs file-level cleanup. Close graph file, generate coverage
961 variables and constructor. */
964 coverage_finish (void)
969 int error
= gcov_close ();
972 unlink (bbg_file_name
);
974 /* Only remove the da file, if we cannot stamp it. If we can
975 stamp it, libgcov will DTRT. */
976 unlink (da_file_name
);
980 #include "gt-coverage.h"