1 //===-- tsan_debugging.cc -------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of ThreadSanitizer (TSan), a race detector.
12 // TSan debugging API implementation.
13 //===----------------------------------------------------------------------===//
14 #include "tsan_interface.h"
15 #include "tsan_report.h"
18 #include "sanitizer_common/sanitizer_stackdepot.h"
20 using namespace __tsan;
22 static 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";
27 if (typ == ReportTypeExternalRace) return "external-race";
28 if (typ == ReportTypeThreadLeak) return "thread-leak";
29 if (typ == ReportTypeMutexDestroyLocked) return "locked-mutex-destroy";
30 if (typ == ReportTypeMutexDoubleLock) return "mutex-double-lock";
31 if (typ == ReportTypeMutexInvalidAccess) return "mutex-invalid-access";
32 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";
41 static 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";
50 static void CopyTrace(SymbolizedStack *first_frame, void **trace,
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;
60 // Meant to be called by the debugger.
61 SANITIZER_INTERFACE_ATTRIBUTE
62 void *__tsan_get_current_report() {
63 return const_cast<ReportDesc*>(cur_thread()->current_report);
66 SANITIZER_INTERFACE_ATTRIBUTE
67 int __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,
72 const ReportDesc *rep = (ReportDesc *)report;
73 *description = ReportTypeDescription(rep->typ);
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);
85 SANITIZER_INTERFACE_ATTRIBUTE
86 int __tsan_get_report_tag(void *report, uptr *tag) {
87 const ReportDesc *rep = (ReportDesc *)report;
92 SANITIZER_INTERFACE_ATTRIBUTE
93 int __tsan_get_report_stack(void *report, uptr idx, void **trace,
95 const ReportDesc *rep = (ReportDesc *)report;
96 CHECK_LT(idx, rep->stacks.Size());
97 ReportStack *stack = rep->stacks[idx];
98 if (stack) CopyTrace(stack->frames, trace, trace_size);
102 SANITIZER_INTERFACE_ATTRIBUTE
103 int __tsan_get_report_mop(void *report, uptr idx, int *tid, void **addr,
104 int *size, int *write, int *atomic, void **trace,
106 const ReportDesc *rep = (ReportDesc *)report;
107 CHECK_LT(idx, rep->mops.Size());
108 ReportMop *mop = rep->mops[idx];
110 *addr = (void *)mop->addr;
112 *write = mop->write ? 1 : 0;
113 *atomic = mop->atomic ? 1 : 0;
114 if (mop->stack) CopyTrace(mop->stack->frames, trace, trace_size);
118 SANITIZER_INTERFACE_ATTRIBUTE
119 int __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,
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;
132 *suppressable = loc->suppressable;
133 if (loc->stack) CopyTrace(loc->stack->frames, trace, trace_size);
137 SANITIZER_INTERFACE_ATTRIBUTE
138 int __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);
147 SANITIZER_INTERFACE_ATTRIBUTE
148 int __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;
156 if (mutex->stack) CopyTrace(mutex->stack->frames, trace, trace_size);
160 SANITIZER_INTERFACE_ATTRIBUTE
161 int __tsan_get_report_thread(void *report, uptr idx, int *tid, tid_t *os_id,
162 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];
168 *os_id = thread->os_id;
169 *running = thread->running;
170 *name = thread->name;
171 *parent_tid = thread->parent_tid;
172 if (thread->stack) CopyTrace(thread->stack->frames, trace, trace_size);
176 SANITIZER_INTERFACE_ATTRIBUTE
177 int __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];
184 SANITIZER_INTERFACE_ATTRIBUTE
185 const 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;
193 if (IsMetaMem(addr)) {
194 region_kind = "meta shadow";
195 } else if (IsShadowMem(addr)) {
196 region_kind = "shadow";
198 bool is_stack = false;
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);
207 region_address = (uptr)allocator()->GetBlockBegin((void *)addr);
208 region_size = b->siz;
209 region_kind = "heap";
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();
217 region_kind = is_stack ? "stack" : "tls";
219 region_kind = "global";
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;
231 if (region_address_ptr) *region_address_ptr = region_address;
232 if (region_size_ptr) *region_size_ptr = region_size;
236 SANITIZER_INTERFACE_ATTRIBUTE
237 int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id,
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);
245 if (b == 0) return 0;
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;
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];