blob: 729e79e8d351a2c8cc18bc2e4a058805a53aaa32 [file] [log] [blame]
Dmitry Vyukov512a18e2014-02-28 14:52:20 +00001//===-- dd_rtl.cc ---------------------------------------------------------===//
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#include "dd_rtl.h"
11#include "sanitizer_common/sanitizer_common.h"
Dmitry Vyukov54a03032014-03-04 11:39:56 +000012#include "sanitizer_common/sanitizer_placement_new.h"
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000013#include "sanitizer_common/sanitizer_flags.h"
14#include "sanitizer_common/sanitizer_stacktrace.h"
15#include "sanitizer_common/sanitizer_stackdepot.h"
16
17namespace __dsan {
18
Dmitry Vyukov54a03032014-03-04 11:39:56 +000019static Context *ctx;
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000020
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000021static u32 CurrentStackTrace(Thread *thr, uptr skip) {
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000022 StackTrace trace;
Dmitry Vyukov54a03032014-03-04 11:39:56 +000023 thr->ignore_interceptors = true;
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000024 trace.Unwind(1000, 0, 0, 0, 0, 0, false);
Dmitry Vyukov54a03032014-03-04 11:39:56 +000025 thr->ignore_interceptors = false;
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000026 if (trace.size <= skip)
27 return 0;
28 return StackDepotPut(trace.trace + skip, trace.size - skip);
29}
30
31static void PrintStackTrace(Thread *thr, u32 stk) {
32 uptr size = 0;
33 const uptr *trace = StackDepotGet(stk, &size);
Dmitry Vyukov54a03032014-03-04 11:39:56 +000034 thr->ignore_interceptors = true;
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000035 StackTrace::PrintStack(trace, size);
Dmitry Vyukov54a03032014-03-04 11:39:56 +000036 thr->ignore_interceptors = false;
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000037}
38
39static void ReportDeadlock(Thread *thr, DDReport *rep) {
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000040 if (rep == 0)
41 return;
Dmitry Vyukov51f5b5f2014-04-11 17:54:27 +000042 BlockingMutexLock lock(&ctx->report_mutex);
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000043 Printf("==============================\n");
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000044 Printf("WARNING: lock-order-inversion (potential deadlock)\n");
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000045 for (int i = 0; i < rep->n; i++) {
Dmitry Vyukov69bd9ca2014-03-06 12:02:17 +000046 Printf("Thread %d locks mutex %llu while holding mutex %llu:\n",
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000047 rep->loop[i].thr_ctx, rep->loop[i].mtx_ctx1, rep->loop[i].mtx_ctx0);
Kostya Serebryanye7846202014-03-17 15:16:25 +000048 PrintStackTrace(thr, rep->loop[i].stk[1]);
Dmitry Vyukov3cd028c2014-03-18 13:13:47 +000049 if (rep->loop[i].stk[0]) {
50 Printf("Mutex %llu was acquired here:\n",
51 rep->loop[i].mtx_ctx0);
52 PrintStackTrace(thr, rep->loop[i].stk[0]);
53 }
Dmitry Vyukov512a18e2014-02-28 14:52:20 +000054 }
55 Printf("==============================\n");
56}
57
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000058Callback::Callback(Thread *thr)
59 : thr(thr) {
60 lt = thr->dd_lt;
61 pt = thr->dd_pt;
62}
63
64u32 Callback::Unwind() {
65 return CurrentStackTrace(thr, 3);
66}
67
Dmitry Vyukov3cd028c2014-03-18 13:13:47 +000068void InitializeFlags(Flags *f, const char *env) {
69 internal_memset(f, 0, sizeof(*f));
70
71 // Default values.
72 f->second_deadlock_stack = false;
73
74 SetCommonFlagsDefaults(f);
75 // Override some common flags defaults.
76 f->allow_addr2line = true;
77
78 // Override from command line.
Alexander Potapenko12964362014-03-20 12:52:52 +000079 ParseFlag(env, &f->second_deadlock_stack, "second_deadlock_stack", "");
Dmitry Vyukov3cd028c2014-03-18 13:13:47 +000080 ParseCommonFlagsFromString(f, env);
81
82 // Copy back to common flags.
83 *common_flags() = *f;
84}
85
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000086void Initialize() {
87 static u64 ctx_mem[sizeof(Context) / sizeof(u64) + 1];
88 ctx = new(ctx_mem) Context();
89
90 InitializeInterceptors();
Dmitry Vyukov3cd028c2014-03-18 13:13:47 +000091 InitializeFlags(flags(), GetEnv("DSAN_OPTIONS"));
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000092 common_flags()->symbolize = true;
Dmitry Vyukov3cd028c2014-03-18 13:13:47 +000093 ctx->dd = DDetector::Create(flags());
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +000094}
95
96void ThreadInit(Thread *thr) {
97 static atomic_uintptr_t id_gen;
98 uptr id = atomic_fetch_add(&id_gen, 1, memory_order_relaxed);
99 thr->dd_pt = ctx->dd->CreatePhysicalThread();
100 thr->dd_lt = ctx->dd->CreateLogicalThread(id);
101}
102
103void ThreadDestroy(Thread *thr) {
104 ctx->dd->DestroyPhysicalThread(thr->dd_pt);
105 ctx->dd->DestroyLogicalThread(thr->dd_lt);
106}
107
108void MutexBeforeLock(Thread *thr, uptr m, bool writelock) {
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000109 if (thr->ignore_interceptors)
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000110 return;
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000111 Callback cb(thr);
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000112 {
113 MutexHashMap::Handle h(&ctx->mutex_map, m);
114 if (h.created())
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000115 ctx->dd->MutexInit(&cb, &h->dd);
116 ctx->dd->MutexBeforeLock(&cb, &h->dd, writelock);
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000117 }
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000118 ReportDeadlock(thr, ctx->dd->GetReport(&cb));
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000119}
120
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000121void MutexAfterLock(Thread *thr, uptr m, bool writelock, bool trylock) {
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000122 if (thr->ignore_interceptors)
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000123 return;
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000124 Callback cb(thr);
125 {
126 MutexHashMap::Handle h(&ctx->mutex_map, m);
127 if (h.created())
128 ctx->dd->MutexInit(&cb, &h->dd);
129 ctx->dd->MutexAfterLock(&cb, &h->dd, writelock, trylock);
130 }
131 ReportDeadlock(thr, ctx->dd->GetReport(&cb));
132}
133
134void MutexBeforeUnlock(Thread *thr, uptr m, bool writelock) {
135 if (thr->ignore_interceptors)
136 return;
137 Callback cb(thr);
138 {
139 MutexHashMap::Handle h(&ctx->mutex_map, m);
140 ctx->dd->MutexBeforeUnlock(&cb, &h->dd, writelock);
141 }
142 ReportDeadlock(thr, ctx->dd->GetReport(&cb));
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000143}
144
145void MutexDestroy(Thread *thr, uptr m) {
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000146 if (thr->ignore_interceptors)
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000147 return;
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000148 Callback cb(thr);
Dmitry Vyukov54a03032014-03-04 11:39:56 +0000149 MutexHashMap::Handle h(&ctx->mutex_map, m, true);
150 if (!h.exists())
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000151 return;
Dmitry Vyukov9b410fb2014-03-05 13:41:21 +0000152 ctx->dd->MutexDestroy(&cb, &h->dd);
Dmitry Vyukov512a18e2014-02-28 14:52:20 +0000153}
154
155} // namespace __dsan