1 /* Copyright (c) 2008-2009, Google Inc.
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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.
13 *
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.
25 *
26 * ---
27 * Author: Kostya Serebryany
28 */
29
30 #ifdef _MSC_VER
31 # include <windows.h>
32 #endif
33
34 #ifdef __cplusplus
35 # error "This file should be built as pure C to avoid name mangling"
36 #endif
37
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
42
43 #ifdef __GNUC__
44 /* valgrind.h uses gcc extensions so it won't build with other compilers */
45 # include "base/third_party/valgrind/valgrind.h"
46 #endif
47
48 /* Each function is empty and called (via a macro) only in debug mode.
49 The arguments are captured by dynamic tools at runtime. */
50
51 #if DYNAMIC_ANNOTATIONS_ENABLED == 1
52
DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockCreate)53 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockCreate)(
54 const char *file, int line, const volatile void *lock){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockDestroy)55 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockDestroy)(
56 const char *file, int line, const volatile void *lock){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockAcquired)57 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockAcquired)(
58 const char *file, int line, const volatile void *lock, long is_w){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockReleased)59 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockReleased)(
60 const char *file, int line, const volatile void *lock, long is_w){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierInit)61 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierInit)(
62 const char *file, int line, const volatile void *barrier, long count,
63 long reinitialization_allowed) {}
DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitBefore)64 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitBefore)(
65 const char *file, int line, const volatile void *barrier) {}
DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitAfter)66 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitAfter)(
67 const char *file, int line, const volatile void *barrier) {}
DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierDestroy)68 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierDestroy)(
69 const char *file, int line, const volatile void *barrier) {}
70
DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarWait)71 void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarWait)(
72 const char *file, int line, const volatile void *cv,
73 const volatile void *lock){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignal)74 void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignal)(
75 const char *file, int line, const volatile void *cv){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignalAll)76 void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignalAll)(
77 const char *file, int line, const volatile void *cv){}
DYNAMIC_ANNOTATIONS_NAME(AnnotatePublishMemoryRange)78 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePublishMemoryRange)(
79 const char *file, int line, const volatile void *address, long size){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateUnpublishMemoryRange)80 void DYNAMIC_ANNOTATIONS_NAME(AnnotateUnpublishMemoryRange)(
81 const char *file, int line, const volatile void *address, long size){}
DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQCreate)82 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQCreate)(
83 const char *file, int line, const volatile void *pcq){}
DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQDestroy)84 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQDestroy)(
85 const char *file, int line, const volatile void *pcq){}
DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQPut)86 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQPut)(
87 const char *file, int line, const volatile void *pcq){}
DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQGet)88 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQGet)(
89 const char *file, int line, const volatile void *pcq){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateNewMemory)90 void DYNAMIC_ANNOTATIONS_NAME(AnnotateNewMemory)(
91 const char *file, int line, const volatile void *mem, long size){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateExpectRace)92 void DYNAMIC_ANNOTATIONS_NAME(AnnotateExpectRace)(
93 const char *file, int line, const volatile void *mem,
94 const char *description){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushExpectedRaces)95 void DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushExpectedRaces)(
96 const char *file, int line){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRace)97 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRace)(
98 const char *file, int line, const volatile void *mem,
99 const char *description){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRaceSized)100 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRaceSized)(
101 const char *file, int line, const volatile void *mem, long size,
102 const char *description){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsUsedAsCondVar)103 void DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsUsedAsCondVar)(
104 const char *file, int line, const volatile void *mu){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsNotPHB)105 void DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsNotPHB)(
106 const char *file, int line, const volatile void *mu){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateTraceMemory)107 void DYNAMIC_ANNOTATIONS_NAME(AnnotateTraceMemory)(
108 const char *file, int line, const volatile void *arg){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateThreadName)109 void DYNAMIC_ANNOTATIONS_NAME(AnnotateThreadName)(
110 const char *file, int line, const char *name){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsBegin)111 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsBegin)(
112 const char *file, int line){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsEnd)113 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsEnd)(
114 const char *file, int line){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesBegin)115 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesBegin)(
116 const char *file, int line){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesEnd)117 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesEnd)(
118 const char *file, int line){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncBegin)119 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncBegin)(
120 const char *file, int line){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncEnd)121 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncEnd)(
122 const char *file, int line){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateEnableRaceDetection)123 void DYNAMIC_ANNOTATIONS_NAME(AnnotateEnableRaceDetection)(
124 const char *file, int line, int enable){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateNoOp)125 void DYNAMIC_ANNOTATIONS_NAME(AnnotateNoOp)(
126 const char *file, int line, const volatile void *arg){}
DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushState)127 void DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushState)(
128 const char *file, int line){}
129
130 #endif /* DYNAMIC_ANNOTATIONS_ENABLED == 1 */
131
132 #if DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND == 1
GetRunningOnValgrind(void)133 static int GetRunningOnValgrind(void) {
134 #ifdef RUNNING_ON_VALGRIND
135 if (RUNNING_ON_VALGRIND) return 1;
136 #endif
137
138 #ifndef _MSC_VER
139 char *running_on_valgrind_str = getenv("RUNNING_ON_VALGRIND");
140 if (running_on_valgrind_str) {
141 return strcmp(running_on_valgrind_str, "0") != 0;
142 }
143 #else
144 /* Visual Studio issues warnings if we use getenv,
145 * so we use GetEnvironmentVariableA instead.
146 */
147 char value[100] = "1";
148 int res = GetEnvironmentVariableA("RUNNING_ON_VALGRIND",
149 value, sizeof(value));
150 /* value will remain "1" if res == 0 or res >= sizeof(value). The latter
151 * can happen only if the given value is long, in this case it can't be "0".
152 */
153 if (res > 0 && strcmp(value, "0") != 0)
154 return 1;
155 #endif
156 return 0;
157 }
158
159 /* See the comments in dynamic_annotations.h */
RunningOnValgrind(void)160 int RunningOnValgrind(void) {
161 static volatile int running_on_valgrind = -1;
162 /* C doesn't have thread-safe initialization of statics, and we
163 don't want to depend on pthread_once here, so hack it. */
164 int local_running_on_valgrind = running_on_valgrind;
165 if (local_running_on_valgrind == -1)
166 running_on_valgrind = local_running_on_valgrind = GetRunningOnValgrind();
167 return local_running_on_valgrind;
168 }
169
170 #endif /* DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND == 1 */
171