1 /* Header file for libgcov-*.c.
2 Copyright (C) 1996-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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.
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/>. */
28 /* work around the poisoned malloc/calloc in system.h. */
30 #define xmalloc malloc
33 #define xcalloc calloc
37 /* About the target. */
38 /* This path will be used by libgcov runtime. */
41 #include "auto-target.h"
43 #include "coretypes.h"
45 #include "libgcc_tm.h"
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
)));
55 typedef signed gcov_type
__attribute__ ((mode (SI
)));
56 typedef unsigned gcov_type_unsigned
__attribute__ ((mode (SI
)));
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
)));
66 typedef signed gcov_type
__attribute__ ((mode (HI
)));
67 typedef unsigned gcov_type_unsigned
__attribute__ ((mode (HI
)));
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
)));
76 typedef signed gcov_type
__attribute__ ((mode (QI
)));
77 typedef unsigned gcov_type_unsigned
__attribute__ ((mode (QI
)));
82 #if defined (TARGET_POSIX_IO)
88 /* In libgcov we need these functions to be extern, so prefix them with
89 __gcov. In libgcov they must also be hidden so that the instance in
90 the executable is not also used in a DSO. */
91 #define gcov_var __gcov_var
92 #define gcov_open __gcov_open
93 #define gcov_close __gcov_close
94 #define gcov_write_tag_length __gcov_write_tag_length
95 #define gcov_position __gcov_position
96 #define gcov_seek __gcov_seek
97 #define gcov_rewrite __gcov_rewrite
98 #define gcov_is_error __gcov_is_error
99 #define gcov_write_unsigned __gcov_write_unsigned
100 #define gcov_write_counter __gcov_write_counter
101 #define gcov_write_summary __gcov_write_summary
102 #define gcov_read_unsigned __gcov_read_unsigned
103 #define gcov_read_counter __gcov_read_counter
104 #define gcov_read_summary __gcov_read_summary
106 #else /* IN_GCOV_TOOL */
107 /* About the host. */
108 /* This path will be compiled for the host and linked into
113 #include "coretypes.h"
116 typedef unsigned gcov_unsigned_t
;
117 typedef unsigned gcov_position_t
;
118 /* gcov_type is typedef'd elsewhere for the compiler */
119 #if defined (HOST_HAS_F_SETLKW)
120 #define GCOV_LOCKED 1
122 #define GCOV_LOCKED 0
125 /* Some Macros specific to gcov-tool. */
128 #define L_gcov_merge_add 1
129 #define L_gcov_merge_topn 1
130 #define L_gcov_merge_ior 1
131 #define L_gcov_merge_time_profile 1
133 extern gcov_type
gcov_read_counter_mem ();
134 extern unsigned gcov_get_merge_weight ();
135 extern struct gcov_info
*gcov_list
;
137 #endif /* !IN_GCOV_TOOL */
139 #if defined(inhibit_libc)
140 #define IN_LIBGCOV (-1)
144 #define GCOV_LINKAGE /* nothing */
148 /* Poison these, so they don't accidentally slip in. */
149 #pragma GCC poison gcov_write_string gcov_write_tag gcov_write_length
150 #pragma GCC poison gcov_time
152 #ifdef HAVE_GAS_HIDDEN
153 #define ATTRIBUTE_HIDDEN __attribute__ ((__visibility__ ("hidden")))
155 #define ATTRIBUTE_HIDDEN
160 /* Structures embedded in coveraged program. The structures generated
161 by write_profile must match these. */
163 /* Information about counters for a single function. */
166 gcov_unsigned_t num
; /* number of counters. */
167 gcov_type
*values
; /* their values. */
170 /* Information about a single function. This uses the trailing array
171 idiom. The number of counters is determined from the merge pointer
172 array in gcov_info. The key is used to detect which of a set of
173 comdat functions was selected -- it points to the gcov_info object
174 of the object file containing the selected comdat function. */
178 const struct gcov_info
*key
; /* comdat key */
179 gcov_unsigned_t ident
; /* unique ident of function */
180 gcov_unsigned_t lineno_checksum
; /* function lineo_checksum */
181 gcov_unsigned_t cfg_checksum
; /* function cfg checksum */
182 struct gcov_ctr_info ctrs
[1]; /* instrumented counters */
185 /* Type of function used to merge counters. */
186 typedef void (*gcov_merge_fn
) (gcov_type
*, gcov_unsigned_t
);
188 /* Information about a single object file. */
191 gcov_unsigned_t version
; /* expected version number */
192 struct gcov_info
*next
; /* link to next, used by libgcov */
194 gcov_unsigned_t stamp
; /* uniquifying time stamp */
195 const char *filename
; /* output file name */
197 gcov_merge_fn merge
[GCOV_COUNTERS
]; /* merge functions (null for
200 unsigned n_functions
; /* number of functions */
203 const struct gcov_fn_info
*const *functions
; /* pointer to pointers
204 to function information */
206 const struct gcov_fn_info
**functions
;
207 #endif /* !IN_GCOV_TOOL */
210 /* Root of a program/shared-object state */
213 struct gcov_info
*list
;
214 unsigned dumped
: 1; /* counts have been dumped. */
215 unsigned run_counted
: 1; /* run has been accounted for. */
216 struct gcov_root
*next
;
217 struct gcov_root
*prev
;
220 extern struct gcov_root __gcov_root ATTRIBUTE_HIDDEN
;
224 gcov_unsigned_t version
;
225 struct gcov_root
*root
;
228 struct indirect_call_tuple
230 /* Callee function. */
233 /* Pointer to counters. */
237 /* Exactly one of these will be active in the process. */
238 extern struct gcov_master __gcov_master
;
240 /* Dump a set of gcov objects. */
241 extern void __gcov_dump_one (struct gcov_root
*) ATTRIBUTE_HIDDEN
;
243 /* Register a new object file module. */
244 extern void __gcov_init (struct gcov_info
*) ATTRIBUTE_HIDDEN
;
246 /* GCOV exit function registered via a static destructor. */
247 extern void __gcov_exit (void) ATTRIBUTE_HIDDEN
;
249 /* Function to reset all counters to 0. Both externally visible (and
250 overridable) and internal version. */
251 extern void __gcov_reset_int (void) ATTRIBUTE_HIDDEN
;
253 /* User function to enable early write of profile information so far. */
254 extern void __gcov_dump_int (void) ATTRIBUTE_HIDDEN
;
256 /* Lock critical section for __gcov_dump and __gcov_reset functions. */
257 extern void __gcov_lock (void) ATTRIBUTE_HIDDEN
;
259 /* Unlock critical section for __gcov_dump and __gcov_reset functions. */
260 extern void __gcov_unlock (void) ATTRIBUTE_HIDDEN
;
262 /* The merge function that just sums the counters. */
263 extern void __gcov_merge_add (gcov_type
*, unsigned) ATTRIBUTE_HIDDEN
;
265 /* The merge function to select the minimum valid counter value. */
266 extern void __gcov_merge_time_profile (gcov_type
*, unsigned) ATTRIBUTE_HIDDEN
;
268 /* The merge function to choose the most common N values. */
269 extern void __gcov_merge_topn (gcov_type
*, unsigned) ATTRIBUTE_HIDDEN
;
271 /* The merge function that just ors the counters together. */
272 extern void __gcov_merge_ior (gcov_type
*, unsigned) ATTRIBUTE_HIDDEN
;
274 /* The profiler functions. */
275 extern void __gcov_interval_profiler (gcov_type
*, gcov_type
, int, unsigned);
276 extern void __gcov_interval_profiler_atomic (gcov_type
*, gcov_type
, int,
278 extern void __gcov_pow2_profiler (gcov_type
*, gcov_type
);
279 extern void __gcov_pow2_profiler_atomic (gcov_type
*, gcov_type
);
280 extern void __gcov_topn_values_profiler (gcov_type
*, gcov_type
);
281 extern void __gcov_topn_values_profiler_atomic (gcov_type
*, gcov_type
);
282 extern void __gcov_indirect_call_profiler_v4 (gcov_type
, void *);
283 extern void __gcov_indirect_call_profiler_v4_atomic (gcov_type
, void *);
284 extern void __gcov_time_profiler (gcov_type
*);
285 extern void __gcov_time_profiler_atomic (gcov_type
*);
286 extern void __gcov_average_profiler (gcov_type
*, gcov_type
);
287 extern void __gcov_average_profiler_atomic (gcov_type
*, gcov_type
);
288 extern void __gcov_ior_profiler (gcov_type
*, gcov_type
);
289 extern void __gcov_ior_profiler_atomic (gcov_type
*, gcov_type
);
292 /* The wrappers around some library functions.. */
293 extern pid_t
__gcov_fork (void) ATTRIBUTE_HIDDEN
;
294 extern int __gcov_execl (const char *, char *, ...) ATTRIBUTE_HIDDEN
;
295 extern int __gcov_execlp (const char *, char *, ...) ATTRIBUTE_HIDDEN
;
296 extern int __gcov_execle (const char *, char *, ...) ATTRIBUTE_HIDDEN
;
297 extern int __gcov_execv (const char *, char *const []) ATTRIBUTE_HIDDEN
;
298 extern int __gcov_execvp (const char *, char *const []) ATTRIBUTE_HIDDEN
;
299 extern int __gcov_execve (const char *, char *const [], char *const [])
302 /* Functions that only available in libgcov. */
303 GCOV_LINKAGE
int gcov_open (const char */
*name*/
) ATTRIBUTE_HIDDEN
;
304 GCOV_LINKAGE
void gcov_write_counter (gcov_type
) ATTRIBUTE_HIDDEN
;
305 GCOV_LINKAGE
void gcov_write_tag_length (gcov_unsigned_t
, gcov_unsigned_t
)
307 GCOV_LINKAGE
void gcov_write_summary (gcov_unsigned_t
/*tag*/,
308 const struct gcov_summary
*)
310 GCOV_LINKAGE
void gcov_seek (gcov_position_t
/*position*/) ATTRIBUTE_HIDDEN
;
311 GCOV_LINKAGE
void gcov_rewrite (void) ATTRIBUTE_HIDDEN
;
313 /* "Counts" stored in gcda files can be a real counter value, or
314 an target address. When differentiate these two types because
315 when manipulating counts, we should only change real counter values,
316 rather target addresses. */
318 static inline gcov_type
319 gcov_get_counter (void)
322 /* This version is for reading count values in libgcov runtime:
323 we read from gcda files. */
325 return gcov_read_counter ();
327 /* This version is for gcov-tool. We read the value from memory and
328 multiply it by the merge weight. */
330 return gcov_read_counter_mem () * gcov_get_merge_weight ();
334 /* Similar function as gcov_get_counter(), but do not scale
335 when read value is equal to IGNORE_SCALING. */
337 static inline gcov_type
338 gcov_get_counter_ignore_scaling (gcov_type ignore_scaling ATTRIBUTE_UNUSED
)
341 /* This version is for reading count values in libgcov runtime:
342 we read from gcda files. */
344 return gcov_read_counter ();
346 /* This version is for gcov-tool. We read the value from memory and
347 multiply it by the merge weight. */
349 gcov_type v
= gcov_read_counter_mem ();
350 if (v
!= ignore_scaling
)
351 v
*= gcov_get_merge_weight ();
357 /* Similar function as gcov_get_counter(), but handles target address
360 static inline gcov_type
361 gcov_get_counter_target (void)
364 /* This version is for reading count target values in libgcov runtime:
365 we read from gcda files. */
367 return gcov_read_counter ();
369 /* This version is for gcov-tool. We read the value from memory and we do NOT
370 multiply it by the merge weight. */
372 return gcov_read_counter_mem ();
376 /* Add VALUE to *COUNTER and make it with atomic operation
377 if USE_ATOMIC is true. */
380 gcov_counter_add (gcov_type
*counter
, gcov_type value
, int use_atomic
)
383 __atomic_fetch_add (counter
, value
, __ATOMIC_RELAXED
);
388 /* Set NODE to memory location COUNTER and make it with atomic operation
389 if USE_ATOMIC is true. */
392 gcov_counter_set_if_null (gcov_type
*counter
, struct gcov_kvp
*node
,
396 return !__sync_val_compare_and_swap (counter
, NULL
, (intptr_t)node
);
399 *counter
= (intptr_t)node
;
404 /* Add key value pair VALUE:COUNT to a top N COUNTERS. When INCREMENT_TOTAL
405 is true, add COUNT to total of the TOP counter. If USE_ATOMIC is true,
406 do it in atomic way. */
409 gcov_topn_add_value (gcov_type
*counters
, gcov_type value
, gcov_type count
,
410 int use_atomic
, int increment_total
)
413 gcov_counter_add (&counters
[0], 1, use_atomic
);
415 struct gcov_kvp
*prev_node
= NULL
;
416 struct gcov_kvp
*minimal_node
= NULL
;
417 struct gcov_kvp
*current_node
= (struct gcov_kvp
*)counters
[2];
421 if (current_node
->value
== value
)
423 gcov_counter_add (¤t_node
->count
, count
, use_atomic
);
427 if (minimal_node
== NULL
428 || current_node
->count
< minimal_node
->count
)
429 minimal_node
= current_node
;
431 prev_node
= current_node
;
432 current_node
= current_node
->next
;
435 if (counters
[1] == GCOV_TOPN_MAXIMUM_TRACKED_VALUES
)
437 if (--minimal_node
->count
< count
)
439 minimal_node
->value
= value
;
440 minimal_node
->count
= count
;
445 struct gcov_kvp
*new_node
446 = (struct gcov_kvp
*)xcalloc (1, sizeof (struct gcov_kvp
));
447 new_node
->value
= value
;
448 new_node
->count
= count
;
452 success
= gcov_counter_set_if_null (&counters
[2], new_node
, use_atomic
);
453 else if (prev_node
&& !prev_node
->next
)
454 success
= gcov_counter_set_if_null ((gcov_type
*)&prev_node
->next
,
455 new_node
, use_atomic
);
457 /* Increment number of nodes. */
459 gcov_counter_add (&counters
[1], 1, use_atomic
);
463 #endif /* !inhibit_libc */
465 #endif /* GCC_LIBGCOV_H */