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 int);
29 static gcov_unsigned_t
*gcov_write_words(unsigned int);
31 static const gcov_unsigned_t
*gcov_read_words(unsigned int);
33 static void gcov_allocate(unsigned int);
36 static inline gcov_unsigned_t
from_file(gcov_unsigned_t value
)
39 if (gcov_var
.endian
) {
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;
87 /* Read-only mode - acquire a read-lock. */
88 s_flock
.l_type
= F_RDLCK
;
89 fd
= open(name
, O_RDONLY
);
91 /* Write mode - acquire a write-lock. */
92 s_flock
.l_type
= F_WRLCK
;
93 fd
= open(name
, O_RDWR
| O_CREAT
, 0666);
98 while (fcntl(fd
, F_SETLKW
, &s_flock
) && errno
== EINTR
)
101 gcov_var
.file
= fdopen(fd
, (mode
> 0) ? "rb" : "r+b");
103 if (!gcov_var
.file
) {
110 else if (mode
== 0) {
113 if (fstat(fd
, &st
) < 0) {
114 fclose(gcov_var
.file
);
121 gcov_var
.mode
= mode
* 2 + 1;
123 gcov_var
.mode
= mode
* 2 + 1;
126 gcov_var
.file
= fopen(name
, (mode
> 0) ? "rb" : "r+b");
130 else if (mode
<= 0) {
131 gcov_var
.file
= fopen(name
, "w+b");
133 gcov_var
.mode
= mode
* 2 + 1;
139 setbuf(gcov_var
.file
, (char *)0);
144 /* Close the current gcov file. Flushes data to disk. Returns nonzero
145 on failure or error flag set. */
152 if (gcov_var
.offset
&& gcov_var
.mode
< 0)
153 gcov_write_block(gcov_var
.offset
);
155 fclose(gcov_var
.file
);
160 free(gcov_var
.buffer
);
165 return gcov_var
.error
;
169 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
170 file. Returns +1 for same endian, -1 for other endian and zero for
174 gcov_magic(gcov_unsigned_t magic
, gcov_unsigned_t expected
)
176 if (magic
== expected
)
178 magic
= (magic
>> 16) | (magic
<< 16);
179 magic
= ((magic
& 0xff00ff) << 8) | ((magic
>> 8) & 0xff00ff);
180 if (magic
== expected
) {
190 gcov_allocate(unsigned int length
)
192 size_t new_size
= gcov_var
.alloc
;
195 new_size
= GCOV_BLOCK_SIZE
;
199 gcov_var
.alloc
= new_size
;
200 gcov_var
.buffer
= XRESIZEVAR(gcov_unsigned_t
, gcov_var
.buffer
,
206 /* Write out the current block, if needs be. */
209 gcov_write_block(unsigned int size
)
211 if (fwrite(gcov_var
.buffer
, size
<< 2, 1, gcov_var
.file
) != 1)
213 gcov_var
.start
+= size
;
214 gcov_var
.offset
-= size
;
217 /* Allocate space to write BYTES bytes to the gcov file. Return a
218 pointer to those bytes, or NULL on failure. */
220 static gcov_unsigned_t
*
221 gcov_write_words(unsigned int words
)
223 gcov_unsigned_t
*result
;
225 gcc_assert(gcov_var
.mode
< 0);
227 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
) {
228 gcov_write_block(GCOV_BLOCK_SIZE
);
229 if (gcov_var
.offset
) {
230 gcc_assert(gcov_var
.offset
== 1);
231 memcpy(gcov_var
.buffer
, gcov_var
.buffer
232 + GCOV_BLOCK_SIZE
, 4);
236 if (gcov_var
.offset
+ words
> gcov_var
.alloc
)
237 gcov_allocate(gcov_var
.offset
+ words
);
239 result
= &gcov_var
.buffer
[gcov_var
.offset
];
240 gcov_var
.offset
+= words
;
245 /* Write unsigned VALUE to coverage file. Sets error flag
249 gcov_write_unsigned(gcov_unsigned_t value
)
251 gcov_unsigned_t
*buffer
= gcov_write_words(1);
256 /* Write counter VALUE to coverage file. Sets error flag
261 gcov_write_counter(gcov_type value
)
263 gcov_unsigned_t
*buffer
= gcov_write_words(2);
265 buffer
[0] = (gcov_unsigned_t
) value
;
266 if (sizeof(value
) > sizeof(gcov_unsigned_t
))
267 buffer
[1] = (gcov_unsigned_t
) (value
>> 32);
271 #endif /* IN_LIBGCOV */
274 /* Write STRING to coverage file. Sets error flag on file
275 error, overflow flag on overflow */
278 gcov_write_string(const char *string
)
280 unsigned int length
= 0;
281 unsigned int alloc
= 0;
282 gcov_unsigned_t
*buffer
;
285 length
= strlen(string
);
286 alloc
= (length
+ 4) >> 2;
289 buffer
= gcov_write_words(1 + alloc
);
293 memcpy(&buffer
[1], string
, length
);
298 /* Write a tag TAG and reserve space for the record length. Return a
299 value to be used for gcov_write_length. */
301 GCOV_LINKAGE gcov_position_t
302 gcov_write_tag(gcov_unsigned_t tag
)
304 gcov_position_t result
= gcov_var
.start
+ gcov_var
.offset
;
305 gcov_unsigned_t
*buffer
= gcov_write_words(2);
313 /* Write a record length using POSITION, which was returned by
314 gcov_write_tag. The current file position is the end of the
315 record, and is restored before returning. Returns nonzero on
319 gcov_write_length(gcov_position_t position
)
322 gcov_unsigned_t length
;
323 gcov_unsigned_t
*buffer
;
325 gcc_assert(gcov_var
.mode
< 0);
326 gcc_assert(position
+ 2 <= gcov_var
.start
+ gcov_var
.offset
);
327 gcc_assert(position
>= gcov_var
.start
);
328 offset
= position
- gcov_var
.start
;
329 length
= gcov_var
.offset
- offset
- 2;
330 buffer
= (gcov_unsigned_t
*) &gcov_var
.buffer
[offset
];
332 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
333 gcov_write_block(gcov_var
.offset
);
336 #else /* IN_LIBGCOV */
338 /* Write a tag TAG and length LENGTH. */
341 gcov_write_tag_length(gcov_unsigned_t tag
, gcov_unsigned_t length
)
343 gcov_unsigned_t
*buffer
= gcov_write_words(2);
349 /* Write a summary structure to the gcov file. Return nonzero on
353 gcov_write_summary(gcov_unsigned_t tag
, const struct gcov_summary
*summary
)
356 const struct gcov_ctr_summary
*csum
;
358 gcov_write_tag_length(tag
, GCOV_TAG_SUMMARY_LENGTH
);
359 gcov_write_unsigned(summary
->checksum
);
360 for (csum
= summary
->ctrs
, ix
= GCOV_COUNTERS_SUMMABLE
; ix
--; csum
++) {
361 gcov_write_unsigned(csum
->num
);
362 gcov_write_unsigned(csum
->runs
);
363 gcov_write_counter(csum
->sum_all
);
364 gcov_write_counter(csum
->run_max
);
365 gcov_write_counter(csum
->sum_max
);
368 #endif /* IN_LIBGCOV */
372 /* Return a pointer to read BYTES bytes from the gcov file. Returns
373 NULL on failure (read past EOF). */
375 static const gcov_unsigned_t
*
376 gcov_read_words(unsigned int words
)
378 const gcov_unsigned_t
*result
;
379 unsigned int excess
= gcov_var
.length
- gcov_var
.offset
;
381 gcc_assert(gcov_var
.mode
> 0);
382 if (excess
< words
) {
383 gcov_var
.start
+= gcov_var
.offset
;
386 gcc_assert(excess
== 1);
387 memcpy(gcov_var
.buffer
, gcov_var
.buffer
388 + gcov_var
.offset
, 4);
391 memmove(gcov_var
.buffer
, gcov_var
.buffer
392 + gcov_var
.offset
, excess
* 4);
395 gcov_var
.length
= excess
;
397 gcc_assert(!gcov_var
.length
|| gcov_var
.length
== 1);
398 excess
= GCOV_BLOCK_SIZE
;
400 if (gcov_var
.length
+ words
> gcov_var
.alloc
)
401 gcov_allocate(gcov_var
.length
+ words
);
402 excess
= gcov_var
.alloc
- gcov_var
.length
;
404 excess
= fread(gcov_var
.buffer
+ gcov_var
.length
,
405 1, excess
<< 2, gcov_var
.file
) >> 2;
406 gcov_var
.length
+= excess
;
407 if (gcov_var
.length
< words
) {
408 gcov_var
.overread
+= words
- gcov_var
.length
;
413 result
= &gcov_var
.buffer
[gcov_var
.offset
];
414 gcov_var
.offset
+= words
;
418 /* Read unsigned value from a coverage file. Sets error flag on file
419 error, overflow flag on overflow */
421 GCOV_LINKAGE gcov_unsigned_t
422 gcov_read_unsigned(void)
424 gcov_unsigned_t value
;
425 const gcov_unsigned_t
*buffer
= gcov_read_words(1);
429 value
= from_file(buffer
[0]);
433 /* Read counter value from a coverage file. Sets error flag on file
434 error, overflow flag on overflow */
436 GCOV_LINKAGE gcov_type
437 gcov_read_counter(void)
440 const gcov_unsigned_t
*buffer
= gcov_read_words(2);
444 value
= from_file(buffer
[0]);
445 if (sizeof(value
) > sizeof(gcov_unsigned_t
))
446 value
|= ((gcov_type
) from_file(buffer
[1])) << 32;
453 /* Read string from coverage file. Returns a pointer to a static
454 buffer, or NULL on empty string. You must copy the string before
455 calling another gcov function. */
458 GCOV_LINKAGE
const char *
459 gcov_read_string(void)
461 unsigned int length
= gcov_read_unsigned();
466 return (const char *) gcov_read_words(length
);
471 gcov_read_summary(struct gcov_summary
*summary
)
474 struct gcov_ctr_summary
*csum
;
476 summary
->checksum
= gcov_read_unsigned();
477 for (csum
= summary
->ctrs
, ix
= GCOV_COUNTERS_SUMMABLE
; ix
--; csum
++) {
478 csum
->num
= gcov_read_unsigned();
479 csum
->runs
= gcov_read_unsigned();
480 csum
->sum_all
= gcov_read_counter();
481 csum
->run_max
= gcov_read_counter();
482 csum
->sum_max
= gcov_read_counter();
487 /* Reset to a known position. BASE should have been obtained from
488 gcov_position, LENGTH should be a record length. */
491 gcov_sync(gcov_position_t base
, gcov_unsigned_t length
)
493 gcc_assert(gcov_var
.mode
> 0);
495 if (base
- gcov_var
.start
<= gcov_var
.length
)
496 gcov_var
.offset
= base
- gcov_var
.start
;
498 gcov_var
.offset
= gcov_var
.length
= 0;
499 fseek(gcov_var
.file
, base
<< 2, SEEK_SET
);
500 gcov_var
.start
= ftell(gcov_var
.file
) >> 2;
506 /* Move to a given position in a gcov file. */
509 gcov_seek(gcov_position_t base
)
511 gcc_assert(gcov_var
.mode
< 0);
513 gcov_write_block(gcov_var
.offset
);
514 fseek(gcov_var
.file
, base
<< 2, SEEK_SET
);
515 gcov_var
.start
= ftell(gcov_var
.file
) >> 2;
520 /* Return the modification time of the current gcov file. */
527 if (fstat(fileno(gcov_var
.file
), &status
))
530 return status
.st_mtime
;