13cab2bb3Spatrick //===-- tsan_report.cpp ---------------------------------------------------===//
23cab2bb3Spatrick //
33cab2bb3Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick // See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick //
73cab2bb3Spatrick //===----------------------------------------------------------------------===//
83cab2bb3Spatrick //
93cab2bb3Spatrick // This file is a part of ThreadSanitizer (TSan), a race detector.
103cab2bb3Spatrick //
113cab2bb3Spatrick //===----------------------------------------------------------------------===//
123cab2bb3Spatrick #include "tsan_report.h"
133cab2bb3Spatrick #include "tsan_platform.h"
143cab2bb3Spatrick #include "tsan_rtl.h"
153cab2bb3Spatrick #include "sanitizer_common/sanitizer_file.h"
163cab2bb3Spatrick #include "sanitizer_common/sanitizer_placement_new.h"
173cab2bb3Spatrick #include "sanitizer_common/sanitizer_report_decorator.h"
183cab2bb3Spatrick #include "sanitizer_common/sanitizer_stacktrace_printer.h"
193cab2bb3Spatrick
203cab2bb3Spatrick namespace __tsan {
213cab2bb3Spatrick
223cab2bb3Spatrick class Decorator: public __sanitizer::SanitizerCommonDecorator {
233cab2bb3Spatrick public:
Decorator()243cab2bb3Spatrick Decorator() : SanitizerCommonDecorator() { }
Access()253cab2bb3Spatrick const char *Access() { return Blue(); }
ThreadDescription()263cab2bb3Spatrick const char *ThreadDescription() { return Cyan(); }
Location()273cab2bb3Spatrick const char *Location() { return Green(); }
Sleep()283cab2bb3Spatrick const char *Sleep() { return Yellow(); }
Mutex()293cab2bb3Spatrick const char *Mutex() { return Magenta(); }
303cab2bb3Spatrick };
313cab2bb3Spatrick
ReportDesc()323cab2bb3Spatrick ReportDesc::ReportDesc()
333cab2bb3Spatrick : tag(kExternalTagNone)
343cab2bb3Spatrick , stacks()
353cab2bb3Spatrick , mops()
363cab2bb3Spatrick , locs()
373cab2bb3Spatrick , mutexes()
383cab2bb3Spatrick , threads()
393cab2bb3Spatrick , unique_tids()
403cab2bb3Spatrick , sleep()
413cab2bb3Spatrick , count() {
423cab2bb3Spatrick }
433cab2bb3Spatrick
ReportMop()443cab2bb3Spatrick ReportMop::ReportMop()
453cab2bb3Spatrick : mset() {
463cab2bb3Spatrick }
473cab2bb3Spatrick
~ReportDesc()483cab2bb3Spatrick ReportDesc::~ReportDesc() {
493cab2bb3Spatrick // FIXME(dvyukov): it must be leaking a lot of memory.
503cab2bb3Spatrick }
513cab2bb3Spatrick
523cab2bb3Spatrick #if !SANITIZER_GO
533cab2bb3Spatrick
543cab2bb3Spatrick const int kThreadBufSize = 32;
thread_name(char * buf,Tid tid)55*810390e3Srobert const char *thread_name(char *buf, Tid tid) {
56d89ec533Spatrick if (tid == kMainTid)
573cab2bb3Spatrick return "main thread";
583cab2bb3Spatrick internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
593cab2bb3Spatrick return buf;
603cab2bb3Spatrick }
613cab2bb3Spatrick
ReportTypeString(ReportType typ,uptr tag)623cab2bb3Spatrick static const char *ReportTypeString(ReportType typ, uptr tag) {
633cab2bb3Spatrick switch (typ) {
643cab2bb3Spatrick case ReportTypeRace:
653cab2bb3Spatrick return "data race";
663cab2bb3Spatrick case ReportTypeVptrRace:
673cab2bb3Spatrick return "data race on vptr (ctor/dtor vs virtual call)";
683cab2bb3Spatrick case ReportTypeUseAfterFree:
693cab2bb3Spatrick return "heap-use-after-free";
703cab2bb3Spatrick case ReportTypeVptrUseAfterFree:
713cab2bb3Spatrick return "heap-use-after-free (virtual call vs free)";
723cab2bb3Spatrick case ReportTypeExternalRace: {
733cab2bb3Spatrick const char *str = GetReportHeaderFromTag(tag);
743cab2bb3Spatrick return str ? str : "race on external object";
753cab2bb3Spatrick }
763cab2bb3Spatrick case ReportTypeThreadLeak:
773cab2bb3Spatrick return "thread leak";
783cab2bb3Spatrick case ReportTypeMutexDestroyLocked:
793cab2bb3Spatrick return "destroy of a locked mutex";
803cab2bb3Spatrick case ReportTypeMutexDoubleLock:
813cab2bb3Spatrick return "double lock of a mutex";
823cab2bb3Spatrick case ReportTypeMutexInvalidAccess:
833cab2bb3Spatrick return "use of an invalid mutex (e.g. uninitialized or destroyed)";
843cab2bb3Spatrick case ReportTypeMutexBadUnlock:
853cab2bb3Spatrick return "unlock of an unlocked mutex (or by a wrong thread)";
863cab2bb3Spatrick case ReportTypeMutexBadReadLock:
873cab2bb3Spatrick return "read lock of a write locked mutex";
883cab2bb3Spatrick case ReportTypeMutexBadReadUnlock:
893cab2bb3Spatrick return "read unlock of a write locked mutex";
903cab2bb3Spatrick case ReportTypeSignalUnsafe:
913cab2bb3Spatrick return "signal-unsafe call inside of a signal";
923cab2bb3Spatrick case ReportTypeErrnoInSignal:
933cab2bb3Spatrick return "signal handler spoils errno";
943cab2bb3Spatrick case ReportTypeDeadlock:
953cab2bb3Spatrick return "lock-order-inversion (potential deadlock)";
963cab2bb3Spatrick // No default case so compiler warns us if we miss one
973cab2bb3Spatrick }
983cab2bb3Spatrick UNREACHABLE("missing case");
993cab2bb3Spatrick }
1003cab2bb3Spatrick
101*810390e3Srobert #if SANITIZER_APPLE
1023cab2bb3Spatrick static const char *const kInterposedFunctionPrefix = "wrap_";
1033cab2bb3Spatrick #else
1043cab2bb3Spatrick static const char *const kInterposedFunctionPrefix = "__interceptor_";
1053cab2bb3Spatrick #endif
1063cab2bb3Spatrick
PrintStack(const ReportStack * ent)1073cab2bb3Spatrick void PrintStack(const ReportStack *ent) {
1083cab2bb3Spatrick if (ent == 0 || ent->frames == 0) {
1093cab2bb3Spatrick Printf(" [failed to restore the stack]\n\n");
1103cab2bb3Spatrick return;
1113cab2bb3Spatrick }
1123cab2bb3Spatrick SymbolizedStack *frame = ent->frames;
1133cab2bb3Spatrick for (int i = 0; frame && frame->info.address; frame = frame->next, i++) {
114d89ec533Spatrick InternalScopedString res;
115d89ec533Spatrick RenderFrame(&res, common_flags()->stack_trace_format, i,
116d89ec533Spatrick frame->info.address, &frame->info,
1173cab2bb3Spatrick common_flags()->symbolize_vs_style,
1183cab2bb3Spatrick common_flags()->strip_path_prefix, kInterposedFunctionPrefix);
1193cab2bb3Spatrick Printf("%s\n", res.data());
1203cab2bb3Spatrick }
1213cab2bb3Spatrick Printf("\n");
1223cab2bb3Spatrick }
1233cab2bb3Spatrick
PrintMutexSet(Vector<ReportMopMutex> const & mset)1243cab2bb3Spatrick static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
1253cab2bb3Spatrick for (uptr i = 0; i < mset.Size(); i++) {
1263cab2bb3Spatrick if (i == 0)
1273cab2bb3Spatrick Printf(" (mutexes:");
1283cab2bb3Spatrick const ReportMopMutex m = mset[i];
129*810390e3Srobert Printf(" %s M%u", m.write ? "write" : "read", m.id);
1303cab2bb3Spatrick Printf(i == mset.Size() - 1 ? ")" : ",");
1313cab2bb3Spatrick }
1323cab2bb3Spatrick }
1333cab2bb3Spatrick
MopDesc(bool first,bool write,bool atomic)1343cab2bb3Spatrick static const char *MopDesc(bool first, bool write, bool atomic) {
1353cab2bb3Spatrick return atomic ? (first ? (write ? "Atomic write" : "Atomic read")
1363cab2bb3Spatrick : (write ? "Previous atomic write" : "Previous atomic read"))
1373cab2bb3Spatrick : (first ? (write ? "Write" : "Read")
1383cab2bb3Spatrick : (write ? "Previous write" : "Previous read"));
1393cab2bb3Spatrick }
1403cab2bb3Spatrick
ExternalMopDesc(bool first,bool write)1413cab2bb3Spatrick static const char *ExternalMopDesc(bool first, bool write) {
1423cab2bb3Spatrick return first ? (write ? "Modifying" : "Read-only")
1433cab2bb3Spatrick : (write ? "Previous modifying" : "Previous read-only");
1443cab2bb3Spatrick }
1453cab2bb3Spatrick
PrintMop(const ReportMop * mop,bool first)1463cab2bb3Spatrick static void PrintMop(const ReportMop *mop, bool first) {
1473cab2bb3Spatrick Decorator d;
1483cab2bb3Spatrick char thrbuf[kThreadBufSize];
1493cab2bb3Spatrick Printf("%s", d.Access());
1503cab2bb3Spatrick if (mop->external_tag == kExternalTagNone) {
1513cab2bb3Spatrick Printf(" %s of size %d at %p by %s",
1523cab2bb3Spatrick MopDesc(first, mop->write, mop->atomic), mop->size,
1533cab2bb3Spatrick (void *)mop->addr, thread_name(thrbuf, mop->tid));
1543cab2bb3Spatrick } else {
1553cab2bb3Spatrick const char *object_type = GetObjectTypeFromTag(mop->external_tag);
1563cab2bb3Spatrick if (object_type == nullptr)
1573cab2bb3Spatrick object_type = "external object";
1583cab2bb3Spatrick Printf(" %s access of %s at %p by %s",
1593cab2bb3Spatrick ExternalMopDesc(first, mop->write), object_type,
1603cab2bb3Spatrick (void *)mop->addr, thread_name(thrbuf, mop->tid));
1613cab2bb3Spatrick }
1623cab2bb3Spatrick PrintMutexSet(mop->mset);
1633cab2bb3Spatrick Printf(":\n");
1643cab2bb3Spatrick Printf("%s", d.Default());
1653cab2bb3Spatrick PrintStack(mop->stack);
1663cab2bb3Spatrick }
1673cab2bb3Spatrick
PrintLocation(const ReportLocation * loc)1683cab2bb3Spatrick static void PrintLocation(const ReportLocation *loc) {
1693cab2bb3Spatrick Decorator d;
1703cab2bb3Spatrick char thrbuf[kThreadBufSize];
1713cab2bb3Spatrick bool print_stack = false;
1723cab2bb3Spatrick Printf("%s", d.Location());
1733cab2bb3Spatrick if (loc->type == ReportLocationGlobal) {
1743cab2bb3Spatrick const DataInfo &global = loc->global;
1753cab2bb3Spatrick if (global.size != 0)
176*810390e3Srobert Printf(" Location is global '%s' of size %zu at %p (%s+0x%zx)\n\n",
177*810390e3Srobert global.name, global.size, reinterpret_cast<void *>(global.start),
1783cab2bb3Spatrick StripModuleName(global.module), global.module_offset);
1793cab2bb3Spatrick else
180*810390e3Srobert Printf(" Location is global '%s' at %p (%s+0x%zx)\n\n", global.name,
181*810390e3Srobert reinterpret_cast<void *>(global.start),
182*810390e3Srobert StripModuleName(global.module), global.module_offset);
1833cab2bb3Spatrick } else if (loc->type == ReportLocationHeap) {
1843cab2bb3Spatrick char thrbuf[kThreadBufSize];
1853cab2bb3Spatrick const char *object_type = GetObjectTypeFromTag(loc->external_tag);
1863cab2bb3Spatrick if (!object_type) {
1873cab2bb3Spatrick Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
188*810390e3Srobert loc->heap_chunk_size,
189*810390e3Srobert reinterpret_cast<void *>(loc->heap_chunk_start),
1903cab2bb3Spatrick thread_name(thrbuf, loc->tid));
1913cab2bb3Spatrick } else {
1923cab2bb3Spatrick Printf(" Location is %s of size %zu at %p allocated by %s:\n",
193*810390e3Srobert object_type, loc->heap_chunk_size,
194*810390e3Srobert reinterpret_cast<void *>(loc->heap_chunk_start),
1953cab2bb3Spatrick thread_name(thrbuf, loc->tid));
1963cab2bb3Spatrick }
1973cab2bb3Spatrick print_stack = true;
1983cab2bb3Spatrick } else if (loc->type == ReportLocationStack) {
1993cab2bb3Spatrick Printf(" Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
2003cab2bb3Spatrick } else if (loc->type == ReportLocationTLS) {
2013cab2bb3Spatrick Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
2023cab2bb3Spatrick } else if (loc->type == ReportLocationFD) {
203*810390e3Srobert Printf(" Location is file descriptor %d %s by %s at:\n", loc->fd,
204*810390e3Srobert loc->fd_closed ? "destroyed" : "created",
205*810390e3Srobert thread_name(thrbuf, loc->tid));
2063cab2bb3Spatrick print_stack = true;
2073cab2bb3Spatrick }
2083cab2bb3Spatrick Printf("%s", d.Default());
2093cab2bb3Spatrick if (print_stack)
2103cab2bb3Spatrick PrintStack(loc->stack);
2113cab2bb3Spatrick }
2123cab2bb3Spatrick
PrintMutexShort(const ReportMutex * rm,const char * after)2133cab2bb3Spatrick static void PrintMutexShort(const ReportMutex *rm, const char *after) {
2143cab2bb3Spatrick Decorator d;
215*810390e3Srobert Printf("%sM%d%s%s", d.Mutex(), rm->id, d.Default(), after);
2163cab2bb3Spatrick }
2173cab2bb3Spatrick
PrintMutexShortWithAddress(const ReportMutex * rm,const char * after)2183cab2bb3Spatrick static void PrintMutexShortWithAddress(const ReportMutex *rm,
2193cab2bb3Spatrick const char *after) {
2203cab2bb3Spatrick Decorator d;
221*810390e3Srobert Printf("%sM%d (%p)%s%s", d.Mutex(), rm->id,
222*810390e3Srobert reinterpret_cast<void *>(rm->addr), d.Default(), after);
2233cab2bb3Spatrick }
2243cab2bb3Spatrick
PrintMutex(const ReportMutex * rm)2253cab2bb3Spatrick static void PrintMutex(const ReportMutex *rm) {
2263cab2bb3Spatrick Decorator d;
2273cab2bb3Spatrick Printf("%s", d.Mutex());
228*810390e3Srobert Printf(" Mutex M%u (%p) created at:\n", rm->id,
229*810390e3Srobert reinterpret_cast<void *>(rm->addr));
2303cab2bb3Spatrick Printf("%s", d.Default());
2313cab2bb3Spatrick PrintStack(rm->stack);
2323cab2bb3Spatrick }
2333cab2bb3Spatrick
PrintThread(const ReportThread * rt)2343cab2bb3Spatrick static void PrintThread(const ReportThread *rt) {
2353cab2bb3Spatrick Decorator d;
236d89ec533Spatrick if (rt->id == kMainTid) // Little sense in describing the main thread.
2373cab2bb3Spatrick return;
2383cab2bb3Spatrick Printf("%s", d.ThreadDescription());
2393cab2bb3Spatrick Printf(" Thread T%d", rt->id);
2403cab2bb3Spatrick if (rt->name && rt->name[0] != '\0')
2413cab2bb3Spatrick Printf(" '%s'", rt->name);
2423cab2bb3Spatrick char thrbuf[kThreadBufSize];
2433cab2bb3Spatrick const char *thread_status = rt->running ? "running" : "finished";
2443cab2bb3Spatrick if (rt->thread_type == ThreadType::Worker) {
245*810390e3Srobert Printf(" (tid=%llu, %s) is a GCD worker thread\n", rt->os_id,
246*810390e3Srobert thread_status);
2473cab2bb3Spatrick Printf("\n");
2483cab2bb3Spatrick Printf("%s", d.Default());
2493cab2bb3Spatrick return;
2503cab2bb3Spatrick }
251*810390e3Srobert Printf(" (tid=%llu, %s) created by %s", rt->os_id, thread_status,
2523cab2bb3Spatrick thread_name(thrbuf, rt->parent_tid));
2533cab2bb3Spatrick if (rt->stack)
2543cab2bb3Spatrick Printf(" at:");
2553cab2bb3Spatrick Printf("\n");
2563cab2bb3Spatrick Printf("%s", d.Default());
2573cab2bb3Spatrick PrintStack(rt->stack);
2583cab2bb3Spatrick }
2593cab2bb3Spatrick
PrintSleep(const ReportStack * s)2603cab2bb3Spatrick static void PrintSleep(const ReportStack *s) {
2613cab2bb3Spatrick Decorator d;
2623cab2bb3Spatrick Printf("%s", d.Sleep());
2633cab2bb3Spatrick Printf(" As if synchronized via sleep:\n");
2643cab2bb3Spatrick Printf("%s", d.Default());
2653cab2bb3Spatrick PrintStack(s);
2663cab2bb3Spatrick }
2673cab2bb3Spatrick
ChooseSummaryStack(const ReportDesc * rep)2683cab2bb3Spatrick static ReportStack *ChooseSummaryStack(const ReportDesc *rep) {
2693cab2bb3Spatrick if (rep->mops.Size())
2703cab2bb3Spatrick return rep->mops[0]->stack;
2713cab2bb3Spatrick if (rep->stacks.Size())
2723cab2bb3Spatrick return rep->stacks[0];
2733cab2bb3Spatrick if (rep->mutexes.Size())
2743cab2bb3Spatrick return rep->mutexes[0]->stack;
2753cab2bb3Spatrick if (rep->threads.Size())
2763cab2bb3Spatrick return rep->threads[0]->stack;
2773cab2bb3Spatrick return 0;
2783cab2bb3Spatrick }
2793cab2bb3Spatrick
FrameIsInternal(const SymbolizedStack * frame)2803cab2bb3Spatrick static bool FrameIsInternal(const SymbolizedStack *frame) {
2813cab2bb3Spatrick if (frame == 0)
2823cab2bb3Spatrick return false;
2833cab2bb3Spatrick const char *file = frame->info.file;
2843cab2bb3Spatrick const char *module = frame->info.module;
2853cab2bb3Spatrick if (file != 0 &&
2863cab2bb3Spatrick (internal_strstr(file, "tsan_interceptors_posix.cpp") ||
2873cab2bb3Spatrick internal_strstr(file, "sanitizer_common_interceptors.inc") ||
2883cab2bb3Spatrick internal_strstr(file, "tsan_interface_")))
2893cab2bb3Spatrick return true;
2903cab2bb3Spatrick if (module != 0 && (internal_strstr(module, "libclang_rt.tsan_")))
2913cab2bb3Spatrick return true;
2923cab2bb3Spatrick return false;
2933cab2bb3Spatrick }
2943cab2bb3Spatrick
SkipTsanInternalFrames(SymbolizedStack * frames)2953cab2bb3Spatrick static SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) {
2963cab2bb3Spatrick while (FrameIsInternal(frames) && frames->next)
2973cab2bb3Spatrick frames = frames->next;
2983cab2bb3Spatrick return frames;
2993cab2bb3Spatrick }
3003cab2bb3Spatrick
PrintReport(const ReportDesc * rep)3013cab2bb3Spatrick void PrintReport(const ReportDesc *rep) {
3023cab2bb3Spatrick Decorator d;
3033cab2bb3Spatrick Printf("==================\n");
3043cab2bb3Spatrick const char *rep_typ_str = ReportTypeString(rep->typ, rep->tag);
3053cab2bb3Spatrick Printf("%s", d.Warning());
3063cab2bb3Spatrick Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str,
3073cab2bb3Spatrick (int)internal_getpid());
3083cab2bb3Spatrick Printf("%s", d.Default());
3093cab2bb3Spatrick
310*810390e3Srobert if (rep->typ == ReportTypeErrnoInSignal)
311*810390e3Srobert Printf(" Signal %u handler invoked at:\n", rep->signum);
312*810390e3Srobert
3133cab2bb3Spatrick if (rep->typ == ReportTypeDeadlock) {
3143cab2bb3Spatrick char thrbuf[kThreadBufSize];
3153cab2bb3Spatrick Printf(" Cycle in lock order graph: ");
3163cab2bb3Spatrick for (uptr i = 0; i < rep->mutexes.Size(); i++)
3173cab2bb3Spatrick PrintMutexShortWithAddress(rep->mutexes[i], " => ");
3183cab2bb3Spatrick PrintMutexShort(rep->mutexes[0], "\n\n");
3193cab2bb3Spatrick CHECK_GT(rep->mutexes.Size(), 0U);
3203cab2bb3Spatrick CHECK_EQ(rep->mutexes.Size() * (flags()->second_deadlock_stack ? 2 : 1),
3213cab2bb3Spatrick rep->stacks.Size());
3223cab2bb3Spatrick for (uptr i = 0; i < rep->mutexes.Size(); i++) {
3233cab2bb3Spatrick Printf(" Mutex ");
3243cab2bb3Spatrick PrintMutexShort(rep->mutexes[(i + 1) % rep->mutexes.Size()],
3253cab2bb3Spatrick " acquired here while holding mutex ");
3263cab2bb3Spatrick PrintMutexShort(rep->mutexes[i], " in ");
3273cab2bb3Spatrick Printf("%s", d.ThreadDescription());
3283cab2bb3Spatrick Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i]));
3293cab2bb3Spatrick Printf("%s", d.Default());
3303cab2bb3Spatrick if (flags()->second_deadlock_stack) {
3313cab2bb3Spatrick PrintStack(rep->stacks[2*i]);
3323cab2bb3Spatrick Printf(" Mutex ");
3333cab2bb3Spatrick PrintMutexShort(rep->mutexes[i],
3343cab2bb3Spatrick " previously acquired by the same thread here:\n");
3353cab2bb3Spatrick PrintStack(rep->stacks[2*i+1]);
3363cab2bb3Spatrick } else {
3373cab2bb3Spatrick PrintStack(rep->stacks[i]);
3383cab2bb3Spatrick if (i == 0)
3393cab2bb3Spatrick Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
3403cab2bb3Spatrick "to get more informative warning message\n\n");
3413cab2bb3Spatrick }
3423cab2bb3Spatrick }
3433cab2bb3Spatrick } else {
3443cab2bb3Spatrick for (uptr i = 0; i < rep->stacks.Size(); i++) {
3453cab2bb3Spatrick if (i)
3463cab2bb3Spatrick Printf(" and:\n");
3473cab2bb3Spatrick PrintStack(rep->stacks[i]);
3483cab2bb3Spatrick }
3493cab2bb3Spatrick }
3503cab2bb3Spatrick
3513cab2bb3Spatrick for (uptr i = 0; i < rep->mops.Size(); i++)
3523cab2bb3Spatrick PrintMop(rep->mops[i], i == 0);
3533cab2bb3Spatrick
3543cab2bb3Spatrick if (rep->sleep)
3553cab2bb3Spatrick PrintSleep(rep->sleep);
3563cab2bb3Spatrick
3573cab2bb3Spatrick for (uptr i = 0; i < rep->locs.Size(); i++)
3583cab2bb3Spatrick PrintLocation(rep->locs[i]);
3593cab2bb3Spatrick
3603cab2bb3Spatrick if (rep->typ != ReportTypeDeadlock) {
3613cab2bb3Spatrick for (uptr i = 0; i < rep->mutexes.Size(); i++)
3623cab2bb3Spatrick PrintMutex(rep->mutexes[i]);
3633cab2bb3Spatrick }
3643cab2bb3Spatrick
3653cab2bb3Spatrick for (uptr i = 0; i < rep->threads.Size(); i++)
3663cab2bb3Spatrick PrintThread(rep->threads[i]);
3673cab2bb3Spatrick
3683cab2bb3Spatrick if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
3693cab2bb3Spatrick Printf(" And %d more similar thread leaks.\n\n", rep->count - 1);
3703cab2bb3Spatrick
3713cab2bb3Spatrick if (ReportStack *stack = ChooseSummaryStack(rep)) {
3723cab2bb3Spatrick if (SymbolizedStack *frame = SkipTsanInternalFrames(stack->frames))
3733cab2bb3Spatrick ReportErrorSummary(rep_typ_str, frame->info);
3743cab2bb3Spatrick }
3753cab2bb3Spatrick
376d89ec533Spatrick if (common_flags()->print_module_map == 2)
377d89ec533Spatrick DumpProcessMap();
3783cab2bb3Spatrick
3793cab2bb3Spatrick Printf("==================\n");
3803cab2bb3Spatrick }
3813cab2bb3Spatrick
3823cab2bb3Spatrick #else // #if !SANITIZER_GO
3833cab2bb3Spatrick
384*810390e3Srobert const Tid kMainGoroutineId = 1;
3853cab2bb3Spatrick
PrintStack(const ReportStack * ent)3863cab2bb3Spatrick void PrintStack(const ReportStack *ent) {
3873cab2bb3Spatrick if (ent == 0 || ent->frames == 0) {
3883cab2bb3Spatrick Printf(" [failed to restore the stack]\n");
3893cab2bb3Spatrick return;
3903cab2bb3Spatrick }
3913cab2bb3Spatrick SymbolizedStack *frame = ent->frames;
3923cab2bb3Spatrick for (int i = 0; frame; frame = frame->next, i++) {
3933cab2bb3Spatrick const AddressInfo &info = frame->info;
3943cab2bb3Spatrick Printf(" %s()\n %s:%d +0x%zx\n", info.function,
3953cab2bb3Spatrick StripPathPrefix(info.file, common_flags()->strip_path_prefix),
396*810390e3Srobert info.line, info.module_offset);
3973cab2bb3Spatrick }
3983cab2bb3Spatrick }
3993cab2bb3Spatrick
PrintMop(const ReportMop * mop,bool first)4003cab2bb3Spatrick static void PrintMop(const ReportMop *mop, bool first) {
4013cab2bb3Spatrick Printf("\n");
4023cab2bb3Spatrick Printf("%s at %p by ",
4033cab2bb3Spatrick (first ? (mop->write ? "Write" : "Read")
404*810390e3Srobert : (mop->write ? "Previous write" : "Previous read")),
405*810390e3Srobert reinterpret_cast<void *>(mop->addr));
406d89ec533Spatrick if (mop->tid == kMainGoroutineId)
4073cab2bb3Spatrick Printf("main goroutine:\n");
4083cab2bb3Spatrick else
4093cab2bb3Spatrick Printf("goroutine %d:\n", mop->tid);
4103cab2bb3Spatrick PrintStack(mop->stack);
4113cab2bb3Spatrick }
4123cab2bb3Spatrick
PrintLocation(const ReportLocation * loc)4133cab2bb3Spatrick static void PrintLocation(const ReportLocation *loc) {
4143cab2bb3Spatrick switch (loc->type) {
4153cab2bb3Spatrick case ReportLocationHeap: {
4163cab2bb3Spatrick Printf("\n");
417*810390e3Srobert Printf("Heap block of size %zu at %p allocated by ", loc->heap_chunk_size,
418*810390e3Srobert reinterpret_cast<void *>(loc->heap_chunk_start));
419d89ec533Spatrick if (loc->tid == kMainGoroutineId)
4203cab2bb3Spatrick Printf("main goroutine:\n");
4213cab2bb3Spatrick else
4223cab2bb3Spatrick Printf("goroutine %d:\n", loc->tid);
4233cab2bb3Spatrick PrintStack(loc->stack);
4243cab2bb3Spatrick break;
4253cab2bb3Spatrick }
4263cab2bb3Spatrick case ReportLocationGlobal: {
4273cab2bb3Spatrick Printf("\n");
4283cab2bb3Spatrick Printf("Global var %s of size %zu at %p declared at %s:%zu\n",
429*810390e3Srobert loc->global.name, loc->global.size,
430*810390e3Srobert reinterpret_cast<void *>(loc->global.start), loc->global.file,
431*810390e3Srobert loc->global.line);
4323cab2bb3Spatrick break;
4333cab2bb3Spatrick }
4343cab2bb3Spatrick default:
4353cab2bb3Spatrick break;
4363cab2bb3Spatrick }
4373cab2bb3Spatrick }
4383cab2bb3Spatrick
PrintThread(const ReportThread * rt)4393cab2bb3Spatrick static void PrintThread(const ReportThread *rt) {
440d89ec533Spatrick if (rt->id == kMainGoroutineId)
4413cab2bb3Spatrick return;
4423cab2bb3Spatrick Printf("\n");
4433cab2bb3Spatrick Printf("Goroutine %d (%s) created at:\n",
4443cab2bb3Spatrick rt->id, rt->running ? "running" : "finished");
4453cab2bb3Spatrick PrintStack(rt->stack);
4463cab2bb3Spatrick }
4473cab2bb3Spatrick
PrintReport(const ReportDesc * rep)4483cab2bb3Spatrick void PrintReport(const ReportDesc *rep) {
4493cab2bb3Spatrick Printf("==================\n");
4503cab2bb3Spatrick if (rep->typ == ReportTypeRace) {
4513cab2bb3Spatrick Printf("WARNING: DATA RACE");
4523cab2bb3Spatrick for (uptr i = 0; i < rep->mops.Size(); i++)
4533cab2bb3Spatrick PrintMop(rep->mops[i], i == 0);
4543cab2bb3Spatrick for (uptr i = 0; i < rep->locs.Size(); i++)
4553cab2bb3Spatrick PrintLocation(rep->locs[i]);
4563cab2bb3Spatrick for (uptr i = 0; i < rep->threads.Size(); i++)
4573cab2bb3Spatrick PrintThread(rep->threads[i]);
4583cab2bb3Spatrick } else if (rep->typ == ReportTypeDeadlock) {
4593cab2bb3Spatrick Printf("WARNING: DEADLOCK\n");
4603cab2bb3Spatrick for (uptr i = 0; i < rep->mutexes.Size(); i++) {
461*810390e3Srobert Printf("Goroutine %d lock mutex %u while holding mutex %u:\n", 999,
462*810390e3Srobert rep->mutexes[i]->id,
4633cab2bb3Spatrick rep->mutexes[(i + 1) % rep->mutexes.Size()]->id);
4643cab2bb3Spatrick PrintStack(rep->stacks[2*i]);
4653cab2bb3Spatrick Printf("\n");
466*810390e3Srobert Printf("Mutex %u was previously locked here:\n",
4673cab2bb3Spatrick rep->mutexes[(i + 1) % rep->mutexes.Size()]->id);
4683cab2bb3Spatrick PrintStack(rep->stacks[2*i + 1]);
4693cab2bb3Spatrick Printf("\n");
4703cab2bb3Spatrick }
4713cab2bb3Spatrick }
4723cab2bb3Spatrick Printf("==================\n");
4733cab2bb3Spatrick }
4743cab2bb3Spatrick
4753cab2bb3Spatrick #endif
4763cab2bb3Spatrick
4773cab2bb3Spatrick } // namespace __tsan
478