1//===-- sanitizer_deadlock_detector_interface.h -----------------*- C++ -*-===//
2//
3// This file is distributed under the University of Illinois Open Source
4// License. See LICENSE.TXT for details.
5//
6//===----------------------------------------------------------------------===//
7//
8// This file is a part of Sanitizer runtime.
9// Abstract deadlock detector interface.
10// FIXME: this is work in progress, nothing really works yet.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef SANITIZER_DEADLOCK_DETECTOR_INTERFACE_H
15#define SANITIZER_DEADLOCK_DETECTOR_INTERFACE_H
16
17#ifndef SANITIZER_DEADLOCK_DETECTOR_VERSION
18# define SANITIZER_DEADLOCK_DETECTOR_VERSION 1
19#endif
20
21#include "sanitizer_internal_defs.h"
22#include "sanitizer_atomic.h"
23
24namespace __sanitizer {
25
26// dd - deadlock detector.
27// lt - logical (user) thread.
28// pt - physical (OS) thread.
29
30struct DDPhysicalThread;
31struct DDLogicalThread;
32
33struct DDMutex {
34#if SANITIZER_DEADLOCK_DETECTOR_VERSION == 1
35  uptr id;
36  u32  stk;  // creation stack
37#elif SANITIZER_DEADLOCK_DETECTOR_VERSION == 2
38  u32              id;
39  u32              recursion;
40  atomic_uintptr_t owner;
41#else
42# error "BAD SANITIZER_DEADLOCK_DETECTOR_VERSION"
43#endif
44  u64  ctx;
45};
46
47struct DDFlags {
48  bool second_deadlock_stack;
49};
50
51struct DDReport {
52  enum { kMaxLoopSize = 20 };
53  int n;  // number of entries in loop
54  struct {
55    u64 thr_ctx;   // user thread context
56    u64 mtx_ctx0;  // user mutex context, start of the edge
57    u64 mtx_ctx1;  // user mutex context, end of the edge
58    u32 stk[2];  // stack ids for the edge
59  } loop[kMaxLoopSize];
60};
61
62struct DDCallback {
63  DDPhysicalThread *pt;
64  DDLogicalThread  *lt;
65
66  virtual u32 Unwind() { return 0; }
67  virtual int UniqueTid() { return 0; }
68};
69
70struct DDetector {
71  static DDetector *Create(const DDFlags *flags);
72
73  virtual DDPhysicalThread* CreatePhysicalThread() { return nullptr; }
74  virtual void DestroyPhysicalThread(DDPhysicalThread *pt) {}
75
76  virtual DDLogicalThread* CreateLogicalThread(u64 ctx) { return nullptr; }
77  virtual void DestroyLogicalThread(DDLogicalThread *lt) {}
78
79  virtual void MutexInit(DDCallback *cb, DDMutex *m) {}
80  virtual void MutexBeforeLock(DDCallback *cb, DDMutex *m, bool wlock) {}
81  virtual void MutexAfterLock(DDCallback *cb, DDMutex *m, bool wlock,
82      bool trylock) {}
83  virtual void MutexBeforeUnlock(DDCallback *cb, DDMutex *m, bool wlock) {}
84  virtual void MutexDestroy(DDCallback *cb, DDMutex *m) {}
85
86  virtual DDReport *GetReport(DDCallback *cb) { return nullptr; }
87};
88
89} // namespace __sanitizer
90
91#endif // SANITIZER_DEADLOCK_DETECTOR_INTERFACE_H
92