blob: 2c6d6aaadfff9e2a5c886a1efc4f69d044eca187 [file] [log] [blame]
Jeffrey Yasskin3ddd88f2010-03-15 04:57:55 +00001//===-- Valgrind.cpp - Implement Valgrind communication ---------*- 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// Defines Valgrind communication methods, if HAVE_VALGRIND_VALGRIND_H is
11// defined. If we have valgrind.h but valgrind isn't running, its macros are
12// no-ops.
13//
14//===----------------------------------------------------------------------===//
15
Michael J. Spencer447762d2010-11-29 18:16:10 +000016#include "llvm/Support/Valgrind.h"
Jeffrey Yasskin3ddd88f2010-03-15 04:57:55 +000017#include "llvm/Config/config.h"
18
19#if HAVE_VALGRIND_VALGRIND_H
20#include <valgrind/valgrind.h>
21
22static bool InitNotUnderValgrind() {
23 return !RUNNING_ON_VALGRIND;
24}
25
26// This bool is negated from what we'd expect because code may run before it
27// gets initialized. If that happens, it will appear to be 0 (false), and we
28// want that to cause the rest of the code in this file to run the
29// Valgrind-provided macros.
30static const bool NotUnderValgrind = InitNotUnderValgrind();
31
32bool llvm::sys::RunningOnValgrind() {
33 if (NotUnderValgrind)
34 return false;
35 return RUNNING_ON_VALGRIND;
36}
37
38void llvm::sys::ValgrindDiscardTranslations(const void *Addr, size_t Len) {
39 if (NotUnderValgrind)
40 return;
41
Duncan Sandsa1c44332011-07-02 13:14:22 +000042 VALGRIND_DISCARD_TRANSLATIONS(Addr, Len);
Jeffrey Yasskin3ddd88f2010-03-15 04:57:55 +000043}
44
45#else // !HAVE_VALGRIND_VALGRIND_H
46
47bool llvm::sys::RunningOnValgrind() {
48 return false;
49}
50
51void llvm::sys::ValgrindDiscardTranslations(const void *Addr, size_t Len) {
52}
53
54#endif // !HAVE_VALGRIND_VALGRIND_H
Nick Lewyckyfe856112011-11-14 20:50:16 +000055
Nick Lewycky76c62992011-11-28 22:14:02 +000056#if LLVM_ENABLE_THREADS != 0 && !defined(NDEBUG)
Nick Lewyckyfe856112011-11-14 20:50:16 +000057// These functions require no implementation, tsan just looks at the arguments
Chandler Carrutha48ecb72014-03-30 11:20:25 +000058// they're called with. However, they are required to be weak as some other
59// application or library may already be providing these definitions for the
60// same reason we are.
Nick Lewyckyfe856112011-11-14 20:50:16 +000061extern "C" {
Chandler Carrutha48ecb72014-03-30 11:20:25 +000062LLVM_ATTRIBUTE_WEAK void AnnotateHappensAfter(const char *file, int line,
63 const volatile void *cv);
64void AnnotateHappensAfter(const char *file, int line, const volatile void *cv) {
65}
66LLVM_ATTRIBUTE_WEAK void AnnotateHappensBefore(const char *file, int line,
67 const volatile void *cv);
Nick Lewycky6804d272011-11-15 01:23:22 +000068void AnnotateHappensBefore(const char *file, int line,
69 const volatile void *cv) {}
Chandler Carrutha48ecb72014-03-30 11:20:25 +000070LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesBegin(const char *file, int line);
Nick Lewycky6804d272011-11-15 01:23:22 +000071void AnnotateIgnoreWritesBegin(const char *file, int line) {}
Chandler Carrutha48ecb72014-03-30 11:20:25 +000072LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesEnd(const char *file, int line);
Nick Lewycky6804d272011-11-15 01:23:22 +000073void AnnotateIgnoreWritesEnd(const char *file, int line) {}
Nick Lewyckyfe856112011-11-14 20:50:16 +000074}
Nick Lewycky76c62992011-11-28 22:14:02 +000075#endif