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 
24 namespace __sanitizer {
25 
26 // dd - deadlock detector.
27 // lt - logical (user) thread.
28 // pt - physical (OS) thread.
29 
30 struct DDPhysicalThread;
31 struct DDLogicalThread;
32 
33 struct 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 
47 struct DDFlags {
48   bool second_deadlock_stack;
49 };
50 
51 struct 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 
62 struct DDCallback {
63   DDPhysicalThread *pt;
64   DDLogicalThread  *lt;
65 
UnwindDDCallback66   virtual u32 Unwind() { return 0; }
UniqueTidDDCallback67   virtual int UniqueTid() { return 0; }
68 };
69 
70 struct DDetector {
71   static DDetector *Create(const DDFlags *flags);
72 
CreatePhysicalThreadDDetector73   virtual DDPhysicalThread* CreatePhysicalThread() { return nullptr; }
DestroyPhysicalThreadDDetector74   virtual void DestroyPhysicalThread(DDPhysicalThread *pt) {}
75 
CreateLogicalThreadDDetector76   virtual DDLogicalThread* CreateLogicalThread(u64 ctx) { return nullptr; }
DestroyLogicalThreadDDetector77   virtual void DestroyLogicalThread(DDLogicalThread *lt) {}
78 
MutexInitDDetector79   virtual void MutexInit(DDCallback *cb, DDMutex *m) {}
MutexBeforeLockDDetector80   virtual void MutexBeforeLock(DDCallback *cb, DDMutex *m, bool wlock) {}
MutexAfterLockDDetector81   virtual void MutexAfterLock(DDCallback *cb, DDMutex *m, bool wlock,
82       bool trylock) {}
MutexBeforeUnlockDDetector83   virtual void MutexBeforeUnlock(DDCallback *cb, DDMutex *m, bool wlock) {}
MutexDestroyDDetector84   virtual void MutexDestroy(DDCallback *cb, DDMutex *m) {}
85 
GetReportDDetector86   virtual DDReport *GetReport(DDCallback *cb) { return nullptr; }
87 };
88 
89 } // namespace __sanitizer
90 
91 #endif // SANITIZER_DEADLOCK_DETECTOR_INTERFACE_H
92