soc/intel/skylake: Correct address of I2C5 Device
[coreboot.git] / src / lib / gcov-io.c
blobaeb9d123a98dc23d61c25ffe8f8963ccc984c587
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);
29 static gcov_unsigned_t *gcov_write_words (unsigned);
30 #endif
31 static const gcov_unsigned_t *gcov_read_words (unsigned);
32 #if !IN_LIBGCOV
33 static void gcov_allocate (unsigned);
34 #endif
36 static inline gcov_unsigned_t from_file (gcov_unsigned_t value)
38 #if !IN_LIBGCOV
39 if (gcov_var.endian)
41 value = (value >> 16) | (value << 16);
42 value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff);
44 #endif
45 return value;
48 /* Open a gcov file. NAME is the name of the file to open and MODE
49 indicates whether a new file should be created, or an existing file
50 opened. If MODE is >= 0 an existing file will be opened, if
51 possible, and if MODE is <= 0, a new file will be created. Use
52 MODE=0 to attempt to reopen an existing file and then fall back on
53 creating a new one. If MODE < 0, the file will be opened in
54 read-only mode. Otherwise it will be opened for modification.
55 Return zero on failure, >0 on opening an existing file and <0 on
56 creating a new one. */
58 GCOV_LINKAGE int
59 #if IN_LIBGCOV
60 gcov_open (const char *name)
61 #else
62 gcov_open (const char *name, int mode)
63 #endif
65 #if IN_LIBGCOV
66 const int mode = 0;
67 #endif
68 #if GCOV_LOCKED
69 struct flock s_flock;
70 int fd;
72 s_flock.l_whence = SEEK_SET;
73 s_flock.l_start = 0;
74 s_flock.l_len = 0; /* Until EOF. */
75 s_flock.l_pid = getpid ();
76 #endif
78 gcc_assert (!gcov_var.file);
79 gcov_var.start = 0;
80 gcov_var.offset = gcov_var.length = 0;
81 gcov_var.overread = -1u;
82 gcov_var.error = 0;
83 #if !IN_LIBGCOV
84 gcov_var.endian = 0;
85 #endif
86 #if GCOV_LOCKED
87 if (mode > 0)
89 /* Read-only mode - acquire a read-lock. */
90 s_flock.l_type = F_RDLCK;
91 fd = open (name, O_RDONLY);
93 else
95 /* Write mode - acquire a write-lock. */
96 s_flock.l_type = F_WRLCK;
97 fd = open (name, O_RDWR | O_CREAT, 0666);
99 if (fd < 0)
100 return 0;
102 while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR)
103 continue;
105 gcov_var.file = fdopen (fd, (mode > 0) ? "rb" : "r+b");
107 if (!gcov_var.file)
109 close (fd);
110 return 0;
113 if (mode > 0)
114 gcov_var.mode = 1;
115 else if (mode == 0)
117 struct stat st;
119 if (fstat (fd, &st) < 0)
121 fclose (gcov_var.file);
122 gcov_var.file = 0;
123 return 0;
125 if (st.st_size != 0)
126 gcov_var.mode = 1;
127 else
128 gcov_var.mode = mode * 2 + 1;
130 else
131 gcov_var.mode = mode * 2 + 1;
132 #else
133 if (mode >= 0)
134 gcov_var.file = fopen (name, (mode > 0) ? "rb" : "r+b");
136 if (gcov_var.file)
137 gcov_var.mode = 1;
138 else if (mode <= 0)
140 gcov_var.file = fopen (name, "w+b");
141 if (gcov_var.file)
142 gcov_var.mode = mode * 2 + 1;
144 if (!gcov_var.file)
145 return 0;
146 #endif
148 setbuf (gcov_var.file, (char *)0);
150 return 1;
153 /* Close the current gcov file. Flushes data to disk. Returns nonzero
154 on failure or error flag set. */
156 GCOV_LINKAGE int
157 gcov_close (void)
159 if (gcov_var.file)
161 #if !IN_GCOV
162 if (gcov_var.offset && gcov_var.mode < 0)
163 gcov_write_block (gcov_var.offset);
164 #endif
165 fclose (gcov_var.file);
166 gcov_var.file = 0;
167 gcov_var.length = 0;
169 #if !IN_LIBGCOV
170 free (gcov_var.buffer);
171 gcov_var.alloc = 0;
172 gcov_var.buffer = 0;
173 #endif
174 gcov_var.mode = 0;
175 return gcov_var.error;
178 #if !IN_LIBGCOV
179 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
180 file. Returns +1 for same endian, -1 for other endian and zero for
181 not EXPECTED. */
183 GCOV_LINKAGE int
184 gcov_magic (gcov_unsigned_t magic, gcov_unsigned_t expected)
186 if (magic == expected)
187 return 1;
188 magic = (magic >> 16) | (magic << 16);
189 magic = ((magic & 0xff00ff) << 8) | ((magic >> 8) & 0xff00ff);
190 if (magic == expected)
192 gcov_var.endian = 1;
193 return -1;
195 return 0;
197 #endif
199 #if !IN_LIBGCOV
200 static void
201 gcov_allocate (unsigned length)
203 size_t new_size = gcov_var.alloc;
205 if (!new_size)
206 new_size = GCOV_BLOCK_SIZE;
207 new_size += length;
208 new_size *= 2;
210 gcov_var.alloc = new_size;
211 gcov_var.buffer = XRESIZEVAR (gcov_unsigned_t, gcov_var.buffer, new_size << 2);
213 #endif
215 #if !IN_GCOV
216 /* Write out the current block, if needs be. */
218 static void
219 gcov_write_block (unsigned size)
221 if (fwrite (gcov_var.buffer, size << 2, 1, gcov_var.file) != 1)
222 gcov_var.error = 1;
223 gcov_var.start += size;
224 gcov_var.offset -= size;
227 /* Allocate space to write BYTES bytes to the gcov file. Return a
228 pointer to those bytes, or NULL on failure. */
230 static gcov_unsigned_t *
231 gcov_write_words (unsigned words)
233 gcov_unsigned_t *result;
235 gcc_assert (gcov_var.mode < 0);
236 #if IN_LIBGCOV
237 if (gcov_var.offset >= GCOV_BLOCK_SIZE)
239 gcov_write_block (GCOV_BLOCK_SIZE);
240 if (gcov_var.offset)
242 gcc_assert (gcov_var.offset == 1);
243 memcpy (gcov_var.buffer, gcov_var.buffer + GCOV_BLOCK_SIZE, 4);
246 #else
247 if (gcov_var.offset + words > gcov_var.alloc)
248 gcov_allocate (gcov_var.offset + words);
249 #endif
250 result = &gcov_var.buffer[gcov_var.offset];
251 gcov_var.offset += words;
253 return result;
256 /* Write unsigned VALUE to coverage file. Sets error flag
257 appropriately. */
259 GCOV_LINKAGE void
260 gcov_write_unsigned (gcov_unsigned_t value)
262 gcov_unsigned_t *buffer = gcov_write_words (1);
264 buffer[0] = value;
267 /* Write counter VALUE to coverage file. Sets error flag
268 appropriately. */
270 #if IN_LIBGCOV
271 GCOV_LINKAGE void
272 gcov_write_counter (gcov_type value)
274 gcov_unsigned_t *buffer = gcov_write_words (2);
276 buffer[0] = (gcov_unsigned_t) value;
277 if (sizeof (value) > sizeof (gcov_unsigned_t))
278 buffer[1] = (gcov_unsigned_t) (value >> 32);
279 else
280 buffer[1] = 0;
282 #endif /* IN_LIBGCOV */
284 #if !IN_LIBGCOV
285 /* Write STRING to coverage file. Sets error flag on file
286 error, overflow flag on overflow */
288 GCOV_LINKAGE void
289 gcov_write_string (const char *string)
291 unsigned length = 0;
292 unsigned alloc = 0;
293 gcov_unsigned_t *buffer;
295 if (string)
297 length = strlen (string);
298 alloc = (length + 4) >> 2;
301 buffer = gcov_write_words (1 + alloc);
303 buffer[0] = alloc;
304 buffer[alloc] = 0;
305 memcpy (&buffer[1], string, length);
307 #endif
309 #if !IN_LIBGCOV
310 /* Write a tag TAG and reserve space for the record length. Return a
311 value to be used for gcov_write_length. */
313 GCOV_LINKAGE gcov_position_t
314 gcov_write_tag (gcov_unsigned_t tag)
316 gcov_position_t result = gcov_var.start + gcov_var.offset;
317 gcov_unsigned_t *buffer = gcov_write_words (2);
319 buffer[0] = tag;
320 buffer[1] = 0;
322 return result;
325 /* Write a record length using POSITION, which was returned by
326 gcov_write_tag. The current file position is the end of the
327 record, and is restored before returning. Returns nonzero on
328 overflow. */
330 GCOV_LINKAGE void
331 gcov_write_length (gcov_position_t position)
333 unsigned offset;
334 gcov_unsigned_t length;
335 gcov_unsigned_t *buffer;
337 gcc_assert (gcov_var.mode < 0);
338 gcc_assert (position + 2 <= gcov_var.start + gcov_var.offset);
339 gcc_assert (position >= gcov_var.start);
340 offset = position - gcov_var.start;
341 length = gcov_var.offset - offset - 2;
342 buffer = (gcov_unsigned_t *) &gcov_var.buffer[offset];
343 buffer[1] = length;
344 if (gcov_var.offset >= GCOV_BLOCK_SIZE)
345 gcov_write_block (gcov_var.offset);
348 #else /* IN_LIBGCOV */
350 /* Write a tag TAG and length LENGTH. */
352 GCOV_LINKAGE void
353 gcov_write_tag_length (gcov_unsigned_t tag, gcov_unsigned_t length)
355 gcov_unsigned_t *buffer = gcov_write_words (2);
357 buffer[0] = tag;
358 buffer[1] = length;
361 /* Write a summary structure to the gcov file. Return nonzero on
362 overflow. */
364 GCOV_LINKAGE void
365 gcov_write_summary (gcov_unsigned_t tag, const struct gcov_summary *summary)
367 unsigned ix;
368 const struct gcov_ctr_summary *csum;
370 gcov_write_tag_length (tag, GCOV_TAG_SUMMARY_LENGTH);
371 gcov_write_unsigned (summary->checksum);
372 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
374 gcov_write_unsigned (csum->num);
375 gcov_write_unsigned (csum->runs);
376 gcov_write_counter (csum->sum_all);
377 gcov_write_counter (csum->run_max);
378 gcov_write_counter (csum->sum_max);
381 #endif /* IN_LIBGCOV */
383 #endif /*!IN_GCOV */
385 /* Return a pointer to read BYTES bytes from the gcov file. Returns
386 NULL on failure (read past EOF). */
388 static const gcov_unsigned_t *
389 gcov_read_words (unsigned words)
391 const gcov_unsigned_t *result;
392 unsigned excess = gcov_var.length - gcov_var.offset;
394 gcc_assert (gcov_var.mode > 0);
395 if (excess < words)
397 gcov_var.start += gcov_var.offset;
398 #if IN_LIBGCOV
399 if (excess)
401 gcc_assert (excess == 1);
402 memcpy (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, 4);
404 #else
405 memmove (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, excess * 4);
406 #endif
407 gcov_var.offset = 0;
408 gcov_var.length = excess;
409 #if IN_LIBGCOV
410 gcc_assert (!gcov_var.length || gcov_var.length == 1);
411 excess = GCOV_BLOCK_SIZE;
412 #else
413 if (gcov_var.length + words > gcov_var.alloc)
414 gcov_allocate (gcov_var.length + words);
415 excess = gcov_var.alloc - gcov_var.length;
416 #endif
417 excess = fread (gcov_var.buffer + gcov_var.length,
418 1, excess << 2, gcov_var.file) >> 2;
419 gcov_var.length += excess;
420 if (gcov_var.length < words)
422 gcov_var.overread += words - gcov_var.length;
423 gcov_var.length = 0;
424 return 0;
427 result = &gcov_var.buffer[gcov_var.offset];
428 gcov_var.offset += words;
429 return result;
432 /* Read unsigned value from a coverage file. Sets error flag on file
433 error, overflow flag on overflow */
435 GCOV_LINKAGE gcov_unsigned_t
436 gcov_read_unsigned (void)
438 gcov_unsigned_t value;
439 const gcov_unsigned_t *buffer = gcov_read_words (1);
441 if (!buffer)
442 return 0;
443 value = from_file (buffer[0]);
444 return value;
447 /* Read counter value from a coverage file. Sets error flag on file
448 error, overflow flag on overflow */
450 GCOV_LINKAGE gcov_type
451 gcov_read_counter (void)
453 gcov_type value;
454 const gcov_unsigned_t *buffer = gcov_read_words (2);
456 if (!buffer)
457 return 0;
458 value = from_file (buffer[0]);
459 if (sizeof (value) > sizeof (gcov_unsigned_t))
460 value |= ((gcov_type) from_file (buffer[1])) << 32;
461 else if (buffer[1])
462 gcov_var.error = -1;
464 return value;
467 /* Read string from coverage file. Returns a pointer to a static
468 buffer, or NULL on empty string. You must copy the string before
469 calling another gcov function. */
471 #if !IN_LIBGCOV
472 GCOV_LINKAGE const char *
473 gcov_read_string (void)
475 unsigned length = gcov_read_unsigned ();
477 if (!length)
478 return 0;
480 return (const char *) gcov_read_words (length);
482 #endif
484 GCOV_LINKAGE void
485 gcov_read_summary (struct gcov_summary *summary)
487 unsigned ix;
488 struct gcov_ctr_summary *csum;
490 summary->checksum = gcov_read_unsigned ();
491 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
493 csum->num = gcov_read_unsigned ();
494 csum->runs = gcov_read_unsigned ();
495 csum->sum_all = gcov_read_counter ();
496 csum->run_max = gcov_read_counter ();
497 csum->sum_max = gcov_read_counter ();
501 #if !IN_LIBGCOV
502 /* Reset to a known position. BASE should have been obtained from
503 gcov_position, LENGTH should be a record length. */
505 GCOV_LINKAGE void
506 gcov_sync (gcov_position_t base, gcov_unsigned_t length)
508 gcc_assert (gcov_var.mode > 0);
509 base += length;
510 if (base - gcov_var.start <= gcov_var.length)
511 gcov_var.offset = base - gcov_var.start;
512 else
514 gcov_var.offset = gcov_var.length = 0;
515 fseek (gcov_var.file, base << 2, SEEK_SET);
516 gcov_var.start = ftell (gcov_var.file) >> 2;
519 #endif
521 #if IN_LIBGCOV
522 /* Move to a given position in a gcov file. */
524 GCOV_LINKAGE void
525 gcov_seek (gcov_position_t base)
527 gcc_assert (gcov_var.mode < 0);
528 if (gcov_var.offset)
529 gcov_write_block (gcov_var.offset);
530 fseek (gcov_var.file, base << 2, SEEK_SET);
531 gcov_var.start = ftell (gcov_var.file) >> 2;
533 #endif
535 #if IN_GCOV > 0
536 /* Return the modification time of the current gcov file. */
538 GCOV_LINKAGE time_t
539 gcov_time (void)
541 struct stat status;
543 if (fstat (fileno (gcov_var.file), &status))
544 return 0;
545 else
546 return status.st_mtime;
548 #endif /* IN_GCOV */