dynamic_annotations.c revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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
53void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockCreate)(
54    const char *file, int line, const volatile void *lock){}
55void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockDestroy)(
56    const char *file, int line, const volatile void *lock){}
57void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockAcquired)(
58    const char *file, int line, const volatile void *lock, long is_w){}
59void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockReleased)(
60    const char *file, int line, const volatile void *lock, long is_w){}
61void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierInit)(
62    const char *file, int line, const volatile void *barrier, long count,
63    long reinitialization_allowed) {}
64void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitBefore)(
65    const char *file, int line, const volatile void *barrier) {}
66void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitAfter)(
67    const char *file, int line, const volatile void *barrier) {}
68void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierDestroy)(
69    const char *file, int line, const volatile void *barrier) {}
70
71void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarWait)(
72    const char *file, int line, const volatile void *cv,
73    const volatile void *lock){}
74void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignal)(
75    const char *file, int line, const volatile void *cv){}
76void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignalAll)(
77    const char *file, int line, const volatile void *cv){}
78void DYNAMIC_ANNOTATIONS_NAME(AnnotatePublishMemoryRange)(
79    const char *file, int line, const volatile void *address, long size){}
80void DYNAMIC_ANNOTATIONS_NAME(AnnotateUnpublishMemoryRange)(
81    const char *file, int line, const volatile void *address, long size){}
82void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQCreate)(
83    const char *file, int line, const volatile void *pcq){}
84void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQDestroy)(
85    const char *file, int line, const volatile void *pcq){}
86void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQPut)(
87    const char *file, int line, const volatile void *pcq){}
88void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQGet)(
89    const char *file, int line, const volatile void *pcq){}
90void DYNAMIC_ANNOTATIONS_NAME(AnnotateNewMemory)(
91    const char *file, int line, const volatile void *mem, long size){}
92void DYNAMIC_ANNOTATIONS_NAME(AnnotateExpectRace)(
93    const char *file, int line, const volatile void *mem,
94    const char *description){}
95void DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushExpectedRaces)(
96    const char *file, int line){}
97void DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRace)(
98    const char *file, int line, const volatile void *mem,
99    const char *description){}
100void DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRaceSized)(
101    const char *file, int line, const volatile void *mem, long size,
102    const char *description){}
103void DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsUsedAsCondVar)(
104    const char *file, int line, const volatile void *mu){}
105void DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsNotPHB)(
106    const char *file, int line, const volatile void *mu){}
107void DYNAMIC_ANNOTATIONS_NAME(AnnotateTraceMemory)(
108    const char *file, int line, const volatile void *arg){}
109void DYNAMIC_ANNOTATIONS_NAME(AnnotateThreadName)(
110    const char *file, int line, const char *name){}
111void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsBegin)(
112    const char *file, int line){}
113void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsEnd)(
114    const char *file, int line){}
115void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesBegin)(
116    const char *file, int line){}
117void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesEnd)(
118    const char *file, int line){}
119void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncBegin)(
120    const char *file, int line){}
121void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncEnd)(
122    const char *file, int line){}
123void DYNAMIC_ANNOTATIONS_NAME(AnnotateEnableRaceDetection)(
124    const char *file, int line, int enable){}
125void DYNAMIC_ANNOTATIONS_NAME(AnnotateNoOp)(
126    const char *file, int line, const volatile void *arg){}
127void 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
133static 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 */
160int 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