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 "dynamic_annotations.h"
42
43/* Each function is empty and called (via a macro) only in debug mode.
44   The arguments are captured by dynamic tools at runtime. */
45
46#if DYNAMIC_ANNOTATIONS_ENABLED == 1
47
48void AnnotateRWLockCreate(const char *file, int line,
49                          const volatile void *lock){}
50void AnnotateRWLockDestroy(const char *file, int line,
51                           const volatile void *lock){}
52void AnnotateRWLockAcquired(const char *file, int line,
53                            const volatile void *lock, long is_w){}
54void AnnotateRWLockReleased(const char *file, int line,
55                            const volatile void *lock, long is_w){}
56void AnnotateBarrierInit(const char *file, int line,
57                         const volatile void *barrier, long count,
58                         long reinitialization_allowed) {}
59void AnnotateBarrierWaitBefore(const char *file, int line,
60                               const volatile void *barrier) {}
61void AnnotateBarrierWaitAfter(const char *file, int line,
62                              const volatile void *barrier) {}
63void AnnotateBarrierDestroy(const char *file, int line,
64                            const volatile void *barrier) {}
65
66void AnnotateCondVarWait(const char *file, int line,
67                         const volatile void *cv,
68                         const volatile void *lock){}
69void AnnotateCondVarSignal(const char *file, int line,
70                           const volatile void *cv){}
71void AnnotateCondVarSignalAll(const char *file, int line,
72                              const volatile void *cv){}
73void AnnotatePublishMemoryRange(const char *file, int line,
74                                const volatile void *address,
75                                long size){}
76void AnnotateUnpublishMemoryRange(const char *file, int line,
77                                  const volatile void *address,
78                                  long size){}
79void AnnotatePCQCreate(const char *file, int line,
80                       const volatile void *pcq){}
81void AnnotatePCQDestroy(const char *file, int line,
82                        const volatile void *pcq){}
83void AnnotatePCQPut(const char *file, int line,
84                    const volatile void *pcq){}
85void AnnotatePCQGet(const char *file, int line,
86                    const volatile void *pcq){}
87void AnnotateNewMemory(const char *file, int line,
88                       const volatile void *mem,
89                       long size){}
90void AnnotateExpectRace(const char *file, int line,
91                        const volatile void *mem,
92                        const char *description){}
93void AnnotateBenignRace(const char *file, int line,
94                        const volatile void *mem,
95                        const char *description){}
96void AnnotateBenignRaceSized(const char *file, int line,
97                             const volatile void *mem,
98                             long size,
99                             const char *description) {}
100void AnnotateMutexIsUsedAsCondVar(const char *file, int line,
101                                  const volatile void *mu){}
102void AnnotateTraceMemory(const char *file, int line,
103                         const volatile void *arg){}
104void AnnotateThreadName(const char *file, int line,
105                        const char *name){}
106void AnnotateIgnoreReadsBegin(const char *file, int line){}
107void AnnotateIgnoreReadsEnd(const char *file, int line){}
108void AnnotateIgnoreWritesBegin(const char *file, int line){}
109void AnnotateIgnoreWritesEnd(const char *file, int line){}
110void AnnotateIgnoreSyncBegin(const char *file, int line){}
111void AnnotateIgnoreSyncEnd(const char *file, int line){}
112void AnnotateEnableRaceDetection(const char *file, int line, int enable){}
113void AnnotateNoOp(const char *file, int line,
114                  const volatile void *arg){}
115void AnnotateFlushState(const char *file, int line){}
116
117static int GetRunningOnValgrind(void) {
118#ifdef RUNNING_ON_VALGRIND
119  if (RUNNING_ON_VALGRIND) return 1;
120#endif
121
122#ifndef _MSC_VER
123  const char *running_on_valgrind_str = getenv("RUNNING_ON_VALGRIND");
124  if (running_on_valgrind_str) {
125    return strcmp(running_on_valgrind_str, "0") != 0;
126  }
127#else
128  /* Visual Studio issues warnings if we use getenv,
129   * so we use GetEnvironmentVariableA instead.
130   */
131  char value[100] = "1";
132  int res = GetEnvironmentVariableA("RUNNING_ON_VALGRIND",
133                                    value, sizeof(value));
134  /* value will remain "1" if res == 0 or res >= sizeof(value). The latter
135   * can happen only if the given value is long, in this case it can't be "0".
136   */
137  if (res > 0 && !strcmp(value, "0"))
138    return 1;
139#endif
140  return 0;
141}
142
143/* See the comments in dynamic_annotations.h */
144int RunningOnValgrind(void) {
145  static volatile int running_on_valgrind = -1;
146  /* C doesn't have thread-safe initialization of statics, and we
147     don't want to depend on pthread_once here, so hack it. */
148  int local_running_on_valgrind = running_on_valgrind;
149  if (local_running_on_valgrind == -1)
150    running_on_valgrind = local_running_on_valgrind = GetRunningOnValgrind();
151  return local_running_on_valgrind;
152}
153
154#endif  /* DYNAMIC_ANNOTATIONS_ENABLED == 1 */
155