1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
30 #include "coretypes.h"
32 #include "libgcc_tm.h"
35 #if defined(inhibit_libc)
36 #define IN_LIBGCOV (-1)
40 #define GCOV_LINKAGE /* nothing */
45 #if defined(inhibit_libc)
46 /* If libc and its header files are not available, provide dummy functions. */
49 void __gcov_init (struct gcov_info
*p
__attribute__ ((unused
))) {}
50 void __gcov_flush (void) {}
54 void __gcov_reset (void) {}
58 void __gcov_dump (void) {}
61 #ifdef L_gcov_merge_add
62 void __gcov_merge_add (gcov_type
*counters
__attribute__ ((unused
)),
63 unsigned n_counters
__attribute__ ((unused
))) {}
66 #ifdef L_gcov_merge_single
67 void __gcov_merge_single (gcov_type
*counters
__attribute__ ((unused
)),
68 unsigned n_counters
__attribute__ ((unused
))) {}
71 #ifdef L_gcov_merge_delta
72 void __gcov_merge_delta (gcov_type
*counters
__attribute__ ((unused
)),
73 unsigned n_counters
__attribute__ ((unused
))) {}
85 extern void gcov_clear (void) ATTRIBUTE_HIDDEN
;
86 extern void gcov_exit (void) ATTRIBUTE_HIDDEN
;
87 extern int gcov_dump_complete ATTRIBUTE_HIDDEN
;
94 struct gcov_fn_buffer
*next
;
96 struct gcov_fn_info info
;
97 /* note gcov_fn_info ends in a trailing array. */
100 struct gcov_summary_buffer
102 struct gcov_summary_buffer
*next
;
103 struct gcov_summary summary
;
106 /* Chain of per-object gcov structures. */
107 static struct gcov_info
*gcov_list
;
109 /* Size of the longest file name. */
110 static size_t gcov_max_filename
= 0;
112 /* Flag when the profile has already been dumped via __gcov_dump(). */
113 int gcov_dump_complete
= 0;
115 /* Make sure path component of the given FILENAME exists, create
116 missing directories. FILENAME must be writable.
117 Returns zero on success, or -1 if an error occurred. */
120 create_file_directory (char *filename
)
122 #if !defined(TARGET_POSIX_IO) && !defined(_WIN32)
130 if (HAS_DRIVE_SPEC(s
))
132 if (IS_DIR_SEPARATOR(*s
))
134 for (; *s
!= '\0'; s
++)
135 if (IS_DIR_SEPARATOR(*s
))
140 /* Try to make directory if it doesn't already exist. */
141 if (access (filename
, F_OK
) == -1
142 #ifdef TARGET_POSIX_IO
143 && mkdir (filename
, 0755) == -1
145 && mkdir (filename
) == -1
147 /* The directory might have been made by another process. */
150 fprintf (stderr
, "profiling:%s:Cannot create directory\n",
162 static struct gcov_fn_buffer
*
163 free_fn_data (const struct gcov_info
*gi_ptr
, struct gcov_fn_buffer
*buffer
,
166 struct gcov_fn_buffer
*next
;
167 unsigned ix
, n_ctr
= 0;
173 for (ix
= 0; ix
!= limit
; ix
++)
174 if (gi_ptr
->merge
[ix
])
175 free (buffer
->info
.ctrs
[n_ctr
++].values
);
180 static struct gcov_fn_buffer
**
181 buffer_fn_data (const char *filename
, const struct gcov_info
*gi_ptr
,
182 struct gcov_fn_buffer
**end_ptr
, unsigned fn_ix
)
184 unsigned n_ctrs
= 0, ix
= 0;
185 struct gcov_fn_buffer
*fn_buffer
;
188 for (ix
= GCOV_COUNTERS
; ix
--;)
189 if (gi_ptr
->merge
[ix
])
192 len
= sizeof (*fn_buffer
) + sizeof (fn_buffer
->info
.ctrs
[0]) * n_ctrs
;
193 fn_buffer
= (struct gcov_fn_buffer
*)malloc (len
);
199 fn_buffer
->fn_ix
= fn_ix
;
200 fn_buffer
->info
.ident
= gcov_read_unsigned ();
201 fn_buffer
->info
.lineno_checksum
= gcov_read_unsigned ();
202 fn_buffer
->info
.cfg_checksum
= gcov_read_unsigned ();
204 for (n_ctrs
= ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
206 gcov_unsigned_t length
;
209 if (!gi_ptr
->merge
[ix
])
212 if (gcov_read_unsigned () != GCOV_TAG_FOR_COUNTER (ix
))
218 length
= GCOV_TAG_COUNTER_NUM (gcov_read_unsigned ());
219 len
= length
* sizeof (gcov_type
);
220 values
= (gcov_type
*)malloc (len
);
224 fn_buffer
->info
.ctrs
[n_ctrs
].num
= length
;
225 fn_buffer
->info
.ctrs
[n_ctrs
].values
= values
;
228 *values
++ = gcov_read_counter ();
232 *end_ptr
= fn_buffer
;
233 return &fn_buffer
->next
;
236 fprintf (stderr
, "profiling:%s:Function %u %s %u \n", filename
, fn_ix
,
237 len
? "cannot allocate" : "counter mismatch", len
? len
: ix
);
239 return (struct gcov_fn_buffer
**)free_fn_data (gi_ptr
, fn_buffer
, ix
);
242 /* Add an unsigned value to the current crc */
244 static gcov_unsigned_t
245 crc32_unsigned (gcov_unsigned_t crc32
, gcov_unsigned_t value
)
249 for (ix
= 32; ix
--; value
<<= 1)
253 feedback
= (value
^ crc32
) & 0x80000000 ? 0x04c11db7 : 0;
261 /* Check if VERSION of the info block PTR matches libgcov one.
262 Return 1 on success, or zero in case of versions mismatch.
263 If FILENAME is not NULL, its value used for reporting purposes
264 instead of value from the info block. */
267 gcov_version (struct gcov_info
*ptr
, gcov_unsigned_t version
,
268 const char *filename
)
270 if (version
!= GCOV_VERSION
)
274 GCOV_UNSIGNED2STRING (v
, version
);
275 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
278 "profiling:%s:Version mismatch - expected %.4s got %.4s\n",
279 filename
? filename
: ptr
->filename
, e
, v
);
285 /* Insert counter VALUE into HISTOGRAM. */
288 gcov_histogram_insert(gcov_bucket_type
*histogram
, gcov_type value
)
292 i
= gcov_histo_index(value
);
293 histogram
[i
].num_counters
++;
294 histogram
[i
].cum_value
+= value
;
295 if (value
< histogram
[i
].min_value
)
296 histogram
[i
].min_value
= value
;
299 /* Computes a histogram of the arc counters to place in the summary SUM. */
302 gcov_compute_histogram (struct gcov_summary
*sum
)
304 struct gcov_info
*gi_ptr
;
305 const struct gcov_fn_info
*gfi_ptr
;
306 const struct gcov_ctr_info
*ci_ptr
;
307 struct gcov_ctr_summary
*cs_ptr
;
308 unsigned t_ix
, f_ix
, ctr_info_ix
, ix
;
311 /* This currently only applies to arc counters. */
312 t_ix
= GCOV_COUNTER_ARCS
;
314 /* First check if there are any counts recorded for this counter. */
315 cs_ptr
= &(sum
->ctrs
[t_ix
]);
319 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
321 cs_ptr
->histogram
[h_ix
].num_counters
= 0;
322 cs_ptr
->histogram
[h_ix
].min_value
= cs_ptr
->run_max
;
323 cs_ptr
->histogram
[h_ix
].cum_value
= 0;
326 /* Walk through all the per-object structures and record each of
327 the count values in histogram. */
328 for (gi_ptr
= gcov_list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
330 if (!gi_ptr
->merge
[t_ix
])
333 /* Find the appropriate index into the gcov_ctr_info array
334 for the counter we are currently working on based on the
335 existence of the merge function pointer for this object. */
336 for (ix
= 0, ctr_info_ix
= 0; ix
< t_ix
; ix
++)
338 if (gi_ptr
->merge
[ix
])
341 for (f_ix
= 0; f_ix
!= gi_ptr
->n_functions
; f_ix
++)
343 gfi_ptr
= gi_ptr
->functions
[f_ix
];
345 if (!gfi_ptr
|| gfi_ptr
->key
!= gi_ptr
)
348 ci_ptr
= &gfi_ptr
->ctrs
[ctr_info_ix
];
349 for (ix
= 0; ix
< ci_ptr
->num
; ix
++)
350 gcov_histogram_insert (cs_ptr
->histogram
, ci_ptr
->values
[ix
]);
355 /* Dump the coverage counts. We merge with existing counts when
356 possible, to avoid growing the .da files ad infinitum. We use this
357 program's checksum to make sure we only accumulate whole program
358 statistics to the correct summary. An object file might be embedded
359 in two separate programs, and we must keep the two program
360 summaries separate. */
365 struct gcov_info
*gi_ptr
;
366 const struct gcov_fn_info
*gfi_ptr
;
367 struct gcov_summary this_prg
; /* summary for program. */
369 struct gcov_summary all_prg
; /* summary for all instances of program. */
371 struct gcov_ctr_summary
*cs_ptr
;
372 const struct gcov_ctr_info
*ci_ptr
;
375 gcov_unsigned_t c_num
;
376 const char *gcov_prefix
;
377 int gcov_prefix_strip
= 0;
378 size_t prefix_length
;
379 char *gi_filename
, *gi_filename_up
;
380 gcov_unsigned_t crc32
= 0;
382 /* Prevent the counters from being dumped a second time on exit when the
383 application already wrote out the profile using __gcov_dump(). */
384 if (gcov_dump_complete
)
388 memset (&all_prg
, 0, sizeof (all_prg
));
390 /* Find the totals for this execution. */
391 memset (&this_prg
, 0, sizeof (this_prg
));
392 for (gi_ptr
= gcov_list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
394 crc32
= crc32_unsigned (crc32
, gi_ptr
->stamp
);
395 crc32
= crc32_unsigned (crc32
, gi_ptr
->n_functions
);
397 for (f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
; f_ix
++)
399 gfi_ptr
= gi_ptr
->functions
[f_ix
];
401 if (gfi_ptr
&& gfi_ptr
->key
!= gi_ptr
)
404 crc32
= crc32_unsigned (crc32
, gfi_ptr
? gfi_ptr
->cfg_checksum
: 0);
405 crc32
= crc32_unsigned (crc32
,
406 gfi_ptr
? gfi_ptr
->lineno_checksum
: 0);
410 ci_ptr
= gfi_ptr
->ctrs
;
411 for (t_ix
= 0; t_ix
!= GCOV_COUNTERS_SUMMABLE
; t_ix
++)
413 if (!gi_ptr
->merge
[t_ix
])
416 cs_ptr
= &this_prg
.ctrs
[t_ix
];
417 cs_ptr
->num
+= ci_ptr
->num
;
418 crc32
= crc32_unsigned (crc32
, ci_ptr
->num
);
420 for (c_num
= 0; c_num
< ci_ptr
->num
; c_num
++)
422 cs_ptr
->sum_all
+= ci_ptr
->values
[c_num
];
423 if (cs_ptr
->run_max
< ci_ptr
->values
[c_num
])
424 cs_ptr
->run_max
= ci_ptr
->values
[c_num
];
430 gcov_compute_histogram (&this_prg
);
433 /* Check if the level of dirs to strip off specified. */
434 char *tmp
= getenv("GCOV_PREFIX_STRIP");
437 gcov_prefix_strip
= atoi (tmp
);
438 /* Do not consider negative values. */
439 if (gcov_prefix_strip
< 0)
440 gcov_prefix_strip
= 0;
444 /* Get file name relocation prefix. Non-absolute values are ignored. */
445 gcov_prefix
= getenv("GCOV_PREFIX");
448 prefix_length
= strlen(gcov_prefix
);
450 /* Remove an unnecessary trailing '/' */
451 if (IS_DIR_SEPARATOR (gcov_prefix
[prefix_length
- 1]))
457 /* If no prefix was specified and a prefix stip, then we assume
459 if (gcov_prefix_strip
!= 0 && prefix_length
== 0)
464 /* Allocate and initialize the filename scratch space plus one. */
465 gi_filename
= (char *) alloca (prefix_length
+ gcov_max_filename
+ 2);
467 memcpy (gi_filename
, gcov_prefix
, prefix_length
);
468 gi_filename_up
= gi_filename
+ prefix_length
;
470 /* Now merge each file. */
471 for (gi_ptr
= gcov_list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
474 struct gcov_summary prg
; /* summary for this object over all
476 struct gcov_ctr_summary
*cs_prg
, *cs_tprg
;
478 struct gcov_ctr_summary
*cs_all
;
481 gcov_unsigned_t tag
, length
;
482 gcov_position_t summary_pos
= 0;
483 gcov_position_t eof_pos
= 0;
484 const char *fname
, *s
;
485 struct gcov_fn_buffer
*fn_buffer
= 0;
486 struct gcov_fn_buffer
**fn_tail
= &fn_buffer
;
487 struct gcov_summary_buffer
*next_sum_buffer
, *sum_buffer
= 0;
488 struct gcov_summary_buffer
**sum_tail
= &sum_buffer
;
490 fname
= gi_ptr
->filename
;
492 /* Avoid to add multiple drive letters into combined path. */
493 if (prefix_length
!= 0 && HAS_DRIVE_SPEC(fname
))
496 /* Build relocated filename, stripping off leading
497 directories from the initial filename if requested. */
498 if (gcov_prefix_strip
> 0)
502 if (IS_DIR_SEPARATOR(*s
))
505 /* Skip selected directory levels. */
506 for (; (*s
!= '\0') && (level
< gcov_prefix_strip
); s
++)
507 if (IS_DIR_SEPARATOR(*s
))
514 /* Update complete filename with stripped original. */
515 if (prefix_length
!= 0 && !IS_DIR_SEPARATOR (*fname
))
517 /* If prefix is given, add directory separator. */
518 strcpy (gi_filename_up
, "/");
519 strcpy (gi_filename_up
+ 1, fname
);
522 strcpy (gi_filename_up
, fname
);
524 if (!gcov_open (gi_filename
))
526 /* Open failed likely due to missed directory.
527 Create directory and retry to open file. */
528 if (create_file_directory (gi_filename
))
530 fprintf (stderr
, "profiling:%s:Skip\n", gi_filename
);
533 if (!gcov_open (gi_filename
))
535 fprintf (stderr
, "profiling:%s:Cannot open\n", gi_filename
);
540 tag
= gcov_read_unsigned ();
543 /* Merge data from file. */
544 if (tag
!= GCOV_DATA_MAGIC
)
546 fprintf (stderr
, "profiling:%s:Not a gcov data file\n",
550 length
= gcov_read_unsigned ();
551 if (!gcov_version (gi_ptr
, length
, gi_filename
))
554 length
= gcov_read_unsigned ();
555 if (length
!= gi_ptr
->stamp
)
556 /* Read from a different compilation. Overwrite the file. */
559 /* Look for program summary. */
562 struct gcov_summary tmp
;
564 eof_pos
= gcov_position ();
565 tag
= gcov_read_unsigned ();
566 if (tag
!= GCOV_TAG_PROGRAM_SUMMARY
)
570 length
= gcov_read_unsigned ();
571 gcov_read_summary (&tmp
);
572 if ((error
= gcov_is_error ()))
576 /* Save all summaries after the one that will be
577 merged into below. These will need to be rewritten
578 as histogram merging may change the number of non-zero
579 histogram entries that will be emitted, and thus the
580 size of the merged summary. */
581 (*sum_tail
) = (struct gcov_summary_buffer
*)
582 malloc (sizeof(struct gcov_summary_buffer
));
583 (*sum_tail
)->summary
= tmp
;
584 (*sum_tail
)->next
= 0;
585 sum_tail
= &((*sum_tail
)->next
);
588 if (tmp
.checksum
!= crc32
)
591 for (t_ix
= 0; t_ix
!= GCOV_COUNTERS_SUMMABLE
; t_ix
++)
592 if (tmp
.ctrs
[t_ix
].num
!= this_prg
.ctrs
[t_ix
].num
)
595 summary_pos
= eof_pos
;
600 /* Merge execution counts for each function. */
601 for (f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
;
602 f_ix
++, tag
= gcov_read_unsigned ())
604 gfi_ptr
= gi_ptr
->functions
[f_ix
];
606 if (tag
!= GCOV_TAG_FUNCTION
)
609 length
= gcov_read_unsigned ();
611 /* This function did not appear in the other program.
612 We have nothing to merge. */
615 if (length
!= GCOV_TAG_FUNCTION_LENGTH
)
618 if (!gfi_ptr
|| gfi_ptr
->key
!= gi_ptr
)
620 /* This function appears in the other program. We
621 need to buffer the information in order to write
622 it back out -- we'll be inserting data before
623 this point, so cannot simply keep the data in the
625 fn_tail
= buffer_fn_data (gi_filename
,
626 gi_ptr
, fn_tail
, f_ix
);
632 length
= gcov_read_unsigned ();
633 if (length
!= gfi_ptr
->ident
)
636 length
= gcov_read_unsigned ();
637 if (length
!= gfi_ptr
->lineno_checksum
)
640 length
= gcov_read_unsigned ();
641 if (length
!= gfi_ptr
->cfg_checksum
)
644 ci_ptr
= gfi_ptr
->ctrs
;
645 for (t_ix
= 0; t_ix
< GCOV_COUNTERS
; t_ix
++)
647 gcov_merge_fn merge
= gi_ptr
->merge
[t_ix
];
652 tag
= gcov_read_unsigned ();
653 length
= gcov_read_unsigned ();
654 if (tag
!= GCOV_TAG_FOR_COUNTER (t_ix
)
655 || length
!= GCOV_TAG_COUNTER_LENGTH (ci_ptr
->num
))
657 (*merge
) (ci_ptr
->values
, ci_ptr
->num
);
660 if ((error
= gcov_is_error ()))
667 fprintf (stderr
, "profiling:%s:Merge mismatch for %s %u\n",
668 gi_filename
, f_ix
>= 0 ? "function" : "summary",
669 f_ix
< 0 ? -1 - f_ix
: f_ix
);
676 fprintf (stderr
, "profiling:%s:%s merging\n", gi_filename
,
677 error
< 0 ? "Overflow": "Error");
685 memset (&prg
, 0, sizeof (prg
));
686 summary_pos
= eof_pos
;
689 /* Merge the summaries. */
690 for (t_ix
= 0; t_ix
< GCOV_COUNTERS_SUMMABLE
; t_ix
++)
692 cs_prg
= &prg
.ctrs
[t_ix
];
693 cs_tprg
= &this_prg
.ctrs
[t_ix
];
695 if (gi_ptr
->merge
[t_ix
])
698 cs_prg
->num
= cs_tprg
->num
;
699 cs_prg
->sum_all
+= cs_tprg
->sum_all
;
700 if (cs_prg
->run_max
< cs_tprg
->run_max
)
701 cs_prg
->run_max
= cs_tprg
->run_max
;
702 cs_prg
->sum_max
+= cs_tprg
->run_max
;
703 if (cs_prg
->runs
== 1)
704 memcpy (cs_prg
->histogram
, cs_tprg
->histogram
,
705 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
707 gcov_histogram_merge (cs_prg
->histogram
, cs_tprg
->histogram
);
709 else if (cs_prg
->runs
)
713 cs_all
= &all_prg
.ctrs
[t_ix
];
714 if (!cs_all
->runs
&& cs_prg
->runs
)
716 cs_all
->num
= cs_prg
->num
;
717 cs_all
->runs
= cs_prg
->runs
;
718 cs_all
->sum_all
= cs_prg
->sum_all
;
719 cs_all
->run_max
= cs_prg
->run_max
;
720 cs_all
->sum_max
= cs_prg
->sum_max
;
722 else if (!all_prg
.checksum
723 /* Don't compare the histograms, which may have slight
724 variations depending on the order they were updated
725 due to the truncating integer divides used in the
727 && (cs_all
->num
!= cs_prg
->num
728 || cs_all
->runs
!= cs_prg
->runs
729 || cs_all
->sum_all
!= cs_prg
->sum_all
730 || cs_all
->run_max
!= cs_prg
->run_max
731 || cs_all
->sum_max
!= cs_prg
->sum_max
))
734 "profiling:%s:Data file mismatch - some data files may "
735 "have been concurrently updated without locking support\n",
737 all_prg
.checksum
= ~0u;
742 prg
.checksum
= crc32
;
744 /* Write out the data. */
747 gcov_write_tag_length (GCOV_DATA_MAGIC
, GCOV_VERSION
);
748 gcov_write_unsigned (gi_ptr
->stamp
);
752 gcov_seek (summary_pos
);
754 /* Generate whole program statistics. */
755 gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY
, &prg
);
757 /* Rewrite all the summaries that were after the summary we merged
758 into. This is necessary as the merged summary may have a different
759 size due to the number of non-zero histogram entries changing after
764 gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY
, &sum_buffer
->summary
);
765 next_sum_buffer
= sum_buffer
->next
;
767 sum_buffer
= next_sum_buffer
;
770 /* Write execution counts for each function. */
771 for (f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
; f_ix
++)
773 unsigned buffered
= 0;
775 if (fn_buffer
&& fn_buffer
->fn_ix
== (unsigned)f_ix
)
777 /* Buffered data from another program. */
779 gfi_ptr
= &fn_buffer
->info
;
780 length
= GCOV_TAG_FUNCTION_LENGTH
;
784 gfi_ptr
= gi_ptr
->functions
[f_ix
];
785 if (gfi_ptr
&& gfi_ptr
->key
== gi_ptr
)
786 length
= GCOV_TAG_FUNCTION_LENGTH
;
791 gcov_write_tag_length (GCOV_TAG_FUNCTION
, length
);
795 gcov_write_unsigned (gfi_ptr
->ident
);
796 gcov_write_unsigned (gfi_ptr
->lineno_checksum
);
797 gcov_write_unsigned (gfi_ptr
->cfg_checksum
);
799 ci_ptr
= gfi_ptr
->ctrs
;
800 for (t_ix
= 0; t_ix
< GCOV_COUNTERS
; t_ix
++)
802 if (!gi_ptr
->merge
[t_ix
])
805 n_counts
= ci_ptr
->num
;
806 gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix
),
807 GCOV_TAG_COUNTER_LENGTH (n_counts
));
808 gcov_type
*c_ptr
= ci_ptr
->values
;
810 gcov_write_counter (*c_ptr
++);
814 fn_buffer
= free_fn_data (gi_ptr
, fn_buffer
, GCOV_COUNTERS
);
817 gcov_write_unsigned (0);
821 fn_buffer
= free_fn_data (gi_ptr
, fn_buffer
, GCOV_COUNTERS
);
823 if ((error
= gcov_close ()))
824 fprintf (stderr
, error
< 0 ?
825 "profiling:%s:Overflow writing\n" :
826 "profiling:%s:Error writing\n",
831 /* Reset all counters to zero. */
836 const struct gcov_info
*gi_ptr
;
838 for (gi_ptr
= gcov_list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
842 for (f_ix
= 0; f_ix
< gi_ptr
->n_functions
; f_ix
++)
845 const struct gcov_fn_info
*gfi_ptr
= gi_ptr
->functions
[f_ix
];
847 if (!gfi_ptr
|| gfi_ptr
->key
!= gi_ptr
)
849 const struct gcov_ctr_info
*ci_ptr
= gfi_ptr
->ctrs
;
850 for (t_ix
= 0; t_ix
!= GCOV_COUNTERS
; t_ix
++)
852 if (!gi_ptr
->merge
[t_ix
])
855 memset (ci_ptr
->values
, 0, sizeof (gcov_type
) * ci_ptr
->num
);
862 /* Add a new object file onto the bb chain. Invoked automatically
863 when running an object file's global ctors. */
866 __gcov_init (struct gcov_info
*info
)
868 if (!info
->version
|| !info
->n_functions
)
870 if (gcov_version (info
, info
->version
, 0))
872 size_t filename_length
= strlen(info
->filename
);
874 /* Refresh the longest file name information */
875 if (filename_length
> gcov_max_filename
)
876 gcov_max_filename
= filename_length
;
881 info
->next
= gcov_list
;
887 #ifdef __GTHREAD_MUTEX_INIT
888 ATTRIBUTE_HIDDEN __gthread_mutex_t __gcov_flush_mx
= __GTHREAD_MUTEX_INIT
;
889 #define init_mx_once()
891 __gthread_mutex_t __gcov_flush_mx ATTRIBUTE_HIDDEN
;
896 __GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_flush_mx
);
901 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
902 __gthread_once (&once
, init_mx
);
906 /* Called before fork or exec - write out profile information gathered so
907 far and reset it to zero. This avoids duplication or loss of the
908 profile information gathered so far. */
914 __gthread_mutex_lock (&__gcov_flush_mx
);
919 __gthread_mutex_unlock (&__gcov_flush_mx
);
926 /* Function that can be called from application to reset counters to zero,
927 in order to collect profile in region of interest. */
933 /* Re-enable dumping to support collecting profile in multiple regions
935 gcov_dump_complete
= 0;
938 #endif /* L_gcov_reset */
942 /* Function that can be called from application to write profile collected
943 so far, in order to collect profile in region of interest. */
949 /* Prevent profile from being dumped a second time on application exit. */
950 gcov_dump_complete
= 1;
953 #endif /* L_gcov_dump */
955 #ifdef L_gcov_merge_add
956 /* The profile merging function that just adds the counters. It is given
957 an array COUNTERS of N_COUNTERS old counters and it reads the same number
958 of counters from the gcov file. */
960 __gcov_merge_add (gcov_type
*counters
, unsigned n_counters
)
962 for (; n_counters
; counters
++, n_counters
--)
963 *counters
+= gcov_read_counter ();
965 #endif /* L_gcov_merge_add */
967 #ifdef L_gcov_merge_ior
968 /* The profile merging function that just adds the counters. It is given
969 an array COUNTERS of N_COUNTERS old counters and it reads the same number
970 of counters from the gcov file. */
972 __gcov_merge_ior (gcov_type
*counters
, unsigned n_counters
)
974 for (; n_counters
; counters
++, n_counters
--)
975 *counters
|= gcov_read_counter ();
979 #ifdef L_gcov_merge_single
980 /* The profile merging function for choosing the most common value.
981 It is given an array COUNTERS of N_COUNTERS old counters and it
982 reads the same number of counters from the gcov file. The counters
983 are split into 3-tuples where the members of the tuple have
986 -- the stored candidate on the most common value of the measured entity
988 -- total number of evaluations of the value */
990 __gcov_merge_single (gcov_type
*counters
, unsigned n_counters
)
992 unsigned i
, n_measures
;
993 gcov_type value
, counter
, all
;
995 gcc_assert (!(n_counters
% 3));
996 n_measures
= n_counters
/ 3;
997 for (i
= 0; i
< n_measures
; i
++, counters
+= 3)
999 value
= gcov_read_counter ();
1000 counter
= gcov_read_counter ();
1001 all
= gcov_read_counter ();
1003 if (counters
[0] == value
)
1004 counters
[1] += counter
;
1005 else if (counter
> counters
[1])
1007 counters
[0] = value
;
1008 counters
[1] = counter
- counters
[1];
1011 counters
[1] -= counter
;
1015 #endif /* L_gcov_merge_single */
1017 #ifdef L_gcov_merge_delta
1018 /* The profile merging function for choosing the most common
1019 difference between two consecutive evaluations of the value. It is
1020 given an array COUNTERS of N_COUNTERS old counters and it reads the
1021 same number of counters from the gcov file. The counters are split
1022 into 4-tuples where the members of the tuple have meanings:
1024 -- the last value of the measured entity
1025 -- the stored candidate on the most common difference
1027 -- total number of evaluations of the value */
1029 __gcov_merge_delta (gcov_type
*counters
, unsigned n_counters
)
1031 unsigned i
, n_measures
;
1032 gcov_type value
, counter
, all
;
1034 gcc_assert (!(n_counters
% 4));
1035 n_measures
= n_counters
/ 4;
1036 for (i
= 0; i
< n_measures
; i
++, counters
+= 4)
1038 /* last = */ gcov_read_counter ();
1039 value
= gcov_read_counter ();
1040 counter
= gcov_read_counter ();
1041 all
= gcov_read_counter ();
1043 if (counters
[1] == value
)
1044 counters
[2] += counter
;
1045 else if (counter
> counters
[2])
1047 counters
[1] = value
;
1048 counters
[2] = counter
- counters
[2];
1051 counters
[2] -= counter
;
1055 #endif /* L_gcov_merge_delta */
1057 #ifdef L_gcov_interval_profiler
1058 /* If VALUE is in interval <START, START + STEPS - 1>, then increases the
1059 corresponding counter in COUNTERS. If the VALUE is above or below
1060 the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased
1064 __gcov_interval_profiler (gcov_type
*counters
, gcov_type value
,
1065 int start
, unsigned steps
)
1067 gcov_type delta
= value
- start
;
1069 counters
[steps
+ 1]++;
1070 else if (delta
>= steps
)
1077 #ifdef L_gcov_pow2_profiler
1078 /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
1079 COUNTERS[0] is incremented. */
1082 __gcov_pow2_profiler (gcov_type
*counters
, gcov_type value
)
1084 if (value
& (value
- 1))
1091 /* Tries to determine the most common value among its inputs. Checks if the
1092 value stored in COUNTERS[0] matches VALUE. If this is the case, COUNTERS[1]
1093 is incremented. If this is not the case and COUNTERS[1] is not zero,
1094 COUNTERS[1] is decremented. Otherwise COUNTERS[1] is set to one and
1095 VALUE is stored to COUNTERS[0]. This algorithm guarantees that if this
1096 function is called more than 50% of the time with one value, this value
1097 will be in COUNTERS[0] in the end.
1099 In any case, COUNTERS[2] is incremented. */
1102 __gcov_one_value_profiler_body (gcov_type
*counters
, gcov_type value
)
1104 if (value
== counters
[0])
1106 else if (counters
[1] == 0)
1109 counters
[0] = value
;
1116 #ifdef L_gcov_one_value_profiler
1118 __gcov_one_value_profiler (gcov_type
*counters
, gcov_type value
)
1120 __gcov_one_value_profiler_body (counters
, value
);
1124 #ifdef L_gcov_indirect_call_profiler
1126 /* By default, the C++ compiler will use function addresses in the
1127 vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero
1128 tells the compiler to use function descriptors instead. The value
1129 of this macro says how many words wide the descriptor is (normally 2),
1130 but it may be dependent on target flags. Since we do not have access
1131 to the target flags here we just check to see if it is set and use
1132 that to set VTABLE_USES_DESCRIPTORS to 0 or 1.
1134 It is assumed that the address of a function descriptor may be treated
1135 as a pointer to a function. */
1137 #ifdef TARGET_VTABLE_USES_DESCRIPTORS
1138 #define VTABLE_USES_DESCRIPTORS 1
1140 #define VTABLE_USES_DESCRIPTORS 0
1143 /* Tries to determine the most common value among its inputs. */
1145 __gcov_indirect_call_profiler (gcov_type
* counter
, gcov_type value
,
1146 void* cur_func
, void* callee_func
)
1148 /* If the C++ virtual tables contain function descriptors then one
1149 function may have multiple descriptors and we need to dereference
1150 the descriptors to see if they point to the same function. */
1151 if (cur_func
== callee_func
1152 || (VTABLE_USES_DESCRIPTORS
&& callee_func
1153 && *(void **) cur_func
== *(void **) callee_func
))
1154 __gcov_one_value_profiler_body (counter
, value
);
1159 #ifdef L_gcov_average_profiler
1160 /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
1164 __gcov_average_profiler (gcov_type
*counters
, gcov_type value
)
1166 counters
[0] += value
;
1171 #ifdef L_gcov_ior_profiler
1172 /* Bitwise-OR VALUE into COUNTER. */
1175 __gcov_ior_profiler (gcov_type
*counters
, gcov_type value
)
1182 /* A wrapper for the fork function. Flushes the accumulated profiling data, so
1183 that they are not counted twice. */
1189 extern __gthread_mutex_t __gcov_flush_mx
;
1193 __GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_flush_mx
);
1199 /* A wrapper for the execl function. Flushes the accumulated profiling data, so
1200 that they are not lost. */
1203 __gcov_execl (const char *path
, char *arg
, ...)
1215 while (va_arg (ap
, char *))
1219 args
= (char **) alloca (length
* sizeof (void *));
1221 for (i
= 1; i
< length
; i
++)
1222 args
[i
] = va_arg (aq
, char *);
1225 return execv (path
, args
);
1229 #ifdef L_gcov_execlp
1230 /* A wrapper for the execlp function. Flushes the accumulated profiling data, so
1231 that they are not lost. */
1234 __gcov_execlp (const char *path
, char *arg
, ...)
1246 while (va_arg (ap
, char *))
1250 args
= (char **) alloca (length
* sizeof (void *));
1252 for (i
= 1; i
< length
; i
++)
1253 args
[i
] = va_arg (aq
, char *);
1256 return execvp (path
, args
);
1260 #ifdef L_gcov_execle
1261 /* A wrapper for the execle function. Flushes the accumulated profiling data, so
1262 that they are not lost. */
1265 __gcov_execle (const char *path
, char *arg
, ...)
1278 while (va_arg (ap
, char *))
1282 args
= (char **) alloca (length
* sizeof (void *));
1284 for (i
= 1; i
< length
; i
++)
1285 args
[i
] = va_arg (aq
, char *);
1286 envp
= va_arg (aq
, char **);
1289 return execve (path
, args
, envp
);
1294 /* A wrapper for the execv function. Flushes the accumulated profiling data, so
1295 that they are not lost. */
1298 __gcov_execv (const char *path
, char *const argv
[])
1301 return execv (path
, argv
);
1305 #ifdef L_gcov_execvp
1306 /* A wrapper for the execvp function. Flushes the accumulated profiling data, so
1307 that they are not lost. */
1310 __gcov_execvp (const char *path
, char *const argv
[])
1313 return execvp (path
, argv
);
1317 #ifdef L_gcov_execve
1318 /* A wrapper for the execve function. Flushes the accumulated profiling data, so
1319 that they are not lost. */
1322 __gcov_execve (const char *path
, char *const argv
[], char *const envp
[])
1325 return execve (path
, argv
, envp
);
1328 #endif /* inhibit_libc */