1 /* File format for coverage information
2 Copyright (C) 1996, 1997, 1998, 2000, 2002 Free Software Foundation, Inc.
3 Contributed by Bob Manson <manson@cygnus.com>.
4 Completely remangled by Nathan Sidwell <nathan@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 /* Coverage information is held in two files. A basic block graph
24 file, which is generated by the compiler, and a counter file, which
25 is generated by the program under test. Both files use a similar
26 structure. We do not attempt to make these files backwards
27 compatible with previous versions, as you only need coverage
28 information when developing a program. We do hold version
29 information, so that mismatches can be detected, and we use a
30 format that allows tools to skip information they do not understand
31 or are not interested in.
33 Numbers are recorded in big endian unsigned binary form. Either in
34 32 or 64 bits. Strings are stored with a length count and NUL
35 terminator, and 0 to 3 bytes of zero padding up to the next 4 byte
36 boundary. Zero length and NULL strings are simply stored as a
37 length of zero (they have no trailing NUL or padding).
39 int32: byte3 byte2 byte1 byte0
40 int64: byte7 byte6 byte5 byte4 byte3 byte2 byte1 byte0
41 string: int32:0 | int32:length char* char:0 padding
42 padding: | char:0 | char:0 char:0 | char:0 char:0 char:0
43 item: int32 | int64 | string
45 The basic format of the files is
47 file : int32:magic int32:version record*
49 The magic ident is different for the bbg and the counter files.
50 The version is the same for both files and is derived from gcc's
51 version number. Although the ident and version are formally 32 bit
52 numbers, they are derived from 4 character ASCII strings. The
53 version number consists of the single character major version
54 number, a two character minor version number (leading zero for
55 versions less than 10), and a single character indicating the
56 status of the release. That will be 'e' experimental, 'p'
57 prerelease and 'r' for release. Because, by good fortune, these are
58 in alphabetical order, string collating can be used to compare
59 version strings, and because numbers are stored big endian, numeric
60 comparison can be used when it is read as a 32 bit value. Be aware
61 that the 'e' designation will (naturally) be unstable and might be
62 incompatible with itself. For gcc 3.4 experimental, it would be
63 '304e' (0x33303465). When the major version reaches 10, the letters
64 A-Z will be used. Assuming minor increments releases every 6
65 months, we have to make a major increment every 50 years. Assuming
66 major increments releases every 5 years, we're ok for the next 155
67 years -- good enough for me.
69 A record has a tag, length and variable amount of data.
72 header: int32:tag int32:length
75 Records are not nested, but there is a record hierarchy. Tag
76 numbers reflect this hierarchy. Tags are unique across bbg and da
77 files. Some record types have a varying amount of data. The LENGTH
78 is usually used to determine how much data. The tag value is split
79 into 4 8-bit fields, one for each of four possible levels. The
80 most significant is allocated first. Unused levels are zero.
81 Active levels are odd-valued, so that the LSB of the level is one.
82 A sub-level incorporates the values of its superlevels. This
83 formatting allows you to determine the tag heirarchy, without
84 understanding the tags themselves, and is similar to the standard
85 section numbering used in technical documents. Level values
86 [1..3f] are used for common tags, values [41..9f] for the graph
87 file and [a1..ff] for the counter file.
89 The basic block graph file contains the following records
91 function-graph: announce_function basic_blocks {arcs | lines}*
92 announce_function: header string:name int32:checksum
93 string:source int32:lineno
94 basic_block: header int32:flags*
95 arcs: header int32:block_no arc*
96 arc: int32:dest_block int32:flags
97 lines: header int32:block_no line*
99 line: int32:line_no | int32:0 string:filename
101 The BASIC_BLOCK record holds per-bb flags. The number of blocks
102 can be inferred from its data length. There is one ARCS record per
103 basic block. The number of arcs from a bb is implicit from the
104 data length. It enumerates the destination bb and per-arc flags.
105 There is one LINES record per basic block, it enumerates the source
106 lines which belong to that basic block. Source file names are
107 introduced by a line number of 0, following lines are from the new
108 source file. The initial source file for the function is NULL, but
109 the current source file should be remembered from one LINES record
110 to the next. The end of a block is indicated by an empty filename
111 - this does not reset the current source file. Note there is no
112 ordering of the ARCS and LINES records: they may be in any order,
113 interleaved in any manner. The current filename follows the order
114 the LINES records are stored in the file, *not* the ordering of the
117 The data file contains the following records.
118 da: {function-data* summary:object summary:program*}*
119 function-data: announce_function arc_counts
120 announce_function: header string:name int32:checksum
121 arc_counts: header int64:count*
122 summary: in32:checksum int32:runs int32:arcs int64:sum int64:max \
123 int64:max_sum int64:sum_max
125 The ANNOUNCE_FUNCTION record is the same as that in the BBG file,
126 but without the source location.
127 The ARC_COUNTS gives the counter values for those arcs that are
128 instrumented. The SUMMARY records give information about the whole
129 object file and about the whole program. The checksum is used for
130 whole program summaries, and disambiguates different programs which
131 include the same instrumented object file. There may be several
132 program summaries, each with a unique checksum. The object
133 summary's checkum is zero. Note that the da file might contain
134 information from several runs concatenated, or the data might be
137 This file is included by both the compiler, gcov tools and the
138 runtime support library libgcov. IN_LIBGCOV and IN_GCOV are used to
139 distinguish which case is which. If IN_LIBGCOV is non-zero,
140 libgcov is being built. If IN_GCOV is non-zero, the gcov tools are
141 being built. Otherwise the compiler is being built. IN_GCOV may be
142 positive or negative. If positive, we are compiling a tool that
143 requires additional functions (see the code for knowledge of what
144 those functions are). */
146 #ifndef GCC_GCOV_IO_H
147 #define GCC_GCOV_IO_H
150 #if LONG_TYPE_SIZE == GCOV_TYPE_SIZE
151 typedef long gcov_type;
153 typedef long long gcov_type;
155 #endif /* IN_LIBGCOV */
157 typedef HOST_WIDEST_INT gcov_type;
159 #include <sys/types.h>
164 #define GCOV_DATA_SUFFIX ".da"
165 #define GCOV_GRAPH_SUFFIX ".bbg"
168 #define GCOV_DATA_MAGIC 0x67636f76 /* "gcov" */
169 #define GCOV_GRAPH_MAGIC 0x67626267 /* "gbbg" */
171 /* gcov-iov.h is automatically generated by the makefile from
172 version.c, it looks like
173 #define GCOV_VERSION ((unsigned)0x89abcdef)
175 #include "gcov-iov.h"
177 /* The record tags. Values [1..3f] are for tags which may be in either
178 file. Values [41..9f] for those in the bbg file and [a1..ff] for
181 #define GCOV_TAG_FUNCTION ((unsigned)0x01000000)
182 #define GCOV_TAG_BLOCKS ((unsigned)0x01410000)
183 #define GCOV_TAG_ARCS ((unsigned)0x01430000)
184 #define GCOV_TAG_LINES ((unsigned)0x01450000)
185 #define GCOV_TAG_ARC_COUNTS ((unsigned)0x01a10000)
186 #define GCOV_TAG_OBJECT_SUMMARY ((unsigned)0xa1000000)
187 #define GCOV_TAG_PROGRAM_SUMMARY ((unsigned)0xa3000000)
188 #define GCOV_TAG_PLACEHOLDER_SUMMARY ((unsigned)0xa5000000)
189 #define GCOV_TAG_INCORRECT_SUMMARY ((unsigned)0xa7000000)
191 /* The tag level mask has 1's in the position of the inner levels, &
192 the lsb of the current level, and zero on the current and outer
194 #define GCOV_TAG_MASK(TAG) (((TAG) - 1) ^ (TAG))
196 /* Return nonzero if SUB is an immediate subtag of TAG. */
197 #define GCOV_TAG_IS_SUBTAG(TAG,SUB) \
198 (GCOV_TAG_MASK (TAG) >> 8 == GCOV_TAG_MASK (SUB) \
199 && !(((SUB) ^ (TAG)) & ~GCOV_TAG_MASK(TAG)))
201 /* Return nonzero if SUB is at a sublevel to TAG. */
202 #define GCOV_TAG_IS_SUBLEVEL(TAG,SUB) \
203 (GCOV_TAG_MASK (TAG) > GCOV_TAG_MASK (SUB))
205 /* Basic block flags. */
206 #define GCOV_BLOCK_UNEXPECTED (1 << 1)
209 #define GCOV_ARC_ON_TREE (1 << 0)
210 #define GCOV_ARC_FAKE (1 << 1)
211 #define GCOV_ARC_FALLTHROUGH (1 << 2)
213 /* Structured records. */
215 /* Object & program summary record. */
218 unsigned checksum; /* checksum of program */
219 unsigned runs; /* number of program runs */
220 unsigned arcs; /* number of instrumented arcs */
221 gcov_type arc_sum; /* sum of all arc counters */
222 gcov_type arc_max_one; /* max counter on any one run */
223 gcov_type arc_max_sum; /* maximum arc_sum */
224 gcov_type arc_sum_max; /* sum of max_one */
227 /* Structures embedded in coveraged program. The structures generated
228 by write_profile must match these. */
230 /* Information about section of counters for a function. */
231 struct counter_section
233 unsigned tag; /* Tag of the section. */
234 unsigned n_counters; /* Number of counters in the section. */
238 /* Information about section of counters for an object file. */
239 struct counter_section_data
241 unsigned tag; /* Tag of the section. */
242 unsigned n_counters; /* Number of counters in the section. */
243 gcov_type *counters; /* The data. */
246 /* Information about a single function. */
249 const char *name; /* (mangled) name of function */
250 unsigned checksum; /* function checksum */
251 unsigned n_counter_sections; /* Number of types of counters */
252 const struct counter_section *counter_sections;
253 /* The section descriptions */
256 /* Information about a single object file. */
259 unsigned long version; /* expected version number */
260 struct gcov_info *next; /* link to next, used by libgcc */
262 const char *filename; /* output file name */
263 long wkspc; /* libgcc workspace */
265 unsigned n_functions; /* number of functions */
266 const struct function_info *functions; /* table of functions */
268 unsigned n_counter_sections; /* Number of types of counters */
269 const struct counter_section_data *counter_sections;
270 /* The data to be put into the sections. */
273 /* Register a new object file module. */
274 extern void __gcov_init (struct gcov_info *);
276 /* Called before fork, to avoid double counting. */
277 extern void __gcov_flush (void);
279 /* Since this file is used in both host and target files, and we don't
280 include ansidecl.h in target files, provide some necessary macros. */
281 #ifndef ATTRIBUTE_UNUSED
282 # define ATTRIBUTE_UNUSED __attribute__ ((__unused__))
285 #endif /* IN_LIBGCOV */
287 /* Because small reads and writes, interspersed with seeks cause lots
288 of disk activity, we buffer the entire count files. */
290 static FILE *gcov_file;
291 static size_t gcov_position;
292 static size_t gcov_length;
293 static unsigned char *gcov_buffer;
294 static size_t gcov_alloc;
295 static int gcov_modified;
296 static int gcov_errored = 1;
298 /* Functions for reading and writing gcov files. */
299 static int gcov_open (const char */*name*/, int /*truncate*/);
300 static int gcov_close (void);
302 static unsigned char *gcov_write_bytes (unsigned);
303 static int gcov_write_unsigned (unsigned);
305 static int gcov_write_counter (gcov_type);
307 static int gcov_write_string (const char *);
308 static unsigned long gcov_reserve_length (void);
309 static int gcov_write_length (unsigned long /*position*/);
311 static int gcov_write_summary (unsigned, const struct gcov_summary *);
313 #endif /* !IN_GCOV */
314 static const unsigned char *gcov_read_bytes (unsigned);
315 static int gcov_read_unsigned (unsigned *);
316 static int gcov_read_counter (gcov_type *);
318 static int gcov_read_string (char **);
320 static int gcov_read_summary (struct gcov_summary *);
321 static unsigned long gcov_save_position (void);
322 static int gcov_resync (unsigned long /*base*/, unsigned /*length */);
323 static unsigned long gcov_seek_end (void);
324 static int gcov_skip (unsigned /*length*/);
325 static int gcov_skip_string (unsigned /*length*/);
326 static int gcov_ok (void);
327 static int gcov_error (void);
328 static int gcov_eof (void);
330 static time_t gcov_time (void);
333 /* Open a gcov file. NAME is the name of the file to open and MODE
334 indicates whether a new file should be created, or an existing file
335 opened for modification. If MODE is >= 0 an existing file will be
336 opened, if possible, and if MODE is <= 0, a new file will be
337 created. Use MODE=0 to attempt to reopen an existing file and then
338 fall back on creating a new one. Return zero on failure, >0 on
339 opening an existing file and <0 on creating a new one. */
342 gcov_open (const char *name, int mode)
346 #if defined (TARGET_HAS_F_SETLKW) && IN_LIBGCOV
347 struct flock s_flock;
349 s_flock.l_type = F_WRLCK;
350 s_flock.l_whence = SEEK_SET;
352 s_flock.l_len = 0; /* Until EOF. */
353 s_flock.l_pid = getpid ();
358 gcov_position = gcov_length = 0;
359 gcov_errored = gcov_modified = 0;
361 gcov_file = fopen (name, "r+b");
362 if (!gcov_file && mode <= 0)
365 gcov_file = fopen (name, "w+b");
370 #if defined (TARGET_HAS_F_SETLKW) && IN_LIBGCOV
371 while (fcntl (fileno (gcov_file), F_SETLKW, &s_flock)
378 if (fseek (gcov_file, 0, SEEK_END))
384 gcov_length = ftell (gcov_file);
385 fseek (gcov_file, 0, SEEK_SET);
386 alloc += gcov_length;
388 if (alloc > gcov_alloc)
394 gcov_buffer = malloc (gcov_alloc);
404 gcov_buffer = xmalloc (gcov_alloc);
407 if (result >= 0 && fread (gcov_buffer, gcov_length, 1, gcov_file) != 1)
417 /* Close the current gcov file. Flushes data to disk. Returns nonzero
418 on failure or error flag set. */
428 && (fseek (gcov_file, 0, SEEK_SET)
429 || fwrite (gcov_buffer, gcov_length, 1, gcov_file) != 1))
435 return result || gcov_errored;
439 /* Allocate space to write BYTES bytes to the gcov file. Return a
440 pointer to those bytes, or NULL on failure. */
442 static unsigned char *
443 gcov_write_bytes (unsigned bytes)
445 char unsigned *result;
447 if (gcov_position + bytes > gcov_alloc)
449 size_t new_size = (gcov_alloc + bytes) * 3 / 2;
454 result = realloc (gcov_buffer, new_size);
460 gcov_position = gcov_length = 0;
464 result = xrealloc (gcov_buffer, new_size);
466 gcov_alloc = new_size;
467 gcov_buffer = result;
470 result = &gcov_buffer[gcov_position];
471 gcov_position += bytes;
473 if (gcov_position > gcov_length)
474 gcov_length = gcov_position;
478 /* Write VALUE to coverage file. Return nonzero if failed due to
479 file i/o error, or value error. */
482 gcov_write_unsigned (unsigned value)
484 unsigned char *buffer = gcov_write_bytes (4);
495 return sizeof (value) > 4 && value;
498 /* Write VALUE to coverage file. Return nonzero if failed due to
499 file i/o error, or value error. Negative values are not checked
500 here -- they are checked in gcov_read_counter. */
504 gcov_write_counter (gcov_type value)
506 unsigned char *buffer = gcov_write_bytes (8);
517 return sizeof (value) > 8 && value;
519 #endif /* IN_LIBGCOV */
521 /* Write VALUE to coverage file. Return nonzero if failed due to
522 file i/o error, or value error. */
525 gcov_write_string (const char *string)
529 unsigned length = strlen (string);
531 unsigned rem = 4 - (length & 3);
532 unsigned char *buffer;
534 if (gcov_write_unsigned (length))
536 buffer = gcov_write_bytes (length + rem);
539 memcpy (buffer, string, length);
540 memcpy (buffer + length, &pad, rem);
544 return gcov_write_unsigned (0);
547 /* Allocate space to write a record tag length. Return a value to be
548 used for gcov_write_length. */
551 gcov_reserve_length (void)
553 unsigned long result = gcov_position;
554 unsigned char *buffer = gcov_write_bytes (4);
558 memset (buffer, 0, 4);
562 /* Write a record length at PLACE. The current file position is the
563 end of the record, and is restored before returning. Returns
564 nonzero on failure. */
567 gcov_write_length (unsigned long position)
569 unsigned length = gcov_position - position - 4;
570 unsigned char *buffer = &gcov_buffer[position];
584 /* Write a summary structure to the gcov file. */
587 gcov_write_summary (unsigned tag, const struct gcov_summary *summary)
589 volatile unsigned long base; /* volatile is necessary to work around
592 if (gcov_write_unsigned (tag))
594 base = gcov_reserve_length ();
595 if (gcov_write_unsigned (summary->checksum))
597 if (gcov_write_unsigned (summary->runs)
598 || gcov_write_unsigned (summary->arcs))
600 if (gcov_write_counter (summary->arc_sum)
601 || gcov_write_counter (summary->arc_max_one)
602 || gcov_write_counter (summary->arc_max_sum)
603 || gcov_write_counter (summary->arc_sum_max))
605 if (gcov_write_length (base))
609 #endif /* IN_LIBGCOV */
613 /* Return a pointer to read BYTES bytes from the gcov file. Returns
614 NULL on failure (read past EOF). */
616 static const unsigned char *
617 gcov_read_bytes (unsigned bytes)
619 const unsigned char *result;
621 if (gcov_position + bytes > gcov_length)
623 result = &gcov_buffer[gcov_position];
624 gcov_position += bytes;
628 /* Read *VALUE_P from coverage file. Return nonzero if failed
629 due to file i/o error, or range error. */
632 gcov_read_unsigned (unsigned *value_p)
636 const unsigned char *buffer = gcov_read_bytes (4);
640 for (ix = sizeof (value); ix < 4; ix++)
643 for (ix = 0; ix != 4; ix++)
652 /* Read *VALUE_P from coverage file. Return nonzero if failed
653 due to file i/o error, or range error. */
656 gcov_read_counter (gcov_type *value_p)
660 const unsigned char *buffer = gcov_read_bytes (8);
664 for (ix = sizeof (value); ix < 8; ix++)
667 for (ix = 0; ix != 8; ix++)
679 /* Read string from coverage file. A buffer is allocated and returned
680 in *STRING_P. Return nonzero if failed due to file i/o error, or
681 range error. Uses xmalloc to allocate the string buffer. */
684 gcov_read_string (char **string_p)
687 const unsigned char *buffer;
689 if (gcov_read_unsigned (&length))
697 length += 4 - (length & 3);
698 buffer = gcov_read_bytes (length);
702 *string_p = xmalloc (length);
706 memcpy (*string_p, buffer, length);
710 #endif /* !IN_LIBGCOV */
712 #define GCOV_SUMMARY_LENGTH 44
714 gcov_read_summary (struct gcov_summary *summary)
716 return (gcov_read_unsigned (&summary->checksum)
717 || gcov_read_unsigned (&summary->runs)
718 || gcov_read_unsigned (&summary->arcs)
719 || gcov_read_counter (&summary->arc_sum)
720 || gcov_read_counter (&summary->arc_max_one)
721 || gcov_read_counter (&summary->arc_max_sum)
722 || gcov_read_counter (&summary->arc_sum_max));
725 /* Save the current position in the gcov file. */
727 static inline unsigned long
728 gcov_save_position (void)
730 return gcov_position;
733 /* Reset to a known position. BASE should have been obtained from
734 gcov_save_position, LENGTH should be a record length, or zero. */
737 gcov_resync (unsigned long base, unsigned length)
740 gcov_position = base + length;
744 /* Move to the end of the gcov file. */
746 static inline unsigned long
749 gcov_position = gcov_length;
750 return gcov_position;
753 /* Skip LENGTH bytes in the file. */
756 gcov_skip (unsigned length)
758 if (gcov_length < gcov_position + length)
760 gcov_position += length;
764 /* Skip a string of LENGTH bytes. */
767 gcov_skip_string (unsigned length)
769 return gcov_skip (length + 4 - (length & 3));
772 /* Tests whether we have reached end of .da file. */
777 return gcov_position == gcov_length;
780 /* Return non-zero if the error flag is set. */
785 return gcov_file != 0 && !gcov_errored;
788 /* Set the error flag. */
792 int error = gcov_errored;
799 /* Return the modification time of the current gcov file. */
806 if (fstat (fileno (gcov_file), &status))
809 return status.st_mtime;
812 #endif /* GCC_GCOV_IO_H */