blob: 842a6379dc4f0d44b987b18d9ab37f62d16a2770 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 bart.vanassche@gmail.com
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
bart886b87c2008-06-28 13:40:41 +000026#include "drd_clientobj.h" /* struct mutex_info */
sewardjaf44c822007-11-25 14:01:38 +000027#include "drd_error.h"
28#include "drd_malloc_wrappers.h"
bart886b87c2008-06-28 13:40:41 +000029#include "drd_mutex.h"
30#include "drd_suppression.h" /* drd_start_suppression() */
31#include "pub_drd_bitmap.h" /* LHS_W, ... */
sewardjaf44c822007-11-25 14:01:38 +000032#include "pub_tool_vki.h"
33#include "pub_tool_basics.h"
bart886b87c2008-06-28 13:40:41 +000034#include "pub_tool_libcassert.h" /* tl_assert() */
35#include "pub_tool_libcbase.h" /* strlen() */
36#include "pub_tool_libcfile.h" /* VG_(get_startup_wd)() */
37#include "pub_tool_libcprint.h" /* VG_(printf)() */
sewardjaf44c822007-11-25 14:01:38 +000038#include "pub_tool_machine.h"
bart886b87c2008-06-28 13:40:41 +000039#include "pub_tool_mallocfree.h" /* VG_(malloc), VG_(free) */
40#include "pub_tool_threadstate.h" /* VG_(get_pthread_id)() */
41#include "pub_tool_tooliface.h" /* VG_(needs_tool_errors)() */
sewardjaf44c822007-11-25 14:01:38 +000042
43
bart16d76e52008-03-18 17:08:08 +000044/* Local variables. */
45
46static Bool s_drd_show_conflicting_segments = True;
47
48
49void set_show_conflicting_segments(const Bool scs)
50{
51 s_drd_show_conflicting_segments = scs;
52}
53
bart886b87c2008-06-28 13:40:41 +000054/** Describe a data address range [a,a+len[ as good as possible, for error
55 * messages, putting the result in ai.
56 */
bartff1252a2008-03-16 17:27:25 +000057static
58void describe_malloced_addr(Addr const a, SizeT const len, AddrInfo* const ai)
sewardjaf44c822007-11-25 14:01:38 +000059{
bartff1252a2008-03-16 17:27:25 +000060 Addr data;
sewardjaf44c822007-11-25 14:01:38 +000061
bartff1252a2008-03-16 17:27:25 +000062 if (drd_heap_addrinfo(a, &data, &ai->size, &ai->lastchange))
bart3772a982008-03-15 08:11:03 +000063 {
bartff1252a2008-03-16 17:27:25 +000064 ai->akind = eMallocd;
65 ai->rwoffset = a - data;
bart3772a982008-03-15 08:11:03 +000066 }
bartff1252a2008-03-16 17:27:25 +000067 else
bart3772a982008-03-15 08:11:03 +000068 {
bartff1252a2008-03-16 17:27:25 +000069 ai->akind = eUnknown;
bart3772a982008-03-15 08:11:03 +000070 }
sewardjaf44c822007-11-25 14:01:38 +000071}
72
bart886b87c2008-06-28 13:40:41 +000073/** Report where a mutex has been observed for the first time. The printed
74 * call stack will either refer to a pthread_mutex_init() or a
75 * pthread_mutex_lock() call.
76 */
77static void mutex_first_observed(const Addr mutex)
78{
79 struct mutex_info* mi;
80
81 mi = mutex_get(mutex);
82 if (mi)
83 {
84 tl_assert(mi->first_observed_at);
85 VG_(message)(Vg_UserMsg,
86 "Mutex 0x%lx was first observed at:", mutex);
87 VG_(pp_ExeContext)(mi->first_observed_at);
88 }
89}
90
sewardjaf44c822007-11-25 14:01:38 +000091static
92void drd_report_data_race2(Error* const err, const DataRaceErrInfo* const dri)
93{
bart3772a982008-03-15 08:11:03 +000094 AddrInfo ai;
bartf993b872008-04-01 18:19:50 +000095 const unsigned descr_size = 256;
96 Char* descr1 = VG_(malloc)(descr_size);
97 Char* descr2 = VG_(malloc)(descr_size);
sewardjaf44c822007-11-25 14:01:38 +000098
bart3772a982008-03-15 08:11:03 +000099 tl_assert(dri);
100 tl_assert(dri->addr);
101 tl_assert(dri->size > 0);
bartfdd8d4e2008-04-01 18:38:29 +0000102 tl_assert(descr1);
103 tl_assert(descr2);
bartb515eb12008-03-07 18:52:38 +0000104
bart3772a982008-03-15 08:11:03 +0000105 descr1[0] = 0;
106 descr2[0] = 0;
bartf993b872008-04-01 18:19:50 +0000107 VG_(get_data_description)(descr1, descr2, descr_size, dri->addr);
bart3772a982008-03-15 08:11:03 +0000108 if (descr1[0] == 0)
109 {
bartff1252a2008-03-16 17:27:25 +0000110 describe_malloced_addr(dri->addr, dri->size, &ai);
bart3772a982008-03-15 08:11:03 +0000111 }
112 VG_(message)(Vg_UserMsg,
bartaa97a542008-03-16 17:57:01 +0000113 "Conflicting %s by thread %d/%d at 0x%08lx size %ld",
bart3772a982008-03-15 08:11:03 +0000114 dri->access_type == eStore ? "store" : "load",
bart354009c2008-03-16 10:42:33 +0000115 DrdThreadIdToVgThreadId(dri->tid),
bartaa97a542008-03-16 17:57:01 +0000116 dri->tid,
bart3772a982008-03-15 08:11:03 +0000117 dri->addr,
118 dri->size);
119 VG_(pp_ExeContext)(VG_(get_error_where)(err));
120 if (descr1[0])
121 {
122 VG_(message)(Vg_UserMsg, "%s", descr1);
123 VG_(message)(Vg_UserMsg, "%s", descr2);
124 }
125 else if (ai.akind == eMallocd && ai.lastchange)
126 {
127 VG_(message)(Vg_UserMsg,
128 "Address 0x%lx is at offset %ld from 0x%lx."
129 " Allocation context:",
130 dri->addr, ai.rwoffset, dri->addr - ai.rwoffset);
131 VG_(pp_ExeContext)(ai.lastchange);
132 }
133 else
134 {
135 VG_(message)(Vg_UserMsg, "Allocation context: unknown.");
136 }
bart16d76e52008-03-18 17:08:08 +0000137 if (s_drd_show_conflicting_segments)
138 {
139 thread_report_conflicting_segments(dri->tid,
140 dri->addr, dri->size, dri->access_type);
141 }
bartf993b872008-04-01 18:19:50 +0000142
143 VG_(free)(descr2);
144 VG_(free)(descr1);
sewardjaf44c822007-11-25 14:01:38 +0000145}
146
147static Bool drd_tool_error_eq(VgRes res, Error* e1, Error* e2)
148{
bart3772a982008-03-15 08:11:03 +0000149 return False;
sewardjaf44c822007-11-25 14:01:38 +0000150}
151
152static void drd_tool_error_pp(Error* const e)
153{
bart3772a982008-03-15 08:11:03 +0000154 switch (VG_(get_error_kind)(e))
155 {
156 case DataRaceErr: {
157 drd_report_data_race2(e, VG_(get_error_extra)(e));
158 break;
159 }
160 case MutexErr: {
161 MutexErrInfo* p = (MutexErrInfo*)(VG_(get_error_extra)(e));
162 tl_assert(p);
bart6b717612008-03-24 09:29:38 +0000163 if (p->recursion_count >= 0)
164 {
165 VG_(message)(Vg_UserMsg,
166 "%s: mutex 0x%lx, recursion count %d, owner %d.",
167 VG_(get_error_string)(e),
168 p->mutex,
169 p->recursion_count,
170 p->owner);
171 }
172 else
173 {
174 VG_(message)(Vg_UserMsg,
bart52e82912008-03-24 19:31:33 +0000175 "The object at address 0x%lx is not a mutex.",
bart6b717612008-03-24 09:29:38 +0000176 p->mutex);
177 }
bart3772a982008-03-15 08:11:03 +0000178 VG_(pp_ExeContext)(VG_(get_error_where)(e));
179 break;
180 }
181 case CondErr: {
182 CondErrInfo* cdei =(CondErrInfo*)(VG_(get_error_extra)(e));
183 VG_(message)(Vg_UserMsg,
184 "%s: cond 0x%lx",
bartbe8a12c2008-03-17 18:36:55 +0000185 VG_(get_error_string)(e),
186 cdei->cond);
bart3772a982008-03-15 08:11:03 +0000187 VG_(pp_ExeContext)(VG_(get_error_where)(e));
188 break;
189 }
190 case CondRaceErr: {
191 CondRaceErrInfo* cei = (CondRaceErrInfo*)(VG_(get_error_extra)(e));
192 VG_(message)(Vg_UserMsg,
bart46b5fce2008-06-28 13:01:30 +0000193 "Probably a race condition: condition variable 0x%lx has been"
bart886b87c2008-06-28 13:40:41 +0000194 " signaled but the associated mutex 0x%lx is not locked"
195 " by the signalling thread.",
bart3772a982008-03-15 08:11:03 +0000196 cei->cond, cei->mutex);
197 VG_(pp_ExeContext)(VG_(get_error_where)(e));
bart886b87c2008-06-28 13:40:41 +0000198 mutex_first_observed(cei->mutex);
bart3772a982008-03-15 08:11:03 +0000199 break;
200 }
201 case CondDestrErr: {
202 CondDestrErrInfo* cdi = (CondDestrErrInfo*)(VG_(get_error_extra)(e));
203 VG_(message)(Vg_UserMsg,
bartaa97a542008-03-16 17:57:01 +0000204 "%s: cond 0x%lx, mutex 0x%lx locked by thread %d/%d",
bartbe8a12c2008-03-17 18:36:55 +0000205 VG_(get_error_string)(e),
bartaa97a542008-03-16 17:57:01 +0000206 cdi->cond, cdi->mutex,
207 DrdThreadIdToVgThreadId(cdi->tid), cdi->tid);
bart3772a982008-03-15 08:11:03 +0000208 VG_(pp_ExeContext)(VG_(get_error_where)(e));
bart886b87c2008-06-28 13:40:41 +0000209 mutex_first_observed(cdi->mutex);
bart3772a982008-03-15 08:11:03 +0000210 break;
211 }
212 case SemaphoreErr: {
bart886b87c2008-06-28 13:40:41 +0000213 SemaphoreErrInfo* sei = (SemaphoreErrInfo*)(VG_(get_error_extra)(e));
bart3772a982008-03-15 08:11:03 +0000214 tl_assert(sei);
215 VG_(message)(Vg_UserMsg,
216 "%s: semaphore 0x%lx",
217 VG_(get_error_string)(e),
218 sei->semaphore);
219 VG_(pp_ExeContext)(VG_(get_error_where)(e));
220 break;
221 }
222 case BarrierErr: {
223 BarrierErrInfo* sei =(BarrierErrInfo*)(VG_(get_error_extra)(e));
224 tl_assert(sei);
225 VG_(message)(Vg_UserMsg,
226 "%s: barrier 0x%lx",
227 VG_(get_error_string)(e),
228 sei->barrier);
229 VG_(pp_ExeContext)(VG_(get_error_where)(e));
230 break;
231 }
232 case RwlockErr: {
233 RwlockErrInfo* p = (RwlockErrInfo*)(VG_(get_error_extra)(e));
234 tl_assert(p);
235 VG_(message)(Vg_UserMsg,
236 "%s: rwlock 0x%lx.",
237 VG_(get_error_string)(e),
238 p->rwlock);
239 VG_(pp_ExeContext)(VG_(get_error_where)(e));
240 break;
241 }
bart9d5b7962008-05-14 12:25:00 +0000242 case HoldtimeErr: {
243 HoldtimeErrInfo* p =(HoldtimeErrInfo*)(VG_(get_error_extra)(e));
244 tl_assert(p);
245 tl_assert(p->acquired_at);
246 VG_(message)(Vg_UserMsg, "Acquired at:");
247 VG_(pp_ExeContext)(p->acquired_at);
248 VG_(message)(Vg_UserMsg,
249 "Lock on %s 0x%lx was held during %d ms (threshold: %d ms).",
250 VG_(get_error_string)(e),
251 p->synchronization_object,
252 p->hold_time_ms,
253 p->threshold_ms);
254 VG_(pp_ExeContext)(VG_(get_error_where)(e));
255 break;
256 }
bart3772a982008-03-15 08:11:03 +0000257 case GenericErr: {
258 //GenericErrInfo* gei =(GenericErrInfo*)(VG_(get_error_extra)(e));
259 VG_(message)(Vg_UserMsg, "%s", VG_(get_error_string)(e));
260 VG_(pp_ExeContext)(VG_(get_error_where)(e));
261 break;
262 }
263 default:
264 VG_(message)(Vg_UserMsg,
265 "%s",
266 VG_(get_error_string)(e));
267 VG_(pp_ExeContext)(VG_(get_error_where)(e));
268 break;
269 }
sewardjaf44c822007-11-25 14:01:38 +0000270}
271
272static UInt drd_tool_error_update_extra(Error* e)
273{
bart3772a982008-03-15 08:11:03 +0000274 switch (VG_(get_error_kind)(e))
275 {
276 case DataRaceErr:
277 return sizeof(DataRaceErrInfo);
278 case MutexErr:
279 return sizeof(MutexErrInfo);
280 case CondErr:
281 return sizeof(CondErrInfo);
282 case CondRaceErr:
283 return sizeof(CondRaceErrInfo);
284 case CondDestrErr:
285 return sizeof(CondDestrErrInfo);
286 case SemaphoreErr:
287 return sizeof(SemaphoreErrInfo);
288 case BarrierErr:
289 return sizeof(BarrierErrInfo);
290 case RwlockErr:
291 return sizeof(RwlockErrInfo);
bart9d5b7962008-05-14 12:25:00 +0000292 case HoldtimeErr:
293 return sizeof(HoldtimeErrInfo);
bart3772a982008-03-15 08:11:03 +0000294 case GenericErr:
295 return sizeof(GenericErrInfo);
296 default:
297 tl_assert(False);
298 break;
299 }
sewardjaf44c822007-11-25 14:01:38 +0000300}
301
302static Bool drd_tool_error_recog(Char* const name, Supp* const supp)
303{
bart5fc70e62008-03-23 07:54:02 +0000304 SuppKind skind = 0;
sewardjaf44c822007-11-25 14:01:38 +0000305
bart5fc70e62008-03-23 07:54:02 +0000306 if (VG_(strcmp)(name, STR_DataRaceErr) == 0)
307 ;
308 else if (VG_(strcmp)(name, STR_MutexErr) == 0)
309 ;
310 else if (VG_(strcmp)(name, STR_CondErr) == 0)
311 ;
312 else if (VG_(strcmp)(name, STR_CondRaceErr) == 0)
313 ;
314 else if (VG_(strcmp)(name, STR_CondDestrErr) == 0)
315 ;
316 else if (VG_(strcmp)(name, STR_SemaphoreErr) == 0)
317 ;
318 else if (VG_(strcmp)(name, STR_BarrierErr) == 0)
319 ;
320 else if (VG_(strcmp)(name, STR_RwlockErr) == 0)
321 ;
bart9d5b7962008-05-14 12:25:00 +0000322 else if (VG_(strcmp)(name, STR_HoldtimeErr) == 0)
323 ;
bart5fc70e62008-03-23 07:54:02 +0000324 else if (VG_(strcmp)(name, STR_GenericErr) == 0)
325 ;
bart3772a982008-03-15 08:11:03 +0000326 else
327 return False;
sewardjaf44c822007-11-25 14:01:38 +0000328
bart3772a982008-03-15 08:11:03 +0000329 VG_(set_supp_kind)(supp, skind);
330 return True;
sewardjaf44c822007-11-25 14:01:38 +0000331}
332
333static Bool drd_tool_error_read_extra(Int fd, Char* buf, Int nBuf, Supp* supp)
334{
bart3772a982008-03-15 08:11:03 +0000335 return True;
sewardjaf44c822007-11-25 14:01:38 +0000336}
337
338static Bool drd_tool_error_matches(Error* const e, Supp* const supp)
339{
bart3772a982008-03-15 08:11:03 +0000340 switch (VG_(get_supp_kind)(supp))
341 {
342 }
343 return True;
sewardjaf44c822007-11-25 14:01:38 +0000344}
345
346static Char* drd_tool_error_name(Error* e)
347{
bart3772a982008-03-15 08:11:03 +0000348 switch (VG_(get_error_kind)(e))
349 {
bart5fc70e62008-03-23 07:54:02 +0000350 case DataRaceErr: return VGAPPEND(STR_, DataRaceErr);
351 case MutexErr: return VGAPPEND(STR_, MutexErr);
352 case CondErr: return VGAPPEND(STR_, CondErr);
353 case CondRaceErr: return VGAPPEND(STR_, CondRaceErr);
354 case CondDestrErr: return VGAPPEND(STR_, CondDestrErr);
355 case SemaphoreErr: return VGAPPEND(STR_, SemaphoreErr);
356 case BarrierErr: return VGAPPEND(STR_, BarrierErr);
357 case RwlockErr: return VGAPPEND(STR_, RwlockErr);
bart9d5b7962008-05-14 12:25:00 +0000358 case HoldtimeErr: return VGAPPEND(STR_, HoldtimeErr);
bart5fc70e62008-03-23 07:54:02 +0000359 case GenericErr: return VGAPPEND(STR_, GenericErr);
bart3772a982008-03-15 08:11:03 +0000360 default:
361 tl_assert(0);
362 }
363 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000364}
365
366static void drd_tool_error_print_extra(Error* e)
367{
bart3772a982008-03-15 08:11:03 +0000368 switch (VG_(get_error_kind)(e))
369 {
370 // VG_(printf)(" %s\n", VG_(get_error_string)(err));
371 }
sewardjaf44c822007-11-25 14:01:38 +0000372}
373
374void drd_register_error_handlers(void)
375{
bart3772a982008-03-15 08:11:03 +0000376 // Tool error reporting.
377 VG_(needs_tool_errors)(drd_tool_error_eq,
378 drd_tool_error_pp,
379 True,
380 drd_tool_error_update_extra,
381 drd_tool_error_recog,
382 drd_tool_error_read_extra,
383 drd_tool_error_matches,
384 drd_tool_error_name,
385 drd_tool_error_print_extra);
sewardjaf44c822007-11-25 14:01:38 +0000386}