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