1 /* Routines required for instrumenting a program. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 1989-2020 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
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
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/>. */
29 #if defined(inhibit_libc)
30 /* If libc and its header files are not available, provide dummy functions. */
33 void __gcov_init (struct gcov_info
*p
__attribute__ ((unused
))) {}
36 #else /* inhibit_libc */
47 /* A utility function for outputting errors. */
48 static int gcov_error (const char *, ...);
51 static void gcov_error_exit (void);
56 #define GCOV_PROF_PREFIX "libgcov profiling error:%s:"
60 struct gcov_fn_buffer
*next
;
62 struct gcov_fn_info info
;
63 /* note gcov_fn_info ends in a trailing array. */
66 struct gcov_summary_buffer
68 struct gcov_summary_buffer
*next
;
69 struct gcov_summary summary
;
72 /* A struct that bundles all the related information about the
77 char *filename
; /* filename buffer */
78 int strip
; /* leading chars to strip from filename */
79 char *prefix
; /* prefix string */
82 static struct gcov_fn_buffer
*
83 free_fn_data (const struct gcov_info
*gi_ptr
, struct gcov_fn_buffer
*buffer
,
86 struct gcov_fn_buffer
*next
;
87 unsigned ix
, n_ctr
= 0;
93 for (ix
= 0; ix
!= limit
; ix
++)
94 if (gi_ptr
->merge
[ix
])
95 free (buffer
->info
.ctrs
[n_ctr
++].values
);
100 static struct gcov_fn_buffer
**
101 buffer_fn_data (const char *filename
, const struct gcov_info
*gi_ptr
,
102 struct gcov_fn_buffer
**end_ptr
, unsigned fn_ix
)
104 unsigned n_ctrs
= 0, ix
= 0;
105 struct gcov_fn_buffer
*fn_buffer
;
108 for (ix
= GCOV_COUNTERS
; ix
--;)
109 if (gi_ptr
->merge
[ix
])
112 len
= sizeof (*fn_buffer
) + sizeof (fn_buffer
->info
.ctrs
[0]) * n_ctrs
;
113 fn_buffer
= (struct gcov_fn_buffer
*) xmalloc (len
);
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 ();
124 for (n_ctrs
= ix
= 0; ix
!= GCOV_COUNTERS
; ix
++)
126 gcov_unsigned_t length
;
129 if (!gi_ptr
->merge
[ix
])
132 if (gcov_read_unsigned () != GCOV_TAG_FOR_COUNTER (ix
))
138 length
= GCOV_TAG_COUNTER_NUM (gcov_read_unsigned ());
139 len
= length
* sizeof (gcov_type
);
140 values
= (gcov_type
*) xmalloc (len
);
144 fn_buffer
->info
.ctrs
[n_ctrs
].num
= length
;
145 fn_buffer
->info
.ctrs
[n_ctrs
].values
= values
;
148 *values
++ = gcov_read_counter ();
152 *end_ptr
= fn_buffer
;
153 return &fn_buffer
->next
;
156 gcov_error (GCOV_PROF_PREFIX
"Function %u %s %u \n", filename
, fn_ix
,
157 len
? "cannot allocate" : "counter mismatch", len
? len
: ix
);
159 return (struct gcov_fn_buffer
**)free_fn_data (gi_ptr
, fn_buffer
, ix
);
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. */
167 gcov_version_string (char *buffer
, char version
[4])
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)");
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");
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. */
189 gcov_version (struct gcov_info
*ptr
, gcov_unsigned_t version
,
190 const char *filename
)
192 if (version
!= GCOV_VERSION
)
195 char version_string
[128], expected_string
[128];
197 GCOV_UNSIGNED2STRING (v
, version
);
198 GCOV_UNSIGNED2STRING (e
, GCOV_VERSION
);
200 gcov_error (GCOV_PROF_PREFIX
"Version mismatch - expected %s (%.4s) "
202 filename
? filename
: ptr
->filename
,
203 gcov_version_string (expected_string
, e
), e
,
204 gcov_version_string (version_string
, v
), v
);
210 /* buffer for the fn_data from another program. */
211 static struct gcov_fn_buffer
*fn_buffer
;
213 /* Including system dependent components. */
214 #include "libgcov-driver-system.c"
216 /* This function merges counters in GI_PTR to an existing gcda file.
218 Return -1 on error. In this case, caller will goto read_fatal. */
221 merge_one_data (const char *filename
,
222 struct gcov_info
*gi_ptr
,
223 struct gcov_summary
*summary
)
225 gcov_unsigned_t tag
, length
;
229 struct gcov_fn_buffer
**fn_tail
= &fn_buffer
;
231 length
= gcov_read_unsigned ();
232 if (!gcov_version (gi_ptr
, length
, filename
))
235 length
= gcov_read_unsigned ();
236 if (length
!= gi_ptr
->stamp
)
238 /* Read from a different compilation. Overwrite the file. */
239 gcov_error (GCOV_PROF_PREFIX
"overwriting an existing profile data "
240 "with a different timestamp\n", filename
);
244 tag
= gcov_read_unsigned ();
245 if (tag
!= GCOV_TAG_OBJECT_SUMMARY
)
247 length
= gcov_read_unsigned ();
248 gcc_assert (length
> 0);
249 gcov_read_summary (summary
);
251 tag
= gcov_read_unsigned ();
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 ())
256 const struct gcov_ctr_info
*ci_ptr
;
257 const struct gcov_fn_info
*gfi_ptr
= gi_ptr
->functions
[f_ix
];
259 if (tag
!= GCOV_TAG_FUNCTION
)
262 length
= gcov_read_unsigned ();
264 /* This function did not appear in the other program.
265 We have nothing to merge. */
268 if (length
!= GCOV_TAG_FUNCTION_LENGTH
)
271 if (!gfi_ptr
|| gfi_ptr
->key
!= gi_ptr
)
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
278 fn_tail
= buffer_fn_data (filename
, gi_ptr
, fn_tail
, f_ix
);
284 length
= gcov_read_unsigned ();
285 if (length
!= gfi_ptr
->ident
)
288 length
= gcov_read_unsigned ();
289 if (length
!= gfi_ptr
->lineno_checksum
)
292 length
= gcov_read_unsigned ();
293 if (length
!= gfi_ptr
->cfg_checksum
)
296 ci_ptr
= gfi_ptr
->ctrs
;
297 for (t_ix
= 0; t_ix
< GCOV_COUNTERS
; t_ix
++)
299 gcov_merge_fn merge
= gi_ptr
->merge
[t_ix
];
304 tag
= gcov_read_unsigned ();
305 int read_length
= (int)gcov_read_unsigned ();
306 length
= abs (read_length
);
307 if (tag
!= GCOV_TAG_FOR_COUNTER (t_ix
)
308 || (length
!= GCOV_TAG_COUNTER_LENGTH (ci_ptr
->num
)
309 && t_ix
!= GCOV_COUNTER_V_TOPN
310 && t_ix
!= GCOV_COUNTER_V_INDIR
))
312 /* Merging with all zero counters does not make sense. */
314 (*merge
) (ci_ptr
->values
, ci_ptr
->num
);
317 if ((error
= gcov_is_error ()))
324 gcov_error (GCOV_PROF_PREFIX
"Merge mismatch for %s %u\n",
325 filename
, f_ix
>= 0 ? "function" : "summary",
326 f_ix
< 0 ? -1 - f_ix
: f_ix
);
332 gcov_error (GCOV_PROF_PREFIX
"%s merging\n", filename
,
333 error
< 0 ? "Overflow": "Error");
337 /* Store all TOP N counters where each has a dynamic length. */
340 write_top_counters (const struct gcov_ctr_info
*ci_ptr
,
342 gcov_unsigned_t n_counts
)
344 unsigned counters
= n_counts
/ GCOV_TOPN_MEM_COUNTERS
;
345 gcc_assert (n_counts
% GCOV_TOPN_MEM_COUNTERS
== 0);
346 unsigned pair_total
= 0;
347 for (unsigned i
= 0; i
< counters
; i
++)
348 pair_total
+= ci_ptr
->values
[GCOV_TOPN_MEM_COUNTERS
* i
+ 1];
349 unsigned disk_size
= GCOV_TOPN_DISK_COUNTERS
* counters
+ 2 * pair_total
;
350 gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix
),
351 GCOV_TAG_COUNTER_LENGTH (disk_size
));
353 for (unsigned i
= 0; i
< counters
; i
++)
355 gcov_type pair_count
= ci_ptr
->values
[GCOV_TOPN_MEM_COUNTERS
* i
+ 1];
356 gcov_write_counter (ci_ptr
->values
[GCOV_TOPN_MEM_COUNTERS
* i
]);
357 gcov_write_counter (pair_count
);
358 gcov_type start
= ci_ptr
->values
[GCOV_TOPN_MEM_COUNTERS
* i
+ 2];
359 for (struct gcov_kvp
*node
= (struct gcov_kvp
*)(intptr_t)start
;
360 node
!= NULL
; node
= node
->next
)
362 gcov_write_counter (node
->value
);
363 gcov_write_counter (node
->count
);
368 /* Write counters in GI_PTR and the summary in PRG to a gcda file. In
369 the case of appending to an existing file, SUMMARY_POS will be non-zero.
370 We will write the file starting from SUMMAY_POS. */
373 write_one_data (const struct gcov_info
*gi_ptr
,
374 const struct gcov_summary
*prg_p
)
378 gcov_write_tag_length (GCOV_DATA_MAGIC
, GCOV_VERSION
);
379 gcov_write_unsigned (gi_ptr
->stamp
);
381 /* Generate whole program statistics. */
382 gcov_write_summary (GCOV_TAG_OBJECT_SUMMARY
, prg_p
);
384 /* Write execution counts for each function. */
385 for (f_ix
= 0; f_ix
!= gi_ptr
->n_functions
; f_ix
++)
387 unsigned buffered
= 0;
388 const struct gcov_fn_info
*gfi_ptr
;
389 const struct gcov_ctr_info
*ci_ptr
;
390 gcov_unsigned_t length
;
393 if (fn_buffer
&& fn_buffer
->fn_ix
== f_ix
)
395 /* Buffered data from another program. */
397 gfi_ptr
= &fn_buffer
->info
;
398 length
= GCOV_TAG_FUNCTION_LENGTH
;
402 gfi_ptr
= gi_ptr
->functions
[f_ix
];
403 if (gfi_ptr
&& gfi_ptr
->key
== gi_ptr
)
404 length
= GCOV_TAG_FUNCTION_LENGTH
;
409 gcov_write_tag_length (GCOV_TAG_FUNCTION
, length
);
413 gcov_write_unsigned (gfi_ptr
->ident
);
414 gcov_write_unsigned (gfi_ptr
->lineno_checksum
);
415 gcov_write_unsigned (gfi_ptr
->cfg_checksum
);
417 ci_ptr
= gfi_ptr
->ctrs
;
418 for (t_ix
= 0; t_ix
< GCOV_COUNTERS
; t_ix
++)
420 gcov_position_t n_counts
;
422 if (!gi_ptr
->merge
[t_ix
])
425 n_counts
= ci_ptr
->num
;
427 if (gi_ptr
->merge
[t_ix
] == __gcov_merge_topn
)
428 write_top_counters (ci_ptr
, t_ix
, n_counts
);
431 /* Do not stream when all counters are zero. */
433 for (unsigned i
= 0; i
< n_counts
; i
++)
434 if (ci_ptr
->values
[i
] != 0)
441 gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix
),
442 GCOV_TAG_COUNTER_LENGTH (-n_counts
));
445 gcov_write_tag_length (GCOV_TAG_FOR_COUNTER (t_ix
),
446 GCOV_TAG_COUNTER_LENGTH (n_counts
));
447 for (unsigned i
= 0; i
< n_counts
; i
++)
448 gcov_write_counter (ci_ptr
->values
[i
]);
455 fn_buffer
= free_fn_data (gi_ptr
, fn_buffer
, GCOV_COUNTERS
);
458 gcov_write_unsigned (0);
461 /* Dump the coverage counts for one gcov_info object. We merge with existing
462 counts when possible, to avoid growing the .da files ad infinitum. We use
463 this program's checksum to make sure we only accumulate whole program
464 statistics to the correct summary. An object file might be embedded
465 in two separate programs, and we must keep the two program
466 summaries separate. */
469 dump_one_gcov (struct gcov_info
*gi_ptr
, struct gcov_filename
*gf
,
470 unsigned run_counted ATTRIBUTE_UNUSED
,
471 gcov_type run_max ATTRIBUTE_UNUSED
)
473 struct gcov_summary summary
= {};
478 error
= gcov_exit_open_gcda_file (gi_ptr
, gf
);
482 tag
= gcov_read_unsigned ();
485 /* Merge data from file. */
486 if (tag
!= GCOV_DATA_MAGIC
)
488 gcov_error (GCOV_PROF_PREFIX
"Not a gcov data file\n",
492 error
= merge_one_data (gf
->filename
, gi_ptr
, &summary
);
503 summary
.sum_max
+= run_max
;
506 summary
= gi_ptr
->summary
;
509 write_one_data (gi_ptr
, &summary
);
514 fn_buffer
= free_fn_data (gi_ptr
, fn_buffer
, GCOV_COUNTERS
);
516 if ((error
= gcov_close ()))
517 gcov_error (error
< 0 ?
518 GCOV_PROF_PREFIX
"Overflow writing\n" :
519 GCOV_PROF_PREFIX
"Error writing\n",
524 /* Dump all the coverage counts for the program. It first computes program
525 summary and then traverses gcov_list list and dumps the gcov_info
526 objects one by one. */
532 gcov_do_dump (struct gcov_info
*list
, int run_counted
)
534 struct gcov_info
*gi_ptr
;
535 struct gcov_filename gf
;
537 /* Compute run_max of this program run. */
538 gcov_type run_max
= 0;
539 for (gi_ptr
= list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
540 for (unsigned f_ix
= 0; (unsigned)f_ix
!= gi_ptr
->n_functions
; f_ix
++)
542 const struct gcov_ctr_info
*cinfo
543 = &gi_ptr
->functions
[f_ix
]->ctrs
[GCOV_COUNTER_ARCS
];
545 for (unsigned i
= 0; i
< cinfo
->num
; i
++)
546 if (run_max
< cinfo
->values
[i
])
547 run_max
= cinfo
->values
[i
];
550 allocate_filename_struct (&gf
);
552 /* Now merge each file. */
553 for (gi_ptr
= list
; gi_ptr
; gi_ptr
= gi_ptr
->next
)
555 dump_one_gcov (gi_ptr
, &gf
, run_counted
, run_max
);
564 __attribute__ ((unused
))
565 gcov_get_filename (struct gcov_info
*list
)
567 return list
->filename
;
573 __gcov_dump_one (struct gcov_root
*root
)
578 gcov_do_dump (root
->list
, root
->run_counted
);
581 root
->run_counted
= 1;
584 /* Per-dynamic-object gcov state. */
585 struct gcov_root __gcov_root
;
587 /* Exactly one of these will be live in the process image. */
588 struct gcov_master __gcov_master
=
594 __gcov_dump_one (&__gcov_root
);
595 if (__gcov_root
.next
)
596 __gcov_root
.next
->prev
= __gcov_root
.prev
;
597 if (__gcov_root
.prev
)
598 __gcov_root
.prev
->next
= __gcov_root
.next
;
600 __gcov_master
.root
= __gcov_root
.next
;
605 /* Add a new object file onto the bb chain. Invoked automatically
606 when running an object file's global ctors. */
609 __gcov_init (struct gcov_info
*info
)
611 if (!info
->version
|| !info
->n_functions
)
613 if (gcov_version (info
, info
->version
, 0))
615 if (!__gcov_root
.list
)
617 /* Add to master list and at exit function. */
618 if (gcov_version (NULL
, __gcov_master
.version
, "<master>"))
620 __gcov_root
.next
= __gcov_master
.root
;
621 if (__gcov_master
.root
)
622 __gcov_master
.root
->prev
= &__gcov_root
;
623 __gcov_master
.root
= &__gcov_root
;
627 info
->next
= __gcov_root
.list
;
628 __gcov_root
.list
= info
;
631 #endif /* !IN_GCOV_TOOL */
633 #endif /* inhibit_libc */