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.1  mrg const char *thread_name(char *buf, Tid tid) {
     56  1.1  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.1  mrg     // No default case so compiler warns us if we miss one
     97  1.1  mrg   }
     98  1.1  mrg   UNREACHABLE("missing case");
     99  1.1  mrg }
    100  1.1  mrg 
    101  1.1  mrg #if SANITIZER_MAC
    102  1.1  mrg static const char *const kInterposedFunctionPrefix = "wrap_";
    103  1.1  mrg #else
    104  1.1  mrg static const char *const kInterposedFunctionPrefix = "__interceptor_";
    105  1.1  mrg #endif
    106  1.1  mrg 
    107  1.1  mrg void PrintStack(const ReportStack *ent) {
    108  1.1  mrg   if (ent == 0 || ent->frames == 0) {
    109  1.1  mrg     Printf("    [failed to restore the stack]\n\n");
    110  1.1  mrg     return;
    111  1.1  mrg   }
    112  1.1  mrg   SymbolizedStack *frame = ent->frames;
    113  1.1  mrg   for (int i = 0; frame && frame->info.address; frame = frame->next, i++) {
    114  1.1  mrg     InternalScopedString res;
    115  1.1  mrg     RenderFrame(&res, common_flags()->stack_trace_format, i,
    116  1.1  mrg                 frame->info.address, &frame->info,
    117  1.1  mrg                 common_flags()->symbolize_vs_style,
    118  1.1  mrg                 common_flags()->strip_path_prefix, kInterposedFunctionPrefix);
    119  1.1  mrg     Printf("%s\n", res.data());
    120  1.1  mrg   }
    121  1.1  mrg   Printf("\n");
    122  1.1  mrg }
    123  1.1  mrg 
    124  1.1  mrg static void PrintMutexSet(Vector<ReportMopMutex> const& mset) {
    125  1.1  mrg   for (uptr i = 0; i < mset.Size(); i++) {
    126  1.1  mrg     if (i == 0)
    127  1.1  mrg       Printf(" (mutexes:");
    128  1.1  mrg     const ReportMopMutex m = mset[i];
    129  1.1  mrg     Printf(" %s M%llu", m.write ? "write" : "read", m.id);
    130  1.1  mrg     Printf(i == mset.Size() - 1 ? ")" : ",");
    131  1.1  mrg   }
    132  1.1  mrg }
    133  1.1  mrg 
    134  1.1  mrg static const char *MopDesc(bool first, bool write, bool atomic) {
    135  1.1  mrg   return atomic ? (first ? (write ? "Atomic write" : "Atomic read")
    136  1.1  mrg                 : (write ? "Previous atomic write" : "Previous atomic read"))
    137  1.1  mrg                 : (first ? (write ? "Write" : "Read")
    138  1.1  mrg                 : (write ? "Previous write" : "Previous read"));
    139  1.1  mrg }
    140  1.1  mrg 
    141  1.1  mrg static const char *ExternalMopDesc(bool first, bool write) {
    142  1.1  mrg   return first ? (write ? "Modifying" : "Read-only")
    143  1.1  mrg                : (write ? "Previous modifying" : "Previous read-only");
    144  1.1  mrg }
    145  1.1  mrg 
    146  1.1  mrg static void PrintMop(const ReportMop *mop, bool first) {
    147  1.1  mrg   Decorator d;
    148  1.1  mrg   char thrbuf[kThreadBufSize];
    149  1.1  mrg   Printf("%s", d.Access());
    150  1.1  mrg   if (mop->external_tag == kExternalTagNone) {
    151  1.1  mrg     Printf("  %s of size %d at %p by %s",
    152  1.1  mrg            MopDesc(first, mop->write, mop->atomic), mop->size,
    153  1.1  mrg            (void *)mop->addr, thread_name(thrbuf, mop->tid));
    154  1.1  mrg   } else {
    155  1.1  mrg     const char *object_type = GetObjectTypeFromTag(mop->external_tag);
    156  1.1  mrg     if (object_type == nullptr)
    157  1.1  mrg         object_type = "external object";
    158  1.1  mrg     Printf("  %s access of %s at %p by %s",
    159  1.1  mrg            ExternalMopDesc(first, mop->write), object_type,
    160  1.1  mrg            (void *)mop->addr, thread_name(thrbuf, mop->tid));
    161  1.1  mrg   }
    162  1.1  mrg   PrintMutexSet(mop->mset);
    163  1.1  mrg   Printf(":\n");
    164  1.1  mrg   Printf("%s", d.Default());
    165  1.1  mrg   PrintStack(mop->stack);
    166  1.1  mrg }
    167  1.1  mrg 
    168  1.1  mrg static void PrintLocation(const ReportLocation *loc) {
    169  1.1  mrg   Decorator d;
    170  1.1  mrg   char thrbuf[kThreadBufSize];
    171  1.1  mrg   bool print_stack = false;
    172  1.1  mrg   Printf("%s", d.Location());
    173  1.1  mrg   if (loc->type == ReportLocationGlobal) {
    174  1.1  mrg     const DataInfo &global = loc->global;
    175  1.1  mrg     if (global.size != 0)
    176  1.1  mrg       Printf("  Location is global '%s' of size %zu at %p (%s+0x%zx)\n\n",
    177  1.1  mrg              global.name, global.size, reinterpret_cast<void *>(global.start),
    178  1.1  mrg              StripModuleName(global.module), global.module_offset);
    179  1.1  mrg     else
    180  1.1  mrg       Printf("  Location is global '%s' at %p (%s+0x%zx)\n\n", global.name,
    181  1.1  mrg              reinterpret_cast<void *>(global.start),
    182  1.1  mrg              StripModuleName(global.module), global.module_offset);
    183  1.1  mrg   } else if (loc->type == ReportLocationHeap) {
    184  1.1  mrg     char thrbuf[kThreadBufSize];
    185  1.1  mrg     const char *object_type = GetObjectTypeFromTag(loc->external_tag);
    186  1.1  mrg     if (!object_type) {
    187  1.1  mrg       Printf("  Location is heap block of size %zu at %p allocated by %s:\n",
    188  1.1  mrg              loc->heap_chunk_size,
    189  1.1  mrg              reinterpret_cast<void *>(loc->heap_chunk_start),
    190  1.1  mrg              thread_name(thrbuf, loc->tid));
    191  1.1  mrg     } else {
    192  1.1  mrg       Printf("  Location is %s of size %zu at %p allocated by %s:\n",
    193  1.1  mrg              object_type, loc->heap_chunk_size,
    194  1.1  mrg              reinterpret_cast<void *>(loc->heap_chunk_start),
    195  1.1  mrg              thread_name(thrbuf, loc->tid));
    196  1.1  mrg     }
    197  1.1  mrg     print_stack = true;
    198  1.1  mrg   } else if (loc->type == ReportLocationStack) {
    199  1.1  mrg     Printf("  Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid));
    200  1.1  mrg   } else if (loc->type == ReportLocationTLS) {
    201  1.1  mrg     Printf("  Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid));
    202  1.1  mrg   } else if (loc->type == ReportLocationFD) {
    203  1.1  mrg     Printf("  Location is file descriptor %d created by %s at:\n",
    204  1.1  mrg         loc->fd, thread_name(thrbuf, loc->tid));
    205  1.1  mrg     print_stack = true;
    206  1.1  mrg   }
    207  1.1  mrg   Printf("%s", d.Default());
    208  1.1  mrg   if (print_stack)
    209  1.1  mrg     PrintStack(loc->stack);
    210  1.1  mrg }
    211  1.1  mrg 
    212  1.1  mrg static void PrintMutexShort(const ReportMutex *rm, const char *after) {
    213  1.1  mrg   Decorator d;
    214  1.1  mrg   Printf("%sM%lld%s%s", d.Mutex(), rm->id, d.Default(), after);
    215  1.1  mrg }
    216  1.1  mrg 
    217  1.1  mrg static void PrintMutexShortWithAddress(const ReportMutex *rm,
    218  1.1  mrg                                        const char *after) {
    219  1.1  mrg   Decorator d;
    220  1.1  mrg   Printf("%sM%lld (%p)%s%s", d.Mutex(), rm->id,
    221  1.1  mrg          reinterpret_cast<void *>(rm->addr), d.Default(), after);
    222  1.1  mrg }
    223  1.1  mrg 
    224  1.1  mrg static void PrintMutex(const ReportMutex *rm) {
    225  1.1  mrg   Decorator d;
    226  1.1  mrg   if (rm->destroyed) {
    227  1.1  mrg     Printf("%s", d.Mutex());
    228  1.1  mrg     Printf("  Mutex M%llu is already destroyed.\n\n", rm->id);
    229  1.1  mrg     Printf("%s", d.Default());
    230  1.1  mrg   } else {
    231  1.1  mrg     Printf("%s", d.Mutex());
    232  1.1  mrg     Printf("  Mutex M%llu (%p) created at:\n", rm->id,
    233  1.1  mrg            reinterpret_cast<void *>(rm->addr));
    234  1.1  mrg     Printf("%s", d.Default());
    235  1.1  mrg     PrintStack(rm->stack);
    236  1.1  mrg   }
    237  1.1  mrg }
    238  1.1  mrg 
    239  1.1  mrg static void PrintThread(const ReportThread *rt) {
    240  1.1  mrg   Decorator d;
    241  1.1  mrg   if (rt->id == kMainTid)  // Little sense in describing the main thread.
    242  1.1  mrg     return;
    243  1.1  mrg   Printf("%s", d.ThreadDescription());
    244  1.1  mrg   Printf("  Thread T%d", rt->id);
    245  1.1  mrg   if (rt->name && rt->name[0] != '\0')
    246  1.1  mrg     Printf(" '%s'", rt->name);
    247  1.1  mrg   char thrbuf[kThreadBufSize];
    248  1.1  mrg   const char *thread_status = rt->running ? "running" : "finished";
    249  1.1  mrg   if (rt->thread_type == ThreadType::Worker) {
    250  1.1  mrg     Printf(" (tid=%llu, %s) is a GCD worker thread\n", rt->os_id,
    251  1.1  mrg            thread_status);
    252  1.1  mrg     Printf("\n");
    253  1.1  mrg     Printf("%s", d.Default());
    254  1.1  mrg     return;
    255  1.1  mrg   }
    256  1.1  mrg   Printf(" (tid=%llu, %s) created by %s", rt->os_id, thread_status,
    257  1.1  mrg          thread_name(thrbuf, rt->parent_tid));
    258  1.1  mrg   if (rt->stack)
    259  1.1  mrg     Printf(" at:");
    260  1.1  mrg   Printf("\n");
    261  1.1  mrg   Printf("%s", d.Default());
    262  1.1  mrg   PrintStack(rt->stack);
    263  1.1  mrg }
    264  1.1  mrg 
    265  1.1  mrg static void PrintSleep(const ReportStack *s) {
    266  1.1  mrg   Decorator d;
    267  1.1  mrg   Printf("%s", d.Sleep());
    268  1.1  mrg   Printf("  As if synchronized via sleep:\n");
    269  1.1  mrg   Printf("%s", d.Default());
    270  1.1  mrg   PrintStack(s);
    271  1.1  mrg }
    272  1.1  mrg 
    273  1.1  mrg static ReportStack *ChooseSummaryStack(const ReportDesc *rep) {
    274  1.1  mrg   if (rep->mops.Size())
    275  1.1  mrg     return rep->mops[0]->stack;
    276  1.1  mrg   if (rep->stacks.Size())
    277  1.1  mrg     return rep->stacks[0];
    278  1.1  mrg   if (rep->mutexes.Size())
    279  1.1  mrg     return rep->mutexes[0]->stack;
    280  1.1  mrg   if (rep->threads.Size())
    281  1.1  mrg     return rep->threads[0]->stack;
    282  1.1  mrg   return 0;
    283  1.1  mrg }
    284  1.1  mrg 
    285  1.1  mrg static bool FrameIsInternal(const SymbolizedStack *frame) {
    286  1.1  mrg   if (frame == 0)
    287  1.1  mrg     return false;
    288  1.1  mrg   const char *file = frame->info.file;
    289  1.1  mrg   const char *module = frame->info.module;
    290  1.1  mrg   if (file != 0 &&
    291  1.1  mrg       (internal_strstr(file, "tsan_interceptors_posix.cpp") ||
    292  1.1  mrg        internal_strstr(file, "sanitizer_common_interceptors.inc") ||
    293  1.1  mrg        internal_strstr(file, "tsan_interface_")))
    294  1.1  mrg     return true;
    295  1.1  mrg   if (module != 0 && (internal_strstr(module, "libclang_rt.tsan_")))
    296  1.1  mrg     return true;
    297  1.1  mrg   return false;
    298  1.1  mrg }
    299  1.1  mrg 
    300  1.1  mrg static SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) {
    301  1.1  mrg   while (FrameIsInternal(frames) && frames->next)
    302  1.1  mrg     frames = frames->next;
    303  1.1  mrg   return frames;
    304  1.1  mrg }
    305  1.1  mrg 
    306  1.1  mrg void PrintReport(const ReportDesc *rep) {
    307  1.1  mrg   Decorator d;
    308  1.1  mrg   Printf("==================\n");
    309  1.1  mrg   const char *rep_typ_str = ReportTypeString(rep->typ, rep->tag);
    310  1.1  mrg   Printf("%s", d.Warning());
    311  1.1  mrg   Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str,
    312  1.1  mrg          (int)internal_getpid());
    313  1.1  mrg   Printf("%s", d.Default());
    314  1.1  mrg 
    315  1.1  mrg   if (rep->typ == ReportTypeDeadlock) {
    316  1.1  mrg     char thrbuf[kThreadBufSize];
    317  1.1  mrg     Printf("  Cycle in lock order graph: ");
    318  1.1  mrg     for (uptr i = 0; i < rep->mutexes.Size(); i++)
    319  1.1  mrg       PrintMutexShortWithAddress(rep->mutexes[i], " => ");
    320  1.1  mrg     PrintMutexShort(rep->mutexes[0], "\n\n");
    321  1.1  mrg     CHECK_GT(rep->mutexes.Size(), 0U);
    322  1.1  mrg     CHECK_EQ(rep->mutexes.Size() * (flags()->second_deadlock_stack ? 2 : 1),
    323  1.1  mrg              rep->stacks.Size());
    324  1.1  mrg     for (uptr i = 0; i < rep->mutexes.Size(); i++) {
    325  1.1  mrg       Printf("  Mutex ");
    326  1.1  mrg       PrintMutexShort(rep->mutexes[(i + 1) % rep->mutexes.Size()],
    327  1.1  mrg                       " acquired here while holding mutex ");
    328  1.1  mrg       PrintMutexShort(rep->mutexes[i], " in ");
    329  1.1  mrg       Printf("%s", d.ThreadDescription());
    330  1.1  mrg       Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i]));
    331  1.1  mrg       Printf("%s", d.Default());
    332  1.1  mrg       if (flags()->second_deadlock_stack) {
    333  1.1  mrg         PrintStack(rep->stacks[2*i]);
    334  1.1  mrg         Printf("  Mutex ");
    335  1.1  mrg         PrintMutexShort(rep->mutexes[i],
    336  1.1  mrg                         " previously acquired by the same thread here:\n");
    337  1.1  mrg         PrintStack(rep->stacks[2*i+1]);
    338  1.1  mrg       } else {
    339  1.1  mrg         PrintStack(rep->stacks[i]);
    340  1.1  mrg         if (i == 0)
    341  1.1  mrg           Printf("    Hint: use TSAN_OPTIONS=second_deadlock_stack=1 "
    342  1.1  mrg                  "to get more informative warning message\n\n");
    343  1.1  mrg       }
    344  1.1  mrg     }
    345  1.1  mrg   } else {
    346  1.1  mrg     for (uptr i = 0; i < rep->stacks.Size(); i++) {
    347  1.1  mrg       if (i)
    348  1.1  mrg         Printf("  and:\n");
    349  1.1  mrg       PrintStack(rep->stacks[i]);
    350  1.1  mrg     }
    351  1.1  mrg   }
    352  1.1  mrg 
    353  1.1  mrg   for (uptr i = 0; i < rep->mops.Size(); i++)
    354  1.1  mrg     PrintMop(rep->mops[i], i == 0);
    355  1.1  mrg 
    356  1.1  mrg   if (rep->sleep)
    357  1.1  mrg     PrintSleep(rep->sleep);
    358  1.1  mrg 
    359  1.1  mrg   for (uptr i = 0; i < rep->locs.Size(); i++)
    360  1.1  mrg     PrintLocation(rep->locs[i]);
    361  1.1  mrg 
    362  1.1  mrg   if (rep->typ != ReportTypeDeadlock) {
    363  1.1  mrg     for (uptr i = 0; i < rep->mutexes.Size(); i++)
    364  1.1  mrg       PrintMutex(rep->mutexes[i]);
    365  1.1  mrg   }
    366  1.1  mrg 
    367  1.1  mrg   for (uptr i = 0; i < rep->threads.Size(); i++)
    368  1.1  mrg     PrintThread(rep->threads[i]);
    369  1.1  mrg 
    370  1.1  mrg   if (rep->typ == ReportTypeThreadLeak && rep->count > 1)
    371  1.1  mrg     Printf("  And %d more similar thread leaks.\n\n", rep->count - 1);
    372  1.1  mrg 
    373  1.1  mrg   if (ReportStack *stack = ChooseSummaryStack(rep)) {
    374  1.1  mrg     if (SymbolizedStack *frame = SkipTsanInternalFrames(stack->frames))
    375  1.1  mrg       ReportErrorSummary(rep_typ_str, frame->info);
    376  1.1  mrg   }
    377  1.1  mrg 
    378  1.1  mrg   if (common_flags()->print_module_map == 2)
    379  1.1  mrg     DumpProcessMap();
    380  1.1  mrg 
    381  1.1  mrg   Printf("==================\n");
    382  1.1  mrg }
    383  1.1  mrg 
    384  1.1  mrg #else  // #if !SANITIZER_GO
    385  1.1  mrg 
    386  1.1  mrg const Tid kMainGoroutineId = 1;
    387  1.1  mrg 
    388  1.1  mrg void PrintStack(const ReportStack *ent) {
    389  1.1  mrg   if (ent == 0 || ent->frames == 0) {
    390  1.1  mrg     Printf("  [failed to restore the stack]\n");
    391  1.1  mrg     return;
    392  1.1  mrg   }
    393  1.1  mrg   SymbolizedStack *frame = ent->frames;
    394  1.1  mrg   for (int i = 0; frame; frame = frame->next, i++) {
    395  1.1  mrg     const AddressInfo &info = frame->info;
    396  1.1  mrg     Printf("  %s()\n      %s:%d +0x%zx\n", info.function,
    397  1.1  mrg            StripPathPrefix(info.file, common_flags()->strip_path_prefix),
    398  1.1  mrg            info.line, info.module_offset);
    399  1.1  mrg   }
    400  1.1  mrg }
    401  1.1  mrg 
    402  1.1  mrg static void PrintMop(const ReportMop *mop, bool first) {
    403  1.1  mrg   Printf("\n");
    404  1.1  mrg   Printf("%s at %p by ",
    405  1.1  mrg          (first ? (mop->write ? "Write" : "Read")
    406  1.1  mrg                 : (mop->write ? "Previous write" : "Previous read")),
    407  1.1  mrg          reinterpret_cast<void *>(mop->addr));
    408  1.1  mrg   if (mop->tid == kMainGoroutineId)
    409  1.1  mrg     Printf("main goroutine:\n");
    410  1.1  mrg   else
    411  1.1  mrg     Printf("goroutine %d:\n", mop->tid);
    412  1.1  mrg   PrintStack(mop->stack);
    413  1.1  mrg }
    414  1.1  mrg 
    415  1.1  mrg static void PrintLocation(const ReportLocation *loc) {
    416  1.1  mrg   switch (loc->type) {
    417  1.1  mrg   case ReportLocationHeap: {
    418  1.1  mrg     Printf("\n");
    419  1.1  mrg     Printf("Heap block of size %zu at %p allocated by ", loc->heap_chunk_size,
    420  1.1  mrg            reinterpret_cast<void *>(loc->heap_chunk_start));
    421  1.1  mrg     if (loc->tid == kMainGoroutineId)
    422  1.1  mrg       Printf("main goroutine:\n");
    423  1.1  mrg     else
    424  1.1  mrg       Printf("goroutine %d:\n", loc->tid);
    425  1.1  mrg     PrintStack(loc->stack);
    426  1.1  mrg     break;
    427  1.1  mrg   }
    428  1.1  mrg   case ReportLocationGlobal: {
    429  1.1  mrg     Printf("\n");
    430  1.1  mrg     Printf("Global var %s of size %zu at %p declared at %s:%zu\n",
    431  1.1  mrg            loc->global.name, loc->global.size,
    432  1.1  mrg            reinterpret_cast<void *>(loc->global.start), loc->global.file,
    433  1.1  mrg            loc->global.line);
    434  1.1  mrg     break;
    435  1.1  mrg   }
    436  1.1  mrg   default:
    437  1.1  mrg     break;
    438  1.1  mrg   }
    439  1.1  mrg }
    440  1.1  mrg 
    441  1.1  mrg static void PrintThread(const ReportThread *rt) {
    442  1.1  mrg   if (rt->id == kMainGoroutineId)
    443  1.1  mrg     return;
    444  1.1  mrg   Printf("\n");
    445  1.1  mrg   Printf("Goroutine %d (%s) created at:\n",
    446  1.1  mrg     rt->id, rt->running ? "running" : "finished");
    447  1.1  mrg   PrintStack(rt->stack);
    448  1.1  mrg }
    449  1.1  mrg 
    450  1.1  mrg void PrintReport(const ReportDesc *rep) {
    451  1.1  mrg   Printf("==================\n");
    452  1.1  mrg   if (rep->typ == ReportTypeRace) {
    453  1.1  mrg     Printf("WARNING: DATA RACE");
    454  1.1  mrg     for (uptr i = 0; i < rep->mops.Size(); i++)
    455  1.1  mrg       PrintMop(rep->mops[i], i == 0);
    456  1.1  mrg     for (uptr i = 0; i < rep->locs.Size(); i++)
    457  1.1  mrg       PrintLocation(rep->locs[i]);
    458  1.1  mrg     for (uptr i = 0; i < rep->threads.Size(); i++)
    459  1.1  mrg       PrintThread(rep->threads[i]);
    460  1.1  mrg   } else if (rep->typ == ReportTypeDeadlock) {
    461  1.1  mrg     Printf("WARNING: DEADLOCK\n");
    462  1.1  mrg     for (uptr i = 0; i < rep->mutexes.Size(); i++) {
    463  1.1  mrg       Printf("Goroutine %d lock mutex %llu while holding mutex %llu:\n", 999,
    464  1.1  mrg              rep->mutexes[i]->id,
    465  1.1  mrg              rep->mutexes[(i + 1) % rep->mutexes.Size()]->id);
    466  1.1  mrg       PrintStack(rep->stacks[2*i]);
    467  1.1  mrg       Printf("\n");
    468  1.1  mrg       Printf("Mutex %llu was previously locked here:\n",
    469  1.1  mrg              rep->mutexes[(i + 1) % rep->mutexes.Size()]->id);
    470  1.1  mrg       PrintStack(rep->stacks[2*i + 1]);
    471  1.1  mrg       Printf("\n");
    472  1.1  mrg     }
    473  1.1  mrg   }
    474  1.1  mrg   Printf("==================\n");
    475  1.1  mrg }
    476  1.1  mrg 
    477  1.1  mrg #endif
    478  1.1  mrg 
    479  1.1  mrg }  // namespace __tsan
    480