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>.
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
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
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
24 /* Routines declared in gcov-io.h. This file should be #included by
25 another source file, after having #included gcov-io.h. */
27 /* Open a gcov file. NAME is the name of the file to open and MODE
28 indicates whether a new file should be created, or an existing file
29 opened for modification. If MODE is >= 0 an existing file will be
30 opened, if possible, and if MODE is <= 0, a new file will be
31 created. Use MODE=0 to attempt to reopen an existing file and then
32 fall back on creating a new one. Return zero on failure, >0 on
33 opening an existing file and <0 on creating a new one. */
36 gcov_open (const char *name, int mode)
43 s_flock.l_type = F_WRLCK;
44 s_flock.l_whence = SEEK_SET;
46 s_flock.l_len = 0; /* Until EOF. */
47 s_flock.l_pid = getpid ();
52 gcov_var.position = gcov_var.length = 0;
53 gcov_var.error = gcov_var.modified = 0;
55 gcov_var.file = fopen (name, "r+b");
56 if (!gcov_var.file && mode <= 0)
59 gcov_var.file = fopen (name, "w+b");
65 while (fcntl (fileno (gcov_var.file), F_SETLKW, &s_flock)
72 if (fseek (gcov_var.file, 0, SEEK_END))
74 fclose (gcov_var.file);
78 gcov_var.length = ftell (gcov_var.file);
79 fseek (gcov_var.file, 0, SEEK_SET);
80 alloc += gcov_var.length;
82 if (alloc > gcov_var.alloc)
85 free (gcov_var.buffer);
86 gcov_var.alloc = alloc;
88 gcov_var.buffer = malloc (gcov_var.alloc);
91 fclose (gcov_var.file);
98 gcov_var.buffer = xmalloc (gcov_var.alloc);
102 && fread (gcov_var.buffer, gcov_var.length, 1, gcov_var.file) != 1)
104 fclose (gcov_var.file);
112 /* Close the current gcov file. Flushes data to disk. Returns nonzero
113 on failure or error flag set. */
122 if (gcov_var.modified
123 && (fseek (gcov_var.file, 0, SEEK_SET)
124 || fwrite (gcov_var.buffer, gcov_var.length,
125 1, gcov_var.file) != 1))
127 fclose (gcov_var.file);
132 free (gcov_var.buffer);
136 return result ? 1 : gcov_var.error;
140 /* Allocate space to write BYTES bytes to the gcov file. Return a
141 pointer to those bytes, or NULL on failure. */
143 GCOV_LINKAGE unsigned char *
144 gcov_write_bytes (unsigned bytes)
146 char unsigned *result;
148 if (gcov_var.position + bytes > gcov_var.alloc)
150 size_t new_size = (gcov_var.alloc + bytes) * 3 / 2;
152 if (!gcov_var.buffer)
155 result = realloc (gcov_var.buffer, new_size);
158 free (gcov_var.buffer);
161 gcov_var.position = gcov_var.length = 0;
166 result = xrealloc (gcov_var.buffer, new_size);
168 gcov_var.alloc = new_size;
169 gcov_var.buffer = result;
172 result = &gcov_var.buffer[gcov_var.position];
173 gcov_var.position += bytes;
174 gcov_var.modified = 1;
175 if (gcov_var.position > gcov_var.length)
176 gcov_var.length = gcov_var.position;
180 /* Write unsigned VALUE to coverage file. Sets error flag
184 gcov_write_unsigned (unsigned value)
186 unsigned char *buffer = gcov_write_bytes (4);
196 if (sizeof (value) > 4 && value)
202 /* Write counter VALUE to coverage file. Sets error flag
207 gcov_write_counter (gcov_type value)
209 unsigned char *buffer = gcov_write_bytes (8);
219 if ((sizeof (value) > 8 && value) || value < 0)
223 #endif /* IN_LIBGCOV */
226 /* Write STRING to coverage file. Sets error flag on file
227 error, overflow flag on overflow */
230 gcov_write_string (const char *string)
235 unsigned char *buffer;
239 length = strlen (string);
240 rem = 4 - (length & 3);
243 buffer = gcov_write_bytes (4 + length + rem);
247 unsigned value = length;
254 memcpy (buffer + 4, string, length);
255 memcpy (buffer + 4 + length, &pad, rem);
261 /* Write a tag TAG and reserve space for the record length. Return a
262 value to be used for gcov_write_length. */
264 GCOV_LINKAGE unsigned long
265 gcov_write_tag (unsigned tag)
267 unsigned long result = gcov_var.position;
268 unsigned char *buffer = gcov_write_bytes (8);
278 memset (buffer + 4, 0, 4);
282 /* Write a record length using POSITION, which was returned by
283 gcov_write_tag. The current file position is the end of the
284 record, and is restored before returning. Returns nonzero on
288 gcov_write_length (unsigned long position)
292 unsigned length = gcov_var.position - position - 8;
293 unsigned char *buffer = &gcov_var.buffer[position + 4];
305 /* Write a tag TAG and length LENGTH. */
308 gcov_write_tag_length (unsigned tag, unsigned length)
310 unsigned char *buffer = gcov_write_bytes (8);
322 buffer[ix + 4] = length;
329 /* Write a summary structure to the gcov file. Return non-zero on
333 gcov_write_summary (unsigned tag, const struct gcov_summary *summary)
336 const struct gcov_ctr_summary *csum;
338 gcov_write_tag_length (tag, GCOV_TAG_SUMMARY_LENGTH);
339 gcov_write_unsigned (summary->checksum);
340 for (csum = summary->ctrs, ix = GCOV_COUNTERS; ix--; csum++)
342 gcov_write_unsigned (csum->num);
343 gcov_write_unsigned (csum->runs);
344 gcov_write_counter (csum->sum_all);
345 gcov_write_counter (csum->run_max);
346 gcov_write_counter (csum->sum_max);
349 #endif /* IN_LIBGCOV */
353 /* Return a pointer to read BYTES bytes from the gcov file. Returns
354 NULL on failure (read past EOF). */
356 GCOV_LINKAGE const unsigned char *
357 gcov_read_bytes (unsigned bytes)
359 const unsigned char *result;
361 if (gcov_var.position + bytes > gcov_var.length)
367 result = &gcov_var.buffer[gcov_var.position];
368 gcov_var.position += bytes;
372 /* Read unsigned value from a coverage file. Sets error flag on file
373 error, overflow flag on overflow */
375 GCOV_LINKAGE unsigned
376 gcov_read_unsigned ()
380 const unsigned char *buffer = gcov_read_bytes (4);
384 for (ix = sizeof (value); ix < 4; ix++)
387 for (ix = 0; ix != 4; ix++)
395 /* Read counter value from a coverage file. Sets error flag on file
396 error, overflow flag on overflow */
398 GCOV_LINKAGE gcov_type
403 const unsigned char *buffer = gcov_read_bytes (8);
407 for (ix = sizeof (value); ix < 8; ix++)
410 for (ix = 0; ix != 8; ix++)
420 /* Read string from coverage file. Returns a pointer to a static
421 buffer, or NULL on empty string. You must copy the string before
422 calling another gcov function. */
425 GCOV_LINKAGE const char *
428 unsigned length = gcov_read_unsigned ();
433 length += 4 - (length & 3);
434 return (const char *) gcov_read_bytes (length);
439 gcov_read_summary (struct gcov_summary *summary)
442 struct gcov_ctr_summary *csum;
444 summary->checksum = gcov_read_unsigned ();
445 for (csum = summary->ctrs, ix = GCOV_COUNTERS; ix--; csum++)
447 csum->num = gcov_read_unsigned ();
448 csum->runs = gcov_read_unsigned ();
449 csum->sum_all = gcov_read_counter ();
450 csum->run_max = gcov_read_counter ();
451 csum->sum_max = gcov_read_counter ();
456 /* Return the modification time of the current gcov file. */
463 if (fstat (fileno (gcov_var.file), &status))
466 return status.st_mtime;