PR rtl-optimization/88018
[official-gcc.git] / libgcc / libgcov-driver.c
blobcdebb7473262665e4c589f70b3db0e0d08b1acd7
1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989-2018 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgcov.h"
27 #include "gcov-io.h"
29 #if defined(inhibit_libc)
30 /* If libc and its header files are not available, provide dummy functions. */
32 #if defined(L_gcov)
33 void __gcov_init (struct gcov_info *p __attribute__ ((unused))) {}
34 #endif
36 #else /* inhibit_libc */
38 #include <string.h>
39 #if GCOV_LOCKED
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <sys/stat.h>
43 #endif
45 #ifdef L_gcov
47 /* A utility function for outputting errors. */
48 static int gcov_error (const char *, ...);
50 #if !IN_GCOV_TOOL
51 static void gcov_error_exit (void);
52 #endif
54 #include "gcov-io.c"
56 struct gcov_fn_buffer
58 struct gcov_fn_buffer *next;
59 unsigned fn_ix;
60 struct gcov_fn_info info;
61 /* note gcov_fn_info ends in a trailing array. */
64 struct gcov_summary_buffer
66 struct gcov_summary_buffer *next;
67 struct gcov_summary summary;
70 /* A struct that bundles all the related information about the
71 gcda filename. */
73 struct gcov_filename
75 char *filename; /* filename buffer */
76 int strip; /* leading chars to strip from filename */
77 char *prefix; /* prefix string */
80 static struct gcov_fn_buffer *
81 free_fn_data (const struct gcov_info *gi_ptr, struct gcov_fn_buffer *buffer,
82 unsigned limit)
84 struct gcov_fn_buffer *next;
85 unsigned ix, n_ctr = 0;
87 if (!buffer)
88 return 0;
89 next = buffer->next;
91 for (ix = 0; ix != limit; ix++)
92 if (gi_ptr->merge[ix])
93 free (buffer->info.ctrs[n_ctr++].values);
94 free (buffer);
95 return next;
98 static struct gcov_fn_buffer **
99 buffer_fn_data (const char *filename, const struct gcov_info *gi_ptr,
100 struct gcov_fn_buffer **end_ptr, unsigned fn_ix)
102 unsigned n_ctrs = 0, ix = 0;
103 struct gcov_fn_buffer *fn_buffer;
104 unsigned len;
106 for (ix = GCOV_COUNTERS; ix--;)
107 if (gi_ptr->merge[ix])
108 n_ctrs++;
110 len = sizeof (*fn_buffer) + sizeof (fn_buffer->info.ctrs[0]) * n_ctrs;
111 fn_buffer = (struct gcov_fn_buffer *) xmalloc (len);
113 if (!fn_buffer)
114 goto fail;
116 fn_buffer->next = 0;
117 fn_buffer->fn_ix = fn_ix;
118 fn_buffer->info.ident = gcov_read_unsigned ();
119 fn_buffer->info.lineno_checksum = gcov_read_unsigned ();
120 fn_buffer->info.cfg_checksum = gcov_read_unsigned ();
122 for (n_ctrs = ix = 0; ix != GCOV_COUNTERS; ix++)
124 gcov_unsigned_t length;
125 gcov_type *values;
127 if (!gi_ptr->merge[ix])
128 continue;
130 if (gcov_read_unsigned () != GCOV_TAG_FOR_COUNTER (ix))
132 len = 0;
133 goto fail;
136 length = GCOV_TAG_COUNTER_NUM (gcov_read_unsigned ());
137 len = length * sizeof (gcov_type);
138 values = (gcov_type *) xmalloc (len);
139 if (!values)
140 goto fail;
142 fn_buffer->info.ctrs[n_ctrs].num = length;
143 fn_buffer->info.ctrs[n_ctrs].values = values;
145 while (length--)
146 *values++ = gcov_read_counter ();
147 n_ctrs++;
150 *end_ptr = fn_buffer;
151 return &fn_buffer->next;
153 fail:
154 gcov_error ("profiling:%s:Function %u %s %u \n", filename, fn_ix,
155 len ? "cannot allocate" : "counter mismatch", len ? len : ix);
157 return (struct gcov_fn_buffer **)free_fn_data (gi_ptr, fn_buffer, ix);
160 /* Check if VERSION of the info block PTR matches libgcov one.
161 Return 1 on success, or zero in case of versions mismatch.
162 If FILENAME is not NULL, its value used for reporting purposes
163 instead of value from the info block. */
165 static int
166 gcov_version (struct gcov_info *ptr, gcov_unsigned_t version,
167 const char *filename)
169 if (version != GCOV_VERSION)
171 char v[4], e[4];
173 GCOV_UNSIGNED2STRING (v, version);
174 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
176 gcov_error ("profiling:%s:Version mismatch - expected %.4s got %.4s\n",
177 filename? filename : ptr->filename, e, v);
178 return 0;
180 return 1;
183 /* buffer for the fn_data from another program. */
184 static struct gcov_fn_buffer *fn_buffer;
186 /* Including system dependent components. */
187 #include "libgcov-driver-system.c"
189 /* This function merges counters in GI_PTR to an existing gcda file.
190 Return 0 on success.
191 Return -1 on error. In this case, caller will goto read_fatal. */
193 static int
194 merge_one_data (const char *filename,
195 struct gcov_info *gi_ptr,
196 struct gcov_summary *summary)
198 gcov_unsigned_t tag, length;
199 unsigned t_ix;
200 int f_ix = -1;
201 int error = 0;
202 struct gcov_fn_buffer **fn_tail = &fn_buffer;
204 length = gcov_read_unsigned ();
205 if (!gcov_version (gi_ptr, length, filename))
206 return -1;
208 length = gcov_read_unsigned ();
209 if (length != gi_ptr->stamp)
211 /* Read from a different compilation. Overwrite the file. */
212 gcov_error ("profiling:%s:overwriting an existing profile data "
213 "with a different timestamp\n", filename);
214 return 0;
217 tag = gcov_read_unsigned ();
218 if (tag != GCOV_TAG_OBJECT_SUMMARY)
219 goto read_mismatch;
220 length = gcov_read_unsigned ();
221 gcc_assert (length > 0);
222 gcov_read_summary (summary);
224 tag = gcov_read_unsigned ();
225 /* Merge execution counts for each function. */
226 for (f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions;
227 f_ix++, tag = gcov_read_unsigned ())
229 const struct gcov_ctr_info *ci_ptr;
230 const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix];
232 if (tag != GCOV_TAG_FUNCTION)
233 goto read_mismatch;
235 length = gcov_read_unsigned ();
236 if (!length)
237 /* This function did not appear in the other program.
238 We have nothing to merge. */
239 continue;
241 if (length != GCOV_TAG_FUNCTION_LENGTH)
242 goto read_mismatch;
244 if (!gfi_ptr || gfi_ptr->key != gi_ptr)
246 /* This function appears in the other program. We
247 need to buffer the information in order to write
248 it back out -- we'll be inserting data before
249 this point, so cannot simply keep the data in the
250 file. */
251 fn_tail = buffer_fn_data (filename, gi_ptr, fn_tail, f_ix);
252 if (!fn_tail)
253 goto read_mismatch;
254 continue;
257 length = gcov_read_unsigned ();
258 if (length != gfi_ptr->ident)
259 goto read_mismatch;
261 length = gcov_read_unsigned ();
262 if (length != gfi_ptr->lineno_checksum)
263 goto read_mismatch;
265 length = gcov_read_unsigned ();
266 if (length != gfi_ptr->cfg_checksum)
267 goto read_mismatch;
269 ci_ptr = gfi_ptr->ctrs;
270 for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
272 gcov_merge_fn merge = gi_ptr->merge[t_ix];
274 if (!merge)
275 continue;
277 tag = gcov_read_unsigned ();
278 length = gcov_read_unsigned ();
279 if (tag != GCOV_TAG_FOR_COUNTER (t_ix)
280 || length != GCOV_TAG_COUNTER_LENGTH (ci_ptr->num))
281 goto read_mismatch;
282 (*merge) (ci_ptr->values, ci_ptr->num);
283 ci_ptr++;
285 if ((error = gcov_is_error ()))
286 goto read_error;
289 if (tag)
291 read_mismatch:;
292 gcov_error ("profiling:%s:Merge mismatch for %s %u\n",
293 filename, f_ix >= 0 ? "function" : "summary",
294 f_ix < 0 ? -1 - f_ix : f_ix);
295 return -1;
297 return 0;
299 read_error:
300 gcov_error ("profiling:%s:%s merging\n", filename,
301 error < 0 ? "Overflow": "Error");
302 return -1;
305 /* Write counters in GI_PTR and the summary in PRG to a gcda file. In
306 the case of appending to an existing file, SUMMARY_POS will be non-zero.
307 We will write the file starting from SUMMAY_POS. */
309 static void
310 write_one_data (const struct gcov_info *gi_ptr,
311 const struct gcov_summary *prg_p)
313 unsigned f_ix;
315 gcov_write_tag_length (GCOV_DATA_MAGIC, GCOV_VERSION);
316 gcov_write_unsigned (gi_ptr->stamp);
318 /* Generate whole program statistics. */
319 gcov_write_summary (GCOV_TAG_OBJECT_SUMMARY, prg_p);
321 /* Write execution counts for each function. */
322 for (f_ix = 0; f_ix != gi_ptr->n_functions; f_ix++)
324 unsigned buffered = 0;
325 const struct gcov_fn_info *gfi_ptr;
326 const struct gcov_ctr_info *ci_ptr;
327 gcov_unsigned_t length;
328 unsigned t_ix;
330 if (fn_buffer && fn_buffer->fn_ix == f_ix)
332 /* Buffered data from another program. */
333 buffered = 1;
334 gfi_ptr = &fn_buffer->info;
335 length = GCOV_TAG_FUNCTION_LENGTH;
337 else
339 gfi_ptr = gi_ptr->functions[f_ix];
340 if (gfi_ptr && gfi_ptr->key == gi_ptr)
341 length = GCOV_TAG_FUNCTION_LENGTH;
342 else
343 length = 0;
346 gcov_write_tag_length (GCOV_TAG_FUNCTION, length);
347 if (!length)
348 continue;
350 gcov_write_unsigned (gfi_ptr->ident);
351 gcov_write_unsigned (gfi_ptr->lineno_checksum);
352 gcov_write_unsigned (gfi_ptr->cfg_checksum);
354 ci_ptr = gfi_ptr->ctrs;
355 for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
357 gcov_unsigned_t n_counts;
358 gcov_type *c_ptr;
360 if (!gi_ptr->merge[t_ix])
361 continue;
363 n_counts = ci_ptr->num;
364 gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix),
365 GCOV_TAG_COUNTER_LENGTH (n_counts));
366 c_ptr = ci_ptr->values;
367 while (n_counts--)
368 gcov_write_counter (*c_ptr++);
369 ci_ptr++;
371 if (buffered)
372 fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
375 gcov_write_unsigned (0);
378 /* Helper function for merging summary. */
380 static void
381 merge_summary (int run_counted, struct gcov_summary *summary,
382 gcov_type run_max)
384 if (!run_counted)
386 summary->runs++;
387 summary->sum_max += run_max;
391 /* Sort N entries in VALUE_ARRAY in descending order.
392 Each entry in VALUE_ARRAY has two values. The sorting
393 is based on the second value. */
395 GCOV_LINKAGE void
396 gcov_sort_n_vals (gcov_type *value_array, int n)
398 int j, k;
400 for (j = 2; j < n; j += 2)
402 gcov_type cur_ent[2];
404 cur_ent[0] = value_array[j];
405 cur_ent[1] = value_array[j + 1];
406 k = j - 2;
407 while (k >= 0 && value_array[k + 1] < cur_ent[1])
409 value_array[k + 2] = value_array[k];
410 value_array[k + 3] = value_array[k+1];
411 k -= 2;
413 value_array[k + 2] = cur_ent[0];
414 value_array[k + 3] = cur_ent[1];
418 /* Sort the profile counters for all indirect call sites. Counters
419 for each call site are allocated in array COUNTERS. */
421 static void
422 gcov_sort_icall_topn_counter (const struct gcov_ctr_info *counters)
424 int i;
425 gcov_type *values;
426 int n = counters->num;
428 gcc_assert (!(n % GCOV_ICALL_TOPN_NCOUNTS));
429 values = counters->values;
431 for (i = 0; i < n; i += GCOV_ICALL_TOPN_NCOUNTS)
433 gcov_type *value_array = &values[i + 1];
434 gcov_sort_n_vals (value_array, GCOV_ICALL_TOPN_NCOUNTS - 1);
438 /* Sort topn indirect_call profile counters in GI_PTR. */
440 static void
441 gcov_sort_topn_counter_arrays (const struct gcov_info *gi_ptr)
443 unsigned int i;
444 int f_ix;
445 const struct gcov_fn_info *gfi_ptr;
446 const struct gcov_ctr_info *ci_ptr;
448 if (!gi_ptr->merge[GCOV_COUNTER_ICALL_TOPNV])
449 return;
451 for (f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions; f_ix++)
453 gfi_ptr = gi_ptr->functions[f_ix];
454 ci_ptr = gfi_ptr->ctrs;
455 for (i = 0; i < GCOV_COUNTERS; i++)
457 if (!gi_ptr->merge[i])
458 continue;
459 if (i == GCOV_COUNTER_ICALL_TOPNV)
461 gcov_sort_icall_topn_counter (ci_ptr);
462 break;
464 ci_ptr++;
469 /* Dump the coverage counts for one gcov_info object. We merge with existing
470 counts when possible, to avoid growing the .da files ad infinitum. We use
471 this program's checksum to make sure we only accumulate whole program
472 statistics to the correct summary. An object file might be embedded
473 in two separate programs, and we must keep the two program
474 summaries separate. */
476 static void
477 dump_one_gcov (struct gcov_info *gi_ptr, struct gcov_filename *gf,
478 unsigned run_counted, gcov_type run_max)
480 struct gcov_summary summary = {};
481 int error;
482 gcov_unsigned_t tag;
484 fn_buffer = 0;
486 gcov_sort_topn_counter_arrays (gi_ptr);
488 error = gcov_exit_open_gcda_file (gi_ptr, gf);
489 if (error == -1)
490 return;
492 tag = gcov_read_unsigned ();
493 if (tag)
495 /* Merge data from file. */
496 if (tag != GCOV_DATA_MAGIC)
498 gcov_error ("profiling:%s:Not a gcov data file\n", gf->filename);
499 goto read_fatal;
501 error = merge_one_data (gf->filename, gi_ptr, &summary);
502 if (error == -1)
503 goto read_fatal;
506 gcov_rewrite ();
508 merge_summary (run_counted, &summary, run_max);
510 write_one_data (gi_ptr, &summary);
511 /* fall through */
513 read_fatal:;
514 while (fn_buffer)
515 fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
517 if ((error = gcov_close ()))
518 gcov_error (error < 0 ?
519 "profiling:%s:Overflow writing\n" :
520 "profiling:%s:Error writing\n",
521 gf->filename);
525 /* Dump all the coverage counts for the program. It first computes program
526 summary and then traverses gcov_list list and dumps the gcov_info
527 objects one by one. */
529 #if !IN_GCOV_TOOL
530 static
531 #endif
532 void
533 gcov_do_dump (struct gcov_info *list, int run_counted)
535 struct gcov_info *gi_ptr;
536 struct gcov_filename gf;
538 /* Compute run_max of this program run. */
539 gcov_type run_max = 0;
540 for (gi_ptr = list; gi_ptr; gi_ptr = gi_ptr->next)
541 for (unsigned f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions; f_ix++)
543 const struct gcov_ctr_info *cinfo
544 = &gi_ptr->functions[f_ix]->ctrs[GCOV_COUNTER_ARCS];
546 for (unsigned i = 0; i < cinfo->num; i++)
547 if (run_max < cinfo->values[i])
548 run_max = cinfo->values[i];
551 allocate_filename_struct (&gf);
553 /* Now merge each file. */
554 for (gi_ptr = list; gi_ptr; gi_ptr = gi_ptr->next)
556 dump_one_gcov (gi_ptr, &gf, run_counted, run_max);
557 free (gf.filename);
560 free (gf.prefix);
563 #if IN_GCOV_TOOL
564 const char *
565 __attribute__ ((unused))
566 gcov_get_filename (struct gcov_info *list)
568 return list->filename;
570 #endif
572 #if !IN_GCOV_TOOL
573 void
574 __gcov_dump_one (struct gcov_root *root)
576 if (root->dumped)
577 return;
579 gcov_do_dump (root->list, root->run_counted);
581 root->dumped = 1;
582 root->run_counted = 1;
585 /* Per-dynamic-object gcov state. */
586 struct gcov_root __gcov_root;
588 /* Exactly one of these will be live in the process image. */
589 struct gcov_master __gcov_master =
590 {GCOV_VERSION, 0};
592 void
593 __gcov_exit (void)
595 __gcov_dump_one (&__gcov_root);
596 if (__gcov_root.next)
597 __gcov_root.next->prev = __gcov_root.prev;
598 if (__gcov_root.prev)
599 __gcov_root.prev->next = __gcov_root.next;
600 else
601 __gcov_master.root = __gcov_root.next;
603 gcov_error_exit ();
606 /* Add a new object file onto the bb chain. Invoked automatically
607 when running an object file's global ctors. */
609 void
610 __gcov_init (struct gcov_info *info)
612 if (!info->version || !info->n_functions)
613 return;
614 if (gcov_version (info, info->version, 0))
616 if (!__gcov_root.list)
618 /* Add to master list and at exit function. */
619 if (gcov_version (NULL, __gcov_master.version, "<master>"))
621 __gcov_root.next = __gcov_master.root;
622 if (__gcov_master.root)
623 __gcov_master.root->prev = &__gcov_root;
624 __gcov_master.root = &__gcov_root;
628 info->next = __gcov_root.list;
629 __gcov_root.list = info;
632 #endif /* !IN_GCOV_TOOL */
633 #endif /* L_gcov */
634 #endif /* inhibit_libc */