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