coregrind/m_gdbserver/remote-utils.c (prepare_resume_reply): Use memcpy
[valgrind.git] / drd / drd_thread.c
blob891967b0394aafd398789e15f33f8c3d2e00093b
1 /*
2 This file is part of drd, a thread error detector.
4 Copyright (C) 2006-2020 Bart Van Assche <bvanassche@acm.org>.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
19 The GNU General Public License is contained in the file COPYING.
23 #include "drd_error.h"
24 #include "drd_barrier.h"
25 #include "drd_clientobj.h"
26 #include "drd_cond.h"
27 #include "drd_mutex.h"
28 #include "drd_segment.h"
29 #include "drd_semaphore.h"
30 #include "drd_suppression.h"
31 #include "drd_thread.h"
32 #include "pub_tool_vki.h"
33 #include "pub_tool_basics.h" // Addr, SizeT
34 #include "pub_tool_libcassert.h" // tl_assert()
35 #include "pub_tool_libcbase.h" // VG_(strlen)()
36 #include "pub_tool_libcprint.h" // VG_(printf)()
37 #include "pub_tool_machine.h"
38 #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
39 #include "pub_tool_options.h" // VG_(clo_backtrace_size)
40 #include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
44 /* Local functions. */
46 static void thread_append_segment(const DrdThreadId tid, Segment* const sg);
47 static void thread_discard_segment(const DrdThreadId tid, Segment* const sg);
48 static void thread_compute_conflict_set(struct bitmap** conflict_set,
49 const DrdThreadId tid);
50 static Bool thread_conflict_set_up_to_date(const DrdThreadId tid);
53 /* Local variables. */
55 static ULong s_context_switch_count;
56 static ULong s_discard_ordered_segments_count;
57 static ULong s_compute_conflict_set_count;
58 static ULong s_update_conflict_set_count;
59 static ULong s_update_conflict_set_new_sg_count;
60 static ULong s_update_conflict_set_sync_count;
61 static ULong s_update_conflict_set_join_count;
62 static ULong s_conflict_set_bitmap_creation_count;
63 static ULong s_conflict_set_bitmap2_creation_count;
64 static ThreadId s_vg_running_tid = VG_INVALID_THREADID;
65 DrdThreadId DRD_(g_drd_running_tid) = DRD_INVALID_THREADID;
66 ThreadInfo* DRD_(g_threadinfo);
67 struct bitmap* DRD_(g_conflict_set);
68 Bool DRD_(verify_conflict_set);
69 static Bool s_trace_context_switches = False;
70 static Bool s_trace_conflict_set = False;
71 static Bool s_trace_conflict_set_bm = False;
72 static Bool s_trace_fork_join = False;
73 static Bool s_segment_merging = True;
74 static Bool s_new_segments_since_last_merge;
75 static int s_segment_merge_interval = 10;
76 static unsigned s_join_list_vol = 10;
77 static unsigned s_deletion_head;
78 static unsigned s_deletion_tail;
79 #if defined(VGO_solaris)
80 Bool DRD_(ignore_thread_creation) = True;
81 #else
82 Bool DRD_(ignore_thread_creation) = False;
83 #endif /* VGO_solaris */
86 /* Function definitions. */
88 /** Enables/disables context switch tracing. */
89 void DRD_(thread_trace_context_switches)(const Bool t)
91 tl_assert(t == False || t == True);
92 s_trace_context_switches = t;
95 /** Enables/disables conflict set tracing. */
96 void DRD_(thread_trace_conflict_set)(const Bool t)
98 tl_assert(t == False || t == True);
99 s_trace_conflict_set = t;
102 /** Enables/disables conflict set bitmap tracing. */
103 void DRD_(thread_trace_conflict_set_bm)(const Bool t)
105 tl_assert(t == False || t == True);
106 s_trace_conflict_set_bm = t;
109 /** Report whether fork/join tracing is enabled. */
110 Bool DRD_(thread_get_trace_fork_join)(void)
112 return s_trace_fork_join;
115 /** Enables/disables fork/join tracing. */
116 void DRD_(thread_set_trace_fork_join)(const Bool t)
118 tl_assert(t == False || t == True);
119 s_trace_fork_join = t;
122 /** Enables/disables segment merging. */
123 void DRD_(thread_set_segment_merging)(const Bool m)
125 tl_assert(m == False || m == True);
126 s_segment_merging = m;
129 /** Get the segment merging interval. */
130 int DRD_(thread_get_segment_merge_interval)(void)
132 return s_segment_merge_interval;
135 /** Set the segment merging interval. */
136 void DRD_(thread_set_segment_merge_interval)(const int i)
138 s_segment_merge_interval = i;
141 void DRD_(thread_set_join_list_vol)(const int jlv)
143 s_join_list_vol = jlv;
146 void DRD_(thread_init)(void)
148 DRD_(g_threadinfo) = VG_(malloc)("drd.main.ti.1",
149 DRD_N_THREADS * sizeof DRD_(g_threadinfo)[0]);
150 for (UInt i = 0; i < DRD_N_THREADS; ++i) {
151 static ThreadInfo initval;
152 DRD_(g_threadinfo)[i] = initval;
157 * Convert Valgrind's ThreadId into a DrdThreadId.
159 * @return DRD thread ID upon success and DRD_INVALID_THREADID if the passed
160 * Valgrind ThreadId does not yet exist.
162 DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid)
164 UInt i;
166 if (tid == VG_INVALID_THREADID)
167 return DRD_INVALID_THREADID;
169 for (i = 1; i < DRD_N_THREADS; i++)
171 if (DRD_(g_threadinfo)[i].vg_thread_exists == True
172 && DRD_(g_threadinfo)[i].vg_threadid == tid)
174 return i;
178 return DRD_INVALID_THREADID;
181 /** Allocate a new DRD thread ID for the specified Valgrind thread ID. */
182 static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid)
184 UInt i;
186 tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID);
188 for (i = 1; i < DRD_N_THREADS; i++)
190 if (!DRD_(g_threadinfo)[i].valid)
192 tl_assert(! DRD_(IsValidDrdThreadId)(i));
194 DRD_(g_threadinfo)[i].valid = True;
195 DRD_(g_threadinfo)[i].vg_thread_exists = True;
196 DRD_(g_threadinfo)[i].vg_threadid = tid;
197 DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID;
198 DRD_(g_threadinfo)[i].stack_min = 0;
199 DRD_(g_threadinfo)[i].stack_min_min = 0;
200 DRD_(g_threadinfo)[i].stack_startup = 0;
201 DRD_(g_threadinfo)[i].stack_max = 0;
202 DRD_(thread_set_name)(i, "");
203 DRD_(g_threadinfo)[i].on_alt_stack = False;
204 DRD_(g_threadinfo)[i].is_recording_loads = True;
205 DRD_(g_threadinfo)[i].is_recording_stores = True;
206 DRD_(g_threadinfo)[i].pthread_create_nesting_level = 0;
207 DRD_(g_threadinfo)[i].synchr_nesting = 0;
208 DRD_(g_threadinfo)[i].deletion_seq = s_deletion_tail - 1;
209 DRD_(g_threadinfo)[i].creator_thread = DRD_INVALID_THREADID;
210 #if defined (VGO_solaris)
211 DRD_(g_threadinfo)[i].bind_guard_flag = 0;
212 #endif /* VGO_solaris */
214 tl_assert(DRD_(g_threadinfo)[i].sg_first == NULL);
215 tl_assert(DRD_(g_threadinfo)[i].sg_last == NULL);
217 tl_assert(DRD_(IsValidDrdThreadId)(i));
219 return i;
223 VG_(printf)(
224 "\nSorry, but the maximum number of threads supported by DRD has been exceeded."
225 "Aborting.\n");
227 tl_assert(False);
229 return DRD_INVALID_THREADID;
232 /** Convert a POSIX thread ID into a DRD thread ID. */
233 DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid)
235 UInt i;
237 if (tid != INVALID_POSIX_THREADID)
239 for (i = 1; i < DRD_N_THREADS; i++)
241 if (DRD_(g_threadinfo)[i].posix_thread_exists
242 && DRD_(g_threadinfo)[i].pt_threadid == tid)
244 return i;
248 return DRD_INVALID_THREADID;
251 /** Convert a DRD thread ID into a Valgrind thread ID. */
252 ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid)
254 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
255 && tid != DRD_INVALID_THREADID);
257 return (DRD_(g_threadinfo)[tid].vg_thread_exists
258 ? DRD_(g_threadinfo)[tid].vg_threadid
259 : VG_INVALID_THREADID);
262 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
264 * Sanity check of the doubly linked list of segments referenced by a
265 * ThreadInfo struct.
266 * @return True if sane, False if not.
268 static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti)
270 Segment* p;
272 for (p = ti->sg_first; p; p = p->thr_next) {
273 if (p->thr_next && p->thr_next->thr_prev != p)
274 return False;
275 if (p->thr_next == 0 && p != ti->sg_last)
276 return False;
278 for (p = ti->sg_last; p; p = p->thr_prev) {
279 if (p->thr_prev && p->thr_prev->thr_next != p)
280 return False;
281 if (p->thr_prev == 0 && p != ti->sg_first)
282 return False;
284 return True;
286 #endif
289 * Create the first segment for a newly started thread.
291 * This function is called from the handler installed via
292 * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler
293 * from the context of the creator thread, before the new thread has been
294 * created.
296 * @param[in] creator DRD thread ID of the creator thread.
297 * @param[in] vg_created Valgrind thread ID of the created thread.
299 * @return DRD thread ID of the created thread.
301 DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
302 const ThreadId vg_created)
304 DrdThreadId created;
306 tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID);
307 created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created);
308 tl_assert(0 <= (int)created && created < DRD_N_THREADS
309 && created != DRD_INVALID_THREADID);
311 tl_assert(DRD_(g_threadinfo)[created].sg_first == NULL);
312 tl_assert(DRD_(g_threadinfo)[created].sg_last == NULL);
314 if (creator != DRD_INVALID_THREADID) {
315 if (DRD_(ignore_thread_creation)) {
316 tl_assert(DRD_(thread_get_synchr_nesting_count)(created) == 0);
317 DRD_(thread_enter_synchr)(created);
318 /* Counterpart in DRD_(thread_set_pthreadid)(). */
321 DRD_(g_threadinfo)[created].creator_thread = creator;
323 /* Create an initial segment for the newly created thread. */
324 thread_append_segment(created, DRD_(sg_new)(creator, created));
326 return created;
330 * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called
331 * after the thread has been created and before any client instructions are run
332 * on the newly created thread, e.g. from the handler installed via
333 * VG_(track_pre_thread_first_insn)().
335 * @param[in] vg_created Valgrind thread ID of the newly created thread.
337 * @return DRD thread ID for the new thread.
339 DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created)
341 const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created);
343 tl_assert(0 <= (int)created && created < DRD_N_THREADS
344 && created != DRD_INVALID_THREADID);
346 DRD_(g_threadinfo)[created].stack_max
347 = VG_(thread_get_stack_max)(vg_created);
348 DRD_(g_threadinfo)[created].stack_startup
349 = DRD_(g_threadinfo)[created].stack_max;
350 DRD_(g_threadinfo)[created].stack_min
351 = DRD_(g_threadinfo)[created].stack_max;
352 DRD_(g_threadinfo)[created].stack_min_min
353 = DRD_(g_threadinfo)[created].stack_max;
354 DRD_(g_threadinfo)[created].stack_size
355 = VG_(thread_get_stack_size)(vg_created);
356 tl_assert(DRD_(g_threadinfo)[created].stack_max != 0);
358 return created;
361 static void DRD_(thread_delayed_delete)(const DrdThreadId tid)
363 UInt j;
365 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
366 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
367 DRD_(g_threadinfo)[tid].deletion_seq = s_deletion_head++;
368 #if 0
369 VG_(message)(Vg_DebugMsg, "Adding thread %d to the deletion list\n", tid);
370 #endif
371 if (s_deletion_head - s_deletion_tail >= s_join_list_vol) {
372 for (j = 0; j < DRD_N_THREADS; ++j) {
373 if (DRD_(IsValidDrdThreadId)(j)
374 && DRD_(g_threadinfo)[j].deletion_seq == s_deletion_tail)
376 s_deletion_tail++;
377 #if 0
378 VG_(message)(Vg_DebugMsg, "Delayed delete of thread %d\n", j);
379 #endif
380 DRD_(thread_delete)(j, False);
381 break;
388 * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just
389 * after thread drd_joiner joined thread drd_joinee.
391 void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
393 tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner));
394 tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee));
396 DRD_(thread_new_segment)(drd_joiner);
397 DRD_(thread_combine_vc_join)(drd_joiner, drd_joinee);
398 DRD_(thread_new_segment)(drd_joinee);
400 if (s_trace_fork_join)
402 const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner);
403 const unsigned msg_size = 256;
404 HChar* msg;
406 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
408 VG_(snprintf)(msg, msg_size,
409 "drd_post_thread_join joiner = %u, joinee = %u",
410 drd_joiner, drd_joinee);
411 if (joiner)
413 HChar* vc;
415 vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(drd_joiner));
416 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
417 ", new vc: %s", vc);
418 VG_(free)(vc);
420 DRD_(trace_msg)("%pS", msg);
421 VG_(free)(msg);
424 if (! DRD_(get_check_stack_accesses)())
426 DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee)
427 - DRD_(thread_get_stack_size)(drd_joinee),
428 DRD_(thread_get_stack_max)(drd_joinee));
430 DRD_(clientobj_delete_thread)(drd_joinee);
431 DRD_(thread_delayed_delete)(drd_joinee);
434 void DRD_(thread_register_stack)(DrdThreadId tid, Addr addr1, Addr addr2)
436 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
437 && tid != DRD_INVALID_THREADID);
438 tl_assert(addr1 < addr2);
439 DRD_(g_threadinfo)[tid].stack_min = addr2;
440 DRD_(g_threadinfo)[tid].stack_min_min = addr2;
441 DRD_(g_threadinfo)[tid].stack_startup = addr2;
442 DRD_(g_threadinfo)[tid].stack_max = addr2;
446 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
447 * and accesses this data structure from multiple threads without locking.
448 * Any conflicting accesses in the range stack_startup..stack_max will be
449 * ignored.
451 void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
452 const Addr stack_startup)
454 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
455 && tid != DRD_INVALID_THREADID);
456 tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup);
457 tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max);
458 DRD_(g_threadinfo)[tid].stack_startup = stack_startup;
461 /** Return the stack pointer for the specified thread. */
462 Addr DRD_(thread_get_stack_min)(const DrdThreadId tid)
464 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
465 && tid != DRD_INVALID_THREADID);
466 return DRD_(g_threadinfo)[tid].stack_min;
470 * Return the lowest value that was ever assigned to the stack pointer
471 * for the specified thread.
473 Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid)
475 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
476 && tid != DRD_INVALID_THREADID);
477 return DRD_(g_threadinfo)[tid].stack_min_min;
480 /** Return the top address for the stack of the specified thread. */
481 Addr DRD_(thread_get_stack_max)(const DrdThreadId tid)
483 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
484 && tid != DRD_INVALID_THREADID);
485 return DRD_(g_threadinfo)[tid].stack_max;
488 /** Return the maximum stack size for the specified thread. */
489 SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid)
491 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
492 && tid != DRD_INVALID_THREADID);
493 return DRD_(g_threadinfo)[tid].stack_size;
496 Bool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid)
498 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
499 && tid != DRD_INVALID_THREADID);
500 return DRD_(g_threadinfo)[tid].on_alt_stack;
503 void DRD_(thread_set_on_alt_stack)(const DrdThreadId tid,
504 const Bool on_alt_stack)
506 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
507 && tid != DRD_INVALID_THREADID);
508 tl_assert(on_alt_stack == !!on_alt_stack);
509 DRD_(g_threadinfo)[tid].on_alt_stack = on_alt_stack;
512 Int DRD_(thread_get_threads_on_alt_stack)(void)
514 int n = 0;
516 for (UInt i = 1; i < DRD_N_THREADS; i++)
517 n += DRD_(g_threadinfo)[i].on_alt_stack;
518 return n;
522 * Clean up thread-specific data structures.
524 void DRD_(thread_delete)(const DrdThreadId tid, const Bool detached)
526 Segment* sg;
527 Segment* sg_prev;
529 tl_assert(DRD_(IsValidDrdThreadId)(tid));
531 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0);
532 for (sg = DRD_(g_threadinfo)[tid].sg_last; sg; sg = sg_prev) {
533 sg_prev = sg->thr_prev;
534 sg->thr_next = NULL;
535 sg->thr_prev = NULL;
536 DRD_(sg_put)(sg);
538 DRD_(g_threadinfo)[tid].valid = False;
539 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
540 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
541 if (detached)
542 DRD_(g_threadinfo)[tid].detached_posix_thread = False;
543 else
544 tl_assert(!DRD_(g_threadinfo)[tid].detached_posix_thread);
545 DRD_(g_threadinfo)[tid].sg_first = NULL;
546 DRD_(g_threadinfo)[tid].sg_last = NULL;
548 tl_assert(!DRD_(IsValidDrdThreadId)(tid));
552 * Called after a thread performed its last memory access and before
553 * thread_delete() is called. Note: thread_delete() is only called for
554 * joinable threads, not for detached threads.
556 void DRD_(thread_finished)(const DrdThreadId tid)
558 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
559 && tid != DRD_INVALID_THREADID);
561 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
563 if (DRD_(g_threadinfo)[tid].detached_posix_thread)
566 * Once a detached thread has finished, its stack is deallocated and
567 * should no longer be taken into account when computing the conflict set.
569 DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max;
572 * For a detached thread, calling pthread_exit() invalidates the
573 * POSIX thread ID associated with the detached thread. For joinable
574 * POSIX threads however, the POSIX thread ID remains live after the
575 * pthread_exit() call until pthread_join() is called.
577 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
581 /** Called just after fork() in the child process. */
582 void DRD_(drd_thread_atfork_child)(const ThreadId tid)
584 unsigned i;
586 for (i = 1; i < DRD_N_THREADS; i++)
588 if (DRD_(g_threadinfo)[i].vg_threadid == tid)
589 continue;
590 if (DRD_(IsValidDrdThreadId(i)))
591 DRD_(thread_delete)(i, True);
592 tl_assert(!DRD_(IsValidDrdThreadId(i)));
595 DRD_(bm_cleanup)(DRD_(g_conflict_set));
596 DRD_(bm_init)(DRD_(g_conflict_set));
599 /** Called just before pthread_cancel(). */
600 void DRD_(thread_pre_cancel)(const DrdThreadId tid)
602 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
603 && tid != DRD_INVALID_THREADID);
604 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
606 if (DRD_(thread_get_trace_fork_join)())
607 DRD_(trace_msg)("[%u] drd_thread_pre_cancel %u",
608 DRD_(g_drd_running_tid), tid);
612 * Store the POSIX thread ID for the specified thread.
614 * @note This function can be called multiple times for the same thread -- see
615 * also the comment block preceding the pthread_create() wrapper in
616 * drd_pthread_intercepts.c.
618 void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
620 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
621 && tid != DRD_INVALID_THREADID);
622 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID
623 || DRD_(g_threadinfo)[tid].pt_threadid == ptid);
624 tl_assert(ptid != INVALID_POSIX_THREADID);
625 if (DRD_(g_threadinfo)[tid].posix_thread_exists) {
626 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == ptid);
627 return;
629 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
630 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
632 if (DRD_(g_threadinfo)[tid].creator_thread != DRD_INVALID_THREADID) {
633 if (DRD_(ignore_thread_creation)) {
634 DRD_(thread_leave_synchr)(tid);
635 tl_assert(DRD_(thread_get_synchr_nesting_count)(tid) == 0);
640 /** Returns true for joinable threads and false for detached threads. */
641 Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
643 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
644 && tid != DRD_INVALID_THREADID);
645 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
648 /** Store the thread mode: joinable or detached. */
649 #if defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
650 /* There is a cse related issue in gcc for MIPS. Optimization level
651 has to be lowered, so cse related optimizations are not
652 included.*/
653 __attribute__((optimize("O1")))
654 #endif
655 void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
657 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
658 && tid != DRD_INVALID_THREADID);
659 tl_assert((!! joinable) == joinable);
660 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
662 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
665 /** Tells DRD that the calling thread is about to enter pthread_create(). */
666 void DRD_(thread_entering_pthread_create)(const DrdThreadId tid)
668 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
669 && tid != DRD_INVALID_THREADID);
670 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
671 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level >= 0);
673 DRD_(g_threadinfo)[tid].pthread_create_nesting_level++;
675 if (DRD_(ignore_thread_creation)) {
676 tl_assert(DRD_(thread_get_synchr_nesting_count)(tid) == 0);
677 DRD_(thread_enter_synchr)(tid);
681 /** Tells DRD that the calling thread has left pthread_create(). */
682 void DRD_(thread_left_pthread_create)(const DrdThreadId tid)
684 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
685 && tid != DRD_INVALID_THREADID);
686 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
687 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level > 0);
689 DRD_(g_threadinfo)[tid].pthread_create_nesting_level--;
691 if (DRD_(ignore_thread_creation)) {
692 DRD_(thread_leave_synchr)(tid);
693 tl_assert(DRD_(thread_get_synchr_nesting_count)(tid) == 0);
697 #if defined(VGO_solaris)
698 /** Handles the bind_guard() intercept. */
699 void DRD_(thread_entering_rtld_bind_guard)(const DrdThreadId tid, int flags)
701 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
702 && tid != DRD_INVALID_THREADID);
704 Int bindflag = (flags & VKI_THR_FLG_RTLD);
705 if ((bindflag & DRD_(g_threadinfo)[tid].bind_guard_flag) == 0) {
706 DRD_(g_threadinfo)[tid].bind_guard_flag |= bindflag;
707 DRD_(thread_enter_synchr)(tid);
712 * Handles the bind_clear() intercept.
713 * Call to bind_clear(0) is typically used to determine value of bind_flags.
715 void DRD_(thread_leaving_rtld_bind_clear)(const DrdThreadId tid, int flags)
717 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
718 && tid != DRD_INVALID_THREADID);
720 Int bindflag = (flags & VKI_THR_FLG_RTLD);
721 if ((DRD_(g_threadinfo)[tid].bind_guard_flag & bindflag) != 0) {
722 DRD_(g_threadinfo)[tid].bind_guard_flag &= ~bindflag;
723 DRD_(thread_leave_synchr)(tid);
726 #endif /* VGO_solaris */
728 /** Obtain the thread number and the user-assigned thread name. */
729 const HChar* DRD_(thread_get_name)(const DrdThreadId tid)
731 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
732 && tid != DRD_INVALID_THREADID);
734 return DRD_(g_threadinfo)[tid].name;
737 /** Set the name of the specified thread. */
738 void DRD_(thread_set_name)(const DrdThreadId tid, const HChar* const name)
740 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
741 && tid != DRD_INVALID_THREADID);
743 if (name == NULL || name[0] == 0)
744 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
745 sizeof(DRD_(g_threadinfo)[tid].name),
746 "Thread %u",
747 tid);
748 else
749 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
750 sizeof(DRD_(g_threadinfo)[tid].name),
751 "Thread %u (%s)",
752 tid, name);
753 DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0;
757 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
758 * conflict set.
760 void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
762 tl_assert(vg_tid != VG_INVALID_THREADID);
764 if (vg_tid != s_vg_running_tid)
766 DRD_(thread_set_running_tid)(vg_tid,
767 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
770 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
771 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
775 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
776 * conflict set.
778 void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
779 const DrdThreadId drd_tid)
781 tl_assert(vg_tid != VG_INVALID_THREADID);
782 tl_assert(drd_tid != DRD_INVALID_THREADID);
784 if (vg_tid != s_vg_running_tid)
786 if (s_trace_context_switches
787 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
789 VG_(message)(Vg_DebugMsg,
790 "Context switch from thread %u to thread %u;"
791 " segments: %llu\n",
792 DRD_(g_drd_running_tid), drd_tid,
793 DRD_(sg_get_segments_alive_count)());
795 s_vg_running_tid = vg_tid;
796 DRD_(g_drd_running_tid) = drd_tid;
797 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
798 s_context_switch_count++;
801 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
802 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
806 * Increase the synchronization nesting counter. Must be called before the
807 * client calls a synchronization function.
809 int DRD_(thread_enter_synchr)(const DrdThreadId tid)
811 tl_assert(DRD_(IsValidDrdThreadId)(tid));
812 return DRD_(g_threadinfo)[tid].synchr_nesting++;
816 * Decrease the synchronization nesting counter. Must be called after the
817 * client left a synchronization function.
819 int DRD_(thread_leave_synchr)(const DrdThreadId tid)
821 tl_assert(DRD_(IsValidDrdThreadId)(tid));
822 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
823 return --DRD_(g_threadinfo)[tid].synchr_nesting;
826 /** Returns the synchronization nesting counter. */
827 int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
829 tl_assert(DRD_(IsValidDrdThreadId)(tid));
830 return DRD_(g_threadinfo)[tid].synchr_nesting;
833 /** Append a new segment at the end of the segment list. */
834 static
835 void thread_append_segment(const DrdThreadId tid, Segment* const sg)
837 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
838 && tid != DRD_INVALID_THREADID);
840 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
841 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
842 #endif
844 // add at tail
845 sg->thr_prev = DRD_(g_threadinfo)[tid].sg_last;
846 sg->thr_next = NULL;
847 if (DRD_(g_threadinfo)[tid].sg_last)
848 DRD_(g_threadinfo)[tid].sg_last->thr_next = sg;
849 DRD_(g_threadinfo)[tid].sg_last = sg;
850 if (DRD_(g_threadinfo)[tid].sg_first == NULL)
851 DRD_(g_threadinfo)[tid].sg_first = sg;
853 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
854 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
855 #endif
859 * Remove a segment from the segment list of thread threadid, and free the
860 * associated memory.
862 static
863 void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
865 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
866 && tid != DRD_INVALID_THREADID);
868 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
869 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
870 #endif
872 if (sg->thr_prev)
873 sg->thr_prev->thr_next = sg->thr_next;
874 if (sg->thr_next)
875 sg->thr_next->thr_prev = sg->thr_prev;
876 if (sg == DRD_(g_threadinfo)[tid].sg_first)
877 DRD_(g_threadinfo)[tid].sg_first = sg->thr_next;
878 if (sg == DRD_(g_threadinfo)[tid].sg_last)
879 DRD_(g_threadinfo)[tid].sg_last = sg->thr_prev;
880 DRD_(sg_put)(sg);
882 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
883 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
884 #endif
888 * Returns a pointer to the vector clock of the most recent segment associated
889 * with thread 'tid'.
891 VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
893 Segment* latest_sg;
895 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
896 && tid != DRD_INVALID_THREADID);
897 latest_sg = DRD_(g_threadinfo)[tid].sg_last;
898 tl_assert(latest_sg);
899 return &latest_sg->vc;
903 * Return the latest segment of thread 'tid' and increment its reference count.
905 void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
907 Segment* latest_sg;
909 tl_assert(sg);
910 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
911 && tid != DRD_INVALID_THREADID);
912 latest_sg = DRD_(g_threadinfo)[tid].sg_last;
913 tl_assert(latest_sg);
915 DRD_(sg_put)(*sg);
916 *sg = DRD_(sg_get)(latest_sg);
920 * Compute the minimum of all latest vector clocks of all threads
921 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
923 * @param vc pointer to a vectorclock, holds result upon return.
925 static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
927 unsigned i;
928 Bool first;
929 Segment* latest_sg;
931 first = True;
932 for (i = 0; i < DRD_N_THREADS; i++)
934 latest_sg = DRD_(g_threadinfo)[i].sg_last;
935 if (latest_sg) {
936 if (first)
937 DRD_(vc_assign)(vc, &latest_sg->vc);
938 else
939 DRD_(vc_min)(vc, &latest_sg->vc);
940 first = False;
946 * Compute the maximum of all latest vector clocks of all threads.
948 * @param vc pointer to a vectorclock, holds result upon return.
950 static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
952 unsigned i;
953 Bool first;
954 Segment* latest_sg;
956 first = True;
957 for (i = 0; i < DRD_N_THREADS; i++)
959 latest_sg = DRD_(g_threadinfo)[i].sg_last;
960 if (latest_sg) {
961 if (first)
962 DRD_(vc_assign)(vc, &latest_sg->vc);
963 else
964 DRD_(vc_combine)(vc, &latest_sg->vc);
965 first = False;
971 * Discard all segments that have a defined order against the latest vector
972 * clock of all threads -- these segments can no longer be involved in a
973 * data race.
975 static void thread_discard_ordered_segments(void)
977 unsigned i;
978 VectorClock thread_vc_min;
980 s_discard_ordered_segments_count++;
982 DRD_(vc_init)(&thread_vc_min, 0, 0);
983 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
984 if (DRD_(sg_get_trace)())
986 HChar *vc_min, *vc_max;
987 VectorClock thread_vc_max;
989 DRD_(vc_init)(&thread_vc_max, 0, 0);
990 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
991 vc_min = DRD_(vc_aprint)(&thread_vc_min);
992 vc_max = DRD_(vc_aprint)(&thread_vc_max);
993 VG_(message)(Vg_DebugMsg,
994 "Discarding ordered segments -- min vc is %s, max vc is %s\n",
995 vc_min, vc_max);
996 VG_(free)(vc_min);
997 VG_(free)(vc_max);
998 DRD_(vc_cleanup)(&thread_vc_max);
1001 for (i = 0; i < DRD_N_THREADS; i++) {
1002 Segment* sg;
1003 Segment* sg_next;
1005 for (sg = DRD_(g_threadinfo)[i].sg_first;
1006 sg && (sg_next = sg->thr_next)
1007 && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
1008 sg = sg_next)
1010 thread_discard_segment(i, sg);
1013 DRD_(vc_cleanup)(&thread_vc_min);
1017 * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper
1018 * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if
1019 * all segments in the set CS are ordered consistently against both sg1 and
1020 * sg2. The set CS is defined as the set of segments that can immediately
1021 * precede future segments via inter-thread synchronization operations. In
1022 * DRD the set CS consists of the latest segment of each thread combined with
1023 * all segments for which the reference count is strictly greater than one.
1024 * The code below is an optimized version of the following:
1026 * for (i = 0; i < DRD_N_THREADS; i++)
1028 * Segment* sg;
1030 * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
1032 * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1)
1034 * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc)
1035 * != DRD_(vc_lte)(&sg2->vc, &sg->vc)
1036 * || DRD_(vc_lte)(&sg->vc, &sg1->vc)
1037 * != DRD_(vc_lte)(&sg->vc, &sg2->vc))
1039 * return False;
1045 static Bool thread_consistent_segment_ordering(const DrdThreadId tid,
1046 Segment* const sg1,
1047 Segment* const sg2)
1049 unsigned i;
1051 tl_assert(sg1->thr_next);
1052 tl_assert(sg2->thr_next);
1053 tl_assert(sg1->thr_next == sg2);
1054 tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc));
1056 for (i = 0; i < DRD_N_THREADS; i++)
1058 Segment* sg;
1060 for (sg = DRD_(g_threadinfo)[i].sg_first; sg; sg = sg->thr_next) {
1061 if (!sg->thr_next || DRD_(sg_get_refcnt)(sg) > 1) {
1062 if (DRD_(vc_lte)(&sg2->vc, &sg->vc))
1063 break;
1064 if (DRD_(vc_lte)(&sg1->vc, &sg->vc))
1065 return False;
1068 for (sg = DRD_(g_threadinfo)[i].sg_last; sg; sg = sg->thr_prev) {
1069 if (!sg->thr_next || DRD_(sg_get_refcnt)(sg) > 1) {
1070 if (DRD_(vc_lte)(&sg->vc, &sg1->vc))
1071 break;
1072 if (DRD_(vc_lte)(&sg->vc, &sg2->vc))
1073 return False;
1077 return True;
1081 * Merge all segments that may be merged without triggering false positives
1082 * or discarding real data races. For the theoretical background of segment
1083 * merging, see also the following paper: Mark Christiaens, Michiel Ronsse
1084 * and Koen De Bosschere. Bounding the number of segment histories during
1085 * data race detection. Parallel Computing archive, Volume 28, Issue 9,
1086 * pp 1221-1238, September 2002. This paper contains a proof that merging
1087 * consecutive segments for which the property equiv(s1,s2) holds can be
1088 * merged without reducing the accuracy of datarace detection. Furthermore
1089 * it is also proven that the total number of all segments will never grow
1090 * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged
1091 * every time a new segment is created. The property equiv(s1, s2) is defined
1092 * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector
1093 * clocks of segments s and s1 are ordered in the same way as those of segments
1094 * s and s2. The set CS is defined as the set of existing segments s that have
1095 * the potential to conflict with not yet created segments, either because the
1096 * segment s is the latest segment of a thread or because it can become the
1097 * immediate predecessor of a new segment due to a synchronization operation.
1099 static void thread_merge_segments(void)
1101 unsigned i;
1103 s_new_segments_since_last_merge = 0;
1105 for (i = 0; i < DRD_N_THREADS; i++)
1107 Segment* sg;
1109 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1110 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1111 #endif
1113 for (sg = DRD_(g_threadinfo)[i].sg_first; sg; sg = sg->thr_next) {
1114 if (DRD_(sg_get_refcnt)(sg) == 1 && sg->thr_next) {
1115 Segment* const sg_next = sg->thr_next;
1116 if (DRD_(sg_get_refcnt)(sg_next) == 1
1117 && sg_next->thr_next
1118 && thread_consistent_segment_ordering(i, sg, sg_next))
1120 /* Merge sg and sg_next into sg. */
1121 DRD_(sg_merge)(sg, sg_next);
1122 thread_discard_segment(i, sg_next);
1127 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1128 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1129 #endif
1134 * Create a new segment for the specified thread, and discard any segments
1135 * that cannot cause races anymore.
1137 void DRD_(thread_new_segment)(const DrdThreadId tid)
1139 Segment* last_sg;
1140 Segment* new_sg;
1142 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1143 && tid != DRD_INVALID_THREADID);
1144 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1146 last_sg = DRD_(g_threadinfo)[tid].sg_last;
1147 new_sg = DRD_(sg_new)(tid, tid);
1148 thread_append_segment(tid, new_sg);
1149 if (tid == DRD_(g_drd_running_tid) && last_sg)
1151 DRD_(thread_update_conflict_set)(tid, &last_sg->vc);
1152 s_update_conflict_set_new_sg_count++;
1155 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1157 if (s_segment_merging
1158 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
1160 thread_discard_ordered_segments();
1161 thread_merge_segments();
1165 /** Call this function after thread 'joiner' joined thread 'joinee'. */
1166 void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee)
1168 tl_assert(joiner != joinee);
1169 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
1170 && joiner != DRD_INVALID_THREADID);
1171 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
1172 && joinee != DRD_INVALID_THREADID);
1173 tl_assert(DRD_(g_threadinfo)[joiner].sg_first);
1174 tl_assert(DRD_(g_threadinfo)[joiner].sg_last);
1175 tl_assert(DRD_(g_threadinfo)[joinee].sg_first);
1176 tl_assert(DRD_(g_threadinfo)[joinee].sg_last);
1178 if (DRD_(sg_get_trace)())
1180 HChar *str1, *str2;
1181 str1 = DRD_(vc_aprint)(DRD_(thread_get_vc)(joiner));
1182 str2 = DRD_(vc_aprint)(DRD_(thread_get_vc)(joinee));
1183 VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n",
1184 str1, str2);
1185 VG_(free)(str1);
1186 VG_(free)(str2);
1188 if (joiner == DRD_(g_drd_running_tid)) {
1189 VectorClock old_vc;
1191 DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(joiner));
1192 DRD_(vc_combine)(DRD_(thread_get_vc)(joiner),
1193 DRD_(thread_get_vc)(joinee));
1194 DRD_(thread_update_conflict_set)(joiner, &old_vc);
1195 s_update_conflict_set_join_count++;
1196 DRD_(vc_cleanup)(&old_vc);
1197 } else {
1198 DRD_(vc_combine)(DRD_(thread_get_vc)(joiner),
1199 DRD_(thread_get_vc)(joinee));
1202 thread_discard_ordered_segments();
1204 if (DRD_(sg_get_trace)()) {
1205 HChar* str;
1207 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(joiner));
1208 VG_(message)(Vg_DebugMsg, "After join: %s\n", str);
1209 VG_(free)(str);
1214 * Update the vector clock of the last segment of thread tid with the
1215 * the vector clock of segment sg.
1217 static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg)
1219 const VectorClock* const vc = &sg->vc;
1221 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1222 && tid != DRD_INVALID_THREADID);
1223 tl_assert(DRD_(g_threadinfo)[tid].sg_first);
1224 tl_assert(DRD_(g_threadinfo)[tid].sg_last);
1225 tl_assert(sg);
1226 tl_assert(vc);
1228 if (tid != sg->tid) {
1229 VectorClock old_vc;
1231 DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(tid));
1232 DRD_(vc_combine)(DRD_(thread_get_vc)(tid), vc);
1233 if (DRD_(sg_get_trace)()) {
1234 HChar *str1, *str2;
1235 str1 = DRD_(vc_aprint)(&old_vc);
1236 str2 = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
1237 VG_(message)(Vg_DebugMsg, "thread %u: vc %s -> %s\n", tid, str1, str2);
1238 VG_(free)(str1);
1239 VG_(free)(str2);
1242 thread_discard_ordered_segments();
1244 DRD_(thread_update_conflict_set)(tid, &old_vc);
1245 s_update_conflict_set_sync_count++;
1247 DRD_(vc_cleanup)(&old_vc);
1248 } else {
1249 tl_assert(DRD_(vc_lte)(vc, DRD_(thread_get_vc)(tid)));
1254 * Create a new segment for thread tid and update the vector clock of the last
1255 * segment of this thread with the vector clock of segment sg. Call this
1256 * function after thread tid had to wait because of thread synchronization
1257 * until the memory accesses in the segment sg finished.
1259 void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, const Segment* sg)
1261 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1262 && tid != DRD_INVALID_THREADID);
1263 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1264 tl_assert(sg);
1266 thread_append_segment(tid, DRD_(sg_new)(tid, tid));
1268 thread_combine_vc_sync(tid, sg);
1270 if (s_segment_merging
1271 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
1273 thread_discard_ordered_segments();
1274 thread_merge_segments();
1279 * Call this function whenever a thread is no longer using the memory
1280 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
1281 * increase.
1283 void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2)
1285 Segment* p;
1287 for (p = DRD_(g_sg_list); p; p = p->g_next)
1288 DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2);
1290 DRD_(bm_clear)(DRD_(g_conflict_set), a1, a2);
1293 /** Specify whether memory loads should be recorded. */
1294 void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled)
1296 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1297 && tid != DRD_INVALID_THREADID);
1298 tl_assert(enabled == !! enabled);
1300 DRD_(g_threadinfo)[tid].is_recording_loads = enabled;
1303 /** Specify whether memory stores should be recorded. */
1304 void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled)
1306 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1307 && tid != DRD_INVALID_THREADID);
1308 tl_assert(enabled == !! enabled);
1310 DRD_(g_threadinfo)[tid].is_recording_stores = enabled;
1314 * Print the segment information for all threads.
1316 * This function is only used for debugging purposes.
1318 void DRD_(thread_print_all)(void)
1320 UInt i;
1321 Segment* p;
1323 for (i = 0; i < DRD_N_THREADS; i++)
1325 p = DRD_(g_threadinfo)[i].sg_first;
1326 if (p) {
1327 VG_(printf)("**************\n"
1328 "* thread %3u (%d/%u/%u/%u/0x%lx/%d) *\n"
1329 "**************\n",
1331 DRD_(g_threadinfo)[i].valid,
1332 DRD_(g_threadinfo)[i].vg_thread_exists,
1333 DRD_(g_threadinfo)[i].vg_threadid,
1334 DRD_(g_threadinfo)[i].posix_thread_exists,
1335 DRD_(g_threadinfo)[i].pt_threadid,
1336 DRD_(g_threadinfo)[i].detached_posix_thread);
1337 for ( ; p; p = p->thr_next)
1338 DRD_(sg_print)(p);
1343 /** Show a call stack involved in a data race. */
1344 static void show_call_stack(const DrdThreadId tid, ExeContext* const callstack)
1346 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
1348 if (vg_tid != VG_INVALID_THREADID) {
1349 if (callstack)
1350 VG_(pp_ExeContext)(callstack);
1351 else
1352 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
1353 } else {
1354 if (!VG_(clo_xml))
1355 VG_(message)(Vg_UserMsg,
1356 " (thread finished, call stack no longer available)\n");
1360 /** Print information about the segments involved in a data race. */
1361 static void
1362 thread_report_conflicting_segments_segment(const DrdThreadId tid,
1363 const Addr addr,
1364 const SizeT size,
1365 const BmAccessTypeT access_type,
1366 const Segment* const p)
1368 unsigned i;
1370 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1371 && tid != DRD_INVALID_THREADID);
1372 tl_assert(p);
1374 for (i = 0; i < DRD_N_THREADS; i++) {
1375 if (i != tid) {
1376 Segment* q;
1378 for (q = DRD_(g_threadinfo)[i].sg_last; q; q = q->thr_prev) {
1380 * Since q iterates over the segments of thread i in order of
1381 * decreasing vector clocks, if q->vc <= p->vc, then
1382 * q->next->vc <= p->vc will also hold. Hence, break out of the
1383 * loop once this condition is met.
1385 if (DRD_(vc_lte)(&q->vc, &p->vc))
1386 break;
1387 if (!DRD_(vc_lte)(&p->vc, &q->vc)) {
1388 if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size,
1389 access_type)) {
1390 Segment* q_next;
1392 tl_assert(q->stacktrace);
1393 if (VG_(clo_xml))
1394 VG_(printf_xml)(" <other_segment_start>\n");
1395 else
1396 VG_(message)(Vg_UserMsg,
1397 "Other segment start (thread %u)\n", i);
1398 show_call_stack(i, q->stacktrace);
1399 if (VG_(clo_xml))
1400 VG_(printf_xml)(" </other_segment_start>\n"
1401 " <other_segment_end>\n");
1402 else
1403 VG_(message)(Vg_UserMsg,
1404 "Other segment end (thread %u)\n", i);
1405 q_next = q->thr_next;
1406 show_call_stack(i, q_next ? q_next->stacktrace : 0);
1407 if (VG_(clo_xml))
1408 VG_(printf_xml)(" </other_segment_end>\n");
1416 /** Print information about all segments involved in a data race. */
1417 void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1418 const Addr addr,
1419 const SizeT size,
1420 const BmAccessTypeT access_type)
1422 Segment* p;
1424 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1425 && tid != DRD_INVALID_THREADID);
1427 for (p = DRD_(g_threadinfo)[tid].sg_first; p; p = p->thr_next) {
1428 if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type))
1429 thread_report_conflicting_segments_segment(tid, addr, size,
1430 access_type, p);
1435 * Verify whether the conflict set for thread tid is up to date. Only perform
1436 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
1438 static Bool thread_conflict_set_up_to_date(const DrdThreadId tid)
1440 Bool result;
1441 struct bitmap* computed_conflict_set = 0;
1443 if (!DRD_(verify_conflict_set))
1444 return True;
1446 thread_compute_conflict_set(&computed_conflict_set, tid);
1447 result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set);
1448 if (! result)
1450 VG_(printf)("actual conflict set:\n");
1451 DRD_(bm_print)(DRD_(g_conflict_set));
1452 VG_(printf)("\n");
1453 VG_(printf)("computed conflict set:\n");
1454 DRD_(bm_print)(computed_conflict_set);
1455 VG_(printf)("\n");
1457 DRD_(bm_delete)(computed_conflict_set);
1458 return result;
1462 * Compute the conflict set: a bitmap that represents the union of all memory
1463 * accesses of all segments that are unordered to the current segment of the
1464 * thread tid.
1466 static void thread_compute_conflict_set(struct bitmap** conflict_set,
1467 const DrdThreadId tid)
1469 Segment* p;
1471 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1472 && tid != DRD_INVALID_THREADID);
1473 tl_assert(tid == DRD_(g_drd_running_tid));
1475 s_compute_conflict_set_count++;
1476 s_conflict_set_bitmap_creation_count
1477 -= DRD_(bm_get_bitmap_creation_count)();
1478 s_conflict_set_bitmap2_creation_count
1479 -= DRD_(bm_get_bitmap2_creation_count)();
1481 if (*conflict_set) {
1482 DRD_(bm_cleanup)(*conflict_set);
1483 DRD_(bm_init)(*conflict_set);
1484 } else {
1485 *conflict_set = DRD_(bm_new)();
1488 if (s_trace_conflict_set) {
1489 HChar* str;
1491 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
1492 VG_(message)(Vg_DebugMsg,
1493 "computing conflict set for thread %u with vc %s\n",
1494 tid, str);
1495 VG_(free)(str);
1498 p = DRD_(g_threadinfo)[tid].sg_last;
1500 unsigned j;
1502 if (s_trace_conflict_set) {
1503 HChar* vc;
1505 vc = DRD_(vc_aprint)(&p->vc);
1506 VG_(message)(Vg_DebugMsg, "conflict set: thread [%u] at vc %s\n",
1507 tid, vc);
1508 VG_(free)(vc);
1511 for (j = 0; j < DRD_N_THREADS; j++) {
1512 if (j != tid && DRD_(IsValidDrdThreadId)(j)) {
1513 Segment* q;
1515 for (q = DRD_(g_threadinfo)[j].sg_last; q; q = q->thr_prev) {
1516 if (!DRD_(vc_lte)(&q->vc, &p->vc)
1517 && !DRD_(vc_lte)(&p->vc, &q->vc)) {
1518 if (s_trace_conflict_set) {
1519 HChar* str;
1521 str = DRD_(vc_aprint)(&q->vc);
1522 VG_(message)(Vg_DebugMsg,
1523 "conflict set: [%u] merging segment %s\n",
1524 j, str);
1525 VG_(free)(str);
1527 DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q));
1528 } else {
1529 if (s_trace_conflict_set) {
1530 HChar* str;
1532 str = DRD_(vc_aprint)(&q->vc);
1533 VG_(message)(Vg_DebugMsg,
1534 "conflict set: [%u] ignoring segment %s\n",
1535 j, str);
1536 VG_(free)(str);
1544 s_conflict_set_bitmap_creation_count
1545 += DRD_(bm_get_bitmap_creation_count)();
1546 s_conflict_set_bitmap2_creation_count
1547 += DRD_(bm_get_bitmap2_creation_count)();
1549 if (s_trace_conflict_set_bm) {
1550 VG_(message)(Vg_DebugMsg, "[%u] new conflict set:\n", tid);
1551 DRD_(bm_print)(*conflict_set);
1552 VG_(message)(Vg_DebugMsg, "[%u] end of new conflict set.\n", tid);
1557 * Update the conflict set after the vector clock of thread tid has been
1558 * updated from old_vc to its current value, either because a new segment has
1559 * been created or because of a synchronization operation.
1561 void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
1562 const VectorClock* const old_vc)
1564 const VectorClock* new_vc;
1565 Segment* p;
1566 unsigned j;
1568 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1569 && tid != DRD_INVALID_THREADID);
1570 tl_assert(old_vc);
1571 tl_assert(tid == DRD_(g_drd_running_tid));
1572 tl_assert(DRD_(g_conflict_set));
1574 if (s_trace_conflict_set) {
1575 HChar* str;
1577 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
1578 VG_(message)(Vg_DebugMsg,
1579 "updating conflict set for thread %u with vc %s\n",
1580 tid, str);
1581 VG_(free)(str);
1584 new_vc = DRD_(thread_get_vc)(tid);
1585 tl_assert(DRD_(vc_lte)(old_vc, new_vc));
1587 DRD_(bm_unmark)(DRD_(g_conflict_set));
1589 for (j = 0; j < DRD_N_THREADS; j++)
1591 Segment* q;
1593 if (j == tid || ! DRD_(IsValidDrdThreadId)(j))
1594 continue;
1596 for (q = DRD_(g_threadinfo)[j].sg_last;
1597 q && !DRD_(vc_lte)(&q->vc, new_vc);
1598 q = q->thr_prev) {
1599 const Bool included_in_old_conflict_set
1600 = !DRD_(vc_lte)(old_vc, &q->vc);
1601 const Bool included_in_new_conflict_set
1602 = !DRD_(vc_lte)(new_vc, &q->vc);
1604 if (UNLIKELY(s_trace_conflict_set)) {
1605 HChar* str;
1607 str = DRD_(vc_aprint)(&q->vc);
1608 VG_(message)(Vg_DebugMsg,
1609 "conflict set: [%u] %s segment %s\n", j,
1610 included_in_old_conflict_set
1611 != included_in_new_conflict_set
1612 ? "merging" : "ignoring", str);
1613 VG_(free)(str);
1615 if (included_in_old_conflict_set != included_in_new_conflict_set)
1616 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1619 for ( ; q && !DRD_(vc_lte)(&q->vc, old_vc); q = q->thr_prev) {
1620 const Bool included_in_old_conflict_set
1621 = !DRD_(vc_lte)(old_vc, &q->vc);
1622 const Bool included_in_new_conflict_set
1623 = !DRD_(vc_lte)(&q->vc, new_vc)
1624 && !DRD_(vc_lte)(new_vc, &q->vc);
1626 if (UNLIKELY(s_trace_conflict_set)) {
1627 HChar* str;
1629 str = DRD_(vc_aprint)(&q->vc);
1630 VG_(message)(Vg_DebugMsg,
1631 "conflict set: [%u] %s segment %s\n", j,
1632 included_in_old_conflict_set
1633 != included_in_new_conflict_set
1634 ? "merging" : "ignoring", str);
1635 VG_(free)(str);
1637 if (included_in_old_conflict_set != included_in_new_conflict_set)
1638 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1642 DRD_(bm_clear_marked)(DRD_(g_conflict_set));
1644 p = DRD_(g_threadinfo)[tid].sg_last;
1645 for (j = 0; j < DRD_N_THREADS; j++) {
1646 if (j != tid && DRD_(IsValidDrdThreadId)(j)) {
1647 Segment* q;
1648 for (q = DRD_(g_threadinfo)[j].sg_last;
1649 q && !DRD_(vc_lte)(&q->vc, &p->vc);
1650 q = q->thr_prev) {
1651 if (!DRD_(vc_lte)(&p->vc, &q->vc))
1652 DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1657 DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set));
1659 s_update_conflict_set_count++;
1661 if (s_trace_conflict_set_bm)
1663 VG_(message)(Vg_DebugMsg, "[%u] updated conflict set:\n", tid);
1664 DRD_(bm_print)(DRD_(g_conflict_set));
1665 VG_(message)(Vg_DebugMsg, "[%u] end of updated conflict set.\n", tid);
1668 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1671 /** Report the number of context switches performed. */
1672 ULong DRD_(thread_get_context_switch_count)(void)
1674 return s_context_switch_count;
1677 /** Report the number of ordered segments that have been discarded. */
1678 ULong DRD_(thread_get_discard_ordered_segments_count)(void)
1680 return s_discard_ordered_segments_count;
1683 /** Return how many times the conflict set has been updated entirely. */
1684 ULong DRD_(thread_get_compute_conflict_set_count)(void)
1686 return s_compute_conflict_set_count;
1689 /** Return how many times the conflict set has been updated partially. */
1690 ULong DRD_(thread_get_update_conflict_set_count)(void)
1692 return s_update_conflict_set_count;
1696 * Return how many times the conflict set has been updated partially
1697 * because a new segment has been created.
1699 ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void)
1701 return s_update_conflict_set_new_sg_count;
1705 * Return how many times the conflict set has been updated partially
1706 * because of combining vector clocks due to synchronization operations
1707 * other than reader/writer lock or barrier operations.
1709 ULong DRD_(thread_get_update_conflict_set_sync_count)(void)
1711 return s_update_conflict_set_sync_count;
1715 * Return how many times the conflict set has been updated partially
1716 * because of thread joins.
1718 ULong DRD_(thread_get_update_conflict_set_join_count)(void)
1720 return s_update_conflict_set_join_count;
1724 * Return the number of first-level bitmaps that have been created during
1725 * conflict set updates.
1727 ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
1729 return s_conflict_set_bitmap_creation_count;
1733 * Return the number of second-level bitmaps that have been created during
1734 * conflict set updates.
1736 ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
1738 return s_conflict_set_bitmap2_creation_count;