blob: 9bb320091f4cc854fc9c85dc0936da6d71c62afb [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
sewardjb3a1e4b2015-08-21 11:32:26 +00004 Copyright (C) 2006-2015 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 *
605 * @note This function can be called two times for the same thread -- see also
606 * the comment block preceding the pthread_create() wrapper in
607 * 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);
616 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
617 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardj8eb8bab2015-07-21 14:44:28 +0000618
619 if (DRD_(g_threadinfo)[tid].creator_thread != DRD_INVALID_THREADID) {
620 if (DRD_(ignore_thread_creation)) {
621 DRD_(thread_leave_synchr)(tid);
622 tl_assert(DRD_(thread_get_synchr_nesting_count)(tid) == 0);
623 }
624 }
sewardjaf44c822007-11-25 14:01:38 +0000625}
626
bart86a87df2009-03-04 19:26:47 +0000627/** Returns true for joinable threads and false for detached threads. */
bart62a784c2009-02-15 13:11:14 +0000628Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000629{
bartbedfd232009-03-26 19:07:15 +0000630 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
631 && tid != DRD_INVALID_THREADID);
632 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000633}
634
bart86a87df2009-03-04 19:26:47 +0000635/** Store the thread mode: joinable or detached. */
petarj4df0bfc2013-02-27 23:17:33 +0000636#if defined(VGP_mips32_linux) || defined(VGP_mips64_linux)
sewardj5db15402012-06-07 09:13:21 +0000637 /* There is a cse related issue in gcc for MIPS. Optimization level
638 has to be lowered, so cse related optimizations are not
639 included.*/
640 __attribute__((optimize("O1")))
641#endif
bart62a784c2009-02-15 13:11:14 +0000642void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000643{
bartbedfd232009-03-26 19:07:15 +0000644 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
645 && tid != DRD_INVALID_THREADID);
sewardjd56b7e22015-01-20 00:12:18 +0000646 tl_assert((!! joinable) == joinable);
bartbedfd232009-03-26 19:07:15 +0000647 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bart8f822af2009-06-08 18:20:42 +0000648
bartbedfd232009-03-26 19:07:15 +0000649 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000650}
651
bartdd75cdf2009-07-24 08:20:10 +0000652/** Tells DRD that the calling thread is about to enter pthread_create(). */
653void DRD_(thread_entering_pthread_create)(const DrdThreadId tid)
654{
655 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
656 && tid != DRD_INVALID_THREADID);
657 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
658 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level >= 0);
659
660 DRD_(g_threadinfo)[tid].pthread_create_nesting_level++;
sewardj8eb8bab2015-07-21 14:44:28 +0000661
662 if (DRD_(ignore_thread_creation)) {
663 tl_assert(DRD_(thread_get_synchr_nesting_count)(tid) == 0);
664 DRD_(thread_enter_synchr)(tid);
665 }
bartdd75cdf2009-07-24 08:20:10 +0000666}
667
668/** Tells DRD that the calling thread has left pthread_create(). */
669void DRD_(thread_left_pthread_create)(const DrdThreadId tid)
670{
671 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
672 && tid != DRD_INVALID_THREADID);
673 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
674 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level > 0);
675
676 DRD_(g_threadinfo)[tid].pthread_create_nesting_level--;
sewardj8eb8bab2015-07-21 14:44:28 +0000677
678 if (DRD_(ignore_thread_creation)) {
679 DRD_(thread_leave_synchr)(tid);
680 tl_assert(DRD_(thread_get_synchr_nesting_count)(tid) == 0);
681 }
bartdd75cdf2009-07-24 08:20:10 +0000682}
683
sewardj8eb8bab2015-07-21 14:44:28 +0000684#if defined(VGO_solaris)
685/** Handles the bind_guard() intercept. */
686void DRD_(thread_entering_rtld_bind_guard)(const DrdThreadId tid, int flags)
687{
688 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
689 && tid != DRD_INVALID_THREADID);
690
691 Int bindflag = (flags & VKI_THR_FLG_RTLD);
692 if ((bindflag & DRD_(g_threadinfo)[tid].bind_guard_flag) == 0) {
693 DRD_(g_threadinfo)[tid].bind_guard_flag |= bindflag;
694 DRD_(thread_enter_synchr)(tid);
695 }
696}
697
698/**
699 * Handles the bind_clear() intercept.
700 * Call to bind_clear(0) is typically used to determine value of bind_flags.
701 */
702void DRD_(thread_leaving_rtld_bind_clear)(const DrdThreadId tid, int flags)
703{
704 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
705 && tid != DRD_INVALID_THREADID);
706
707 Int bindflag = (flags & VKI_THR_FLG_RTLD);
708 if ((DRD_(g_threadinfo)[tid].bind_guard_flag & bindflag) != 0) {
709 DRD_(g_threadinfo)[tid].bind_guard_flag &= ~bindflag;
710 DRD_(thread_leave_synchr)(tid);
711 }
712}
713#endif /* VGO_solaris */
714
bartd45d9952009-05-31 18:53:54 +0000715/** Obtain the thread number and the user-assigned thread name. */
florian19f91bb2012-11-10 22:29:54 +0000716const HChar* DRD_(thread_get_name)(const DrdThreadId tid)
bartd45d9952009-05-31 18:53:54 +0000717{
718 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
719 && tid != DRD_INVALID_THREADID);
720
721 return DRD_(g_threadinfo)[tid].name;
722}
723
724/** Set the name of the specified thread. */
florian19f91bb2012-11-10 22:29:54 +0000725void DRD_(thread_set_name)(const DrdThreadId tid, const HChar* const name)
bartd45d9952009-05-31 18:53:54 +0000726{
727 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
728 && tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000729
bartd45d9952009-05-31 18:53:54 +0000730 if (name == NULL || name[0] == 0)
731 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
732 sizeof(DRD_(g_threadinfo)[tid].name),
florianea71ffb2015-08-05 14:38:57 +0000733 "Thread %u",
bartd45d9952009-05-31 18:53:54 +0000734 tid);
735 else
736 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
737 sizeof(DRD_(g_threadinfo)[tid].name),
florianea71ffb2015-08-05 14:38:57 +0000738 "Thread %u (%s)",
bartd45d9952009-05-31 18:53:54 +0000739 tid, name);
740 DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0;
741}
742
bart86a87df2009-03-04 19:26:47 +0000743/**
744 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
745 * conflict set.
746 */
bart62a784c2009-02-15 13:11:14 +0000747void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000748{
bartbedfd232009-03-26 19:07:15 +0000749 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000750
bartbedfd232009-03-26 19:07:15 +0000751 if (vg_tid != s_vg_running_tid)
752 {
753 DRD_(thread_set_running_tid)(vg_tid,
754 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
755 }
sewardj8b09d4f2007-12-04 21:27:18 +0000756
bartbedfd232009-03-26 19:07:15 +0000757 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
758 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000759}
760
bart86a87df2009-03-04 19:26:47 +0000761/**
762 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
763 * conflict set.
764 */
bart62a784c2009-02-15 13:11:14 +0000765void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
766 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000767{
bartbedfd232009-03-26 19:07:15 +0000768 tl_assert(vg_tid != VG_INVALID_THREADID);
769 tl_assert(drd_tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000770
bartbedfd232009-03-26 19:07:15 +0000771 if (vg_tid != s_vg_running_tid)
772 {
773 if (s_trace_context_switches
774 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
775 {
776 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +0000777 "Context switch from thread %u to thread %u;"
sewardj1e29ebc2009-07-15 14:49:17 +0000778 " segments: %llu\n",
bart63c92ea2009-07-19 17:53:56 +0000779 DRD_(g_drd_running_tid), drd_tid,
bartbedfd232009-03-26 19:07:15 +0000780 DRD_(sg_get_segments_alive_count)());
781 }
782 s_vg_running_tid = vg_tid;
783 DRD_(g_drd_running_tid) = drd_tid;
784 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
785 s_context_switch_count++;
786 }
sewardj8b09d4f2007-12-04 21:27:18 +0000787
bartbedfd232009-03-26 19:07:15 +0000788 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
789 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000790}
791
bart86a87df2009-03-04 19:26:47 +0000792/**
793 * Increase the synchronization nesting counter. Must be called before the
794 * client calls a synchronization function.
795 */
bart62a784c2009-02-15 13:11:14 +0000796int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000797{
bartbedfd232009-03-26 19:07:15 +0000798 tl_assert(DRD_(IsValidDrdThreadId)(tid));
799 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000800}
801
bart86a87df2009-03-04 19:26:47 +0000802/**
803 * Decrease the synchronization nesting counter. Must be called after the
804 * client left a synchronization function.
805 */
bart62a784c2009-02-15 13:11:14 +0000806int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000807{
bartbedfd232009-03-26 19:07:15 +0000808 tl_assert(DRD_(IsValidDrdThreadId)(tid));
809 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
810 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000811}
812
bart86a87df2009-03-04 19:26:47 +0000813/** Returns the synchronization nesting counter. */
bart62a784c2009-02-15 13:11:14 +0000814int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000815{
bartbedfd232009-03-26 19:07:15 +0000816 tl_assert(DRD_(IsValidDrdThreadId)(tid));
817 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000818}
819
bart1a473c72008-03-13 19:03:38 +0000820/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000821static
bart86a87df2009-03-04 19:26:47 +0000822void thread_append_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000823{
bartbedfd232009-03-26 19:07:15 +0000824 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
825 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000826
827#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
828 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
829#endif
830
bart91b7ec32012-01-25 20:36:27 +0000831 // add at tail
832 sg->thr_prev = DRD_(g_threadinfo)[tid].sg_last;
833 sg->thr_next = NULL;
834 if (DRD_(g_threadinfo)[tid].sg_last)
835 DRD_(g_threadinfo)[tid].sg_last->thr_next = sg;
836 DRD_(g_threadinfo)[tid].sg_last = sg;
837 if (DRD_(g_threadinfo)[tid].sg_first == NULL)
838 DRD_(g_threadinfo)[tid].sg_first = sg;
bart8f822af2009-06-08 18:20:42 +0000839
840#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
841 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
842#endif
sewardjaf44c822007-11-25 14:01:38 +0000843}
844
bart324a23b2009-02-15 12:14:52 +0000845/**
846 * Remove a segment from the segment list of thread threadid, and free the
847 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000848 */
bart62a784c2009-02-15 13:11:14 +0000849static
bart86a87df2009-03-04 19:26:47 +0000850void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000851{
bartbedfd232009-03-26 19:07:15 +0000852 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
853 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000854
855#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
856 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
857#endif
bart26f73e12008-02-24 18:37:08 +0000858
bart91b7ec32012-01-25 20:36:27 +0000859 if (sg->thr_prev)
860 sg->thr_prev->thr_next = sg->thr_next;
861 if (sg->thr_next)
862 sg->thr_next->thr_prev = sg->thr_prev;
863 if (sg == DRD_(g_threadinfo)[tid].sg_first)
864 DRD_(g_threadinfo)[tid].sg_first = sg->thr_next;
865 if (sg == DRD_(g_threadinfo)[tid].sg_last)
866 DRD_(g_threadinfo)[tid].sg_last = sg->thr_prev;
bartbedfd232009-03-26 19:07:15 +0000867 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000868
bart8f822af2009-06-08 18:20:42 +0000869#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
870 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
871#endif
sewardjaf44c822007-11-25 14:01:38 +0000872}
873
bart86a87df2009-03-04 19:26:47 +0000874/**
875 * Returns a pointer to the vector clock of the most recent segment associated
876 * with thread 'tid'.
877 */
bart62a784c2009-02-15 13:11:14 +0000878VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000879{
bart91b7ec32012-01-25 20:36:27 +0000880 Segment* latest_sg;
barte278ab52012-01-24 18:28:55 +0000881
bartbedfd232009-03-26 19:07:15 +0000882 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
883 && tid != DRD_INVALID_THREADID);
bart91b7ec32012-01-25 20:36:27 +0000884 latest_sg = DRD_(g_threadinfo)[tid].sg_last;
885 tl_assert(latest_sg);
886 return &latest_sg->vc;
sewardjaf44c822007-11-25 14:01:38 +0000887}
888
bart324a23b2009-02-15 12:14:52 +0000889/**
890 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000891 */
bart62a784c2009-02-15 13:11:14 +0000892void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000893{
bart91b7ec32012-01-25 20:36:27 +0000894 Segment* latest_sg;
barte278ab52012-01-24 18:28:55 +0000895
bartbedfd232009-03-26 19:07:15 +0000896 tl_assert(sg);
897 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
898 && tid != DRD_INVALID_THREADID);
bart91b7ec32012-01-25 20:36:27 +0000899 latest_sg = DRD_(g_threadinfo)[tid].sg_last;
900 tl_assert(latest_sg);
barta2b6e1b2008-03-17 18:32:39 +0000901
bartbedfd232009-03-26 19:07:15 +0000902 DRD_(sg_put)(*sg);
bart91b7ec32012-01-25 20:36:27 +0000903 *sg = DRD_(sg_get)(latest_sg);
barta2b6e1b2008-03-17 18:32:39 +0000904}
905
sewardjaf44c822007-11-25 14:01:38 +0000906/**
907 * Compute the minimum of all latest vector clocks of all threads
908 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
bart86a87df2009-03-04 19:26:47 +0000909 *
sewardjaf44c822007-11-25 14:01:38 +0000910 * @param vc pointer to a vectorclock, holds result upon return.
911 */
bart62a784c2009-02-15 13:11:14 +0000912static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000913{
bartbedfd232009-03-26 19:07:15 +0000914 unsigned i;
915 Bool first;
916 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000917
bartbedfd232009-03-26 19:07:15 +0000918 first = True;
bart8f822af2009-06-08 18:20:42 +0000919 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000920 {
bart91b7ec32012-01-25 20:36:27 +0000921 latest_sg = DRD_(g_threadinfo)[i].sg_last;
922 if (latest_sg) {
bartbedfd232009-03-26 19:07:15 +0000923 if (first)
924 DRD_(vc_assign)(vc, &latest_sg->vc);
925 else
926 DRD_(vc_min)(vc, &latest_sg->vc);
927 first = False;
928 }
929 }
sewardjaf44c822007-11-25 14:01:38 +0000930}
931
bart86a87df2009-03-04 19:26:47 +0000932/**
933 * Compute the maximum of all latest vector clocks of all threads.
934 *
935 * @param vc pointer to a vectorclock, holds result upon return.
936 */
bart62a784c2009-02-15 13:11:14 +0000937static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000938{
bartbedfd232009-03-26 19:07:15 +0000939 unsigned i;
940 Bool first;
941 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000942
bartbedfd232009-03-26 19:07:15 +0000943 first = True;
bart8f822af2009-06-08 18:20:42 +0000944 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000945 {
bart91b7ec32012-01-25 20:36:27 +0000946 latest_sg = DRD_(g_threadinfo)[i].sg_last;
947 if (latest_sg) {
bartbedfd232009-03-26 19:07:15 +0000948 if (first)
949 DRD_(vc_assign)(vc, &latest_sg->vc);
950 else
951 DRD_(vc_combine)(vc, &latest_sg->vc);
952 first = False;
953 }
954 }
sewardjaf44c822007-11-25 14:01:38 +0000955}
956
957/**
bart5bd9f2d2008-03-03 20:31:58 +0000958 * Discard all segments that have a defined order against the latest vector
bart86a87df2009-03-04 19:26:47 +0000959 * clock of all threads -- these segments can no longer be involved in a
sewardjaf44c822007-11-25 14:01:38 +0000960 * data race.
961 */
bart8f822af2009-06-08 18:20:42 +0000962static void thread_discard_ordered_segments(void)
sewardjaf44c822007-11-25 14:01:38 +0000963{
bartbedfd232009-03-26 19:07:15 +0000964 unsigned i;
965 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000966
bartbedfd232009-03-26 19:07:15 +0000967 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000968
bartbedfd232009-03-26 19:07:15 +0000969 DRD_(vc_init)(&thread_vc_min, 0, 0);
970 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
971 if (DRD_(sg_get_trace)())
972 {
florian19f91bb2012-11-10 22:29:54 +0000973 HChar *vc_min, *vc_max;
bartbedfd232009-03-26 19:07:15 +0000974 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000975
bartbedfd232009-03-26 19:07:15 +0000976 DRD_(vc_init)(&thread_vc_max, 0, 0);
977 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
bart8f822af2009-06-08 18:20:42 +0000978 vc_min = DRD_(vc_aprint)(&thread_vc_min);
979 vc_max = DRD_(vc_aprint)(&thread_vc_max);
980 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000981 "Discarding ordered segments -- min vc is %s, max vc is %s\n",
bart8f822af2009-06-08 18:20:42 +0000982 vc_min, vc_max);
983 VG_(free)(vc_min);
984 VG_(free)(vc_max);
bartbedfd232009-03-26 19:07:15 +0000985 DRD_(vc_cleanup)(&thread_vc_max);
986 }
sewardjaf44c822007-11-25 14:01:38 +0000987
bart91b7ec32012-01-25 20:36:27 +0000988 for (i = 0; i < DRD_N_THREADS; i++) {
bartbedfd232009-03-26 19:07:15 +0000989 Segment* sg;
990 Segment* sg_next;
barte278ab52012-01-24 18:28:55 +0000991
bart91b7ec32012-01-25 20:36:27 +0000992 for (sg = DRD_(g_threadinfo)[i].sg_first;
993 sg && (sg_next = sg->thr_next)
994 && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
995 sg = sg_next)
996 {
bartbedfd232009-03-26 19:07:15 +0000997 thread_discard_segment(i, sg);
998 }
999 }
1000 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +00001001}
1002
bart324a23b2009-02-15 12:14:52 +00001003/**
bart8f822af2009-06-08 18:20:42 +00001004 * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper
1005 * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if
1006 * all segments in the set CS are ordered consistently against both sg1 and
1007 * sg2. The set CS is defined as the set of segments that can immediately
1008 * precede future segments via inter-thread synchronization operations. In
1009 * DRD the set CS consists of the latest segment of each thread combined with
1010 * all segments for which the reference count is strictly greater than one.
1011 * The code below is an optimized version of the following:
1012 *
1013 * for (i = 0; i < DRD_N_THREADS; i++)
1014 * {
1015 * Segment* sg;
1016 *
1017 * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
1018 * {
1019 * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1)
1020 * {
1021 * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc)
1022 * != DRD_(vc_lte)(&sg2->vc, &sg->vc)
1023 * || DRD_(vc_lte)(&sg->vc, &sg1->vc)
1024 * != DRD_(vc_lte)(&sg->vc, &sg2->vc))
1025 * {
1026 * return False;
1027 * }
1028 * }
1029 * }
1030 * }
1031 */
1032static Bool thread_consistent_segment_ordering(const DrdThreadId tid,
1033 Segment* const sg1,
1034 Segment* const sg2)
1035{
1036 unsigned i;
1037
bart91b7ec32012-01-25 20:36:27 +00001038 tl_assert(sg1->thr_next);
1039 tl_assert(sg2->thr_next);
1040 tl_assert(sg1->thr_next == sg2);
bart8f822af2009-06-08 18:20:42 +00001041 tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc));
1042
1043 for (i = 0; i < DRD_N_THREADS; i++)
1044 {
1045 Segment* sg;
1046
bart91b7ec32012-01-25 20:36:27 +00001047 for (sg = DRD_(g_threadinfo)[i].sg_first; sg; sg = sg->thr_next) {
1048 if (!sg->thr_next || DRD_(sg_get_refcnt)(sg) > 1) {
bart8f822af2009-06-08 18:20:42 +00001049 if (DRD_(vc_lte)(&sg2->vc, &sg->vc))
1050 break;
1051 if (DRD_(vc_lte)(&sg1->vc, &sg->vc))
1052 return False;
1053 }
1054 }
bart91b7ec32012-01-25 20:36:27 +00001055 for (sg = DRD_(g_threadinfo)[i].sg_last; sg; sg = sg->thr_prev) {
1056 if (!sg->thr_next || DRD_(sg_get_refcnt)(sg) > 1) {
bart8f822af2009-06-08 18:20:42 +00001057 if (DRD_(vc_lte)(&sg->vc, &sg1->vc))
1058 break;
1059 if (DRD_(vc_lte)(&sg->vc, &sg2->vc))
1060 return False;
1061 }
1062 }
1063 }
1064 return True;
1065}
1066
1067/**
bart324a23b2009-02-15 12:14:52 +00001068 * Merge all segments that may be merged without triggering false positives
1069 * or discarding real data races. For the theoretical background of segment
bart8f822af2009-06-08 18:20:42 +00001070 * merging, see also the following paper: Mark Christiaens, Michiel Ronsse
1071 * and Koen De Bosschere. Bounding the number of segment histories during
1072 * data race detection. Parallel Computing archive, Volume 28, Issue 9,
1073 * pp 1221-1238, September 2002. This paper contains a proof that merging
1074 * consecutive segments for which the property equiv(s1,s2) holds can be
1075 * merged without reducing the accuracy of datarace detection. Furthermore
1076 * it is also proven that the total number of all segments will never grow
1077 * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged
1078 * every time a new segment is created. The property equiv(s1, s2) is defined
1079 * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector
1080 * clocks of segments s and s1 are ordered in the same way as those of segments
1081 * s and s2. The set CS is defined as the set of existing segments s that have
1082 * the potential to conflict with not yet created segments, either because the
1083 * segment s is the latest segment of a thread or because it can become the
1084 * immediate predecessor of a new segment due to a synchronization operation.
barta9c37392008-03-22 09:38:48 +00001085 */
1086static void thread_merge_segments(void)
1087{
bartbedfd232009-03-26 19:07:15 +00001088 unsigned i;
barta9c37392008-03-22 09:38:48 +00001089
bart8f822af2009-06-08 18:20:42 +00001090 s_new_segments_since_last_merge = 0;
1091
1092 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001093 {
1094 Segment* sg;
barta9c37392008-03-22 09:38:48 +00001095
bart8f822af2009-06-08 18:20:42 +00001096#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1097 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1098#endif
barta9c37392008-03-22 09:38:48 +00001099
bart91b7ec32012-01-25 20:36:27 +00001100 for (sg = DRD_(g_threadinfo)[i].sg_first; sg; sg = sg->thr_next) {
1101 if (DRD_(sg_get_refcnt)(sg) == 1 && sg->thr_next) {
1102 Segment* const sg_next = sg->thr_next;
barte278ab52012-01-24 18:28:55 +00001103 if (DRD_(sg_get_refcnt)(sg_next) == 1
bart91b7ec32012-01-25 20:36:27 +00001104 && sg_next->thr_next
barte278ab52012-01-24 18:28:55 +00001105 && thread_consistent_segment_ordering(i, sg, sg_next))
1106 {
1107 /* Merge sg and sg_next into sg. */
1108 DRD_(sg_merge)(sg, sg_next);
1109 thread_discard_segment(i, sg_next);
1110 }
bartbedfd232009-03-26 19:07:15 +00001111 }
barta9c37392008-03-22 09:38:48 +00001112 }
barta9c37392008-03-22 09:38:48 +00001113
bart8f822af2009-06-08 18:20:42 +00001114#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1115 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1116#endif
bartbedfd232009-03-26 19:07:15 +00001117 }
barta9c37392008-03-22 09:38:48 +00001118}
1119
bart324a23b2009-02-15 12:14:52 +00001120/**
bart324a23b2009-02-15 12:14:52 +00001121 * Create a new segment for the specified thread, and discard any segments
1122 * that cannot cause races anymore.
sewardjaf44c822007-11-25 14:01:38 +00001123 */
bart62a784c2009-02-15 13:11:14 +00001124void DRD_(thread_new_segment)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001125{
bart8f822af2009-06-08 18:20:42 +00001126 Segment* last_sg;
bartbedfd232009-03-26 19:07:15 +00001127 Segment* new_sg;
bartd66e3a82008-04-06 15:02:17 +00001128
bartbedfd232009-03-26 19:07:15 +00001129 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1130 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +00001131 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001132
bart91b7ec32012-01-25 20:36:27 +00001133 last_sg = DRD_(g_threadinfo)[tid].sg_last;
bartbedfd232009-03-26 19:07:15 +00001134 new_sg = DRD_(sg_new)(tid, tid);
1135 thread_append_segment(tid, new_sg);
bart8f822af2009-06-08 18:20:42 +00001136 if (tid == DRD_(g_drd_running_tid) && last_sg)
barte5214662009-06-21 11:51:23 +00001137 {
bart8f822af2009-06-08 18:20:42 +00001138 DRD_(thread_update_conflict_set)(tid, &last_sg->vc);
barte5214662009-06-21 11:51:23 +00001139 s_update_conflict_set_new_sg_count++;
1140 }
bartd66e3a82008-04-06 15:02:17 +00001141
bart8f822af2009-06-08 18:20:42 +00001142 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001143
bart8f822af2009-06-08 18:20:42 +00001144 if (s_segment_merging
1145 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
bartbedfd232009-03-26 19:07:15 +00001146 {
bart8f822af2009-06-08 18:20:42 +00001147 thread_discard_ordered_segments();
bartbedfd232009-03-26 19:07:15 +00001148 thread_merge_segments();
1149 }
sewardjaf44c822007-11-25 14:01:38 +00001150}
1151
bart26f73e12008-02-24 18:37:08 +00001152/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart8f822af2009-06-08 18:20:42 +00001153void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +00001154{
bartbedfd232009-03-26 19:07:15 +00001155 tl_assert(joiner != joinee);
1156 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
1157 && joiner != DRD_INVALID_THREADID);
1158 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
1159 && joinee != DRD_INVALID_THREADID);
bart91b7ec32012-01-25 20:36:27 +00001160 tl_assert(DRD_(g_threadinfo)[joiner].sg_first);
1161 tl_assert(DRD_(g_threadinfo)[joiner].sg_last);
1162 tl_assert(DRD_(g_threadinfo)[joinee].sg_first);
1163 tl_assert(DRD_(g_threadinfo)[joinee].sg_last);
bart8f822af2009-06-08 18:20:42 +00001164
1165 if (DRD_(sg_get_trace)())
1166 {
florian19f91bb2012-11-10 22:29:54 +00001167 HChar *str1, *str2;
bartc6bf1842012-01-22 08:40:42 +00001168 str1 = DRD_(vc_aprint)(DRD_(thread_get_vc)(joiner));
1169 str2 = DRD_(vc_aprint)(DRD_(thread_get_vc)(joinee));
sewardj1e29ebc2009-07-15 14:49:17 +00001170 VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n",
bart8f822af2009-06-08 18:20:42 +00001171 str1, str2);
1172 VG_(free)(str1);
1173 VG_(free)(str2);
1174 }
bartae37e6d2012-01-22 08:58:31 +00001175 if (joiner == DRD_(g_drd_running_tid)) {
barte5214662009-06-21 11:51:23 +00001176 VectorClock old_vc;
1177
bartc6bf1842012-01-22 08:40:42 +00001178 DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(joiner));
1179 DRD_(vc_combine)(DRD_(thread_get_vc)(joiner),
1180 DRD_(thread_get_vc)(joinee));
barte5214662009-06-21 11:51:23 +00001181 DRD_(thread_update_conflict_set)(joiner, &old_vc);
1182 s_update_conflict_set_join_count++;
1183 DRD_(vc_cleanup)(&old_vc);
bartae37e6d2012-01-22 08:58:31 +00001184 } else {
bartc6bf1842012-01-22 08:40:42 +00001185 DRD_(vc_combine)(DRD_(thread_get_vc)(joiner),
1186 DRD_(thread_get_vc)(joinee));
barte5214662009-06-21 11:51:23 +00001187 }
1188
1189 thread_discard_ordered_segments();
1190
bartae37e6d2012-01-22 08:58:31 +00001191 if (DRD_(sg_get_trace)()) {
florian19f91bb2012-11-10 22:29:54 +00001192 HChar* str;
bartae37e6d2012-01-22 08:58:31 +00001193
bartc6bf1842012-01-22 08:40:42 +00001194 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(joiner));
sewardj1e29ebc2009-07-15 14:49:17 +00001195 VG_(message)(Vg_DebugMsg, "After join: %s\n", str);
bart8f822af2009-06-08 18:20:42 +00001196 VG_(free)(str);
1197 }
sewardjaf44c822007-11-25 14:01:38 +00001198}
1199
bart324a23b2009-02-15 12:14:52 +00001200/**
bart8f822af2009-06-08 18:20:42 +00001201 * Update the vector clock of the last segment of thread tid with the
bartf6ec1fe2009-06-21 18:07:35 +00001202 * the vector clock of segment sg.
bart26f73e12008-02-24 18:37:08 +00001203 */
bartf6ec1fe2009-06-21 18:07:35 +00001204static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg)
sewardjaf44c822007-11-25 14:01:38 +00001205{
bart8f822af2009-06-08 18:20:42 +00001206 const VectorClock* const vc = &sg->vc;
1207
bartbedfd232009-03-26 19:07:15 +00001208 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1209 && tid != DRD_INVALID_THREADID);
bart91b7ec32012-01-25 20:36:27 +00001210 tl_assert(DRD_(g_threadinfo)[tid].sg_first);
1211 tl_assert(DRD_(g_threadinfo)[tid].sg_last);
bart8f822af2009-06-08 18:20:42 +00001212 tl_assert(sg);
bartbedfd232009-03-26 19:07:15 +00001213 tl_assert(vc);
bart8f822af2009-06-08 18:20:42 +00001214
bartae37e6d2012-01-22 08:58:31 +00001215 if (tid != sg->tid) {
bart8f822af2009-06-08 18:20:42 +00001216 VectorClock old_vc;
1217
bartc6bf1842012-01-22 08:40:42 +00001218 DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(tid));
1219 DRD_(vc_combine)(DRD_(thread_get_vc)(tid), vc);
bartae37e6d2012-01-22 08:58:31 +00001220 if (DRD_(sg_get_trace)()) {
florian19f91bb2012-11-10 22:29:54 +00001221 HChar *str1, *str2;
bart8f822af2009-06-08 18:20:42 +00001222 str1 = DRD_(vc_aprint)(&old_vc);
bartc6bf1842012-01-22 08:40:42 +00001223 str2 = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
florianea71ffb2015-08-05 14:38:57 +00001224 VG_(message)(Vg_DebugMsg, "thread %u: vc %s -> %s\n", tid, str1, str2);
bart8f822af2009-06-08 18:20:42 +00001225 VG_(free)(str1);
1226 VG_(free)(str2);
1227 }
barte5214662009-06-21 11:51:23 +00001228
bart8f822af2009-06-08 18:20:42 +00001229 thread_discard_ordered_segments();
barte5214662009-06-21 11:51:23 +00001230
bart8f822af2009-06-08 18:20:42 +00001231 DRD_(thread_update_conflict_set)(tid, &old_vc);
barte5214662009-06-21 11:51:23 +00001232 s_update_conflict_set_sync_count++;
1233
bart8f822af2009-06-08 18:20:42 +00001234 DRD_(vc_cleanup)(&old_vc);
bartae37e6d2012-01-22 08:58:31 +00001235 } else {
bartc6bf1842012-01-22 08:40:42 +00001236 tl_assert(DRD_(vc_lte)(vc, DRD_(thread_get_vc)(tid)));
bart8f822af2009-06-08 18:20:42 +00001237 }
sewardjaf44c822007-11-25 14:01:38 +00001238}
1239
bart324a23b2009-02-15 12:14:52 +00001240/**
bartf6ec1fe2009-06-21 18:07:35 +00001241 * Create a new segment for thread tid and update the vector clock of the last
florianad4e9792015-07-05 21:53:33 +00001242 * segment of this thread with the vector clock of segment sg. Call this
bartf6ec1fe2009-06-21 18:07:35 +00001243 * function after thread tid had to wait because of thread synchronization
1244 * until the memory accesses in the segment sg finished.
1245 */
1246void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, const Segment* sg)
1247{
1248 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1249 && tid != DRD_INVALID_THREADID);
1250 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1251 tl_assert(sg);
1252
1253 thread_append_segment(tid, DRD_(sg_new)(tid, tid));
1254
1255 thread_combine_vc_sync(tid, sg);
1256
1257 if (s_segment_merging
1258 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
1259 {
1260 thread_discard_ordered_segments();
1261 thread_merge_segments();
1262 }
1263}
1264
1265/**
bart324a23b2009-02-15 12:14:52 +00001266 * Call this function whenever a thread is no longer using the memory
1267 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
1268 * increase.
bart26f73e12008-02-24 18:37:08 +00001269 */
bart23ef19d2011-03-12 12:34:44 +00001270void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +00001271{
bart178b6862011-07-29 12:30:43 +00001272 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001273
bart91b7ec32012-01-25 20:36:27 +00001274 for (p = DRD_(g_sg_list); p; p = p->g_next)
barte278ab52012-01-24 18:28:55 +00001275 DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001276
bart178b6862011-07-29 12:30:43 +00001277 DRD_(bm_clear)(DRD_(g_conflict_set), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001278}
1279
bartd45d9952009-05-31 18:53:54 +00001280/** Specify whether memory loads should be recorded. */
1281void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001282{
bartbedfd232009-03-26 19:07:15 +00001283 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1284 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001285 tl_assert(enabled == !! enabled);
1286
1287 DRD_(g_threadinfo)[tid].is_recording_loads = enabled;
bart0268dfa2008-03-11 20:10:21 +00001288}
1289
bartd45d9952009-05-31 18:53:54 +00001290/** Specify whether memory stores should be recorded. */
1291void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001292{
bartbedfd232009-03-26 19:07:15 +00001293 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1294 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001295 tl_assert(enabled == !! enabled);
1296
1297 DRD_(g_threadinfo)[tid].is_recording_stores = enabled;
bart0268dfa2008-03-11 20:10:21 +00001298}
1299
bart86a87df2009-03-04 19:26:47 +00001300/**
1301 * Print the segment information for all threads.
1302 *
1303 * This function is only used for debugging purposes.
1304 */
bart62a784c2009-02-15 13:11:14 +00001305void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +00001306{
florianea71ffb2015-08-05 14:38:57 +00001307 UInt i;
bartbedfd232009-03-26 19:07:15 +00001308 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001309
bart8f822af2009-06-08 18:20:42 +00001310 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001311 {
bart91b7ec32012-01-25 20:36:27 +00001312 p = DRD_(g_threadinfo)[i].sg_first;
1313 if (p) {
bartbedfd232009-03-26 19:07:15 +00001314 VG_(printf)("**************\n"
florianea71ffb2015-08-05 14:38:57 +00001315 "* thread %3u (%d/%u/%u/%u/0x%lx/%d) *\n"
bartbedfd232009-03-26 19:07:15 +00001316 "**************\n",
1317 i,
bart6d956dc2011-07-28 09:54:37 +00001318 DRD_(g_threadinfo)[i].valid,
bartbedfd232009-03-26 19:07:15 +00001319 DRD_(g_threadinfo)[i].vg_thread_exists,
1320 DRD_(g_threadinfo)[i].vg_threadid,
1321 DRD_(g_threadinfo)[i].posix_thread_exists,
1322 DRD_(g_threadinfo)[i].pt_threadid,
1323 DRD_(g_threadinfo)[i].detached_posix_thread);
bart91b7ec32012-01-25 20:36:27 +00001324 for ( ; p; p = p->thr_next)
bartbedfd232009-03-26 19:07:15 +00001325 DRD_(sg_print)(p);
sewardjaf44c822007-11-25 14:01:38 +00001326 }
bartbedfd232009-03-26 19:07:15 +00001327 }
sewardjaf44c822007-11-25 14:01:38 +00001328}
1329
bart86a87df2009-03-04 19:26:47 +00001330/** Show a call stack involved in a data race. */
barte7086002011-10-11 19:08:39 +00001331static void show_call_stack(const DrdThreadId tid, ExeContext* const callstack)
sewardjaf44c822007-11-25 14:01:38 +00001332{
bartbedfd232009-03-26 19:07:15 +00001333 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +00001334
barte7086002011-10-11 19:08:39 +00001335 if (vg_tid != VG_INVALID_THREADID) {
bartbedfd232009-03-26 19:07:15 +00001336 if (callstack)
bartbedfd232009-03-26 19:07:15 +00001337 VG_(pp_ExeContext)(callstack);
bartbedfd232009-03-26 19:07:15 +00001338 else
bartbedfd232009-03-26 19:07:15 +00001339 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
barte7086002011-10-11 19:08:39 +00001340 } else {
1341 if (!VG_(clo_xml))
1342 VG_(message)(Vg_UserMsg,
1343 " (thread finished, call stack no longer available)\n");
bartbedfd232009-03-26 19:07:15 +00001344 }
sewardjaf44c822007-11-25 14:01:38 +00001345}
1346
bart86a87df2009-03-04 19:26:47 +00001347/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001348static void
1349thread_report_conflicting_segments_segment(const DrdThreadId tid,
1350 const Addr addr,
1351 const SizeT size,
1352 const BmAccessTypeT access_type,
1353 const Segment* const p)
1354{
bartbedfd232009-03-26 19:07:15 +00001355 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001356
bartbedfd232009-03-26 19:07:15 +00001357 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1358 && tid != DRD_INVALID_THREADID);
1359 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +00001360
bart91b7ec32012-01-25 20:36:27 +00001361 for (i = 0; i < DRD_N_THREADS; i++) {
1362 if (i != tid) {
bartbedfd232009-03-26 19:07:15 +00001363 Segment* q;
barte278ab52012-01-24 18:28:55 +00001364
bart91b7ec32012-01-25 20:36:27 +00001365 for (q = DRD_(g_threadinfo)[i].sg_last; q; q = q->thr_prev) {
bartbedfd232009-03-26 19:07:15 +00001366 /*
bart31b983d2010-02-21 14:52:59 +00001367 * Since q iterates over the segments of thread i in order of
1368 * decreasing vector clocks, if q->vc <= p->vc, then
bartbedfd232009-03-26 19:07:15 +00001369 * q->next->vc <= p->vc will also hold. Hence, break out of the
1370 * loop once this condition is met.
1371 */
1372 if (DRD_(vc_lte)(&q->vc, &p->vc))
1373 break;
bart91b7ec32012-01-25 20:36:27 +00001374 if (!DRD_(vc_lte)(&p->vc, &q->vc)) {
bart8f822af2009-06-08 18:20:42 +00001375 if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size,
bart91b7ec32012-01-25 20:36:27 +00001376 access_type)) {
barte278ab52012-01-24 18:28:55 +00001377 Segment* q_next;
1378
bartbedfd232009-03-26 19:07:15 +00001379 tl_assert(q->stacktrace);
barte7086002011-10-11 19:08:39 +00001380 if (VG_(clo_xml))
1381 VG_(printf_xml)(" <other_segment_start>\n");
1382 else
1383 VG_(message)(Vg_UserMsg,
florianea71ffb2015-08-05 14:38:57 +00001384 "Other segment start (thread %u)\n", i);
barte7086002011-10-11 19:08:39 +00001385 show_call_stack(i, q->stacktrace);
1386 if (VG_(clo_xml))
1387 VG_(printf_xml)(" </other_segment_start>\n"
1388 " <other_segment_end>\n");
1389 else
1390 VG_(message)(Vg_UserMsg,
florianea71ffb2015-08-05 14:38:57 +00001391 "Other segment end (thread %u)\n", i);
bart91b7ec32012-01-25 20:36:27 +00001392 q_next = q->thr_next;
barte278ab52012-01-24 18:28:55 +00001393 show_call_stack(i, q_next ? q_next->stacktrace : 0);
barte7086002011-10-11 19:08:39 +00001394 if (VG_(clo_xml))
1395 VG_(printf_xml)(" </other_segment_end>\n");
bartbedfd232009-03-26 19:07:15 +00001396 }
1397 }
1398 }
sewardjaf44c822007-11-25 14:01:38 +00001399 }
bartbedfd232009-03-26 19:07:15 +00001400 }
sewardjaf44c822007-11-25 14:01:38 +00001401}
1402
bart86a87df2009-03-04 19:26:47 +00001403/** Print information about all segments involved in a data race. */
bart62a784c2009-02-15 13:11:14 +00001404void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1405 const Addr addr,
1406 const SizeT size,
1407 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +00001408{
bartbedfd232009-03-26 19:07:15 +00001409 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001410
bartbedfd232009-03-26 19:07:15 +00001411 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1412 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001413
bart91b7ec32012-01-25 20:36:27 +00001414 for (p = DRD_(g_threadinfo)[tid].sg_first; p; p = p->thr_next) {
bart8f822af2009-06-08 18:20:42 +00001415 if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type))
bartbedfd232009-03-26 19:07:15 +00001416 thread_report_conflicting_segments_segment(tid, addr, size,
1417 access_type, p);
bartbedfd232009-03-26 19:07:15 +00001418 }
sewardjaf44c822007-11-25 14:01:38 +00001419}
sewardjaf44c822007-11-25 14:01:38 +00001420
bart324a23b2009-02-15 12:14:52 +00001421/**
bart8f822af2009-06-08 18:20:42 +00001422 * Verify whether the conflict set for thread tid is up to date. Only perform
1423 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
1424 */
1425static Bool thread_conflict_set_up_to_date(const DrdThreadId tid)
1426{
bart8f822af2009-06-08 18:20:42 +00001427 Bool result;
1428 struct bitmap* computed_conflict_set = 0;
1429
bart9cdc0832014-08-09 12:58:17 +00001430 if (!DRD_(verify_conflict_set))
bart8f822af2009-06-08 18:20:42 +00001431 return True;
1432
1433 thread_compute_conflict_set(&computed_conflict_set, tid);
1434 result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set);
1435 if (! result)
1436 {
1437 VG_(printf)("actual conflict set:\n");
1438 DRD_(bm_print)(DRD_(g_conflict_set));
1439 VG_(printf)("\n");
1440 VG_(printf)("computed conflict set:\n");
1441 DRD_(bm_print)(computed_conflict_set);
1442 VG_(printf)("\n");
1443 }
1444 DRD_(bm_delete)(computed_conflict_set);
1445 return result;
1446}
1447
1448/**
1449 * Compute the conflict set: a bitmap that represents the union of all memory
1450 * accesses of all segments that are unordered to the current segment of the
1451 * thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001452 */
bart86a87df2009-03-04 19:26:47 +00001453static void thread_compute_conflict_set(struct bitmap** conflict_set,
1454 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001455{
bartbedfd232009-03-26 19:07:15 +00001456 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001457
bartbedfd232009-03-26 19:07:15 +00001458 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1459 && tid != DRD_INVALID_THREADID);
1460 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001461
bart54803fe2009-06-21 09:26:27 +00001462 s_compute_conflict_set_count++;
bartbedfd232009-03-26 19:07:15 +00001463 s_conflict_set_bitmap_creation_count
1464 -= DRD_(bm_get_bitmap_creation_count)();
1465 s_conflict_set_bitmap2_creation_count
1466 -= DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001467
bartae37e6d2012-01-22 08:58:31 +00001468 if (*conflict_set) {
bartf6ec1fe2009-06-21 18:07:35 +00001469 DRD_(bm_cleanup)(*conflict_set);
1470 DRD_(bm_init)(*conflict_set);
bartae37e6d2012-01-22 08:58:31 +00001471 } else {
bartf6ec1fe2009-06-21 18:07:35 +00001472 *conflict_set = DRD_(bm_new)();
1473 }
bart26f73e12008-02-24 18:37:08 +00001474
bartae37e6d2012-01-22 08:58:31 +00001475 if (s_trace_conflict_set) {
florian19f91bb2012-11-10 22:29:54 +00001476 HChar* str;
bart26f73e12008-02-24 18:37:08 +00001477
bartc6bf1842012-01-22 08:40:42 +00001478 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
bart8f822af2009-06-08 18:20:42 +00001479 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +00001480 "computing conflict set for thread %u with vc %s\n",
bart63c92ea2009-07-19 17:53:56 +00001481 tid, str);
bart8f822af2009-06-08 18:20:42 +00001482 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001483 }
sewardjaf44c822007-11-25 14:01:38 +00001484
bart91b7ec32012-01-25 20:36:27 +00001485 p = DRD_(g_threadinfo)[tid].sg_last;
bartbedfd232009-03-26 19:07:15 +00001486 {
1487 unsigned j;
1488
bart91b7ec32012-01-25 20:36:27 +00001489 if (s_trace_conflict_set) {
florian19f91bb2012-11-10 22:29:54 +00001490 HChar* vc;
bartbedfd232009-03-26 19:07:15 +00001491
bart8f822af2009-06-08 18:20:42 +00001492 vc = DRD_(vc_aprint)(&p->vc);
florianea71ffb2015-08-05 14:38:57 +00001493 VG_(message)(Vg_DebugMsg, "conflict set: thread [%u] at vc %s\n",
bart8f822af2009-06-08 18:20:42 +00001494 tid, vc);
1495 VG_(free)(vc);
bart26f73e12008-02-24 18:37:08 +00001496 }
sewardjaf44c822007-11-25 14:01:38 +00001497
bart91b7ec32012-01-25 20:36:27 +00001498 for (j = 0; j < DRD_N_THREADS; j++) {
1499 if (j != tid && DRD_(IsValidDrdThreadId)(j)) {
bart8f822af2009-06-08 18:20:42 +00001500 Segment* q;
bart91b7ec32012-01-25 20:36:27 +00001501
1502 for (q = DRD_(g_threadinfo)[j].sg_last; q; q = q->thr_prev) {
1503 if (!DRD_(vc_lte)(&q->vc, &p->vc)
1504 && !DRD_(vc_lte)(&p->vc, &q->vc)) {
1505 if (s_trace_conflict_set) {
florian19f91bb2012-11-10 22:29:54 +00001506 HChar* str;
bart8f822af2009-06-08 18:20:42 +00001507
1508 str = DRD_(vc_aprint)(&q->vc);
1509 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +00001510 "conflict set: [%u] merging segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001511 j, str);
1512 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001513 }
bart8f822af2009-06-08 18:20:42 +00001514 DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q));
bart91b7ec32012-01-25 20:36:27 +00001515 } else {
1516 if (s_trace_conflict_set) {
florian19f91bb2012-11-10 22:29:54 +00001517 HChar* str;
bart8f822af2009-06-08 18:20:42 +00001518
1519 str = DRD_(vc_aprint)(&q->vc);
1520 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +00001521 "conflict set: [%u] ignoring segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001522 j, str);
1523 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001524 }
1525 }
1526 }
1527 }
1528 }
1529 }
sewardjaf44c822007-11-25 14:01:38 +00001530
bartbedfd232009-03-26 19:07:15 +00001531 s_conflict_set_bitmap_creation_count
1532 += DRD_(bm_get_bitmap_creation_count)();
1533 s_conflict_set_bitmap2_creation_count
1534 += DRD_(bm_get_bitmap2_creation_count)();
1535
bart91b7ec32012-01-25 20:36:27 +00001536 if (s_trace_conflict_set_bm) {
florianea71ffb2015-08-05 14:38:57 +00001537 VG_(message)(Vg_DebugMsg, "[%u] new conflict set:\n", tid);
bartbedfd232009-03-26 19:07:15 +00001538 DRD_(bm_print)(*conflict_set);
florianea71ffb2015-08-05 14:38:57 +00001539 VG_(message)(Vg_DebugMsg, "[%u] end of new conflict set.\n", tid);
bartbedfd232009-03-26 19:07:15 +00001540 }
sewardjaf44c822007-11-25 14:01:38 +00001541}
1542
bart8f822af2009-06-08 18:20:42 +00001543/**
1544 * Update the conflict set after the vector clock of thread tid has been
1545 * updated from old_vc to its current value, either because a new segment has
1546 * been created or because of a synchronization operation.
1547 */
1548void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
1549 const VectorClock* const old_vc)
1550{
1551 const VectorClock* new_vc;
1552 Segment* p;
1553 unsigned j;
1554
1555 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1556 && tid != DRD_INVALID_THREADID);
1557 tl_assert(old_vc);
1558 tl_assert(tid == DRD_(g_drd_running_tid));
1559 tl_assert(DRD_(g_conflict_set));
1560
bartae37e6d2012-01-22 08:58:31 +00001561 if (s_trace_conflict_set) {
florian19f91bb2012-11-10 22:29:54 +00001562 HChar* str;
bart8f822af2009-06-08 18:20:42 +00001563
bartc6bf1842012-01-22 08:40:42 +00001564 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
bart8f822af2009-06-08 18:20:42 +00001565 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +00001566 "updating conflict set for thread %u with vc %s\n",
bart63c92ea2009-07-19 17:53:56 +00001567 tid, str);
bart8f822af2009-06-08 18:20:42 +00001568 VG_(free)(str);
1569 }
1570
bartc6bf1842012-01-22 08:40:42 +00001571 new_vc = DRD_(thread_get_vc)(tid);
bartf5fe4b62011-07-03 11:39:30 +00001572 tl_assert(DRD_(vc_lte)(old_vc, new_vc));
bart8f822af2009-06-08 18:20:42 +00001573
1574 DRD_(bm_unmark)(DRD_(g_conflict_set));
1575
1576 for (j = 0; j < DRD_N_THREADS; j++)
1577 {
1578 Segment* q;
1579
1580 if (j == tid || ! DRD_(IsValidDrdThreadId)(j))
1581 continue;
1582
bart91b7ec32012-01-25 20:36:27 +00001583 for (q = DRD_(g_threadinfo)[j].sg_last;
1584 q && !DRD_(vc_lte)(&q->vc, new_vc);
1585 q = q->thr_prev) {
1586 const Bool included_in_old_conflict_set
1587 = !DRD_(vc_lte)(old_vc, &q->vc);
1588 const Bool included_in_new_conflict_set
1589 = !DRD_(vc_lte)(new_vc, &q->vc);
bart178b6862011-07-29 12:30:43 +00001590
1591 if (UNLIKELY(s_trace_conflict_set)) {
florian19f91bb2012-11-10 22:29:54 +00001592 HChar* str;
bart178b6862011-07-29 12:30:43 +00001593
1594 str = DRD_(vc_aprint)(&q->vc);
1595 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +00001596 "conflict set: [%u] %s segment %s\n", j,
bart178b6862011-07-29 12:30:43 +00001597 included_in_old_conflict_set
1598 != included_in_new_conflict_set
1599 ? "merging" : "ignoring", str);
1600 VG_(free)(str);
1601 }
1602 if (included_in_old_conflict_set != included_in_new_conflict_set)
1603 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1604 }
1605
bart91b7ec32012-01-25 20:36:27 +00001606 for ( ; q && !DRD_(vc_lte)(&q->vc, old_vc); q = q->thr_prev) {
1607 const Bool included_in_old_conflict_set
1608 = !DRD_(vc_lte)(old_vc, &q->vc);
1609 const Bool included_in_new_conflict_set
1610 = !DRD_(vc_lte)(&q->vc, new_vc)
1611 && !DRD_(vc_lte)(new_vc, &q->vc);
bart178b6862011-07-29 12:30:43 +00001612
1613 if (UNLIKELY(s_trace_conflict_set)) {
florian19f91bb2012-11-10 22:29:54 +00001614 HChar* str;
bartac5b95b2011-07-03 11:43:45 +00001615
1616 str = DRD_(vc_aprint)(&q->vc);
1617 VG_(message)(Vg_DebugMsg,
florianea71ffb2015-08-05 14:38:57 +00001618 "conflict set: [%u] %s segment %s\n", j,
bartac5b95b2011-07-03 11:43:45 +00001619 included_in_old_conflict_set
1620 != included_in_new_conflict_set
1621 ? "merging" : "ignoring", str);
1622 VG_(free)(str);
1623 }
bart8f822af2009-06-08 18:20:42 +00001624 if (included_in_old_conflict_set != included_in_new_conflict_set)
bart8f822af2009-06-08 18:20:42 +00001625 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
bart8f822af2009-06-08 18:20:42 +00001626 }
1627 }
1628
1629 DRD_(bm_clear_marked)(DRD_(g_conflict_set));
1630
bart91b7ec32012-01-25 20:36:27 +00001631 p = DRD_(g_threadinfo)[tid].sg_last;
1632 for (j = 0; j < DRD_N_THREADS; j++) {
1633 if (j != tid && DRD_(IsValidDrdThreadId)(j)) {
bart4b3fdb22011-07-03 11:40:49 +00001634 Segment* q;
bart91b7ec32012-01-25 20:36:27 +00001635 for (q = DRD_(g_threadinfo)[j].sg_last;
1636 q && !DRD_(vc_lte)(&q->vc, &p->vc);
1637 q = q->thr_prev) {
bart178b6862011-07-29 12:30:43 +00001638 if (!DRD_(vc_lte)(&p->vc, &q->vc))
bart4b3fdb22011-07-03 11:40:49 +00001639 DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
bart8f822af2009-06-08 18:20:42 +00001640 }
1641 }
1642 }
1643
1644 DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set));
1645
bart54803fe2009-06-21 09:26:27 +00001646 s_update_conflict_set_count++;
bart8f822af2009-06-08 18:20:42 +00001647
1648 if (s_trace_conflict_set_bm)
1649 {
florianea71ffb2015-08-05 14:38:57 +00001650 VG_(message)(Vg_DebugMsg, "[%u] updated conflict set:\n", tid);
bart8f822af2009-06-08 18:20:42 +00001651 DRD_(bm_print)(DRD_(g_conflict_set));
florianea71ffb2015-08-05 14:38:57 +00001652 VG_(message)(Vg_DebugMsg, "[%u] end of updated conflict set.\n", tid);
bart8f822af2009-06-08 18:20:42 +00001653 }
1654
1655 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1656}
1657
bart86a87df2009-03-04 19:26:47 +00001658/** Report the number of context switches performed. */
bart62a784c2009-02-15 13:11:14 +00001659ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001660{
bartbedfd232009-03-26 19:07:15 +00001661 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +00001662}
1663
bart86a87df2009-03-04 19:26:47 +00001664/** Report the number of ordered segments that have been discarded. */
bart62a784c2009-02-15 13:11:14 +00001665ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001666{
bartbedfd232009-03-26 19:07:15 +00001667 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +00001668}
1669
bart54803fe2009-06-21 09:26:27 +00001670/** Return how many times the conflict set has been updated entirely. */
1671ULong DRD_(thread_get_compute_conflict_set_count)()
sewardjaf44c822007-11-25 14:01:38 +00001672{
bart54803fe2009-06-21 09:26:27 +00001673 return s_compute_conflict_set_count;
1674}
1675
1676/** Return how many times the conflict set has been updated partially. */
1677ULong DRD_(thread_get_update_conflict_set_count)(void)
1678{
bartbedfd232009-03-26 19:07:15 +00001679 return s_update_conflict_set_count;
sewardjaf44c822007-11-25 14:01:38 +00001680}
1681
bart86a87df2009-03-04 19:26:47 +00001682/**
barte5214662009-06-21 11:51:23 +00001683 * Return how many times the conflict set has been updated partially
1684 * because a new segment has been created.
1685 */
1686ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void)
1687{
1688 return s_update_conflict_set_new_sg_count;
1689}
1690
1691/**
1692 * Return how many times the conflict set has been updated partially
1693 * because of combining vector clocks due to synchronization operations
1694 * other than reader/writer lock or barrier operations.
1695 */
1696ULong DRD_(thread_get_update_conflict_set_sync_count)(void)
1697{
1698 return s_update_conflict_set_sync_count;
1699}
1700
1701/**
1702 * Return how many times the conflict set has been updated partially
1703 * because of thread joins.
1704 */
1705ULong DRD_(thread_get_update_conflict_set_join_count)(void)
1706{
1707 return s_update_conflict_set_join_count;
1708}
1709
1710/**
bart86a87df2009-03-04 19:26:47 +00001711 * Return the number of first-level bitmaps that have been created during
1712 * conflict set updates.
1713 */
bart62a784c2009-02-15 13:11:14 +00001714ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001715{
bartbedfd232009-03-26 19:07:15 +00001716 return s_conflict_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001717}
1718
bart86a87df2009-03-04 19:26:47 +00001719/**
1720 * Return the number of second-level bitmaps that have been created during
1721 * conflict set updates.
1722 */
bart62a784c2009-02-15 13:11:14 +00001723ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001724{
bartbedfd232009-03-26 19:07:15 +00001725 return s_conflict_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001726}