blob: bb198a168b9dbb1c0957f34bc101c0f684e0e839 [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
26#include "drd_error.h"
27#include "drd_malloc_wrappers.h"
28#include "drd_mutex.h" // struct mutex_info
29#include "drd_suppression.h" // drd_start_suppression()
30#include "pub_drd_bitmap.h" // LHS_W, ...
31#include "pub_tool_vki.h"
32#include "pub_tool_basics.h"
33#include "pub_tool_libcassert.h" // tl_assert()
34#include "pub_tool_libcbase.h" // strlen()
35#include "pub_tool_libcfile.h" // VG_(get_startup_wd)()
36#include "pub_tool_libcprint.h" // VG_(printf)()
37#include "pub_tool_machine.h"
bartf993b872008-04-01 18:19:50 +000038#include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free)
sewardjaf44c822007-11-25 14:01:38 +000039#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
40#include "pub_tool_tooliface.h" // VG_(needs_tool_errors)()
41
42
bart16d76e52008-03-18 17:08:08 +000043/* Local variables. */
44
45static Bool s_drd_show_conflicting_segments = True;
46
47
48void set_show_conflicting_segments(const Bool scs)
49{
50 s_drd_show_conflicting_segments = scs;
51}
52
bartff1252a2008-03-16 17:27:25 +000053/* Describe a data address range [a,a+len[ as good as possible, for error */
sewardjaf44c822007-11-25 14:01:38 +000054/* messages, putting the result in ai. */
bartff1252a2008-03-16 17:27:25 +000055static
56void describe_malloced_addr(Addr const a, SizeT const len, AddrInfo* const ai)
sewardjaf44c822007-11-25 14:01:38 +000057{
bartff1252a2008-03-16 17:27:25 +000058 Addr data;
sewardjaf44c822007-11-25 14:01:38 +000059
bartff1252a2008-03-16 17:27:25 +000060 if (drd_heap_addrinfo(a, &data, &ai->size, &ai->lastchange))
bart3772a982008-03-15 08:11:03 +000061 {
bartff1252a2008-03-16 17:27:25 +000062 ai->akind = eMallocd;
63 ai->rwoffset = a - data;
bart3772a982008-03-15 08:11:03 +000064 }
bartff1252a2008-03-16 17:27:25 +000065 else
bart3772a982008-03-15 08:11:03 +000066 {
bartff1252a2008-03-16 17:27:25 +000067 ai->akind = eUnknown;
bart3772a982008-03-15 08:11:03 +000068 }
sewardjaf44c822007-11-25 14:01:38 +000069}
70
sewardjaf44c822007-11-25 14:01:38 +000071static
72void drd_report_data_race2(Error* const err, const DataRaceErrInfo* const dri)
73{
bart3772a982008-03-15 08:11:03 +000074 AddrInfo ai;
bartf993b872008-04-01 18:19:50 +000075 const unsigned descr_size = 256;
76 Char* descr1 = VG_(malloc)(descr_size);
77 Char* descr2 = VG_(malloc)(descr_size);
sewardjaf44c822007-11-25 14:01:38 +000078
bart3772a982008-03-15 08:11:03 +000079 tl_assert(dri);
80 tl_assert(dri->addr);
81 tl_assert(dri->size > 0);
bartfdd8d4e2008-04-01 18:38:29 +000082 tl_assert(descr1);
83 tl_assert(descr2);
bartb515eb12008-03-07 18:52:38 +000084
bart3772a982008-03-15 08:11:03 +000085 descr1[0] = 0;
86 descr2[0] = 0;
bartf993b872008-04-01 18:19:50 +000087 VG_(get_data_description)(descr1, descr2, descr_size, dri->addr);
bart3772a982008-03-15 08:11:03 +000088 if (descr1[0] == 0)
89 {
bartff1252a2008-03-16 17:27:25 +000090 describe_malloced_addr(dri->addr, dri->size, &ai);
bart3772a982008-03-15 08:11:03 +000091 }
92 VG_(message)(Vg_UserMsg,
bartaa97a542008-03-16 17:57:01 +000093 "Conflicting %s by thread %d/%d at 0x%08lx size %ld",
bart3772a982008-03-15 08:11:03 +000094 dri->access_type == eStore ? "store" : "load",
bart354009c2008-03-16 10:42:33 +000095 DrdThreadIdToVgThreadId(dri->tid),
bartaa97a542008-03-16 17:57:01 +000096 dri->tid,
bart3772a982008-03-15 08:11:03 +000097 dri->addr,
98 dri->size);
99 VG_(pp_ExeContext)(VG_(get_error_where)(err));
100 if (descr1[0])
101 {
102 VG_(message)(Vg_UserMsg, "%s", descr1);
103 VG_(message)(Vg_UserMsg, "%s", descr2);
104 }
105 else if (ai.akind == eMallocd && ai.lastchange)
106 {
107 VG_(message)(Vg_UserMsg,
108 "Address 0x%lx is at offset %ld from 0x%lx."
109 " Allocation context:",
110 dri->addr, ai.rwoffset, dri->addr - ai.rwoffset);
111 VG_(pp_ExeContext)(ai.lastchange);
112 }
113 else
114 {
115 VG_(message)(Vg_UserMsg, "Allocation context: unknown.");
116 }
bart16d76e52008-03-18 17:08:08 +0000117 if (s_drd_show_conflicting_segments)
118 {
119 thread_report_conflicting_segments(dri->tid,
120 dri->addr, dri->size, dri->access_type);
121 }
bartf993b872008-04-01 18:19:50 +0000122
123 VG_(free)(descr2);
124 VG_(free)(descr1);
sewardjaf44c822007-11-25 14:01:38 +0000125}
126
127static Bool drd_tool_error_eq(VgRes res, Error* e1, Error* e2)
128{
bart3772a982008-03-15 08:11:03 +0000129 return False;
sewardjaf44c822007-11-25 14:01:38 +0000130}
131
132static void drd_tool_error_pp(Error* const e)
133{
bart3772a982008-03-15 08:11:03 +0000134 switch (VG_(get_error_kind)(e))
135 {
136 case DataRaceErr: {
137 drd_report_data_race2(e, VG_(get_error_extra)(e));
138 break;
139 }
140 case MutexErr: {
141 MutexErrInfo* p = (MutexErrInfo*)(VG_(get_error_extra)(e));
142 tl_assert(p);
bart6b717612008-03-24 09:29:38 +0000143 if (p->recursion_count >= 0)
144 {
145 VG_(message)(Vg_UserMsg,
146 "%s: mutex 0x%lx, recursion count %d, owner %d.",
147 VG_(get_error_string)(e),
148 p->mutex,
149 p->recursion_count,
150 p->owner);
151 }
152 else
153 {
154 VG_(message)(Vg_UserMsg,
bart52e82912008-03-24 19:31:33 +0000155 "The object at address 0x%lx is not a mutex.",
bart6b717612008-03-24 09:29:38 +0000156 p->mutex);
157 }
bart3772a982008-03-15 08:11:03 +0000158 VG_(pp_ExeContext)(VG_(get_error_where)(e));
159 break;
160 }
161 case CondErr: {
162 CondErrInfo* cdei =(CondErrInfo*)(VG_(get_error_extra)(e));
163 VG_(message)(Vg_UserMsg,
164 "%s: cond 0x%lx",
bartbe8a12c2008-03-17 18:36:55 +0000165 VG_(get_error_string)(e),
166 cdei->cond);
bart3772a982008-03-15 08:11:03 +0000167 VG_(pp_ExeContext)(VG_(get_error_where)(e));
168 break;
169 }
170 case CondRaceErr: {
171 CondRaceErrInfo* cei = (CondRaceErrInfo*)(VG_(get_error_extra)(e));
172 VG_(message)(Vg_UserMsg,
173 "Race condition: condition variable 0x%lx has been"
174 " signalled but the associated mutex 0x%lx is not locked"
175 " by the signalling thread",
176 cei->cond, cei->mutex);
177 VG_(pp_ExeContext)(VG_(get_error_where)(e));
178 break;
179 }
180 case CondDestrErr: {
181 CondDestrErrInfo* cdi = (CondDestrErrInfo*)(VG_(get_error_extra)(e));
182 VG_(message)(Vg_UserMsg,
bartaa97a542008-03-16 17:57:01 +0000183 "%s: cond 0x%lx, mutex 0x%lx locked by thread %d/%d",
bartbe8a12c2008-03-17 18:36:55 +0000184 VG_(get_error_string)(e),
bartaa97a542008-03-16 17:57:01 +0000185 cdi->cond, cdi->mutex,
186 DrdThreadIdToVgThreadId(cdi->tid), cdi->tid);
bart3772a982008-03-15 08:11:03 +0000187 VG_(pp_ExeContext)(VG_(get_error_where)(e));
188 break;
189 }
190 case SemaphoreErr: {
191 SemaphoreErrInfo* sei =(SemaphoreErrInfo*)(VG_(get_error_extra)(e));
192 tl_assert(sei);
193 VG_(message)(Vg_UserMsg,
194 "%s: semaphore 0x%lx",
195 VG_(get_error_string)(e),
196 sei->semaphore);
197 VG_(pp_ExeContext)(VG_(get_error_where)(e));
198 break;
199 }
200 case BarrierErr: {
201 BarrierErrInfo* sei =(BarrierErrInfo*)(VG_(get_error_extra)(e));
202 tl_assert(sei);
203 VG_(message)(Vg_UserMsg,
204 "%s: barrier 0x%lx",
205 VG_(get_error_string)(e),
206 sei->barrier);
207 VG_(pp_ExeContext)(VG_(get_error_where)(e));
208 break;
209 }
210 case RwlockErr: {
211 RwlockErrInfo* p = (RwlockErrInfo*)(VG_(get_error_extra)(e));
212 tl_assert(p);
213 VG_(message)(Vg_UserMsg,
214 "%s: rwlock 0x%lx.",
215 VG_(get_error_string)(e),
216 p->rwlock);
217 VG_(pp_ExeContext)(VG_(get_error_where)(e));
218 break;
219 }
bart9d5b7962008-05-14 12:25:00 +0000220 case HoldtimeErr: {
221 HoldtimeErrInfo* p =(HoldtimeErrInfo*)(VG_(get_error_extra)(e));
222 tl_assert(p);
223 tl_assert(p->acquired_at);
224 VG_(message)(Vg_UserMsg, "Acquired at:");
225 VG_(pp_ExeContext)(p->acquired_at);
226 VG_(message)(Vg_UserMsg,
227 "Lock on %s 0x%lx was held during %d ms (threshold: %d ms).",
228 VG_(get_error_string)(e),
229 p->synchronization_object,
230 p->hold_time_ms,
231 p->threshold_ms);
232 VG_(pp_ExeContext)(VG_(get_error_where)(e));
233 break;
234 }
bart3772a982008-03-15 08:11:03 +0000235 case GenericErr: {
236 //GenericErrInfo* gei =(GenericErrInfo*)(VG_(get_error_extra)(e));
237 VG_(message)(Vg_UserMsg, "%s", VG_(get_error_string)(e));
238 VG_(pp_ExeContext)(VG_(get_error_where)(e));
239 break;
240 }
241 default:
242 VG_(message)(Vg_UserMsg,
243 "%s",
244 VG_(get_error_string)(e));
245 VG_(pp_ExeContext)(VG_(get_error_where)(e));
246 break;
247 }
sewardjaf44c822007-11-25 14:01:38 +0000248}
249
250static UInt drd_tool_error_update_extra(Error* e)
251{
bart3772a982008-03-15 08:11:03 +0000252 switch (VG_(get_error_kind)(e))
253 {
254 case DataRaceErr:
255 return sizeof(DataRaceErrInfo);
256 case MutexErr:
257 return sizeof(MutexErrInfo);
258 case CondErr:
259 return sizeof(CondErrInfo);
260 case CondRaceErr:
261 return sizeof(CondRaceErrInfo);
262 case CondDestrErr:
263 return sizeof(CondDestrErrInfo);
264 case SemaphoreErr:
265 return sizeof(SemaphoreErrInfo);
266 case BarrierErr:
267 return sizeof(BarrierErrInfo);
268 case RwlockErr:
269 return sizeof(RwlockErrInfo);
bart9d5b7962008-05-14 12:25:00 +0000270 case HoldtimeErr:
271 return sizeof(HoldtimeErrInfo);
bart3772a982008-03-15 08:11:03 +0000272 case GenericErr:
273 return sizeof(GenericErrInfo);
274 default:
275 tl_assert(False);
276 break;
277 }
sewardjaf44c822007-11-25 14:01:38 +0000278}
279
280static Bool drd_tool_error_recog(Char* const name, Supp* const supp)
281{
bart5fc70e62008-03-23 07:54:02 +0000282 SuppKind skind = 0;
sewardjaf44c822007-11-25 14:01:38 +0000283
bart5fc70e62008-03-23 07:54:02 +0000284 if (VG_(strcmp)(name, STR_DataRaceErr) == 0)
285 ;
286 else if (VG_(strcmp)(name, STR_MutexErr) == 0)
287 ;
288 else if (VG_(strcmp)(name, STR_CondErr) == 0)
289 ;
290 else if (VG_(strcmp)(name, STR_CondRaceErr) == 0)
291 ;
292 else if (VG_(strcmp)(name, STR_CondDestrErr) == 0)
293 ;
294 else if (VG_(strcmp)(name, STR_SemaphoreErr) == 0)
295 ;
296 else if (VG_(strcmp)(name, STR_BarrierErr) == 0)
297 ;
298 else if (VG_(strcmp)(name, STR_RwlockErr) == 0)
299 ;
bart9d5b7962008-05-14 12:25:00 +0000300 else if (VG_(strcmp)(name, STR_HoldtimeErr) == 0)
301 ;
bart5fc70e62008-03-23 07:54:02 +0000302 else if (VG_(strcmp)(name, STR_GenericErr) == 0)
303 ;
bart3772a982008-03-15 08:11:03 +0000304 else
305 return False;
sewardjaf44c822007-11-25 14:01:38 +0000306
bart3772a982008-03-15 08:11:03 +0000307 VG_(set_supp_kind)(supp, skind);
308 return True;
sewardjaf44c822007-11-25 14:01:38 +0000309}
310
311static Bool drd_tool_error_read_extra(Int fd, Char* buf, Int nBuf, Supp* supp)
312{
bart3772a982008-03-15 08:11:03 +0000313 return True;
sewardjaf44c822007-11-25 14:01:38 +0000314}
315
316static Bool drd_tool_error_matches(Error* const e, Supp* const supp)
317{
bart3772a982008-03-15 08:11:03 +0000318 switch (VG_(get_supp_kind)(supp))
319 {
320 }
321 return True;
sewardjaf44c822007-11-25 14:01:38 +0000322}
323
324static Char* drd_tool_error_name(Error* e)
325{
bart3772a982008-03-15 08:11:03 +0000326 switch (VG_(get_error_kind)(e))
327 {
bart5fc70e62008-03-23 07:54:02 +0000328 case DataRaceErr: return VGAPPEND(STR_, DataRaceErr);
329 case MutexErr: return VGAPPEND(STR_, MutexErr);
330 case CondErr: return VGAPPEND(STR_, CondErr);
331 case CondRaceErr: return VGAPPEND(STR_, CondRaceErr);
332 case CondDestrErr: return VGAPPEND(STR_, CondDestrErr);
333 case SemaphoreErr: return VGAPPEND(STR_, SemaphoreErr);
334 case BarrierErr: return VGAPPEND(STR_, BarrierErr);
335 case RwlockErr: return VGAPPEND(STR_, RwlockErr);
bart9d5b7962008-05-14 12:25:00 +0000336 case HoldtimeErr: return VGAPPEND(STR_, HoldtimeErr);
bart5fc70e62008-03-23 07:54:02 +0000337 case GenericErr: return VGAPPEND(STR_, GenericErr);
bart3772a982008-03-15 08:11:03 +0000338 default:
339 tl_assert(0);
340 }
341 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000342}
343
344static void drd_tool_error_print_extra(Error* e)
345{
bart3772a982008-03-15 08:11:03 +0000346 switch (VG_(get_error_kind)(e))
347 {
348 // VG_(printf)(" %s\n", VG_(get_error_string)(err));
349 }
sewardjaf44c822007-11-25 14:01:38 +0000350}
351
352void drd_register_error_handlers(void)
353{
bart3772a982008-03-15 08:11:03 +0000354 // Tool error reporting.
355 VG_(needs_tool_errors)(drd_tool_error_eq,
356 drd_tool_error_pp,
357 True,
358 drd_tool_error_update_extra,
359 drd_tool_error_recog,
360 drd_tool_error_read_extra,
361 drd_tool_error_matches,
362 drd_tool_error_name,
363 drd_tool_error_print_extra);
sewardjaf44c822007-11-25 14:01:38 +0000364}