blob: 4389e7bf238d77c910966677b4c545ba93bbbab0 [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);
136 VG_(message)(Vg_UserMsg,
137 "%s: mutex 0x%lx, recursion count %d, owner %d.",
138 VG_(get_error_string)(e),
139 p->mutex,
140 p->recursion_count,
141 p->owner);
142 VG_(pp_ExeContext)(VG_(get_error_where)(e));
143 break;
144 }
145 case CondErr: {
146 CondErrInfo* cdei =(CondErrInfo*)(VG_(get_error_extra)(e));
147 VG_(message)(Vg_UserMsg,
148 "%s: cond 0x%lx",
bartbe8a12c2008-03-17 18:36:55 +0000149 VG_(get_error_string)(e),
150 cdei->cond);
bart3772a982008-03-15 08:11:03 +0000151 VG_(pp_ExeContext)(VG_(get_error_where)(e));
152 break;
153 }
154 case CondRaceErr: {
155 CondRaceErrInfo* cei = (CondRaceErrInfo*)(VG_(get_error_extra)(e));
156 VG_(message)(Vg_UserMsg,
157 "Race condition: condition variable 0x%lx has been"
158 " signalled but the associated mutex 0x%lx is not locked"
159 " by the signalling thread",
160 cei->cond, cei->mutex);
161 VG_(pp_ExeContext)(VG_(get_error_where)(e));
162 break;
163 }
164 case CondDestrErr: {
165 CondDestrErrInfo* cdi = (CondDestrErrInfo*)(VG_(get_error_extra)(e));
166 VG_(message)(Vg_UserMsg,
bartaa97a542008-03-16 17:57:01 +0000167 "%s: cond 0x%lx, mutex 0x%lx locked by thread %d/%d",
bartbe8a12c2008-03-17 18:36:55 +0000168 VG_(get_error_string)(e),
bartaa97a542008-03-16 17:57:01 +0000169 cdi->cond, cdi->mutex,
170 DrdThreadIdToVgThreadId(cdi->tid), cdi->tid);
bart3772a982008-03-15 08:11:03 +0000171 VG_(pp_ExeContext)(VG_(get_error_where)(e));
172 break;
173 }
174 case SemaphoreErr: {
175 SemaphoreErrInfo* sei =(SemaphoreErrInfo*)(VG_(get_error_extra)(e));
176 tl_assert(sei);
177 VG_(message)(Vg_UserMsg,
178 "%s: semaphore 0x%lx",
179 VG_(get_error_string)(e),
180 sei->semaphore);
181 VG_(pp_ExeContext)(VG_(get_error_where)(e));
182 break;
183 }
184 case BarrierErr: {
185 BarrierErrInfo* sei =(BarrierErrInfo*)(VG_(get_error_extra)(e));
186 tl_assert(sei);
187 VG_(message)(Vg_UserMsg,
188 "%s: barrier 0x%lx",
189 VG_(get_error_string)(e),
190 sei->barrier);
191 VG_(pp_ExeContext)(VG_(get_error_where)(e));
192 break;
193 }
194 case RwlockErr: {
195 RwlockErrInfo* p = (RwlockErrInfo*)(VG_(get_error_extra)(e));
196 tl_assert(p);
197 VG_(message)(Vg_UserMsg,
198 "%s: rwlock 0x%lx.",
199 VG_(get_error_string)(e),
200 p->rwlock);
201 VG_(pp_ExeContext)(VG_(get_error_where)(e));
202 break;
203 }
204 case GenericErr: {
205 //GenericErrInfo* gei =(GenericErrInfo*)(VG_(get_error_extra)(e));
206 VG_(message)(Vg_UserMsg, "%s", VG_(get_error_string)(e));
207 VG_(pp_ExeContext)(VG_(get_error_where)(e));
208 break;
209 }
210 default:
211 VG_(message)(Vg_UserMsg,
212 "%s",
213 VG_(get_error_string)(e));
214 VG_(pp_ExeContext)(VG_(get_error_where)(e));
215 break;
216 }
sewardjaf44c822007-11-25 14:01:38 +0000217}
218
219static UInt drd_tool_error_update_extra(Error* e)
220{
bart3772a982008-03-15 08:11:03 +0000221 switch (VG_(get_error_kind)(e))
222 {
223 case DataRaceErr:
224 return sizeof(DataRaceErrInfo);
225 case MutexErr:
226 return sizeof(MutexErrInfo);
227 case CondErr:
228 return sizeof(CondErrInfo);
229 case CondRaceErr:
230 return sizeof(CondRaceErrInfo);
231 case CondDestrErr:
232 return sizeof(CondDestrErrInfo);
233 case SemaphoreErr:
234 return sizeof(SemaphoreErrInfo);
235 case BarrierErr:
236 return sizeof(BarrierErrInfo);
237 case RwlockErr:
238 return sizeof(RwlockErrInfo);
239 case GenericErr:
240 return sizeof(GenericErrInfo);
241 default:
242 tl_assert(False);
243 break;
244 }
sewardjaf44c822007-11-25 14:01:38 +0000245}
246
247static Bool drd_tool_error_recog(Char* const name, Supp* const supp)
248{
bart5fc70e62008-03-23 07:54:02 +0000249 SuppKind skind = 0;
sewardjaf44c822007-11-25 14:01:38 +0000250
bart5fc70e62008-03-23 07:54:02 +0000251 if (VG_(strcmp)(name, STR_DataRaceErr) == 0)
252 ;
253 else if (VG_(strcmp)(name, STR_MutexErr) == 0)
254 ;
255 else if (VG_(strcmp)(name, STR_CondErr) == 0)
256 ;
257 else if (VG_(strcmp)(name, STR_CondRaceErr) == 0)
258 ;
259 else if (VG_(strcmp)(name, STR_CondDestrErr) == 0)
260 ;
261 else if (VG_(strcmp)(name, STR_SemaphoreErr) == 0)
262 ;
263 else if (VG_(strcmp)(name, STR_BarrierErr) == 0)
264 ;
265 else if (VG_(strcmp)(name, STR_RwlockErr) == 0)
266 ;
267 else if (VG_(strcmp)(name, STR_GenericErr) == 0)
268 ;
bart3772a982008-03-15 08:11:03 +0000269 else
270 return False;
sewardjaf44c822007-11-25 14:01:38 +0000271
bart3772a982008-03-15 08:11:03 +0000272 VG_(set_supp_kind)(supp, skind);
273 return True;
sewardjaf44c822007-11-25 14:01:38 +0000274}
275
276static Bool drd_tool_error_read_extra(Int fd, Char* buf, Int nBuf, Supp* supp)
277{
bart3772a982008-03-15 08:11:03 +0000278 return True;
sewardjaf44c822007-11-25 14:01:38 +0000279}
280
281static Bool drd_tool_error_matches(Error* const e, Supp* const supp)
282{
bart3772a982008-03-15 08:11:03 +0000283 switch (VG_(get_supp_kind)(supp))
284 {
285 }
286 return True;
sewardjaf44c822007-11-25 14:01:38 +0000287}
288
289static Char* drd_tool_error_name(Error* e)
290{
bart3772a982008-03-15 08:11:03 +0000291 switch (VG_(get_error_kind)(e))
292 {
bart5fc70e62008-03-23 07:54:02 +0000293 case DataRaceErr: return VGAPPEND(STR_, DataRaceErr);
294 case MutexErr: return VGAPPEND(STR_, MutexErr);
295 case CondErr: return VGAPPEND(STR_, CondErr);
296 case CondRaceErr: return VGAPPEND(STR_, CondRaceErr);
297 case CondDestrErr: return VGAPPEND(STR_, CondDestrErr);
298 case SemaphoreErr: return VGAPPEND(STR_, SemaphoreErr);
299 case BarrierErr: return VGAPPEND(STR_, BarrierErr);
300 case RwlockErr: return VGAPPEND(STR_, RwlockErr);
301 case GenericErr: return VGAPPEND(STR_, GenericErr);
bart3772a982008-03-15 08:11:03 +0000302 default:
303 tl_assert(0);
304 }
305 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000306}
307
308static void drd_tool_error_print_extra(Error* e)
309{
bart3772a982008-03-15 08:11:03 +0000310 switch (VG_(get_error_kind)(e))
311 {
312 // VG_(printf)(" %s\n", VG_(get_error_string)(err));
313 }
sewardjaf44c822007-11-25 14:01:38 +0000314}
315
316void drd_register_error_handlers(void)
317{
bart3772a982008-03-15 08:11:03 +0000318 // Tool error reporting.
319 VG_(needs_tool_errors)(drd_tool_error_eq,
320 drd_tool_error_pp,
321 True,
322 drd_tool_error_update_extra,
323 drd_tool_error_recog,
324 drd_tool_error_read_extra,
325 drd_tool_error_matches,
326 drd_tool_error_name,
327 drd_tool_error_print_extra);
sewardjaf44c822007-11-25 14:01:38 +0000328}