OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [gcov-io.c] - Blame information for rev 831

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 280 jeremybenn
/* 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
You should have received a copy of the GNU General Public License
20
along with GCC; see the file COPYING3.  If not see
21
<http://www.gnu.org/licenses/>.  */
22
 
23
/* Routines declared in gcov-io.h.  This file should be #included by
24
   another source file, after having #included gcov-io.h.  */
25
 
26
#if !IN_GCOV
27
static void gcov_write_block (unsigned);
28
static gcov_unsigned_t *gcov_write_words (unsigned);
29
#endif
30
static const gcov_unsigned_t *gcov_read_words (unsigned);
31
#if !IN_LIBGCOV
32
static void gcov_allocate (unsigned);
33
#endif
34
 
35
static inline gcov_unsigned_t from_file (gcov_unsigned_t value)
36
{
37
#if !IN_LIBGCOV
38
  if (gcov_var.endian)
39
    {
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
 
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
    {
88
      /* Read-only mode - acquire a read-lock.  */
89
      s_flock.l_type = F_RDLCK;
90
      fd = open (name, O_RDONLY);
91
    }
92
  else
93
    {
94
      /* Write mode - acquire a write-lock.  */
95
      s_flock.l_type = F_WRLCK;
96
      fd = open (name, O_RDWR | O_CREAT, 0666);
97
    }
98
  if (fd < 0)
99
    return 0;
100
 
101
  while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR)
102
    continue;
103
 
104
  gcov_var.file = fdopen (fd, (mode > 0) ? "rb" : "r+b");
105
 
106
  if (!gcov_var.file)
107
    {
108
      close (fd);
109
      return 0;
110
    }
111
 
112
  if (mode > 0)
113
    gcov_var.mode = 1;
114
  else if (mode == 0)
115
    {
116
      struct stat st;
117
 
118
      if (fstat (fd, &st) < 0)
119
        {
120
          fclose (gcov_var.file);
121
          gcov_var.file = 0;
122
          return 0;
123
        }
124
      if (st.st_size != 0)
125
        gcov_var.mode = 1;
126
      else
127
        gcov_var.mode = mode * 2 + 1;
128
    }
129
  else
130
    gcov_var.mode = mode * 2 + 1;
131
#else
132
  if (mode >= 0)
133
    gcov_var.file = fopen (name, (mode > 0) ? "rb" : "r+b");
134
 
135
  if (gcov_var.file)
136
    gcov_var.mode = 1;
137
  else if (mode <= 0)
138
    {
139
      gcov_var.file = fopen (name, "w+b");
140
      if (gcov_var.file)
141
        gcov_var.mode = mode * 2 + 1;
142
    }
143
  if (!gcov_var.file)
144
    return 0;
145
#endif
146
 
147
  setbuf (gcov_var.file, (char *)0);
148
 
149
  return 1;
150
}
151
 
152
/* Close the current gcov file. Flushes data to disk. Returns nonzero
153
   on failure or error flag set.  */
154
 
155
GCOV_LINKAGE int
156
gcov_close (void)
157
{
158
  if (gcov_var.file)
159
    {
160
#if !IN_GCOV
161
      if (gcov_var.offset && gcov_var.mode < 0)
162
        gcov_write_block (gcov_var.offset);
163
#endif
164
      fclose (gcov_var.file);
165
      gcov_var.file = 0;
166
      gcov_var.length = 0;
167
    }
168
#if !IN_LIBGCOV
169
  free (gcov_var.buffer);
170
  gcov_var.alloc = 0;
171
  gcov_var.buffer = 0;
172
#endif
173
  gcov_var.mode = 0;
174
  return gcov_var.error;
175
}
176
 
177
#if !IN_LIBGCOV
178
/* Check if MAGIC is EXPECTED. Use it to determine endianness of the
179
   file. Returns +1 for same endian, -1 for other endian and zero for
180
   not EXPECTED.  */
181
 
182
GCOV_LINKAGE int
183
gcov_magic (gcov_unsigned_t magic, gcov_unsigned_t expected)
184
{
185
  if (magic == expected)
186
    return 1;
187
  magic = (magic >> 16) | (magic << 16);
188
  magic = ((magic & 0xff00ff) << 8) | ((magic >> 8) & 0xff00ff);
189
  if (magic == expected)
190
    {
191
      gcov_var.endian = 1;
192
      return -1;
193
    }
194
  return 0;
195
}
196
#endif
197
 
