Rong Xu | d6d3f03 | 2013-11-13 00:24:49 +0000 | [diff] [blame] | 1 | /* Routines required for instrumenting a program. */ |
| 2 | /* Compile this one with gcc. */ |
Richard Sandiford | ac1dca3 | 2014-01-02 22:25:22 +0000 | [diff] [blame] | 3 | /* Copyright (C) 1989-2014 Free Software Foundation, Inc. |
Rong Xu | d6d3f03 | 2013-11-13 00:24:49 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of GCC. |
| 6 | |
| 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 |
| 10 | version. |
| 11 | |
| 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 |
| 15 | for more details. |
| 16 | |
| 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. |
| 20 | |
| 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/>. */ |
| 25 | |
Rong Xu | 40d6b75 | 2014-01-08 16:37:08 +0000 | [diff] [blame] | 26 | #include "libgcov.h" |
Rong Xu | d6d3f03 | 2013-11-13 00:24:49 +0000 | [diff] [blame] | 27 | #if !defined(inhibit_libc) |
Rong Xu | d6d3f03 | 2013-11-13 00:24:49 +0000 | [diff] [blame] | 28 | |
| 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 | |
| 35 | void |
| 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 | |
| 49 | #ifdef L_gcov_pow2_profiler |
| 50 | /* If VALUE is a power of two, COUNTERS[1] is incremented. Otherwise |
| 51 | COUNTERS[0] is incremented. */ |
| 52 | |
| 53 | void |
| 54 | __gcov_pow2_profiler (gcov_type *counters, gcov_type value) |
| 55 | { |
| 56 | if (value & (value - 1)) |
| 57 | counters[0]++; |
| 58 | else |
| 59 | counters[1]++; |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | /* Tries to determine the most common value among its inputs. Checks if the |
| 64 | value stored in COUNTERS[0] matches VALUE. If this is the case, COUNTERS[1] |
| 65 | is incremented. If this is not the case and COUNTERS[1] is not zero, |
| 66 | COUNTERS[1] is decremented. Otherwise COUNTERS[1] is set to one and |
| 67 | VALUE is stored to COUNTERS[0]. This algorithm guarantees that if this |
| 68 | function is called more than 50% of the time with one value, this value |
| 69 | will be in COUNTERS[0] in the end. |
| 70 | |
| 71 | In any case, COUNTERS[2] is incremented. */ |
| 72 | |
| 73 | static inline void |
| 74 | __gcov_one_value_profiler_body (gcov_type *counters, gcov_type value) |
| 75 | { |
| 76 | if (value == counters[0]) |
| 77 | counters[1]++; |
| 78 | else if (counters[1] == 0) |
| 79 | { |
| 80 | counters[1] = 1; |
| 81 | counters[0] = value; |
| 82 | } |
| 83 | else |
| 84 | counters[1]--; |
| 85 | counters[2]++; |
| 86 | } |
| 87 | |
| 88 | #ifdef L_gcov_one_value_profiler |
| 89 | void |
| 90 | __gcov_one_value_profiler (gcov_type *counters, gcov_type value) |
| 91 | { |
| 92 | __gcov_one_value_profiler_body (counters, value); |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | #ifdef L_gcov_indirect_call_profiler |
| 97 | /* This function exist only for workaround of binutils bug 14342. |
| 98 | Once this compatibility hack is obsolette, it can be removed. */ |
| 99 | |
| 100 | /* By default, the C++ compiler will use function addresses in the |
| 101 | vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero |
| 102 | tells the compiler to use function descriptors instead. The value |
Joseph Myers | 53d68b9 | 2014-09-05 13:03:46 +0100 | [diff] [blame] | 103 | of this macro says how many words wide the descriptor is (normally 2). |
Rong Xu | d6d3f03 | 2013-11-13 00:24:49 +0000 | [diff] [blame] | 104 | |
| 105 | It is assumed that the address of a function descriptor may be treated |
| 106 | as a pointer to a function. */ |
| 107 | |
Rong Xu | d6d3f03 | 2013-11-13 00:24:49 +0000 | [diff] [blame] | 108 | /* Tries to determine the most common value among its inputs. */ |
| 109 | void |
| 110 | __gcov_indirect_call_profiler (gcov_type* counter, gcov_type value, |
| 111 | void* cur_func, void* callee_func) |
| 112 | { |
| 113 | /* If the C++ virtual tables contain function descriptors then one |
| 114 | function may have multiple descriptors and we need to dereference |
| 115 | the descriptors to see if they point to the same function. */ |
| 116 | if (cur_func == callee_func |
Joseph Myers | 53d68b9 | 2014-09-05 13:03:46 +0100 | [diff] [blame] | 117 | || (__LIBGCC_VTABLE_USES_DESCRIPTORS__ && callee_func |
Rong Xu | d6d3f03 | 2013-11-13 00:24:49 +0000 | [diff] [blame] | 118 | && *(void **) cur_func == *(void **) callee_func)) |
| 119 | __gcov_one_value_profiler_body (counter, value); |
| 120 | } |
| 121 | |
| 122 | #endif |
| 123 | #ifdef L_gcov_indirect_call_profiler_v2 |
| 124 | |
| 125 | /* These two variables are used to actually track caller and callee. Keep |
| 126 | them in TLS memory so races are not common (they are written to often). |
| 127 | The variables are set directly by GCC instrumented code, so declaration |
| 128 | here must match one in tree-profile.c */ |
| 129 | |
| 130 | #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS) |
| 131 | __thread |
| 132 | #endif |
| 133 | void * __gcov_indirect_call_callee; |
| 134 | #if defined(HAVE_CC_TLS) && !defined (USE_EMUTLS) |
| 135 | __thread |
| 136 | #endif |
| 137 | gcov_type * __gcov_indirect_call_counters; |
| 138 | |
| 139 | /* By default, the C++ compiler will use function addresses in the |
| 140 | vtable entries. Setting TARGET_VTABLE_USES_DESCRIPTORS to nonzero |
| 141 | tells the compiler to use function descriptors instead. The value |
Joseph Myers | 53d68b9 | 2014-09-05 13:03:46 +0100 | [diff] [blame] | 142 | of this macro says how many words wide the descriptor is (normally 2). |
Rong Xu | d6d3f03 | 2013-11-13 00:24:49 +0000 | [diff] [blame] | 143 | |
| 144 | It is assumed that the address of a function descriptor may be treated |
| 145 | as a pointer to a function. */ |
| 146 | |
Rong Xu | d6d3f03 | 2013-11-13 00:24:49 +0000 | [diff] [blame] | 147 | /* Tries to determine the most common value among its inputs. */ |
| 148 | void |
| 149 | __gcov_indirect_call_profiler_v2 (gcov_type value, void* cur_func) |
| 150 | { |
| 151 | /* If the C++ virtual tables contain function descriptors then one |
| 152 | function may have multiple descriptors and we need to dereference |
| 153 | the descriptors to see if they point to the same function. */ |
| 154 | if (cur_func == __gcov_indirect_call_callee |
Joseph Myers | 53d68b9 | 2014-09-05 13:03:46 +0100 | [diff] [blame] | 155 | || (__LIBGCC_VTABLE_USES_DESCRIPTORS__ && __gcov_indirect_call_callee |
Rong Xu | d6d3f03 | 2013-11-13 00:24:49 +0000 | [diff] [blame] | 156 | && *(void **) cur_func == *(void **) __gcov_indirect_call_callee)) |
| 157 | __gcov_one_value_profiler_body (__gcov_indirect_call_counters, value); |
| 158 | } |
| 159 | #endif |
| 160 | |
| 161 | #ifdef L_gcov_time_profiler |
| 162 | |
| 163 | /* Counter for first visit of each function. */ |
| 164 | static gcov_type function_counter; |
| 165 | |
| 166 | /* Sets corresponding COUNTERS if there is no value. */ |
| 167 | |
| 168 | void |
| 169 | __gcov_time_profiler (gcov_type* counters) |
| 170 | { |
| 171 | if (!counters[0]) |
| 172 | counters[0] = ++function_counter; |
| 173 | } |
| 174 | #endif |
| 175 | |
| 176 | #ifdef L_gcov_average_profiler |
| 177 | /* Increase corresponding COUNTER by VALUE. FIXME: Perhaps we want |
| 178 | to saturate up. */ |
| 179 | |
| 180 | void |
| 181 | __gcov_average_profiler (gcov_type *counters, gcov_type value) |
| 182 | { |
| 183 | counters[0] += value; |
| 184 | counters[1] ++; |
| 185 | } |
| 186 | #endif |
| 187 | |
| 188 | #ifdef L_gcov_ior_profiler |
| 189 | /* Bitwise-OR VALUE into COUNTER. */ |
| 190 | |
| 191 | void |
| 192 | __gcov_ior_profiler (gcov_type *counters, gcov_type value) |
| 193 | { |
| 194 | *counters |= value; |
| 195 | } |
| 196 | #endif |
| 197 | |
| 198 | #endif /* inhibit_libc */ |