153ca1f3190680f3e86aebe0f72f7918d63f71e0dCharles Davis//===- llvm/Support/Valgrind.h - Communication with Valgrind -----*- 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// Methods for communicating with a valgrind instance this program is running
11f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin// under.  These are all no-ops unless LLVM was configured on a system with the
12f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin// valgrind headers installed and valgrind is controlling this process.
13f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//
14f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin//===----------------------------------------------------------------------===//
15f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
16f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin#ifndef LLVM_SYSTEM_VALGRIND_H
17f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin#define LLVM_SYSTEM_VALGRIND_H
18f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
194d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky#include "llvm/Support/Compiler.h"
2038831b014a57718a1755ab1c3c1df08679183dfbDylan Noblesmith#include "llvm/Config/llvm-config.h"
21f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin#include <stddef.h>
22f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
2308b73a30bbb6407c7b48a734a29f65f4c8ddd782Dylan Noblesmith#if LLVM_ENABLE_THREADS != 0 && !defined(NDEBUG)
244d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky// tsan (Thread Sanitizer) is a valgrind-based tool that detects these exact
254d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky// functions by name.
264d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewyckyextern "C" {
27e7c1aef2b824f29ea92b2b324975915fe2115fa4Nick LewyckyLLVM_ATTRIBUTE_WEAK void AnnotateHappensAfter(const char *file, int line,
28e7c1aef2b824f29ea92b2b324975915fe2115fa4Nick Lewycky                                              const volatile void *cv);
29e7c1aef2b824f29ea92b2b324975915fe2115fa4Nick LewyckyLLVM_ATTRIBUTE_WEAK void AnnotateHappensBefore(const char *file, int line,
30e7c1aef2b824f29ea92b2b324975915fe2115fa4Nick Lewycky                                               const volatile void *cv);
31e7c1aef2b824f29ea92b2b324975915fe2115fa4Nick LewyckyLLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesBegin(const char *file, int line);
32e7c1aef2b824f29ea92b2b324975915fe2115fa4Nick LewyckyLLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesEnd(const char *file, int line);
334d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky}
344d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky#endif
354d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky
36f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskinnamespace llvm {
37f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskinnamespace sys {
38f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin  // True if Valgrind is controlling this process.
39f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin  bool RunningOnValgrind();
40f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
41f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin  // Discard valgrind's translation of code in the range [Addr .. Addr + Len).
42f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin  // Otherwise valgrind may continue to execute the old version of the code.
43f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin  void ValgrindDiscardTranslations(const void *Addr, size_t Len);
444d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky
4508b73a30bbb6407c7b48a734a29f65f4c8ddd782Dylan Noblesmith#if LLVM_ENABLE_THREADS != 0 && !defined(NDEBUG)
464d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  // Thread Sanitizer is a valgrind tool that finds races in code.
474d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  // See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations .
484d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky
494d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  // This marker is used to define a happens-before arc. The race detector will
504d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  // infer an arc from the begin to the end when they share the same pointer
514d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  // argument.
524d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  #define TsanHappensBefore(cv) \
534d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky    AnnotateHappensBefore(__FILE__, __LINE__, cv)
544d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky
554d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  // This marker defines the destination of a happens-before arc.
564d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  #define TsanHappensAfter(cv) \
574d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky    AnnotateHappensAfter(__FILE__, __LINE__, cv)
584d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky
594d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  // Ignore any races on writes between here and the next TsanIgnoreWritesEnd.
604d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  #define TsanIgnoreWritesBegin() \
614d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky    AnnotateIgnoreWritesBegin(__FILE__, __LINE__)
624d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky
634d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  // Resume checking for racy writes.
644d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  #define TsanIgnoreWritesEnd() \
654d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky    AnnotateIgnoreWritesEnd(__FILE__, __LINE__)
664d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky#else
674d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  #define TsanHappensBefore(cv)
684d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  #define TsanHappensAfter(cv)
694d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  #define TsanIgnoreWritesBegin()
704d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky  #define TsanIgnoreWritesEnd()
714d0a9ff36574da0c042e9bd3ae816301b392ac41Nick Lewycky#endif
72f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin}
73f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin}
74f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin
75f28411f732960981f8920195ad8f7e6792396961Jeffrey Yasskin#endif
76