Alexey Samsonov | 603c4be | 2012-06-04 13:55:19 +0000 | [diff] [blame] | 1 | //===-- tsan_report.cc ----------------------------------------------------===// |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 2 | // |
| 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 Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 16 | #include "sanitizer_common/sanitizer_placement_new.h" |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 17 | #include "sanitizer_common/sanitizer_report_decorator.h" |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_stacktrace_printer.h" |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 19 | |
| 20 | namespace __tsan { |
| 21 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 22 | ReportStack::ReportStack() : frames(nullptr), suppressable(false) {} |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 23 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 24 | ReportStack *ReportStack::New() { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 25 | void *mem = internal_alloc(MBlockReportStack, sizeof(ReportStack)); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 26 | return new(mem) ReportStack(); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | ReportLocation::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 | |
| 33 | ReportLocation *ReportLocation::New(ReportLocationType type) { |
| 34 | void *mem = internal_alloc(MBlockReportStack, sizeof(ReportLocation)); |
| 35 | return new(mem) ReportLocation(type); |
| 36 | } |
| 37 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 38 | class Decorator: public __sanitizer::SanitizerCommonDecorator { |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 39 | public: |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 40 | Decorator() : SanitizerCommonDecorator() { } |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 41 | 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 Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 55 | ReportDesc::ReportDesc() |
| 56 | : stacks(MBlockReportStack) |
| 57 | , mops(MBlockReportMop) |
| 58 | , locs(MBlockReportLoc) |
| 59 | , mutexes(MBlockReportMutex) |
Dmitry Vyukov | 8485311 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 60 | , threads(MBlockReportThread) |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 61 | , unique_tids(MBlockReportThread) |
Dmitry Vyukov | 4536cb1 | 2013-03-21 16:55:17 +0000 | [diff] [blame] | 62 | , sleep() |
| 63 | , count() { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 66 | ReportMop::ReportMop() |
| 67 | : mset(MBlockReportMutex) { |
| 68 | } |
| 69 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 70 | ReportDesc::~ReportDesc() { |
Dmitry Vyukov | aecf2e5 | 2012-12-04 15:46:05 +0000 | [diff] [blame] | 71 | // FIXME(dvyukov): it must be leaking a lot of memory. |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 74 | #ifndef SANITIZER_GO |
Dmitry Vyukov | c510a2f | 2012-07-06 14:54:25 +0000 | [diff] [blame] | 75 | |
Dmitry Vyukov | da35037 | 2012-12-17 16:28:15 +0000 | [diff] [blame] | 76 | const int kThreadBufSize = 32; |
| 77 | const 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 Serebryany | 2f588f9 | 2013-02-06 14:24:00 +0000 | [diff] [blame] | 84 | static const char *ReportTypeString(ReportType typ) { |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 85 | if (typ == ReportTypeRace) |
Kostya Serebryany | 2f588f9 | 2013-02-06 14:24:00 +0000 | [diff] [blame] | 86 | return "data race"; |
Dmitry Vyukov | 0dc47b6 | 2013-03-21 15:37:39 +0000 | [diff] [blame] | 87 | if (typ == ReportTypeVptrRace) |
| 88 | return "data race on vptr (ctor/dtor vs virtual call)"; |
Kostya Serebryany | 2f588f9 | 2013-02-06 14:24:00 +0000 | [diff] [blame] | 89 | if (typ == ReportTypeUseAfterFree) |
| 90 | return "heap-use-after-free"; |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 91 | if (typ == ReportTypeVptrUseAfterFree) |
| 92 | return "heap-use-after-free (virtual call vs free)"; |
Kostya Serebryany | 2f588f9 | 2013-02-06 14:24:00 +0000 | [diff] [blame] | 93 | if (typ == ReportTypeThreadLeak) |
| 94 | return "thread leak"; |
| 95 | if (typ == ReportTypeMutexDestroyLocked) |
| 96 | return "destroy of a locked mutex"; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 97 | 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 Serebryany | 2f588f9 | 2013-02-06 14:24:00 +0000 | [diff] [blame] | 105 | if (typ == ReportTypeSignalUnsafe) |
| 106 | return "signal-unsafe call inside of a signal"; |
| 107 | if (typ == ReportTypeErrnoInSignal) |
| 108 | return "signal handler spoils errno"; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 109 | if (typ == ReportTypeDeadlock) |
| 110 | return "lock-order-inversion (potential deadlock)"; |
Kostya Serebryany | 2f588f9 | 2013-02-06 14:24:00 +0000 | [diff] [blame] | 111 | return ""; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Dmitry Vyukov | 1da1056 | 2012-09-01 12:13:18 +0000 | [diff] [blame] | 114 | void PrintStack(const ReportStack *ent) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 115 | if (ent == 0 || ent->frames == 0) { |
Dmitry Vyukov | 95f50b1 | 2012-11-22 13:38:28 +0000 | [diff] [blame] | 116 | Printf(" [failed to restore the stack]\n\n"); |
Dmitry Vyukov | dc2a27e | 2012-11-21 11:44:20 +0000 | [diff] [blame] | 117 | return; |
| 118 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 119 | SymbolizedStack *frame = ent->frames; |
| 120 | for (int i = 0; frame && frame->info.address; frame = frame->next, i++) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 121 | InternalScopedString res(2 * GetPageSizeCached()); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 122 | RenderFrame(&res, common_flags()->stack_trace_format, i, frame->info, |
Pirama Arumuga Nainar | cdce50b | 2015-07-01 12:26:56 -0700 | [diff] [blame] | 123 | common_flags()->symbolize_vs_style, |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 124 | common_flags()->strip_path_prefix, "__interceptor_"); |
| 125 | Printf("%s\n", res.data()); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 126 | } |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 127 | Printf("\n"); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 130 | static void PrintMutexSet(Vector<ReportMopMutex> const& mset) { |
| 131 | for (uptr i = 0; i < mset.Size(); i++) { |
| 132 | if (i == 0) |
| 133 | Printf(" (mutexes:"); |
| 134 | const ReportMopMutex m = mset[i]; |
| 135 | Printf(" %s M%llu", m.write ? "write" : "read", m.id); |
| 136 | Printf(i == mset.Size() - 1 ? ")" : ","); |
| 137 | } |
| 138 | } |
| 139 | |
Dmitry Vyukov | 0a07b35 | 2013-02-01 11:10:53 +0000 | [diff] [blame] | 140 | static const char *MopDesc(bool first, bool write, bool atomic) { |
| 141 | return atomic ? (first ? (write ? "Atomic write" : "Atomic read") |
| 142 | : (write ? "Previous atomic write" : "Previous atomic read")) |
| 143 | : (first ? (write ? "Write" : "Read") |
| 144 | : (write ? "Previous write" : "Previous read")); |
| 145 | } |
| 146 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 147 | static void PrintMop(const ReportMop *mop, bool first) { |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 148 | Decorator d; |
Dmitry Vyukov | da35037 | 2012-12-17 16:28:15 +0000 | [diff] [blame] | 149 | char thrbuf[kThreadBufSize]; |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 150 | Printf("%s", d.Access()); |
Dmitry Vyukov | da35037 | 2012-12-17 16:28:15 +0000 | [diff] [blame] | 151 | Printf(" %s of size %d at %p by %s", |
Dmitry Vyukov | 0a07b35 | 2013-02-01 11:10:53 +0000 | [diff] [blame] | 152 | MopDesc(first, mop->write, mop->atomic), |
Dmitry Vyukov | da35037 | 2012-12-17 16:28:15 +0000 | [diff] [blame] | 153 | mop->size, (void*)mop->addr, |
| 154 | thread_name(thrbuf, mop->tid)); |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 155 | PrintMutexSet(mop->mset); |
| 156 | Printf(":\n"); |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 157 | Printf("%s", d.EndAccess()); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 158 | PrintStack(mop->stack); |
| 159 | } |
| 160 | |
| 161 | static void PrintLocation(const ReportLocation *loc) { |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 162 | Decorator d; |
Dmitry Vyukov | c2234cd | 2012-12-18 06:57:34 +0000 | [diff] [blame] | 163 | char thrbuf[kThreadBufSize]; |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 164 | bool print_stack = false; |
| 165 | Printf("%s", d.Location()); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 166 | if (loc->type == ReportLocationGlobal) { |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 167 | const DataInfo &global = loc->global; |
Alexey Samsonov | c842dab | 2013-10-16 09:56:17 +0000 | [diff] [blame] | 168 | Printf(" Location is global '%s' of size %zu at %p (%s+%p)\n\n", |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 169 | global.name, global.size, global.start, |
| 170 | StripModuleName(global.module), global.module_offset); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 171 | } else if (loc->type == ReportLocationHeap) { |
Dmitry Vyukov | da35037 | 2012-12-17 16:28:15 +0000 | [diff] [blame] | 172 | char thrbuf[kThreadBufSize]; |
| 173 | Printf(" Location is heap block of size %zu at %p allocated by %s:\n", |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 174 | loc->heap_chunk_size, loc->heap_chunk_start, |
| 175 | thread_name(thrbuf, loc->tid)); |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 176 | print_stack = true; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 177 | } else if (loc->type == ReportLocationStack) { |
Dmitry Vyukov | fb917e9 | 2013-01-14 10:00:03 +0000 | [diff] [blame] | 178 | Printf(" Location is stack of %s.\n\n", thread_name(thrbuf, loc->tid)); |
| 179 | } else if (loc->type == ReportLocationTLS) { |
| 180 | Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf, loc->tid)); |
Dmitry Vyukov | c2234cd | 2012-12-18 06:57:34 +0000 | [diff] [blame] | 181 | } else if (loc->type == ReportLocationFD) { |
| 182 | Printf(" Location is file descriptor %d created by %s at:\n", |
| 183 | loc->fd, thread_name(thrbuf, loc->tid)); |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 184 | print_stack = true; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 185 | } |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 186 | Printf("%s", d.EndLocation()); |
| 187 | if (print_stack) |
| 188 | PrintStack(loc->stack); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 191 | static void PrintMutexShort(const ReportMutex *rm, const char *after) { |
| 192 | Decorator d; |
| 193 | Printf("%sM%zd%s%s", d.Mutex(), rm->id, d.EndMutex(), after); |
| 194 | } |
| 195 | |
| 196 | static void PrintMutexShortWithAddress(const ReportMutex *rm, |
| 197 | const char *after) { |
| 198 | Decorator d; |
| 199 | Printf("%sM%zd (%p)%s%s", d.Mutex(), rm->id, rm->addr, d.EndMutex(), after); |
| 200 | } |
| 201 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 202 | static void PrintMutex(const ReportMutex *rm) { |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 203 | Decorator d; |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 204 | if (rm->destroyed) { |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 205 | Printf("%s", d.Mutex()); |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 206 | Printf(" Mutex M%llu is already destroyed.\n\n", rm->id); |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 207 | Printf("%s", d.EndMutex()); |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 208 | } else { |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 209 | Printf("%s", d.Mutex()); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 210 | Printf(" Mutex M%llu (%p) created at:\n", rm->id, rm->addr); |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 211 | Printf("%s", d.EndMutex()); |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 212 | PrintStack(rm->stack); |
| 213 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static void PrintThread(const ReportThread *rt) { |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 217 | Decorator d; |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 218 | if (rt->id == 0) // Little sense in describing the main thread. |
| 219 | return; |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 220 | Printf("%s", d.ThreadDescription()); |
Dmitry Vyukov | ad9da37 | 2012-12-06 12:16:15 +0000 | [diff] [blame] | 221 | Printf(" Thread T%d", rt->id); |
Alexey Samsonov | 2bbd8be | 2013-03-15 13:48:44 +0000 | [diff] [blame] | 222 | if (rt->name && rt->name[0] != '\0') |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 223 | Printf(" '%s'", rt->name); |
Dmitry Vyukov | da35037 | 2012-12-17 16:28:15 +0000 | [diff] [blame] | 224 | char thrbuf[kThreadBufSize]; |
| 225 | Printf(" (tid=%zu, %s) created by %s", |
| 226 | rt->pid, rt->running ? "running" : "finished", |
| 227 | thread_name(thrbuf, rt->parent_tid)); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 228 | if (rt->stack) |
Dmitry Vyukov | da35037 | 2012-12-17 16:28:15 +0000 | [diff] [blame] | 229 | Printf(" at:"); |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 230 | Printf("\n"); |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 231 | Printf("%s", d.EndThreadDescription()); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 232 | PrintStack(rt->stack); |
| 233 | } |
| 234 | |
Dmitry Vyukov | 8485311 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 235 | static void PrintSleep(const ReportStack *s) { |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 236 | Decorator d; |
| 237 | Printf("%s", d.Sleep()); |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 238 | Printf(" As if synchronized via sleep:\n"); |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 239 | Printf("%s", d.EndSleep()); |
Dmitry Vyukov | 8485311 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 240 | PrintStack(s); |
| 241 | } |
| 242 | |
Kostya Serebryany | 2f588f9 | 2013-02-06 14:24:00 +0000 | [diff] [blame] | 243 | static ReportStack *ChooseSummaryStack(const ReportDesc *rep) { |
| 244 | if (rep->mops.Size()) |
| 245 | return rep->mops[0]->stack; |
| 246 | if (rep->stacks.Size()) |
| 247 | return rep->stacks[0]; |
| 248 | if (rep->mutexes.Size()) |
| 249 | return rep->mutexes[0]->stack; |
| 250 | if (rep->threads.Size()) |
| 251 | return rep->threads[0]->stack; |
| 252 | return 0; |
| 253 | } |
| 254 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 255 | static bool FrameIsInternal(const SymbolizedStack *frame) { |
| 256 | if (frame == 0) |
| 257 | return false; |
| 258 | const char *file = frame->info.file; |
| 259 | return file != 0 && |
| 260 | (internal_strstr(file, "tsan_interceptors.cc") || |
| 261 | internal_strstr(file, "sanitizer_common_interceptors.inc") || |
| 262 | internal_strstr(file, "tsan_interface_")); |
| 263 | } |
| 264 | |
| 265 | static SymbolizedStack *SkipTsanInternalFrames(SymbolizedStack *frames) { |
| 266 | while (FrameIsInternal(frames) && frames->next) |
| 267 | frames = frames->next; |
| 268 | return frames; |
Kostya Serebryany | 2f588f9 | 2013-02-06 14:24:00 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 271 | void PrintReport(const ReportDesc *rep) { |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 272 | Decorator d; |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 273 | Printf("==================\n"); |
Kostya Serebryany | 2f588f9 | 2013-02-06 14:24:00 +0000 | [diff] [blame] | 274 | const char *rep_typ_str = ReportTypeString(rep->typ); |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 275 | Printf("%s", d.Warning()); |
Peter Collingbourne | 0b694fc | 2013-05-17 16:56:53 +0000 | [diff] [blame] | 276 | Printf("WARNING: ThreadSanitizer: %s (pid=%d)\n", rep_typ_str, |
| 277 | (int)internal_getpid()); |
Kostya Serebryany | a27512c | 2013-09-05 11:23:27 +0000 | [diff] [blame] | 278 | Printf("%s", d.EndWarning()); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 279 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 280 | if (rep->typ == ReportTypeDeadlock) { |
| 281 | char thrbuf[kThreadBufSize]; |
| 282 | Printf(" Cycle in lock order graph: "); |
| 283 | for (uptr i = 0; i < rep->mutexes.Size(); i++) |
| 284 | PrintMutexShortWithAddress(rep->mutexes[i], " => "); |
| 285 | PrintMutexShort(rep->mutexes[0], "\n\n"); |
| 286 | CHECK_GT(rep->mutexes.Size(), 0U); |
| 287 | CHECK_EQ(rep->mutexes.Size() * (flags()->second_deadlock_stack ? 2 : 1), |
| 288 | rep->stacks.Size()); |
| 289 | for (uptr i = 0; i < rep->mutexes.Size(); i++) { |
| 290 | Printf(" Mutex "); |
| 291 | PrintMutexShort(rep->mutexes[(i + 1) % rep->mutexes.Size()], |
| 292 | " acquired here while holding mutex "); |
| 293 | PrintMutexShort(rep->mutexes[i], " in "); |
| 294 | Printf("%s", d.ThreadDescription()); |
| 295 | Printf("%s:\n", thread_name(thrbuf, rep->unique_tids[i])); |
| 296 | Printf("%s", d.EndThreadDescription()); |
| 297 | if (flags()->second_deadlock_stack) { |
| 298 | PrintStack(rep->stacks[2*i]); |
| 299 | Printf(" Mutex "); |
| 300 | PrintMutexShort(rep->mutexes[i], |
| 301 | " previously acquired by the same thread here:\n"); |
| 302 | PrintStack(rep->stacks[2*i+1]); |
| 303 | } else { |
| 304 | PrintStack(rep->stacks[i]); |
| 305 | if (i == 0) |
| 306 | Printf(" Hint: use TSAN_OPTIONS=second_deadlock_stack=1 " |
| 307 | "to get more informative warning message\n\n"); |
| 308 | } |
| 309 | } |
| 310 | } else { |
| 311 | for (uptr i = 0; i < rep->stacks.Size(); i++) { |
| 312 | if (i) |
| 313 | Printf(" and:\n"); |
| 314 | PrintStack(rep->stacks[i]); |
| 315 | } |
Dmitry Vyukov | 332c62b | 2012-08-16 15:08:49 +0000 | [diff] [blame] | 316 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 317 | |
| 318 | for (uptr i = 0; i < rep->mops.Size(); i++) |
| 319 | PrintMop(rep->mops[i], i == 0); |
| 320 | |
Dmitry Vyukov | 8485311 | 2012-08-31 17:27:49 +0000 | [diff] [blame] | 321 | if (rep->sleep) |
| 322 | PrintSleep(rep->sleep); |
| 323 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 324 | for (uptr i = 0; i < rep->locs.Size(); i++) |
| 325 | PrintLocation(rep->locs[i]); |
| 326 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 327 | if (rep->typ != ReportTypeDeadlock) { |
| 328 | for (uptr i = 0; i < rep->mutexes.Size(); i++) |
| 329 | PrintMutex(rep->mutexes[i]); |
| 330 | } |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 331 | |
| 332 | for (uptr i = 0; i < rep->threads.Size(); i++) |
| 333 | PrintThread(rep->threads[i]); |
| 334 | |
Dmitry Vyukov | 4536cb1 | 2013-03-21 16:55:17 +0000 | [diff] [blame] | 335 | if (rep->typ == ReportTypeThreadLeak && rep->count > 1) |
| 336 | Printf(" And %d more similar thread leaks.\n\n", rep->count - 1); |
| 337 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 338 | if (ReportStack *stack = ChooseSummaryStack(rep)) { |
Pirama Arumuga Nainar | 7c91505 | 2015-04-08 08:58:29 -0700 | [diff] [blame] | 339 | if (SymbolizedStack *frame = SkipTsanInternalFrames(stack->frames)) |
| 340 | ReportErrorSummary(rep_typ_str, frame->info); |
Stephen Hines | 6d18623 | 2014-11-26 17:56:19 -0800 | [diff] [blame] | 341 | } |
Kostya Serebryany | 2f588f9 | 2013-02-06 14:24:00 +0000 | [diff] [blame] | 342 | |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 343 | Printf("==================\n"); |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 346 | #else // #ifndef SANITIZER_GO |
Dmitry Vyukov | 9d95475 | 2013-08-16 11:15:14 +0000 | [diff] [blame] | 347 | |
| 348 | const int kMainThreadId = 1; |
Dmitry Vyukov | c510a2f | 2012-07-06 14:54:25 +0000 | [diff] [blame] | 349 | |
Dmitry Vyukov | 0ab628c | 2012-09-06 15:18:14 +0000 | [diff] [blame] | 350 | void PrintStack(const ReportStack *ent) { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 351 | if (ent == 0 || ent->frames == 0) { |
Dmitry Vyukov | 9d95475 | 2013-08-16 11:15:14 +0000 | [diff] [blame] | 352 | Printf(" [failed to restore the stack]\n"); |
Dmitry Vyukov | 6c0fbca | 2012-11-28 13:30:06 +0000 | [diff] [blame] | 353 | return; |
| 354 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 355 | SymbolizedStack *frame = ent->frames; |
| 356 | for (int i = 0; frame; frame = frame->next, i++) { |
| 357 | const AddressInfo &info = frame->info; |
| 358 | Printf(" %s()\n %s:%d +0x%zx\n", info.function, |
| 359 | StripPathPrefix(info.file, common_flags()->strip_path_prefix), |
| 360 | info.line, (void *)info.module_offset); |
Dmitry Vyukov | c510a2f | 2012-07-06 14:54:25 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | static void PrintMop(const ReportMop *mop, bool first) { |
Dmitry Vyukov | 9d95475 | 2013-08-16 11:15:14 +0000 | [diff] [blame] | 365 | Printf("\n"); |
| 366 | Printf("%s by ", |
Dmitry Vyukov | c510a2f | 2012-07-06 14:54:25 +0000 | [diff] [blame] | 367 | (first ? (mop->write ? "Write" : "Read") |
Dmitry Vyukov | 9d95475 | 2013-08-16 11:15:14 +0000 | [diff] [blame] | 368 | : (mop->write ? "Previous write" : "Previous read"))); |
| 369 | if (mop->tid == kMainThreadId) |
| 370 | Printf("main goroutine:\n"); |
| 371 | else |
| 372 | Printf("goroutine %d:\n", mop->tid); |
Dmitry Vyukov | c510a2f | 2012-07-06 14:54:25 +0000 | [diff] [blame] | 373 | PrintStack(mop->stack); |
| 374 | } |
| 375 | |
Dmitry Vyukov | 43046e0 | 2012-07-24 12:03:47 +0000 | [diff] [blame] | 376 | static void PrintThread(const ReportThread *rt) { |
Dmitry Vyukov | 9d95475 | 2013-08-16 11:15:14 +0000 | [diff] [blame] | 377 | if (rt->id == kMainThreadId) |
Dmitry Vyukov | 43046e0 | 2012-07-24 12:03:47 +0000 | [diff] [blame] | 378 | return; |
Dmitry Vyukov | 9d95475 | 2013-08-16 11:15:14 +0000 | [diff] [blame] | 379 | Printf("\n"); |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 380 | Printf("Goroutine %d (%s) created at:\n", |
Dmitry Vyukov | 43046e0 | 2012-07-24 12:03:47 +0000 | [diff] [blame] | 381 | rt->id, rt->running ? "running" : "finished"); |
| 382 | PrintStack(rt->stack); |
| 383 | } |
| 384 | |
Dmitry Vyukov | c510a2f | 2012-07-06 14:54:25 +0000 | [diff] [blame] | 385 | void PrintReport(const ReportDesc *rep) { |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 386 | Printf("==================\n"); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 387 | if (rep->typ == ReportTypeRace) { |
| 388 | Printf("WARNING: DATA RACE"); |
| 389 | for (uptr i = 0; i < rep->mops.Size(); i++) |
| 390 | PrintMop(rep->mops[i], i == 0); |
| 391 | for (uptr i = 0; i < rep->threads.Size(); i++) |
| 392 | PrintThread(rep->threads[i]); |
| 393 | } else if (rep->typ == ReportTypeDeadlock) { |
| 394 | Printf("WARNING: DEADLOCK\n"); |
| 395 | for (uptr i = 0; i < rep->mutexes.Size(); i++) { |
| 396 | Printf("Goroutine %d lock mutex %d while holding mutex %d:\n", |
| 397 | 999, rep->mutexes[i]->id, |
| 398 | rep->mutexes[(i+1) % rep->mutexes.Size()]->id); |
| 399 | PrintStack(rep->stacks[2*i]); |
| 400 | Printf("\n"); |
| 401 | Printf("Mutex %d was previously locked here:\n", |
| 402 | rep->mutexes[(i+1) % rep->mutexes.Size()]->id); |
| 403 | PrintStack(rep->stacks[2*i + 1]); |
| 404 | Printf("\n"); |
| 405 | } |
| 406 | } |
Alexey Samsonov | b1fe302 | 2012-11-02 12:17:51 +0000 | [diff] [blame] | 407 | Printf("==================\n"); |
Dmitry Vyukov | c510a2f | 2012-07-06 14:54:25 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | #endif |
| 411 | |
Kostya Serebryany | 7ac4148 | 2012-05-10 13:48:04 +0000 | [diff] [blame] | 412 | } // namespace __tsan |