1f4a2713aSLionel Sambuc //===-- Valgrind.cpp - Implement Valgrind communication ---------*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // Defines Valgrind communication methods, if HAVE_VALGRIND_VALGRIND_H is
11f4a2713aSLionel Sambuc // defined. If we have valgrind.h but valgrind isn't running, its macros are
12f4a2713aSLionel Sambuc // no-ops.
13f4a2713aSLionel Sambuc //
14f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
15f4a2713aSLionel Sambuc
16f4a2713aSLionel Sambuc #include "llvm/Support/Valgrind.h"
17f4a2713aSLionel Sambuc #include "llvm/Config/config.h"
18f4a2713aSLionel Sambuc
19f4a2713aSLionel Sambuc #if HAVE_VALGRIND_VALGRIND_H
20f4a2713aSLionel Sambuc #include <valgrind/valgrind.h>
21f4a2713aSLionel Sambuc
InitNotUnderValgrind()22f4a2713aSLionel Sambuc static bool InitNotUnderValgrind() {
23f4a2713aSLionel Sambuc return !RUNNING_ON_VALGRIND;
24f4a2713aSLionel Sambuc }
25f4a2713aSLionel Sambuc
26f4a2713aSLionel Sambuc // This bool is negated from what we'd expect because code may run before it
27f4a2713aSLionel Sambuc // gets initialized. If that happens, it will appear to be 0 (false), and we
28f4a2713aSLionel Sambuc // want that to cause the rest of the code in this file to run the
29f4a2713aSLionel Sambuc // Valgrind-provided macros.
30f4a2713aSLionel Sambuc static const bool NotUnderValgrind = InitNotUnderValgrind();
31f4a2713aSLionel Sambuc
RunningOnValgrind()32f4a2713aSLionel Sambuc bool llvm::sys::RunningOnValgrind() {
33f4a2713aSLionel Sambuc if (NotUnderValgrind)
34f4a2713aSLionel Sambuc return false;
35f4a2713aSLionel Sambuc return RUNNING_ON_VALGRIND;
36f4a2713aSLionel Sambuc }
37f4a2713aSLionel Sambuc
ValgrindDiscardTranslations(const void * Addr,size_t Len)38f4a2713aSLionel Sambuc void llvm::sys::ValgrindDiscardTranslations(const void *Addr, size_t Len) {
39f4a2713aSLionel Sambuc if (NotUnderValgrind)
40f4a2713aSLionel Sambuc return;
41f4a2713aSLionel Sambuc
42f4a2713aSLionel Sambuc VALGRIND_DISCARD_TRANSLATIONS(Addr, Len);
43f4a2713aSLionel Sambuc }
44f4a2713aSLionel Sambuc
45f4a2713aSLionel Sambuc #else // !HAVE_VALGRIND_VALGRIND_H
46f4a2713aSLionel Sambuc
RunningOnValgrind()47f4a2713aSLionel Sambuc bool llvm::sys::RunningOnValgrind() {
48f4a2713aSLionel Sambuc return false;
49f4a2713aSLionel Sambuc }
50f4a2713aSLionel Sambuc
ValgrindDiscardTranslations(const void * Addr,size_t Len)51f4a2713aSLionel Sambuc void llvm::sys::ValgrindDiscardTranslations(const void *Addr, size_t Len) {
52f4a2713aSLionel Sambuc }
53f4a2713aSLionel Sambuc
54f4a2713aSLionel Sambuc #endif // !HAVE_VALGRIND_VALGRIND_H
55f4a2713aSLionel Sambuc
56f4a2713aSLionel Sambuc #if LLVM_ENABLE_THREADS != 0 && !defined(NDEBUG)
57f4a2713aSLionel Sambuc // These functions require no implementation, tsan just looks at the arguments
58*0a6a1f1dSLionel Sambuc // they're called with. However, they are required to be weak as some other
59*0a6a1f1dSLionel Sambuc // application or library may already be providing these definitions for the
60*0a6a1f1dSLionel Sambuc // same reason we are.
61f4a2713aSLionel Sambuc extern "C" {
62*0a6a1f1dSLionel Sambuc LLVM_ATTRIBUTE_WEAK void AnnotateHappensAfter(const char *file, int line,
63*0a6a1f1dSLionel Sambuc const volatile void *cv);
AnnotateHappensAfter(const char * file,int line,const volatile void * cv)64*0a6a1f1dSLionel Sambuc void AnnotateHappensAfter(const char *file, int line, const volatile void *cv) {
65*0a6a1f1dSLionel Sambuc }
66*0a6a1f1dSLionel Sambuc LLVM_ATTRIBUTE_WEAK void AnnotateHappensBefore(const char *file, int line,
67*0a6a1f1dSLionel Sambuc const volatile void *cv);
AnnotateHappensBefore(const char * file,int line,const volatile void * cv)68f4a2713aSLionel Sambuc void AnnotateHappensBefore(const char *file, int line,
69f4a2713aSLionel Sambuc const volatile void *cv) {}
70*0a6a1f1dSLionel Sambuc LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesBegin(const char *file, int line);
AnnotateIgnoreWritesBegin(const char * file,int line)71f4a2713aSLionel Sambuc void AnnotateIgnoreWritesBegin(const char *file, int line) {}
72*0a6a1f1dSLionel Sambuc LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesEnd(const char *file, int line);
AnnotateIgnoreWritesEnd(const char * file,int line)73f4a2713aSLionel Sambuc void AnnotateIgnoreWritesEnd(const char *file, int line) {}
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc #endif
76