dynamic_annotations.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. /* Copyright (c) 2011, 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. /* This file defines dynamic annotations for use with dynamic analysis
  27. tool such as valgrind, PIN, etc.
  28. Dynamic annotation is a source code annotation that affects
  29. the generated code (that is, the annotation is not a comment).
  30. Each such annotation is attached to a particular
  31. instruction and/or to a particular object (address) in the program.
  32. The annotations that should be used by users are macros in all upper-case
  33. (e.g., ANNOTATE_NEW_MEMORY).
  34. Actual implementation of these macros may differ depending on the
  35. dynamic analysis tool being used.
  36. See http://code.google.com/p/data-race-test/ for more information.
  37. This file supports the following dynamic analysis tools:
  38. - None (DYNAMIC_ANNOTATIONS_ENABLED is not defined or zero).
  39. Macros are defined empty.
  40. - ThreadSanitizer, Helgrind, DRD (DYNAMIC_ANNOTATIONS_ENABLED is 1).
  41. Macros are defined as calls to non-inlinable empty functions
  42. that are intercepted by Valgrind. */
  43. #ifndef __DYNAMIC_ANNOTATIONS_H__
  44. #define __DYNAMIC_ANNOTATIONS_H__
  45. #ifndef DYNAMIC_ANNOTATIONS_PREFIX
  46. # define DYNAMIC_ANNOTATIONS_PREFIX
  47. #endif
  48. #ifndef DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND
  49. # define DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND 1
  50. #endif
  51. #ifdef DYNAMIC_ANNOTATIONS_WANT_ATTRIBUTE_WEAK
  52. # ifdef __GNUC__
  53. # define DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK __attribute__((weak))
  54. # else
  55. /* TODO(glider): for Windows support we may want to change this macro in order
  56. to prepend __declspec(selectany) to the annotations' declarations. */
  57. # error weak annotations are not supported for your compiler
  58. # endif
  59. #else
  60. # define DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK
  61. #endif
  62. /* The following preprocessor magic prepends the value of
  63. DYNAMIC_ANNOTATIONS_PREFIX to annotation function names. */
  64. #define DYNAMIC_ANNOTATIONS_GLUE0(A, B) A##B
  65. #define DYNAMIC_ANNOTATIONS_GLUE(A, B) DYNAMIC_ANNOTATIONS_GLUE0(A, B)
  66. #define DYNAMIC_ANNOTATIONS_NAME(name) \
  67. DYNAMIC_ANNOTATIONS_GLUE(DYNAMIC_ANNOTATIONS_PREFIX, name)
  68. #ifndef DYNAMIC_ANNOTATIONS_ENABLED
  69. # define DYNAMIC_ANNOTATIONS_ENABLED 0
  70. #endif
  71. #if DYNAMIC_ANNOTATIONS_ENABLED != 0
  72. /* -------------------------------------------------------------
  73. Annotations useful when implementing condition variables such as CondVar,
  74. using conditional critical sections (Await/LockWhen) and when constructing
  75. user-defined synchronization mechanisms.
  76. The annotations ANNOTATE_HAPPENS_BEFORE() and ANNOTATE_HAPPENS_AFTER() can
  77. be used to define happens-before arcs in user-defined synchronization
  78. mechanisms: the race detector will infer an arc from the former to the
  79. latter when they share the same argument pointer.
  80. Example 1 (reference counting):
  81. void Unref() {
  82. ANNOTATE_HAPPENS_BEFORE(&refcount_);
  83. if (AtomicDecrementByOne(&refcount_) == 0) {
  84. ANNOTATE_HAPPENS_AFTER(&refcount_);
  85. delete this;
  86. }
  87. }
  88. Example 2 (message queue):
  89. void MyQueue::Put(Type *e) {
  90. MutexLock lock(&mu_);
  91. ANNOTATE_HAPPENS_BEFORE(e);
  92. PutElementIntoMyQueue(e);
  93. }
  94. Type *MyQueue::Get() {
  95. MutexLock lock(&mu_);
  96. Type *e = GetElementFromMyQueue();
  97. ANNOTATE_HAPPENS_AFTER(e);
  98. return e;
  99. }
  100. Note: when possible, please use the existing reference counting and message
  101. queue implementations instead of inventing new ones. */
  102. /* Report that wait on the condition variable at address "cv" has succeeded
  103. and the lock at address "lock" is held. */
  104. #define ANNOTATE_CONDVAR_LOCK_WAIT(cv, lock) \
  105. DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarWait)(__FILE__, __LINE__, cv, lock)
  106. /* Report that wait on the condition variable at "cv" has succeeded. Variant
  107. w/o lock. */
  108. #define ANNOTATE_CONDVAR_WAIT(cv) \
  109. DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarWait)(__FILE__, __LINE__, cv, NULL)
  110. /* Report that we are about to signal on the condition variable at address
  111. "cv". */
  112. #define ANNOTATE_CONDVAR_SIGNAL(cv) \
  113. DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignal)(__FILE__, __LINE__, cv)
  114. /* Report that we are about to signal_all on the condition variable at address
  115. "cv". */
  116. #define ANNOTATE_CONDVAR_SIGNAL_ALL(cv) \
  117. DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignalAll)(__FILE__, __LINE__, cv)
  118. /* Annotations for user-defined synchronization mechanisms. */
  119. #define ANNOTATE_HAPPENS_BEFORE(obj) \
  120. DYNAMIC_ANNOTATIONS_NAME(AnnotateHappensBefore)(__FILE__, __LINE__, obj)
  121. #define ANNOTATE_HAPPENS_AFTER(obj) \
  122. DYNAMIC_ANNOTATIONS_NAME(AnnotateHappensAfter)(__FILE__, __LINE__, obj)
  123. /* DEPRECATED. Don't use it. */
  124. #define ANNOTATE_PUBLISH_MEMORY_RANGE(pointer, size) \
  125. DYNAMIC_ANNOTATIONS_NAME(AnnotatePublishMemoryRange)(__FILE__, __LINE__, \
  126. pointer, size)
  127. /* DEPRECATED. Don't use it. */
  128. #define ANNOTATE_UNPUBLISH_MEMORY_RANGE(pointer, size) \
  129. DYNAMIC_ANNOTATIONS_NAME(AnnotateUnpublishMemoryRange)(__FILE__, __LINE__, \
  130. pointer, size)
  131. /* DEPRECATED. Don't use it. */
  132. #define ANNOTATE_SWAP_MEMORY_RANGE(pointer, size) \
  133. do { \
  134. ANNOTATE_UNPUBLISH_MEMORY_RANGE(pointer, size); \
  135. ANNOTATE_PUBLISH_MEMORY_RANGE(pointer, size); \
  136. } while (0)
  137. /* Instruct the tool to create a happens-before arc between mu->Unlock() and
  138. mu->Lock(). This annotation may slow down the race detector and hide real
  139. races. Normally it is used only when it would be difficult to annotate each
  140. of the mutex's critical sections individually using the annotations above.
  141. This annotation makes sense only for hybrid race detectors. For pure
  142. happens-before detectors this is a no-op. For more details see
  143. http://code.google.com/p/data-race-test/wiki/PureHappensBeforeVsHybrid . */
  144. #define ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(mu) \
  145. DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsUsedAsCondVar)(__FILE__, __LINE__, \
  146. mu)
  147. /* Opposite to ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX.
  148. Instruct the tool to NOT create h-b arcs between Unlock and Lock, even in
  149. pure happens-before mode. For a hybrid mode this is a no-op. */
  150. #define ANNOTATE_NOT_HAPPENS_BEFORE_MUTEX(mu) \
  151. DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsNotPHB)(__FILE__, __LINE__, mu)
  152. /* Deprecated. Use ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX. */
  153. #define ANNOTATE_MUTEX_IS_USED_AS_CONDVAR(mu) \
  154. DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsUsedAsCondVar)(__FILE__, __LINE__, \
  155. mu)
  156. /* -------------------------------------------------------------
  157. Annotations useful when defining memory allocators, or when memory that
  158. was protected in one way starts to be protected in another. */
  159. /* Report that a new memory at "address" of size "size" has been allocated.
  160. This might be used when the memory has been retrieved from a free list and
  161. is about to be reused, or when a the locking discipline for a variable
  162. changes. */
  163. #define ANNOTATE_NEW_MEMORY(address, size) \
  164. DYNAMIC_ANNOTATIONS_NAME(AnnotateNewMemory)(__FILE__, __LINE__, address, \
  165. size)
  166. /* -------------------------------------------------------------
  167. Annotations useful when defining FIFO queues that transfer data between
  168. threads. */
  169. /* Report that the producer-consumer queue (such as ProducerConsumerQueue) at
  170. address "pcq" has been created. The ANNOTATE_PCQ_* annotations
  171. should be used only for FIFO queues. For non-FIFO queues use
  172. ANNOTATE_HAPPENS_BEFORE (for put) and ANNOTATE_HAPPENS_AFTER (for get). */
  173. #define ANNOTATE_PCQ_CREATE(pcq) \
  174. DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQCreate)(__FILE__, __LINE__, pcq)
  175. /* Report that the queue at address "pcq" is about to be destroyed. */
  176. #define ANNOTATE_PCQ_DESTROY(pcq) \
  177. DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQDestroy)(__FILE__, __LINE__, pcq)
  178. /* Report that we are about to put an element into a FIFO queue at address
  179. "pcq". */
  180. #define ANNOTATE_PCQ_PUT(pcq) \
  181. DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQPut)(__FILE__, __LINE__, pcq)
  182. /* Report that we've just got an element from a FIFO queue at address
  183. "pcq". */
  184. #define ANNOTATE_PCQ_GET(pcq) \
  185. DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQGet)(__FILE__, __LINE__, pcq)
  186. /* -------------------------------------------------------------
  187. Annotations that suppress errors. It is usually better to express the
  188. program's synchronization using the other annotations, but these can
  189. be used when all else fails. */
  190. /* Report that we may have a benign race at "pointer", with size
  191. "sizeof(*(pointer))". "pointer" must be a non-void* pointer. Insert at the
  192. point where "pointer" has been allocated, preferably close to the point
  193. where the race happens. See also ANNOTATE_BENIGN_RACE_STATIC. */
  194. #define ANNOTATE_BENIGN_RACE(pointer, description) \
  195. DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRaceSized)(__FILE__, __LINE__, \
  196. pointer, sizeof(*(pointer)), description)
  197. /* Same as ANNOTATE_BENIGN_RACE(address, description), but applies to
  198. the memory range [address, address+size). */
  199. #define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) \
  200. DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRaceSized)(__FILE__, __LINE__, \
  201. address, size, description)
  202. /* Request the analysis tool to ignore all reads in the current thread
  203. until ANNOTATE_IGNORE_READS_END is called.
  204. Useful to ignore intentional racey reads, while still checking
  205. other reads and all writes.
  206. See also ANNOTATE_UNPROTECTED_READ. */
  207. #define ANNOTATE_IGNORE_READS_BEGIN() \
  208. DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsBegin)(__FILE__, __LINE__)
  209. /* Stop ignoring reads. */
  210. #define ANNOTATE_IGNORE_READS_END() \
  211. DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsEnd)(__FILE__, __LINE__)
  212. /* Similar to ANNOTATE_IGNORE_READS_BEGIN, but ignore writes. */
  213. #define ANNOTATE_IGNORE_WRITES_BEGIN() \
  214. DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesBegin)(__FILE__, __LINE__)
  215. /* Stop ignoring writes. */
  216. #define ANNOTATE_IGNORE_WRITES_END() \
  217. DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesEnd)(__FILE__, __LINE__)
  218. /* Start ignoring all memory accesses (reads and writes). */
  219. #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() \
  220. do {\
  221. ANNOTATE_IGNORE_READS_BEGIN();\
  222. ANNOTATE_IGNORE_WRITES_BEGIN();\
  223. }while(0)\
  224. /* Stop ignoring all memory accesses. */
  225. #define ANNOTATE_IGNORE_READS_AND_WRITES_END() \
  226. do {\
  227. ANNOTATE_IGNORE_WRITES_END();\
  228. ANNOTATE_IGNORE_READS_END();\
  229. }while(0)\
  230. /* Similar to ANNOTATE_IGNORE_READS_BEGIN, but ignore synchronization events:
  231. RWLOCK* and CONDVAR*. */
  232. #define ANNOTATE_IGNORE_SYNC_BEGIN() \
  233. DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncBegin)(__FILE__, __LINE__)
  234. /* Stop ignoring sync events. */
  235. #define ANNOTATE_IGNORE_SYNC_END() \
  236. DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncEnd)(__FILE__, __LINE__)
  237. /* Enable (enable!=0) or disable (enable==0) race detection for all threads.
  238. This annotation could be useful if you want to skip expensive race analysis
  239. during some period of program execution, e.g. during initialization. */
  240. #define ANNOTATE_ENABLE_RACE_DETECTION(enable) \
  241. DYNAMIC_ANNOTATIONS_NAME(AnnotateEnableRaceDetection)(__FILE__, __LINE__, \
  242. enable)
  243. /* -------------------------------------------------------------
  244. Annotations useful for debugging. */
  245. /* Request to trace every access to "address". */
  246. #define ANNOTATE_TRACE_MEMORY(address) \
  247. DYNAMIC_ANNOTATIONS_NAME(AnnotateTraceMemory)(__FILE__, __LINE__, address)
  248. /* Report the current thread name to a race detector. */
  249. #define ANNOTATE_THREAD_NAME(name) \
  250. DYNAMIC_ANNOTATIONS_NAME(AnnotateThreadName)(__FILE__, __LINE__, name)
  251. /* -------------------------------------------------------------
  252. Annotations useful when implementing locks. They are not
  253. normally needed by modules that merely use locks.
  254. The "lock" argument is a pointer to the lock object. */
  255. /* Report that a lock has been created at address "lock". */
  256. #define ANNOTATE_RWLOCK_CREATE(lock) \
  257. DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockCreate)(__FILE__, __LINE__, lock)
  258. /* Report that the lock at address "lock" is about to be destroyed. */
  259. #define ANNOTATE_RWLOCK_DESTROY(lock) \
  260. DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockDestroy)(__FILE__, __LINE__, lock)
  261. /* Report that the lock at address "lock" has been acquired.
  262. is_w=1 for writer lock, is_w=0 for reader lock. */
  263. #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) \
  264. DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockAcquired)(__FILE__, __LINE__, lock, \
  265. is_w)
  266. /* Report that the lock at address "lock" is about to be released. */
  267. #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) \
  268. DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockReleased)(__FILE__, __LINE__, lock, \
  269. is_w)
  270. /* -------------------------------------------------------------
  271. Annotations useful when implementing barriers. They are not
  272. normally needed by modules that merely use barriers.
  273. The "barrier" argument is a pointer to the barrier object. */
  274. /* Report that the "barrier" has been initialized with initial "count".
  275. If 'reinitialization_allowed' is true, initialization is allowed to happen
  276. multiple times w/o calling barrier_destroy() */
  277. #define ANNOTATE_BARRIER_INIT(barrier, count, reinitialization_allowed) \
  278. DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierInit)(__FILE__, __LINE__, barrier, \
  279. count, reinitialization_allowed)
  280. /* Report that we are about to enter barrier_wait("barrier"). */
  281. #define ANNOTATE_BARRIER_WAIT_BEFORE(barrier) \
  282. DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitBefore)(__FILE__, __LINE__, \
  283. barrier)
  284. /* Report that we just exited barrier_wait("barrier"). */
  285. #define ANNOTATE_BARRIER_WAIT_AFTER(barrier) \
  286. DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitAfter)(__FILE__, __LINE__, \
  287. barrier)
  288. /* Report that the "barrier" has been destroyed. */
  289. #define ANNOTATE_BARRIER_DESTROY(barrier) \
  290. DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierDestroy)(__FILE__, __LINE__, \
  291. barrier)
  292. /* -------------------------------------------------------------
  293. Annotations useful for testing race detectors. */
  294. /* Report that we expect a race on the variable at "address".
  295. Use only in unit tests for a race detector. */
  296. #define ANNOTATE_EXPECT_RACE(address, description) \
  297. DYNAMIC_ANNOTATIONS_NAME(AnnotateExpectRace)(__FILE__, __LINE__, address, \
  298. description)
  299. #define ANNOTATE_FLUSH_EXPECTED_RACES() \
  300. DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushExpectedRaces)(__FILE__, __LINE__)
  301. /* A no-op. Insert where you like to test the interceptors. */
  302. #define ANNOTATE_NO_OP(arg) \
  303. DYNAMIC_ANNOTATIONS_NAME(AnnotateNoOp)(__FILE__, __LINE__, arg)
  304. /* Force the race detector to flush its state. The actual effect depends on
  305. * the implementation of the detector. */
  306. #define ANNOTATE_FLUSH_STATE() \
  307. DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushState)(__FILE__, __LINE__)
  308. #else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */
  309. #define ANNOTATE_RWLOCK_CREATE(lock) /* empty */
  310. #define ANNOTATE_RWLOCK_DESTROY(lock) /* empty */
  311. #define ANNOTATE_RWLOCK_ACQUIRED(lock, is_w) /* empty */
  312. #define ANNOTATE_RWLOCK_RELEASED(lock, is_w) /* empty */
  313. #define ANNOTATE_BARRIER_INIT(barrier, count, reinitialization_allowed) /* */
  314. #define ANNOTATE_BARRIER_WAIT_BEFORE(barrier) /* empty */
  315. #define ANNOTATE_BARRIER_WAIT_AFTER(barrier) /* empty */
  316. #define ANNOTATE_BARRIER_DESTROY(barrier) /* empty */
  317. #define ANNOTATE_CONDVAR_LOCK_WAIT(cv, lock) /* empty */
  318. #define ANNOTATE_CONDVAR_WAIT(cv) /* empty */
  319. #define ANNOTATE_CONDVAR_SIGNAL(cv) /* empty */
  320. #define ANNOTATE_CONDVAR_SIGNAL_ALL(cv) /* empty */
  321. #define ANNOTATE_HAPPENS_BEFORE(obj) /* empty */
  322. #define ANNOTATE_HAPPENS_AFTER(obj) /* empty */
  323. #define ANNOTATE_PUBLISH_MEMORY_RANGE(address, size) /* empty */
  324. #define ANNOTATE_UNPUBLISH_MEMORY_RANGE(address, size) /* empty */
  325. #define ANNOTATE_SWAP_MEMORY_RANGE(address, size) /* empty */
  326. #define ANNOTATE_PCQ_CREATE(pcq) /* empty */
  327. #define ANNOTATE_PCQ_DESTROY(pcq) /* empty */
  328. #define ANNOTATE_PCQ_PUT(pcq) /* empty */
  329. #define ANNOTATE_PCQ_GET(pcq) /* empty */
  330. #define ANNOTATE_NEW_MEMORY(address, size) /* empty */
  331. #define ANNOTATE_EXPECT_RACE(address, description) /* empty */
  332. #define ANNOTATE_FLUSH_EXPECTED_RACES(address, description) /* empty */
  333. #define ANNOTATE_BENIGN_RACE(address, description) /* empty */
  334. #define ANNOTATE_BENIGN_RACE_SIZED(address, size, description) /* empty */
  335. #define ANNOTATE_PURE_HAPPENS_BEFORE_MUTEX(mu) /* empty */
  336. #define ANNOTATE_MUTEX_IS_USED_AS_CONDVAR(mu) /* empty */
  337. #define ANNOTATE_TRACE_MEMORY(arg) /* empty */
  338. #define ANNOTATE_THREAD_NAME(name) /* empty */
  339. #define ANNOTATE_IGNORE_READS_BEGIN() /* empty */
  340. #define ANNOTATE_IGNORE_READS_END() /* empty */
  341. #define ANNOTATE_IGNORE_WRITES_BEGIN() /* empty */
  342. #define ANNOTATE_IGNORE_WRITES_END() /* empty */
  343. #define ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN() /* empty */
  344. #define ANNOTATE_IGNORE_READS_AND_WRITES_END() /* empty */
  345. #define ANNOTATE_IGNORE_SYNC_BEGIN() /* empty */
  346. #define ANNOTATE_IGNORE_SYNC_END() /* empty */
  347. #define ANNOTATE_ENABLE_RACE_DETECTION(enable) /* empty */
  348. #define ANNOTATE_NO_OP(arg) /* empty */
  349. #define ANNOTATE_FLUSH_STATE() /* empty */
  350. #endif /* DYNAMIC_ANNOTATIONS_ENABLED */
  351. /* Use the macros above rather than using these functions directly. */
  352. #ifdef __cplusplus
  353. extern "C" {
  354. #endif
  355. void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockCreate)(
  356. const char *file, int line,
  357. const volatile void *lock) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  358. void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockDestroy)(
  359. const char *file, int line,
  360. const volatile void *lock) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  361. void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockAcquired)(
  362. const char *file, int line,
  363. const volatile void *lock, long is_w) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  364. void DYNAMIC_ANNOTATIONS_NAME(AnnotateRWLockReleased)(
  365. const char *file, int line,
  366. const volatile void *lock, long is_w) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  367. void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierInit)(
  368. const char *file, int line, const volatile void *barrier, long count,
  369. long reinitialization_allowed) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  370. void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitBefore)(
  371. const char *file, int line,
  372. const volatile void *barrier) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  373. void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierWaitAfter)(
  374. const char *file, int line,
  375. const volatile void *barrier) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  376. void DYNAMIC_ANNOTATIONS_NAME(AnnotateBarrierDestroy)(
  377. const char *file, int line,
  378. const volatile void *barrier) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  379. void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarWait)(
  380. const char *file, int line, const volatile void *cv,
  381. const volatile void *lock) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  382. void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignal)(
  383. const char *file, int line,
  384. const volatile void *cv) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  385. void DYNAMIC_ANNOTATIONS_NAME(AnnotateCondVarSignalAll)(
  386. const char *file, int line,
  387. const volatile void *cv) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  388. void DYNAMIC_ANNOTATIONS_NAME(AnnotateHappensBefore)(
  389. const char *file, int line,
  390. const volatile void *obj) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  391. void DYNAMIC_ANNOTATIONS_NAME(AnnotateHappensAfter)(
  392. const char *file, int line,
  393. const volatile void *obj) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  394. void DYNAMIC_ANNOTATIONS_NAME(AnnotatePublishMemoryRange)(
  395. const char *file, int line,
  396. const volatile void *address, long size) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  397. void DYNAMIC_ANNOTATIONS_NAME(AnnotateUnpublishMemoryRange)(
  398. const char *file, int line,
  399. const volatile void *address, long size) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  400. void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQCreate)(
  401. const char *file, int line,
  402. const volatile void *pcq) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  403. void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQDestroy)(
  404. const char *file, int line,
  405. const volatile void *pcq) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  406. void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQPut)(
  407. const char *file, int line,
  408. const volatile void *pcq) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  409. void DYNAMIC_ANNOTATIONS_NAME(AnnotatePCQGet)(
  410. const char *file, int line,
  411. const volatile void *pcq) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  412. void DYNAMIC_ANNOTATIONS_NAME(AnnotateNewMemory)(
  413. const char *file, int line,
  414. const volatile void *mem, long size) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  415. void DYNAMIC_ANNOTATIONS_NAME(AnnotateExpectRace)(
  416. const char *file, int line, const volatile void *mem,
  417. const char *description) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  418. void DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushExpectedRaces)(
  419. const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  420. void DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRace)(
  421. const char *file, int line, const volatile void *mem,
  422. const char *description) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  423. void DYNAMIC_ANNOTATIONS_NAME(AnnotateBenignRaceSized)(
  424. const char *file, int line, const volatile void *mem, long size,
  425. const char *description) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  426. void DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsUsedAsCondVar)(
  427. const char *file, int line,
  428. const volatile void *mu) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  429. void DYNAMIC_ANNOTATIONS_NAME(AnnotateMutexIsNotPHB)(
  430. const char *file, int line,
  431. const volatile void *mu) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  432. void DYNAMIC_ANNOTATIONS_NAME(AnnotateTraceMemory)(
  433. const char *file, int line,
  434. const volatile void *arg) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  435. void DYNAMIC_ANNOTATIONS_NAME(AnnotateThreadName)(
  436. const char *file, int line,
  437. const char *name) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  438. void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsBegin)(
  439. const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  440. void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreReadsEnd)(
  441. const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  442. void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesBegin)(
  443. const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  444. void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreWritesEnd)(
  445. const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  446. void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncBegin)(
  447. const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  448. void DYNAMIC_ANNOTATIONS_NAME(AnnotateIgnoreSyncEnd)(
  449. const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  450. void DYNAMIC_ANNOTATIONS_NAME(AnnotateEnableRaceDetection)(
  451. const char *file, int line, int enable) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  452. void DYNAMIC_ANNOTATIONS_NAME(AnnotateNoOp)(
  453. const char *file, int line,
  454. const volatile void *arg) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  455. void DYNAMIC_ANNOTATIONS_NAME(AnnotateFlushState)(
  456. const char *file, int line) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  457. #if DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND == 1
  458. /* Return non-zero value if running under valgrind.
  459. If "valgrind.h" is included into dynamic_annotations.c,
  460. the regular valgrind mechanism will be used.
  461. See http://valgrind.org/docs/manual/manual-core-adv.html about
  462. RUNNING_ON_VALGRIND and other valgrind "client requests".
  463. The file "valgrind.h" may be obtained by doing
  464. svn co svn://svn.valgrind.org/valgrind/trunk/include
  465. If for some reason you can't use "valgrind.h" or want to fake valgrind,
  466. there are two ways to make this function return non-zero:
  467. - Use environment variable: export RUNNING_ON_VALGRIND=1
  468. - Make your tool intercept the function RunningOnValgrind() and
  469. change its return value.
  470. */
  471. int RunningOnValgrind(void) DYNAMIC_ANNOTATIONS_ATTRIBUTE_WEAK;
  472. #endif /* DYNAMIC_ANNOTATIONS_PROVIDE_RUNNING_ON_VALGRIND == 1 */
  473. #ifdef __cplusplus
  474. }
  475. #endif
  476. #if DYNAMIC_ANNOTATIONS_ENABLED != 0 && defined(__cplusplus)
  477. /* ANNOTATE_UNPROTECTED_READ is the preferred way to annotate racey reads.
  478. Instead of doing
  479. ANNOTATE_IGNORE_READS_BEGIN();
  480. ... = x;
  481. ANNOTATE_IGNORE_READS_END();
  482. one can use
  483. ... = ANNOTATE_UNPROTECTED_READ(x); */
  484. template <class T>
  485. inline T ANNOTATE_UNPROTECTED_READ(const volatile T &x) {
  486. ANNOTATE_IGNORE_READS_BEGIN();
  487. T res = x;
  488. ANNOTATE_IGNORE_READS_END();
  489. return res;
  490. }
  491. /* Apply ANNOTATE_BENIGN_RACE_SIZED to a static variable. */
  492. #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) \
  493. namespace { \
  494. class static_var ## _annotator { \
  495. public: \
  496. static_var ## _annotator() { \
  497. ANNOTATE_BENIGN_RACE_SIZED(&static_var, \
  498. sizeof(static_var), \
  499. # static_var ": " description); \
  500. } \
  501. }; \
  502. static static_var ## _annotator the ## static_var ## _annotator;\
  503. }
  504. #else /* DYNAMIC_ANNOTATIONS_ENABLED == 0 */
  505. #define ANNOTATE_UNPROTECTED_READ(x) (x)
  506. #define ANNOTATE_BENIGN_RACE_STATIC(static_var, description) /* empty */
  507. #endif /* DYNAMIC_ANNOTATIONS_ENABLED */
  508. #endif /* __DYNAMIC_ANNOTATIONS_H__ */