blob: 04ec9125afda1f571657640dabc18f30575f3871 [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);
bartbedfd232009-03-26 19:07:15 +0000336 const unsigned msg_size = 256;
337 char* msg;
bart09dc13f2009-02-14 15:13:31 +0000338
bartbedfd232009-03-26 19:07:15 +0000339 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
340 tl_assert(msg);
341 VG_(snprintf)(msg, msg_size,
bart63c92ea2009-07-19 17:53:56 +0000342 "drd_post_thread_join joiner = %d, joinee = %d",
343 drd_joiner, drd_joinee);
bartbedfd232009-03-26 19:07:15 +0000344 if (joiner)
345 {
bart8f822af2009-06-08 18:20:42 +0000346 char* vc;
347
348 vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(drd_joiner));
bartbedfd232009-03-26 19:07:15 +0000349 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart8f822af2009-06-08 18:20:42 +0000350 ", new vc: %s", vc);
351 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000352 }
sewardj1e29ebc2009-07-15 14:49:17 +0000353 VG_(message)(Vg_DebugMsg, "%s\n", msg);
bartbedfd232009-03-26 19:07:15 +0000354 VG_(free)(msg);
355 }
bart09dc13f2009-02-14 15:13:31 +0000356
bartbedfd232009-03-26 19:07:15 +0000357 if (! DRD_(get_check_stack_accesses)())
358 {
359 DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee)
360 - DRD_(thread_get_stack_size)(drd_joinee),
361 DRD_(thread_get_stack_max)(drd_joinee));
362 }
363 DRD_(clientobj_delete_thread)(drd_joinee);
364 DRD_(thread_delete)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000365}
366
bart324a23b2009-02-15 12:14:52 +0000367/**
368 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
369 * and accesses this data structure from multiple threads without locking.
370 * Any conflicting accesses in the range stack_startup..stack_max will be
371 * ignored.
372 */
bart62a784c2009-02-15 13:11:14 +0000373void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
374 const Addr stack_startup)
sewardjaf44c822007-11-25 14:01:38 +0000375{
bartbedfd232009-03-26 19:07:15 +0000376 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
377 && tid != DRD_INVALID_THREADID);
378 tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup);
379 tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max);
380 DRD_(g_threadinfo)[tid].stack_startup = stack_startup;
sewardjaf44c822007-11-25 14:01:38 +0000381}
382
bart86a87df2009-03-04 19:26:47 +0000383/** Return the stack pointer for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000384Addr DRD_(thread_get_stack_min)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000385{
bartbedfd232009-03-26 19:07:15 +0000386 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
387 && tid != DRD_INVALID_THREADID);
388 return DRD_(g_threadinfo)[tid].stack_min;
sewardjaf44c822007-11-25 14:01:38 +0000389}
390
bart86a87df2009-03-04 19:26:47 +0000391/**
392 * Return the lowest value that was ever assigned to the stack pointer
393 * for the specified thread.
394 */
bart62a784c2009-02-15 13:11:14 +0000395Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000396{
bartbedfd232009-03-26 19:07:15 +0000397 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
398 && tid != DRD_INVALID_THREADID);
399 return DRD_(g_threadinfo)[tid].stack_min_min;
bartcac53462008-03-29 09:27:08 +0000400}
401
bart86a87df2009-03-04 19:26:47 +0000402/** Return the top address for the stack of the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000403Addr DRD_(thread_get_stack_max)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000404{
bartbedfd232009-03-26 19:07:15 +0000405 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
406 && tid != DRD_INVALID_THREADID);
407 return DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000408}
409
bart86a87df2009-03-04 19:26:47 +0000410/** Return the maximum stack size for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000411SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000412{
bartbedfd232009-03-26 19:07:15 +0000413 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
414 && tid != DRD_INVALID_THREADID);
415 return DRD_(g_threadinfo)[tid].stack_size;
bartcac53462008-03-29 09:27:08 +0000416}
417
bart09dc13f2009-02-14 15:13:31 +0000418/**
419 * Clean up thread-specific data structures. Call this just after
420 * pthread_join().
sewardjaf44c822007-11-25 14:01:38 +0000421 */
bart62a784c2009-02-15 13:11:14 +0000422void DRD_(thread_delete)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000423{
bartbedfd232009-03-26 19:07:15 +0000424 Segment* sg;
425 Segment* sg_prev;
sewardjaf44c822007-11-25 14:01:38 +0000426
bartbedfd232009-03-26 19:07:15 +0000427 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart86a87df2009-03-04 19:26:47 +0000428
bartbedfd232009-03-26 19:07:15 +0000429 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0);
430 for (sg = DRD_(g_threadinfo)[tid].last; sg; sg = sg_prev)
431 {
432 sg_prev = sg->prev;
433 sg->prev = 0;
434 sg->next = 0;
435 DRD_(sg_put)(sg);
436 }
437 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
438 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
439 tl_assert(DRD_(g_threadinfo)[tid].detached_posix_thread == False);
440 DRD_(g_threadinfo)[tid].first = 0;
441 DRD_(g_threadinfo)[tid].last = 0;
bart86a87df2009-03-04 19:26:47 +0000442
bartbedfd232009-03-26 19:07:15 +0000443 tl_assert(! DRD_(IsValidDrdThreadId)(tid));
sewardjaf44c822007-11-25 14:01:38 +0000444}
445
bart324a23b2009-02-15 12:14:52 +0000446/**
447 * Called after a thread performed its last memory access and before
448 * thread_delete() is called. Note: thread_delete() is only called for
449 * joinable threads, not for detached threads.
450 */
bart62a784c2009-02-15 13:11:14 +0000451void DRD_(thread_finished)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000452{
bartbedfd232009-03-26 19:07:15 +0000453 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
454 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000455
bartbedfd232009-03-26 19:07:15 +0000456 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
sewardjaf44c822007-11-25 14:01:38 +0000457
bartbedfd232009-03-26 19:07:15 +0000458 if (DRD_(g_threadinfo)[tid].detached_posix_thread)
459 {
460 /*
461 * Once a detached thread has finished, its stack is deallocated and
462 * should no longer be taken into account when computing the conflict set.
463 */
464 DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000465
bartbedfd232009-03-26 19:07:15 +0000466 /*
467 * For a detached thread, calling pthread_exit() invalidates the
468 * POSIX thread ID associated with the detached thread. For joinable
469 * POSIX threads however, the POSIX thread ID remains live after the
470 * pthread_exit() call until pthread_join() is called.
471 */
472 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
473 }
sewardjaf44c822007-11-25 14:01:38 +0000474}
475
bart9b2974a2008-09-27 12:35:31 +0000476/** Called just before pthread_cancel(). */
bart62a784c2009-02-15 13:11:14 +0000477void DRD_(thread_pre_cancel)(const DrdThreadId tid)
bartaf0691b2008-09-27 12:26:50 +0000478{
bartbedfd232009-03-26 19:07:15 +0000479 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
480 && tid != DRD_INVALID_THREADID);
481 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bartaf0691b2008-09-27 12:26:50 +0000482
bartbedfd232009-03-26 19:07:15 +0000483 DRD_(g_threadinfo)[tid].synchr_nesting = 0;
bartaf0691b2008-09-27 12:26:50 +0000484}
485
barte7dff242009-04-23 17:12:39 +0000486/**
487 * Store the POSIX thread ID for the specified thread.
488 *
489 * @note This function can be called two times for the same thread -- see also
490 * the comment block preceding the pthread_create() wrapper in
491 * drd_pthread_intercepts.c.
492 */
bart62a784c2009-02-15 13:11:14 +0000493void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
sewardjaf44c822007-11-25 14:01:38 +0000494{
bartbedfd232009-03-26 19:07:15 +0000495 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
496 && tid != DRD_INVALID_THREADID);
barte7dff242009-04-23 17:12:39 +0000497 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID
498 || DRD_(g_threadinfo)[tid].pt_threadid == ptid);
bartbedfd232009-03-26 19:07:15 +0000499 tl_assert(ptid != INVALID_POSIX_THREADID);
500 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
501 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardjaf44c822007-11-25 14:01:38 +0000502}
503
bart86a87df2009-03-04 19:26:47 +0000504/** Returns true for joinable threads and false for detached threads. */
bart62a784c2009-02-15 13:11:14 +0000505Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000506{
bartbedfd232009-03-26 19:07:15 +0000507 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
508 && tid != DRD_INVALID_THREADID);
509 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000510}
511
bart86a87df2009-03-04 19:26:47 +0000512/** Store the thread mode: joinable or detached. */
bart62a784c2009-02-15 13:11:14 +0000513void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000514{
bartbedfd232009-03-26 19:07:15 +0000515 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
516 && tid != DRD_INVALID_THREADID);
517 tl_assert(!! joinable == joinable);
518 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bart8f822af2009-06-08 18:20:42 +0000519
bartbedfd232009-03-26 19:07:15 +0000520 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000521}
522
bartd45d9952009-05-31 18:53:54 +0000523/** Obtain the thread number and the user-assigned thread name. */
524const char* DRD_(thread_get_name)(const DrdThreadId tid)
525{
526 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
527 && tid != DRD_INVALID_THREADID);
528
529 return DRD_(g_threadinfo)[tid].name;
530}
531
532/** Set the name of the specified thread. */
533void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name)
534{
535 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
536 && tid != DRD_INVALID_THREADID);
537
538 if (name == NULL || name[0] == 0)
539 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
540 sizeof(DRD_(g_threadinfo)[tid].name),
541 "Thread %d",
542 tid);
543 else
544 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
545 sizeof(DRD_(g_threadinfo)[tid].name),
546 "Thread %d (%s)",
547 tid, name);
548 DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0;
549}
550
bart86a87df2009-03-04 19:26:47 +0000551/**
552 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
553 * conflict set.
554 */
bart62a784c2009-02-15 13:11:14 +0000555void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000556{
bartbedfd232009-03-26 19:07:15 +0000557 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000558
bartbedfd232009-03-26 19:07:15 +0000559 if (vg_tid != s_vg_running_tid)
560 {
561 DRD_(thread_set_running_tid)(vg_tid,
562 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
563 }
sewardj8b09d4f2007-12-04 21:27:18 +0000564
bartbedfd232009-03-26 19:07:15 +0000565 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
566 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000567}
568
bart86a87df2009-03-04 19:26:47 +0000569/**
570 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
571 * conflict set.
572 */
bart62a784c2009-02-15 13:11:14 +0000573void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
574 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000575{
bartbedfd232009-03-26 19:07:15 +0000576 tl_assert(vg_tid != VG_INVALID_THREADID);
577 tl_assert(drd_tid != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000578
bartbedfd232009-03-26 19:07:15 +0000579 if (vg_tid != s_vg_running_tid)
580 {
581 if (s_trace_context_switches
582 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
583 {
584 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +0000585 "Context switch from thread %d to thread %d;"
sewardj1e29ebc2009-07-15 14:49:17 +0000586 " segments: %llu\n",
bart63c92ea2009-07-19 17:53:56 +0000587 DRD_(g_drd_running_tid), drd_tid,
bartbedfd232009-03-26 19:07:15 +0000588 DRD_(sg_get_segments_alive_count)());
589 }
590 s_vg_running_tid = vg_tid;
591 DRD_(g_drd_running_tid) = drd_tid;
592 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
593 s_context_switch_count++;
594 }
sewardj8b09d4f2007-12-04 21:27:18 +0000595
bartbedfd232009-03-26 19:07:15 +0000596 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
597 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000598}
599
bart86a87df2009-03-04 19:26:47 +0000600/**
601 * Increase the synchronization nesting counter. Must be called before the
602 * client calls a synchronization function.
603 */
bart62a784c2009-02-15 13:11:14 +0000604int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000605{
bartbedfd232009-03-26 19:07:15 +0000606 tl_assert(DRD_(IsValidDrdThreadId)(tid));
607 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000608}
609
bart86a87df2009-03-04 19:26:47 +0000610/**
611 * Decrease the synchronization nesting counter. Must be called after the
612 * client left a synchronization function.
613 */
bart62a784c2009-02-15 13:11:14 +0000614int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000615{
bartbedfd232009-03-26 19:07:15 +0000616 tl_assert(DRD_(IsValidDrdThreadId)(tid));
617 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
618 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000619}
620
bart86a87df2009-03-04 19:26:47 +0000621/** Returns the synchronization nesting counter. */
bart62a784c2009-02-15 13:11:14 +0000622int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000623{
bartbedfd232009-03-26 19:07:15 +0000624 tl_assert(DRD_(IsValidDrdThreadId)(tid));
625 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000626}
627
bart1a473c72008-03-13 19:03:38 +0000628/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000629static
bart86a87df2009-03-04 19:26:47 +0000630void thread_append_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000631{
bartbedfd232009-03-26 19:07:15 +0000632 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
633 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000634
635#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
636 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
637#endif
638
bartbedfd232009-03-26 19:07:15 +0000639 sg->prev = DRD_(g_threadinfo)[tid].last;
640 sg->next = 0;
641 if (DRD_(g_threadinfo)[tid].last)
642 DRD_(g_threadinfo)[tid].last->next = sg;
643 DRD_(g_threadinfo)[tid].last = sg;
644 if (DRD_(g_threadinfo)[tid].first == 0)
645 DRD_(g_threadinfo)[tid].first = sg;
bart8f822af2009-06-08 18:20:42 +0000646
647#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
648 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
649#endif
sewardjaf44c822007-11-25 14:01:38 +0000650}
651
bart324a23b2009-02-15 12:14:52 +0000652/**
653 * Remove a segment from the segment list of thread threadid, and free the
654 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000655 */
bart62a784c2009-02-15 13:11:14 +0000656static
bart86a87df2009-03-04 19:26:47 +0000657void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000658{
bartbedfd232009-03-26 19:07:15 +0000659 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
660 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000661
662#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
663 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
664#endif
bart26f73e12008-02-24 18:37:08 +0000665
bartbedfd232009-03-26 19:07:15 +0000666 if (sg->prev)
667 sg->prev->next = sg->next;
668 if (sg->next)
669 sg->next->prev = sg->prev;
670 if (sg == DRD_(g_threadinfo)[tid].first)
671 DRD_(g_threadinfo)[tid].first = sg->next;
672 if (sg == DRD_(g_threadinfo)[tid].last)
673 DRD_(g_threadinfo)[tid].last = sg->prev;
674 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000675
bart8f822af2009-06-08 18:20:42 +0000676#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
677 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
678#endif
sewardjaf44c822007-11-25 14:01:38 +0000679}
680
bart86a87df2009-03-04 19:26:47 +0000681/**
682 * Returns a pointer to the vector clock of the most recent segment associated
683 * with thread 'tid'.
684 */
bart62a784c2009-02-15 13:11:14 +0000685VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000686{
bartbedfd232009-03-26 19:07:15 +0000687 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
688 && tid != DRD_INVALID_THREADID);
689 tl_assert(DRD_(g_threadinfo)[tid].last);
690 return &DRD_(g_threadinfo)[tid].last->vc;
sewardjaf44c822007-11-25 14:01:38 +0000691}
692
bart324a23b2009-02-15 12:14:52 +0000693/**
694 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000695 */
bart62a784c2009-02-15 13:11:14 +0000696void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000697{
bartbedfd232009-03-26 19:07:15 +0000698 tl_assert(sg);
699 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
700 && tid != DRD_INVALID_THREADID);
701 tl_assert(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000702
bartbedfd232009-03-26 19:07:15 +0000703 DRD_(sg_put)(*sg);
704 *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000705}
706
sewardjaf44c822007-11-25 14:01:38 +0000707/**
708 * Compute the minimum of all latest vector clocks of all threads
709 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
bart86a87df2009-03-04 19:26:47 +0000710 *
sewardjaf44c822007-11-25 14:01:38 +0000711 * @param vc pointer to a vectorclock, holds result upon return.
712 */
bart62a784c2009-02-15 13:11:14 +0000713static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000714{
bartbedfd232009-03-26 19:07:15 +0000715 unsigned i;
716 Bool first;
717 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000718
bartbedfd232009-03-26 19:07:15 +0000719 first = True;
bart8f822af2009-06-08 18:20:42 +0000720 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000721 {
722 latest_sg = DRD_(g_threadinfo)[i].last;
723 if (latest_sg)
724 {
725 if (first)
726 DRD_(vc_assign)(vc, &latest_sg->vc);
727 else
728 DRD_(vc_min)(vc, &latest_sg->vc);
729 first = False;
730 }
731 }
sewardjaf44c822007-11-25 14:01:38 +0000732}
733
bart86a87df2009-03-04 19:26:47 +0000734/**
735 * Compute the maximum of all latest vector clocks of all threads.
736 *
737 * @param vc pointer to a vectorclock, holds result upon return.
738 */
bart62a784c2009-02-15 13:11:14 +0000739static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000740{
bartbedfd232009-03-26 19:07:15 +0000741 unsigned i;
742 Bool first;
743 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000744
bartbedfd232009-03-26 19:07:15 +0000745 first = True;
bart8f822af2009-06-08 18:20:42 +0000746 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000747 {
748 latest_sg = DRD_(g_threadinfo)[i].last;
749 if (latest_sg)
750 {
751 if (first)
752 DRD_(vc_assign)(vc, &latest_sg->vc);
753 else
754 DRD_(vc_combine)(vc, &latest_sg->vc);
755 first = False;
756 }
757 }
sewardjaf44c822007-11-25 14:01:38 +0000758}
759
760/**
bart5bd9f2d2008-03-03 20:31:58 +0000761 * Discard all segments that have a defined order against the latest vector
bart86a87df2009-03-04 19:26:47 +0000762 * clock of all threads -- these segments can no longer be involved in a
sewardjaf44c822007-11-25 14:01:38 +0000763 * data race.
764 */
bart8f822af2009-06-08 18:20:42 +0000765static void thread_discard_ordered_segments(void)
sewardjaf44c822007-11-25 14:01:38 +0000766{
bartbedfd232009-03-26 19:07:15 +0000767 unsigned i;
768 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000769
bartbedfd232009-03-26 19:07:15 +0000770 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000771
bartbedfd232009-03-26 19:07:15 +0000772 DRD_(vc_init)(&thread_vc_min, 0, 0);
773 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
774 if (DRD_(sg_get_trace)())
775 {
bart8f822af2009-06-08 18:20:42 +0000776 char *vc_min, *vc_max;
bartbedfd232009-03-26 19:07:15 +0000777 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000778
bartbedfd232009-03-26 19:07:15 +0000779 DRD_(vc_init)(&thread_vc_max, 0, 0);
780 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
bart8f822af2009-06-08 18:20:42 +0000781 vc_min = DRD_(vc_aprint)(&thread_vc_min);
782 vc_max = DRD_(vc_aprint)(&thread_vc_max);
783 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000784 "Discarding ordered segments -- min vc is %s, max vc is %s\n",
bart8f822af2009-06-08 18:20:42 +0000785 vc_min, vc_max);
786 VG_(free)(vc_min);
787 VG_(free)(vc_max);
bartbedfd232009-03-26 19:07:15 +0000788 DRD_(vc_cleanup)(&thread_vc_max);
789 }
sewardjaf44c822007-11-25 14:01:38 +0000790
bart8f822af2009-06-08 18:20:42 +0000791 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000792 {
793 Segment* sg;
794 Segment* sg_next;
795 for (sg = DRD_(g_threadinfo)[i].first;
796 sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
797 sg = sg_next)
798 {
799 thread_discard_segment(i, sg);
800 }
801 }
802 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +0000803}
804
bart324a23b2009-02-15 12:14:52 +0000805/**
bart8f822af2009-06-08 18:20:42 +0000806 * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper
807 * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if
808 * all segments in the set CS are ordered consistently against both sg1 and
809 * sg2. The set CS is defined as the set of segments that can immediately
810 * precede future segments via inter-thread synchronization operations. In
811 * DRD the set CS consists of the latest segment of each thread combined with
812 * all segments for which the reference count is strictly greater than one.
813 * The code below is an optimized version of the following:
814 *
815 * for (i = 0; i < DRD_N_THREADS; i++)
816 * {
817 * Segment* sg;
818 *
819 * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
820 * {
821 * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1)
822 * {
823 * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc)
824 * != DRD_(vc_lte)(&sg2->vc, &sg->vc)
825 * || DRD_(vc_lte)(&sg->vc, &sg1->vc)
826 * != DRD_(vc_lte)(&sg->vc, &sg2->vc))
827 * {
828 * return False;
829 * }
830 * }
831 * }
832 * }
833 */
834static Bool thread_consistent_segment_ordering(const DrdThreadId tid,
835 Segment* const sg1,
836 Segment* const sg2)
837{
838 unsigned i;
839
840 tl_assert(sg1->next);
841 tl_assert(sg2->next);
842 tl_assert(sg1->next == sg2);
843 tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc));
844
845 for (i = 0; i < DRD_N_THREADS; i++)
846 {
847 Segment* sg;
848
849 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
850 {
851 if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1)
852 {
853 if (DRD_(vc_lte)(&sg2->vc, &sg->vc))
854 break;
855 if (DRD_(vc_lte)(&sg1->vc, &sg->vc))
856 return False;
857 }
858 }
859 for (sg = DRD_(g_threadinfo)[i].last; sg; sg = sg->prev)
860 {
861 if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1)
862 {
863 if (DRD_(vc_lte)(&sg->vc, &sg1->vc))
864 break;
865 if (DRD_(vc_lte)(&sg->vc, &sg2->vc))
866 return False;
867 }
868 }
869 }
870 return True;
871}
872
873/**
bart324a23b2009-02-15 12:14:52 +0000874 * Merge all segments that may be merged without triggering false positives
875 * or discarding real data races. For the theoretical background of segment
bart8f822af2009-06-08 18:20:42 +0000876 * merging, see also the following paper: Mark Christiaens, Michiel Ronsse
877 * and Koen De Bosschere. Bounding the number of segment histories during
878 * data race detection. Parallel Computing archive, Volume 28, Issue 9,
879 * pp 1221-1238, September 2002. This paper contains a proof that merging
880 * consecutive segments for which the property equiv(s1,s2) holds can be
881 * merged without reducing the accuracy of datarace detection. Furthermore
882 * it is also proven that the total number of all segments will never grow
883 * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged
884 * every time a new segment is created. The property equiv(s1, s2) is defined
885 * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector
886 * clocks of segments s and s1 are ordered in the same way as those of segments
887 * s and s2. The set CS is defined as the set of existing segments s that have
888 * the potential to conflict with not yet created segments, either because the
889 * segment s is the latest segment of a thread or because it can become the
890 * immediate predecessor of a new segment due to a synchronization operation.
barta9c37392008-03-22 09:38:48 +0000891 */
892static void thread_merge_segments(void)
893{
bartbedfd232009-03-26 19:07:15 +0000894 unsigned i;
barta9c37392008-03-22 09:38:48 +0000895
bart8f822af2009-06-08 18:20:42 +0000896 s_new_segments_since_last_merge = 0;
897
898 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000899 {
900 Segment* sg;
barta9c37392008-03-22 09:38:48 +0000901
bart8f822af2009-06-08 18:20:42 +0000902#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
903 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
904#endif
barta9c37392008-03-22 09:38:48 +0000905
bartbedfd232009-03-26 19:07:15 +0000906 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
barta9c37392008-03-22 09:38:48 +0000907 {
bartbedfd232009-03-26 19:07:15 +0000908 if (DRD_(sg_get_refcnt)(sg) == 1
909 && sg->next
910 && DRD_(sg_get_refcnt)(sg->next) == 1
bart8f822af2009-06-08 18:20:42 +0000911 && sg->next->next
912 && thread_consistent_segment_ordering(i, sg, sg->next))
bartbedfd232009-03-26 19:07:15 +0000913 {
914 /* Merge sg and sg->next into sg. */
915 DRD_(sg_merge)(sg, sg->next);
916 thread_discard_segment(i, sg->next);
917 }
barta9c37392008-03-22 09:38:48 +0000918 }
barta9c37392008-03-22 09:38:48 +0000919
bart8f822af2009-06-08 18:20:42 +0000920#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
921 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
922#endif
bartbedfd232009-03-26 19:07:15 +0000923 }
barta9c37392008-03-22 09:38:48 +0000924}
925
bart324a23b2009-02-15 12:14:52 +0000926/**
bart324a23b2009-02-15 12:14:52 +0000927 * Create a new segment for the specified thread, and discard any segments
928 * that cannot cause races anymore.
sewardjaf44c822007-11-25 14:01:38 +0000929 */
bart62a784c2009-02-15 13:11:14 +0000930void DRD_(thread_new_segment)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000931{
bart8f822af2009-06-08 18:20:42 +0000932 Segment* last_sg;
bartbedfd232009-03-26 19:07:15 +0000933 Segment* new_sg;
bartd66e3a82008-04-06 15:02:17 +0000934
bartbedfd232009-03-26 19:07:15 +0000935 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
936 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000937 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +0000938
bart8f822af2009-06-08 18:20:42 +0000939 last_sg = DRD_(g_threadinfo)[tid].last;
bartbedfd232009-03-26 19:07:15 +0000940 new_sg = DRD_(sg_new)(tid, tid);
941 thread_append_segment(tid, new_sg);
bart8f822af2009-06-08 18:20:42 +0000942 if (tid == DRD_(g_drd_running_tid) && last_sg)
barte5214662009-06-21 11:51:23 +0000943 {
bart8f822af2009-06-08 18:20:42 +0000944 DRD_(thread_update_conflict_set)(tid, &last_sg->vc);
barte5214662009-06-21 11:51:23 +0000945 s_update_conflict_set_new_sg_count++;
946 }
bartd66e3a82008-04-06 15:02:17 +0000947
bart8f822af2009-06-08 18:20:42 +0000948 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +0000949
bart8f822af2009-06-08 18:20:42 +0000950 if (s_segment_merging
951 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
bartbedfd232009-03-26 19:07:15 +0000952 {
bart8f822af2009-06-08 18:20:42 +0000953 thread_discard_ordered_segments();
bartbedfd232009-03-26 19:07:15 +0000954 thread_merge_segments();
955 }
sewardjaf44c822007-11-25 14:01:38 +0000956}
957
bart26f73e12008-02-24 18:37:08 +0000958/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart8f822af2009-06-08 18:20:42 +0000959void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +0000960{
bartbedfd232009-03-26 19:07:15 +0000961 tl_assert(joiner != joinee);
962 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
963 && joiner != DRD_INVALID_THREADID);
964 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
965 && joinee != DRD_INVALID_THREADID);
966 tl_assert(DRD_(g_threadinfo)[joiner].last);
967 tl_assert(DRD_(g_threadinfo)[joinee].last);
bart8f822af2009-06-08 18:20:42 +0000968
969 if (DRD_(sg_get_trace)())
970 {
971 char *str1, *str2;
972 str1 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc);
973 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joinee].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +0000974 VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n",
bart8f822af2009-06-08 18:20:42 +0000975 str1, str2);
976 VG_(free)(str1);
977 VG_(free)(str2);
978 }
barte5214662009-06-21 11:51:23 +0000979 if (joiner == DRD_(g_drd_running_tid))
980 {
981 VectorClock old_vc;
982
983 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[joiner].last->vc);
984 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
bartf6ec1fe2009-06-21 18:07:35 +0000985 &DRD_(g_threadinfo)[joinee].last->vc);
barte5214662009-06-21 11:51:23 +0000986 DRD_(thread_update_conflict_set)(joiner, &old_vc);
987 s_update_conflict_set_join_count++;
988 DRD_(vc_cleanup)(&old_vc);
989 }
990 else
991 {
992 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
bartf6ec1fe2009-06-21 18:07:35 +0000993 &DRD_(g_threadinfo)[joinee].last->vc);
barte5214662009-06-21 11:51:23 +0000994 }
995
996 thread_discard_ordered_segments();
997
bart8f822af2009-06-08 18:20:42 +0000998 if (DRD_(sg_get_trace)())
999 {
1000 char* str;
1001 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001002 VG_(message)(Vg_DebugMsg, "After join: %s\n", str);
bart8f822af2009-06-08 18:20:42 +00001003 VG_(free)(str);
1004 }
sewardjaf44c822007-11-25 14:01:38 +00001005}
1006
bart324a23b2009-02-15 12:14:52 +00001007/**
bart8f822af2009-06-08 18:20:42 +00001008 * Update the vector clock of the last segment of thread tid with the
bartf6ec1fe2009-06-21 18:07:35 +00001009 * the vector clock of segment sg.
bart26f73e12008-02-24 18:37:08 +00001010 */
bartf6ec1fe2009-06-21 18:07:35 +00001011static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg)
sewardjaf44c822007-11-25 14:01:38 +00001012{
bart8f822af2009-06-08 18:20:42 +00001013 const VectorClock* const vc = &sg->vc;
1014
bartbedfd232009-03-26 19:07:15 +00001015 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1016 && tid != DRD_INVALID_THREADID);
1017 tl_assert(DRD_(g_threadinfo)[tid].last);
bart8f822af2009-06-08 18:20:42 +00001018 tl_assert(sg);
bartbedfd232009-03-26 19:07:15 +00001019 tl_assert(vc);
bart8f822af2009-06-08 18:20:42 +00001020
1021 if (tid != sg->tid)
1022 {
1023 VectorClock old_vc;
1024
1025 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc);
1026 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc);
1027 if (DRD_(sg_get_trace)())
1028 {
1029 char *str1, *str2;
1030 str1 = DRD_(vc_aprint)(&old_vc);
1031 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001032 VG_(message)(Vg_DebugMsg, "thread %d: vc %s -> %s\n", tid, str1, str2);
bart8f822af2009-06-08 18:20:42 +00001033 VG_(free)(str1);
1034 VG_(free)(str2);
1035 }
barte5214662009-06-21 11:51:23 +00001036
bart8f822af2009-06-08 18:20:42 +00001037 thread_discard_ordered_segments();
barte5214662009-06-21 11:51:23 +00001038
bart8f822af2009-06-08 18:20:42 +00001039 DRD_(thread_update_conflict_set)(tid, &old_vc);
barte5214662009-06-21 11:51:23 +00001040 s_update_conflict_set_sync_count++;
1041
bart8f822af2009-06-08 18:20:42 +00001042 DRD_(vc_cleanup)(&old_vc);
1043 }
1044 else
1045 {
1046 tl_assert(DRD_(vc_lte)(vc, &DRD_(g_threadinfo)[tid].last->vc));
1047 }
sewardjaf44c822007-11-25 14:01:38 +00001048}
1049
bart324a23b2009-02-15 12:14:52 +00001050/**
bartf6ec1fe2009-06-21 18:07:35 +00001051 * Create a new segment for thread tid and update the vector clock of the last
1052 * segment of this thread with the the vector clock of segment sg. Call this
1053 * function after thread tid had to wait because of thread synchronization
1054 * until the memory accesses in the segment sg finished.
1055 */
1056void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, const Segment* sg)
1057{
1058 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1059 && tid != DRD_INVALID_THREADID);
1060 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1061 tl_assert(sg);
1062
1063 thread_append_segment(tid, DRD_(sg_new)(tid, tid));
1064
1065 thread_combine_vc_sync(tid, sg);
1066
1067 if (s_segment_merging
1068 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
1069 {
1070 thread_discard_ordered_segments();
1071 thread_merge_segments();
1072 }
1073}
1074
1075/**
bart324a23b2009-02-15 12:14:52 +00001076 * Call this function whenever a thread is no longer using the memory
1077 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
1078 * increase.
bart26f73e12008-02-24 18:37:08 +00001079 */
bart62a784c2009-02-15 13:11:14 +00001080void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +00001081{
bartbedfd232009-03-26 19:07:15 +00001082 DrdThreadId other_user;
1083 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001084
bartbedfd232009-03-26 19:07:15 +00001085 /* For all threads, mark the range [ a1, a2 [ as no longer in use. */
1086 other_user = DRD_INVALID_THREADID;
bart8f822af2009-06-08 18:20:42 +00001087 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001088 {
1089 Segment* p;
1090 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
sewardjaf44c822007-11-25 14:01:38 +00001091 {
bartbedfd232009-03-26 19:07:15 +00001092 if (other_user == DRD_INVALID_THREADID
1093 && i != DRD_(g_drd_running_tid))
1094 {
bart8f822af2009-06-08 18:20:42 +00001095 if (UNLIKELY(DRD_(bm_test_and_clear)(DRD_(sg_bm)(p), a1, a2)))
bartbedfd232009-03-26 19:07:15 +00001096 {
1097 other_user = i;
1098 }
1099 continue;
1100 }
bart8f822af2009-06-08 18:20:42 +00001101 DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001102 }
bartbedfd232009-03-26 19:07:15 +00001103 }
sewardjaf44c822007-11-25 14:01:38 +00001104
bartbedfd232009-03-26 19:07:15 +00001105 /*
1106 * If any other thread had accessed memory in [ a1, a2 [, update the
1107 * conflict set.
1108 */
1109 if (other_user != DRD_INVALID_THREADID
1110 && DRD_(bm_has_any_access)(DRD_(g_conflict_set), a1, a2))
1111 {
1112 thread_compute_conflict_set(&DRD_(g_conflict_set),
1113 DRD_(thread_get_running_tid)());
1114 }
sewardjaf44c822007-11-25 14:01:38 +00001115}
1116
bartd45d9952009-05-31 18:53:54 +00001117/** Specify whether memory loads should be recorded. */
1118void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001119{
bartbedfd232009-03-26 19:07:15 +00001120 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1121 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001122 tl_assert(enabled == !! enabled);
1123
1124 DRD_(g_threadinfo)[tid].is_recording_loads = enabled;
bart0268dfa2008-03-11 20:10:21 +00001125}
1126
bartd45d9952009-05-31 18:53:54 +00001127/** Specify whether memory stores should be recorded. */
1128void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001129{
bartbedfd232009-03-26 19:07:15 +00001130 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1131 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001132 tl_assert(enabled == !! enabled);
1133
1134 DRD_(g_threadinfo)[tid].is_recording_stores = enabled;
bart0268dfa2008-03-11 20:10:21 +00001135}
1136
bart86a87df2009-03-04 19:26:47 +00001137/**
1138 * Print the segment information for all threads.
1139 *
1140 * This function is only used for debugging purposes.
1141 */
bart62a784c2009-02-15 13:11:14 +00001142void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +00001143{
bartbedfd232009-03-26 19:07:15 +00001144 unsigned i;
1145 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001146
bart8f822af2009-06-08 18:20:42 +00001147 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001148 {
1149 if (DRD_(g_threadinfo)[i].first)
sewardjaf44c822007-11-25 14:01:38 +00001150 {
bartbedfd232009-03-26 19:07:15 +00001151 VG_(printf)("**************\n"
1152 "* thread %3d (%d/%d/%d/0x%lx/%d) *\n"
1153 "**************\n",
1154 i,
1155 DRD_(g_threadinfo)[i].vg_thread_exists,
1156 DRD_(g_threadinfo)[i].vg_threadid,
1157 DRD_(g_threadinfo)[i].posix_thread_exists,
1158 DRD_(g_threadinfo)[i].pt_threadid,
1159 DRD_(g_threadinfo)[i].detached_posix_thread);
1160 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
1161 {
1162 DRD_(sg_print)(p);
1163 }
sewardjaf44c822007-11-25 14:01:38 +00001164 }
bartbedfd232009-03-26 19:07:15 +00001165 }
sewardjaf44c822007-11-25 14:01:38 +00001166}
1167
bart86a87df2009-03-04 19:26:47 +00001168/** Show a call stack involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001169static void show_call_stack(const DrdThreadId tid,
1170 const Char* const msg,
1171 ExeContext* const callstack)
1172{
bartbedfd232009-03-26 19:07:15 +00001173 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +00001174
bart63c92ea2009-07-19 17:53:56 +00001175 VG_(message)(Vg_UserMsg, "%s (thread %d)\n", msg, tid);
sewardjaf44c822007-11-25 14:01:38 +00001176
bartbedfd232009-03-26 19:07:15 +00001177 if (vg_tid != VG_INVALID_THREADID)
1178 {
1179 if (callstack)
1180 {
1181 VG_(pp_ExeContext)(callstack);
1182 }
1183 else
1184 {
1185 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
1186 }
1187 }
1188 else
1189 {
1190 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001191 " (thread finished, call stack no longer available)\n");
bartbedfd232009-03-26 19:07:15 +00001192 }
sewardjaf44c822007-11-25 14:01:38 +00001193}
1194
bart86a87df2009-03-04 19:26:47 +00001195/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001196static void
1197thread_report_conflicting_segments_segment(const DrdThreadId tid,
1198 const Addr addr,
1199 const SizeT size,
1200 const BmAccessTypeT access_type,
1201 const Segment* const p)
1202{
bartbedfd232009-03-26 19:07:15 +00001203 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001204
bartbedfd232009-03-26 19:07:15 +00001205 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1206 && tid != DRD_INVALID_THREADID);
1207 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +00001208
bart8f822af2009-06-08 18:20:42 +00001209 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001210 {
1211 if (i != tid)
sewardjaf44c822007-11-25 14:01:38 +00001212 {
bartbedfd232009-03-26 19:07:15 +00001213 Segment* q;
1214 for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev)
1215 {
1216 /*
1217 * Since q iterates over the segments of thread i in order of
1218 * decreasing vector clocks, if q->vc <= p->vc, then
1219 * q->next->vc <= p->vc will also hold. Hence, break out of the
1220 * loop once this condition is met.
1221 */
1222 if (DRD_(vc_lte)(&q->vc, &p->vc))
1223 break;
1224 if (! DRD_(vc_lte)(&p->vc, &q->vc))
1225 {
bart8f822af2009-06-08 18:20:42 +00001226 if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size,
bartbedfd232009-03-26 19:07:15 +00001227 access_type))
1228 {
1229 tl_assert(q->stacktrace);
1230 show_call_stack(i, "Other segment start",
1231 q->stacktrace);
1232 show_call_stack(i, "Other segment end",
1233 q->next ? q->next->stacktrace : 0);
1234 }
1235 }
1236 }
sewardjaf44c822007-11-25 14:01:38 +00001237 }
bartbedfd232009-03-26 19:07:15 +00001238 }
sewardjaf44c822007-11-25 14:01:38 +00001239}
1240
bart86a87df2009-03-04 19:26:47 +00001241/** Print information about all segments involved in a data race. */
bart62a784c2009-02-15 13:11:14 +00001242void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1243 const Addr addr,
1244 const SizeT size,
1245 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +00001246{
bartbedfd232009-03-26 19:07:15 +00001247 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001248
bartbedfd232009-03-26 19:07:15 +00001249 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1250 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001251
bartbedfd232009-03-26 19:07:15 +00001252 for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next)
1253 {
bart8f822af2009-06-08 18:20:42 +00001254 if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type))
bartbedfd232009-03-26 19:07:15 +00001255 {
1256 thread_report_conflicting_segments_segment(tid, addr, size,
1257 access_type, p);
1258 }
1259 }
sewardjaf44c822007-11-25 14:01:38 +00001260}
sewardjaf44c822007-11-25 14:01:38 +00001261
bart324a23b2009-02-15 12:14:52 +00001262/**
bart8f822af2009-06-08 18:20:42 +00001263 * Verify whether the conflict set for thread tid is up to date. Only perform
1264 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
1265 */
1266static Bool thread_conflict_set_up_to_date(const DrdThreadId tid)
1267{
1268 static int do_verify_conflict_set = -1;
1269 Bool result;
1270 struct bitmap* computed_conflict_set = 0;
1271
1272 if (do_verify_conflict_set < 0)
1273 do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0;
1274
1275 if (do_verify_conflict_set == 0)
1276 return True;
1277
1278 thread_compute_conflict_set(&computed_conflict_set, tid);
1279 result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set);
1280 if (! result)
1281 {
1282 VG_(printf)("actual conflict set:\n");
1283 DRD_(bm_print)(DRD_(g_conflict_set));
1284 VG_(printf)("\n");
1285 VG_(printf)("computed conflict set:\n");
1286 DRD_(bm_print)(computed_conflict_set);
1287 VG_(printf)("\n");
1288 }
1289 DRD_(bm_delete)(computed_conflict_set);
1290 return result;
1291}
1292
1293/**
1294 * Compute the conflict set: a bitmap that represents the union of all memory
1295 * accesses of all segments that are unordered to the current segment of the
1296 * thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001297 */
bart86a87df2009-03-04 19:26:47 +00001298static void thread_compute_conflict_set(struct bitmap** conflict_set,
1299 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001300{
bartbedfd232009-03-26 19:07:15 +00001301 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001302
bartbedfd232009-03-26 19:07:15 +00001303 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1304 && tid != DRD_INVALID_THREADID);
1305 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001306
bart54803fe2009-06-21 09:26:27 +00001307 s_compute_conflict_set_count++;
bartbedfd232009-03-26 19:07:15 +00001308 s_conflict_set_bitmap_creation_count
1309 -= DRD_(bm_get_bitmap_creation_count)();
1310 s_conflict_set_bitmap2_creation_count
1311 -= DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001312
bartbedfd232009-03-26 19:07:15 +00001313 if (*conflict_set)
1314 {
bartf6ec1fe2009-06-21 18:07:35 +00001315 DRD_(bm_cleanup)(*conflict_set);
1316 DRD_(bm_init)(*conflict_set);
bartbedfd232009-03-26 19:07:15 +00001317 }
bartf6ec1fe2009-06-21 18:07:35 +00001318 else
1319 {
1320 *conflict_set = DRD_(bm_new)();
1321 }
bart26f73e12008-02-24 18:37:08 +00001322
bartbedfd232009-03-26 19:07:15 +00001323 if (s_trace_conflict_set)
1324 {
bart8f822af2009-06-08 18:20:42 +00001325 char* str;
bart26f73e12008-02-24 18:37:08 +00001326
bart8f822af2009-06-08 18:20:42 +00001327 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1328 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +00001329 "computing conflict set for thread %d with vc %s\n",
1330 tid, str);
bart8f822af2009-06-08 18:20:42 +00001331 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001332 }
sewardjaf44c822007-11-25 14:01:38 +00001333
bartbedfd232009-03-26 19:07:15 +00001334 p = DRD_(g_threadinfo)[tid].last;
1335 {
1336 unsigned j;
1337
1338 if (s_trace_conflict_set)
bart26f73e12008-02-24 18:37:08 +00001339 {
bart8f822af2009-06-08 18:20:42 +00001340 char* vc;
bartbedfd232009-03-26 19:07:15 +00001341
bart8f822af2009-06-08 18:20:42 +00001342 vc = DRD_(vc_aprint)(&p->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001343 VG_(message)(Vg_DebugMsg, "conflict set: thread [%d] at vc %s\n",
bart8f822af2009-06-08 18:20:42 +00001344 tid, vc);
1345 VG_(free)(vc);
bart26f73e12008-02-24 18:37:08 +00001346 }
sewardjaf44c822007-11-25 14:01:38 +00001347
bart8f822af2009-06-08 18:20:42 +00001348 for (j = 0; j < DRD_N_THREADS; j++)
bartbedfd232009-03-26 19:07:15 +00001349 {
1350 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1351 {
bart8f822af2009-06-08 18:20:42 +00001352 Segment* q;
bartbedfd232009-03-26 19:07:15 +00001353 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1354 {
1355 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1356 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1357 {
1358 if (s_trace_conflict_set)
1359 {
bart8f822af2009-06-08 18:20:42 +00001360 char* str;
1361
1362 str = DRD_(vc_aprint)(&q->vc);
1363 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001364 "conflict set: [%d] merging segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001365 j, str);
1366 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001367 }
bart8f822af2009-06-08 18:20:42 +00001368 DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q));
bartbedfd232009-03-26 19:07:15 +00001369 }
1370 else
1371 {
1372 if (s_trace_conflict_set)
1373 {
bart8f822af2009-06-08 18:20:42 +00001374 char* str;
1375
1376 str = DRD_(vc_aprint)(&q->vc);
1377 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001378 "conflict set: [%d] ignoring segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001379 j, str);
1380 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001381 }
1382 }
1383 }
1384 }
1385 }
1386 }
sewardjaf44c822007-11-25 14:01:38 +00001387
bartbedfd232009-03-26 19:07:15 +00001388 s_conflict_set_bitmap_creation_count
1389 += DRD_(bm_get_bitmap_creation_count)();
1390 s_conflict_set_bitmap2_creation_count
1391 += DRD_(bm_get_bitmap2_creation_count)();
1392
bart8f822af2009-06-08 18:20:42 +00001393 if (s_trace_conflict_set_bm)
bartbedfd232009-03-26 19:07:15 +00001394 {
sewardj1e29ebc2009-07-15 14:49:17 +00001395 VG_(message)(Vg_DebugMsg, "[%d] new conflict set:\n", tid);
bartbedfd232009-03-26 19:07:15 +00001396 DRD_(bm_print)(*conflict_set);
sewardj1e29ebc2009-07-15 14:49:17 +00001397 VG_(message)(Vg_DebugMsg, "[%d] end of new conflict set.\n", tid);
bartbedfd232009-03-26 19:07:15 +00001398 }
sewardjaf44c822007-11-25 14:01:38 +00001399}
1400
bart8f822af2009-06-08 18:20:42 +00001401/**
1402 * Update the conflict set after the vector clock of thread tid has been
1403 * updated from old_vc to its current value, either because a new segment has
1404 * been created or because of a synchronization operation.
1405 */
1406void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
1407 const VectorClock* const old_vc)
1408{
1409 const VectorClock* new_vc;
1410 Segment* p;
1411 unsigned j;
1412
1413 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1414 && tid != DRD_INVALID_THREADID);
1415 tl_assert(old_vc);
1416 tl_assert(tid == DRD_(g_drd_running_tid));
1417 tl_assert(DRD_(g_conflict_set));
1418
1419 if (s_trace_conflict_set)
1420 {
1421 char* str;
1422
1423 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1424 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +00001425 "updating conflict set for thread %d with vc %s\n",
1426 tid, str);
bart8f822af2009-06-08 18:20:42 +00001427 VG_(free)(str);
1428 }
1429
1430 new_vc = &DRD_(g_threadinfo)[tid].last->vc;
1431
1432 DRD_(bm_unmark)(DRD_(g_conflict_set));
1433
1434 for (j = 0; j < DRD_N_THREADS; j++)
1435 {
1436 Segment* q;
1437
1438 if (j == tid || ! DRD_(IsValidDrdThreadId)(j))
1439 continue;
1440
1441 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1442 {
1443 const int included_in_old_conflict_set
1444 = ! DRD_(vc_lte)(&q->vc, old_vc)
1445 && ! DRD_(vc_lte)(old_vc, &q->vc);
1446 const int included_in_new_conflict_set
1447 = ! DRD_(vc_lte)(&q->vc, new_vc)
1448 && ! DRD_(vc_lte)(new_vc, &q->vc);
1449 if (included_in_old_conflict_set != included_in_new_conflict_set)
1450 {
1451 if (s_trace_conflict_set)
1452 {
1453 char* str;
1454
1455 str = DRD_(vc_aprint)(&q->vc);
1456 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001457 "conflict set: [%d] merging segment %s\n", j, str);
bart8f822af2009-06-08 18:20:42 +00001458 VG_(free)(str);
1459 }
1460 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1461 }
1462 else
1463 {
1464 if (s_trace_conflict_set)
1465 {
1466 char* str;
1467
1468 str = DRD_(vc_aprint)(&q->vc);
1469 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001470 "conflict set: [%d] ignoring segment %s\n", j, str);
bart8f822af2009-06-08 18:20:42 +00001471 VG_(free)(str);
1472 }
1473 }
1474 }
1475 }
1476
1477 DRD_(bm_clear_marked)(DRD_(g_conflict_set));
1478
1479 p = DRD_(g_threadinfo)[tid].last;
1480 {
1481 for (j = 0; j < DRD_N_THREADS; j++)
1482 {
1483 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1484 {
1485 Segment* q;
1486 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1487 {
1488 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1489 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1490 {
1491 DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1492 }
1493 }
1494 }
1495 }
1496 }
1497
1498 DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set));
1499
bart54803fe2009-06-21 09:26:27 +00001500 s_update_conflict_set_count++;
bart8f822af2009-06-08 18:20:42 +00001501
1502 if (s_trace_conflict_set_bm)
1503 {
sewardj1e29ebc2009-07-15 14:49:17 +00001504 VG_(message)(Vg_DebugMsg, "[%d] updated conflict set:\n", tid);
bart8f822af2009-06-08 18:20:42 +00001505 DRD_(bm_print)(DRD_(g_conflict_set));
sewardj1e29ebc2009-07-15 14:49:17 +00001506 VG_(message)(Vg_DebugMsg, "[%d] end of updated conflict set.\n", tid);
bart8f822af2009-06-08 18:20:42 +00001507 }
1508
1509 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1510}
1511
bart86a87df2009-03-04 19:26:47 +00001512/** Report the number of context switches performed. */
bart62a784c2009-02-15 13:11:14 +00001513ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001514{
bartbedfd232009-03-26 19:07:15 +00001515 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +00001516}
1517
bart86a87df2009-03-04 19:26:47 +00001518/** Report the number of ordered segments that have been discarded. */
bart62a784c2009-02-15 13:11:14 +00001519ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001520{
bartbedfd232009-03-26 19:07:15 +00001521 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +00001522}
1523
bart54803fe2009-06-21 09:26:27 +00001524/** Return how many times the conflict set has been updated entirely. */
1525ULong DRD_(thread_get_compute_conflict_set_count)()
sewardjaf44c822007-11-25 14:01:38 +00001526{
bart54803fe2009-06-21 09:26:27 +00001527 return s_compute_conflict_set_count;
1528}
1529
1530/** Return how many times the conflict set has been updated partially. */
1531ULong DRD_(thread_get_update_conflict_set_count)(void)
1532{
bartbedfd232009-03-26 19:07:15 +00001533 return s_update_conflict_set_count;
sewardjaf44c822007-11-25 14:01:38 +00001534}
1535
bart86a87df2009-03-04 19:26:47 +00001536/**
barte5214662009-06-21 11:51:23 +00001537 * Return how many times the conflict set has been updated partially
1538 * because a new segment has been created.
1539 */
1540ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void)
1541{
1542 return s_update_conflict_set_new_sg_count;
1543}
1544
1545/**
1546 * Return how many times the conflict set has been updated partially
1547 * because of combining vector clocks due to synchronization operations
1548 * other than reader/writer lock or barrier operations.
1549 */
1550ULong DRD_(thread_get_update_conflict_set_sync_count)(void)
1551{
1552 return s_update_conflict_set_sync_count;
1553}
1554
1555/**
1556 * Return how many times the conflict set has been updated partially
1557 * because of thread joins.
1558 */
1559ULong DRD_(thread_get_update_conflict_set_join_count)(void)
1560{
1561 return s_update_conflict_set_join_count;
1562}
1563
1564/**
bart86a87df2009-03-04 19:26:47 +00001565 * Return the number of first-level bitmaps that have been created during
1566 * conflict set updates.
1567 */
bart62a784c2009-02-15 13:11:14 +00001568ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001569{
bartbedfd232009-03-26 19:07:15 +00001570 return s_conflict_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001571}
1572
bart86a87df2009-03-04 19:26:47 +00001573/**
1574 * Return the number of second-level bitmaps that have been created during
1575 * conflict set updates.
1576 */
bart62a784c2009-02-15 13:11:14 +00001577ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001578{
bartbedfd232009-03-26 19:07:15 +00001579 return s_conflict_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001580}