1f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//===-- Valgrind.cpp - Implement Valgrind communication ---------*- C++ -*-===//
2f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//
3f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//                     The LLVM Compiler Infrastructure
4f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//
5f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin// This file is distributed under the University of Illinois Open Source
6f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin// License. See LICENSE.TXT for details.
7f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//
8f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//===----------------------------------------------------------------------===//
9f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//
10f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//  Defines Valgrind communication methods, if HAVE_VALGRIND_VALGRIND_H is
11f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//  defined.  If we have valgrind.h but valgrind isn't running, its macros are
12f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//  no-ops.
13f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//
14f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//===----------------------------------------------------------------------===//
15f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
161f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Valgrind.h"
17f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin#include "llvm/Config/config.h"
18f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#include <cstddef>
19f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
20f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin#if HAVE_VALGRIND_VALGRIND_H
21f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin#include <valgrind/valgrind.h>
22f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
23f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskinstatic bool InitNotUnderValgrind() {
24f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin  return !RUNNING_ON_VALGRIND;
25f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin}
26f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
27f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin// This bool is negated from what we'd expect because code may run before it
28f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin// gets initialized.  If that happens, it will appear to be 0 (false), and we
29f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin// want that to cause the rest of the code in this file to run the
30f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin// Valgrind-provided macros.
31f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskinstatic const bool NotUnderValgrind = InitNotUnderValgrind();
32f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
33f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskinbool llvm::sys::RunningOnValgrind() {
34f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin  if (NotUnderValgrind)
35f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin    return false;
36f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin  return RUNNING_ON_VALGRIND;
37f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin}
38f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
39f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskinvoid llvm::sys::ValgrindDiscardTranslations(const void *Addr, size_t Len) {
40f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin  if (NotUnderValgrind)
41f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin    return;
42f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
43ba519f8fc9778a86080e89b62c20b4c420edc22bDuncan Sands  VALGRIND_DISCARD_TRANSLATIONS(Addr, Len);
44f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin}
45f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
46f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin#else  // !HAVE_VALGRIND_VALGRIND_H
47f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
48f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskinbool llvm::sys::RunningOnValgrind() {
49f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin  return false;
50f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin}
51f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
52f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskinvoid llvm::sys::ValgrindDiscardTranslations(const void *Addr, size_t Len) {
53f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin}
54f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
55f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin#endif  // !HAVE_VALGRIND_VALGRIND_H
56