blob: 249c4f3babbc1ec10eb15bb333b567a9f552e3ce [file] [log] [blame]
Rong Xud6d3f032013-11-13 00:24:49 +00001/* Routines required for instrumenting a program. */
2/* Compile this one with gcc. */
Thomas Koenigb18a97e2021-09-13 19:49:49 +02003/* Copyright (C) 1989-2021 Free Software Foundation, Inc.
Rong Xud6d3f032013-11-13 00:24:49 +00004
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
Rong Xu40d6b752014-01-08 16:37:08 +000026#include "libgcov.h"
Rong Xud6d3f032013-11-13 00:24:49 +000027#if !defined(inhibit_libc)
Rong Xud6d3f032013-11-13 00:24:49 +000028
29#ifdef L_gcov_interval_profiler
30/* If VALUE is in interval <START, START + STEPS - 1>, then increases the
31 corresponding counter in COUNTERS. If the VALUE is above or below
32 the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased
33 instead. */
34
35void
36__gcov_interval_profiler (gcov_type *counters, gcov_type value,
37 int start, unsigned steps)
38{
39 gcov_type delta = value - start;
40 if (delta < 0)
41 counters[steps + 1]++;
42 else if (delta >= steps)
43 counters[steps]++;
44 else
45 counters[delta]++;
46}
47#endif
48
Martin Liska7fe76f62016-09-06 16:13:21 +020049#if defined(L_gcov_interval_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
Martin Liskaa2662362016-08-10 15:14:56 +020050/* If VALUE is in interval <START, START + STEPS - 1>, then increases the
51 corresponding counter in COUNTERS. If the VALUE is above or below
52 the interval, COUNTERS[STEPS] or COUNTERS[STEPS + 1] is increased
53 instead. Function is thread-safe. */
54
55void
56__gcov_interval_profiler_atomic (gcov_type *counters, gcov_type value,
57 int start, unsigned steps)
58{
59 gcov_type delta = value - start;
60 if (delta < 0)
Thomas Preud'homme4d0cdd02016-10-13 14:17:52 +000061 __atomic_fetch_add (&counters[steps + 1], 1, __ATOMIC_RELAXED);
Martin Liskaa2662362016-08-10 15:14:56 +020062 else if (delta >= steps)
Thomas Preud'homme4d0cdd02016-10-13 14:17:52 +000063 __atomic_fetch_add (&counters[steps], 1, __ATOMIC_RELAXED);
Martin Liskaa2662362016-08-10 15:14:56 +020064 else
Thomas Preud'homme4d0cdd02016-10-13 14:17:52 +000065 __atomic_fetch_add (&counters[delta], 1, __ATOMIC_RELAXED);
Martin Liskaa2662362016-08-10 15:14:56 +020066}
67#endif
68
Rong Xud6d3f032013-11-13 00:24:49 +000069#ifdef L_gcov_pow2_profiler
70/* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
71 COUNTERS[0] is incremented. */
72
73void
74__gcov_pow2_profiler (gcov_type *counters, gcov_type value)
75{
Martin Liskadcb1e132016-08-09 22:57:14 +020076 if (value == 0 || (value & (value - 1)))
Rong Xud6d3f032013-11-13 00:24:49 +000077 counters[0]++;
78 else
79 counters[1]++;
80}
81#endif
82
Martin Liska7fe76f62016-09-06 16:13:21 +020083#if defined(L_gcov_pow2_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
Martin Liskaa2662362016-08-10 15:14:56 +020084/* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise
85 COUNTERS[0] is incremented. Function is thread-safe. */
86
87void
88__gcov_pow2_profiler_atomic (gcov_type *counters, gcov_type value)
89{
90 if (value == 0 || (value & (value - 1)))
Thomas Preud'homme4d0cdd02016-10-13 14:17:52 +000091 __atomic_fetch_add (&counters[0], 1, __ATOMIC_RELAXED);
Martin Liskaa2662362016-08-10 15:14:56 +020092 else
Thomas Preud'homme4d0cdd02016-10-13 14:17:52 +000093 __atomic_fetch_add (&counters[1], 1, __ATOMIC_RELAXED);
Martin Liskaa2662362016-08-10 15:14:56 +020094}
95#endif
96
Martin Liska596341c2019-07-03 14:42:23 +020097/* Tries to determine N most commons value among its inputs. */
Rong Xud6d3f032013-11-13 00:24:49 +000098
99static inline void
Martin Liska596341c2019-07-03 14:42:23 +0200100__gcov_topn_values_profiler_body (gcov_type *counters, gcov_type value,
101 int use_atomic)
Rong Xud6d3f032013-11-13 00:24:49 +0000102{
Martin Liska871e5ad2020-01-31 13:10:14 +0100103 gcov_topn_add_value (counters, value, 1, use_atomic, 1);
Rong Xud6d3f032013-11-13 00:24:49 +0000104}
105
Martin Liska596341c2019-07-03 14:42:23 +0200106#ifdef L_gcov_topn_values_profiler
Rong Xud6d3f032013-11-13 00:24:49 +0000107void
Martin Liska596341c2019-07-03 14:42:23 +0200108__gcov_topn_values_profiler (gcov_type *counters, gcov_type value)
Rong Xud6d3f032013-11-13 00:24:49 +0000109{
Martin Liska596341c2019-07-03 14:42:23 +0200110 __gcov_topn_values_profiler_body (counters, value, 0);
Martin Liskaa2662362016-08-10 15:14:56 +0200111}
112#endif
113
Martin Liska596341c2019-07-03 14:42:23 +0200114#if defined(L_gcov_topn_values_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
Martin Liskaa2662362016-08-10 15:14:56 +0200115
116/* Update one value profilers (COUNTERS) for a given VALUE.
117
118 CAVEAT: Following function is not thread-safe, only total number
119 of executions (COUNTERS[2]) is update with an atomic instruction.
120 Problem is that one cannot atomically update two counters
121 (COUNTERS[0] and COUNTERS[1]), for more information please read
122 following email thread:
123 https://gcc.gnu.org/ml/gcc-patches/2016-08/msg00024.html. */
124
125void
Martin Liska596341c2019-07-03 14:42:23 +0200126__gcov_topn_values_profiler_atomic (gcov_type *counters, gcov_type value)
Martin Liskaa2662362016-08-10 15:14:56 +0200127{
Martin Liska596341c2019-07-03 14:42:23 +0200128 __gcov_topn_values_profiler_body (counters, value, 1);
Rong Xud6d3f032013-11-13 00:24:49 +0000129}
130#endif
131
Martin Liska92d41712019-06-10 09:38:59 +0200132#ifdef L_gcov_indirect_call_profiler_v4
Rong Xud6d3f032013-11-13 00:24:49 +0000133
134/* These two variables are used to actually track caller and callee. Keep
135 them in TLS memory so races are not common (they are written to often).
136 The variables are set directly by GCC instrumented code, so declaration
137 here must match one in tree-profile.c */
138
139#if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS)
140__thread
141#endif
Martin Liska3edbcdb2018-10-04 14:41:14 +0200142struct indirect_call_tuple __gcov_indirect_call;
Rong Xud6d3f032013-11-13 00:24:49 +0000143
144/* By default, the C++ compiler will use function addresses in the
145 vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero
146 tells the compiler to use function descriptors instead. The value
Joseph Myers53d68b92014-09-05 13:03:46 +0100147 of this macro says how many words wide the descriptor is (normally 2).
Rong Xud6d3f032013-11-13 00:24:49 +0000148
149 It is assumed that the address of a function descriptor may be treated
150 as a pointer to a function. */
151
Rong Xud6d3f032013-11-13 00:24:49 +0000152/* Tries to determine the most common value among its inputs. */
Martin Liska3ae37f92020-01-27 16:20:10 +0100153static inline void
154__gcov_indirect_call_profiler_body (gcov_type value, void *cur_func,
155 int use_atomic)
Rong Xud6d3f032013-11-13 00:24:49 +0000156{
157 /* If the C++ virtual tables contain function descriptors then one
158 function may have multiple descriptors and we need to dereference
159 the descriptors to see if they point to the same function. */
Martin Liska3edbcdb2018-10-04 14:41:14 +0200160 if (cur_func == __gcov_indirect_call.callee
Martin Liskafd2e1dc2018-08-01 12:22:18 +0200161 || (__LIBGCC_VTABLE_USES_DESCRIPTORS__
Martin Liska3edbcdb2018-10-04 14:41:14 +0200162 && *(void **) cur_func == *(void **) __gcov_indirect_call.callee))
Martin Liska3ae37f92020-01-27 16:20:10 +0100163 __gcov_topn_values_profiler_body (__gcov_indirect_call.counters, value,
164 use_atomic);
Martin Liska4f751c52017-06-09 14:54:53 +0200165
Martin Liska3edbcdb2018-10-04 14:41:14 +0200166 __gcov_indirect_call.callee = NULL;
Rong Xud6d3f032013-11-13 00:24:49 +0000167}
Martin Liska3ae37f92020-01-27 16:20:10 +0100168
169void
170__gcov_indirect_call_profiler_v4 (gcov_type value, void *cur_func)
171{
172 __gcov_indirect_call_profiler_body (value, cur_func, 0);
173}
174
175#if GCOV_SUPPORTS_ATOMIC
176void
177__gcov_indirect_call_profiler_v4_atomic (gcov_type value, void *cur_func)
178{
179 __gcov_indirect_call_profiler_body (value, cur_func, 1);
180}
181#endif
182
Rong Xud6d3f032013-11-13 00:24:49 +0000183#endif
184
185#ifdef L_gcov_time_profiler
186
187/* Counter for first visit of each function. */
Martin Liska3239d722018-10-25 17:36:12 +0200188gcov_type __gcov_time_profiler_counter ATTRIBUTE_HIDDEN;
Rong Xud6d3f032013-11-13 00:24:49 +0000189
Rong Xud6d3f032013-11-13 00:24:49 +0000190#endif
Martin Liskaa2662362016-08-10 15:14:56 +0200191
Rong Xud6d3f032013-11-13 00:24:49 +0000192#ifdef L_gcov_average_profiler
193/* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
194 to saturate up. */
195
196void
197__gcov_average_profiler (gcov_type *counters, gcov_type value)
198{
199 counters[0] += value;
200 counters[1] ++;
201}
202#endif
203
Martin Liska7fe76f62016-09-06 16:13:21 +0200204#if defined(L_gcov_average_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
Martin Liskaa2662362016-08-10 15:14:56 +0200205/* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want
206 to saturate up. Function is thread-safe. */
207
208void
209__gcov_average_profiler_atomic (gcov_type *counters, gcov_type value)
210{
Thomas Preud'homme4d0cdd02016-10-13 14:17:52 +0000211 __atomic_fetch_add (&counters[0], value, __ATOMIC_RELAXED);
212 __atomic_fetch_add (&counters[1], 1, __ATOMIC_RELAXED);
Martin Liskaa2662362016-08-10 15:14:56 +0200213}
214#endif
215
Rong Xud6d3f032013-11-13 00:24:49 +0000216#ifdef L_gcov_ior_profiler
217/* Bitwise-OR VALUE into COUNTER. */
218
219void
220__gcov_ior_profiler (gcov_type *counters, gcov_type value)
221{
222 *counters |= value;
223}
224#endif
225
Martin Liska7fe76f62016-09-06 16:13:21 +0200226#if defined(L_gcov_ior_profiler_atomic) && GCOV_SUPPORTS_ATOMIC
Martin Liskaa2662362016-08-10 15:14:56 +0200227/* Bitwise-OR VALUE into COUNTER. Function is thread-safe. */
228
229void
230__gcov_ior_profiler_atomic (gcov_type *counters, gcov_type value)
231{
Thomas Preud'homme4d0cdd02016-10-13 14:17:52 +0000232 __atomic_fetch_or (&counters[0], value, __ATOMIC_RELAXED);
Martin Liskaa2662362016-08-10 15:14:56 +0200233}
234#endif
235
236
Rong Xud6d3f032013-11-13 00:24:49 +0000237#endif /* inhibit_libc */