blob: 9a267e541df1bb9d7b215a2141452a22d2115715 [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"
sewardjaf44c822007-11-25 14:01:38 +000034#include "pub_tool_basics.h" // Bool
bartb93846e2008-04-16 18:17:12 +000035#include "pub_tool_debuginfo.h" // VG_(describe_IP)()
sewardjaf44c822007-11-25 14:01:38 +000036#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);
bart4a975e12008-03-30 13:28:33 +000069 mutex_post_lock(mutex, took_lock, True);
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
bartf1ac71a2008-04-04 16:54:37 +000082/** Walk the stack up to the highest stack frame, and return the stack pointer
83 * of the highest stack frame. It is assumed that there are no more than
84 * ten stack frames above the current frame. This should be no problem
85 * since this function is either called indirectly from the _init() function
86 * in vgpreload_exp-drd-*.so or from the thread wrapper for a newly created
87 * thread. See also drd_pthread_intercepts.c.
88 */
89static Addr highest_used_stack_address(const ThreadId vg_tid)
90{
91 UInt nframes;
92 const UInt n_ips = 10;
bartd2765d82008-05-04 11:59:01 +000093 UInt i;
bartf1ac71a2008-04-04 16:54:37 +000094 Addr ips[n_ips], sps[n_ips];
bart6ad00b72008-05-02 17:27:08 +000095 Addr husa;
bartf1ac71a2008-04-04 16:54:37 +000096
97 nframes = VG_(get_StackTrace)(vg_tid, ips, n_ips, sps, 0, 0);
bartd2765d82008-05-04 11:59:01 +000098 tl_assert(1 <= nframes && nframes <= n_ips);
bartf1ac71a2008-04-04 16:54:37 +000099
bartd2765d82008-05-04 11:59:01 +0000100 /* A hack to work around VG_(get_StackTrace)()'s behavior that sometimes */
101 /* the topmost stackframes it returns are bogus (this occurs sometimes */
102 /* at least on amd64, ppc32 and ppc64). */
bartab96eca2008-05-02 19:12:43 +0000103
bartd2765d82008-05-04 11:59:01 +0000104 husa = sps[0];
105
bart6ad00b72008-05-02 17:27:08 +0000106 tl_assert(VG_(thread_get_stack_max)(vg_tid)
107 - VG_(thread_get_stack_size)(vg_tid) <= husa
bart32fcd8d2008-05-02 19:21:02 +0000108 && husa < VG_(thread_get_stack_max)(vg_tid));
bartd2765d82008-05-04 11:59:01 +0000109
110 for (i = 1; i < nframes; i++)
111 {
112 if (sps[i] == 0)
113 break;
114 if (husa < sps[i] && sps[i] < VG_(thread_get_stack_max)(vg_tid))
115 husa = sps[i];
116 }
117
118 tl_assert(VG_(thread_get_stack_max)(vg_tid)
119 - VG_(thread_get_stack_size)(vg_tid) <= husa
120 && husa < VG_(thread_get_stack_max)(vg_tid));
121
bart6ad00b72008-05-02 17:27:08 +0000122 return husa;
bartf1ac71a2008-04-04 16:54:37 +0000123}
124
bart0268dfa2008-03-11 20:10:21 +0000125static Bool drd_handle_client_request(ThreadId vg_tid, UWord* arg, UWord* ret)
sewardjaf44c822007-11-25 14:01:38 +0000126{
bart3772a982008-03-15 08:11:03 +0000127 UWord result = 0;
128 const DrdThreadId drd_tid = thread_get_running_tid();
bart0268dfa2008-03-11 20:10:21 +0000129
bart3772a982008-03-15 08:11:03 +0000130 tl_assert(vg_tid == VG_(get_running_tid()));
131 tl_assert(VgThreadIdToDrdThreadId(vg_tid) == drd_tid);
sewardjaf44c822007-11-25 14:01:38 +0000132
bart3772a982008-03-15 08:11:03 +0000133 switch (arg[0])
134 {
bart5f57be92008-07-01 08:48:56 +0000135 case VG_USERREQ__DRD_GET_VALGRIND_THREAD_ID:
bart3772a982008-03-15 08:11:03 +0000136 result = vg_tid;
137 break;
sewardjaf44c822007-11-25 14:01:38 +0000138
bart5f57be92008-07-01 08:48:56 +0000139 case VG_USERREQ__DRD_GET_DRD_THREAD_ID:
140 result = drd_tid;
141 break;
142
bart3772a982008-03-15 08:11:03 +0000143 case VG_USERREQ__DRD_START_SUPPRESSION:
bartf5bb46a2008-03-29 13:18:02 +0000144 drd_start_suppression(arg[1], arg[1] + arg[2], "client");
bart3772a982008-03-15 08:11:03 +0000145 break;
sewardjaf44c822007-11-25 14:01:38 +0000146
bart3772a982008-03-15 08:11:03 +0000147 case VG_USERREQ__DRD_FINISH_SUPPRESSION:
bartf5bb46a2008-03-29 13:18:02 +0000148 drd_finish_suppression(arg[1], arg[1] + arg[2]);
bart3772a982008-03-15 08:11:03 +0000149 break;
sewardjaf44c822007-11-25 14:01:38 +0000150
bart3772a982008-03-15 08:11:03 +0000151 case VG_USERREQ__DRD_SUPPRESS_CURRENT_STACK:
bartf1ac71a2008-04-04 16:54:37 +0000152 {
153 const Addr topmost_sp = highest_used_stack_address(vg_tid);
bart0ff483d2008-04-06 13:08:32 +0000154#if 0
155 UInt nframes;
156 const UInt n_ips = 20;
157 Addr ips[n_ips], sps[n_ips], fps[n_ips];
bartb93846e2008-04-16 18:17:12 +0000158 Char desc[128];
bart0ff483d2008-04-06 13:08:32 +0000159 unsigned i;
160
161 nframes = VG_(get_StackTrace)(vg_tid, ips, n_ips, sps, fps, 0);
162
163 VG_(message)(Vg_DebugMsg, "thread %d/%d", vg_tid, drd_tid);
164 for (i = 0; i < nframes; i++)
165 {
bartb93846e2008-04-16 18:17:12 +0000166 VG_(describe_IP)(ips[i], desc, sizeof(desc));
167 VG_(message)(Vg_DebugMsg, "[%2d] sp 0x%09lx fp 0x%09lx ip %s",
168 i, sps[i], fps[i], desc);
bart0ff483d2008-04-06 13:08:32 +0000169 }
170#endif
bart3772a982008-03-15 08:11:03 +0000171 thread_set_stack_startup(drd_tid, VG_(get_SP)(vg_tid));
bartf1ac71a2008-04-04 16:54:37 +0000172 drd_start_suppression(topmost_sp, VG_(thread_get_stack_max)(vg_tid),
173 "stack top");
bart3772a982008-03-15 08:11:03 +0000174 break;
bartf1ac71a2008-04-04 16:54:37 +0000175 }
sewardjaf44c822007-11-25 14:01:38 +0000176
bart3772a982008-03-15 08:11:03 +0000177 case VG_USERREQ__DRD_START_NEW_SEGMENT:
178 thread_new_segment(PtThreadIdToDrdThreadId(arg[1]));
179 break;
sewardjaf44c822007-11-25 14:01:38 +0000180
bart005dc972008-03-29 14:42:59 +0000181 case VG_USERREQ__DRD_START_TRACE_ADDR:
182 drd_start_tracing_address_range(arg[1], arg[1] + arg[2]);
183 break;
184
185 case VG_USERREQ__DRD_STOP_TRACE_ADDR:
186 drd_stop_tracing_address_range(arg[1], arg[1] + arg[2]);
bart3772a982008-03-15 08:11:03 +0000187 break;
bart5bd9f2d2008-03-03 20:31:58 +0000188
bartbf3a60c2008-04-04 19:10:21 +0000189 case VG_USERREQ__DRD_STOP_RECORDING:
190 thread_stop_recording(drd_tid);
191 break;
192
193 case VG_USERREQ__DRD_START_RECORDING:
194 thread_start_recording(drd_tid);
195 break;
196
bart3772a982008-03-15 08:11:03 +0000197 case VG_USERREQ__SET_PTHREADID:
bart145fe1c2008-04-21 17:12:45 +0000198 // pthread_self() returns 0 for programs not linked with libpthread.so.
199 if (arg[1] != INVALID_POSIX_THREADID)
200 thread_set_pthreadid(drd_tid, arg[1]);
bart3772a982008-03-15 08:11:03 +0000201 break;
sewardjaf44c822007-11-25 14:01:38 +0000202
bart3772a982008-03-15 08:11:03 +0000203 case VG_USERREQ__SET_JOINABLE:
204 thread_set_joinable(PtThreadIdToDrdThreadId(arg[1]), (Bool)arg[2]);
205 break;
sewardjaf44c822007-11-25 14:01:38 +0000206
bart3772a982008-03-15 08:11:03 +0000207 case VG_USERREQ__POST_THREAD_JOIN:
208 tl_assert(arg[1]);
209 drd_post_thread_join(drd_tid,
210 PtThreadIdToDrdThreadId(arg[1]));
211 break;
sewardjaf44c822007-11-25 14:01:38 +0000212
bart3772a982008-03-15 08:11:03 +0000213 case VG_USERREQ__PRE_MUTEX_INIT:
214 if (thread_enter_synchr(drd_tid) == 0)
215 drd_pre_mutex_init(arg[1], arg[2]);
216 break;
bart0268dfa2008-03-11 20:10:21 +0000217
bart3772a982008-03-15 08:11:03 +0000218 case VG_USERREQ__POST_MUTEX_INIT:
219 thread_leave_synchr(drd_tid);
220 break;
bart0268dfa2008-03-11 20:10:21 +0000221
bart3772a982008-03-15 08:11:03 +0000222 case VG_USERREQ__PRE_MUTEX_DESTROY:
223 thread_enter_synchr(drd_tid);
224 break;
sewardjaf44c822007-11-25 14:01:38 +0000225
bart3772a982008-03-15 08:11:03 +0000226 case VG_USERREQ__POST_MUTEX_DESTROY:
227 if (thread_leave_synchr(drd_tid) == 0)
228 drd_post_mutex_destroy(arg[1], arg[2]);
229 break;
sewardjaf44c822007-11-25 14:01:38 +0000230
bart3772a982008-03-15 08:11:03 +0000231 case VG_USERREQ__PRE_MUTEX_LOCK:
232 if (thread_enter_synchr(drd_tid) == 0)
bart2e3a3c12008-03-24 08:33:47 +0000233 drd_pre_mutex_lock(arg[1], arg[2], arg[3]);
bart3772a982008-03-15 08:11:03 +0000234 break;
sewardjaf44c822007-11-25 14:01:38 +0000235
bart3772a982008-03-15 08:11:03 +0000236 case VG_USERREQ__POST_MUTEX_LOCK:
237 if (thread_leave_synchr(drd_tid) == 0)
238 drd_post_mutex_lock(arg[1], arg[2]);
239 break;
sewardjaf44c822007-11-25 14:01:38 +0000240
bart3772a982008-03-15 08:11:03 +0000241 case VG_USERREQ__PRE_MUTEX_UNLOCK:
242 if (thread_enter_synchr(drd_tid) == 0)
243 drd_pre_mutex_unlock(arg[1], arg[2]);
244 break;
bart0268dfa2008-03-11 20:10:21 +0000245
bart3772a982008-03-15 08:11:03 +0000246 case VG_USERREQ__POST_MUTEX_UNLOCK:
247 thread_leave_synchr(drd_tid);
248 break;
sewardjaf44c822007-11-25 14:01:38 +0000249
bartf4f05812008-07-07 08:10:56 +0000250 case VG_USERREQ__PRE_SPIN_INIT_OR_UNLOCK:
251 if (thread_enter_synchr(drd_tid) == 0)
252 drd_spin_init_or_unlock(arg[1]);
253 break;
254
255 case VG_USERREQ__POST_SPIN_INIT_OR_UNLOCK:
256 thread_leave_synchr(drd_tid);
bart3772a982008-03-15 08:11:03 +0000257 break;
sewardjaf44c822007-11-25 14:01:38 +0000258
bart3772a982008-03-15 08:11:03 +0000259 case VG_USERREQ__PRE_COND_INIT:
260 tl_assert(thread_get_synchr_nesting_count(drd_tid) == 0);
261 drd_pre_cond_init(arg[1]);
262 break;
sewardjaf44c822007-11-25 14:01:38 +0000263
bart3772a982008-03-15 08:11:03 +0000264 case VG_USERREQ__POST_COND_DESTROY:
265 tl_assert(thread_get_synchr_nesting_count(drd_tid) == 0);
266 drd_post_cond_destroy(arg[1]);
267 break;
sewardjaf44c822007-11-25 14:01:38 +0000268
bart3772a982008-03-15 08:11:03 +0000269 case VG_USERREQ__PRE_COND_WAIT:
270 if (thread_enter_synchr(drd_tid) == 0)
271 drd_pre_cond_wait(arg[1], arg[2], arg[3]);
272 break;
sewardjaf44c822007-11-25 14:01:38 +0000273
bart3772a982008-03-15 08:11:03 +0000274 case VG_USERREQ__POST_COND_WAIT:
275 if (thread_leave_synchr(drd_tid) == 0)
276 drd_post_cond_wait(arg[1], arg[2], arg[3]);
277 break;
sewardjaf44c822007-11-25 14:01:38 +0000278
bart3772a982008-03-15 08:11:03 +0000279 case VG_USERREQ__PRE_COND_SIGNAL:
280 tl_assert(thread_get_synchr_nesting_count(drd_tid) == 0);
281 drd_pre_cond_signal(arg[1]);
282 break;
sewardjaf44c822007-11-25 14:01:38 +0000283
bart3772a982008-03-15 08:11:03 +0000284 case VG_USERREQ__PRE_COND_BROADCAST:
285 tl_assert(thread_get_synchr_nesting_count(drd_tid) == 0);
286 drd_pre_cond_broadcast(arg[1]);
287 break;
sewardjaf44c822007-11-25 14:01:38 +0000288
bart3772a982008-03-15 08:11:03 +0000289 case VG_USERREQ__PRE_SEM_INIT:
290 if (thread_enter_synchr(drd_tid) == 0)
291 drd_semaphore_init(arg[1], arg[2], arg[3]);
292 break;
sewardj85642922008-01-14 11:54:56 +0000293
bart3772a982008-03-15 08:11:03 +0000294 case VG_USERREQ__POST_SEM_INIT:
295 thread_leave_synchr(drd_tid);
296 break;
bart0268dfa2008-03-11 20:10:21 +0000297
bart3772a982008-03-15 08:11:03 +0000298 case VG_USERREQ__PRE_SEM_DESTROY:
299 thread_enter_synchr(drd_tid);
300 break;
bart0268dfa2008-03-11 20:10:21 +0000301
bart3772a982008-03-15 08:11:03 +0000302 case VG_USERREQ__POST_SEM_DESTROY:
303 if (thread_leave_synchr(drd_tid) == 0)
304 drd_semaphore_destroy(arg[1]);
305 break;
sewardj85642922008-01-14 11:54:56 +0000306
bart3772a982008-03-15 08:11:03 +0000307 case VG_USERREQ__PRE_SEM_WAIT:
308 if (thread_enter_synchr(drd_tid) == 0)
309 drd_semaphore_pre_wait(drd_tid, arg[1]);
310 break;
bart28230a32008-02-29 17:27:03 +0000311
bart3772a982008-03-15 08:11:03 +0000312 case VG_USERREQ__POST_SEM_WAIT:
313 if (thread_leave_synchr(drd_tid) == 0)
314 drd_semaphore_post_wait(drd_tid, arg[1], arg[2]);
315 break;
sewardj85642922008-01-14 11:54:56 +0000316
bart3772a982008-03-15 08:11:03 +0000317 case VG_USERREQ__PRE_SEM_POST:
318 if (thread_enter_synchr(drd_tid) == 0)
319 drd_semaphore_pre_post(drd_tid, arg[1]);
320 break;
sewardj85642922008-01-14 11:54:56 +0000321
bart3772a982008-03-15 08:11:03 +0000322 case VG_USERREQ__POST_SEM_POST:
323 if (thread_leave_synchr(drd_tid) == 0)
324 drd_semaphore_post_post(drd_tid, arg[1], arg[2]);
325 break;
sewardj85642922008-01-14 11:54:56 +0000326
bart3772a982008-03-15 08:11:03 +0000327 case VG_USERREQ__PRE_BARRIER_INIT:
328 if (thread_enter_synchr(drd_tid) == 0)
329 drd_barrier_init(arg[1], arg[2], arg[3], arg[4]);
330 break;
sewardj85642922008-01-14 11:54:56 +0000331
bart3772a982008-03-15 08:11:03 +0000332 case VG_USERREQ__POST_BARRIER_INIT:
333 thread_leave_synchr(drd_tid);
334 break;
bart0268dfa2008-03-11 20:10:21 +0000335
bart3772a982008-03-15 08:11:03 +0000336 case VG_USERREQ__PRE_BARRIER_DESTROY:
337 thread_enter_synchr(drd_tid);
338 break;
bart0268dfa2008-03-11 20:10:21 +0000339
bart3772a982008-03-15 08:11:03 +0000340 case VG_USERREQ__POST_BARRIER_DESTROY:
341 if (thread_leave_synchr(drd_tid) == 0)
342 drd_barrier_destroy(arg[1], arg[2]);
343 break;
sewardj85642922008-01-14 11:54:56 +0000344
bart3772a982008-03-15 08:11:03 +0000345 case VG_USERREQ__PRE_BARRIER_WAIT:
346 if (thread_enter_synchr(drd_tid) == 0)
347 drd_barrier_pre_wait(drd_tid, arg[1], arg[2]);
348 break;
sewardj85642922008-01-14 11:54:56 +0000349
bart3772a982008-03-15 08:11:03 +0000350 case VG_USERREQ__POST_BARRIER_WAIT:
351 if (thread_leave_synchr(drd_tid) == 0)
352 drd_barrier_post_wait(drd_tid, arg[1], arg[2], arg[3]);
353 break;
sewardj85642922008-01-14 11:54:56 +0000354
bart3772a982008-03-15 08:11:03 +0000355 case VG_USERREQ__PRE_RWLOCK_INIT:
356 rwlock_pre_init(arg[1]);
357 break;
bart00344642008-03-01 15:27:41 +0000358
bart3772a982008-03-15 08:11:03 +0000359 case VG_USERREQ__POST_RWLOCK_DESTROY:
360 rwlock_post_destroy(arg[1]);
361 break;
bart00344642008-03-01 15:27:41 +0000362
bart3772a982008-03-15 08:11:03 +0000363 case VG_USERREQ__PRE_RWLOCK_RDLOCK:
364 if (thread_enter_synchr(drd_tid) == 0)
365 rwlock_pre_rdlock(arg[1]);
366 break;
bart00344642008-03-01 15:27:41 +0000367
bart3772a982008-03-15 08:11:03 +0000368 case VG_USERREQ__POST_RWLOCK_RDLOCK:
369 if (thread_leave_synchr(drd_tid) == 0)
370 rwlock_post_rdlock(arg[1], arg[2]);
371 break;
bart00344642008-03-01 15:27:41 +0000372
bart3772a982008-03-15 08:11:03 +0000373 case VG_USERREQ__PRE_RWLOCK_WRLOCK:
374 if (thread_enter_synchr(drd_tid) == 0)
375 rwlock_pre_wrlock(arg[1]);
376 break;
bart00344642008-03-01 15:27:41 +0000377
bart3772a982008-03-15 08:11:03 +0000378 case VG_USERREQ__POST_RWLOCK_WRLOCK:
379 if (thread_leave_synchr(drd_tid) == 0)
380 rwlock_post_wrlock(arg[1], arg[2]);
381 break;
bart00344642008-03-01 15:27:41 +0000382
bart3772a982008-03-15 08:11:03 +0000383 case VG_USERREQ__PRE_RWLOCK_UNLOCK:
384 if (thread_enter_synchr(drd_tid) == 0)
385 rwlock_pre_unlock(arg[1]);
386 break;
bart0268dfa2008-03-11 20:10:21 +0000387
bart3772a982008-03-15 08:11:03 +0000388 case VG_USERREQ__POST_RWLOCK_UNLOCK:
389 thread_leave_synchr(drd_tid);
390 break;
bart00344642008-03-01 15:27:41 +0000391
bart3772a982008-03-15 08:11:03 +0000392 default:
393 VG_(message)(Vg_DebugMsg, "Unrecognized client request 0x%lx 0x%lx",
394 arg[0], arg[1]);
395 tl_assert(0);
396 return False;
397 }
sewardjaf44c822007-11-25 14:01:38 +0000398
bart3772a982008-03-15 08:11:03 +0000399 *ret = result;
400 return True;
sewardjaf44c822007-11-25 14:01:38 +0000401}
402
403void drd_clientreq_init(void)
404{
bart3772a982008-03-15 08:11:03 +0000405 VG_(needs_client_requests)(drd_handle_client_request);
sewardjaf44c822007-11-25 14:01:38 +0000406}