blob: 36087948120d7f86a9e45f0c16e9101f373aa128 [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
131int __tsan_get_report_mutex(void *report, uptr idx, uptr *mutex_id, void **addr,
132 int *destroyed, void **trace, uptr trace_size) {
133 const ReportDesc *rep = (ReportDesc *)report;
134 CHECK_LT(idx, rep->mutexes.Size());
135 ReportMutex *mutex = rep->mutexes[idx];
136 *mutex_id = mutex->id;
137 *addr = (void *)mutex->addr;
138 *destroyed = mutex->destroyed;
Kuba Brecka4b3833d2016-03-21 12:12:44 +0000139 if (mutex->stack) CopyTrace(mutex->stack->frames, trace, trace_size);
Kuba Breckaa1496f72016-03-10 17:00:29 +0000140 return 1;
141}
142
143SANITIZER_INTERFACE_ATTRIBUTE
Kuba Breckabf8b5f82016-04-21 14:49:25 +0000144int __tsan_get_report_thread(void *report, uptr idx, int *tid, uptr *os_id,
Kuba Breckaa1496f72016-03-10 17:00:29 +0000145 int *running, const char **name, int *parent_tid,
146 void **trace, uptr trace_size) {
147 const ReportDesc *rep = (ReportDesc *)report;
148 CHECK_LT(idx, rep->threads.Size());
149 ReportThread *thread = rep->threads[idx];
150 *tid = thread->id;
Kuba Breckabf8b5f82016-04-21 14:49:25 +0000151 *os_id = thread->os_id;
Kuba Breckaa1496f72016-03-10 17:00:29 +0000152 *running = thread->running;
153 *name = thread->name;
154 *parent_tid = thread->parent_tid;
Kuba Brecka4b3833d2016-03-21 12:12:44 +0000155 if (thread->stack) CopyTrace(thread->stack->frames, trace, trace_size);
Kuba Breckaa1496f72016-03-10 17:00:29 +0000156 return 1;
157}
158
159SANITIZER_INTERFACE_ATTRIBUTE
160int __tsan_get_report_unique_tid(void *report, uptr idx, int *tid) {
161 const ReportDesc *rep = (ReportDesc *)report;
162 CHECK_LT(idx, rep->unique_tids.Size());
163 *tid = rep->unique_tids[idx];
164 return 1;
165}
Kuba Mracek1187cbd2016-12-19 17:52:20 +0000166
167SANITIZER_INTERFACE_ATTRIBUTE
168const char *__tsan_locate_address(uptr addr, char *name, uptr name_size,
169 uptr *region_address_ptr,
170 uptr *region_size_ptr) {
171 uptr region_address = 0;
172 uptr region_size = 0;
173 const char *region_kind = nullptr;
174 if (name && name_size > 0) name[0] = 0;
175
176 if (IsMetaMem(addr)) {
177 region_kind = "meta shadow";
178 } else if (IsShadowMem(addr)) {
179 region_kind = "shadow";
180 } else {
181 bool is_stack = false;
182 MBlock *b = 0;
183 Allocator *a = allocator();
184 if (a->PointerIsMine((void *)addr)) {
185 void *block_begin = a->GetBlockBegin((void *)addr);
186 if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
187 }
188
189 if (b != 0) {
190 region_address = (uptr)allocator()->GetBlockBegin((void *)addr);
191 region_size = b->siz;
192 region_kind = "heap";
193 } else {
194 // TODO(kuba.brecka): We should not lock. This is supposed to be called
195 // from within the debugger when other threads are stopped.
196 ctx->thread_registry->Lock();
197 ThreadContext *tctx = IsThreadStackOrTls(addr, &is_stack);
198 ctx->thread_registry->Unlock();
199 if (tctx) {
200 region_kind = is_stack ? "stack" : "tls";
201 } else {
202 region_kind = "global";
203 DataInfo info;
204 if (Symbolizer::GetOrInit()->SymbolizeData(addr, &info)) {
205 internal_strncpy(name, info.name, name_size);
206 region_address = info.start;
207 region_size = info.size;
208 }
209 }
210 }
211 }
212
213 CHECK(region_kind);
214 if (region_address_ptr) *region_address_ptr = region_address;
215 if (region_size_ptr) *region_size_ptr = region_size;
216 return region_kind;
217}
218
219SANITIZER_INTERFACE_ATTRIBUTE
220int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id,
221 uptr *os_id) {
222 MBlock *b = 0;
223 Allocator *a = allocator();
224 if (a->PointerIsMine((void *)addr)) {
225 void *block_begin = a->GetBlockBegin((void *)addr);
226 if (block_begin) b = ctx->metamap.GetBlock((uptr)block_begin);
227 }
228 if (b == 0) return 0;
229
230 *thread_id = b->tid;
231 // No locking. This is supposed to be called from within the debugger when
232 // other threads are stopped.
233 ThreadContextBase *tctx = ctx->thread_registry->GetThreadLocked(b->tid);
234 *os_id = tctx->os_id;
235
236 StackTrace stack = StackDepotGet(b->stk);
237 size = Min(size, (uptr)stack.size);
238 for (uptr i = 0; i < size; i++) trace[i] = stack.trace[stack.size - i - 1];
239 return size;
240}