1 /* File format for coverage information
2 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2007,
3 2008 Free Software Foundation, Inc.
4 Contributed by Bob Manson <manson@cygnus.com>.
5 Completely remangled by Nathan Sidwell <nathan@codesourcery.com>.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
28 /* Routines declared in gcov-io.h. This file should be #included by
29 another source file, after having #included gcov-io.h. */
32 static void gcov_write_block (unsigned);
33 static gcov_unsigned_t
*gcov_write_words (unsigned);
35 static const gcov_unsigned_t
*gcov_read_words (unsigned);
37 static void gcov_allocate (unsigned);
40 static inline gcov_unsigned_t
from_file (gcov_unsigned_t value
)
45 value
= (value
>> 16) | (value
<< 16);
46 value
= ((value
& 0xff00ff) << 8) | ((value
>> 8) & 0xff00ff);
52 /* Open a gcov file. NAME is the name of the file to open and MODE
53 indicates whether a new file should be created, or an existing file
54 opened. If MODE is >= 0 an existing file will be opened, if
55 possible, and if MODE is <= 0, a new file will be created. Use
56 MODE=0 to attempt to reopen an existing file and then fall back on
57 creating a new one. If MODE < 0, the file will be opened in
58 read-only mode. Otherwise it will be opened for modification.
59 Return zero on failure, >0 on opening an existing file and <0 on
60 creating a new one. */
64 gcov_open (const char *name
)
66 gcov_open (const char *name
, int mode
)
76 s_flock
.l_whence
= SEEK_SET
;
78 s_flock
.l_len
= 0; /* Until EOF. */
79 s_flock
.l_pid
= getpid ();
82 gcc_assert (!gcov_var
.file
);
84 gcov_var
.offset
= gcov_var
.length
= 0;
85 gcov_var
.overread
= -1u;
93 /* Read-only mode - acquire a read-lock. */
94 s_flock
.l_type
= F_RDLCK
;
95 /* pass mode (ignored) for compatibility */
96 fd
= open (name
, O_RDONLY
, S_IRUSR
| S_IWUSR
);
100 /* Write mode - acquire a write-lock. */
101 s_flock
.l_type
= F_WRLCK
;
102 fd
= open (name
, O_RDWR
| O_CREAT
, 0666);
107 while (fcntl (fd
, F_SETLKW
, &s_flock
) && errno
== EINTR
)
110 gcov_var
.file
= fdopen (fd
, (mode
> 0) ? "rb" : "r+b");
124 if (fstat (fd
, &st
) < 0)
126 fclose (gcov_var
.file
);
133 gcov_var
.mode
= mode
* 2 + 1;
136 gcov_var
.mode
= mode
* 2 + 1;
139 gcov_var
.file
= fopen (name
, (mode
> 0) ? "rb" : "r+b");
145 gcov_var
.file
= fopen (name
, "w+b");
147 gcov_var
.mode
= mode
* 2 + 1;
153 setbuf (gcov_var
.file
, (char *)0);
158 /* Close the current gcov file. Flushes data to disk. Returns nonzero
159 on failure or error flag set. */
167 if (gcov_var
.offset
&& gcov_var
.mode
< 0)
168 gcov_write_block (gcov_var
.offset
);
170 fclose (gcov_var
.file
);
175 free (gcov_var
.buffer
);
180 return gcov_var
.error
;
184 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
185 file. Returns +1 for same endian, -1 for other endian and zero for
189 gcov_magic (gcov_unsigned_t magic
, gcov_unsigned_t expected
)
191 if (magic
== expected
)
193 magic
= (magic
>> 16) | (magic
<< 16);
194 magic
= ((magic
& 0xff00ff) << 8) | ((magic
>> 8) & 0xff00ff);
195 if (magic
== expected
)
206 gcov_allocate (unsigned length
)
208 size_t new_size
= gcov_var
.alloc
;
211 new_size
= GCOV_BLOCK_SIZE
;
215 gcov_var
.alloc
= new_size
;
216 gcov_var
.buffer
= XRESIZEVAR (gcov_unsigned_t
, gcov_var
.buffer
, new_size
<< 2);
221 /* Write out the current block, if needs be. */
224 gcov_write_block (unsigned size
)
226 if (fwrite (gcov_var
.buffer
, size
<< 2, 1, gcov_var
.file
) != 1)
228 gcov_var
.start
+= size
;
229 gcov_var
.offset
-= size
;
232 /* Allocate space to write BYTES bytes to the gcov file. Return a
233 pointer to those bytes, or NULL on failure. */
235 static gcov_unsigned_t
*
236 gcov_write_words (unsigned words
)
238 gcov_unsigned_t
*result
;
240 gcc_assert (gcov_var
.mode
< 0);
242 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
244 gcov_write_block (GCOV_BLOCK_SIZE
);
247 gcc_assert (gcov_var
.offset
== 1);
248 memcpy (gcov_var
.buffer
, gcov_var
.buffer
+ GCOV_BLOCK_SIZE
, 4);
252 if (gcov_var
.offset
+ words
> gcov_var
.alloc
)
253 gcov_allocate (gcov_var
.offset
+ words
);
255 result
= &gcov_var
.buffer
[gcov_var
.offset
];
256 gcov_var
.offset
+= words
;
261 /* Write unsigned VALUE to coverage file. Sets error flag
265 gcov_write_unsigned (gcov_unsigned_t value
)
267 gcov_unsigned_t
*buffer
= gcov_write_words (1);
272 /* Write counter VALUE to coverage file. Sets error flag
277 gcov_write_counter (gcov_type value
)
279 gcov_unsigned_t
*buffer
= gcov_write_words (2);
281 buffer
[0] = (gcov_unsigned_t
) value
;
282 if (sizeof (value
) > sizeof (gcov_unsigned_t
))
283 buffer
[1] = (gcov_unsigned_t
) (value
>> 32);
287 #endif /* IN_LIBGCOV */
290 /* Write STRING to coverage file. Sets error flag on file
291 error, overflow flag on overflow */
294 gcov_write_string (const char *string
)
298 gcov_unsigned_t
*buffer
;
302 length
= strlen (string
);
303 alloc
= (length
+ 4) >> 2;
306 buffer
= gcov_write_words (1 + alloc
);
310 memcpy (&buffer
[1], string
, length
);
315 /* Write a tag TAG and reserve space for the record length. Return a
316 value to be used for gcov_write_length. */
318 GCOV_LINKAGE gcov_position_t
319 gcov_write_tag (gcov_unsigned_t tag
)
321 gcov_position_t result
= gcov_var
.start
+ gcov_var
.offset
;
322 gcov_unsigned_t
*buffer
= gcov_write_words (2);
330 /* Write a record length using POSITION, which was returned by
331 gcov_write_tag. The current file position is the end of the
332 record, and is restored before returning. Returns nonzero on
336 gcov_write_length (gcov_position_t position
)
339 gcov_unsigned_t length
;
340 gcov_unsigned_t
*buffer
;
342 gcc_assert (gcov_var
.mode
< 0);
343 gcc_assert (position
+ 2 <= gcov_var
.start
+ gcov_var
.offset
);
344 gcc_assert (position
>= gcov_var
.start
);
345 offset
= position
- gcov_var
.start
;
346 length
= gcov_var
.offset
- offset
- 2;
347 buffer
= (gcov_unsigned_t
*) &gcov_var
.buffer
[offset
];
349 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
350 gcov_write_block (gcov_var
.offset
);
353 #else /* IN_LIBGCOV */
355 /* Write a tag TAG and length LENGTH. */
358 gcov_write_tag_length (gcov_unsigned_t tag
, gcov_unsigned_t length
)
360 gcov_unsigned_t
*buffer
= gcov_write_words (2);
366 /* Write a summary structure to the gcov file. Return nonzero on
370 gcov_write_summary (gcov_unsigned_t tag
, const struct gcov_summary
*summary
)
372 unsigned ix
, h_ix
, bv_ix
, h_cnt
= 0;
373 const struct gcov_ctr_summary
*csum
;
374 unsigned histo_bitvector
[GCOV_HISTOGRAM_BITVECTOR_SIZE
];
376 /* Count number of non-zero histogram entries, and fill in a bit vector
377 of non-zero indices. The histogram is only currently computed for arc
379 for (bv_ix
= 0; bv_ix
< GCOV_HISTOGRAM_BITVECTOR_SIZE
; bv_ix
++)
380 histo_bitvector
[bv_ix
] = 0;
381 csum
= &summary
->ctrs
[GCOV_COUNTER_ARCS
];
382 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
384 if (csum
->histogram
[h_ix
].num_counters
> 0)
386 histo_bitvector
[h_ix
/ 32] |= 1 << (h_ix
% 32);
390 gcov_write_tag_length (tag
, GCOV_TAG_SUMMARY_LENGTH(h_cnt
));
391 gcov_write_unsigned (summary
->checksum
);
392 for (csum
= summary
->ctrs
, ix
= GCOV_COUNTERS_SUMMABLE
; ix
--; csum
++)
394 gcov_write_unsigned (csum
->num
);
395 gcov_write_unsigned (csum
->runs
);
396 gcov_write_counter (csum
->sum_all
);
397 gcov_write_counter (csum
->run_max
);
398 gcov_write_counter (csum
->sum_max
);
399 if (ix
!= GCOV_COUNTER_ARCS
)
401 for (bv_ix
= 0; bv_ix
< GCOV_HISTOGRAM_BITVECTOR_SIZE
; bv_ix
++)
402 gcov_write_unsigned (0);
405 for (bv_ix
= 0; bv_ix
< GCOV_HISTOGRAM_BITVECTOR_SIZE
; bv_ix
++)
406 gcov_write_unsigned (histo_bitvector
[bv_ix
]);
407 for (h_ix
= 0; h_ix
< GCOV_HISTOGRAM_SIZE
; h_ix
++)
409 if (!csum
->histogram
[h_ix
].num_counters
)
411 gcov_write_unsigned (csum
->histogram
[h_ix
].num_counters
);
412 gcov_write_counter (csum
->histogram
[h_ix
].min_value
);
413 gcov_write_counter (csum
->histogram
[h_ix
].cum_value
);
417 #endif /* IN_LIBGCOV */
421 /* Return a pointer to read BYTES bytes from the gcov file. Returns
422 NULL on failure (read past EOF). */
424 static const gcov_unsigned_t
*
425 gcov_read_words (unsigned words
)
427 const gcov_unsigned_t
*result
;
428 unsigned excess
= gcov_var
.length
- gcov_var
.offset
;
430 gcc_assert (gcov_var
.mode
> 0);
433 gcov_var
.start
+= gcov_var
.offset
;
437 gcc_assert (excess
== 1);
438 memcpy (gcov_var
.buffer
, gcov_var
.buffer
+ gcov_var
.offset
, 4);
441 memmove (gcov_var
.buffer
, gcov_var
.buffer
+ gcov_var
.offset
, excess
* 4);
444 gcov_var
.length
= excess
;
446 gcc_assert (!gcov_var
.length
|| gcov_var
.length
== 1);
447 excess
= GCOV_BLOCK_SIZE
;
449 if (gcov_var
.length
+ words
> gcov_var
.alloc
)
450 gcov_allocate (gcov_var
.length
+ words
);
451 excess
= gcov_var
.alloc
- gcov_var
.length
;
453 excess
= fread (gcov_var
.buffer
+ gcov_var
.length
,
454 1, excess
<< 2, gcov_var
.file
) >> 2;
455 gcov_var
.length
+= excess
;
456 if (gcov_var
.length
< words
)
458 gcov_var
.overread
+= words
- gcov_var
.length
;
463 result
= &gcov_var
.buffer
[gcov_var
.offset
];
464 gcov_var
.offset
+= words
;
468 /* Read unsigned value from a coverage file. Sets error flag on file
469 error, overflow flag on overflow */
471 GCOV_LINKAGE gcov_unsigned_t
472 gcov_read_unsigned (void)
474 gcov_unsigned_t value
;
475 const gcov_unsigned_t
*buffer
= gcov_read_words (1);
479 value
= from_file (buffer
[0]);
483 /* Read counter value from a coverage file. Sets error flag on file
484 error, overflow flag on overflow */
486 GCOV_LINKAGE gcov_type
487 gcov_read_counter (void)
490 const gcov_unsigned_t
*buffer
= gcov_read_words (2);
494 value
= from_file (buffer
[0]);
495 if (sizeof (value
) > sizeof (gcov_unsigned_t
))
496 value
|= ((gcov_type
) from_file (buffer
[1])) << 32;
503 /* Read string from coverage file. Returns a pointer to a static
504 buffer, or NULL on empty string. You must copy the string before
505 calling another gcov function. */
508 GCOV_LINKAGE
const char *
509 gcov_read_string (void)
511 unsigned length
= gcov_read_unsigned ();
516 return (const char *) gcov_read_words (length
);
521 gcov_read_summary (struct gcov_summary
*summary
)
523 unsigned ix
, h_ix
, bv_ix
, h_cnt
= 0;
524 struct gcov_ctr_summary
*csum
;
525 unsigned histo_bitvector
[GCOV_HISTOGRAM_BITVECTOR_SIZE
];
526 unsigned cur_bitvector
;
528 summary
->checksum
= gcov_read_unsigned ();
529 for (csum
= summary
->ctrs
, ix
= GCOV_COUNTERS_SUMMABLE
; ix
--; csum
++)
531 csum
->num
= gcov_read_unsigned ();
532 csum
->runs
= gcov_read_unsigned ();
533 csum
->sum_all
= gcov_read_counter ();
534 csum
->run_max
= gcov_read_counter ();
535 csum
->sum_max
= gcov_read_counter ();
536 memset (csum
->histogram
, 0,
537 sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
538 for (bv_ix
= 0; bv_ix
< GCOV_HISTOGRAM_BITVECTOR_SIZE
; bv_ix
++)
540 histo_bitvector
[bv_ix
] = gcov_read_unsigned ();
541 h_cnt
+= __builtin_popcountll (histo_bitvector
[bv_ix
]);
548 /* Find the index corresponding to the next entry we will read in.
549 First find the next non-zero bitvector and re-initialize
550 the histogram index accordingly, then right shift and increment
551 the index until we find a set bit. */
552 while (!cur_bitvector
)
555 gcc_assert(bv_ix
< GCOV_HISTOGRAM_BITVECTOR_SIZE
);
556 cur_bitvector
= histo_bitvector
[bv_ix
++];
558 while (!(cur_bitvector
& 0x1))
563 gcc_assert(h_ix
< GCOV_HISTOGRAM_SIZE
);
565 csum
->histogram
[h_ix
].num_counters
= gcov_read_unsigned ();
566 csum
->histogram
[h_ix
].min_value
= gcov_read_counter ();
567 csum
->histogram
[h_ix
].cum_value
= gcov_read_counter ();
568 /* Shift off the index we are done with and increment to the
569 corresponding next histogram entry. */
577 /* Reset to a known position. BASE should have been obtained from
578 gcov_position, LENGTH should be a record length. */
581 gcov_sync (gcov_position_t base
, gcov_unsigned_t length
)
583 gcc_assert (gcov_var
.mode
> 0);
585 if (base
- gcov_var
.start
<= gcov_var
.length
)
586 gcov_var
.offset
= base
- gcov_var
.start
;
589 gcov_var
.offset
= gcov_var
.length
= 0;
590 fseek (gcov_var
.file
, base
<< 2, SEEK_SET
);
591 gcov_var
.start
= ftell (gcov_var
.file
) >> 2;
597 /* Move to a given position in a gcov file. */
600 gcov_seek (gcov_position_t base
)
602 gcc_assert (gcov_var
.mode
< 0);
604 gcov_write_block (gcov_var
.offset
);
605 fseek (gcov_var
.file
, base
<< 2, SEEK_SET
);
606 gcov_var
.start
= ftell (gcov_var
.file
) >> 2;
611 /* Return the modification time of the current gcov file. */
618 if (fstat (fileno (gcov_var
.file
), &status
))
621 return status
.st_mtime
;
625 #if IN_LIBGCOV || !IN_GCOV
626 /* Determine the index into histogram for VALUE. */
629 gcov_histo_index(gcov_type value
)
631 gcov_type_unsigned v
= (gcov_type_unsigned
)value
;
633 unsigned prev2bits
= 0;
635 /* Find index into log2 scale histogram, where each of the log2
636 sized buckets is divided into 4 linear sub-buckets for better
637 focus in the higher buckets. */
639 /* Find the place of the most-significant bit set. */
641 r
= 63 - __builtin_clzll (v
);
643 /* If at most the 2 least significant bits are set (value is
644 0 - 3) then that value is our index into the lowest set of
647 return (unsigned)value
;
651 /* Find the two next most significant bits to determine which
652 of the four linear sub-buckets to select. */
653 prev2bits
= (v
>> (r
- 2)) & 0x3;
654 /* Finally, compose the final bucket index from the log2 index and
655 the next 2 bits. The minimum r value at this point is 2 since we
656 returned above if r was 2 or more, so the minimum bucket at this
658 return (r
- 1) * 4 + prev2bits
;
661 /* Merge SRC_HISTO into TGT_HISTO. The counters are assumed to be in
662 the same relative order in both histograms, and are matched up
663 and merged in reverse order. Each counter is assigned an equal portion of
664 its entry's original cumulative counter value when computing the
665 new merged cum_value. */
667 static void gcov_histogram_merge(gcov_bucket_type
*tgt_histo
,
668 gcov_bucket_type
*src_histo
)
670 int src_i
, tgt_i
, tmp_i
= 0;
671 unsigned src_num
, tgt_num
, merge_num
;
672 gcov_type src_cum
, tgt_cum
, merge_src_cum
, merge_tgt_cum
, merge_cum
;
674 gcov_bucket_type tmp_histo
[GCOV_HISTOGRAM_SIZE
];
677 memset(tmp_histo
, 0, sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
679 /* Assume that the counters are in the same relative order in both
680 histograms. Walk the histograms from largest to smallest entry,
681 matching up and combining counters in order. */
684 src_i
= GCOV_HISTOGRAM_SIZE
- 1;
685 for (tgt_i
= GCOV_HISTOGRAM_SIZE
- 1; tgt_i
>= 0 && !src_done
; tgt_i
--)
687 tgt_num
= tgt_histo
[tgt_i
].num_counters
;
688 tgt_cum
= tgt_histo
[tgt_i
].cum_value
;
689 /* Keep going until all of the target histogram's counters at this
690 position have been matched and merged with counters from the
692 while (tgt_num
> 0 && !src_done
)
694 /* If this is either the first time through this loop or we just
695 exhausted the previous non-zero source histogram entry, look
696 for the next non-zero source histogram entry. */
699 /* Locate the next non-zero entry. */
700 while (src_i
>= 0 && !src_histo
[src_i
].num_counters
)
702 /* If source histogram has fewer counters, then just copy over the
703 remaining target counters and quit. */
706 tmp_histo
[tgt_i
].num_counters
+= tgt_num
;
707 tmp_histo
[tgt_i
].cum_value
+= tgt_cum
;
708 if (!tmp_histo
[tgt_i
].min_value
||
709 tgt_histo
[tgt_i
].min_value
< tmp_histo
[tgt_i
].min_value
)
710 tmp_histo
[tgt_i
].min_value
= tgt_histo
[tgt_i
].min_value
;
713 tmp_histo
[tgt_i
].num_counters
714 += tgt_histo
[tgt_i
].num_counters
;
715 tmp_histo
[tgt_i
].cum_value
+= tgt_histo
[tgt_i
].cum_value
;
716 if (!tmp_histo
[tgt_i
].min_value
||
717 tgt_histo
[tgt_i
].min_value
718 < tmp_histo
[tgt_i
].min_value
)
719 tmp_histo
[tgt_i
].min_value
= tgt_histo
[tgt_i
].min_value
;
726 src_num
= src_histo
[src_i
].num_counters
;
727 src_cum
= src_histo
[src_i
].cum_value
;
730 /* The number of counters to merge on this pass is the minimum
731 of the remaining counters from the current target and source
732 histogram entries. */
734 if (src_num
< merge_num
)
737 /* The merged min_value is the sum of the min_values from target
739 merge_min
= tgt_histo
[tgt_i
].min_value
+ src_histo
[src_i
].min_value
;
741 /* Compute the portion of source and target entries' cum_value
742 that will be apportioned to the counters being merged.
743 The total remaining cum_value from each entry is divided
744 equally among the counters from that histogram entry if we
745 are not merging all of them. */
746 merge_src_cum
= src_cum
;
747 if (merge_num
< src_num
)
748 merge_src_cum
= merge_num
* src_cum
/ src_num
;
749 merge_tgt_cum
= tgt_cum
;
750 if (merge_num
< tgt_num
)
751 merge_tgt_cum
= merge_num
* tgt_cum
/ tgt_num
;
752 /* The merged cum_value is the sum of the source and target
754 merge_cum
= merge_src_cum
+ merge_tgt_cum
;
756 /* Update the remaining number of counters and cum_value left
757 to be merged from this source and target entry. */
758 src_cum
-= merge_src_cum
;
759 tgt_cum
-= merge_tgt_cum
;
760 src_num
-= merge_num
;
761 tgt_num
-= merge_num
;
763 /* The merged counters get placed in the new merged histogram
764 at the entry for the merged min_value. */
765 tmp_i
= gcov_histo_index(merge_min
);
766 gcc_assert (tmp_i
< GCOV_HISTOGRAM_SIZE
);
767 tmp_histo
[tmp_i
].num_counters
+= merge_num
;
768 tmp_histo
[tmp_i
].cum_value
+= merge_cum
;
769 if (!tmp_histo
[tmp_i
].min_value
||
770 merge_min
< tmp_histo
[tmp_i
].min_value
)
771 tmp_histo
[tmp_i
].min_value
= merge_min
;
773 /* Ensure the search for the next non-zero src_histo entry starts
774 at the next smallest histogram bucket. */
780 gcc_assert (tgt_i
< 0);
782 /* In the case where there were more counters in the source histogram,
783 accumulate the remaining unmerged cumulative counter values. Add
784 those to the smallest non-zero target histogram entry. Otherwise,
785 the total cumulative counter values in the histogram will be smaller
786 than the sum_all stored in the summary, which will complicate
787 computing the working set information from the histogram later on. */
792 src_cum
+= src_histo
[src_i
].cum_value
;
795 /* At this point, tmp_i should be the smallest non-zero entry in the
797 gcc_assert(tmp_i
>= 0 && tmp_i
< GCOV_HISTOGRAM_SIZE
798 && tmp_histo
[tmp_i
].num_counters
> 0);
799 tmp_histo
[tmp_i
].cum_value
+= src_cum
;
801 /* Finally, copy the merged histogram into tgt_histo. */
802 memcpy(tgt_histo
, tmp_histo
, sizeof (gcov_bucket_type
) * GCOV_HISTOGRAM_SIZE
);
804 #endif /* IN_LIBGCOV || !IN_GCOV */