1 /* File format for coverage information
2 Copyright (C) 1996-2018 Free Software Foundation, Inc.
3 Contributed by Bob Manson <manson@cygnus.com>.
4 Completely remangled by Nathan Sidwell <nathan@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 /* Routines declared in gcov-io.h. This file should be #included by
28 another source file, after having #included gcov-io.h. */
31 static void gcov_write_block (unsigned);
32 static gcov_unsigned_t
*gcov_write_words (unsigned);
34 static const gcov_unsigned_t
*gcov_read_words (unsigned);
36 static void gcov_allocate (unsigned);
39 /* Optimum number of gcov_unsigned_t's read from or written to disk. */
40 #define GCOV_BLOCK_SIZE (1 << 10)
45 gcov_position_t start
; /* Position of first byte of block */
46 unsigned offset
; /* Read/write position within the block. */
47 unsigned length
; /* Read limit in the block. */
48 unsigned overread
; /* Number of words overread. */
49 int error
; /* < 0 overflow, > 0 disk error. */
50 int mode
; /* < 0 writing, > 0 reading */
52 /* Holds one block plus 4 bytes, thus all coverage reads & writes
53 fit within this buffer and we always can transfer GCOV_BLOCK_SIZE
54 to and from the disk. libgcov never backtracks and only writes 4
56 gcov_unsigned_t buffer
[GCOV_BLOCK_SIZE
+ 1];
58 int endian
; /* Swap endianness. */
59 /* Holds a variable length block, as the compiler can write
60 strings and needs to backtrack. */
62 gcov_unsigned_t
*buffer
;
66 /* Save the current position in the gcov file. */
67 /* We need to expose this function when compiling for gcov-tool. */
74 gcov_nonruntime_assert (gcov_var
.mode
> 0);
75 return gcov_var
.start
+ gcov_var
.offset
;
78 /* Return nonzero if the error flag is set. */
79 /* We need to expose this function when compiling for gcov-tool. */
86 return gcov_var
.file
? gcov_var
.error
: 1;
90 /* Move to beginning of file and initialize for writing. */
91 GCOV_LINKAGE
inline void
97 fseek (gcov_var
.file
, 0L, SEEK_SET
);
101 static inline gcov_unsigned_t
from_file (gcov_unsigned_t value
)
106 value
= (value
>> 16) | (value
<< 16);
107 value
= ((value
& 0xff00ff) << 8) | ((value
>> 8) & 0xff00ff);
113 /* Open a gcov file. NAME is the name of the file to open and MODE
114 indicates whether a new file should be created, or an existing file
115 opened. If MODE is >= 0 an existing file will be opened, if
116 possible, and if MODE is <= 0, a new file will be created. Use
117 MODE=0 to attempt to reopen an existing file and then fall back on
118 creating a new one. If MODE > 0, the file will be opened in
119 read-only mode. Otherwise it will be opened for modification.
120 Return zero on failure, non-zero on success. */
124 gcov_open (const char *name
)
126 gcov_open (const char *name
, int mode
)
133 struct flock s_flock
;
136 s_flock
.l_whence
= SEEK_SET
;
138 s_flock
.l_len
= 0; /* Until EOF. */
139 s_flock
.l_pid
= getpid ();
142 gcov_nonruntime_assert (!gcov_var
.file
);
144 gcov_var
.offset
= gcov_var
.length
= 0;
145 gcov_var
.overread
= -1u;
153 /* Read-only mode - acquire a read-lock. */
154 s_flock
.l_type
= F_RDLCK
;
155 /* pass mode (ignored) for compatibility */
156 fd
= open (name
, O_RDONLY
, S_IRUSR
| S_IWUSR
);
160 /* Write mode - acquire a write-lock. */
161 s_flock
.l_type
= F_WRLCK
;
162 /* Truncate if force new mode. */
163 fd
= open (name
, O_RDWR
| O_CREAT
| (mode
< 0 ? O_TRUNC
: 0), 0666);
168 while (fcntl (fd
, F_SETLKW
, &s_flock
) && errno
== EINTR
)
171 gcov_var
.file
= fdopen (fd
, (mode
> 0) ? "rb" : "r+b");
180 /* Open an existing file. */
181 gcov_var
.file
= fopen (name
, (mode
> 0) ? "rb" : "r+b");
186 /* Create a new file. */
187 gcov_var
.file
= fopen (name
, "w+b");
193 gcov_var
.mode
= mode
? mode
: 1;
195 setbuf (gcov_var
.file
, (char *)0);
200 /* Close the current gcov file. Flushes data to disk. Returns nonzero
201 on failure or error flag set. */
209 if (gcov_var
.offset
&& gcov_var
.mode
< 0)
210 gcov_write_block (gcov_var
.offset
);
212 fclose (gcov_var
.file
);
217 free (gcov_var
.buffer
);
222 return gcov_var
.error
;
226 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
227 file. Returns +1 for same endian, -1 for other endian and zero for
231 gcov_magic (gcov_unsigned_t magic
, gcov_unsigned_t expected
)
233 if (magic
== expected
)
235 magic
= (magic
>> 16) | (magic
<< 16);
236 magic
= ((magic
& 0xff00ff) << 8) | ((magic
>> 8) & 0xff00ff);
237 if (magic
== expected
)
248 gcov_allocate (unsigned length
)
250 size_t new_size
= gcov_var
.alloc
;
253 new_size
= GCOV_BLOCK_SIZE
;
257 gcov_var
.alloc
= new_size
;
258 gcov_var
.buffer
= XRESIZEVAR (gcov_unsigned_t
, gcov_var
.buffer
, new_size
<< 2);
263 /* Write out the current block, if needs be. */
266 gcov_write_block (unsigned size
)
268 if (fwrite (gcov_var
.buffer
, size
<< 2, 1, gcov_var
.file
) != 1)
270 gcov_var
.start
+= size
;
271 gcov_var
.offset
-= size
;
274 /* Allocate space to write BYTES bytes to the gcov file. Return a
275 pointer to those bytes, or NULL on failure. */
277 static gcov_unsigned_t
*
278 gcov_write_words (unsigned words
)
280 gcov_unsigned_t
*result
;
282 gcov_nonruntime_assert (gcov_var
.mode
< 0);
284 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
286 gcov_write_block (GCOV_BLOCK_SIZE
);
289 memcpy (gcov_var
.buffer
, gcov_var
.buffer
+ GCOV_BLOCK_SIZE
, 4);
293 if (gcov_var
.offset
+ words
> gcov_var
.alloc
)
294 gcov_allocate (gcov_var
.offset
+ words
);
296 result
= &gcov_var
.buffer
[gcov_var
.offset
];
297 gcov_var
.offset
+= words
;
302 /* Write unsigned VALUE to coverage file. Sets error flag
306 gcov_write_unsigned (gcov_unsigned_t value
)
308 gcov_unsigned_t
*buffer
= gcov_write_words (1);
313 /* Write counter VALUE to coverage file. Sets error flag
318 gcov_write_counter (gcov_type value
)
320 gcov_unsigned_t
*buffer
= gcov_write_words (2);
322 buffer
[0] = (gcov_unsigned_t
) value
;
323 if (sizeof (value
) > sizeof (gcov_unsigned_t
))
324 buffer
[1] = (gcov_unsigned_t
) (value
>> 32);
328 #endif /* IN_LIBGCOV */
331 /* Write STRING to coverage file. Sets error flag on file
332 error, overflow flag on overflow */
335 gcov_write_string (const char *string
)
339 gcov_unsigned_t
*buffer
;
343 length
= strlen (string
);
344 alloc
= (length
+ 4) >> 2;
347 buffer
= gcov_write_words (1 + alloc
);
353 buffer
[alloc
] = 0; /* place nul terminators. */
354 memcpy (&buffer
[1], string
, length
);
360 /* Write FILENAME to coverage file. Sets error flag on file
361 error, overflow flag on overflow */
364 gcov_write_filename (const char *filename
)
366 if (profile_abs_path_flag
&& filename
&& filename
[0]
367 && !(IS_DIR_SEPARATOR (filename
[0])
368 #if HAVE_DOS_BASED_FILE_SYSTEM
369 || filename
[1] == ':'
373 char *buf
= getcwd (NULL
, 0);
374 if (buf
!= NULL
&& buf
[0])
376 size_t len
= strlen (buf
);
377 buf
= (char*)xrealloc (buf
, len
+ strlen (filename
) + 2);
378 if (!IS_DIR_SEPARATOR (buf
[len
- 1]))
380 strcat (buf
, filename
);
381 gcov_write_string (buf
);
387 gcov_write_string (filename
);
392 /* Write a tag TAG and reserve space for the record length. Return a
393 value to be used for gcov_write_length. */
395 GCOV_LINKAGE gcov_position_t
396 gcov_write_tag (gcov_unsigned_t tag
)
398 gcov_position_t result
= gcov_var
.start
+ gcov_var
.offset
;
399 gcov_unsigned_t
*buffer
= gcov_write_words (2);
407 /* Write a record length using POSITION, which was returned by
408 gcov_write_tag. The current file position is the end of the
409 record, and is restored before returning. Returns nonzero on
413 gcov_write_length (gcov_position_t position
)
416 gcov_unsigned_t length
;
417 gcov_unsigned_t
*buffer
;
419 gcov_nonruntime_assert (gcov_var
.mode
< 0);
420 gcov_nonruntime_assert (position
+ 2 <= gcov_var
.start
+ gcov_var
.offset
);
421 gcov_nonruntime_assert (position
>= gcov_var
.start
);
422 offset
= position
- gcov_var
.start
;
423 length
= gcov_var
.offset
- offset
- 2;
424 buffer
= (gcov_unsigned_t
*) &gcov_var
.buffer
[offset
];
426 if (gcov_var
.offset
>= GCOV_BLOCK_SIZE
)
427 gcov_write_block (gcov_var
.offset
);
430 #else /* IN_LIBGCOV */
432 /* Write a tag TAG and length LENGTH. */
435 gcov_write_tag_length (gcov_unsigned_t tag
, gcov_unsigned_t length
)
437 gcov_unsigned_t
*buffer
= gcov_write_words (2);
443 /* Write a summary structure to the gcov file. Return nonzero on
447 gcov_write_summary (gcov_unsigned_t tag
, const struct gcov_summary
*summary
)
449 gcov_write_tag_length (tag
, GCOV_TAG_SUMMARY_LENGTH
);
450 gcov_write_unsigned (summary
->runs
);
451 gcov_write_unsigned (summary
->sum_max
);
454 #endif /* IN_LIBGCOV */
458 /* Return a pointer to read BYTES bytes from the gcov file. Returns
459 NULL on failure (read past EOF). */
461 static const gcov_unsigned_t
*
462 gcov_read_words (unsigned words
)
464 const gcov_unsigned_t
*result
;
465 unsigned excess
= gcov_var
.length
- gcov_var
.offset
;
467 if (gcov_var
.mode
<= 0)
472 gcov_var
.start
+= gcov_var
.offset
;
476 memcpy (gcov_var
.buffer
, gcov_var
.buffer
+ gcov_var
.offset
, 4);
478 memmove (gcov_var
.buffer
, gcov_var
.buffer
+ gcov_var
.offset
,
483 gcov_var
.length
= excess
;
485 excess
= GCOV_BLOCK_SIZE
;
487 if (gcov_var
.length
+ words
> gcov_var
.alloc
)
488 gcov_allocate (gcov_var
.length
+ words
);
489 excess
= gcov_var
.alloc
- gcov_var
.length
;
491 excess
= fread (gcov_var
.buffer
+ gcov_var
.length
,
492 1, excess
<< 2, gcov_var
.file
) >> 2;
493 gcov_var
.length
+= excess
;
494 if (gcov_var
.length
< words
)
496 gcov_var
.overread
+= words
- gcov_var
.length
;
501 result
= &gcov_var
.buffer
[gcov_var
.offset
];
502 gcov_var
.offset
+= words
;
506 /* Read unsigned value from a coverage file. Sets error flag on file
507 error, overflow flag on overflow */
509 GCOV_LINKAGE gcov_unsigned_t
510 gcov_read_unsigned (void)
512 gcov_unsigned_t value
;
513 const gcov_unsigned_t
*buffer
= gcov_read_words (1);
517 value
= from_file (buffer
[0]);
521 /* Read counter value from a coverage file. Sets error flag on file
522 error, overflow flag on overflow */
524 GCOV_LINKAGE gcov_type
525 gcov_read_counter (void)
528 const gcov_unsigned_t
*buffer
= gcov_read_words (2);
532 value
= from_file (buffer
[0]);
533 if (sizeof (value
) > sizeof (gcov_unsigned_t
))
534 value
|= ((gcov_type
) from_file (buffer
[1])) << 32;
541 /* Mangle filename path of BASE and output new allocated pointer with
545 mangle_path (char const *base
)
547 /* Convert '/' to '#', convert '..' to '^',
548 convert ':' to '~' on DOS based file system. */
550 char *buffer
= (char *)xmalloc (strlen (base
) + 10);
553 #if HAVE_DOS_BASED_FILE_SYSTEM
554 if (base
[0] && base
[1] == ':')
562 for (; *base
; base
= probe
)
566 for (probe
= base
; *probe
; probe
++)
570 if (len
== 2 && base
[0] == '.' && base
[1] == '.')
574 memcpy (ptr
, base
, len
);
584 /* Terminate the string. */
590 /* We need to expose the below function when compiling for gcov-tool. */
592 #if !IN_LIBGCOV || defined (IN_GCOV_TOOL)
593 /* Read string from coverage file. Returns a pointer to a static
594 buffer, or NULL on empty string. You must copy the string before
595 calling another gcov function. */
597 GCOV_LINKAGE
const char *
598 gcov_read_string (void)
600 unsigned length
= gcov_read_unsigned ();
605 return (const char *) gcov_read_words (length
);
610 gcov_read_summary (struct gcov_summary
*summary
)
612 summary
->runs
= gcov_read_unsigned ();
613 summary
->sum_max
= gcov_read_unsigned ();
616 /* We need to expose the below function when compiling for gcov-tool. */
618 #if !IN_LIBGCOV || defined (IN_GCOV_TOOL)
619 /* Reset to a known position. BASE should have been obtained from
620 gcov_position, LENGTH should be a record length. */
623 gcov_sync (gcov_position_t base
, gcov_unsigned_t length
)
625 gcov_nonruntime_assert (gcov_var
.mode
> 0);
627 if (base
- gcov_var
.start
<= gcov_var
.length
)
628 gcov_var
.offset
= base
- gcov_var
.start
;
631 gcov_var
.offset
= gcov_var
.length
= 0;
632 fseek (gcov_var
.file
, base
<< 2, SEEK_SET
);
633 gcov_var
.start
= ftell (gcov_var
.file
) >> 2;
639 /* Move to a given position in a gcov file. */
642 gcov_seek (gcov_position_t base
)
645 gcov_write_block (gcov_var
.offset
);
646 fseek (gcov_var
.file
, base
<< 2, SEEK_SET
);
647 gcov_var
.start
= ftell (gcov_var
.file
) >> 2;
652 /* Return the modification time of the current gcov file. */
659 if (fstat (fileno (gcov_var
.file
), &status
))
662 return status
.st_mtime
;