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 fields
= TYPE_FIELDS (type
);
677 VEC(constructor_elt
,gc
) *v1
= NULL
;
678 VEC(constructor_elt
,gc
) *v2
= NULL
;
681 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
682 build_int_cstu (get_gcov_unsigned_t (),
684 fields
= TREE_CHAIN (fields
);
687 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
688 build_int_cstu (get_gcov_unsigned_t (),
689 function
->checksum
));
690 fields
= TREE_CHAIN (fields
);
693 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
694 if (prg_ctr_mask
& (1 << ix
))
695 CONSTRUCTOR_APPEND_ELT (v2
, NULL
,
696 build_int_cstu (get_gcov_unsigned_t (),
697 function
->n_ctrs
[ix
]));
699 CONSTRUCTOR_APPEND_ELT (v1
, fields
,
700 build_constructor (TREE_TYPE (fields
), v2
));
702 return build_constructor (type
, v1
);
705 /* Creates the gcov_ctr_info RECORD_TYPE. */
708 build_ctr_info_type (void)
710 tree type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
711 tree field
, fields
= NULL_TREE
;
712 tree gcov_ptr_type
= build_pointer_type (get_gcov_type ());
713 tree gcov_merge_fn_type
;
716 field
= build_decl (BUILTINS_LOCATION
,
717 FIELD_DECL
, NULL_TREE
, get_gcov_unsigned_t ());
718 TREE_CHAIN (field
) = fields
;
722 field
= build_decl (BUILTINS_LOCATION
,
723 FIELD_DECL
, NULL_TREE
, gcov_ptr_type
);
724 TREE_CHAIN (field
) = fields
;
729 build_function_type_list (void_type_node
,
730 gcov_ptr_type
, get_gcov_unsigned_t (),
732 field
= build_decl (BUILTINS_LOCATION
,
733 FIELD_DECL
, NULL_TREE
,
734 build_pointer_type (gcov_merge_fn_type
));
735 TREE_CHAIN (field
) = fields
;
738 finish_builtin_struct (type
, "__gcov_ctr_info", fields
, NULL_TREE
);
743 /* Creates a CONSTRUCTOR for a gcov_ctr_info. COUNTER is
744 the counter being processed and TYPE is the gcov_ctr_info
748 build_ctr_info_value (unsigned int counter
, tree type
)
750 tree fields
= TYPE_FIELDS (type
);
752 VEC(constructor_elt
,gc
) *v
= NULL
;
755 CONSTRUCTOR_APPEND_ELT (v
, fields
,
756 build_int_cstu (get_gcov_unsigned_t (),
757 prg_n_ctrs
[counter
]));
758 fields
= TREE_CHAIN (fields
);
760 if (prg_n_ctrs
[counter
])
764 array_type
= build_int_cstu (get_gcov_unsigned_t (),
765 prg_n_ctrs
[counter
] - 1);
766 array_type
= build_index_type (array_type
);
767 array_type
= build_array_type (TREE_TYPE (TREE_TYPE (fields
)),
770 TREE_TYPE (tree_ctr_tables
[counter
]) = array_type
;
771 DECL_SIZE (tree_ctr_tables
[counter
]) = TYPE_SIZE (array_type
);
772 DECL_SIZE_UNIT (tree_ctr_tables
[counter
]) = TYPE_SIZE_UNIT (array_type
);
773 varpool_finalize_decl (tree_ctr_tables
[counter
]);
775 CONSTRUCTOR_APPEND_ELT (v
, fields
,
776 build1 (ADDR_EXPR
, TREE_TYPE (fields
),
777 tree_ctr_tables
[counter
]));
780 CONSTRUCTOR_APPEND_ELT (v
, fields
, null_pointer_node
);
781 fields
= TREE_CHAIN (fields
);
783 fn
= build_decl (BUILTINS_LOCATION
,
785 get_identifier (ctr_merge_functions
[counter
]),
786 TREE_TYPE (TREE_TYPE (fields
)));
787 DECL_EXTERNAL (fn
) = 1;
788 TREE_PUBLIC (fn
) = 1;
789 DECL_ARTIFICIAL (fn
) = 1;
790 TREE_NOTHROW (fn
) = 1;
791 DECL_ASSEMBLER_NAME (fn
); /* Initialize assembler name so we can stream out. */
792 CONSTRUCTOR_APPEND_ELT (v
, fields
, build1 (ADDR_EXPR
, TREE_TYPE (fields
), fn
));
794 return build_constructor (type
, v
);
797 /* Creates the gcov_info RECORD_TYPE and initializer for it. Returns a
801 build_gcov_info (void)
803 unsigned n_ctr_types
, ix
;
804 tree type
, const_type
;
805 tree fn_info_type
, fn_info_value
= NULL_TREE
;
806 tree fn_info_ptr_type
;
807 tree ctr_info_type
, ctr_info_ary_type
, ctr_info_value
= NULL_TREE
;
808 tree field
, fields
= NULL_TREE
;
809 tree filename_string
;
810 int da_file_name_len
;
812 const struct function_list
*fn
;
814 VEC(constructor_elt
,gc
) *v1
= NULL
;
815 VEC(constructor_elt
,gc
) *v2
= NULL
;
817 /* Count the number of active counters. */
818 for (n_ctr_types
= 0, ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
819 if (prg_ctr_mask
& (1 << ix
))
822 type
= lang_hooks
.types
.make_type (RECORD_TYPE
);
823 const_type
= build_qualified_type (type
, TYPE_QUAL_CONST
);
826 field
= build_decl (BUILTINS_LOCATION
,
827 FIELD_DECL
, NULL_TREE
, get_gcov_unsigned_t ());
828 TREE_CHAIN (field
) = fields
;
830 CONSTRUCTOR_APPEND_ELT (v1
, field
,
831 build_int_cstu (TREE_TYPE (field
), GCOV_VERSION
));
834 field
= build_decl (BUILTINS_LOCATION
,
835 FIELD_DECL
, NULL_TREE
, build_pointer_type (const_type
));
836 TREE_CHAIN (field
) = fields
;
838 CONSTRUCTOR_APPEND_ELT (v1
, field
, null_pointer_node
);
841 field
= build_decl (BUILTINS_LOCATION
,
842 FIELD_DECL
, NULL_TREE
, get_gcov_unsigned_t ());
843 TREE_CHAIN (field
) = fields
;
845 CONSTRUCTOR_APPEND_ELT (v1
, field
,
846 build_int_cstu (TREE_TYPE (field
), local_tick
));
849 string_type
= build_pointer_type (build_qualified_type (char_type_node
,
851 field
= build_decl (BUILTINS_LOCATION
,
852 FIELD_DECL
, NULL_TREE
, string_type
);
853 TREE_CHAIN (field
) = fields
;
855 da_file_name_len
= strlen (da_file_name
);
856 filename_string
= build_string (da_file_name_len
+ 1, da_file_name
);
857 TREE_TYPE (filename_string
) = build_array_type
858 (char_type_node
, build_index_type
859 (build_int_cst (NULL_TREE
, da_file_name_len
)));
860 CONSTRUCTOR_APPEND_ELT (v1
, field
,
861 build1 (ADDR_EXPR
, string_type
, filename_string
));
863 /* Build the fn_info type and initializer. */
864 fn_info_type
= build_fn_info_type (n_ctr_types
);
865 fn_info_ptr_type
= build_pointer_type (build_qualified_type
866 (fn_info_type
, TYPE_QUAL_CONST
));
867 for (fn
= functions_head
, n_fns
= 0; fn
; fn
= fn
->next
, n_fns
++)
868 CONSTRUCTOR_APPEND_ELT (v2
, NULL_TREE
,
869 build_fn_info_value (fn
, fn_info_type
));
875 array_type
= build_index_type (build_int_cst (NULL_TREE
, n_fns
- 1));
876 array_type
= build_array_type (fn_info_type
, array_type
);
878 fn_info_value
= build_constructor (array_type
, v2
);
879 fn_info_value
= build1 (ADDR_EXPR
, fn_info_ptr_type
, fn_info_value
);
882 fn_info_value
= null_pointer_node
;
884 /* number of functions */
885 field
= build_decl (BUILTINS_LOCATION
,
886 FIELD_DECL
, NULL_TREE
, get_gcov_unsigned_t ());
887 TREE_CHAIN (field
) = fields
;
889 CONSTRUCTOR_APPEND_ELT (v1
, field
,
890 build_int_cstu (get_gcov_unsigned_t (), n_fns
));
893 field
= build_decl (BUILTINS_LOCATION
,
894 FIELD_DECL
, NULL_TREE
, fn_info_ptr_type
);
895 TREE_CHAIN (field
) = fields
;
897 CONSTRUCTOR_APPEND_ELT (v1
, field
, fn_info_value
);
900 field
= build_decl (BUILTINS_LOCATION
,
901 FIELD_DECL
, NULL_TREE
, get_gcov_unsigned_t ());
902 TREE_CHAIN (field
) = fields
;
904 CONSTRUCTOR_APPEND_ELT (v1
, field
,
905 build_int_cstu (get_gcov_unsigned_t (),
909 ctr_info_type
= build_ctr_info_type ();
910 ctr_info_ary_type
= build_index_type (build_int_cst (NULL_TREE
,
912 ctr_info_ary_type
= build_array_type (ctr_info_type
, ctr_info_ary_type
);
914 for (ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
915 if (prg_ctr_mask
& (1 << ix
))
916 CONSTRUCTOR_APPEND_ELT (v2
, NULL_TREE
,
917 build_ctr_info_value (ix
, ctr_info_type
));
918 ctr_info_value
= build_constructor (ctr_info_ary_type
, v2
);
920 field
= build_decl (BUILTINS_LOCATION
,
921 FIELD_DECL
, NULL_TREE
, ctr_info_ary_type
);
922 TREE_CHAIN (field
) = fields
;
924 CONSTRUCTOR_APPEND_ELT (v1
, field
, ctr_info_value
);
926 finish_builtin_struct (type
, "__gcov_info", fields
, NULL_TREE
);
928 return build_constructor (type
, v1
);
931 /* Write out the structure which libgcov uses to locate all the
932 counters. The structures used here must match those defined in
933 gcov-io.h. Write out the constructor to call __gcov_init. */
936 create_coverage (void)
938 tree gcov_info
, gcov_init
, body
, t
;
941 no_coverage
= 1; /* Disable any further coverage. */
946 t
= build_gcov_info ();
948 gcov_info
= build_decl (BUILTINS_LOCATION
,
949 VAR_DECL
, NULL_TREE
, TREE_TYPE (t
));
950 TREE_STATIC (gcov_info
) = 1;
951 ASM_GENERATE_INTERNAL_LABEL (name_buf
, "LPBX", 0);
952 DECL_NAME (gcov_info
) = get_identifier (name_buf
);
953 DECL_INITIAL (gcov_info
) = t
;
955 /* Build structure. */
956 varpool_finalize_decl (gcov_info
);
958 /* Build a decl for __gcov_init. */
959 t
= build_pointer_type (TREE_TYPE (gcov_info
));
960 t
= build_function_type_list (void_type_node
, t
, NULL
);
961 t
= build_decl (BUILTINS_LOCATION
,
962 FUNCTION_DECL
, get_identifier ("__gcov_init"), t
);
964 DECL_EXTERNAL (t
) = 1;
965 DECL_ASSEMBLER_NAME (t
); /* Initialize assembler name so we can stream out. */
968 /* Generate a call to __gcov_init(&gcov_info). */
970 t
= build_fold_addr_expr (gcov_info
);
971 t
= build_call_expr (gcov_init
, 1, t
);
972 append_to_statement_list (t
, &body
);
974 /* Generate a constructor to run it. */
975 cgraph_build_static_cdtor ('I', body
, DEFAULT_INIT_PRIORITY
);
978 /* Perform file-level initialization. Read in data file, generate name
982 coverage_init (const char *filename
)
984 int len
= strlen (filename
);
985 /* + 1 for extra '/', in case prefix doesn't end with /. */
988 if (profile_data_prefix
== 0 && filename
[0] != '/')
989 profile_data_prefix
= getpwd ();
991 prefix_len
= (profile_data_prefix
) ? strlen (profile_data_prefix
) + 1 : 0;
993 /* Name of da file. */
994 da_file_name
= XNEWVEC (char, len
+ strlen (GCOV_DATA_SUFFIX
)
997 if (profile_data_prefix
)
999 strcpy (da_file_name
, profile_data_prefix
);
1000 da_file_name
[prefix_len
- 1] = '/';
1001 da_file_name
[prefix_len
] = 0;
1004 da_file_name
[0] = 0;
1005 strcat (da_file_name
, filename
);
1006 strcat (da_file_name
, GCOV_DATA_SUFFIX
);
1008 /* Name of bbg file. */
1009 bbg_file_name
= XNEWVEC (char, len
+ strlen (GCOV_NOTE_SUFFIX
) + 1);
1010 strcpy (bbg_file_name
, filename
);
1011 strcat (bbg_file_name
, GCOV_NOTE_SUFFIX
);
1013 if (flag_profile_use
)
1014 read_counts_file ();
1017 /* Performs file-level cleanup. Close graph file, generate coverage
1018 variables and constructor. */
1021 coverage_finish (void)
1024 if (bbg_file_opened
)
1026 int error
= gcov_close ();
1029 unlink (bbg_file_name
);
1031 /* Only remove the da file, if we cannot stamp it. If we can
1032 stamp it, libgcov will DTRT. */
1033 unlink (da_file_name
);
1037 #include "gt-coverage.h"