blob: c2a34857f57630757c5265bfc55a3386fcd52bed [file] [log] [blame]
sewardj85642922008-01-14 11:54:56 +00001/*
2 This file is part of drd, a data race detector.
3
4 Copyright (C) 2006-2008 Bart Van Assche
5 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
sewardjaf44c822007-11-25 14:01:38 +000026#include "drd_clientreq.h"
27#include "drd_cond.h"
28#include "drd_mutex.h"
sewardj85642922008-01-14 11:54:56 +000029#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000030#include "drd_suppression.h" // drd_start_suppression()
31#include "drd_thread.h"
32#include "drd_track.h"
bart777f7fe2008-03-02 17:43:18 +000033#include "drd_rwlock.h"
sewardj721ad7b2007-11-30 08:30:29 +000034#include "priv_drd_clientreq.h"
sewardjaf44c822007-11-25 14:01:38 +000035#include "pub_tool_basics.h" // Bool
36#include "pub_tool_libcassert.h"
37#include "pub_tool_libcassert.h" // tl_assert()
38#include "pub_tool_libcprint.h" // VG_(message)()
39#include "pub_tool_machine.h" // VG_(get_SP)()
40#include "pub_tool_threadstate.h"
41#include "pub_tool_tooliface.h" // VG_(needs_...)()
42
43
bart0268dfa2008-03-11 20:10:21 +000044static void drd_spin_init_or_unlock(const Addr spinlock)
sewardjaf44c822007-11-25 14:01:38 +000045{
bart3772a982008-03-15 08:11:03 +000046 struct mutex_info* mutex_p = mutex_get(spinlock);
47 if (mutex_p)
48 {
49 mutex_unlock(spinlock, mutex_type_spinlock);
50 }
51 else
52 {
53 mutex_init(spinlock, mutex_type_spinlock);
54 }
sewardjaf44c822007-11-25 14:01:38 +000055}
56
bart0268dfa2008-03-11 20:10:21 +000057static void drd_pre_cond_wait(const Addr cond,
bart5357fcb2008-02-27 15:46:00 +000058 const Addr mutex, const MutexT mutex_type)
sewardjaf44c822007-11-25 14:01:38 +000059{
bart3772a982008-03-15 08:11:03 +000060 mutex_unlock(mutex, mutex_type);
61 cond_pre_wait(cond, mutex);
sewardjaf44c822007-11-25 14:01:38 +000062}
63
bart00344642008-03-01 15:27:41 +000064static void drd_post_cond_wait(const Addr cond,
65 const Addr mutex,
bart3b1ee452008-02-29 19:28:15 +000066 const Bool took_lock)
sewardjaf44c822007-11-25 14:01:38 +000067{
bart3772a982008-03-15 08:11:03 +000068 cond_post_wait(cond);
69 mutex_post_lock(mutex, took_lock);
sewardjaf44c822007-11-25 14:01:38 +000070}
71
72static void drd_pre_cond_signal(const Addr cond)
73{
bart3772a982008-03-15 08:11:03 +000074 cond_pre_signal(cond);
sewardjaf44c822007-11-25 14:01:38 +000075}
76
77static void drd_pre_cond_broadcast(const Addr cond)
78{
bart3772a982008-03-15 08:11:03 +000079 cond_pre_broadcast(cond);
sewardjaf44c822007-11-25 14:01:38 +000080}
81
bart0268dfa2008-03-11 20:10:21 +000082static Bool drd_handle_client_request(ThreadId vg_tid, UWord* arg, UWord* ret)
sewardjaf44c822007-11-25 14:01:38 +000083{
bart3772a982008-03-15 08:11:03 +000084 UWord result = 0;
85 const DrdThreadId drd_tid = thread_get_running_tid();
bart0268dfa2008-03-11 20:10:21 +000086
bart3772a982008-03-15 08:11:03 +000087 tl_assert(vg_tid == VG_(get_running_tid()));
88 tl_assert(VgThreadIdToDrdThreadId(vg_tid) == drd_tid);
sewardjaf44c822007-11-25 14:01:38 +000089
bart3772a982008-03-15 08:11:03 +000090 switch (arg[0])
91 {
92 case VG_USERREQ__GET_THREAD_SELF:
93 result = vg_tid;
94 break;
sewardjaf44c822007-11-25 14:01:38 +000095
bart3772a982008-03-15 08:11:03 +000096 case VG_USERREQ__DRD_START_SUPPRESSION:
97 drd_start_suppression(arg[1], arg[2], "client");
98 break;
sewardjaf44c822007-11-25 14:01:38 +000099
bart3772a982008-03-15 08:11:03 +0000100 case VG_USERREQ__DRD_FINISH_SUPPRESSION:
101 drd_finish_suppression(arg[1], arg[2]);
102 break;
sewardjaf44c822007-11-25 14:01:38 +0000103
bart3772a982008-03-15 08:11:03 +0000104 case VG_USERREQ__DRD_SUPPRESS_CURRENT_STACK:
105 thread_set_stack_startup(drd_tid, VG_(get_SP)(vg_tid));
106 break;
sewardjaf44c822007-11-25 14:01:38 +0000107
bart3772a982008-03-15 08:11:03 +0000108 case VG_USERREQ__DRD_START_NEW_SEGMENT:
109 thread_new_segment(PtThreadIdToDrdThreadId(arg[1]));
110 break;
sewardjaf44c822007-11-25 14:01:38 +0000111
bart3772a982008-03-15 08:11:03 +0000112 case VG_USERREQ__DRD_TRACE_ADDR:
113 drd_trace_addr(arg[1]);
114 break;
bart5bd9f2d2008-03-03 20:31:58 +0000115
bart3772a982008-03-15 08:11:03 +0000116 case VG_USERREQ__SET_PTHREADID:
117 thread_set_pthreadid(drd_tid, arg[1]);
118 break;
sewardjaf44c822007-11-25 14:01:38 +0000119
bart3772a982008-03-15 08:11:03 +0000120 case VG_USERREQ__SET_JOINABLE:
121 thread_set_joinable(PtThreadIdToDrdThreadId(arg[1]), (Bool)arg[2]);
122 break;
sewardjaf44c822007-11-25 14:01:38 +0000123
bart3772a982008-03-15 08:11:03 +0000124 case VG_USERREQ__POST_THREAD_JOIN:
125 tl_assert(arg[1]);
126 drd_post_thread_join(drd_tid,
127 PtThreadIdToDrdThreadId(arg[1]));
128 break;
sewardjaf44c822007-11-25 14:01:38 +0000129
bart3772a982008-03-15 08:11:03 +0000130 case VG_USERREQ__PRE_MUTEX_INIT:
131 if (thread_enter_synchr(drd_tid) == 0)
132 drd_pre_mutex_init(arg[1], arg[2]);
133 break;
bart0268dfa2008-03-11 20:10:21 +0000134
bart3772a982008-03-15 08:11:03 +0000135 case VG_USERREQ__POST_MUTEX_INIT:
136 thread_leave_synchr(drd_tid);
137 break;
bart0268dfa2008-03-11 20:10:21 +0000138
bart3772a982008-03-15 08:11:03 +0000139 case VG_USERREQ__PRE_MUTEX_DESTROY:
140 thread_enter_synchr(drd_tid);
141 break;
sewardjaf44c822007-11-25 14:01:38 +0000142
bart3772a982008-03-15 08:11:03 +0000143 case VG_USERREQ__POST_MUTEX_DESTROY:
144 if (thread_leave_synchr(drd_tid) == 0)
145 drd_post_mutex_destroy(arg[1], arg[2]);
146 break;
sewardjaf44c822007-11-25 14:01:38 +0000147
bart3772a982008-03-15 08:11:03 +0000148 case VG_USERREQ__PRE_MUTEX_LOCK:
149 if (thread_enter_synchr(drd_tid) == 0)
150 drd_pre_mutex_lock(arg[1], arg[2]);
151 break;
sewardjaf44c822007-11-25 14:01:38 +0000152
bart3772a982008-03-15 08:11:03 +0000153 case VG_USERREQ__POST_MUTEX_LOCK:
154 if (thread_leave_synchr(drd_tid) == 0)
155 drd_post_mutex_lock(arg[1], arg[2]);
156 break;
sewardjaf44c822007-11-25 14:01:38 +0000157
bart3772a982008-03-15 08:11:03 +0000158 case VG_USERREQ__PRE_MUTEX_UNLOCK:
159 if (thread_enter_synchr(drd_tid) == 0)
160 drd_pre_mutex_unlock(arg[1], arg[2]);
161 break;
bart0268dfa2008-03-11 20:10:21 +0000162
bart3772a982008-03-15 08:11:03 +0000163 case VG_USERREQ__POST_MUTEX_UNLOCK:
164 thread_leave_synchr(drd_tid);
165 break;
sewardjaf44c822007-11-25 14:01:38 +0000166
bart3772a982008-03-15 08:11:03 +0000167 case VG_USERREQ__SPIN_INIT_OR_UNLOCK:
168 tl_assert(thread_get_synchr_nesting_count(drd_tid) == 0);
169 drd_spin_init_or_unlock(arg[1]);
170 break;
sewardjaf44c822007-11-25 14:01:38 +0000171
bart3772a982008-03-15 08:11:03 +0000172 case VG_USERREQ__PRE_COND_INIT:
173 tl_assert(thread_get_synchr_nesting_count(drd_tid) == 0);
174 drd_pre_cond_init(arg[1]);
175 break;
sewardjaf44c822007-11-25 14:01:38 +0000176
bart3772a982008-03-15 08:11:03 +0000177 case VG_USERREQ__POST_COND_DESTROY:
178 tl_assert(thread_get_synchr_nesting_count(drd_tid) == 0);
179 drd_post_cond_destroy(arg[1]);
180 break;
sewardjaf44c822007-11-25 14:01:38 +0000181
bart3772a982008-03-15 08:11:03 +0000182 case VG_USERREQ__PRE_COND_WAIT:
183 if (thread_enter_synchr(drd_tid) == 0)
184 drd_pre_cond_wait(arg[1], arg[2], arg[3]);
185 break;
sewardjaf44c822007-11-25 14:01:38 +0000186
bart3772a982008-03-15 08:11:03 +0000187 case VG_USERREQ__POST_COND_WAIT:
188 if (thread_leave_synchr(drd_tid) == 0)
189 drd_post_cond_wait(arg[1], arg[2], arg[3]);
190 break;
sewardjaf44c822007-11-25 14:01:38 +0000191
bart3772a982008-03-15 08:11:03 +0000192 case VG_USERREQ__PRE_COND_SIGNAL:
193 tl_assert(thread_get_synchr_nesting_count(drd_tid) == 0);
194 drd_pre_cond_signal(arg[1]);
195 break;
sewardjaf44c822007-11-25 14:01:38 +0000196
bart3772a982008-03-15 08:11:03 +0000197 case VG_USERREQ__PRE_COND_BROADCAST:
198 tl_assert(thread_get_synchr_nesting_count(drd_tid) == 0);
199 drd_pre_cond_broadcast(arg[1]);
200 break;
sewardjaf44c822007-11-25 14:01:38 +0000201
bart3772a982008-03-15 08:11:03 +0000202 case VG_USERREQ__PRE_SEM_INIT:
203 if (thread_enter_synchr(drd_tid) == 0)
204 drd_semaphore_init(arg[1], arg[2], arg[3]);
205 break;
sewardj85642922008-01-14 11:54:56 +0000206
bart3772a982008-03-15 08:11:03 +0000207 case VG_USERREQ__POST_SEM_INIT:
208 thread_leave_synchr(drd_tid);
209 break;
bart0268dfa2008-03-11 20:10:21 +0000210
bart3772a982008-03-15 08:11:03 +0000211 case VG_USERREQ__PRE_SEM_DESTROY:
212 thread_enter_synchr(drd_tid);
213 break;
bart0268dfa2008-03-11 20:10:21 +0000214
bart3772a982008-03-15 08:11:03 +0000215 case VG_USERREQ__POST_SEM_DESTROY:
216 if (thread_leave_synchr(drd_tid) == 0)
217 drd_semaphore_destroy(arg[1]);
218 break;
sewardj85642922008-01-14 11:54:56 +0000219
bart3772a982008-03-15 08:11:03 +0000220 case VG_USERREQ__PRE_SEM_WAIT:
221 if (thread_enter_synchr(drd_tid) == 0)
222 drd_semaphore_pre_wait(drd_tid, arg[1]);
223 break;
bart28230a32008-02-29 17:27:03 +0000224
bart3772a982008-03-15 08:11:03 +0000225 case VG_USERREQ__POST_SEM_WAIT:
226 if (thread_leave_synchr(drd_tid) == 0)
227 drd_semaphore_post_wait(drd_tid, arg[1], arg[2]);
228 break;
sewardj85642922008-01-14 11:54:56 +0000229
bart3772a982008-03-15 08:11:03 +0000230 case VG_USERREQ__PRE_SEM_POST:
231 if (thread_enter_synchr(drd_tid) == 0)
232 drd_semaphore_pre_post(drd_tid, arg[1]);
233 break;
sewardj85642922008-01-14 11:54:56 +0000234
bart3772a982008-03-15 08:11:03 +0000235 case VG_USERREQ__POST_SEM_POST:
236 if (thread_leave_synchr(drd_tid) == 0)
237 drd_semaphore_post_post(drd_tid, arg[1], arg[2]);
238 break;
sewardj85642922008-01-14 11:54:56 +0000239
bart3772a982008-03-15 08:11:03 +0000240 case VG_USERREQ__PRE_BARRIER_INIT:
241 if (thread_enter_synchr(drd_tid) == 0)
242 drd_barrier_init(arg[1], arg[2], arg[3], arg[4]);
243 break;
sewardj85642922008-01-14 11:54:56 +0000244
bart3772a982008-03-15 08:11:03 +0000245 case VG_USERREQ__POST_BARRIER_INIT:
246 thread_leave_synchr(drd_tid);
247 break;
bart0268dfa2008-03-11 20:10:21 +0000248
bart3772a982008-03-15 08:11:03 +0000249 case VG_USERREQ__PRE_BARRIER_DESTROY:
250 thread_enter_synchr(drd_tid);
251 break;
bart0268dfa2008-03-11 20:10:21 +0000252
bart3772a982008-03-15 08:11:03 +0000253 case VG_USERREQ__POST_BARRIER_DESTROY:
254 if (thread_leave_synchr(drd_tid) == 0)
255 drd_barrier_destroy(arg[1], arg[2]);
256 break;
sewardj85642922008-01-14 11:54:56 +0000257
bart3772a982008-03-15 08:11:03 +0000258 case VG_USERREQ__PRE_BARRIER_WAIT:
259 if (thread_enter_synchr(drd_tid) == 0)
260 drd_barrier_pre_wait(drd_tid, arg[1], arg[2]);
261 break;
sewardj85642922008-01-14 11:54:56 +0000262
bart3772a982008-03-15 08:11:03 +0000263 case VG_USERREQ__POST_BARRIER_WAIT:
264 if (thread_leave_synchr(drd_tid) == 0)
265 drd_barrier_post_wait(drd_tid, arg[1], arg[2], arg[3]);
266 break;
sewardj85642922008-01-14 11:54:56 +0000267
bart3772a982008-03-15 08:11:03 +0000268 case VG_USERREQ__PRE_RWLOCK_INIT:
269 rwlock_pre_init(arg[1]);
270 break;
bart00344642008-03-01 15:27:41 +0000271
bart3772a982008-03-15 08:11:03 +0000272 case VG_USERREQ__POST_RWLOCK_DESTROY:
273 rwlock_post_destroy(arg[1]);
274 break;
bart00344642008-03-01 15:27:41 +0000275
bart3772a982008-03-15 08:11:03 +0000276 case VG_USERREQ__PRE_RWLOCK_RDLOCK:
277 if (thread_enter_synchr(drd_tid) == 0)
278 rwlock_pre_rdlock(arg[1]);
279 break;
bart00344642008-03-01 15:27:41 +0000280
bart3772a982008-03-15 08:11:03 +0000281 case VG_USERREQ__POST_RWLOCK_RDLOCK:
282 if (thread_leave_synchr(drd_tid) == 0)
283 rwlock_post_rdlock(arg[1], arg[2]);
284 break;
bart00344642008-03-01 15:27:41 +0000285
bart3772a982008-03-15 08:11:03 +0000286 case VG_USERREQ__PRE_RWLOCK_WRLOCK:
287 if (thread_enter_synchr(drd_tid) == 0)
288 rwlock_pre_wrlock(arg[1]);
289 break;
bart00344642008-03-01 15:27:41 +0000290
bart3772a982008-03-15 08:11:03 +0000291 case VG_USERREQ__POST_RWLOCK_WRLOCK:
292 if (thread_leave_synchr(drd_tid) == 0)
293 rwlock_post_wrlock(arg[1], arg[2]);
294 break;
bart00344642008-03-01 15:27:41 +0000295
bart3772a982008-03-15 08:11:03 +0000296 case VG_USERREQ__PRE_RWLOCK_UNLOCK:
297 if (thread_enter_synchr(drd_tid) == 0)
298 rwlock_pre_unlock(arg[1]);
299 break;
bart0268dfa2008-03-11 20:10:21 +0000300
bart3772a982008-03-15 08:11:03 +0000301 case VG_USERREQ__POST_RWLOCK_UNLOCK:
302 thread_leave_synchr(drd_tid);
303 break;
bart00344642008-03-01 15:27:41 +0000304
bart3772a982008-03-15 08:11:03 +0000305 default:
306 VG_(message)(Vg_DebugMsg, "Unrecognized client request 0x%lx 0x%lx",
307 arg[0], arg[1]);
308 tl_assert(0);
309 return False;
310 }
sewardjaf44c822007-11-25 14:01:38 +0000311
bart3772a982008-03-15 08:11:03 +0000312 *ret = result;
313 return True;
sewardjaf44c822007-11-25 14:01:38 +0000314}
315
316void drd_clientreq_init(void)
317{
bart3772a982008-03-15 08:11:03 +0000318 VG_(needs_client_requests)(drd_handle_client_request);
sewardjaf44c822007-11-25 14:01:38 +0000319}