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 |
| 86 | int __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 Brecka | 4b3833d | 2016-03-21 12:12:44 +0000 | [diff] [blame] | 91 | if (stack) CopyTrace(stack->frames, trace, trace_size); |
| 92 | return stack ? 1 : 0; |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 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, |
| 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 Brecka | 4b3833d | 2016-03-21 12:12:44 +0000 | [diff] [blame] | 107 | if (mop->stack) CopyTrace(mop->stack->frames, trace, trace_size); |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 108 | return 1; |
| 109 | } |
| 110 | |
| 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, |
| 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 | |
| 130 | SANITIZER_INTERFACE_ATTRIBUTE |
Kuba Mracek | c90b79c | 2017-02-22 01:13:34 +0000 | [diff] [blame^] | 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); |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | SANITIZER_INTERFACE_ATTRIBUTE |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 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; |
Kuba Brecka | 4b3833d | 2016-03-21 12:12:44 +0000 | [diff] [blame] | 149 | if (mutex->stack) CopyTrace(mutex->stack->frames, trace, trace_size); |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | SANITIZER_INTERFACE_ATTRIBUTE |
Kuba Brecka | bf8b5f8 | 2016-04-21 14:49:25 +0000 | [diff] [blame] | 154 | int __tsan_get_report_thread(void *report, uptr idx, int *tid, uptr *os_id, |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 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]; |
| 160 | *tid = thread->id; |
Kuba Brecka | bf8b5f8 | 2016-04-21 14:49:25 +0000 | [diff] [blame] | 161 | *os_id = thread->os_id; |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 162 | *running = thread->running; |
| 163 | *name = thread->name; |
| 164 | *parent_tid = thread->parent_tid; |
Kuba Brecka | 4b3833d | 2016-03-21 12:12:44 +0000 | [diff] [blame] | 165 | if (thread->stack) CopyTrace(thread->stack->frames, trace, trace_size); |
Kuba Brecka | a1496f7 | 2016-03-10 17:00:29 +0000 | [diff] [blame] | 166 | return 1; |
| 167 | } |
| 168 | |
| 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]; |
| 174 | return 1; |
| 175 | } |
Kuba Mracek | 1187cbd | 2016-12-19 17:52:20 +0000 | [diff] [blame] | 176 | |
| 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; |
| 185 | |
| 186 | if (IsMetaMem(addr)) { |
| 187 | region_kind = "meta shadow"; |
| 188 | } else if (IsShadowMem(addr)) { |
| 189 | region_kind = "shadow"; |
| 190 | } else { |
| 191 | bool is_stack = false; |
| 192 | MBlock *b = 0; |
| 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); |
| 197 | } |
| 198 | |
| 199 | if (b != 0) { |
| 200 | region_address = (uptr)allocator()->GetBlockBegin((void *)addr); |
| 201 | region_size = b->siz; |
| 202 | region_kind = "heap"; |
| 203 | } else { |
| 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(); |
| 209 | if (tctx) { |
| 210 | region_kind = is_stack ? "stack" : "tls"; |
| 211 | } else { |
| 212 | region_kind = "global"; |
| 213 | DataInfo info; |
| 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; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | CHECK(region_kind); |
| 224 | if (region_address_ptr) *region_address_ptr = region_address; |
| 225 | if (region_size_ptr) *region_size_ptr = region_size; |
| 226 | return region_kind; |
| 227 | } |
| 228 | |
| 229 | SANITIZER_INTERFACE_ATTRIBUTE |
| 230 | int __tsan_get_alloc_stack(uptr addr, uptr *trace, uptr size, int *thread_id, |
| 231 | uptr *os_id) { |
| 232 | MBlock *b = 0; |
| 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); |
| 237 | } |
| 238 | if (b == 0) return 0; |
| 239 | |
| 240 | *thread_id = b->tid; |
| 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; |
| 245 | |
| 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]; |
| 249 | return size; |
| 250 | } |