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_SUPPORT_VALGRIND_H
17 #define LLVM_SUPPORT_VALGRIND_H
18 
19 #include "llvm/Config/llvm-config.h"
20 #include "llvm/Support/Compiler.h"
21 #include <stddef.h>
22 
23 #if LLVM_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 void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);
28 void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);
29 void AnnotateIgnoreWritesBegin(const char *file, int line);
30 void AnnotateIgnoreWritesEnd(const char *file, int line);
31 }
32 #endif
33 
34 namespace llvm {
35 namespace sys {
36   // True if Valgrind is controlling this process.
37   bool RunningOnValgrind();
38 
39   // Discard valgrind's translation of code in the range [Addr .. Addr + Len).
40   // Otherwise valgrind may continue to execute the old version of the code.
41   void ValgrindDiscardTranslations(const void *Addr, size_t Len);
42 
43 #if LLVM_ENABLE_THREADS != 0 && !defined(NDEBUG)
44   // Thread Sanitizer is a valgrind tool that finds races in code.
45   // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
46 
47   // This marker is used to define a happens-before arc. The race detector will
48   // infer an arc from the begin to the end when they share the same pointer
49   // argument.
50   #define TsanHappensBefore(cv) \
51     AnnotateHappensBefore(__FILE__, __LINE__, cv)
52 
53   // This marker defines the destination of a happens-before arc.
54   #define TsanHappensAfter(cv) \
55     AnnotateHappensAfter(__FILE__, __LINE__, cv)
56 
57   // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
58   #define TsanIgnoreWritesBegin() \
59     AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
60 
61   // Resume checking for racy writes.
62   #define TsanIgnoreWritesEnd() \
63     AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
64 #else
65   #define TsanHappensBefore(cv)
66   #define TsanHappensAfter(cv)
67   #define TsanIgnoreWritesBegin()
68   #define TsanIgnoreWritesEnd()
69 #endif
70 }
71 }
72 
73 #endif
74