OSDN Git Service

* Makefile.in (LIBGCC_DEPS): Add gcov headers.
[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 /* 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.  */
34
35 GCOV_LINKAGE int
36 gcov_open (const char *name, int mode)
37 {
38   int result = 1;
39   size_t alloc = 1024;
40 #if defined (TARGET_HAS_F_SETLKW) && IN_LIBGCOV
41   struct flock s_flock;
42
43   s_flock.l_type = F_WRLCK;
44   s_flock.l_whence = SEEK_SET;
45   s_flock.l_start = 0;
46   s_flock.l_len = 0; /* Until EOF.  */
47   s_flock.l_pid = getpid ();
48 #endif
49   
50   if (gcov_var.file)
51     abort ();
52   gcov_var.position = gcov_var.length = 0;
53   gcov_var.error = gcov_var.modified = 0;
54   if (mode >= 0)
55     gcov_var.file = fopen (name, "r+b");
56   if (!gcov_var.file && mode <= 0)
57     {
58       result = -1;
59       gcov_var.file = fopen (name, "w+b");
60     }
61   if (!gcov_var.file)
62     return 0;
63
64 #if defined (TARGET_HAS_F_SETLKW) && IN_LIBGCOV
65   while (fcntl (fileno (gcov_var.file), F_SETLKW, &s_flock)
66          && errno == EINTR)
67     continue;
68 #endif
69
70   if (result >= 0)
71     {
72       if (fseek (gcov_var.file, 0, SEEK_END))
73         {
74           fclose (gcov_var.file);
75           gcov_var.file = 0;
76           return 0;
77         }
78       gcov_var.length = ftell (gcov_var.file);
79       fseek (gcov_var.file, 0, SEEK_SET);
80       alloc += gcov_var.length;
81     }
82   if (alloc > gcov_var.alloc)
83     {
84       if (gcov_var.buffer)
85         free (gcov_var.buffer);
86       gcov_var.alloc = alloc;
87 #if IN_LIBGCOV
88       gcov_var.buffer = malloc (gcov_var.alloc);
89       if (!gcov_var.buffer)
90         {
91           fclose (gcov_var.file);
92           gcov_var.file = 0;
93           gcov_var.length = 0;
94           gcov_var.alloc = 0;
95           return 0;
96         }
97 #else
98       gcov_var.buffer = xmalloc (gcov_var.alloc);
99 #endif
100     }
101   if (result >= 0
102       && fread (gcov_var.buffer, gcov_var.length, 1, gcov_var.file) != 1)
103     {
104       fclose (gcov_var.file);
105       gcov_var.file = 0;
106       gcov_var.length = 0;
107       return 0;
108     }
109   return result;
110 }
111
112 /* Close the current gcov file. Flushes data to disk. Returns nonzero
113    on failure or error flag set.  */
114
115 GCOV_LINKAGE int
116 gcov_close ()
117 {
118   int result = 0;
119   
120   if (gcov_var.file)
121     {
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))
126         result = 1;
127       fclose (gcov_var.file);
128       gcov_var.file = 0;
129       gcov_var.length = 0;
130     }
131 #if !IN_LIBGCOV
132   free (gcov_var.buffer);
133   gcov_var.alloc = 0;
134   gcov_var.buffer = 0;
135 #endif
136   return result ? 1 : gcov_var.error;
137 }
138
139 #if !IN_GCOV
140 /* Allocate space to write BYTES bytes to the gcov file. Return a
141    pointer to those bytes, or NULL on failure.  */
142
143 GCOV_LINKAGE unsigned char *
144 gcov_write_bytes (unsigned bytes)
145 {
146   char unsigned *result;
147
148   if (gcov_var.position + bytes > gcov_var.alloc)
149     {
150       size_t new_size = (gcov_var.alloc + bytes) * 3 / 2;
151
152       if (!gcov_var.buffer)
153         return 0;
154 #if IN_LIBGCOV
155       result = realloc (gcov_var.buffer, new_size);
156       if (!result)
157         {
158           free (gcov_var.buffer);
159           gcov_var.buffer = 0;
160           gcov_var.alloc = 0;
161           gcov_var.position = gcov_var.length = 0;
162           gcov_var.error = 1;
163           return 0;
164         }
165 #else
166       result = xrealloc (gcov_var.buffer, new_size);
167 #endif
168       gcov_var.alloc = new_size;
169       gcov_var.buffer = result;
170     }
171   
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;
177   return result;
178 }
179
180 /* Write unsigned VALUE to coverage file.  Sets error flag
181    appropriately.  */
182
183 GCOV_LINKAGE void
184 gcov_write_unsigned (unsigned value)
185 {
186   unsigned char *buffer = gcov_write_bytes (4);
187   unsigned ix;
188
189   if (!buffer)
190     return;
191   for (ix = 4; ix--; )
192     {
193       buffer[ix] = value;
194       value >>= 8;
195     }
196   if (sizeof (value) > 4 && value)
197     gcov_var.error = -1;
198
199   return;
200 }
201
202 /* Write counter VALUE to coverage file.  Sets error flag
203    appropriately.  */
204
205 #if IN_LIBGCOV
206 GCOV_LINKAGE void
207 gcov_write_counter (gcov_type value)
208 {
209   unsigned char *buffer = gcov_write_bytes (8);
210   unsigned ix;
211
212   if (!buffer)
213     return;
214   for (ix = 8; ix--; )
215     {
216       buffer[ix] = value;
217       value >>= 8;
218     }
219   if ((sizeof (value) > 8 && value) || value < 0)
220     gcov_var.error = -1;
221   return;
222 }
223 #endif /* IN_LIBGCOV */
224
225 /* Write STRING to coverage file.  Sets error flag on file
226    error, overflow flag on overflow */
227
228 GCOV_LINKAGE void
229 gcov_write_string (const char *string)
230 {
231   unsigned length = 0;
232   unsigned pad = 0;
233   unsigned rem = 0;
234   unsigned char *buffer;
235
236   if (string)
237     {
238       length = strlen (string);
239       rem = 4 - (length & 3);
240     }
241   
242   buffer = gcov_write_bytes (4 + length + rem);
243   if (buffer)
244     {
245       unsigned ix;
246       unsigned value = length;
247       
248       for (ix = 4; ix--; )
249         {
250           buffer[ix] = value;
251           value >>= 8;
252         }
253       memcpy (buffer + 4, string, length);
254       memcpy (buffer + 4 + length, &pad, rem);
255     }
256 }
257
258 /* Write a tag TAG and reserve space for the record length. Return a
259    value to be used for gcov_write_length.  */
260
261 GCOV_LINKAGE unsigned long
262 gcov_write_tag (unsigned tag)
263 {
264   unsigned long result = gcov_var.position;
265   unsigned char *buffer = gcov_write_bytes (8);
266   unsigned ix;
267
268   if (!buffer)
269     return 0;
270   for (ix = 4; ix--; )
271     {
272       buffer[ix] = tag;
273       tag >>= 8;
274     }
275   memset (buffer + 4, 0, 4);
276   return result;
277 }
278
279 /* Write a record length using POSITION, which was returned by
280    gcov_write_tag.  The current file position is the end of the
281    record, and is restored before returning.  Returns nonzero on
282    overflow.  */
283
284 GCOV_LINKAGE void
285 gcov_write_length (unsigned long position)
286 {
287   if (position)
288     {
289       unsigned length = gcov_var.position - position - 8;
290       unsigned char *buffer = &gcov_var.buffer[position + 4];
291       unsigned ix;
292       
293       for (ix = 4; ix--; )
294         {
295           buffer[ix] = length;
296           length >>= 8;
297         }
298     }
299 }
300
301 #if IN_LIBGCOV
302 /* Write a summary structure to the gcov file.  Return non-zero on
303    overflow.  */
304
305 GCOV_LINKAGE void
306 gcov_write_summary (unsigned tag, const struct gcov_summary *summary)
307 {
308   unsigned ix;
309   const struct gcov_ctr_summary *csum;
310   unsigned long base;
311
312   base = gcov_write_tag (tag);
313   gcov_write_unsigned (summary->checksum);
314   for (csum = summary->ctrs, ix = GCOV_COUNTERS; ix--; csum++)
315     {
316       gcov_write_unsigned (csum->num);
317       gcov_write_unsigned (csum->runs);
318       gcov_write_counter (csum->sum_all);
319       gcov_write_counter (csum->run_max);
320       gcov_write_counter (csum->sum_max);
321     }
322   gcov_write_length (base);
323 }
324 #endif /* IN_LIBGCOV */
325
326 #endif /*!IN_GCOV */
327
328 /* Return a pointer to read BYTES bytes from the gcov file. Returns
329    NULL on failure (read past EOF). */
330
331 GCOV_LINKAGE const unsigned char *
332 gcov_read_bytes (unsigned bytes)
333 {
334   const unsigned char *result;
335   
336   if (gcov_var.position + bytes > gcov_var.length)
337     {
338       gcov_var.error = 1;
339       return 0;
340     }
341   
342   result = &gcov_var.buffer[gcov_var.position];
343   gcov_var.position += bytes;
344   return result;
345 }
346
347 /* Read unsigned value from a coverage file. Sets error flag on file
348    error, overflow flag on overflow */
349
350 GCOV_LINKAGE unsigned
351 gcov_read_unsigned ()
352 {
353   unsigned value = 0;
354   unsigned ix;
355   const unsigned char *buffer = gcov_read_bytes (4);
356
357   if (!buffer)
358     return 0;
359   for (ix = sizeof (value); ix < 4; ix++)
360     if (buffer[ix])
361       gcov_var.error = -1;
362   for (ix = 0; ix != 4; ix++)
363     {
364       value <<= 8;
365       value |= buffer[ix];
366     }
367   return value;
368 }
369
370 /* Read counter value from a coverage file. Sets error flag on file
371    error, overflow flag on overflow */
372
373 GCOV_LINKAGE gcov_type
374 gcov_read_counter ()
375 {
376   gcov_type value = 0;
377   unsigned ix;
378   const unsigned char *buffer = gcov_read_bytes (8);
379
380   if (!buffer)
381     return 0;
382   for (ix = sizeof (value); ix < 8; ix++)
383     if (buffer[ix])
384       gcov_var.error = -1;
385   for (ix = 0; ix != 8; ix++)
386     {
387       value <<= 8;
388       value |= buffer[ix];
389     }
390   if (value < 0)
391     gcov_var.error = -1;
392   return value;
393 }
394
395 /* Read string from coverage file. Returns a pointer to a static
396    buffer, or NULL on empty string. You must copy the string before
397    calling another gcov function.  */
398
399 GCOV_LINKAGE const char *
400 gcov_read_string ()
401 {
402   unsigned length = gcov_read_unsigned ();
403   
404   if (!length)
405     return 0;
406
407   length += 4 - (length & 3);
408   return (const char *) gcov_read_bytes (length);
409 }
410
411 GCOV_LINKAGE void
412 gcov_read_summary (struct gcov_summary *summary)
413 {
414   unsigned ix;
415   struct gcov_ctr_summary *csum;
416   
417   summary->checksum = gcov_read_unsigned ();
418   for (csum = summary->ctrs, ix = GCOV_COUNTERS; ix--; csum++)
419     {
420       csum->num = gcov_read_unsigned ();
421       csum->runs = gcov_read_unsigned ();
422       csum->sum_all = gcov_read_counter ();
423       csum->run_max = gcov_read_counter ();
424       csum->sum_max = gcov_read_counter ();
425     }
426 }
427
428 #if IN_GCOV > 0
429 /* Return the modification time of the current gcov file.  */
430
431 GCOV_LINKAGE time_t
432 gcov_time ()
433 {
434   struct stat status;
435   
436   if (fstat (fileno (gcov_var.file), &status))
437     return 0;
438   else
439     return status.st_mtime;
440 }
441 #endif /* IN_GCOV */