OSDN Git Service

* xtensa-config.h: Undef all macros before defining them.
[pf3gnuchains/gcc-fork.git] / gcc / gcov-io.c
1 /* File format for coverage information
2    Copyright (C) 1996, 1997, 1998, 2000, 2002,
3    2003  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 2, 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 COPYING.  If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA.  */
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);
29 static gcov_unsigned_t *gcov_write_bytes (unsigned);
30 #endif
31 static const gcov_unsigned_t *gcov_read_bytes (unsigned);
32 #if !IN_LIBGCOV
33 static void gcov_allocate (unsigned);
34 #endif
35
36 static inline gcov_unsigned_t from_file (gcov_unsigned_t value)
37 {
38 #if !IN_LIBGCOV
39   if (gcov_var.endian)
40     {
41       value = (value >> 16) | (value << 16);
42       value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff);
43     }
44 #endif
45   return value;
46 }
47
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 for modification. If MODE is >= 0 an existing file will be
51    opened, if possible, and if MODE is <= 0, a new file will be
52    created. Use MODE=0 to attempt to reopen an existing file and then
53    fall back on creating a new one.  Return zero on failure, >0 on
54    opening an existing file and <0 on creating a new one.  */
55
56 GCOV_LINKAGE int
57 #if IN_LIBGCOV
58 gcov_open (const char *name)
59 #else
60 gcov_open (const char *name, int mode)
61 #endif
62 {
63 #if IN_LIBGCOV
64   const int mode = 0;
65 #endif
66 #if GCOV_LOCKED
67   struct flock s_flock;
68
69   s_flock.l_type = F_WRLCK;
70   s_flock.l_whence = SEEK_SET;
71   s_flock.l_start = 0;
72   s_flock.l_len = 0; /* Until EOF.  */
73   s_flock.l_pid = getpid ();
74 #endif
75   
76   if (gcov_var.file)
77     abort ();
78   gcov_var.start = 0;
79   gcov_var.offset = gcov_var.length = 0;
80   gcov_var.overread = -4u;
81   gcov_var.error = 0;
82 #if !IN_LIBGCOV
83   gcov_var.endian = 0;
84 #endif
85   if (mode >= 0)
86     gcov_var.file = fopen (name, "r+b");
87   if (gcov_var.file)
88     gcov_var.mode = 1;
89   else if (mode <= 0)
90     {
91       gcov_var.file = fopen (name, "w+b");
92       if (gcov_var.file)
93         gcov_var.mode = mode * 2 + 1;
94     }
95   if (!gcov_var.file)
96     return 0;
97   
98   setbuf (gcov_var.file, (char *)0);
99   
100 #if GCOV_LOCKED
101   while (fcntl (fileno (gcov_var.file), F_SETLKW, &s_flock)
102          && errno == EINTR)
103     continue;
104 #endif
105
106   return 1;
107 }
108
109 /* Close the current gcov file. Flushes data to disk. Returns nonzero
110    on failure or error flag set.  */
111
112 GCOV_LINKAGE int
113 gcov_close (void)
114 {
115   if (gcov_var.file)
116     {
117 #if !IN_GCOV
118       if (gcov_var.offset && gcov_var.mode < 0)
119         gcov_write_block (gcov_var.offset);
120 #endif
121       fclose (gcov_var.file);
122       gcov_var.file = 0;
123       gcov_var.length = 0;
124     }
125 #if !IN_LIBGCOV
126   free (gcov_var.buffer);
127   gcov_var.alloc = 0;
128   gcov_var.buffer = 0;
129 #endif
130   gcov_var.mode = 0;
131   return gcov_var.error;
132 }
133
134 #if !IN_LIBGCOV
135 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
136    file. Returns +1 for same endian, -1 for other endian and zero for
137    not EXPECTED.  */
138
139 GCOV_LINKAGE int
140 gcov_magic (gcov_unsigned_t magic, gcov_unsigned_t expected)
141 {
142   if (magic == expected)
143     return 1;
144   magic = (magic >> 16) | (magic << 16);
145   magic = ((magic & 0xff00ff) << 8) | ((magic >> 8) & 0xff00ff);
146   if (magic == expected)
147     {
148       gcov_var.endian = 1;
149       return -1;
150     }
151   return 0;
152 }
153 #endif
154
155 #if !IN_LIBGCOV
156 static void
157 gcov_allocate (unsigned length)
158 {
159   size_t new_size = gcov_var.alloc;
160   
161   if (!new_size)
162     new_size = GCOV_BLOCK_SIZE;
163   new_size += length;
164   new_size *= 2;
165   
166   gcov_var.alloc = new_size;
167   gcov_var.buffer = xrealloc (gcov_var.buffer, new_size);
168 }
169 #endif
170
171 #if !IN_GCOV
172 /* Write out the current block, if needs be.  */
173
174 static void
175 gcov_write_block (unsigned size)
176 {
177   if (fwrite (gcov_var.buffer, size, 1, gcov_var.file) != 1)
178     gcov_var.error = 1;
179   gcov_var.start += size;
180   gcov_var.offset -= size;
181 }
182
183 /* Allocate space to write BYTES bytes to the gcov file. Return a
184    pointer to those bytes, or NULL on failure.  */
185
186 static gcov_unsigned_t *
187 gcov_write_bytes (unsigned bytes)
188 {
189   gcov_unsigned_t *result;
190
191   GCOV_CHECK_WRITING ();
192   GCOV_CHECK (!(bytes & 3));
193 #if IN_LIBGCOV
194   if (gcov_var.offset >= GCOV_BLOCK_SIZE)
195     {
196       gcov_write_block (GCOV_BLOCK_SIZE);
197       if (gcov_var.offset)
198         {
199           GCOV_CHECK (gcov_var.offset == 4);
200           memcpy (gcov_var.buffer, gcov_var.buffer + GCOV_BLOCK_SIZE, 4);
201         }
202     }
203 #else
204   if (gcov_var.offset + bytes > gcov_var.alloc)
205     gcov_allocate (gcov_var.offset + bytes);
206 #endif
207   result = (gcov_unsigned_t *)&gcov_var.buffer[gcov_var.offset];
208   gcov_var.offset += bytes;
209   
210   return result;
211 }
212
213 /* Write unsigned VALUE to coverage file.  Sets error flag
214    appropriately.  */
215
216 GCOV_LINKAGE void
217 gcov_write_unsigned (gcov_unsigned_t value)
218 {
219   gcov_unsigned_t *buffer = gcov_write_bytes (4);
220
221   buffer[0] = value;
222 }
223
224 /* Write counter VALUE to coverage file.  Sets error flag
225    appropriately.  */
226
227 #if IN_LIBGCOV
228 GCOV_LINKAGE void
229 gcov_write_counter (gcov_type value)
230 {
231   gcov_unsigned_t *buffer = gcov_write_bytes (8);
232
233   buffer[0] = (gcov_unsigned_t) value;
234   if (sizeof (value) > sizeof (gcov_unsigned_t))
235     buffer[1] = (gcov_unsigned_t) (value >> 32);
236   else
237     buffer[1] = 0;
238   
239   if (value < 0)
240     gcov_var.error = -1;
241 }
242 #endif /* IN_LIBGCOV */
243
244 #if !IN_LIBGCOV
245 /* Write STRING to coverage file.  Sets error flag on file
246    error, overflow flag on overflow */
247
248 GCOV_LINKAGE void
249 gcov_write_string (const char *string)
250 {
251   unsigned length = 0;
252   unsigned alloc = 0;
253   gcov_unsigned_t *buffer;
254
255   if (string)
256     {
257       length = strlen (string);
258       alloc = (length + 4) >> 2;
259     }
260   
261   buffer = gcov_write_bytes (4 + alloc * 4);
262
263   buffer[0] = alloc;
264   buffer[alloc] = 0;
265   memcpy (&buffer[1], string, length);
266 }
267 #endif
268
269 #if !IN_LIBGCOV
270 /* Write a tag TAG and reserve space for the record length. Return a
271    value to be used for gcov_write_length.  */
272
273 GCOV_LINKAGE gcov_position_t
274 gcov_write_tag (gcov_unsigned_t tag)
275 {
276   gcov_position_t result = gcov_var.start + gcov_var.offset;
277   gcov_unsigned_t *buffer = gcov_write_bytes (8);
278
279   buffer[0] = tag;
280   buffer[1] = 0;
281   
282   return result;
283 }
284
285 /* Write a record length using POSITION, which was returned by
286    gcov_write_tag.  The current file position is the end of the
287    record, and is restored before returning.  Returns nonzero on
288    overflow.  */
289
290 GCOV_LINKAGE void
291 gcov_write_length (gcov_position_t position)
292 {
293   unsigned offset;
294   gcov_unsigned_t length;
295   gcov_unsigned_t *buffer;
296
297   GCOV_CHECK_WRITING ();
298   GCOV_CHECK (position + 8 <= gcov_var.start + gcov_var.offset);
299   GCOV_CHECK (position >= gcov_var.start);
300   offset = position - gcov_var.start;
301   length = gcov_var.offset - offset - 8;
302   buffer = (gcov_unsigned_t *) &gcov_var.buffer[offset];
303   buffer[1] = length;
304   if (gcov_var.offset >= GCOV_BLOCK_SIZE)
305     gcov_write_block (gcov_var.offset);
306 }
307
308 #else /* IN_LIBGCOV */
309
310 /* Write a tag TAG and length LENGTH.  */
311
312 GCOV_LINKAGE void
313 gcov_write_tag_length (gcov_unsigned_t tag, gcov_unsigned_t length)
314 {
315   gcov_unsigned_t *buffer = gcov_write_bytes (8);
316
317   buffer[0] = tag;
318   buffer[1] = length;
319 }
320
321 /* Write a summary structure to the gcov file.  Return nonzero on
322    overflow.  */
323
324 GCOV_LINKAGE void
325 gcov_write_summary (gcov_unsigned_t tag, const struct gcov_summary *summary)
326 {
327   unsigned ix;
328   const struct gcov_ctr_summary *csum;
329
330   gcov_write_tag_length (tag, GCOV_TAG_SUMMARY_LENGTH);
331   gcov_write_unsigned (summary->checksum);
332   for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
333     {
334       gcov_write_unsigned (csum->num);
335       gcov_write_unsigned (csum->runs);
336       gcov_write_counter (csum->sum_all);
337       gcov_write_counter (csum->run_max);
338       gcov_write_counter (csum->sum_max);
339     }
340 }
341 #endif /* IN_LIBGCOV */
342
343 #endif /*!IN_GCOV */
344
345 /* Return a pointer to read BYTES bytes from the gcov file. Returns
346    NULL on failure (read past EOF).  */
347
348 static const gcov_unsigned_t *
349 gcov_read_bytes (unsigned bytes)
350 {
351   const gcov_unsigned_t *result;
352   unsigned excess = gcov_var.length - gcov_var.offset;
353   
354   GCOV_CHECK_READING ();
355   GCOV_CHECK (!(bytes & 3));
356   if (excess < bytes)
357     {
358       gcov_var.start += gcov_var.offset;
359 #if IN_LIBGCOV
360       if (excess)
361         {
362           GCOV_CHECK (excess == 4);
363           memcpy (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, 4);
364         }
365 #else
366       memmove (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, excess);
367 #endif
368       gcov_var.offset = 0;
369       gcov_var.length = excess;
370 #if IN_LIBGCOV
371       GCOV_CHECK (!gcov_var.length || gcov_var.length == 4);
372       excess = GCOV_BLOCK_SIZE;
373 #else
374       if (gcov_var.length + bytes > gcov_var.alloc)
375         gcov_allocate (gcov_var.length + bytes);
376       excess = gcov_var.alloc - gcov_var.length;
377 #endif
378       excess = fread (gcov_var.buffer + gcov_var.length,
379                       1, excess, gcov_var.file);
380       gcov_var.length += excess;
381       if (gcov_var.length < bytes)
382         {
383           gcov_var.overread += bytes - gcov_var.length;
384           gcov_var.length = 0;
385           return 0;
386         }
387     }
388   result = (gcov_unsigned_t *)&gcov_var.buffer[gcov_var.offset];
389   gcov_var.offset += bytes;
390   return result;
391 }
392
393 /* Read unsigned value from a coverage file. Sets error flag on file
394    error, overflow flag on overflow */
395
396 GCOV_LINKAGE gcov_unsigned_t
397 gcov_read_unsigned (void)
398 {
399   gcov_unsigned_t value;
400   const gcov_unsigned_t *buffer = gcov_read_bytes (4);
401
402   if (!buffer)
403     return 0;
404   value = from_file (buffer[0]);
405   return value;
406 }
407
408 /* Read counter value from a coverage file. Sets error flag on file
409    error, overflow flag on overflow */
410
411 GCOV_LINKAGE gcov_type
412 gcov_read_counter (void)
413 {
414   gcov_type value;
415   const gcov_unsigned_t *buffer = gcov_read_bytes (8);
416
417   if (!buffer)
418     return 0;
419   value = from_file (buffer[0]);
420   if (sizeof (value) > sizeof (gcov_unsigned_t))
421     value |= ((gcov_type) from_file (buffer[1])) << 32;
422   else if (buffer[1])
423     gcov_var.error = -1;
424   
425   if (value < 0)
426     gcov_var.error = -1;
427   return value;
428 }
429
430 /* Read string from coverage file. Returns a pointer to a static
431    buffer, or NULL on empty string. You must copy the string before
432    calling another gcov function.  */
433
434 #if !IN_LIBGCOV
435 GCOV_LINKAGE const char *
436 gcov_read_string (void)
437 {
438   unsigned length = gcov_read_unsigned ();
439   
440   if (!length)
441     return 0;
442
443   return (const char *) gcov_read_bytes (length);
444 }
445 #endif
446
447 GCOV_LINKAGE void
448 gcov_read_summary (struct gcov_summary *summary)
449 {
450   unsigned ix;
451   struct gcov_ctr_summary *csum;
452   
453   summary->checksum = gcov_read_unsigned ();
454   for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
455     {
456       csum->num = gcov_read_unsigned ();
457       csum->runs = gcov_read_unsigned ();
458       csum->sum_all = gcov_read_counter ();
459       csum->run_max = gcov_read_counter ();
460       csum->sum_max = gcov_read_counter ();
461     }
462 }
463
464 #if !IN_LIBGCOV
465 /* Reset to a known position.  BASE should have been obtained from
466    gcov_position, LENGTH should be a record length.  */
467
468 GCOV_LINKAGE void
469 gcov_sync (gcov_position_t base, gcov_unsigned_t length)
470 {
471   GCOV_CHECK_READING ();
472   base += length;
473   if (base - gcov_var.start <= gcov_var.length)
474     gcov_var.offset = base - gcov_var.start;
475   else
476     {
477       gcov_var.offset = gcov_var.length = 0;
478       fseek (gcov_var.file, base, SEEK_SET);
479       gcov_var.start = ftell (gcov_var.file);
480     }
481 }
482 #endif
483
484 #if IN_LIBGCOV
485 /* Move to the a set position in a gcov file.  BASE is zero to move to
486    the end, and nonzero to move to that position.  */
487
488 GCOV_LINKAGE void
489 gcov_seek (gcov_position_t base)
490 {
491   GCOV_CHECK_WRITING ();
492   if (gcov_var.offset)
493     gcov_write_block (gcov_var.offset);
494   fseek (gcov_var.file, base, base ? SEEK_SET : SEEK_END);
495   gcov_var.start = ftell (gcov_var.file);
496 }
497 #endif
498
499 #if IN_GCOV > 0
500 /* Return the modification time of the current gcov file.  */
501
502 GCOV_LINKAGE time_t
503 gcov_time (void)
504 {
505   struct stat status;
506   
507   if (fstat (fileno (gcov_var.file), &status))
508     return 0;
509   else
510     return status.st_mtime;
511 }
512 #endif /* IN_GCOV */