dynamic_annotations.c 6.6 KB

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