Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 1 | //===-- 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 Mracek | 1187cbd | 2016-12-19 17:52:20 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_stackdepot.h" |
| 19 | |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 20 | using namespace __tsan; |
| 21 | |
| 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"; |
Kuba Mracek | aa78ad5 | 2017-02-02 13:17:05 +0000 | [diff] [blame] | 27 | if (typ == ReportTypeExternalRace) return "external-race"; |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 28 | if (typ == ReportTypeThreadLeak) return "thread-leak"; |
| 29 | if (typ == ReportTypeMutexDestroyLocked) return "locked-mutex-destroy"; |
| 30 | if (typ == ReportTypeMutexDoubleLock) return "mutex-double-lock"; |
Kuba Brecka | 46bf454 | 2016-03-16 15:39:20 +0000 | [diff] [blame] | 31 | if (typ == ReportTypeMutexInvalidAccess) return "mutex-invalid-access"; |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 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"; |
| 38 | return ""; |
| 39 | } |
| 40 | |
| 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"; |
| 47 | return ""; |
| 48 | } |
| 49 | |
| 50 | static 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. |
| 61 | SANITIZER_INTERFACE_ATTRIBUTE |
| 62 | void *__tsan_get_current_report() { |
Dmitry Vyukov | 066fefc | 2016-04-27 08:28:08 +0000 | [diff] [blame] | 63 | return const_cast<ReportDesc*>(cur_thread()->current_report); |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 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, |
| 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 | |
| 85 | SANITIZER_INTERFACE_ATTRIBUTE |
Kuba Mracek | 2903a9b | 2018-05-10 21:46:00 +0000 | [diff] [blame] | 86 | int __tsan_get_report_tag(void *report, uptr *tag) { |
| 87 | const ReportDesc *rep = (ReportDesc *)report; |
| 88 | *tag = rep->tag; |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | SANITIZER_INTERFACE_ATTRIBUTE |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 93 | int __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 Brecka | 4b3833d | 2016-03-21 12:12:44 +0000 | [diff] [blame] | 98 | if (stack) CopyTrace(stack->frames, trace, trace_size); |
| 99 | return stack ? 1 : 0; |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 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, |
| 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 Brecka | 4b3833d | 2016-03-21 12:12:44 +0000 | [diff] [blame] | 114 | if (mop->stack) CopyTrace(mop->stack->frames, trace, trace_size); |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 115 | return 1; |
| 116 | } |
| 117 | |
| 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, |
| 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 | |
| 137 | SANITIZER_INTERFACE_ATTRIBUTE |
Kuba Mracek | c90b79c | 2017-02-22 01:13:34 +0000 | [diff] [blame] | 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); |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | SANITIZER_INTERFACE_ATTRIBUTE |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 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; |
Kuba Brecka | 4b3833d | 2016-03-21 12:12:44 +0000 | [diff] [blame] | 156 | if (mutex->stack) CopyTrace(mutex->stack->frames, trace, trace_size); |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | SANITIZER_INTERFACE_ATTRIBUTE |
Kuba Mracek | ceb30b0 | 2017-04-17 18:17:38 +0000 | [diff] [blame] | 161 | int __tsan_get_report_thread(void *report, uptr idx, int *tid, tid_t *os_id, |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 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]; |
| 167 | *tid = thread->id; |
Kuba Brecka | bf8b5f8 | 2016-04-21 14:49:25 +0000 | [diff] [blame] | 168 | *os_id = thread->os_id; |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 169 | *running = thread->running; |
| 170 | *name = thread->name; |
| 171 | *parent_tid = thread->parent_tid; |
Kuba Brecka | 4b3833d | 2016-03-21 12:12:44 +0000 | [diff] [blame] | 172 | if (thread->stack) CopyTrace(thread->stack->frames, trace, trace_size); |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 173 | return 1; |
| 174 | } |
| 175 | |
| 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]; |
| 181 | return 1; |
| 182 | } |
Kuba Mracek | 1187cbd | 2016-12-19 17:52:20 +0000 | [diff] [blame] | 183 | |
| 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; |
| 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 | |
| 236 | SANITIZER_INTERFACE_ATTRIBUTE |
| 237 | int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id, |
Kuba Mracek | ceb30b0 | 2017-04-17 18:17:38 +0000 | [diff] [blame] | 238 | tid_t *os_id) { |
Kuba Mracek | 1187cbd | 2016-12-19 17:52:20 +0000 | [diff] [blame] | 239 | 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 | } |