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_stack(void *report, uptr idx, void **trace,
88 const ReportDesc *rep = (ReportDesc *)report;
89 CHECK_LT(idx, rep->stacks.Size());
90 ReportStack *stack = rep->stacks[idx];
91 if (stack) CopyTrace(stack->frames, trace, trace_size);
95 SANITIZER_INTERFACE_ATTRIBUTE
96 int __tsan_get_report_mop(void *report, uptr idx, int *tid, void **addr,
97 int *size, int *write, int *atomic, void **trace,
99 const ReportDesc *rep = (ReportDesc *)report;
100 CHECK_LT(idx, rep->mops.Size());
101 ReportMop *mop = rep->mops[idx];
103 *addr = (void *)mop->addr;
105 *write = mop->write ? 1 : 0;
106 *atomic = mop->atomic ? 1 : 0;
107 if (mop->stack) CopyTrace(mop->stack->frames, trace, trace_size);
111 SANITIZER_INTERFACE_ATTRIBUTE
112 int __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,
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;
125 *suppressable = loc->suppressable;
126 if (loc->stack) CopyTrace(loc->stack->frames, trace, trace_size);
130 SANITIZER_INTERFACE_ATTRIBUTE
131 int __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);
140 SANITIZER_INTERFACE_ATTRIBUTE
141 int __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;
149 if (mutex->stack) CopyTrace(mutex->stack->frames, trace, trace_size);
153 SANITIZER_INTERFACE_ATTRIBUTE
154 int __tsan_get_report_thread(void *report, uptr idx, int *tid, tid_t *os_id,
155 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];
161 *os_id = thread->os_id;
162 *running = thread->running;
163 *name = thread->name;
164 *parent_tid = thread->parent_tid;
165 if (thread->stack) CopyTrace(thread->stack->frames, trace, trace_size);
169 SANITIZER_INTERFACE_ATTRIBUTE
170 int __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];
177 SANITIZER_INTERFACE_ATTRIBUTE
178 const 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;
186 if (IsMetaMem(addr)) {
187 region_kind = "meta shadow";
188 } else if (IsShadowMem(addr)) {
189 region_kind = "shadow";
191 bool is_stack = false;
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);
200 region_address = (uptr)allocator()->GetBlockBegin((void *)addr);
201 region_size = b->siz;
202 region_kind = "heap";
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();
210 region_kind = is_stack ? "stack" : "tls";
212 region_kind = "global";
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;
224 if (region_address_ptr) *region_address_ptr = region_address;
225 if (region_size_ptr) *region_size_ptr = region_size;
229 SANITIZER_INTERFACE_ATTRIBUTE
230 int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id,
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);
238 if (b == 0) return 0;
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;
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];