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