blob: e68e6a604fc62e0de84d5afd444bd596a6d9a0d1 [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
42typedef enum {
bart3772a982008-03-15 08:11:03 +000043 ConflictingAccessSupp
sewardjaf44c822007-11-25 14:01:38 +000044} DRD_SuppKind;
45
46
bartff1252a2008-03-16 17:27:25 +000047/* Describe a data address range [a,a+len[ as good as possible, for error */
sewardjaf44c822007-11-25 14:01:38 +000048/* messages, putting the result in ai. */
bartff1252a2008-03-16 17:27:25 +000049static
50void describe_malloced_addr(Addr const a, SizeT const len, AddrInfo* const ai)
sewardjaf44c822007-11-25 14:01:38 +000051{
bartff1252a2008-03-16 17:27:25 +000052 Addr data;
sewardjaf44c822007-11-25 14:01:38 +000053
bartff1252a2008-03-16 17:27:25 +000054 if (drd_heap_addrinfo(a, &data, &ai->size, &ai->lastchange))
bart3772a982008-03-15 08:11:03 +000055 {
bartff1252a2008-03-16 17:27:25 +000056 ai->akind = eMallocd;
57 ai->rwoffset = a - data;
bart3772a982008-03-15 08:11:03 +000058 }
bartff1252a2008-03-16 17:27:25 +000059 else
bart3772a982008-03-15 08:11:03 +000060 {
bartff1252a2008-03-16 17:27:25 +000061 ai->akind = eUnknown;
bart3772a982008-03-15 08:11:03 +000062 }
sewardjaf44c822007-11-25 14:01:38 +000063}
64
sewardjaf44c822007-11-25 14:01:38 +000065static
66void drd_report_data_race2(Error* const err, const DataRaceErrInfo* const dri)
67{
bart3772a982008-03-15 08:11:03 +000068 AddrInfo ai;
69 Char descr1[256];
70 Char descr2[256];
sewardjaf44c822007-11-25 14:01:38 +000071
bart3772a982008-03-15 08:11:03 +000072 tl_assert(dri);
73 tl_assert(dri->addr);
74 tl_assert(dri->size > 0);
bartb515eb12008-03-07 18:52:38 +000075
bart3772a982008-03-15 08:11:03 +000076 descr1[0] = 0;
77 descr2[0] = 0;
78 VG_(get_data_description)(descr1, descr2, sizeof(descr1), dri->addr);
79 if (descr1[0] == 0)
80 {
bartff1252a2008-03-16 17:27:25 +000081 describe_malloced_addr(dri->addr, dri->size, &ai);
bart3772a982008-03-15 08:11:03 +000082 }
83 VG_(message)(Vg_UserMsg,
bartaa97a542008-03-16 17:57:01 +000084 "Conflicting %s by thread %d/%d at 0x%08lx size %ld",
bart3772a982008-03-15 08:11:03 +000085 dri->access_type == eStore ? "store" : "load",
bart354009c2008-03-16 10:42:33 +000086 DrdThreadIdToVgThreadId(dri->tid),
bartaa97a542008-03-16 17:57:01 +000087 dri->tid,
bart3772a982008-03-15 08:11:03 +000088 dri->addr,
89 dri->size);
90 VG_(pp_ExeContext)(VG_(get_error_where)(err));
91 if (descr1[0])
92 {
93 VG_(message)(Vg_UserMsg, "%s", descr1);
94 VG_(message)(Vg_UserMsg, "%s", descr2);
95 }
96 else if (ai.akind == eMallocd && ai.lastchange)
97 {
98 VG_(message)(Vg_UserMsg,
99 "Address 0x%lx is at offset %ld from 0x%lx."
100 " Allocation context:",
101 dri->addr, ai.rwoffset, dri->addr - ai.rwoffset);
102 VG_(pp_ExeContext)(ai.lastchange);
103 }
104 else
105 {
106 VG_(message)(Vg_UserMsg, "Allocation context: unknown.");
107 }
bart354009c2008-03-16 10:42:33 +0000108 thread_report_conflicting_segments(dri->tid,
bart3772a982008-03-15 08:11:03 +0000109 dri->addr, dri->size, dri->access_type);
sewardjaf44c822007-11-25 14:01:38 +0000110}
111
112static Bool drd_tool_error_eq(VgRes res, Error* e1, Error* e2)
113{
bart3772a982008-03-15 08:11:03 +0000114 return False;
sewardjaf44c822007-11-25 14:01:38 +0000115}
116
117static void drd_tool_error_pp(Error* const e)
118{
bart3772a982008-03-15 08:11:03 +0000119 switch (VG_(get_error_kind)(e))
120 {
121 case DataRaceErr: {
122 drd_report_data_race2(e, VG_(get_error_extra)(e));
123 break;
124 }
125 case MutexErr: {
126 MutexErrInfo* p = (MutexErrInfo*)(VG_(get_error_extra)(e));
127 tl_assert(p);
128 VG_(message)(Vg_UserMsg,
129 "%s: mutex 0x%lx, recursion count %d, owner %d.",
130 VG_(get_error_string)(e),
131 p->mutex,
132 p->recursion_count,
133 p->owner);
134 VG_(pp_ExeContext)(VG_(get_error_where)(e));
135 break;
136 }
137 case CondErr: {
138 CondErrInfo* cdei =(CondErrInfo*)(VG_(get_error_extra)(e));
139 VG_(message)(Vg_UserMsg,
140 "%s: cond 0x%lx",
bartbe8a12c2008-03-17 18:36:55 +0000141 VG_(get_error_string)(e),
142 cdei->cond);
bart3772a982008-03-15 08:11:03 +0000143 VG_(pp_ExeContext)(VG_(get_error_where)(e));
144 break;
145 }
146 case CondRaceErr: {
147 CondRaceErrInfo* cei = (CondRaceErrInfo*)(VG_(get_error_extra)(e));
148 VG_(message)(Vg_UserMsg,
149 "Race condition: condition variable 0x%lx has been"
150 " signalled but the associated mutex 0x%lx is not locked"
151 " by the signalling thread",
152 cei->cond, cei->mutex);
153 VG_(pp_ExeContext)(VG_(get_error_where)(e));
154 break;
155 }
156 case CondDestrErr: {
157 CondDestrErrInfo* cdi = (CondDestrErrInfo*)(VG_(get_error_extra)(e));
158 VG_(message)(Vg_UserMsg,
bartaa97a542008-03-16 17:57:01 +0000159 "%s: cond 0x%lx, mutex 0x%lx locked by thread %d/%d",
bartbe8a12c2008-03-17 18:36:55 +0000160 VG_(get_error_string)(e),
bartaa97a542008-03-16 17:57:01 +0000161 cdi->cond, cdi->mutex,
162 DrdThreadIdToVgThreadId(cdi->tid), cdi->tid);
bart3772a982008-03-15 08:11:03 +0000163 VG_(pp_ExeContext)(VG_(get_error_where)(e));
164 break;
165 }
166 case SemaphoreErr: {
167 SemaphoreErrInfo* sei =(SemaphoreErrInfo*)(VG_(get_error_extra)(e));
168 tl_assert(sei);
169 VG_(message)(Vg_UserMsg,
170 "%s: semaphore 0x%lx",
171 VG_(get_error_string)(e),
172 sei->semaphore);
173 VG_(pp_ExeContext)(VG_(get_error_where)(e));
174 break;
175 }
176 case BarrierErr: {
177 BarrierErrInfo* sei =(BarrierErrInfo*)(VG_(get_error_extra)(e));
178 tl_assert(sei);
179 VG_(message)(Vg_UserMsg,
180 "%s: barrier 0x%lx",
181 VG_(get_error_string)(e),
182 sei->barrier);
183 VG_(pp_ExeContext)(VG_(get_error_where)(e));
184 break;
185 }
186 case RwlockErr: {
187 RwlockErrInfo* p = (RwlockErrInfo*)(VG_(get_error_extra)(e));
188 tl_assert(p);
189 VG_(message)(Vg_UserMsg,
190 "%s: rwlock 0x%lx.",
191 VG_(get_error_string)(e),
192 p->rwlock);
193 VG_(pp_ExeContext)(VG_(get_error_where)(e));
194 break;
195 }
196 case GenericErr: {
197 //GenericErrInfo* gei =(GenericErrInfo*)(VG_(get_error_extra)(e));
198 VG_(message)(Vg_UserMsg, "%s", VG_(get_error_string)(e));
199 VG_(pp_ExeContext)(VG_(get_error_where)(e));
200 break;
201 }
202 default:
203 VG_(message)(Vg_UserMsg,
204 "%s",
205 VG_(get_error_string)(e));
206 VG_(pp_ExeContext)(VG_(get_error_where)(e));
207 break;
208 }
sewardjaf44c822007-11-25 14:01:38 +0000209}
210
211static UInt drd_tool_error_update_extra(Error* e)
212{
bart3772a982008-03-15 08:11:03 +0000213 switch (VG_(get_error_kind)(e))
214 {
215 case DataRaceErr:
216 return sizeof(DataRaceErrInfo);
217 case MutexErr:
218 return sizeof(MutexErrInfo);
219 case CondErr:
220 return sizeof(CondErrInfo);
221 case CondRaceErr:
222 return sizeof(CondRaceErrInfo);
223 case CondDestrErr:
224 return sizeof(CondDestrErrInfo);
225 case SemaphoreErr:
226 return sizeof(SemaphoreErrInfo);
227 case BarrierErr:
228 return sizeof(BarrierErrInfo);
229 case RwlockErr:
230 return sizeof(RwlockErrInfo);
231 case GenericErr:
232 return sizeof(GenericErrInfo);
233 default:
234 tl_assert(False);
235 break;
236 }
sewardjaf44c822007-11-25 14:01:38 +0000237}
238
239static Bool drd_tool_error_recog(Char* const name, Supp* const supp)
240{
bart3772a982008-03-15 08:11:03 +0000241 SuppKind skind;
sewardjaf44c822007-11-25 14:01:38 +0000242
bart3772a982008-03-15 08:11:03 +0000243 if (VG_(strcmp)(name, "ConflictingAccess") == 0)
244 skind = ConflictingAccessSupp;
245 else
246 return False;
sewardjaf44c822007-11-25 14:01:38 +0000247
bart3772a982008-03-15 08:11:03 +0000248 VG_(set_supp_kind)(supp, skind);
249 return True;
sewardjaf44c822007-11-25 14:01:38 +0000250}
251
252static Bool drd_tool_error_read_extra(Int fd, Char* buf, Int nBuf, Supp* supp)
253{
bart3772a982008-03-15 08:11:03 +0000254 return True;
sewardjaf44c822007-11-25 14:01:38 +0000255}
256
257static Bool drd_tool_error_matches(Error* const e, Supp* const supp)
258{
bart3772a982008-03-15 08:11:03 +0000259 switch (VG_(get_supp_kind)(supp))
260 {
261 }
262 return True;
sewardjaf44c822007-11-25 14:01:38 +0000263}
264
265static Char* drd_tool_error_name(Error* e)
266{
bart3772a982008-03-15 08:11:03 +0000267 switch (VG_(get_error_kind)(e))
268 {
bartff1252a2008-03-16 17:27:25 +0000269 case DataRaceErr: return "ConflictingAccess";
bart3772a982008-03-15 08:11:03 +0000270 case MutexErr: return "MutexErr";
271 case CondErr: return "CondErr";
272 case CondRaceErr: return "CondRaceErr";
273 case CondDestrErr: return "CondDestrErr";
274 case SemaphoreErr: return "SemaphoreErr";
275 case BarrierErr: return "BarrierErr";
276 case RwlockErr: return "RwlockErr";
277 case GenericErr: return "GenericErr";
278 default:
279 tl_assert(0);
280 }
281 return 0;
sewardjaf44c822007-11-25 14:01:38 +0000282}
283
284static void drd_tool_error_print_extra(Error* e)
285{
bart3772a982008-03-15 08:11:03 +0000286 switch (VG_(get_error_kind)(e))
287 {
288 // VG_(printf)(" %s\n", VG_(get_error_string)(err));
289 }
sewardjaf44c822007-11-25 14:01:38 +0000290}
291
292void drd_register_error_handlers(void)
293{
bart3772a982008-03-15 08:11:03 +0000294 // Tool error reporting.
295 VG_(needs_tool_errors)(drd_tool_error_eq,
296 drd_tool_error_pp,
297 True,
298 drd_tool_error_update_extra,
299 drd_tool_error_recog,
300 drd_tool_error_read_extra,
301 drd_tool_error_matches,
302 drd_tool_error_name,
303 drd_tool_error_print_extra);
sewardjaf44c822007-11-25 14:01:38 +0000304}