198
#if !IN_LIBGCOV
199
static void
200
gcov_allocate (unsigned length)
201
{
202
  size_t new_size = gcov_var.alloc;
203
 
204
  if (!new_size)
205
    new_size = GCOV_BLOCK_SIZE;
206
  new_size += length;
207
  new_size *= 2;
208
 
209
  gcov_var.alloc = new_size;
210
  gcov_var.buffer = XRESIZEVAR (gcov_unsigned_t, gcov_var.buffer, new_size << 2);
211
}
212
#endif
213
 
214
#if !IN_GCOV
215
/* Write out the current block, if needs be.  */
216
 
217
static void
218
gcov_write_block (unsigned size)
219
{
220
  if (fwrite (gcov_var.buffer, size << 2, 1, gcov_var.file) != 1)
221
    gcov_var.error = 1;
222
  gcov_var.start += size;
223
  gcov_var.offset -= size;
224
}
225
 
226
/* Allocate space to write BYTES bytes to the gcov file. Return a
227
   pointer to those bytes, or NULL on failure.  */
228
 
229
static gcov_unsigned_t *
230
gcov_write_words (unsigned words)
231
{
232
  gcov_unsigned_t *result;
233
 
234
  gcc_assert (gcov_var.mode < 0);
235
#if IN_LIBGCOV
236
  if (gcov_var.offset >= GCOV_BLOCK_SIZE)
237
    {
238
      gcov_write_block (GCOV_BLOCK_SIZE);
239
      if (gcov_var.offset)
240
        {
241
          gcc_assert (gcov_var.offset == 1);
242
          memcpy (gcov_var.buffer, gcov_var.buffer + GCOV_BLOCK_SIZE, 4);
243
        }
244
    }
245
#else
246
  if (gcov_var.offset + words > gcov_var.alloc)
247
    gcov_allocate (gcov_var.offset + words);
248
#endif
249
  result = &gcov_var.buffer[gcov_var.offset];
250
  gcov_var.offset += words;
251
 
252
  return result;
253
}
254
 
255
/* Write unsigned VALUE to coverage file.  Sets error flag
256
   appropriately.  */
257
 
258
GCOV_LINKAGE void
259
gcov_write_unsigned (gcov_unsigned_t value)
260
{
261
  gcov_unsigned_t *buffer = gcov_write_words (1);
262
 
263
  buffer[0] = value;
264
}
265
 
266
/* Write counter VALUE to coverage file.  Sets error flag
267
   appropriately.  */
268
 
269
#if IN_LIBGCOV
270
GCOV_LINKAGE void
271
gcov_write_counter (gcov_type value)
272
{
273
  gcov_unsigned_t *buffer = gcov_write_words (2);
274
 
275
  buffer[0] = (gcov_unsigned_t) value;
276
  if (sizeof (value) > sizeof (gcov_unsigned_t))
277
    buffer[1] = (gcov_unsigned_t) (value >> 32);
278
  else
279
    buffer[1] = 0;
280
}
281
#endif /* IN_LIBGCOV */
282
 
283
#if !IN_LIBGCOV
284
/* Write STRING to coverage file.  Sets error flag on file
285
   error, overflow flag on overflow */
286
 
287
GCOV_LINKAGE void
288
gcov_write_string (const char *string)
289
{
290
  unsigned length = 0;
291
  unsigned alloc = 0;
292
  gcov_unsigned_t *buffer;
293
 
294
  if (string)
295
    {
296
      length = strlen (string);
297
      alloc = (length + 4) >> 2;
298
    }
299
 
300
  buffer = gcov_write_words (1 + alloc);
301
 
302
  buffer[0] = alloc;
303
  buffer[alloc] = 0;
304
  memcpy (&buffer[1], string, length);
305
}
306
#endif
307
 
308
#if !IN_LIBGCOV
309
/* Write a tag TAG and reserve space for the record length. Return a
310
   value to be used for gcov_write_length.  */
311
 
