Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / libsanitizer / tsan / tsan_report.cc
1 //===-- tsan_report.cc ----------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
9 //
10 //===----------------------------------------------------------------------===//
11 #include "tsan_report.h"
12 #include "tsan_platform.h"
13 #include "tsan_rtl.h"
14
15 namespace __tsan {
16
17 ReportDesc::ReportDesc()
18     : stacks(MBlockReportStack)
19     , mops(MBlockReportMop)
20     , locs(MBlockReportLoc)
21     , mutexes(MBlockReportMutex)
22     , threads(MBlockReportThread)
23     , sleep() {
24 }
25
26 ReportMop::ReportMop()
27     : mset(MBlockReportMutex) {
28 }
29
30 ReportDesc::~ReportDesc() {
31   // FIXME(dvyukov): it must be leaking a lot of memory.
32 }
33
34 #ifndef TSAN_GO
35
36 const int kThreadBufSize = 32;
37 const char *thread_name(char *buf, int tid) {
38   if (tid == 0)
39     return "main thread";
40   internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
41   return buf;
42 }
43
44 static const char *ReportTypeString(ReportType typ) {
45   if (typ == ReportTypeRace)
46     return "data race";
47   if (typ == ReportTypeUseAfterFree)
48     return "heap-use-after-free";
49   if (typ == ReportTypeThreadLeak)
50     return "thread leak";
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";
57   return "";
58 }
59
60 void PrintStack(const ReportStack *ent) {
61   if (ent == 0) {
62     Printf("    [failed to restore the stack]\n\n");
63     return;
64   }
65   for (int i = 0; ent; ent = ent->next, i++) {
66     Printf("    #%d %s %s:%d", i, ent->func, ent->file, ent->line);
67     if (ent->col)
68       Printf(":%d", ent->col);
69     if (ent->module && ent->offset)
70       Printf(" (%s+%p)\n", ent->module, (void*)ent->offset);
71     else
72       Printf(" (%p)\n", (void*)ent->pc);
73   }
74   Printf("\n");
75 }
76
77 static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
78   for (uptr i = 0; i < mset.Size(); i++) {
79     if (i == 0)
80       Printf(" (mutexes:");
81     const ReportMopMutex m = mset[i];
82     Printf(" %s M%llu", m.write ? "write" : "read", m.id);
83     Printf(i == mset.Size() - 1 ? ")" : ",");
84   }
85 }
86
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"));
92 }
93
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);
101   Printf(":\n");
102   PrintStack(mop->stack);
103 }
104
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);
123   }
124 }
125
126 static void PrintMutex(const ReportMutex *rm) {
127   if (rm->destroyed) {
128     Printf("  Mutex M%llu is already destroyed.\n\n", rm->id);
129   } else {
130     Printf("  Mutex M%llu created at:\n", rm->id);
131     PrintStack(rm->stack);
132   }
133 }
134
135 static void PrintThread(const ReportThread *rt) {
136   if (rt->id == 0)  // Little sense in describing the main thread.
137     return;
138   Printf("  Thread T%d", rt->id);
139   if (rt->name)
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));
145   if (rt->stack)
146     Printf(" at:");
147   Printf("\n");
148   PrintStack(rt->stack);
149 }
150
151 static void PrintSleep(const ReportStack *s) {
152   Printf("  As if synchronized via sleep:\n");
153   PrintStack(s);
154 }
155
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;
165   return 0;
166 }
167
168 ReportStack *SkipTsanInternalFrames(ReportStack *ent) {
169   while (FrameIsInternal(ent) && ent->next)
170     ent = ent->next;
171   return ent;
172 }
173
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());
178
179   for (uptr i = 0; i < rep->stacks.Size(); i++) {
180     if (i)
181       Printf("  and:\n");
182     PrintStack(rep->stacks[i]);
183   }
184
185   for (uptr i = 0; i < rep->mops.Size(); i++)
186     PrintMop(rep->mops[i], i == 0);
187
188   if (rep->sleep)
189     PrintSleep(rep->sleep);
190
191   for (uptr i = 0; i < rep->locs.Size(); i++)
192     PrintLocation(rep->locs[i]);
193
194   for (uptr i = 0; i < rep->mutexes.Size(); i++)
195     PrintMutex(rep->mutexes[i]);
196
197   for (uptr i = 0; i < rep->threads.Size(); i++)
198     PrintThread(rep->threads[i]);
199
200   if (ReportStack *ent = SkipTsanInternalFrames(ChooseSummaryStack(rep)))
201     ReportErrorSummary(rep_typ_str, ent->file, ent->line, ent->func);
202
203   Printf("==================\n");
204 }
205
206 #else
207
208 void PrintStack(const ReportStack *ent) {
209   if (ent == 0) {
210     Printf("  [failed to restore the stack]\n\n");
211     return;
212   }
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);
216   }
217   Printf("\n");
218 }
219
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")),
224       mop->tid);
225   PrintStack(mop->stack);
226 }
227
228 static void PrintThread(const ReportThread *rt) {
229   if (rt->id == 0)  // Little sense in describing the main thread.
230     return;
231   Printf("Goroutine %d (%s) created at:\n",
232     rt->id, rt->running ? "running" : "finished");
233   PrintStack(rt->stack);
234 }
235
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");
244 }
245
246 #endif
247
248 }  // namespace __tsan