blob: 6e765ce2e42e6840df49d60149086d083a4df5a5 [file] [log] [blame]
bartbedfd232009-03-26 19:07:15 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
sewardjaf44c822007-11-25 14:01:38 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00004
bart86562bd2009-02-16 19:43:56 +00005 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
sewardjaf44c822007-11-25 14:01:38 +00006
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
26#include "drd_error.h"
bart09dc13f2009-02-14 15:13:31 +000027#include "drd_barrier.h"
bartd2c5eae2009-02-21 15:27:04 +000028#include "drd_clientobj.h"
bart09dc13f2009-02-14 15:13:31 +000029#include "drd_cond.h"
30#include "drd_mutex.h"
sewardjaf44c822007-11-25 14:01:38 +000031#include "drd_segment.h"
bart09dc13f2009-02-14 15:13:31 +000032#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000033#include "drd_suppression.h"
34#include "drd_thread.h"
bart82195c12008-04-13 17:35:08 +000035#include "pub_tool_vki.h"
sewardjaf44c822007-11-25 14:01:38 +000036#include "pub_tool_basics.h" // Addr, SizeT
37#include "pub_tool_errormgr.h" // VG_(unique_error)()
38#include "pub_tool_libcassert.h" // tl_assert()
39#include "pub_tool_libcbase.h" // VG_(strlen)()
40#include "pub_tool_libcprint.h" // VG_(printf)()
bart82195c12008-04-13 17:35:08 +000041#include "pub_tool_libcproc.h" // VG_(getenv)()
sewardjaf44c822007-11-25 14:01:38 +000042#include "pub_tool_machine.h"
43#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardj85642922008-01-14 11:54:56 +000044#include "pub_tool_options.h" // VG_(clo_backtrace_size)
sewardjaf44c822007-11-25 14:01:38 +000045#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
46
bart32ba2082008-06-05 08:53:42 +000047
sewardjaf44c822007-11-25 14:01:38 +000048
bart324a23b2009-02-15 12:14:52 +000049/* Local functions. */
sewardjaf44c822007-11-25 14:01:38 +000050
bart86a87df2009-03-04 19:26:47 +000051static void thread_append_segment(const DrdThreadId tid, Segment* const sg);
52static void thread_discard_segment(const DrdThreadId tid, Segment* const sg);
53static void thread_compute_conflict_set(struct bitmap** conflict_set,
54 const DrdThreadId tid);
bart8f822af2009-06-08 18:20:42 +000055static Bool thread_conflict_set_up_to_date(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +000056
57
bart324a23b2009-02-15 12:14:52 +000058/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000059
bart86a87df2009-03-04 19:26:47 +000060static ULong s_context_switch_count;
61static ULong s_discard_ordered_segments_count;
bart54803fe2009-06-21 09:26:27 +000062static ULong s_compute_conflict_set_count;
bart86a87df2009-03-04 19:26:47 +000063static ULong s_update_conflict_set_count;
barte5214662009-06-21 11:51:23 +000064static ULong s_update_conflict_set_new_sg_count;
65static ULong s_update_conflict_set_sync_count;
66static ULong s_update_conflict_set_join_count;
bart86a87df2009-03-04 19:26:47 +000067static ULong s_conflict_set_bitmap_creation_count;
68static ULong s_conflict_set_bitmap2_creation_count;
69static ThreadId s_vg_running_tid = VG_INVALID_THREADID;
bart324a23b2009-02-15 12:14:52 +000070DrdThreadId DRD_(g_drd_running_tid) = DRD_INVALID_THREADID;
71ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS];
72struct bitmap* DRD_(g_conflict_set);
bart86a87df2009-03-04 19:26:47 +000073static Bool s_trace_context_switches = False;
74static Bool s_trace_conflict_set = False;
bart8f822af2009-06-08 18:20:42 +000075static Bool s_trace_conflict_set_bm = False;
bart86a87df2009-03-04 19:26:47 +000076static Bool s_trace_fork_join = False;
77static Bool s_segment_merging = True;
bart8f822af2009-06-08 18:20:42 +000078static Bool s_new_segments_since_last_merge;
bart6f1d7162009-06-24 18:34:10 +000079static int s_segment_merge_interval = 10;
sewardjaf44c822007-11-25 14:01:38 +000080
81
bart324a23b2009-02-15 12:14:52 +000082/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000083
bart86a87df2009-03-04 19:26:47 +000084/** Enables/disables context switch tracing. */
bart62a784c2009-02-15 13:11:14 +000085void DRD_(thread_trace_context_switches)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000086{
bartbedfd232009-03-26 19:07:15 +000087 tl_assert(t == False || t == True);
88 s_trace_context_switches = t;
bart26f73e12008-02-24 18:37:08 +000089}
90
bart86a87df2009-03-04 19:26:47 +000091/** Enables/disables conflict set tracing. */
bart62a784c2009-02-15 13:11:14 +000092void DRD_(thread_trace_conflict_set)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000093{
bartbedfd232009-03-26 19:07:15 +000094 tl_assert(t == False || t == True);
95 s_trace_conflict_set = t;
bart26f73e12008-02-24 18:37:08 +000096}
97
bart8f822af2009-06-08 18:20:42 +000098/** Enables/disables conflict set bitmap tracing. */
99void DRD_(thread_trace_conflict_set_bm)(const Bool t)
100{
101 tl_assert(t == False || t == True);
102 s_trace_conflict_set_bm = t;
103}
104
bart86a87df2009-03-04 19:26:47 +0000105/** Report whether fork/join tracing is enabled. */
bart09dc13f2009-02-14 15:13:31 +0000106Bool DRD_(thread_get_trace_fork_join)(void)
107{
bartbedfd232009-03-26 19:07:15 +0000108 return s_trace_fork_join;
bart09dc13f2009-02-14 15:13:31 +0000109}
110
bart86a87df2009-03-04 19:26:47 +0000111/** Enables/disables fork/join tracing. */
bart09dc13f2009-02-14 15:13:31 +0000112void DRD_(thread_set_trace_fork_join)(const Bool t)
113{
bartbedfd232009-03-26 19:07:15 +0000114 tl_assert(t == False || t == True);
115 s_trace_fork_join = t;
bart09dc13f2009-02-14 15:13:31 +0000116}
117
bart86a87df2009-03-04 19:26:47 +0000118/** Enables/disables segment merging. */
bart62a784c2009-02-15 13:11:14 +0000119void DRD_(thread_set_segment_merging)(const Bool m)
barta9c37392008-03-22 09:38:48 +0000120{
bartbedfd232009-03-26 19:07:15 +0000121 tl_assert(m == False || m == True);
122 s_segment_merging = m;
barta9c37392008-03-22 09:38:48 +0000123}
124
bart8f822af2009-06-08 18:20:42 +0000125/** Get the segment merging interval. */
126int DRD_(thread_get_segment_merge_interval)(void)
127{
128 return s_segment_merge_interval;
129}
130
131/** Set the segment merging interval. */
132void DRD_(thread_set_segment_merge_interval)(const int i)
133{
134 s_segment_merge_interval = i;
135}
136
sewardjaf44c822007-11-25 14:01:38 +0000137/**
bart86a87df2009-03-04 19:26:47 +0000138 * Convert Valgrind's ThreadId into a DrdThreadId.
139 *
140 * @return DRD thread ID upon success and DRD_INVALID_THREADID if the passed
141 * Valgrind ThreadId does not yet exist.
bart324a23b2009-02-15 12:14:52 +0000142 */
bart62a784c2009-02-15 13:11:14 +0000143DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000144{
bartbedfd232009-03-26 19:07:15 +0000145 int i;
sewardjaf44c822007-11-25 14:01:38 +0000146
bartbedfd232009-03-26 19:07:15 +0000147 if (tid == VG_INVALID_THREADID)
148 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000149
bartbedfd232009-03-26 19:07:15 +0000150 for (i = 1; i < DRD_N_THREADS; i++)
151 {
152 if (DRD_(g_threadinfo)[i].vg_thread_exists == True
153 && DRD_(g_threadinfo)[i].vg_threadid == tid)
154 {
155 return i;
156 }
157 }
sewardjaf44c822007-11-25 14:01:38 +0000158
bartbedfd232009-03-26 19:07:15 +0000159 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000160}
161
bart86a87df2009-03-04 19:26:47 +0000162/** Allocate a new DRD thread ID for the specified Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000163static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000164{
bartbedfd232009-03-26 19:07:15 +0000165 int i;
sewardjaf44c822007-11-25 14:01:38 +0000166
bartbedfd232009-03-26 19:07:15 +0000167 tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000168
bartbedfd232009-03-26 19:07:15 +0000169 for (i = 1; i < DRD_N_THREADS; i++)
170 {
171 if (DRD_(g_threadinfo)[i].vg_thread_exists == False
172 && DRD_(g_threadinfo)[i].posix_thread_exists == False
173 && DRD_(g_threadinfo)[i].detached_posix_thread == False)
174 {
175 tl_assert(! DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000176
bartbedfd232009-03-26 19:07:15 +0000177 DRD_(g_threadinfo)[i].vg_thread_exists = True;
178 DRD_(g_threadinfo)[i].vg_threadid = tid;
179 DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID;
180 DRD_(g_threadinfo)[i].stack_min = 0;
181 DRD_(g_threadinfo)[i].stack_min_min = 0;
182 DRD_(g_threadinfo)[i].stack_startup = 0;
183 DRD_(g_threadinfo)[i].stack_max = 0;
bartd45d9952009-05-31 18:53:54 +0000184 DRD_(thread_set_name)(i, "");
185 DRD_(g_threadinfo)[i].is_recording_loads = True;
186 DRD_(g_threadinfo)[i].is_recording_stores = True;
bartbedfd232009-03-26 19:07:15 +0000187 DRD_(g_threadinfo)[i].synchr_nesting = 0;
188 tl_assert(DRD_(g_threadinfo)[i].first == 0);
189 tl_assert(DRD_(g_threadinfo)[i].last == 0);
bart86a87df2009-03-04 19:26:47 +0000190
bartbedfd232009-03-26 19:07:15 +0000191 tl_assert(DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000192
bartbedfd232009-03-26 19:07:15 +0000193 return i;
194 }
195 }
sewardjaf44c822007-11-25 14:01:38 +0000196
bartbedfd232009-03-26 19:07:15 +0000197 tl_assert(False);
sewardjaf44c822007-11-25 14:01:38 +0000198
bartbedfd232009-03-26 19:07:15 +0000199 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000200}
201
bart86a87df2009-03-04 19:26:47 +0000202/** Convert a POSIX thread ID into a DRD thread ID. */
bart62a784c2009-02-15 13:11:14 +0000203DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000204{
bartbedfd232009-03-26 19:07:15 +0000205 int i;
sewardjaf44c822007-11-25 14:01:38 +0000206
bartbedfd232009-03-26 19:07:15 +0000207 tl_assert(tid != INVALID_POSIX_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000208
bartbedfd232009-03-26 19:07:15 +0000209 for (i = 1; i < DRD_N_THREADS; i++)
210 {
211 if (DRD_(g_threadinfo)[i].posix_thread_exists
212 && DRD_(g_threadinfo)[i].pt_threadid == tid)
213 {
214 return i;
215 }
216 }
217 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000218}
219
bart86a87df2009-03-04 19:26:47 +0000220/** Convert a DRD thread ID into a Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000221ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000222{
bartbedfd232009-03-26 19:07:15 +0000223 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
224 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000225
bartbedfd232009-03-26 19:07:15 +0000226 return (DRD_(g_threadinfo)[tid].vg_thread_exists
227 ? DRD_(g_threadinfo)[tid].vg_threadid
228 : VG_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000229}
230
bart8f822af2009-06-08 18:20:42 +0000231#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart324a23b2009-02-15 12:14:52 +0000232/**
233 * Sanity check of the doubly linked list of segments referenced by a
234 * ThreadInfo struct.
235 * @return True if sane, False if not.
sewardjaf44c822007-11-25 14:01:38 +0000236 */
bart62a784c2009-02-15 13:11:14 +0000237static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti)
sewardjaf44c822007-11-25 14:01:38 +0000238{
bartbedfd232009-03-26 19:07:15 +0000239 Segment* p;
bart8f822af2009-06-08 18:20:42 +0000240
bartbedfd232009-03-26 19:07:15 +0000241 for (p = ti->first; p; p = p->next) {
242 if (p->next && p->next->prev != p)
243 return False;
244 if (p->next == 0 && p != ti->last)
245 return False;
246 }
247 for (p = ti->last; p; p = p->prev) {
248 if (p->prev && p->prev->next != p)
249 return False;
250 if (p->prev == 0 && p != ti->first)
251 return False;
252 }
253 return True;
sewardjaf44c822007-11-25 14:01:38 +0000254}
bart23d3a4e2008-04-05 12:53:00 +0000255#endif
sewardjaf44c822007-11-25 14:01:38 +0000256
bart439c55f2009-02-15 10:38:37 +0000257/**
258 * Create the first segment for a newly started thread.
259 *
260 * This function is called from the handler installed via
261 * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler
262 * from the context of the creator thread, before the new thread has been
263 * created.
bart86a87df2009-03-04 19:26:47 +0000264 *
265 * @param[in] creator DRD thread ID of the creator thread.
266 * @param[in] vg_created Valgrind thread ID of the created thread.
267 *
268 * @return DRD thread ID of the created thread.
bart439c55f2009-02-15 10:38:37 +0000269 */
bart62a784c2009-02-15 13:11:14 +0000270DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
271 const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000272{
bartbedfd232009-03-26 19:07:15 +0000273 DrdThreadId created;
sewardjaf44c822007-11-25 14:01:38 +0000274
bartbedfd232009-03-26 19:07:15 +0000275 tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID);
276 created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created);
277 tl_assert(0 <= (int)created && created < DRD_N_THREADS
278 && created != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000279
bartbedfd232009-03-26 19:07:15 +0000280 tl_assert(DRD_(g_threadinfo)[created].first == 0);
281 tl_assert(DRD_(g_threadinfo)[created].last == 0);
bart8f822af2009-06-08 18:20:42 +0000282 /* Create an initial segment for the newly created thread. */
bartbedfd232009-03-26 19:07:15 +0000283 thread_append_segment(created, DRD_(sg_new)(creator, created));
sewardjaf44c822007-11-25 14:01:38 +0000284
bartbedfd232009-03-26 19:07:15 +0000285 return created;
sewardjaf44c822007-11-25 14:01:38 +0000286}
287
bart439c55f2009-02-15 10:38:37 +0000288/**
bart86a87df2009-03-04 19:26:47 +0000289 * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called
290 * after the thread has been created and before any client instructions are run
bart439c55f2009-02-15 10:38:37 +0000291 * on the newly created thread, e.g. from the handler installed via
292 * VG_(track_pre_thread_first_insn)().
bart86a87df2009-03-04 19:26:47 +0000293 *
294 * @param[in] vg_created Valgrind thread ID of the newly created thread.
295 *
296 * @return DRD thread ID for the new thread.
bart439c55f2009-02-15 10:38:37 +0000297 */
bart62a784c2009-02-15 13:11:14 +0000298DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created)
bart439c55f2009-02-15 10:38:37 +0000299{
bartbedfd232009-03-26 19:07:15 +0000300 const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created);
bart439c55f2009-02-15 10:38:37 +0000301
bartbedfd232009-03-26 19:07:15 +0000302 tl_assert(0 <= (int)created && created < DRD_N_THREADS
303 && created != DRD_INVALID_THREADID);
bart439c55f2009-02-15 10:38:37 +0000304
bartbedfd232009-03-26 19:07:15 +0000305 DRD_(g_threadinfo)[created].stack_max
306 = VG_(thread_get_stack_max)(vg_created);
307 DRD_(g_threadinfo)[created].stack_startup
308 = DRD_(g_threadinfo)[created].stack_max;
309 DRD_(g_threadinfo)[created].stack_min
310 = DRD_(g_threadinfo)[created].stack_max;
311 DRD_(g_threadinfo)[created].stack_min_min
312 = DRD_(g_threadinfo)[created].stack_max;
313 DRD_(g_threadinfo)[created].stack_size
314 = VG_(thread_get_stack_size)(vg_created);
315 tl_assert(DRD_(g_threadinfo)[created].stack_max != 0);
bart439c55f2009-02-15 10:38:37 +0000316
bartbedfd232009-03-26 19:07:15 +0000317 return created;
bart439c55f2009-02-15 10:38:37 +0000318}
bart09dc13f2009-02-14 15:13:31 +0000319
bart324a23b2009-02-15 12:14:52 +0000320/**
321 * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just
322 * after thread drd_joiner joined thread drd_joinee.
323 */
bart09dc13f2009-02-14 15:13:31 +0000324void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
325{
bartbedfd232009-03-26 19:07:15 +0000326 tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner));
327 tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee));
bart7627be32009-06-06 12:26:05 +0000328
bartbedfd232009-03-26 19:07:15 +0000329 DRD_(thread_new_segment)(drd_joiner);
bart8f822af2009-06-08 18:20:42 +0000330 DRD_(thread_combine_vc_join)(drd_joiner, drd_joinee);
bart7627be32009-06-06 12:26:05 +0000331 DRD_(thread_new_segment)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000332
bartbedfd232009-03-26 19:07:15 +0000333 if (s_trace_fork_join)
334 {
335 const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner);
336 const ThreadId joinee = DRD_(DrdThreadIdToVgThreadId)(drd_joinee);
337 const unsigned msg_size = 256;
338 char* msg;
bart09dc13f2009-02-14 15:13:31 +0000339
bartbedfd232009-03-26 19:07:15 +0000340 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
341 tl_assert(msg);
342 VG_(snprintf)(msg, msg_size,
343 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
344 joiner, drd_joiner, joinee, drd_joinee);
345 if (joiner)
346 {
bart8f822af2009-06-08 18:20:42 +0000347 char* vc;
348
349 vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(drd_joiner));
bartbedfd232009-03-26 19:07:15 +0000350 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart8f822af2009-06-08 18:20:42 +0000351 ", new vc: %s", vc);
352 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000353 }
sewardj1e29ebc2009-07-15 14:49:17 +0000354 VG_(message)(Vg_DebugMsg, "%s\n", msg);
bartbedfd232009-03-26 19:07:15 +0000355 VG_(free)(msg);
356 }
bart09dc13f2009-02-14 15:13:31 +0000357
bartbedfd232009-03-26 19:07:15 +0000358 if (! DRD_(get_check_stack_accesses)())
359 {
360 DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee)
361 - DRD_(thread_get_stack_size)(drd_joinee),
362 DRD_(thread_get_stack_max)(drd_joinee));
363 }
364 DRD_(clientobj_delete_thread)(drd_joinee);
365 DRD_(thread_delete)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000366}
367
bart324a23b2009-02-15 12:14:52 +0000368/**
369 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
370 * and accesses this data structure from multiple threads without locking.
371 * Any conflicting accesses in the range stack_startup..stack_max will be
372 * ignored.
373 */
bart62a784c2009-02-15 13:11:14 +0000374void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
375 const Addr stack_startup)
sewardjaf44c822007-11-25 14:01:38 +0000376{
bartbedfd232009-03-26 19:07:15 +0000377 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
378 && tid != DRD_INVALID_THREADID);
379 tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup);
380 tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max);
381 DRD_(g_threadinfo)[tid].stack_startup = stack_startup;
sewardjaf44c822007-11-25 14:01:38 +0000382}
383
bart86a87df2009-03-04 19:26:47 +0000384/** Return the stack pointer for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000385Addr DRD_(thread_get_stack_min)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000386{
bartbedfd232009-03-26 19:07:15 +0000387 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
388 && tid != DRD_INVALID_THREADID);
389 return DRD_(g_threadinfo)[tid].stack_min;
sewardjaf44c822007-11-25 14:01:38 +0000390}
391
bart86a87df2009-03-04 19:26:47 +0000392/**
393 * Return the lowest value that was ever assigned to the stack pointer
394 * for the specified thread.
395 */
bart62a784c2009-02-15 13:11:14 +0000396Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000397{
bartbedfd232009-03-26 19:07:15 +0000398 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
399 && tid != DRD_INVALID_THREADID);
400 return DRD_(g_threadinfo)[tid].stack_min_min;
bartcac53462008-03-29 09:27:08 +0000401}
402
bart86a87df2009-03-04 19:26:47 +0000403/** Return the top address for the stack of the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000404Addr DRD_(thread_get_stack_max)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000405{
bartbedfd232009-03-26 19:07:15 +0000406 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
407 && tid != DRD_INVALID_THREADID);
408 return DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000409}
410
bart86a87df2009-03-04 19:26:47 +0000411/** Return the maximum stack size for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000412SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000413{
bartbedfd232009-03-26 19:07:15 +0000414 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
415 && tid != DRD_INVALID_THREADID);
416 return DRD_(g_threadinfo)[tid].stack_size;
bartcac53462008-03-29 09:27:08 +0000417}
418
bart09dc13f2009-02-14 15:13:31 +0000419/**
420 * Clean up thread-specific data structures. Call this just after
421 * pthread_join().
sewardjaf44c822007-11-25 14:01:38 +0000422 */
bart62a784c2009-02-15 13:11:14 +0000423void DRD_(thread_delete)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000424{
bartbedfd232009-03-26 19:07:15 +0000425 Segment* sg;
426 Segment* sg_prev;
sewardjaf44c822007-11-25 14:01:38 +0000427
bartbedfd232009-03-26 19:07:15 +0000428 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart86a87df2009-03-04 19:26:47 +0000429
bartbedfd232009-03-26 19:07:15 +0000430 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0);
431 for (sg = DRD_(g_threadinfo)[tid].last; sg; sg = sg_prev)
432 {
433 sg_prev = sg->prev;
434 sg->prev = 0;
435 sg->next = 0;
436 DRD_(sg_put)(sg);
437 }
438 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
439 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
440 tl_assert(DRD_(g_threadinfo)[tid].detached_posix_thread == False);
441 DRD_(g_threadinfo)[tid].first = 0;
442 DRD_(g_threadinfo)[tid].last = 0;
bart86a87df2009-03-04 19:26:47 +0000443
bartbedfd232009-03-26 19:07:15 +0000444 tl_assert(! DRD_(IsValidDrdThreadId)(tid));
sewardjaf44c822007-11-25 14:01:38 +0000445}
446
bart324a23b2009-02-15 12:14:52 +0000447/**
448 * Called after a thread performed its last memory access and before
449 * thread_delete() is called. Note: thread_delete() is only called for
450 * joinable threads, not for detached threads.
451 */
bart62a784c2009-02-15 13:11:14 +0000452void DRD_(thread_finished)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000453{
bartbedfd232009-03-26 19:07:15 +0000454 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
455 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000456
bartbedfd232009-03-26 19:07:15 +0000457 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
sewardjaf44c822007-11-25 14:01:38 +0000458
bartbedfd232009-03-26 19:07:15 +0000459 if (DRD_(g_threadinfo)[tid].detached_posix_thread)
460 {
461 /*
462 * Once a detached thread has finished, its stack is deallocated and
463 * should no longer be taken into account when computing the conflict set.
464 */
465 DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000466
bartbedfd232009-03-26 19:07:15 +0000467 /*
468 * For a detached thread, calling pthread_exit() invalidates the
469 * POSIX thread ID associated with the detached thread. For joinable
470 * POSIX threads however, the POSIX thread ID remains live after the
471 * pthread_exit() call until pthread_join() is called.
472 */
473 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
474 }
sewardjaf44c822007-11-25 14:01:38 +0000475}
476
bart9b2974a2008-09-27 12:35:31 +0000477/** Called just before pthread_cancel(). */
bart62a784c2009-02-15 13:11:14 +0000478void DRD_(thread_pre_cancel)(const DrdThreadId tid)
bartaf0691b2008-09-27 12:26:50 +0000479{
bartbedfd232009-03-26 19:07:15 +0000480 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
481 && tid != DRD_INVALID_THREADID);
482 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bartaf0691b2008-09-27 12:26:50 +0000483
bartbedfd232009-03-26 19:07:15 +0000484 DRD_(g_threadinfo)[tid].synchr_nesting = 0;
bartaf0691b2008-09-27 12:26:50 +0000485}
486
barte7dff242009-04-23 17:12:39 +0000487/**
488 * Store the POSIX thread ID for the specified thread.
489 *
490 * @note This function can be called two times for the same thread -- see also
491 * the comment block preceding the pthread_create() wrapper in
492 * drd_pthread_intercepts.c.
493 */
bart62a784c2009-02-15 13:11:14 +0000494void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
sewardjaf44c822007-11-25 14:01:38 +0000495{
bartbedfd232009-03-26 19:07:15 +0000496 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
497 && tid != DRD_INVALID_THREADID);
barte7dff242009-04-23 17:12:39 +0000498 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID
499 || DRD_(g_threadinfo)[tid].pt_threadid == ptid);
bartbedfd232009-03-26 19:07:15 +0000500 tl_assert(ptid != INVALID_POSIX_THREADID);
501 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
502 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardjaf44c822007-11-25 14:01:38 +0000503}
504
bart86a87df2009-03-04 19:26:47 +0000505/** Returns true for joinable threads and false for detached threads. */
bart62a784c2009-02-15 13:11:14 +0000506Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000507{
bartbedfd232009-03-26 19:07:15 +0000508 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
509 && tid != DRD_INVALID_THREADID);
510 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000511}
512
bart86a87df2009-03-04 19:26:47 +0000513/** Store the thread mode: joinable or detached. */
bart62a784c2009-02-15 13:11:14 +0000514void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000515{
bartbedfd232009-03-26 19:07:15 +0000516 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
517 && tid != DRD_INVALID_THREADID);
518 tl_assert(!! joinable == joinable);
519 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bart8f822af2009-06-08 18:20:42 +0000520
bartbedfd232009-03-26 19:07:15 +0000521 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000522}
523
bartd45d9952009-05-31 18:53:54 +0000524/** Obtain the thread number and the user-assigned thread name. */
525const char* DRD_(thread_get_name)(const DrdThreadId tid)
526{
527 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
528 && tid != DRD_INVALID_THREADID);
529
530 return DRD_(g_threadinfo)[tid].name;
531}
532
533/** Set the name of the specified thread. */
534void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name)
535{
536 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
537 && tid != DRD_INVALID_THREADID);
538
539 if (name == NULL || name[0] == 0)
540 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
541 sizeof(DRD_(g_threadinfo)[tid].name),
542 "Thread %d",
543 tid);
544 else
545 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
546 sizeof(DRD_(g_threadinfo)[tid].name),
547 "Thread %d (%s)",
548 tid, name);
549 DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0;
550}
551
bart86a87df2009-03-04 19:26:47 +0000552/**
553 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
554 * conflict set.
555 */
bart62a784c2009-02-15 13:11:14 +0000556void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000557{
bartbedfd232009-03-26 19:07:15 +0000558 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000559
bartbedfd232009-03-26 19:07:15 +0000560 if (vg_tid != s_vg_running_tid)
561 {
562 DRD_(thread_set_running_tid)(vg_tid,
563 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
564 }
sewardj8b09d4f2007-12-04 21:27:18 +0000565
bartbedfd232009-03-26 19:07:15 +0000566 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
567 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000568}
569
bart86a87df2009-03-04 19:26:47 +0000570/**
571 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
572 * conflict set.
573 */
bart62a784c2009-02-15 13:11:14 +0000574void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
575 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000576{
bartbedfd232009-03-26 19:07:15 +0000577 tl_assert(vg_tid != VG_INVALID_THREADID);
578 tl_assert(drd_tid != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000579
bartbedfd232009-03-26 19:07:15 +0000580 if (vg_tid != s_vg_running_tid)
581 {
582 if (s_trace_context_switches
583 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
584 {
585 VG_(message)(Vg_DebugMsg,
586 "Context switch from thread %d/%d to thread %d/%d;"
sewardj1e29ebc2009-07-15 14:49:17 +0000587 " segments: %llu\n",
bartbedfd232009-03-26 19:07:15 +0000588 s_vg_running_tid, DRD_(g_drd_running_tid),
589 DRD_(DrdThreadIdToVgThreadId)(drd_tid), drd_tid,
590 DRD_(sg_get_segments_alive_count)());
591 }
592 s_vg_running_tid = vg_tid;
593 DRD_(g_drd_running_tid) = drd_tid;
594 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
595 s_context_switch_count++;
596 }
sewardj8b09d4f2007-12-04 21:27:18 +0000597
bartbedfd232009-03-26 19:07:15 +0000598 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
599 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000600}
601
bart86a87df2009-03-04 19:26:47 +0000602/**
603 * Increase the synchronization nesting counter. Must be called before the
604 * client calls a synchronization function.
605 */
bart62a784c2009-02-15 13:11:14 +0000606int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000607{
bartbedfd232009-03-26 19:07:15 +0000608 tl_assert(DRD_(IsValidDrdThreadId)(tid));
609 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000610}
611
bart86a87df2009-03-04 19:26:47 +0000612/**
613 * Decrease the synchronization nesting counter. Must be called after the
614 * client left a synchronization function.
615 */
bart62a784c2009-02-15 13:11:14 +0000616int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000617{
bartbedfd232009-03-26 19:07:15 +0000618 tl_assert(DRD_(IsValidDrdThreadId)(tid));
619 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
620 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000621}
622
bart86a87df2009-03-04 19:26:47 +0000623/** Returns the synchronization nesting counter. */
bart62a784c2009-02-15 13:11:14 +0000624int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000625{
bartbedfd232009-03-26 19:07:15 +0000626 tl_assert(DRD_(IsValidDrdThreadId)(tid));
627 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000628}
629
bart1a473c72008-03-13 19:03:38 +0000630/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000631static
bart86a87df2009-03-04 19:26:47 +0000632void thread_append_segment(const DrdThreadId tid, Segment* const sg)
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);
bart8f822af2009-06-08 18:20:42 +0000636
637#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
638 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
639#endif
640
bartbedfd232009-03-26 19:07:15 +0000641 sg->prev = DRD_(g_threadinfo)[tid].last;
642 sg->next = 0;
643 if (DRD_(g_threadinfo)[tid].last)
644 DRD_(g_threadinfo)[tid].last->next = sg;
645 DRD_(g_threadinfo)[tid].last = sg;
646 if (DRD_(g_threadinfo)[tid].first == 0)
647 DRD_(g_threadinfo)[tid].first = sg;
bart8f822af2009-06-08 18:20:42 +0000648
649#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
650 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
651#endif
sewardjaf44c822007-11-25 14:01:38 +0000652}
653
bart324a23b2009-02-15 12:14:52 +0000654/**
655 * Remove a segment from the segment list of thread threadid, and free the
656 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000657 */
bart62a784c2009-02-15 13:11:14 +0000658static
bart86a87df2009-03-04 19:26:47 +0000659void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000660{
bartbedfd232009-03-26 19:07:15 +0000661 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
662 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000663
664#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
665 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
666#endif
bart26f73e12008-02-24 18:37:08 +0000667
bartbedfd232009-03-26 19:07:15 +0000668 if (sg->prev)
669 sg->prev->next = sg->next;
670 if (sg->next)
671 sg->next->prev = sg->prev;
672 if (sg == DRD_(g_threadinfo)[tid].first)
673 DRD_(g_threadinfo)[tid].first = sg->next;
674 if (sg == DRD_(g_threadinfo)[tid].last)
675 DRD_(g_threadinfo)[tid].last = sg->prev;
676 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000677
bart8f822af2009-06-08 18:20:42 +0000678#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
679 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
680#endif
sewardjaf44c822007-11-25 14:01:38 +0000681}
682
bart86a87df2009-03-04 19:26:47 +0000683/**
684 * Returns a pointer to the vector clock of the most recent segment associated
685 * with thread 'tid'.
686 */
bart62a784c2009-02-15 13:11:14 +0000687VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000688{
bartbedfd232009-03-26 19:07:15 +0000689 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
690 && tid != DRD_INVALID_THREADID);
691 tl_assert(DRD_(g_threadinfo)[tid].last);
692 return &DRD_(g_threadinfo)[tid].last->vc;
sewardjaf44c822007-11-25 14:01:38 +0000693}
694
bart324a23b2009-02-15 12:14:52 +0000695/**
696 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000697 */
bart62a784c2009-02-15 13:11:14 +0000698void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000699{
bartbedfd232009-03-26 19:07:15 +0000700 tl_assert(sg);
701 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
702 && tid != DRD_INVALID_THREADID);
703 tl_assert(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000704
bartbedfd232009-03-26 19:07:15 +0000705 DRD_(sg_put)(*sg);
706 *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000707}
708
sewardjaf44c822007-11-25 14:01:38 +0000709/**
710 * Compute the minimum of all latest vector clocks of all threads
711 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
bart86a87df2009-03-04 19:26:47 +0000712 *
sewardjaf44c822007-11-25 14:01:38 +0000713 * @param vc pointer to a vectorclock, holds result upon return.
714 */
bart62a784c2009-02-15 13:11:14 +0000715static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000716{
bartbedfd232009-03-26 19:07:15 +0000717 unsigned i;
718 Bool first;
719 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000720
bartbedfd232009-03-26 19:07:15 +0000721 first = True;
bart8f822af2009-06-08 18:20:42 +0000722 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000723 {
724 latest_sg = DRD_(g_threadinfo)[i].last;
725 if (latest_sg)
726 {
727 if (first)
728 DRD_(vc_assign)(vc, &latest_sg->vc);
729 else
730 DRD_(vc_min)(vc, &latest_sg->vc);
731 first = False;
732 }
733 }
sewardjaf44c822007-11-25 14:01:38 +0000734}
735
bart86a87df2009-03-04 19:26:47 +0000736/**
737 * Compute the maximum of all latest vector clocks of all threads.
738 *
739 * @param vc pointer to a vectorclock, holds result upon return.
740 */
bart62a784c2009-02-15 13:11:14 +0000741static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000742{
bartbedfd232009-03-26 19:07:15 +0000743 unsigned i;
744 Bool first;
745 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000746
bartbedfd232009-03-26 19:07:15 +0000747 first = True;
bart8f822af2009-06-08 18:20:42 +0000748 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000749 {
750 latest_sg = DRD_(g_threadinfo)[i].last;
751 if (latest_sg)
752 {
753 if (first)
754 DRD_(vc_assign)(vc, &latest_sg->vc);
755 else
756 DRD_(vc_combine)(vc, &latest_sg->vc);
757 first = False;
758 }
759 }
sewardjaf44c822007-11-25 14:01:38 +0000760}
761
762/**
bart5bd9f2d2008-03-03 20:31:58 +0000763 * Discard all segments that have a defined order against the latest vector
bart86a87df2009-03-04 19:26:47 +0000764 * clock of all threads -- these segments can no longer be involved in a
sewardjaf44c822007-11-25 14:01:38 +0000765 * data race.
766 */
bart8f822af2009-06-08 18:20:42 +0000767static void thread_discard_ordered_segments(void)
sewardjaf44c822007-11-25 14:01:38 +0000768{
bartbedfd232009-03-26 19:07:15 +0000769 unsigned i;
770 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000771
bartbedfd232009-03-26 19:07:15 +0000772 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000773
bartbedfd232009-03-26 19:07:15 +0000774 DRD_(vc_init)(&thread_vc_min, 0, 0);
775 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
776 if (DRD_(sg_get_trace)())
777 {
bart8f822af2009-06-08 18:20:42 +0000778 char *vc_min, *vc_max;
bartbedfd232009-03-26 19:07:15 +0000779 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000780
bartbedfd232009-03-26 19:07:15 +0000781 DRD_(vc_init)(&thread_vc_max, 0, 0);
782 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
bart8f822af2009-06-08 18:20:42 +0000783 vc_min = DRD_(vc_aprint)(&thread_vc_min);
784 vc_max = DRD_(vc_aprint)(&thread_vc_max);
785 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000786 "Discarding ordered segments -- min vc is %s, max vc is %s\n",
bart8f822af2009-06-08 18:20:42 +0000787 vc_min, vc_max);
788 VG_(free)(vc_min);
789 VG_(free)(vc_max);
bartbedfd232009-03-26 19:07:15 +0000790 DRD_(vc_cleanup)(&thread_vc_max);
791 }
sewardjaf44c822007-11-25 14:01:38 +0000792
bart8f822af2009-06-08 18:20:42 +0000793 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000794 {
795 Segment* sg;
796 Segment* sg_next;
797 for (sg = DRD_(g_threadinfo)[i].first;
798 sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
799 sg = sg_next)
800 {
801 thread_discard_segment(i, sg);
802 }
803 }
804 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +0000805}
806
bart324a23b2009-02-15 12:14:52 +0000807/**
bart8f822af2009-06-08 18:20:42 +0000808 * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper
809 * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if
810 * all segments in the set CS are ordered consistently against both sg1 and
811 * sg2. The set CS is defined as the set of segments that can immediately
812 * precede future segments via inter-thread synchronization operations. In
813 * DRD the set CS consists of the latest segment of each thread combined with
814 * all segments for which the reference count is strictly greater than one.
815 * The code below is an optimized version of the following:
816 *
817 * for (i = 0; i < DRD_N_THREADS; i++)
818 * {
819 * Segment* sg;
820 *
821 * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
822 * {
823 * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1)
824 * {
825 * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc)
826 * != DRD_(vc_lte)(&sg2->vc, &sg->vc)
827 * || DRD_(vc_lte)(&sg->vc, &sg1->vc)
828 * != DRD_(vc_lte)(&sg->vc, &sg2->vc))
829 * {
830 * return False;
831 * }
832 * }
833 * }
834 * }
835 */
836static Bool thread_consistent_segment_ordering(const DrdThreadId tid,
837 Segment* const sg1,
838 Segment* const sg2)
839{
840 unsigned i;
841
842 tl_assert(sg1->next);
843 tl_assert(sg2->next);
844 tl_assert(sg1->next == sg2);
845 tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc));
846
847 for (i = 0; i < DRD_N_THREADS; i++)
848 {
849 Segment* sg;
850
851 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
852 {
853 if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1)
854 {
855 if (DRD_(vc_lte)(&sg2->vc, &sg->vc))
856 break;
857 if (DRD_(vc_lte)(&sg1->vc, &sg->vc))
858 return False;
859 }
860 }
861 for (sg = DRD_(g_threadinfo)[i].last; sg; sg = sg->prev)
862 {
863 if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1)
864 {
865 if (DRD_(vc_lte)(&sg->vc, &sg1->vc))
866 break;
867 if (DRD_(vc_lte)(&sg->vc, &sg2->vc))
868 return False;
869 }
870 }
871 }
872 return True;
873}
874
875/**
bart324a23b2009-02-15 12:14:52 +0000876 * Merge all segments that may be merged without triggering false positives
877 * or discarding real data races. For the theoretical background of segment
bart8f822af2009-06-08 18:20:42 +0000878 * merging, see also the following paper: Mark Christiaens, Michiel Ronsse
879 * and Koen De Bosschere. Bounding the number of segment histories during
880 * data race detection. Parallel Computing archive, Volume 28, Issue 9,
881 * pp 1221-1238, September 2002. This paper contains a proof that merging
882 * consecutive segments for which the property equiv(s1,s2) holds can be
883 * merged without reducing the accuracy of datarace detection. Furthermore
884 * it is also proven that the total number of all segments will never grow
885 * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged
886 * every time a new segment is created. The property equiv(s1, s2) is defined
887 * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector
888 * clocks of segments s and s1 are ordered in the same way as those of segments
889 * s and s2. The set CS is defined as the set of existing segments s that have
890 * the potential to conflict with not yet created segments, either because the
891 * segment s is the latest segment of a thread or because it can become the
892 * immediate predecessor of a new segment due to a synchronization operation.
barta9c37392008-03-22 09:38:48 +0000893 */
894static void thread_merge_segments(void)
895{
bartbedfd232009-03-26 19:07:15 +0000896 unsigned i;
barta9c37392008-03-22 09:38:48 +0000897
bart8f822af2009-06-08 18:20:42 +0000898 s_new_segments_since_last_merge = 0;
899
900 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000901 {
902 Segment* sg;
barta9c37392008-03-22 09:38:48 +0000903
bart8f822af2009-06-08 18:20:42 +0000904#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
905 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
906#endif
barta9c37392008-03-22 09:38:48 +0000907
bartbedfd232009-03-26 19:07:15 +0000908 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
barta9c37392008-03-22 09:38:48 +0000909 {
bartbedfd232009-03-26 19:07:15 +0000910 if (DRD_(sg_get_refcnt)(sg) == 1
911 && sg->next
912 && DRD_(sg_get_refcnt)(sg->next) == 1
bart8f822af2009-06-08 18:20:42 +0000913 && sg->next->next
914 && thread_consistent_segment_ordering(i, sg, sg->next))
bartbedfd232009-03-26 19:07:15 +0000915 {
916 /* Merge sg and sg->next into sg. */
917 DRD_(sg_merge)(sg, sg->next);
918 thread_discard_segment(i, sg->next);
919 }
barta9c37392008-03-22 09:38:48 +0000920 }
barta9c37392008-03-22 09:38:48 +0000921
bart8f822af2009-06-08 18:20:42 +0000922#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
923 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
924#endif
bartbedfd232009-03-26 19:07:15 +0000925 }
barta9c37392008-03-22 09:38:48 +0000926}
927
bart324a23b2009-02-15 12:14:52 +0000928/**
bart324a23b2009-02-15 12:14:52 +0000929 * Create a new segment for the specified thread, and discard any segments
930 * that cannot cause races anymore.
sewardjaf44c822007-11-25 14:01:38 +0000931 */
bart62a784c2009-02-15 13:11:14 +0000932void DRD_(thread_new_segment)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000933{
bart8f822af2009-06-08 18:20:42 +0000934 Segment* last_sg;
bartbedfd232009-03-26 19:07:15 +0000935 Segment* new_sg;
bartd66e3a82008-04-06 15:02:17 +0000936
bartbedfd232009-03-26 19:07:15 +0000937 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
938 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000939 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +0000940
bart8f822af2009-06-08 18:20:42 +0000941 last_sg = DRD_(g_threadinfo)[tid].last;
bartbedfd232009-03-26 19:07:15 +0000942 new_sg = DRD_(sg_new)(tid, tid);
943 thread_append_segment(tid, new_sg);
bart8f822af2009-06-08 18:20:42 +0000944 if (tid == DRD_(g_drd_running_tid) && last_sg)
barte5214662009-06-21 11:51:23 +0000945 {
bart8f822af2009-06-08 18:20:42 +0000946 DRD_(thread_update_conflict_set)(tid, &last_sg->vc);
barte5214662009-06-21 11:51:23 +0000947 s_update_conflict_set_new_sg_count++;
948 }
bartd66e3a82008-04-06 15:02:17 +0000949
bart8f822af2009-06-08 18:20:42 +0000950 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +0000951
bart8f822af2009-06-08 18:20:42 +0000952 if (s_segment_merging
953 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
bartbedfd232009-03-26 19:07:15 +0000954 {
bart8f822af2009-06-08 18:20:42 +0000955 thread_discard_ordered_segments();
bartbedfd232009-03-26 19:07:15 +0000956 thread_merge_segments();
957 }
sewardjaf44c822007-11-25 14:01:38 +0000958}
959
bart26f73e12008-02-24 18:37:08 +0000960/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart8f822af2009-06-08 18:20:42 +0000961void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +0000962{
bartbedfd232009-03-26 19:07:15 +0000963 tl_assert(joiner != joinee);
964 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
965 && joiner != DRD_INVALID_THREADID);
966 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
967 && joinee != DRD_INVALID_THREADID);
968 tl_assert(DRD_(g_threadinfo)[joiner].last);
969 tl_assert(DRD_(g_threadinfo)[joinee].last);
bart8f822af2009-06-08 18:20:42 +0000970
971 if (DRD_(sg_get_trace)())
972 {
973 char *str1, *str2;
974 str1 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc);
975 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joinee].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +0000976 VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n",
bart8f822af2009-06-08 18:20:42 +0000977 str1, str2);
978 VG_(free)(str1);
979 VG_(free)(str2);
980 }
barte5214662009-06-21 11:51:23 +0000981 if (joiner == DRD_(g_drd_running_tid))
982 {
983 VectorClock old_vc;
984
985 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[joiner].last->vc);
986 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
bartf6ec1fe2009-06-21 18:07:35 +0000987 &DRD_(g_threadinfo)[joinee].last->vc);
barte5214662009-06-21 11:51:23 +0000988 DRD_(thread_update_conflict_set)(joiner, &old_vc);
989 s_update_conflict_set_join_count++;
990 DRD_(vc_cleanup)(&old_vc);
991 }
992 else
993 {
994 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
bartf6ec1fe2009-06-21 18:07:35 +0000995 &DRD_(g_threadinfo)[joinee].last->vc);
barte5214662009-06-21 11:51:23 +0000996 }
997
998 thread_discard_ordered_segments();
999
bart8f822af2009-06-08 18:20:42 +00001000 if (DRD_(sg_get_trace)())
1001 {
1002 char* str;
1003 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001004 VG_(message)(Vg_DebugMsg, "After join: %s\n", str);
bart8f822af2009-06-08 18:20:42 +00001005 VG_(free)(str);
1006 }
sewardjaf44c822007-11-25 14:01:38 +00001007}
1008
bart324a23b2009-02-15 12:14:52 +00001009/**
bart8f822af2009-06-08 18:20:42 +00001010 * Update the vector clock of the last segment of thread tid with the
bartf6ec1fe2009-06-21 18:07:35 +00001011 * the vector clock of segment sg.
bart26f73e12008-02-24 18:37:08 +00001012 */
bartf6ec1fe2009-06-21 18:07:35 +00001013static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg)
sewardjaf44c822007-11-25 14:01:38 +00001014{
bart8f822af2009-06-08 18:20:42 +00001015 const VectorClock* const vc = &sg->vc;
1016
bartbedfd232009-03-26 19:07:15 +00001017 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1018 && tid != DRD_INVALID_THREADID);
1019 tl_assert(DRD_(g_threadinfo)[tid].last);
bart8f822af2009-06-08 18:20:42 +00001020 tl_assert(sg);
bartbedfd232009-03-26 19:07:15 +00001021 tl_assert(vc);
bart8f822af2009-06-08 18:20:42 +00001022
1023 if (tid != sg->tid)
1024 {
1025 VectorClock old_vc;
1026
1027 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc);
1028 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc);
1029 if (DRD_(sg_get_trace)())
1030 {
1031 char *str1, *str2;
1032 str1 = DRD_(vc_aprint)(&old_vc);
1033 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001034 VG_(message)(Vg_DebugMsg, "thread %d: vc %s -> %s\n", tid, str1, str2);
bart8f822af2009-06-08 18:20:42 +00001035 VG_(free)(str1);
1036 VG_(free)(str2);
1037 }
barte5214662009-06-21 11:51:23 +00001038
bart8f822af2009-06-08 18:20:42 +00001039 thread_discard_ordered_segments();
barte5214662009-06-21 11:51:23 +00001040
bart8f822af2009-06-08 18:20:42 +00001041 DRD_(thread_update_conflict_set)(tid, &old_vc);
barte5214662009-06-21 11:51:23 +00001042 s_update_conflict_set_sync_count++;
1043
bart8f822af2009-06-08 18:20:42 +00001044 DRD_(vc_cleanup)(&old_vc);
1045 }
1046 else
1047 {
1048 tl_assert(DRD_(vc_lte)(vc, &DRD_(g_threadinfo)[tid].last->vc));
1049 }
sewardjaf44c822007-11-25 14:01:38 +00001050}
1051
bart324a23b2009-02-15 12:14:52 +00001052/**
bartf6ec1fe2009-06-21 18:07:35 +00001053 * Create a new segment for thread tid and update the vector clock of the last
1054 * segment of this thread with the the vector clock of segment sg. Call this
1055 * function after thread tid had to wait because of thread synchronization
1056 * until the memory accesses in the segment sg finished.
1057 */
1058void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, const Segment* sg)
1059{
1060 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1061 && tid != DRD_INVALID_THREADID);
1062 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1063 tl_assert(sg);
1064
1065 thread_append_segment(tid, DRD_(sg_new)(tid, tid));
1066
1067 thread_combine_vc_sync(tid, sg);
1068
1069 if (s_segment_merging
1070 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
1071 {
1072 thread_discard_ordered_segments();
1073 thread_merge_segments();
1074 }
1075}
1076
1077/**
bart324a23b2009-02-15 12:14:52 +00001078 * Call this function whenever a thread is no longer using the memory
1079 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
1080 * increase.
bart26f73e12008-02-24 18:37:08 +00001081 */
bart62a784c2009-02-15 13:11:14 +00001082void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +00001083{
bartbedfd232009-03-26 19:07:15 +00001084 DrdThreadId other_user;
1085 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001086
bartbedfd232009-03-26 19:07:15 +00001087 /* For all threads, mark the range [ a1, a2 [ as no longer in use. */
1088 other_user = DRD_INVALID_THREADID;
bart8f822af2009-06-08 18:20:42 +00001089 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001090 {
1091 Segment* p;
1092 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
sewardjaf44c822007-11-25 14:01:38 +00001093 {
bartbedfd232009-03-26 19:07:15 +00001094 if (other_user == DRD_INVALID_THREADID
1095 && i != DRD_(g_drd_running_tid))
1096 {
bart8f822af2009-06-08 18:20:42 +00001097 if (UNLIKELY(DRD_(bm_test_and_clear)(DRD_(sg_bm)(p), a1, a2)))
bartbedfd232009-03-26 19:07:15 +00001098 {
1099 other_user = i;
1100 }
1101 continue;
1102 }
bart8f822af2009-06-08 18:20:42 +00001103 DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001104 }
bartbedfd232009-03-26 19:07:15 +00001105 }
sewardjaf44c822007-11-25 14:01:38 +00001106
bartbedfd232009-03-26 19:07:15 +00001107 /*
1108 * If any other thread had accessed memory in [ a1, a2 [, update the
1109 * conflict set.
1110 */
1111 if (other_user != DRD_INVALID_THREADID
1112 && DRD_(bm_has_any_access)(DRD_(g_conflict_set), a1, a2))
1113 {
1114 thread_compute_conflict_set(&DRD_(g_conflict_set),
1115 DRD_(thread_get_running_tid)());
1116 }
sewardjaf44c822007-11-25 14:01:38 +00001117}
1118
bartd45d9952009-05-31 18:53:54 +00001119/** Specify whether memory loads should be recorded. */
1120void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001121{
bartbedfd232009-03-26 19:07:15 +00001122 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1123 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001124 tl_assert(enabled == !! enabled);
1125
1126 DRD_(g_threadinfo)[tid].is_recording_loads = enabled;
bart0268dfa2008-03-11 20:10:21 +00001127}
1128
bartd45d9952009-05-31 18:53:54 +00001129/** Specify whether memory stores should be recorded. */
1130void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001131{
bartbedfd232009-03-26 19:07:15 +00001132 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1133 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001134 tl_assert(enabled == !! enabled);
1135
1136 DRD_(g_threadinfo)[tid].is_recording_stores = enabled;
bart0268dfa2008-03-11 20:10:21 +00001137}
1138
bart86a87df2009-03-04 19:26:47 +00001139/**
1140 * Print the segment information for all threads.
1141 *
1142 * This function is only used for debugging purposes.
1143 */
bart62a784c2009-02-15 13:11:14 +00001144void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +00001145{
bartbedfd232009-03-26 19:07:15 +00001146 unsigned i;
1147 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001148
bart8f822af2009-06-08 18:20:42 +00001149 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001150 {
1151 if (DRD_(g_threadinfo)[i].first)
sewardjaf44c822007-11-25 14:01:38 +00001152 {
bartbedfd232009-03-26 19:07:15 +00001153 VG_(printf)("**************\n"
1154 "* thread %3d (%d/%d/%d/0x%lx/%d) *\n"
1155 "**************\n",
1156 i,
1157 DRD_(g_threadinfo)[i].vg_thread_exists,
1158 DRD_(g_threadinfo)[i].vg_threadid,
1159 DRD_(g_threadinfo)[i].posix_thread_exists,
1160 DRD_(g_threadinfo)[i].pt_threadid,
1161 DRD_(g_threadinfo)[i].detached_posix_thread);
1162 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
1163 {
1164 DRD_(sg_print)(p);
1165 }
sewardjaf44c822007-11-25 14:01:38 +00001166 }
bartbedfd232009-03-26 19:07:15 +00001167 }
sewardjaf44c822007-11-25 14:01:38 +00001168}
1169
bart86a87df2009-03-04 19:26:47 +00001170/** Show a call stack involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001171static void show_call_stack(const DrdThreadId tid,
1172 const Char* const msg,
1173 ExeContext* const callstack)
1174{
bartbedfd232009-03-26 19:07:15 +00001175 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +00001176
sewardj1e29ebc2009-07-15 14:49:17 +00001177 VG_(message)(Vg_UserMsg, "%s (thread %d/%d)\n", msg, vg_tid, tid);
sewardjaf44c822007-11-25 14:01:38 +00001178
bartbedfd232009-03-26 19:07:15 +00001179 if (vg_tid != VG_INVALID_THREADID)
1180 {
1181 if (callstack)
1182 {
1183 VG_(pp_ExeContext)(callstack);
1184 }
1185 else
1186 {
1187 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
1188 }
1189 }
1190 else
1191 {
1192 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001193 " (thread finished, call stack no longer available)\n");
bartbedfd232009-03-26 19:07:15 +00001194 }
sewardjaf44c822007-11-25 14:01:38 +00001195}
1196
bart86a87df2009-03-04 19:26:47 +00001197/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001198static void
1199thread_report_conflicting_segments_segment(const DrdThreadId tid,
1200 const Addr addr,
1201 const SizeT size,
1202 const BmAccessTypeT access_type,
1203 const Segment* const p)
1204{
bartbedfd232009-03-26 19:07:15 +00001205 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001206
bartbedfd232009-03-26 19:07:15 +00001207 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1208 && tid != DRD_INVALID_THREADID);
1209 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +00001210
bart8f822af2009-06-08 18:20:42 +00001211 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001212 {
1213 if (i != tid)
sewardjaf44c822007-11-25 14:01:38 +00001214 {
bartbedfd232009-03-26 19:07:15 +00001215 Segment* q;
1216 for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev)
1217 {
1218 /*
1219 * Since q iterates over the segments of thread i in order of
1220 * decreasing vector clocks, if q->vc <= p->vc, then
1221 * q->next->vc <= p->vc will also hold. Hence, break out of the
1222 * loop once this condition is met.
1223 */
1224 if (DRD_(vc_lte)(&q->vc, &p->vc))
1225 break;
1226 if (! DRD_(vc_lte)(&p->vc, &q->vc))
1227 {
bart8f822af2009-06-08 18:20:42 +00001228 if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size,
bartbedfd232009-03-26 19:07:15 +00001229 access_type))
1230 {
1231 tl_assert(q->stacktrace);
1232 show_call_stack(i, "Other segment start",
1233 q->stacktrace);
1234 show_call_stack(i, "Other segment end",
1235 q->next ? q->next->stacktrace : 0);
1236 }
1237 }
1238 }
sewardjaf44c822007-11-25 14:01:38 +00001239 }
bartbedfd232009-03-26 19:07:15 +00001240 }
sewardjaf44c822007-11-25 14:01:38 +00001241}
1242
bart86a87df2009-03-04 19:26:47 +00001243/** Print information about all segments involved in a data race. */
bart62a784c2009-02-15 13:11:14 +00001244void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1245 const Addr addr,
1246 const SizeT size,
1247 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +00001248{
bartbedfd232009-03-26 19:07:15 +00001249 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001250
bartbedfd232009-03-26 19:07:15 +00001251 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1252 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001253
bartbedfd232009-03-26 19:07:15 +00001254 for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next)
1255 {
bart8f822af2009-06-08 18:20:42 +00001256 if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type))
bartbedfd232009-03-26 19:07:15 +00001257 {
1258 thread_report_conflicting_segments_segment(tid, addr, size,
1259 access_type, p);
1260 }
1261 }
sewardjaf44c822007-11-25 14:01:38 +00001262}
sewardjaf44c822007-11-25 14:01:38 +00001263
bart324a23b2009-02-15 12:14:52 +00001264/**
bart8f822af2009-06-08 18:20:42 +00001265 * Verify whether the conflict set for thread tid is up to date. Only perform
1266 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
1267 */
1268static Bool thread_conflict_set_up_to_date(const DrdThreadId tid)
1269{
1270 static int do_verify_conflict_set = -1;
1271 Bool result;
1272 struct bitmap* computed_conflict_set = 0;
1273
1274 if (do_verify_conflict_set < 0)
1275 do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0;
1276
1277 if (do_verify_conflict_set == 0)
1278 return True;
1279
1280 thread_compute_conflict_set(&computed_conflict_set, tid);
1281 result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set);
1282 if (! result)
1283 {
1284 VG_(printf)("actual conflict set:\n");
1285 DRD_(bm_print)(DRD_(g_conflict_set));
1286 VG_(printf)("\n");
1287 VG_(printf)("computed conflict set:\n");
1288 DRD_(bm_print)(computed_conflict_set);
1289 VG_(printf)("\n");
1290 }
1291 DRD_(bm_delete)(computed_conflict_set);
1292 return result;
1293}
1294
1295/**
1296 * Compute the conflict set: a bitmap that represents the union of all memory
1297 * accesses of all segments that are unordered to the current segment of the
1298 * thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001299 */
bart86a87df2009-03-04 19:26:47 +00001300static void thread_compute_conflict_set(struct bitmap** conflict_set,
1301 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001302{
bartbedfd232009-03-26 19:07:15 +00001303 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001304
bartbedfd232009-03-26 19:07:15 +00001305 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1306 && tid != DRD_INVALID_THREADID);
1307 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001308
bart54803fe2009-06-21 09:26:27 +00001309 s_compute_conflict_set_count++;
bartbedfd232009-03-26 19:07:15 +00001310 s_conflict_set_bitmap_creation_count
1311 -= DRD_(bm_get_bitmap_creation_count)();
1312 s_conflict_set_bitmap2_creation_count
1313 -= DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001314
bartbedfd232009-03-26 19:07:15 +00001315 if (*conflict_set)
1316 {
bartf6ec1fe2009-06-21 18:07:35 +00001317 DRD_(bm_cleanup)(*conflict_set);
1318 DRD_(bm_init)(*conflict_set);
bartbedfd232009-03-26 19:07:15 +00001319 }
bartf6ec1fe2009-06-21 18:07:35 +00001320 else
1321 {
1322 *conflict_set = DRD_(bm_new)();
1323 }
bart26f73e12008-02-24 18:37:08 +00001324
bartbedfd232009-03-26 19:07:15 +00001325 if (s_trace_conflict_set)
1326 {
bart8f822af2009-06-08 18:20:42 +00001327 char* str;
bart26f73e12008-02-24 18:37:08 +00001328
bart8f822af2009-06-08 18:20:42 +00001329 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1330 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001331 "computing conflict set for thread %d/%d with vc %s\n",
bart8f822af2009-06-08 18:20:42 +00001332 DRD_(DrdThreadIdToVgThreadId)(tid), tid, str);
1333 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001334 }
sewardjaf44c822007-11-25 14:01:38 +00001335
bartbedfd232009-03-26 19:07:15 +00001336 p = DRD_(g_threadinfo)[tid].last;
1337 {
1338 unsigned j;
1339
1340 if (s_trace_conflict_set)
bart26f73e12008-02-24 18:37:08 +00001341 {
bart8f822af2009-06-08 18:20:42 +00001342 char* vc;
bartbedfd232009-03-26 19:07:15 +00001343
bart8f822af2009-06-08 18:20:42 +00001344 vc = DRD_(vc_aprint)(&p->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001345 VG_(message)(Vg_DebugMsg, "conflict set: thread [%d] at vc %s\n",
bart8f822af2009-06-08 18:20:42 +00001346 tid, vc);
1347 VG_(free)(vc);
bart26f73e12008-02-24 18:37:08 +00001348 }
sewardjaf44c822007-11-25 14:01:38 +00001349
bart8f822af2009-06-08 18:20:42 +00001350 for (j = 0; j < DRD_N_THREADS; j++)
bartbedfd232009-03-26 19:07:15 +00001351 {
1352 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1353 {
bart8f822af2009-06-08 18:20:42 +00001354 Segment* q;
bartbedfd232009-03-26 19:07:15 +00001355 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1356 {
1357 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1358 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1359 {
1360 if (s_trace_conflict_set)
1361 {
bart8f822af2009-06-08 18:20:42 +00001362 char* str;
1363
1364 str = DRD_(vc_aprint)(&q->vc);
1365 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001366 "conflict set: [%d] merging segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001367 j, str);
1368 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001369 }
bart8f822af2009-06-08 18:20:42 +00001370 DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q));
bartbedfd232009-03-26 19:07:15 +00001371 }
1372 else
1373 {
1374 if (s_trace_conflict_set)
1375 {
bart8f822af2009-06-08 18:20:42 +00001376 char* str;
1377
1378 str = DRD_(vc_aprint)(&q->vc);
1379 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001380 "conflict set: [%d] ignoring segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001381 j, str);
1382 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001383 }
1384 }
1385 }
1386 }
1387 }
1388 }
sewardjaf44c822007-11-25 14:01:38 +00001389
bartbedfd232009-03-26 19:07:15 +00001390 s_conflict_set_bitmap_creation_count
1391 += DRD_(bm_get_bitmap_creation_count)();
1392 s_conflict_set_bitmap2_creation_count
1393 += DRD_(bm_get_bitmap2_creation_count)();
1394
bart8f822af2009-06-08 18:20:42 +00001395 if (s_trace_conflict_set_bm)
bartbedfd232009-03-26 19:07:15 +00001396 {
sewardj1e29ebc2009-07-15 14:49:17 +00001397 VG_(message)(Vg_DebugMsg, "[%d] new conflict set:\n", tid);
bartbedfd232009-03-26 19:07:15 +00001398 DRD_(bm_print)(*conflict_set);
sewardj1e29ebc2009-07-15 14:49:17 +00001399 VG_(message)(Vg_DebugMsg, "[%d] end of new conflict set.\n", tid);
bartbedfd232009-03-26 19:07:15 +00001400 }
sewardjaf44c822007-11-25 14:01:38 +00001401}
1402
bart8f822af2009-06-08 18:20:42 +00001403/**
1404 * Update the conflict set after the vector clock of thread tid has been
1405 * updated from old_vc to its current value, either because a new segment has
1406 * been created or because of a synchronization operation.
1407 */
1408void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
1409 const VectorClock* const old_vc)
1410{
1411 const VectorClock* new_vc;
1412 Segment* p;
1413 unsigned j;
1414
1415 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1416 && tid != DRD_INVALID_THREADID);
1417 tl_assert(old_vc);
1418 tl_assert(tid == DRD_(g_drd_running_tid));
1419 tl_assert(DRD_(g_conflict_set));
1420
1421 if (s_trace_conflict_set)
1422 {
1423 char* str;
1424
1425 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1426 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001427 "updating conflict set for thread %d/%d with vc %s\n",
bart8f822af2009-06-08 18:20:42 +00001428 DRD_(DrdThreadIdToVgThreadId)(tid), tid, str);
1429 VG_(free)(str);
1430 }
1431
1432 new_vc = &DRD_(g_threadinfo)[tid].last->vc;
1433
1434 DRD_(bm_unmark)(DRD_(g_conflict_set));
1435
1436 for (j = 0; j < DRD_N_THREADS; j++)
1437 {
1438 Segment* q;
1439
1440 if (j == tid || ! DRD_(IsValidDrdThreadId)(j))
1441 continue;
1442
1443 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1444 {
1445 const int included_in_old_conflict_set
1446 = ! DRD_(vc_lte)(&q->vc, old_vc)
1447 && ! DRD_(vc_lte)(old_vc, &q->vc);
1448 const int included_in_new_conflict_set
1449 = ! DRD_(vc_lte)(&q->vc, new_vc)
1450 && ! DRD_(vc_lte)(new_vc, &q->vc);
1451 if (included_in_old_conflict_set != included_in_new_conflict_set)
1452 {
1453 if (s_trace_conflict_set)
1454 {
1455 char* str;
1456
1457 str = DRD_(vc_aprint)(&q->vc);
1458 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001459 "conflict set: [%d] merging segment %s\n", j, str);
bart8f822af2009-06-08 18:20:42 +00001460 VG_(free)(str);
1461 }
1462 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1463 }
1464 else
1465 {
1466 if (s_trace_conflict_set)
1467 {
1468 char* str;
1469
1470 str = DRD_(vc_aprint)(&q->vc);
1471 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001472 "conflict set: [%d] ignoring segment %s\n", j, str);
bart8f822af2009-06-08 18:20:42 +00001473 VG_(free)(str);
1474 }
1475 }
1476 }
1477 }
1478
1479 DRD_(bm_clear_marked)(DRD_(g_conflict_set));
1480
1481 p = DRD_(g_threadinfo)[tid].last;
1482 {
1483 for (j = 0; j < DRD_N_THREADS; j++)
1484 {
1485 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1486 {
1487 Segment* q;
1488 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1489 {
1490 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1491 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1492 {
1493 DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1494 }
1495 }
1496 }
1497 }
1498 }
1499
1500 DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set));
1501
bart54803fe2009-06-21 09:26:27 +00001502 s_update_conflict_set_count++;
bart8f822af2009-06-08 18:20:42 +00001503
1504 if (s_trace_conflict_set_bm)
1505 {
sewardj1e29ebc2009-07-15 14:49:17 +00001506 VG_(message)(Vg_DebugMsg, "[%d] updated conflict set:\n", tid);
bart8f822af2009-06-08 18:20:42 +00001507 DRD_(bm_print)(DRD_(g_conflict_set));
sewardj1e29ebc2009-07-15 14:49:17 +00001508 VG_(message)(Vg_DebugMsg, "[%d] end of updated conflict set.\n", tid);
bart8f822af2009-06-08 18:20:42 +00001509 }
1510
1511 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1512}
1513
bart86a87df2009-03-04 19:26:47 +00001514/** Report the number of context switches performed. */
bart62a784c2009-02-15 13:11:14 +00001515ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001516{
bartbedfd232009-03-26 19:07:15 +00001517 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +00001518}
1519
bart86a87df2009-03-04 19:26:47 +00001520/** Report the number of ordered segments that have been discarded. */
bart62a784c2009-02-15 13:11:14 +00001521ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001522{
bartbedfd232009-03-26 19:07:15 +00001523 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +00001524}
1525
bart54803fe2009-06-21 09:26:27 +00001526/** Return how many times the conflict set has been updated entirely. */
1527ULong DRD_(thread_get_compute_conflict_set_count)()
sewardjaf44c822007-11-25 14:01:38 +00001528{
bart54803fe2009-06-21 09:26:27 +00001529 return s_compute_conflict_set_count;
1530}
1531
1532/** Return how many times the conflict set has been updated partially. */
1533ULong DRD_(thread_get_update_conflict_set_count)(void)
1534{
bartbedfd232009-03-26 19:07:15 +00001535 return s_update_conflict_set_count;
sewardjaf44c822007-11-25 14:01:38 +00001536}
1537
bart86a87df2009-03-04 19:26:47 +00001538/**
barte5214662009-06-21 11:51:23 +00001539 * Return how many times the conflict set has been updated partially
1540 * because a new segment has been created.
1541 */
1542ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void)
1543{
1544 return s_update_conflict_set_new_sg_count;
1545}
1546
1547/**
1548 * Return how many times the conflict set has been updated partially
1549 * because of combining vector clocks due to synchronization operations
1550 * other than reader/writer lock or barrier operations.
1551 */
1552ULong DRD_(thread_get_update_conflict_set_sync_count)(void)
1553{
1554 return s_update_conflict_set_sync_count;
1555}
1556
1557/**
1558 * Return how many times the conflict set has been updated partially
1559 * because of thread joins.
1560 */
1561ULong DRD_(thread_get_update_conflict_set_join_count)(void)
1562{
1563 return s_update_conflict_set_join_count;
1564}
1565
1566/**
bart86a87df2009-03-04 19:26:47 +00001567 * Return the number of first-level bitmaps that have been created during
1568 * conflict set updates.
1569 */
bart62a784c2009-02-15 13:11:14 +00001570ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001571{
bartbedfd232009-03-26 19:07:15 +00001572 return s_conflict_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001573}
1574
bart86a87df2009-03-04 19:26:47 +00001575/**
1576 * Return the number of second-level bitmaps that have been created during
1577 * conflict set updates.
1578 */
bart62a784c2009-02-15 13:11:14 +00001579ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001580{
bartbedfd232009-03-26 19:07:15 +00001581 return s_conflict_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001582}