2013-02-04 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libsanitizer / tsan / tsan_report.cc
blobf99fd2ea1053279dcccaa31b5d7114288467b226
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"
15 namespace __tsan {
17 ReportDesc::ReportDesc()
18 : stacks(MBlockReportStack)
19 , mops(MBlockReportMop)
20 , locs(MBlockReportLoc)
21 , mutexes(MBlockReportMutex)
22 , threads(MBlockReportThread)
23 , sleep() {
26 ReportMop::ReportMop()
27 : mset(MBlockReportMutex) {
30 ReportDesc::~ReportDesc() {
31 // FIXME(dvyukov): it must be leaking a lot of memory.
34 #ifndef TSAN_GO
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;
44 static void PrintHeader(ReportType typ) {
45 Printf("WARNING: ThreadSanitizer: ");
47 if (typ == ReportTypeRace)
48 Printf("data race");
49 else if (typ == ReportTypeUseAfterFree)
50 Printf("heap-use-after-free");
51 else if (typ == ReportTypeThreadLeak)
52 Printf("thread leak");
53 else if (typ == ReportTypeMutexDestroyLocked)
54 Printf("destroy of a locked mutex");
55 else if (typ == ReportTypeSignalUnsafe)
56 Printf("signal-unsafe call inside of a signal");
57 else if (typ == ReportTypeErrnoInSignal)
58 Printf("signal handler spoils errno");
60 Printf(" (pid=%d)\n", GetPid());
63 void PrintStack(const ReportStack *ent) {
64 if (ent == 0) {
65 Printf(" [failed to restore the stack]\n\n");
66 return;
68 for (int i = 0; ent; ent = ent->next, i++) {
69 Printf(" #%d %s %s:%d", i, ent->func, ent->file, ent->line);
70 if (ent->col)
71 Printf(":%d", ent->col);
72 if (ent->module && ent->offset)
73 Printf(" (%s+%p)\n", ent->module, (void*)ent->offset);
74 else
75 Printf(" (%p)\n", (void*)ent->pc);
77 Printf("\n");
80 static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
81 for (uptr i = 0; i < mset.Size(); i++) {
82 if (i == 0)
83 Printf(" (mutexes:");
84 const ReportMopMutex m = mset[i];
85 Printf(" %s M%llu", m.write ? "write" : "read", m.id);
86 Printf(i == mset.Size() - 1 ? ")" : ",");
90 static void PrintMop(const ReportMop *mop, bool first) {
91 char thrbuf[kThreadBufSize];
92 Printf(" %s of size %d at %p by %s",
93 (first ? (mop->write ? "Write" : "Read")
94 : (mop->write ? "Previous write" : "Previous read")),
95 mop->size, (void*)mop->addr,
96 thread_name(thrbuf, mop->tid));
97 PrintMutexSet(mop->mset);
98 Printf(":\n");
99 PrintStack(mop->stack);
102 static void PrintLocation(const ReportLocation *loc) {
103 char thrbuf[kThreadBufSize];
104 if (loc->type == ReportLocationGlobal) {
105 Printf(" Location is global '%s' of size %zu at %zx (%s+%p)\n\n",
106 loc->name, loc->size, loc->addr, loc->module, loc->offset);
107 } else if (loc->type == ReportLocationHeap) {
108 char thrbuf[kThreadBufSize];
109 Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
110 loc->size, loc->addr, thread_name(thrbuf, loc->tid));
111 PrintStack(loc->stack);
112 } else if (loc->type == ReportLocationStack) {
113 Printf(" Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
114 } else if (loc->type == ReportLocationTLS) {
115 Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
116 } else if (loc->type == ReportLocationFD) {
117 Printf(" Location is file descriptor %d created by %s at:\n",
118 loc->fd, thread_name(thrbuf, loc->tid));
119 PrintStack(loc->stack);
123 static void PrintMutex(const ReportMutex *rm) {
124 if (rm->destroyed) {
125 Printf(" Mutex M%llu is already destroyed.\n\n", rm->id);
126 } else {
127 Printf(" Mutex M%llu created at:\n", rm->id);
128 PrintStack(rm->stack);
132 static void PrintThread(const ReportThread *rt) {
133 if (rt->id == 0) // Little sense in describing the main thread.
134 return;
135 Printf(" Thread T%d", rt->id);
136 if (rt->name)
137 Printf(" '%s'", rt->name);
138 char thrbuf[kThreadBufSize];
139 Printf(" (tid=%zu, %s) created by %s",
140 rt->pid, rt->running ? "running" : "finished",
141 thread_name(thrbuf, rt->parent_tid));
142 if (rt->stack)
143 Printf(" at:");
144 Printf("\n");
145 PrintStack(rt->stack);
148 static void PrintSleep(const ReportStack *s) {
149 Printf(" As if synchronized via sleep:\n");
150 PrintStack(s);
153 void PrintReport(const ReportDesc *rep) {
154 Printf("==================\n");
155 PrintHeader(rep->typ);
157 for (uptr i = 0; i < rep->stacks.Size(); i++) {
158 if (i)
159 Printf(" and:\n");
160 PrintStack(rep->stacks[i]);
163 for (uptr i = 0; i < rep->mops.Size(); i++)
164 PrintMop(rep->mops[i], i == 0);
166 if (rep->sleep)
167 PrintSleep(rep->sleep);
169 for (uptr i = 0; i < rep->locs.Size(); i++)
170 PrintLocation(rep->locs[i]);
172 for (uptr i = 0; i < rep->mutexes.Size(); i++)
173 PrintMutex(rep->mutexes[i]);
175 for (uptr i = 0; i < rep->threads.Size(); i++)
176 PrintThread(rep->threads[i]);
178 Printf("==================\n");
181 #else
183 void PrintStack(const ReportStack *ent) {
184 if (ent == 0) {
185 Printf(" [failed to restore the stack]\n\n");
186 return;
188 for (int i = 0; ent; ent = ent->next, i++) {
189 Printf(" %s()\n %s:%d +0x%zx\n",
190 ent->func, ent->file, ent->line, (void*)ent->offset);
192 Printf("\n");
195 static void PrintMop(const ReportMop *mop, bool first) {
196 Printf("%s by goroutine %d:\n",
197 (first ? (mop->write ? "Write" : "Read")
198 : (mop->write ? "Previous write" : "Previous read")),
199 mop->tid);
200 PrintStack(mop->stack);
203 static void PrintThread(const ReportThread *rt) {
204 if (rt->id == 0) // Little sense in describing the main thread.
205 return;
206 Printf("Goroutine %d (%s) created at:\n",
207 rt->id, rt->running ? "running" : "finished");
208 PrintStack(rt->stack);
211 void PrintReport(const ReportDesc *rep) {
212 Printf("==================\n");
213 Printf("WARNING: DATA RACE\n");
214 for (uptr i = 0; i < rep->mops.Size(); i++)
215 PrintMop(rep->mops[i], i == 0);
216 for (uptr i = 0; i < rep->threads.Size(); i++)
217 PrintThread(rep->threads[i]);
218 Printf("==================\n");
221 #endif
223 } // namespace __tsan