1 //===-- tsan_report.cc ----------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //===----------------------------------------------------------------------===//
11 #include "tsan_report.h"
12 #include "tsan_platform.h"
17 ReportDesc::ReportDesc()
18 : stacks(MBlockReportStack)
19 , mops(MBlockReportMop)
20 , locs(MBlockReportLoc)
21 , mutexes(MBlockReportMutex)
22 , threads(MBlockReportThread)
26 ReportMop::ReportMop()
27 : mset(MBlockReportMutex) {
30 ReportDesc::~ReportDesc() {
31 // FIXME(dvyukov): it must be leaking a lot of memory.
36 const int kThreadBufSize = 32;
37 const char *thread_name(char *buf, int tid) {
40 internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
44 static const char *ReportTypeString(ReportType typ) {
45 if (typ == ReportTypeRace)
47 if (typ == ReportTypeUseAfterFree)
48 return "heap-use-after-free";
49 if (typ == ReportTypeThreadLeak)
51 if (typ == ReportTypeMutexDestroyLocked)
52 return "destroy of a locked mutex";
53 if (typ == ReportTypeSignalUnsafe)
54 return "signal-unsafe call inside of a signal";
55 if (typ == ReportTypeErrnoInSignal)
56 return "signal handler spoils errno";
60 void PrintStack(const ReportStack *ent) {
62 Printf(" [failed to restore the stack]\n\n");
65 for (int i = 0; ent; ent = ent->next, i++) {
66 Printf(" #%d %s %s:%d", i, ent->func, ent->file, ent->line);
68 Printf(":%d", ent->col);
69 if (ent->module && ent->offset)
70 Printf(" (%s+%p)\n", ent->module, (void*)ent->offset);
72 Printf(" (%p)\n", (void*)ent->pc);
77 static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
78 for (uptr i = 0; i < mset.Size(); i++) {
81 const ReportMopMutex m = mset[i];
82 Printf(" %s M%llu", m.write ? "write" : "read", m.id);
83 Printf(i == mset.Size() - 1 ? ")" : ",");
87 static const char *MopDesc(bool first, bool write, bool atomic) {
88 return atomic ? (first ? (write ? "Atomic write" : "Atomic read")
89 : (write ? "Previous atomic write" : "Previous atomic read"))
90 : (first ? (write ? "Write" : "Read")
91 : (write ? "Previous write" : "Previous read"));
94 static void PrintMop(const ReportMop *mop, bool first) {
95 char thrbuf[kThreadBufSize];
96 Printf(" %s of size %d at %p by %s",
97 MopDesc(first, mop->write, mop->atomic),
98 mop->size, (void*)mop->addr,
99 thread_name(thrbuf, mop->tid));
100 PrintMutexSet(mop->mset);
102 PrintStack(mop->stack);
105 static void PrintLocation(const ReportLocation *loc) {
106 char thrbuf[kThreadBufSize];
107 if (loc->type == ReportLocationGlobal) {
108 Printf(" Location is global '%s' of size %zu at %zx (%s+%p)\n\n",
109 loc->name, loc->size, loc->addr, loc->module, loc->offset);
110 } else if (loc->type == ReportLocationHeap) {
111 char thrbuf[kThreadBufSize];
112 Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
113 loc->size, loc->addr, thread_name(thrbuf, loc->tid));
114 PrintStack(loc->stack);
115 } else if (loc->type == ReportLocationStack) {
116 Printf(" Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
117 } else if (loc->type == ReportLocationTLS) {
118 Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
119 } else if (loc->type == ReportLocationFD) {
120 Printf(" Location is file descriptor %d created by %s at:\n",
121 loc->fd, thread_name(thrbuf, loc->tid));
122 PrintStack(loc->stack);
126 static void PrintMutex(const ReportMutex *rm) {
128 Printf(" Mutex M%llu is already destroyed.\n\n", rm->id);
130 Printf(" Mutex M%llu created at:\n", rm->id);
131 PrintStack(rm->stack);
135 static void PrintThread(const ReportThread *rt) {
136 if (rt->id == 0) // Little sense in describing the main thread.
138 Printf(" Thread T%d", rt->id);
140 Printf(" '%s'", rt->name);
141 char thrbuf[kThreadBufSize];
142 Printf(" (tid=%zu, %s) created by %s",
143 rt->pid, rt->running ? "running" : "finished",
144 thread_name(thrbuf, rt->parent_tid));
148 PrintStack(rt->stack);
151 static void PrintSleep(const ReportStack *s) {
152 Printf(" As if synchronized via sleep:\n");
156 static ReportStack *ChooseSummaryStack(const ReportDesc *rep) {
157 if (rep->mops.Size())
158 return rep->mops[0]->stack;
159 if (rep->stacks.Size())
160 return rep->stacks[0];
161 if (rep->mutexes.Size())
162 return rep->mutexes[0]->stack;
163 if (rep->threads.Size())
164 return rep->threads[0]->stack;
168 ReportStack *SkipTsanInternalFrames(ReportStack *ent) {
169 while (FrameIsInternal(ent) && ent->next)
174 void PrintReport(const ReportDesc *rep) {
175 Printf("==================\n");
176 const char *rep_typ_str = ReportTypeString(rep->typ);
177 Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str, GetPid());
179 for (uptr i = 0; i < rep->stacks.Size(); i++) {
182 PrintStack(rep->stacks[i]);
185 for (uptr i = 0; i < rep->mops.Size(); i++)
186 PrintMop(rep->mops[i], i == 0);
189 PrintSleep(rep->sleep);
191 for (uptr i = 0; i < rep->locs.Size(); i++)
192 PrintLocation(rep->locs[i]);
194 for (uptr i = 0; i < rep->mutexes.Size(); i++)
195 PrintMutex(rep->mutexes[i]);
197 for (uptr i = 0; i < rep->threads.Size(); i++)
198 PrintThread(rep->threads[i]);
200 if (ReportStack *ent = SkipTsanInternalFrames(ChooseSummaryStack(rep)))
201 ReportErrorSummary(rep_typ_str, ent->file, ent->line, ent->func);
203 Printf("==================\n");
208 void PrintStack(const ReportStack *ent) {
210 Printf(" [failed to restore the stack]\n\n");
213 for (int i = 0; ent; ent = ent->next, i++) {
214 Printf(" %s()\n %s:%d +0x%zx\n",
215 ent->func, ent->file, ent->line, (void*)ent->offset);
220 static void PrintMop(const ReportMop *mop, bool first) {
221 Printf("%s by goroutine %d:\n",
222 (first ? (mop->write ? "Write" : "Read")
223 : (mop->write ? "Previous write" : "Previous read")),
225 PrintStack(mop->stack);
228 static void PrintThread(const ReportThread *rt) {
229 if (rt->id == 0) // Little sense in describing the main thread.
231 Printf("Goroutine %d (%s) created at:\n",
232 rt->id, rt->running ? "running" : "finished");
233 PrintStack(rt->stack);
236 void PrintReport(const ReportDesc *rep) {
237 Printf("==================\n");
238 Printf("WARNING: DATA RACE\n");
239 for (uptr i = 0; i < rep->mops.Size(); i++)
240 PrintMop(rep->mops[i], i == 0);
241 for (uptr i = 0; i < rep->threads.Size(); i++)
242 PrintThread(rep->threads[i]);
243 Printf("==================\n");
248 } // namespace __tsan