mb/google/nissa/var/rull: add ssd timing and modify ssd GPIO pins of rtd3
[coreboot2.git] / src / lib / gcov-io.c
blob1a41b79f73e110281951c29b41754f69056a2fb7
1 /* SPDX-License-Identifier: GPL-3.0-only WITH GCC-exception-3.1 */
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>.
9 This file is part of GCC.
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.
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.
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.
26 /* Routines declared in gcov-io.h. This file should be #included by
27 another source file, after having #included gcov-io.h. */
29 #if !IN_GCOV
30 static void gcov_write_block(unsigned int);
31 static gcov_unsigned_t *gcov_write_words(unsigned int);
32 #endif
33 static const gcov_unsigned_t *gcov_read_words(unsigned int);
34 #if !IN_LIBGCOV
35 static void gcov_allocate(unsigned int);
36 #endif
38 static inline gcov_unsigned_t from_file(gcov_unsigned_t value)
40 #if !IN_LIBGCOV
41 if (gcov_var.endian) {
42 value = (value >> 16) | (value << 16);
43 value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff);
45 #endif
46 return value;
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. */
59 GCOV_LINKAGE int
60 #if IN_LIBGCOV
61 gcov_open(const char *name)
62 #else
63 gcov_open(const char *name, int mode)
64 #endif
66 #if IN_LIBGCOV
67 const int mode = 0;
68 #endif
69 #if GCOV_LOCKED
70 struct flock s_flock;
71 int fd;
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();
77 #endif
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;
84 #if !IN_LIBGCOV
85 gcov_var.endian = 0;
86 #endif
87 #if GCOV_LOCKED
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);
97 if (fd < 0)
98 return 0;
100 while (fcntl(fd, F_SETLKW, &s_flock) && errno == EINTR)
101 continue;
103 gcov_var.file = fdopen(fd, (mode > 0) ? "rb" : "r+b");
105 if (!gcov_var.file) {
106 close(fd);
107 return 0;
110 if (mode > 0)
111 gcov_var.mode = 1;
112 else if (mode == 0) {
113 struct stat st;
115 if (fstat(fd, &st) < 0) {
116 fclose(gcov_var.file);
117 gcov_var.file = 0;
118 return 0;
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");
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;
137 if (!gcov_var.file)
138 return 0;
139 #endif
141 setbuf(gcov_var.file, (char *)0);
143 return 1;
146 /* Close the current gcov file. Flushes data to disk. Returns nonzero
147 on failure or error flag set. */
149 GCOV_LINKAGE int
150 gcov_close(void)
152 if (gcov_var.file) {
153 #if !IN_GCOV
154 if (gcov_var.offset && gcov_var.mode < 0)
155 gcov_write_block(gcov_var.offset);
156 #endif
157 fclose(gcov_var.file);
158 gcov_var.file = 0;
159 gcov_var.length = 0;
161 #if !IN_LIBGCOV
162 free(gcov_var.buffer);
163 gcov_var.alloc = 0;
164 gcov_var.buffer = 0;
165 #endif
166 gcov_var.mode = 0;
167 return gcov_var.error;
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. */
175 GCOV_LINKAGE int
176 gcov_magic(gcov_unsigned_t magic, gcov_unsigned_t expected)
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;
186 return 0;
188 #endif
190 #if !IN_LIBGCOV
191 static void
192 gcov_allocate(unsigned int length)
194 size_t new_size = gcov_var.alloc;
196 if (!new_size)
197 new_size = GCOV_BLOCK_SIZE;
198 new_size += length;
199 new_size *= 2;
201 gcov_var.alloc = new_size;
202 gcov_var.buffer = XRESIZEVAR(gcov_unsigned_t, gcov_var.buffer,
203 new_size << 2);
205 #endif
207 #if !IN_GCOV
208 /* Write out the current block, if needs be. */
210 static void
211 gcov_write_block(unsigned int size)
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;
219 /* Allocate space to write BYTES bytes to the gcov file. Return a
220 pointer to those bytes, or NULL on failure. */
222 static gcov_unsigned_t *
223 gcov_write_words(unsigned int words)
225 gcov_unsigned_t *result;
227 gcc_assert(gcov_var.mode < 0);
228 #if IN_LIBGCOV
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);
237 #else
238 if (gcov_var.offset + words > gcov_var.alloc)
239 gcov_allocate(gcov_var.offset + words);
240 #endif
241 result = &gcov_var.buffer[gcov_var.offset];
242 gcov_var.offset += words;
244 return result;
247 /* Write unsigned VALUE to coverage file. Sets error flag
248 appropriately. */
250 GCOV_LINKAGE void
251 gcov_write_unsigned(gcov_unsigned_t value)
253 gcov_unsigned_t *buffer = gcov_write_words(1);
255 buffer[0] = value;
258 /* Write counter VALUE to coverage file. Sets error flag
259 appropriately. */
261 #if IN_LIBGCOV
262 GCOV_LINKAGE void
263 gcov_write_counter(gcov_type value)
265 gcov_unsigned_t *buffer = gcov_write_words(2);
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;
273 #endif /* IN_LIBGCOV */
275 #if !IN_LIBGCOV
276 /* Write STRING to coverage file. Sets error flag on file
277 error, overflow flag on overflow */
279 GCOV_LINKAGE void
280 gcov_write_string(const char *string)
282 unsigned int length = 0;
283 unsigned int alloc = 0;
284 gcov_unsigned_t *buffer;
286 if (string) {
287 length = strlen(string);
288 alloc = (length + 4) >> 2;
291 buffer = gcov_write_words(1 + alloc);
293 buffer[0] = alloc;
294 buffer[alloc] = 0;
295 memcpy(&buffer[1], string, length);
297 #endif
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. */
303 GCOV_LINKAGE gcov_position_t
304 gcov_write_tag(gcov_unsigned_t tag)
306 gcov_position_t result = gcov_var.start + gcov_var.offset;
307 gcov_unsigned_t *buffer = gcov_write_words(2);
309 buffer[0] = tag;
310 buffer[1] = 0;
312 return result;
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. */
320 GCOV_LINKAGE void
321 gcov_write_length(gcov_position_t position)
323 unsigned int offset;
324 gcov_unsigned_t length;
325 gcov_unsigned_t *buffer;
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);
338 #else /* IN_LIBGCOV */
340 /* Write a tag TAG and length LENGTH. */
342 GCOV_LINKAGE void
343 gcov_write_tag_length(gcov_unsigned_t tag, gcov_unsigned_t length)
345 gcov_unsigned_t *buffer = gcov_write_words(2);
347 buffer[0] = tag;
348 buffer[1] = length;
351 /* Write a summary structure to the gcov file. Return nonzero on
352 overflow. */
354 GCOV_LINKAGE void
355 gcov_write_summary(gcov_unsigned_t tag, const struct gcov_summary *summary)
357 unsigned int ix;
358 const struct gcov_ctr_summary *csum;
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);
370 #endif /* IN_LIBGCOV */
372 #endif /*!IN_GCOV */
374 /* Return a pointer to read BYTES bytes from the gcov file. Returns
375 NULL on failure (read past EOF). */
377 static const gcov_unsigned_t *
378 gcov_read_words(unsigned int words)
380 const gcov_unsigned_t *result;
381 unsigned int excess = gcov_var.length - gcov_var.offset;
383 gcc_assert(gcov_var.mode > 0);
384 if (excess < words) {
385 gcov_var.start += gcov_var.offset;
386 #if IN_LIBGCOV
387 if (excess) {
388 gcc_assert(excess == 1);
389 memcpy(gcov_var.buffer, gcov_var.buffer
390 + gcov_var.offset, 4);
392 #else
393 memmove(gcov_var.buffer, gcov_var.buffer
394 + gcov_var.offset, excess * 4);
395 #endif
396 gcov_var.offset = 0;
397 gcov_var.length = excess;
398 #if IN_LIBGCOV
399 gcc_assert(!gcov_var.length || gcov_var.length == 1);
400 excess = GCOV_BLOCK_SIZE;
401 #else
402 if (gcov_var.length + words > gcov_var.alloc)
403 gcov_allocate(gcov_var.length + words);
404 excess = gcov_var.alloc - gcov_var.length;
405 #endif
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;
415 result = &gcov_var.buffer[gcov_var.offset];
416 gcov_var.offset += words;
417 return result;
420 /* Read unsigned value from a coverage file. Sets error flag on file
421 error, overflow flag on overflow */
423 GCOV_LINKAGE gcov_unsigned_t
424 gcov_read_unsigned(void)
426 gcov_unsigned_t value;
427 const gcov_unsigned_t *buffer = gcov_read_words(1);
429 if (!buffer)
430 return 0;
431 value = from_file(buffer[0]);
432 return value;
435 /* Read counter value from a coverage file. Sets error flag on file
436 error, overflow flag on overflow */
438 GCOV_LINKAGE gcov_type
439 gcov_read_counter(void)
441 gcov_type value;
442 const gcov_unsigned_t *buffer = gcov_read_words(2);
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;
452 return value;
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. */
459 #if !IN_LIBGCOV
460 GCOV_LINKAGE const char *
461 gcov_read_string(void)
463 unsigned int length = gcov_read_unsigned();
465 if (!length)
466 return 0;
468 return (const char *) gcov_read_words(length);
470 #endif
472 GCOV_LINKAGE void
473 gcov_read_summary(struct gcov_summary *summary)
475 unsigned int ix;
476 struct gcov_ctr_summary *csum;
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();
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. */
492 GCOV_LINKAGE void
493 gcov_sync(gcov_position_t base, gcov_unsigned_t length)
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;
505 #endif
507 #if IN_LIBGCOV
508 /* Move to a given position in a gcov file. */
510 GCOV_LINKAGE void
511 gcov_seek(gcov_position_t base)
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;
519 #endif
521 #if IN_GCOV > 0
522 /* Return the modification time of the current gcov file. */
524 GCOV_LINKAGE time_t
525 gcov_time(void)
527 struct stat status;
529 if (fstat(fileno(gcov_var.file), &status))
530 return 0;
531 else
532 return status.st_mtime;
534 #endif /* IN_GCOV */