]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgcc/libgcov-driver.c
* doc/extend.texi (Common Function Attributes): Clarify
[thirdparty/gcc.git] / libgcc / libgcov-driver.c
CommitLineData
ded3d3f8 1/* Routines required for instrumenting a program. */
2/* Compile this one with gcc. */
fbd26352 3/* Copyright (C) 1989-2019 Free Software Foundation, Inc.
ded3d3f8 4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
25
67745126 26#include "libgcov.h"
56621355 27#include "gcov-io.h"
ded3d3f8 28
29#if defined(inhibit_libc)
30/* If libc and its header files are not available, provide dummy functions. */
31
32#if defined(L_gcov)
33void __gcov_init (struct gcov_info *p __attribute__ ((unused))) {}
34#endif
35
36#else /* inhibit_libc */
37
38#include <string.h>
39#if GCOV_LOCKED
40#include <fcntl.h>
41#include <errno.h>
42#include <sys/stat.h>
43#endif
44
45#ifdef L_gcov
f1919901 46
3237fc56 47/* A utility function for outputting errors. */
f1919901 48static int gcov_error (const char *, ...);
49
3237fc56 50#if !IN_GCOV_TOOL
51static void gcov_error_exit (void);
52#endif
53
ded3d3f8 54#include "gcov-io.c"
55
085a4ac7 56#define GCOV_PROF_PREFIX "libgcov profiling error:%s:"
57
ded3d3f8 58struct gcov_fn_buffer
59{
60 struct gcov_fn_buffer *next;
61 unsigned fn_ix;
62 struct gcov_fn_info info;
63 /* note gcov_fn_info ends in a trailing array. */
64};
65
66struct gcov_summary_buffer
67{
68 struct gcov_summary_buffer *next;
69 struct gcov_summary summary;
70};
71
6401b74d 72/* A struct that bundles all the related information about the
73 gcda filename. */
74
75struct gcov_filename
76{
77 char *filename; /* filename buffer */
6401b74d 78 int strip; /* leading chars to strip from filename */
5b24ad4b 79 char *prefix; /* prefix string */
6401b74d 80};
81
ded3d3f8 82static struct gcov_fn_buffer *
83free_fn_data (const struct gcov_info *gi_ptr, struct gcov_fn_buffer *buffer,
84 unsigned limit)
85{
86 struct gcov_fn_buffer *next;
87 unsigned ix, n_ctr = 0;
88
89 if (!buffer)
90 return 0;
91 next = buffer->next;
92
93 for (ix = 0; ix != limit; ix++)
94 if (gi_ptr->merge[ix])
95 free (buffer->info.ctrs[n_ctr++].values);
96 free (buffer);
97 return next;
98}
99
100static struct gcov_fn_buffer **
101buffer_fn_data (const char *filename, const struct gcov_info *gi_ptr,
102 struct gcov_fn_buffer **end_ptr, unsigned fn_ix)
103{
104 unsigned n_ctrs = 0, ix = 0;
105 struct gcov_fn_buffer *fn_buffer;
106 unsigned len;
107
108 for (ix = GCOV_COUNTERS; ix--;)
109 if (gi_ptr->merge[ix])
110 n_ctrs++;
111
112 len = sizeof (*fn_buffer) + sizeof (fn_buffer->info.ctrs[0]) * n_ctrs;
67745126 113 fn_buffer = (struct gcov_fn_buffer *) xmalloc (len);
ded3d3f8 114
115 if (!fn_buffer)
116 goto fail;
117
118 fn_buffer->next = 0;
119 fn_buffer->fn_ix = fn_ix;
120 fn_buffer->info.ident = gcov_read_unsigned ();
121 fn_buffer->info.lineno_checksum = gcov_read_unsigned ();
122 fn_buffer->info.cfg_checksum = gcov_read_unsigned ();
123
124 for (n_ctrs = ix = 0; ix != GCOV_COUNTERS; ix++)
125 {
126 gcov_unsigned_t length;
127 gcov_type *values;
128
129 if (!gi_ptr->merge[ix])
130 continue;
131
132 if (gcov_read_unsigned () != GCOV_TAG_FOR_COUNTER (ix))
133 {
134 len = 0;
135 goto fail;
136 }
137
138 length = GCOV_TAG_COUNTER_NUM (gcov_read_unsigned ());
139 len = length * sizeof (gcov_type);
67745126 140 values = (gcov_type *) xmalloc (len);
ded3d3f8 141 if (!values)
142 goto fail;
143
144 fn_buffer->info.ctrs[n_ctrs].num = length;
145 fn_buffer->info.ctrs[n_ctrs].values = values;
146
147 while (length--)
148 *values++ = gcov_read_counter ();
149 n_ctrs++;
150 }
151
152 *end_ptr = fn_buffer;
153 return &fn_buffer->next;
154
155fail:
085a4ac7 156 gcov_error (GCOV_PROF_PREFIX "Function %u %s %u \n", filename, fn_ix,
ded3d3f8 157 len ? "cannot allocate" : "counter mismatch", len ? len : ix);
158
159 return (struct gcov_fn_buffer **)free_fn_data (gi_ptr, fn_buffer, ix);
160}
161
02638fde 162/* Convert VERSION into a string description and return the it.
163 BUFFER is used for storage of the string. The code should be
164 aligned wit gcov-iov.c. */
165
166static char *
167gcov_version_string (char *buffer, char version[4])
168{
169 if (version[0] < 'A' || version[0] > 'Z'
170 || version[1] < '0' || version[1] > '9'
171 || version[2] < '0' || version[2] > '9')
172 sprintf (buffer, "(unknown)");
173 else
174 {
175 unsigned major = 10 * (version[0] - 'A') + (version[1] - '0');
176 unsigned minor = version[2] - '0';
177 sprintf (buffer, "%u.%u (%s)", major, minor,
178 version[3] == '*' ? "release" : "experimental");
179 }
180 return buffer;
181}
182
ded3d3f8 183/* Check if VERSION of the info block PTR matches libgcov one.
184 Return 1 on success, or zero in case of versions mismatch.
185 If FILENAME is not NULL, its value used for reporting purposes
186 instead of value from the info block. */
187
188static int
189gcov_version (struct gcov_info *ptr, gcov_unsigned_t version,
190 const char *filename)
191{
192 if (version != GCOV_VERSION)
193 {
194 char v[4], e[4];
02638fde 195 char version_string[128], expected_string[128];
ded3d3f8 196
197 GCOV_UNSIGNED2STRING (v, version);
198 GCOV_UNSIGNED2STRING (e, GCOV_VERSION);
199
085a4ac7 200 gcov_error (GCOV_PROF_PREFIX "Version mismatch - expected %s (%.4s) "
02638fde 201 "got %s (%.4s)\n",
202 filename? filename : ptr->filename,
203 gcov_version_string (expected_string, e), e,
204 gcov_version_string (version_string, v), v);
ded3d3f8 205 return 0;
206 }
207 return 1;
208}
209
ded3d3f8 210/* buffer for the fn_data from another program. */
211static struct gcov_fn_buffer *fn_buffer;
ded3d3f8 212
ded3d3f8 213/* Including system dependent components. */
214#include "libgcov-driver-system.c"
215
216/* This function merges counters in GI_PTR to an existing gcda file.
217 Return 0 on success.
218 Return -1 on error. In this case, caller will goto read_fatal. */
219
220static int
1186f97a 221merge_one_data (const char *filename,
222 struct gcov_info *gi_ptr,
56621355 223 struct gcov_summary *summary)
ded3d3f8 224{
225 gcov_unsigned_t tag, length;
226 unsigned t_ix;
56621355 227 int f_ix = -1;
ded3d3f8 228 int error = 0;
229 struct gcov_fn_buffer **fn_tail = &fn_buffer;
ded3d3f8 230
231 length = gcov_read_unsigned ();
6401b74d 232 if (!gcov_version (gi_ptr, length, filename))
ded3d3f8 233 return -1;
234
235 length = gcov_read_unsigned ();
236 if (length != gi_ptr->stamp)
5522d934 237 {
238 /* Read from a different compilation. Overwrite the file. */
085a4ac7 239 gcov_error (GCOV_PROF_PREFIX "overwriting an existing profile data "
5522d934 240 "with a different timestamp\n", filename);
241 return 0;
242 }
ded3d3f8 243
56621355 244 tag = gcov_read_unsigned ();
245 if (tag != GCOV_TAG_OBJECT_SUMMARY)
246 goto read_mismatch;
247 length = gcov_read_unsigned ();
248 gcc_assert (length > 0);
249 gcov_read_summary (summary);
ded3d3f8 250
56621355 251 tag = gcov_read_unsigned ();
ded3d3f8 252 /* Merge execution counts for each function. */
253 for (f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions;
254 f_ix++, tag = gcov_read_unsigned ())
255 {
256 const struct gcov_ctr_info *ci_ptr;
257 const struct gcov_fn_info *gfi_ptr = gi_ptr->functions[f_ix];
258
259 if (tag != GCOV_TAG_FUNCTION)
260 goto read_mismatch;
261
262 length = gcov_read_unsigned ();
263 if (!length)
264 /* This function did not appear in the other program.
265 We have nothing to merge. */
266 continue;
267
268 if (length != GCOV_TAG_FUNCTION_LENGTH)
269 goto read_mismatch;
270
271 if (!gfi_ptr || gfi_ptr->key != gi_ptr)
272 {
273 /* This function appears in the other program. We
274 need to buffer the information in order to write
275 it back out -- we'll be inserting data before
276 this point, so cannot simply keep the data in the
277 file. */
6401b74d 278 fn_tail = buffer_fn_data (filename, gi_ptr, fn_tail, f_ix);
ded3d3f8 279 if (!fn_tail)
280 goto read_mismatch;
281 continue;
282 }
283
284 length = gcov_read_unsigned ();
285 if (length != gfi_ptr->ident)
286 goto read_mismatch;
287
288 length = gcov_read_unsigned ();
289 if (length != gfi_ptr->lineno_checksum)
290 goto read_mismatch;
291
292 length = gcov_read_unsigned ();
293 if (length != gfi_ptr->cfg_checksum)
294 goto read_mismatch;
295
296 ci_ptr = gfi_ptr->ctrs;
297 for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
298 {
299 gcov_merge_fn merge = gi_ptr->merge[t_ix];
300
301 if (!merge)
302 continue;
303
304 tag = gcov_read_unsigned ();
305 length = gcov_read_unsigned ();
306 if (tag != GCOV_TAG_FOR_COUNTER (t_ix)
307 || length != GCOV_TAG_COUNTER_LENGTH (ci_ptr->num))
308 goto read_mismatch;
309 (*merge) (ci_ptr->values, ci_ptr->num);
310 ci_ptr++;
311 }
312 if ((error = gcov_is_error ()))
313 goto read_error;
314 }
315
316 if (tag)
317 {
318 read_mismatch:;
085a4ac7 319 gcov_error (GCOV_PROF_PREFIX "Merge mismatch for %s %u\n",
6401b74d 320 filename, f_ix >= 0 ? "function" : "summary",
ded3d3f8 321 f_ix < 0 ? -1 - f_ix : f_ix);
322 return -1;
323 }
324 return 0;
325
326read_error:
085a4ac7 327 gcov_error (GCOV_PROF_PREFIX "%s merging\n", filename,
ded3d3f8 328 error < 0 ? "Overflow": "Error");
329 return -1;
330}
331
332/* Write counters in GI_PTR and the summary in PRG to a gcda file. In
333 the case of appending to an existing file, SUMMARY_POS will be non-zero.
334 We will write the file starting from SUMMAY_POS. */
335
336static void
1186f97a 337write_one_data (const struct gcov_info *gi_ptr,
56621355 338 const struct gcov_summary *prg_p)
ded3d3f8 339{
340 unsigned f_ix;
ded3d3f8 341
56621355 342 gcov_write_tag_length (GCOV_DATA_MAGIC, GCOV_VERSION);
343 gcov_write_unsigned (gi_ptr->stamp);
ded3d3f8 344
345 /* Generate whole program statistics. */
56621355 346 gcov_write_summary (GCOV_TAG_OBJECT_SUMMARY, prg_p);
ded3d3f8 347
348 /* Write execution counts for each function. */
349 for (f_ix = 0; f_ix != gi_ptr->n_functions; f_ix++)
350 {
351 unsigned buffered = 0;
352 const struct gcov_fn_info *gfi_ptr;
353 const struct gcov_ctr_info *ci_ptr;
354 gcov_unsigned_t length;
355 unsigned t_ix;
356
357 if (fn_buffer && fn_buffer->fn_ix == f_ix)
358 {
359 /* Buffered data from another program. */
360 buffered = 1;
361 gfi_ptr = &fn_buffer->info;
362 length = GCOV_TAG_FUNCTION_LENGTH;
363 }
364 else
365 {
366 gfi_ptr = gi_ptr->functions[f_ix];
367 if (gfi_ptr && gfi_ptr->key == gi_ptr)
368 length = GCOV_TAG_FUNCTION_LENGTH;
369 else
370 length = 0;
371 }
372
373 gcov_write_tag_length (GCOV_TAG_FUNCTION, length);
374 if (!length)
375 continue;
376
377 gcov_write_unsigned (gfi_ptr->ident);
378 gcov_write_unsigned (gfi_ptr->lineno_checksum);
379 gcov_write_unsigned (gfi_ptr->cfg_checksum);
380
381 ci_ptr = gfi_ptr->ctrs;
382 for (t_ix = 0; t_ix < GCOV_COUNTERS; t_ix++)
383 {
384 gcov_unsigned_t n_counts;
385 gcov_type *c_ptr;
386
387 if (!gi_ptr->merge[t_ix])
388 continue;
389
390 n_counts = ci_ptr->num;
391 gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix),
392 GCOV_TAG_COUNTER_LENGTH (n_counts));
393 c_ptr = ci_ptr->values;
394 while (n_counts--)
395 gcov_write_counter (*c_ptr++);
396 ci_ptr++;
397 }
398 if (buffered)
399 fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
400 }
401
402 gcov_write_unsigned (0);
403}
404
56621355 405/* Helper function for merging summary. */
ded3d3f8 406
56621355 407static void
408merge_summary (int run_counted, struct gcov_summary *summary,
409 gcov_type run_max)
ded3d3f8 410{
5860b185 411 if (!run_counted)
5860b185 412 {
56621355 413 summary->runs++;
414 summary->sum_max += run_max;
ded3d3f8 415 }
ded3d3f8 416}
417
418/* Dump the coverage counts for one gcov_info object. We merge with existing
419 counts when possible, to avoid growing the .da files ad infinitum. We use
420 this program's checksum to make sure we only accumulate whole program
421 statistics to the correct summary. An object file might be embedded
422 in two separate programs, and we must keep the two program
423 summaries separate. */
424
425static void
1186f97a 426dump_one_gcov (struct gcov_info *gi_ptr, struct gcov_filename *gf,
56621355 427 unsigned run_counted, gcov_type run_max)
ded3d3f8 428{
56621355 429 struct gcov_summary summary = {};
ded3d3f8 430 int error;
431 gcov_unsigned_t tag;
ded3d3f8 432
433 fn_buffer = 0;
ded3d3f8 434
435 error = gcov_exit_open_gcda_file (gi_ptr, gf);
436 if (error == -1)
437 return;
438
439 tag = gcov_read_unsigned ();
440 if (tag)
441 {
442 /* Merge data from file. */
443 if (tag != GCOV_DATA_MAGIC)
444 {
085a4ac7 445 gcov_error (GCOV_PROF_PREFIX "Not a gcov data file\n",
446 gf->filename);
ded3d3f8 447 goto read_fatal;
448 }
56621355 449 error = merge_one_data (gf->filename, gi_ptr, &summary);
ded3d3f8 450 if (error == -1)
451 goto read_fatal;
452 }
453
454 gcov_rewrite ();
455
56621355 456 merge_summary (run_counted, &summary, run_max);
ded3d3f8 457
56621355 458 write_one_data (gi_ptr, &summary);
ded3d3f8 459 /* fall through */
460
461read_fatal:;
462 while (fn_buffer)
463 fn_buffer = free_fn_data (gi_ptr, fn_buffer, GCOV_COUNTERS);
464
465 if ((error = gcov_close ()))
466 gcov_error (error < 0 ?
085a4ac7 467 GCOV_PROF_PREFIX "Overflow writing\n" :
468 GCOV_PROF_PREFIX "Error writing\n",
6401b74d 469 gf->filename);
ded3d3f8 470}
471
472
473/* Dump all the coverage counts for the program. It first computes program
474 summary and then traverses gcov_list list and dumps the gcov_info
475 objects one by one. */
476
859fa1a9 477#if !IN_GCOV_TOOL
478static
479#endif
480void
1186f97a 481gcov_do_dump (struct gcov_info *list, int run_counted)
ded3d3f8 482{
483 struct gcov_info *gi_ptr;
6401b74d 484 struct gcov_filename gf;
ded3d3f8 485
56621355 486 /* Compute run_max of this program run. */
487 gcov_type run_max = 0;
488 for (gi_ptr = list; gi_ptr; gi_ptr = gi_ptr->next)
489 for (unsigned f_ix = 0; (unsigned)f_ix != gi_ptr->n_functions; f_ix++)
490 {
491 const struct gcov_ctr_info *cinfo
492 = &gi_ptr->functions[f_ix]->ctrs[GCOV_COUNTER_ARCS];
493
494 for (unsigned i = 0; i < cinfo->num; i++)
495 if (run_max < cinfo->values[i])
496 run_max = cinfo->values[i];
497 }
ded3d3f8 498
499 allocate_filename_struct (&gf);
500
501 /* Now merge each file. */
1186f97a 502 for (gi_ptr = list; gi_ptr; gi_ptr = gi_ptr->next)
5b24ad4b 503 {
56621355 504 dump_one_gcov (gi_ptr, &gf, run_counted, run_max);
5b24ad4b 505 free (gf.filename);
506 }
ded3d3f8 507
5b24ad4b 508 free (gf.prefix);
ded3d3f8 509}
510
6db59335 511#if IN_GCOV_TOOL
512const char *
513__attribute__ ((unused))
514gcov_get_filename (struct gcov_info *list)
515{
516 return list->filename;
517}
518#endif
519
cb3ea3de 520#if !IN_GCOV_TOOL
1186f97a 521void
859fa1a9 522__gcov_dump_one (struct gcov_root *root)
1186f97a 523{
859fa1a9 524 if (root->dumped)
1186f97a 525 return;
526
859fa1a9 527 gcov_do_dump (root->list, root->run_counted);
1186f97a 528
859fa1a9 529 root->dumped = 1;
530 root->run_counted = 1;
1186f97a 531}
532
bc587267 533/* Per-dynamic-object gcov state. */
859fa1a9 534struct gcov_root __gcov_root;
ded3d3f8 535
bc587267 536/* Exactly one of these will be live in the process image. */
537struct gcov_master __gcov_master =
538 {GCOV_VERSION, 0};
539
db46bcd3 540void
541__gcov_exit (void)
ded3d3f8 542{
859fa1a9 543 __gcov_dump_one (&__gcov_root);
bc587267 544 if (__gcov_root.next)
545 __gcov_root.next->prev = __gcov_root.prev;
546 if (__gcov_root.prev)
547 __gcov_root.prev->next = __gcov_root.next;
548 else
549 __gcov_master.root = __gcov_root.next;
3237fc56 550
551 gcov_error_exit ();
ded3d3f8 552}
553
554/* Add a new object file onto the bb chain. Invoked automatically
555 when running an object file's global ctors. */
556
557void
558__gcov_init (struct gcov_info *info)
559{
560 if (!info->version || !info->n_functions)
561 return;
562 if (gcov_version (info, info->version, 0))
563 {
859fa1a9 564 if (!__gcov_root.list)
bc587267 565 {
566 /* Add to master list and at exit function. */
567 if (gcov_version (NULL, __gcov_master.version, "<master>"))
568 {
569 __gcov_root.next = __gcov_master.root;
570 if (__gcov_master.root)
571 __gcov_master.root->prev = &__gcov_root;
572 __gcov_master.root = &__gcov_root;
573 }
bc587267 574 }
ded3d3f8 575
859fa1a9 576 info->next = __gcov_root.list;
577 __gcov_root.list = info;
ded3d3f8 578 }
ded3d3f8 579}
cb3ea3de 580#endif /* !IN_GCOV_TOOL */
ded3d3f8 581#endif /* L_gcov */
582#endif /* inhibit_libc */