0ecd33b6dd1c963095f21b771a657563b8413dfc
[oota-llvm.git] / include / llvm / Support / Valgrind.h
1 //===- llvm/Support/Valgrind.h - Communication with Valgrind -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Methods for communicating with a valgrind instance this program is running
11 // under.  These are all no-ops unless LLVM was configured on a system with the
12 // valgrind headers installed and valgrind is controlling this process.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_SYSTEM_VALGRIND_H
17 #define LLVM_SYSTEM_VALGRIND_H
18
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Config/config.h"
21 #include <stddef.h>
22
23 #if ENABLE_THREADS != 0 && !defined(NDEBUG)
24 // tsan (Thread Sanitizer) is a valgrind-based tool that detects these exact
25 // functions by name.
26 extern "C" {
27 LLVM_ATTRIBUTE_NOINLINE void AnnotateHappensAfter(const char *file, int line,
28                                                   const volatile void *cv);
29 LLVM_ATTRIBUTE_NOINLINE void AnnotateHappensBefore(const char *file, int line,
30                                                    const volatile void *cv);
31 LLVM_ATTRIBUTE_NOINLINE void AnnotateIgnoreWritesBegin(const char *file,
32                                                        int line);
33 LLVM_ATTRIBUTE_NOINLINE void AnnotateIgnoreWritesEnd(const char *file,
34                                                      int line);
35 }
36 #endif
37
38 namespace llvm {
39 namespace sys {
40   // True if Valgrind is controlling this process.
41   bool RunningOnValgrind();
42
43   // Discard valgrind's translation of code in the range [Addr .. Addr + Len).
44   // Otherwise valgrind may continue to execute the old version of the code.
45   void ValgrindDiscardTranslations(const void *Addr, size_t Len);
46
47 #if ENABLE_THREADS != 0 && !defined(NDEBUG)
48   // Thread Sanitizer is a valgrind tool that finds races in code.
49   // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
50
51   // This marker is used to define a happens-before arc. The race detector will
52   // infer an arc from the begin to the end when they share the same pointer
53   // argument.
54   #define TsanHappensBefore(cv) \
55     AnnotateHappensBefore(__FILE__, __LINE__, cv)
56
57   // This marker defines the destination of a happens-before arc.
58   #define TsanHappensAfter(cv) \
59     AnnotateHappensAfter(__FILE__, __LINE__, cv)
60
61   // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
62   #define TsanIgnoreWritesBegin() \
63     AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
64
65   // Resume checking for racy writes.
66   #define TsanIgnoreWritesEnd() \
67     AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
68 #else
69   #define TsanHappensBefore(cv)
70   #define TsanHappensAfter(cv)
71   #define TsanIgnoreWritesBegin()
72   #define TsanIgnoreWritesEnd()
73 #endif
74 }
75 }
76
77 #endif