1 /* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
3 This file is part of drd, a thread error detector.
5 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
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.
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.
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
22 The GNU General Public License is contained in the file COPYING.
26 #include "drd_basics.h"
27 #include "drd_clientobj.h"
28 #include "drd_error.h"
29 #include "drd_mutex.h"
30 #include "pub_tool_vki.h"
31 #include "pub_tool_errormgr.h" /* VG_(maybe_record_error)() */
32 #include "pub_tool_libcassert.h" /* tl_assert() */
33 #include "pub_tool_libcbase.h" /* VG_(strlen) */
34 #include "pub_tool_libcprint.h" /* VG_(message)() */
35 #include "pub_tool_libcproc.h" /* VG_(read_millisecond_timer)() */
36 #include "pub_tool_machine.h" /* VG_(get_IP)() */
37 #include "pub_tool_threadstate.h" /* VG_(get_running_tid)() */
40 /* Local functions. */
42 static void mutex_cleanup(struct mutex_info
* p
);
43 static Bool
mutex_is_locked(struct mutex_info
* const p
);
44 static void mutex_delete_thread(struct mutex_info
* p
, const DrdThreadId tid
);
47 /* Local variables. */
49 static Bool s_trace_mutex
;
50 static ULong s_mutex_lock_count
;
51 static ULong s_mutex_segment_creation_count
;
52 static UInt s_mutex_lock_threshold_ms
;
55 /* Function definitions. */
57 void DRD_(mutex_set_trace
)(const Bool trace_mutex
)
59 tl_assert(!! trace_mutex
== trace_mutex
);
60 s_trace_mutex
= trace_mutex
;
63 void DRD_(mutex_set_lock_threshold
)(const UInt lock_threshold_ms
)
65 s_mutex_lock_threshold_ms
= lock_threshold_ms
;
69 void DRD_(mutex_initialize
)(struct mutex_info
* const p
,
70 const Addr mutex
, const MutexT mutex_type
)
73 tl_assert(p
->a1
== mutex
);
75 p
->cleanup
= (void(*)(DrdClientobj
*))mutex_cleanup
;
77 = (void(*)(DrdClientobj
*, DrdThreadId
))mutex_delete_thread
;
78 p
->mutex_type
= mutex_type
;
79 p
->recursion_count
= 0;
80 p
->owner
= DRD_INVALID_THREADID
;
81 p
->last_locked_segment
= 0;
82 p
->acquiry_time_ms
= 0;
86 /** Deallocate the memory that was allocated by mutex_initialize(). */
87 static void mutex_cleanup(struct mutex_info
* p
)
92 DRD_(trace_msg
)("[%d] mutex_destroy %s 0x%lx rc %d owner %d",
93 DRD_(thread_get_running_tid
)(),
94 DRD_(mutex_get_typename
)(p
), p
->a1
,
95 p
? p
->recursion_count
: -1,
96 p
? p
->owner
: DRD_INVALID_THREADID
);
98 if (mutex_is_locked(p
))
100 MutexErrInfo MEI
= { DRD_(thread_get_running_tid
)(),
101 p
->a1
, p
->recursion_count
, p
->owner
};
102 VG_(maybe_record_error
)(VG_(get_running_tid
)(),
104 VG_(get_IP
)(VG_(get_running_tid
)()),
105 "Destroying locked mutex",
109 DRD_(sg_put
)(p
->last_locked_segment
);
110 p
->last_locked_segment
= 0;
113 /** Report that address 'mutex' is not the address of a mutex object. */
114 void DRD_(not_a_mutex
)(const Addr mutex
)
116 MutexErrInfo MEI
= { DRD_(thread_get_running_tid
)(),
117 mutex
, -1, DRD_INVALID_THREADID
};
118 VG_(maybe_record_error
)(VG_(get_running_tid
)(),
120 VG_(get_IP
)(VG_(get_running_tid
)()),
126 * Report that address 'mutex' is not the address of a mutex object of the
129 static void wrong_mutex_type(const Addr mutex
)
131 MutexErrInfo MEI
= { DRD_(thread_get_running_tid
)(),
132 mutex
, -1, DRD_INVALID_THREADID
};
133 VG_(maybe_record_error
)(VG_(get_running_tid
)(),
135 VG_(get_IP
)(VG_(get_running_tid
)()),
136 "Mutex type mismatch",
142 DRD_(mutex_get_or_allocate
)(const Addr mutex
, const MutexT mutex_type
)
144 struct mutex_info
* p
;
146 tl_assert(offsetof(DrdClientobj
, mutex
) == 0);
147 p
= &(DRD_(clientobj_get
)(mutex
, ClientMutex
)->mutex
);
150 if (mutex_type
== mutex_type_unknown
|| p
->mutex_type
== mutex_type
)
154 wrong_mutex_type(mutex
);
159 if (DRD_(clientobj_present
)(mutex
, mutex
+ 1))
161 DRD_(not_a_mutex
)(mutex
);
165 p
= &(DRD_(clientobj_add
)(mutex
, ClientMutex
)->mutex
);
166 DRD_(mutex_initialize
)(p
, mutex
, mutex_type
);
170 struct mutex_info
* DRD_(mutex_get
)(const Addr mutex
)
172 tl_assert(offsetof(DrdClientobj
, mutex
) == 0);
173 return &(DRD_(clientobj_get
)(mutex
, ClientMutex
)->mutex
);
176 /** Called before pthread_mutex_init(). */
178 DRD_(mutex_init
)(const Addr mutex
, const MutexT mutex_type
)
180 struct mutex_info
* p
;
183 DRD_(trace_msg
)("[%d] mutex_init %s 0x%lx",
184 DRD_(thread_get_running_tid
)(),
185 DRD_(mutex_type_name
)(mutex_type
),
188 if (mutex_type
== mutex_type_invalid_mutex
)
190 DRD_(not_a_mutex
)(mutex
);
194 p
= DRD_(mutex_get
)(mutex
);
197 const ThreadId vg_tid
= VG_(get_running_tid
)();
198 MutexErrInfo MEI
= { DRD_(thread_get_running_tid
)(),
199 p
->a1
, p
->recursion_count
, p
->owner
};
200 VG_(maybe_record_error
)(vg_tid
,
203 "Mutex reinitialization",
205 p
->mutex_type
= mutex_type
;
208 p
= DRD_(mutex_get_or_allocate
)(mutex
, mutex_type
);
213 /** Called after pthread_mutex_destroy(). */
214 void DRD_(mutex_post_destroy
)(const Addr mutex
)
216 struct mutex_info
* p
;
218 p
= DRD_(mutex_get
)(mutex
);
221 DRD_(not_a_mutex
)(mutex
);
225 DRD_(clientobj_remove
)(mutex
, ClientMutex
);
229 * Called before pthread_mutex_lock() is invoked. If a data structure for the
230 * client-side object was not yet created, do this now. Also check whether an
231 * attempt is made to lock recursively a synchronization object that must not
232 * be locked recursively.
234 void DRD_(mutex_pre_lock
)(const Addr mutex
, MutexT mutex_type
,
237 struct mutex_info
* p
;
239 p
= DRD_(mutex_get_or_allocate
)(mutex
, mutex_type
);
240 if (p
&& mutex_type
== mutex_type_unknown
)
241 mutex_type
= p
->mutex_type
;
244 DRD_(trace_msg
)("[%d] %s %s 0x%lx rc %d owner %d",
245 DRD_(thread_get_running_tid
)(),
246 trylock
? "pre_mutex_lock " : "mutex_trylock ",
247 p
? DRD_(mutex_get_typename
)(p
) : "(?)",
248 mutex
, p
? p
->recursion_count
: -1,
249 p
? p
->owner
: DRD_INVALID_THREADID
);
253 DRD_(not_a_mutex
)(mutex
);
259 if (mutex_type
== mutex_type_invalid_mutex
)
261 DRD_(not_a_mutex
)(mutex
);
266 && p
->owner
== DRD_(thread_get_running_tid
)()
267 && p
->recursion_count
>= 1
268 && mutex_type
!= mutex_type_recursive_mutex
)
270 MutexErrInfo MEI
= { DRD_(thread_get_running_tid
)(),
271 p
->a1
, p
->recursion_count
, p
->owner
};
272 VG_(maybe_record_error
)(VG_(get_running_tid
)(),
274 VG_(get_IP
)(VG_(get_running_tid
)()),
275 "Recursive locking not allowed",
281 * Update mutex_info state when locking the pthread_mutex_t mutex.
282 * Note: this function must be called after pthread_mutex_lock() has been
283 * called, or a race condition is triggered !
285 void DRD_(mutex_post_lock
)(const Addr mutex
, const Bool took_lock
,
286 const Bool post_cond_wait
)
288 const DrdThreadId drd_tid
= DRD_(thread_get_running_tid
)();
289 struct mutex_info
* p
;
291 p
= DRD_(mutex_get
)(mutex
);
294 DRD_(trace_msg
)("[%d] %s %s 0x%lx rc %d owner %d%s",
296 post_cond_wait
? "cond_post_wait " : "post_mutex_lock",
297 p
? DRD_(mutex_get_typename
)(p
) : "(?)",
298 mutex
, p
? p
->recursion_count
: 0,
299 p
? p
->owner
: VG_INVALID_THREADID
,
300 took_lock
? "" : " (locking failed)");
302 if (! p
|| ! took_lock
)
305 if (p
->recursion_count
== 0) {
306 if (p
->owner
!= drd_tid
&& p
->owner
!= DRD_INVALID_THREADID
)
308 tl_assert(p
->last_locked_segment
);
310 DRD_(thread_new_segment_and_combine_vc
)(drd_tid
,
311 p
->last_locked_segment
);
314 DRD_(thread_new_segment
)(drd_tid
);
316 s_mutex_segment_creation_count
++;
319 p
->acquiry_time_ms
= VG_(read_millisecond_timer
)();
320 p
->acquired_at
= VG_(record_ExeContext
)(VG_(get_running_tid
)(), 0);
321 s_mutex_lock_count
++;
322 } else if (p
->owner
!= drd_tid
) {
323 const ThreadId vg_tid
= VG_(get_running_tid
)();
324 MutexErrInfo MEI
= { DRD_(thread_get_running_tid
)(),
325 p
->a1
, p
->recursion_count
, p
->owner
};
326 VG_(maybe_record_error
)(vg_tid
,
329 "The impossible happened: mutex is locked"
330 " simultaneously by two threads",
334 p
->recursion_count
++;
338 * Update mutex_info state when unlocking the pthread_mutex_t mutex.
340 * @param[in] mutex Address of the client mutex.
341 * @param[in] mutex_type Mutex type.
343 * @return New value of the mutex recursion count.
345 * @note This function must be called before pthread_mutex_unlock() is called,
346 * or a race condition is triggered !
348 void DRD_(mutex_unlock
)(const Addr mutex
, MutexT mutex_type
)
350 const DrdThreadId drd_tid
= DRD_(thread_get_running_tid
)();
351 const ThreadId vg_tid
= VG_(get_running_tid
)();
352 struct mutex_info
* p
;
354 p
= DRD_(mutex_get
)(mutex
);
355 if (p
&& mutex_type
== mutex_type_unknown
)
356 mutex_type
= p
->mutex_type
;
359 DRD_(trace_msg
)("[%d] mutex_unlock %s 0x%lx rc %d",
360 drd_tid
, p
? DRD_(mutex_get_typename
)(p
) : "(?)",
361 mutex
, p
? p
->recursion_count
: 0);
364 if (p
== 0 || mutex_type
== mutex_type_invalid_mutex
)
366 DRD_(not_a_mutex
)(mutex
);
370 if (p
->owner
== DRD_INVALID_THREADID
)
372 MutexErrInfo MEI
= { DRD_(thread_get_running_tid
)(),
373 p
->a1
, p
->recursion_count
, p
->owner
};
374 VG_(maybe_record_error
)(vg_tid
,
383 if (p
->mutex_type
!= mutex_type
) {
384 MutexErrInfo MEI
= { DRD_(thread_get_running_tid
)(),
385 p
->a1
, p
->recursion_count
, p
->owner
};
386 VG_(maybe_record_error
)(vg_tid
, MutexErr
, VG_(get_IP
)(vg_tid
),
387 "Mutex type changed", &MEI
);
389 tl_assert(p
->mutex_type
== mutex_type
);
390 tl_assert(p
->owner
!= DRD_INVALID_THREADID
);
392 if (p
->owner
!= drd_tid
|| p
->recursion_count
<= 0)
394 MutexErrInfo MEI
= { DRD_(thread_get_running_tid
)(),
395 p
->a1
, p
->recursion_count
, p
->owner
};
396 VG_(maybe_record_error
)(vg_tid
,
399 "Mutex not locked by calling thread",
403 tl_assert(p
->recursion_count
> 0);
404 p
->recursion_count
--;
405 tl_assert(p
->recursion_count
>= 0);
407 if (p
->recursion_count
== 0)
409 if (s_mutex_lock_threshold_ms
> 0)
411 Long held
= VG_(read_millisecond_timer
)() - p
->acquiry_time_ms
;
412 if (held
> s_mutex_lock_threshold_ms
)
415 = { DRD_(thread_get_running_tid
)(),
416 mutex
, p
->acquired_at
, held
, s_mutex_lock_threshold_ms
};
417 VG_(maybe_record_error
)(vg_tid
,
425 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
426 /* current vector clock of the thread such that it is available when */
427 /* this mutex is locked again. */
429 DRD_(thread_get_latest_segment
)(&p
->last_locked_segment
, drd_tid
);
430 DRD_(thread_new_segment
)(drd_tid
);
432 s_mutex_segment_creation_count
++;
436 void DRD_(spinlock_init_or_unlock
)(const Addr spinlock
)
438 struct mutex_info
* mutex_p
= DRD_(mutex_get
)(spinlock
);
441 DRD_(mutex_unlock
)(spinlock
, mutex_type_spinlock
);
445 DRD_(mutex_init
)(spinlock
, mutex_type_spinlock
);
449 const char* DRD_(mutex_get_typename
)(struct mutex_info
* const p
)
453 return DRD_(mutex_type_name
)(p
->mutex_type
);
456 const char* DRD_(mutex_type_name
)(const MutexT mt
)
460 case mutex_type_unknown
:
462 case mutex_type_invalid_mutex
:
463 return "invalid mutex";
464 case mutex_type_recursive_mutex
:
465 return "recursive mutex";
466 case mutex_type_errorcheck_mutex
:
467 return "error checking mutex";
468 case mutex_type_default_mutex
:
470 case mutex_type_spinlock
:
477 /** Return true if the specified mutex is locked by any thread. */
478 static Bool
mutex_is_locked(struct mutex_info
* const p
)
481 return (p
->recursion_count
> 0);
484 Bool
DRD_(mutex_is_locked_by
)(const Addr mutex
, const DrdThreadId tid
)
486 struct mutex_info
* const p
= DRD_(mutex_get
)(mutex
);
489 return (p
->recursion_count
> 0 && p
->owner
== tid
);
494 int DRD_(mutex_get_recursion_count
)(const Addr mutex
)
496 struct mutex_info
* const p
= DRD_(mutex_get
)(mutex
);
498 return p
->recursion_count
;
502 * Call this function when thread tid stops to exist, such that the
503 * "last owner" field can be cleared if it still refers to that thread.
505 static void mutex_delete_thread(struct mutex_info
* p
, const DrdThreadId tid
)
509 if (p
->owner
== tid
&& p
->recursion_count
> 0)
511 MutexErrInfo MEI
= { DRD_(thread_get_running_tid
)(),
512 p
->a1
, p
->recursion_count
, p
->owner
};
513 VG_(maybe_record_error
)(VG_(get_running_tid
)(),
515 VG_(get_IP
)(VG_(get_running_tid
)()),
516 "Mutex still locked at thread exit",
518 p
->owner
= VG_INVALID_THREADID
;
522 ULong
DRD_(get_mutex_lock_count
)(void)
524 return s_mutex_lock_count
;
527 ULong
DRD_(get_mutex_segment_creation_count
)(void)
529 return s_mutex_segment_creation_count
;