blob: 9e5465d37557a3d1033fe327c31e615d54605bfa [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
Kuba Mracek2903a9b2018-05-10 21:46:00 +000086int __tsan_get_report_tag(void *report, uptr *tag) {
87 const ReportDesc *rep = (ReportDesc *)report;
88 *tag = rep->tag;
89 return 1;
90}
91
92SANITIZER_INTERFACE_ATTRIBUTE
Kuba Breckaa1496f72016-03-10 17:00:29 +000093int __tsan_get_report_stack(void *report, uptr idx, void **trace,
94 uptr trace_size) {
95 const ReportDesc *rep = (ReportDesc *)report;
96 CHECK_LT(idx, rep->stacks.Size());
97 ReportStack *stack = rep->stacks[idx];
Kuba Brecka4b3833d2016-03-21 12:12:44 +000098 if (stack) CopyTrace(stack->frames, trace, trace_size);
99 return stack ? 1 : 0;
Kuba Breckaa1496f72016-03-10 17:00:29 +0000100}
101
102SANITIZER_INTERFACE_ATTRIBUTE
103int __tsan_get_report_mop(void *report, uptr idx, int *tid, void **addr,
104 int *size, int *write, int *atomic, void **trace,
105 uptr trace_size) {
106 const ReportDesc *rep = (ReportDesc *)report;
107 CHECK_LT(idx, rep->mops.Size());
108 ReportMop *mop = rep->mops[idx];
109 *tid = mop->tid;
110 *addr = (void *)mop->addr;
111 *size = mop->size;
112 *write = mop->write ? 1 : 0;
113 *atomic = mop->atomic ? 1 : 0;
Kuba Brecka4b3833d2016-03-21 12:12:44 +0000114 if (mop->stack) CopyTrace(mop->stack->frames, trace, trace_size);
Kuba Breckaa1496f72016-03-10 17:00:29 +0000115 return 1;
116}
117
118SANITIZER_INTERFACE_ATTRIBUTE
119int __tsan_get_report_loc(void *report, uptr idx, const char **type,
120 void **addr, uptr *start, uptr *size, int *tid,
121 int *fd, int *suppressable, void **trace,
122 uptr trace_size) {
123 const ReportDesc *rep = (ReportDesc *)report;
124 CHECK_LT(idx, rep->locs.Size());
125 ReportLocation *loc = rep->locs[idx];
126 *type = ReportLocationTypeDescription(loc->type);
127 *addr = (void *)loc->global.start;
128 *start = loc->heap_chunk_start;
129 *size = loc->heap_chunk_size;
130 *tid = loc->tid;
131 *fd = loc->fd;
132 *suppressable = loc->suppressable;
133 if (loc->stack) CopyTrace(loc->stack->frames, trace, trace_size);
134 return 1;
135}
136
137SANITIZER_INTERFACE_ATTRIBUTE
Kuba Mracekc90b79c2017-02-22 01:13:34 +0000138int __tsan_get_report_loc_object_type(void *report, uptr idx,
139 const char **object_type) {
140 const ReportDesc *rep = (ReportDesc *)report;
141 CHECK_LT(idx, rep->locs.Size());
142 ReportLocation *loc = rep->locs[idx];
143 *object_type = GetObjectTypeFromTag(loc->external_tag);
144 return 1;
145}
146
147SANITIZER_INTERFACE_ATTRIBUTE
Kuba Breckaa1496f72016-03-10 17:00:29 +0000148int __tsan_get_report_mutex(void *report, uptr idx, uptr *mutex_id, void **addr,
149 int *destroyed, void **trace, uptr trace_size) {
150 const ReportDesc *rep = (ReportDesc *)report;
151 CHECK_LT(idx, rep->mutexes.Size());
152 ReportMutex *mutex = rep->mutexes[idx];
153 *mutex_id = mutex->id;
154 *addr = (void *)mutex->addr;
155 *destroyed = mutex->destroyed;
Kuba Brecka4b3833d2016-03-21 12:12:44 +0000156 if (mutex->stack) CopyTrace(mutex->stack->frames, trace, trace_size);
Kuba Breckaa1496f72016-03-10 17:00:29 +0000157 return 1;
158}
159
160SANITIZER_INTERFACE_ATTRIBUTE
Kuba Mracekceb30b02017-04-17 18:17:38 +0000161int __tsan_get_report_thread(void *report, uptr idx, int *tid, tid_t *os_id,
Kuba Breckaa1496f72016-03-10 17:00:29 +0000162 int *running, const char **name, int *parent_tid,
163 void **trace, uptr trace_size) {
164 const ReportDesc *rep = (ReportDesc *)report;
165 CHECK_LT(idx, rep->threads.Size());
166 ReportThread *thread = rep->threads[idx];
167 *tid = thread->id;
Kuba Breckabf8b5f82016-04-21 14:49:25 +0000168 *os_id = thread->os_id;
Kuba Breckaa1496f72016-03-10 17:00:29 +0000169 *running = thread->running;
170 *name = thread->name;
171 *parent_tid = thread->parent_tid;
Kuba Brecka4b3833d2016-03-21 12:12:44 +0000172 if (thread->stack) CopyTrace(thread->stack->frames, trace, trace_size);
Kuba Breckaa1496f72016-03-10 17:00:29 +0000173 return 1;
174}
175
176SANITIZER_INTERFACE_ATTRIBUTE
177int __tsan_get_report_unique_tid(void *report, uptr idx, int *tid) {
178 const ReportDesc *rep = (ReportDesc *)report;
179 CHECK_LT(idx, rep->unique_tids.Size());
180 *tid = rep->unique_tids[idx];
181 return 1;
182}
Kuba Mracek1187cbd2016-12-19 17:52:20 +0000183
184SANITIZER_INTERFACE_ATTRIBUTE
185const char *__tsan_locate_address(uptr addr, char *name, uptr name_size,
186 uptr *region_address_ptr,
187 uptr *region_size_ptr) {
188 uptr region_address = 0;
189 uptr region_size = 0;
190 const char *region_kind = nullptr;
191 if (name && name_size > 0) name[0] = 0;
192
193 if (IsMetaMem(addr)) {
194 region_kind = "meta shadow";
195 } else if (IsShadowMem(addr)) {
196 region_kind = "shadow";
197 } else {
198 bool is_stack = false;
199 MBlock *b = 0;
200 Allocator *a = allocator();
201 if (a->PointerIsMine((void *)addr)) {
202 void *block_begin = a->GetBlockBegin((void *)addr);
203 if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
204 }
205
206 if (b != 0) {
207 region_address = (uptr)allocator()->GetBlockBegin((void *)addr);
208 region_size = b->siz;
209 region_kind = "heap";
210 } else {
211 // TODO(kuba.brecka): We should not lock. This is supposed to be called
212 // from within the debugger when other threads are stopped.
213 ctx->thread_registry->Lock();
214 ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack);
215 ctx->thread_registry->Unlock();
216 if (tctx) {
217 region_kind = is_stack ? "stack" : "tls";
218 } else {
219 region_kind = "global";
220 DataInfo info;
221 if (Symbolizer::GetOrInit()->SymbolizeData(addr, &info)) {
222 internal_strncpy(name, info.name, name_size);
223 region_address = info.start;
224 region_size = info.size;
225 }
226 }
227 }
228 }
229
230 CHECK(region_kind);
231 if (region_address_ptr) *region_address_ptr = region_address;
232 if (region_size_ptr) *region_size_ptr = region_size;
233 return region_kind;
234}
235
236SANITIZER_INTERFACE_ATTRIBUTE
237int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id,
Kuba Mracekceb30b02017-04-17 18:17:38 +0000238 tid_t *os_id) {
Kuba Mracek1187cbd2016-12-19 17:52:20 +0000239 MBlock *b = 0;
240 Allocator *a = allocator();
241 if (a->PointerIsMine((void *)addr)) {
242 void *block_begin = a->GetBlockBegin((void *)addr);
243 if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
244 }
245 if (b == 0) return 0;
246
247 *thread_id = b->tid;
248 // No locking. This is supposed to be called from within the debugger when
249 // other threads are stopped.
250 ThreadContextBase *tctx = ctx->thread_registry->GetThreadLocked(b->tid);
251 *os_id = tctx->os_id;
252
253 StackTrace stack = StackDepotGet(b->stk);
254 size = Min(size, (uptr)stack.size);
255 for (uptr i = 0; i < size; i++) trace[i] = stack.trace[stack.size - i - 1];
256 return size;
257}