blob: aeb9d123a98dc23d61c25ffe8f8963ccc984c587 [file] [log] [blame]
Stefan Reinauerd37ab452012-12-18 16:23:28 -08001/* 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>.
6
7This file is part of GCC.
8
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 3, or (at your option) any later
12version.
13
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
18
19Under Section 7 of GPL version 3, you are granted additional
20permissions described in the GCC Runtime Library Exception, version
213.1, as published by the Free Software Foundation.
Patrick Georgia73b9312015-10-31 11:55:10 +010022*/
Stefan Reinauerd37ab452012-12-18 16:23:28 -080023
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#if !IN_GCOV
28static void gcov_write_block (unsigned);
29static gcov_unsigned_t *gcov_write_words (unsigned);
30#endif
31static const gcov_unsigned_t *gcov_read_words (unsigned);
32#if !IN_LIBGCOV
33static void gcov_allocate (unsigned);
34#endif
35
36static inline gcov_unsigned_t from_file (gcov_unsigned_t value)
37{
38#if !IN_LIBGCOV
39 if (gcov_var.endian)
40 {
41 value = (value >> 16) | (value << 16);
42 value = ((value & 0xff00ff) << 8) | ((value >> 8) & 0xff00ff);
43 }
44#endif
45 return value;
46}
47
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. If MODE is >= 0 an existing file will be opened, if
51 possible, and if MODE is <= 0, a new file will be created. Use
52 MODE=0 to attempt to reopen an existing file and then fall back on
53 creating a new one. If MODE < 0, the file will be opened in
54 read-only mode. Otherwise it will be opened for modification.
55 Return zero on failure, >0 on opening an existing file and <0 on
56 creating a new one. */
57
58GCOV_LINKAGE int
59#if IN_LIBGCOV
60gcov_open (const char *name)
61#else
62gcov_open (const char *name, int mode)
63#endif
64{
65#if IN_LIBGCOV
66 const int mode = 0;
67#endif
68#if GCOV_LOCKED
69 struct flock s_flock;
70 int fd;
71
72 s_flock.l_whence = SEEK_SET;
73 s_flock.l_start = 0;
74 s_flock.l_len = 0; /* Until EOF. */
75 s_flock.l_pid = getpid ();
76#endif
77
78 gcc_assert (!gcov_var.file);
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 {
89 /* Read-only mode - acquire a read-lock. */
90 s_flock.l_type = F_RDLCK;
91 fd = open (name, O_RDONLY);
92 }
93 else
94 {
95 /* Write mode - acquire a write-lock. */
96 s_flock.l_type = F_WRLCK;
97 fd = open (name, O_RDWR | O_CREAT, 0666);
98 }
99 if (fd < 0)
100 return 0;
101
102 while (fcntl (fd, F_SETLKW, &s_flock) && errno == EINTR)
103 continue;
104
105 gcov_var.file = fdopen (fd, (mode > 0) ? "rb" : "r+b");
106
107 if (!gcov_var.file)
108 {
109 close (fd);
110 return 0;
111 }
112
113 if (mode > 0)
114 gcov_var.mode = 1;
115 else if (mode == 0)
116 {
117 struct stat st;
118
119 if (fstat (fd, &st) < 0)
120 {
121 fclose (gcov_var.file);
122 gcov_var.file = 0;
123 return 0;
124 }
125 if (st.st_size != 0)
126 gcov_var.mode = 1;
127 else
128 gcov_var.mode = mode * 2 + 1;
129 }
130 else
131 gcov_var.mode = mode * 2 + 1;
132#else
133 if (mode >= 0)
134 gcov_var.file = fopen (name, (mode > 0) ? "rb" : "r+b");
135
136 if (gcov_var.file)
137 gcov_var.mode = 1;
138 else if (mode <= 0)
139 {
140 gcov_var.file = fopen (name, "w+b");
141 if (gcov_var.file)
142 gcov_var.mode = mode * 2 + 1;
143 }
144 if (!gcov_var.file)
145 return 0;
146#endif
147
148 setbuf (gcov_var.file, (char *)0);
149
150 return 1;
151}
152
153/* Close the current gcov file. Flushes data to disk. Returns nonzero
154 on failure or error flag set. */
155
156GCOV_LINKAGE int
157gcov_close (void)
158{
159 if (gcov_var.file)
160 {
161#if !IN_GCOV
162 if (gcov_var.offset && gcov_var.mode < 0)
163 gcov_write_block (gcov_var.offset);
164#endif
165 fclose (gcov_var.file);
166 gcov_var.file = 0;
167 gcov_var.length = 0;
168 }
169#if !IN_LIBGCOV
170 free (gcov_var.buffer);
171 gcov_var.alloc = 0;
172 gcov_var.buffer = 0;
173#endif
174 gcov_var.mode = 0;
175 return gcov_var.error;
176}
177
178#if !IN_LIBGCOV
179/* Check if MAGIC is EXPECTED. Use it to determine endianness of the
180 file. Returns +1 for same endian, -1 for other endian and zero for
181 not EXPECTED. */
182
183GCOV_LINKAGE int
184gcov_magic (gcov_unsigned_t magic, gcov_unsigned_t expected)
185{
186 if (magic == expected)
187 return 1;
188 magic = (magic >> 16) | (magic << 16);
189 magic = ((magic & 0xff00ff) << 8) | ((magic >> 8) & 0xff00ff);
190 if (magic == expected)
191 {
192 gcov_var.endian = 1;
193 return -1;
194 }
195 return 0;
196}
197#endif
198
199#if !IN_LIBGCOV
200static void
201gcov_allocate (unsigned length)
202{
203 size_t new_size = gcov_var.alloc;
204
205 if (!new_size)
206 new_size = GCOV_BLOCK_SIZE;
207 new_size += length;
208 new_size *= 2;
209
210 gcov_var.alloc = new_size;
211 gcov_var.buffer = XRESIZEVAR (gcov_unsigned_t, gcov_var.buffer, new_size << 2);
212}
213#endif
214
215#if !IN_GCOV
216/* Write out the current block, if needs be. */
217
218static void
219gcov_write_block (unsigned size)
220{
221 if (fwrite (gcov_var.buffer, size << 2, 1, gcov_var.file) != 1)
222 gcov_var.error = 1;
223 gcov_var.start += size;
224 gcov_var.offset -= size;
225}
226
227/* Allocate space to write BYTES bytes to the gcov file. Return a
228 pointer to those bytes, or NULL on failure. */
229
230static gcov_unsigned_t *
231gcov_write_words (unsigned words)
232{
233 gcov_unsigned_t *result;
234
235 gcc_assert (gcov_var.mode < 0);
236#if IN_LIBGCOV
237 if (gcov_var.offset >= GCOV_BLOCK_SIZE)
238 {
239 gcov_write_block (GCOV_BLOCK_SIZE);
240 if (gcov_var.offset)
241 {
242 gcc_assert (gcov_var.offset == 1);
243 memcpy (gcov_var.buffer, gcov_var.buffer + GCOV_BLOCK_SIZE, 4);
244 }
245 }
246#else
247 if (gcov_var.offset + words > gcov_var.alloc)
248 gcov_allocate (gcov_var.offset + words);
249#endif
250 result = &gcov_var.buffer[gcov_var.offset];
251 gcov_var.offset += words;
252
253 return result;
254}
255
256/* Write unsigned VALUE to coverage file. Sets error flag
257 appropriately. */
258
259GCOV_LINKAGE void
260gcov_write_unsigned (gcov_unsigned_t value)
261{
262 gcov_unsigned_t *buffer = gcov_write_words (1);
263
264 buffer[0] = value;
265}
266
267/* Write counter VALUE to coverage file. Sets error flag
268 appropriately. */
269
270#if IN_LIBGCOV
271GCOV_LINKAGE void
272gcov_write_counter (gcov_type value)
273{
274 gcov_unsigned_t *buffer = gcov_write_words (2);
275
276 buffer[0] = (gcov_unsigned_t) value;
277 if (sizeof (value) > sizeof (gcov_unsigned_t))
278 buffer[1] = (gcov_unsigned_t) (value >> 32);
279 else
280 buffer[1] = 0;
281}
282#endif /* IN_LIBGCOV */
283
284#if !IN_LIBGCOV
285/* Write STRING to coverage file. Sets error flag on file
286 error, overflow flag on overflow */
287
288GCOV_LINKAGE void
289gcov_write_string (const char *string)
290{
291 unsigned length = 0;
292 unsigned alloc = 0;
293 gcov_unsigned_t *buffer;
294
295 if (string)
296 {
297 length = strlen (string);
298 alloc = (length + 4) >> 2;
299 }
300
301 buffer = gcov_write_words (1 + alloc);
302
303 buffer[0] = alloc;
304 buffer[alloc] = 0;
305 memcpy (&buffer[1], string, length);
306}
307#endif
308
309#if !IN_LIBGCOV
310/* Write a tag TAG and reserve space for the record length. Return a
311 value to be used for gcov_write_length. */
312
313GCOV_LINKAGE gcov_position_t
314gcov_write_tag (gcov_unsigned_t tag)
315{
316 gcov_position_t result = gcov_var.start + gcov_var.offset;
317 gcov_unsigned_t *buffer = gcov_write_words (2);
318
319 buffer[0] = tag;
320 buffer[1] = 0;
321
322 return result;
323}
324
325/* Write a record length using POSITION, which was returned by
326 gcov_write_tag. The current file position is the end of the
327 record, and is restored before returning. Returns nonzero on
328 overflow. */
329
330GCOV_LINKAGE void
331gcov_write_length (gcov_position_t position)
332{
333 unsigned offset;
334 gcov_unsigned_t length;
335 gcov_unsigned_t *buffer;
336
337 gcc_assert (gcov_var.mode < 0);
338 gcc_assert (position + 2 <= gcov_var.start + gcov_var.offset);
339 gcc_assert (position >= gcov_var.start);
340 offset = position - gcov_var.start;
341 length = gcov_var.offset - offset - 2;
342 buffer = (gcov_unsigned_t *) &gcov_var.buffer[offset];
343 buffer[1] = length;
344 if (gcov_var.offset >= GCOV_BLOCK_SIZE)
345 gcov_write_block (gcov_var.offset);
346}
347
348#else /* IN_LIBGCOV */
349
350/* Write a tag TAG and length LENGTH. */
351
352GCOV_LINKAGE void
353gcov_write_tag_length (gcov_unsigned_t tag, gcov_unsigned_t length)
354{
355 gcov_unsigned_t *buffer = gcov_write_words (2);
356
357 buffer[0] = tag;
358 buffer[1] = length;
359}
360
361/* Write a summary structure to the gcov file. Return nonzero on
362 overflow. */
363
364GCOV_LINKAGE void
365gcov_write_summary (gcov_unsigned_t tag, const struct gcov_summary *summary)
366{
367 unsigned ix;
368 const struct gcov_ctr_summary *csum;
369
370 gcov_write_tag_length (tag, GCOV_TAG_SUMMARY_LENGTH);
371 gcov_write_unsigned (summary->checksum);
372 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
373 {
374 gcov_write_unsigned (csum->num);
375 gcov_write_unsigned (csum->runs);
376 gcov_write_counter (csum->sum_all);
377 gcov_write_counter (csum->run_max);
378 gcov_write_counter (csum->sum_max);
379 }
380}
381#endif /* IN_LIBGCOV */
382
383#endif /*!IN_GCOV */
384
385/* Return a pointer to read BYTES bytes from the gcov file. Returns
386 NULL on failure (read past EOF). */
387
388static const gcov_unsigned_t *
389gcov_read_words (unsigned words)
390{
391 const gcov_unsigned_t *result;
392 unsigned excess = gcov_var.length - gcov_var.offset;
393
394 gcc_assert (gcov_var.mode > 0);
395 if (excess < words)
396 {
397 gcov_var.start += gcov_var.offset;
398#if IN_LIBGCOV
399 if (excess)
400 {
401 gcc_assert (excess == 1);
402 memcpy (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, 4);
403 }
404#else
405 memmove (gcov_var.buffer, gcov_var.buffer + gcov_var.offset, excess * 4);
406#endif
407 gcov_var.offset = 0;
408 gcov_var.length = excess;
409#if IN_LIBGCOV
410 gcc_assert (!gcov_var.length || gcov_var.length == 1);
411 excess = GCOV_BLOCK_SIZE;
412#else
413 if (gcov_var.length + words > gcov_var.alloc)
414 gcov_allocate (gcov_var.length + words);
415 excess = gcov_var.alloc - gcov_var.length;
416#endif
417 excess = fread (gcov_var.buffer + gcov_var.length,
418 1, excess << 2, gcov_var.file) >> 2;
419 gcov_var.length += excess;
420 if (gcov_var.length < words)
421 {
422 gcov_var.overread += words - gcov_var.length;
423 gcov_var.length = 0;
424 return 0;
425 }
426 }
427 result = &gcov_var.buffer[gcov_var.offset];
428 gcov_var.offset += words;
429 return result;
430}
431
432/* Read unsigned value from a coverage file. Sets error flag on file
433 error, overflow flag on overflow */
434
435GCOV_LINKAGE gcov_unsigned_t
436gcov_read_unsigned (void)
437{
438 gcov_unsigned_t value;
439 const gcov_unsigned_t *buffer = gcov_read_words (1);
440
441 if (!buffer)
442 return 0;
443 value = from_file (buffer[0]);
444 return value;
445}
446
447/* Read counter value from a coverage file. Sets error flag on file
448 error, overflow flag on overflow */
449
450GCOV_LINKAGE gcov_type
451gcov_read_counter (void)
452{
453 gcov_type value;
454 const gcov_unsigned_t *buffer = gcov_read_words (2);
455
456 if (!buffer)
457 return 0;
458 value = from_file (buffer[0]);
459 if (sizeof (value) > sizeof (gcov_unsigned_t))
460 value |= ((gcov_type) from_file (buffer[1])) << 32;
461 else if (buffer[1])
462 gcov_var.error = -1;
463
464 return value;
465}
466
467/* Read string from coverage file. Returns a pointer to a static
468 buffer, or NULL on empty string. You must copy the string before
469 calling another gcov function. */
470
471#if !IN_LIBGCOV
472GCOV_LINKAGE const char *
473gcov_read_string (void)
474{
475 unsigned length = gcov_read_unsigned ();
476
477 if (!length)
478 return 0;
479
480 return (const char *) gcov_read_words (length);
481}
482#endif
483
484GCOV_LINKAGE void
485gcov_read_summary (struct gcov_summary *summary)
486{
487 unsigned ix;
488 struct gcov_ctr_summary *csum;
489
490 summary->checksum = gcov_read_unsigned ();
491 for (csum = summary->ctrs, ix = GCOV_COUNTERS_SUMMABLE; ix--; csum++)
492 {
493 csum->num = gcov_read_unsigned ();
494 csum->runs = gcov_read_unsigned ();
495 csum->sum_all = gcov_read_counter ();
496 csum->run_max = gcov_read_counter ();
497 csum->sum_max = gcov_read_counter ();
498 }
499}
500
501#if !IN_LIBGCOV
502/* Reset to a known position. BASE should have been obtained from
503 gcov_position, LENGTH should be a record length. */
504
505GCOV_LINKAGE void
506gcov_sync (gcov_position_t base, gcov_unsigned_t length)
507{
508 gcc_assert (gcov_var.mode > 0);
509 base += length;
510 if (base - gcov_var.start <= gcov_var.length)
511 gcov_var.offset = base - gcov_var.start;
512 else
513 {
514 gcov_var.offset = gcov_var.length = 0;
515 fseek (gcov_var.file, base << 2, SEEK_SET);
516 gcov_var.start = ftell (gcov_var.file) >> 2;
517 }
518}
519#endif
520
521#if IN_LIBGCOV
522/* Move to a given position in a gcov file. */
523
524GCOV_LINKAGE void
525gcov_seek (gcov_position_t base)
526{
527 gcc_assert (gcov_var.mode < 0);
528 if (gcov_var.offset)
529 gcov_write_block (gcov_var.offset);
530 fseek (gcov_var.file, base << 2, SEEK_SET);
531 gcov_var.start = ftell (gcov_var.file) >> 2;
532}
533#endif
534
535#if IN_GCOV > 0
536/* Return the modification time of the current gcov file. */
537
538GCOV_LINKAGE time_t
539gcov_time (void)
540{
541 struct stat status;
542
543 if (fstat (fileno (gcov_var.file), &status))
544 return 0;
545 else
546 return status.st_mtime;
547}
548#endif /* IN_GCOV */