xtensa: libgcc: fix PR target/95571
[gcc.git] / libgcc / libgcov.h
1 /* Header file for libgcov-*.c.
2 Copyright (C) 1996-2020 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 __CHAR_BIT__ == 8
49 typedef unsigned gcov_unsigned_t __attribute__ ((mode (SI)));
50 typedef unsigned gcov_position_t __attribute__ ((mode (SI)));
51 #if LONG_LONG_TYPE_SIZE > 32
52 typedef signed gcov_type __attribute__ ((mode (DI)));
53 typedef unsigned gcov_type_unsigned __attribute__ ((mode (DI)));
54 #else
55 typedef signed gcov_type __attribute__ ((mode (SI)));
56 typedef unsigned gcov_type_unsigned __attribute__ ((mode (SI)));
57 #endif
58 #else
59 #if __CHAR_BIT__ == 16
60 typedef unsigned gcov_unsigned_t __attribute__ ((mode (HI)));
61 typedef unsigned gcov_position_t __attribute__ ((mode (HI)));
62 #if LONG_LONG_TYPE_SIZE > 32
63 typedef signed gcov_type __attribute__ ((mode (SI)));
64 typedef unsigned gcov_type_unsigned __attribute__ ((mode (SI)));
65 #else
66 typedef signed gcov_type __attribute__ ((mode (HI)));
67 typedef unsigned gcov_type_unsigned __attribute__ ((mode (HI)));
68 #endif
69 #else
70 typedef unsigned gcov_unsigned_t __attribute__ ((mode (QI)));
71 typedef unsigned gcov_position_t __attribute__ ((mode (QI)));
72 #if LONG_LONG_TYPE_SIZE > 32
73 typedef signed gcov_type __attribute__ ((mode (HI)));
74 typedef unsigned gcov_type_unsigned __attribute__ ((mode (HI)));
75 #else
76 typedef signed gcov_type __attribute__ ((mode (QI)));
77 typedef unsigned gcov_type_unsigned __attribute__ ((mode (QI)));
78 #endif
79 #endif
80 #endif
81
82 #if defined (TARGET_POSIX_IO)
83 #define GCOV_LOCKED 1
84 #else
85 #define GCOV_LOCKED 0
86 #endif
87
88 #ifndef GCOV_SUPPORTS_ATOMIC
89 /* Detect whether target can support atomic update of profilers. */
90 #if __SIZEOF_LONG_LONG__ == 4 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
91 #define GCOV_SUPPORTS_ATOMIC 1
92 #else
93 #if __SIZEOF_LONG_LONG__ == 8 && __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
94 #define GCOV_SUPPORTS_ATOMIC 1
95 #else
96 #define GCOV_SUPPORTS_ATOMIC 0
97 #endif
98 #endif
99 #endif
100
101 /* In libgcov we need these functions to be extern, so prefix them with
102 __gcov. In libgcov they must also be hidden so that the instance in
103 the executable is not also used in a DSO. */
104 #define gcov_var __gcov_var
105 #define gcov_open __gcov_open
106 #define gcov_close __gcov_close
107 #define gcov_write_tag_length __gcov_write_tag_length
108 #define gcov_position __gcov_position
109 #define gcov_seek __gcov_seek
110 #define gcov_rewrite __gcov_rewrite
111 #define gcov_is_error __gcov_is_error
112 #define gcov_write_unsigned __gcov_write_unsigned
113 #define gcov_write_counter __gcov_write_counter
114 #define gcov_write_summary __gcov_write_summary
115 #define gcov_read_unsigned __gcov_read_unsigned
116 #define gcov_read_counter __gcov_read_counter
117 #define gcov_read_summary __gcov_read_summary
118
119 #else /* IN_GCOV_TOOL */
120 /* About the host. */
121 /* This path will be compiled for the host and linked into
122 gcov-tool binary. */
123
124 #include "config.h"
125 #include "system.h"
126 #include "coretypes.h"
127 #include "tm.h"
128
129 typedef unsigned gcov_unsigned_t;
130 typedef unsigned gcov_position_t;
131 /* gcov_type is typedef'd elsewhere for the compiler */
132 #if defined (HOST_HAS_F_SETLKW)
133 #define GCOV_LOCKED 1
134 #else
135 #define GCOV_LOCKED 0
136 #endif
137
138 /* Some Macros specific to gcov-tool. */
139
140 #define L_gcov 1
141 #define L_gcov_merge_add 1
142 #define L_gcov_merge_topn 1
143 #define L_gcov_merge_ior 1
144 #define L_gcov_merge_time_profile 1
145
146 extern gcov_type gcov_read_counter_mem ();
147 extern unsigned gcov_get_merge_weight ();
148 extern struct gcov_info *gcov_list;
149
150 #endif /* !IN_GCOV_TOOL */
151
152 #if defined(inhibit_libc)
153 #define IN_LIBGCOV (-1)
154 #else
155 #define IN_LIBGCOV 1
156 #if defined(L_gcov)
157 #define GCOV_LINKAGE /* nothing */
158 #endif
159 #endif
160
161 /* Poison these, so they don't accidentally slip in. */
162 #pragma GCC poison gcov_write_string gcov_write_tag gcov_write_length
163 #pragma GCC poison gcov_time
164
165 #ifdef HAVE_GAS_HIDDEN
166 #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
167 #else
168 #define ATTRIBUTE_HIDDEN
169 #endif
170
171 #include "gcov-io.h"
172
173 /* Structures embedded in coveraged program. The structures generated
174 by write_profile must match these. */
175
176 /* Information about counters for a single function. */
177 struct gcov_ctr_info
178 {
179 gcov_unsigned_t num; /* number of counters. */
180 gcov_type *values; /* their values. */
181 };
182
183 /* Information about a single function. This uses the trailing array
184 idiom. The number of counters is determined from the merge pointer
185 array in gcov_info. The key is used to detect which of a set of
186 comdat functions was selected -- it points to the gcov_info object
187 of the object file containing the selected comdat function. */
188
189 struct gcov_fn_info
190 {
191 const struct gcov_info *key; /* comdat key */
192 gcov_unsigned_t ident; /* unique ident of function */
193 gcov_unsigned_t lineno_checksum; /* function lineo_checksum */
194 gcov_unsigned_t cfg_checksum; /* function cfg checksum */
195 struct gcov_ctr_info ctrs[1]; /* instrumented counters */
196 };
197
198 /* Type of function used to merge counters. */
199 typedef void (*gcov_merge_fn) (gcov_type *, gcov_unsigned_t);
200
201 /* Information about a single object file. */
202 struct gcov_info
203 {
204 gcov_unsigned_t version; /* expected version number */
205 struct gcov_info *next; /* link to next, used by libgcov */
206
207 gcov_unsigned_t stamp; /* uniquifying time stamp */
208 const char *filename; /* output file name */
209
210 gcov_merge_fn merge[GCOV_COUNTERS]; /* merge functions (null for
211 unused) */
212
213 unsigned n_functions; /* number of functions */
214
215 #ifndef IN_GCOV_TOOL
216 const struct gcov_fn_info *const *functions; /* pointer to pointers
217 to function information */
218 #else
219 const struct gcov_fn_info **functions;
220 #endif /* !IN_GCOV_TOOL */
221 };
222
223 /* Root of a program/shared-object state */
224 struct gcov_root
225 {
226 struct gcov_info *list;
227 unsigned dumped : 1; /* counts have been dumped. */
228 unsigned run_counted : 1; /* run has been accounted for. */
229 struct gcov_root *next;
230 struct gcov_root *prev;
231 };
232
233 extern struct gcov_root __gcov_root ATTRIBUTE_HIDDEN;
234
235 struct gcov_master
236 {
237 gcov_unsigned_t version;
238 struct gcov_root *root;
239 };
240
241 struct indirect_call_tuple
242 {
243 /* Callee function. */
244 void *callee;
245
246 /* Pointer to counters. */
247 gcov_type *counters;
248 };
249
250 /* Exactly one of these will be active in the process. */
251 extern struct gcov_master __gcov_master;
252
253 /* Dump a set of gcov objects. */
254 extern void __gcov_dump_one (struct gcov_root *) ATTRIBUTE_HIDDEN;
255
256 /* Register a new object file module. */
257 extern void __gcov_init (struct gcov_info *) ATTRIBUTE_HIDDEN;
258
259 /* GCOV exit function registered via a static destructor. */
260 extern void __gcov_exit (void) ATTRIBUTE_HIDDEN;
261
262 /* Function to reset all counters to 0. Both externally visible (and
263 overridable) and internal version. */
264 extern void __gcov_reset_int (void) ATTRIBUTE_HIDDEN;
265
266 /* User function to enable early write of profile information so far. */
267 extern void __gcov_dump_int (void) ATTRIBUTE_HIDDEN;
268
269 /* Lock critical section for __gcov_dump and __gcov_reset functions. */
270 extern void __gcov_lock (void) ATTRIBUTE_HIDDEN;
271
272 /* Unlock critical section for __gcov_dump and __gcov_reset functions. */
273 extern void __gcov_unlock (void) ATTRIBUTE_HIDDEN;
274
275 /* The merge function that just sums the counters. */
276 extern void __gcov_merge_add (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
277
278 /* The merge function to select the minimum valid counter value. */
279 extern void __gcov_merge_time_profile (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
280
281 /* The merge function to choose the most common N values. */
282 extern void __gcov_merge_topn (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
283
284 /* The merge function that just ors the counters together. */
285 extern void __gcov_merge_ior (gcov_type *, unsigned) ATTRIBUTE_HIDDEN;
286
287 /* The profiler functions. */
288 extern void __gcov_interval_profiler (gcov_type *, gcov_type, int, unsigned);
289 extern void __gcov_interval_profiler_atomic (gcov_type *, gcov_type, int,
290 unsigned);
291 extern void __gcov_pow2_profiler (gcov_type *, gcov_type);
292 extern void __gcov_pow2_profiler_atomic (gcov_type *, gcov_type);
293 extern void __gcov_topn_values_profiler (gcov_type *, gcov_type);
294 extern void __gcov_topn_values_profiler_atomic (gcov_type *, gcov_type);
295 extern void __gcov_indirect_call_profiler_v4 (gcov_type, void *);
296 extern void __gcov_indirect_call_profiler_v4_atomic (gcov_type, void *);
297 extern void __gcov_time_profiler (gcov_type *);
298 extern void __gcov_time_profiler_atomic (gcov_type *);
299 extern void __gcov_average_profiler (gcov_type *, gcov_type);
300 extern void __gcov_average_profiler_atomic (gcov_type *, gcov_type);
301 extern void __gcov_ior_profiler (gcov_type *, gcov_type);
302 extern void __gcov_ior_profiler_atomic (gcov_type *, gcov_type);
303
304 #ifndef inhibit_libc
305 /* The wrappers around some library functions.. */
306 extern pid_t __gcov_fork (void) ATTRIBUTE_HIDDEN;
307 extern int __gcov_execl (const char *, char *, ...) ATTRIBUTE_HIDDEN;
308 extern int __gcov_execlp (const char *, char *, ...) ATTRIBUTE_HIDDEN;
309 extern int __gcov_execle (const char *, char *, ...) ATTRIBUTE_HIDDEN;
310 extern int __gcov_execv (const char *, char *const []) ATTRIBUTE_HIDDEN;
311 extern int __gcov_execvp (const char *, char *const []) ATTRIBUTE_HIDDEN;
312 extern int __gcov_execve (const char *, char *const [], char *const [])
313 ATTRIBUTE_HIDDEN;
314
315 /* Functions that only available in libgcov. */
316 GCOV_LINKAGE int gcov_open (const char */*name*/) ATTRIBUTE_HIDDEN;
317 GCOV_LINKAGE void gcov_write_counter (gcov_type) ATTRIBUTE_HIDDEN;
318 GCOV_LINKAGE void gcov_write_tag_length (gcov_unsigned_t, gcov_unsigned_t)
319 ATTRIBUTE_HIDDEN;
320 GCOV_LINKAGE void gcov_write_summary (gcov_unsigned_t /*tag*/,
321 const struct gcov_summary *)
322 ATTRIBUTE_HIDDEN;
323 GCOV_LINKAGE void gcov_seek (gcov_position_t /*position*/) ATTRIBUTE_HIDDEN;
324 GCOV_LINKAGE void gcov_rewrite (void) ATTRIBUTE_HIDDEN;
325
326 /* "Counts" stored in gcda files can be a real counter value, or
327 an target address. When differentiate these two types because
328 when manipulating counts, we should only change real counter values,
329 rather target addresses. */
330
331 static inline gcov_type
332 gcov_get_counter (void)
333 {
334 #ifndef IN_GCOV_TOOL
335 /* This version is for reading count values in libgcov runtime:
336 we read from gcda files. */
337
338 return gcov_read_counter ();
339 #else
340 /* This version is for gcov-tool. We read the value from memory and
341 multiply it by the merge weight. */
342
343 return gcov_read_counter_mem () * gcov_get_merge_weight ();
344 #endif
345 }
346
347 /* Similar function as gcov_get_counter(), but do not scale
348 when read value is equal to IGNORE_SCALING. */
349
350 static inline gcov_type
351 gcov_get_counter_ignore_scaling (gcov_type ignore_scaling ATTRIBUTE_UNUSED)
352 {
353 #ifndef IN_GCOV_TOOL
354 /* This version is for reading count values in libgcov runtime:
355 we read from gcda files. */
356
357 return gcov_read_counter ();
358 #else
359 /* This version is for gcov-tool. We read the value from memory and
360 multiply it by the merge weight. */
361
362 gcov_type v = gcov_read_counter_mem ();
363 if (v != ignore_scaling)
364 v *= gcov_get_merge_weight ();
365
366 return v;
367 #endif
368 }
369
370 /* Similar function as gcov_get_counter(), but handles target address
371 counters. */
372
373 static inline gcov_type
374 gcov_get_counter_target (void)
375 {
376 #ifndef IN_GCOV_TOOL
377 /* This version is for reading count target values in libgcov runtime:
378 we read from gcda files. */
379
380 return gcov_read_counter ();
381 #else
382 /* This version is for gcov-tool. We read the value from memory and we do NOT
383 multiply it by the merge weight. */
384
385 return gcov_read_counter_mem ();
386 #endif
387 }
388
389 /* Add VALUE to *COUNTER and make it with atomic operation
390 if USE_ATOMIC is true. */
391
392 static inline void
393 gcov_counter_add (gcov_type *counter, gcov_type value,
394 int use_atomic ATTRIBUTE_UNUSED)
395 {
396 #if GCOV_SUPPORTS_ATOMIC
397 if (use_atomic)
398 __atomic_fetch_add (counter, value, __ATOMIC_RELAXED);
399 else
400 #endif
401 *counter += value;
402 }
403
404 /* Set NODE to memory location COUNTER and make it with atomic operation
405 if USE_ATOMIC is true. */
406
407 static inline int
408 gcov_counter_set_if_null (gcov_type *counter, struct gcov_kvp *node,
409 int use_atomic ATTRIBUTE_UNUSED)
410 {
411 #if GCOV_SUPPORTS_ATOMIC
412 if (use_atomic)
413 return !__sync_val_compare_and_swap (counter, NULL, (intptr_t)node);
414 else
415 #endif
416 {
417 *counter = (intptr_t)node;
418 return 1;
419 }
420 }
421
422 /* Add key value pair VALUE:COUNT to a top N COUNTERS. When INCREMENT_TOTAL
423 is true, add COUNT to total of the TOP counter. If USE_ATOMIC is true,
424 do it in atomic way. */
425
426 static inline void
427 gcov_topn_add_value (gcov_type *counters, gcov_type value, gcov_type count,
428 int use_atomic, int increment_total)
429 {
430 if (increment_total)
431 gcov_counter_add (&counters[0], 1, use_atomic);
432
433 struct gcov_kvp *prev_node = NULL;
434 struct gcov_kvp *minimal_node = NULL;
435 struct gcov_kvp *current_node = (struct gcov_kvp *)counters[2];
436
437 while (current_node)
438 {
439 if (current_node->value == value)
440 {
441 gcov_counter_add (&current_node->count, count, use_atomic);
442 return;
443 }
444
445 if (minimal_node == NULL
446 || current_node->count < minimal_node->count)
447 minimal_node = current_node;
448
449 prev_node = current_node;
450 current_node = current_node->next;
451 }
452
453 if (counters[1] == GCOV_TOPN_MAXIMUM_TRACKED_VALUES)
454 {
455 if (--minimal_node->count < count)
456 {
457 minimal_node->value = value;
458 minimal_node->count = count;
459 }
460 }
461 else
462 {
463 struct gcov_kvp *new_node
464 = (struct gcov_kvp *)xcalloc (1, sizeof (struct gcov_kvp));
465 new_node->value = value;
466 new_node->count = count;
467
468 int success = 0;
469 if (!counters[2])
470 success = gcov_counter_set_if_null (&counters[2], new_node, use_atomic);
471 else if (prev_node && !prev_node->next)
472 success = gcov_counter_set_if_null ((gcov_type *)&prev_node->next,
473 new_node, use_atomic);
474
475 /* Increment number of nodes. */
476 if (success)
477 gcov_counter_add (&counters[1], 1, use_atomic);
478 }
479 }
480
481 #endif /* !inhibit_libc */
482
483 #endif /* GCC_LIBGCOV_H */