1 /* Copyright (c) 2011, Google Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Neither the name of Google Inc. nor the names of its
11 * contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 # error "This file should be built as pure C to avoid name mangling"
38 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
41 /* valgrind.h uses gcc extensions so it won't build with other compilers */
42 # include "base/third_party/valgrind/valgrind.h"
45 /* Each function is empty and called (via a macro) only in debug mode.
46 The arguments are captured by dynamic tools at runtime. */
48 #if DYNAMIC_ANNOTATIONS_ENABLED == 1
50 /* Identical code folding(-Wl,--icf=all) countermeasures.
51 This makes all Annotate* functions different, which prevents the linker from
54 #define DYNAMIC_ANNOTATIONS_IMPL \
55 volatile short lineno = (__LINE__ << 8) + __COUNTER__; (void)lineno;
57 #define DYNAMIC_ANNOTATIONS_IMPL \
58 volatile short lineno = (__LINE__ << 8); (void)lineno;
61 /* WARNING: always add new annotations to the end of the list.
62 Otherwise, lineno (see above) numbers for different Annotate* functions may
64 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockCreate)(
65 const char *file, int line, const volatile void *lock)
66 {DYNAMIC_ANNOTATIONS_IMPL}
68 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockDestroy)(
69 const char *file, int line, const volatile void *lock)
70 {DYNAMIC_ANNOTATIONS_IMPL}
72 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockAcquired)(
73 const char *file, int line, const volatile void *lock, long is_w)
74 {DYNAMIC_ANNOTATIONS_IMPL}
76 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockReleased)(
77 const char *file, int line, const volatile void *lock, long is_w)
78 {DYNAMIC_ANNOTATIONS_IMPL}
80 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierInit)(
81 const char *file, int line, const volatile void *barrier, long count,
82 long reinitialization_allowed)
83 {DYNAMIC_ANNOTATIONS_IMPL}
85 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitBefore)(
86 const char *file, int line, const volatile void *barrier)
87 {DYNAMIC_ANNOTATIONS_IMPL}
89 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitAfter)(
90 const char *file, int line, const volatile void *barrier)
91 {DYNAMIC_ANNOTATIONS_IMPL}
93 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierDestroy)(
94 const char *file, int line, const volatile void *barrier)
95 {DYNAMIC_ANNOTATIONS_IMPL}
97 void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarWait)(
98 const char *file, int line, const volatile void *cv,
99 const volatile void *lock)
100 {DYNAMIC_ANNOTATIONS_IMPL}
102 void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignal)(
103 const char *file, int line, const volatile void *cv)
104 {DYNAMIC_ANNOTATIONS_IMPL}
106 void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignalAll)(
107 const char *file, int line, const volatile void *cv)
108 {DYNAMIC_ANNOTATIONS_IMPL}
110 void DYNAMIC_ANNOTATIONS_NAME(AnnotateHappensBefore)(
111 const char *file, int line, const volatile void *obj)
112 {DYNAMIC_ANNOTATIONS_IMPL};
114 void DYNAMIC_ANNOTATIONS_NAME(AnnotateHappensAfter)(
115 const char *file, int line, const volatile void *obj)
116 {DYNAMIC_ANNOTATIONS_IMPL};
118 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePublishMemoryRange)(
119 const char *file, int line, const volatile void *address, long size)
120 {DYNAMIC_ANNOTATIONS_IMPL}
122 void DYNAMIC_ANNOTATIONS_NAME(AnnotateUnpublishMemoryRange)(
123 const char *file, int line, const volatile void *address, long size)
124 {DYNAMIC_ANNOTATIONS_IMPL}
126 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQCreate)(
127 const char *file, int line, const volatile void *pcq)
128 {DYNAMIC_ANNOTATIONS_IMPL}
130 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQDestroy)(
131 const char *file, int line, const volatile void *pcq)
132 {DYNAMIC_ANNOTATIONS_IMPL}
134 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQPut)(
135 const char *file, int line, const volatile void *pcq)
136 {DYNAMIC_ANNOTATIONS_IMPL}
138 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQGet)(
139 const char *file, int line, const volatile void *pcq)
140 {DYNAMIC_ANNOTATIONS_IMPL}
142 void DYNAMIC_ANNOTATIONS_NAME(AnnotateNewMemory)(
143 const char *file, int line, const volatile void *mem, long size)
144 {DYNAMIC_ANNOTATIONS_IMPL}
146 void DYNAMIC_ANNOTATIONS_NAME(AnnotateExpectRace)(
147 const char *file, int line, const volatile void *mem,
148 const char *description)
149 {DYNAMIC_ANNOTATIONS_IMPL}
151 void DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushExpectedRaces)(
152 const char *file, int line)
153 {DYNAMIC_ANNOTATIONS_IMPL}
155 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRace)(
156 const char *file, int line, const volatile void *mem,
157 const char *description)
158 {DYNAMIC_ANNOTATIONS_IMPL}
160 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRaceSized)(
161 const char *file, int line, const volatile void *mem, long size,
162 const char *description)
163 {DYNAMIC_ANNOTATIONS_IMPL}
165 void DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsUsedAsCondVar)(
166 const char *file, int line, const volatile void *mu)
167 {DYNAMIC_ANNOTATIONS_IMPL}
169 void DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsNotPHB)(
170 const char *file, int line, const volatile void *mu)
171 {DYNAMIC_ANNOTATIONS_IMPL}
173 void DYNAMIC_ANNOTATIONS_NAME(AnnotateTraceMemory)(
174 const char *file, int line, const volatile void *arg)
175 {DYNAMIC_ANNOTATIONS_IMPL}
177 void DYNAMIC_ANNOTATIONS_NAME(AnnotateThreadName)(
178 const char *file, int line, const char *name)
179 {DYNAMIC_ANNOTATIONS_IMPL}
181 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsBegin)(
182 const char *file, int line)
183 {DYNAMIC_ANNOTATIONS_IMPL}
185 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsEnd)(
186 const char *file, int line)
187 {DYNAMIC_ANNOTATIONS_IMPL}
189 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesBegin)(
190 const char *file, int line)
191 {DYNAMIC_ANNOTATIONS_IMPL}
193 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesEnd)(
194 const char *file, int line)
195 {DYNAMIC_ANNOTATIONS_IMPL}
197 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncBegin)(
198 const char *file, int line)
199 {DYNAMIC_ANNOTATIONS_IMPL}
201 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncEnd)(
202 const char *file, int line)
203 {DYNAMIC_ANNOTATIONS_IMPL}
205 void DYNAMIC_ANNOTATIONS_NAME(AnnotateEnableRaceDetection)(
206 const char *file, int line, int enable)
207 {DYNAMIC_ANNOTATIONS_IMPL}
209 void DYNAMIC_ANNOTATIONS_NAME(AnnotateNoOp)(
210 const char *file, int line, const volatile void *arg)
211 {DYNAMIC_ANNOTATIONS_IMPL}
213 void DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushState)(
214 const char *file, int line)
215 {DYNAMIC_ANNOTATIONS_IMPL}
217 #endif /* DYNAMIC_ANNOTATIONS_ENABLED == 1 */
219 #if DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND == 1
220 static int GetRunningOnValgrind(void) {
221 #ifdef RUNNING_ON_VALGRIND
222 if (RUNNING_ON_VALGRIND) return 1;
226 char *running_on_valgrind_str = getenv("RUNNING_ON_VALGRIND");
227 if (running_on_valgrind_str) {
228 return strcmp(running_on_valgrind_str, "0") != 0;
231 /* Visual Studio issues warnings if we use getenv,
232 * so we use GetEnvironmentVariableA instead.
234 char value[100] = "1";
235 int res = GetEnvironmentVariableA("RUNNING_ON_VALGRIND",
236 value, sizeof(value));
237 /* value will remain "1" if res == 0 or res >= sizeof(value). The latter
238 * can happen only if the given value is long, in this case it can't be "0".
240 if (res > 0 && strcmp(value, "0") != 0)
246 /* See the comments in dynamic_annotations.h */
247 int RunningOnValgrind(void) {
248 static volatile int running_on_valgrind = -1;
249 /* C doesn't have thread-safe initialization of statics, and we
250 don't want to depend on pthread_once here, so hack it. */
251 int local_running_on_valgrind = running_on_valgrind;
252 if (local_running_on_valgrind == -1)
253 running_on_valgrind = local_running_on_valgrind = GetRunningOnValgrind();
254 return local_running_on_valgrind;
257 #endif /* DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND == 1 */