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.
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. If MODE is >= 0 an existing file will be opened, if
51 possible, and if MODE is <= 0, a new file will be created. Use
52 MODE=0 to attempt to reopen an existing file and then fall back on
53 creating a new one. If MODE < 0, the file will be opened in
54 read-only mode. Otherwise it will be opened for modification.
55 Return zero on failure, >0 on opening an existing file and <0 on
56 creating a new one. */
60 gcov_open (const char *name
)
62 gcov_open (const char *name
, int mode
)
72 s_flock
.l_whence
= SEEK_SET
;
74 s_flock
.l_len
= 0; /* Until EOF. */
75 s_flock
.l_pid
= getpid ();
78 gcc_assert (!gcov_var
.file
);
80 gcov_var
.offset
= gcov_var
.length
= 0;
81 gcov_var
.overread
= -1u;
89 /* Read-only mode - acquire a read-lock. */
90 s_flock
.l_type
= F_RDLCK
;
91 fd
= open (name
, O_RDONLY
);
95 /* Write mode - acquire a write-lock. */
96 s_flock
.l_type
= F_WRLCK
;
97 fd
= open (name
, O_RDWR
| O_CREAT
, 0666);
102 while (fcntl (fd
, F_SETLKW
, &s_flock
) && errno
== EINTR
)
105 gcov_var
.file
= fdopen (fd
, (mode
> 0) ? "rb" : "r+b");
119 if (fstat (fd
, &st
) < 0)
121 fclose (gcov_var
.file
);
128 gcov_var
.mode
= mode
* 2 + 1;
131 gcov_var
.mode
= mode
* 2 + 1;
134 gcov_var
.file
= fopen (name
, (mode
> 0) ? "rb" : "r+b");
140 gcov_var
.file
= fopen (name
, "w+b");
142 gcov_var
.mode
= mode
* 2 + 1;
148 setbuf (gcov_var
.file
, (char *)0);
153 /* Close the current gcov file. Flushes data to disk. Returns nonzero
154 on failure or error flag set. */
162 if (gcov_var
.offset
&& gcov_var
.mode
< 0)
163 gcov_write_block (gcov_var
.offset
);
165 fclose (gcov_var
.file
);
170 free (gcov_var
.buffer
);
175 return gcov_var
.error
;
179 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
180 file. Returns +1 for same endian, -1 for other endian and zero for
184 gcov_magic (gcov_unsigned_t magic
, gcov_unsigned_t expected
)
186 if (magic
== expected
)
188 magic
= (magic
>> 16) | (magic
<< 16);
189 magic
= ((magic
& 0xff00ff) << 8) | ((magic
>> 8) & 0xff00ff);
190 if (magic
== expected
)
201 gcov_allocate (unsigned length
)
203 size_t new_size
= gcov_var
.alloc
;
206 new_size
= GCOV_BLOCK_SIZE
;
210 gcov_var
.alloc
= new_size
;
211 gcov_var
.buffer
= XRESIZEVAR (gcov_unsigned_t
, gcov_var
.buffer
, new_size
<< 2);
216 /* Write out the current block, if needs be. */
219 gcov_write_block (unsigned size
)
221 if (fwrite (gcov_var
.buffer
, size
<< 2, 1, gcov_var
.file
) != 1)
223 gcov_var
.start
+= size
;
224 gcov_var
.offset
-= size
;
227 /* Allocate space to write BYTES bytes to the gcov file. Return a
228 pointer to those bytes, or NULL on failure. */
230 static gcov_unsigned_t
*
231 gcov_write_words (unsigned words
)
233 gcov_unsigned_t
*result
;
235 gcc_assert (gcov_var
.mode
< 0);
237 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
239 gcov_write_block (GCOV_BLOCK_SIZE
);
242 gcc_assert (gcov_var
.offset
== 1);
243 memcpy (gcov_var
.buffer
, gcov_var
.buffer
+ GCOV_BLOCK_SIZE
, 4);
247 if (gcov_var
.offset
+ words
> gcov_var
.alloc
)
248 gcov_allocate (gcov_var
.offset
+ words
);
250 result
= &gcov_var
.buffer
[gcov_var
.offset
];
251 gcov_var
.offset
+= words
;
256 /* Write unsigned VALUE to coverage file. Sets error flag
260 gcov_write_unsigned (gcov_unsigned_t value
)
262 gcov_unsigned_t
*buffer
= gcov_write_words (1);
267 /* Write counter VALUE to coverage file. Sets error flag
272 gcov_write_counter (gcov_type value
)
274 gcov_unsigned_t
*buffer
= gcov_write_words (2);
276 buffer
[0] = (gcov_unsigned_t
) value
;
277 if (sizeof (value
) > sizeof (gcov_unsigned_t
))
278 buffer
[1] = (gcov_unsigned_t
) (value
>> 32);
282 #endif /* IN_LIBGCOV */
285 /* Write STRING to coverage file. Sets error flag on file
286 error, overflow flag on overflow */
289 gcov_write_string (const char *string
)
293 gcov_unsigned_t
*buffer
;
297 length
= strlen (string
);
298 alloc
= (length
+ 4) >> 2;
301 buffer
= gcov_write_words (1 + alloc
);
305 memcpy (&buffer
[1], string
, length
);
310 /* Write a tag TAG and reserve space for the record length. Return a
311 value to be used for gcov_write_length. */
313 GCOV_LINKAGE gcov_position_t
314 gcov_write_tag (gcov_unsigned_t tag
)
316 gcov_position_t result
= gcov_var
.start
+ gcov_var
.offset
;
317 gcov_unsigned_t
*buffer
= gcov_write_words (2);
325 /* Write a record length using POSITION, which was returned by
326 gcov_write_tag. The current file position is the end of the
327 record, and is restored before returning. Returns nonzero on
331 gcov_write_length (gcov_position_t position
)
334 gcov_unsigned_t length
;
335 gcov_unsigned_t
*buffer
;
337 gcc_assert (gcov_var
.mode
< 0);
338 gcc_assert (position
+ 2 <= gcov_var
.start
+ gcov_var
.offset
);
339 gcc_assert (position
>= gcov_var
.start
);
340 offset
= position
- gcov_var
.start
;
341 length
= gcov_var
.offset
- offset
- 2;
342 buffer
= (gcov_unsigned_t
*) &gcov_var
.buffer
[offset
];
344 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
345 gcov_write_block (gcov_var
.offset
);
348 #else /* IN_LIBGCOV */
350 /* Write a tag TAG and length LENGTH. */
353 gcov_write_tag_length (gcov_unsigned_t tag
, gcov_unsigned_t length
)
355 gcov_unsigned_t
*buffer
= gcov_write_words (2);
361 /* Write a summary structure to the gcov file. Return nonzero on
365 gcov_write_summary (gcov_unsigned_t tag
, const struct gcov_summary
*summary
)
368 const struct gcov_ctr_summary
*csum
;
370 gcov_write_tag_length (tag
, GCOV_TAG_SUMMARY_LENGTH
);
371 gcov_write_unsigned (summary
->checksum
);
372 for (csum
= summary
->ctrs
, ix
= GCOV_COUNTERS_SUMMABLE
; ix
--; csum
++)
374 gcov_write_unsigned (csum
->num
);
375 gcov_write_unsigned (csum
->runs
);
376 gcov_write_counter (csum
->sum_all
);
377 gcov_write_counter (csum
->run_max
);
378 gcov_write_counter (csum
->sum_max
);
381 #endif /* IN_LIBGCOV */
385 /* Return a pointer to read BYTES bytes from the gcov file. Returns
386 NULL on failure (read past EOF). */
388 static const gcov_unsigned_t
*
389 gcov_read_words (unsigned words
)
391 const gcov_unsigned_t
*result
;
392 unsigned excess
= gcov_var
.length
- gcov_var
.offset
;
394 gcc_assert (gcov_var
.mode
> 0);
397 gcov_var
.start
+= gcov_var
.offset
;
401 gcc_assert (excess
== 1);
402 memcpy (gcov_var
.buffer
, gcov_var
.buffer
+ gcov_var
.offset
, 4);
405 memmove (gcov_var
.buffer
, gcov_var
.buffer
+ gcov_var
.offset
, excess
* 4);
408 gcov_var
.length
= excess
;
410 gcc_assert (!gcov_var
.length
|| gcov_var
.length
== 1);
411 excess
= GCOV_BLOCK_SIZE
;
413 if (gcov_var
.length
+ words
> gcov_var
.alloc
)
414 gcov_allocate (gcov_var
.length
+ words
);
415 excess
= gcov_var
.alloc
- gcov_var
.length
;
417 excess
= fread (gcov_var
.buffer
+ gcov_var
.length
,
418 1, excess
<< 2, gcov_var
.file
) >> 2;
419 gcov_var
.length
+= excess
;
420 if (gcov_var
.length
< words
)
422 gcov_var
.overread
+= words
- gcov_var
.length
;
427 result
= &gcov_var
.buffer
[gcov_var
.offset
];
428 gcov_var
.offset
+= words
;
432 /* Read unsigned value from a coverage file. Sets error flag on file
433 error, overflow flag on overflow */
435 GCOV_LINKAGE gcov_unsigned_t
436 gcov_read_unsigned (void)
438 gcov_unsigned_t value
;
439 const gcov_unsigned_t
*buffer
= gcov_read_words (1);
443 value
= from_file (buffer
[0]);
447 /* Read counter value from a coverage file. Sets error flag on file
448 error, overflow flag on overflow */
450 GCOV_LINKAGE gcov_type
451 gcov_read_counter (void)
454 const gcov_unsigned_t
*buffer
= gcov_read_words (2);
458 value
= from_file (buffer
[0]);
459 if (sizeof (value
) > sizeof (gcov_unsigned_t
))
460 value
|= ((gcov_type
) from_file (buffer
[1])) << 32;
467 /* Read string from coverage file. Returns a pointer to a static
468 buffer, or NULL on empty string. You must copy the string before
469 calling another gcov function. */
472 GCOV_LINKAGE
const char *
473 gcov_read_string (void)
475 unsigned length
= gcov_read_unsigned ();
480 return (const char *) gcov_read_words (length
);
485 gcov_read_summary (struct gcov_summary
*summary
)
488 struct gcov_ctr_summary
*csum
;
490 summary
->checksum
= gcov_read_unsigned ();
491 for (csum
= summary
->ctrs
, ix
= GCOV_COUNTERS_SUMMABLE
; ix
--; csum
++)
493 csum
->num
= gcov_read_unsigned ();
494 csum
->runs
= gcov_read_unsigned ();
495 csum
->sum_all
= gcov_read_counter ();
496 csum
->run_max
= gcov_read_counter ();
497 csum
->sum_max
= gcov_read_counter ();
502 /* Reset to a known position. BASE should have been obtained from
503 gcov_position, LENGTH should be a record length. */
506 gcov_sync (gcov_position_t base
, gcov_unsigned_t length
)
508 gcc_assert (gcov_var
.mode
> 0);
510 if (base
- gcov_var
.start
<= gcov_var
.length
)
511 gcov_var
.offset
= base
- gcov_var
.start
;
514 gcov_var
.offset
= gcov_var
.length
= 0;
515 fseek (gcov_var
.file
, base
<< 2, SEEK_SET
);
516 gcov_var
.start
= ftell (gcov_var
.file
) >> 2;
522 /* Move to a given position in a gcov file. */
525 gcov_seek (gcov_position_t base
)
527 gcc_assert (gcov_var
.mode
< 0);
529 gcov_write_block (gcov_var
.offset
);
530 fseek (gcov_var
.file
, base
<< 2, SEEK_SET
);
531 gcov_var
.start
= ftell (gcov_var
.file
) >> 2;
536 /* Return the modification time of the current gcov file. */
543 if (fstat (fileno (gcov_var
.file
), &status
))
546 return status
.st_mtime
;