blob: d61ae066865b457b94004274f848352d7e5244b8 [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;
79static int s_segment_merge_interval = 64;
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 }
354 VG_(message)(Vg_DebugMsg, "%s", msg);
355 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;"
587 " segments: %llu",
588 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,
786 "Discarding ordered segments -- min vc is %s, max vc is %s",
787 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);
976 VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s",
977 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,
987 &DRD_(g_threadinfo)[joinee].last->vc);
988 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,
995 &DRD_(g_threadinfo)[joinee].last->vc);
996 }
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);
1004 VG_(message)(Vg_DebugMsg, "After join: %s", str);
1005 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
1011 * the vector clock of segment sg. Call this function after thread tid had
1012 * to wait because of thread synchronization until the memory accesses in the
1013 * segment sg finished.
bart26f73e12008-02-24 18:37:08 +00001014 */
bart8f822af2009-06-08 18:20:42 +00001015void DRD_(thread_combine_vc_sync)(DrdThreadId tid, const Segment* sg)
sewardjaf44c822007-11-25 14:01:38 +00001016{
bart8f822af2009-06-08 18:20:42 +00001017 const VectorClock* const vc = &sg->vc;
1018
bartbedfd232009-03-26 19:07:15 +00001019 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1020 && tid != DRD_INVALID_THREADID);
1021 tl_assert(DRD_(g_threadinfo)[tid].last);
bart8f822af2009-06-08 18:20:42 +00001022 tl_assert(sg);
bartbedfd232009-03-26 19:07:15 +00001023 tl_assert(vc);
bart8f822af2009-06-08 18:20:42 +00001024
1025 if (tid != sg->tid)
1026 {
1027 VectorClock old_vc;
1028
1029 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc);
1030 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc);
1031 if (DRD_(sg_get_trace)())
1032 {
1033 char *str1, *str2;
1034 str1 = DRD_(vc_aprint)(&old_vc);
1035 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1036 VG_(message)(Vg_DebugMsg, "thread %d: vc %s -> %s", tid, str1, str2);
1037 VG_(free)(str1);
1038 VG_(free)(str2);
1039 }
barte5214662009-06-21 11:51:23 +00001040
bart8f822af2009-06-08 18:20:42 +00001041 thread_discard_ordered_segments();
barte5214662009-06-21 11:51:23 +00001042
bart8f822af2009-06-08 18:20:42 +00001043 DRD_(thread_update_conflict_set)(tid, &old_vc);
barte5214662009-06-21 11:51:23 +00001044 s_update_conflict_set_sync_count++;
1045
bart8f822af2009-06-08 18:20:42 +00001046 DRD_(vc_cleanup)(&old_vc);
1047 }
1048 else
1049 {
1050 tl_assert(DRD_(vc_lte)(vc, &DRD_(g_threadinfo)[tid].last->vc));
1051 }
sewardjaf44c822007-11-25 14:01:38 +00001052}
1053
bart324a23b2009-02-15 12:14:52 +00001054/**
1055 * Call this function whenever a thread is no longer using the memory
1056 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
1057 * increase.
bart26f73e12008-02-24 18:37:08 +00001058 */
bart62a784c2009-02-15 13:11:14 +00001059void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +00001060{
bartbedfd232009-03-26 19:07:15 +00001061 DrdThreadId other_user;
1062 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001063
bartbedfd232009-03-26 19:07:15 +00001064 /* For all threads, mark the range [ a1, a2 [ as no longer in use. */
1065 other_user = DRD_INVALID_THREADID;
bart8f822af2009-06-08 18:20:42 +00001066 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001067 {
1068 Segment* p;
1069 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
sewardjaf44c822007-11-25 14:01:38 +00001070 {
bartbedfd232009-03-26 19:07:15 +00001071 if (other_user == DRD_INVALID_THREADID
1072 && i != DRD_(g_drd_running_tid))
1073 {
bart8f822af2009-06-08 18:20:42 +00001074 if (UNLIKELY(DRD_(bm_test_and_clear)(DRD_(sg_bm)(p), a1, a2)))
bartbedfd232009-03-26 19:07:15 +00001075 {
1076 other_user = i;
1077 }
1078 continue;
1079 }
bart8f822af2009-06-08 18:20:42 +00001080 DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001081 }
bartbedfd232009-03-26 19:07:15 +00001082 }
sewardjaf44c822007-11-25 14:01:38 +00001083
bartbedfd232009-03-26 19:07:15 +00001084 /*
1085 * If any other thread had accessed memory in [ a1, a2 [, update the
1086 * conflict set.
1087 */
1088 if (other_user != DRD_INVALID_THREADID
1089 && DRD_(bm_has_any_access)(DRD_(g_conflict_set), a1, a2))
1090 {
1091 thread_compute_conflict_set(&DRD_(g_conflict_set),
1092 DRD_(thread_get_running_tid)());
1093 }
sewardjaf44c822007-11-25 14:01:38 +00001094}
1095
bartd45d9952009-05-31 18:53:54 +00001096/** Specify whether memory loads should be recorded. */
1097void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001098{
bartbedfd232009-03-26 19:07:15 +00001099 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1100 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001101 tl_assert(enabled == !! enabled);
1102
1103 DRD_(g_threadinfo)[tid].is_recording_loads = enabled;
bart0268dfa2008-03-11 20:10:21 +00001104}
1105
bartd45d9952009-05-31 18:53:54 +00001106/** Specify whether memory stores should be recorded. */
1107void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001108{
bartbedfd232009-03-26 19:07:15 +00001109 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1110 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001111 tl_assert(enabled == !! enabled);
1112
1113 DRD_(g_threadinfo)[tid].is_recording_stores = enabled;
bart0268dfa2008-03-11 20:10:21 +00001114}
1115
bart86a87df2009-03-04 19:26:47 +00001116/**
1117 * Print the segment information for all threads.
1118 *
1119 * This function is only used for debugging purposes.
1120 */
bart62a784c2009-02-15 13:11:14 +00001121void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +00001122{
bartbedfd232009-03-26 19:07:15 +00001123 unsigned i;
1124 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001125
bart8f822af2009-06-08 18:20:42 +00001126 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001127 {
1128 if (DRD_(g_threadinfo)[i].first)
sewardjaf44c822007-11-25 14:01:38 +00001129 {
bartbedfd232009-03-26 19:07:15 +00001130 VG_(printf)("**************\n"
1131 "* thread %3d (%d/%d/%d/0x%lx/%d) *\n"
1132 "**************\n",
1133 i,
1134 DRD_(g_threadinfo)[i].vg_thread_exists,
1135 DRD_(g_threadinfo)[i].vg_threadid,
1136 DRD_(g_threadinfo)[i].posix_thread_exists,
1137 DRD_(g_threadinfo)[i].pt_threadid,
1138 DRD_(g_threadinfo)[i].detached_posix_thread);
1139 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
1140 {
1141 DRD_(sg_print)(p);
1142 }
sewardjaf44c822007-11-25 14:01:38 +00001143 }
bartbedfd232009-03-26 19:07:15 +00001144 }
sewardjaf44c822007-11-25 14:01:38 +00001145}
1146
bart86a87df2009-03-04 19:26:47 +00001147/** Show a call stack involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001148static void show_call_stack(const DrdThreadId tid,
1149 const Char* const msg,
1150 ExeContext* const callstack)
1151{
bartbedfd232009-03-26 19:07:15 +00001152 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +00001153
bartbedfd232009-03-26 19:07:15 +00001154 VG_(message)(Vg_UserMsg, "%s (thread %d/%d)", msg, vg_tid, tid);
sewardjaf44c822007-11-25 14:01:38 +00001155
bartbedfd232009-03-26 19:07:15 +00001156 if (vg_tid != VG_INVALID_THREADID)
1157 {
1158 if (callstack)
1159 {
1160 VG_(pp_ExeContext)(callstack);
1161 }
1162 else
1163 {
1164 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
1165 }
1166 }
1167 else
1168 {
1169 VG_(message)(Vg_UserMsg,
1170 " (thread finished, call stack no longer available)");
1171 }
sewardjaf44c822007-11-25 14:01:38 +00001172}
1173
bart86a87df2009-03-04 19:26:47 +00001174/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001175static void
1176thread_report_conflicting_segments_segment(const DrdThreadId tid,
1177 const Addr addr,
1178 const SizeT size,
1179 const BmAccessTypeT access_type,
1180 const Segment* const p)
1181{
bartbedfd232009-03-26 19:07:15 +00001182 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001183
bartbedfd232009-03-26 19:07:15 +00001184 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1185 && tid != DRD_INVALID_THREADID);
1186 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +00001187
bart8f822af2009-06-08 18:20:42 +00001188 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001189 {
1190 if (i != tid)
sewardjaf44c822007-11-25 14:01:38 +00001191 {
bartbedfd232009-03-26 19:07:15 +00001192 Segment* q;
1193 for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev)
1194 {
1195 /*
1196 * Since q iterates over the segments of thread i in order of
1197 * decreasing vector clocks, if q->vc <= p->vc, then
1198 * q->next->vc <= p->vc will also hold. Hence, break out of the
1199 * loop once this condition is met.
1200 */
1201 if (DRD_(vc_lte)(&q->vc, &p->vc))
1202 break;
1203 if (! DRD_(vc_lte)(&p->vc, &q->vc))
1204 {
bart8f822af2009-06-08 18:20:42 +00001205 if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size,
bartbedfd232009-03-26 19:07:15 +00001206 access_type))
1207 {
1208 tl_assert(q->stacktrace);
1209 show_call_stack(i, "Other segment start",
1210 q->stacktrace);
1211 show_call_stack(i, "Other segment end",
1212 q->next ? q->next->stacktrace : 0);
1213 }
1214 }
1215 }
sewardjaf44c822007-11-25 14:01:38 +00001216 }
bartbedfd232009-03-26 19:07:15 +00001217 }
sewardjaf44c822007-11-25 14:01:38 +00001218}
1219
bart86a87df2009-03-04 19:26:47 +00001220/** Print information about all segments involved in a data race. */
bart62a784c2009-02-15 13:11:14 +00001221void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1222 const Addr addr,
1223 const SizeT size,
1224 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +00001225{
bartbedfd232009-03-26 19:07:15 +00001226 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001227
bartbedfd232009-03-26 19:07:15 +00001228 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1229 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001230
bartbedfd232009-03-26 19:07:15 +00001231 for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next)
1232 {
bart8f822af2009-06-08 18:20:42 +00001233 if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type))
bartbedfd232009-03-26 19:07:15 +00001234 {
1235 thread_report_conflicting_segments_segment(tid, addr, size,
1236 access_type, p);
1237 }
1238 }
sewardjaf44c822007-11-25 14:01:38 +00001239}
sewardjaf44c822007-11-25 14:01:38 +00001240
bart324a23b2009-02-15 12:14:52 +00001241/**
bart8f822af2009-06-08 18:20:42 +00001242 * Verify whether the conflict set for thread tid is up to date. Only perform
1243 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
1244 */
1245static Bool thread_conflict_set_up_to_date(const DrdThreadId tid)
1246{
1247 static int do_verify_conflict_set = -1;
1248 Bool result;
1249 struct bitmap* computed_conflict_set = 0;
1250
1251 if (do_verify_conflict_set < 0)
1252 do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0;
1253
1254 if (do_verify_conflict_set == 0)
1255 return True;
1256
1257 thread_compute_conflict_set(&computed_conflict_set, tid);
1258 result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set);
1259 if (! result)
1260 {
1261 VG_(printf)("actual conflict set:\n");
1262 DRD_(bm_print)(DRD_(g_conflict_set));
1263 VG_(printf)("\n");
1264 VG_(printf)("computed conflict set:\n");
1265 DRD_(bm_print)(computed_conflict_set);
1266 VG_(printf)("\n");
1267 }
1268 DRD_(bm_delete)(computed_conflict_set);
1269 return result;
1270}
1271
1272/**
1273 * Compute the conflict set: a bitmap that represents the union of all memory
1274 * accesses of all segments that are unordered to the current segment of the
1275 * thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001276 */
bart86a87df2009-03-04 19:26:47 +00001277static void thread_compute_conflict_set(struct bitmap** conflict_set,
1278 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001279{
bartbedfd232009-03-26 19:07:15 +00001280 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001281
bartbedfd232009-03-26 19:07:15 +00001282 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1283 && tid != DRD_INVALID_THREADID);
1284 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001285
bart54803fe2009-06-21 09:26:27 +00001286 s_compute_conflict_set_count++;
bartbedfd232009-03-26 19:07:15 +00001287 s_conflict_set_bitmap_creation_count
1288 -= DRD_(bm_get_bitmap_creation_count)();
1289 s_conflict_set_bitmap2_creation_count
1290 -= DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001291
bartbedfd232009-03-26 19:07:15 +00001292 if (*conflict_set)
1293 {
1294 DRD_(bm_delete)(*conflict_set);
1295 }
1296 *conflict_set = DRD_(bm_new)();
bart26f73e12008-02-24 18:37:08 +00001297
bartbedfd232009-03-26 19:07:15 +00001298 if (s_trace_conflict_set)
1299 {
bart8f822af2009-06-08 18:20:42 +00001300 char* str;
bart26f73e12008-02-24 18:37:08 +00001301
bart8f822af2009-06-08 18:20:42 +00001302 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1303 VG_(message)(Vg_DebugMsg,
1304 "computing conflict set for thread %d/%d with vc %s",
1305 DRD_(DrdThreadIdToVgThreadId)(tid), tid, str);
1306 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001307 }
sewardjaf44c822007-11-25 14:01:38 +00001308
bartbedfd232009-03-26 19:07:15 +00001309 p = DRD_(g_threadinfo)[tid].last;
1310 {
1311 unsigned j;
1312
1313 if (s_trace_conflict_set)
bart26f73e12008-02-24 18:37:08 +00001314 {
bart8f822af2009-06-08 18:20:42 +00001315 char* vc;
bartbedfd232009-03-26 19:07:15 +00001316
bart8f822af2009-06-08 18:20:42 +00001317 vc = DRD_(vc_aprint)(&p->vc);
1318 VG_(message)(Vg_DebugMsg, "conflict set: thread [%d] at vc %s",
1319 tid, vc);
1320 VG_(free)(vc);
bart26f73e12008-02-24 18:37:08 +00001321 }
sewardjaf44c822007-11-25 14:01:38 +00001322
bart8f822af2009-06-08 18:20:42 +00001323 for (j = 0; j < DRD_N_THREADS; j++)
bartbedfd232009-03-26 19:07:15 +00001324 {
1325 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1326 {
bart8f822af2009-06-08 18:20:42 +00001327 Segment* q;
bartbedfd232009-03-26 19:07:15 +00001328 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1329 {
1330 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1331 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1332 {
1333 if (s_trace_conflict_set)
1334 {
bart8f822af2009-06-08 18:20:42 +00001335 char* str;
1336
1337 str = DRD_(vc_aprint)(&q->vc);
1338 VG_(message)(Vg_DebugMsg,
1339 "conflict set: [%d] merging segment %s",
1340 j, str);
1341 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001342 }
bart8f822af2009-06-08 18:20:42 +00001343 DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q));
bartbedfd232009-03-26 19:07:15 +00001344 }
1345 else
1346 {
1347 if (s_trace_conflict_set)
1348 {
bart8f822af2009-06-08 18:20:42 +00001349 char* str;
1350
1351 str = DRD_(vc_aprint)(&q->vc);
1352 VG_(message)(Vg_DebugMsg,
1353 "conflict set: [%d] ignoring segment %s",
1354 j, str);
1355 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001356 }
1357 }
1358 }
1359 }
1360 }
1361 }
sewardjaf44c822007-11-25 14:01:38 +00001362
bartbedfd232009-03-26 19:07:15 +00001363 s_conflict_set_bitmap_creation_count
1364 += DRD_(bm_get_bitmap_creation_count)();
1365 s_conflict_set_bitmap2_creation_count
1366 += DRD_(bm_get_bitmap2_creation_count)();
1367
bart8f822af2009-06-08 18:20:42 +00001368 if (s_trace_conflict_set_bm)
bartbedfd232009-03-26 19:07:15 +00001369 {
bart8f822af2009-06-08 18:20:42 +00001370 VG_(message)(Vg_DebugMsg, "[%d] new conflict set:", tid);
bartbedfd232009-03-26 19:07:15 +00001371 DRD_(bm_print)(*conflict_set);
bart8f822af2009-06-08 18:20:42 +00001372 VG_(message)(Vg_DebugMsg, "[%d] end of new conflict set.", tid);
bartbedfd232009-03-26 19:07:15 +00001373 }
sewardjaf44c822007-11-25 14:01:38 +00001374}
1375
bart8f822af2009-06-08 18:20:42 +00001376/**
1377 * Update the conflict set after the vector clock of thread tid has been
1378 * updated from old_vc to its current value, either because a new segment has
1379 * been created or because of a synchronization operation.
1380 */
1381void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
1382 const VectorClock* const old_vc)
1383{
1384 const VectorClock* new_vc;
1385 Segment* p;
1386 unsigned j;
1387
1388 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1389 && tid != DRD_INVALID_THREADID);
1390 tl_assert(old_vc);
1391 tl_assert(tid == DRD_(g_drd_running_tid));
1392 tl_assert(DRD_(g_conflict_set));
1393
1394 if (s_trace_conflict_set)
1395 {
1396 char* str;
1397
1398 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1399 VG_(message)(Vg_DebugMsg,
1400 "updating conflict set for thread %d/%d with vc %s",
1401 DRD_(DrdThreadIdToVgThreadId)(tid), tid, str);
1402 VG_(free)(str);
1403 }
1404
1405 new_vc = &DRD_(g_threadinfo)[tid].last->vc;
1406
1407 DRD_(bm_unmark)(DRD_(g_conflict_set));
1408
1409 for (j = 0; j < DRD_N_THREADS; j++)
1410 {
1411 Segment* q;
1412
1413 if (j == tid || ! DRD_(IsValidDrdThreadId)(j))
1414 continue;
1415
1416 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1417 {
1418 const int included_in_old_conflict_set
1419 = ! DRD_(vc_lte)(&q->vc, old_vc)
1420 && ! DRD_(vc_lte)(old_vc, &q->vc);
1421 const int included_in_new_conflict_set
1422 = ! DRD_(vc_lte)(&q->vc, new_vc)
1423 && ! DRD_(vc_lte)(new_vc, &q->vc);
1424 if (included_in_old_conflict_set != included_in_new_conflict_set)
1425 {
1426 if (s_trace_conflict_set)
1427 {
1428 char* str;
1429
1430 str = DRD_(vc_aprint)(&q->vc);
1431 VG_(message)(Vg_DebugMsg,
1432 "conflict set: [%d] merging segment %s", j, str);
1433 VG_(free)(str);
1434 }
1435 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1436 }
1437 else
1438 {
1439 if (s_trace_conflict_set)
1440 {
1441 char* str;
1442
1443 str = DRD_(vc_aprint)(&q->vc);
1444 VG_(message)(Vg_DebugMsg,
1445 "conflict set: [%d] ignoring segment %s", j, str);
1446 VG_(free)(str);
1447 }
1448 }
1449 }
1450 }
1451
1452 DRD_(bm_clear_marked)(DRD_(g_conflict_set));
1453
1454 p = DRD_(g_threadinfo)[tid].last;
1455 {
1456 for (j = 0; j < DRD_N_THREADS; j++)
1457 {
1458 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1459 {
1460 Segment* q;
1461 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1462 {
1463 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1464 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1465 {
1466 DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1467 }
1468 }
1469 }
1470 }
1471 }
1472
1473 DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set));
1474
bart54803fe2009-06-21 09:26:27 +00001475 s_update_conflict_set_count++;
bart8f822af2009-06-08 18:20:42 +00001476
1477 if (s_trace_conflict_set_bm)
1478 {
1479 VG_(message)(Vg_DebugMsg, "[%d] updated conflict set:", tid);
1480 DRD_(bm_print)(DRD_(g_conflict_set));
1481 VG_(message)(Vg_DebugMsg, "[%d] end of updated conflict set.", tid);
1482 }
1483
1484 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1485}
1486
bart86a87df2009-03-04 19:26:47 +00001487/** Report the number of context switches performed. */
bart62a784c2009-02-15 13:11:14 +00001488ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001489{
bartbedfd232009-03-26 19:07:15 +00001490 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +00001491}
1492
bart86a87df2009-03-04 19:26:47 +00001493/** Report the number of ordered segments that have been discarded. */
bart62a784c2009-02-15 13:11:14 +00001494ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001495{
bartbedfd232009-03-26 19:07:15 +00001496 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +00001497}
1498
bart54803fe2009-06-21 09:26:27 +00001499/** Return how many times the conflict set has been updated entirely. */
1500ULong DRD_(thread_get_compute_conflict_set_count)()
sewardjaf44c822007-11-25 14:01:38 +00001501{
bart54803fe2009-06-21 09:26:27 +00001502 return s_compute_conflict_set_count;
1503}
1504
1505/** Return how many times the conflict set has been updated partially. */
1506ULong DRD_(thread_get_update_conflict_set_count)(void)
1507{
bartbedfd232009-03-26 19:07:15 +00001508 return s_update_conflict_set_count;
sewardjaf44c822007-11-25 14:01:38 +00001509}
1510
bart86a87df2009-03-04 19:26:47 +00001511/**
barte5214662009-06-21 11:51:23 +00001512 * Return how many times the conflict set has been updated partially
1513 * because a new segment has been created.
1514 */
1515ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void)
1516{
1517 return s_update_conflict_set_new_sg_count;
1518}
1519
1520/**
1521 * Return how many times the conflict set has been updated partially
1522 * because of combining vector clocks due to synchronization operations
1523 * other than reader/writer lock or barrier operations.
1524 */
1525ULong DRD_(thread_get_update_conflict_set_sync_count)(void)
1526{
1527 return s_update_conflict_set_sync_count;
1528}
1529
1530/**
1531 * Return how many times the conflict set has been updated partially
1532 * because of thread joins.
1533 */
1534ULong DRD_(thread_get_update_conflict_set_join_count)(void)
1535{
1536 return s_update_conflict_set_join_count;
1537}
1538
1539/**
bart86a87df2009-03-04 19:26:47 +00001540 * Return the number of first-level bitmaps that have been created during
1541 * conflict set updates.
1542 */
bart62a784c2009-02-15 13:11:14 +00001543ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001544{
bartbedfd232009-03-26 19:07:15 +00001545 return s_conflict_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001546}
1547
bart86a87df2009-03-04 19:26:47 +00001548/**
1549 * Return the number of second-level bitmaps that have been created during
1550 * conflict set updates.
1551 */
bart62a784c2009-02-15 13:11:14 +00001552ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001553{
bartbedfd232009-03-26 19:07:15 +00001554 return s_conflict_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001555}