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
18 using namespace __tsan;
19
ReportTypeDescription(ReportType typ)20 static const char *ReportTypeDescription(ReportType typ) {
21 if (typ == ReportTypeRace) return "data-race";
22 if (typ == ReportTypeVptrRace) return "data-race-vptr";
23 if (typ == ReportTypeUseAfterFree) return "heap-use-after-free";
24 if (typ == ReportTypeVptrUseAfterFree) return "heap-use-after-free-vptr";
25 if (typ == ReportTypeThreadLeak) return "thread-leak";
26 if (typ == ReportTypeMutexDestroyLocked) return "locked-mutex-destroy";
27 if (typ == ReportTypeMutexDoubleLock) return "mutex-double-lock";
28 if (typ == ReportTypeMutexInvalidAccess) return "mutex-invalid-access";
29 if (typ == ReportTypeMutexBadUnlock) return "mutex-bad-unlock";
30 if (typ == ReportTypeMutexBadReadLock) return "mutex-bad-read-lock";
31 if (typ == ReportTypeMutexBadReadUnlock) return "mutex-bad-read-unlock";
32 if (typ == ReportTypeSignalUnsafe) return "signal-unsafe-call";
33 if (typ == ReportTypeErrnoInSignal) return "errno-in-signal-handler";
34 if (typ == ReportTypeDeadlock) return "lock-order-inversion";
35 return "";
36 }
37
ReportLocationTypeDescription(ReportLocationType typ)38 static const char *ReportLocationTypeDescription(ReportLocationType typ) {
39 if (typ == ReportLocationGlobal) return "global";
40 if (typ == ReportLocationHeap) return "heap";
41 if (typ == ReportLocationStack) return "stack";
42 if (typ == ReportLocationTLS) return "tls";
43 if (typ == ReportLocationFD) return "fd";
44 return "";
45 }
46
CopyTrace(SymbolizedStack * first_frame,void ** trace,uptr trace_size)47 static void CopyTrace(SymbolizedStack *first_frame, void **trace,
48 uptr trace_size) {
49 uptr i = 0;
50 for (SymbolizedStack *frame = first_frame; frame != nullptr;
51 frame = frame->next) {
52 trace[i++] = (void *)frame->info.address;
53 if (i >= trace_size) break;
54 }
55 }
56
57 // Meant to be called by the debugger.
58 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_get_current_report()59 void *__tsan_get_current_report() {
60 return const_cast<ReportDesc*>(cur_thread()->current_report);
61 }
62
63 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_get_report_data(void * report,const char ** description,int * count,int * stack_count,int * mop_count,int * loc_count,int * mutex_count,int * thread_count,int * unique_tid_count,void ** sleep_trace,uptr trace_size)64 int __tsan_get_report_data(void *report, const char **description, int *count,
65 int *stack_count, int *mop_count, int *loc_count,
66 int *mutex_count, int *thread_count,
67 int *unique_tid_count, void **sleep_trace,
68 uptr trace_size) {
69 const ReportDesc *rep = (ReportDesc *)report;
70 *description = ReportTypeDescription(rep->typ);
71 *count = rep->count;
72 *stack_count = rep->stacks.Size();
73 *mop_count = rep->mops.Size();
74 *loc_count = rep->locs.Size();
75 *mutex_count = rep->mutexes.Size();
76 *thread_count = rep->threads.Size();
77 *unique_tid_count = rep->unique_tids.Size();
78 if (rep->sleep) CopyTrace(rep->sleep->frames, sleep_trace, trace_size);
79 return 1;
80 }
81
82 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_get_report_stack(void * report,uptr idx,void ** trace,uptr trace_size)83 int __tsan_get_report_stack(void *report, uptr idx, void **trace,
84 uptr trace_size) {
85 const ReportDesc *rep = (ReportDesc *)report;
86 CHECK_LT(idx, rep->stacks.Size());
87 ReportStack *stack = rep->stacks[idx];
88 if (stack) CopyTrace(stack->frames, trace, trace_size);
89 return stack ? 1 : 0;
90 }
91
92 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_get_report_mop(void * report,uptr idx,int * tid,void ** addr,int * size,int * write,int * atomic,void ** trace,uptr trace_size)93 int __tsan_get_report_mop(void *report, uptr idx, int *tid, void **addr,
94 int *size, int *write, int *atomic, void **trace,
95 uptr trace_size) {
96 const ReportDesc *rep = (ReportDesc *)report;
97 CHECK_LT(idx, rep->mops.Size());
98 ReportMop *mop = rep->mops[idx];
99 *tid = mop->tid;
100 *addr = (void *)mop->addr;
101 *size = mop->size;
102 *write = mop->write ? 1 : 0;
103 *atomic = mop->atomic ? 1 : 0;
104 if (mop->stack) CopyTrace(mop->stack->frames, trace, trace_size);
105 return 1;
106 }
107
108 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_get_report_loc(void * report,uptr idx,const char ** type,void ** addr,uptr * start,uptr * size,int * tid,int * fd,int * suppressable,void ** trace,uptr trace_size)109 int __tsan_get_report_loc(void *report, uptr idx, const char **type,
110 void **addr, uptr *start, uptr *size, int *tid,
111 int *fd, int *suppressable, void **trace,
112 uptr trace_size) {
113 const ReportDesc *rep = (ReportDesc *)report;
114 CHECK_LT(idx, rep->locs.Size());
115 ReportLocation *loc = rep->locs[idx];
116 *type = ReportLocationTypeDescription(loc->type);
117 *addr = (void *)loc->global.start;
118 *start = loc->heap_chunk_start;
119 *size = loc->heap_chunk_size;
120 *tid = loc->tid;
121 *fd = loc->fd;
122 *suppressable = loc->suppressable;
123 if (loc->stack) CopyTrace(loc->stack->frames, trace, trace_size);
124 return 1;
125 }
126
127 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_get_report_mutex(void * report,uptr idx,uptr * mutex_id,void ** addr,int * destroyed,void ** trace,uptr trace_size)128 int __tsan_get_report_mutex(void *report, uptr idx, uptr *mutex_id, void **addr,
129 int *destroyed, void **trace, uptr trace_size) {
130 const ReportDesc *rep = (ReportDesc *)report;
131 CHECK_LT(idx, rep->mutexes.Size());
132 ReportMutex *mutex = rep->mutexes[idx];
133 *mutex_id = mutex->id;
134 *addr = (void *)mutex->addr;
135 *destroyed = mutex->destroyed;
136 if (mutex->stack) CopyTrace(mutex->stack->frames, trace, trace_size);
137 return 1;
138 }
139
140 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_get_report_thread(void * report,uptr idx,int * tid,uptr * os_id,int * running,const char ** name,int * parent_tid,void ** trace,uptr trace_size)141 int __tsan_get_report_thread(void *report, uptr idx, int *tid, uptr *os_id,
142 int *running, const char **name, int *parent_tid,
143 void **trace, uptr trace_size) {
144 const ReportDesc *rep = (ReportDesc *)report;
145 CHECK_LT(idx, rep->threads.Size());
146 ReportThread *thread = rep->threads[idx];
147 *tid = thread->id;
148 *os_id = thread->os_id;
149 *running = thread->running;
150 *name = thread->name;
151 *parent_tid = thread->parent_tid;
152 if (thread->stack) CopyTrace(thread->stack->frames, trace, trace_size);
153 return 1;
154 }
155
156 SANITIZER_INTERFACE_ATTRIBUTE
__tsan_get_report_unique_tid(void * report,uptr idx,int * tid)157 int __tsan_get_report_unique_tid(void *report, uptr idx, int *tid) {
158 const ReportDesc *rep = (ReportDesc *)report;
159 CHECK_LT(idx, rep->unique_tids.Size());
160 *tid = rep->unique_tids[idx];
161 return 1;
162 }
163