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