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 gcov_type function_counter ATTRIBUTE_HIDDEN
;
84 extern void gcov_clear (void) ATTRIBUTE_HIDDEN
;
85 extern void gcov_exit (void) ATTRIBUTE_HIDDEN
;
86 extern int gcov_dump_complete ATTRIBUTE_HIDDEN
;
93 struct gcov_fn_buffer
*next
;
95 struct gcov_fn_info info
;
96 /* note gcov_fn_info ends in a trailing array. */
99 struct gcov_summary_buffer
101 struct gcov_summary_buffer
*next
;
102 struct gcov_summary summary
;
105 /* Chain of per-object gcov structures. */
106 static struct gcov_info
*gcov_list
;
108 /* Size of the longest file name. */
109 static size_t gcov_max_filename
= 0;
111 /* Flag when the profile has already been dumped via __gcov_dump(). */
112 int gcov_dump_complete
= 0;
114 /* Make sure path component of the given FILENAME exists, create
115 missing directories. FILENAME must be writable.
116 Returns zero on success, or -1 if an error occurred. */
119 create_file_directory (char *filename
)
121 #if !defined(TARGET_POSIX_IO) && !defined(_WIN32)
129 if (HAS_DRIVE_SPEC(s
))
131 if (IS_DIR_SEPARATOR(*s
))
133 for (; *s
!= '\0'; s
++)
134 if (IS_DIR_SEPARATOR(*s
))
139 /* Try to make directory if it doesn't already exist. */
140 if (access (filename
, F_OK
) == -1
141 #ifdef TARGET_POSIX_IO
142 && mkdir (filename
, 0755) == -1
144 && mkdir (filename
) == -1
146 /* The directory might have been made by another process. */
149 fprintf (stderr
, "profiling:%s:Cannot create directory\n",
161 static struct gcov_fn_buffer
*
162 free_fn_data (const struct gcov_info
*gi_ptr
, struct gcov_fn_buffer
*buffer
,
165 struct gcov_fn_buffer
*next
;
166 unsigned ix
, n_ctr
= 0;
172 for (ix
= 0; ix
!= limit
; ix
++)
173 if (gi_ptr
->merge
[ix
])
174 free (buffer
->info
.ctrs
[n_ctr
++].values
);
179 static struct gcov_fn_buffer
**
180 buffer_fn_data (const char *filename
, const struct gcov_info
*gi_ptr
,
181 struct gcov_fn_buffer
**end_ptr
, unsigned fn_ix
)
183 unsigned n_ctrs
= 0, ix
= 0;
184 struct gcov_fn_buffer
*fn_buffer
;
187 for (ix
= GCOV_COUNTERS
; ix
--;)
188 if (gi_ptr
->merge
[ix
])
191 len
= sizeof (*fn_buffer
) + sizeof (fn_buffer
->info
.ctrs
[0]) * n_ctrs
;
192 fn_buffer
= (struct gcov_fn_buffer
*)malloc (len
);
198 fn_buffer
->fn_ix
= fn_ix
;
199 fn_buffer
->info
.ident
= gcov_read_unsigned ();
200 fn_buffer
->info
.lineno_checksum
= gcov_read_unsigned ();
201 fn_buffer
->info
.cfg_checksum
= gcov_read_unsigned ();
203 for (n_ctrs
= ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
205 gcov_unsigned_t length
;
208 if (!gi_ptr
->merge
[ix
])
211 if (gcov_read_unsigned () != GCOV_TAG_FOR_COUNTER (ix
))
217 length
= GCOV_TAG_COUNTER_NUM (gcov_read_unsigned ());
218 len
= length
* sizeof (gcov_type
);
219 values
= (gcov_type
*)malloc (len
);
223 fn_buffer
->info
.ctrs
[n_ctrs
].num
= length
;
224 fn_buffer
->info
.ctrs
[n_ctrs
].values
= values
;
227 *values
++ = gcov_read_counter ();
231 *end_ptr
= fn_buffer
;
232 return &fn_buffer
->next
;
235 fprintf (stderr
, "profiling:%s:Function %u %s %u \n", filename
, fn_ix
,
236 len
? "cannot allocate" : "counter mismatch", len
? len
: ix
);
238 return (struct gcov_fn_buffer
**)free_fn_data (gi_ptr
, fn_buffer
, ix
);
241 /* Add an unsigned value to the current crc */
243 static gcov_unsigned_t
244 crc32_unsigned (gcov_unsigned_t crc32
, gcov_unsigned_t value
)
248 for (ix
= 32; ix
--; value
<<= 1)
252 feedback
= (value
^ crc32
) & 0x80000000 ? 0x04c11db7 : 0;
260 /* Check if VERSION of the info block PTR matches libgcov one.
261 Return 1 on success, or zero in case of versions mismatch.
262 If FILENAME is not NULL, its value used for reporting purposes
263 instead of value from the info block. */
266 gcov_version (struct gcov_info
*ptr
, gcov_unsigned_t version
,
267 const char *filename
)
269 if (version
!= GCOV_VERSION
)
273 GCOV_UNSIGNED2STRING (v
, version
);
274 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
277 "profiling:%s:Version mismatch - expected %.4s got %.4s\n",
278 filename
? filename
: ptr
->filename
, e
, v
);
284 /* Insert counter VALUE into HISTOGRAM. */
287 gcov_histogram_insert(gcov_bucket_type
*histogram
, gcov_type value
)
291 i
= gcov_histo_index(value
);
292 histogram
[i
].num_counters
++;
293 histogram
[i
].cum_value
+= value
;
294 if (value
< histogram
[i
].min_value
)
295 histogram
[i
].min_value
= value
;
298 /* Computes a histogram of the arc counters to place in the summary SUM. */
301 gcov_compute_histogram (struct gcov_summary
*sum
)
303 struct gcov_info
*gi_ptr
;
304 const struct gcov_fn_info
*gfi_ptr
;
305 const struct gcov_ctr_info
*ci_ptr
;
306 struct gcov_ctr_summary
*cs_ptr
;
307 unsigned t_ix
, f_ix
, ctr_info_ix
, ix
;
310 /* This currently only applies to arc counters. */
311 t_ix
= GCOV_COUNTER_ARCS
;
313 /* First check if there are any counts recorded for this counter. */
314 cs_ptr
= &(sum
->ctrs
[t_ix
]);
318 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
320 cs_ptr
->histogram
[h_ix
].num_counters
= 0;
321 cs_ptr
->histogram
[h_ix
].min_value
= cs_ptr
->run_max
;
322 cs_ptr
->histogram
[h_ix
].cum_value
= 0;
325 /* Walk through all the per-object structures and record each of
326 the count values in histogram. */
327 for (gi_ptr
= gcov_list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
329 if (!gi_ptr
->merge
[t_ix
])
332 /* Find the appropriate index into the gcov_ctr_info array
333 for the counter we are currently working on based on the
334 existence of the merge function pointer for this object. */
335 for (ix
= 0, ctr_info_ix
= 0; ix
< t_ix
; ix
++)
337 if (gi_ptr
->merge
[ix
])
340 for (f_ix
= 0; f_ix
!= gi_ptr
->n_functions
; f_ix
++)
342 gfi_ptr
= gi_ptr
->functions
[f_ix
];
344 if (!gfi_ptr
|| gfi_ptr
->key
!= gi_ptr
)
347 ci_ptr
= &gfi_ptr
->ctrs
[ctr_info_ix
];
348 for (ix
= 0; ix
< ci_ptr
->num
; ix
++)
349 gcov_histogram_insert (cs_ptr
->histogram
, ci_ptr
->values
[ix
]);
355 /* Counter for first visit of each function. */
356 gcov_type function_counter
;
358 /* Dump the coverage counts. We merge with existing counts when
359 possible, to avoid growing the .da files ad infinitum. We use this
360 program's checksum to make sure we only accumulate whole program
361 statistics to the correct summary. An object file might be embedded
362 in two separate programs, and we must keep the two program
363 summaries separate. */
368 struct gcov_info
*gi_ptr
;
369 const struct gcov_fn_info
*gfi_ptr
;
370 struct gcov_summary this_prg
; /* summary for program. */
372 struct gcov_summary all_prg
; /* summary for all instances of program. */
374 struct gcov_ctr_summary
*cs_ptr
;
375 const struct gcov_ctr_info
*ci_ptr
;
378 gcov_unsigned_t c_num
;
379 const char *gcov_prefix
;
380 int gcov_prefix_strip
= 0;
381 size_t prefix_length
;
382 char *gi_filename
, *gi_filename_up
;
383 gcov_unsigned_t crc32
= 0;
385 /* Prevent the counters from being dumped a second time on exit when the
386 application already wrote out the profile using __gcov_dump(). */
387 if (gcov_dump_complete
)
391 memset (&all_prg
, 0, sizeof (all_prg
));
393 /* Find the totals for this execution. */
394 memset (&this_prg
, 0, sizeof (this_prg
));
395 for (gi_ptr
= gcov_list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
397 crc32
= crc32_unsigned (crc32
, gi_ptr
->stamp
);
398 crc32
= crc32_unsigned (crc32
, gi_ptr
->n_functions
);
400 for (f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
; f_ix
++)
402 gfi_ptr
= gi_ptr
->functions
[f_ix
];
404 if (gfi_ptr
&& gfi_ptr
->key
!= gi_ptr
)
407 crc32
= crc32_unsigned (crc32
, gfi_ptr
? gfi_ptr
->cfg_checksum
: 0);
408 crc32
= crc32_unsigned (crc32
,
409 gfi_ptr
? gfi_ptr
->lineno_checksum
: 0);
413 ci_ptr
= gfi_ptr
->ctrs
;
414 for (t_ix
= 0; t_ix
!= GCOV_COUNTERS_SUMMABLE
; t_ix
++)
416 if (!gi_ptr
->merge
[t_ix
])
419 cs_ptr
= &this_prg
.ctrs
[t_ix
];
420 cs_ptr
->num
+= ci_ptr
->num
;
421 crc32
= crc32_unsigned (crc32
, ci_ptr
->num
);
423 for (c_num
= 0; c_num
< ci_ptr
->num
; c_num
++)
425 cs_ptr
->sum_all
+= ci_ptr
->values
[c_num
];
426 if (cs_ptr
->run_max
< ci_ptr
->values
[c_num
])
427 cs_ptr
->run_max
= ci_ptr
->values
[c_num
];
433 gcov_compute_histogram (&this_prg
);
436 /* Check if the level of dirs to strip off specified. */
437 char *tmp
= getenv("GCOV_PREFIX_STRIP");
440 gcov_prefix_strip
= atoi (tmp
);
441 /* Do not consider negative values. */
442 if (gcov_prefix_strip
< 0)
443 gcov_prefix_strip
= 0;
447 /* Get file name relocation prefix. Non-absolute values are ignored. */
448 gcov_prefix
= getenv("GCOV_PREFIX");
451 prefix_length
= strlen(gcov_prefix
);
453 /* Remove an unnecessary trailing '/' */
454 if (IS_DIR_SEPARATOR (gcov_prefix
[prefix_length
- 1]))
460 /* If no prefix was specified and a prefix stip, then we assume
462 if (gcov_prefix_strip
!= 0 && prefix_length
== 0)
467 /* Allocate and initialize the filename scratch space plus one. */
468 gi_filename
= (char *) alloca (prefix_length
+ gcov_max_filename
+ 2);
470 memcpy (gi_filename
, gcov_prefix
, prefix_length
);
471 gi_filename_up
= gi_filename
+ prefix_length
;
473 /* Now merge each file. */
474 for (gi_ptr
= gcov_list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
477 struct gcov_summary prg
; /* summary for this object over all
479 struct gcov_ctr_summary
*cs_prg
, *cs_tprg
;
481 struct gcov_ctr_summary
*cs_all
;
484 gcov_unsigned_t tag
, length
;
485 gcov_position_t summary_pos
= 0;
486 gcov_position_t eof_pos
= 0;
487 const char *fname
, *s
;
488 struct gcov_fn_buffer
*fn_buffer
= 0;
489 struct gcov_fn_buffer
**fn_tail
= &fn_buffer
;
490 struct gcov_summary_buffer
*next_sum_buffer
, *sum_buffer
= 0;
491 struct gcov_summary_buffer
**sum_tail
= &sum_buffer
;
493 fname
= gi_ptr
->filename
;
495 /* Avoid to add multiple drive letters into combined path. */
496 if (prefix_length
!= 0 && HAS_DRIVE_SPEC(fname
))
499 /* Build relocated filename, stripping off leading
500 directories from the initial filename if requested. */
501 if (gcov_prefix_strip
> 0)
505 if (IS_DIR_SEPARATOR(*s
))
508 /* Skip selected directory levels. */
509 for (; (*s
!= '\0') && (level
< gcov_prefix_strip
); s
++)
510 if (IS_DIR_SEPARATOR(*s
))
517 /* Update complete filename with stripped original. */
518 if (prefix_length
!= 0 && !IS_DIR_SEPARATOR (*fname
))
520 /* If prefix is given, add directory separator. */
521 strcpy (gi_filename_up
, "/");
522 strcpy (gi_filename_up
+ 1, fname
);
525 strcpy (gi_filename_up
, fname
);
527 if (!gcov_open (gi_filename
))
529 /* Open failed likely due to missed directory.
530 Create directory and retry to open file. */
531 if (create_file_directory (gi_filename
))
533 fprintf (stderr
, "profiling:%s:Skip\n", gi_filename
);
536 if (!gcov_open (gi_filename
))
538 fprintf (stderr
, "profiling:%s:Cannot open\n", gi_filename
);
543 tag
= gcov_read_unsigned ();
546 /* Merge data from file. */
547 if (tag
!= GCOV_DATA_MAGIC
)
549 fprintf (stderr
, "profiling:%s:Not a gcov data file\n",
553 length
= gcov_read_unsigned ();
554 if (!gcov_version (gi_ptr
, length
, gi_filename
))
557 length
= gcov_read_unsigned ();
558 if (length
!= gi_ptr
->stamp
)
559 /* Read from a different compilation. Overwrite the file. */
562 /* Look for program summary. */
565 struct gcov_summary tmp
;
567 eof_pos
= gcov_position ();
568 tag
= gcov_read_unsigned ();
569 if (tag
!= GCOV_TAG_PROGRAM_SUMMARY
)
573 length
= gcov_read_unsigned ();
574 gcov_read_summary (&tmp
);
575 if ((error
= gcov_is_error ()))
579 /* Save all summaries after the one that will be
580 merged into below. These will need to be rewritten
581 as histogram merging may change the number of non-zero
582 histogram entries that will be emitted, and thus the
583 size of the merged summary. */
584 (*sum_tail
) = (struct gcov_summary_buffer
*)
585 malloc (sizeof(struct gcov_summary_buffer
));
586 (*sum_tail
)->summary
= tmp
;
587 (*sum_tail
)->next
= 0;
588 sum_tail
= &((*sum_tail
)->next
);
591 if (tmp
.checksum
!= crc32
)
594 for (t_ix
= 0; t_ix
!= GCOV_COUNTERS_SUMMABLE
; t_ix
++)
595 if (tmp
.ctrs
[t_ix
].num
!= this_prg
.ctrs
[t_ix
].num
)
598 summary_pos
= eof_pos
;
603 /* Merge execution counts for each function. */
604 for (f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
;
605 f_ix
++, tag
= gcov_read_unsigned ())
607 gfi_ptr
= gi_ptr
->functions
[f_ix
];
609 if (tag
!= GCOV_TAG_FUNCTION
)
612 length
= gcov_read_unsigned ();
614 /* This function did not appear in the other program.
615 We have nothing to merge. */
618 if (length
!= GCOV_TAG_FUNCTION_LENGTH
)
621 if (!gfi_ptr
|| gfi_ptr
->key
!= gi_ptr
)
623 /* This function appears in the other program. We
624 need to buffer the information in order to write
625 it back out -- we'll be inserting data before
626 this point, so cannot simply keep the data in the
628 fn_tail
= buffer_fn_data (gi_filename
,
629 gi_ptr
, fn_tail
, f_ix
);
635 length
= gcov_read_unsigned ();
636 if (length
!= gfi_ptr
->ident
)
639 length
= gcov_read_unsigned ();
640 if (length
!= gfi_ptr
->lineno_checksum
)
643 length
= gcov_read_unsigned ();
644 if (length
!= gfi_ptr
->cfg_checksum
)
647 ci_ptr
= gfi_ptr
->ctrs
;
648 for (t_ix
= 0; t_ix
< GCOV_COUNTERS
; t_ix
++)
650 gcov_merge_fn merge
= gi_ptr
->merge
[t_ix
];
655 tag
= gcov_read_unsigned ();
656 length
= gcov_read_unsigned ();
657 if (tag
!= GCOV_TAG_FOR_COUNTER (t_ix
)
658 || length
!= GCOV_TAG_COUNTER_LENGTH (ci_ptr
->num
))
660 (*merge
) (ci_ptr
->values
, ci_ptr
->num
);
663 if ((error
= gcov_is_error ()))
670 fprintf (stderr
, "profiling:%s:Merge mismatch for %s %u\n",
671 gi_filename
, f_ix
>= 0 ? "function" : "summary",
672 f_ix
< 0 ? -1 - f_ix
: f_ix
);
679 fprintf (stderr
, "profiling:%s:%s merging\n", gi_filename
,
680 error
< 0 ? "Overflow": "Error");
688 memset (&prg
, 0, sizeof (prg
));
689 summary_pos
= eof_pos
;
692 /* Merge the summaries. */
693 for (t_ix
= 0; t_ix
< GCOV_COUNTERS_SUMMABLE
; t_ix
++)
695 cs_prg
= &prg
.ctrs
[t_ix
];
696 cs_tprg
= &this_prg
.ctrs
[t_ix
];
698 if (gi_ptr
->merge
[t_ix
])
701 cs_prg
->num
= cs_tprg
->num
;
702 cs_prg
->sum_all
+= cs_tprg
->sum_all
;
703 if (cs_prg
->run_max
< cs_tprg
->run_max
)
704 cs_prg
->run_max
= cs_tprg
->run_max
;
705 cs_prg
->sum_max
+= cs_tprg
->run_max
;
706 if (cs_prg
->runs
== 1)
707 memcpy (cs_prg
->histogram
, cs_tprg
->histogram
,
708 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
710 gcov_histogram_merge (cs_prg
->histogram
, cs_tprg
->histogram
);
712 else if (cs_prg
->runs
)
716 cs_all
= &all_prg
.ctrs
[t_ix
];
717 if (!cs_all
->runs
&& cs_prg
->runs
)
719 cs_all
->num
= cs_prg
->num
;
720 cs_all
->runs
= cs_prg
->runs
;
721 cs_all
->sum_all
= cs_prg
->sum_all
;
722 cs_all
->run_max
= cs_prg
->run_max
;
723 cs_all
->sum_max
= cs_prg
->sum_max
;
725 else if (!all_prg
.checksum
726 /* Don't compare the histograms, which may have slight
727 variations depending on the order they were updated
728 due to the truncating integer divides used in the
730 && (cs_all
->num
!= cs_prg
->num
731 || cs_all
->runs
!= cs_prg
->runs
732 || cs_all
->sum_all
!= cs_prg
->sum_all
733 || cs_all
->run_max
!= cs_prg
->run_max
734 || cs_all
->sum_max
!= cs_prg
->sum_max
))
737 "profiling:%s:Data file mismatch - some data files may "
738 "have been concurrently updated without locking support\n",
740 all_prg
.checksum
= ~0u;
745 prg
.checksum
= crc32
;
747 /* Write out the data. */
750 gcov_write_tag_length (GCOV_DATA_MAGIC
, GCOV_VERSION
);
751 gcov_write_unsigned (gi_ptr
->stamp
);
755 gcov_seek (summary_pos
);
757 /* Generate whole program statistics. */
758 gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY
, &prg
);
760 /* Rewrite all the summaries that were after the summary we merged
761 into. This is necessary as the merged summary may have a different
762 size due to the number of non-zero histogram entries changing after
767 gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY
, &sum_buffer
->summary
);
768 next_sum_buffer
= sum_buffer
->next
;
770 sum_buffer
= next_sum_buffer
;
773 /* Write execution counts for each function. */
774 for (f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
; f_ix
++)
776 unsigned buffered
= 0;
778 if (fn_buffer
&& fn_buffer
->fn_ix
== (unsigned)f_ix
)
780 /* Buffered data from another program. */
782 gfi_ptr
= &fn_buffer
->info
;
783 length
= GCOV_TAG_FUNCTION_LENGTH
;
787 gfi_ptr
= gi_ptr
->functions
[f_ix
];
788 if (gfi_ptr
&& gfi_ptr
->key
== gi_ptr
)
789 length
= GCOV_TAG_FUNCTION_LENGTH
;
794 gcov_write_tag_length (GCOV_TAG_FUNCTION
, length
);
798 gcov_write_unsigned (gfi_ptr
->ident
);
799 gcov_write_unsigned (gfi_ptr
->lineno_checksum
);
800 gcov_write_unsigned (gfi_ptr
->cfg_checksum
);
802 ci_ptr
= gfi_ptr
->ctrs
;
803 for (t_ix
= 0; t_ix
< GCOV_COUNTERS
; t_ix
++)
805 if (!gi_ptr
->merge
[t_ix
])
808 n_counts
= ci_ptr
->num
;
809 gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix
),
810 GCOV_TAG_COUNTER_LENGTH (n_counts
));
811 gcov_type
*c_ptr
= ci_ptr
->values
;
813 gcov_write_counter (*c_ptr
++);
817 fn_buffer
= free_fn_data (gi_ptr
, fn_buffer
, GCOV_COUNTERS
);
820 gcov_write_unsigned (0);
824 fn_buffer
= free_fn_data (gi_ptr
, fn_buffer
, GCOV_COUNTERS
);
826 if ((error
= gcov_close ()))
827 fprintf (stderr
, error
< 0 ?
828 "profiling:%s:Overflow writing\n" :
829 "profiling:%s:Error writing\n",
834 /* Reset all counters to zero. */
839 const struct gcov_info
*gi_ptr
;
841 for (gi_ptr
= gcov_list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
845 for (f_ix
= 0; f_ix
< gi_ptr
->n_functions
; f_ix
++)
848 const struct gcov_fn_info
*gfi_ptr
= gi_ptr
->functions
[f_ix
];
850 if (!gfi_ptr
|| gfi_ptr
->key
!= gi_ptr
)
852 const struct gcov_ctr_info
*ci_ptr
= gfi_ptr
->ctrs
;
853 for (t_ix
= 0; t_ix
!= GCOV_COUNTERS
; t_ix
++)
855 if (!gi_ptr
->merge
[t_ix
])
858 memset (ci_ptr
->values
, 0, sizeof (gcov_type
) * ci_ptr
->num
);
865 /* Add a new object file onto the bb chain. Invoked automatically
866 when running an object file's global ctors. */
869 __gcov_init (struct gcov_info
*info
)
871 if (!info
->version
|| !info
->n_functions
)
873 if (gcov_version (info
, info
->version
, 0))
875 size_t filename_length
= strlen(info
->filename
);
877 /* Refresh the longest file name information */
878 if (filename_length
> gcov_max_filename
)
879 gcov_max_filename
= filename_length
;
884 info
->next
= gcov_list
;
890 #ifdef __GTHREAD_MUTEX_INIT
891 ATTRIBUTE_HIDDEN __gthread_mutex_t __gcov_flush_mx
= __GTHREAD_MUTEX_INIT
;
892 #define init_mx_once()
894 __gthread_mutex_t __gcov_flush_mx ATTRIBUTE_HIDDEN
;
899 __GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_flush_mx
);
904 static __gthread_once_t once
= __GTHREAD_ONCE_INIT
;
905 __gthread_once (&once
, init_mx
);
909 /* Called before fork or exec - write out profile information gathered so
910 far and reset it to zero. This avoids duplication or loss of the
911 profile information gathered so far. */
917 __gthread_mutex_lock (&__gcov_flush_mx
);
922 __gthread_mutex_unlock (&__gcov_flush_mx
);
929 /* Function that can be called from application to reset counters to zero,
930 in order to collect profile in region of interest. */
936 /* Re-enable dumping to support collecting profile in multiple regions
938 gcov_dump_complete
= 0;
941 #endif /* L_gcov_reset */
945 /* Function that can be called from application to write profile collected
946 so far, in order to collect profile in region of interest. */
952 /* Prevent profile from being dumped a second time on application exit. */
953 gcov_dump_complete
= 1;
956 #endif /* L_gcov_dump */
958 #ifdef L_gcov_merge_add
959 /* The profile merging function that just adds the counters. It is given
960 an array COUNTERS of N_COUNTERS old counters and it reads the same number
961 of counters from the gcov file. */
963 __gcov_merge_add (gcov_type
*counters
, unsigned n_counters
)
965 for (; n_counters
; counters
++, n_counters
--)
966 *counters
+= gcov_read_counter ();
968 #endif /* L_gcov_merge_add */
970 #ifdef L_gcov_merge_ior
971 /* The profile merging function that just adds the counters. It is given
972 an array COUNTERS of N_COUNTERS old counters and it reads the same number
973 of counters from the gcov file. */
975 __gcov_merge_ior (gcov_type
*counters
, unsigned n_counters
)
977 for (; n_counters
; counters
++, n_counters
--)
978 *counters
|= gcov_read_counter ();
982 /* Time profiles are merged so that minimum from all valid (greater than zero)
983 * is stored. There could be a fork that creates new counters. To have
984 * the profile stable, we chosen to pick the smallest function visit time. */
986 #ifdef L_gcov_merge_time_profile
988 __gcov_merge_time_profile (gcov_type
*counters
, unsigned n_counters
)
993 for (i
= 0; i
< n_counters
; i
++)
995 value
= gcov_read_counter ();
997 if (value
&& (!counters
[i
] || value
< counters
[i
]))
1001 #endif /* L_gcov_merge_time_profile */
1003 #ifdef L_gcov_merge_single
1004 /* The profile merging function for choosing the most common value.
1005 It is given an array COUNTERS of N_COUNTERS old counters and it
1006 reads the same number of counters from the gcov file. The counters
1007 are split into 3-tuples where the members of the tuple have
1010 -- the stored candidate on the most common value of the measured entity
1012 -- total number of evaluations of the value */
1014 __gcov_merge_single (gcov_type
*counters
, unsigned n_counters
)
1016 unsigned i
, n_measures
;
1017 gcov_type value
, counter
, all
;
1019 gcc_assert (!(n_counters
% 3));
1020 n_measures
= n_counters
/ 3;
1021 for (i
= 0; i
< n_measures
; i
++, counters
+= 3)
1023 value
= gcov_read_counter ();
1024 counter
= gcov_read_counter ();
1025 all
= gcov_read_counter ();
1027 if (counters
[0] == value
)
1028 counters
[1] += counter
;
1029 else if (counter
> counters
[1])
1031 counters
[0] = value
;
1032 counters
[1] = counter
- counters
[1];
1035 counters
[1] -= counter
;
1039 #endif /* L_gcov_merge_single */
1041 #ifdef L_gcov_merge_delta
1042 /* The profile merging function for choosing the most common
1043 difference between two consecutive evaluations of the value. It is
1044 given an array COUNTERS of N_COUNTERS old counters and it reads the
1045 same number of counters from the gcov file. The counters are split
1046 into 4-tuples where the members of the tuple have meanings:
1048 -- the last value of the measured entity
1049 -- the stored candidate on the most common difference
1051 -- total number of evaluations of the value */
1053 __gcov_merge_delta (gcov_type
*counters
, unsigned n_counters
)
1055 unsigned i
, n_measures
;
1056 gcov_type value
, counter
, all
;
1058 gcc_assert (!(n_counters
% 4));
1059 n_measures
= n_counters
/ 4;
1060 for (i
= 0; i
< n_measures
; i
++, counters
+= 4)
1062 /* last = */ gcov_read_counter ();
1063 value
= gcov_read_counter ();
1064 counter
= gcov_read_counter ();
1065 all
= gcov_read_counter ();
1067 if (counters
[1] == value
)
1068 counters
[2] += counter
;
1069 else if (counter
> counters
[2])
1071 counters
[1] = value
;
1072 counters
[2] = counter
- counters
[2];
1075 counters
[2] -= counter
;
1079 #endif /* L_gcov_merge_delta */
1081 #ifdef L_gcov_interval_profiler
1082 /* If VALUE is in interval <START, START + STEPS - 1>, then increases the
1083 corresponding counter in COUNTERS. If the VALUE is above or below
1084 the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased
1088 __gcov_interval_profiler (gcov_type
*counters
, gcov_type value
,
1089 int start
, unsigned steps
)
1091 gcov_type delta
= value
- start
;
1093 counters
[steps
+ 1]++;
1094 else if (delta
>= steps
)
1101 #ifdef L_gcov_pow2_profiler
1102 /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
1103 COUNTERS[0] is incremented. */
1106 __gcov_pow2_profiler (gcov_type
*counters
, gcov_type value
)
1108 if (value
& (value
- 1))
1115 /* Tries to determine the most common value among its inputs. Checks if the
1116 value stored in COUNTERS[0] matches VALUE. If this is the case, COUNTERS[1]
1117 is incremented. If this is not the case and COUNTERS[1] is not zero,
1118 COUNTERS[1] is decremented. Otherwise COUNTERS[1] is set to one and
1119 VALUE is stored to COUNTERS[0]. This algorithm guarantees that if this
1120 function is called more than 50% of the time with one value, this value
1121 will be in COUNTERS[0] in the end.
1123 In any case, COUNTERS[2] is incremented. */
1126 __gcov_one_value_profiler_body (gcov_type
*counters
, gcov_type value
)
1128 if (value
== counters
[0])
1130 else if (counters
[1] == 0)
1133 counters
[0] = value
;
1140 #ifdef L_gcov_one_value_profiler
1142 __gcov_one_value_profiler (gcov_type
*counters
, gcov_type value
)
1144 __gcov_one_value_profiler_body (counters
, value
);
1148 #ifdef L_gcov_indirect_call_profiler
1149 /* This function exist only for workaround of binutils bug 14342.
1150 Once this compatibility hack is obsolette, it can be removed. */
1152 /* By default, the C++ compiler will use function addresses in the
1153 vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero
1154 tells the compiler to use function descriptors instead. The value
1155 of this macro says how many words wide the descriptor is (normally 2),
1156 but it may be dependent on target flags. Since we do not have access
1157 to the target flags here we just check to see if it is set and use
1158 that to set VTABLE_USES_DESCRIPTORS to 0 or 1.
1160 It is assumed that the address of a function descriptor may be treated
1161 as a pointer to a function. */
1163 #ifdef TARGET_VTABLE_USES_DESCRIPTORS
1164 #define VTABLE_USES_DESCRIPTORS 1
1166 #define VTABLE_USES_DESCRIPTORS 0
1169 /* Tries to determine the most common value among its inputs. */
1171 __gcov_indirect_call_profiler (gcov_type
* counter
, gcov_type value
,
1172 void* cur_func
, void* callee_func
)
1174 /* If the C++ virtual tables contain function descriptors then one
1175 function may have multiple descriptors and we need to dereference
1176 the descriptors to see if they point to the same function. */
1177 if (cur_func
== callee_func
1178 || (VTABLE_USES_DESCRIPTORS
&& callee_func
1179 && *(void **) cur_func
== *(void **) callee_func
))
1180 __gcov_one_value_profiler_body (counter
, value
);
1184 #ifdef L_gcov_indirect_call_profiler_v2
1186 /* These two variables are used to actually track caller and callee. Keep
1187 them in TLS memory so races are not common (they are written to often).
1188 The variables are set directly by GCC instrumented code, so declaration
1189 here must match one in tree-profile.c */
1191 #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
1194 void * __gcov_indirect_call_callee
;
1195 #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
1198 gcov_type
* __gcov_indirect_call_counters
;
1200 /* By default, the C++ compiler will use function addresses in the
1201 vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero
1202 tells the compiler to use function descriptors instead. The value
1203 of this macro says how many words wide the descriptor is (normally 2),
1204 but it may be dependent on target flags. Since we do not have access
1205 to the target flags here we just check to see if it is set and use
1206 that to set VTABLE_USES_DESCRIPTORS to 0 or 1.
1208 It is assumed that the address of a function descriptor may be treated
1209 as a pointer to a function. */
1211 #ifdef TARGET_VTABLE_USES_DESCRIPTORS
1212 #define VTABLE_USES_DESCRIPTORS 1
1214 #define VTABLE_USES_DESCRIPTORS 0
1217 /* Tries to determine the most common value among its inputs. */
1219 __gcov_indirect_call_profiler_v2 (gcov_type value
, void* cur_func
)
1221 /* If the C++ virtual tables contain function descriptors then one
1222 function may have multiple descriptors and we need to dereference
1223 the descriptors to see if they point to the same function. */
1224 if (cur_func
== __gcov_indirect_call_callee
1225 || (VTABLE_USES_DESCRIPTORS
&& __gcov_indirect_call_callee
1226 && *(void **) cur_func
== *(void **) __gcov_indirect_call_callee
))
1227 __gcov_one_value_profiler_body (__gcov_indirect_call_counters
, value
);
1231 #ifdef L_gcov_time_profiler
1233 /* Sets corresponding COUNTERS if there is no value. */
1236 __gcov_time_profiler (gcov_type
* counters
)
1239 counters
[0] = ++function_counter
;
1243 #ifdef L_gcov_average_profiler
1244 /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
1248 __gcov_average_profiler (gcov_type
*counters
, gcov_type value
)
1250 counters
[0] += value
;
1255 #ifdef L_gcov_ior_profiler
1256 /* Bitwise-OR VALUE into COUNTER. */
1259 __gcov_ior_profiler (gcov_type
*counters
, gcov_type value
)
1266 /* A wrapper for the fork function. Flushes the accumulated profiling data, so
1267 that they are not counted twice. */
1273 extern __gthread_mutex_t __gcov_flush_mx
;
1277 __GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_flush_mx
);
1283 /* A wrapper for the execl function. Flushes the accumulated profiling data, so
1284 that they are not lost. */
1287 __gcov_execl (const char *path
, char *arg
, ...)
1299 while (va_arg (ap
, char *))
1303 args
= (char **) alloca (length
* sizeof (void *));
1305 for (i
= 1; i
< length
; i
++)
1306 args
[i
] = va_arg (aq
, char *);
1309 return execv (path
, args
);
1313 #ifdef L_gcov_execlp
1314 /* A wrapper for the execlp function. Flushes the accumulated profiling data, so
1315 that they are not lost. */
1318 __gcov_execlp (const char *path
, char *arg
, ...)
1330 while (va_arg (ap
, char *))
1334 args
= (char **) alloca (length
* sizeof (void *));
1336 for (i
= 1; i
< length
; i
++)
1337 args
[i
] = va_arg (aq
, char *);
1340 return execvp (path
, args
);
1344 #ifdef L_gcov_execle
1345 /* A wrapper for the execle function. Flushes the accumulated profiling data, so
1346 that they are not lost. */
1349 __gcov_execle (const char *path
, char *arg
, ...)
1362 while (va_arg (ap
, char *))
1366 args
= (char **) alloca (length
* sizeof (void *));
1368 for (i
= 1; i
< length
; i
++)
1369 args
[i
] = va_arg (aq
, char *);
1370 envp
= va_arg (aq
, char **);
1373 return execve (path
, args
, envp
);
1378 /* A wrapper for the execv function. Flushes the accumulated profiling data, so
1379 that they are not lost. */
1382 __gcov_execv (const char *path
, char *const argv
[])
1385 return execv (path
, argv
);
1389 #ifdef L_gcov_execvp
1390 /* A wrapper for the execvp function. Flushes the accumulated profiling data, so
1391 that they are not lost. */
1394 __gcov_execvp (const char *path
, char *const argv
[])
1397 return execvp (path
, argv
);
1401 #ifdef L_gcov_execve
1402 /* A wrapper for the execve function. Flushes the accumulated profiling data, so
1403 that they are not lost. */
1406 __gcov_execve (const char *path
, char *const argv
[], char *const envp
[])
1409 return execve (path
, argv
, envp
);
1412 #endif /* inhibit_libc */