soc/intel/tigerlake: Utilize vbt data size Kconfig option
[coreboot.git] / src / lib / gcov-io.c
blobe14252412ee2a93e6c15af38dbd4a2ba45f143e7
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>.
7 This file is part of GCC.
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.
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.
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.
24 /* Routines declared in gcov-io.h. This file should be #included by
25 another source file, after having #included gcov-io.h. */
27 #if !IN_GCOV
28 static void gcov_write_block(unsigned int);
29 static gcov_unsigned_t *gcov_write_words(unsigned int);
30 #endif
31 static const gcov_unsigned_t *gcov_read_words(unsigned int);
32 #if !IN_LIBGCOV
33 static void gcov_allocate(unsigned int);
34 #endif
36 static inline gcov_unsigned_t from_file(gcov_unsigned_t value)
38 #if !IN_LIBGCOV
39 if (gcov_var.endian) {
40 value = (value >> 16) | (value << 16);
41 value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff);
43 #endif
44 return value;
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. */
57 GCOV_LINKAGE int
58 #if IN_LIBGCOV
59 gcov_open(const char *name)
60 #else
61 gcov_open(const char *name, int mode)
62 #endif
64 #if IN_LIBGCOV
65 const int mode = 0;
66 #endif
67 #if GCOV_LOCKED
68 struct flock s_flock;
69 int fd;
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();
75 #endif
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;
82 #if !IN_LIBGCOV
83 gcov_var.endian = 0;
84 #endif
85 #if GCOV_LOCKED
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);
95 if (fd < 0)
96 return 0;
98 while (fcntl(fd, F_SETLKW, &s_flock) && errno == EINTR)
99 continue;
101 gcov_var.file = fdopen(fd, (mode > 0) ? "rb" : "r+b");
103 if (!gcov_var.file) {
104 close(fd);
105 return 0;
108 if (mode > 0)
109 gcov_var.mode = 1;
110 else if (mode == 0) {
111 struct stat st;
113 if (fstat(fd, &st) < 0) {
114 fclose(gcov_var.file);
115 gcov_var.file = 0;
116 return 0;
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");
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;
135 if (!gcov_var.file)
136 return 0;
137 #endif
139 setbuf(gcov_var.file, (char *)0);
141 return 1;
144 /* Close the current gcov file. Flushes data to disk. Returns nonzero
145 on failure or error flag set. */
147 GCOV_LINKAGE int
148 gcov_close(void)
150 if (gcov_var.file) {
151 #if !IN_GCOV
152 if (gcov_var.offset && gcov_var.mode < 0)
153 gcov_write_block(gcov_var.offset);
154 #endif
155 fclose(gcov_var.file);
156 gcov_var.file = 0;
157 gcov_var.length = 0;
159 #if !IN_LIBGCOV
160 free(gcov_var.buffer);
161 gcov_var.alloc = 0;
162 gcov_var.buffer = 0;
163 #endif
164 gcov_var.mode = 0;
165 return gcov_var.error;
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. */
173 GCOV_LINKAGE int
174 gcov_magic(gcov_unsigned_t magic, gcov_unsigned_t expected)
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;
184 return 0;
186 #endif
188 #if !IN_LIBGCOV
189 static void
190 gcov_allocate(unsigned int length)
192 size_t new_size = gcov_var.alloc;
194 if (!new_size)
195 new_size = GCOV_BLOCK_SIZE;
196 new_size += length;
197 new_size *= 2;
199 gcov_var.alloc = new_size;
200 gcov_var.buffer = XRESIZEVAR(gcov_unsigned_t, gcov_var.buffer,
201 new_size << 2);
203 #endif
205 #if !IN_GCOV
206 /* Write out the current block, if needs be. */
208 static void
209 gcov_write_block(unsigned int size)
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;
217 /* Allocate space to write BYTES bytes to the gcov file. Return a
218 pointer to those bytes, or NULL on failure. */
220 static gcov_unsigned_t *
221 gcov_write_words(unsigned int words)
223 gcov_unsigned_t *result;
225 gcc_assert(gcov_var.mode < 0);
226 #if IN_LIBGCOV
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);
235 #else
236 if (gcov_var.offset + words > gcov_var.alloc)
237 gcov_allocate(gcov_var.offset + words);
238 #endif
239 result = &gcov_var.buffer[gcov_var.offset];
240 gcov_var.offset += words;
242 return result;
245 /* Write unsigned VALUE to coverage file. Sets error flag
246 appropriately. */
248 GCOV_LINKAGE void
249 gcov_write_unsigned(gcov_unsigned_t value)
251 gcov_unsigned_t *buffer = gcov_write_words(1);
253 buffer[0] = value;
256 /* Write counter VALUE to coverage file. Sets error flag
257 appropriately. */
259 #if IN_LIBGCOV
260 GCOV_LINKAGE void
261 gcov_write_counter(gcov_type value)
263 gcov_unsigned_t *buffer = gcov_write_words(2);
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;
271 #endif /* IN_LIBGCOV */
273 #if !IN_LIBGCOV
274 /* Write STRING to coverage file. Sets error flag on file
275 error, overflow flag on overflow */
277 GCOV_LINKAGE void
278 gcov_write_string(const char *string)
280 unsigned int length = 0;
281 unsigned int alloc = 0;
282 gcov_unsigned_t *buffer;
284 if (string) {
285 length = strlen(string);
286 alloc = (length + 4) >> 2;
289 buffer = gcov_write_words(1 + alloc);
291 buffer[0] = alloc;
292 buffer[alloc] = 0;
293 memcpy(&buffer[1], string, length);
295 #endif
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. */
301 GCOV_LINKAGE gcov_position_t
302 gcov_write_tag(gcov_unsigned_t tag)
304 gcov_position_t result = gcov_var.start + gcov_var.offset;
305 gcov_unsigned_t *buffer = gcov_write_words(2);
307 buffer[0] = tag;
308 buffer[1] = 0;
310 return result;
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. */
318 GCOV_LINKAGE void
319 gcov_write_length(gcov_position_t position)
321 unsigned int offset;
322 gcov_unsigned_t length;
323 gcov_unsigned_t *buffer;
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);
336 #else /* IN_LIBGCOV */
338 /* Write a tag TAG and length LENGTH. */
340 GCOV_LINKAGE void
341 gcov_write_tag_length(gcov_unsigned_t tag, gcov_unsigned_t length)
343 gcov_unsigned_t *buffer = gcov_write_words(2);
345 buffer[0] = tag;
346 buffer[1] = length;
349 /* Write a summary structure to the gcov file. Return nonzero on
350 overflow. */
352 GCOV_LINKAGE void
353 gcov_write_summary(gcov_unsigned_t tag, const struct gcov_summary *summary)
355 unsigned int ix;
356 const struct gcov_ctr_summary *csum;
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);
368 #endif /* IN_LIBGCOV */
370 #endif /*!IN_GCOV */
372 /* Return a pointer to read BYTES bytes from the gcov file. Returns
373 NULL on failure (read past EOF). */
375 static const gcov_unsigned_t *
376 gcov_read_words(unsigned int words)
378 const gcov_unsigned_t *result;
379 unsigned int excess = gcov_var.length - gcov_var.offset;
381 gcc_assert(gcov_var.mode > 0);
382 if (excess < words) {
383 gcov_var.start += gcov_var.offset;
384 #if IN_LIBGCOV
385 if (excess) {
386 gcc_assert(excess == 1);
387 memcpy(gcov_var.buffer, gcov_var.buffer
388 + gcov_var.offset, 4);
390 #else
391 memmove(gcov_var.buffer, gcov_var.buffer
392 + gcov_var.offset, excess * 4);
393 #endif
394 gcov_var.offset = 0;
395 gcov_var.length = excess;
396 #if IN_LIBGCOV
397 gcc_assert(!gcov_var.length || gcov_var.length == 1);
398 excess = GCOV_BLOCK_SIZE;
399 #else
400 if (gcov_var.length + words > gcov_var.alloc)
401 gcov_allocate(gcov_var.length + words);
402 excess = gcov_var.alloc - gcov_var.length;
403 #endif
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;
413 result = &gcov_var.buffer[gcov_var.offset];
414 gcov_var.offset += words;
415 return result;
418 /* Read unsigned value from a coverage file. Sets error flag on file
419 error, overflow flag on overflow */
421 GCOV_LINKAGE gcov_unsigned_t
422 gcov_read_unsigned(void)
424 gcov_unsigned_t value;
425 const gcov_unsigned_t *buffer = gcov_read_words(1);
427 if (!buffer)
428 return 0;
429 value = from_file(buffer[0]);
430 return value;
433 /* Read counter value from a coverage file. Sets error flag on file
434 error, overflow flag on overflow */
436 GCOV_LINKAGE gcov_type
437 gcov_read_counter(void)
439 gcov_type value;
440 const gcov_unsigned_t *buffer = gcov_read_words(2);
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;
450 return value;
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. */
457 #if !IN_LIBGCOV
458 GCOV_LINKAGE const char *
459 gcov_read_string(void)
461 unsigned int length = gcov_read_unsigned();
463 if (!length)
464 return 0;
466 return (const char *) gcov_read_words(length);
468 #endif
470 GCOV_LINKAGE void
471 gcov_read_summary(struct gcov_summary *summary)
473 unsigned int ix;
474 struct gcov_ctr_summary *csum;
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();
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. */
490 GCOV_LINKAGE void
491 gcov_sync(gcov_position_t base, gcov_unsigned_t length)
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;
503 #endif
505 #if IN_LIBGCOV
506 /* Move to a given position in a gcov file. */
508 GCOV_LINKAGE void
509 gcov_seek(gcov_position_t base)
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;
517 #endif
519 #if IN_GCOV > 0
520 /* Return the modification time of the current gcov file. */
522 GCOV_LINKAGE time_t
523 gcov_time(void)
525 struct stat status;
527 if (fstat(fileno(gcov_var.file), &status))
528 return 0;
529 else
530 return status.st_mtime;
532 #endif /* IN_GCOV */