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 ReportMop::ReportMop()
27 : mset(MBlockReportMutex
) {
30 ReportDesc::~ReportDesc() {
31 // FIXME(dvyukov): it must be leaking a lot of memory.
36 const int kThreadBufSize
= 32;
37 const char *thread_name(char *buf
, int tid
) {
40 internal_snprintf(buf
, kThreadBufSize
, "thread T%d", tid
);
44 static void PrintHeader(ReportType typ
) {
45 Printf("WARNING: ThreadSanitizer: ");
47 if (typ
== ReportTypeRace
)
49 else if (typ
== ReportTypeUseAfterFree
)
50 Printf("heap-use-after-free");
51 else if (typ
== ReportTypeThreadLeak
)
52 Printf("thread leak");
53 else if (typ
== ReportTypeMutexDestroyLocked
)
54 Printf("destroy of a locked mutex");
55 else if (typ
== ReportTypeSignalUnsafe
)
56 Printf("signal-unsafe call inside of a signal");
57 else if (typ
== ReportTypeErrnoInSignal
)
58 Printf("signal handler spoils errno");
60 Printf(" (pid=%d)\n", GetPid());
63 void PrintStack(const ReportStack
*ent
) {
65 Printf(" [failed to restore the stack]\n\n");
68 for (int i
= 0; ent
; ent
= ent
->next
, i
++) {
69 Printf(" #%d %s %s:%d", i
, ent
->func
, ent
->file
, ent
->line
);
71 Printf(":%d", ent
->col
);
72 if (ent
->module
&& ent
->offset
)
73 Printf(" (%s+%p)\n", ent
->module
, (void*)ent
->offset
);
75 Printf(" (%p)\n", (void*)ent
->pc
);
80 static void PrintMutexSet(Vector
<ReportMopMutex
> const& mset
) {
81 for (uptr i
= 0; i
< mset
.Size(); i
++) {
84 const ReportMopMutex m
= mset
[i
];
85 Printf(" %s M%llu", m
.write
? "write" : "read", m
.id
);
86 Printf(i
== mset
.Size() - 1 ? ")" : ",");
90 static void PrintMop(const ReportMop
*mop
, bool first
) {
91 char thrbuf
[kThreadBufSize
];
92 Printf(" %s of size %d at %p by %s",
93 (first
? (mop
->write
? "Write" : "Read")
94 : (mop
->write
? "Previous write" : "Previous read")),
95 mop
->size
, (void*)mop
->addr
,
96 thread_name(thrbuf
, mop
->tid
));
97 PrintMutexSet(mop
->mset
);
99 PrintStack(mop
->stack
);
102 static void PrintLocation(const ReportLocation
*loc
) {
103 char thrbuf
[kThreadBufSize
];
104 if (loc
->type
== ReportLocationGlobal
) {
105 Printf(" Location is global '%s' of size %zu at %zx (%s+%p)\n\n",
106 loc
->name
, loc
->size
, loc
->addr
, loc
->module
, loc
->offset
);
107 } else if (loc
->type
== ReportLocationHeap
) {
108 char thrbuf
[kThreadBufSize
];
109 Printf(" Location is heap block of size %zu at %p allocated by %s:\n",
110 loc
->size
, loc
->addr
, thread_name(thrbuf
, loc
->tid
));
111 PrintStack(loc
->stack
);
112 } else if (loc
->type
== ReportLocationStack
) {
113 Printf(" Location is stack of %s.\n\n", thread_name(thrbuf
, loc
->tid
));
114 } else if (loc
->type
== ReportLocationTLS
) {
115 Printf(" Location is TLS of %s.\n\n", thread_name(thrbuf
, loc
->tid
));
116 } else if (loc
->type
== ReportLocationFD
) {
117 Printf(" Location is file descriptor %d created by %s at:\n",
118 loc
->fd
, thread_name(thrbuf
, loc
->tid
));
119 PrintStack(loc
->stack
);
123 static void PrintMutex(const ReportMutex
*rm
) {
125 Printf(" Mutex M%llu is already destroyed.\n\n", rm
->id
);
127 Printf(" Mutex M%llu created at:\n", rm
->id
);
128 PrintStack(rm
->stack
);
132 static void PrintThread(const ReportThread
*rt
) {
133 if (rt
->id
== 0) // Little sense in describing the main thread.
135 Printf(" Thread T%d", rt
->id
);
137 Printf(" '%s'", rt
->name
);
138 char thrbuf
[kThreadBufSize
];
139 Printf(" (tid=%zu, %s) created by %s",
140 rt
->pid
, rt
->running
? "running" : "finished",
141 thread_name(thrbuf
, rt
->parent_tid
));
145 PrintStack(rt
->stack
);
148 static void PrintSleep(const ReportStack
*s
) {
149 Printf(" As if synchronized via sleep:\n");
153 void PrintReport(const ReportDesc
*rep
) {
154 Printf("==================\n");
155 PrintHeader(rep
->typ
);
157 for (uptr i
= 0; i
< rep
->stacks
.Size(); i
++) {
160 PrintStack(rep
->stacks
[i
]);
163 for (uptr i
= 0; i
< rep
->mops
.Size(); i
++)
164 PrintMop(rep
->mops
[i
], i
== 0);
167 PrintSleep(rep
->sleep
);
169 for (uptr i
= 0; i
< rep
->locs
.Size(); i
++)
170 PrintLocation(rep
->locs
[i
]);
172 for (uptr i
= 0; i
< rep
->mutexes
.Size(); i
++)
173 PrintMutex(rep
->mutexes
[i
]);
175 for (uptr i
= 0; i
< rep
->threads
.Size(); i
++)
176 PrintThread(rep
->threads
[i
]);
178 Printf("==================\n");
183 void PrintStack(const ReportStack
*ent
) {
185 Printf(" [failed to restore the stack]\n\n");
188 for (int i
= 0; ent
; ent
= ent
->next
, i
++) {
189 Printf(" %s()\n %s:%d +0x%zx\n",
190 ent
->func
, ent
->file
, ent
->line
, (void*)ent
->offset
);
195 static void PrintMop(const ReportMop
*mop
, bool first
) {
196 Printf("%s by goroutine %d:\n",
197 (first
? (mop
->write
? "Write" : "Read")
198 : (mop
->write
? "Previous write" : "Previous read")),
200 PrintStack(mop
->stack
);
203 static void PrintThread(const ReportThread
*rt
) {
204 if (rt
->id
== 0) // Little sense in describing the main thread.
206 Printf("Goroutine %d (%s) created at:\n",
207 rt
->id
, rt
->running
? "running" : "finished");
208 PrintStack(rt
->stack
);
211 void PrintReport(const ReportDesc
*rep
) {
212 Printf("==================\n");
213 Printf("WARNING: DATA RACE\n");
214 for (uptr i
= 0; i
< rep
->mops
.Size(); i
++)
215 PrintMop(rep
->mops
[i
], i
== 0);
216 for (uptr i
= 0; i
< rep
->threads
.Size(); i
++)
217 PrintThread(rep
->threads
[i
]);
218 Printf("==================\n");
223 } // namespace __tsan