1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989-2013 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
28 #include "coretypes.h"
30 #include "libgcc_tm.h"
33 #if defined(inhibit_libc)
34 #define IN_LIBGCOV (-1)
38 #define GCOV_LINKAGE /* nothing */
43 #if defined(inhibit_libc)
44 /* If libc and its header files are not available, provide dummy functions. */
47 void __gcov_init (struct gcov_info
*p
__attribute__ ((unused
))) {}
48 void __gcov_flush (void) {}
52 void __gcov_reset (void) {}
56 void __gcov_dump (void) {}
59 #ifdef L_gcov_merge_add
60 void __gcov_merge_add (gcov_type
*counters
__attribute__ ((unused
)),
61 unsigned n_counters
__attribute__ ((unused
))) {}
64 #ifdef L_gcov_merge_single
65 void __gcov_merge_single (gcov_type
*counters
__attribute__ ((unused
)),
66 unsigned n_counters
__attribute__ ((unused
))) {}
69 #ifdef L_gcov_merge_delta
70 void __gcov_merge_delta (gcov_type
*counters
__attribute__ ((unused
)),
71 unsigned n_counters
__attribute__ ((unused
))) {}
83 extern void gcov_clear (void) ATTRIBUTE_HIDDEN
;
84 extern void gcov_exit (void) ATTRIBUTE_HIDDEN
;
85 extern int gcov_dump_complete ATTRIBUTE_HIDDEN
;
92 struct gcov_fn_buffer
*next
;
94 struct gcov_fn_info info
;
95 /* note gcov_fn_info ends in a trailing array. */
98 struct gcov_summary_buffer
100 struct gcov_summary_buffer
*next
;
101 struct gcov_summary summary
;
104 /* Chain of per-object gcov structures. */
105 static struct gcov_info
*gcov_list
;
107 /* Size of the longest file name. */
108 static size_t gcov_max_filename
= 0;
110 /* Flag when the profile has already been dumped via __gcov_dump(). */
111 int gcov_dump_complete
= 0;
113 /* Make sure path component of the given FILENAME exists, create
114 missing directories. FILENAME must be writable.
115 Returns zero on success, or -1 if an error occurred. */
118 create_file_directory (char *filename
)
120 #if !defined(TARGET_POSIX_IO) && !defined(_WIN32)
128 if (HAS_DRIVE_SPEC(s
))
130 if (IS_DIR_SEPARATOR(*s
))
132 for (; *s
!= '\0'; s
++)
133 if (IS_DIR_SEPARATOR(*s
))
138 /* Try to make directory if it doesn't already exist. */
139 if (access (filename
, F_OK
) == -1
140 #ifdef TARGET_POSIX_IO
141 && mkdir (filename
, 0755) == -1
143 && mkdir (filename
) == -1
145 /* The directory might have been made by another process. */
148 fprintf (stderr
, "profiling:%s:Cannot create directory\n",
160 static struct gcov_fn_buffer
*
161 free_fn_data (const struct gcov_info
*gi_ptr
, struct gcov_fn_buffer
*buffer
,
164 struct gcov_fn_buffer
*next
;
165 unsigned ix
, n_ctr
= 0;
171 for (ix
= 0; ix
!= limit
; ix
++)
172 if (gi_ptr
->merge
[ix
])
173 free (buffer
->info
.ctrs
[n_ctr
++].values
);
178 static struct gcov_fn_buffer
**
179 buffer_fn_data (const char *filename
, const struct gcov_info
*gi_ptr
,
180 struct gcov_fn_buffer
**end_ptr
, unsigned fn_ix
)
182 unsigned n_ctrs
= 0, ix
= 0;
183 struct gcov_fn_buffer
*fn_buffer
;
186 for (ix
= GCOV_COUNTERS
; ix
--;)
187 if (gi_ptr
->merge
[ix
])
190 len
= sizeof (*fn_buffer
) + sizeof (fn_buffer
->info
.ctrs
[0]) * n_ctrs
;
191 fn_buffer
= (struct gcov_fn_buffer
*)malloc (len
);
197 fn_buffer
->fn_ix
= fn_ix
;
198 fn_buffer
->info
.ident
= gcov_read_unsigned ();
199 fn_buffer
->info
.lineno_checksum
= gcov_read_unsigned ();
200 fn_buffer
->info
.cfg_checksum
= gcov_read_unsigned ();
202 for (n_ctrs
= ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
204 gcov_unsigned_t length
;
207 if (!gi_ptr
->merge
[ix
])
210 if (gcov_read_unsigned () != GCOV_TAG_FOR_COUNTER (ix
))
216 length
= GCOV_TAG_COUNTER_NUM (gcov_read_unsigned ());
217 len
= length
* sizeof (gcov_type
);
218 values
= (gcov_type
*)malloc (len
);
222 fn_buffer
->info
.ctrs
[n_ctrs
].num
= length
;
223 fn_buffer
->info
.ctrs
[n_ctrs
].values
= values
;
226 *values
++ = gcov_read_counter ();
230 *end_ptr
= fn_buffer
;
231 return &fn_buffer
->next
;
234 fprintf (stderr
, "profiling:%s:Function %u %s %u \n", filename
, fn_ix
,
235 len
? "cannot allocate" : "counter mismatch", len
? len
: ix
);
237 return (struct gcov_fn_buffer
**)free_fn_data (gi_ptr
, fn_buffer
, ix
);
240 /* Add an unsigned value to the current crc */
242 static gcov_unsigned_t
243 crc32_unsigned (gcov_unsigned_t crc32
, gcov_unsigned_t value
)
247 for (ix
= 32; ix
--; value
<<= 1)
251 feedback
= (value
^ crc32
) & 0x80000000 ? 0x04c11db7 : 0;
259 /* Check if VERSION of the info block PTR matches libgcov one.
260 Return 1 on success, or zero in case of versions mismatch.
261 If FILENAME is not NULL, its value used for reporting purposes
262 instead of value from the info block. */
265 gcov_version (struct gcov_info
*ptr
, gcov_unsigned_t version
,
266 const char *filename
)
268 if (version
!= GCOV_VERSION
)
272 GCOV_UNSIGNED2STRING (v
, version
);
273 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
276 "profiling:%s:Version mismatch - expected %.4s got %.4s\n",
277 filename
? filename
: ptr
->filename
, e
, v
);
283 /* Insert counter VALUE into HISTOGRAM. */
286 gcov_histogram_insert(gcov_bucket_type
*histogram
, gcov_type value
)
290 i
= gcov_histo_index(value
);
291 histogram
[i
].num_counters
++;
292 histogram
[i
].cum_value
+= value
;
293 if (value
< histogram
[i
].min_value
)
294 histogram
[i
].min_value
= value
;
297 /* Computes a histogram of the arc counters to place in the summary SUM. */
300 gcov_compute_histogram (struct gcov_summary
*sum
)
302 struct gcov_info
*gi_ptr
;
303 const struct gcov_fn_info
*gfi_ptr
;
304 const struct gcov_ctr_info
*ci_ptr
;
305 struct gcov_ctr_summary
*cs_ptr
;
306 unsigned t_ix
, f_ix
, ctr_info_ix
, ix
;
309 /* This currently only applies to arc counters. */
310 t_ix
= GCOV_COUNTER_ARCS
;
312 /* First check if there are any counts recorded for this counter. */
313 cs_ptr
= &(sum
->ctrs
[t_ix
]);
317 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
319 cs_ptr
->histogram
[h_ix
].num_counters
= 0;
320 cs_ptr
->histogram
[h_ix
].min_value
= cs_ptr
->run_max
;
321 cs_ptr
->histogram
[h_ix
].cum_value
= 0;
324 /* Walk through all the per-object structures and record each of
325 the count values in histogram. */
326 for (gi_ptr
= gcov_list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
328 if (!gi_ptr
->merge
[t_ix
])
331 /* Find the appropriate index into the gcov_ctr_info array
332 for the counter we are currently working on based on the
333 existence of the merge function pointer for this object. */
334 for (ix
= 0, ctr_info_ix
= 0; ix
< t_ix
; ix
++)
336 if (gi_ptr
->merge
[ix
])
339 for (f_ix
= 0; f_ix
!= gi_ptr
->n_functions
; f_ix
++)
341 gfi_ptr
= gi_ptr
->functions
[f_ix
];
343 if (!gfi_ptr
|| gfi_ptr
->key
!= gi_ptr
)
346 ci_ptr
= &gfi_ptr
->ctrs
[ctr_info_ix
];
347 for (ix
= 0; ix
< ci_ptr
->num
; ix
++)
348 gcov_histogram_insert (cs_ptr
->histogram
, ci_ptr
->values
[ix
]);
353 /* Dump the coverage counts. We merge with existing counts when
354 possible, to avoid growing the .da files ad infinitum. We use this
355 program's checksum to make sure we only accumulate whole program
356 statistics to the correct summary. An object file might be embedded
357 in two separate programs, and we must keep the two program
358 summaries separate. */
363 struct gcov_info
*gi_ptr
;
364 const struct gcov_fn_info
*gfi_ptr
;
365 struct gcov_summary this_prg
; /* summary for program. */
367 struct gcov_summary all_prg
; /* summary for all instances of program. */
369 struct gcov_ctr_summary
*cs_ptr
;
370 const struct gcov_ctr_info
*ci_ptr
;
373 gcov_unsigned_t c_num
;
374 const char *gcov_prefix
;
375 int gcov_prefix_strip
= 0;
376 size_t prefix_length
;
377 char *gi_filename
, *gi_filename_up
;
378 gcov_unsigned_t crc32
= 0;
380 /* Prevent the counters from being dumped a second time on exit when the
381 application already wrote out the profile using __gcov_dump(). */
382 if (gcov_dump_complete
)
386 memset (&all_prg
, 0, sizeof (all_prg
));
388 /* Find the totals for this execution. */
389 memset (&this_prg
, 0, sizeof (this_prg
));
390 for (gi_ptr
= gcov_list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
392 crc32
= crc32_unsigned (crc32
, gi_ptr
->stamp
);
393 crc32
= crc32_unsigned (crc32
, gi_ptr
->n_functions
);
395 for (f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
; f_ix
++)
397 gfi_ptr
= gi_ptr
->functions
[f_ix
];
399 if (gfi_ptr
&& gfi_ptr
->key
!= gi_ptr
)
402 crc32
= crc32_unsigned (crc32
, gfi_ptr
? gfi_ptr
->cfg_checksum
: 0);
403 crc32
= crc32_unsigned (crc32
,
404 gfi_ptr
? gfi_ptr
->lineno_checksum
: 0);
408 ci_ptr
= gfi_ptr
->ctrs
;
409 for (t_ix
= 0; t_ix
!= GCOV_COUNTERS_SUMMABLE
; t_ix
++)
411 if (!gi_ptr
->merge
[t_ix
])
414 cs_ptr
= &this_prg
.ctrs
[t_ix
];
415 cs_ptr
->num
+= ci_ptr
->num
;
416 crc32
= crc32_unsigned (crc32
, ci_ptr
->num
);
418 for (c_num
= 0; c_num
< ci_ptr
->num
; c_num
++)
420 cs_ptr
->sum_all
+= ci_ptr
->values
[c_num
];
421 if (cs_ptr
->run_max
< ci_ptr
->values
[c_num
])
422 cs_ptr
->run_max
= ci_ptr
->values
[c_num
];
428 gcov_compute_histogram (&this_prg
);
431 /* Check if the level of dirs to strip off specified. */
432 char *tmp
= getenv("GCOV_PREFIX_STRIP");
435 gcov_prefix_strip
= atoi (tmp
);
436 /* Do not consider negative values. */
437 if (gcov_prefix_strip
< 0)
438 gcov_prefix_strip
= 0;
442 /* Get file name relocation prefix. Non-absolute values are ignored. */
443 gcov_prefix
= getenv("GCOV_PREFIX");
446 prefix_length
= strlen(gcov_prefix
);
448 /* Remove an unnecessary trailing '/' */
449 if (IS_DIR_SEPARATOR (gcov_prefix
[prefix_length
- 1]))
455 /* If no prefix was specified and a prefix stip, then we assume
457 if (gcov_prefix_strip
!= 0 && prefix_length
== 0)
462 /* Allocate and initialize the filename scratch space plus one. */
463 gi_filename
= (char *) alloca (prefix_length
+ gcov_max_filename
+ 2);
465 memcpy (gi_filename
, gcov_prefix
, prefix_length
);
466 gi_filename_up
= gi_filename
+ prefix_length
;
468 /* Now merge each file. */
469 for (gi_ptr
= gcov_list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
472 struct gcov_summary prg
; /* summary for this object over all
474 struct gcov_ctr_summary
*cs_prg
, *cs_tprg
;
476 struct gcov_ctr_summary
*cs_all
;
479 gcov_unsigned_t tag
, length
;
480 gcov_position_t summary_pos
= 0;
481 gcov_position_t eof_pos
= 0;
482 const char *fname
, *s
;
483 struct gcov_fn_buffer
*fn_buffer
= 0;
484 struct gcov_fn_buffer
**fn_tail
= &fn_buffer
;
485 struct gcov_summary_buffer
*next_sum_buffer
, *sum_buffer
= 0;
486 struct gcov_summary_buffer
**sum_tail
= &sum_buffer
;
488 fname
= gi_ptr
->filename
;
490 /* Avoid to add multiple drive letters into combined path. */
491 if (prefix_length
!= 0 && HAS_DRIVE_SPEC(fname
))
494 /* Build relocated filename, stripping off leading
495 directories from the initial filename if requested. */
496 if (gcov_prefix_strip
> 0)
500 if (IS_DIR_SEPARATOR(*s
))
503 /* Skip selected directory levels. */
504 for (; (*s
!= '\0') && (level
< gcov_prefix_strip
); s
++)
505 if (IS_DIR_SEPARATOR(*s
))
512 /* Update complete filename with stripped original. */
513 if (prefix_length
!= 0 && !IS_DIR_SEPARATOR (*fname
))
515 /* If prefix is given, add directory separator. */
516 strcpy (gi_filename_up
, "/");
517 strcpy (gi_filename_up
+ 1, fname
);
520 strcpy (gi_filename_up
, fname
);
522 if (!gcov_open (gi_filename
))
524 /* Open failed likely due to missed directory.
525 Create directory and retry to open file. */
526 if (create_file_directory (gi_filename
))
528 fprintf (stderr
, "profiling:%s:Skip\n", gi_filename
);
531 if (!gcov_open (gi_filename
))
533 fprintf (stderr
, "profiling:%s:Cannot open\n", gi_filename
);
538 tag
= gcov_read_unsigned ();
541 /* Merge data from file. */
542 if (tag
!= GCOV_DATA_MAGIC
)
544 fprintf (stderr
, "profiling:%s:Not a gcov data file\n",
548 length
= gcov_read_unsigned ();
549 if (!gcov_version (gi_ptr
, length
, gi_filename
))
552 length
= gcov_read_unsigned ();
553 if (length
!= gi_ptr
->stamp
)
554 /* Read from a different compilation. Overwrite the file. */
557 /* Look for program summary. */
560 struct gcov_summary tmp
;
562 eof_pos
= gcov_position ();
563 tag
= gcov_read_unsigned ();
564 if (tag
!= GCOV_TAG_PROGRAM_SUMMARY
)
568 length
= gcov_read_unsigned ();
569 gcov_read_summary (&tmp
);
570 if ((error
= gcov_is_error ()))
574 /* Save all summaries after the one that will be
575 merged into below. These will need to be rewritten
576 as histogram merging may change the number of non-zero
577 histogram entries that will be emitted, and thus the
578 size of the merged summary. */
579 (*sum_tail
) = (struct gcov_summary_buffer
*)
580 malloc (sizeof(struct gcov_summary_buffer
));
581 (*sum_tail
)->summary
= tmp
;
582 (*sum_tail
)->next
= 0;
583 sum_tail
= &((*sum_tail
)->next
);
586 if (tmp
.checksum
!= crc32
)
589 for (t_ix
= 0; t_ix
!= GCOV_COUNTERS_SUMMABLE
; t_ix
++)
590 if (tmp
.ctrs
[t_ix
].num
!= this_prg
.ctrs
[t_ix
].num
)
593 summary_pos
= eof_pos
;
598 /* Merge execution counts for each function. */
599 for (f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
;
600 f_ix
++, tag
= gcov_read_unsigned ())
602 gfi_ptr
= gi_ptr
->functions
[f_ix
];
604 if (tag
!= GCOV_TAG_FUNCTION
)
607 length
= gcov_read_unsigned ();
609 /* This function did not appear in the other program.
610 We have nothing to merge. */
613 if (length
!= GCOV_TAG_FUNCTION_LENGTH
)
616 if (!gfi_ptr
|| gfi_ptr
->key
!= gi_ptr
)
618 /* This function appears in the other program. We
619 need to buffer the information in order to write
620 it back out -- we'll be inserting data before
621 this point, so cannot simply keep the data in the
623 fn_tail
= buffer_fn_data (gi_filename
,
624 gi_ptr
, fn_tail
, f_ix
);
630 length
= gcov_read_unsigned ();
631 if (length
!= gfi_ptr
->ident
)
634 length
= gcov_read_unsigned ();
635 if (length
!= gfi_ptr
->lineno_checksum
)
638 length
= gcov_read_unsigned ();
639 if (length
!= gfi_ptr
->cfg_checksum
)
642 ci_ptr
= gfi_ptr
->ctrs
;
643 for (t_ix
= 0; t_ix
< GCOV_COUNTERS
; t_ix
++)
645 gcov_merge_fn merge
= gi_ptr
->merge
[t_ix
];
650 tag
= gcov_read_unsigned ();
651 length
= gcov_read_unsigned ();
652 if (tag
!= GCOV_TAG_FOR_COUNTER (t_ix
)
653 || length
!= GCOV_TAG_COUNTER_LENGTH (ci_ptr
->num
))
655 (*merge
) (ci_ptr
->values
, ci_ptr
->num
);
658 if ((error
= gcov_is_error ()))
665 fprintf (stderr
, "profiling:%s:Merge mismatch for %s %u\n",
666 gi_filename
, f_ix
>= 0 ? "function" : "summary",
667 f_ix
< 0 ? -1 - f_ix
: f_ix
);
674 fprintf (stderr
, "profiling:%s:%s merging\n", gi_filename
,
675 error
< 0 ? "Overflow": "Error");
683 memset (&prg
, 0, sizeof (prg
));
684 summary_pos
= eof_pos
;
687 /* Merge the summaries. */
688 for (t_ix
= 0; t_ix
< GCOV_COUNTERS_SUMMABLE
; t_ix
++)
690 cs_prg
= &prg
.ctrs
[t_ix
];
691 cs_tprg
= &this_prg
.ctrs
[t_ix
];
693 if (gi_ptr
->merge
[t_ix
])
696 cs_prg
->num
= cs_tprg
->num
;
697 cs_prg
->sum_all
+= cs_tprg
->sum_all
;
698 if (cs_prg
->run_max
< cs_tprg
->run_max
)
699 cs_prg
->run_max
= cs_tprg
->run_max
;
700 cs_prg
->sum_max
+= cs_tprg
->run_max
;
701 if (cs_prg
->runs
== 1)
702 memcpy (cs_prg
->histogram
, cs_tprg
->histogram
,
703 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
705 gcov_histogram_merge (cs_prg
->histogram
, cs_tprg
->histogram
);
707 else if (cs_prg
->runs
)
711 cs_all
= &all_prg
.ctrs
[t_ix
];
712 if (!cs_all
->runs
&& cs_prg
->runs
)
714 cs_all
->num
= cs_prg
->num
;
715 cs_all
->runs
= cs_prg
->runs
;
716 cs_all
->sum_all
= cs_prg
->sum_all
;
717 cs_all
->run_max
= cs_prg
->run_max
;
718 cs_all
->sum_max
= cs_prg
->sum_max
;
720 else if (!all_prg
.checksum
721 /* Don't compare the histograms, which may have slight
722 variations depending on the order they were updated
723 due to the truncating integer divides used in the
725 && (cs_all
->num
!= cs_prg
->num
726 || cs_all
->runs
!= cs_prg
->runs
727 || cs_all
->sum_all
!= cs_prg
->sum_all
728 || cs_all
->run_max
!= cs_prg
->run_max
729 || cs_all
->sum_max
!= cs_prg
->sum_max
))
732 "profiling:%s:Data file mismatch - some data files may "
733 "have been concurrently updated without locking support\n",
735 all_prg
.checksum
= ~0u;
740 prg
.checksum
= crc32
;
742 /* Write out the data. */
745 gcov_write_tag_length (GCOV_DATA_MAGIC
, GCOV_VERSION
);
746 gcov_write_unsigned (gi_ptr
->stamp
);
750 gcov_seek (summary_pos
);
752 /* Generate whole program statistics. */
753 gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY
, &prg
);
755 /* Rewrite all the summaries that were after the summary we merged
756 into. This is necessary as the merged summary may have a different
757 size due to the number of non-zero histogram entries changing after
762 gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY
, &sum_buffer
->summary
);
763 next_sum_buffer
= sum_buffer
->next
;
765 sum_buffer
= next_sum_buffer
;
768 /* Write execution counts for each function. */
769 for (f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
; f_ix
++)
771 unsigned buffered
= 0;
773 if (fn_buffer
&& fn_buffer
->fn_ix
== (unsigned)f_ix
)
775 /* Buffered data from another program. */
777 gfi_ptr
= &fn_buffer
->info
;
778 length
= GCOV_TAG_FUNCTION_LENGTH
;
782 gfi_ptr
= gi_ptr
->functions
[f_ix
];
783 if (gfi_ptr
&& gfi_ptr
->key
== gi_ptr
)
784 length
= GCOV_TAG_FUNCTION_LENGTH
;
789 gcov_write_tag_length (GCOV_TAG_FUNCTION
, length
);
793 gcov_write_unsigned (gfi_ptr
->ident
);
794 gcov_write_unsigned (gfi_ptr
->lineno_checksum
);
795 gcov_write_unsigned (gfi_ptr
->cfg_checksum
);
797 ci_ptr
= gfi_ptr
->ctrs
;
798 for (t_ix
= 0; t_ix
< GCOV_COUNTERS
; t_ix
++)
800 if (!gi_ptr
->merge
[t_ix
])
803 n_counts
= ci_ptr
->num
;
804 gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix
),
805 GCOV_TAG_COUNTER_LENGTH (n_counts
));
806 gcov_type
*c_ptr
= ci_ptr
->values
;
808 gcov_write_counter (*c_ptr
++);
812 fn_buffer
= free_fn_data (gi_ptr
, fn_buffer
, GCOV_COUNTERS
);
815 gcov_write_unsigned (0);
819 fn_buffer
= free_fn_data (gi_ptr
, fn_buffer
, GCOV_COUNTERS
);
821 if ((error
= gcov_close ()))
822 fprintf (stderr
, error
< 0 ?
823 "profiling:%s:Overflow writing\n" :
824 "profiling:%s:Error writing\n",
829 /* Reset all counters to zero. */
834 const struct gcov_info
*gi_ptr
;
836 for (gi_ptr
= gcov_list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
840 for (f_ix
= 0; f_ix
< gi_ptr
->n_functions
; f_ix
++)
843 const struct gcov_fn_info
*gfi_ptr
= gi_ptr
->functions
[f_ix
];
845 if (!gfi_ptr
|| gfi_ptr
->key
!= gi_ptr
)
847 const struct gcov_ctr_info
*ci_ptr
= gfi_ptr
->ctrs
;
848 for (t_ix
= 0; t_ix
!= GCOV_COUNTERS
; t_ix
++)
850 if (!gi_ptr
->merge
[t_ix
])
853 memset (ci_ptr
->values
, 0, sizeof (gcov_type
) * ci_ptr
->num
);
860 /* Add a new object file onto the bb chain. Invoked automatically
861 when running an object file's global ctors. */
864 __gcov_init (struct gcov_info
*info
)
866 if (!info
->version
|| !info
->n_functions
)
868 if (gcov_version (info
, info
->version
, 0))
870 size_t filename_length
= strlen(info
->filename
);
872 /* Refresh the longest file name information */
873 if (filename_length
> gcov_max_filename
)
874 gcov_max_filename
= filename_length
;
879 info
->next
= gcov_list
;
885 #ifdef __GTHREAD_MUTEX_INIT
886 ATTRIBUTE_HIDDEN __gthread_mutex_t __gcov_flush_mx
= __GTHREAD_MUTEX_INIT
;
887 #define init_mx_once()
889 __gthread_mutex_t __gcov_flush_mx ATTRIBUTE_HIDDEN
;
894 __GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_flush_mx
);
899 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
900 __gthread_once (&once
, init_mx
);
904 /* Called before fork or exec - write out profile information gathered so
905 far and reset it to zero. This avoids duplication or loss of the
906 profile information gathered so far. */
912 __gthread_mutex_lock (&__gcov_flush_mx
);
917 __gthread_mutex_unlock (&__gcov_flush_mx
);
924 /* Function that can be called from application to reset counters to zero,
925 in order to collect profile in region of interest. */
931 /* Re-enable dumping to support collecting profile in multiple regions
933 gcov_dump_complete
= 0;
936 #endif /* L_gcov_reset */
940 /* Function that can be called from application to write profile collected
941 so far, in order to collect profile in region of interest. */
947 /* Prevent profile from being dumped a second time on application exit. */
948 gcov_dump_complete
= 1;
951 #endif /* L_gcov_dump */
953 #ifdef L_gcov_merge_add
954 /* The profile merging function that just adds the counters. It is given
955 an array COUNTERS of N_COUNTERS old counters and it reads the same number
956 of counters from the gcov file. */
958 __gcov_merge_add (gcov_type
*counters
, unsigned n_counters
)
960 for (; n_counters
; counters
++, n_counters
--)
961 *counters
+= gcov_read_counter ();
963 #endif /* L_gcov_merge_add */
965 #ifdef L_gcov_merge_ior
966 /* The profile merging function that just adds the counters. It is given
967 an array COUNTERS of N_COUNTERS old counters and it reads the same number
968 of counters from the gcov file. */
970 __gcov_merge_ior (gcov_type
*counters
, unsigned n_counters
)
972 for (; n_counters
; counters
++, n_counters
--)
973 *counters
|= gcov_read_counter ();
977 #ifdef L_gcov_merge_single
978 /* The profile merging function for choosing the most common value.
979 It is given an array COUNTERS of N_COUNTERS old counters and it
980 reads the same number of counters from the gcov file. The counters
981 are split into 3-tuples where the members of the tuple have
984 -- the stored candidate on the most common value of the measured entity
986 -- total number of evaluations of the value */
988 __gcov_merge_single (gcov_type
*counters
, unsigned n_counters
)
990 unsigned i
, n_measures
;
991 gcov_type value
, counter
, all
;
993 gcc_assert (!(n_counters
% 3));
994 n_measures
= n_counters
/ 3;
995 for (i
= 0; i
< n_measures
; i
++, counters
+= 3)
997 value
= gcov_read_counter ();
998 counter
= gcov_read_counter ();
999 all
= gcov_read_counter ();
1001 if (counters
[0] == value
)
1002 counters
[1] += counter
;
1003 else if (counter
> counters
[1])
1005 counters
[0] = value
;
1006 counters
[1] = counter
- counters
[1];
1009 counters
[1] -= counter
;
1013 #endif /* L_gcov_merge_single */
1015 #ifdef L_gcov_merge_delta
1016 /* The profile merging function for choosing the most common
1017 difference between two consecutive evaluations of the value. It is
1018 given an array COUNTERS of N_COUNTERS old counters and it reads the
1019 same number of counters from the gcov file. The counters are split
1020 into 4-tuples where the members of the tuple have meanings:
1022 -- the last value of the measured entity
1023 -- the stored candidate on the most common difference
1025 -- total number of evaluations of the value */
1027 __gcov_merge_delta (gcov_type
*counters
, unsigned n_counters
)
1029 unsigned i
, n_measures
;
1030 gcov_type value
, counter
, all
;
1032 gcc_assert (!(n_counters
% 4));
1033 n_measures
= n_counters
/ 4;
1034 for (i
= 0; i
< n_measures
; i
++, counters
+= 4)
1036 /* last = */ gcov_read_counter ();
1037 value
= gcov_read_counter ();
1038 counter
= gcov_read_counter ();
1039 all
= gcov_read_counter ();
1041 if (counters
[1] == value
)
1042 counters
[2] += counter
;
1043 else if (counter
> counters
[2])
1045 counters
[1] = value
;
1046 counters
[2] = counter
- counters
[2];
1049 counters
[2] -= counter
;
1053 #endif /* L_gcov_merge_delta */
1055 #ifdef L_gcov_interval_profiler
1056 /* If VALUE is in interval <START, START + STEPS - 1>, then increases the
1057 corresponding counter in COUNTERS. If the VALUE is above or below
1058 the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased
1062 __gcov_interval_profiler (gcov_type
*counters
, gcov_type value
,
1063 int start
, unsigned steps
)
1065 gcov_type delta
= value
- start
;
1067 counters
[steps
+ 1]++;
1068 else if (delta
>= steps
)
1075 #ifdef L_gcov_pow2_profiler
1076 /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
1077 COUNTERS[0] is incremented. */
1080 __gcov_pow2_profiler (gcov_type
*counters
, gcov_type value
)
1082 if (value
& (value
- 1))
1089 /* Tries to determine the most common value among its inputs. Checks if the
1090 value stored in COUNTERS[0] matches VALUE. If this is the case, COUNTERS[1]
1091 is incremented. If this is not the case and COUNTERS[1] is not zero,
1092 COUNTERS[1] is decremented. Otherwise COUNTERS[1] is set to one and
1093 VALUE is stored to COUNTERS[0]. This algorithm guarantees that if this
1094 function is called more than 50% of the time with one value, this value
1095 will be in COUNTERS[0] in the end.
1097 In any case, COUNTERS[2] is incremented. */
1100 __gcov_one_value_profiler_body (gcov_type
*counters
, gcov_type value
)
1102 if (value
== counters
[0])
1104 else if (counters
[1] == 0)
1107 counters
[0] = value
;
1114 #ifdef L_gcov_one_value_profiler
1116 __gcov_one_value_profiler (gcov_type
*counters
, gcov_type value
)
1118 __gcov_one_value_profiler_body (counters
, value
);
1122 #ifdef L_gcov_indirect_call_profiler
1123 /* This function exist only for workaround of binutils bug 14342.
1124 Once this compatibility hack is obsolette, it can be removed. */
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
);
1158 #ifdef L_gcov_indirect_call_profiler_v2
1160 /* These two variables are used to actually track caller and callee. Keep
1161 them in TLS memory so races are not common (they are written to often).
1162 The variables are set directly by GCC instrumented code, so declaration
1163 here must match one in tree-profile.c */
1168 void * __gcov_indirect_call_callee
;
1172 gcov_type
* __gcov_indirect_call_counters
;
1174 /* By default, the C++ compiler will use function addresses in the
1175 vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero
1176 tells the compiler to use function descriptors instead. The value
1177 of this macro says how many words wide the descriptor is (normally 2),
1178 but it may be dependent on target flags. Since we do not have access
1179 to the target flags here we just check to see if it is set and use
1180 that to set VTABLE_USES_DESCRIPTORS to 0 or 1.
1182 It is assumed that the address of a function descriptor may be treated
1183 as a pointer to a function. */
1185 #ifdef TARGET_VTABLE_USES_DESCRIPTORS
1186 #define VTABLE_USES_DESCRIPTORS 1
1188 #define VTABLE_USES_DESCRIPTORS 0
1191 /* Tries to determine the most common value among its inputs. */
1193 __gcov_indirect_call_profiler_v2 (gcov_type value
, void* cur_func
)
1195 /* If the C++ virtual tables contain function descriptors then one
1196 function may have multiple descriptors and we need to dereference
1197 the descriptors to see if they point to the same function. */
1198 if (cur_func
== __gcov_indirect_call_callee
1199 || (VTABLE_USES_DESCRIPTORS
&& __gcov_indirect_call_callee
1200 && *(void **) cur_func
== *(void **) __gcov_indirect_call_callee
))
1201 __gcov_one_value_profiler_body (__gcov_indirect_call_counters
, value
);
1205 #ifdef L_gcov_average_profiler
1206 /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
1210 __gcov_average_profiler (gcov_type
*counters
, gcov_type value
)
1212 counters
[0] += value
;
1217 #ifdef L_gcov_ior_profiler
1218 /* Bitwise-OR VALUE into COUNTER. */
1221 __gcov_ior_profiler (gcov_type
*counters
, gcov_type value
)
1228 /* A wrapper for the fork function. Flushes the accumulated profiling data, so
1229 that they are not counted twice. */
1235 extern __gthread_mutex_t __gcov_flush_mx
;
1239 __GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_flush_mx
);
1245 /* A wrapper for the execl function. Flushes the accumulated profiling data, so
1246 that they are not lost. */
1249 __gcov_execl (const char *path
, char *arg
, ...)
1261 while (va_arg (ap
, char *))
1265 args
= (char **) alloca (length
* sizeof (void *));
1267 for (i
= 1; i
< length
; i
++)
1268 args
[i
] = va_arg (aq
, char *);
1271 return execv (path
, args
);
1275 #ifdef L_gcov_execlp
1276 /* A wrapper for the execlp function. Flushes the accumulated profiling data, so
1277 that they are not lost. */
1280 __gcov_execlp (const char *path
, char *arg
, ...)
1292 while (va_arg (ap
, char *))
1296 args
= (char **) alloca (length
* sizeof (void *));
1298 for (i
= 1; i
< length
; i
++)
1299 args
[i
] = va_arg (aq
, char *);
1302 return execvp (path
, args
);
1306 #ifdef L_gcov_execle
1307 /* A wrapper for the execle function. Flushes the accumulated profiling data, so
1308 that they are not lost. */
1311 __gcov_execle (const char *path
, char *arg
, ...)
1324 while (va_arg (ap
, char *))
1328 args
= (char **) alloca (length
* sizeof (void *));
1330 for (i
= 1; i
< length
; i
++)
1331 args
[i
] = va_arg (aq
, char *);
1332 envp
= va_arg (aq
, char **);
1335 return execve (path
, args
, envp
);
1340 /* A wrapper for the execv function. Flushes the accumulated profiling data, so
1341 that they are not lost. */
1344 __gcov_execv (const char *path
, char *const argv
[])
1347 return execv (path
, argv
);
1351 #ifdef L_gcov_execvp
1352 /* A wrapper for the execvp function. Flushes the accumulated profiling data, so
1353 that they are not lost. */
1356 __gcov_execvp (const char *path
, char *const argv
[])
1359 return execvp (path
, argv
);
1363 #ifdef L_gcov_execve
1364 /* A wrapper for the execve function. Flushes the accumulated profiling data, so
1365 that they are not lost. */
1368 __gcov_execve (const char *path
, char *const argv
[], char *const envp
[])
1371 return execve (path
, argv
, envp
);
1374 #endif /* inhibit_libc */