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 == ReportTypeThreadLeak) return "thread-leak";
28 if (typ == ReportTypeMutexDestroyLocked) return "locked-mutex-destroy";
29 if (typ == ReportTypeMutexDoubleLock) return "mutex-double-lock";
30 if (typ == ReportTypeMutexInvalidAccess) return "mutex-invalid-access";
31 if (typ == ReportTypeMutexBadUnlock) return "mutex-bad-unlock";
32 if (typ == ReportTypeMutexBadReadLock) return "mutex-bad-read-lock";
33 if (typ == ReportTypeMutexBadReadUnlock) return "mutex-bad-read-unlock";
34 if (typ == ReportTypeSignalUnsafe) return "signal-unsafe-call";
35 if (typ == ReportTypeErrnoInSignal) return "errno-in-signal-handler";
36 if (typ == ReportTypeDeadlock) return "lock-order-inversion";
40 static const char *ReportLocationTypeDescription(ReportLocationType typ) {
41 if (typ == ReportLocationGlobal) return "global";
42 if (typ == ReportLocationHeap) return "heap";
43 if (typ == ReportLocationStack) return "stack";
44 if (typ == ReportLocationTLS) return "tls";
45 if (typ == ReportLocationFD) return "fd";
49 static void CopyTrace(SymbolizedStack *first_frame, void **trace,
52 for (SymbolizedStack *frame = first_frame; frame != nullptr;
53 frame = frame->next) {
54 trace[i++] = (void *)frame->info.address;
55 if (i >= trace_size) break;
59 // Meant to be called by the debugger.
60 SANITIZER_INTERFACE_ATTRIBUTE
61 void *__tsan_get_current_report() {
62 return const_cast<ReportDesc*>(cur_thread()->current_report);
65 SANITIZER_INTERFACE_ATTRIBUTE
66 int __tsan_get_report_data(void *report, const char **description, int *count,
67 int *stack_count, int *mop_count, int *loc_count,
68 int *mutex_count, int *thread_count,
69 int *unique_tid_count, void **sleep_trace,
71 const ReportDesc *rep = (ReportDesc *)report;
72 *description = ReportTypeDescription(rep->typ);
74 *stack_count = rep->stacks.Size();
75 *mop_count = rep->mops.Size();
76 *loc_count = rep->locs.Size();
77 *mutex_count = rep->mutexes.Size();
78 *thread_count = rep->threads.Size();
79 *unique_tid_count = rep->unique_tids.Size();
80 if (rep->sleep) CopyTrace(rep->sleep->frames, sleep_trace, trace_size);
84 SANITIZER_INTERFACE_ATTRIBUTE
85 int __tsan_get_report_stack(void *report, uptr idx, void **trace,
87 const ReportDesc *rep = (ReportDesc *)report;
88 CHECK_LT(idx, rep->stacks.Size());
89 ReportStack *stack = rep->stacks[idx];
90 if (stack) CopyTrace(stack->frames, trace, trace_size);
94 SANITIZER_INTERFACE_ATTRIBUTE
95 int __tsan_get_report_mop(void *report, uptr idx, int *tid, void **addr,
96 int *size, int *write, int *atomic, void **trace,
98 const ReportDesc *rep = (ReportDesc *)report;
99 CHECK_LT(idx, rep->mops.Size());
100 ReportMop *mop = rep->mops[idx];
102 *addr = (void *)mop->addr;
104 *write = mop->write ? 1 : 0;
105 *atomic = mop->atomic ? 1 : 0;
106 if (mop->stack) CopyTrace(mop->stack->frames, trace, trace_size);
110 SANITIZER_INTERFACE_ATTRIBUTE
111 int __tsan_get_report_loc(void *report, uptr idx, const char **type,
112 void **addr, uptr *start, uptr *size, int *tid,
113 int *fd, int *suppressable, void **trace,
115 const ReportDesc *rep = (ReportDesc *)report;
116 CHECK_LT(idx, rep->locs.Size());
117 ReportLocation *loc = rep->locs[idx];
118 *type = ReportLocationTypeDescription(loc->type);
119 *addr = (void *)loc->global.start;
120 *start = loc->heap_chunk_start;
121 *size = loc->heap_chunk_size;
124 *suppressable = loc->suppressable;
125 if (loc->stack) CopyTrace(loc->stack->frames, trace, trace_size);
129 SANITIZER_INTERFACE_ATTRIBUTE
130 int __tsan_get_report_mutex(void *report, uptr idx, uptr *mutex_id, void **addr,
131 int *destroyed, void **trace, uptr trace_size) {
132 const ReportDesc *rep = (ReportDesc *)report;
133 CHECK_LT(idx, rep->mutexes.Size());
134 ReportMutex *mutex = rep->mutexes[idx];
135 *mutex_id = mutex->id;
136 *addr = (void *)mutex->addr;
137 *destroyed = mutex->destroyed;
138 if (mutex->stack) CopyTrace(mutex->stack->frames, trace, trace_size);
142 SANITIZER_INTERFACE_ATTRIBUTE
143 int __tsan_get_report_thread(void *report, uptr idx, int *tid, uptr *os_id,
144 int *running, const char **name, int *parent_tid,
145 void **trace, uptr trace_size) {
146 const ReportDesc *rep = (ReportDesc *)report;
147 CHECK_LT(idx, rep->threads.Size());
148 ReportThread *thread = rep->threads[idx];
150 *os_id = thread->os_id;
151 *running = thread->running;
152 *name = thread->name;
153 *parent_tid = thread->parent_tid;
154 if (thread->stack) CopyTrace(thread->stack->frames, trace, trace_size);
158 SANITIZER_INTERFACE_ATTRIBUTE
159 int __tsan_get_report_unique_tid(void *report, uptr idx, int *tid) {
160 const ReportDesc *rep = (ReportDesc *)report;
161 CHECK_LT(idx, rep->unique_tids.Size());
162 *tid = rep->unique_tids[idx];
166 SANITIZER_INTERFACE_ATTRIBUTE
167 const char *__tsan_locate_address(uptr addr, char *name, uptr name_size,
168 uptr *region_address_ptr,
169 uptr *region_size_ptr) {
170 uptr region_address = 0;
171 uptr region_size = 0;
172 const char *region_kind = nullptr;
173 if (name && name_size > 0) name[0] = 0;
175 if (IsMetaMem(addr)) {
176 region_kind = "meta shadow";
177 } else if (IsShadowMem(addr)) {
178 region_kind = "shadow";
180 bool is_stack = false;
182 Allocator *a = allocator();
183 if (a->PointerIsMine((void *)addr)) {
184 void *block_begin = a->GetBlockBegin((void *)addr);
185 if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
189 region_address = (uptr)allocator()->GetBlockBegin((void *)addr);
190 region_size = b->siz;
191 region_kind = "heap";
193 // TODO(kuba.brecka): We should not lock. This is supposed to be called
194 // from within the debugger when other threads are stopped.
195 ctx->thread_registry->Lock();
196 ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack);
197 ctx->thread_registry->Unlock();
199 region_kind = is_stack ? "stack" : "tls";
201 region_kind = "global";
203 if (Symbolizer::GetOrInit()->SymbolizeData(addr, &info)) {
204 internal_strncpy(name, info.name, name_size);
205 region_address = info.start;
206 region_size = info.size;
213 if (region_address_ptr) *region_address_ptr = region_address;
214 if (region_size_ptr) *region_size_ptr = region_size;
218 SANITIZER_INTERFACE_ATTRIBUTE
219 int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id,
222 Allocator *a = allocator();
223 if (a->PointerIsMine((void *)addr)) {
224 void *block_begin = a->GetBlockBegin((void *)addr);
225 if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
227 if (b == 0) return 0;
230 // No locking. This is supposed to be called from within the debugger when
231 // other threads are stopped.
232 ThreadContextBase *tctx = ctx->thread_registry->GetThreadLocked(b->tid);
233 *os_id = tctx->os_id;
235 StackTrace stack = StackDepotGet(b->stk);
236 size = Min(size, (uptr)stack.size);
237 for (uptr i = 0; i < size; i++) trace[i] = stack.trace[stack.size - i - 1];