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)
93 VG_(message)(Vg_UserMsg,
94 "[%d] mutex_destroy %s 0x%lx rc %d owner %d\n",
95 DRD_(thread_get_running_tid)(),
96 DRD_(mutex_get_typename)(p),
98 p ? p->recursion_count : -1,
99 p ? p->owner : DRD_INVALID_THREADID);
102 if (mutex_is_locked(p))
104 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
105 p->a1, p->recursion_count, p->owner };
106 VG_(maybe_record_error)(VG_(get_running_tid)(),
108 VG_(get_IP)(VG_(get_running_tid)()),
109 "Destroying locked mutex",
113 DRD_(sg_put)(p->last_locked_segment);
114 p->last_locked_segment = 0;
117 /** Report that address 'mutex' is not the address of a mutex object. */
118 void DRD_(not_a_mutex)(const Addr mutex)
120 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
121 mutex, -1, DRD_INVALID_THREADID };
122 VG_(maybe_record_error)(VG_(get_running_tid)(),
124 VG_(get_IP)(VG_(get_running_tid)()),
130 * Report that address 'mutex' is not the address of a mutex object of the
133 static void wrong_mutex_type(const Addr mutex)
135 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
136 mutex, -1, DRD_INVALID_THREADID };
137 VG_(maybe_record_error)(VG_(get_running_tid)(),
139 VG_(get_IP)(VG_(get_running_tid)()),
140 "Mutex type mismatch",
146 DRD_(mutex_get_or_allocate)(const Addr mutex, const MutexT mutex_type)
148 struct mutex_info* p;
150 tl_assert(offsetof(DrdClientobj, mutex) == 0);
151 p = &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex);
154 if (mutex_type == mutex_type_unknown || p->mutex_type == mutex_type)
158 wrong_mutex_type(mutex);
163 if (DRD_(clientobj_present)(mutex, mutex + 1))
165 DRD_(not_a_mutex)(mutex);
169 p = &(DRD_(clientobj_add)(mutex, ClientMutex)->mutex);
170 DRD_(mutex_initialize)(p, mutex, mutex_type);
174 struct mutex_info* DRD_(mutex_get)(const Addr mutex)
176 tl_assert(offsetof(DrdClientobj, mutex) == 0);
177 return &(DRD_(clientobj_get)(mutex, ClientMutex)->mutex);
180 /** Called before pthread_mutex_init(). */
182 DRD_(mutex_init)(const Addr mutex, const MutexT mutex_type)
184 struct mutex_info* p;
188 VG_(message)(Vg_UserMsg,
189 "[%d] mutex_init %s 0x%lx\n",
190 DRD_(thread_get_running_tid)(),
191 DRD_(mutex_type_name)(mutex_type),
195 if (mutex_type == mutex_type_invalid_mutex)
197 DRD_(not_a_mutex)(mutex);
201 p = DRD_(mutex_get)(mutex);
204 const ThreadId vg_tid = VG_(get_running_tid)();
205 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
206 p->a1, p->recursion_count, p->owner };
207 VG_(maybe_record_error)(vg_tid,
210 "Mutex reinitialization",
212 p->mutex_type = mutex_type;
215 p = DRD_(mutex_get_or_allocate)(mutex, mutex_type);
220 /** Called after pthread_mutex_destroy(). */
221 void DRD_(mutex_post_destroy)(const Addr mutex)
223 struct mutex_info* p;
225 p = DRD_(mutex_get)(mutex);
228 DRD_(not_a_mutex)(mutex);
232 DRD_(clientobj_remove)(mutex, ClientMutex);
236 * Called before pthread_mutex_lock() is invoked. If a data structure for the
237 * client-side object was not yet created, do this now. Also check whether an
238 * attempt is made to lock recursively a synchronization object that must not
239 * be locked recursively.
241 void DRD_(mutex_pre_lock)(const Addr mutex, MutexT mutex_type,
244 struct mutex_info* p;
246 p = DRD_(mutex_get_or_allocate)(mutex, mutex_type);
247 if (p && mutex_type == mutex_type_unknown)
248 mutex_type = p->mutex_type;
252 VG_(message)(Vg_UserMsg,
253 "[%d] %s %s 0x%lx rc %d owner %d\n",
254 DRD_(thread_get_running_tid)(),
255 trylock ? "pre_mutex_lock " : "mutex_trylock ",
256 p ? DRD_(mutex_get_typename)(p) : "(?)",
258 p ? p->recursion_count : -1,
259 p ? p->owner : DRD_INVALID_THREADID);
264 DRD_(not_a_mutex)(mutex);
270 if (mutex_type == mutex_type_invalid_mutex)
272 DRD_(not_a_mutex)(mutex);
277 && p->owner == DRD_(thread_get_running_tid)()
278 && p->recursion_count >= 1
279 && mutex_type != mutex_type_recursive_mutex)
281 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
282 p->a1, p->recursion_count, p->owner };
283 VG_(maybe_record_error)(VG_(get_running_tid)(),
285 VG_(get_IP)(VG_(get_running_tid)()),
286 "Recursive locking not allowed",
292 * Update mutex_info state when locking the pthread_mutex_t mutex.
293 * Note: this function must be called after pthread_mutex_lock() has been
294 * called, or a race condition is triggered !
296 void DRD_(mutex_post_lock)(const Addr mutex, const Bool took_lock,
297 const Bool post_cond_wait)
299 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
300 struct mutex_info* p;
302 p = DRD_(mutex_get)(mutex);
306 VG_(message)(Vg_UserMsg,
307 "[%d] %s %s 0x%lx rc %d owner %d%s\n",
309 post_cond_wait ? "cond_post_wait " : "post_mutex_lock",
310 p ? DRD_(mutex_get_typename)(p) : "(?)",
312 p ? p->recursion_count : 0,
313 p ? p->owner : VG_INVALID_THREADID,
314 took_lock ? "" : " (locking failed)");
317 if (! p || ! took_lock)
320 if (p->recursion_count == 0)
322 if (p->owner != drd_tid && p->owner != DRD_INVALID_THREADID)
324 tl_assert(p->last_locked_segment);
326 DRD_(thread_new_segment_and_combine_vc)(drd_tid,
327 p->last_locked_segment);
330 DRD_(thread_new_segment)(drd_tid);
332 s_mutex_segment_creation_count++;
335 p->acquiry_time_ms = VG_(read_millisecond_timer)();
336 p->acquired_at = VG_(record_ExeContext)(VG_(get_running_tid)(), 0);
337 s_mutex_lock_count++;
339 else if (p->owner != drd_tid)
341 VG_(message)(Vg_UserMsg,
342 "The impossible happened: mutex 0x%lx is locked"
343 " simultaneously by two threads (recursion count %d,"
344 " owners %d and %d) !\n",
345 p->a1, p->recursion_count, p->owner, drd_tid);
348 p->recursion_count++;
352 * Update mutex_info state when unlocking the pthread_mutex_t mutex.
354 * @param[in] mutex Address of the client mutex.
355 * @param[in] mutex_type Mutex type.
357 * @return New value of the mutex recursion count.
359 * @note This function must be called before pthread_mutex_unlock() is called,
360 * or a race condition is triggered !
362 void DRD_(mutex_unlock)(const Addr mutex, MutexT mutex_type)
364 const DrdThreadId drd_tid = DRD_(thread_get_running_tid)();
365 const ThreadId vg_tid = VG_(get_running_tid)();
366 struct mutex_info* p;
368 p = DRD_(mutex_get)(mutex);
369 if (p && mutex_type == mutex_type_unknown)
370 mutex_type = p->mutex_type;
374 VG_(message)(Vg_UserMsg,
375 "[%d] mutex_unlock %s 0x%lx rc %d\n",
377 p ? DRD_(mutex_get_typename)(p) : "(?)",
379 p ? p->recursion_count : 0);
382 if (p == 0 || mutex_type == mutex_type_invalid_mutex)
384 DRD_(not_a_mutex)(mutex);
388 if (p->owner == DRD_INVALID_THREADID)
390 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
391 p->a1, p->recursion_count, p->owner };
392 VG_(maybe_record_error)(vg_tid,
401 if (p->mutex_type != mutex_type)
403 VG_(message)(Vg_UserMsg, "??? mutex 0x%lx: type changed from %d into %d\n",
404 p->a1, p->mutex_type, mutex_type);
406 tl_assert(p->mutex_type == mutex_type);
407 tl_assert(p->owner != DRD_INVALID_THREADID);
409 if (p->owner != drd_tid || p->recursion_count <= 0)
411 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
412 p->a1, p->recursion_count, p->owner };
413 VG_(maybe_record_error)(vg_tid,
416 "Mutex not locked by calling thread",
420 tl_assert(p->recursion_count > 0);
421 p->recursion_count--;
422 tl_assert(p->recursion_count >= 0);
424 if (p->recursion_count == 0)
426 if (s_mutex_lock_threshold_ms > 0)
428 Long held = VG_(read_millisecond_timer)() - p->acquiry_time_ms;
429 if (held > s_mutex_lock_threshold_ms)
432 = { DRD_(thread_get_running_tid)(),
433 mutex, p->acquired_at, held, s_mutex_lock_threshold_ms };
434 VG_(maybe_record_error)(vg_tid,
442 /* This pthread_mutex_unlock() call really unlocks the mutex. Save the */
443 /* current vector clock of the thread such that it is available when */
444 /* this mutex is locked again. */
446 DRD_(thread_get_latest_segment)(&p->last_locked_segment, drd_tid);
447 DRD_(thread_new_segment)(drd_tid);
449 s_mutex_segment_creation_count++;
453 void DRD_(spinlock_init_or_unlock)(const Addr spinlock)
455 struct mutex_info* mutex_p = DRD_(mutex_get)(spinlock);
458 DRD_(mutex_unlock)(spinlock, mutex_type_spinlock);
462 DRD_(mutex_init)(spinlock, mutex_type_spinlock);
466 const char* DRD_(mutex_get_typename)(struct mutex_info* const p)
470 return DRD_(mutex_type_name)(p->mutex_type);
473 const char* DRD_(mutex_type_name)(const MutexT mt)
477 case mutex_type_unknown:
479 case mutex_type_invalid_mutex:
480 return "invalid mutex";
481 case mutex_type_recursive_mutex:
482 return "recursive mutex";
483 case mutex_type_errorcheck_mutex:
484 return "error checking mutex";
485 case mutex_type_default_mutex:
487 case mutex_type_spinlock:
494 /** Return true if the specified mutex is locked by any thread. */
495 static Bool mutex_is_locked(struct mutex_info* const p)
498 return (p->recursion_count > 0);
501 Bool DRD_(mutex_is_locked_by)(const Addr mutex, const DrdThreadId tid)
503 struct mutex_info* const p = DRD_(mutex_get)(mutex);
506 return (p->recursion_count > 0 && p->owner == tid);
511 int DRD_(mutex_get_recursion_count)(const Addr mutex)
513 struct mutex_info* const p = DRD_(mutex_get)(mutex);
515 return p->recursion_count;
519 * Call this function when thread tid stops to exist, such that the
520 * "last owner" field can be cleared if it still refers to that thread.
522 static void mutex_delete_thread(struct mutex_info* p, const DrdThreadId tid)
526 if (p->owner == tid && p->recursion_count > 0)
528 MutexErrInfo MEI = { DRD_(thread_get_running_tid)(),
529 p->a1, p->recursion_count, p->owner };
530 VG_(maybe_record_error)(VG_(get_running_tid)(),
532 VG_(get_IP)(VG_(get_running_tid)()),
533 "Mutex still locked at thread exit",
535 p->owner = VG_INVALID_THREADID;
539 ULong DRD_(get_mutex_lock_count)(void)
541 return s_mutex_lock_count;
544 ULong DRD_(get_mutex_segment_creation_count)(void)
546 return s_mutex_segment_creation_count;