+2013-11-12 Balaji V. Iyer <balaji.v.iyer@intel.com>
[official-gcc.git] / libgcc / libgcov.c
blob6450fd7654867775fab7418c43b736ff66a721cc
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
10 version.
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
15 for more details.
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/>. */
26 #include "tconfig.h"
27 #include "tsystem.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "libgcc_tm.h"
31 #include "gthr.h"
33 #if defined(inhibit_libc)
34 #define IN_LIBGCOV (-1)
35 #else
36 #define IN_LIBGCOV 1
37 #if defined(L_gcov)
38 #define GCOV_LINKAGE /* nothing */
39 #endif
40 #endif
41 #include "gcov-io.h"
43 #if defined(inhibit_libc)
44 /* If libc and its header files are not available, provide dummy functions. */
46 #ifdef L_gcov
47 void __gcov_init (struct gcov_info *p __attribute__ ((unused))) {}
48 void __gcov_flush (void) {}
49 #endif
51 #ifdef L_gcov_reset
52 void __gcov_reset (void) {}
53 #endif
55 #ifdef L_gcov_dump
56 void __gcov_dump (void) {}
57 #endif
59 #ifdef L_gcov_merge_add
60 void __gcov_merge_add (gcov_type *counters __attribute__ ((unused)),
61 unsigned n_counters __attribute__ ((unused))) {}
62 #endif
64 #ifdef L_gcov_merge_single
65 void __gcov_merge_single (gcov_type *counters __attribute__ ((unused)),
66 unsigned n_counters __attribute__ ((unused))) {}
67 #endif
69 #ifdef L_gcov_merge_delta
70 void __gcov_merge_delta (gcov_type *counters __attribute__ ((unused)),
71 unsigned n_counters __attribute__ ((unused))) {}
72 #endif
74 #else
76 #include <string.h>
77 #if GCOV_LOCKED
78 #include <fcntl.h>
79 #include <errno.h>
80 #include <sys/stat.h>
81 #endif
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;
88 #ifdef L_gcov
89 #include "gcov-io.c"
91 struct gcov_fn_buffer
93 struct gcov_fn_buffer *next;
94 unsigned fn_ix;
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. */
118 static int
119 create_file_directory (char *filename)
121 #if !defined(TARGET_POSIX_IO) && !defined(_WIN32)
122 (void) filename;
123 return -1;
124 #else
125 char *s;
127 s = filename;
129 if (HAS_DRIVE_SPEC(s))
130 s += 2;
131 if (IS_DIR_SEPARATOR(*s))
132 ++s;
133 for (; *s != '\0'; s++)
134 if (IS_DIR_SEPARATOR(*s))
136 char sep = *s;
137 *s = '\0';
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
143 #else
144 && mkdir (filename) == -1
145 #endif
146 /* The directory might have been made by another process. */
147 && errno != EEXIST)
149 fprintf (stderr, "profiling:%s:Cannot create directory\n",
150 filename);
151 *s = sep;
152 return -1;
155 *s = sep;
157 return 0;
158 #endif
161 static struct gcov_fn_buffer *
162 free_fn_data (const struct gcov_info *gi_ptr, struct gcov_fn_buffer *buffer,
163 unsigned limit)
165 struct gcov_fn_buffer *next;
166 unsigned ix, n_ctr = 0;
168 if (!buffer)
169 return 0;
170 next = buffer->next;
172 for (ix = 0; ix != limit; ix++)
173 if (gi_ptr->merge[ix])
174 free (buffer->info.ctrs[n_ctr++].values);
175 free (buffer);
176 return next;
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;
185 unsigned len;
187 for (ix = GCOV_COUNTERS; ix--;)
188 if (gi_ptr->merge[ix])
189 n_ctrs++;
191 len = sizeof (*fn_buffer) + sizeof (fn_buffer->info.ctrs[0]) * n_ctrs;
192 fn_buffer = (struct gcov_fn_buffer *)malloc (len);
194 if (!fn_buffer)
195 goto fail;
197 fn_buffer->next = 0;
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;
206 gcov_type *values;
208 if (!gi_ptr->merge[ix])
209 continue;
211 if (gcov_read_unsigned () != GCOV_TAG_FOR_COUNTER (ix))
213 len = 0;
214 goto fail;
217 length = GCOV_TAG_COUNTER_NUM (gcov_read_unsigned ());
218 len = length * sizeof (gcov_type);
219 values = (gcov_type *)malloc (len);
220 if (!values)
221 goto fail;
223 fn_buffer->info.ctrs[n_ctrs].num = length;
224 fn_buffer->info.ctrs[n_ctrs].values = values;
226 while (length--)
227 *values++ = gcov_read_counter ();
228 n_ctrs++;
231 *end_ptr = fn_buffer;
232 return &fn_buffer->next;
234 fail:
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)
246 unsigned ix;
248 for (ix = 32; ix--; value <<= 1)
250 unsigned feedback;
252 feedback = (value ^ crc32) & 0x80000000 ? 0x04c11db7 : 0;
253 crc32 <<= 1;
254 crc32 ^= feedback;
257 return crc32;
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. */
265 static int
266 gcov_version (struct gcov_info *ptr, gcov_unsigned_t version,
267 const char *filename)
269 if (version != GCOV_VERSION)
271 char v[4], e[4];
273 GCOV_UNSIGNED2STRING (v, version);
274 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
276 fprintf (stderr,
277 "profiling:%s:Version mismatch - expected %.4s got %.4s\n",
278 filename? filename : ptr->filename, e, v);
279 return 0;
281 return 1;
284 /* Insert counter VALUE into HISTOGRAM. */
286 static void
287 gcov_histogram_insert(gcov_bucket_type *histogram, gcov_type value)
289 unsigned i;
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. */
300 static void
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;
308 int h_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]);
315 if (!cs_ptr->num)
316 return;
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])
330 continue;
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])
338 ctr_info_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)
345 continue;
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. */
365 void
366 gcov_exit (void)
368 struct gcov_info *gi_ptr;
369 const struct gcov_fn_info *gfi_ptr;
370 struct gcov_summary this_prg; /* summary for program. */
371 #if !GCOV_LOCKED
372 struct gcov_summary all_prg; /* summary for all instances of program. */
373 #endif
374 struct gcov_ctr_summary *cs_ptr;
375 const struct gcov_ctr_info *ci_ptr;
376 unsigned t_ix;
377 int f_ix;
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)
388 return;
390 #if !GCOV_LOCKED
391 memset (&all_prg, 0, sizeof (all_prg));
392 #endif
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)
405 gfi_ptr = 0;
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);
410 if (!gfi_ptr)
411 continue;
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])
417 continue;
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];
429 ci_ptr++;
433 gcov_compute_histogram (&this_prg);
436 /* Check if the level of dirs to strip off specified. */
437 char *tmp = getenv("GCOV_PREFIX_STRIP");
438 if (tmp)
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");
449 if (gcov_prefix)
451 prefix_length = strlen(gcov_prefix);
453 /* Remove an unnecessary trailing '/' */
454 if (IS_DIR_SEPARATOR (gcov_prefix[prefix_length - 1]))
455 prefix_length--;
457 else
458 prefix_length = 0;
460 /* If no prefix was specified and a prefix stip, then we assume
461 relative. */
462 if (gcov_prefix_strip != 0 && prefix_length == 0)
464 gcov_prefix = ".";
465 prefix_length = 1;
467 /* Allocate and initialize the filename scratch space plus one. */
468 gi_filename = (char *) alloca (prefix_length + gcov_max_filename + 2);
469 if (prefix_length)
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)
476 unsigned n_counts;
477 struct gcov_summary prg; /* summary for this object over all
478 program. */
479 struct gcov_ctr_summary *cs_prg, *cs_tprg;
480 #if !GCOV_LOCKED
481 struct gcov_ctr_summary *cs_all;
482 #endif
483 int error = 0;
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))
497 fname += 2;
499 /* Build relocated filename, stripping off leading
500 directories from the initial filename if requested. */
501 if (gcov_prefix_strip > 0)
503 int level = 0;
504 s = fname;
505 if (IS_DIR_SEPARATOR(*s))
506 ++s;
508 /* Skip selected directory levels. */
509 for (; (*s != '\0') && (level < gcov_prefix_strip); s++)
510 if (IS_DIR_SEPARATOR(*s))
512 fname = s;
513 level++;
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);
524 else
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);
534 continue;
536 if (!gcov_open (gi_filename))
538 fprintf (stderr, "profiling:%s:Cannot open\n", gi_filename);
539 continue;
543 tag = gcov_read_unsigned ();
544 if (tag)
546 /* Merge data from file. */
547 if (tag != GCOV_DATA_MAGIC)
549 fprintf (stderr, "profiling:%s:Not a gcov data file\n",
550 gi_filename);
551 goto read_fatal;
553 length = gcov_read_unsigned ();
554 if (!gcov_version (gi_ptr, length, gi_filename))
555 goto read_fatal;
557 length = gcov_read_unsigned ();
558 if (length != gi_ptr->stamp)
559 /* Read from a different compilation. Overwrite the file. */
560 goto rewrite;
562 /* Look for program summary. */
563 for (f_ix = 0;;)
565 struct gcov_summary tmp;
567 eof_pos = gcov_position ();
568 tag = gcov_read_unsigned ();
569 if (tag != GCOV_TAG_PROGRAM_SUMMARY)
570 break;
572 f_ix--;
573 length = gcov_read_unsigned ();
574 gcov_read_summary (&tmp);
575 if ((error = gcov_is_error ()))
576 goto read_error;
577 if (summary_pos)
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);
589 goto next_summary;
591 if (tmp.checksum != crc32)
592 goto next_summary;
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)
596 goto next_summary;
597 prg = tmp;
598 summary_pos = eof_pos;
600 next_summary:;
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)
610 goto read_mismatch;
612 length = gcov_read_unsigned ();
613 if (!length)
614 /* This function did not appear in the other program.
615 We have nothing to merge. */
616 continue;
618 if (length != GCOV_TAG_FUNCTION_LENGTH)
619 goto read_mismatch;
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
627 file. */
628 fn_tail = buffer_fn_data (gi_filename,
629 gi_ptr, fn_tail, f_ix);
630 if (!fn_tail)
631 goto read_mismatch;
632 continue;
635 length = gcov_read_unsigned ();
636 if (length != gfi_ptr->ident)
637 goto read_mismatch;
639 length = gcov_read_unsigned ();
640 if (length != gfi_ptr->lineno_checksum)
641 goto read_mismatch;
643 length = gcov_read_unsigned ();
644 if (length != gfi_ptr->cfg_checksum)
645 goto read_mismatch;
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];
652 if (!merge)
653 continue;
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))
659 goto read_mismatch;
660 (*merge) (ci_ptr->values, ci_ptr->num);
661 ci_ptr++;
663 if ((error = gcov_is_error ()))
664 goto read_error;
667 if (tag)
669 read_mismatch:;
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);
673 goto read_fatal;
676 goto rewrite;
678 read_error:;
679 fprintf (stderr, "profiling:%s:%s merging\n", gi_filename,
680 error < 0 ? "Overflow": "Error");
682 goto read_fatal;
684 rewrite:;
685 gcov_rewrite ();
686 if (!summary_pos)
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])
700 if (!cs_prg->runs++)
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);
709 else
710 gcov_histogram_merge (cs_prg->histogram, cs_tprg->histogram);
712 else if (cs_prg->runs)
713 goto read_mismatch;
715 #if !GCOV_LOCKED
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
729 merge. */
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))
736 fprintf (stderr,
737 "profiling:%s:Data file mismatch - some data files may "
738 "have been concurrently updated without locking support\n",
739 gi_filename);
740 all_prg.checksum = ~0u;
742 #endif
745 prg.checksum = crc32;
747 /* Write out the data. */
748 if (!eof_pos)
750 gcov_write_tag_length (GCOV_DATA_MAGIC, GCOV_VERSION);
751 gcov_write_unsigned (gi_ptr->stamp);
754 if (summary_pos)
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
763 merging. */
765 while (sum_buffer)
767 gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY, &sum_buffer->summary);
768 next_sum_buffer = sum_buffer->next;
769 free (sum_buffer);
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. */
781 buffered = 1;
782 gfi_ptr = &fn_buffer->info;
783 length = GCOV_TAG_FUNCTION_LENGTH;
785 else
787 gfi_ptr = gi_ptr->functions[f_ix];
788 if (gfi_ptr && gfi_ptr->key == gi_ptr)
789 length = GCOV_TAG_FUNCTION_LENGTH;
790 else
791 length = 0;
794 gcov_write_tag_length (GCOV_TAG_FUNCTION, length);
795 if (!length)
796 continue;
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])
806 continue;
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;
812 while (n_counts--)
813 gcov_write_counter (*c_ptr++);
814 ci_ptr++;
816 if (buffered)
817 fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
820 gcov_write_unsigned (0);
822 read_fatal:;
823 while (fn_buffer)
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",
830 gi_filename);
834 /* Reset all counters to zero. */
836 void
837 gcov_clear (void)
839 const struct gcov_info *gi_ptr;
841 for (gi_ptr = gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
843 unsigned f_ix;
845 for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
847 unsigned t_ix;
848 const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix];
850 if (!gfi_ptr || gfi_ptr->key != gi_ptr)
851 continue;
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])
856 continue;
858 memset (ci_ptr->values, 0, sizeof (gcov_type) * ci_ptr->num);
859 ci_ptr++;
865 /* Add a new object file onto the bb chain. Invoked automatically
866 when running an object file's global ctors. */
868 void
869 __gcov_init (struct gcov_info *info)
871 if (!info->version || !info->n_functions)
872 return;
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;
881 if (!gcov_list)
882 atexit (gcov_exit);
884 info->next = gcov_list;
885 gcov_list = info;
887 info->version = 0;
890 #ifdef __GTHREAD_MUTEX_INIT
891 ATTRIBUTE_HIDDEN __gthread_mutex_t __gcov_flush_mx = __GTHREAD_MUTEX_INIT;
892 #define init_mx_once()
893 #else
894 __gthread_mutex_t __gcov_flush_mx ATTRIBUTE_HIDDEN;
896 static void
897 init_mx (void)
899 __GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_flush_mx);
901 static void
902 init_mx_once (void)
904 static __gthread_once_t once = __GTHREAD_ONCE_INIT;
905 __gthread_once (&once, init_mx);
907 #endif
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. */
913 void
914 __gcov_flush (void)
916 init_mx_once ();
917 __gthread_mutex_lock (&__gcov_flush_mx);
919 gcov_exit ();
920 gcov_clear ();
922 __gthread_mutex_unlock (&__gcov_flush_mx);
925 #endif /* L_gcov */
927 #ifdef L_gcov_reset
929 /* Function that can be called from application to reset counters to zero,
930 in order to collect profile in region of interest. */
932 void
933 __gcov_reset (void)
935 gcov_clear ();
936 /* Re-enable dumping to support collecting profile in multiple regions
937 of interest. */
938 gcov_dump_complete = 0;
941 #endif /* L_gcov_reset */
943 #ifdef L_gcov_dump
945 /* Function that can be called from application to write profile collected
946 so far, in order to collect profile in region of interest. */
948 void
949 __gcov_dump (void)
951 gcov_exit ();
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. */
962 void
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. */
974 void
975 __gcov_merge_ior (gcov_type *counters, unsigned n_counters)
977 for (; n_counters; counters++, n_counters--)
978 *counters |= gcov_read_counter ();
980 #endif
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
987 void
988 __gcov_merge_time_profile (gcov_type *counters, unsigned n_counters)
990 unsigned int i;
991 gcov_type value;
993 for (i = 0; i < n_counters; i++)
995 value = gcov_read_counter ();
997 if (value && (!counters[i] || value < counters[i]))
998 counters[i] = value;
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
1008 meanings:
1010 -- the stored candidate on the most common value of the measured entity
1011 -- counter
1012 -- total number of evaluations of the value */
1013 void
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];
1034 else
1035 counters[1] -= counter;
1036 counters[2] += all;
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
1050 -- counter
1051 -- total number of evaluations of the value */
1052 void
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];
1074 else
1075 counters[2] -= counter;
1076 counters[3] += all;
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
1085 instead. */
1087 void
1088 __gcov_interval_profiler (gcov_type *counters, gcov_type value,
1089 int start, unsigned steps)
1091 gcov_type delta = value - start;
1092 if (delta < 0)
1093 counters[steps + 1]++;
1094 else if (delta >= steps)
1095 counters[steps]++;
1096 else
1097 counters[delta]++;
1099 #endif
1101 #ifdef L_gcov_pow2_profiler
1102 /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
1103 COUNTERS[0] is incremented. */
1105 void
1106 __gcov_pow2_profiler (gcov_type *counters, gcov_type value)
1108 if (value & (value - 1))
1109 counters[0]++;
1110 else
1111 counters[1]++;
1113 #endif
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. */
1125 static inline void
1126 __gcov_one_value_profiler_body (gcov_type *counters, gcov_type value)
1128 if (value == counters[0])
1129 counters[1]++;
1130 else if (counters[1] == 0)
1132 counters[1] = 1;
1133 counters[0] = value;
1135 else
1136 counters[1]--;
1137 counters[2]++;
1140 #ifdef L_gcov_one_value_profiler
1141 void
1142 __gcov_one_value_profiler (gcov_type *counters, gcov_type value)
1144 __gcov_one_value_profiler_body (counters, value);
1146 #endif
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
1165 #else
1166 #define VTABLE_USES_DESCRIPTORS 0
1167 #endif
1169 /* Tries to determine the most common value among its inputs. */
1170 void
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);
1183 #endif
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)
1192 __thread
1193 #endif
1194 void * __gcov_indirect_call_callee;
1195 #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
1196 __thread
1197 #endif
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
1213 #else
1214 #define VTABLE_USES_DESCRIPTORS 0
1215 #endif
1217 /* Tries to determine the most common value among its inputs. */
1218 void
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);
1229 #endif
1231 #ifdef L_gcov_time_profiler
1233 /* Sets corresponding COUNTERS if there is no value. */
1235 void
1236 __gcov_time_profiler (gcov_type* counters)
1238 if (!counters[0])
1239 counters[0] = ++function_counter;
1241 #endif
1243 #ifdef L_gcov_average_profiler
1244 /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
1245 to saturate up. */
1247 void
1248 __gcov_average_profiler (gcov_type *counters, gcov_type value)
1250 counters[0] += value;
1251 counters[1] ++;
1253 #endif
1255 #ifdef L_gcov_ior_profiler
1256 /* Bitwise-OR VALUE into COUNTER. */
1258 void
1259 __gcov_ior_profiler (gcov_type *counters, gcov_type value)
1261 *counters |= value;
1263 #endif
1265 #ifdef L_gcov_fork
1266 /* A wrapper for the fork function. Flushes the accumulated profiling data, so
1267 that they are not counted twice. */
1269 pid_t
1270 __gcov_fork (void)
1272 pid_t pid;
1273 extern __gthread_mutex_t __gcov_flush_mx;
1274 __gcov_flush ();
1275 pid = fork ();
1276 if (pid == 0)
1277 __GTHREAD_MUTEX_INIT_FUNCTION (&__gcov_flush_mx);
1278 return pid;
1280 #endif
1282 #ifdef L_gcov_execl
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, ...)
1289 va_list ap, aq;
1290 unsigned i, length;
1291 char **args;
1293 __gcov_flush ();
1295 va_start (ap, arg);
1296 va_copy (aq, ap);
1298 length = 2;
1299 while (va_arg (ap, char *))
1300 length++;
1301 va_end (ap);
1303 args = (char **) alloca (length * sizeof (void *));
1304 args[0] = arg;
1305 for (i = 1; i < length; i++)
1306 args[i] = va_arg (aq, char *);
1307 va_end (aq);
1309 return execv (path, args);
1311 #endif
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, ...)
1320 va_list ap, aq;
1321 unsigned i, length;
1322 char **args;
1324 __gcov_flush ();
1326 va_start (ap, arg);
1327 va_copy (aq, ap);
1329 length = 2;
1330 while (va_arg (ap, char *))
1331 length++;
1332 va_end (ap);
1334 args = (char **) alloca (length * sizeof (void *));
1335 args[0] = arg;
1336 for (i = 1; i < length; i++)
1337 args[i] = va_arg (aq, char *);
1338 va_end (aq);
1340 return execvp (path, args);
1342 #endif
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, ...)
1351 va_list ap, aq;
1352 unsigned i, length;
1353 char **args;
1354 char **envp;
1356 __gcov_flush ();
1358 va_start (ap, arg);
1359 va_copy (aq, ap);
1361 length = 2;
1362 while (va_arg (ap, char *))
1363 length++;
1364 va_end (ap);
1366 args = (char **) alloca (length * sizeof (void *));
1367 args[0] = arg;
1368 for (i = 1; i < length; i++)
1369 args[i] = va_arg (aq, char *);
1370 envp = va_arg (aq, char **);
1371 va_end (aq);
1373 return execve (path, args, envp);
1375 #endif
1377 #ifdef L_gcov_execv
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[])
1384 __gcov_flush ();
1385 return execv (path, argv);
1387 #endif
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[])
1396 __gcov_flush ();
1397 return execvp (path, argv);
1399 #endif
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[])
1408 __gcov_flush ();
1409 return execve (path, argv, envp);
1411 #endif
1412 #endif /* inhibit_libc */