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"
14 #include "sanitizer_common/sanitizer_report_decorator.h"
18 class Decorator
: public __sanitizer::SanitizerCommonDecorator
{
20 Decorator() : SanitizerCommonDecorator() { }
21 const char *Warning() { return Red(); }
22 const char *EndWarning() { return Default(); }
23 const char *Access() { return Blue(); }
24 const char *EndAccess() { return Default(); }
25 const char *ThreadDescription() { return Cyan(); }
26 const char *EndThreadDescription() { return Default(); }
27 const char *Location() { return Green(); }
28 const char *EndLocation() { return Default(); }
29 const char *Sleep() { return Yellow(); }
30 const char *EndSleep() { return Default(); }
31 const char *Mutex() { return Magenta(); }
32 const char *EndMutex() { return Default(); }
35 ReportDesc::ReportDesc()
36 : stacks(MBlockReportStack
)
37 , mops(MBlockReportMop
)
38 , locs(MBlockReportLoc
)
39 , mutexes(MBlockReportMutex
)
40 , threads(MBlockReportThread
)
41 , unique_tids(MBlockReportThread
)
46 ReportMop::ReportMop()
47 : mset(MBlockReportMutex
) {
50 ReportDesc::~ReportDesc() {
51 // FIXME(dvyukov): it must be leaking a lot of memory.
56 const int kThreadBufSize
= 32;
57 const char *thread_name(char *buf
, int tid
) {
60 internal_snprintf(buf
, kThreadBufSize
, "thread T%d", tid
);
64 static const char *ReportTypeString(ReportType typ
) {
65 if (typ
== ReportTypeRace
)
67 if (typ
== ReportTypeVptrRace
)
68 return "data race on vptr (ctor/dtor vs virtual call)";
69 if (typ
== ReportTypeUseAfterFree
)
70 return "heap-use-after-free";
71 if (typ
== ReportTypeThreadLeak
)
73 if (typ
== ReportTypeMutexDestroyLocked
)
74 return "destroy of a locked mutex";
75 if (typ
== ReportTypeMutexDoubleLock
)
76 return "double lock of a mutex";
77 if (typ
== ReportTypeMutexBadUnlock
)
78 return "unlock of an unlocked mutex (or by a wrong thread)";
79 if (typ
== ReportTypeMutexBadReadLock
)
80 return "read lock of a write locked mutex";
81 if (typ
== ReportTypeMutexBadReadUnlock
)
82 return "read unlock of a write locked mutex";
83 if (typ
== ReportTypeSignalUnsafe
)
84 return "signal-unsafe call inside of a signal";
85 if (typ
== ReportTypeErrnoInSignal
)
86 return "signal handler spoils errno";
87 if (typ
== ReportTypeDeadlock
)
88 return "lock-order-inversion (potential deadlock)";
92 void PrintStack(const ReportStack
*ent
) {
94 Printf(" [failed to restore the stack]\n\n");
97 for (int i
= 0; ent
; ent
= ent
->next
, i
++) {
98 Printf(" #%d %s %s:%d", i
, ent
->func
, ent
->file
, ent
->line
);
100 Printf(":%d", ent
->col
);
101 if (ent
->module
&& ent
->offset
)
102 Printf(" (%s+%p)\n", ent
->module
, (void*)ent
->offset
);
104 Printf(" (%p)\n", (void*)ent
->pc
);
109 static void PrintMutexSet(Vector
<ReportMopMutex
> const& mset
) {
110 for (uptr i
= 0; i
< mset
.Size(); i
++) {
112 Printf(" (mutexes:");
113 const ReportMopMutex m
= mset
[i
];
114 Printf(" %s M%llu", m
.write
? "write" : "read", m
.id
);
115 Printf(i
== mset
.Size() - 1 ? ")" : ",");
119 static const char *MopDesc(bool first
, bool write
, bool atomic
) {
120 return atomic
? (first
? (write
? "Atomic write" : "Atomic read")
121 : (write
? "Previous atomic write" : "Previous atomic read"))
122 : (first
? (write
? "Write" : "Read")
123 : (write
? "Previous write" : "Previous read"));
126 static void PrintMop(const ReportMop
*mop
, bool first
) {
128 char thrbuf
[kThreadBufSize
];
129 Printf("%s", d
.Access());
130 Printf(" %s of size %d at %p by %s",
131 MopDesc(first
, mop
->write
, mop
->atomic
),
132 mop
->size
, (void*)mop
->addr
,
133 thread_name(thrbuf
, mop
->tid
));
134 PrintMutexSet(mop
->mset
);
136 Printf("%s", d
.EndAccess());
137 PrintStack(mop
->stack
);
140 static void PrintLocation(const ReportLocation
*loc
) {
142 char thrbuf
[kThreadBufSize
];
143 bool print_stack
= false;
144 Printf("%s", d
.Location());
145 if (loc
->type
== ReportLocationGlobal
) {
146 Printf(" Location is global '%s' of size %zu at %p (%s+%p)\n\n",
147 loc
->name
, loc
->size
, loc
->addr
, loc
->module
, loc
->offset
);
148 } else if (loc
->type
== ReportLocationHeap
) {
149 char thrbuf
[kThreadBufSize
];
150 Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
151 loc
->size
, loc
->addr
, thread_name(thrbuf
, loc
->tid
));
153 } else if (loc
->type
== ReportLocationStack
) {
154 Printf(" Location is stack of %s.\n\n", thread_name(thrbuf
, loc
->tid
));
155 } else if (loc
->type
== ReportLocationTLS
) {
156 Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf
, loc
->tid
));
157 } else if (loc
->type
== ReportLocationFD
) {
158 Printf(" Location is file descriptor %d created by %s at:\n",
159 loc
->fd
, thread_name(thrbuf
, loc
->tid
));
162 Printf("%s", d
.EndLocation());
164 PrintStack(loc
->stack
);
167 static void PrintMutexShort(const ReportMutex
*rm
, const char *after
) {
169 Printf("%sM%zd%s%s", d
.Mutex(), rm
->id
, d
.EndMutex(), after
);
172 static void PrintMutexShortWithAddress(const ReportMutex
*rm
,
175 Printf("%sM%zd (%p)%s%s", d
.Mutex(), rm
->id
, rm
->addr
, d
.EndMutex(), after
);
178 static void PrintMutex(const ReportMutex
*rm
) {
181 Printf("%s", d
.Mutex());
182 Printf(" Mutex M%llu is already destroyed.\n\n", rm
->id
);
183 Printf("%s", d
.EndMutex());
185 Printf("%s", d
.Mutex());
186 Printf(" Mutex M%llu (%p) created at:\n", rm
->id
, rm
->addr
);
187 Printf("%s", d
.EndMutex());
188 PrintStack(rm
->stack
);
192 static void PrintThread(const ReportThread
*rt
) {
194 if (rt
->id
== 0) // Little sense in describing the main thread.
196 Printf("%s", d
.ThreadDescription());
197 Printf(" Thread T%d", rt
->id
);
198 if (rt
->name
&& rt
->name
[0] != '\0')
199 Printf(" '%s'", rt
->name
);
200 char thrbuf
[kThreadBufSize
];
201 Printf(" (tid=%zu, %s) created by %s",
202 rt
->pid
, rt
->running
? "running" : "finished",
203 thread_name(thrbuf
, rt
->parent_tid
));
207 Printf("%s", d
.EndThreadDescription());
208 PrintStack(rt
->stack
);
211 static void PrintSleep(const ReportStack
*s
) {
213 Printf("%s", d
.Sleep());
214 Printf(" As if synchronized via sleep:\n");
215 Printf("%s", d
.EndSleep());
219 static ReportStack
*ChooseSummaryStack(const ReportDesc
*rep
) {
220 if (rep
->mops
.Size())
221 return rep
->mops
[0]->stack
;
222 if (rep
->stacks
.Size())
223 return rep
->stacks
[0];
224 if (rep
->mutexes
.Size())
225 return rep
->mutexes
[0]->stack
;
226 if (rep
->threads
.Size())
227 return rep
->threads
[0]->stack
;
231 ReportStack
*SkipTsanInternalFrames(ReportStack
*ent
) {
232 while (FrameIsInternal(ent
) && ent
->next
)
237 void PrintReport(const ReportDesc
*rep
) {
239 Printf("==================\n");
240 const char *rep_typ_str
= ReportTypeString(rep
->typ
);
241 Printf("%s", d
.Warning());
242 Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str
,
243 (int)internal_getpid());
244 Printf("%s", d
.EndWarning());
246 if (rep
->typ
== ReportTypeDeadlock
) {
247 char thrbuf
[kThreadBufSize
];
248 Printf(" Cycle in lock order graph: ");
249 for (uptr i
= 0; i
< rep
->mutexes
.Size(); i
++)
250 PrintMutexShortWithAddress(rep
->mutexes
[i
], " => ");
251 PrintMutexShort(rep
->mutexes
[0], "\n\n");
252 CHECK_GT(rep
->mutexes
.Size(), 0U);
253 CHECK_EQ(rep
->mutexes
.Size() * (flags()->second_deadlock_stack
? 2 : 1),
255 for (uptr i
= 0; i
< rep
->mutexes
.Size(); i
++) {
257 PrintMutexShort(rep
->mutexes
[(i
+ 1) % rep
->mutexes
.Size()],
258 " acquired here while holding mutex ");
259 PrintMutexShort(rep
->mutexes
[i
], " in ");
260 Printf("%s", d
.ThreadDescription());
261 Printf("%s:\n", thread_name(thrbuf
, rep
->unique_tids
[i
]));
262 Printf("%s", d
.EndThreadDescription());
263 if (flags()->second_deadlock_stack
) {
264 PrintStack(rep
->stacks
[2*i
]);
266 PrintMutexShort(rep
->mutexes
[i
],
267 " previously acquired by the same thread here:\n");
268 PrintStack(rep
->stacks
[2*i
+1]);
270 PrintStack(rep
->stacks
[i
]);
272 Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
273 "to get more informative warning message\n\n");
277 for (uptr i
= 0; i
< rep
->stacks
.Size(); i
++) {
280 PrintStack(rep
->stacks
[i
]);
284 for (uptr i
= 0; i
< rep
->mops
.Size(); i
++)
285 PrintMop(rep
->mops
[i
], i
== 0);
288 PrintSleep(rep
->sleep
);
290 for (uptr i
= 0; i
< rep
->locs
.Size(); i
++)
291 PrintLocation(rep
->locs
[i
]);
293 if (rep
->typ
!= ReportTypeDeadlock
) {
294 for (uptr i
= 0; i
< rep
->mutexes
.Size(); i
++)
295 PrintMutex(rep
->mutexes
[i
]);
298 for (uptr i
= 0; i
< rep
->threads
.Size(); i
++)
299 PrintThread(rep
->threads
[i
]);
301 if (rep
->typ
== ReportTypeThreadLeak
&& rep
->count
> 1)
302 Printf(" And %d more similar thread leaks.\n\n", rep
->count
- 1);
304 if (ReportStack
*ent
= SkipTsanInternalFrames(ChooseSummaryStack(rep
)))
305 ReportErrorSummary(rep_typ_str
, ent
->file
, ent
->line
, ent
->func
);
307 Printf("==================\n");
310 #else // #ifndef TSAN_GO
312 const int kMainThreadId
= 1;
314 void PrintStack(const ReportStack
*ent
) {
316 Printf(" [failed to restore the stack]\n");
319 for (int i
= 0; ent
; ent
= ent
->next
, i
++) {
320 Printf(" %s()\n %s:%d +0x%zx\n",
321 ent
->func
, ent
->file
, ent
->line
, (void*)ent
->offset
);
325 static void PrintMop(const ReportMop
*mop
, bool first
) {
328 (first
? (mop
->write
? "Write" : "Read")
329 : (mop
->write
? "Previous write" : "Previous read")));
330 if (mop
->tid
== kMainThreadId
)
331 Printf("main goroutine:\n");
333 Printf("goroutine %d:\n", mop
->tid
);
334 PrintStack(mop
->stack
);
337 static void PrintThread(const ReportThread
*rt
) {
338 if (rt
->id
== kMainThreadId
)
341 Printf("Goroutine %d (%s) created at:\n",
342 rt
->id
, rt
->running
? "running" : "finished");
343 PrintStack(rt
->stack
);
346 void PrintReport(const ReportDesc
*rep
) {
347 Printf("==================\n");
348 if (rep
->typ
== ReportTypeRace
) {
349 Printf("WARNING: DATA RACE");
350 for (uptr i
= 0; i
< rep
->mops
.Size(); i
++)
351 PrintMop(rep
->mops
[i
], i
== 0);
352 for (uptr i
= 0; i
< rep
->threads
.Size(); i
++)
353 PrintThread(rep
->threads
[i
]);
354 } else if (rep
->typ
== ReportTypeDeadlock
) {
355 Printf("WARNING: DEADLOCK\n");
356 for (uptr i
= 0; i
< rep
->mutexes
.Size(); i
++) {
357 Printf("Goroutine %d lock mutex %d while holding mutex %d:\n",
358 999, rep
->mutexes
[i
]->id
,
359 rep
->mutexes
[(i
+1) % rep
->mutexes
.Size()]->id
);
360 PrintStack(rep
->stacks
[2*i
]);
362 Printf("Mutex %d was previously locked here:\n",
363 rep
->mutexes
[(i
+1) % rep
->mutexes
.Size()]->id
);
364 PrintStack(rep
->stacks
[2*i
+ 1]);
368 Printf("==================\n");
373 } // namespace __tsan