blob: 06154bc135a9c3a8ee2386d9efdd1b03fb1e069d [file] [log] [blame]
Kuba Breckaa1496f72016-03-10 17:00:29 +00001//===-- tsan_debugging.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// This file is a part of ThreadSanitizer (TSan), a race detector.
11//
12// TSan debugging API implementation.
13//===----------------------------------------------------------------------===//
14#include "tsan_interface.h"
15#include "tsan_report.h"
16#include "tsan_rtl.h"
17
Kuba Mracek1187cbd2016-12-19 17:52:20 +000018#include "sanitizer_common/sanitizer_stackdepot.h"
19
Kuba Breckaa1496f72016-03-10 17:00:29 +000020using namespace __tsan;
21
22static const char *ReportTypeDescription(ReportType typ) {
23 if (typ == ReportTypeRace) return "data-race";
24 if (typ == ReportTypeVptrRace) return "data-race-vptr";
25 if (typ == ReportTypeUseAfterFree) return "heap-use-after-free";
26 if (typ == ReportTypeVptrUseAfterFree) return "heap-use-after-free-vptr";
Kuba Mracekaa78ad52017-02-02 13:17:05 +000027 if (typ == ReportTypeExternalRace) return "external-race";
Kuba Breckaa1496f72016-03-10 17:00:29 +000028 if (typ == ReportTypeThreadLeak) return "thread-leak";
29 if (typ == ReportTypeMutexDestroyLocked) return "locked-mutex-destroy";
30 if (typ == ReportTypeMutexDoubleLock) return "mutex-double-lock";
Kuba Brecka46bf4542016-03-16 15:39:20 +000031 if (typ == ReportTypeMutexInvalidAccess) return "mutex-invalid-access";
Kuba Breckaa1496f72016-03-10 17:00:29 +000032 if (typ == ReportTypeMutexBadUnlock) return "mutex-bad-unlock";
33 if (typ == ReportTypeMutexBadReadLock) return "mutex-bad-read-lock";
34 if (typ == ReportTypeMutexBadReadUnlock) return "mutex-bad-read-unlock";
35 if (typ == ReportTypeSignalUnsafe) return "signal-unsafe-call";
36 if (typ == ReportTypeErrnoInSignal) return "errno-in-signal-handler";
37 if (typ == ReportTypeDeadlock) return "lock-order-inversion";
38 return "";
39}
40
41static const char *ReportLocationTypeDescription(ReportLocationType typ) {
42 if (typ == ReportLocationGlobal) return "global";
43 if (typ == ReportLocationHeap) return "heap";
44 if (typ == ReportLocationStack) return "stack";
45 if (typ == ReportLocationTLS) return "tls";
46 if (typ == ReportLocationFD) return "fd";
47 return "";
48}
49
50static void CopyTrace(SymbolizedStack *first_frame, void **trace,
51 uptr trace_size) {
52 uptr i = 0;
53 for (SymbolizedStack *frame = first_frame; frame != nullptr;
54 frame = frame->next) {
55 trace[i++] = (void *)frame->info.address;
56 if (i >= trace_size) break;
57 }
58}
59
60// Meant to be called by the debugger.
61SANITIZER_INTERFACE_ATTRIBUTE
62void *__tsan_get_current_report() {
Dmitry Vyukov066fefc2016-04-27 08:28:08 +000063 return const_cast<ReportDesc*>(cur_thread()->current_report);
Kuba Breckaa1496f72016-03-10 17:00:29 +000064}
65
66SANITIZER_INTERFACE_ATTRIBUTE
67int __tsan_get_report_data(void *report, const char **description, int *count,
68 int *stack_count, int *mop_count, int *loc_count,
69 int *mutex_count, int *thread_count,
70 int *unique_tid_count, void **sleep_trace,
71 uptr trace_size) {
72 const ReportDesc *rep = (ReportDesc *)report;
73 *description = ReportTypeDescription(rep->typ);
74 *count = rep->count;
75 *stack_count = rep->stacks.Size();
76 *mop_count = rep->mops.Size();
77 *loc_count = rep->locs.Size();
78 *mutex_count = rep->mutexes.Size();
79 *thread_count = rep->threads.Size();
80 *unique_tid_count = rep->unique_tids.Size();
81 if (rep->sleep) CopyTrace(rep->sleep->frames, sleep_trace, trace_size);
82 return 1;
83}
84
85SANITIZER_INTERFACE_ATTRIBUTE
86int __tsan_get_report_stack(void *report, uptr idx, void **trace,
87 uptr trace_size) {
88 const ReportDesc *rep = (ReportDesc *)report;
89 CHECK_LT(idx, rep->stacks.Size());
90 ReportStack *stack = rep->stacks[idx];
Kuba Brecka4b3833d2016-03-21 12:12:44 +000091 if (stack) CopyTrace(stack->frames, trace, trace_size);
92 return stack ? 1 : 0;
Kuba Breckaa1496f72016-03-10 17:00:29 +000093}
94
95SANITIZER_INTERFACE_ATTRIBUTE
96int __tsan_get_report_mop(void *report, uptr idx, int *tid, void **addr,
97 int *size, int *write, int *atomic, void **trace,
98 uptr trace_size) {
99 const ReportDesc *rep = (ReportDesc *)report;
100 CHECK_LT(idx, rep->mops.Size());
101 ReportMop *mop = rep->mops[idx];
102 *tid = mop->tid;
103 *addr = (void *)mop->addr;
104 *size = mop->size;
105 *write = mop->write ? 1 : 0;
106 *atomic = mop->atomic ? 1 : 0;
Kuba Brecka4b3833d2016-03-21 12:12:44 +0000107 if (mop->stack) CopyTrace(mop->stack->frames, trace, trace_size);
Kuba Breckaa1496f72016-03-10 17:00:29 +0000108 return 1;
109}
110
111SANITIZER_INTERFACE_ATTRIBUTE
112int __tsan_get_report_loc(void *report, uptr idx, const char **type,
113 void **addr, uptr *start, uptr *size, int *tid,
114 int *fd, int *suppressable, void **trace,
115 uptr trace_size) {
116 const ReportDesc *rep = (ReportDesc *)report;
117 CHECK_LT(idx, rep->locs.Size());
118 ReportLocation *loc = rep->locs[idx];
119 *type = ReportLocationTypeDescription(loc->type);
120 *addr = (void *)loc->global.start;
121 *start = loc->heap_chunk_start;
122 *size = loc->heap_chunk_size;
123 *tid = loc->tid;
124 *fd = loc->fd;
125 *suppressable = loc->suppressable;
126 if (loc->stack) CopyTrace(loc->stack->frames, trace, trace_size);
127 return 1;
128}
129
130SANITIZER_INTERFACE_ATTRIBUTE
Kuba Mracekc90b79c2017-02-22 01:13:34 +0000131int __tsan_get_report_loc_object_type(void *report, uptr idx,
132 const char **object_type) {
133 const ReportDesc *rep = (ReportDesc *)report;
134 CHECK_LT(idx, rep->locs.Size());
135 ReportLocation *loc = rep->locs[idx];
136 *object_type = GetObjectTypeFromTag(loc->external_tag);
137 return 1;
138}
139
140SANITIZER_INTERFACE_ATTRIBUTE
Kuba Breckaa1496f72016-03-10 17:00:29 +0000141int __tsan_get_report_mutex(void *report, uptr idx, uptr *mutex_id, void **addr,
142 int *destroyed, void **trace, uptr trace_size) {
143 const ReportDesc *rep = (ReportDesc *)report;
144 CHECK_LT(idx, rep->mutexes.Size());
145 ReportMutex *mutex = rep->mutexes[idx];
146 *mutex_id = mutex->id;
147 *addr = (void *)mutex->addr;
148 *destroyed = mutex->destroyed;
Kuba Brecka4b3833d2016-03-21 12:12:44 +0000149 if (mutex->stack) CopyTrace(mutex->stack->frames, trace, trace_size);
Kuba Breckaa1496f72016-03-10 17:00:29 +0000150 return 1;
151}
152
153SANITIZER_INTERFACE_ATTRIBUTE
Kuba Breckabf8b5f82016-04-21 14:49:25 +0000154int __tsan_get_report_thread(void *report, uptr idx, int *tid, uptr *os_id,
Kuba Breckaa1496f72016-03-10 17:00:29 +0000155 int *running, const char **name, int *parent_tid,
156 void **trace, uptr trace_size) {
157 const ReportDesc *rep = (ReportDesc *)report;
158 CHECK_LT(idx, rep->threads.Size());
159 ReportThread *thread = rep->threads[idx];
160 *tid = thread->id;
Kuba Breckabf8b5f82016-04-21 14:49:25 +0000161 *os_id = thread->os_id;
Kuba Breckaa1496f72016-03-10 17:00:29 +0000162 *running = thread->running;
163 *name = thread->name;
164 *parent_tid = thread->parent_tid;
Kuba Brecka4b3833d2016-03-21 12:12:44 +0000165 if (thread->stack) CopyTrace(thread->stack->frames, trace, trace_size);
Kuba Breckaa1496f72016-03-10 17:00:29 +0000166 return 1;
167}
168
169SANITIZER_INTERFACE_ATTRIBUTE
170int __tsan_get_report_unique_tid(void *report, uptr idx, int *tid) {
171 const ReportDesc *rep = (ReportDesc *)report;
172 CHECK_LT(idx, rep->unique_tids.Size());
173 *tid = rep->unique_tids[idx];
174 return 1;
175}
Kuba Mracek1187cbd2016-12-19 17:52:20 +0000176
177SANITIZER_INTERFACE_ATTRIBUTE
178const char *__tsan_locate_address(uptr addr, char *name, uptr name_size,
179 uptr *region_address_ptr,
180 uptr *region_size_ptr) {
181 uptr region_address = 0;
182 uptr region_size = 0;
183 const char *region_kind = nullptr;
184 if (name && name_size > 0) name[0] = 0;
185
186 if (IsMetaMem(addr)) {
187 region_kind = "meta shadow";
188 } else if (IsShadowMem(addr)) {
189 region_kind = "shadow";
190 } else {
191 bool is_stack = false;
192 MBlock *b = 0;
193 Allocator *a = allocator();
194 if (a->PointerIsMine((void *)addr)) {
195 void *block_begin = a->GetBlockBegin((void *)addr);
196 if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
197 }
198
199 if (b != 0) {
200 region_address = (uptr)allocator()->GetBlockBegin((void *)addr);
201 region_size = b->siz;
202 region_kind = "heap";
203 } else {
204 // TODO(kuba.brecka): We should not lock. This is supposed to be called
205 // from within the debugger when other threads are stopped.
206 ctx->thread_registry->Lock();
207 ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack);
208 ctx->thread_registry->Unlock();
209 if (tctx) {
210 region_kind = is_stack ? "stack" : "tls";
211 } else {
212 region_kind = "global";
213 DataInfo info;
214 if (Symbolizer::GetOrInit()->SymbolizeData(addr, &info)) {
215 internal_strncpy(name, info.name, name_size);
216 region_address = info.start;
217 region_size = info.size;
218 }
219 }
220 }
221 }
222
223 CHECK(region_kind);
224 if (region_address_ptr) *region_address_ptr = region_address;
225 if (region_size_ptr) *region_size_ptr = region_size;
226 return region_kind;
227}
228
229SANITIZER_INTERFACE_ATTRIBUTE
230int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id,
231 uptr *os_id) {
232 MBlock *b = 0;
233 Allocator *a = allocator();
234 if (a->PointerIsMine((void *)addr)) {
235 void *block_begin = a->GetBlockBegin((void *)addr);
236 if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
237 }
238 if (b == 0) return 0;
239
240 *thread_id = b->tid;
241 // No locking. This is supposed to be called from within the debugger when
242 // other threads are stopped.
243 ThreadContextBase *tctx = ctx->thread_registry->GetThreadLocked(b->tid);
244 *os_id = tctx->os_id;
245
246 StackTrace stack = StackDepotGet(b->stk);
247 size = Min(size, (uptr)stack.size);
248 for (uptr i = 0; i < size; i++) trace[i] = stack.trace[stack.size - i - 1];
249 return size;
250}