Martin Roth | fb8876d | 2022-08-07 15:12:12 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-3.0-only WITH GCC-exception-3.1 */ |
| 2 | |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 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>. |
| 8 | |
| 9 | This file is part of GCC. |
| 10 | |
| 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 |
| 14 | version. |
| 15 | |
| 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 |
| 19 | for more details. |
| 20 | |
| 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. |
Patrick Georgi | a73b931 | 2015-10-31 11:55:10 +0100 | [diff] [blame] | 24 | */ |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 25 | |
| 26 | /* Routines declared in gcov-io.h. This file should be #included by |
| 27 | another source file, after having #included gcov-io.h. */ |
| 28 | |
| 29 | #if !IN_GCOV |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 30 | static void gcov_write_block(unsigned int); |
| 31 | static gcov_unsigned_t *gcov_write_words(unsigned int); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 32 | #endif |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 33 | static const gcov_unsigned_t *gcov_read_words(unsigned int); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 34 | #if !IN_LIBGCOV |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 35 | static void gcov_allocate(unsigned int); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 36 | #endif |
| 37 | |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 38 | static inline gcov_unsigned_t from_file(gcov_unsigned_t value) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 39 | { |
| 40 | #if !IN_LIBGCOV |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 41 | if (gcov_var.endian) { |
| 42 | value = (value >> 16) | (value << 16); |
| 43 | value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff); |
| 44 | } |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 45 | #endif |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 46 | return value; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 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. */ |
| 58 | |
| 59 | GCOV_LINKAGE int |
| 60 | #if IN_LIBGCOV |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 61 | gcov_open(const char *name) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 62 | #else |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 63 | gcov_open(const char *name, int mode) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 64 | #endif |
| 65 | { |
| 66 | #if IN_LIBGCOV |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 67 | const int mode = 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 68 | #endif |
| 69 | #if GCOV_LOCKED |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 70 | struct flock s_flock; |
| 71 | int fd; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 72 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 73 | s_flock.l_whence = SEEK_SET; |
| 74 | s_flock.l_start = 0; |
| 75 | s_flock.l_len = 0; /* Until EOF. */ |
| 76 | s_flock.l_pid = getpid(); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 77 | #endif |
| 78 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 79 | gcc_assert(!gcov_var.file); |
| 80 | gcov_var.start = 0; |
| 81 | gcov_var.offset = gcov_var.length = 0; |
| 82 | gcov_var.overread = -1u; |
| 83 | gcov_var.error = 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 84 | #if !IN_LIBGCOV |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 85 | gcov_var.endian = 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 86 | #endif |
| 87 | #if GCOV_LOCKED |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 88 | if (mode > 0) { |
| 89 | /* Read-only mode - acquire a read-lock. */ |
| 90 | s_flock.l_type = F_RDLCK; |
| 91 | fd = open(name, O_RDONLY); |
| 92 | } else { |
| 93 | /* Write mode - acquire a write-lock. */ |
| 94 | s_flock.l_type = F_WRLCK; |
| 95 | fd = open(name, O_RDWR | O_CREAT, 0666); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 96 | } |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 97 | if (fd < 0) |
| 98 | return 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 99 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 100 | while (fcntl(fd, F_SETLKW, &s_flock) && errno == EINTR) |
| 101 | continue; |
| 102 | |
| 103 | gcov_var.file = fdopen(fd, (mode > 0) ? "rb" : "r+b"); |
| 104 | |
| 105 | if (!gcov_var.file) { |
| 106 | close(fd); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | if (mode > 0) |
| 111 | gcov_var.mode = 1; |
| 112 | else if (mode == 0) { |
| 113 | struct stat st; |
| 114 | |
| 115 | if (fstat(fd, &st) < 0) { |
| 116 | fclose(gcov_var.file); |
| 117 | gcov_var.file = 0; |
| 118 | return 0; |
| 119 | } |
| 120 | if (st.st_size != 0) |
| 121 | gcov_var.mode = 1; |
| 122 | else |
| 123 | gcov_var.mode = mode * 2 + 1; |
| 124 | } else |
| 125 | gcov_var.mode = mode * 2 + 1; |
| 126 | #else |
| 127 | if (mode >= 0) |
| 128 | gcov_var.file = fopen(name, (mode > 0) ? "rb" : "r+b"); |
| 129 | |
| 130 | if (gcov_var.file) |
| 131 | gcov_var.mode = 1; |
| 132 | else if (mode <= 0) { |
| 133 | gcov_var.file = fopen(name, "w+b"); |
| 134 | if (gcov_var.file) |
| 135 | gcov_var.mode = mode * 2 + 1; |
| 136 | } |
| 137 | if (!gcov_var.file) |
| 138 | return 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 139 | #endif |
| 140 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 141 | setbuf(gcov_var.file, (char *)0); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 142 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 143 | return 1; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* Close the current gcov file. Flushes data to disk. Returns nonzero |
| 147 | on failure or error flag set. */ |
| 148 | |
| 149 | GCOV_LINKAGE int |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 150 | gcov_close(void) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 151 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 152 | if (gcov_var.file) { |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 153 | #if !IN_GCOV |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 154 | if (gcov_var.offset && gcov_var.mode < 0) |
| 155 | gcov_write_block(gcov_var.offset); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 156 | #endif |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 157 | fclose(gcov_var.file); |
| 158 | gcov_var.file = 0; |
| 159 | gcov_var.length = 0; |
| 160 | } |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 161 | #if !IN_LIBGCOV |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 162 | free(gcov_var.buffer); |
| 163 | gcov_var.alloc = 0; |
| 164 | gcov_var.buffer = 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 165 | #endif |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 166 | gcov_var.mode = 0; |
| 167 | return gcov_var.error; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | #if !IN_LIBGCOV |
| 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 |
| 173 | not EXPECTED. */ |
| 174 | |
| 175 | GCOV_LINKAGE int |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 176 | gcov_magic(gcov_unsigned_t magic, gcov_unsigned_t expected) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 177 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 178 | if (magic == expected) |
| 179 | return 1; |
| 180 | magic = (magic >> 16) | (magic << 16); |
| 181 | magic = ((magic & 0xff00ff) << 8) | ((magic >> 8) & 0xff00ff); |
| 182 | if (magic == expected) { |
| 183 | gcov_var.endian = 1; |
| 184 | return -1; |
| 185 | } |
| 186 | return 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 187 | } |
| 188 | #endif |
| 189 | |
| 190 | #if !IN_LIBGCOV |
| 191 | static void |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 192 | gcov_allocate(unsigned int length) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 193 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 194 | size_t new_size = gcov_var.alloc; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 195 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 196 | if (!new_size) |
| 197 | new_size = GCOV_BLOCK_SIZE; |
| 198 | new_size += length; |
| 199 | new_size *= 2; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 200 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 201 | gcov_var.alloc = new_size; |
| 202 | gcov_var.buffer = XRESIZEVAR(gcov_unsigned_t, gcov_var.buffer, |
| 203 | new_size << 2); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 204 | } |
| 205 | #endif |
| 206 | |
| 207 | #if !IN_GCOV |
| 208 | /* Write out the current block, if needs be. */ |
| 209 | |
| 210 | static void |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 211 | gcov_write_block(unsigned int size) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 212 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 213 | if (fwrite(gcov_var.buffer, size << 2, 1, gcov_var.file) != 1) |
| 214 | gcov_var.error = 1; |
| 215 | gcov_var.start += size; |
| 216 | gcov_var.offset -= size; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* Allocate space to write BYTES bytes to the gcov file. Return a |
| 220 | pointer to those bytes, or NULL on failure. */ |
| 221 | |
| 222 | static gcov_unsigned_t * |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 223 | gcov_write_words(unsigned int words) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 224 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 225 | gcov_unsigned_t *result; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 226 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 227 | gcc_assert(gcov_var.mode < 0); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 228 | #if IN_LIBGCOV |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 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); |
| 235 | } |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 236 | } |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 237 | #else |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 238 | if (gcov_var.offset + words > gcov_var.alloc) |
| 239 | gcov_allocate(gcov_var.offset + words); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 240 | #endif |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 241 | result = &gcov_var.buffer[gcov_var.offset]; |
| 242 | gcov_var.offset += words; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 243 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 244 | return result; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* Write unsigned VALUE to coverage file. Sets error flag |
| 248 | appropriately. */ |
| 249 | |
| 250 | GCOV_LINKAGE void |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 251 | gcov_write_unsigned(gcov_unsigned_t value) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 252 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 253 | gcov_unsigned_t *buffer = gcov_write_words(1); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 254 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 255 | buffer[0] = value; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /* Write counter VALUE to coverage file. Sets error flag |
| 259 | appropriately. */ |
| 260 | |
| 261 | #if IN_LIBGCOV |
| 262 | GCOV_LINKAGE void |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 263 | gcov_write_counter(gcov_type value) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 264 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 265 | gcov_unsigned_t *buffer = gcov_write_words(2); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 266 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 267 | buffer[0] = (gcov_unsigned_t) value; |
| 268 | if (sizeof(value) > sizeof(gcov_unsigned_t)) |
| 269 | buffer[1] = (gcov_unsigned_t) (value >> 32); |
| 270 | else |
| 271 | buffer[1] = 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 272 | } |
| 273 | #endif /* IN_LIBGCOV */ |
| 274 | |
| 275 | #if !IN_LIBGCOV |
| 276 | /* Write STRING to coverage file. Sets error flag on file |
| 277 | error, overflow flag on overflow */ |
| 278 | |
| 279 | GCOV_LINKAGE void |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 280 | gcov_write_string(const char *string) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 281 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 282 | unsigned int length = 0; |
| 283 | unsigned int alloc = 0; |
| 284 | gcov_unsigned_t *buffer; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 285 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 286 | if (string) { |
| 287 | length = strlen(string); |
| 288 | alloc = (length + 4) >> 2; |
| 289 | } |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 290 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 291 | buffer = gcov_write_words(1 + alloc); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 292 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 293 | buffer[0] = alloc; |
| 294 | buffer[alloc] = 0; |
| 295 | memcpy(&buffer[1], string, length); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 296 | } |
| 297 | #endif |
| 298 | |
| 299 | #if !IN_LIBGCOV |
| 300 | /* Write a tag TAG and reserve space for the record length. Return a |
| 301 | value to be used for gcov_write_length. */ |
| 302 | |
| 303 | GCOV_LINKAGE gcov_position_t |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 304 | gcov_write_tag(gcov_unsigned_t tag) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 305 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 306 | gcov_position_t result = gcov_var.start + gcov_var.offset; |
| 307 | gcov_unsigned_t *buffer = gcov_write_words(2); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 308 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 309 | buffer[0] = tag; |
| 310 | buffer[1] = 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 311 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 312 | return result; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 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 |
| 318 | overflow. */ |
| 319 | |
| 320 | GCOV_LINKAGE void |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 321 | gcov_write_length(gcov_position_t position) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 322 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 323 | unsigned int offset; |
| 324 | gcov_unsigned_t length; |
| 325 | gcov_unsigned_t *buffer; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 326 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 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]; |
| 333 | buffer[1] = length; |
| 334 | if (gcov_var.offset >= GCOV_BLOCK_SIZE) |
| 335 | gcov_write_block(gcov_var.offset); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | #else /* IN_LIBGCOV */ |
| 339 | |
| 340 | /* Write a tag TAG and length LENGTH. */ |
| 341 | |
| 342 | GCOV_LINKAGE void |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 343 | gcov_write_tag_length(gcov_unsigned_t tag, gcov_unsigned_t length) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 344 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 345 | gcov_unsigned_t *buffer = gcov_write_words(2); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 346 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 347 | buffer[0] = tag; |
| 348 | buffer[1] = length; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* Write a summary structure to the gcov file. Return nonzero on |
| 352 | overflow. */ |
| 353 | |
| 354 | GCOV_LINKAGE void |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 355 | gcov_write_summary(gcov_unsigned_t tag, const struct gcov_summary *summary) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 356 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 357 | unsigned int ix; |
| 358 | const struct gcov_ctr_summary *csum; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 359 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 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); |
| 368 | } |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 369 | } |
| 370 | #endif /* IN_LIBGCOV */ |
| 371 | |
| 372 | #endif /*!IN_GCOV */ |
| 373 | |
| 374 | /* Return a pointer to read BYTES bytes from the gcov file. Returns |
| 375 | NULL on failure (read past EOF). */ |
| 376 | |
| 377 | static const gcov_unsigned_t * |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 378 | gcov_read_words(unsigned int words) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 379 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 380 | const gcov_unsigned_t *result; |
| 381 | unsigned int excess = gcov_var.length - gcov_var.offset; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 382 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 383 | gcc_assert(gcov_var.mode > 0); |
| 384 | if (excess < words) { |
| 385 | gcov_var.start += gcov_var.offset; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 386 | #if IN_LIBGCOV |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 387 | if (excess) { |
| 388 | gcc_assert(excess == 1); |
| 389 | memcpy(gcov_var.buffer, gcov_var.buffer |
| 390 | + gcov_var.offset, 4); |
| 391 | } |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 392 | #else |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 393 | memmove(gcov_var.buffer, gcov_var.buffer |
| 394 | + gcov_var.offset, excess * 4); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 395 | #endif |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 396 | gcov_var.offset = 0; |
| 397 | gcov_var.length = excess; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 398 | #if IN_LIBGCOV |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 399 | gcc_assert(!gcov_var.length || gcov_var.length == 1); |
| 400 | excess = GCOV_BLOCK_SIZE; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 401 | #else |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 402 | if (gcov_var.length + words > gcov_var.alloc) |
| 403 | gcov_allocate(gcov_var.length + words); |
| 404 | excess = gcov_var.alloc - gcov_var.length; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 405 | #endif |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 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; |
| 411 | gcov_var.length = 0; |
| 412 | return 0; |
| 413 | } |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 414 | } |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 415 | result = &gcov_var.buffer[gcov_var.offset]; |
| 416 | gcov_var.offset += words; |
| 417 | return result; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* Read unsigned value from a coverage file. Sets error flag on file |
| 421 | error, overflow flag on overflow */ |
| 422 | |
| 423 | GCOV_LINKAGE gcov_unsigned_t |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 424 | gcov_read_unsigned(void) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 425 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 426 | gcov_unsigned_t value; |
| 427 | const gcov_unsigned_t *buffer = gcov_read_words(1); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 428 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 429 | if (!buffer) |
| 430 | return 0; |
| 431 | value = from_file(buffer[0]); |
| 432 | return value; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* Read counter value from a coverage file. Sets error flag on file |
| 436 | error, overflow flag on overflow */ |
| 437 | |
| 438 | GCOV_LINKAGE gcov_type |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 439 | gcov_read_counter(void) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 440 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 441 | gcov_type value; |
| 442 | const gcov_unsigned_t *buffer = gcov_read_words(2); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 443 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 444 | if (!buffer) |
| 445 | return 0; |
| 446 | value = from_file(buffer[0]); |
| 447 | if (sizeof(value) > sizeof(gcov_unsigned_t)) |
| 448 | value |= ((gcov_type) from_file(buffer[1])) << 32; |
| 449 | else if (buffer[1]) |
| 450 | gcov_var.error = -1; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 451 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 452 | return value; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 453 | } |
| 454 | |
| 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. */ |
| 458 | |
| 459 | #if !IN_LIBGCOV |
| 460 | GCOV_LINKAGE const char * |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 461 | gcov_read_string(void) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 462 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 463 | unsigned int length = gcov_read_unsigned(); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 464 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 465 | if (!length) |
| 466 | return 0; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 467 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 468 | return (const char *) gcov_read_words(length); |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 469 | } |
| 470 | #endif |
| 471 | |
| 472 | GCOV_LINKAGE void |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 473 | gcov_read_summary(struct gcov_summary *summary) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 474 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 475 | unsigned int ix; |
| 476 | struct gcov_ctr_summary *csum; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 477 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 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(); |
| 485 | } |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | #if !IN_LIBGCOV |
| 489 | /* Reset to a known position. BASE should have been obtained from |
| 490 | gcov_position, LENGTH should be a record length. */ |
| 491 | |
| 492 | GCOV_LINKAGE void |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 493 | gcov_sync(gcov_position_t base, gcov_unsigned_t length) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 494 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 495 | gcc_assert(gcov_var.mode > 0); |
| 496 | base += length; |
| 497 | if (base - gcov_var.start <= gcov_var.length) |
| 498 | gcov_var.offset = base - gcov_var.start; |
| 499 | else { |
| 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; |
| 503 | } |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 504 | } |
| 505 | #endif |
| 506 | |
| 507 | #if IN_LIBGCOV |
| 508 | /* Move to a given position in a gcov file. */ |
| 509 | |
| 510 | GCOV_LINKAGE void |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 511 | gcov_seek(gcov_position_t base) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 512 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 513 | gcc_assert(gcov_var.mode < 0); |
| 514 | if (gcov_var.offset) |
| 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; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 518 | } |
| 519 | #endif |
| 520 | |
| 521 | #if IN_GCOV > 0 |
| 522 | /* Return the modification time of the current gcov file. */ |
| 523 | |
| 524 | GCOV_LINKAGE time_t |
Lee Leahy | 38768c3 | 2017-03-09 14:07:18 -0800 | [diff] [blame] | 525 | gcov_time(void) |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 526 | { |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 527 | struct stat status; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 528 | |
Lee Leahy | e20a319 | 2017-03-09 16:21:34 -0800 | [diff] [blame] | 529 | if (fstat(fileno(gcov_var.file), &status)) |
| 530 | return 0; |
| 531 | else |
| 532 | return status.st_mtime; |
Stefan Reinauer | d37ab45 | 2012-12-18 16:23:28 -0800 | [diff] [blame] | 533 | } |
| 534 | #endif /* IN_GCOV */ |