1218885Sdim//===- llvm/Support/Valgrind.h - Communication with Valgrind -----*- C++ -*-===//
2218885Sdim//
3218885Sdim//                     The LLVM Compiler Infrastructure
4218885Sdim//
5218885Sdim// This file is distributed under the University of Illinois Open Source
6218885Sdim// License. See LICENSE.TXT for details.
7218885Sdim//
8218885Sdim//===----------------------------------------------------------------------===//
9218885Sdim//
10218885Sdim// Methods for communicating with a valgrind instance this program is running
11218885Sdim// under.  These are all no-ops unless LLVM was configured on a system with the
12218885Sdim// valgrind headers installed and valgrind is controlling this process.
13218885Sdim//
14218885Sdim//===----------------------------------------------------------------------===//
15218885Sdim
16263508Sdim#ifndef LLVM_SUPPORT_VALGRIND_H
17263508Sdim#define LLVM_SUPPORT_VALGRIND_H
18218885Sdim
19249423Sdim#include "llvm/Config/llvm-config.h"
20234353Sdim#include "llvm/Support/Compiler.h"
21218885Sdim#include <stddef.h>
22218885Sdim
23234353Sdim#if LLVM_ENABLE_THREADS != 0 && !defined(NDEBUG)
24234353Sdim// tsan (Thread Sanitizer) is a valgrind-based tool that detects these exact
25234353Sdim// functions by name.
26234353Sdimextern "C" {
27234353SdimLLVM_ATTRIBUTE_WEAK void AnnotateHappensAfter(const char *file, int line,
28234353Sdim                                              const volatile void *cv);
29234353SdimLLVM_ATTRIBUTE_WEAK void AnnotateHappensBefore(const char *file, int line,
30234353Sdim                                               const volatile void *cv);
31234353SdimLLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesBegin(const char *file, int line);
32234353SdimLLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesEnd(const char *file, int line);
33234353Sdim}
34234353Sdim#endif
35234353Sdim
36218885Sdimnamespace llvm {
37218885Sdimnamespace sys {
38218885Sdim  // True if Valgrind is controlling this process.
39218885Sdim  bool RunningOnValgrind();
40218885Sdim
41218885Sdim  // Discard valgrind's translation of code in the range [Addr .. Addr + Len).
42218885Sdim  // Otherwise valgrind may continue to execute the old version of the code.
43218885Sdim  void ValgrindDiscardTranslations(const void *Addr, size_t Len);
44234353Sdim
45234353Sdim#if LLVM_ENABLE_THREADS != 0 && !defined(NDEBUG)
46234353Sdim  // Thread Sanitizer is a valgrind tool that finds races in code.
47234353Sdim  // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
48234353Sdim
49234353Sdim  // This marker is used to define a happens-before arc. The race detector will
50234353Sdim  // infer an arc from the begin to the end when they share the same pointer
51234353Sdim  // argument.
52234353Sdim  #define TsanHappensBefore(cv) \
53234353Sdim    AnnotateHappensBefore(__FILE__, __LINE__, cv)
54234353Sdim
55234353Sdim  // This marker defines the destination of a happens-before arc.
56234353Sdim  #define TsanHappensAfter(cv) \
57234353Sdim    AnnotateHappensAfter(__FILE__, __LINE__, cv)
58234353Sdim
59234353Sdim  // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
60234353Sdim  #define TsanIgnoreWritesBegin() \
61234353Sdim    AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
62234353Sdim
63234353Sdim  // Resume checking for racy writes.
64234353Sdim  #define TsanIgnoreWritesEnd() \
65234353Sdim    AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
66234353Sdim#else
67234353Sdim  #define TsanHappensBefore(cv)
68234353Sdim  #define TsanHappensAfter(cv)
69234353Sdim  #define TsanIgnoreWritesBegin()
70234353Sdim  #define TsanIgnoreWritesEnd()
71234353Sdim#endif
72218885Sdim}
73218885Sdim}
74218885Sdim
75218885Sdim#endif
76