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"
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
;
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
)
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
)
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
)
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
));
224 "\nSorry, but the maximum number of threads supported by DRD has been exceeded."
229 return DRD_INVALID_THREADID
;
232 /** Convert a POSIX thread ID into a DRD thread ID. */
233 DrdThreadId
DRD_(PtThreadIdToDrdThreadId
)(const PThreadId tid
)
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
)
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
266 * @return True if sane, False if not.
268 static Bool
DRD_(sane_ThreadInfo
)(const ThreadInfo
* const ti
)
272 for (p
= ti
->sg_first
; p
; p
= p
->thr_next
) {
273 if (p
->thr_next
&& p
->thr_next
->thr_prev
!= p
)
275 if (p
->thr_next
== 0 && p
!= ti
->sg_last
)
278 for (p
= ti
->sg_last
; p
; p
= p
->thr_prev
) {
279 if (p
->thr_prev
&& p
->thr_prev
->thr_next
!= p
)
281 if (p
->thr_prev
== 0 && p
!= ti
->sg_first
)
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
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
)
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
));
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);
361 static void DRD_(thread_delayed_delete
)(const DrdThreadId tid
)
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
++;
369 VG_(message
)(Vg_DebugMsg
, "Adding thread %d to the deletion list\n", tid
);
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
)
378 VG_(message
)(Vg_DebugMsg
, "Delayed delete of thread %d\n", j
);
380 DRD_(thread_delete
)(j
, False
);
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;
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
);
415 vc
= DRD_(vc_aprint
)(DRD_(thread_get_vc
)(drd_joiner
));
416 VG_(snprintf
)(msg
+ VG_(strlen
)(msg
), msg_size
- VG_(strlen
)(msg
),
420 DRD_(trace_msg
)("%pS", 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
);
435 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
436 * and accesses this data structure from multiple threads without locking.
437 * Any conflicting accesses in the range stack_startup..stack_max will be
440 void DRD_(thread_set_stack_startup
)(const DrdThreadId tid
,
441 const Addr stack_startup
)
443 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
444 && tid
!= DRD_INVALID_THREADID
);
445 tl_assert(DRD_(g_threadinfo
)[tid
].stack_min
<= stack_startup
);
446 tl_assert(stack_startup
<= DRD_(g_threadinfo
)[tid
].stack_max
);
447 DRD_(g_threadinfo
)[tid
].stack_startup
= stack_startup
;
450 /** Return the stack pointer for the specified thread. */
451 Addr
DRD_(thread_get_stack_min
)(const DrdThreadId tid
)
453 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
454 && tid
!= DRD_INVALID_THREADID
);
455 return DRD_(g_threadinfo
)[tid
].stack_min
;
459 * Return the lowest value that was ever assigned to the stack pointer
460 * for the specified thread.
462 Addr
DRD_(thread_get_stack_min_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_min
;
469 /** Return the top address for the stack of the specified thread. */
470 Addr
DRD_(thread_get_stack_max
)(const DrdThreadId tid
)
472 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
473 && tid
!= DRD_INVALID_THREADID
);
474 return DRD_(g_threadinfo
)[tid
].stack_max
;
477 /** Return the maximum stack size for the specified thread. */
478 SizeT
DRD_(thread_get_stack_size
)(const DrdThreadId tid
)
480 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
481 && tid
!= DRD_INVALID_THREADID
);
482 return DRD_(g_threadinfo
)[tid
].stack_size
;
485 Bool
DRD_(thread_get_on_alt_stack
)(const DrdThreadId tid
)
487 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
488 && tid
!= DRD_INVALID_THREADID
);
489 return DRD_(g_threadinfo
)[tid
].on_alt_stack
;
492 void DRD_(thread_set_on_alt_stack
)(const DrdThreadId tid
,
493 const Bool on_alt_stack
)
495 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
496 && tid
!= DRD_INVALID_THREADID
);
497 tl_assert(on_alt_stack
== !!on_alt_stack
);
498 DRD_(g_threadinfo
)[tid
].on_alt_stack
= on_alt_stack
;
501 Int
DRD_(thread_get_threads_on_alt_stack
)(void)
505 for (UInt i
= 1; i
< DRD_N_THREADS
; i
++)
506 n
+= DRD_(g_threadinfo
)[i
].on_alt_stack
;
511 * Clean up thread-specific data structures.
513 void DRD_(thread_delete
)(const DrdThreadId tid
, const Bool detached
)
518 tl_assert(DRD_(IsValidDrdThreadId
)(tid
));
520 tl_assert(DRD_(g_threadinfo
)[tid
].synchr_nesting
>= 0);
521 for (sg
= DRD_(g_threadinfo
)[tid
].sg_last
; sg
; sg
= sg_prev
) {
522 sg_prev
= sg
->thr_prev
;
527 DRD_(g_threadinfo
)[tid
].valid
= False
;
528 DRD_(g_threadinfo
)[tid
].vg_thread_exists
= False
;
529 DRD_(g_threadinfo
)[tid
].posix_thread_exists
= False
;
531 DRD_(g_threadinfo
)[tid
].detached_posix_thread
= False
;
533 tl_assert(!DRD_(g_threadinfo
)[tid
].detached_posix_thread
);
534 DRD_(g_threadinfo
)[tid
].sg_first
= NULL
;
535 DRD_(g_threadinfo
)[tid
].sg_last
= NULL
;
537 tl_assert(!DRD_(IsValidDrdThreadId
)(tid
));
541 * Called after a thread performed its last memory access and before
542 * thread_delete() is called. Note: thread_delete() is only called for
543 * joinable threads, not for detached threads.
545 void DRD_(thread_finished
)(const DrdThreadId tid
)
547 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
548 && tid
!= DRD_INVALID_THREADID
);
550 DRD_(g_threadinfo
)[tid
].vg_thread_exists
= False
;
552 if (DRD_(g_threadinfo
)[tid
].detached_posix_thread
)
555 * Once a detached thread has finished, its stack is deallocated and
556 * should no longer be taken into account when computing the conflict set.
558 DRD_(g_threadinfo
)[tid
].stack_min
= DRD_(g_threadinfo
)[tid
].stack_max
;
561 * For a detached thread, calling pthread_exit() invalidates the
562 * POSIX thread ID associated with the detached thread. For joinable
563 * POSIX threads however, the POSIX thread ID remains live after the
564 * pthread_exit() call until pthread_join() is called.
566 DRD_(g_threadinfo
)[tid
].posix_thread_exists
= False
;
570 /** Called just after fork() in the child process. */
571 void DRD_(drd_thread_atfork_child
)(const ThreadId tid
)
575 for (i
= 1; i
< DRD_N_THREADS
; i
++)
577 if (DRD_(g_threadinfo
)[i
].vg_threadid
== tid
)
579 if (DRD_(IsValidDrdThreadId(i
)))
580 DRD_(thread_delete
)(i
, True
);
581 tl_assert(!DRD_(IsValidDrdThreadId(i
)));
584 DRD_(bm_cleanup
)(DRD_(g_conflict_set
));
585 DRD_(bm_init
)(DRD_(g_conflict_set
));
588 /** Called just before pthread_cancel(). */
589 void DRD_(thread_pre_cancel
)(const DrdThreadId tid
)
591 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
592 && tid
!= DRD_INVALID_THREADID
);
593 tl_assert(DRD_(g_threadinfo
)[tid
].pt_threadid
!= INVALID_POSIX_THREADID
);
595 if (DRD_(thread_get_trace_fork_join
)())
596 DRD_(trace_msg
)("[%u] drd_thread_pre_cancel %u",
597 DRD_(g_drd_running_tid
), tid
);
601 * Store the POSIX thread ID for the specified thread.
603 * @note This function can be called multiple times for the same thread -- see
604 * also the comment block preceding the pthread_create() wrapper in
605 * drd_pthread_intercepts.c.
607 void DRD_(thread_set_pthreadid
)(const DrdThreadId tid
, const PThreadId ptid
)
609 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
610 && tid
!= DRD_INVALID_THREADID
);
611 tl_assert(DRD_(g_threadinfo
)[tid
].pt_threadid
== INVALID_POSIX_THREADID
612 || DRD_(g_threadinfo
)[tid
].pt_threadid
== ptid
);
613 tl_assert(ptid
!= INVALID_POSIX_THREADID
);
614 if (DRD_(g_threadinfo
)[tid
].posix_thread_exists
) {
615 tl_assert(DRD_(g_threadinfo
)[tid
].pt_threadid
== ptid
);
618 DRD_(g_threadinfo
)[tid
].posix_thread_exists
= True
;
619 DRD_(g_threadinfo
)[tid
].pt_threadid
= ptid
;
621 if (DRD_(g_threadinfo
)[tid
].creator_thread
!= DRD_INVALID_THREADID
) {
622 if (DRD_(ignore_thread_creation
)) {
623 DRD_(thread_leave_synchr
)(tid
);
624 tl_assert(DRD_(thread_get_synchr_nesting_count
)(tid
) == 0);
629 /** Returns true for joinable threads and false for detached threads. */
630 Bool
DRD_(thread_get_joinable
)(const DrdThreadId tid
)
632 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
633 && tid
!= DRD_INVALID_THREADID
);
634 return ! DRD_(g_threadinfo
)[tid
].detached_posix_thread
;
637 /** Store the thread mode: joinable or detached. */
638 #if defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
639 /* There is a cse related issue in gcc for MIPS. Optimization level
640 has to be lowered, so cse related optimizations are not
642 __attribute__((optimize("O1")))
644 void DRD_(thread_set_joinable
)(const DrdThreadId tid
, const Bool joinable
)
646 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
647 && tid
!= DRD_INVALID_THREADID
);
648 tl_assert((!! joinable
) == joinable
);
649 tl_assert(DRD_(g_threadinfo
)[tid
].pt_threadid
!= INVALID_POSIX_THREADID
);
651 DRD_(g_threadinfo
)[tid
].detached_posix_thread
= ! joinable
;
654 /** Tells DRD that the calling thread is about to enter pthread_create(). */
655 void DRD_(thread_entering_pthread_create
)(const DrdThreadId tid
)
657 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
658 && tid
!= DRD_INVALID_THREADID
);
659 tl_assert(DRD_(g_threadinfo
)[tid
].pt_threadid
!= INVALID_POSIX_THREADID
);
660 tl_assert(DRD_(g_threadinfo
)[tid
].pthread_create_nesting_level
>= 0);
662 DRD_(g_threadinfo
)[tid
].pthread_create_nesting_level
++;
664 if (DRD_(ignore_thread_creation
)) {
665 tl_assert(DRD_(thread_get_synchr_nesting_count
)(tid
) == 0);
666 DRD_(thread_enter_synchr
)(tid
);
670 /** Tells DRD that the calling thread has left pthread_create(). */
671 void DRD_(thread_left_pthread_create
)(const DrdThreadId tid
)
673 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
674 && tid
!= DRD_INVALID_THREADID
);
675 tl_assert(DRD_(g_threadinfo
)[tid
].pt_threadid
!= INVALID_POSIX_THREADID
);
676 tl_assert(DRD_(g_threadinfo
)[tid
].pthread_create_nesting_level
> 0);
678 DRD_(g_threadinfo
)[tid
].pthread_create_nesting_level
--;
680 if (DRD_(ignore_thread_creation
)) {
681 DRD_(thread_leave_synchr
)(tid
);
682 tl_assert(DRD_(thread_get_synchr_nesting_count
)(tid
) == 0);
686 #if defined(VGO_solaris)
687 /** Handles the bind_guard() intercept. */
688 void DRD_(thread_entering_rtld_bind_guard
)(const DrdThreadId tid
, int flags
)
690 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
691 && tid
!= DRD_INVALID_THREADID
);
693 Int bindflag
= (flags
& VKI_THR_FLG_RTLD
);
694 if ((bindflag
& DRD_(g_threadinfo
)[tid
].bind_guard_flag
) == 0) {
695 DRD_(g_threadinfo
)[tid
].bind_guard_flag
|= bindflag
;
696 DRD_(thread_enter_synchr
)(tid
);
701 * Handles the bind_clear() intercept.
702 * Call to bind_clear(0) is typically used to determine value of bind_flags.
704 void DRD_(thread_leaving_rtld_bind_clear
)(const DrdThreadId tid
, int flags
)
706 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
707 && tid
!= DRD_INVALID_THREADID
);
709 Int bindflag
= (flags
& VKI_THR_FLG_RTLD
);
710 if ((DRD_(g_threadinfo
)[tid
].bind_guard_flag
& bindflag
) != 0) {
711 DRD_(g_threadinfo
)[tid
].bind_guard_flag
&= ~bindflag
;
712 DRD_(thread_leave_synchr
)(tid
);
715 #endif /* VGO_solaris */
717 /** Obtain the thread number and the user-assigned thread name. */
718 const HChar
* DRD_(thread_get_name
)(const DrdThreadId tid
)
720 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
721 && tid
!= DRD_INVALID_THREADID
);
723 return DRD_(g_threadinfo
)[tid
].name
;
726 /** Set the name of the specified thread. */
727 void DRD_(thread_set_name
)(const DrdThreadId tid
, const HChar
* const name
)
729 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
730 && tid
!= DRD_INVALID_THREADID
);
732 if (name
== NULL
|| name
[0] == 0)
733 VG_(snprintf
)(DRD_(g_threadinfo
)[tid
].name
,
734 sizeof(DRD_(g_threadinfo
)[tid
].name
),
738 VG_(snprintf
)(DRD_(g_threadinfo
)[tid
].name
,
739 sizeof(DRD_(g_threadinfo
)[tid
].name
),
742 DRD_(g_threadinfo
)[tid
].name
[sizeof(DRD_(g_threadinfo
)[tid
].name
) - 1] = 0;
746 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
749 void DRD_(thread_set_vg_running_tid
)(const ThreadId vg_tid
)
751 tl_assert(vg_tid
!= VG_INVALID_THREADID
);
753 if (vg_tid
!= s_vg_running_tid
)
755 DRD_(thread_set_running_tid
)(vg_tid
,
756 DRD_(VgThreadIdToDrdThreadId
)(vg_tid
));
759 tl_assert(s_vg_running_tid
!= VG_INVALID_THREADID
);
760 tl_assert(DRD_(g_drd_running_tid
) != DRD_INVALID_THREADID
);
764 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
767 void DRD_(thread_set_running_tid
)(const ThreadId vg_tid
,
768 const DrdThreadId drd_tid
)
770 tl_assert(vg_tid
!= VG_INVALID_THREADID
);
771 tl_assert(drd_tid
!= DRD_INVALID_THREADID
);
773 if (vg_tid
!= s_vg_running_tid
)
775 if (s_trace_context_switches
776 && DRD_(g_drd_running_tid
) != DRD_INVALID_THREADID
)
778 VG_(message
)(Vg_DebugMsg
,
779 "Context switch from thread %u to thread %u;"
781 DRD_(g_drd_running_tid
), drd_tid
,
782 DRD_(sg_get_segments_alive_count
)());
784 s_vg_running_tid
= vg_tid
;
785 DRD_(g_drd_running_tid
) = drd_tid
;
786 thread_compute_conflict_set(&DRD_(g_conflict_set
), drd_tid
);
787 s_context_switch_count
++;
790 tl_assert(s_vg_running_tid
!= VG_INVALID_THREADID
);
791 tl_assert(DRD_(g_drd_running_tid
) != DRD_INVALID_THREADID
);
795 * Increase the synchronization nesting counter. Must be called before the
796 * client calls a synchronization function.
798 int DRD_(thread_enter_synchr
)(const DrdThreadId tid
)
800 tl_assert(DRD_(IsValidDrdThreadId
)(tid
));
801 return DRD_(g_threadinfo
)[tid
].synchr_nesting
++;
805 * Decrease the synchronization nesting counter. Must be called after the
806 * client left a synchronization function.
808 int DRD_(thread_leave_synchr
)(const DrdThreadId tid
)
810 tl_assert(DRD_(IsValidDrdThreadId
)(tid
));
811 tl_assert(DRD_(g_threadinfo
)[tid
].synchr_nesting
>= 1);
812 return --DRD_(g_threadinfo
)[tid
].synchr_nesting
;
815 /** Returns the synchronization nesting counter. */
816 int DRD_(thread_get_synchr_nesting_count
)(const DrdThreadId tid
)
818 tl_assert(DRD_(IsValidDrdThreadId
)(tid
));
819 return DRD_(g_threadinfo
)[tid
].synchr_nesting
;
822 /** Append a new segment at the end of the segment list. */
824 void thread_append_segment(const DrdThreadId tid
, Segment
* const sg
)
826 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
827 && tid
!= DRD_INVALID_THREADID
);
829 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
830 tl_assert(DRD_(sane_ThreadInfo
)(&DRD_(g_threadinfo
)[tid
]));
834 sg
->thr_prev
= DRD_(g_threadinfo
)[tid
].sg_last
;
836 if (DRD_(g_threadinfo
)[tid
].sg_last
)
837 DRD_(g_threadinfo
)[tid
].sg_last
->thr_next
= sg
;
838 DRD_(g_threadinfo
)[tid
].sg_last
= sg
;
839 if (DRD_(g_threadinfo
)[tid
].sg_first
== NULL
)
840 DRD_(g_threadinfo
)[tid
].sg_first
= sg
;
842 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
843 tl_assert(DRD_(sane_ThreadInfo
)(&DRD_(g_threadinfo
)[tid
]));
848 * Remove a segment from the segment list of thread threadid, and free the
852 void thread_discard_segment(const DrdThreadId tid
, Segment
* const sg
)
854 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
855 && tid
!= DRD_INVALID_THREADID
);
857 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
858 tl_assert(DRD_(sane_ThreadInfo
)(&DRD_(g_threadinfo
)[tid
]));
862 sg
->thr_prev
->thr_next
= sg
->thr_next
;
864 sg
->thr_next
->thr_prev
= sg
->thr_prev
;
865 if (sg
== DRD_(g_threadinfo
)[tid
].sg_first
)
866 DRD_(g_threadinfo
)[tid
].sg_first
= sg
->thr_next
;
867 if (sg
== DRD_(g_threadinfo
)[tid
].sg_last
)
868 DRD_(g_threadinfo
)[tid
].sg_last
= sg
->thr_prev
;
871 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
872 tl_assert(DRD_(sane_ThreadInfo
)(&DRD_(g_threadinfo
)[tid
]));
877 * Returns a pointer to the vector clock of the most recent segment associated
880 VectorClock
* DRD_(thread_get_vc
)(const DrdThreadId tid
)
884 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
885 && tid
!= DRD_INVALID_THREADID
);
886 latest_sg
= DRD_(g_threadinfo
)[tid
].sg_last
;
887 tl_assert(latest_sg
);
888 return &latest_sg
->vc
;
892 * Return the latest segment of thread 'tid' and increment its reference count.
894 void DRD_(thread_get_latest_segment
)(Segment
** sg
, const DrdThreadId tid
)
899 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
900 && tid
!= DRD_INVALID_THREADID
);
901 latest_sg
= DRD_(g_threadinfo
)[tid
].sg_last
;
902 tl_assert(latest_sg
);
905 *sg
= DRD_(sg_get
)(latest_sg
);
909 * Compute the minimum of all latest vector clocks of all threads
910 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
912 * @param vc pointer to a vectorclock, holds result upon return.
914 static void DRD_(thread_compute_minimum_vc
)(VectorClock
* vc
)
921 for (i
= 0; i
< DRD_N_THREADS
; i
++)
923 latest_sg
= DRD_(g_threadinfo
)[i
].sg_last
;
926 DRD_(vc_assign
)(vc
, &latest_sg
->vc
);
928 DRD_(vc_min
)(vc
, &latest_sg
->vc
);
935 * Compute the maximum of all latest vector clocks of all threads.
937 * @param vc pointer to a vectorclock, holds result upon return.
939 static void DRD_(thread_compute_maximum_vc
)(VectorClock
* vc
)
946 for (i
= 0; i
< DRD_N_THREADS
; i
++)
948 latest_sg
= DRD_(g_threadinfo
)[i
].sg_last
;
951 DRD_(vc_assign
)(vc
, &latest_sg
->vc
);
953 DRD_(vc_combine
)(vc
, &latest_sg
->vc
);
960 * Discard all segments that have a defined order against the latest vector
961 * clock of all threads -- these segments can no longer be involved in a
964 static void thread_discard_ordered_segments(void)
967 VectorClock thread_vc_min
;
969 s_discard_ordered_segments_count
++;
971 DRD_(vc_init
)(&thread_vc_min
, 0, 0);
972 DRD_(thread_compute_minimum_vc
)(&thread_vc_min
);
973 if (DRD_(sg_get_trace
)())
975 HChar
*vc_min
, *vc_max
;
976 VectorClock thread_vc_max
;
978 DRD_(vc_init
)(&thread_vc_max
, 0, 0);
979 DRD_(thread_compute_maximum_vc
)(&thread_vc_max
);
980 vc_min
= DRD_(vc_aprint
)(&thread_vc_min
);
981 vc_max
= DRD_(vc_aprint
)(&thread_vc_max
);
982 VG_(message
)(Vg_DebugMsg
,
983 "Discarding ordered segments -- min vc is %s, max vc is %s\n",
987 DRD_(vc_cleanup
)(&thread_vc_max
);
990 for (i
= 0; i
< DRD_N_THREADS
; i
++) {
994 for (sg
= DRD_(g_threadinfo
)[i
].sg_first
;
995 sg
&& (sg_next
= sg
->thr_next
)
996 && DRD_(vc_lte
)(&sg
->vc
, &thread_vc_min
);
999 thread_discard_segment(i
, sg
);
1002 DRD_(vc_cleanup
)(&thread_vc_min
);
1006 * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper
1007 * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if
1008 * all segments in the set CS are ordered consistently against both sg1 and
1009 * sg2. The set CS is defined as the set of segments that can immediately
1010 * precede future segments via inter-thread synchronization operations. In
1011 * DRD the set CS consists of the latest segment of each thread combined with
1012 * all segments for which the reference count is strictly greater than one.
1013 * The code below is an optimized version of the following:
1015 * for (i = 0; i < DRD_N_THREADS; i++)
1019 * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
1021 * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1)
1023 * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc)
1024 * != DRD_(vc_lte)(&sg2->vc, &sg->vc)
1025 * || DRD_(vc_lte)(&sg->vc, &sg1->vc)
1026 * != DRD_(vc_lte)(&sg->vc, &sg2->vc))
1034 static Bool
thread_consistent_segment_ordering(const DrdThreadId tid
,
1040 tl_assert(sg1
->thr_next
);
1041 tl_assert(sg2
->thr_next
);
1042 tl_assert(sg1
->thr_next
== sg2
);
1043 tl_assert(DRD_(vc_lte
)(&sg1
->vc
, &sg2
->vc
));
1045 for (i
= 0; i
< DRD_N_THREADS
; i
++)
1049 for (sg
= DRD_(g_threadinfo
)[i
].sg_first
; sg
; sg
= sg
->thr_next
) {
1050 if (!sg
->thr_next
|| DRD_(sg_get_refcnt
)(sg
) > 1) {
1051 if (DRD_(vc_lte
)(&sg2
->vc
, &sg
->vc
))
1053 if (DRD_(vc_lte
)(&sg1
->vc
, &sg
->vc
))
1057 for (sg
= DRD_(g_threadinfo
)[i
].sg_last
; sg
; sg
= sg
->thr_prev
) {
1058 if (!sg
->thr_next
|| DRD_(sg_get_refcnt
)(sg
) > 1) {
1059 if (DRD_(vc_lte
)(&sg
->vc
, &sg1
->vc
))
1061 if (DRD_(vc_lte
)(&sg
->vc
, &sg2
->vc
))
1070 * Merge all segments that may be merged without triggering false positives
1071 * or discarding real data races. For the theoretical background of segment
1072 * merging, see also the following paper: Mark Christiaens, Michiel Ronsse
1073 * and Koen De Bosschere. Bounding the number of segment histories during
1074 * data race detection. Parallel Computing archive, Volume 28, Issue 9,
1075 * pp 1221-1238, September 2002. This paper contains a proof that merging
1076 * consecutive segments for which the property equiv(s1,s2) holds can be
1077 * merged without reducing the accuracy of datarace detection. Furthermore
1078 * it is also proven that the total number of all segments will never grow
1079 * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged
1080 * every time a new segment is created. The property equiv(s1, s2) is defined
1081 * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector
1082 * clocks of segments s and s1 are ordered in the same way as those of segments
1083 * s and s2. The set CS is defined as the set of existing segments s that have
1084 * the potential to conflict with not yet created segments, either because the
1085 * segment s is the latest segment of a thread or because it can become the
1086 * immediate predecessor of a new segment due to a synchronization operation.
1088 static void thread_merge_segments(void)
1092 s_new_segments_since_last_merge
= 0;
1094 for (i
= 0; i
< DRD_N_THREADS
; i
++)
1098 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1099 tl_assert(DRD_(sane_ThreadInfo
)(&DRD_(g_threadinfo
)[i
]));
1102 for (sg
= DRD_(g_threadinfo
)[i
].sg_first
; sg
; sg
= sg
->thr_next
) {
1103 if (DRD_(sg_get_refcnt
)(sg
) == 1 && sg
->thr_next
) {
1104 Segment
* const sg_next
= sg
->thr_next
;
1105 if (DRD_(sg_get_refcnt
)(sg_next
) == 1
1106 && sg_next
->thr_next
1107 && thread_consistent_segment_ordering(i
, sg
, sg_next
))
1109 /* Merge sg and sg_next into sg. */
1110 DRD_(sg_merge
)(sg
, sg_next
);
1111 thread_discard_segment(i
, sg_next
);
1116 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1117 tl_assert(DRD_(sane_ThreadInfo
)(&DRD_(g_threadinfo
)[i
]));
1123 * Create a new segment for the specified thread, and discard any segments
1124 * that cannot cause races anymore.
1126 void DRD_(thread_new_segment
)(const DrdThreadId tid
)
1131 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
1132 && tid
!= DRD_INVALID_THREADID
);
1133 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid
)));
1135 last_sg
= DRD_(g_threadinfo
)[tid
].sg_last
;
1136 new_sg
= DRD_(sg_new
)(tid
, tid
);
1137 thread_append_segment(tid
, new_sg
);
1138 if (tid
== DRD_(g_drd_running_tid
) && last_sg
)
1140 DRD_(thread_update_conflict_set
)(tid
, &last_sg
->vc
);
1141 s_update_conflict_set_new_sg_count
++;
1144 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid
)));
1146 if (s_segment_merging
1147 && ++s_new_segments_since_last_merge
>= s_segment_merge_interval
)
1149 thread_discard_ordered_segments();
1150 thread_merge_segments();
1154 /** Call this function after thread 'joiner' joined thread 'joinee'. */
1155 void DRD_(thread_combine_vc_join
)(DrdThreadId joiner
, DrdThreadId joinee
)
1157 tl_assert(joiner
!= joinee
);
1158 tl_assert(0 <= (int)joiner
&& joiner
< DRD_N_THREADS
1159 && joiner
!= DRD_INVALID_THREADID
);
1160 tl_assert(0 <= (int)joinee
&& joinee
< DRD_N_THREADS
1161 && joinee
!= DRD_INVALID_THREADID
);
1162 tl_assert(DRD_(g_threadinfo
)[joiner
].sg_first
);
1163 tl_assert(DRD_(g_threadinfo
)[joiner
].sg_last
);
1164 tl_assert(DRD_(g_threadinfo
)[joinee
].sg_first
);
1165 tl_assert(DRD_(g_threadinfo
)[joinee
].sg_last
);
1167 if (DRD_(sg_get_trace
)())
1170 str1
= DRD_(vc_aprint
)(DRD_(thread_get_vc
)(joiner
));
1171 str2
= DRD_(vc_aprint
)(DRD_(thread_get_vc
)(joinee
));
1172 VG_(message
)(Vg_DebugMsg
, "Before join: joiner %s, joinee %s\n",
1177 if (joiner
== DRD_(g_drd_running_tid
)) {
1180 DRD_(vc_copy
)(&old_vc
, DRD_(thread_get_vc
)(joiner
));
1181 DRD_(vc_combine
)(DRD_(thread_get_vc
)(joiner
),
1182 DRD_(thread_get_vc
)(joinee
));
1183 DRD_(thread_update_conflict_set
)(joiner
, &old_vc
);
1184 s_update_conflict_set_join_count
++;
1185 DRD_(vc_cleanup
)(&old_vc
);
1187 DRD_(vc_combine
)(DRD_(thread_get_vc
)(joiner
),
1188 DRD_(thread_get_vc
)(joinee
));
1191 thread_discard_ordered_segments();
1193 if (DRD_(sg_get_trace
)()) {
1196 str
= DRD_(vc_aprint
)(DRD_(thread_get_vc
)(joiner
));
1197 VG_(message
)(Vg_DebugMsg
, "After join: %s\n", str
);
1203 * Update the vector clock of the last segment of thread tid with the
1204 * the vector clock of segment sg.
1206 static void thread_combine_vc_sync(DrdThreadId tid
, const Segment
* sg
)
1208 const VectorClock
* const vc
= &sg
->vc
;
1210 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
1211 && tid
!= DRD_INVALID_THREADID
);
1212 tl_assert(DRD_(g_threadinfo
)[tid
].sg_first
);
1213 tl_assert(DRD_(g_threadinfo
)[tid
].sg_last
);
1217 if (tid
!= sg
->tid
) {
1220 DRD_(vc_copy
)(&old_vc
, DRD_(thread_get_vc
)(tid
));
1221 DRD_(vc_combine
)(DRD_(thread_get_vc
)(tid
), vc
);
1222 if (DRD_(sg_get_trace
)()) {
1224 str1
= DRD_(vc_aprint
)(&old_vc
);
1225 str2
= DRD_(vc_aprint
)(DRD_(thread_get_vc
)(tid
));
1226 VG_(message
)(Vg_DebugMsg
, "thread %u: vc %s -> %s\n", tid
, str1
, str2
);
1231 thread_discard_ordered_segments();
1233 DRD_(thread_update_conflict_set
)(tid
, &old_vc
);
1234 s_update_conflict_set_sync_count
++;
1236 DRD_(vc_cleanup
)(&old_vc
);
1238 tl_assert(DRD_(vc_lte
)(vc
, DRD_(thread_get_vc
)(tid
)));
1243 * Create a new segment for thread tid and update the vector clock of the last
1244 * segment of this thread with the vector clock of segment sg. Call this
1245 * function after thread tid had to wait because of thread synchronization
1246 * until the memory accesses in the segment sg finished.
1248 void DRD_(thread_new_segment_and_combine_vc
)(DrdThreadId tid
, const Segment
* sg
)
1250 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
1251 && tid
!= DRD_INVALID_THREADID
);
1252 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid
)));
1255 thread_append_segment(tid
, DRD_(sg_new
)(tid
, tid
));
1257 thread_combine_vc_sync(tid
, sg
);
1259 if (s_segment_merging
1260 && ++s_new_segments_since_last_merge
>= s_segment_merge_interval
)
1262 thread_discard_ordered_segments();
1263 thread_merge_segments();
1268 * Call this function whenever a thread is no longer using the memory
1269 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
1272 void DRD_(thread_stop_using_mem
)(const Addr a1
, const Addr a2
)
1276 for (p
= DRD_(g_sg_list
); p
; p
= p
->g_next
)
1277 DRD_(bm_clear
)(DRD_(sg_bm
)(p
), a1
, a2
);
1279 DRD_(bm_clear
)(DRD_(g_conflict_set
), a1
, a2
);
1282 /** Specify whether memory loads should be recorded. */
1283 void DRD_(thread_set_record_loads
)(const DrdThreadId tid
, const Bool enabled
)
1285 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
1286 && tid
!= DRD_INVALID_THREADID
);
1287 tl_assert(enabled
== !! enabled
);
1289 DRD_(g_threadinfo
)[tid
].is_recording_loads
= enabled
;
1292 /** Specify whether memory stores should be recorded. */
1293 void DRD_(thread_set_record_stores
)(const DrdThreadId tid
, const Bool enabled
)
1295 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
1296 && tid
!= DRD_INVALID_THREADID
);
1297 tl_assert(enabled
== !! enabled
);
1299 DRD_(g_threadinfo
)[tid
].is_recording_stores
= enabled
;
1303 * Print the segment information for all threads.
1305 * This function is only used for debugging purposes.
1307 void DRD_(thread_print_all
)(void)
1312 for (i
= 0; i
< DRD_N_THREADS
; i
++)
1314 p
= DRD_(g_threadinfo
)[i
].sg_first
;
1316 VG_(printf
)("**************\n"
1317 "* thread %3u (%d/%u/%u/%u/0x%lx/%d) *\n"
1320 DRD_(g_threadinfo
)[i
].valid
,
1321 DRD_(g_threadinfo
)[i
].vg_thread_exists
,
1322 DRD_(g_threadinfo
)[i
].vg_threadid
,
1323 DRD_(g_threadinfo
)[i
].posix_thread_exists
,
1324 DRD_(g_threadinfo
)[i
].pt_threadid
,
1325 DRD_(g_threadinfo
)[i
].detached_posix_thread
);
1326 for ( ; p
; p
= p
->thr_next
)
1332 /** Show a call stack involved in a data race. */
1333 static void show_call_stack(const DrdThreadId tid
, ExeContext
* const callstack
)
1335 const ThreadId vg_tid
= DRD_(DrdThreadIdToVgThreadId
)(tid
);
1337 if (vg_tid
!= VG_INVALID_THREADID
) {
1339 VG_(pp_ExeContext
)(callstack
);
1341 VG_(get_and_pp_StackTrace
)(vg_tid
, VG_(clo_backtrace_size
));
1344 VG_(message
)(Vg_UserMsg
,
1345 " (thread finished, call stack no longer available)\n");
1349 /** Print information about the segments involved in a data race. */
1351 thread_report_conflicting_segments_segment(const DrdThreadId tid
,
1354 const BmAccessTypeT access_type
,
1355 const Segment
* const p
)
1359 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
1360 && tid
!= DRD_INVALID_THREADID
);
1363 for (i
= 0; i
< DRD_N_THREADS
; i
++) {
1367 for (q
= DRD_(g_threadinfo
)[i
].sg_last
; q
; q
= q
->thr_prev
) {
1369 * Since q iterates over the segments of thread i in order of
1370 * decreasing vector clocks, if q->vc <= p->vc, then
1371 * q->next->vc <= p->vc will also hold. Hence, break out of the
1372 * loop once this condition is met.
1374 if (DRD_(vc_lte
)(&q
->vc
, &p
->vc
))
1376 if (!DRD_(vc_lte
)(&p
->vc
, &q
->vc
)) {
1377 if (DRD_(bm_has_conflict_with
)(DRD_(sg_bm
)(q
), addr
, addr
+ size
,
1381 tl_assert(q
->stacktrace
);
1383 VG_(printf_xml
)(" <other_segment_start>\n");
1385 VG_(message
)(Vg_UserMsg
,
1386 "Other segment start (thread %u)\n", i
);
1387 show_call_stack(i
, q
->stacktrace
);
1389 VG_(printf_xml
)(" </other_segment_start>\n"
1390 " <other_segment_end>\n");
1392 VG_(message
)(Vg_UserMsg
,
1393 "Other segment end (thread %u)\n", i
);
1394 q_next
= q
->thr_next
;
1395 show_call_stack(i
, q_next
? q_next
->stacktrace
: 0);
1397 VG_(printf_xml
)(" </other_segment_end>\n");
1405 /** Print information about all segments involved in a data race. */
1406 void DRD_(thread_report_conflicting_segments
)(const DrdThreadId tid
,
1409 const BmAccessTypeT access_type
)
1413 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
1414 && tid
!= DRD_INVALID_THREADID
);
1416 for (p
= DRD_(g_threadinfo
)[tid
].sg_first
; p
; p
= p
->thr_next
) {
1417 if (DRD_(bm_has
)(DRD_(sg_bm
)(p
), addr
, addr
+ size
, access_type
))
1418 thread_report_conflicting_segments_segment(tid
, addr
, size
,
1424 * Verify whether the conflict set for thread tid is up to date. Only perform
1425 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
1427 static Bool
thread_conflict_set_up_to_date(const DrdThreadId tid
)
1430 struct bitmap
* computed_conflict_set
= 0;
1432 if (!DRD_(verify_conflict_set
))
1435 thread_compute_conflict_set(&computed_conflict_set
, tid
);
1436 result
= DRD_(bm_equal
)(DRD_(g_conflict_set
), computed_conflict_set
);
1439 VG_(printf
)("actual conflict set:\n");
1440 DRD_(bm_print
)(DRD_(g_conflict_set
));
1442 VG_(printf
)("computed conflict set:\n");
1443 DRD_(bm_print
)(computed_conflict_set
);
1446 DRD_(bm_delete
)(computed_conflict_set
);
1451 * Compute the conflict set: a bitmap that represents the union of all memory
1452 * accesses of all segments that are unordered to the current segment of the
1455 static void thread_compute_conflict_set(struct bitmap
** conflict_set
,
1456 const DrdThreadId tid
)
1460 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
1461 && tid
!= DRD_INVALID_THREADID
);
1462 tl_assert(tid
== DRD_(g_drd_running_tid
));
1464 s_compute_conflict_set_count
++;
1465 s_conflict_set_bitmap_creation_count
1466 -= DRD_(bm_get_bitmap_creation_count
)();
1467 s_conflict_set_bitmap2_creation_count
1468 -= DRD_(bm_get_bitmap2_creation_count
)();
1470 if (*conflict_set
) {
1471 DRD_(bm_cleanup
)(*conflict_set
);
1472 DRD_(bm_init
)(*conflict_set
);
1474 *conflict_set
= DRD_(bm_new
)();
1477 if (s_trace_conflict_set
) {
1480 str
= DRD_(vc_aprint
)(DRD_(thread_get_vc
)(tid
));
1481 VG_(message
)(Vg_DebugMsg
,
1482 "computing conflict set for thread %u with vc %s\n",
1487 p
= DRD_(g_threadinfo
)[tid
].sg_last
;
1491 if (s_trace_conflict_set
) {
1494 vc
= DRD_(vc_aprint
)(&p
->vc
);
1495 VG_(message
)(Vg_DebugMsg
, "conflict set: thread [%u] at vc %s\n",
1500 for (j
= 0; j
< DRD_N_THREADS
; j
++) {
1501 if (j
!= tid
&& DRD_(IsValidDrdThreadId
)(j
)) {
1504 for (q
= DRD_(g_threadinfo
)[j
].sg_last
; q
; q
= q
->thr_prev
) {
1505 if (!DRD_(vc_lte
)(&q
->vc
, &p
->vc
)
1506 && !DRD_(vc_lte
)(&p
->vc
, &q
->vc
)) {
1507 if (s_trace_conflict_set
) {
1510 str
= DRD_(vc_aprint
)(&q
->vc
);
1511 VG_(message
)(Vg_DebugMsg
,
1512 "conflict set: [%u] merging segment %s\n",
1516 DRD_(bm_merge2
)(*conflict_set
, DRD_(sg_bm
)(q
));
1518 if (s_trace_conflict_set
) {
1521 str
= DRD_(vc_aprint
)(&q
->vc
);
1522 VG_(message
)(Vg_DebugMsg
,
1523 "conflict set: [%u] ignoring segment %s\n",
1533 s_conflict_set_bitmap_creation_count
1534 += DRD_(bm_get_bitmap_creation_count
)();
1535 s_conflict_set_bitmap2_creation_count
1536 += DRD_(bm_get_bitmap2_creation_count
)();
1538 if (s_trace_conflict_set_bm
) {
1539 VG_(message
)(Vg_DebugMsg
, "[%u] new conflict set:\n", tid
);
1540 DRD_(bm_print
)(*conflict_set
);
1541 VG_(message
)(Vg_DebugMsg
, "[%u] end of new conflict set.\n", tid
);
1546 * Update the conflict set after the vector clock of thread tid has been
1547 * updated from old_vc to its current value, either because a new segment has
1548 * been created or because of a synchronization operation.
1550 void DRD_(thread_update_conflict_set
)(const DrdThreadId tid
,
1551 const VectorClock
* const old_vc
)
1553 const VectorClock
* new_vc
;
1557 tl_assert(0 <= (int)tid
&& tid
< DRD_N_THREADS
1558 && tid
!= DRD_INVALID_THREADID
);
1560 tl_assert(tid
== DRD_(g_drd_running_tid
));
1561 tl_assert(DRD_(g_conflict_set
));
1563 if (s_trace_conflict_set
) {
1566 str
= DRD_(vc_aprint
)(DRD_(thread_get_vc
)(tid
));
1567 VG_(message
)(Vg_DebugMsg
,
1568 "updating conflict set for thread %u with vc %s\n",
1573 new_vc
= DRD_(thread_get_vc
)(tid
);
1574 tl_assert(DRD_(vc_lte
)(old_vc
, new_vc
));
1576 DRD_(bm_unmark
)(DRD_(g_conflict_set
));
1578 for (j
= 0; j
< DRD_N_THREADS
; j
++)
1582 if (j
== tid
|| ! DRD_(IsValidDrdThreadId
)(j
))
1585 for (q
= DRD_(g_threadinfo
)[j
].sg_last
;
1586 q
&& !DRD_(vc_lte
)(&q
->vc
, new_vc
);
1588 const Bool included_in_old_conflict_set
1589 = !DRD_(vc_lte
)(old_vc
, &q
->vc
);
1590 const Bool included_in_new_conflict_set
1591 = !DRD_(vc_lte
)(new_vc
, &q
->vc
);
1593 if (UNLIKELY(s_trace_conflict_set
)) {
1596 str
= DRD_(vc_aprint
)(&q
->vc
);
1597 VG_(message
)(Vg_DebugMsg
,
1598 "conflict set: [%u] %s segment %s\n", j
,
1599 included_in_old_conflict_set
1600 != included_in_new_conflict_set
1601 ? "merging" : "ignoring", str
);
1604 if (included_in_old_conflict_set
!= included_in_new_conflict_set
)
1605 DRD_(bm_mark
)(DRD_(g_conflict_set
), DRD_(sg_bm
)(q
));
1608 for ( ; q
&& !DRD_(vc_lte
)(&q
->vc
, old_vc
); q
= q
->thr_prev
) {
1609 const Bool included_in_old_conflict_set
1610 = !DRD_(vc_lte
)(old_vc
, &q
->vc
);
1611 const Bool included_in_new_conflict_set
1612 = !DRD_(vc_lte
)(&q
->vc
, new_vc
)
1613 && !DRD_(vc_lte
)(new_vc
, &q
->vc
);
1615 if (UNLIKELY(s_trace_conflict_set
)) {
1618 str
= DRD_(vc_aprint
)(&q
->vc
);
1619 VG_(message
)(Vg_DebugMsg
,
1620 "conflict set: [%u] %s segment %s\n", j
,
1621 included_in_old_conflict_set
1622 != included_in_new_conflict_set
1623 ? "merging" : "ignoring", str
);
1626 if (included_in_old_conflict_set
!= included_in_new_conflict_set
)
1627 DRD_(bm_mark
)(DRD_(g_conflict_set
), DRD_(sg_bm
)(q
));
1631 DRD_(bm_clear_marked
)(DRD_(g_conflict_set
));
1633 p
= DRD_(g_threadinfo
)[tid
].sg_last
;
1634 for (j
= 0; j
< DRD_N_THREADS
; j
++) {
1635 if (j
!= tid
&& DRD_(IsValidDrdThreadId
)(j
)) {
1637 for (q
= DRD_(g_threadinfo
)[j
].sg_last
;
1638 q
&& !DRD_(vc_lte
)(&q
->vc
, &p
->vc
);
1640 if (!DRD_(vc_lte
)(&p
->vc
, &q
->vc
))
1641 DRD_(bm_merge2_marked
)(DRD_(g_conflict_set
), DRD_(sg_bm
)(q
));
1646 DRD_(bm_remove_cleared_marked
)(DRD_(g_conflict_set
));
1648 s_update_conflict_set_count
++;
1650 if (s_trace_conflict_set_bm
)
1652 VG_(message
)(Vg_DebugMsg
, "[%u] updated conflict set:\n", tid
);
1653 DRD_(bm_print
)(DRD_(g_conflict_set
));
1654 VG_(message
)(Vg_DebugMsg
, "[%u] end of updated conflict set.\n", tid
);
1657 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid
)));
1660 /** Report the number of context switches performed. */
1661 ULong
DRD_(thread_get_context_switch_count
)(void)
1663 return s_context_switch_count
;
1666 /** Report the number of ordered segments that have been discarded. */
1667 ULong
DRD_(thread_get_discard_ordered_segments_count
)(void)
1669 return s_discard_ordered_segments_count
;
1672 /** Return how many times the conflict set has been updated entirely. */
1673 ULong
DRD_(thread_get_compute_conflict_set_count
)()
1675 return s_compute_conflict_set_count
;
1678 /** Return how many times the conflict set has been updated partially. */
1679 ULong
DRD_(thread_get_update_conflict_set_count
)(void)
1681 return s_update_conflict_set_count
;
1685 * Return how many times the conflict set has been updated partially
1686 * because a new segment has been created.
1688 ULong
DRD_(thread_get_update_conflict_set_new_sg_count
)(void)
1690 return s_update_conflict_set_new_sg_count
;
1694 * Return how many times the conflict set has been updated partially
1695 * because of combining vector clocks due to synchronization operations
1696 * other than reader/writer lock or barrier operations.
1698 ULong
DRD_(thread_get_update_conflict_set_sync_count
)(void)
1700 return s_update_conflict_set_sync_count
;
1704 * Return how many times the conflict set has been updated partially
1705 * because of thread joins.
1707 ULong
DRD_(thread_get_update_conflict_set_join_count
)(void)
1709 return s_update_conflict_set_join_count
;
1713 * Return the number of first-level bitmaps that have been created during
1714 * conflict set updates.
1716 ULong
DRD_(thread_get_conflict_set_bitmap_creation_count
)(void)
1718 return s_conflict_set_bitmap_creation_count
;
1722 * Return the number of second-level bitmaps that have been created during
1723 * conflict set updates.
1725 ULong
DRD_(thread_get_conflict_set_bitmap2_creation_count
)(void)
1727 return s_conflict_set_bitmap2_creation_count
;