coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
gcov-io.c
Go to the documentation of this file.
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.
22 */
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
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
35 
37 {
38 #if !IN_LIBGCOV
39  if (gcov_var.endian) {
40  value = (value >> 16) | (value << 16);
41  value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff);
42  }
43 #endif
44  return value;
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
59 gcov_open(const char *name)
60 #else
61 gcov_open(const char *name, int mode)
62 #endif
63 {
64 #if IN_LIBGCOV
65  const int mode = 0;
66 #endif
67 #if GCOV_LOCKED
68  struct flock s_flock;
69  int fd;
70 
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
76 
78  gcov_var.start = 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);
94  }
95  if (fd < 0)
96  return 0;
97 
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) {
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;
137 #endif
138 
139  setbuf(gcov_var.file, (char *)0);
140 
141  return 1;
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
149 {
150  if (gcov_var.file) {
151 #if !IN_GCOV
152  if (gcov_var.offset && gcov_var.mode < 0)
154 #endif
156  gcov_var.file = 0;
157  gcov_var.length = 0;
158  }
159 #if !IN_LIBGCOV
161  gcov_var.alloc = 0;
162  gcov_var.buffer = 0;
163 #endif
164  gcov_var.mode = 0;
165  return gcov_var.error;
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
175 {
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;
185 }
186 #endif
187 
188 #if !IN_LIBGCOV
189 static void
190 gcov_allocate(unsigned int length)
191 {
192  size_t new_size = gcov_var.alloc;
193 
194  if (!new_size)
195  new_size = GCOV_BLOCK_SIZE;
196  new_size += length;
197  new_size *= 2;
198 
199  gcov_var.alloc = new_size;
201  new_size << 2);
202 }
203 #endif
204 
205 #if !IN_GCOV
206 /* Write out the current block, if needs be. */
207 
208 static void
209 gcov_write_block(unsigned int size)
210 {
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;
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 *
221 gcov_write_words(unsigned int words)
222 {
224 
225  gcc_assert(gcov_var.mode < 0);
226 #if IN_LIBGCOV
229  if (gcov_var.offset) {
230  gcc_assert(gcov_var.offset == 1);
232  + GCOV_BLOCK_SIZE, 4);
233  }
234  }
235 #else
236  if (gcov_var.offset + words > gcov_var.alloc)
237  gcov_allocate(gcov_var.offset + words);
238 #endif
240  gcov_var.offset += words;
241 
242  return result;
243 }
244 
245 /* Write unsigned VALUE to coverage file. Sets error flag
246  appropriately. */
247 
248 GCOV_LINKAGE void
250 {
252 
253  buffer[0] = value;
254 }
255 
256 /* Write counter VALUE to coverage file. Sets error flag
257  appropriately. */
258 
259 #if IN_LIBGCOV
260 GCOV_LINKAGE void
261 gcov_write_counter(gcov_type value)
262 {
264 
266  if (sizeof(value) > sizeof(gcov_unsigned_t))
267  buffer[1] = (gcov_unsigned_t) (value >> 32);
268  else
269  buffer[1] = 0;
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
278 gcov_write_string(const char *string)
279 {
280  unsigned int length = 0;
281  unsigned int alloc = 0;
283 
284  if (string) {
285  length = strlen(string);
286  alloc = (length + 4) >> 2;
287  }
288 
289  buffer = gcov_write_words(1 + alloc);
290 
291  buffer[0] = alloc;
292  buffer[alloc] = 0;
293  memcpy(&buffer[1], string, length);
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 
303 {
306 
307  buffer[0] = tag;
308  buffer[1] = 0;
309 
310  return result;
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
320 {
321  unsigned int offset;
324 
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;
331  buffer[1] = length;
334 }
335 
336 #else /* IN_LIBGCOV */
337 
338 /* Write a tag TAG and length LENGTH. */
339 
340 GCOV_LINKAGE void
341 gcov_write_tag_length(gcov_unsigned_t tag, gcov_unsigned_t length)
342 {
344 
345  buffer[0] = tag;
346  buffer[1] = length;
347 }
348 
349 /* Write a summary structure to the gcov file. Return nonzero on
350  overflow. */
351 
352 GCOV_LINKAGE void
353 gcov_write_summary(gcov_unsigned_t tag, const struct gcov_summary *summary)
354 {
355  unsigned int ix;
356  const struct gcov_ctr_summary *csum;
357 
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  }
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 *
376 gcov_read_words(unsigned int words)
377 {
378  const gcov_unsigned_t *result;
379  unsigned int excess = gcov_var.length - gcov_var.offset;
380 
381  gcc_assert(gcov_var.mode > 0);
382  if (excess < words) {
384 #if IN_LIBGCOV
385  if (excess) {
386  gcc_assert(excess == 1);
388  + gcov_var.offset, 4);
389  }
390 #else
392  + gcov_var.offset, excess * 4);
393 #endif
394  gcov_var.offset = 0;
395  gcov_var.length = excess;
396 #if IN_LIBGCOV
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;
411  }
412  }
414  gcov_var.offset += words;
415  return result;
416 }
417 
418 /* Read unsigned value from a coverage file. Sets error flag on file
419  error, overflow flag on overflow */
420 
423 {
426 
427  if (!buffer)
428  return 0;
429  value = from_file(buffer[0]);
430  return value;
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
438 {
439  gcov_type value;
441 
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;
449 
450  return value;
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 *
460 {
461  unsigned int length = gcov_read_unsigned();
462 
463  if (!length)
464  return 0;
465 
466  return (const char *) gcov_read_words(length);
467 }
468 #endif
469 
470 GCOV_LINKAGE void
472 {
473  unsigned int ix;
474  struct gcov_ctr_summary *csum;
475 
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  }
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
492 {
493  gcc_assert(gcov_var.mode > 0);
494  base += length;
495  if (base - gcov_var.start <= gcov_var.length)
497  else {
499  fseek(gcov_var.file, base << 2, SEEK_SET);
500  gcov_var.start = ftell(gcov_var.file) >> 2;
501  }
502 }
503 #endif
504 
505 #if IN_LIBGCOV
506 /* Move to a given position in a gcov file. */
507 
508 GCOV_LINKAGE void
509 gcov_seek(gcov_position_t base)
510 {
511  gcc_assert(gcov_var.mode < 0);
512  if (gcov_var.offset)
514  fseek(gcov_var.file, base << 2, SEEK_SET);
515  gcov_var.start = ftell(gcov_var.file) >> 2;
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
523 gcov_time(void)
524 {
525  struct stat status;
526 
527  if (fstat(fileno(gcov_var.file), &status))
528  return 0;
529  else
530  return status.st_mtime;
531 }
532 #endif /* IN_GCOV */
pte_t value
Definition: mmu.c:91
const char * name
Definition: mmu.c:92
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
void * memmove(void *dest, const void *src, size_t n)
Definition: memmove.c:10
#define EINTR
Definition: errno.h:9
static size_t offset
Definition: flashconsole.c:16
uint64_t length
Definition: fw_cfg_if.h:1
static void setbuf(FILE *stream, char *buf)
Definition: gcov-glue.c:115
#define SEEK_SET
Definition: gcov-glue.c:16
static long ftell(FILE *stream)
Definition: gcov-glue.c:82
static size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition: gcov-glue.c:101
static int fclose(FILE *stream)
Definition: gcov-glue.c:62
static size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Definition: gcov-glue.c:92
static int fseek(FILE *stream, long offset, int whence)
Definition: gcov-glue.c:70
static FILE * fopen(const char *path, const char *mode)
Definition: gcov-glue.c:29
static const gcov_unsigned_t * gcov_read_words(unsigned int)
Definition: gcov-io.c:376
GCOV_LINKAGE void gcov_write_length(gcov_position_t position)
Definition: gcov-io.c:319
GCOV_LINKAGE void gcov_sync(gcov_position_t base, gcov_unsigned_t length)
Definition: gcov-io.c:491
GCOV_LINKAGE void gcov_write_unsigned(gcov_unsigned_t value)
Definition: gcov-io.c:249
static gcov_unsigned_t * gcov_write_words(unsigned int)
Definition: gcov-io.c:221
GCOV_LINKAGE gcov_position_t gcov_write_tag(gcov_unsigned_t tag)
Definition: gcov-io.c:302
static void gcov_write_block(unsigned int)
Definition: gcov-io.c:209
GCOV_LINKAGE const char * gcov_read_string(void)
Definition: gcov-io.c:459
static void gcov_allocate(unsigned int)
Definition: gcov-io.c:190
static gcov_unsigned_t from_file(gcov_unsigned_t value)
Definition: gcov-io.c:36
GCOV_LINKAGE gcov_type gcov_read_counter(void)
Definition: gcov-io.c:437
GCOV_LINKAGE int gcov_open(const char *name, int mode)
Definition: gcov-io.c:61
GCOV_LINKAGE int gcov_magic(gcov_unsigned_t magic, gcov_unsigned_t expected)
Definition: gcov-io.c:174
GCOV_LINKAGE void gcov_write_string(const char *string)
Definition: gcov-io.c:278
GCOV_LINKAGE gcov_unsigned_t gcov_read_unsigned(void)
Definition: gcov-io.c:422
GCOV_LINKAGE int gcov_close(void)
Definition: gcov-io.c:148
GCOV_LINKAGE void gcov_read_summary(struct gcov_summary *summary)
Definition: gcov-io.c:471
unsigned int gcov_position_t
Definition: gcov-io.h:222
unsigned int gcov_unsigned_t
Definition: gcov-io.h:221
#define GCOV_LINKAGE
Definition: gcov-io.h:285
#define GCOV_TAG_SUMMARY_LENGTH
Definition: gcov-io.h:328
#define GCOV_COUNTERS_SUMMABLE
Definition: gcov-io.h:333
#define GCOV_BLOCK_SIZE
Definition: gcov-io.h:503
void free(void *ptr)
Definition: malloc.c:67
#define gcc_assert(x)
Definition: libgcov.c:32
result
Definition: mrc_cache.c:35
u8 buffer[C2P_BUFFER_MAXSIZE]
Definition: psp_smm.c:18
uintptr_t base
Definition: uart.c:17
size_t strlen(const char *src)
Definition: string.c:42
gcov_type sum_max
Definition: gcov-io.h:404
gcov_unsigned_t runs
Definition: gcov-io.h:401
gcov_unsigned_t num
Definition: gcov-io.h:400
gcov_type sum_all
Definition: gcov-io.h:402
gcov_type run_max
Definition: gcov-io.h:403
gcov_unsigned_t checksum
Definition: gcov-io.h:409
struct gcov_ctr_summary ctrs[GCOV_COUNTERS_SUMMABLE]
Definition: gcov-io.h:410
gcov_unsigned_t * buffer
Definition: gcov-io.h:527
int endian
Definition: gcov-io.h:522
int mode
Definition: gcov-io.h:513
size_t alloc
Definition: gcov-io.h:526
gcov_position_t start
Definition: gcov-io.h:508
unsigned int overread
Definition: gcov-io.h:511
unsigned int length
Definition: gcov-io.h:510
int error
Definition: gcov-io.h:512
unsigned int offset
Definition: gcov-io.h:509
FILE * file
Definition: gcov-io.h:507