]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgcc/libgcov.h
Daily bump.
[thirdparty/gcc.git] / libgcc / libgcov.h
1 /* Header file for libgcov-*.c.
2 Copyright (C) 1996-2021 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
19
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
24
25 #ifndef GCC_LIBGCOV_H
26 #define GCC_LIBGCOV_H
27
28 /* work around the poisoned malloc/calloc in system.h. */
29 #ifndef xmalloc
30 #define xmalloc malloc
31 #endif
32 #ifndef xcalloc
33 #define xcalloc calloc
34 #endif
35
36 #ifndef IN_GCOV_TOOL
37 /* About the target. */
38 /* This path will be used by libgcov runtime. */
39
40 #include "tconfig.h"
41 #include "auto-target.h"
42 #include "tsystem.h"
43 #include "coretypes.h"
44 #include "tm.h"
45 #include "libgcc_tm.h"
46 #include "gcov.h"
47
48 #if HAVE_SYS_MMAN_H
49 #include <sys/mman.h>
50 #endif
51
52 #if __CHAR_BIT__ == 8
53 typedef unsigned gcov_unsigned_t __attribute__ ((mode (SI)));
54 typedef unsigned gcov_position_t __attribute__ ((mode (SI)));
55 #if LONG_LONG_TYPE_SIZE > 32
56 typedef signed gcov_type __attribute__ ((mode (DI)));
57 typedef unsigned gcov_type_unsigned __attribute__ ((mode (DI)));
58 #else
59 typedef signed gcov_type __attribute__ ((mode (SI)));
60 typedef unsigned gcov_type_unsigned __attribute__ ((mode (SI)));
61 #endif
62 #else
63 #if __CHAR_BIT__ == 16
64 typedef unsigned gcov_unsigned_t __attribute__ ((mode (HI)));
65 typedef unsigned gcov_position_t __attribute__ ((mode (HI)));
66 #if LONG_LONG_TYPE_SIZE > 32
67 typedef signed gcov_type __attribute__ ((mode (SI)));
68 typedef unsigned gcov_type_unsigned __attribute__ ((mode (SI)));
69 #else
70 typedef signed gcov_type __attribute__ ((mode (HI)));
71 typedef unsigned gcov_type_unsigned __attribute__ ((mode (HI)));
72 #endif
73 #else
74 typedef unsigned gcov_unsigned_t __attribute__ ((mode (QI)));
75 typedef unsigned gcov_position_t __attribute__ ((mode (QI)));
76 #if LONG_LONG_TYPE_SIZE > 32
77 typedef signed gcov_type __attribute__ ((mode (HI)));
78 typedef unsigned gcov_type_unsigned __attribute__ ((mode (HI)));
79 #else
80 typedef signed gcov_type __attribute__ ((mode (QI)));
81 typedef unsigned gcov_type_unsigned __attribute__ ((mode (QI)));
82 #endif
83 #endif
84 #endif
85
86 #if defined (TARGET_POSIX_IO)
87 #define GCOV_LOCKED 1
88 #else
89 #define GCOV_LOCKED 0
90 #endif
91
92 #if defined (__MSVCRT__)
93 #define GCOV_LOCKED_WITH_LOCKING 1
94 #else
95 #define GCOV_LOCKED_WITH_LOCKING 0
96 #endif
97
98 #ifndef GCOV_SUPPORTS_ATOMIC
99 /* Detect whether target can support atomic update of profilers. */
100 #if __SIZEOF_LONG_LONG__ == 4 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
101 #define GCOV_SUPPORTS_ATOMIC 1
102 #else
103 #if __SIZEOF_LONG_LONG__ == 8 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
104 #define GCOV_SUPPORTS_ATOMIC 1
105 #else
106 #define GCOV_SUPPORTS_ATOMIC 0
107 #endif
108 #endif
109 #endif
110
111 /* In libgcov we need these functions to be extern, so prefix them with
112 __gcov. In libgcov they must also be hidden so that the instance in
113 the executable is not also used in a DSO. */
114 #define gcov_var __gcov_var
115 #define gcov_open __gcov_open
116 #define gcov_close __gcov_close
117 #define gcov_write_tag_length __gcov_write_tag_length
118 #define gcov_position __gcov_position
119 #define gcov_seek __gcov_seek
120 #define gcov_rewrite __gcov_rewrite
121 #define gcov_is_error __gcov_is_error
122 #define gcov_write_unsigned __gcov_write_unsigned
123 #define gcov_write_counter __gcov_write_counter
124 #define gcov_write_summary __gcov_write_summary
125 #define gcov_read_unsigned __gcov_read_unsigned
126 #define gcov_read_counter __gcov_read_counter
127 #define gcov_read_summary __gcov_read_summary
128
129 #else /* IN_GCOV_TOOL */
130 /* About the host. */
131 /* This path will be compiled for the host and linked into
132 gcov-tool binary. */
133
134 #include "config.h"
135 #include "system.h"
136 #include "coretypes.h"
137 #include "tm.h"
138
139 typedef unsigned gcov_unsigned_t;
140 typedef unsigned gcov_position_t;
141 /* gcov_type is typedef'd elsewhere for the compiler */
142
143 #if defined (HOST_HAS_F_SETLKW)
144 #define GCOV_LOCKED 1
145 #else
146 #define GCOV_LOCKED 0
147 #endif
148
149 #if defined (HOST_HAS_LK_LOCK)
150 #define GCOV_LOCKED_WITH_LOCKING 1
151 #else
152 #define GCOV_LOCKED_WITH_LOCKING 0
153 #endif
154
155 /* Some Macros specific to gcov-tool. */
156
157 #define L_gcov 1
158 #define L_gcov_merge_add 1
159 #define L_gcov_merge_topn 1
160 #define L_gcov_merge_ior 1
161 #define L_gcov_merge_time_profile 1
162
163 extern gcov_type gcov_read_counter_mem ();
164 extern unsigned gcov_get_merge_weight ();
165 extern struct gcov_info *gcov_list;
166
167 #endif /* !IN_GCOV_TOOL */
168
169 #if defined(inhibit_libc)
170 #define IN_LIBGCOV (-1)
171 #else
172 #define IN_LIBGCOV 1
173 #if defined(L_gcov)
174 #define GCOV_LINKAGE /* nothing */
175 #endif
176 #endif
177
178 /* Poison these, so they don't accidentally slip in. */
179 #pragma GCC poison gcov_write_string gcov_write_tag gcov_write_length
180 #pragma GCC poison gcov_time
181
182 #ifdef HAVE_GAS_HIDDEN
183 #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
184 #else
185 #define ATTRIBUTE_HIDDEN
186 #endif
187
188 #if HAVE_SYS_MMAN_H
189 #ifndef MAP_FAILED
190 #define MAP_FAILED ((void *)-1)
191 #endif
192
193 #if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
194 #define MAP_ANONYMOUS MAP_ANON
195 #endif
196 #endif
197
198 #include "gcov-io.h"
199
200 /* Structures embedded in coveraged program. The structures generated
201 by write_profile must match these. */
202
203 /* Information about counters for a single function. */
204 struct gcov_ctr_info
205 {
206 gcov_unsigned_t num; /* number of counters. */
207 gcov_type *values; /* their values. */
208 };
209
210 /* Information about a single function. This uses the trailing array
211 idiom. The number of counters is determined from the merge pointer
212 array in gcov_info. The key is used to detect which of a set of
213 comdat functions was selected -- it points to the gcov_info object
214 of the object file containing the selected comdat function. */
215
216 struct gcov_fn_info
217 {
218 const struct gcov_info *key; /* comdat key */
219 gcov_unsigned_t ident; /* unique ident of function */
220 gcov_unsigned_t lineno_checksum; /* function lineo_checksum */
221 gcov_unsigned_t cfg_checksum; /* function cfg checksum */
222 struct gcov_ctr_info ctrs[1]; /* instrumented counters */
223 };
224
225 /* Type of function used to merge counters. */
226 typedef void (*gcov_merge_fn) (gcov_type *, gcov_unsigned_t);
227
228 /* Information about a single object file. */
229 struct gcov_info
230 {
231 gcov_unsigned_t version; /* expected version number */
232 struct gcov_info *next; /* link to next, used by libgcov */
233
234 gcov_unsigned_t stamp; /* uniquifying time stamp */
235 const char *filename; /* output file name */
236
237 gcov_merge_fn merge[GCOV_COUNTERS]; /* merge functions (null for
238 unused) */
239
240 unsigned n_functions; /* number of functions */
241
242 #ifndef IN_GCOV_TOOL
243 const struct gcov_fn_info *const *functions; /* pointer to pointers
244 to function information */
245 #else
246 struct gcov_fn_info **functions;
247 struct gcov_summary summary;
248 #endif /* !IN_GCOV_TOOL */
249 };
250
251 /* Root of a program/shared-object state */
252 struct gcov_root
253 {
254 struct gcov_info *list;
255 unsigned dumped : 1; /* counts have been dumped. */
256 unsigned run_counted : 1; /* run has been accounted for. */
257 struct gcov_root *next;
258 struct gcov_root *prev;
259 };
260
261 extern struct gcov_root __gcov_root ATTRIBUTE_HIDDEN;
262
263 struct gcov_master
264 {
265 gcov_unsigned_t version;
266 struct gcov_root *root;
267 };
268
269 struct indirect_call_tuple
270 {
271 /* Callee function. */
272 void *callee;
273
274 /* Pointer to counters. */
275 gcov_type *counters;
276 };
277
278 /* Exactly one of these will be active in the process. */
279 extern struct gcov_master __gcov_master;
280 extern struct gcov_kvp *__gcov_kvp_dynamic_pool;
281 extern unsigned __gcov_kvp_dynamic_pool_index;
282 extern unsigned __gcov_kvp_dynamic_pool_size;
283
284 /* Dump a set of gcov objects. */
285 extern void __gcov_dump_one (struct gcov_root *) ATTRIBUTE_HIDDEN;
286
287 /* Register a new object file module. */
288 extern void __gcov_init (struct gcov_info *) ATTRIBUTE_HIDDEN;
289
290 /* GCOV exit function registered via a static destructor. */
291 extern void __gcov_exit (void) ATTRIBUTE_HIDDEN;
292
293 /* Function to reset all counters to 0. Both externally visible (and
294 overridable) and internal version. */
295 extern void __gcov_reset_int (void) ATTRIBUTE_HIDDEN;
296
297 /* User function to enable early write of profile information so far. */
298 extern void __gcov_dump_int (void) ATTRIBUTE_HIDDEN;
299
300 /* Lock critical section for __gcov_dump and __gcov_reset functions. */
301 extern void __gcov_lock (void) ATTRIBUTE_HIDDEN;
302
303 /* Unlock critical section for __gcov_dump and __gcov_reset functions. */
304 extern void __gcov_unlock (void) ATTRIBUTE_HIDDEN;
305
306 /* The merge function that just sums the counters. */
307 extern void __gcov_merge_add (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
308
309 /* The merge function to select the minimum valid counter value. */
310 extern void __gcov_merge_time_profile (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
311
312 /* The merge function to choose the most common N values. */
313 extern void __gcov_merge_topn (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
314
315 /* The merge function that just ors the counters together. */
316 extern void __gcov_merge_ior (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
317
318 /* The profiler functions. */
319 extern void __gcov_interval_profiler (gcov_type *, gcov_type, int, unsigned);
320 extern void __gcov_interval_profiler_atomic (gcov_type *, gcov_type, int,
321 unsigned);
322 extern void __gcov_pow2_profiler (gcov_type *, gcov_type);
323 extern void __gcov_pow2_profiler_atomic (gcov_type *, gcov_type);
324 extern void __gcov_topn_values_profiler (gcov_type *, gcov_type);
325 extern void __gcov_topn_values_profiler_atomic (gcov_type *, gcov_type);
326 extern void __gcov_indirect_call_profiler_v4 (gcov_type, void *);
327 extern void __gcov_indirect_call_profiler_v4_atomic (gcov_type, void *);
328 extern void __gcov_time_profiler (gcov_type *);
329 extern void __gcov_time_profiler_atomic (gcov_type *);
330 extern void __gcov_average_profiler (gcov_type *, gcov_type);
331 extern void __gcov_average_profiler_atomic (gcov_type *, gcov_type);
332 extern void __gcov_ior_profiler (gcov_type *, gcov_type);
333 extern void __gcov_ior_profiler_atomic (gcov_type *, gcov_type);
334
335 #ifndef inhibit_libc
336 /* The wrappers around some library functions.. */
337 extern pid_t __gcov_fork (void) ATTRIBUTE_HIDDEN;
338 extern int __gcov_execl (const char *, char *, ...) ATTRIBUTE_HIDDEN;
339 extern int __gcov_execlp (const char *, char *, ...) ATTRIBUTE_HIDDEN;
340 extern int __gcov_execle (const char *, char *, ...) ATTRIBUTE_HIDDEN;
341 extern int __gcov_execv (const char *, char *const []) ATTRIBUTE_HIDDEN;
342 extern int __gcov_execvp (const char *, char *const []) ATTRIBUTE_HIDDEN;
343 extern int __gcov_execve (const char *, char *const [], char *const [])
344 ATTRIBUTE_HIDDEN;
345
346 /* Functions that only available in libgcov. */
347 GCOV_LINKAGE int gcov_open (const char */*name*/) ATTRIBUTE_HIDDEN;
348 GCOV_LINKAGE void gcov_write_counter (gcov_type) ATTRIBUTE_HIDDEN;
349 GCOV_LINKAGE void gcov_write_tag_length (gcov_unsigned_t, gcov_unsigned_t)
350 ATTRIBUTE_HIDDEN;
351 GCOV_LINKAGE void gcov_write_summary (gcov_unsigned_t /*tag*/,
352 const struct gcov_summary *)
353 ATTRIBUTE_HIDDEN;
354 GCOV_LINKAGE void gcov_seek (gcov_position_t /*position*/) ATTRIBUTE_HIDDEN;
355 GCOV_LINKAGE void gcov_rewrite (void) ATTRIBUTE_HIDDEN;
356
357 /* "Counts" stored in gcda files can be a real counter value, or
358 an target address. When differentiate these two types because
359 when manipulating counts, we should only change real counter values,
360 rather target addresses. */
361
362 static inline gcov_type
363 gcov_get_counter (void)
364 {
365 #ifndef IN_GCOV_TOOL
366 /* This version is for reading count values in libgcov runtime:
367 we read from gcda files. */
368
369 return gcov_read_counter ();
370 #else
371 /* This version is for gcov-tool. We read the value from memory and
372 multiply it by the merge weight. */
373
374 return gcov_read_counter_mem () * gcov_get_merge_weight ();
375 #endif
376 }
377
378 /* Similar function as gcov_get_counter(), but do not scale
379 when read value is equal to IGNORE_SCALING. */
380
381 static inline gcov_type
382 gcov_get_counter_ignore_scaling (gcov_type ignore_scaling ATTRIBUTE_UNUSED)
383 {
384 #ifndef IN_GCOV_TOOL
385 /* This version is for reading count values in libgcov runtime:
386 we read from gcda files. */
387
388 return gcov_read_counter ();
389 #else
390 /* This version is for gcov-tool. We read the value from memory and
391 multiply it by the merge weight. */
392
393 gcov_type v = gcov_read_counter_mem ();
394 if (v != ignore_scaling)
395 v *= gcov_get_merge_weight ();
396
397 return v;
398 #endif
399 }
400
401 /* Similar function as gcov_get_counter(), but handles target address
402 counters. */
403
404 static inline gcov_type
405 gcov_get_counter_target (void)
406 {
407 #ifndef IN_GCOV_TOOL
408 /* This version is for reading count target values in libgcov runtime:
409 we read from gcda files. */
410
411 return gcov_read_counter ();
412 #else
413 /* This version is for gcov-tool. We read the value from memory and we do NOT
414 multiply it by the merge weight. */
415
416 return gcov_read_counter_mem ();
417 #endif
418 }
419
420 /* Add VALUE to *COUNTER and make it with atomic operation
421 if USE_ATOMIC is true. */
422
423 static inline void
424 gcov_counter_add (gcov_type *counter, gcov_type value,
425 int use_atomic ATTRIBUTE_UNUSED)
426 {
427 #if GCOV_SUPPORTS_ATOMIC
428 if (use_atomic)
429 __atomic_fetch_add (counter, value, __ATOMIC_RELAXED);
430 else
431 #endif
432 *counter += value;
433 }
434
435 #if HAVE_SYS_MMAN_H
436
437 /* Allocate LENGTH with mmap function. */
438
439 static inline void *
440 malloc_mmap (size_t length)
441 {
442 return mmap (NULL, length, PROT_READ | PROT_WRITE,
443 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
444 }
445
446 #endif
447
448 /* Allocate gcov_kvp from statically pre-allocated pool,
449 or use heap otherwise. */
450
451 static inline struct gcov_kvp *
452 allocate_gcov_kvp (void)
453 {
454 #define MMAP_CHUNK_SIZE (128 * 1024)
455 struct gcov_kvp *new_node = NULL;
456 unsigned kvp_sizeof = sizeof(struct gcov_kvp);
457
458 /* Try mmaped pool if available. */
459 #if !defined(IN_GCOV_TOOL) && !defined(L_gcov_merge_topn) && HAVE_SYS_MMAN_H
460 if (__gcov_kvp_dynamic_pool == NULL
461 || __gcov_kvp_dynamic_pool_index >= __gcov_kvp_dynamic_pool_size)
462 {
463 void *ptr = malloc_mmap (MMAP_CHUNK_SIZE);
464 if (ptr != MAP_FAILED)
465 {
466 __gcov_kvp_dynamic_pool = ptr;
467 __gcov_kvp_dynamic_pool_size = MMAP_CHUNK_SIZE / kvp_sizeof;
468 __gcov_kvp_dynamic_pool_index = 0;
469 }
470 }
471
472 if (__gcov_kvp_dynamic_pool != NULL)
473 {
474 unsigned index;
475 #if GCOV_SUPPORTS_ATOMIC
476 index
477 = __atomic_fetch_add (&__gcov_kvp_dynamic_pool_index, 1,
478 __ATOMIC_RELAXED);
479 #else
480 index = __gcov_kvp_dynamic_pool_index++;
481 #endif
482 if (index < __gcov_kvp_dynamic_pool_size)
483 new_node = __gcov_kvp_dynamic_pool + index;
484 }
485 #endif
486
487 /* Fallback to malloc. */
488 if (new_node == NULL)
489 new_node = (struct gcov_kvp *)xcalloc (1, kvp_sizeof);
490
491 return new_node;
492 }
493
494 /* Add key value pair VALUE:COUNT to a top N COUNTERS. When INCREMENT_TOTAL
495 is true, add COUNT to total of the TOP counter. If USE_ATOMIC is true,
496 do it in atomic way. Return true when the counter is full, otherwise
497 return false. */
498
499 static inline unsigned
500 gcov_topn_add_value (gcov_type *counters, gcov_type value, gcov_type count,
501 int use_atomic, int increment_total)
502 {
503 if (increment_total)
504 {
505 /* In the multi-threaded mode, we can have an already merged profile
506 with a negative total value. In that case, we should bail out. */
507 if (counters[0] < 0)
508 return 0;
509 gcov_counter_add (&counters[0], 1, use_atomic);
510 }
511
512 struct gcov_kvp *prev_node = NULL;
513 struct gcov_kvp *minimal_node = NULL;
514 struct gcov_kvp *current_node = (struct gcov_kvp *)(intptr_t)counters[2];
515
516 while (current_node)
517 {
518 if (current_node->value == value)
519 {
520 gcov_counter_add (&current_node->count, count, use_atomic);
521 return 0;
522 }
523
524 if (minimal_node == NULL
525 || current_node->count < minimal_node->count)
526 minimal_node = current_node;
527
528 prev_node = current_node;
529 current_node = current_node->next;
530 }
531
532 if (counters[1] == GCOV_TOPN_MAXIMUM_TRACKED_VALUES)
533 {
534 if (--minimal_node->count < count)
535 {
536 minimal_node->value = value;
537 minimal_node->count = count;
538 }
539
540 return 1;
541 }
542 else
543 {
544 struct gcov_kvp *new_node = allocate_gcov_kvp ();
545 if (new_node == NULL)
546 return 0;
547
548 new_node->value = value;
549 new_node->count = count;
550
551 int success = 0;
552 if (!counters[2])
553 {
554 #if GCOV_SUPPORTS_ATOMIC
555 if (use_atomic)
556 {
557 struct gcov_kvp **ptr = (struct gcov_kvp **)(intptr_t)&counters[2];
558 success = !__sync_val_compare_and_swap (ptr, 0, new_node);
559 }
560 else
561 #endif
562 {
563 counters[2] = (intptr_t)new_node;
564 success = 1;
565 }
566 }
567 else if (prev_node && !prev_node->next)
568 {
569 #if GCOV_SUPPORTS_ATOMIC
570 if (use_atomic)
571 success = !__sync_val_compare_and_swap (&prev_node->next, 0,
572 new_node);
573 else
574 #endif
575 {
576 prev_node->next = new_node;
577 success = 1;
578 }
579 }
580
581 /* Increment number of nodes. */
582 if (success)
583 gcov_counter_add (&counters[1], 1, use_atomic);
584 }
585
586 return 0;
587 }
588
589 #endif /* !inhibit_libc */
590
591 #endif /* GCC_LIBGCOV_H */