312
GCOV_LINKAGE gcov_position_t
313
gcov_write_tag (gcov_unsigned_t tag)
314
{
315
  gcov_position_t result = gcov_var.start + gcov_var.offset;
316
  gcov_unsigned_t *buffer = gcov_write_words (2);
317
 
318
  buffer[0] = tag;
319
  buffer[1] = 0;
320
 
321
  return result;
322
}
323
 
324
/* Write a record length using POSITION, which was returned by
325
   gcov_write_tag.  The current file position is the end of the
326
   record, and is restored before returning.  Returns nonzero on
327
   overflow.  */
328
 
329
GCOV_LINKAGE void
330
gcov_write_length (gcov_position_t position)
331
{
332
  unsigned offset;
333
  gcov_unsigned_t length;
334
  gcov_unsigned_t *buffer;
335
 
336
  gcc_assert (gcov_var.mode < 0);
337
  gcc_assert (position + 2 <= gcov_var.start + gcov_var.offset);
338
  gcc_assert (position >= gcov_var.start);
339
  offset = position - gcov_var.start;
340
  length = gcov_var.offset - offset - 2;
341
  buffer = (gcov_unsigned_t *) &gcov_var.buffer[offset];
342
  buffer[1] = length;
343
  if (gcov_var.offset >= GCOV_BLOCK_SIZE)
344
    gcov_write_block (gcov_var.offset);
345
}
346
 
347
#else /* IN_LIBGCOV */
348
 
349
/* Write a tag TAG and length LENGTH.  */
350
 
351
GCOV_LINKAGE void
352
gcov_write_tag_length (gcov_unsigned_t tag, gcov_unsigned_t length)
353
{
354
  gcov_unsigned_t *buffer = gcov_write_words (2);
355
 
356
  buffer[0] = tag;
357
  buffer[1] = length;
358
}
359
 
360
/* Write a summary structure to the gcov file.  Return nonzero on
361
   overflow.  */
362
 
363
GCOV_LINKAGE void
364
gcov_write_summary (gcov_unsigned_t tag, const struct gcov_summary *summary)
365
{
366
  unsigned ix;
367
  const struct gcov_ctr_summary *csum;
368
 
369
  gcov_write_tag_length (tag, GCOV_TAG_SUMMARY_LENGTH);
370
  gcov_write_unsigned (summary->checksum);
371
  for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
372
    {
373
      gcov_write_unsigned (csum->num);
374
      gcov_write_unsigned (csum->runs);
375
      gcov_write_counter (csum->sum_all);
376
      gcov_write_counter (csum->run_max);
377
      gcov_write_counter (csum->sum_max);
378
    }
379
}
380
#endif /* IN_LIBGCOV */
381
 
382
#endif /*!IN_GCOV */
383
 
384
/* Return a pointer to read BYTES bytes from the gcov file. Returns
385
   NULL on failure (read past EOF).  */
386
 
387
static const gcov_unsigned_t *
388
gcov_read_words (unsigned words)
389
{
390
  const gcov_unsigned_t *result;
391
  unsigned excess = gcov_var.length - gcov_var.offset;
392
 
393
  gcc_assert (gcov_var.mode > 0);
394
  if (excess < words)
395
    {
396
      gcov_var.start += gcov_var.offset;
397
#if IN_LIBGCOV
398
      if (excess)
399
        {
400
          gcc_assert (excess == 1);
401
          memcpy (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, 4);
402
        }
403
#else
404
      memmove (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, excess * 4);
405
#endif
406
      gcov_var.offset = 0;
407
      gcov_var.length = excess;
408
#if IN_LIBGCOV
409
      gcc_assert (!gcov_var.length || gcov_var.length == 1);
410
      excess = GCOV_BLOCK_SIZE;
411
#else
412
      if (gcov_var.length + words > gcov_var.alloc)
413
        gcov_allocate (gcov_var.length + words);
414
      excess = gcov_var.alloc - gcov_var.length;
415
#endif
416
      excess = fread (gcov_var.buffer + gcov_var.length,
417
                      1, excess << 2, gcov_var.file) >> 2;
418
      gcov_var.length += excess;
419
      if (gcov_var.length < words)
420
        {
421
          gcov_var.overread += words - gcov_var.length;
422
          gcov_var.length = 0;
423
          return 0;
424
        }
425
    }
426
  result = &gcov_var.buffer[gcov_var.offset];
427
  gcov_var.offset += words;
428
  return result;
429
}
430
 
