2010-11-27 François Dumont <francois.cppdevs@free.fr>
[official-gcc.git] / gcc / gcov-io.c
blob6d371cdcddf28048364452a92ae3fc02603eea4b
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>.
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 3, 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 COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 /* Routines declared in gcov-io.h. This file should be #included by
24 another source file, after having #included gcov-io.h. */
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
35 static inline gcov_unsigned_t from_file (gcov_unsigned_t value)
37 #if !IN_LIBGCOV
38 if (gcov_var.endian)
40 value = (value >> 16) | (value << 16);
41 value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff);
43 #endif
44 return value;
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. */
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
64 #if IN_LIBGCOV
65 const int mode = 0;
66 #endif
67 #if GCOV_LOCKED
68 struct flock s_flock;
69 int fd;
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 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)
88 /* Read-only mode - acquire a read-lock. */
89 s_flock.l_type = F_RDLCK;
90 fd = open (name, O_RDONLY);
92 else
94 /* Write mode - acquire a write-lock. */
95 s_flock.l_type = F_WRLCK;
96 fd = open (name, O_RDWR | O_CREAT, 0666);
98 if (fd < 0)
99 return 0;
101 while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR)
102 continue;
104 gcov_var.file = fdopen (fd, (mode > 0) ? "rb" : "r+b");
106 if (!gcov_var.file)
108 close (fd);
109 return 0;
112 if (mode > 0)
113 gcov_var.mode = 1;
114 else if (mode == 0)
116 struct stat st;
118 if (fstat (fd, &st) < 0)
120 fclose (gcov_var.file);
121 gcov_var.file = 0;
122 return 0;
124 if (st.st_size != 0)
125 gcov_var.mode = 1;
126 else
127 gcov_var.mode = mode * 2 + 1;
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");
135 if (gcov_var.file)
136 gcov_var.mode = 1;
137 else if (mode <= 0)
139 gcov_var.file = fopen (name, "w+b");
140 if (gcov_var.file)
141 gcov_var.mode = mode * 2 + 1;
143 if (!gcov_var.file)
144 return 0;
145 #endif
147 setbuf (gcov_var.file, (char *)0);
149 return 1;
152 /* Close the current gcov file. Flushes data to disk. Returns nonzero
153 on failure or error flag set. */
155 GCOV_LINKAGE int
156 gcov_close (void)
158 if (gcov_var.file)
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;
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;
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. */
182 GCOV_LINKAGE int
183 gcov_magic (gcov_unsigned_t magic, gcov_unsigned_t expected)
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)
191 gcov_var.endian = 1;
192 return -1;
194 return 0;
196 #endif
198 #if !IN_LIBGCOV
199 static void
200 gcov_allocate (unsigned length)
202 size_t new_size = gcov_var.alloc;
204 if (!new_size)
205 new_size = GCOV_BLOCK_SIZE;
206 new_size += length;
207 new_size *= 2;
209 gcov_var.alloc = new_size;
210 gcov_var.buffer = XRESIZEVAR (gcov_unsigned_t, gcov_var.buffer, new_size << 2);
212 #endif
214 #if !IN_GCOV
215 /* Write out the current block, if needs be. */
217 static void
218 gcov_write_block (unsigned size)
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;
226 /* Allocate space to write BYTES bytes to the gcov file. Return a
227 pointer to those bytes, or NULL on failure. */
229 static gcov_unsigned_t *
230 gcov_write_words (unsigned words)
232 gcov_unsigned_t *result;
234 gcc_assert (gcov_var.mode < 0);
235 #if IN_LIBGCOV
236 if (gcov_var.offset >= GCOV_BLOCK_SIZE)
238 gcov_write_block (GCOV_BLOCK_SIZE);
239 if (gcov_var.offset)
241 gcc_assert (gcov_var.offset == 1);
242 memcpy (gcov_var.buffer, gcov_var.buffer + GCOV_BLOCK_SIZE, 4);
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;
252 return result;
255 /* Write unsigned VALUE to coverage file. Sets error flag
256 appropriately. */
258 GCOV_LINKAGE void
259 gcov_write_unsigned (gcov_unsigned_t value)
261 gcov_unsigned_t *buffer = gcov_write_words (1);
263 buffer[0] = value;
266 /* Write counter VALUE to coverage file. Sets error flag
267 appropriately. */
269 #if IN_LIBGCOV
270 GCOV_LINKAGE void
271 gcov_write_counter (gcov_type value)
273 gcov_unsigned_t *buffer = gcov_write_words (2);
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;
281 #endif /* IN_LIBGCOV */
283 #if !IN_LIBGCOV
284 /* Write STRING to coverage file. Sets error flag on file
285 error, overflow flag on overflow */
287 GCOV_LINKAGE void
288 gcov_write_string (const char *string)
290 unsigned length = 0;
291 unsigned alloc = 0;
292 gcov_unsigned_t *buffer;
294 if (string)
296 length = strlen (string);
297 alloc = (length + 4) >> 2;
300 buffer = gcov_write_words (1 + alloc);
302 buffer[0] = alloc;
303 buffer[alloc] = 0;
304 memcpy (&buffer[1], string, length);
306 #endif
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. */
312 GCOV_LINKAGE gcov_position_t
313 gcov_write_tag (gcov_unsigned_t tag)
315 gcov_position_t result = gcov_var.start + gcov_var.offset;
316 gcov_unsigned_t *buffer = gcov_write_words (2);
318 buffer[0] = tag;
319 buffer[1] = 0;
321 return result;
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. */
329 GCOV_LINKAGE void
330 gcov_write_length (gcov_position_t position)
332 unsigned offset;
333 gcov_unsigned_t length;
334 gcov_unsigned_t *buffer;
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);
347 #else /* IN_LIBGCOV */
349 /* Write a tag TAG and length LENGTH. */
351 GCOV_LINKAGE void
352 gcov_write_tag_length (gcov_unsigned_t tag, gcov_unsigned_t length)
354 gcov_unsigned_t *buffer = gcov_write_words (2);
356 buffer[0] = tag;
357 buffer[1] = length;
360 /* Write a summary structure to the gcov file. Return nonzero on
361 overflow. */
363 GCOV_LINKAGE void
364 gcov_write_summary (gcov_unsigned_t tag, const struct gcov_summary *summary)
366 unsigned ix;
367 const struct gcov_ctr_summary *csum;
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++)
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);
380 #endif /* IN_LIBGCOV */
382 #endif /*!IN_GCOV */
384 /* Return a pointer to read BYTES bytes from the gcov file. Returns
385 NULL on failure (read past EOF). */
387 static const gcov_unsigned_t *
388 gcov_read_words (unsigned words)
390 const gcov_unsigned_t *result;
391 unsigned excess = gcov_var.length - gcov_var.offset;
393 gcc_assert (gcov_var.mode > 0);
394 if (excess < words)
396 gcov_var.start += gcov_var.offset;
397 #if IN_LIBGCOV
398 if (excess)
400 gcc_assert (excess == 1);
401 memcpy (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, 4);
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)
421 gcov_var.overread += words - gcov_var.length;
422 gcov_var.length = 0;
423 return 0;
426 result = &gcov_var.buffer[gcov_var.offset];
427 gcov_var.offset += words;
428 return result;
431 /* Read unsigned value from a coverage file. Sets error flag on file
432 error, overflow flag on overflow */
434 GCOV_LINKAGE gcov_unsigned_t
435 gcov_read_unsigned (void)
437 gcov_unsigned_t value;
438 const gcov_unsigned_t *buffer = gcov_read_words (1);
440 if (!buffer)
441 return 0;
442 value = from_file (buffer[0]);
443 return value;
446 /* Read counter value from a coverage file. Sets error flag on file
447 error, overflow flag on overflow */
449 GCOV_LINKAGE gcov_type
450 gcov_read_counter (void)
452 gcov_type value;
453 const gcov_unsigned_t *buffer = gcov_read_words (2);
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;
463 return value;
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. */
470 #if !IN_LIBGCOV
471 GCOV_LINKAGE const char *
472 gcov_read_string (void)
474 unsigned length = gcov_read_unsigned ();
476 if (!length)
477 return 0;
479 return (const char *) gcov_read_words (length);
481 #endif
483 GCOV_LINKAGE void
484 gcov_read_summary (struct gcov_summary *summary)
486 unsigned ix;
487 struct gcov_ctr_summary *csum;
489 summary->checksum = gcov_read_unsigned ();
490 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
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 ();
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. */
504 GCOV_LINKAGE void
505 gcov_sync (gcov_position_t base, gcov_unsigned_t length)
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
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;
518 #endif
520 #if IN_LIBGCOV
521 /* Move to a given position in a gcov file. */
523 GCOV_LINKAGE void
524 gcov_seek (gcov_position_t base)
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;
532 #endif
534 #if IN_GCOV > 0
535 /* Return the modification time of the current gcov file. */
537 GCOV_LINKAGE time_t
538 gcov_time (void)
540 struct stat status;
542 if (fstat (fileno (gcov_var.file), &status))
543 return 0;
544 else
545 return status.st_mtime;
547 #endif /* IN_GCOV */