1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
22 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
23 WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received a copy of the GNU General Public License
28 along with GCC; see the file COPYING. If not, write to the Free
29 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 #include "coretypes.h"
37 #if defined(inhibit_libc)
38 #define IN_LIBGCOV (-1)
40 #undef NULL /* Avoid errors if stdio.h and our stddef.h mismatch. */
44 #define GCOV_LINKAGE /* nothing */
49 #if defined(inhibit_libc)
50 /* If libc and its header files are not available, provide dummy functions. */
53 void __gcov_init (struct gcov_info *p __attribute__ ((unused))) {}
54 void __gcov_flush (void) {}
57 #ifdef L_gcov_merge_add
58 void __gcov_merge_add (gcov_type *counters __attribute__ ((unused)),
59 unsigned n_counters __attribute__ ((unused))) {}
62 #ifdef L_gcov_merge_single
63 void __gcov_merge_single (gcov_type *counters __attribute__ ((unused)),
64 unsigned n_counters __attribute__ ((unused))) {}
67 #ifdef L_gcov_merge_delta
68 void __gcov_merge_delta (gcov_type *counters __attribute__ ((unused)),
69 unsigned n_counters __attribute__ ((unused))) {}
84 /* Chain of per-object gcov structures. */
85 static struct gcov_info *gcov_list;
87 /* A program checksum allows us to distinguish program data for an
88 object file included in multiple programs. */
89 static gcov_unsigned_t gcov_crc32;
92 gcov_version (struct gcov_info *ptr, gcov_unsigned_t version)
94 if (version != GCOV_VERSION)
98 GCOV_UNSIGNED2STRING (v, version);
99 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
102 "profiling:%s:Version mismatch - expected %.4s got %.4s\n",
103 ptr->filename, e, v);
109 /* Dump the coverage counts. We merge with existing counts when
110 possible, to avoid growing the .da files ad infinitum. We use this
111 program's checksum to make sure we only accumulate whole program
112 statistics to the correct summary. An object file might be embedded
113 in two separate programs, and we must keep the two program
114 summaries separate. */
119 struct gcov_info *gi_ptr;
120 struct gcov_summary this_program;
121 struct gcov_summary all;
122 struct gcov_ctr_summary *cs_ptr;
123 const struct gcov_ctr_info *ci_ptr;
125 gcov_unsigned_t c_num;
127 memset (&all, 0, sizeof (all));
128 /* Find the totals for this execution. */
129 memset (&this_program, 0, sizeof (this_program));
130 for (gi_ptr = gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
132 ci_ptr = gi_ptr->counts;
133 for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
135 if (!((1 << t_ix) & gi_ptr->ctr_mask))
138 cs_ptr = &this_program.ctrs[t_ix];
139 cs_ptr->num += ci_ptr->num;
140 for (c_num = 0; c_num < ci_ptr->num; c_num++)
142 cs_ptr->sum_all += ci_ptr->values[c_num];
143 if (cs_ptr->run_max < ci_ptr->values[c_num])
144 cs_ptr->run_max = ci_ptr->values[c_num];
150 /* Now merge each file. */
151 for (gi_ptr = gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
153 struct gcov_summary this_object;
154 struct gcov_summary object, program;
155 gcov_type *values[GCOV_COUNTERS];
156 const struct gcov_fn_info *fi_ptr;
158 unsigned c_ix, f_ix, n_counts;
159 struct gcov_ctr_summary *cs_obj, *cs_tobj, *cs_prg, *cs_tprg, *cs_all;
161 gcov_unsigned_t tag, length;
162 gcov_position_t summary_pos = 0;
163 gcov_position_t eof_pos = 0;
165 memset (&this_object, 0, sizeof (this_object));
166 memset (&object, 0, sizeof (object));
168 /* Totals for this object file. */
169 ci_ptr = gi_ptr->counts;
170 for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
172 if (!((1 << t_ix) & gi_ptr->ctr_mask))
175 cs_ptr = &this_object.ctrs[t_ix];
176 cs_ptr->num += ci_ptr->num;
177 for (c_num = 0; c_num < ci_ptr->num; c_num++)
179 cs_ptr->sum_all += ci_ptr->values[c_num];
180 if (cs_ptr->run_max < ci_ptr->values[c_num])
181 cs_ptr->run_max = ci_ptr->values[c_num];
188 for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
189 if ((1 << t_ix) & gi_ptr->ctr_mask)
191 values[c_ix] = gi_ptr->counts[c_ix].values;
195 /* Calculate the function_info stride. This depends on the
196 number of counter types being measured. */
197 fi_stride = sizeof (struct gcov_fn_info) + c_ix * sizeof (unsigned);
198 if (__alignof__ (struct gcov_fn_info) > sizeof (unsigned))
200 fi_stride += __alignof__ (struct gcov_fn_info) - 1;
201 fi_stride &= ~(__alignof__ (struct gcov_fn_info) - 1);
204 if (!gcov_open (gi_ptr->filename))
206 fprintf (stderr, "profiling:%s:Cannot open\n", gi_ptr->filename);
210 tag = gcov_read_unsigned ();
213 /* Merge data from file. */
214 if (tag != GCOV_DATA_MAGIC)
216 fprintf (stderr, "profiling:%s:Not a gcov data file\n",
220 length = gcov_read_unsigned ();
221 if (!gcov_version (gi_ptr, length))
224 length = gcov_read_unsigned ();
225 if (length != gi_ptr->stamp)
226 /* Read from a different compilation. Overwrite the file. */
229 /* Merge execution counts for each function. */
230 for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
232 fi_ptr = (const struct gcov_fn_info *)
233 ((const char *) gi_ptr->functions + f_ix * fi_stride);
234 tag = gcov_read_unsigned ();
235 length = gcov_read_unsigned ();
237 /* Check function. */
238 if (tag != GCOV_TAG_FUNCTION
239 || length != GCOV_TAG_FUNCTION_LENGTH
240 || gcov_read_unsigned () != fi_ptr->ident
241 || gcov_read_unsigned () != fi_ptr->checksum)
244 fprintf (stderr, "profiling:%s:Merge mismatch for %s\n",
246 f_ix + 1 ? "function" : "summaries");
251 for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
255 if (!((1 << t_ix) & gi_ptr->ctr_mask))
258 n_counts = fi_ptr->n_ctrs[c_ix];
259 merge = gi_ptr->counts[c_ix].merge;
261 tag = gcov_read_unsigned ();
262 length = gcov_read_unsigned ();
263 if (tag != GCOV_TAG_FOR_COUNTER (t_ix)
264 || length != GCOV_TAG_COUNTER_LENGTH (n_counts))
266 (*merge) (values[c_ix], n_counts);
267 values[c_ix] += n_counts;
270 if ((error = gcov_is_error ()))
275 /* Check program & object summary */
280 eof_pos = gcov_position ();
281 tag = gcov_read_unsigned ();
285 length = gcov_read_unsigned ();
286 is_program = tag == GCOV_TAG_PROGRAM_SUMMARY;
287 if (length != GCOV_TAG_SUMMARY_LENGTH
288 || (!is_program && tag != GCOV_TAG_OBJECT_SUMMARY))
290 gcov_read_summary (is_program ? &program : &object);
291 if ((error = gcov_is_error ()))
293 if (is_program && program.checksum == gcov_crc32)
295 summary_pos = eof_pos;
303 fprintf (stderr, error < 0 ? "profiling:%s:Overflow merging\n"
304 : "profiling:%s:Error merging\n", gi_ptr->filename);
313 memset (&program, 0, sizeof (program));
315 /* Merge the summaries. */
317 for (t_ix = 0; t_ix < GCOV_COUNTERS_SUMMABLE; t_ix++)
319 cs_obj = &object.ctrs[t_ix];
320 cs_tobj = &this_object.ctrs[t_ix];
321 cs_prg = &program.ctrs[t_ix];
322 cs_tprg = &this_program.ctrs[t_ix];
323 cs_all = &all.ctrs[t_ix];
325 if ((1 << t_ix) & gi_ptr->ctr_mask)
328 cs_obj->num = cs_tobj->num;
329 else if (cs_obj->num != cs_tobj->num)
331 cs_obj->sum_all += cs_tobj->sum_all;
332 if (cs_obj->run_max < cs_tobj->run_max)
333 cs_obj->run_max = cs_tobj->run_max;
334 cs_obj->sum_max += cs_tobj->run_max;
337 cs_prg->num = cs_tprg->num;
338 else if (cs_prg->num != cs_tprg->num)
340 cs_prg->sum_all += cs_tprg->sum_all;
341 if (cs_prg->run_max < cs_tprg->run_max)
342 cs_prg->run_max = cs_tprg->run_max;
343 cs_prg->sum_max += cs_tprg->run_max;
345 else if (cs_obj->num || cs_prg->num)
348 if (!cs_all->runs && cs_prg->runs)
349 memcpy (cs_all, cs_prg, sizeof (*cs_all));
350 else if (!all.checksum
351 && (!GCOV_LOCKED || cs_all->runs == cs_prg->runs)
352 && memcmp (cs_all, cs_prg, sizeof (*cs_all)))
354 fprintf (stderr, "profiling:%s:Invocation mismatch - some data files may have been removed%s",
355 gi_ptr->filename, GCOV_LOCKED
356 ? "" : " or concurrent update without locking support");
362 for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
363 if ((1 << t_ix) & gi_ptr->ctr_mask)
365 values[c_ix] = gi_ptr->counts[c_ix].values;
369 program.checksum = gcov_crc32;
371 /* Write out the data. */
372 gcov_write_tag_length (GCOV_DATA_MAGIC, GCOV_VERSION);
373 gcov_write_unsigned (gi_ptr->stamp);
375 /* Write execution counts for each function. */
376 for (f_ix = 0; f_ix < gi_ptr->n_functions; f_ix++)
378 fi_ptr = (const struct gcov_fn_info *)
379 ((const char *) gi_ptr->functions + f_ix * fi_stride);
381 /* Announce function. */
382 gcov_write_tag_length (GCOV_TAG_FUNCTION, GCOV_TAG_FUNCTION_LENGTH);
383 gcov_write_unsigned (fi_ptr->ident);
384 gcov_write_unsigned (fi_ptr->checksum);
387 for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
391 if (!((1 << t_ix) & gi_ptr->ctr_mask))
394 n_counts = fi_ptr->n_ctrs[c_ix];
396 gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix),
397 GCOV_TAG_COUNTER_LENGTH (n_counts));
398 c_ptr = values[c_ix];
400 gcov_write_counter (*c_ptr++);
402 values[c_ix] = c_ptr;
407 /* Object file summary. */
408 gcov_write_summary (GCOV_TAG_OBJECT_SUMMARY, &object);
410 /* Generate whole program statistics. */
413 gcov_write_summary (GCOV_TAG_PROGRAM_SUMMARY, &program);
415 gcov_write_unsigned (0);
416 if ((error = gcov_close ()))
417 fprintf (stderr, error < 0 ?
418 "profiling:%s:Overflow writing\n" :
419 "profiling:%s:Error writing\n",
424 /* Add a new object file onto the bb chain. Invoked automatically
425 when running an object file's global ctors. */
428 __gcov_init (struct gcov_info *info)
432 if (gcov_version (info, info->version))
434 const char *ptr = info->filename;
435 gcov_unsigned_t crc32 = gcov_crc32;
440 gcov_unsigned_t value = *ptr << 24;
442 for (ix = 8; ix--; value <<= 1)
444 gcov_unsigned_t feedback;
446 feedback = (value ^ crc32) & 0x80000000 ? 0x04c11db7 : 0;
458 info->next = gcov_list;
464 /* Called before fork or exec - write out profile information gathered so
465 far and reset it to zero. This avoids duplication or loss of the
466 profile information gathered so far. */
471 const struct gcov_info *gi_ptr;
474 for (gi_ptr = gcov_list; gi_ptr; gi_ptr = gi_ptr->next)
477 const struct gcov_ctr_info *ci_ptr;
479 for (t_ix = 0, ci_ptr = gi_ptr->counts; t_ix != GCOV_COUNTERS; t_ix++)
480 if ((1 << t_ix) & gi_ptr->ctr_mask)
482 memset (ci_ptr->values, 0, sizeof (gcov_type) * ci_ptr->num);
490 #ifdef L_gcov_merge_add
491 /* The profile merging function that just adds the counters. It is given
492 an array COUNTERS of N_COUNTERS old counters and it reads the same number
493 of counters from the gcov file. */
495 __gcov_merge_add (gcov_type *counters, unsigned n_counters)
497 for (; n_counters; counters++, n_counters--)
498 *counters += gcov_read_counter ();
500 #endif /* L_gcov_merge_add */
502 #ifdef L_gcov_merge_single
503 /* The profile merging function for choosing the most common value.
504 It is given an array COUNTERS of N_COUNTERS old counters and it
505 reads the same number of counters from the gcov file. The counters
506 are split into 3-tuples where the members of the tuple have
509 -- the stored candidate on the most common value of the measured entity
511 -- total number of evaluations of the value */
513 __gcov_merge_single (gcov_type *counters, unsigned n_counters)
515 unsigned i, n_measures;
516 gcov_type value, counter, all;
518 gcc_assert (!(n_counters % 3));
519 n_measures = n_counters / 3;
520 for (i = 0; i < n_measures; i++, counters += 3)
522 value = gcov_read_counter ();
523 counter = gcov_read_counter ();
524 all = gcov_read_counter ();
526 if (counters[0] == value)
527 counters[1] += counter;
528 else if (counter > counters[1])
531 counters[1] = counter - counters[1];
534 counters[1] -= counter;
538 #endif /* L_gcov_merge_single */
540 #ifdef L_gcov_merge_delta
541 /* The profile merging function for choosing the most common
542 difference between two consecutive evaluations of the value. It is
543 given an array COUNTERS of N_COUNTERS old counters and it reads the
544 same number of counters from the gcov file. The counters are split
545 into 4-tuples where the members of the tuple have meanings:
547 -- the last value of the measured entity
548 -- the stored candidate on the most common difference
550 -- total number of evaluations of the value */
552 __gcov_merge_delta (gcov_type *counters, unsigned n_counters)
554 unsigned i, n_measures;
555 gcov_type last, value, counter, all;
557 gcc_assert (!(n_counters % 4));
558 n_measures = n_counters / 4;
559 for (i = 0; i < n_measures; i++, counters += 4)
561 last = gcov_read_counter ();
562 value = gcov_read_counter ();
563 counter = gcov_read_counter ();
564 all = gcov_read_counter ();
566 if (counters[1] == value)
567 counters[2] += counter;
568 else if (counter > counters[2])
571 counters[2] = counter - counters[2];
574 counters[2] -= counter;
578 #endif /* L_gcov_merge_delta */
580 #ifdef L_gcov_interval_profiler
581 /* If VALUE is in interval <START, START + STEPS - 1>, then increases the
582 corresponding counter in COUNTERS. If the VALUE is above or below
583 the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased
587 __gcov_interval_profiler (gcov_type *counters, gcov_type value,
588 int start, unsigned steps)
590 gcov_type delta = value - start;
592 counters[steps + 1]++;
593 else if (delta >= steps)
600 #ifdef L_gcov_pow2_profiler
601 /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
602 COUNTERS[0] is incremented. */
605 __gcov_pow2_profiler (gcov_type *counters, gcov_type value)
607 if (value & (value - 1))
614 #ifdef L_gcov_one_value_profiler
615 /* Tries to determine the most common value among its inputs. Checks if the
616 value stored in COUNTERS[0] matches VALUE. If this is the case, COUNTERS[1]
617 is incremented. If this is not the case and COUNTERS[1] is not zero,
618 COUNTERS[1] is decremented. Otherwise COUNTERS[1] is set to one and
619 VALUE is stored to COUNTERS[0]. This algorithm guarantees that if this
620 function is called more than 50% of the time with one value, this value
621 will be in COUNTERS[0] in the end.
623 In any case, COUNTERS[2] is incremented. */
626 __gcov_one_value_profiler (gcov_type *counters, gcov_type value)
628 if (value == counters[0])
630 else if (counters[1] == 0)
642 /* A wrapper for the fork function. Flushes the accumulated profiling data, so
643 that they are not counted twice. */
654 /* A wrapper for the execl function. Flushes the accumulated profiling data, so
655 that they are not lost. */
658 __gcov_execl (const char *path, const char *arg, ...)
670 while (va_arg (ap, char *))
674 args = alloca (length * sizeof (void *));
675 args[0] = (char *) arg;
676 for (i = 1; i < length; i++)
677 args[i] = va_arg (aq, char *);
680 return execv (path, args);
685 /* A wrapper for the execlp function. Flushes the accumulated profiling data, so
686 that they are not lost. */
689 __gcov_execlp (const char *path, const char *arg, ...)
701 while (va_arg (ap, char *))
705 args = alloca (length * sizeof (void *));
706 args[0] = (char *) arg;
707 for (i = 1; i < length; i++)
708 args[i] = va_arg (aq, char *);
711 return execvp (path, args);
716 /* A wrapper for the execle function. Flushes the accumulated profiling data, so
717 that they are not lost. */
720 __gcov_execle (const char *path, const char *arg, ...)
733 while (va_arg (ap, char *))
737 args = alloca (length * sizeof (void *));
738 args[0] = (char *) arg;
739 for (i = 1; i < length; i++)
740 args[i] = va_arg (aq, char *);
741 envp = va_arg (aq, char **);
744 return execve (path, args, envp);
749 /* A wrapper for the execv function. Flushes the accumulated profiling data, so
750 that they are not lost. */
753 __gcov_execv (const char *path, char *const argv[])
756 return execv (path, argv);
761 /* A wrapper for the execvp function. Flushes the accumulated profiling data, so
762 that they are not lost. */
765 __gcov_execvp (const char *path, char *const argv[])
768 return execvp (path, argv);
773 /* A wrapper for the execve function. Flushes the accumulated profiling data, so
774 that they are not lost. */
777 __gcov_execve (const char *path, char *const argv[], char *const envp[])
780 return execve (path, argv, envp);
783 #endif /* inhibit_libc */