1 //===-- tsan_report.cc ----------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer (TSan), a race detector.
10 //===----------------------------------------------------------------------===//
11 #include "tsan_report.h"
12 #include "tsan_platform.h"
17 ReportDesc::ReportDesc()
18 : stacks(MBlockReportStack
)
19 , mops(MBlockReportMop
)
20 , locs(MBlockReportLoc
)
21 , mutexes(MBlockReportMutex
)
22 , threads(MBlockReportThread
)
26 ReportDesc::~ReportDesc() {
27 // FIXME(dvyukov): it must be leaking a lot of memory.
32 static void PrintHeader(ReportType typ
) {
33 Printf("WARNING: ThreadSanitizer: ");
35 if (typ
== ReportTypeRace
)
37 else if (typ
== ReportTypeUseAfterFree
)
38 Printf("heap-use-after-free");
39 else if (typ
== ReportTypeThreadLeak
)
40 Printf("thread leak");
41 else if (typ
== ReportTypeMutexDestroyLocked
)
42 Printf("destroy of a locked mutex");
43 else if (typ
== ReportTypeSignalUnsafe
)
44 Printf("signal-unsafe call inside of a signal");
45 else if (typ
== ReportTypeErrnoInSignal
)
46 Printf("signal handler spoils errno");
48 Printf(" (pid=%d)\n", GetPid());
51 void PrintStack(const ReportStack
*ent
) {
53 Printf(" [failed to restore the stack]\n\n");
56 for (int i
= 0; ent
; ent
= ent
->next
, i
++) {
57 Printf(" #%d %s %s:%d", i
, ent
->func
, ent
->file
, ent
->line
);
59 Printf(":%d", ent
->col
);
60 if (ent
->module
&& ent
->offset
)
61 Printf(" (%s+%p)\n", ent
->module
, (void*)ent
->offset
);
63 Printf(" (%p)\n", (void*)ent
->pc
);
68 static void PrintMop(const ReportMop
*mop
, bool first
) {
69 Printf(" %s of size %d at %p",
70 (first
? (mop
->write
? "Write" : "Read")
71 : (mop
->write
? "Previous write" : "Previous read")),
72 mop
->size
, (void*)mop
->addr
);
74 Printf(" by main thread:\n");
76 Printf(" by thread %d:\n", mop
->tid
);
77 PrintStack(mop
->stack
);
80 static void PrintLocation(const ReportLocation
*loc
) {
81 if (loc
->type
== ReportLocationGlobal
) {
82 Printf(" Location is global '%s' of size %zu at %zx %s:%d (%s+%p)\n\n",
83 loc
->name
, loc
->size
, loc
->addr
, loc
->file
, loc
->line
,
84 loc
->module
, loc
->offset
);
85 } else if (loc
->type
== ReportLocationHeap
) {
86 Printf(" Location is heap block of size %zu at %p allocated",
87 loc
->size
, loc
->addr
);
89 Printf(" by main thread:\n");
91 Printf(" by thread %d:\n", loc
->tid
);
92 PrintStack(loc
->stack
);
93 } else if (loc
->type
== ReportLocationStack
) {
94 Printf(" Location is stack of thread %d:\n\n", loc
->tid
);
98 static void PrintMutex(const ReportMutex
*rm
) {
101 Printf(" Mutex %d created at:\n", rm
->id
);
102 PrintStack(rm
->stack
);
105 static void PrintThread(const ReportThread
*rt
) {
106 if (rt
->id
== 0) // Little sense in describing the main thread.
108 Printf(" Thread %d", rt
->id
);
110 Printf(" '%s'", rt
->name
);
111 Printf(" (tid=%zu, %s)", rt
->pid
, rt
->running
? "running" : "finished");
113 Printf(" created at:");
115 PrintStack(rt
->stack
);
118 static void PrintSleep(const ReportStack
*s
) {
119 Printf(" As if synchronized via sleep:\n");
123 void PrintReport(const ReportDesc
*rep
) {
124 Printf("==================\n");
125 PrintHeader(rep
->typ
);
127 for (uptr i
= 0; i
< rep
->stacks
.Size(); i
++) {
130 PrintStack(rep
->stacks
[i
]);
133 for (uptr i
= 0; i
< rep
->mops
.Size(); i
++)
134 PrintMop(rep
->mops
[i
], i
== 0);
137 PrintSleep(rep
->sleep
);
139 for (uptr i
= 0; i
< rep
->locs
.Size(); i
++)
140 PrintLocation(rep
->locs
[i
]);
142 for (uptr i
= 0; i
< rep
->mutexes
.Size(); i
++)
143 PrintMutex(rep
->mutexes
[i
]);
145 for (uptr i
= 0; i
< rep
->threads
.Size(); i
++)
146 PrintThread(rep
->threads
[i
]);
148 Printf("==================\n");
153 void PrintStack(const ReportStack
*ent
) {
155 Printf(" [failed to restore the stack]\n\n");
158 for (int i
= 0; ent
; ent
= ent
->next
, i
++) {
159 Printf(" %s()\n %s:%d +0x%zx\n",
160 ent
->func
, ent
->file
, ent
->line
, (void*)ent
->offset
);
165 static void PrintMop(const ReportMop
*mop
, bool first
) {
166 Printf("%s by goroutine %d:\n",
167 (first
? (mop
->write
? "Write" : "Read")
168 : (mop
->write
? "Previous write" : "Previous read")),
170 PrintStack(mop
->stack
);
173 static void PrintThread(const ReportThread
*rt
) {
174 if (rt
->id
== 0) // Little sense in describing the main thread.
176 Printf("Goroutine %d (%s) created at:\n",
177 rt
->id
, rt
->running
? "running" : "finished");
178 PrintStack(rt
->stack
);
181 void PrintReport(const ReportDesc
*rep
) {
182 Printf("==================\n");
183 Printf("WARNING: DATA RACE\n");
184 for (uptr i
= 0; i
< rep
->mops
.Size(); i
++)
185 PrintMop(rep
->mops
[i
], i
== 0);
186 for (uptr i
= 0; i
< rep
->threads
.Size(); i
++)
187 PrintThread(rep
->threads
[i
]);
188 Printf("==================\n");
193 } // namespace __tsan