OLD | NEW |
(Empty) | |
| 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 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 * |
| 30 * --- |
| 31 * Author: Kostya Serebryany |
| 32 */ |
| 33 |
| 34 #ifdef __cplusplus |
| 35 # error "This file should be built as pure C to avoid name mangling" |
| 36 #endif |
| 37 |
| 38 #include "config.h" |
| 39 #include <stdlib.h> |
| 40 #include <string.h> |
| 41 |
| 42 #include "base/dynamic_annotations.h" |
| 43 |
| 44 #ifdef __GNUC__ |
| 45 /* valgrind.h uses gcc extensions so it won't build with other compilers */ |
| 46 # ifdef HAVE_VALGRIND_H /* prefer the user's copy if they have it */ |
| 47 # include <valgrind.h> |
| 48 # else /* otherwise just use the copy that we have */ |
| 49 # include "third_party/valgrind.h" |
| 50 # endif |
| 51 #endif |
| 52 |
| 53 /* Each function is empty and called (via a macro) only in debug mode. |
| 54 The arguments are captured by dynamic tools at runtime. */ |
| 55 |
| 56 #if DYNAMIC_ANNOTATIONS_ENABLED == 1 |
| 57 |
| 58 void AnnotateRWLockCreate(const char *file, int line, |
| 59 const volatile void *lock){} |
| 60 void AnnotateRWLockDestroy(const char *file, int line, |
| 61 const volatile void *lock){} |
| 62 void AnnotateRWLockAcquired(const char *file, int line, |
| 63 const volatile void *lock, long is_w){} |
| 64 void AnnotateRWLockReleased(const char *file, int line, |
| 65 const volatile void *lock, long is_w){} |
| 66 void AnnotateBarrierInit(const char *file, int line, |
| 67 const volatile void *barrier, long count, |
| 68 long reinitialization_allowed) {} |
| 69 void AnnotateBarrierWaitBefore(const char *file, int line, |
| 70 const volatile void *barrier) {} |
| 71 void AnnotateBarrierWaitAfter(const char *file, int line, |
| 72 const volatile void *barrier) {} |
| 73 void AnnotateBarrierDestroy(const char *file, int line, |
| 74 const volatile void *barrier) {} |
| 75 |
| 76 void AnnotateCondVarWait(const char *file, int line, |
| 77 const volatile void *cv, |
| 78 const volatile void *lock){} |
| 79 void AnnotateCondVarSignal(const char *file, int line, |
| 80 const volatile void *cv){} |
| 81 void AnnotateCondVarSignalAll(const char *file, int line, |
| 82 const volatile void *cv){} |
| 83 void AnnotatePublishMemoryRange(const char *file, int line, |
| 84 const volatile void *address, |
| 85 long size){} |
| 86 void AnnotateUnpublishMemoryRange(const char *file, int line, |
| 87 const volatile void *address, |
| 88 long size){} |
| 89 void AnnotatePCQCreate(const char *file, int line, |
| 90 const volatile void *pcq){} |
| 91 void AnnotatePCQDestroy(const char *file, int line, |
| 92 const volatile void *pcq){} |
| 93 void AnnotatePCQPut(const char *file, int line, |
| 94 const volatile void *pcq){} |
| 95 void AnnotatePCQGet(const char *file, int line, |
| 96 const volatile void *pcq){} |
| 97 void AnnotateNewMemory(const char *file, int line, |
| 98 const volatile void *mem, |
| 99 long size){} |
| 100 void AnnotateExpectRace(const char *file, int line, |
| 101 const volatile void *mem, |
| 102 const char *description){} |
| 103 void AnnotateBenignRace(const char *file, int line, |
| 104 const volatile void *mem, |
| 105 const char *description){} |
| 106 void AnnotateBenignRaceSized(const char *file, int line, |
| 107 const volatile void *mem, |
| 108 long size, |
| 109 const char *description) {} |
| 110 void AnnotateMutexIsUsedAsCondVar(const char *file, int line, |
| 111 const volatile void *mu){} |
| 112 void AnnotateTraceMemory(const char *file, int line, |
| 113 const volatile void *arg){} |
| 114 void AnnotateThreadName(const char *file, int line, |
| 115 const char *name){} |
| 116 void AnnotateIgnoreReadsBegin(const char *file, int line){} |
| 117 void AnnotateIgnoreReadsEnd(const char *file, int line){} |
| 118 void AnnotateIgnoreWritesBegin(const char *file, int line){} |
| 119 void AnnotateIgnoreWritesEnd(const char *file, int line){} |
| 120 void AnnotateEnableRaceDetection(const char *file, int line, int enable){} |
| 121 void AnnotateNoOp(const char *file, int line, |
| 122 const volatile void *arg){} |
| 123 void AnnotateFlushState(const char *file, int line){} |
| 124 |
| 125 #endif /* DYNAMIC_ANNOTATIONS_ENABLED == 1 */ |
| 126 |
| 127 static int GetRunningOnValgrind(void) { |
| 128 #ifdef RUNNING_ON_VALGRIND |
| 129 if (RUNNING_ON_VALGRIND) return 1; |
| 130 #endif |
| 131 #ifdef _MSC_VER |
| 132 /* Visual Studio can complain about getenv, so use a windows equivalent. */ |
| 133 char value[100] = "1"; /* something that is not "0" */ |
| 134 int res = GetEnvironmentVariableA("RUNNING_ON_VALGRIND", |
| 135 value, sizeof(value)); |
| 136 /* value will remain "1" if the called failed for some reason. */ |
| 137 return (res > 0 && strcmp(value, "0") != 0); |
| 138 #else |
| 139 /* TODO(csilvers): use GetenvBeforeMain() instead? Will need to |
| 140 * change it to be extern "C". |
| 141 */ |
| 142 char *running_on_valgrind_str = getenv("RUNNING_ON_VALGRIND"); |
| 143 if (running_on_valgrind_str) { |
| 144 return strcmp(running_on_valgrind_str, "0") != 0; |
| 145 } |
| 146 return 0; |
| 147 #endif |
| 148 } |
| 149 |
| 150 /* See the comments in dynamic_annotations.h */ |
| 151 int RunningOnValgrind(void) { |
| 152 static volatile int running_on_valgrind = -1; |
| 153 int local_running_on_valgrind = running_on_valgrind; |
| 154 /* C doesn't have thread-safe initialization of statics, and we |
| 155 don't want to depend on pthread_once here, so hack it. */ |
| 156 ANNOTATE_BENIGN_RACE(&running_on_valgrind, "safe hack"); |
| 157 if (local_running_on_valgrind == -1) |
| 158 running_on_valgrind = local_running_on_valgrind = GetRunningOnValgrind(); |
| 159 return local_running_on_valgrind; |
| 160 } |
| 161 |
| 162 /* See the comments in dynamic_annotations.h */ |
| 163 double ValgrindSlowdown(void) { |
| 164 /* Same initialization hack as in RunningOnValgrind(). */ |
| 165 static volatile double slowdown = 0.0; |
| 166 double local_slowdown = slowdown; |
| 167 ANNOTATE_BENIGN_RACE(&slowdown, "safe hack"); |
| 168 if (RunningOnValgrind() == 0) { |
| 169 return 1.0; |
| 170 } |
| 171 if (local_slowdown == 0.0) { |
| 172 char *env = getenv("VALGRIND_SLOWDOWN"); |
| 173 slowdown = local_slowdown = env ? atof(env) : 50.0; |
| 174 } |
| 175 return local_slowdown; |
| 176 } |
OLD | NEW |