* Makefile.in (rtl-profile.o, value-prof.o): Add GCC_H dependency.
[official-gcc.git] / gcc / gcov-io.c
blob7370f510d37959af8df3780b17ec0d3004662cde
1 /* File format for coverage information
2 Copyright (C) 1996, 1997, 1998, 2000, 2002, 2003, 2004
3 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 2, 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 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. */
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 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. */
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
63 #if IN_LIBGCOV
64 const int mode = 0;
65 #endif
66 #if GCOV_LOCKED
67 struct flock s_flock;
68 int fd;
70 s_flock.l_type = F_WRLCK;
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
77 if (gcov_var.file)
78 abort ();
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)
88 fd = open (name, O_RDWR);
89 else
90 fd = open (name, O_RDWR | O_CREAT, 0666);
91 if (fd < 0)
92 return 0;
94 while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR)
95 continue;
97 gcov_var.file = fdopen (fd, "r+b");
98 if (!gcov_var.file)
100 close (fd);
101 return 0;
104 if (mode > 0)
105 gcov_var.mode = 1;
106 else if (mode == 0)
108 struct stat st;
110 if (fstat (fd, &st) < 0)
112 fclose (gcov_var.file);
113 gcov_var.file = 0;
114 return 0;
116 if (st.st_size != 0)
117 gcov_var.mode = 1;
118 else
119 gcov_var.mode = mode * 2 + 1;
121 else
122 gcov_var.mode = mode * 2 + 1;
123 #else
124 if (mode >= 0)
125 gcov_var.file = fopen (name, "r+b");
126 if (gcov_var.file)
127 gcov_var.mode = 1;
128 else if (mode <= 0)
130 gcov_var.file = fopen (name, "w+b");
131 if (gcov_var.file)
132 gcov_var.mode = mode * 2 + 1;
134 if (!gcov_var.file)
135 return 0;
136 #endif
138 setbuf (gcov_var.file, (char *)0);
140 return 1;
143 /* Close the current gcov file. Flushes data to disk. Returns nonzero
144 on failure or error flag set. */
146 GCOV_LINKAGE int
147 gcov_close (void)
149 if (gcov_var.file)
151 #if !IN_GCOV
152 if (gcov_var.offset && gcov_var.mode < 0)
153 gcov_write_block (gcov_var.offset);
154 #endif
155 fclose (gcov_var.file);
156 gcov_var.file = 0;
157 gcov_var.length = 0;
159 #if !IN_LIBGCOV
160 free (gcov_var.buffer);
161 gcov_var.alloc = 0;
162 gcov_var.buffer = 0;
163 #endif
164 gcov_var.mode = 0;
165 return gcov_var.error;
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. */
173 GCOV_LINKAGE int
174 gcov_magic (gcov_unsigned_t magic, gcov_unsigned_t expected)
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)
182 gcov_var.endian = 1;
183 return -1;
185 return 0;
187 #endif
189 #if !IN_LIBGCOV
190 static void
191 gcov_allocate (unsigned length)
193 size_t new_size = gcov_var.alloc;
195 if (!new_size)
196 new_size = GCOV_BLOCK_SIZE;
197 new_size += length;
198 new_size *= 2;
200 gcov_var.alloc = new_size;
201 gcov_var.buffer = xrealloc (gcov_var.buffer, new_size << 2);
203 #endif
205 #if !IN_GCOV
206 /* Write out the current block, if needs be. */
208 static void
209 gcov_write_block (unsigned size)
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;
217 /* Allocate space to write BYTES bytes to the gcov file. Return a
218 pointer to those bytes, or NULL on failure. */
220 static gcov_unsigned_t *
221 gcov_write_words (unsigned words)
223 gcov_unsigned_t *result;
225 GCOV_CHECK_WRITING ();
226 #if IN_LIBGCOV
227 if (gcov_var.offset >= GCOV_BLOCK_SIZE)
229 gcov_write_block (GCOV_BLOCK_SIZE);
230 if (gcov_var.offset)
232 GCOV_CHECK (gcov_var.offset == 1);
233 memcpy (gcov_var.buffer, gcov_var.buffer + GCOV_BLOCK_SIZE, 4);
236 #else
237 if (gcov_var.offset + words > gcov_var.alloc)
238 gcov_allocate (gcov_var.offset + words);
239 #endif
240 result = &gcov_var.buffer[gcov_var.offset];
241 gcov_var.offset += words;
243 return result;
246 /* Write unsigned VALUE to coverage file. Sets error flag
247 appropriately. */
249 GCOV_LINKAGE void
250 gcov_write_unsigned (gcov_unsigned_t value)
252 gcov_unsigned_t *buffer = gcov_write_words (1);
254 buffer[0] = value;
257 /* Write counter VALUE to coverage file. Sets error flag
258 appropriately. */
260 #if IN_LIBGCOV
261 GCOV_LINKAGE void
262 gcov_write_counter (gcov_type value)
264 gcov_unsigned_t *buffer = gcov_write_words (2);
266 buffer[0] = (gcov_unsigned_t) value;
267 if (sizeof (value) > sizeof (gcov_unsigned_t))
268 buffer[1] = (gcov_unsigned_t) (value >> 32);
269 else
270 buffer[1] = 0;
272 #endif /* IN_LIBGCOV */
274 #if !IN_LIBGCOV
275 /* Write STRING to coverage file. Sets error flag on file
276 error, overflow flag on overflow */
278 GCOV_LINKAGE void
279 gcov_write_string (const char *string)
281 unsigned length = 0;
282 unsigned alloc = 0;
283 gcov_unsigned_t *buffer;
285 if (string)
287 length = strlen (string);
288 alloc = (length + 4) >> 2;
291 buffer = gcov_write_words (1 + alloc);
293 buffer[0] = alloc;
294 buffer[alloc] = 0;
295 memcpy (&buffer[1], string, length);
297 #endif
299 #if !IN_LIBGCOV
300 /* Write a tag TAG and reserve space for the record length. Return a
301 value to be used for gcov_write_length. */
303 GCOV_LINKAGE gcov_position_t
304 gcov_write_tag (gcov_unsigned_t tag)
306 gcov_position_t result = gcov_var.start + gcov_var.offset;
307 gcov_unsigned_t *buffer = gcov_write_words (2);
309 buffer[0] = tag;
310 buffer[1] = 0;
312 return result;
315 /* Write a record length using POSITION, which was returned by
316 gcov_write_tag. The current file position is the end of the
317 record, and is restored before returning. Returns nonzero on
318 overflow. */
320 GCOV_LINKAGE void
321 gcov_write_length (gcov_position_t position)
323 unsigned offset;
324 gcov_unsigned_t length;
325 gcov_unsigned_t *buffer;
327 GCOV_CHECK_WRITING ();
328 GCOV_CHECK (position + 2 <= gcov_var.start + gcov_var.offset);
329 GCOV_CHECK (position >= gcov_var.start);
330 offset = position - gcov_var.start;
331 length = gcov_var.offset - offset - 2;
332 buffer = (gcov_unsigned_t *) &gcov_var.buffer[offset];
333 buffer[1] = length;
334 if (gcov_var.offset >= GCOV_BLOCK_SIZE)
335 gcov_write_block (gcov_var.offset);
338 #else /* IN_LIBGCOV */
340 /* Write a tag TAG and length LENGTH. */
342 GCOV_LINKAGE void
343 gcov_write_tag_length (gcov_unsigned_t tag, gcov_unsigned_t length)
345 gcov_unsigned_t *buffer = gcov_write_words (2);
347 buffer[0] = tag;
348 buffer[1] = length;
351 /* Write a summary structure to the gcov file. Return nonzero on
352 overflow. */
354 GCOV_LINKAGE void
355 gcov_write_summary (gcov_unsigned_t tag, const struct gcov_summary *summary)
357 unsigned ix;
358 const struct gcov_ctr_summary *csum;
360 gcov_write_tag_length (tag, GCOV_TAG_SUMMARY_LENGTH);
361 gcov_write_unsigned (summary->checksum);
362 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
364 gcov_write_unsigned (csum->num);
365 gcov_write_unsigned (csum->runs);
366 gcov_write_counter (csum->sum_all);
367 gcov_write_counter (csum->run_max);
368 gcov_write_counter (csum->sum_max);
371 #endif /* IN_LIBGCOV */
373 #endif /*!IN_GCOV */
375 /* Return a pointer to read BYTES bytes from the gcov file. Returns
376 NULL on failure (read past EOF). */
378 static const gcov_unsigned_t *
379 gcov_read_words (unsigned words)
381 const gcov_unsigned_t *result;
382 unsigned excess = gcov_var.length - gcov_var.offset;
384 GCOV_CHECK_READING ();
385 if (excess < words)
387 gcov_var.start += gcov_var.offset;
388 #if IN_LIBGCOV
389 if (excess)
391 GCOV_CHECK (excess == 1);
392 memcpy (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, 4);
394 #else
395 memmove (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, excess * 4);
396 #endif
397 gcov_var.offset = 0;
398 gcov_var.length = excess;
399 #if IN_LIBGCOV
400 GCOV_CHECK (!gcov_var.length || gcov_var.length == 1);
401 excess = GCOV_BLOCK_SIZE;
402 #else
403 if (gcov_var.length + words > gcov_var.alloc)
404 gcov_allocate (gcov_var.length + words);
405 excess = gcov_var.alloc - gcov_var.length;
406 #endif
407 excess = fread (gcov_var.buffer + gcov_var.length,
408 1, excess << 2, gcov_var.file) >> 2;
409 gcov_var.length += excess;
410 if (gcov_var.length < words)
412 gcov_var.overread += words - gcov_var.length;
413 gcov_var.length = 0;
414 return 0;
417 result = &gcov_var.buffer[gcov_var.offset];
418 gcov_var.offset += words;
419 return result;
422 /* Read unsigned value from a coverage file. Sets error flag on file
423 error, overflow flag on overflow */
425 GCOV_LINKAGE gcov_unsigned_t
426 gcov_read_unsigned (void)
428 gcov_unsigned_t value;
429 const gcov_unsigned_t *buffer = gcov_read_words (1);
431 if (!buffer)
432 return 0;
433 value = from_file (buffer[0]);
434 return value;
437 /* Read counter value from a coverage file. Sets error flag on file
438 error, overflow flag on overflow */
440 GCOV_LINKAGE gcov_type
441 gcov_read_counter (void)
443 gcov_type value;
444 const gcov_unsigned_t *buffer = gcov_read_words (2);
446 if (!buffer)
447 return 0;
448 value = from_file (buffer[0]);
449 if (sizeof (value) > sizeof (gcov_unsigned_t))
450 value |= ((gcov_type) from_file (buffer[1])) << 32;
451 else if (buffer[1])
452 gcov_var.error = -1;
454 return value;
457 /* Read string from coverage file. Returns a pointer to a static
458 buffer, or NULL on empty string. You must copy the string before
459 calling another gcov function. */
461 #if !IN_LIBGCOV
462 GCOV_LINKAGE const char *
463 gcov_read_string (void)
465 unsigned length = gcov_read_unsigned ();
467 if (!length)
468 return 0;
470 return (const char *) gcov_read_words (length);
472 #endif
474 GCOV_LINKAGE void
475 gcov_read_summary (struct gcov_summary *summary)
477 unsigned ix;
478 struct gcov_ctr_summary *csum;
480 summary->checksum = gcov_read_unsigned ();
481 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
483 csum->num = gcov_read_unsigned ();
484 csum->runs = gcov_read_unsigned ();
485 csum->sum_all = gcov_read_counter ();
486 csum->run_max = gcov_read_counter ();
487 csum->sum_max = gcov_read_counter ();
491 #if !IN_LIBGCOV
492 /* Reset to a known position. BASE should have been obtained from
493 gcov_position, LENGTH should be a record length. */
495 GCOV_LINKAGE void
496 gcov_sync (gcov_position_t base, gcov_unsigned_t length)
498 GCOV_CHECK_READING ();
499 base += length;
500 if (base - gcov_var.start <= gcov_var.length)
501 gcov_var.offset = base - gcov_var.start;
502 else
504 gcov_var.offset = gcov_var.length = 0;
505 fseek (gcov_var.file, base << 2, SEEK_SET);
506 gcov_var.start = ftell (gcov_var.file) >> 2;
509 #endif
511 #if IN_LIBGCOV
512 /* Move to the a set position in a gcov file. */
514 GCOV_LINKAGE void
515 gcov_seek (gcov_position_t base)
517 GCOV_CHECK_WRITING ();
518 if (gcov_var.offset)
519 gcov_write_block (gcov_var.offset);
520 fseek (gcov_var.file, base << 2, SEEK_SET);
521 gcov_var.start = ftell (gcov_var.file) >> 2;
523 #endif
525 #if IN_GCOV > 0
526 /* Return the modification time of the current gcov file. */
528 GCOV_LINKAGE time_t
529 gcov_time (void)
531 struct stat status;
533 if (fstat (fileno (gcov_var.file), &status))
534 return 0;
535 else
536 return status.st_mtime;
538 #endif /* IN_GCOV */