1 /* SPDX-License-Identifier: GPL-3.0-only WITH GCC-exception-3.1 */
3 /* File format for coverage information
4 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005, 2007,
5 2008 Free Software Foundation, Inc.
6 Contributed by Bob Manson <manson@cygnus.com>.
7 Completely remangled by Nathan Sidwell <nathan@codesourcery.com>.
9 This file is part of GCC.
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 Under Section 7 of GPL version 3, you are granted additional
22 permissions described in the GCC Runtime Library Exception, version
23 3.1, as published by the Free Software Foundation.
26 /* Routines declared in gcov-io.h. This file should be #included by
27 another source file, after having #included gcov-io.h. */
30 static void gcov_write_block(unsigned int);
31 static gcov_unsigned_t
*gcov_write_words(unsigned int);
33 static const gcov_unsigned_t
*gcov_read_words(unsigned int);
35 static void gcov_allocate(unsigned int);
38 static inline gcov_unsigned_t
from_file(gcov_unsigned_t value
)
41 if (gcov_var
.endian
) {
42 value
= (value
>> 16) | (value
<< 16);
43 value
= ((value
& 0xff00ff) << 8) | ((value
>> 8) & 0xff00ff);
49 /* Open a gcov file. NAME is the name of the file to open and MODE
50 indicates whether a new file should be created, or an existing file
51 opened. If MODE is >= 0 an existing file will be opened, if
52 possible, and if MODE is <= 0, a new file will be created. Use
53 MODE=0 to attempt to reopen an existing file and then fall back on
54 creating a new one. If MODE < 0, the file will be opened in
55 read-only mode. Otherwise it will be opened for modification.
56 Return zero on failure, >0 on opening an existing file and <0 on
57 creating a new one. */
61 gcov_open(const char *name
)
63 gcov_open(const char *name
, int mode
)
73 s_flock
.l_whence
= SEEK_SET
;
75 s_flock
.l_len
= 0; /* Until EOF. */
76 s_flock
.l_pid
= getpid();
79 gcc_assert(!gcov_var
.file
);
81 gcov_var
.offset
= gcov_var
.length
= 0;
82 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
);
93 /* Write mode - acquire a write-lock. */
94 s_flock
.l_type
= F_WRLCK
;
95 fd
= open(name
, O_RDWR
| O_CREAT
, 0666);
100 while (fcntl(fd
, F_SETLKW
, &s_flock
) && errno
== EINTR
)
103 gcov_var
.file
= fdopen(fd
, (mode
> 0) ? "rb" : "r+b");
105 if (!gcov_var
.file
) {
112 else if (mode
== 0) {
115 if (fstat(fd
, &st
) < 0) {
116 fclose(gcov_var
.file
);
123 gcov_var
.mode
= mode
* 2 + 1;
125 gcov_var
.mode
= mode
* 2 + 1;
128 gcov_var
.file
= fopen(name
, (mode
> 0) ? "rb" : "r+b");
132 else if (mode
<= 0) {
133 gcov_var
.file
= fopen(name
, "w+b");
135 gcov_var
.mode
= mode
* 2 + 1;
141 setbuf(gcov_var
.file
, (char *)0);
146 /* Close the current gcov file. Flushes data to disk. Returns nonzero
147 on failure or error flag set. */
154 if (gcov_var
.offset
&& gcov_var
.mode
< 0)
155 gcov_write_block(gcov_var
.offset
);
157 fclose(gcov_var
.file
);
162 free(gcov_var
.buffer
);
167 return gcov_var
.error
;
171 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
172 file. Returns +1 for same endian, -1 for other endian and zero for
176 gcov_magic(gcov_unsigned_t magic
, gcov_unsigned_t expected
)
178 if (magic
== expected
)
180 magic
= (magic
>> 16) | (magic
<< 16);
181 magic
= ((magic
& 0xff00ff) << 8) | ((magic
>> 8) & 0xff00ff);
182 if (magic
== expected
) {
192 gcov_allocate(unsigned int length
)
194 size_t new_size
= gcov_var
.alloc
;
197 new_size
= GCOV_BLOCK_SIZE
;
201 gcov_var
.alloc
= new_size
;
202 gcov_var
.buffer
= XRESIZEVAR(gcov_unsigned_t
, gcov_var
.buffer
,
208 /* Write out the current block, if needs be. */
211 gcov_write_block(unsigned int size
)
213 if (fwrite(gcov_var
.buffer
, size
<< 2, 1, gcov_var
.file
) != 1)
215 gcov_var
.start
+= size
;
216 gcov_var
.offset
-= size
;
219 /* Allocate space to write BYTES bytes to the gcov file. Return a
220 pointer to those bytes, or NULL on failure. */
222 static gcov_unsigned_t
*
223 gcov_write_words(unsigned int words
)
225 gcov_unsigned_t
*result
;
227 gcc_assert(gcov_var
.mode
< 0);
229 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
) {
230 gcov_write_block(GCOV_BLOCK_SIZE
);
231 if (gcov_var
.offset
) {
232 gcc_assert(gcov_var
.offset
== 1);
233 memcpy(gcov_var
.buffer
, gcov_var
.buffer
234 + GCOV_BLOCK_SIZE
, 4);
238 if (gcov_var
.offset
+ words
> gcov_var
.alloc
)
239 gcov_allocate(gcov_var
.offset
+ words
);
241 result
= &gcov_var
.buffer
[gcov_var
.offset
];
242 gcov_var
.offset
+= words
;
247 /* Write unsigned VALUE to coverage file. Sets error flag
251 gcov_write_unsigned(gcov_unsigned_t value
)
253 gcov_unsigned_t
*buffer
= gcov_write_words(1);
258 /* Write counter VALUE to coverage file. Sets error flag
263 gcov_write_counter(gcov_type value
)
265 gcov_unsigned_t
*buffer
= gcov_write_words(2);
267 buffer
[0] = (gcov_unsigned_t
) value
;
268 if (sizeof(value
) > sizeof(gcov_unsigned_t
))
269 buffer
[1] = (gcov_unsigned_t
) (value
>> 32);
273 #endif /* IN_LIBGCOV */
276 /* Write STRING to coverage file. Sets error flag on file
277 error, overflow flag on overflow */
280 gcov_write_string(const char *string
)
282 unsigned int length
= 0;
283 unsigned int alloc
= 0;
284 gcov_unsigned_t
*buffer
;
287 length
= strlen(string
);
288 alloc
= (length
+ 4) >> 2;
291 buffer
= gcov_write_words(1 + alloc
);
295 memcpy(&buffer
[1], string
, length
);
300 /* Write a tag TAG and reserve space for the record length. Return a
301 value to be used for gcov_write_length. */
303 GCOV_LINKAGE gcov_position_t
304 gcov_write_tag(gcov_unsigned_t tag
)
306 gcov_position_t result
= gcov_var
.start
+ gcov_var
.offset
;
307 gcov_unsigned_t
*buffer
= gcov_write_words(2);
315 /* Write a record length using POSITION, which was returned by
316 gcov_write_tag. The current file position is the end of the
317 record, and is restored before returning. Returns nonzero on
321 gcov_write_length(gcov_position_t position
)
324 gcov_unsigned_t length
;
325 gcov_unsigned_t
*buffer
;
327 gcc_assert(gcov_var
.mode
< 0);
328 gcc_assert(position
+ 2 <= gcov_var
.start
+ gcov_var
.offset
);
329 gcc_assert(position
>= gcov_var
.start
);
330 offset
= position
- gcov_var
.start
;
331 length
= gcov_var
.offset
- offset
- 2;
332 buffer
= (gcov_unsigned_t
*) &gcov_var
.buffer
[offset
];
334 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
335 gcov_write_block(gcov_var
.offset
);
338 #else /* IN_LIBGCOV */
340 /* Write a tag TAG and length LENGTH. */
343 gcov_write_tag_length(gcov_unsigned_t tag
, gcov_unsigned_t length
)
345 gcov_unsigned_t
*buffer
= gcov_write_words(2);
351 /* Write a summary structure to the gcov file. Return nonzero on
355 gcov_write_summary(gcov_unsigned_t tag
, const struct gcov_summary
*summary
)
358 const struct gcov_ctr_summary
*csum
;
360 gcov_write_tag_length(tag
, GCOV_TAG_SUMMARY_LENGTH
);
361 gcov_write_unsigned(summary
->checksum
);
362 for (csum
= summary
->ctrs
, ix
= GCOV_COUNTERS_SUMMABLE
; ix
--; csum
++) {
363 gcov_write_unsigned(csum
->num
);
364 gcov_write_unsigned(csum
->runs
);
365 gcov_write_counter(csum
->sum_all
);
366 gcov_write_counter(csum
->run_max
);
367 gcov_write_counter(csum
->sum_max
);
370 #endif /* IN_LIBGCOV */
374 /* Return a pointer to read BYTES bytes from the gcov file. Returns
375 NULL on failure (read past EOF). */
377 static const gcov_unsigned_t
*
378 gcov_read_words(unsigned int words
)
380 const gcov_unsigned_t
*result
;
381 unsigned int excess
= gcov_var
.length
- gcov_var
.offset
;
383 gcc_assert(gcov_var
.mode
> 0);
384 if (excess
< words
) {
385 gcov_var
.start
+= gcov_var
.offset
;
388 gcc_assert(excess
== 1);
389 memcpy(gcov_var
.buffer
, gcov_var
.buffer
390 + gcov_var
.offset
, 4);
393 memmove(gcov_var
.buffer
, gcov_var
.buffer
394 + gcov_var
.offset
, excess
* 4);
397 gcov_var
.length
= excess
;
399 gcc_assert(!gcov_var
.length
|| gcov_var
.length
== 1);
400 excess
= GCOV_BLOCK_SIZE
;
402 if (gcov_var
.length
+ words
> gcov_var
.alloc
)
403 gcov_allocate(gcov_var
.length
+ words
);
404 excess
= gcov_var
.alloc
- gcov_var
.length
;
406 excess
= fread(gcov_var
.buffer
+ gcov_var
.length
,
407 1, excess
<< 2, gcov_var
.file
) >> 2;
408 gcov_var
.length
+= excess
;
409 if (gcov_var
.length
< words
) {
410 gcov_var
.overread
+= words
- gcov_var
.length
;
415 result
= &gcov_var
.buffer
[gcov_var
.offset
];
416 gcov_var
.offset
+= words
;
420 /* Read unsigned value from a coverage file. Sets error flag on file
421 error, overflow flag on overflow */
423 GCOV_LINKAGE gcov_unsigned_t
424 gcov_read_unsigned(void)
426 gcov_unsigned_t value
;
427 const gcov_unsigned_t
*buffer
= gcov_read_words(1);
431 value
= from_file(buffer
[0]);
435 /* Read counter value from a coverage file. Sets error flag on file
436 error, overflow flag on overflow */
438 GCOV_LINKAGE gcov_type
439 gcov_read_counter(void)
442 const gcov_unsigned_t
*buffer
= gcov_read_words(2);
446 value
= from_file(buffer
[0]);
447 if (sizeof(value
) > sizeof(gcov_unsigned_t
))
448 value
|= ((gcov_type
) from_file(buffer
[1])) << 32;
455 /* Read string from coverage file. Returns a pointer to a static
456 buffer, or NULL on empty string. You must copy the string before
457 calling another gcov function. */
460 GCOV_LINKAGE
const char *
461 gcov_read_string(void)
463 unsigned int length
= gcov_read_unsigned();
468 return (const char *) gcov_read_words(length
);
473 gcov_read_summary(struct gcov_summary
*summary
)
476 struct gcov_ctr_summary
*csum
;
478 summary
->checksum
= gcov_read_unsigned();
479 for (csum
= summary
->ctrs
, ix
= GCOV_COUNTERS_SUMMABLE
; ix
--; csum
++) {
480 csum
->num
= gcov_read_unsigned();
481 csum
->runs
= gcov_read_unsigned();
482 csum
->sum_all
= gcov_read_counter();
483 csum
->run_max
= gcov_read_counter();
484 csum
->sum_max
= gcov_read_counter();
489 /* Reset to a known position. BASE should have been obtained from
490 gcov_position, LENGTH should be a record length. */
493 gcov_sync(gcov_position_t base
, gcov_unsigned_t length
)
495 gcc_assert(gcov_var
.mode
> 0);
497 if (base
- gcov_var
.start
<= gcov_var
.length
)
498 gcov_var
.offset
= base
- gcov_var
.start
;
500 gcov_var
.offset
= gcov_var
.length
= 0;
501 fseek(gcov_var
.file
, base
<< 2, SEEK_SET
);
502 gcov_var
.start
= ftell(gcov_var
.file
) >> 2;
508 /* Move to a given position in a gcov file. */
511 gcov_seek(gcov_position_t base
)
513 gcc_assert(gcov_var
.mode
< 0);
515 gcov_write_block(gcov_var
.offset
);
516 fseek(gcov_var
.file
, base
<< 2, SEEK_SET
);
517 gcov_var
.start
= ftell(gcov_var
.file
) >> 2;
522 /* Return the modification time of the current gcov file. */
529 if (fstat(fileno(gcov_var
.file
), &status
))
532 return status
.st_mtime
;