1 /* File format for coverage information
2 Copyright (C) 1996, 1997, 1998, 2000, 2002,
3 2003 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 2, 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 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 /* Routines declared in gcov-io.h. This file should be #included by
25 another source file, after having #included gcov-io.h. */
28 static void gcov_write_block (unsigned);
29 static gcov_unsigned_t
*gcov_write_words (unsigned);
31 static const gcov_unsigned_t
*gcov_read_words (unsigned);
33 static void gcov_allocate (unsigned);
36 static inline gcov_unsigned_t
from_file (gcov_unsigned_t value
)
41 value
= (value
>> 16) | (value
<< 16);
42 value
= ((value
& 0xff00ff) << 8) | ((value
>> 8) & 0xff00ff);
48 /* Open a gcov file. NAME is the name of the file to open and MODE
49 indicates whether a new file should be created, or an existing file
50 opened for modification. If MODE is >= 0 an existing file will be
51 opened, if possible, and if MODE is <= 0, a new file will be
52 created. Use MODE=0 to attempt to reopen an existing file and then
53 fall back on creating a new one. Return zero on failure, >0 on
54 opening an existing file and <0 on creating a new one. */
58 gcov_open (const char *name
)
60 gcov_open (const char *name
, int mode
)
69 s_flock
.l_type
= F_WRLCK
;
70 s_flock
.l_whence
= SEEK_SET
;
72 s_flock
.l_len
= 0; /* Until EOF. */
73 s_flock
.l_pid
= getpid ();
79 gcov_var
.offset
= gcov_var
.length
= 0;
80 gcov_var
.overread
= -1u;
86 gcov_var
.file
= fopen (name
, "r+b");
91 gcov_var
.file
= fopen (name
, "w+b");
93 gcov_var
.mode
= mode
* 2 + 1;
98 setbuf (gcov_var
.file
, (char *)0);
101 while (fcntl (fileno (gcov_var
.file
), F_SETLKW
, &s_flock
)
109 /* Close the current gcov file. Flushes data to disk. Returns nonzero
110 on failure or error flag set. */
118 if (gcov_var
.offset
&& gcov_var
.mode
< 0)
119 gcov_write_block (gcov_var
.offset
);
121 fclose (gcov_var
.file
);
126 free (gcov_var
.buffer
);
131 return gcov_var
.error
;
135 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
136 file. Returns +1 for same endian, -1 for other endian and zero for
140 gcov_magic (gcov_unsigned_t magic
, gcov_unsigned_t expected
)
142 if (magic
== expected
)
144 magic
= (magic
>> 16) | (magic
<< 16);
145 magic
= ((magic
& 0xff00ff) << 8) | ((magic
>> 8) & 0xff00ff);
146 if (magic
== expected
)
157 gcov_allocate (unsigned length
)
159 size_t new_size
= gcov_var
.alloc
;
162 new_size
= GCOV_BLOCK_SIZE
;
166 gcov_var
.alloc
= new_size
;
167 gcov_var
.buffer
= xrealloc (gcov_var
.buffer
, new_size
<< 2);
172 /* Write out the current block, if needs be. */
175 gcov_write_block (unsigned size
)
177 if (fwrite (gcov_var
.buffer
, size
<< 2, 1, gcov_var
.file
) != 1)
179 gcov_var
.start
+= size
;
180 gcov_var
.offset
-= size
;
183 /* Allocate space to write BYTES bytes to the gcov file. Return a
184 pointer to those bytes, or NULL on failure. */
186 static gcov_unsigned_t
*
187 gcov_write_words (unsigned words
)
189 gcov_unsigned_t
*result
;
191 GCOV_CHECK_WRITING ();
193 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
195 gcov_write_block (GCOV_BLOCK_SIZE
);
198 GCOV_CHECK (gcov_var
.offset
== 1);
199 memcpy (gcov_var
.buffer
, gcov_var
.buffer
+ GCOV_BLOCK_SIZE
, 4);
203 if (gcov_var
.offset
+ words
> gcov_var
.alloc
)
204 gcov_allocate (gcov_var
.offset
+ words
);
206 result
= &gcov_var
.buffer
[gcov_var
.offset
];
207 gcov_var
.offset
+= words
;
212 /* Write unsigned VALUE to coverage file. Sets error flag
216 gcov_write_unsigned (gcov_unsigned_t value
)
218 gcov_unsigned_t
*buffer
= gcov_write_words (1);
223 /* Write counter VALUE to coverage file. Sets error flag
228 gcov_write_counter (gcov_type value
)
230 gcov_unsigned_t
*buffer
= gcov_write_words (2);
232 buffer
[0] = (gcov_unsigned_t
) value
;
233 if (sizeof (value
) > sizeof (gcov_unsigned_t
))
234 buffer
[1] = (gcov_unsigned_t
) (value
>> 32);
241 #endif /* IN_LIBGCOV */
244 /* Write STRING to coverage file. Sets error flag on file
245 error, overflow flag on overflow */
248 gcov_write_string (const char *string
)
252 gcov_unsigned_t
*buffer
;
256 length
= strlen (string
);
257 alloc
= (length
+ 4) >> 2;
260 buffer
= gcov_write_words (1 + alloc
);
264 memcpy (&buffer
[1], string
, length
);
269 /* Write a tag TAG and reserve space for the record length. Return a
270 value to be used for gcov_write_length. */
272 GCOV_LINKAGE gcov_position_t
273 gcov_write_tag (gcov_unsigned_t tag
)
275 gcov_position_t result
= gcov_var
.start
+ gcov_var
.offset
;
276 gcov_unsigned_t
*buffer
= gcov_write_words (2);
284 /* Write a record length using POSITION, which was returned by
285 gcov_write_tag. The current file position is the end of the
286 record, and is restored before returning. Returns nonzero on
290 gcov_write_length (gcov_position_t position
)
293 gcov_unsigned_t length
;
294 gcov_unsigned_t
*buffer
;
296 GCOV_CHECK_WRITING ();
297 GCOV_CHECK (position
+ 2 <= gcov_var
.start
+ gcov_var
.offset
);
298 GCOV_CHECK (position
>= gcov_var
.start
);
299 offset
= position
- gcov_var
.start
;
300 length
= gcov_var
.offset
- offset
- 2;
301 buffer
= (gcov_unsigned_t
*) &gcov_var
.buffer
[offset
];
303 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
304 gcov_write_block (gcov_var
.offset
);
307 #else /* IN_LIBGCOV */
309 /* Write a tag TAG and length LENGTH. */
312 gcov_write_tag_length (gcov_unsigned_t tag
, gcov_unsigned_t length
)
314 gcov_unsigned_t
*buffer
= gcov_write_words (2);
320 /* Write a summary structure to the gcov file. Return nonzero on
324 gcov_write_summary (gcov_unsigned_t tag
, const struct gcov_summary
*summary
)
327 const struct gcov_ctr_summary
*csum
;
329 gcov_write_tag_length (tag
, GCOV_TAG_SUMMARY_LENGTH
);
330 gcov_write_unsigned (summary
->checksum
);
331 for (csum
= summary
->ctrs
, ix
= GCOV_COUNTERS_SUMMABLE
; ix
--; csum
++)
333 gcov_write_unsigned (csum
->num
);
334 gcov_write_unsigned (csum
->runs
);
335 gcov_write_counter (csum
->sum_all
);
336 gcov_write_counter (csum
->run_max
);
337 gcov_write_counter (csum
->sum_max
);
340 #endif /* IN_LIBGCOV */
344 /* Return a pointer to read BYTES bytes from the gcov file. Returns
345 NULL on failure (read past EOF). */
347 static const gcov_unsigned_t
*
348 gcov_read_words (unsigned words
)
350 const gcov_unsigned_t
*result
;
351 unsigned excess
= gcov_var
.length
- gcov_var
.offset
;
353 GCOV_CHECK_READING ();
356 gcov_var
.start
+= gcov_var
.offset
;
360 GCOV_CHECK (excess
== 1);
361 memcpy (gcov_var
.buffer
, gcov_var
.buffer
+ gcov_var
.offset
, 4);
364 memmove (gcov_var
.buffer
, gcov_var
.buffer
+ gcov_var
.offset
, excess
);
367 gcov_var
.length
= excess
;
369 GCOV_CHECK (!gcov_var
.length
|| gcov_var
.length
== 1);
370 excess
= GCOV_BLOCK_SIZE
;
372 if (gcov_var
.length
+ words
> gcov_var
.alloc
)
373 gcov_allocate (gcov_var
.length
+ words
);
374 excess
= gcov_var
.alloc
- gcov_var
.length
;
376 excess
= fread (gcov_var
.buffer
+ gcov_var
.length
,
377 1, excess
<< 2, gcov_var
.file
) >> 2;
378 gcov_var
.length
+= excess
;
379 if (gcov_var
.length
< words
)
381 gcov_var
.overread
+= words
- gcov_var
.length
;
386 result
= &gcov_var
.buffer
[gcov_var
.offset
];
387 gcov_var
.offset
+= words
;
391 /* Read unsigned value from a coverage file. Sets error flag on file
392 error, overflow flag on overflow */
394 GCOV_LINKAGE gcov_unsigned_t
395 gcov_read_unsigned (void)
397 gcov_unsigned_t value
;
398 const gcov_unsigned_t
*buffer
= gcov_read_words (1);
402 value
= from_file (buffer
[0]);
406 /* Read counter value from a coverage file. Sets error flag on file
407 error, overflow flag on overflow */
409 GCOV_LINKAGE gcov_type
410 gcov_read_counter (void)
413 const gcov_unsigned_t
*buffer
= gcov_read_words (2);
417 value
= from_file (buffer
[0]);
418 if (sizeof (value
) > sizeof (gcov_unsigned_t
))
419 value
|= ((gcov_type
) from_file (buffer
[1])) << 32;
428 /* Read string from coverage file. Returns a pointer to a static
429 buffer, or NULL on empty string. You must copy the string before
430 calling another gcov function. */
433 GCOV_LINKAGE
const char *
434 gcov_read_string (void)
436 unsigned length
= gcov_read_unsigned ();
441 return (const char *) gcov_read_words (length
);
446 gcov_read_summary (struct gcov_summary
*summary
)
449 struct gcov_ctr_summary
*csum
;
451 summary
->checksum
= gcov_read_unsigned ();
452 for (csum
= summary
->ctrs
, ix
= GCOV_COUNTERS_SUMMABLE
; ix
--; csum
++)
454 csum
->num
= gcov_read_unsigned ();
455 csum
->runs
= gcov_read_unsigned ();
456 csum
->sum_all
= gcov_read_counter ();
457 csum
->run_max
= gcov_read_counter ();
458 csum
->sum_max
= gcov_read_counter ();
463 /* Reset to a known position. BASE should have been obtained from
464 gcov_position, LENGTH should be a record length. */
467 gcov_sync (gcov_position_t base
, gcov_unsigned_t length
)
469 GCOV_CHECK_READING ();
471 if (base
- gcov_var
.start
<= gcov_var
.length
)
472 gcov_var
.offset
= base
- gcov_var
.start
;
475 gcov_var
.offset
= gcov_var
.length
= 0;
476 fseek (gcov_var
.file
, base
<< 2, SEEK_SET
);
477 gcov_var
.start
= ftell (gcov_var
.file
) >> 2;
483 /* Move to the a set position in a gcov file. BASE is zero to move to
484 the end, and nonzero to move to that position. */
487 gcov_seek (gcov_position_t base
)
489 GCOV_CHECK_WRITING ();
491 gcov_write_block (gcov_var
.offset
);
492 fseek (gcov_var
.file
, base
<< 2, base
? SEEK_SET
: SEEK_END
);
493 gcov_var
.start
= ftell (gcov_var
.file
) >> 2;
498 /* Return the modification time of the current gcov file. */
505 if (fstat (fileno (gcov_var
.file
), &status
))
508 return status
.st_mtime
;