Initialize Tizen 2.3
[external/chromium.git] / base / third_party / dynamic_annotations / dynamic_annotations.c
1 /* Copyright (c) 2011, 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 #ifdef _MSC_VER
28 # include <windows.h>
29 #endif
30
31 #ifdef __cplusplus
32 # error "This file should be built as pure C to avoid name mangling"
33 #endif
34
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
39
40 #ifdef __GNUC__
41 /* valgrind.h uses gcc extensions so it won't build with other compilers */
42 # include "base/third_party/valgrind/valgrind.h"
43 #endif
44
45 /* Each function is empty and called (via a macro) only in debug mode.
46    The arguments are captured by dynamic tools at runtime. */
47
48 #if DYNAMIC_ANNOTATIONS_ENABLED == 1
49
50 /* Identical code folding(-Wl,--icf=all) countermeasures.
51    This makes all Annotate* functions different, which prevents the linker from
52    folding them. */
53 #ifdef __COUNTER__
54 #define DYNAMIC_ANNOTATIONS_IMPL \
55   volatile short lineno = (__LINE__ << 8) + __COUNTER__; (void)lineno;
56 #else
57 #define DYNAMIC_ANNOTATIONS_IMPL \
58   volatile short lineno = (__LINE__ << 8); (void)lineno;
59 #endif
60
61 /* WARNING: always add new annotations to the end of the list.
62    Otherwise, lineno (see above) numbers for different Annotate* functions may
63    conflict. */
64 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockCreate)(
65     const char *file, int line, const volatile void *lock)
66 {DYNAMIC_ANNOTATIONS_IMPL}
67
68 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockDestroy)(
69     const char *file, int line, const volatile void *lock)
70 {DYNAMIC_ANNOTATIONS_IMPL}
71
72 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockAcquired)(
73     const char *file, int line, const volatile void *lock, long is_w)
74 {DYNAMIC_ANNOTATIONS_IMPL}
75
76 void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockReleased)(
77     const char *file, int line, const volatile void *lock, long is_w)
78 {DYNAMIC_ANNOTATIONS_IMPL}
79
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}
84
85 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitBefore)(
86     const char *file, int line, const volatile void *barrier)
87 {DYNAMIC_ANNOTATIONS_IMPL}
88
89 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitAfter)(
90     const char *file, int line, const volatile void *barrier)
91 {DYNAMIC_ANNOTATIONS_IMPL}
92
93 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierDestroy)(
94     const char *file, int line, const volatile void *barrier)
95 {DYNAMIC_ANNOTATIONS_IMPL}
96
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}
101
102 void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignal)(
103     const char *file, int line, const volatile void *cv)
104 {DYNAMIC_ANNOTATIONS_IMPL}
105
106 void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignalAll)(
107     const char *file, int line, const volatile void *cv)
108 {DYNAMIC_ANNOTATIONS_IMPL}
109
110 void DYNAMIC_ANNOTATIONS_NAME(AnnotateHappensBefore)(
111     const char *file, int line, const volatile void *obj)
112 {DYNAMIC_ANNOTATIONS_IMPL};
113
114 void DYNAMIC_ANNOTATIONS_NAME(AnnotateHappensAfter)(
115     const char *file, int line, const volatile void *obj)
116 {DYNAMIC_ANNOTATIONS_IMPL};
117
118 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePublishMemoryRange)(
119     const char *file, int line, const volatile void *address, long size)
120 {DYNAMIC_ANNOTATIONS_IMPL}
121
122 void DYNAMIC_ANNOTATIONS_NAME(AnnotateUnpublishMemoryRange)(
123     const char *file, int line, const volatile void *address, long size)
124 {DYNAMIC_ANNOTATIONS_IMPL}
125
126 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQCreate)(
127     const char *file, int line, const volatile void *pcq)
128 {DYNAMIC_ANNOTATIONS_IMPL}
129
130 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQDestroy)(
131     const char *file, int line, const volatile void *pcq)
132 {DYNAMIC_ANNOTATIONS_IMPL}
133
134 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQPut)(
135     const char *file, int line, const volatile void *pcq)
136 {DYNAMIC_ANNOTATIONS_IMPL}
137
138 void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQGet)(
139     const char *file, int line, const volatile void *pcq)
140 {DYNAMIC_ANNOTATIONS_IMPL}
141
142 void DYNAMIC_ANNOTATIONS_NAME(AnnotateNewMemory)(
143     const char *file, int line, const volatile void *mem, long size)
144 {DYNAMIC_ANNOTATIONS_IMPL}
145
146 void DYNAMIC_ANNOTATIONS_NAME(AnnotateExpectRace)(
147     const char *file, int line, const volatile void *mem,
148     const char *description)
149 {DYNAMIC_ANNOTATIONS_IMPL}
150
151 void DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushExpectedRaces)(
152     const char *file, int line)
153 {DYNAMIC_ANNOTATIONS_IMPL}
154
155 void DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRace)(
156     const char *file, int line, const volatile void *mem,
157     const char *description)
158 {DYNAMIC_ANNOTATIONS_IMPL}
159
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}
164
165 void DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsUsedAsCondVar)(
166     const char *file, int line, const volatile void *mu)
167 {DYNAMIC_ANNOTATIONS_IMPL}
168
169 void DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsNotPHB)(
170     const char *file, int line, const volatile void *mu)
171 {DYNAMIC_ANNOTATIONS_IMPL}
172
173 void DYNAMIC_ANNOTATIONS_NAME(AnnotateTraceMemory)(
174     const char *file, int line, const volatile void *arg)
175 {DYNAMIC_ANNOTATIONS_IMPL}
176
177 void DYNAMIC_ANNOTATIONS_NAME(AnnotateThreadName)(
178     const char *file, int line, const char *name)
179 {DYNAMIC_ANNOTATIONS_IMPL}
180
181 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsBegin)(
182     const char *file, int line)
183 {DYNAMIC_ANNOTATIONS_IMPL}
184
185 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsEnd)(
186     const char *file, int line)
187 {DYNAMIC_ANNOTATIONS_IMPL}
188
189 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesBegin)(
190     const char *file, int line)
191 {DYNAMIC_ANNOTATIONS_IMPL}
192
193 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesEnd)(
194     const char *file, int line)
195 {DYNAMIC_ANNOTATIONS_IMPL}
196
197 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncBegin)(
198     const char *file, int line)
199 {DYNAMIC_ANNOTATIONS_IMPL}
200
201 void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncEnd)(
202     const char *file, int line)
203 {DYNAMIC_ANNOTATIONS_IMPL}
204
205 void DYNAMIC_ANNOTATIONS_NAME(AnnotateEnableRaceDetection)(
206     const char *file, int line, int enable)
207 {DYNAMIC_ANNOTATIONS_IMPL}
208
209 void DYNAMIC_ANNOTATIONS_NAME(AnnotateNoOp)(
210     const char *file, int line, const volatile void *arg)
211 {DYNAMIC_ANNOTATIONS_IMPL}
212
213 void DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushState)(
214     const char *file, int line)
215 {DYNAMIC_ANNOTATIONS_IMPL}
216
217 #endif  /* DYNAMIC_ANNOTATIONS_ENABLED == 1 */
218
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;
223 #endif
224
225 #ifndef _MSC_VER
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;
229   }
230 #else
231   /* Visual Studio issues warnings if we use getenv,
232    * so we use GetEnvironmentVariableA instead.
233    */
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".
239    */
240   if (res > 0 && strcmp(value, "0") != 0)
241     return 1;
242 #endif
243   return 0;
244 }
245
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;
255 }
256
257 #endif /* DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND == 1 */