OSDN Git Service

* store-motion.c Do not include params.h
[pf3gnuchains/gcc-fork.git] / gcc / gcov-io.c
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 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 for modification. If MODE is >= 0 an existing file will be
50    opened, if possible, and if MODE is <= 0, a new file will be
51    created. Use MODE=0 to attempt to reopen an existing file and then
52    fall back on creating a new one.  Return zero on failure, >0 on
53    opening an existing file and <0 on creating a new one.  */
54
55 GCOV_LINKAGE int
56 #if IN_LIBGCOV
57 gcov_open (const char *name)
58 #else
59 gcov_open (const char *name, int mode)
60 #endif
61 {
62 #if IN_LIBGCOV
63   const int mode = 0;
64 #endif
65 #if GCOV_LOCKED
66   struct flock s_flock;
67   int fd;
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   gcc_assert (!gcov_var.file);
77   gcov_var.start = 0;
78   gcov_var.offset = gcov_var.length = 0;
79   gcov_var.overread = -1u;
80   gcov_var.error = 0;
81 #if !IN_LIBGCOV
82   gcov_var.endian = 0;
83 #endif
84 #if GCOV_LOCKED
85   if (mode > 0)
86     fd = open (name, O_RDWR);
87   else
88     fd = open (name, O_RDWR | O_CREAT, 0666);
89   if (fd < 0)
90     return 0;
91
92   while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR)
93     continue;
94
95   gcov_var.file = fdopen (fd, "r+b");
96   if (!gcov_var.file)
97     {
98       close (fd);
99       return 0;
100     }
101
102   if (mode > 0)
103     gcov_var.mode = 1;
104   else if (mode == 0)
105     {
106       struct stat st;
107
108       if (fstat (fd, &st) < 0)
109         {
110           fclose (gcov_var.file);
111           gcov_var.file = 0;
112           return 0;
113         }
114       if (st.st_size != 0)
115         gcov_var.mode = 1;
116       else
117         gcov_var.mode = mode * 2 + 1;
118     }
119   else
120     gcov_var.mode = mode * 2 + 1;
121 #else
122   if (mode >= 0)
123     gcov_var.file = fopen (name, "r+b");
124   if (gcov_var.file)
125     gcov_var.mode = 1;
126   else if (mode <= 0)
127     {
128       gcov_var.file = fopen (name, "w+b");
129       if (gcov_var.file)
130         gcov_var.mode = mode * 2 + 1;
131     }
132   if (!gcov_var.file)
133     return 0;
134 #endif
135
136   setbuf (gcov_var.file, (char *)0);
137   
138   return 1;
139 }
140
141 /* Close the current gcov file. Flushes data to disk. Returns nonzero
142    on failure or error flag set.  */
143
144 GCOV_LINKAGE int
145 gcov_close (void)
146 {
147   if (gcov_var.file)
148     {
149 #if !IN_GCOV
150       if (gcov_var.offset && gcov_var.mode < 0)
151         gcov_write_block (gcov_var.offset);
152 #endif
153       fclose (gcov_var.file);
154       gcov_var.file = 0;
155       gcov_var.length = 0;
156     }
157 #if !IN_LIBGCOV
158   free (gcov_var.buffer);
159   gcov_var.alloc = 0;
160   gcov_var.buffer = 0;
161 #endif
162   gcov_var.mode = 0;
163   return gcov_var.error;
164 }
165
166 #if !IN_LIBGCOV
167 /* Check if MAGIC is EXPECTED. Use it to determine endianness of the
168    file. Returns +1 for same endian, -1 for other endian and zero for
169    not EXPECTED.  */
170
171 GCOV_LINKAGE int
172 gcov_magic (gcov_unsigned_t magic, gcov_unsigned_t expected)
173 {
174   if (magic == expected)
175     return 1;
176   magic = (magic >> 16) | (magic << 16);
177   magic = ((magic & 0xff00ff) << 8) | ((magic >> 8) & 0xff00ff);
178   if (magic == expected)
179     {
180       gcov_var.endian = 1;
181       return -1;
182     }
183   return 0;
184 }
185 #endif
186
187 #if !IN_LIBGCOV
188 static void
189 gcov_allocate (unsigned length)
190 {
191   size_t new_size = gcov_var.alloc;
192   
193   if (!new_size)
194     new_size = GCOV_BLOCK_SIZE;
195   new_size += length;
196   new_size *= 2;
197   
198   gcov_var.alloc = new_size;
199   gcov_var.buffer = XRESIZEVAR (gcov_unsigned_t, gcov_var.buffer, new_size << 2);
200 }
201 #endif
202
203 #if !IN_GCOV
204 /* Write out the current block, if needs be.  */
205
206 static void
207 gcov_write_block (unsigned size)
208 {
209   if (fwrite (gcov_var.buffer, size << 2, 1, gcov_var.file) != 1)
210     gcov_var.error = 1;
211   gcov_var.start += size;
212   gcov_var.offset -= size;
213 }
214
215 /* Allocate space to write BYTES bytes to the gcov file. Return a
216    pointer to those bytes, or NULL on failure.  */
217
218 static gcov_unsigned_t *
219 gcov_write_words (unsigned words)
220 {
221   gcov_unsigned_t *result;
222
223   gcc_assert (gcov_var.mode < 0);
224 #if IN_LIBGCOV
225   if (gcov_var.offset >= GCOV_BLOCK_SIZE)
226     {
227       gcov_write_block (GCOV_BLOCK_SIZE);
228       if (gcov_var.offset)
229         {
230           gcc_assert (gcov_var.offset == 1);
231           memcpy (gcov_var.buffer, gcov_var.buffer + GCOV_BLOCK_SIZE, 4);
232         }
233     }
234 #else
235   if (gcov_var.offset + words > gcov_var.alloc)
236     gcov_allocate (gcov_var.offset + words);
237 #endif
238   result = &gcov_var.buffer[gcov_var.offset];
239   gcov_var.offset += words;
240   
241   return result;
242 }
243
244 /* Write unsigned VALUE to coverage file.  Sets error flag
245    appropriately.  */
246
247 GCOV_LINKAGE void
248 gcov_write_unsigned (gcov_unsigned_t value)
249 {
250   gcov_unsigned_t *buffer = gcov_write_words (1);
251
252   buffer[0] = value;
253 }
254
255 /* Write counter VALUE to coverage file.  Sets error flag
256    appropriately.  */
257
258 #if IN_LIBGCOV
259 GCOV_LINKAGE void
260 gcov_write_counter (gcov_type value)
261 {
262   gcov_unsigned_t *buffer = gcov_write_words (2);
263
264   buffer[0] = (gcov_unsigned_t) value;
265   if (sizeof (value) > sizeof (gcov_unsigned_t))
266     buffer[1] = (gcov_unsigned_t) (value >> 32);
267   else
268     buffer[1] = 0;
269 }
270 #endif /* IN_LIBGCOV */
271
272 #if !IN_LIBGCOV
273 /* Write STRING to coverage file.  Sets error flag on file
274    error, overflow flag on overflow */
275
276 GCOV_LINKAGE void
277 gcov_write_string (const char *string)
278 {
279   unsigned length = 0;
280   unsigned alloc = 0;
281   gcov_unsigned_t *buffer;
282
283   if (string)
284     {
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
301 GCOV_LINKAGE gcov_position_t
302 gcov_write_tag (gcov_unsigned_t tag)
303 {
304   gcov_position_t result = gcov_var.start + gcov_var.offset;
305   gcov_unsigned_t *buffer = gcov_write_words (2);
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
319 gcov_write_length (gcov_position_t position)
320 {
321   unsigned offset;
322   gcov_unsigned_t length;
323   gcov_unsigned_t *buffer;
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;
330   buffer = (gcov_unsigned_t *) &gcov_var.buffer[offset];
331   buffer[1] = length;
332   if (gcov_var.offset >= GCOV_BLOCK_SIZE)
333     gcov_write_block (gcov_var.offset);
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 {
343   gcov_unsigned_t *buffer = gcov_write_words (2);
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 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     {
362       gcov_write_unsigned (csum->num);
363       gcov_write_unsigned (csum->runs);
364       gcov_write_counter (csum->sum_all);
365       gcov_write_counter (csum->run_max);
366       gcov_write_counter (csum->sum_max);
367     }
368 }
369 #endif /* IN_LIBGCOV */
370
371 #endif /*!IN_GCOV */
372
373 /* Return a pointer to read BYTES bytes from the gcov file. Returns
374    NULL on failure (read past EOF).  */
375
376 static const gcov_unsigned_t *
377 gcov_read_words (unsigned words)
378 {
379   const gcov_unsigned_t *result;
380   unsigned excess = gcov_var.length - gcov_var.offset;
381   
382   gcc_assert (gcov_var.mode > 0);
383   if (excess < words)
384     {
385       gcov_var.start += gcov_var.offset;
386 #if IN_LIBGCOV
387       if (excess)
388         {
389           gcc_assert (excess == 1);
390           memcpy (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, 4);
391         }
392 #else
393       memmove (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, excess * 4);
394 #endif
395       gcov_var.offset = 0;
396       gcov_var.length = excess;
397 #if IN_LIBGCOV
398       gcc_assert (!gcov_var.length || gcov_var.length == 1);
399       excess = GCOV_BLOCK_SIZE;
400 #else
401       if (gcov_var.length + words > gcov_var.alloc)
402         gcov_allocate (gcov_var.length + words);
403       excess = gcov_var.alloc - gcov_var.length;
404 #endif
405       excess = fread (gcov_var.buffer + gcov_var.length,
406                       1, excess << 2, gcov_var.file) >> 2;
407       gcov_var.length += excess;
408       if (gcov_var.length < words)
409         {
410           gcov_var.overread += words - gcov_var.length;
411           gcov_var.length = 0;
412           return 0;
413         }
414     }
415   result = &gcov_var.buffer[gcov_var.offset];
416   gcov_var.offset += words;
417   return result;
418 }
419
420 /* Read unsigned value from a coverage file. Sets error flag on file
421    error, overflow flag on overflow */
422
423 GCOV_LINKAGE gcov_unsigned_t
424 gcov_read_unsigned (void)
425 {
426   gcov_unsigned_t value;
427   const gcov_unsigned_t *buffer = gcov_read_words (1);
428
429   if (!buffer)
430     return 0;
431   value = from_file (buffer[0]);
432   return value;
433 }
434
435 /* Read counter value from a coverage file. Sets error flag on file
436    error, overflow flag on overflow */
437
438 GCOV_LINKAGE gcov_type
439 gcov_read_counter (void)
440 {
441   gcov_type value;
442   const gcov_unsigned_t *buffer = gcov_read_words (2);
443
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;
451
452   return value;
453 }
454
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.  */
458
459 #if !IN_LIBGCOV
460 GCOV_LINKAGE const char *
461 gcov_read_string (void)
462 {
463   unsigned length = gcov_read_unsigned ();
464   
465   if (!length)
466     return 0;
467
468   return (const char *) gcov_read_words (length);
469 }
470 #endif
471
472 GCOV_LINKAGE void
473 gcov_read_summary (struct gcov_summary *summary)
474 {
475   unsigned ix;
476   struct gcov_ctr_summary *csum;
477   
478   summary->checksum = gcov_read_unsigned ();
479   for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
480     {
481       csum->num = gcov_read_unsigned ();
482       csum->runs = gcov_read_unsigned ();
483       csum->sum_all = gcov_read_counter ();
484       csum->run_max = gcov_read_counter ();
485       csum->sum_max = gcov_read_counter ();
486     }
487 }
488
489 #if !IN_LIBGCOV
490 /* Reset to a known position.  BASE should have been obtained from
491    gcov_position, LENGTH should be a record length.  */
492
493 GCOV_LINKAGE void
494 gcov_sync (gcov_position_t base, gcov_unsigned_t length)
495 {
496   gcc_assert (gcov_var.mode > 0);
497   base += length;
498   if (base - gcov_var.start <= gcov_var.length)
499     gcov_var.offset = base - gcov_var.start;
500   else
501     {
502       gcov_var.offset = gcov_var.length = 0;
503       fseek (gcov_var.file, base << 2, SEEK_SET);
504       gcov_var.start = ftell (gcov_var.file) >> 2;
505     }
506 }
507 #endif
508
509 #if IN_LIBGCOV
510 /* Move to a given position in a gcov file.  */
511
512 GCOV_LINKAGE void
513 gcov_seek (gcov_position_t base)
514 {
515   gcc_assert (gcov_var.mode < 0);
516   if (gcov_var.offset)
517     gcov_write_block (gcov_var.offset);
518   fseek (gcov_var.file, base << 2, SEEK_SET);
519   gcov_var.start = ftell (gcov_var.file) >> 2;
520 }
521 #endif
522
523 #if IN_GCOV > 0
524 /* Return the modification time of the current gcov file.  */
525
526 GCOV_LINKAGE time_t
527 gcov_time (void)
528 {
529   struct stat status;
530   
531   if (fstat (fileno (gcov_var.file), &status))
532     return 0;
533   else
534     return status.st_mtime;
535 }
536 #endif /* IN_GCOV */