blob: c1d2fd07c0d99747d6f21875f4e1df93362cac60 [file] [log] [blame]
Alexey Samsonov603c4be2012-06-04 13:55:19 +00001//===-- tsan_report.cc ----------------------------------------------------===//
Kostya Serebryany7ac41482012-05-10 13:48:04 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file is a part of ThreadSanitizer (TSan), a race detector.
11//
12//===----------------------------------------------------------------------===//
13#include "tsan_report.h"
14#include "tsan_platform.h"
15#include "tsan_rtl.h"
Stephen Hines6d186232014-11-26 17:56:19 -080016#include "sanitizer_common/sanitizer_placement_new.h"
Kostya Serebryanya27512c2013-09-05 11:23:27 +000017#include "sanitizer_common/sanitizer_report_decorator.h"
Stephen Hines6d186232014-11-26 17:56:19 -080018#include "sanitizer_common/sanitizer_stacktrace_printer.h"
Kostya Serebryany7ac41482012-05-10 13:48:04 +000019
20namespace __tsan {
21
Stephen Hines86277eb2015-03-23 12:06:32 -070022ReportStack::ReportStack() : frames(nullptr), suppressable(false) {}
Stephen Hines6d186232014-11-26 17:56:19 -080023
Stephen Hines86277eb2015-03-23 12:06:32 -070024ReportStack *ReportStack::New() {
Stephen Hines6d186232014-11-26 17:56:19 -080025 void *mem = internal_alloc(MBlockReportStack, sizeof(ReportStack));
Stephen Hines86277eb2015-03-23 12:06:32 -070026 return new(mem) ReportStack();
Stephen Hines6d186232014-11-26 17:56:19 -080027}
28
29ReportLocation::ReportLocation(ReportLocationType type)
30 : type(type), global(), heap_chunk_start(0), heap_chunk_size(0), tid(0),
31 fd(0), suppressable(false), stack(nullptr) {}
32
33ReportLocation *ReportLocation::New(ReportLocationType type) {
34 void *mem = internal_alloc(MBlockReportStack, sizeof(ReportLocation));
35 return new(mem) ReportLocation(type);
36}
37
Stephen Hines6a211c52014-07-21 00:49:56 -070038class Decorator: public __sanitizer::SanitizerCommonDecorator {
Kostya Serebryanya27512c2013-09-05 11:23:27 +000039 public:
Stephen Hines6a211c52014-07-21 00:49:56 -070040 Decorator() : SanitizerCommonDecorator() { }
Kostya Serebryanya27512c2013-09-05 11:23:27 +000041 const char *Warning() { return Red(); }
42 const char *EndWarning() { return Default(); }
43 const char *Access() { return Blue(); }
44 const char *EndAccess() { return Default(); }
45 const char *ThreadDescription() { return Cyan(); }
46 const char *EndThreadDescription() { return Default(); }
47 const char *Location() { return Green(); }
48 const char *EndLocation() { return Default(); }
49 const char *Sleep() { return Yellow(); }
50 const char *EndSleep() { return Default(); }
51 const char *Mutex() { return Magenta(); }
52 const char *EndMutex() { return Default(); }
53};
54
Kostya Serebryany7ac41482012-05-10 13:48:04 +000055ReportDesc::ReportDesc()
56 : stacks(MBlockReportStack)
57 , mops(MBlockReportMop)
58 , locs(MBlockReportLoc)
59 , mutexes(MBlockReportMutex)
Dmitry Vyukov84853112012-08-31 17:27:49 +000060 , threads(MBlockReportThread)
Stephen Hines2d1fdb22014-05-28 23:58:16 -070061 , unique_tids(MBlockReportThread)
Dmitry Vyukov4536cb12013-03-21 16:55:17 +000062 , sleep()
63 , count() {
Kostya Serebryany7ac41482012-05-10 13:48:04 +000064}
65
Dmitry Vyukovad9da372012-12-06 12:16:15 +000066ReportMop::ReportMop()
67 : mset(MBlockReportMutex) {
68}
69
Kostya Serebryany7ac41482012-05-10 13:48:04 +000070ReportDesc::~ReportDesc() {
Dmitry Vyukovaecf2e52012-12-04 15:46:05 +000071 // FIXME(dvyukov): it must be leaking a lot of memory.
Kostya Serebryany7ac41482012-05-10 13:48:04 +000072}
73
Stephen Hines86277eb2015-03-23 12:06:32 -070074#ifndef SANITIZER_GO
Dmitry Vyukovc510a2f2012-07-06 14:54:25 +000075
Dmitry Vyukovda350372012-12-17 16:28:15 +000076const int kThreadBufSize = 32;
77const char *thread_name(char *buf, int tid) {
78 if (tid == 0)
79 return "main thread";
80 internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
81 return buf;
82}
83
Kostya Serebryany2f588f92013-02-06 14:24:00 +000084static const char *ReportTypeString(ReportType typ) {
Kostya Serebryany7ac41482012-05-10 13:48:04 +000085 if (typ == ReportTypeRace)
Kostya Serebryany2f588f92013-02-06 14:24:00 +000086 return "data race";
Dmitry Vyukov0dc47b62013-03-21 15:37:39 +000087 if (typ == ReportTypeVptrRace)
88 return "data race on vptr (ctor/dtor vs virtual call)";
Kostya Serebryany2f588f92013-02-06 14:24:00 +000089 if (typ == ReportTypeUseAfterFree)
90 return "heap-use-after-free";
Stephen Hines6d186232014-11-26 17:56:19 -080091 if (typ == ReportTypeVptrUseAfterFree)
92 return "heap-use-after-free (virtual call vs free)";
Kostya Serebryany2f588f92013-02-06 14:24:00 +000093 if (typ == ReportTypeThreadLeak)
94 return "thread leak";
95 if (typ == ReportTypeMutexDestroyLocked)
96 return "destroy of a locked mutex";
Stephen Hines2d1fdb22014-05-28 23:58:16 -070097 if (typ == ReportTypeMutexDoubleLock)
98 return "double lock of a mutex";
99 if (typ == ReportTypeMutexBadUnlock)
100 return "unlock of an unlocked mutex (or by a wrong thread)";
101 if (typ == ReportTypeMutexBadReadLock)
102 return "read lock of a write locked mutex";
103 if (typ == ReportTypeMutexBadReadUnlock)
104 return "read unlock of a write locked mutex";
Kostya Serebryany2f588f92013-02-06 14:24:00 +0000105 if (typ == ReportTypeSignalUnsafe)
106 return "signal-unsafe call inside of a signal";
107 if (typ == ReportTypeErrnoInSignal)
108 return "signal handler spoils errno";
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700109 if (typ == ReportTypeDeadlock)
110 return "lock-order-inversion (potential deadlock)";
Kostya Serebryany2f588f92013-02-06 14:24:00 +0000111 return "";
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000112}
113
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800114#if SANITIZER_MAC
115static const char *const kInterposedFunctionPrefix = "wrap_";
116#else
117static const char *const kInterposedFunctionPrefix = "__interceptor_";
118#endif
119
Dmitry Vyukov1da10562012-09-01 12:13:18 +0000120void PrintStack(const ReportStack *ent) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700121 if (ent == 0 || ent->frames == 0) {
Dmitry Vyukov95f50b12012-11-22 13:38:28 +0000122 Printf(" [failed to restore the stack]\n\n");
Dmitry Vyukovdc2a27e2012-11-21 11:44:20 +0000123 return;
124 }
Stephen Hines86277eb2015-03-23 12:06:32 -0700125 SymbolizedStack *frame = ent->frames;
126 for (int i = 0; frame && frame->info.address; frame = frame->next, i++) {
Stephen Hines6d186232014-11-26 17:56:19 -0800127 InternalScopedString res(2 * GetPageSizeCached());
Stephen Hines86277eb2015-03-23 12:06:32 -0700128 RenderFrame(&res, common_flags()->stack_trace_format, i, frame->info,
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700129 common_flags()->symbolize_vs_style,
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800130 common_flags()->strip_path_prefix, kInterposedFunctionPrefix);
Stephen Hines6d186232014-11-26 17:56:19 -0800131 Printf("%s\n", res.data());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000132 }
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000133 Printf("\n");
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000134}
135
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000136static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
137 for (uptr i = 0; i < mset.Size(); i++) {
138 if (i == 0)
139 Printf(" (mutexes:");
140 const ReportMopMutex m = mset[i];
141 Printf(" %s M%llu", m.write ? "write" : "read", m.id);
142 Printf(i == mset.Size() - 1 ? ")" : ",");
143 }
144}
145
Dmitry Vyukov0a07b352013-02-01 11:10:53 +0000146static const char *MopDesc(bool first, bool write, bool atomic) {
147 return atomic ? (first ? (write ? "Atomic write" : "Atomic read")
148 : (write ? "Previous atomic write" : "Previous atomic read"))
149 : (first ? (write ? "Write" : "Read")
150 : (write ? "Previous write" : "Previous read"));
151}
152
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000153static void PrintMop(const ReportMop *mop, bool first) {
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000154 Decorator d;
Dmitry Vyukovda350372012-12-17 16:28:15 +0000155 char thrbuf[kThreadBufSize];
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000156 Printf("%s", d.Access());
Dmitry Vyukovda350372012-12-17 16:28:15 +0000157 Printf(" %s of size %d at %p by %s",
Dmitry Vyukov0a07b352013-02-01 11:10:53 +0000158 MopDesc(first, mop->write, mop->atomic),
Dmitry Vyukovda350372012-12-17 16:28:15 +0000159 mop->size, (void*)mop->addr,
160 thread_name(thrbuf, mop->tid));
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000161 PrintMutexSet(mop->mset);
162 Printf(":\n");
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000163 Printf("%s", d.EndAccess());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000164 PrintStack(mop->stack);
165}
166
167static void PrintLocation(const ReportLocation *loc) {
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000168 Decorator d;
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000169 char thrbuf[kThreadBufSize];
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000170 bool print_stack = false;
171 Printf("%s", d.Location());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000172 if (loc->type == ReportLocationGlobal) {
Stephen Hines6d186232014-11-26 17:56:19 -0800173 const DataInfo &global = loc->global;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800174 if (global.size != 0)
175 Printf(" Location is global '%s' of size %zu at %p (%s+%p)\n\n",
176 global.name, global.size, global.start,
177 StripModuleName(global.module), global.module_offset);
178 else
179 Printf(" Location is global '%s' at %p (%s+%p)\n\n", global.name,
180 global.start, StripModuleName(global.module),
181 global.module_offset);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000182 } else if (loc->type == ReportLocationHeap) {
Dmitry Vyukovda350372012-12-17 16:28:15 +0000183 char thrbuf[kThreadBufSize];
184 Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
Stephen Hines6d186232014-11-26 17:56:19 -0800185 loc->heap_chunk_size, loc->heap_chunk_start,
186 thread_name(thrbuf, loc->tid));
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000187 print_stack = true;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000188 } else if (loc->type == ReportLocationStack) {
Dmitry Vyukovfb917e92013-01-14 10:00:03 +0000189 Printf(" Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
190 } else if (loc->type == ReportLocationTLS) {
191 Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
Dmitry Vyukovc2234cd2012-12-18 06:57:34 +0000192 } else if (loc->type == ReportLocationFD) {
193 Printf(" Location is file descriptor %d created by %s at:\n",
194 loc->fd, thread_name(thrbuf, loc->tid));
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000195 print_stack = true;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000196 }
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000197 Printf("%s", d.EndLocation());
198 if (print_stack)
199 PrintStack(loc->stack);
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000200}
201
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700202static void PrintMutexShort(const ReportMutex *rm, const char *after) {
203 Decorator d;
204 Printf("%sM%zd%s%s", d.Mutex(), rm->id, d.EndMutex(), after);
205}
206
207static void PrintMutexShortWithAddress(const ReportMutex *rm,
208 const char *after) {
209 Decorator d;
210 Printf("%sM%zd (%p)%s%s", d.Mutex(), rm->id, rm->addr, d.EndMutex(), after);
211}
212
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000213static void PrintMutex(const ReportMutex *rm) {
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000214 Decorator d;
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000215 if (rm->destroyed) {
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000216 Printf("%s", d.Mutex());
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000217 Printf(" Mutex M%llu is already destroyed.\n\n", rm->id);
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000218 Printf("%s", d.EndMutex());
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000219 } else {
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000220 Printf("%s", d.Mutex());
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700221 Printf(" Mutex M%llu (%p) created at:\n", rm->id, rm->addr);
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000222 Printf("%s", d.EndMutex());
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000223 PrintStack(rm->stack);
224 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000225}
226
227static void PrintThread(const ReportThread *rt) {
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000228 Decorator d;
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000229 if (rt->id == 0) // Little sense in describing the main thread.
230 return;
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000231 Printf("%s", d.ThreadDescription());
Dmitry Vyukovad9da372012-12-06 12:16:15 +0000232 Printf(" Thread T%d", rt->id);
Alexey Samsonov2bbd8be2013-03-15 13:48:44 +0000233 if (rt->name && rt->name[0] != '\0')
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000234 Printf(" '%s'", rt->name);
Dmitry Vyukovda350372012-12-17 16:28:15 +0000235 char thrbuf[kThreadBufSize];
236 Printf(" (tid=%zu, %s) created by %s",
237 rt->pid, rt->running ? "running" : "finished",
238 thread_name(thrbuf, rt->parent_tid));
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000239 if (rt->stack)
Dmitry Vyukovda350372012-12-17 16:28:15 +0000240 Printf(" at:");
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000241 Printf("\n");
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000242 Printf("%s", d.EndThreadDescription());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000243 PrintStack(rt->stack);
244}
245
Dmitry Vyukov84853112012-08-31 17:27:49 +0000246static void PrintSleep(const ReportStack *s) {
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000247 Decorator d;
248 Printf("%s", d.Sleep());
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000249 Printf(" As if synchronized via sleep:\n");
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000250 Printf("%s", d.EndSleep());
Dmitry Vyukov84853112012-08-31 17:27:49 +0000251 PrintStack(s);
252}
253
Kostya Serebryany2f588f92013-02-06 14:24:00 +0000254static ReportStack *ChooseSummaryStack(const ReportDesc *rep) {
255 if (rep->mops.Size())
256 return rep->mops[0]->stack;
257 if (rep->stacks.Size())
258 return rep->stacks[0];
259 if (rep->mutexes.Size())
260 return rep->mutexes[0]->stack;
261 if (rep->threads.Size())
262 return rep->threads[0]->stack;
263 return 0;
264}
265
Stephen Hines86277eb2015-03-23 12:06:32 -0700266static bool FrameIsInternal(const SymbolizedStack *frame) {
267 if (frame == 0)
268 return false;
269 const char *file = frame->info.file;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800270 const char *module = frame->info.module;
271 if (file != 0 &&
272 (internal_strstr(file, "tsan_interceptors.cc") ||
273 internal_strstr(file, "sanitizer_common_interceptors.inc") ||
274 internal_strstr(file, "tsan_interface_")))
275 return true;
276 if (module != 0 && (internal_strstr(module, "libclang_rt.tsan_")))
277 return true;
278 return false;
Stephen Hines86277eb2015-03-23 12:06:32 -0700279}
280
281static SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) {
282 while (FrameIsInternal(frames) && frames->next)
283 frames = frames->next;
284 return frames;
Kostya Serebryany2f588f92013-02-06 14:24:00 +0000285}
286
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000287void PrintReport(const ReportDesc *rep) {
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000288 Decorator d;
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000289 Printf("==================\n");
Kostya Serebryany2f588f92013-02-06 14:24:00 +0000290 const char *rep_typ_str = ReportTypeString(rep->typ);
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000291 Printf("%s", d.Warning());
Peter Collingbourne0b694fc2013-05-17 16:56:53 +0000292 Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str,
293 (int)internal_getpid());
Kostya Serebryanya27512c2013-09-05 11:23:27 +0000294 Printf("%s", d.EndWarning());
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000295
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700296 if (rep->typ == ReportTypeDeadlock) {
297 char thrbuf[kThreadBufSize];
298 Printf(" Cycle in lock order graph: ");
299 for (uptr i = 0; i < rep->mutexes.Size(); i++)
300 PrintMutexShortWithAddress(rep->mutexes[i], " => ");
301 PrintMutexShort(rep->mutexes[0], "\n\n");
302 CHECK_GT(rep->mutexes.Size(), 0U);
303 CHECK_EQ(rep->mutexes.Size() * (flags()->second_deadlock_stack ? 2 : 1),
304 rep->stacks.Size());
305 for (uptr i = 0; i < rep->mutexes.Size(); i++) {
306 Printf(" Mutex ");
307 PrintMutexShort(rep->mutexes[(i + 1) % rep->mutexes.Size()],
308 " acquired here while holding mutex ");
309 PrintMutexShort(rep->mutexes[i], " in ");
310 Printf("%s", d.ThreadDescription());
311 Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i]));
312 Printf("%s", d.EndThreadDescription());
313 if (flags()->second_deadlock_stack) {
314 PrintStack(rep->stacks[2*i]);
315 Printf(" Mutex ");
316 PrintMutexShort(rep->mutexes[i],
317 " previously acquired by the same thread here:\n");
318 PrintStack(rep->stacks[2*i+1]);
319 } else {
320 PrintStack(rep->stacks[i]);
321 if (i == 0)
322 Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
323 "to get more informative warning message\n\n");
324 }
325 }
326 } else {
327 for (uptr i = 0; i < rep->stacks.Size(); i++) {
328 if (i)
329 Printf(" and:\n");
330 PrintStack(rep->stacks[i]);
331 }
Dmitry Vyukov332c62b2012-08-16 15:08:49 +0000332 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000333
334 for (uptr i = 0; i < rep->mops.Size(); i++)
335 PrintMop(rep->mops[i], i == 0);
336
Dmitry Vyukov84853112012-08-31 17:27:49 +0000337 if (rep->sleep)
338 PrintSleep(rep->sleep);
339
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000340 for (uptr i = 0; i < rep->locs.Size(); i++)
341 PrintLocation(rep->locs[i]);
342
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700343 if (rep->typ != ReportTypeDeadlock) {
344 for (uptr i = 0; i < rep->mutexes.Size(); i++)
345 PrintMutex(rep->mutexes[i]);
346 }
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000347
348 for (uptr i = 0; i < rep->threads.Size(); i++)
349 PrintThread(rep->threads[i]);
350
Dmitry Vyukov4536cb12013-03-21 16:55:17 +0000351 if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
352 Printf(" And %d more similar thread leaks.\n\n", rep->count - 1);
353
Stephen Hines86277eb2015-03-23 12:06:32 -0700354 if (ReportStack *stack = ChooseSummaryStack(rep)) {
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700355 if (SymbolizedStack *frame = SkipTsanInternalFrames(stack->frames))
356 ReportErrorSummary(rep_typ_str, frame->info);
Stephen Hines6d186232014-11-26 17:56:19 -0800357 }
Kostya Serebryany2f588f92013-02-06 14:24:00 +0000358
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000359 Printf("==================\n");
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000360}
361
Stephen Hines86277eb2015-03-23 12:06:32 -0700362#else // #ifndef SANITIZER_GO
Dmitry Vyukov9d954752013-08-16 11:15:14 +0000363
364const int kMainThreadId = 1;
Dmitry Vyukovc510a2f2012-07-06 14:54:25 +0000365
Dmitry Vyukov0ab628c2012-09-06 15:18:14 +0000366void PrintStack(const ReportStack *ent) {
Stephen Hines86277eb2015-03-23 12:06:32 -0700367 if (ent == 0 || ent->frames == 0) {
Dmitry Vyukov9d954752013-08-16 11:15:14 +0000368 Printf(" [failed to restore the stack]\n");
Dmitry Vyukov6c0fbca2012-11-28 13:30:06 +0000369 return;
370 }
Stephen Hines86277eb2015-03-23 12:06:32 -0700371 SymbolizedStack *frame = ent->frames;
372 for (int i = 0; frame; frame = frame->next, i++) {
373 const AddressInfo &info = frame->info;
374 Printf(" %s()\n %s:%d +0x%zx\n", info.function,
375 StripPathPrefix(info.file, common_flags()->strip_path_prefix),
376 info.line, (void *)info.module_offset);
Dmitry Vyukovc510a2f2012-07-06 14:54:25 +0000377 }
378}
379
380static void PrintMop(const ReportMop *mop, bool first) {
Dmitry Vyukov9d954752013-08-16 11:15:14 +0000381 Printf("\n");
382 Printf("%s by ",
Dmitry Vyukovc510a2f2012-07-06 14:54:25 +0000383 (first ? (mop->write ? "Write" : "Read")
Dmitry Vyukov9d954752013-08-16 11:15:14 +0000384 : (mop->write ? "Previous write" : "Previous read")));
385 if (mop->tid == kMainThreadId)
386 Printf("main goroutine:\n");
387 else
388 Printf("goroutine %d:\n", mop->tid);
Dmitry Vyukovc510a2f2012-07-06 14:54:25 +0000389 PrintStack(mop->stack);
390}
391
Dmitry Vyukov43046e02012-07-24 12:03:47 +0000392static void PrintThread(const ReportThread *rt) {
Dmitry Vyukov9d954752013-08-16 11:15:14 +0000393 if (rt->id == kMainThreadId)
Dmitry Vyukov43046e02012-07-24 12:03:47 +0000394 return;
Dmitry Vyukov9d954752013-08-16 11:15:14 +0000395 Printf("\n");
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000396 Printf("Goroutine %d (%s) created at:\n",
Dmitry Vyukov43046e02012-07-24 12:03:47 +0000397 rt->id, rt->running ? "running" : "finished");
398 PrintStack(rt->stack);
399}
400
Dmitry Vyukovc510a2f2012-07-06 14:54:25 +0000401void PrintReport(const ReportDesc *rep) {
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000402 Printf("==================\n");
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700403 if (rep->typ == ReportTypeRace) {
404 Printf("WARNING: DATA RACE");
405 for (uptr i = 0; i < rep->mops.Size(); i++)
406 PrintMop(rep->mops[i], i == 0);
407 for (uptr i = 0; i < rep->threads.Size(); i++)
408 PrintThread(rep->threads[i]);
409 } else if (rep->typ == ReportTypeDeadlock) {
410 Printf("WARNING: DEADLOCK\n");
411 for (uptr i = 0; i < rep->mutexes.Size(); i++) {
412 Printf("Goroutine %d lock mutex %d while holding mutex %d:\n",
413 999, rep->mutexes[i]->id,
414 rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
415 PrintStack(rep->stacks[2*i]);
416 Printf("\n");
417 Printf("Mutex %d was previously locked here:\n",
418 rep->mutexes[(i+1) % rep->mutexes.Size()]->id);
419 PrintStack(rep->stacks[2*i + 1]);
420 Printf("\n");
421 }
422 }
Alexey Samsonovb1fe3022012-11-02 12:17:51 +0000423 Printf("==================\n");
Dmitry Vyukovc510a2f2012-07-06 14:54:25 +0000424}
425
426#endif
427
Kostya Serebryany7ac41482012-05-10 13:48:04 +0000428} // namespace __tsan