blob: 8851f612ae92b31de5a21791d6e4d9b915cd2f2f [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"
38#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
39#include "pub_tool_tooliface.h" // VG_(needs_tool_errors)()
40
41
bart16d76e52008-03-18 17:08:08 +000042/* Local variables. */
43
44static Bool s_drd_show_conflicting_segments = True;
45
46
47void set_show_conflicting_segments(const Bool scs)
48{
49 s_drd_show_conflicting_segments = scs;
50}
51
bartff1252a2008-03-16 17:27:25 +000052/* Describe a data address range [a,a+len[ as good as possible, for error */
sewardjaf44c822007-11-25 14:01:38 +000053/* messages, putting the result in ai. */
bartff1252a2008-03-16 17:27:25 +000054static
55void describe_malloced_addr(Addr const a, SizeT const len, AddrInfo* const ai)
sewardjaf44c822007-11-25 14:01:38 +000056{
bartff1252a2008-03-16 17:27:25 +000057 Addr data;
sewardjaf44c822007-11-25 14:01:38 +000058
bartff1252a2008-03-16 17:27:25 +000059 if (drd_heap_addrinfo(a, &data, &ai->size, &ai->lastchange))
bart3772a982008-03-15 08:11:03 +000060 {
bartff1252a2008-03-16 17:27:25 +000061 ai->akind = eMallocd;
62 ai->rwoffset = a - data;
bart3772a982008-03-15 08:11:03 +000063 }
bartff1252a2008-03-16 17:27:25 +000064 else
bart3772a982008-03-15 08:11:03 +000065 {
bartff1252a2008-03-16 17:27:25 +000066 ai->akind = eUnknown;
bart3772a982008-03-15 08:11:03 +000067 }
sewardjaf44c822007-11-25 14:01:38 +000068}
69
sewardjaf44c822007-11-25 14:01:38 +000070static
71void drd_report_data_race2(Error* const err, const DataRaceErrInfo* const dri)
72{
bart3772a982008-03-15 08:11:03 +000073 AddrInfo ai;
74 Char descr1[256];
75 Char descr2[256];
sewardjaf44c822007-11-25 14:01:38 +000076
bart3772a982008-03-15 08:11:03 +000077 tl_assert(dri);
78 tl_assert(dri->addr);
79 tl_assert(dri->size > 0);
bartb515eb12008-03-07 18:52:38 +000080
bart3772a982008-03-15 08:11:03 +000081 descr1[0] = 0;
82 descr2[0] = 0;
83 VG_(get_data_description)(descr1, descr2, sizeof(descr1), dri->addr);
84 if (descr1[0] == 0)
85 {
bartff1252a2008-03-16 17:27:25 +000086 describe_malloced_addr(dri->addr, dri->size, &ai);
bart3772a982008-03-15 08:11:03 +000087 }
88 VG_(message)(Vg_UserMsg,
bartaa97a542008-03-16 17:57:01 +000089 "Conflicting %s by thread %d/%d at 0x%08lx size %ld",
bart3772a982008-03-15 08:11:03 +000090 dri->access_type == eStore ? "store" : "load",
bart354009c2008-03-16 10:42:33 +000091 DrdThreadIdToVgThreadId(dri->tid),
bartaa97a542008-03-16 17:57:01 +000092 dri->tid,
bart3772a982008-03-15 08:11:03 +000093 dri->addr,
94 dri->size);
95 VG_(pp_ExeContext)(VG_(get_error_where)(err));
96 if (descr1[0])
97 {
98 VG_(message)(Vg_UserMsg, "%s", descr1);
99 VG_(message)(Vg_UserMsg, "%s", descr2);
100 }
101 else if (ai.akind == eMallocd && ai.lastchange)
102 {
103 VG_(message)(Vg_UserMsg,
104 "Address 0x%lx is at offset %ld from 0x%lx."
105 " Allocation context:",
106 dri->addr, ai.rwoffset, dri->addr - ai.rwoffset);
107 VG_(pp_ExeContext)(ai.lastchange);
108 }
109 else
110 {
111 VG_(message)(Vg_UserMsg, "Allocation context: unknown.");
112 }
bart16d76e52008-03-18 17:08:08 +0000113 if (s_drd_show_conflicting_segments)
114 {
115 thread_report_conflicting_segments(dri->tid,
116 dri->addr, dri->size, dri->access_type);
117 }
sewardjaf44c822007-11-25 14:01:38 +0000118}
119
120static Bool drd_tool_error_eq(VgRes res, Error* e1, Error* e2)
121{
bart3772a982008-03-15 08:11:03 +0000122 return False;
sewardjaf44c822007-11-25 14:01:38 +0000123}
124
125static void drd_tool_error_pp(Error* const e)
126{
bart3772a982008-03-15 08:11:03 +0000127 switch (VG_(get_error_kind)(e))
128 {
129 case DataRaceErr: {
130 drd_report_data_race2(e, VG_(get_error_extra)(e));
131 break;
132 }
133 case MutexErr: {
134 MutexErrInfo* p = (MutexErrInfo*)(VG_(get_error_extra)(e));
135 tl_assert(p);
bart6b717612008-03-24 09:29:38 +0000136 if (p->recursion_count >= 0)
137 {
138 VG_(message)(Vg_UserMsg,
139 "%s: mutex 0x%lx, recursion count %d, owner %d.",
140 VG_(get_error_string)(e),
141 p->mutex,
142 p->recursion_count,
143 p->owner);
144 }
145 else
146 {
147 VG_(message)(Vg_UserMsg,
bart52e82912008-03-24 19:31:33 +0000148 "The object at address 0x%lx is not a mutex.",
bart6b717612008-03-24 09:29:38 +0000149 p->mutex);
150 }
bart3772a982008-03-15 08:11:03 +0000151 VG_(pp_ExeContext)(VG_(get_error_where)(e));
152 break;
153 }
154 case CondErr: {
155 CondErrInfo* cdei =(CondErrInfo*)(VG_(get_error_extra)(e));
156 VG_(message)(Vg_UserMsg,
157 "%s: cond 0x%lx",
bartbe8a12c2008-03-17 18:36:55 +0000158 VG_(get_error_string)(e),
159 cdei->cond);
bart3772a982008-03-15 08:11:03 +0000160 VG_(pp_ExeContext)(VG_(get_error_where)(e));
161 break;
162 }
163 case CondRaceErr: {
164 CondRaceErrInfo* cei = (CondRaceErrInfo*)(VG_(get_error_extra)(e));
165 VG_(message)(Vg_UserMsg,
166 "Race condition: condition variable 0x%lx has been"
167 " signalled but the associated mutex 0x%lx is not locked"
168 " by the signalling thread",
169 cei->cond, cei->mutex);
170 VG_(pp_ExeContext)(VG_(get_error_where)(e));
171 break;
172 }
173 case CondDestrErr: {
174 CondDestrErrInfo* cdi = (CondDestrErrInfo*)(VG_(get_error_extra)(e));
175 VG_(message)(Vg_UserMsg,
bartaa97a542008-03-16 17:57:01 +0000176 "%s: cond 0x%lx, mutex 0x%lx locked by thread %d/%d",
bartbe8a12c2008-03-17 18:36:55 +0000177 VG_(get_error_string)(e),
bartaa97a542008-03-16 17:57:01 +0000178 cdi->cond, cdi->mutex,
179 DrdThreadIdToVgThreadId(cdi->tid), cdi->tid);
bart3772a982008-03-15 08:11:03 +0000180 VG_(pp_ExeContext)(VG_(get_error_where)(e));
181 break;
182 }
183 case SemaphoreErr: {
184 SemaphoreErrInfo* sei =(SemaphoreErrInfo*)(VG_(get_error_extra)(e));
185 tl_assert(sei);
186 VG_(message)(Vg_UserMsg,
187 "%s: semaphore 0x%lx",
188 VG_(get_error_string)(e),
189 sei->semaphore);
190 VG_(pp_ExeContext)(VG_(get_error_where)(e));
191 break;
192 }
193 case BarrierErr: {
194 BarrierErrInfo* sei =(BarrierErrInfo*)(VG_(get_error_extra)(e));
195 tl_assert(sei);
196 VG_(message)(Vg_UserMsg,
197 "%s: barrier 0x%lx",
198 VG_(get_error_string)(e),
199 sei->barrier);
200 VG_(pp_ExeContext)(VG_(get_error_where)(e));
201 break;
202 }
203 case RwlockErr: {
204 RwlockErrInfo* p = (RwlockErrInfo*)(VG_(get_error_extra)(e));
205 tl_assert(p);
206 VG_(message)(Vg_UserMsg,
207 "%s: rwlock 0x%lx.",
208 VG_(get_error_string)(e),
209 p->rwlock);
210 VG_(pp_ExeContext)(VG_(get_error_where)(e));
211 break;
212 }
213 case GenericErr: {
214 //GenericErrInfo* gei =(GenericErrInfo*)(VG_(get_error_extra)(e));
215 VG_(message)(Vg_UserMsg, "%s", VG_(get_error_string)(e));
216 VG_(pp_ExeContext)(VG_(get_error_where)(e));
217 break;
218 }
219 default:
220 VG_(message)(Vg_UserMsg,
221 "%s",
222 VG_(get_error_string)(e));
223 VG_(pp_ExeContext)(VG_(get_error_where)(e));
224 break;
225 }
sewardjaf44c822007-11-25 14:01:38 +0000226}
227
228static UInt drd_tool_error_update_extra(Error* e)
229{
bart3772a982008-03-15 08:11:03 +0000230 switch (VG_(get_error_kind)(e))
231 {
232 case DataRaceErr:
233 return sizeof(DataRaceErrInfo);
234 case MutexErr:
235 return sizeof(MutexErrInfo);
236 case CondErr:
237 return sizeof(CondErrInfo);
238 case CondRaceErr:
239 return sizeof(CondRaceErrInfo);
240 case CondDestrErr:
241 return sizeof(CondDestrErrInfo);
242 case SemaphoreErr:
243 return sizeof(SemaphoreErrInfo);
244 case BarrierErr:
245 return sizeof(BarrierErrInfo);
246 case RwlockErr:
247 return sizeof(RwlockErrInfo);
248 case GenericErr:
249 return sizeof(GenericErrInfo);
250 default:
251 tl_assert(False);
252 break;
253 }
sewardjaf44c822007-11-25 14:01:38 +0000254}
255
256static Bool drd_tool_error_recog(Char* const name, Supp* const supp)
257{
bart5fc70e62008-03-23 07:54:02 +0000258 SuppKind skind = 0;
sewardjaf44c822007-11-25 14:01:38 +0000259
bart5fc70e62008-03-23 07:54:02 +0000260 if (VG_(strcmp)(name, STR_DataRaceErr) == 0)
261 ;
262 else if (VG_(strcmp)(name, STR_MutexErr) == 0)
263 ;
264 else if (VG_(strcmp)(name, STR_CondErr) == 0)
265 ;
266 else if (VG_(strcmp)(name, STR_CondRaceErr) == 0)
267 ;
268 else if (VG_(strcmp)(name, STR_CondDestrErr) == 0)
269 ;
270 else if (VG_(strcmp)(name, STR_SemaphoreErr) == 0)
271 ;
272 else if (VG_(strcmp)(name, STR_BarrierErr) == 0)
273 ;
274 else if (VG_(strcmp)(name, STR_RwlockErr) == 0)
275 ;
276 else if (VG_(strcmp)(name, STR_GenericErr) == 0)
277 ;
bart3772a982008-03-15 08:11:03 +0000278 else
279 return False;
sewardjaf44c822007-11-25 14:01:38 +0000280
bart3772a982008-03-15 08:11:03 +0000281 VG_(set_supp_kind)(supp, skind);
282 return True;
sewardjaf44c822007-11-25 14:01:38 +0000283}
284
285static Bool drd_tool_error_read_extra(Int fd, Char* buf, Int nBuf, Supp* supp)
286{
bart3772a982008-03-15 08:11:03 +0000287 return True;
sewardjaf44c822007-11-25 14:01:38 +0000288}
289
290static Bool drd_tool_error_matches(Error* const e, Supp* const supp)
291{
bart3772a982008-03-15 08:11:03 +0000292 switch (VG_(get_supp_kind)(supp))
293 {
294 }
295 return True;
sewardjaf44c822007-11-25 14:01:38 +0000296}
297
298static Char* drd_tool_error_name(Error* e)
299{
bart3772a982008-03-15 08:11:03 +0000300 switch (VG_(get_error_kind)(e))
301 {
bart5fc70e62008-03-23 07:54:02 +0000302 case DataRaceErr: return VGAPPEND(STR_, DataRaceErr);
303 case MutexErr: return VGAPPEND(STR_, MutexErr);
304 case CondErr: return VGAPPEND(STR_, CondErr);
305 case CondRaceErr: return VGAPPEND(STR_, CondRaceErr);
306 case CondDestrErr: return VGAPPEND(STR_, CondDestrErr);
307 case SemaphoreErr: return VGAPPEND(STR_, SemaphoreErr);
308 case BarrierErr: return VGAPPEND(STR_, BarrierErr);
309 case RwlockErr: return VGAPPEND(STR_, RwlockErr);
310 case GenericErr: return VGAPPEND(STR_, GenericErr);
bart3772a982008-03-15 08:11:03 +0000311 default:
312 tl_assert(0);
313 }
314 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000315}
316
317static void drd_tool_error_print_extra(Error* e)
318{
bart3772a982008-03-15 08:11:03 +0000319 switch (VG_(get_error_kind)(e))
320 {
321 // VG_(printf)(" %s\n", VG_(get_error_string)(err));
322 }
sewardjaf44c822007-11-25 14:01:38 +0000323}
324
325void drd_register_error_handlers(void)
326{
bart3772a982008-03-15 08:11:03 +0000327 // Tool error reporting.
328 VG_(needs_tool_errors)(drd_tool_error_eq,
329 drd_tool_error_pp,
330 True,
331 drd_tool_error_update_extra,
332 drd_tool_error_recog,
333 drd_tool_error_read_extra,
334 drd_tool_error_matches,
335 drd_tool_error_name,
336 drd_tool_error_print_extra);
sewardjaf44c822007-11-25 14:01:38 +0000337}