blob: 1a41b79f73e110281951c29b41754f69056a2fb7 [file] [log] [blame]
Martin Rothfb8876d2022-08-07 15:12:12 -06001/* SPDX-License-Identifier: GPL-3.0-only WITH GCC-exception-3.1 */
2
Stefan Reinauerd37ab452012-12-18 16:23:28 -08003/* 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
9This file is part of GCC.
10
11GCC is free software; you can redistribute it and/or modify it under
12the terms of the GNU General Public License as published by the Free
13Software Foundation; either version 3, or (at your option) any later
14version.
15
16GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17WARRANTY; without even the implied warranty of MERCHANTABILITY or
18FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19for more details.
20
21Under Section 7 of GPL version 3, you are granted additional
22permissions described in the GCC Runtime Library Exception, version
233.1, as published by the Free Software Foundation.
Patrick Georgia73b9312015-10-31 11:55:10 +010024*/
Stefan Reinauerd37ab452012-12-18 16:23:28 -080025
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 Leahy38768c32017-03-09 14:07:18 -080030static void gcov_write_block(unsigned int);
31static gcov_unsigned_t *gcov_write_words(unsigned int);
Stefan Reinauerd37ab452012-12-18 16:23:28 -080032#endif
Lee Leahy38768c32017-03-09 14:07:18 -080033static const gcov_unsigned_t *gcov_read_words(unsigned int);
Stefan Reinauerd37ab452012-12-18 16:23:28 -080034#if !IN_LIBGCOV
Lee Leahy38768c32017-03-09 14:07:18 -080035static void gcov_allocate(unsigned int);
Stefan Reinauerd37ab452012-12-18 16:23:28 -080036#endif
37
Lee Leahy38768c32017-03-09 14:07:18 -080038static inline gcov_unsigned_t from_file(gcov_unsigned_t value)
Stefan Reinauerd37ab452012-12-18 16:23:28 -080039{
40#if !IN_LIBGCOV
Lee Leahye20a3192017-03-09 16:21:34 -080041 if (gcov_var.endian) {
42 value = (value >> 16) | (value << 16);
43 value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff);
44 }
Stefan Reinauerd37ab452012-12-18 16:23:28 -080045#endif
Lee Leahye20a3192017-03-09 16:21:34 -080046 return value;
Stefan Reinauerd37ab452012-12-18 16:23:28 -080047}
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
59GCOV_LINKAGE int
60#if IN_LIBGCOV
Lee Leahy38768c32017-03-09 14:07:18 -080061gcov_open(const char *name)
Stefan Reinauerd37ab452012-12-18 16:23:28 -080062#else
Lee Leahy38768c32017-03-09 14:07:18 -080063gcov_open(const char *name, int mode)
Stefan Reinauerd37ab452012-12-18 16:23:28 -080064#endif
65{
66#if IN_LIBGCOV
Lee Leahye20a3192017-03-09 16:21:34 -080067 const int mode = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -080068#endif
69#if GCOV_LOCKED
Lee Leahye20a3192017-03-09 16:21:34 -080070 struct flock s_flock;
71 int fd;
Stefan Reinauerd37ab452012-12-18 16:23:28 -080072
Lee Leahye20a3192017-03-09 16:21:34 -080073 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 Reinauerd37ab452012-12-18 16:23:28 -080077#endif
78
Lee Leahye20a3192017-03-09 16:21:34 -080079 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 Reinauerd37ab452012-12-18 16:23:28 -080084#if !IN_LIBGCOV
Lee Leahye20a3192017-03-09 16:21:34 -080085 gcov_var.endian = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -080086#endif
87#if GCOV_LOCKED
Lee Leahye20a3192017-03-09 16:21:34 -080088 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 Reinauerd37ab452012-12-18 16:23:28 -080096 }
Lee Leahye20a3192017-03-09 16:21:34 -080097 if (fd < 0)
98 return 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -080099
Lee Leahye20a3192017-03-09 16:21:34 -0800100 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 Reinauerd37ab452012-12-18 16:23:28 -0800139#endif
140
Lee Leahye20a3192017-03-09 16:21:34 -0800141 setbuf(gcov_var.file, (char *)0);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800142
Lee Leahye20a3192017-03-09 16:21:34 -0800143 return 1;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800144}
145
146/* Close the current gcov file. Flushes data to disk. Returns nonzero
147 on failure or error flag set. */
148
149GCOV_LINKAGE int
Lee Leahy38768c32017-03-09 14:07:18 -0800150gcov_close(void)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800151{
Lee Leahye20a3192017-03-09 16:21:34 -0800152 if (gcov_var.file) {
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800153#if !IN_GCOV
Lee Leahye20a3192017-03-09 16:21:34 -0800154 if (gcov_var.offset && gcov_var.mode < 0)
155 gcov_write_block(gcov_var.offset);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800156#endif
Lee Leahye20a3192017-03-09 16:21:34 -0800157 fclose(gcov_var.file);
158 gcov_var.file = 0;
159 gcov_var.length = 0;
160 }
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800161#if !IN_LIBGCOV
Lee Leahye20a3192017-03-09 16:21:34 -0800162 free(gcov_var.buffer);
163 gcov_var.alloc = 0;
164 gcov_var.buffer = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800165#endif
Lee Leahye20a3192017-03-09 16:21:34 -0800166 gcov_var.mode = 0;
167 return gcov_var.error;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800168}
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
175GCOV_LINKAGE int
Lee Leahy38768c32017-03-09 14:07:18 -0800176gcov_magic(gcov_unsigned_t magic, gcov_unsigned_t expected)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800177{
Lee Leahye20a3192017-03-09 16:21:34 -0800178 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 Reinauerd37ab452012-12-18 16:23:28 -0800187}
188#endif
189
190#if !IN_LIBGCOV
191static void
Lee Leahy38768c32017-03-09 14:07:18 -0800192gcov_allocate(unsigned int length)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800193{
Lee Leahye20a3192017-03-09 16:21:34 -0800194 size_t new_size = gcov_var.alloc;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800195
Lee Leahye20a3192017-03-09 16:21:34 -0800196 if (!new_size)
197 new_size = GCOV_BLOCK_SIZE;
198 new_size += length;
199 new_size *= 2;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800200
Lee Leahye20a3192017-03-09 16:21:34 -0800201 gcov_var.alloc = new_size;
202 gcov_var.buffer = XRESIZEVAR(gcov_unsigned_t, gcov_var.buffer,
203 new_size << 2);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800204}
205#endif
206
207#if !IN_GCOV
208/* Write out the current block, if needs be. */
209
210static void
Lee Leahy38768c32017-03-09 14:07:18 -0800211gcov_write_block(unsigned int size)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800212{
Lee Leahye20a3192017-03-09 16:21:34 -0800213 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 Reinauerd37ab452012-12-18 16:23:28 -0800217}
218
219/* Allocate space to write BYTES bytes to the gcov file. Return a
220 pointer to those bytes, or NULL on failure. */
221
222static gcov_unsigned_t *
Lee Leahy38768c32017-03-09 14:07:18 -0800223gcov_write_words(unsigned int words)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800224{
Lee Leahye20a3192017-03-09 16:21:34 -0800225 gcov_unsigned_t *result;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800226
Lee Leahye20a3192017-03-09 16:21:34 -0800227 gcc_assert(gcov_var.mode < 0);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800228#if IN_LIBGCOV
Lee Leahye20a3192017-03-09 16:21:34 -0800229 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 Reinauerd37ab452012-12-18 16:23:28 -0800236 }
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800237#else
Lee Leahye20a3192017-03-09 16:21:34 -0800238 if (gcov_var.offset + words > gcov_var.alloc)
239 gcov_allocate(gcov_var.offset + words);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800240#endif
Lee Leahye20a3192017-03-09 16:21:34 -0800241 result = &gcov_var.buffer[gcov_var.offset];
242 gcov_var.offset += words;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800243
Lee Leahye20a3192017-03-09 16:21:34 -0800244 return result;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800245}
246
247/* Write unsigned VALUE to coverage file. Sets error flag
248 appropriately. */
249
250GCOV_LINKAGE void
Lee Leahy38768c32017-03-09 14:07:18 -0800251gcov_write_unsigned(gcov_unsigned_t value)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800252{
Lee Leahye20a3192017-03-09 16:21:34 -0800253 gcov_unsigned_t *buffer = gcov_write_words(1);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800254
Lee Leahye20a3192017-03-09 16:21:34 -0800255 buffer[0] = value;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800256}
257
258/* Write counter VALUE to coverage file. Sets error flag
259 appropriately. */
260
261#if IN_LIBGCOV
262GCOV_LINKAGE void
Lee Leahy38768c32017-03-09 14:07:18 -0800263gcov_write_counter(gcov_type value)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800264{
Lee Leahye20a3192017-03-09 16:21:34 -0800265 gcov_unsigned_t *buffer = gcov_write_words(2);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800266
Lee Leahye20a3192017-03-09 16:21:34 -0800267 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 Reinauerd37ab452012-12-18 16:23:28 -0800272}
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
279GCOV_LINKAGE void
Lee Leahy38768c32017-03-09 14:07:18 -0800280gcov_write_string(const char *string)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800281{
Lee Leahye20a3192017-03-09 16:21:34 -0800282 unsigned int length = 0;
283 unsigned int alloc = 0;
284 gcov_unsigned_t *buffer;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800285
Lee Leahye20a3192017-03-09 16:21:34 -0800286 if (string) {
287 length = strlen(string);
288 alloc = (length + 4) >> 2;
289 }
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800290
Lee Leahye20a3192017-03-09 16:21:34 -0800291 buffer = gcov_write_words(1 + alloc);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800292
Lee Leahye20a3192017-03-09 16:21:34 -0800293 buffer[0] = alloc;
294 buffer[alloc] = 0;
295 memcpy(&buffer[1], string, length);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800296}
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
303GCOV_LINKAGE gcov_position_t
Lee Leahy38768c32017-03-09 14:07:18 -0800304gcov_write_tag(gcov_unsigned_t tag)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800305{
Lee Leahye20a3192017-03-09 16:21:34 -0800306 gcov_position_t result = gcov_var.start + gcov_var.offset;
307 gcov_unsigned_t *buffer = gcov_write_words(2);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800308
Lee Leahye20a3192017-03-09 16:21:34 -0800309 buffer[0] = tag;
310 buffer[1] = 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800311
Lee Leahye20a3192017-03-09 16:21:34 -0800312 return result;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800313}
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
320GCOV_LINKAGE void
Lee Leahy38768c32017-03-09 14:07:18 -0800321gcov_write_length(gcov_position_t position)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800322{
Lee Leahye20a3192017-03-09 16:21:34 -0800323 unsigned int offset;
324 gcov_unsigned_t length;
325 gcov_unsigned_t *buffer;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800326
Lee Leahye20a3192017-03-09 16:21:34 -0800327 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 Reinauerd37ab452012-12-18 16:23:28 -0800336}
337
338#else /* IN_LIBGCOV */
339
340/* Write a tag TAG and length LENGTH. */
341
342GCOV_LINKAGE void
Lee Leahy38768c32017-03-09 14:07:18 -0800343gcov_write_tag_length(gcov_unsigned_t tag, gcov_unsigned_t length)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800344{
Lee Leahye20a3192017-03-09 16:21:34 -0800345 gcov_unsigned_t *buffer = gcov_write_words(2);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800346
Lee Leahye20a3192017-03-09 16:21:34 -0800347 buffer[0] = tag;
348 buffer[1] = length;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800349}
350
351/* Write a summary structure to the gcov file. Return nonzero on
352 overflow. */
353
354GCOV_LINKAGE void
Lee Leahy38768c32017-03-09 14:07:18 -0800355gcov_write_summary(gcov_unsigned_t tag, const struct gcov_summary *summary)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800356{
Lee Leahye20a3192017-03-09 16:21:34 -0800357 unsigned int ix;
358 const struct gcov_ctr_summary *csum;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800359
Lee Leahye20a3192017-03-09 16:21:34 -0800360 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 Reinauerd37ab452012-12-18 16:23:28 -0800369}
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
377static const gcov_unsigned_t *
Lee Leahy38768c32017-03-09 14:07:18 -0800378gcov_read_words(unsigned int words)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800379{
Lee Leahye20a3192017-03-09 16:21:34 -0800380 const gcov_unsigned_t *result;
381 unsigned int excess = gcov_var.length - gcov_var.offset;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800382
Lee Leahye20a3192017-03-09 16:21:34 -0800383 gcc_assert(gcov_var.mode > 0);
384 if (excess < words) {
385 gcov_var.start += gcov_var.offset;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800386#if IN_LIBGCOV
Lee Leahye20a3192017-03-09 16:21:34 -0800387 if (excess) {
388 gcc_assert(excess == 1);
389 memcpy(gcov_var.buffer, gcov_var.buffer
390 + gcov_var.offset, 4);
391 }
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800392#else
Lee Leahye20a3192017-03-09 16:21:34 -0800393 memmove(gcov_var.buffer, gcov_var.buffer
394 + gcov_var.offset, excess * 4);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800395#endif
Lee Leahye20a3192017-03-09 16:21:34 -0800396 gcov_var.offset = 0;
397 gcov_var.length = excess;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800398#if IN_LIBGCOV
Lee Leahye20a3192017-03-09 16:21:34 -0800399 gcc_assert(!gcov_var.length || gcov_var.length == 1);
400 excess = GCOV_BLOCK_SIZE;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800401#else
Lee Leahye20a3192017-03-09 16:21:34 -0800402 if (gcov_var.length + words > gcov_var.alloc)
403 gcov_allocate(gcov_var.length + words);
404 excess = gcov_var.alloc - gcov_var.length;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800405#endif
Lee Leahye20a3192017-03-09 16:21:34 -0800406 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 Reinauerd37ab452012-12-18 16:23:28 -0800414 }
Lee Leahye20a3192017-03-09 16:21:34 -0800415 result = &gcov_var.buffer[gcov_var.offset];
416 gcov_var.offset += words;
417 return result;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800418}
419
420/* Read unsigned value from a coverage file. Sets error flag on file
421 error, overflow flag on overflow */
422
423GCOV_LINKAGE gcov_unsigned_t
Lee Leahy38768c32017-03-09 14:07:18 -0800424gcov_read_unsigned(void)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800425{
Lee Leahye20a3192017-03-09 16:21:34 -0800426 gcov_unsigned_t value;
427 const gcov_unsigned_t *buffer = gcov_read_words(1);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800428
Lee Leahye20a3192017-03-09 16:21:34 -0800429 if (!buffer)
430 return 0;
431 value = from_file(buffer[0]);
432 return value;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800433}
434
435/* Read counter value from a coverage file. Sets error flag on file
436 error, overflow flag on overflow */
437
438GCOV_LINKAGE gcov_type
Lee Leahy38768c32017-03-09 14:07:18 -0800439gcov_read_counter(void)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800440{
Lee Leahye20a3192017-03-09 16:21:34 -0800441 gcov_type value;
442 const gcov_unsigned_t *buffer = gcov_read_words(2);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800443
Lee Leahye20a3192017-03-09 16:21:34 -0800444 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 Reinauerd37ab452012-12-18 16:23:28 -0800451
Lee Leahye20a3192017-03-09 16:21:34 -0800452 return value;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800453}
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
460GCOV_LINKAGE const char *
Lee Leahy38768c32017-03-09 14:07:18 -0800461gcov_read_string(void)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800462{
Lee Leahye20a3192017-03-09 16:21:34 -0800463 unsigned int length = gcov_read_unsigned();
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800464
Lee Leahye20a3192017-03-09 16:21:34 -0800465 if (!length)
466 return 0;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800467
Lee Leahye20a3192017-03-09 16:21:34 -0800468 return (const char *) gcov_read_words(length);
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800469}
470#endif
471
472GCOV_LINKAGE void
Lee Leahy38768c32017-03-09 14:07:18 -0800473gcov_read_summary(struct gcov_summary *summary)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800474{
Lee Leahye20a3192017-03-09 16:21:34 -0800475 unsigned int ix;
476 struct gcov_ctr_summary *csum;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800477
Lee Leahye20a3192017-03-09 16:21:34 -0800478 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 Reinauerd37ab452012-12-18 16:23:28 -0800486}
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
492GCOV_LINKAGE void
Lee Leahy38768c32017-03-09 14:07:18 -0800493gcov_sync(gcov_position_t base, gcov_unsigned_t length)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800494{
Lee Leahye20a3192017-03-09 16:21:34 -0800495 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 Reinauerd37ab452012-12-18 16:23:28 -0800504}
505#endif
506
507#if IN_LIBGCOV
508/* Move to a given position in a gcov file. */
509
510GCOV_LINKAGE void
Lee Leahy38768c32017-03-09 14:07:18 -0800511gcov_seek(gcov_position_t base)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800512{
Lee Leahye20a3192017-03-09 16:21:34 -0800513 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 Reinauerd37ab452012-12-18 16:23:28 -0800518}
519#endif
520
521#if IN_GCOV > 0
522/* Return the modification time of the current gcov file. */
523
524GCOV_LINKAGE time_t
Lee Leahy38768c32017-03-09 14:07:18 -0800525gcov_time(void)
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800526{
Lee Leahye20a3192017-03-09 16:21:34 -0800527 struct stat status;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800528
Lee Leahye20a3192017-03-09 16:21:34 -0800529 if (fstat(fileno(gcov_var.file), &status))
530 return 0;
531 else
532 return status.st_mtime;
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800533}
534#endif /* IN_GCOV */