blob: ac496f050d0321a112ab1a77c2a6acd6b6e0bb96 [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);
bartb515eb12008-03-07 18:52:38 +000082
bart3772a982008-03-15 08:11:03 +000083 descr1[0] = 0;
84 descr2[0] = 0;
bartf993b872008-04-01 18:19:50 +000085 VG_(get_data_description)(descr1, descr2, descr_size, dri->addr);
bart3772a982008-03-15 08:11:03 +000086 if (descr1[0] == 0)
87 {
bartff1252a2008-03-16 17:27:25 +000088 describe_malloced_addr(dri->addr, dri->size, &ai);
bart3772a982008-03-15 08:11:03 +000089 }
90 VG_(message)(Vg_UserMsg,
bartaa97a542008-03-16 17:57:01 +000091 "Conflicting %s by thread %d/%d at 0x%08lx size %ld",
bart3772a982008-03-15 08:11:03 +000092 dri->access_type == eStore ? "store" : "load",
bart354009c2008-03-16 10:42:33 +000093 DrdThreadIdToVgThreadId(dri->tid),
bartaa97a542008-03-16 17:57:01 +000094 dri->tid,
bart3772a982008-03-15 08:11:03 +000095 dri->addr,
96 dri->size);
97 VG_(pp_ExeContext)(VG_(get_error_where)(err));
98 if (descr1[0])
99 {
100 VG_(message)(Vg_UserMsg, "%s", descr1);
101 VG_(message)(Vg_UserMsg, "%s", descr2);
102 }
103 else if (ai.akind == eMallocd && ai.lastchange)
104 {
105 VG_(message)(Vg_UserMsg,
106 "Address 0x%lx is at offset %ld from 0x%lx."
107 " Allocation context:",
108 dri->addr, ai.rwoffset, dri->addr - ai.rwoffset);
109 VG_(pp_ExeContext)(ai.lastchange);
110 }
111 else
112 {
113 VG_(message)(Vg_UserMsg, "Allocation context: unknown.");
114 }
bart16d76e52008-03-18 17:08:08 +0000115 if (s_drd_show_conflicting_segments)
116 {
117 thread_report_conflicting_segments(dri->tid,
118 dri->addr, dri->size, dri->access_type);
119 }
bartf993b872008-04-01 18:19:50 +0000120
121 VG_(free)(descr2);
122 VG_(free)(descr1);
sewardjaf44c822007-11-25 14:01:38 +0000123}
124
125static Bool drd_tool_error_eq(VgRes res, Error* e1, Error* e2)
126{
bart3772a982008-03-15 08:11:03 +0000127 return False;
sewardjaf44c822007-11-25 14:01:38 +0000128}
129
130static void drd_tool_error_pp(Error* const e)
131{
bart3772a982008-03-15 08:11:03 +0000132 switch (VG_(get_error_kind)(e))
133 {
134 case DataRaceErr: {
135 drd_report_data_race2(e, VG_(get_error_extra)(e));
136 break;
137 }
138 case MutexErr: {
139 MutexErrInfo* p = (MutexErrInfo*)(VG_(get_error_extra)(e));
140 tl_assert(p);
bart6b717612008-03-24 09:29:38 +0000141 if (p->recursion_count >= 0)
142 {
143 VG_(message)(Vg_UserMsg,
144 "%s: mutex 0x%lx, recursion count %d, owner %d.",
145 VG_(get_error_string)(e),
146 p->mutex,
147 p->recursion_count,
148 p->owner);
149 }
150 else
151 {
152 VG_(message)(Vg_UserMsg,
bart52e82912008-03-24 19:31:33 +0000153 "The object at address 0x%lx is not a mutex.",
bart6b717612008-03-24 09:29:38 +0000154 p->mutex);
155 }
bart3772a982008-03-15 08:11:03 +0000156 VG_(pp_ExeContext)(VG_(get_error_where)(e));
157 break;
158 }
159 case CondErr: {
160 CondErrInfo* cdei =(CondErrInfo*)(VG_(get_error_extra)(e));
161 VG_(message)(Vg_UserMsg,
162 "%s: cond 0x%lx",
bartbe8a12c2008-03-17 18:36:55 +0000163 VG_(get_error_string)(e),
164 cdei->cond);
bart3772a982008-03-15 08:11:03 +0000165 VG_(pp_ExeContext)(VG_(get_error_where)(e));
166 break;
167 }
168 case CondRaceErr: {
169 CondRaceErrInfo* cei = (CondRaceErrInfo*)(VG_(get_error_extra)(e));
170 VG_(message)(Vg_UserMsg,
171 "Race condition: condition variable 0x%lx has been"
172 " signalled but the associated mutex 0x%lx is not locked"
173 " by the signalling thread",
174 cei->cond, cei->mutex);
175 VG_(pp_ExeContext)(VG_(get_error_where)(e));
176 break;
177 }
178 case CondDestrErr: {
179 CondDestrErrInfo* cdi = (CondDestrErrInfo*)(VG_(get_error_extra)(e));
180 VG_(message)(Vg_UserMsg,
bartaa97a542008-03-16 17:57:01 +0000181 "%s: cond 0x%lx, mutex 0x%lx locked by thread %d/%d",
bartbe8a12c2008-03-17 18:36:55 +0000182 VG_(get_error_string)(e),
bartaa97a542008-03-16 17:57:01 +0000183 cdi->cond, cdi->mutex,
184 DrdThreadIdToVgThreadId(cdi->tid), cdi->tid);
bart3772a982008-03-15 08:11:03 +0000185 VG_(pp_ExeContext)(VG_(get_error_where)(e));
186 break;
187 }
188 case SemaphoreErr: {
189 SemaphoreErrInfo* sei =(SemaphoreErrInfo*)(VG_(get_error_extra)(e));
190 tl_assert(sei);
191 VG_(message)(Vg_UserMsg,
192 "%s: semaphore 0x%lx",
193 VG_(get_error_string)(e),
194 sei->semaphore);
195 VG_(pp_ExeContext)(VG_(get_error_where)(e));
196 break;
197 }
198 case BarrierErr: {
199 BarrierErrInfo* sei =(BarrierErrInfo*)(VG_(get_error_extra)(e));
200 tl_assert(sei);
201 VG_(message)(Vg_UserMsg,
202 "%s: barrier 0x%lx",
203 VG_(get_error_string)(e),
204 sei->barrier);
205 VG_(pp_ExeContext)(VG_(get_error_where)(e));
206 break;
207 }
208 case RwlockErr: {
209 RwlockErrInfo* p = (RwlockErrInfo*)(VG_(get_error_extra)(e));
210 tl_assert(p);
211 VG_(message)(Vg_UserMsg,
212 "%s: rwlock 0x%lx.",
213 VG_(get_error_string)(e),
214 p->rwlock);
215 VG_(pp_ExeContext)(VG_(get_error_where)(e));
216 break;
217 }
218 case GenericErr: {
219 //GenericErrInfo* gei =(GenericErrInfo*)(VG_(get_error_extra)(e));
220 VG_(message)(Vg_UserMsg, "%s", VG_(get_error_string)(e));
221 VG_(pp_ExeContext)(VG_(get_error_where)(e));
222 break;
223 }
224 default:
225 VG_(message)(Vg_UserMsg,
226 "%s",
227 VG_(get_error_string)(e));
228 VG_(pp_ExeContext)(VG_(get_error_where)(e));
229 break;
230 }
sewardjaf44c822007-11-25 14:01:38 +0000231}
232
233static UInt drd_tool_error_update_extra(Error* e)
234{
bart3772a982008-03-15 08:11:03 +0000235 switch (VG_(get_error_kind)(e))
236 {
237 case DataRaceErr:
238 return sizeof(DataRaceErrInfo);
239 case MutexErr:
240 return sizeof(MutexErrInfo);
241 case CondErr:
242 return sizeof(CondErrInfo);
243 case CondRaceErr:
244 return sizeof(CondRaceErrInfo);
245 case CondDestrErr:
246 return sizeof(CondDestrErrInfo);
247 case SemaphoreErr:
248 return sizeof(SemaphoreErrInfo);
249 case BarrierErr:
250 return sizeof(BarrierErrInfo);
251 case RwlockErr:
252 return sizeof(RwlockErrInfo);
253 case GenericErr:
254 return sizeof(GenericErrInfo);
255 default:
256 tl_assert(False);
257 break;
258 }
sewardjaf44c822007-11-25 14:01:38 +0000259}
260
261static Bool drd_tool_error_recog(Char* const name, Supp* const supp)
262{
bart5fc70e62008-03-23 07:54:02 +0000263 SuppKind skind = 0;
sewardjaf44c822007-11-25 14:01:38 +0000264
bart5fc70e62008-03-23 07:54:02 +0000265 if (VG_(strcmp)(name, STR_DataRaceErr) == 0)
266 ;
267 else if (VG_(strcmp)(name, STR_MutexErr) == 0)
268 ;
269 else if (VG_(strcmp)(name, STR_CondErr) == 0)
270 ;
271 else if (VG_(strcmp)(name, STR_CondRaceErr) == 0)
272 ;
273 else if (VG_(strcmp)(name, STR_CondDestrErr) == 0)
274 ;
275 else if (VG_(strcmp)(name, STR_SemaphoreErr) == 0)
276 ;
277 else if (VG_(strcmp)(name, STR_BarrierErr) == 0)
278 ;
279 else if (VG_(strcmp)(name, STR_RwlockErr) == 0)
280 ;
281 else if (VG_(strcmp)(name, STR_GenericErr) == 0)
282 ;
bart3772a982008-03-15 08:11:03 +0000283 else
284 return False;
sewardjaf44c822007-11-25 14:01:38 +0000285
bart3772a982008-03-15 08:11:03 +0000286 VG_(set_supp_kind)(supp, skind);
287 return True;
sewardjaf44c822007-11-25 14:01:38 +0000288}
289
290static Bool drd_tool_error_read_extra(Int fd, Char* buf, Int nBuf, Supp* supp)
291{
bart3772a982008-03-15 08:11:03 +0000292 return True;
sewardjaf44c822007-11-25 14:01:38 +0000293}
294
295static Bool drd_tool_error_matches(Error* const e, Supp* const supp)
296{
bart3772a982008-03-15 08:11:03 +0000297 switch (VG_(get_supp_kind)(supp))
298 {
299 }
300 return True;
sewardjaf44c822007-11-25 14:01:38 +0000301}
302
303static Char* drd_tool_error_name(Error* e)
304{
bart3772a982008-03-15 08:11:03 +0000305 switch (VG_(get_error_kind)(e))
306 {
bart5fc70e62008-03-23 07:54:02 +0000307 case DataRaceErr: return VGAPPEND(STR_, DataRaceErr);
308 case MutexErr: return VGAPPEND(STR_, MutexErr);
309 case CondErr: return VGAPPEND(STR_, CondErr);
310 case CondRaceErr: return VGAPPEND(STR_, CondRaceErr);
311 case CondDestrErr: return VGAPPEND(STR_, CondDestrErr);
312 case SemaphoreErr: return VGAPPEND(STR_, SemaphoreErr);
313 case BarrierErr: return VGAPPEND(STR_, BarrierErr);
314 case RwlockErr: return VGAPPEND(STR_, RwlockErr);
315 case GenericErr: return VGAPPEND(STR_, GenericErr);
bart3772a982008-03-15 08:11:03 +0000316 default:
317 tl_assert(0);
318 }
319 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000320}
321
322static void drd_tool_error_print_extra(Error* e)
323{
bart3772a982008-03-15 08:11:03 +0000324 switch (VG_(get_error_kind)(e))
325 {
326 // VG_(printf)(" %s\n", VG_(get_error_string)(err));
327 }
sewardjaf44c822007-11-25 14:01:38 +0000328}
329
330void drd_register_error_handlers(void)
331{
bart3772a982008-03-15 08:11:03 +0000332 // Tool error reporting.
333 VG_(needs_tool_errors)(drd_tool_error_eq,
334 drd_tool_error_pp,
335 True,
336 drd_tool_error_update_extra,
337 drd_tool_error_recog,
338 drd_tool_error_read_extra,
339 drd_tool_error_matches,
340 drd_tool_error_name,
341 drd_tool_error_print_extra);
sewardjaf44c822007-11-25 14:01:38 +0000342}