aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/Support/Valgrind.h
blob: e14764703932bfd72f9e9a16170d0fd050e5b08b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//===- llvm/Support/Valgrind.h - Communication with Valgrind -----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Methods for communicating with a valgrind instance this program is running
// under.  These are all no-ops unless LLVM was configured on a system with the
// valgrind headers installed and valgrind is controlling this process.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SYSTEM_VALGRIND_H
#define LLVM_SYSTEM_VALGRIND_H

#include "llvm/Support/Compiler.h"
#include "llvm/Config/llvm-config.h"
#include <stddef.h>

#if LLVM_ENABLE_THREADS != 0 && !defined(NDEBUG)
// tsan (Thread Sanitizer) is a valgrind-based tool that detects these exact
// functions by name.
extern "C" {
LLVM_ATTRIBUTE_WEAK void AnnotateHappensAfter(const char *file, int line,
                                              const volatile void *cv);
LLVM_ATTRIBUTE_WEAK void AnnotateHappensBefore(const char *file, int line,
                                               const volatile void *cv);
LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesBegin(const char *file, int line);
LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesEnd(const char *file, int line);
}
#endif

namespace llvm {
namespace sys {
  // True if Valgrind is controlling this process.
  bool RunningOnValgrind();

  // Discard valgrind's translation of code in the range [Addr .. Addr + Len).
  // Otherwise valgrind may continue to execute the old version of the code.
  void ValgrindDiscardTranslations(const void *Addr, size_t Len);

#if LLVM_ENABLE_THREADS != 0 && !defined(NDEBUG)
  // Thread Sanitizer is a valgrind tool that finds races in code.
  // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .

  // This marker is used to define a happens-before arc. The race detector will
  // infer an arc from the begin to the end when they share the same pointer
  // argument.
  #define TsanHappensBefore(cv) \
    AnnotateHappensBefore(__FILE__, __LINE__, cv)

  // This marker defines the destination of a happens-before arc.
  #define TsanHappensAfter(cv) \
    AnnotateHappensAfter(__FILE__, __LINE__, cv)

  // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
  #define TsanIgnoreWritesBegin() \
    AnnotateIgnoreWritesBegin(__FILE__, __LINE__)

  // Resume checking for racy writes.
  #define TsanIgnoreWritesEnd() \
    AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
#else
  #define TsanHappensBefore(cv)
  #define TsanHappensAfter(cv)
  #define TsanIgnoreWritesBegin()
  #define TsanIgnoreWritesEnd()
#endif
}
}

#endif