Home | History | Annotate | Line # | Download | only in tsan
      1  1.1  mrg //===-- tsan_report.cpp ---------------------------------------------------===//
      2  1.1  mrg //
      3  1.1  mrg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4  1.1  mrg // See https://llvm.org/LICENSE.txt for license information.
      5  1.1  mrg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6  1.1  mrg //
      7  1.1  mrg //===----------------------------------------------------------------------===//
      8  1.1  mrg //
      9  1.1  mrg // This file is a part of ThreadSanitizer (TSan), a race detector.
     10  1.1  mrg //
     11  1.1  mrg //===----------------------------------------------------------------------===//
     12  1.1  mrg #include "tsan_report.h"
     13  1.1  mrg #include "tsan_platform.h"
     14  1.1  mrg #include "tsan_rtl.h"
     15  1.1  mrg #include "sanitizer_common/sanitizer_file.h"
     16  1.1  mrg #include "sanitizer_common/sanitizer_placement_new.h"
     17  1.1  mrg #include "sanitizer_common/sanitizer_report_decorator.h"
     18  1.1  mrg #include "sanitizer_common/sanitizer_stacktrace_printer.h"
     19  1.1  mrg 
     20  1.1  mrg namespace __tsan {
     21  1.1  mrg 
     22  1.1  mrg class Decorator: public __sanitizer::SanitizerCommonDecorator {
     23  1.1  mrg  public:
     24  1.1  mrg   Decorator() : SanitizerCommonDecorator() { }
     25  1.1  mrg   const char *Access()     { return Blue(); }
     26  1.1  mrg   const char *ThreadDescription()    { return Cyan(); }
     27  1.1  mrg   const char *Location()   { return Green(); }
     28  1.1  mrg   const char *Sleep()   { return Yellow(); }
     29  1.1  mrg   const char *Mutex()   { return Magenta(); }
     30  1.1  mrg };
     31  1.1  mrg 
     32  1.1  mrg ReportDesc::ReportDesc()
     33  1.1  mrg     : tag(kExternalTagNone)
     34  1.1  mrg     , stacks()
     35  1.1  mrg     , mops()
     36  1.1  mrg     , locs()
     37  1.1  mrg     , mutexes()
     38  1.1  mrg     , threads()
     39  1.1  mrg     , unique_tids()
     40  1.1  mrg     , sleep()
     41  1.1  mrg     , count() {
     42  1.1  mrg }
     43  1.1  mrg 
     44  1.1  mrg ReportMop::ReportMop()
     45  1.1  mrg     : mset() {
     46  1.1  mrg }
     47  1.1  mrg 
     48  1.1  mrg ReportDesc::~ReportDesc() {
     49  1.1  mrg   // FIXME(dvyukov): it must be leaking a lot of memory.
     50  1.1  mrg }
     51  1.1  mrg 
     52  1.1  mrg #if !SANITIZER_GO
     53  1.1  mrg 
     54  1.1  mrg const int kThreadBufSize = 32;
     55  1.3  mrg const char *thread_name(char *buf, Tid tid) {
     56  1.3  mrg   if (tid == kMainTid)
     57  1.1  mrg     return "main thread";
     58  1.1  mrg   internal_snprintf(buf, kThreadBufSize, "thread T%d", tid);
     59  1.1  mrg   return buf;
     60  1.1  mrg }
     61  1.1  mrg 
     62  1.1  mrg static const char *ReportTypeString(ReportType typ, uptr tag) {
     63  1.1  mrg   switch (typ) {
     64  1.1  mrg     case ReportTypeRace:
     65  1.1  mrg       return "data race";
     66  1.1  mrg     case ReportTypeVptrRace:
     67  1.1  mrg       return "data race on vptr (ctor/dtor vs virtual call)";
     68  1.1  mrg     case ReportTypeUseAfterFree:
     69  1.1  mrg       return "heap-use-after-free";
     70  1.1  mrg     case ReportTypeVptrUseAfterFree:
     71  1.1  mrg       return "heap-use-after-free (virtual call vs free)";
     72  1.1  mrg     case ReportTypeExternalRace: {
     73  1.1  mrg       const char *str = GetReportHeaderFromTag(tag);
     74  1.1  mrg       return str ? str : "race on external object";
     75  1.1  mrg     }
     76  1.1  mrg     case ReportTypeThreadLeak:
     77  1.1  mrg       return "thread leak";
     78  1.1  mrg     case ReportTypeMutexDestroyLocked:
     79  1.1  mrg       return "destroy of a locked mutex";
     80  1.1  mrg     case ReportTypeMutexDoubleLock:
     81  1.1  mrg       return "double lock of a mutex";
     82  1.1  mrg     case ReportTypeMutexInvalidAccess:
     83  1.1  mrg       return "use of an invalid mutex (e.g. uninitialized or destroyed)";
     84  1.1  mrg     case ReportTypeMutexBadUnlock:
     85  1.1  mrg       return "unlock of an unlocked mutex (or by a wrong thread)";
     86  1.1  mrg     case ReportTypeMutexBadReadLock:
     87  1.1  mrg       return "read lock of a write locked mutex";
     88  1.1  mrg     case ReportTypeMutexBadReadUnlock:
     89  1.1  mrg       return "read unlock of a write locked mutex";
     90  1.1  mrg     case ReportTypeSignalUnsafe:
     91  1.1  mrg       return "signal-unsafe call inside of a signal";
     92  1.1  mrg     case ReportTypeErrnoInSignal:
     93  1.1  mrg       return "signal handler spoils errno";
     94  1.1  mrg     case ReportTypeDeadlock:
     95  1.1  mrg       return "lock-order-inversion (potential deadlock)";
     96  1.4  mrg     case ReportTypeMutexHeldWrongContext:
     97  1.4  mrg       return "mutex held in the wrong context";
     98  1.4  mrg       // No default case so compiler warns us if we miss one
     99  1.1  mrg   }
    100  1.1  mrg   UNREACHABLE("missing case");
    101  1.1  mrg }
    102  1.1  mrg 
    103  1.1  mrg void PrintStack(const ReportStack *ent) {
    104  1.1  mrg   if (ent == 0 || ent->frames == 0) {
    105  1.1  mrg     Printf("    [failed to restore the stack]\n\n");
    106  1.1  mrg     return;
    107  1.1  mrg   }
    108  1.1  mrg   SymbolizedStack *frame = ent->frames;
    109  1.1  mrg   for (int i = 0; frame && frame->info.address; frame = frame->next, i++) {
    110  1.3  mrg     InternalScopedString res;
    111  1.4  mrg     StackTracePrinter::GetOrInit()->RenderFrame(
    112  1.4  mrg         &res, common_flags()->stack_trace_format, i, frame->info.address,
    113  1.4  mrg         &frame->info, common_flags()->symbolize_vs_style,
    114  1.4  mrg         common_flags()->strip_path_prefix);
    115  1.1  mrg     Printf("%s\n", res.data());
    116  1.1  mrg   }
    117  1.1  mrg   Printf("\n");
    118  1.1  mrg }
    119  1.1  mrg 
    120  1.1  mrg static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
    121  1.1  mrg   for (uptr i = 0; i < mset.Size(); i++) {
    122  1.1  mrg     if (i == 0)
    123  1.1  mrg       Printf(" (mutexes:");
    124  1.1  mrg     const ReportMopMutex m = mset[i];
    125  1.4  mrg     Printf(" %s M%u", m.write ? "write" : "read", m.id);
    126  1.1  mrg     Printf(i == mset.Size() - 1 ? ")" : ",");
    127  1.1  mrg   }
    128  1.1  mrg }
    129  1.1  mrg 
    130  1.1  mrg static const char *MopDesc(bool first, bool write, bool atomic) {
    131  1.1  mrg   return atomic ? (first ? (write ? "Atomic write" : "Atomic read")
    132  1.1  mrg                 : (write ? "Previous atomic write" : "Previous atomic read"))
    133  1.1  mrg                 : (first ? (write ? "Write" : "Read")
    134  1.1  mrg                 : (write ? "Previous write" : "Previous read"));
    135  1.1  mrg }
    136  1.1  mrg 
    137  1.1  mrg static const char *ExternalMopDesc(bool first, bool write) {
    138  1.1  mrg   return first ? (write ? "Modifying" : "Read-only")
    139  1.1  mrg                : (write ? "Previous modifying" : "Previous read-only");
    140  1.1  mrg }
    141  1.1  mrg 
    142  1.1  mrg static void PrintMop(const ReportMop *mop, bool first) {
    143  1.1  mrg   Decorator d;
    144  1.1  mrg   char thrbuf[kThreadBufSize];
    145  1.1  mrg   Printf("%s", d.Access());
    146  1.1  mrg   if (mop->external_tag == kExternalTagNone) {
    147  1.1  mrg     Printf("  %s of size %d at %p by %s",
    148  1.1  mrg            MopDesc(first, mop->write, mop->atomic), mop->size,
    149  1.1  mrg            (void *)mop->addr, thread_name(thrbuf, mop->tid));
    150  1.1  mrg   } else {
    151  1.1  mrg     const char *object_type = GetObjectTypeFromTag(mop->external_tag);
    152  1.1  mrg     if (object_type == nullptr)
    153  1.1  mrg         object_type = "external object";
    154  1.1  mrg     Printf("  %s access of %s at %p by %s",
    155  1.1  mrg            ExternalMopDesc(first, mop->write), object_type,
    156  1.1  mrg            (void *)mop->addr, thread_name(thrbuf, mop->tid));
    157  1.1  mrg   }
    158  1.1  mrg   PrintMutexSet(mop->mset);
    159  1.1  mrg   Printf(":\n");
    160  1.1  mrg   Printf("%s", d.Default());
    161  1.1  mrg   PrintStack(mop->stack);
    162  1.1  mrg }
    163  1.1  mrg 
    164  1.1  mrg static void PrintLocation(const ReportLocation *loc) {
    165  1.1  mrg   Decorator d;
    166  1.1  mrg   char thrbuf[kThreadBufSize];
    167  1.1  mrg   bool print_stack = false;
    168  1.1  mrg   Printf("%s", d.Location());
    169  1.1  mrg   if (loc->type == ReportLocationGlobal) {
    170  1.1  mrg     const DataInfo &global = loc->global;
    171  1.1  mrg     if (global.size != 0)
    172  1.3  mrg       Printf("  Location is global '%s' of size %zu at %p (%s+0x%zx)\n\n",
    173  1.3  mrg              global.name, global.size, reinterpret_cast<void *>(global.start),
    174  1.1  mrg              StripModuleName(global.module), global.module_offset);
    175  1.1  mrg     else
    176  1.3  mrg       Printf("  Location is global '%s' at %p (%s+0x%zx)\n\n", global.name,
    177  1.3  mrg              reinterpret_cast<void *>(global.start),
    178  1.3  mrg              StripModuleName(global.module), global.module_offset);
    179  1.1  mrg   } else if (loc->type == ReportLocationHeap) {
    180  1.1  mrg     char thrbuf[kThreadBufSize];
    181  1.1  mrg     const char *object_type = GetObjectTypeFromTag(loc->external_tag);
    182  1.1  mrg     if (!object_type) {
    183  1.1  mrg       Printf("  Location is heap block of size %zu at %p allocated by %s:\n",
    184  1.3  mrg              loc->heap_chunk_size,
    185  1.3  mrg              reinterpret_cast<void *>(loc->heap_chunk_start),
    186  1.1  mrg              thread_name(thrbuf, loc->tid));
    187  1.1  mrg     } else {
    188  1.1  mrg       Printf("  Location is %s of size %zu at %p allocated by %s:\n",
    189  1.3  mrg              object_type, loc->heap_chunk_size,
    190  1.3  mrg              reinterpret_cast<void *>(loc->heap_chunk_start),
    191  1.1  mrg              thread_name(thrbuf, loc->tid));
    192  1.1  mrg     }
    193  1.1  mrg     print_stack = true;
    194  1.1  mrg   } else if (loc->type == ReportLocationStack) {
    195  1.1  mrg     Printf("  Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
    196  1.1  mrg   } else if (loc->type == ReportLocationTLS) {
    197  1.1  mrg     Printf("  Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
    198  1.1  mrg   } else if (loc->type == ReportLocationFD) {
    199  1.4  mrg     Printf("  Location is file descriptor %d %s by %s at:\n", loc->fd,
    200  1.4  mrg            loc->fd_closed ? "destroyed" : "created",
    201  1.4  mrg            thread_name(thrbuf, loc->tid));
    202  1.1  mrg     print_stack = true;
    203  1.1  mrg   }
    204  1.1  mrg   Printf("%s", d.Default());
    205  1.1  mrg   if (print_stack)
    206  1.1  mrg     PrintStack(loc->stack);
    207  1.1  mrg }
    208  1.1  mrg 
    209  1.1  mrg static void PrintMutexShort(const ReportMutex *rm, const char *after) {
    210  1.1  mrg   Decorator d;
    211  1.4  mrg   Printf("%sM%d%s%s", d.Mutex(), rm->id, d.Default(), after);
    212  1.1  mrg }
    213  1.1  mrg 
    214  1.1  mrg static void PrintMutexShortWithAddress(const ReportMutex *rm,
    215  1.1  mrg                                        const char *after) {
    216  1.1  mrg   Decorator d;
    217  1.4  mrg   Printf("%sM%d (%p)%s%s", d.Mutex(), rm->id,
    218  1.3  mrg          reinterpret_cast<void *>(rm->addr), d.Default(), after);
    219  1.1  mrg }
    220  1.1  mrg 
    221  1.1  mrg static void PrintMutex(const ReportMutex *rm) {
    222  1.1  mrg   Decorator d;
    223  1.4  mrg   Printf("%s", d.Mutex());
    224  1.4  mrg   Printf("  Mutex M%u (%p) created at:\n", rm->id,
    225  1.4  mrg          reinterpret_cast<void *>(rm->addr));
    226  1.4  mrg   Printf("%s", d.Default());
    227  1.4  mrg   PrintStack(rm->stack);
    228  1.1  mrg }
    229  1.1  mrg 
    230  1.1  mrg static void PrintThread(const ReportThread *rt) {
    231  1.1  mrg   Decorator d;
    232  1.3  mrg   if (rt->id == kMainTid)  // Little sense in describing the main thread.
    233  1.1  mrg     return;
    234  1.1  mrg   Printf("%s", d.ThreadDescription());
    235  1.1  mrg   Printf("  Thread T%d", rt->id);
    236  1.1  mrg   if (rt->name && rt->name[0] != '\0')
    237  1.1  mrg     Printf(" '%s'", rt->name);
    238  1.1  mrg   char thrbuf[kThreadBufSize];
    239  1.1  mrg   const char *thread_status = rt->running ? "running" : "finished";
    240  1.1  mrg   if (rt->thread_type == ThreadType::Worker) {
    241  1.3  mrg     Printf(" (tid=%llu, %s) is a GCD worker thread\n", rt->os_id,
    242  1.3  mrg            thread_status);
    243  1.1  mrg     Printf("\n");
    244  1.1  mrg     Printf("%s", d.Default());
    245  1.1  mrg     return;
    246  1.1  mrg   }
    247  1.3  mrg   Printf(" (tid=%llu, %s) created by %s", rt->os_id, thread_status,
    248  1.1  mrg          thread_name(thrbuf, rt->parent_tid));
    249  1.1  mrg   if (rt->stack)
    250  1.1  mrg     Printf(" at:");
    251  1.1  mrg   Printf("\n");
    252  1.1  mrg   Printf("%s", d.Default());
    253  1.1  mrg   PrintStack(rt->stack);
    254  1.1  mrg }
    255  1.1  mrg 
    256  1.1  mrg static void PrintSleep(const ReportStack *s) {
    257  1.1  mrg   Decorator d;
    258  1.1  mrg   Printf("%s", d.Sleep());
    259  1.1  mrg   Printf("  As if synchronized via sleep:\n");
    260  1.1  mrg   Printf("%s", d.Default());
    261  1.1  mrg   PrintStack(s);
    262  1.1  mrg }
    263  1.1  mrg 
    264  1.1  mrg static ReportStack *ChooseSummaryStack(const ReportDesc *rep) {
    265  1.1  mrg   if (rep->mops.Size())
    266  1.1  mrg     return rep->mops[0]->stack;
    267  1.1  mrg   if (rep->stacks.Size())
    268  1.1  mrg     return rep->stacks[0];
    269  1.1  mrg   if (rep->mutexes.Size())
    270  1.1  mrg     return rep->mutexes[0]->stack;
    271  1.1  mrg   if (rep->threads.Size())
    272  1.1  mrg     return rep->threads[0]->stack;
    273  1.1  mrg   return 0;
    274  1.1  mrg }
    275  1.1  mrg 
    276  1.1  mrg static bool FrameIsInternal(const SymbolizedStack *frame) {
    277  1.1  mrg   if (frame == 0)
    278  1.1  mrg     return false;
    279  1.1  mrg   const char *file = frame->info.file;
    280  1.1  mrg   const char *module = frame->info.module;
    281  1.1  mrg   if (file != 0 &&
    282  1.1  mrg       (internal_strstr(file, "tsan_interceptors_posix.cpp") ||
    283  1.4  mrg        internal_strstr(file, "tsan_interceptors_memintrinsics.cpp") ||
    284  1.1  mrg        internal_strstr(file, "sanitizer_common_interceptors.inc") ||
    285  1.1  mrg        internal_strstr(file, "tsan_interface_")))
    286  1.1  mrg     return true;
    287  1.1  mrg   if (module != 0 && (internal_strstr(module, "libclang_rt.tsan_")))
    288  1.1  mrg     return true;
    289  1.1  mrg   return false;
    290  1.1  mrg }
    291  1.1  mrg 
    292  1.1  mrg static SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) {
    293  1.1  mrg   while (FrameIsInternal(frames) && frames->next)
    294  1.1  mrg     frames = frames->next;
    295  1.1  mrg   return frames;
    296  1.1  mrg }
    297  1.1  mrg 
    298  1.1  mrg void PrintReport(const ReportDesc *rep) {
    299  1.1  mrg   Decorator d;
    300  1.1  mrg   Printf("==================\n");
    301  1.1  mrg   const char *rep_typ_str = ReportTypeString(rep->typ, rep->tag);
    302  1.1  mrg   Printf("%s", d.Warning());
    303  1.1  mrg   Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str,
    304  1.1  mrg          (int)internal_getpid());
    305  1.1  mrg   Printf("%s", d.Default());
    306  1.1  mrg 
    307  1.4  mrg   if (rep->typ == ReportTypeErrnoInSignal)
    308  1.4  mrg     Printf("  Signal %u handler invoked at:\n", rep->signum);
    309  1.4  mrg 
    310  1.1  mrg   if (rep->typ == ReportTypeDeadlock) {
    311  1.1  mrg     char thrbuf[kThreadBufSize];
    312  1.1  mrg     Printf("  Cycle in lock order graph: ");
    313  1.1  mrg     for (uptr i = 0; i < rep->mutexes.Size(); i++)
    314  1.1  mrg       PrintMutexShortWithAddress(rep->mutexes[i], " => ");
    315  1.1  mrg     PrintMutexShort(rep->mutexes[0], "\n\n");
    316  1.1  mrg     CHECK_GT(rep->mutexes.Size(), 0U);
    317  1.1  mrg     CHECK_EQ(rep->mutexes.Size() * (flags()->second_deadlock_stack ? 2 : 1),
    318  1.1  mrg              rep->stacks.Size());
    319  1.1  mrg     for (uptr i = 0; i < rep->mutexes.Size(); i++) {
    320  1.1  mrg       Printf("  Mutex ");
    321  1.1  mrg       PrintMutexShort(rep->mutexes[(i + 1) % rep->mutexes.Size()],
    322  1.1  mrg                       " acquired here while holding mutex ");
    323  1.1  mrg       PrintMutexShort(rep->mutexes[i], " in ");
    324  1.1  mrg       Printf("%s", d.ThreadDescription());
    325  1.1  mrg       Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i]));
    326  1.1  mrg       Printf("%s", d.Default());
    327  1.1  mrg       if (flags()->second_deadlock_stack) {
    328  1.1  mrg         PrintStack(rep->stacks[2*i]);
    329  1.1  mrg         Printf("  Mutex ");
    330  1.1  mrg         PrintMutexShort(rep->mutexes[i],
    331  1.1  mrg                         " previously acquired by the same thread here:\n");
    332  1.1  mrg         PrintStack(rep->stacks[2*i+1]);
    333  1.1  mrg       } else {
    334  1.1  mrg         PrintStack(rep->stacks[i]);
    335  1.1  mrg         if (i == 0)
    336  1.1  mrg           Printf("    Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
    337  1.1  mrg                  "to get more informative warning message\n\n");
    338  1.1  mrg       }
    339  1.1  mrg     }
    340  1.1  mrg   } else {
    341  1.1  mrg     for (uptr i = 0; i < rep->stacks.Size(); i++) {
    342  1.1  mrg       if (i)
    343  1.1  mrg         Printf("  and:\n");
    344  1.1  mrg       PrintStack(rep->stacks[i]);
    345  1.1  mrg     }
    346  1.1  mrg   }
    347  1.1  mrg 
    348  1.1  mrg   for (uptr i = 0; i < rep->mops.Size(); i++)
    349  1.1  mrg     PrintMop(rep->mops[i], i == 0);
    350  1.1  mrg 
    351  1.1  mrg   if (rep->sleep)
    352  1.1  mrg     PrintSleep(rep->sleep);
    353  1.1  mrg 
    354  1.1  mrg   for (uptr i = 0; i < rep->locs.Size(); i++)
    355  1.1  mrg     PrintLocation(rep->locs[i]);
    356  1.1  mrg 
    357  1.1  mrg   if (rep->typ != ReportTypeDeadlock) {
    358  1.1  mrg     for (uptr i = 0; i < rep->mutexes.Size(); i++)
    359  1.1  mrg       PrintMutex(rep->mutexes[i]);
    360  1.1  mrg   }
    361  1.1  mrg 
    362  1.1  mrg   for (uptr i = 0; i < rep->threads.Size(); i++)
    363  1.1  mrg     PrintThread(rep->threads[i]);
    364  1.1  mrg 
    365  1.1  mrg   if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
    366  1.1  mrg     Printf("  And %d more similar thread leaks.\n\n", rep->count - 1);
    367  1.1  mrg 
    368  1.1  mrg   if (ReportStack *stack = ChooseSummaryStack(rep)) {
    369  1.1  mrg     if (SymbolizedStack *frame = SkipTsanInternalFrames(stack->frames))
    370  1.1  mrg       ReportErrorSummary(rep_typ_str, frame->info);
    371  1.1  mrg   }
    372  1.1  mrg 
    373  1.3  mrg   if (common_flags()->print_module_map == 2)
    374  1.3  mrg     DumpProcessMap();
    375  1.1  mrg 
    376  1.1  mrg   Printf("==================\n");
    377  1.1  mrg }
    378  1.1  mrg 
    379  1.1  mrg #else  // #if !SANITIZER_GO
    380  1.1  mrg 
    381  1.3  mrg const Tid kMainGoroutineId = 1;
    382  1.1  mrg 
    383  1.1  mrg void PrintStack(const ReportStack *ent) {
    384  1.1  mrg   if (ent == 0 || ent->frames == 0) {
    385  1.1  mrg     Printf("  [failed to restore the stack]\n");
    386  1.1  mrg     return;
    387  1.1  mrg   }
    388  1.1  mrg   SymbolizedStack *frame = ent->frames;
    389  1.1  mrg   for (int i = 0; frame; frame = frame->next, i++) {
    390  1.1  mrg     const AddressInfo &info = frame->info;
    391  1.1  mrg     Printf("  %s()\n      %s:%d +0x%zx\n", info.function,
    392  1.3  mrg            StripPathPrefix(info.file, common_flags()->strip_path_prefix),
    393  1.3  mrg            info.line, info.module_offset);
    394  1.1  mrg   }
    395  1.1  mrg }
    396  1.1  mrg 
    397  1.1  mrg static void PrintMop(const ReportMop *mop, bool first) {
    398  1.1  mrg   Printf("\n");
    399  1.1  mrg   Printf("%s at %p by ",
    400  1.3  mrg          (first ? (mop->write ? "Write" : "Read")
    401  1.3  mrg                 : (mop->write ? "Previous write" : "Previous read")),
    402  1.3  mrg          reinterpret_cast<void *>(mop->addr));
    403  1.3  mrg   if (mop->tid == kMainGoroutineId)
    404  1.1  mrg     Printf("main goroutine:\n");
    405  1.1  mrg   else
    406  1.1  mrg     Printf("goroutine %d:\n", mop->tid);
    407  1.1  mrg   PrintStack(mop->stack);
    408  1.1  mrg }
    409  1.1  mrg 
    410  1.1  mrg static void PrintLocation(const ReportLocation *loc) {
    411  1.1  mrg   switch (loc->type) {
    412  1.1  mrg   case ReportLocationHeap: {
    413  1.1  mrg     Printf("\n");
    414  1.3  mrg     Printf("Heap block of size %zu at %p allocated by ", loc->heap_chunk_size,
    415  1.3  mrg            reinterpret_cast<void *>(loc->heap_chunk_start));
    416  1.3  mrg     if (loc->tid == kMainGoroutineId)
    417  1.1  mrg       Printf("main goroutine:\n");
    418  1.1  mrg     else
    419  1.1  mrg       Printf("goroutine %d:\n", loc->tid);
    420  1.1  mrg     PrintStack(loc->stack);
    421  1.1  mrg     break;
    422  1.1  mrg   }
    423  1.1  mrg   case ReportLocationGlobal: {
    424  1.1  mrg     Printf("\n");
    425  1.1  mrg     Printf("Global var %s of size %zu at %p declared at %s:%zu\n",
    426  1.3  mrg            loc->global.name, loc->global.size,
    427  1.3  mrg            reinterpret_cast<void *>(loc->global.start), loc->global.file,
    428  1.3  mrg            loc->global.line);
    429  1.1  mrg     break;
    430  1.1  mrg   }
    431  1.1  mrg   default:
    432  1.1  mrg     break;
    433  1.1  mrg   }
    434  1.1  mrg }
    435  1.1  mrg 
    436  1.1  mrg static void PrintThread(const ReportThread *rt) {
    437  1.3  mrg   if (rt->id == kMainGoroutineId)
    438  1.1  mrg     return;
    439  1.1  mrg   Printf("\n");
    440  1.1  mrg   Printf("Goroutine %d (%s) created at:\n",
    441  1.1  mrg     rt->id, rt->running ? "running" : "finished");
    442  1.1  mrg   PrintStack(rt->stack);
    443  1.1  mrg }
    444  1.1  mrg 
    445  1.1  mrg void PrintReport(const ReportDesc *rep) {
    446  1.1  mrg   Printf("==================\n");
    447  1.1  mrg   if (rep->typ == ReportTypeRace) {
    448  1.1  mrg     Printf("WARNING: DATA RACE");
    449  1.1  mrg     for (uptr i = 0; i < rep->mops.Size(); i++)
    450  1.1  mrg       PrintMop(rep->mops[i], i == 0);
    451  1.1  mrg     for (uptr i = 0; i < rep->locs.Size(); i++)
    452  1.1  mrg       PrintLocation(rep->locs[i]);
    453  1.1  mrg     for (uptr i = 0; i < rep->threads.Size(); i++)
    454  1.1  mrg       PrintThread(rep->threads[i]);
    455  1.1  mrg   } else if (rep->typ == ReportTypeDeadlock) {
    456  1.1  mrg     Printf("WARNING: DEADLOCK\n");
    457  1.1  mrg     for (uptr i = 0; i < rep->mutexes.Size(); i++) {
    458  1.4  mrg       Printf("Goroutine %d lock mutex %u while holding mutex %u:\n", 999,
    459  1.3  mrg              rep->mutexes[i]->id,
    460  1.3  mrg              rep->mutexes[(i + 1) % rep->mutexes.Size()]->id);
    461  1.1  mrg       PrintStack(rep->stacks[2*i]);
    462  1.1  mrg       Printf("\n");
    463  1.4  mrg       Printf("Mutex %u was previously locked here:\n",
    464  1.3  mrg              rep->mutexes[(i + 1) % rep->mutexes.Size()]->id);
    465  1.1  mrg       PrintStack(rep->stacks[2*i + 1]);
    466  1.1  mrg       Printf("\n");
    467  1.1  mrg     }
    468  1.1  mrg   }
    469  1.1  mrg   Printf("==================\n");
    470  1.1  mrg }
    471  1.1  mrg 
    472  1.1  mrg #endif
    473  1.1  mrg 
    474  1.1  mrg }  // namespace __tsan
    475