blob: 9ada77dfe3778618a25562d8462902976e268236 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00003
Elliott Hughesed398002017-06-21 14:41:24 -07004 Copyright (C) 2006-2017 Bart Van Assche <bvanassche@acm.org>.
sewardjaf44c822007-11-25 14:01:38 +00005
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.
10
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.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307, USA.
20
21 The GNU General Public License is contained in the file COPYING.
22*/
23
24
25#include "drd_error.h"
bart09dc13f2009-02-14 15:13:31 +000026#include "drd_barrier.h"
bartd2c5eae2009-02-21 15:27:04 +000027#include "drd_clientobj.h"
bart09dc13f2009-02-14 15:13:31 +000028#include "drd_cond.h"
29#include "drd_mutex.h"
sewardjaf44c822007-11-25 14:01:38 +000030#include "drd_segment.h"
bart09dc13f2009-02-14 15:13:31 +000031#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000032#include "drd_suppression.h"
33#include "drd_thread.h"
bart82195c12008-04-13 17:35:08 +000034#include "pub_tool_vki.h"
sewardjaf44c822007-11-25 14:01:38 +000035#include "pub_tool_basics.h" // Addr, SizeT
sewardjaf44c822007-11-25 14:01:38 +000036#include "pub_tool_libcassert.h" // tl_assert()
37#include "pub_tool_libcbase.h" // VG_(strlen)()
38#include "pub_tool_libcprint.h" // VG_(printf)()
39#include "pub_tool_machine.h"
40#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardj85642922008-01-14 11:54:56 +000041#include "pub_tool_options.h" // VG_(clo_backtrace_size)
sewardjaf44c822007-11-25 14:01:38 +000042#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
43
bart32ba2082008-06-05 08:53:42 +000044
sewardjaf44c822007-11-25 14:01:38 +000045
bart324a23b2009-02-15 12:14:52 +000046/* Local functions. */
sewardjaf44c822007-11-25 14:01:38 +000047
bart86a87df2009-03-04 19:26:47 +000048static void thread_append_segment(const DrdThreadId tid, Segment* const sg);
49static void thread_discard_segment(const DrdThreadId tid, Segment* const sg);
50static void thread_compute_conflict_set(struct bitmap** conflict_set,
51 const DrdThreadId tid);
bart8f822af2009-06-08 18:20:42 +000052static Bool thread_conflict_set_up_to_date(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +000053
54
bart324a23b2009-02-15 12:14:52 +000055/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000056
bart86a87df2009-03-04 19:26:47 +000057static ULong s_context_switch_count;
58static ULong s_discard_ordered_segments_count;
bart54803fe2009-06-21 09:26:27 +000059static ULong s_compute_conflict_set_count;
bart86a87df2009-03-04 19:26:47 +000060static ULong s_update_conflict_set_count;
barte5214662009-06-21 11:51:23 +000061static ULong s_update_conflict_set_new_sg_count;
62static ULong s_update_conflict_set_sync_count;
63static ULong s_update_conflict_set_join_count;
bart86a87df2009-03-04 19:26:47 +000064static ULong s_conflict_set_bitmap_creation_count;
65static ULong s_conflict_set_bitmap2_creation_count;
66static ThreadId s_vg_running_tid = VG_INVALID_THREADID;
bart324a23b2009-02-15 12:14:52 +000067DrdThreadId DRD_(g_drd_running_tid) = DRD_INVALID_THREADID;
florian1e802b62015-02-13 19:08:26 +000068ThreadInfo* DRD_(g_threadinfo);
bart324a23b2009-02-15 12:14:52 +000069struct bitmap* DRD_(g_conflict_set);
bart9cdc0832014-08-09 12:58:17 +000070Bool DRD_(verify_conflict_set);
bart86a87df2009-03-04 19:26:47 +000071static Bool s_trace_context_switches = False;
72static Bool s_trace_conflict_set = False;
bart8f822af2009-06-08 18:20:42 +000073static Bool s_trace_conflict_set_bm = False;
bart86a87df2009-03-04 19:26:47 +000074static Bool s_trace_fork_join = False;
75static Bool s_segment_merging = True;
bart8f822af2009-06-08 18:20:42 +000076static Bool s_new_segments_since_last_merge;
bart6f1d7162009-06-24 18:34:10 +000077static int s_segment_merge_interval = 10;
bart6d956dc2011-07-28 09:54:37 +000078static unsigned s_join_list_vol = 10;
79static unsigned s_deletion_head;
80static unsigned s_deletion_tail;
sewardj8eb8bab2015-07-21 14:44:28 +000081#if defined(VGO_solaris)
82Bool DRD_(ignore_thread_creation) = True;
83#else
84Bool DRD_(ignore_thread_creation) = False;
85#endif /* VGO_solaris */
sewardjaf44c822007-11-25 14:01:38 +000086
87
bart324a23b2009-02-15 12:14:52 +000088/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000089
bart86a87df2009-03-04 19:26:47 +000090/** Enables/disables context switch tracing. */
bart62a784c2009-02-15 13:11:14 +000091void DRD_(thread_trace_context_switches)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000092{
bartbedfd232009-03-26 19:07:15 +000093 tl_assert(t == False || t == True);
94 s_trace_context_switches = t;
bart26f73e12008-02-24 18:37:08 +000095}
96
bart86a87df2009-03-04 19:26:47 +000097/** Enables/disables conflict set tracing. */
bart62a784c2009-02-15 13:11:14 +000098void DRD_(thread_trace_conflict_set)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000099{
bartbedfd232009-03-26 19:07:15 +0000100 tl_assert(t == False || t == True);
101 s_trace_conflict_set = t;
bart26f73e12008-02-24 18:37:08 +0000102}
103
bart8f822af2009-06-08 18:20:42 +0000104/** Enables/disables conflict set bitmap tracing. */
105void DRD_(thread_trace_conflict_set_bm)(const Bool t)
106{
107 tl_assert(t == False || t == True);
108 s_trace_conflict_set_bm = t;
109}
110
bart86a87df2009-03-04 19:26:47 +0000111/** Report whether fork/join tracing is enabled. */
bart09dc13f2009-02-14 15:13:31 +0000112Bool DRD_(thread_get_trace_fork_join)(void)
113{
bartbedfd232009-03-26 19:07:15 +0000114 return s_trace_fork_join;
bart09dc13f2009-02-14 15:13:31 +0000115}
116
bart86a87df2009-03-04 19:26:47 +0000117/** Enables/disables fork/join tracing. */
bart09dc13f2009-02-14 15:13:31 +0000118void DRD_(thread_set_trace_fork_join)(const Bool t)
119{
bartbedfd232009-03-26 19:07:15 +0000120 tl_assert(t == False || t == True);
121 s_trace_fork_join = t;
bart09dc13f2009-02-14 15:13:31 +0000122}
123
bart86a87df2009-03-04 19:26:47 +0000124/** Enables/disables segment merging. */
bart62a784c2009-02-15 13:11:14 +0000125void DRD_(thread_set_segment_merging)(const Bool m)
barta9c37392008-03-22 09:38:48 +0000126{
bartbedfd232009-03-26 19:07:15 +0000127 tl_assert(m == False || m == True);
128 s_segment_merging = m;
barta9c37392008-03-22 09:38:48 +0000129}
130
bart8f822af2009-06-08 18:20:42 +0000131/** Get the segment merging interval. */
132int DRD_(thread_get_segment_merge_interval)(void)
133{
134 return s_segment_merge_interval;
135}
136
137/** Set the segment merging interval. */
138void DRD_(thread_set_segment_merge_interval)(const int i)
139{
140 s_segment_merge_interval = i;
141}
142
bart6d956dc2011-07-28 09:54:37 +0000143void DRD_(thread_set_join_list_vol)(const int jlv)
144{
145 s_join_list_vol = jlv;
146}
147
barte278ab52012-01-24 18:28:55 +0000148void DRD_(thread_init)(void)
149{
florian1e802b62015-02-13 19:08:26 +0000150 DRD_(g_threadinfo) = VG_(malloc)("drd.main.ti.1",
151 DRD_N_THREADS * sizeof DRD_(g_threadinfo)[0]);
152 for (UInt i = 0; i < DRD_N_THREADS; ++i) {
153 static ThreadInfo initval;
154 DRD_(g_threadinfo)[i] = initval;
155 }
barte278ab52012-01-24 18:28:55 +0000156}
157
sewardjaf44c822007-11-25 14:01:38 +0000158/**
bart86a87df2009-03-04 19:26:47 +0000159 * Convert Valgrind's ThreadId into a DrdThreadId.
160 *
161 * @return DRD thread ID upon success and DRD_INVALID_THREADID if the passed
162 * Valgrind ThreadId does not yet exist.
bart324a23b2009-02-15 12:14:52 +0000163 */
bart62a784c2009-02-15 13:11:14 +0000164DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000165{
florian1e802b62015-02-13 19:08:26 +0000166 UInt i;
sewardjaf44c822007-11-25 14:01:38 +0000167
bartbedfd232009-03-26 19:07:15 +0000168 if (tid == VG_INVALID_THREADID)
169 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000170
bartbedfd232009-03-26 19:07:15 +0000171 for (i = 1; i < DRD_N_THREADS; i++)
172 {
173 if (DRD_(g_threadinfo)[i].vg_thread_exists == True
174 && DRD_(g_threadinfo)[i].vg_threadid == tid)
175 {
176 return i;
177 }
178 }
sewardjaf44c822007-11-25 14:01:38 +0000179
bartbedfd232009-03-26 19:07:15 +0000180 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000181}
182
bart86a87df2009-03-04 19:26:47 +0000183/** Allocate a new DRD thread ID for the specified Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000184static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000185{
florian1e802b62015-02-13 19:08:26 +0000186 UInt i;
sewardjaf44c822007-11-25 14:01:38 +0000187
bartbedfd232009-03-26 19:07:15 +0000188 tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000189
bartbedfd232009-03-26 19:07:15 +0000190 for (i = 1; i < DRD_N_THREADS; i++)
191 {
bart6d956dc2011-07-28 09:54:37 +0000192 if (!DRD_(g_threadinfo)[i].valid)
bartbedfd232009-03-26 19:07:15 +0000193 {
194 tl_assert(! DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000195
bart6d956dc2011-07-28 09:54:37 +0000196 DRD_(g_threadinfo)[i].valid = True;
bartbedfd232009-03-26 19:07:15 +0000197 DRD_(g_threadinfo)[i].vg_thread_exists = True;
198 DRD_(g_threadinfo)[i].vg_threadid = tid;
199 DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID;
200 DRD_(g_threadinfo)[i].stack_min = 0;
201 DRD_(g_threadinfo)[i].stack_min_min = 0;
202 DRD_(g_threadinfo)[i].stack_startup = 0;
203 DRD_(g_threadinfo)[i].stack_max = 0;
bartd45d9952009-05-31 18:53:54 +0000204 DRD_(thread_set_name)(i, "");
bart383d6132010-09-02 14:43:18 +0000205 DRD_(g_threadinfo)[i].on_alt_stack = False;
bartd45d9952009-05-31 18:53:54 +0000206 DRD_(g_threadinfo)[i].is_recording_loads = True;
207 DRD_(g_threadinfo)[i].is_recording_stores = True;
bartdd75cdf2009-07-24 08:20:10 +0000208 DRD_(g_threadinfo)[i].pthread_create_nesting_level = 0;
bartbedfd232009-03-26 19:07:15 +0000209 DRD_(g_threadinfo)[i].synchr_nesting = 0;
bart6d956dc2011-07-28 09:54:37 +0000210 DRD_(g_threadinfo)[i].deletion_seq = s_deletion_tail - 1;
sewardj8eb8bab2015-07-21 14:44:28 +0000211 DRD_(g_threadinfo)[i].creator_thread = DRD_INVALID_THREADID;
212#if defined (VGO_solaris)
213 DRD_(g_threadinfo)[i].bind_guard_flag = 0;
214#endif /* VGO_solaris */
215
bart91b7ec32012-01-25 20:36:27 +0000216 tl_assert(DRD_(g_threadinfo)[i].sg_first == NULL);
217 tl_assert(DRD_(g_threadinfo)[i].sg_last == NULL);
bart86a87df2009-03-04 19:26:47 +0000218
bartbedfd232009-03-26 19:07:15 +0000219 tl_assert(DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000220
bartbedfd232009-03-26 19:07:15 +0000221 return i;
222 }
223 }
sewardjaf44c822007-11-25 14:01:38 +0000224
bart3c9afb12009-07-24 11:11:30 +0000225 VG_(printf)(
226"\nSorry, but the maximum number of threads supported by DRD has been exceeded."
227"Aborting.\n");
228
bartbedfd232009-03-26 19:07:15 +0000229 tl_assert(False);
sewardjaf44c822007-11-25 14:01:38 +0000230
bartbedfd232009-03-26 19:07:15 +0000231 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000232}
233
bart86a87df2009-03-04 19:26:47 +0000234/** Convert a POSIX thread ID into a DRD thread ID. */
bart62a784c2009-02-15 13:11:14 +0000235DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000236{
florian1e802b62015-02-13 19:08:26 +0000237 UInt i;
sewardjaf44c822007-11-25 14:01:38 +0000238
bartb48bde22009-07-31 08:26:17 +0000239 if (tid != INVALID_POSIX_THREADID)
bartbedfd232009-03-26 19:07:15 +0000240 {
bartb48bde22009-07-31 08:26:17 +0000241 for (i = 1; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000242 {
bartb48bde22009-07-31 08:26:17 +0000243 if (DRD_(g_threadinfo)[i].posix_thread_exists
244 && DRD_(g_threadinfo)[i].pt_threadid == tid)
245 {
246 return i;
247 }
bartbedfd232009-03-26 19:07:15 +0000248 }
249 }
250 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000251}
252
bart86a87df2009-03-04 19:26:47 +0000253/** Convert a DRD thread ID into a Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000254ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000255{
bartbedfd232009-03-26 19:07:15 +0000256 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
257 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000258
bartbedfd232009-03-26 19:07:15 +0000259 return (DRD_(g_threadinfo)[tid].vg_thread_exists
260 ? DRD_(g_threadinfo)[tid].vg_threadid
261 : VG_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000262}
263
bart8f822af2009-06-08 18:20:42 +0000264#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart324a23b2009-02-15 12:14:52 +0000265/**
266 * Sanity check of the doubly linked list of segments referenced by a
267 * ThreadInfo struct.
268 * @return True if sane, False if not.
sewardjaf44c822007-11-25 14:01:38 +0000269 */
bart62a784c2009-02-15 13:11:14 +0000270static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti)
sewardjaf44c822007-11-25 14:01:38 +0000271{
bartbedfd232009-03-26 19:07:15 +0000272 Segment* p;
bart8f822af2009-06-08 18:20:42 +0000273
bart2bf4bb92014-03-10 18:58:19 +0000274 for (p = ti->sg_first; p; p = p->thr_next) {
275 if (p->thr_next && p->thr_next->thr_prev != p)
bartbedfd232009-03-26 19:07:15 +0000276 return False;
bart2bf4bb92014-03-10 18:58:19 +0000277 if (p->thr_next == 0 && p != ti->sg_last)
bartbedfd232009-03-26 19:07:15 +0000278 return False;
279 }
bart2bf4bb92014-03-10 18:58:19 +0000280 for (p = ti->sg_last; p; p = p->thr_prev) {
281 if (p->thr_prev && p->thr_prev->thr_next != p)
bartbedfd232009-03-26 19:07:15 +0000282 return False;
bart2bf4bb92014-03-10 18:58:19 +0000283 if (p->thr_prev == 0 && p != ti->sg_first)
bartbedfd232009-03-26 19:07:15 +0000284 return False;
285 }
286 return True;
sewardjaf44c822007-11-25 14:01:38 +0000287}
bart23d3a4e2008-04-05 12:53:00 +0000288#endif
sewardjaf44c822007-11-25 14:01:38 +0000289
bart439c55f2009-02-15 10:38:37 +0000290/**
291 * Create the first segment for a newly started thread.
292 *
293 * This function is called from the handler installed via
294 * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler
295 * from the context of the creator thread, before the new thread has been
296 * created.
bart86a87df2009-03-04 19:26:47 +0000297 *
298 * @param[in] creator DRD thread ID of the creator thread.
299 * @param[in] vg_created Valgrind thread ID of the created thread.
300 *
301 * @return DRD thread ID of the created thread.
bart439c55f2009-02-15 10:38:37 +0000302 */
bart62a784c2009-02-15 13:11:14 +0000303DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
304 const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000305{
bartbedfd232009-03-26 19:07:15 +0000306 DrdThreadId created;
sewardjaf44c822007-11-25 14:01:38 +0000307
bartbedfd232009-03-26 19:07:15 +0000308 tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID);
309 created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created);
310 tl_assert(0 <= (int)created && created < DRD_N_THREADS
311 && created != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000312
bart91b7ec32012-01-25 20:36:27 +0000313 tl_assert(DRD_(g_threadinfo)[created].sg_first == NULL);
314 tl_assert(DRD_(g_threadinfo)[created].sg_last == NULL);
sewardj8eb8bab2015-07-21 14:44:28 +0000315
316 if (creator != DRD_INVALID_THREADID) {
317 if (DRD_(ignore_thread_creation)) {
318 tl_assert(DRD_(thread_get_synchr_nesting_count)(created) == 0);
319 DRD_(thread_enter_synchr)(created);
320 /* Counterpart in DRD_(thread_set_pthreadid)(). */
321 }
322 }
323 DRD_(g_threadinfo)[created].creator_thread = creator;
324
bart8f822af2009-06-08 18:20:42 +0000325 /* Create an initial segment for the newly created thread. */
bartbedfd232009-03-26 19:07:15 +0000326 thread_append_segment(created, DRD_(sg_new)(creator, created));
sewardjaf44c822007-11-25 14:01:38 +0000327
bartbedfd232009-03-26 19:07:15 +0000328 return created;
sewardjaf44c822007-11-25 14:01:38 +0000329}
330
bart439c55f2009-02-15 10:38:37 +0000331/**
bart86a87df2009-03-04 19:26:47 +0000332 * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called
333 * after the thread has been created and before any client instructions are run
bart439c55f2009-02-15 10:38:37 +0000334 * on the newly created thread, e.g. from the handler installed via
335 * VG_(track_pre_thread_first_insn)().
bart86a87df2009-03-04 19:26:47 +0000336 *
337 * @param[in] vg_created Valgrind thread ID of the newly created thread.
338 *
339 * @return DRD thread ID for the new thread.
bart439c55f2009-02-15 10:38:37 +0000340 */
bart62a784c2009-02-15 13:11:14 +0000341DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created)
bart439c55f2009-02-15 10:38:37 +0000342{
bartbedfd232009-03-26 19:07:15 +0000343 const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created);
bart439c55f2009-02-15 10:38:37 +0000344
bartbedfd232009-03-26 19:07:15 +0000345 tl_assert(0 <= (int)created && created < DRD_N_THREADS
346 && created != DRD_INVALID_THREADID);
bart439c55f2009-02-15 10:38:37 +0000347
bartbedfd232009-03-26 19:07:15 +0000348 DRD_(g_threadinfo)[created].stack_max
349 = VG_(thread_get_stack_max)(vg_created);
350 DRD_(g_threadinfo)[created].stack_startup
351 = DRD_(g_threadinfo)[created].stack_max;
352 DRD_(g_threadinfo)[created].stack_min
353 = DRD_(g_threadinfo)[created].stack_max;
354 DRD_(g_threadinfo)[created].stack_min_min
355 = DRD_(g_threadinfo)[created].stack_max;
356 DRD_(g_threadinfo)[created].stack_size
357 = VG_(thread_get_stack_size)(vg_created);
358 tl_assert(DRD_(g_threadinfo)[created].stack_max != 0);
bart439c55f2009-02-15 10:38:37 +0000359
bartbedfd232009-03-26 19:07:15 +0000360 return created;
bart439c55f2009-02-15 10:38:37 +0000361}
bart09dc13f2009-02-14 15:13:31 +0000362
bart6d956dc2011-07-28 09:54:37 +0000363static void DRD_(thread_delayed_delete)(const DrdThreadId tid)
364{
florian1e802b62015-02-13 19:08:26 +0000365 UInt j;
bart6d956dc2011-07-28 09:54:37 +0000366
367 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
368 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
369 DRD_(g_threadinfo)[tid].deletion_seq = s_deletion_head++;
370#if 0
371 VG_(message)(Vg_DebugMsg, "Adding thread %d to the deletion list\n", tid);
372#endif
373 if (s_deletion_head - s_deletion_tail >= s_join_list_vol) {
374 for (j = 0; j < DRD_N_THREADS; ++j) {
375 if (DRD_(IsValidDrdThreadId)(j)
376 && DRD_(g_threadinfo)[j].deletion_seq == s_deletion_tail)
377 {
378 s_deletion_tail++;
379#if 0
380 VG_(message)(Vg_DebugMsg, "Delayed delete of thread %d\n", j);
381#endif
382 DRD_(thread_delete)(j, False);
383 break;
384 }
385 }
386 }
387}
388
bart324a23b2009-02-15 12:14:52 +0000389/**
390 * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just
391 * after thread drd_joiner joined thread drd_joinee.
392 */
bart09dc13f2009-02-14 15:13:31 +0000393void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
394{
bartbedfd232009-03-26 19:07:15 +0000395 tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner));
396 tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee));
bart7627be32009-06-06 12:26:05 +0000397
bartbedfd232009-03-26 19:07:15 +0000398 DRD_(thread_new_segment)(drd_joiner);
bart8f822af2009-06-08 18:20:42 +0000399 DRD_(thread_combine_vc_join)(drd_joiner, drd_joinee);
bart7627be32009-06-06 12:26:05 +0000400 DRD_(thread_new_segment)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000401
bartbedfd232009-03-26 19:07:15 +0000402 if (s_trace_fork_join)
403 {
404 const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner);
bartbedfd232009-03-26 19:07:15 +0000405 const unsigned msg_size = 256;
florian19f91bb2012-11-10 22:29:54 +0000406 HChar* msg;
bart09dc13f2009-02-14 15:13:31 +0000407
bartbedfd232009-03-26 19:07:15 +0000408 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
florianf5d8e652014-09-11 22:15:39 +0000409
bartbedfd232009-03-26 19:07:15 +0000410 VG_(snprintf)(msg, msg_size,
florianea71ffb2015-08-05 14:38:57 +0000411 "drd_post_thread_join joiner = %u, joinee = %u",
bart63c92ea2009-07-19 17:53:56 +0000412 drd_joiner, drd_joinee);
bartbedfd232009-03-26 19:07:15 +0000413 if (joiner)
414 {
florian19f91bb2012-11-10 22:29:54 +0000415 HChar* vc;
bart8f822af2009-06-08 18:20:42 +0000416
417 vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(drd_joiner));
bartbedfd232009-03-26 19:07:15 +0000418 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart8f822af2009-06-08 18:20:42 +0000419 ", new vc: %s", vc);
420 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000421 }
bartad994e82011-10-13 18:04:30 +0000422 DRD_(trace_msg)("%pS", msg);
bartbedfd232009-03-26 19:07:15 +0000423 VG_(free)(msg);
424 }
bart09dc13f2009-02-14 15:13:31 +0000425
bartbedfd232009-03-26 19:07:15 +0000426 if (! DRD_(get_check_stack_accesses)())
427 {
428 DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee)
429 - DRD_(thread_get_stack_size)(drd_joinee),
430 DRD_(thread_get_stack_max)(drd_joinee));
431 }
432 DRD_(clientobj_delete_thread)(drd_joinee);
bart6d956dc2011-07-28 09:54:37 +0000433 DRD_(thread_delayed_delete)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000434}
435
bart324a23b2009-02-15 12:14:52 +0000436/**
437 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
438 * and accesses this data structure from multiple threads without locking.
439 * Any conflicting accesses in the range stack_startup..stack_max will be
440 * ignored.
441 */
bart62a784c2009-02-15 13:11:14 +0000442void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
443 const Addr stack_startup)
sewardjaf44c822007-11-25 14:01:38 +0000444{
bartbedfd232009-03-26 19:07:15 +0000445 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
446 && tid != DRD_INVALID_THREADID);
447 tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup);
448 tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max);
449 DRD_(g_threadinfo)[tid].stack_startup = stack_startup;
sewardjaf44c822007-11-25 14:01:38 +0000450}
451
bart86a87df2009-03-04 19:26:47 +0000452/** Return the stack pointer for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000453Addr DRD_(thread_get_stack_min)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000454{
bartbedfd232009-03-26 19:07:15 +0000455 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
456 && tid != DRD_INVALID_THREADID);
457 return DRD_(g_threadinfo)[tid].stack_min;
sewardjaf44c822007-11-25 14:01:38 +0000458}
459
bart86a87df2009-03-04 19:26:47 +0000460/**
461 * Return the lowest value that was ever assigned to the stack pointer
462 * for the specified thread.
463 */
bart62a784c2009-02-15 13:11:14 +0000464Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000465{
bartbedfd232009-03-26 19:07:15 +0000466 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
467 && tid != DRD_INVALID_THREADID);
468 return DRD_(g_threadinfo)[tid].stack_min_min;
bartcac53462008-03-29 09:27:08 +0000469}
470
bart86a87df2009-03-04 19:26:47 +0000471/** Return the top address for the stack of the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000472Addr DRD_(thread_get_stack_max)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000473{
bartbedfd232009-03-26 19:07:15 +0000474 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
475 && tid != DRD_INVALID_THREADID);
476 return DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000477}
478
bart86a87df2009-03-04 19:26:47 +0000479/** Return the maximum stack size for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000480SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000481{
bartbedfd232009-03-26 19:07:15 +0000482 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
483 && tid != DRD_INVALID_THREADID);
484 return DRD_(g_threadinfo)[tid].stack_size;
bartcac53462008-03-29 09:27:08 +0000485}
486
bart383d6132010-09-02 14:43:18 +0000487Bool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid)
488{
489 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
490 && tid != DRD_INVALID_THREADID);
491 return DRD_(g_threadinfo)[tid].on_alt_stack;
492}
493
494void DRD_(thread_set_on_alt_stack)(const DrdThreadId tid,
495 const Bool on_alt_stack)
496{
497 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
498 && tid != DRD_INVALID_THREADID);
499 tl_assert(on_alt_stack == !!on_alt_stack);
500 DRD_(g_threadinfo)[tid].on_alt_stack = on_alt_stack;
501}
502
503Int DRD_(thread_get_threads_on_alt_stack)(void)
504{
florian1e802b62015-02-13 19:08:26 +0000505 int n = 0;
bart383d6132010-09-02 14:43:18 +0000506
florian1e802b62015-02-13 19:08:26 +0000507 for (UInt i = 1; i < DRD_N_THREADS; i++)
bart383d6132010-09-02 14:43:18 +0000508 n += DRD_(g_threadinfo)[i].on_alt_stack;
509 return n;
510}
511
bart09dc13f2009-02-14 15:13:31 +0000512/**
bart6d956dc2011-07-28 09:54:37 +0000513 * Clean up thread-specific data structures.
sewardjaf44c822007-11-25 14:01:38 +0000514 */
bart9194e932011-02-09 11:55:12 +0000515void DRD_(thread_delete)(const DrdThreadId tid, const Bool detached)
sewardjaf44c822007-11-25 14:01:38 +0000516{
bartbedfd232009-03-26 19:07:15 +0000517 Segment* sg;
518 Segment* sg_prev;
sewardjaf44c822007-11-25 14:01:38 +0000519
bartbedfd232009-03-26 19:07:15 +0000520 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart86a87df2009-03-04 19:26:47 +0000521
bartbedfd232009-03-26 19:07:15 +0000522 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0);
bart91b7ec32012-01-25 20:36:27 +0000523 for (sg = DRD_(g_threadinfo)[tid].sg_last; sg; sg = sg_prev) {
524 sg_prev = sg->thr_prev;
525 sg->thr_next = NULL;
526 sg->thr_prev = NULL;
bartbedfd232009-03-26 19:07:15 +0000527 DRD_(sg_put)(sg);
528 }
bart6d956dc2011-07-28 09:54:37 +0000529 DRD_(g_threadinfo)[tid].valid = False;
bartbedfd232009-03-26 19:07:15 +0000530 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
531 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
bart9194e932011-02-09 11:55:12 +0000532 if (detached)
533 DRD_(g_threadinfo)[tid].detached_posix_thread = False;
534 else
535 tl_assert(!DRD_(g_threadinfo)[tid].detached_posix_thread);
bart91b7ec32012-01-25 20:36:27 +0000536 DRD_(g_threadinfo)[tid].sg_first = NULL;
537 DRD_(g_threadinfo)[tid].sg_last = NULL;
bart86a87df2009-03-04 19:26:47 +0000538
bart91b7ec32012-01-25 20:36:27 +0000539 tl_assert(!DRD_(IsValidDrdThreadId)(tid));
sewardjaf44c822007-11-25 14:01:38 +0000540}
541
bart324a23b2009-02-15 12:14:52 +0000542/**
543 * Called after a thread performed its last memory access and before
544 * thread_delete() is called. Note: thread_delete() is only called for
545 * joinable threads, not for detached threads.
546 */
bart62a784c2009-02-15 13:11:14 +0000547void DRD_(thread_finished)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000548{
bartbedfd232009-03-26 19:07:15 +0000549 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
550 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000551
bartbedfd232009-03-26 19:07:15 +0000552 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
sewardjaf44c822007-11-25 14:01:38 +0000553
bartbedfd232009-03-26 19:07:15 +0000554 if (DRD_(g_threadinfo)[tid].detached_posix_thread)
555 {
556 /*
557 * Once a detached thread has finished, its stack is deallocated and
558 * should no longer be taken into account when computing the conflict set.
559 */
560 DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000561
bartbedfd232009-03-26 19:07:15 +0000562 /*
563 * For a detached thread, calling pthread_exit() invalidates the
564 * POSIX thread ID associated with the detached thread. For joinable
565 * POSIX threads however, the POSIX thread ID remains live after the
566 * pthread_exit() call until pthread_join() is called.
567 */
568 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
569 }
sewardjaf44c822007-11-25 14:01:38 +0000570}
571
bart5c7e6b62011-02-03 17:47:50 +0000572/** Called just after fork() in the child process. */
573void DRD_(drd_thread_atfork_child)(const DrdThreadId tid)
574{
575 unsigned i;
576
577 for (i = 1; i < DRD_N_THREADS; i++)
578 {
579 if (i == tid)
580 continue;
581 if (DRD_(IsValidDrdThreadId(i)))
bart9194e932011-02-09 11:55:12 +0000582 DRD_(thread_delete)(i, True);
bart5c7e6b62011-02-03 17:47:50 +0000583 tl_assert(!DRD_(IsValidDrdThreadId(i)));
bartf7a5b3f2011-12-11 20:34:03 +0000584 }
bart7692f162014-08-08 16:27:30 +0000585
586 DRD_(bm_cleanup)(DRD_(g_conflict_set));
587 DRD_(bm_init)(DRD_(g_conflict_set));
bart5c7e6b62011-02-03 17:47:50 +0000588}
589
bart9b2974a2008-09-27 12:35:31 +0000590/** Called just before pthread_cancel(). */
bart62a784c2009-02-15 13:11:14 +0000591void DRD_(thread_pre_cancel)(const DrdThreadId tid)
bartaf0691b2008-09-27 12:26:50 +0000592{
bartbedfd232009-03-26 19:07:15 +0000593 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
594 && tid != DRD_INVALID_THREADID);
595 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bartaf0691b2008-09-27 12:26:50 +0000596
bart0c0cd772011-03-03 19:59:20 +0000597 if (DRD_(thread_get_trace_fork_join)())
florianea71ffb2015-08-05 14:38:57 +0000598 DRD_(trace_msg)("[%u] drd_thread_pre_cancel %u",
bartb92ff0f2011-10-08 08:29:29 +0000599 DRD_(g_drd_running_tid), tid);
bartaf0691b2008-09-27 12:26:50 +0000600}
601
barte7dff242009-04-23 17:12:39 +0000602/**
603 * Store the POSIX thread ID for the specified thread.
604 *
Elliott Hughesed398002017-06-21 14:41:24 -0700605 * @note This function can be called multiple times for the same thread -- see
606 * also the comment block preceding the pthread_create() wrapper in
barte7dff242009-04-23 17:12:39 +0000607 * drd_pthread_intercepts.c.
608 */
bart62a784c2009-02-15 13:11:14 +0000609void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
sewardjaf44c822007-11-25 14:01:38 +0000610{
bartbedfd232009-03-26 19:07:15 +0000611 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
612 && tid != DRD_INVALID_THREADID);
barte7dff242009-04-23 17:12:39 +0000613 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID
614 || DRD_(g_threadinfo)[tid].pt_threadid == ptid);
bartbedfd232009-03-26 19:07:15 +0000615 tl_assert(ptid != INVALID_POSIX_THREADID);
Elliott Hughesed398002017-06-21 14:41:24 -0700616 if (DRD_(g_threadinfo)[tid].posix_thread_exists) {
617 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == ptid);
618 return;
619 }
bartbedfd232009-03-26 19:07:15 +0000620 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
621 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardj8eb8bab2015-07-21 14:44:28 +0000622
623 if (DRD_(g_threadinfo)[tid].creator_thread != DRD_INVALID_THREADID) {
624 if (DRD_(ignore_thread_creation)) {
625 DRD_(thread_leave_synchr)(tid);
626 tl_assert(DRD_(thread_get_synchr_nesting_count)(tid) == 0);
627 }
628 }
sewardjaf44c822007-11-25 14:01:38 +0000629}
630
bart86a87df2009-03-04 19:26:47 +0000631/** Returns true for joinable threads and false for detached threads. */
bart62a784c2009-02-15 13:11:14 +0000632Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000633{
bartbedfd232009-03-26 19:07:15 +0000634 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
635 && tid != DRD_INVALID_THREADID);
636 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000637}
638
bart86a87df2009-03-04 19:26:47 +0000639/** Store the thread mode: joinable or detached. */
petarj4df0bfc2013-02-27 23:17:33 +0000640#if defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
sewardj5db15402012-06-07 09:13:21 +0000641 /* There is a cse related issue in gcc for MIPS. Optimization level
642 has to be lowered, so cse related optimizations are not
643 included.*/
644 __attribute__((optimize("O1")))
645#endif
bart62a784c2009-02-15 13:11:14 +0000646void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000647{
bartbedfd232009-03-26 19:07:15 +0000648 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
649 && tid != DRD_INVALID_THREADID);
sewardjd56b7e22015-01-20 00:12:18 +0000650 tl_assert((!! joinable) == joinable);
bartbedfd232009-03-26 19:07:15 +0000651 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bart8f822af2009-06-08 18:20:42 +0000652
bartbedfd232009-03-26 19:07:15 +0000653 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000654}
655
bartdd75cdf2009-07-24 08:20:10 +0000656/** Tells DRD that the calling thread is about to enter pthread_create(). */
657void DRD_(thread_entering_pthread_create)(const DrdThreadId tid)
658{
659 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
660 && tid != DRD_INVALID_THREADID);
661 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
662 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level >= 0);
663
664 DRD_(g_threadinfo)[tid].pthread_create_nesting_level++;
sewardj8eb8bab2015-07-21 14:44:28 +0000665
666 if (DRD_(ignore_thread_creation)) {
667 tl_assert(DRD_(thread_get_synchr_nesting_count)(tid) == 0);
668 DRD_(thread_enter_synchr)(tid);
669 }
bartdd75cdf2009-07-24 08:20:10 +0000670}
671
672/** Tells DRD that the calling thread has left pthread_create(). */
673void DRD_(thread_left_pthread_create)(const DrdThreadId tid)
674{
675 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
676 && tid != DRD_INVALID_THREADID);
677 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
678 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level > 0);
679
680 DRD_(g_threadinfo)[tid].pthread_create_nesting_level--;
sewardj8eb8bab2015-07-21 14:44:28 +0000681
682 if (DRD_(ignore_thread_creation)) {
683 DRD_(thread_leave_synchr)(tid);
684 tl_assert(DRD_(thread_get_synchr_nesting_count)(tid) == 0);
685 }
bartdd75cdf2009-07-24 08:20:10 +0000686}
687
sewardj8eb8bab2015-07-21 14:44:28 +0000688#if defined(VGO_solaris)
689/** Handles the bind_guard() intercept. */
690void DRD_(thread_entering_rtld_bind_guard)(const DrdThreadId tid, int flags)
691{
692 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
693 && tid != DRD_INVALID_THREADID);
694
695 Int bindflag = (flags & VKI_THR_FLG_RTLD);
696 if ((bindflag & DRD_(g_threadinfo)[tid].bind_guard_flag) == 0) {
697 DRD_(g_threadinfo)[tid].bind_guard_flag |= bindflag;
698 DRD_(thread_enter_synchr)(tid);
699 }
700}
701
702/**
703 * Handles the bind_clear() intercept.
704 * Call to bind_clear(0) is typically used to determine value of bind_flags.
705 */
706void DRD_(thread_leaving_rtld_bind_clear)(const DrdThreadId tid, int flags)
707{
708 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
709 && tid != DRD_INVALID_THREADID);
710
711 Int bindflag = (flags & VKI_THR_FLG_RTLD);
712 if ((DRD_(g_threadinfo)[tid].bind_guard_flag & bindflag) != 0) {
713 DRD_(g_threadinfo)[tid].bind_guard_flag &= ~bindflag;
714 DRD_(thread_leave_synchr)(tid);
715 }
716}
717#endif /* VGO_solaris */
718
bartd45d9952009-05-31 18:53:54 +0000719/** Obtain the thread number and the user-assigned thread name. */
florian19f91bb2012-11-10 22:29:54 +0000720const HChar* DRD_(thread_get_name)(const DrdThreadId tid)
bartd45d9952009-05-31 18:53:54 +0000721{
722 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
723 && tid != DRD_INVALID_THREADID);
724
725 return DRD_(g_threadinfo)[tid].name;
726}
727
728/** Set the name of the specified thread. */
florian19f91bb2012-11-10 22:29:54 +0000729void DRD_(thread_set_name)(const DrdThreadId tid, const HChar* const name)
bartd45d9952009-05-31 18:53:54 +0000730{
731 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
732 && tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000733
bartd45d9952009-05-31 18:53:54 +0000734 if (name == NULL || name[0] == 0)
735 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
736 sizeof(DRD_(g_threadinfo)[tid].name),
florianea71ffb2015-08-05 14:38:57 +0000737 "Thread %u",
bartd45d9952009-05-31 18:53:54 +0000738 tid);
739 else
740 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
741 sizeof(DRD_(g_threadinfo)[tid].name),
florianea71ffb2015-08-05 14:38:57 +0000742 "Thread %u (%s)",
bartd45d9952009-05-31 18:53:54 +0000743 tid, name);
744 DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0;
745}
746
bart86a87df2009-03-04 19:26:47 +0000747/**
748 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
749 * conflict set.
750 */
bart62a784c2009-02-15 13:11:14 +0000751void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000752{
bartbedfd232009-03-26 19:07:15 +0000753 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000754
bartbedfd232009-03-26 19:07:15 +0000755 if (vg_tid != s_vg_running_tid)
756 {
757 DRD_(thread_set_running_tid)(vg_tid,
758 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
759 }
sewardj8b09d4f2007-12-04 21:27:18 +0000760
bartbedfd232009-03-26 19:07:15 +0000761 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
762 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000763}
764
bart86a87df2009-03-04 19:26:47 +0000765/**
766 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
767 * conflict set.
768 */
bart62a784c2009-02-15 13:11:14 +0000769void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
770 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000771{
bartbedfd232009-03-26 19:07:15 +0000772 tl_assert(vg_tid != VG_INVALID_THREADID);
773 tl_assert(drd_tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000774
bartbedfd232009-03-26 19:07:15 +0000775 if (vg_tid != s_vg_running_tid)
776 {
777 if (s_trace_context_switches
778 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
779 {
780 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +0000781 "Context switch from thread %u to thread %u;"
sewardj1e29ebc2009-07-15 14:49:17 +0000782 " segments: %llu\n",
bart63c92ea2009-07-19 17:53:56 +0000783 DRD_(g_drd_running_tid), drd_tid,
bartbedfd232009-03-26 19:07:15 +0000784 DRD_(sg_get_segments_alive_count)());
785 }
786 s_vg_running_tid = vg_tid;
787 DRD_(g_drd_running_tid) = drd_tid;
788 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
789 s_context_switch_count++;
790 }
sewardj8b09d4f2007-12-04 21:27:18 +0000791
bartbedfd232009-03-26 19:07:15 +0000792 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
793 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000794}
795
bart86a87df2009-03-04 19:26:47 +0000796/**
797 * Increase the synchronization nesting counter. Must be called before the
798 * client calls a synchronization function.
799 */
bart62a784c2009-02-15 13:11:14 +0000800int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000801{
bartbedfd232009-03-26 19:07:15 +0000802 tl_assert(DRD_(IsValidDrdThreadId)(tid));
803 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000804}
805
bart86a87df2009-03-04 19:26:47 +0000806/**
807 * Decrease the synchronization nesting counter. Must be called after the
808 * client left a synchronization function.
809 */
bart62a784c2009-02-15 13:11:14 +0000810int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000811{
bartbedfd232009-03-26 19:07:15 +0000812 tl_assert(DRD_(IsValidDrdThreadId)(tid));
813 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
814 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000815}
816
bart86a87df2009-03-04 19:26:47 +0000817/** Returns the synchronization nesting counter. */
bart62a784c2009-02-15 13:11:14 +0000818int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000819{
bartbedfd232009-03-26 19:07:15 +0000820 tl_assert(DRD_(IsValidDrdThreadId)(tid));
821 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000822}
823
bart1a473c72008-03-13 19:03:38 +0000824/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000825static
bart86a87df2009-03-04 19:26:47 +0000826void thread_append_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000827{
bartbedfd232009-03-26 19:07:15 +0000828 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
829 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000830
831#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
832 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
833#endif
834
bart91b7ec32012-01-25 20:36:27 +0000835 // add at tail
836 sg->thr_prev = DRD_(g_threadinfo)[tid].sg_last;
837 sg->thr_next = NULL;
838 if (DRD_(g_threadinfo)[tid].sg_last)
839 DRD_(g_threadinfo)[tid].sg_last->thr_next = sg;
840 DRD_(g_threadinfo)[tid].sg_last = sg;
841 if (DRD_(g_threadinfo)[tid].sg_first == NULL)
842 DRD_(g_threadinfo)[tid].sg_first = sg;
bart8f822af2009-06-08 18:20:42 +0000843
844#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
845 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
846#endif
sewardjaf44c822007-11-25 14:01:38 +0000847}
848
bart324a23b2009-02-15 12:14:52 +0000849/**
850 * Remove a segment from the segment list of thread threadid, and free the
851 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000852 */
bart62a784c2009-02-15 13:11:14 +0000853static
bart86a87df2009-03-04 19:26:47 +0000854void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000855{
bartbedfd232009-03-26 19:07:15 +0000856 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
857 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000858
859#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
860 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
861#endif
bart26f73e12008-02-24 18:37:08 +0000862
bart91b7ec32012-01-25 20:36:27 +0000863 if (sg->thr_prev)
864 sg->thr_prev->thr_next = sg->thr_next;
865 if (sg->thr_next)
866 sg->thr_next->thr_prev = sg->thr_prev;
867 if (sg == DRD_(g_threadinfo)[tid].sg_first)
868 DRD_(g_threadinfo)[tid].sg_first = sg->thr_next;
869 if (sg == DRD_(g_threadinfo)[tid].sg_last)
870 DRD_(g_threadinfo)[tid].sg_last = sg->thr_prev;
bartbedfd232009-03-26 19:07:15 +0000871 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000872
bart8f822af2009-06-08 18:20:42 +0000873#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
874 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
875#endif
sewardjaf44c822007-11-25 14:01:38 +0000876}
877
bart86a87df2009-03-04 19:26:47 +0000878/**
879 * Returns a pointer to the vector clock of the most recent segment associated
880 * with thread 'tid'.
881 */
bart62a784c2009-02-15 13:11:14 +0000882VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000883{
bart91b7ec32012-01-25 20:36:27 +0000884 Segment* latest_sg;
barte278ab52012-01-24 18:28:55 +0000885
bartbedfd232009-03-26 19:07:15 +0000886 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
887 && tid != DRD_INVALID_THREADID);
bart91b7ec32012-01-25 20:36:27 +0000888 latest_sg = DRD_(g_threadinfo)[tid].sg_last;
889 tl_assert(latest_sg);
890 return &latest_sg->vc;
sewardjaf44c822007-11-25 14:01:38 +0000891}
892
bart324a23b2009-02-15 12:14:52 +0000893/**
894 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000895 */
bart62a784c2009-02-15 13:11:14 +0000896void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000897{
bart91b7ec32012-01-25 20:36:27 +0000898 Segment* latest_sg;
barte278ab52012-01-24 18:28:55 +0000899
bartbedfd232009-03-26 19:07:15 +0000900 tl_assert(sg);
901 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
902 && tid != DRD_INVALID_THREADID);
bart91b7ec32012-01-25 20:36:27 +0000903 latest_sg = DRD_(g_threadinfo)[tid].sg_last;
904 tl_assert(latest_sg);
barta2b6e1b2008-03-17 18:32:39 +0000905
bartbedfd232009-03-26 19:07:15 +0000906 DRD_(sg_put)(*sg);
bart91b7ec32012-01-25 20:36:27 +0000907 *sg = DRD_(sg_get)(latest_sg);
barta2b6e1b2008-03-17 18:32:39 +0000908}
909
sewardjaf44c822007-11-25 14:01:38 +0000910/**
911 * Compute the minimum of all latest vector clocks of all threads
912 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
bart86a87df2009-03-04 19:26:47 +0000913 *
sewardjaf44c822007-11-25 14:01:38 +0000914 * @param vc pointer to a vectorclock, holds result upon return.
915 */
bart62a784c2009-02-15 13:11:14 +0000916static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000917{
bartbedfd232009-03-26 19:07:15 +0000918 unsigned i;
919 Bool first;
920 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000921
bartbedfd232009-03-26 19:07:15 +0000922 first = True;
bart8f822af2009-06-08 18:20:42 +0000923 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000924 {
bart91b7ec32012-01-25 20:36:27 +0000925 latest_sg = DRD_(g_threadinfo)[i].sg_last;
926 if (latest_sg) {
bartbedfd232009-03-26 19:07:15 +0000927 if (first)
928 DRD_(vc_assign)(vc, &latest_sg->vc);
929 else
930 DRD_(vc_min)(vc, &latest_sg->vc);
931 first = False;
932 }
933 }
sewardjaf44c822007-11-25 14:01:38 +0000934}
935
bart86a87df2009-03-04 19:26:47 +0000936/**
937 * Compute the maximum of all latest vector clocks of all threads.
938 *
939 * @param vc pointer to a vectorclock, holds result upon return.
940 */
bart62a784c2009-02-15 13:11:14 +0000941static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000942{
bartbedfd232009-03-26 19:07:15 +0000943 unsigned i;
944 Bool first;
945 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000946
bartbedfd232009-03-26 19:07:15 +0000947 first = True;
bart8f822af2009-06-08 18:20:42 +0000948 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000949 {
bart91b7ec32012-01-25 20:36:27 +0000950 latest_sg = DRD_(g_threadinfo)[i].sg_last;
951 if (latest_sg) {
bartbedfd232009-03-26 19:07:15 +0000952 if (first)
953 DRD_(vc_assign)(vc, &latest_sg->vc);
954 else
955 DRD_(vc_combine)(vc, &latest_sg->vc);
956 first = False;
957 }
958 }
sewardjaf44c822007-11-25 14:01:38 +0000959}
960
961/**
bart5bd9f2d2008-03-03 20:31:58 +0000962 * Discard all segments that have a defined order against the latest vector
bart86a87df2009-03-04 19:26:47 +0000963 * clock of all threads -- these segments can no longer be involved in a
sewardjaf44c822007-11-25 14:01:38 +0000964 * data race.
965 */
bart8f822af2009-06-08 18:20:42 +0000966static void thread_discard_ordered_segments(void)
sewardjaf44c822007-11-25 14:01:38 +0000967{
bartbedfd232009-03-26 19:07:15 +0000968 unsigned i;
969 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000970
bartbedfd232009-03-26 19:07:15 +0000971 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000972
bartbedfd232009-03-26 19:07:15 +0000973 DRD_(vc_init)(&thread_vc_min, 0, 0);
974 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
975 if (DRD_(sg_get_trace)())
976 {
florian19f91bb2012-11-10 22:29:54 +0000977 HChar *vc_min, *vc_max;
bartbedfd232009-03-26 19:07:15 +0000978 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000979
bartbedfd232009-03-26 19:07:15 +0000980 DRD_(vc_init)(&thread_vc_max, 0, 0);
981 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
bart8f822af2009-06-08 18:20:42 +0000982 vc_min = DRD_(vc_aprint)(&thread_vc_min);
983 vc_max = DRD_(vc_aprint)(&thread_vc_max);
984 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000985 "Discarding ordered segments -- min vc is %s, max vc is %s\n",
bart8f822af2009-06-08 18:20:42 +0000986 vc_min, vc_max);
987 VG_(free)(vc_min);
988 VG_(free)(vc_max);
bartbedfd232009-03-26 19:07:15 +0000989 DRD_(vc_cleanup)(&thread_vc_max);
990 }
sewardjaf44c822007-11-25 14:01:38 +0000991
bart91b7ec32012-01-25 20:36:27 +0000992 for (i = 0; i < DRD_N_THREADS; i++) {
bartbedfd232009-03-26 19:07:15 +0000993 Segment* sg;
994 Segment* sg_next;
barte278ab52012-01-24 18:28:55 +0000995
bart91b7ec32012-01-25 20:36:27 +0000996 for (sg = DRD_(g_threadinfo)[i].sg_first;
997 sg && (sg_next = sg->thr_next)
998 && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
999 sg = sg_next)
1000 {
bartbedfd232009-03-26 19:07:15 +00001001 thread_discard_segment(i, sg);
1002 }
1003 }
1004 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +00001005}
1006
bart324a23b2009-02-15 12:14:52 +00001007/**
bart8f822af2009-06-08 18:20:42 +00001008 * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper
1009 * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if
1010 * all segments in the set CS are ordered consistently against both sg1 and
1011 * sg2. The set CS is defined as the set of segments that can immediately
1012 * precede future segments via inter-thread synchronization operations. In
1013 * DRD the set CS consists of the latest segment of each thread combined with
1014 * all segments for which the reference count is strictly greater than one.
1015 * The code below is an optimized version of the following:
1016 *
1017 * for (i = 0; i < DRD_N_THREADS; i++)
1018 * {
1019 * Segment* sg;
1020 *
1021 * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
1022 * {
1023 * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1)
1024 * {
1025 * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc)
1026 * != DRD_(vc_lte)(&sg2->vc, &sg->vc)
1027 * || DRD_(vc_lte)(&sg->vc, &sg1->vc)
1028 * != DRD_(vc_lte)(&sg->vc, &sg2->vc))
1029 * {
1030 * return False;
1031 * }
1032 * }
1033 * }
1034 * }
1035 */
1036static Bool thread_consistent_segment_ordering(const DrdThreadId tid,
1037 Segment* const sg1,
1038 Segment* const sg2)
1039{
1040 unsigned i;
1041
bart91b7ec32012-01-25 20:36:27 +00001042 tl_assert(sg1->thr_next);
1043 tl_assert(sg2->thr_next);
1044 tl_assert(sg1->thr_next == sg2);
bart8f822af2009-06-08 18:20:42 +00001045 tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc));
1046
1047 for (i = 0; i < DRD_N_THREADS; i++)
1048 {
1049 Segment* sg;
1050
bart91b7ec32012-01-25 20:36:27 +00001051 for (sg = DRD_(g_threadinfo)[i].sg_first; sg; sg = sg->thr_next) {
1052 if (!sg->thr_next || DRD_(sg_get_refcnt)(sg) > 1) {
bart8f822af2009-06-08 18:20:42 +00001053 if (DRD_(vc_lte)(&sg2->vc, &sg->vc))
1054 break;
1055 if (DRD_(vc_lte)(&sg1->vc, &sg->vc))
1056 return False;
1057 }
1058 }
bart91b7ec32012-01-25 20:36:27 +00001059 for (sg = DRD_(g_threadinfo)[i].sg_last; sg; sg = sg->thr_prev) {
1060 if (!sg->thr_next || DRD_(sg_get_refcnt)(sg) > 1) {
bart8f822af2009-06-08 18:20:42 +00001061 if (DRD_(vc_lte)(&sg->vc, &sg1->vc))
1062 break;
1063 if (DRD_(vc_lte)(&sg->vc, &sg2->vc))
1064 return False;
1065 }
1066 }
1067 }
1068 return True;
1069}
1070
1071/**
bart324a23b2009-02-15 12:14:52 +00001072 * Merge all segments that may be merged without triggering false positives
1073 * or discarding real data races. For the theoretical background of segment
bart8f822af2009-06-08 18:20:42 +00001074 * merging, see also the following paper: Mark Christiaens, Michiel Ronsse
1075 * and Koen De Bosschere. Bounding the number of segment histories during
1076 * data race detection. Parallel Computing archive, Volume 28, Issue 9,
1077 * pp 1221-1238, September 2002. This paper contains a proof that merging
1078 * consecutive segments for which the property equiv(s1,s2) holds can be
1079 * merged without reducing the accuracy of datarace detection. Furthermore
1080 * it is also proven that the total number of all segments will never grow
1081 * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged
1082 * every time a new segment is created. The property equiv(s1, s2) is defined
1083 * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector
1084 * clocks of segments s and s1 are ordered in the same way as those of segments
1085 * s and s2. The set CS is defined as the set of existing segments s that have
1086 * the potential to conflict with not yet created segments, either because the
1087 * segment s is the latest segment of a thread or because it can become the
1088 * immediate predecessor of a new segment due to a synchronization operation.
barta9c37392008-03-22 09:38:48 +00001089 */
1090static void thread_merge_segments(void)
1091{
bartbedfd232009-03-26 19:07:15 +00001092 unsigned i;
barta9c37392008-03-22 09:38:48 +00001093
bart8f822af2009-06-08 18:20:42 +00001094 s_new_segments_since_last_merge = 0;
1095
1096 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001097 {
1098 Segment* sg;
barta9c37392008-03-22 09:38:48 +00001099
bart8f822af2009-06-08 18:20:42 +00001100#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1101 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1102#endif
barta9c37392008-03-22 09:38:48 +00001103
bart91b7ec32012-01-25 20:36:27 +00001104 for (sg = DRD_(g_threadinfo)[i].sg_first; sg; sg = sg->thr_next) {
1105 if (DRD_(sg_get_refcnt)(sg) == 1 && sg->thr_next) {
1106 Segment* const sg_next = sg->thr_next;
barte278ab52012-01-24 18:28:55 +00001107 if (DRD_(sg_get_refcnt)(sg_next) == 1
bart91b7ec32012-01-25 20:36:27 +00001108 && sg_next->thr_next
barte278ab52012-01-24 18:28:55 +00001109 && thread_consistent_segment_ordering(i, sg, sg_next))
1110 {
1111 /* Merge sg and sg_next into sg. */
1112 DRD_(sg_merge)(sg, sg_next);
1113 thread_discard_segment(i, sg_next);
1114 }
bartbedfd232009-03-26 19:07:15 +00001115 }
barta9c37392008-03-22 09:38:48 +00001116 }
barta9c37392008-03-22 09:38:48 +00001117
bart8f822af2009-06-08 18:20:42 +00001118#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1119 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1120#endif
bartbedfd232009-03-26 19:07:15 +00001121 }
barta9c37392008-03-22 09:38:48 +00001122}
1123
bart324a23b2009-02-15 12:14:52 +00001124/**
bart324a23b2009-02-15 12:14:52 +00001125 * Create a new segment for the specified thread, and discard any segments
1126 * that cannot cause races anymore.
sewardjaf44c822007-11-25 14:01:38 +00001127 */
bart62a784c2009-02-15 13:11:14 +00001128void DRD_(thread_new_segment)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001129{
bart8f822af2009-06-08 18:20:42 +00001130 Segment* last_sg;
bartbedfd232009-03-26 19:07:15 +00001131 Segment* new_sg;
bartd66e3a82008-04-06 15:02:17 +00001132
bartbedfd232009-03-26 19:07:15 +00001133 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1134 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +00001135 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001136
bart91b7ec32012-01-25 20:36:27 +00001137 last_sg = DRD_(g_threadinfo)[tid].sg_last;
bartbedfd232009-03-26 19:07:15 +00001138 new_sg = DRD_(sg_new)(tid, tid);
1139 thread_append_segment(tid, new_sg);
bart8f822af2009-06-08 18:20:42 +00001140 if (tid == DRD_(g_drd_running_tid) && last_sg)
barte5214662009-06-21 11:51:23 +00001141 {
bart8f822af2009-06-08 18:20:42 +00001142 DRD_(thread_update_conflict_set)(tid, &last_sg->vc);
barte5214662009-06-21 11:51:23 +00001143 s_update_conflict_set_new_sg_count++;
1144 }
bartd66e3a82008-04-06 15:02:17 +00001145
bart8f822af2009-06-08 18:20:42 +00001146 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001147
bart8f822af2009-06-08 18:20:42 +00001148 if (s_segment_merging
1149 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
bartbedfd232009-03-26 19:07:15 +00001150 {
bart8f822af2009-06-08 18:20:42 +00001151 thread_discard_ordered_segments();
bartbedfd232009-03-26 19:07:15 +00001152 thread_merge_segments();
1153 }
sewardjaf44c822007-11-25 14:01:38 +00001154}
1155
bart26f73e12008-02-24 18:37:08 +00001156/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart8f822af2009-06-08 18:20:42 +00001157void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +00001158{
bartbedfd232009-03-26 19:07:15 +00001159 tl_assert(joiner != joinee);
1160 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
1161 && joiner != DRD_INVALID_THREADID);
1162 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
1163 && joinee != DRD_INVALID_THREADID);
bart91b7ec32012-01-25 20:36:27 +00001164 tl_assert(DRD_(g_threadinfo)[joiner].sg_first);
1165 tl_assert(DRD_(g_threadinfo)[joiner].sg_last);
1166 tl_assert(DRD_(g_threadinfo)[joinee].sg_first);
1167 tl_assert(DRD_(g_threadinfo)[joinee].sg_last);
bart8f822af2009-06-08 18:20:42 +00001168
1169 if (DRD_(sg_get_trace)())
1170 {
florian19f91bb2012-11-10 22:29:54 +00001171 HChar *str1, *str2;
bartc6bf1842012-01-22 08:40:42 +00001172 str1 = DRD_(vc_aprint)(DRD_(thread_get_vc)(joiner));
1173 str2 = DRD_(vc_aprint)(DRD_(thread_get_vc)(joinee));
sewardj1e29ebc2009-07-15 14:49:17 +00001174 VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n",
bart8f822af2009-06-08 18:20:42 +00001175 str1, str2);
1176 VG_(free)(str1);
1177 VG_(free)(str2);
1178 }
bartae37e6d2012-01-22 08:58:31 +00001179 if (joiner == DRD_(g_drd_running_tid)) {
barte5214662009-06-21 11:51:23 +00001180 VectorClock old_vc;
1181
bartc6bf1842012-01-22 08:40:42 +00001182 DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(joiner));
1183 DRD_(vc_combine)(DRD_(thread_get_vc)(joiner),
1184 DRD_(thread_get_vc)(joinee));
barte5214662009-06-21 11:51:23 +00001185 DRD_(thread_update_conflict_set)(joiner, &old_vc);
1186 s_update_conflict_set_join_count++;
1187 DRD_(vc_cleanup)(&old_vc);
bartae37e6d2012-01-22 08:58:31 +00001188 } else {
bartc6bf1842012-01-22 08:40:42 +00001189 DRD_(vc_combine)(DRD_(thread_get_vc)(joiner),
1190 DRD_(thread_get_vc)(joinee));
barte5214662009-06-21 11:51:23 +00001191 }
1192
1193 thread_discard_ordered_segments();
1194
bartae37e6d2012-01-22 08:58:31 +00001195 if (DRD_(sg_get_trace)()) {
florian19f91bb2012-11-10 22:29:54 +00001196 HChar* str;
bartae37e6d2012-01-22 08:58:31 +00001197
bartc6bf1842012-01-22 08:40:42 +00001198 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(joiner));
sewardj1e29ebc2009-07-15 14:49:17 +00001199 VG_(message)(Vg_DebugMsg, "After join: %s\n", str);
bart8f822af2009-06-08 18:20:42 +00001200 VG_(free)(str);
1201 }
sewardjaf44c822007-11-25 14:01:38 +00001202}
1203
bart324a23b2009-02-15 12:14:52 +00001204/**
bart8f822af2009-06-08 18:20:42 +00001205 * Update the vector clock of the last segment of thread tid with the
bartf6ec1fe2009-06-21 18:07:35 +00001206 * the vector clock of segment sg.
bart26f73e12008-02-24 18:37:08 +00001207 */
bartf6ec1fe2009-06-21 18:07:35 +00001208static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg)
sewardjaf44c822007-11-25 14:01:38 +00001209{
bart8f822af2009-06-08 18:20:42 +00001210 const VectorClock* const vc = &sg->vc;
1211
bartbedfd232009-03-26 19:07:15 +00001212 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1213 && tid != DRD_INVALID_THREADID);
bart91b7ec32012-01-25 20:36:27 +00001214 tl_assert(DRD_(g_threadinfo)[tid].sg_first);
1215 tl_assert(DRD_(g_threadinfo)[tid].sg_last);
bart8f822af2009-06-08 18:20:42 +00001216 tl_assert(sg);
bartbedfd232009-03-26 19:07:15 +00001217 tl_assert(vc);
bart8f822af2009-06-08 18:20:42 +00001218
bartae37e6d2012-01-22 08:58:31 +00001219 if (tid != sg->tid) {
bart8f822af2009-06-08 18:20:42 +00001220 VectorClock old_vc;
1221
bartc6bf1842012-01-22 08:40:42 +00001222 DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(tid));
1223 DRD_(vc_combine)(DRD_(thread_get_vc)(tid), vc);
bartae37e6d2012-01-22 08:58:31 +00001224 if (DRD_(sg_get_trace)()) {
florian19f91bb2012-11-10 22:29:54 +00001225 HChar *str1, *str2;
bart8f822af2009-06-08 18:20:42 +00001226 str1 = DRD_(vc_aprint)(&old_vc);
bartc6bf1842012-01-22 08:40:42 +00001227 str2 = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
florianea71ffb2015-08-05 14:38:57 +00001228 VG_(message)(Vg_DebugMsg, "thread %u: vc %s -> %s\n", tid, str1, str2);
bart8f822af2009-06-08 18:20:42 +00001229 VG_(free)(str1);
1230 VG_(free)(str2);
1231 }
barte5214662009-06-21 11:51:23 +00001232
bart8f822af2009-06-08 18:20:42 +00001233 thread_discard_ordered_segments();
barte5214662009-06-21 11:51:23 +00001234
bart8f822af2009-06-08 18:20:42 +00001235 DRD_(thread_update_conflict_set)(tid, &old_vc);
barte5214662009-06-21 11:51:23 +00001236 s_update_conflict_set_sync_count++;
1237
bart8f822af2009-06-08 18:20:42 +00001238 DRD_(vc_cleanup)(&old_vc);
bartae37e6d2012-01-22 08:58:31 +00001239 } else {
bartc6bf1842012-01-22 08:40:42 +00001240 tl_assert(DRD_(vc_lte)(vc, DRD_(thread_get_vc)(tid)));
bart8f822af2009-06-08 18:20:42 +00001241 }
sewardjaf44c822007-11-25 14:01:38 +00001242}
1243
bart324a23b2009-02-15 12:14:52 +00001244/**
bartf6ec1fe2009-06-21 18:07:35 +00001245 * Create a new segment for thread tid and update the vector clock of the last
florianad4e9792015-07-05 21:53:33 +00001246 * segment of this thread with the vector clock of segment sg. Call this
bartf6ec1fe2009-06-21 18:07:35 +00001247 * function after thread tid had to wait because of thread synchronization
1248 * until the memory accesses in the segment sg finished.
1249 */
1250void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, const Segment* sg)
1251{
1252 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1253 && tid != DRD_INVALID_THREADID);
1254 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1255 tl_assert(sg);
1256
1257 thread_append_segment(tid, DRD_(sg_new)(tid, tid));
1258
1259 thread_combine_vc_sync(tid, sg);
1260
1261 if (s_segment_merging
1262 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
1263 {
1264 thread_discard_ordered_segments();
1265 thread_merge_segments();
1266 }
1267}
1268
1269/**
bart324a23b2009-02-15 12:14:52 +00001270 * Call this function whenever a thread is no longer using the memory
1271 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
1272 * increase.
bart26f73e12008-02-24 18:37:08 +00001273 */
bart23ef19d2011-03-12 12:34:44 +00001274void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +00001275{
bart178b6862011-07-29 12:30:43 +00001276 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001277
bart91b7ec32012-01-25 20:36:27 +00001278 for (p = DRD_(g_sg_list); p; p = p->g_next)
barte278ab52012-01-24 18:28:55 +00001279 DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001280
bart178b6862011-07-29 12:30:43 +00001281 DRD_(bm_clear)(DRD_(g_conflict_set), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001282}
1283
bartd45d9952009-05-31 18:53:54 +00001284/** Specify whether memory loads should be recorded. */
1285void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001286{
bartbedfd232009-03-26 19:07:15 +00001287 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1288 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001289 tl_assert(enabled == !! enabled);
1290
1291 DRD_(g_threadinfo)[tid].is_recording_loads = enabled;
bart0268dfa2008-03-11 20:10:21 +00001292}
1293
bartd45d9952009-05-31 18:53:54 +00001294/** Specify whether memory stores should be recorded. */
1295void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001296{
bartbedfd232009-03-26 19:07:15 +00001297 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1298 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001299 tl_assert(enabled == !! enabled);
1300
1301 DRD_(g_threadinfo)[tid].is_recording_stores = enabled;
bart0268dfa2008-03-11 20:10:21 +00001302}
1303
bart86a87df2009-03-04 19:26:47 +00001304/**
1305 * Print the segment information for all threads.
1306 *
1307 * This function is only used for debugging purposes.
1308 */
bart62a784c2009-02-15 13:11:14 +00001309void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +00001310{
florianea71ffb2015-08-05 14:38:57 +00001311 UInt i;
bartbedfd232009-03-26 19:07:15 +00001312 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001313
bart8f822af2009-06-08 18:20:42 +00001314 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001315 {
bart91b7ec32012-01-25 20:36:27 +00001316 p = DRD_(g_threadinfo)[i].sg_first;
1317 if (p) {
bartbedfd232009-03-26 19:07:15 +00001318 VG_(printf)("**************\n"
florianea71ffb2015-08-05 14:38:57 +00001319 "* thread %3u (%d/%u/%u/%u/0x%lx/%d) *\n"
bartbedfd232009-03-26 19:07:15 +00001320 "**************\n",
1321 i,
bart6d956dc2011-07-28 09:54:37 +00001322 DRD_(g_threadinfo)[i].valid,
bartbedfd232009-03-26 19:07:15 +00001323 DRD_(g_threadinfo)[i].vg_thread_exists,
1324 DRD_(g_threadinfo)[i].vg_threadid,
1325 DRD_(g_threadinfo)[i].posix_thread_exists,
1326 DRD_(g_threadinfo)[i].pt_threadid,
1327 DRD_(g_threadinfo)[i].detached_posix_thread);
bart91b7ec32012-01-25 20:36:27 +00001328 for ( ; p; p = p->thr_next)
bartbedfd232009-03-26 19:07:15 +00001329 DRD_(sg_print)(p);
sewardjaf44c822007-11-25 14:01:38 +00001330 }
bartbedfd232009-03-26 19:07:15 +00001331 }
sewardjaf44c822007-11-25 14:01:38 +00001332}
1333
bart86a87df2009-03-04 19:26:47 +00001334/** Show a call stack involved in a data race. */
barte7086002011-10-11 19:08:39 +00001335static void show_call_stack(const DrdThreadId tid, ExeContext* const callstack)
sewardjaf44c822007-11-25 14:01:38 +00001336{
bartbedfd232009-03-26 19:07:15 +00001337 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +00001338
barte7086002011-10-11 19:08:39 +00001339 if (vg_tid != VG_INVALID_THREADID) {
bartbedfd232009-03-26 19:07:15 +00001340 if (callstack)
bartbedfd232009-03-26 19:07:15 +00001341 VG_(pp_ExeContext)(callstack);
bartbedfd232009-03-26 19:07:15 +00001342 else
bartbedfd232009-03-26 19:07:15 +00001343 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
barte7086002011-10-11 19:08:39 +00001344 } else {
1345 if (!VG_(clo_xml))
1346 VG_(message)(Vg_UserMsg,
1347 " (thread finished, call stack no longer available)\n");
bartbedfd232009-03-26 19:07:15 +00001348 }
sewardjaf44c822007-11-25 14:01:38 +00001349}
1350
bart86a87df2009-03-04 19:26:47 +00001351/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001352static void
1353thread_report_conflicting_segments_segment(const DrdThreadId tid,
1354 const Addr addr,
1355 const SizeT size,
1356 const BmAccessTypeT access_type,
1357 const Segment* const p)
1358{
bartbedfd232009-03-26 19:07:15 +00001359 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001360
bartbedfd232009-03-26 19:07:15 +00001361 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1362 && tid != DRD_INVALID_THREADID);
1363 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +00001364
bart91b7ec32012-01-25 20:36:27 +00001365 for (i = 0; i < DRD_N_THREADS; i++) {
1366 if (i != tid) {
bartbedfd232009-03-26 19:07:15 +00001367 Segment* q;
barte278ab52012-01-24 18:28:55 +00001368
bart91b7ec32012-01-25 20:36:27 +00001369 for (q = DRD_(g_threadinfo)[i].sg_last; q; q = q->thr_prev) {
bartbedfd232009-03-26 19:07:15 +00001370 /*
bart31b983d2010-02-21 14:52:59 +00001371 * Since q iterates over the segments of thread i in order of
1372 * decreasing vector clocks, if q->vc <= p->vc, then
bartbedfd232009-03-26 19:07:15 +00001373 * q->next->vc <= p->vc will also hold. Hence, break out of the
1374 * loop once this condition is met.
1375 */
1376 if (DRD_(vc_lte)(&q->vc, &p->vc))
1377 break;
bart91b7ec32012-01-25 20:36:27 +00001378 if (!DRD_(vc_lte)(&p->vc, &q->vc)) {
bart8f822af2009-06-08 18:20:42 +00001379 if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size,
bart91b7ec32012-01-25 20:36:27 +00001380 access_type)) {
barte278ab52012-01-24 18:28:55 +00001381 Segment* q_next;
1382
bartbedfd232009-03-26 19:07:15 +00001383 tl_assert(q->stacktrace);
barte7086002011-10-11 19:08:39 +00001384 if (VG_(clo_xml))
1385 VG_(printf_xml)(" <other_segment_start>\n");
1386 else
1387 VG_(message)(Vg_UserMsg,
florianea71ffb2015-08-05 14:38:57 +00001388 "Other segment start (thread %u)\n", i);
barte7086002011-10-11 19:08:39 +00001389 show_call_stack(i, q->stacktrace);
1390 if (VG_(clo_xml))
1391 VG_(printf_xml)(" </other_segment_start>\n"
1392 " <other_segment_end>\n");
1393 else
1394 VG_(message)(Vg_UserMsg,
florianea71ffb2015-08-05 14:38:57 +00001395 "Other segment end (thread %u)\n", i);
bart91b7ec32012-01-25 20:36:27 +00001396 q_next = q->thr_next;
barte278ab52012-01-24 18:28:55 +00001397 show_call_stack(i, q_next ? q_next->stacktrace : 0);
barte7086002011-10-11 19:08:39 +00001398 if (VG_(clo_xml))
1399 VG_(printf_xml)(" </other_segment_end>\n");
bartbedfd232009-03-26 19:07:15 +00001400 }
1401 }
1402 }
sewardjaf44c822007-11-25 14:01:38 +00001403 }
bartbedfd232009-03-26 19:07:15 +00001404 }
sewardjaf44c822007-11-25 14:01:38 +00001405}
1406
bart86a87df2009-03-04 19:26:47 +00001407/** Print information about all segments involved in a data race. */
bart62a784c2009-02-15 13:11:14 +00001408void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1409 const Addr addr,
1410 const SizeT size,
1411 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +00001412{
bartbedfd232009-03-26 19:07:15 +00001413 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001414
bartbedfd232009-03-26 19:07:15 +00001415 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1416 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001417
bart91b7ec32012-01-25 20:36:27 +00001418 for (p = DRD_(g_threadinfo)[tid].sg_first; p; p = p->thr_next) {
bart8f822af2009-06-08 18:20:42 +00001419 if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type))
bartbedfd232009-03-26 19:07:15 +00001420 thread_report_conflicting_segments_segment(tid, addr, size,
1421 access_type, p);
bartbedfd232009-03-26 19:07:15 +00001422 }
sewardjaf44c822007-11-25 14:01:38 +00001423}
sewardjaf44c822007-11-25 14:01:38 +00001424
bart324a23b2009-02-15 12:14:52 +00001425/**
bart8f822af2009-06-08 18:20:42 +00001426 * Verify whether the conflict set for thread tid is up to date. Only perform
1427 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
1428 */
1429static Bool thread_conflict_set_up_to_date(const DrdThreadId tid)
1430{
bart8f822af2009-06-08 18:20:42 +00001431 Bool result;
1432 struct bitmap* computed_conflict_set = 0;
1433
bart9cdc0832014-08-09 12:58:17 +00001434 if (!DRD_(verify_conflict_set))
bart8f822af2009-06-08 18:20:42 +00001435 return True;
1436
1437 thread_compute_conflict_set(&computed_conflict_set, tid);
1438 result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set);
1439 if (! result)
1440 {
1441 VG_(printf)("actual conflict set:\n");
1442 DRD_(bm_print)(DRD_(g_conflict_set));
1443 VG_(printf)("\n");
1444 VG_(printf)("computed conflict set:\n");
1445 DRD_(bm_print)(computed_conflict_set);
1446 VG_(printf)("\n");
1447 }
1448 DRD_(bm_delete)(computed_conflict_set);
1449 return result;
1450}
1451
1452/**
1453 * Compute the conflict set: a bitmap that represents the union of all memory
1454 * accesses of all segments that are unordered to the current segment of the
1455 * thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001456 */
bart86a87df2009-03-04 19:26:47 +00001457static void thread_compute_conflict_set(struct bitmap** conflict_set,
1458 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001459{
bartbedfd232009-03-26 19:07:15 +00001460 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001461
bartbedfd232009-03-26 19:07:15 +00001462 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1463 && tid != DRD_INVALID_THREADID);
1464 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001465
bart54803fe2009-06-21 09:26:27 +00001466 s_compute_conflict_set_count++;
bartbedfd232009-03-26 19:07:15 +00001467 s_conflict_set_bitmap_creation_count
1468 -= DRD_(bm_get_bitmap_creation_count)();
1469 s_conflict_set_bitmap2_creation_count
1470 -= DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001471
bartae37e6d2012-01-22 08:58:31 +00001472 if (*conflict_set) {
bartf6ec1fe2009-06-21 18:07:35 +00001473 DRD_(bm_cleanup)(*conflict_set);
1474 DRD_(bm_init)(*conflict_set);
bartae37e6d2012-01-22 08:58:31 +00001475 } else {
bartf6ec1fe2009-06-21 18:07:35 +00001476 *conflict_set = DRD_(bm_new)();
1477 }
bart26f73e12008-02-24 18:37:08 +00001478
bartae37e6d2012-01-22 08:58:31 +00001479 if (s_trace_conflict_set) {
florian19f91bb2012-11-10 22:29:54 +00001480 HChar* str;
bart26f73e12008-02-24 18:37:08 +00001481
bartc6bf1842012-01-22 08:40:42 +00001482 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
bart8f822af2009-06-08 18:20:42 +00001483 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +00001484 "computing conflict set for thread %u with vc %s\n",
bart63c92ea2009-07-19 17:53:56 +00001485 tid, str);
bart8f822af2009-06-08 18:20:42 +00001486 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001487 }
sewardjaf44c822007-11-25 14:01:38 +00001488
bart91b7ec32012-01-25 20:36:27 +00001489 p = DRD_(g_threadinfo)[tid].sg_last;
bartbedfd232009-03-26 19:07:15 +00001490 {
1491 unsigned j;
1492
bart91b7ec32012-01-25 20:36:27 +00001493 if (s_trace_conflict_set) {
florian19f91bb2012-11-10 22:29:54 +00001494 HChar* vc;
bartbedfd232009-03-26 19:07:15 +00001495
bart8f822af2009-06-08 18:20:42 +00001496 vc = DRD_(vc_aprint)(&p->vc);
florianea71ffb2015-08-05 14:38:57 +00001497 VG_(message)(Vg_DebugMsg, "conflict set: thread [%u] at vc %s\n",
bart8f822af2009-06-08 18:20:42 +00001498 tid, vc);
1499 VG_(free)(vc);
bart26f73e12008-02-24 18:37:08 +00001500 }
sewardjaf44c822007-11-25 14:01:38 +00001501
bart91b7ec32012-01-25 20:36:27 +00001502 for (j = 0; j < DRD_N_THREADS; j++) {
1503 if (j != tid && DRD_(IsValidDrdThreadId)(j)) {
bart8f822af2009-06-08 18:20:42 +00001504 Segment* q;
bart91b7ec32012-01-25 20:36:27 +00001505
1506 for (q = DRD_(g_threadinfo)[j].sg_last; q; q = q->thr_prev) {
1507 if (!DRD_(vc_lte)(&q->vc, &p->vc)
1508 && !DRD_(vc_lte)(&p->vc, &q->vc)) {
1509 if (s_trace_conflict_set) {
florian19f91bb2012-11-10 22:29:54 +00001510 HChar* str;
bart8f822af2009-06-08 18:20:42 +00001511
1512 str = DRD_(vc_aprint)(&q->vc);
1513 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +00001514 "conflict set: [%u] merging segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001515 j, str);
1516 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001517 }
bart8f822af2009-06-08 18:20:42 +00001518 DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q));
bart91b7ec32012-01-25 20:36:27 +00001519 } else {
1520 if (s_trace_conflict_set) {
florian19f91bb2012-11-10 22:29:54 +00001521 HChar* str;
bart8f822af2009-06-08 18:20:42 +00001522
1523 str = DRD_(vc_aprint)(&q->vc);
1524 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +00001525 "conflict set: [%u] ignoring segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001526 j, str);
1527 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001528 }
1529 }
1530 }
1531 }
1532 }
1533 }
sewardjaf44c822007-11-25 14:01:38 +00001534
bartbedfd232009-03-26 19:07:15 +00001535 s_conflict_set_bitmap_creation_count
1536 += DRD_(bm_get_bitmap_creation_count)();
1537 s_conflict_set_bitmap2_creation_count
1538 += DRD_(bm_get_bitmap2_creation_count)();
1539
bart91b7ec32012-01-25 20:36:27 +00001540 if (s_trace_conflict_set_bm) {
florianea71ffb2015-08-05 14:38:57 +00001541 VG_(message)(Vg_DebugMsg, "[%u] new conflict set:\n", tid);
bartbedfd232009-03-26 19:07:15 +00001542 DRD_(bm_print)(*conflict_set);
florianea71ffb2015-08-05 14:38:57 +00001543 VG_(message)(Vg_DebugMsg, "[%u] end of new conflict set.\n", tid);
bartbedfd232009-03-26 19:07:15 +00001544 }
sewardjaf44c822007-11-25 14:01:38 +00001545}
1546
bart8f822af2009-06-08 18:20:42 +00001547/**
1548 * Update the conflict set after the vector clock of thread tid has been
1549 * updated from old_vc to its current value, either because a new segment has
1550 * been created or because of a synchronization operation.
1551 */
1552void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
1553 const VectorClock* const old_vc)
1554{
1555 const VectorClock* new_vc;
1556 Segment* p;
1557 unsigned j;
1558
1559 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1560 && tid != DRD_INVALID_THREADID);
1561 tl_assert(old_vc);
1562 tl_assert(tid == DRD_(g_drd_running_tid));
1563 tl_assert(DRD_(g_conflict_set));
1564
bartae37e6d2012-01-22 08:58:31 +00001565 if (s_trace_conflict_set) {
florian19f91bb2012-11-10 22:29:54 +00001566 HChar* str;
bart8f822af2009-06-08 18:20:42 +00001567
bartc6bf1842012-01-22 08:40:42 +00001568 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
bart8f822af2009-06-08 18:20:42 +00001569 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +00001570 "updating conflict set for thread %u with vc %s\n",
bart63c92ea2009-07-19 17:53:56 +00001571 tid, str);
bart8f822af2009-06-08 18:20:42 +00001572 VG_(free)(str);
1573 }
1574
bartc6bf1842012-01-22 08:40:42 +00001575 new_vc = DRD_(thread_get_vc)(tid);
bartf5fe4b62011-07-03 11:39:30 +00001576 tl_assert(DRD_(vc_lte)(old_vc, new_vc));
bart8f822af2009-06-08 18:20:42 +00001577
1578 DRD_(bm_unmark)(DRD_(g_conflict_set));
1579
1580 for (j = 0; j < DRD_N_THREADS; j++)
1581 {
1582 Segment* q;
1583
1584 if (j == tid || ! DRD_(IsValidDrdThreadId)(j))
1585 continue;
1586
bart91b7ec32012-01-25 20:36:27 +00001587 for (q = DRD_(g_threadinfo)[j].sg_last;
1588 q && !DRD_(vc_lte)(&q->vc, new_vc);
1589 q = q->thr_prev) {
1590 const Bool included_in_old_conflict_set
1591 = !DRD_(vc_lte)(old_vc, &q->vc);
1592 const Bool included_in_new_conflict_set
1593 = !DRD_(vc_lte)(new_vc, &q->vc);
bart178b6862011-07-29 12:30:43 +00001594
1595 if (UNLIKELY(s_trace_conflict_set)) {
florian19f91bb2012-11-10 22:29:54 +00001596 HChar* str;
bart178b6862011-07-29 12:30:43 +00001597
1598 str = DRD_(vc_aprint)(&q->vc);
1599 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +00001600 "conflict set: [%u] %s segment %s\n", j,
bart178b6862011-07-29 12:30:43 +00001601 included_in_old_conflict_set
1602 != included_in_new_conflict_set
1603 ? "merging" : "ignoring", str);
1604 VG_(free)(str);
1605 }
1606 if (included_in_old_conflict_set != included_in_new_conflict_set)
1607 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1608 }
1609
bart91b7ec32012-01-25 20:36:27 +00001610 for ( ; q && !DRD_(vc_lte)(&q->vc, old_vc); q = q->thr_prev) {
1611 const Bool included_in_old_conflict_set
1612 = !DRD_(vc_lte)(old_vc, &q->vc);
1613 const Bool included_in_new_conflict_set
1614 = !DRD_(vc_lte)(&q->vc, new_vc)
1615 && !DRD_(vc_lte)(new_vc, &q->vc);
bart178b6862011-07-29 12:30:43 +00001616
1617 if (UNLIKELY(s_trace_conflict_set)) {
florian19f91bb2012-11-10 22:29:54 +00001618 HChar* str;
bartac5b95b2011-07-03 11:43:45 +00001619
1620 str = DRD_(vc_aprint)(&q->vc);
1621 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +00001622 "conflict set: [%u] %s segment %s\n", j,
bartac5b95b2011-07-03 11:43:45 +00001623 included_in_old_conflict_set
1624 != included_in_new_conflict_set
1625 ? "merging" : "ignoring", str);
1626 VG_(free)(str);
1627 }
bart8f822af2009-06-08 18:20:42 +00001628 if (included_in_old_conflict_set != included_in_new_conflict_set)
bart8f822af2009-06-08 18:20:42 +00001629 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
bart8f822af2009-06-08 18:20:42 +00001630 }
1631 }
1632
1633 DRD_(bm_clear_marked)(DRD_(g_conflict_set));
1634
bart91b7ec32012-01-25 20:36:27 +00001635 p = DRD_(g_threadinfo)[tid].sg_last;
1636 for (j = 0; j < DRD_N_THREADS; j++) {
1637 if (j != tid && DRD_(IsValidDrdThreadId)(j)) {
bart4b3fdb22011-07-03 11:40:49 +00001638 Segment* q;
bart91b7ec32012-01-25 20:36:27 +00001639 for (q = DRD_(g_threadinfo)[j].sg_last;
1640 q && !DRD_(vc_lte)(&q->vc, &p->vc);
1641 q = q->thr_prev) {
bart178b6862011-07-29 12:30:43 +00001642 if (!DRD_(vc_lte)(&p->vc, &q->vc))
bart4b3fdb22011-07-03 11:40:49 +00001643 DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
bart8f822af2009-06-08 18:20:42 +00001644 }
1645 }
1646 }
1647
1648 DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set));
1649
bart54803fe2009-06-21 09:26:27 +00001650 s_update_conflict_set_count++;
bart8f822af2009-06-08 18:20:42 +00001651
1652 if (s_trace_conflict_set_bm)
1653 {
florianea71ffb2015-08-05 14:38:57 +00001654 VG_(message)(Vg_DebugMsg, "[%u] updated conflict set:\n", tid);
bart8f822af2009-06-08 18:20:42 +00001655 DRD_(bm_print)(DRD_(g_conflict_set));
florianea71ffb2015-08-05 14:38:57 +00001656 VG_(message)(Vg_DebugMsg, "[%u] end of updated conflict set.\n", tid);
bart8f822af2009-06-08 18:20:42 +00001657 }
1658
1659 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1660}
1661
bart86a87df2009-03-04 19:26:47 +00001662/** Report the number of context switches performed. */
bart62a784c2009-02-15 13:11:14 +00001663ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001664{
bartbedfd232009-03-26 19:07:15 +00001665 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +00001666}
1667
bart86a87df2009-03-04 19:26:47 +00001668/** Report the number of ordered segments that have been discarded. */
bart62a784c2009-02-15 13:11:14 +00001669ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001670{
bartbedfd232009-03-26 19:07:15 +00001671 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +00001672}
1673
bart54803fe2009-06-21 09:26:27 +00001674/** Return how many times the conflict set has been updated entirely. */
1675ULong DRD_(thread_get_compute_conflict_set_count)()
sewardjaf44c822007-11-25 14:01:38 +00001676{
bart54803fe2009-06-21 09:26:27 +00001677 return s_compute_conflict_set_count;
1678}
1679
1680/** Return how many times the conflict set has been updated partially. */
1681ULong DRD_(thread_get_update_conflict_set_count)(void)
1682{
bartbedfd232009-03-26 19:07:15 +00001683 return s_update_conflict_set_count;
sewardjaf44c822007-11-25 14:01:38 +00001684}
1685
bart86a87df2009-03-04 19:26:47 +00001686/**
barte5214662009-06-21 11:51:23 +00001687 * Return how many times the conflict set has been updated partially
1688 * because a new segment has been created.
1689 */
1690ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void)
1691{
1692 return s_update_conflict_set_new_sg_count;
1693}
1694
1695/**
1696 * Return how many times the conflict set has been updated partially
1697 * because of combining vector clocks due to synchronization operations
1698 * other than reader/writer lock or barrier operations.
1699 */
1700ULong DRD_(thread_get_update_conflict_set_sync_count)(void)
1701{
1702 return s_update_conflict_set_sync_count;
1703}
1704
1705/**
1706 * Return how many times the conflict set has been updated partially
1707 * because of thread joins.
1708 */
1709ULong DRD_(thread_get_update_conflict_set_join_count)(void)
1710{
1711 return s_update_conflict_set_join_count;
1712}
1713
1714/**
bart86a87df2009-03-04 19:26:47 +00001715 * Return the number of first-level bitmaps that have been created during
1716 * conflict set updates.
1717 */
bart62a784c2009-02-15 13:11:14 +00001718ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001719{
bartbedfd232009-03-26 19:07:15 +00001720 return s_conflict_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001721}
1722
bart86a87df2009-03-04 19:26:47 +00001723/**
1724 * Return the number of second-level bitmaps that have been created during
1725 * conflict set updates.
1726 */
bart62a784c2009-02-15 13:11:14 +00001727ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001728{
bartbedfd232009-03-26 19:07:15 +00001729 return s_conflict_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001730}