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 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Routines declared in gcov-io.h. This file should be #included by
24 another source file, after having #included gcov-io.h. */
27 static void gcov_write_block (unsigned);
28 static gcov_unsigned_t
*gcov_write_words (unsigned);
30 static const gcov_unsigned_t
*gcov_read_words (unsigned);
32 static void gcov_allocate (unsigned);
35 static inline gcov_unsigned_t
from_file (gcov_unsigned_t value
)
40 value
= (value
>> 16) | (value
<< 16);
41 value
= ((value
& 0xff00ff) << 8) | ((value
>> 8) & 0xff00ff);
47 /* Open a gcov file. NAME is the name of the file to open and MODE
48 indicates whether a new file should be created, or an existing file
49 opened. If MODE is >= 0 an existing file will be opened, if
50 possible, and if MODE is <= 0, a new file will be created. Use
51 MODE=0 to attempt to reopen an existing file and then fall back on
52 creating a new one. If MODE < 0, the file will be opened in
53 read-only mode. Otherwise it will be opened for modification.
54 Return zero on failure, >0 on opening an existing file and <0 on
55 creating a new one. */
59 gcov_open (const char *name
)
61 gcov_open (const char *name
, int mode
)
71 s_flock
.l_whence
= SEEK_SET
;
73 s_flock
.l_len
= 0; /* Until EOF. */
74 s_flock
.l_pid
= getpid ();
77 gcc_assert (!gcov_var
.file
);
79 gcov_var
.offset
= gcov_var
.length
= 0;
80 gcov_var
.overread
= -1u;
88 /* Read-only mode - acquire a read-lock. */
89 s_flock
.l_type
= F_RDLCK
;
90 fd
= open (name
, O_RDONLY
);
94 /* Write mode - acquire a write-lock. */
95 s_flock
.l_type
= F_WRLCK
;
96 fd
= open (name
, O_RDWR
| O_CREAT
, 0666);
101 while (fcntl (fd
, F_SETLKW
, &s_flock
) && errno
== EINTR
)
104 gcov_var
.file
= fdopen (fd
, (mode
> 0) ? "rb" : "r+b");
118 if (fstat (fd
, &st
) < 0)
120 fclose (gcov_var
.file
);
127 gcov_var
.mode
= mode
* 2 + 1;
130 gcov_var
.mode
= mode
* 2 + 1;
133 gcov_var
.file
= fopen (name
, (mode
> 0) ? "rb" : "r+b");
139 gcov_var
.file
= fopen (name
, "w+b");
141 gcov_var
.mode
= mode
* 2 + 1;
147 setbuf (gcov_var
.file
, (char *)0);
152 /* Close the current gcov file. Flushes data to disk. Returns nonzero
153 on failure or error flag set. */
161 if (gcov_var
.offset
&& gcov_var
.mode
< 0)
162 gcov_write_block (gcov_var
.offset
);
164 fclose (gcov_var
.file
);
169 free (gcov_var
.buffer
);
174 return gcov_var
.error
;
178 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
179 file. Returns +1 for same endian, -1 for other endian and zero for
183 gcov_magic (gcov_unsigned_t magic
, gcov_unsigned_t expected
)
185 if (magic
== expected
)
187 magic
= (magic
>> 16) | (magic
<< 16);
188 magic
= ((magic
& 0xff00ff) << 8) | ((magic
>> 8) & 0xff00ff);
189 if (magic
== expected
)
200 gcov_allocate (unsigned length
)
202 size_t new_size
= gcov_var
.alloc
;
205 new_size
= GCOV_BLOCK_SIZE
;
209 gcov_var
.alloc
= new_size
;
210 gcov_var
.buffer
= XRESIZEVAR (gcov_unsigned_t
, gcov_var
.buffer
, new_size
<< 2);
215 /* Write out the current block, if needs be. */
218 gcov_write_block (unsigned size
)
220 if (fwrite (gcov_var
.buffer
, size
<< 2, 1, gcov_var
.file
) != 1)
222 gcov_var
.start
+= size
;
223 gcov_var
.offset
-= size
;
226 /* Allocate space to write BYTES bytes to the gcov file. Return a
227 pointer to those bytes, or NULL on failure. */
229 static gcov_unsigned_t
*
230 gcov_write_words (unsigned words
)
232 gcov_unsigned_t
*result
;
234 gcc_assert (gcov_var
.mode
< 0);
236 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
238 gcov_write_block (GCOV_BLOCK_SIZE
);
241 gcc_assert (gcov_var
.offset
== 1);
242 memcpy (gcov_var
.buffer
, gcov_var
.buffer
+ GCOV_BLOCK_SIZE
, 4);
246 if (gcov_var
.offset
+ words
> gcov_var
.alloc
)
247 gcov_allocate (gcov_var
.offset
+ words
);
249 result
= &gcov_var
.buffer
[gcov_var
.offset
];
250 gcov_var
.offset
+= words
;
255 /* Write unsigned VALUE to coverage file. Sets error flag
259 gcov_write_unsigned (gcov_unsigned_t value
)
261 gcov_unsigned_t
*buffer
= gcov_write_words (1);
266 /* Write counter VALUE to coverage file. Sets error flag
271 gcov_write_counter (gcov_type value
)
273 gcov_unsigned_t
*buffer
= gcov_write_words (2);
275 buffer
[0] = (gcov_unsigned_t
) value
;
276 if (sizeof (value
) > sizeof (gcov_unsigned_t
))
277 buffer
[1] = (gcov_unsigned_t
) (value
>> 32);
281 #endif /* IN_LIBGCOV */
284 /* Write STRING to coverage file. Sets error flag on file
285 error, overflow flag on overflow */
288 gcov_write_string (const char *string
)
292 gcov_unsigned_t
*buffer
;
296 length
= strlen (string
);
297 alloc
= (length
+ 4) >> 2;
300 buffer
= gcov_write_words (1 + alloc
);
304 memcpy (&buffer
[1], string
, length
);
309 /* Write a tag TAG and reserve space for the record length. Return a
310 value to be used for gcov_write_length. */
312 GCOV_LINKAGE gcov_position_t
313 gcov_write_tag (gcov_unsigned_t tag
)
315 gcov_position_t result
= gcov_var
.start
+ gcov_var
.offset
;
316 gcov_unsigned_t
*buffer
= gcov_write_words (2);
324 /* Write a record length using POSITION, which was returned by
325 gcov_write_tag. The current file position is the end of the
326 record, and is restored before returning. Returns nonzero on
330 gcov_write_length (gcov_position_t position
)
333 gcov_unsigned_t length
;
334 gcov_unsigned_t
*buffer
;
336 gcc_assert (gcov_var
.mode
< 0);
337 gcc_assert (position
+ 2 <= gcov_var
.start
+ gcov_var
.offset
);
338 gcc_assert (position
>= gcov_var
.start
);
339 offset
= position
- gcov_var
.start
;
340 length
= gcov_var
.offset
- offset
- 2;
341 buffer
= (gcov_unsigned_t
*) &gcov_var
.buffer
[offset
];
343 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
344 gcov_write_block (gcov_var
.offset
);
347 #else /* IN_LIBGCOV */
349 /* Write a tag TAG and length LENGTH. */
352 gcov_write_tag_length (gcov_unsigned_t tag
, gcov_unsigned_t length
)
354 gcov_unsigned_t
*buffer
= gcov_write_words (2);
360 /* Write a summary structure to the gcov file. Return nonzero on
364 gcov_write_summary (gcov_unsigned_t tag
, const struct gcov_summary
*summary
)
367 const struct gcov_ctr_summary
*csum
;
369 gcov_write_tag_length (tag
, GCOV_TAG_SUMMARY_LENGTH
);
370 gcov_write_unsigned (summary
->checksum
);
371 for (csum
= summary
->ctrs
, ix
= GCOV_COUNTERS_SUMMABLE
; ix
--; csum
++)
373 gcov_write_unsigned (csum
->num
);
374 gcov_write_unsigned (csum
->runs
);
375 gcov_write_counter (csum
->sum_all
);
376 gcov_write_counter (csum
->run_max
);
377 gcov_write_counter (csum
->sum_max
);
380 #endif /* IN_LIBGCOV */
384 /* Return a pointer to read BYTES bytes from the gcov file. Returns
385 NULL on failure (read past EOF). */
387 static const gcov_unsigned_t
*
388 gcov_read_words (unsigned words
)
390 const gcov_unsigned_t
*result
;
391 unsigned excess
= gcov_var
.length
- gcov_var
.offset
;
393 gcc_assert (gcov_var
.mode
> 0);
396 gcov_var
.start
+= gcov_var
.offset
;
400 gcc_assert (excess
== 1);
401 memcpy (gcov_var
.buffer
, gcov_var
.buffer
+ gcov_var
.offset
, 4);
404 memmove (gcov_var
.buffer
, gcov_var
.buffer
+ gcov_var
.offset
, excess
* 4);
407 gcov_var
.length
= excess
;
409 gcc_assert (!gcov_var
.length
|| gcov_var
.length
== 1);
410 excess
= GCOV_BLOCK_SIZE
;
412 if (gcov_var
.length
+ words
> gcov_var
.alloc
)
413 gcov_allocate (gcov_var
.length
+ words
);
414 excess
= gcov_var
.alloc
- gcov_var
.length
;
416 excess
= fread (gcov_var
.buffer
+ gcov_var
.length
,
417 1, excess
<< 2, gcov_var
.file
) >> 2;
418 gcov_var
.length
+= excess
;
419 if (gcov_var
.length
< words
)
421 gcov_var
.overread
+= words
- gcov_var
.length
;
426 result
= &gcov_var
.buffer
[gcov_var
.offset
];
427 gcov_var
.offset
+= words
;
431 /* Read unsigned value from a coverage file. Sets error flag on file
432 error, overflow flag on overflow */
434 GCOV_LINKAGE gcov_unsigned_t
435 gcov_read_unsigned (void)
437 gcov_unsigned_t value
;
438 const gcov_unsigned_t
*buffer
= gcov_read_words (1);
442 value
= from_file (buffer
[0]);
446 /* Read counter value from a coverage file. Sets error flag on file
447 error, overflow flag on overflow */
449 GCOV_LINKAGE gcov_type
450 gcov_read_counter (void)
453 const gcov_unsigned_t
*buffer
= gcov_read_words (2);
457 value
= from_file (buffer
[0]);
458 if (sizeof (value
) > sizeof (gcov_unsigned_t
))
459 value
|= ((gcov_type
) from_file (buffer
[1])) << 32;
466 /* Read string from coverage file. Returns a pointer to a static
467 buffer, or NULL on empty string. You must copy the string before
468 calling another gcov function. */
471 GCOV_LINKAGE
const char *
472 gcov_read_string (void)
474 unsigned length
= gcov_read_unsigned ();
479 return (const char *) gcov_read_words (length
);
484 gcov_read_summary (struct gcov_summary
*summary
)
487 struct gcov_ctr_summary
*csum
;
489 summary
->checksum
= gcov_read_unsigned ();
490 for (csum
= summary
->ctrs
, ix
= GCOV_COUNTERS_SUMMABLE
; ix
--; csum
++)
492 csum
->num
= gcov_read_unsigned ();
493 csum
->runs
= gcov_read_unsigned ();
494 csum
->sum_all
= gcov_read_counter ();
495 csum
->run_max
= gcov_read_counter ();
496 csum
->sum_max
= gcov_read_counter ();
501 /* Reset to a known position. BASE should have been obtained from
502 gcov_position, LENGTH should be a record length. */
505 gcov_sync (gcov_position_t base
, gcov_unsigned_t length
)
507 gcc_assert (gcov_var
.mode
> 0);
509 if (base
- gcov_var
.start
<= gcov_var
.length
)
510 gcov_var
.offset
= base
- gcov_var
.start
;
513 gcov_var
.offset
= gcov_var
.length
= 0;
514 fseek (gcov_var
.file
, base
<< 2, SEEK_SET
);
515 gcov_var
.start
= ftell (gcov_var
.file
) >> 2;
521 /* Move to a given position in a gcov file. */
524 gcov_seek (gcov_position_t base
)
526 gcc_assert (gcov_var
.mode
< 0);
528 gcov_write_block (gcov_var
.offset
);
529 fseek (gcov_var
.file
, base
<< 2, SEEK_SET
);
530 gcov_var
.start
= ftell (gcov_var
.file
) >> 2;
535 /* Return the modification time of the current gcov file. */
542 if (fstat (fileno (gcov_var
.file
), &status
))
545 return status
.st_mtime
;