431
/* Read unsigned value from a coverage file. Sets error flag on file
432
   error, overflow flag on overflow */
433
 
434
GCOV_LINKAGE gcov_unsigned_t
435
gcov_read_unsigned (void)
436
{
437
  gcov_unsigned_t value;
438
  const gcov_unsigned_t *buffer = gcov_read_words (1);
439
 
440
  if (!buffer)
441
    return 0;
442
  value = from_file (buffer[0]);
443
  return value;
444
}
445
 
446
/* Read counter value from a coverage file. Sets error flag on file
447
   error, overflow flag on overflow */
448
 
449
GCOV_LINKAGE gcov_type
450
gcov_read_counter (void)
451
{
452
  gcov_type value;
453
  const gcov_unsigned_t *buffer = gcov_read_words (2);
454
 
455
  if (!buffer)
456
    return 0;
457
  value = from_file (buffer[0]);
458
  if (sizeof (value) > sizeof (gcov_unsigned_t))
459
    value |= ((gcov_type) from_file (buffer[1])) << 32;
460
  else if (buffer[1])
461
    gcov_var.error = -1;
462
 
463
  return value;
464
}
465
 
466
/* Read string from coverage file. Returns a pointer to a static
467
   buffer, or NULL on empty string. You must copy the string before
468
   calling another gcov function.  */
469
 
470
#if !IN_LIBGCOV
471
GCOV_LINKAGE const char *
472
gcov_read_string (void)
473
{
474
  unsigned length = gcov_read_unsigned ();
475
 
476
  if (!length)
477
    return 0;
478
 
479
  return (const char *) gcov_read_words (length);
480
}
481
#endif
482
 
483
GCOV_LINKAGE void
484
gcov_read_summary (struct gcov_summary *summary)
485
{
486
  unsigned ix;
487
  struct gcov_ctr_summary *csum;
488
 
489
  summary->checksum = gcov_read_unsigned ();
490
  for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
491
    {
492
      csum->num = gcov_read_unsigned ();
493
      csum->runs = gcov_read_unsigned ();
494
      csum->sum_all = gcov_read_counter ();
495
      csum->run_max = gcov_read_counter ();
496
      csum->sum_max = gcov_read_counter ();
497
    }
498
}
499
 
500
#if !IN_LIBGCOV
501
/* Reset to a known position.  BASE should have been obtained from
502
   gcov_position, LENGTH should be a record length.  */
503
 
504
GCOV_LINKAGE void
505
gcov_sync (gcov_position_t base, gcov_unsigned_t length)
506
{
507
  gcc_assert (gcov_var.mode > 0);
508
  base += length;
509
  if (base - gcov_var.start <= gcov_var.length)
510
    gcov_var.offset = base - gcov_var.start;
511
  else
512
    {
513
      gcov_var.offset = gcov_var.length = 0;
514
      fseek (gcov_var.file, base << 2, SEEK_SET);
515
      gcov_var.start = ftell (gcov_var.file) >> 2;
516
    }
517
}
518
#endif
519
 
520
#if IN_LIBGCOV
521
/* Move to a given position in a gcov file.  */
522
 
523
GCOV_LINKAGE void
524
gcov_seek (gcov_position_t base)
525
{
526
  gcc_assert (gcov_var.mode < 0);
527
  if (gcov_var.offset)
528
    gcov_write_block (gcov_var.offset);
529
  fseek (gcov_var.file, base << 2, SEEK_SET);
530
  gcov_var.start = ftell (gcov_var.file) >> 2;
531
}
532
#endif
533
 
534
#if IN_GCOV > 0
535
/* Return the modification time of the current gcov file.  */
536
 
537
GCOV_LINKAGE time_t
538
gcov_time (void)
539
{
540
  struct stat status;
541
 
542
  if (fstat (fileno (gcov_var.file), &status))
543
    return 0;
544
  else
545
    return status.st_mtime;
546
}
547
#endif /* IN_GCOV */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.