* config/arm/elf.h (ASM_OUTPUT_ALIGNED_COMMON): Remove definition.
[official-gcc.git] / gcc / gcov-io.c
blob2f2a87fdb217780164b5406564fcd7ef172d9026
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
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 /* 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. */
35 GCOV_LINKAGE int
36 gcov_open (const char *name, int mode)
38 int result = 1;
39 size_t alloc = 1024;
40 #if defined (TARGET_HAS_F_SETLKW) && IN_LIBGCOV
41 struct flock s_flock;
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
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)
58 result = -1;
59 gcov_var.file = fopen (name, "w+b");
61 if (!gcov_var.file)
62 return 0;
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
70 if (result >= 0)
72 if (fseek (gcov_var.file, 0, SEEK_END))
74 fclose (gcov_var.file);
75 gcov_var.file = 0;
76 return 0;
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)
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)
91 fclose (gcov_var.file);
92 gcov_var.file = 0;
93 gcov_var.length = 0;
94 gcov_var.alloc = 0;
95 return 0;
97 #else
98 gcov_var.buffer = xmalloc (gcov_var.alloc);
99 #endif
101 if (result >= 0
102 && fread (gcov_var.buffer, gcov_var.length, 1, gcov_var.file) != 1)
104 fclose (gcov_var.file);
105 gcov_var.file = 0;
106 gcov_var.length = 0;
107 return 0;
109 return result;
112 /* Close the current gcov file. Flushes data to disk. Returns nonzero
113 on failure or error flag set. */
115 GCOV_LINKAGE int
116 gcov_close ()
118 int result = 0;
120 if (gcov_var.file)
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;
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;
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. */
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)
153 return 0;
154 #if IN_LIBGCOV
155 result = realloc (gcov_var.buffer, new_size);
156 if (!result)
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;
165 #else
166 result = xrealloc (gcov_var.buffer, new_size);
167 #endif
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;
177 return result;
180 /* Write unsigned VALUE to coverage file. Sets error flag
181 appropriately. */
183 GCOV_LINKAGE void
184 gcov_write_unsigned (unsigned value)
186 unsigned char *buffer = gcov_write_bytes (4);
187 unsigned ix;
189 if (!buffer)
190 return;
191 for (ix = 4; ix--; )
193 buffer[ix] = value;
194 value >>= 8;
196 if (sizeof (value) > 4 && value)
197 gcov_var.error = -1;
199 return;
202 /* Write counter VALUE to coverage file. Sets error flag
203 appropriately. */
205 #if IN_LIBGCOV
206 GCOV_LINKAGE void
207 gcov_write_counter (gcov_type value)
209 unsigned char *buffer = gcov_write_bytes (8);
210 unsigned ix;
212 if (!buffer)
213 return;
214 for (ix = 8; ix--; )
216 buffer[ix] = value;
217 value >>= 8;
219 if ((sizeof (value) > 8 && value) || value < 0)
220 gcov_var.error = -1;
221 return;
223 #endif /* IN_LIBGCOV */
225 #if !IN_LIBGCOV
226 /* Write STRING to coverage file. Sets error flag on file
227 error, overflow flag on overflow */
229 GCOV_LINKAGE void
230 gcov_write_string (const char *string)
232 unsigned length = 0;
233 unsigned pad = 0;
234 unsigned rem = 0;
235 unsigned char *buffer;
237 if (string)
239 length = strlen (string);
240 rem = 4 - (length & 3);
243 buffer = gcov_write_bytes (4 + length + rem);
244 if (buffer)
246 unsigned ix;
247 unsigned value = length;
249 for (ix = 4; ix--; )
251 buffer[ix] = value;
252 value >>= 8;
254 memcpy (buffer + 4, string, length);
255 memcpy (buffer + 4 + length, &pad, rem);
258 #endif
260 /* Write a tag TAG and reserve space for the record length. Return a
261 value to be used for gcov_write_length. */
263 GCOV_LINKAGE unsigned long
264 gcov_write_tag (unsigned tag)
266 unsigned long result = gcov_var.position;
267 unsigned char *buffer = gcov_write_bytes (8);
268 unsigned ix;
270 if (!buffer)
271 return 0;
272 for (ix = 4; ix--; )
274 buffer[ix] = tag;
275 tag >>= 8;
277 memset (buffer + 4, 0, 4);
278 return result;
281 /* Write a record length using POSITION, which was returned by
282 gcov_write_tag. The current file position is the end of the
283 record, and is restored before returning. Returns nonzero on
284 overflow. */
286 GCOV_LINKAGE void
287 gcov_write_length (unsigned long position)
289 if (position)
291 unsigned length = gcov_var.position - position - 8;
292 unsigned char *buffer = &gcov_var.buffer[position + 4];
293 unsigned ix;
295 for (ix = 4; ix--; )
297 buffer[ix] = length;
298 length >>= 8;
303 #if IN_LIBGCOV
304 /* Write a summary structure to the gcov file. Return non-zero on
305 overflow. */
307 GCOV_LINKAGE void
308 gcov_write_summary (unsigned tag, const struct gcov_summary *summary)
310 unsigned ix;
311 const struct gcov_ctr_summary *csum;
312 unsigned long base;
314 base = gcov_write_tag (tag);
315 gcov_write_unsigned (summary->checksum);
316 for (csum = summary->ctrs, ix = GCOV_COUNTERS; ix--; csum++)
318 gcov_write_unsigned (csum->num);
319 gcov_write_unsigned (csum->runs);
320 gcov_write_counter (csum->sum_all);
321 gcov_write_counter (csum->run_max);
322 gcov_write_counter (csum->sum_max);
324 gcov_write_length (base);
326 #endif /* IN_LIBGCOV */
328 #endif /*!IN_GCOV */
330 /* Return a pointer to read BYTES bytes from the gcov file. Returns
331 NULL on failure (read past EOF). */
333 GCOV_LINKAGE const unsigned char *
334 gcov_read_bytes (unsigned bytes)
336 const unsigned char *result;
338 if (gcov_var.position + bytes > gcov_var.length)
340 gcov_var.error = 1;
341 return 0;
344 result = &gcov_var.buffer[gcov_var.position];
345 gcov_var.position += bytes;
346 return result;
349 /* Read unsigned value from a coverage file. Sets error flag on file
350 error, overflow flag on overflow */
352 GCOV_LINKAGE unsigned
353 gcov_read_unsigned ()
355 unsigned value = 0;
356 unsigned ix;
357 const unsigned char *buffer = gcov_read_bytes (4);
359 if (!buffer)
360 return 0;
361 for (ix = sizeof (value); ix < 4; ix++)
362 if (buffer[ix])
363 gcov_var.error = -1;
364 for (ix = 0; ix != 4; ix++)
366 value <<= 8;
367 value |= buffer[ix];
369 return value;
372 /* Read counter value from a coverage file. Sets error flag on file
373 error, overflow flag on overflow */
375 GCOV_LINKAGE gcov_type
376 gcov_read_counter ()
378 gcov_type value = 0;
379 unsigned ix;
380 const unsigned char *buffer = gcov_read_bytes (8);
382 if (!buffer)
383 return 0;
384 for (ix = sizeof (value); ix < 8; ix++)
385 if (buffer[ix])
386 gcov_var.error = -1;
387 for (ix = 0; ix != 8; ix++)
389 value <<= 8;
390 value |= buffer[ix];
392 if (value < 0)
393 gcov_var.error = -1;
394 return value;
397 /* Read string from coverage file. Returns a pointer to a static
398 buffer, or NULL on empty string. You must copy the string before
399 calling another gcov function. */
401 #if !IN_LIBGCOV
402 GCOV_LINKAGE const char *
403 gcov_read_string ()
405 unsigned length = gcov_read_unsigned ();
407 if (!length)
408 return 0;
410 length += 4 - (length & 3);
411 return (const char *) gcov_read_bytes (length);
413 #endif
415 GCOV_LINKAGE void
416 gcov_read_summary (struct gcov_summary *summary)
418 unsigned ix;
419 struct gcov_ctr_summary *csum;
421 summary->checksum = gcov_read_unsigned ();
422 for (csum = summary->ctrs, ix = GCOV_COUNTERS; ix--; csum++)
424 csum->num = gcov_read_unsigned ();
425 csum->runs = gcov_read_unsigned ();
426 csum->sum_all = gcov_read_counter ();
427 csum->run_max = gcov_read_counter ();
428 csum->sum_max = gcov_read_counter ();
432 #if IN_GCOV > 0
433 /* Return the modification time of the current gcov file. */
435 GCOV_LINKAGE time_t
436 gcov_time ()
438 struct stat status;
440 if (fstat (fileno (gcov_var.file), &status))
441 return 0;
442 else
443 return status.st_mtime;
445 #endif /* IN_GCOV */