blob: 909bb45a1989bf237261fecb015a694eb2bf9688 [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
bart876cafd2010-10-10 18:07:31 +00005 Copyright (C) 2006-2010 Bart Van Assche <bvanassche@acm.org>.
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
sewardjaf44c822007-11-25 14:01:38 +000037#include "pub_tool_libcassert.h" // tl_assert()
38#include "pub_tool_libcbase.h" // VG_(strlen)()
39#include "pub_tool_libcprint.h" // VG_(printf)()
bart82195c12008-04-13 17:35:08 +000040#include "pub_tool_libcproc.h" // VG_(getenv)()
sewardjaf44c822007-11-25 14:01:38 +000041#include "pub_tool_machine.h"
42#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardj85642922008-01-14 11:54:56 +000043#include "pub_tool_options.h" // VG_(clo_backtrace_size)
sewardjaf44c822007-11-25 14:01:38 +000044#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
45
bart32ba2082008-06-05 08:53:42 +000046
sewardjaf44c822007-11-25 14:01:38 +000047
bart324a23b2009-02-15 12:14:52 +000048/* Local functions. */
sewardjaf44c822007-11-25 14:01:38 +000049
bart86a87df2009-03-04 19:26:47 +000050static void thread_append_segment(const DrdThreadId tid, Segment* const sg);
51static void thread_discard_segment(const DrdThreadId tid, Segment* const sg);
52static void thread_compute_conflict_set(struct bitmap** conflict_set,
53 const DrdThreadId tid);
bart8f822af2009-06-08 18:20:42 +000054static Bool thread_conflict_set_up_to_date(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +000055
56
bart324a23b2009-02-15 12:14:52 +000057/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000058
bart86a87df2009-03-04 19:26:47 +000059static ULong s_context_switch_count;
60static ULong s_discard_ordered_segments_count;
bart54803fe2009-06-21 09:26:27 +000061static ULong s_compute_conflict_set_count;
bart86a87df2009-03-04 19:26:47 +000062static ULong s_update_conflict_set_count;
barte5214662009-06-21 11:51:23 +000063static ULong s_update_conflict_set_new_sg_count;
64static ULong s_update_conflict_set_sync_count;
65static ULong s_update_conflict_set_join_count;
bart86a87df2009-03-04 19:26:47 +000066static ULong s_conflict_set_bitmap_creation_count;
67static ULong s_conflict_set_bitmap2_creation_count;
68static ThreadId s_vg_running_tid = VG_INVALID_THREADID;
bart324a23b2009-02-15 12:14:52 +000069DrdThreadId DRD_(g_drd_running_tid) = DRD_INVALID_THREADID;
70ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS];
71struct bitmap* DRD_(g_conflict_set);
bart86a87df2009-03-04 19:26:47 +000072static Bool s_trace_context_switches = False;
73static Bool s_trace_conflict_set = False;
bart8f822af2009-06-08 18:20:42 +000074static Bool s_trace_conflict_set_bm = False;
bart86a87df2009-03-04 19:26:47 +000075static Bool s_trace_fork_join = False;
76static Bool s_segment_merging = True;
bart8f822af2009-06-08 18:20:42 +000077static Bool s_new_segments_since_last_merge;
bart6f1d7162009-06-24 18:34:10 +000078static int s_segment_merge_interval = 10;
sewardjaf44c822007-11-25 14:01:38 +000079
80
bart324a23b2009-02-15 12:14:52 +000081/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000082
bart86a87df2009-03-04 19:26:47 +000083/** Enables/disables context switch tracing. */
bart62a784c2009-02-15 13:11:14 +000084void DRD_(thread_trace_context_switches)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000085{
bartbedfd232009-03-26 19:07:15 +000086 tl_assert(t == False || t == True);
87 s_trace_context_switches = t;
bart26f73e12008-02-24 18:37:08 +000088}
89
bart86a87df2009-03-04 19:26:47 +000090/** Enables/disables conflict set tracing. */
bart62a784c2009-02-15 13:11:14 +000091void DRD_(thread_trace_conflict_set)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000092{
bartbedfd232009-03-26 19:07:15 +000093 tl_assert(t == False || t == True);
94 s_trace_conflict_set = t;
bart26f73e12008-02-24 18:37:08 +000095}
96
bart8f822af2009-06-08 18:20:42 +000097/** Enables/disables conflict set bitmap tracing. */
98void DRD_(thread_trace_conflict_set_bm)(const Bool t)
99{
100 tl_assert(t == False || t == True);
101 s_trace_conflict_set_bm = t;
102}
103
bart86a87df2009-03-04 19:26:47 +0000104/** Report whether fork/join tracing is enabled. */
bart09dc13f2009-02-14 15:13:31 +0000105Bool DRD_(thread_get_trace_fork_join)(void)
106{
bartbedfd232009-03-26 19:07:15 +0000107 return s_trace_fork_join;
bart09dc13f2009-02-14 15:13:31 +0000108}
109
bart86a87df2009-03-04 19:26:47 +0000110/** Enables/disables fork/join tracing. */
bart09dc13f2009-02-14 15:13:31 +0000111void DRD_(thread_set_trace_fork_join)(const Bool t)
112{
bartbedfd232009-03-26 19:07:15 +0000113 tl_assert(t == False || t == True);
114 s_trace_fork_join = t;
bart09dc13f2009-02-14 15:13:31 +0000115}
116
bart86a87df2009-03-04 19:26:47 +0000117/** Enables/disables segment merging. */
bart62a784c2009-02-15 13:11:14 +0000118void DRD_(thread_set_segment_merging)(const Bool m)
barta9c37392008-03-22 09:38:48 +0000119{
bartbedfd232009-03-26 19:07:15 +0000120 tl_assert(m == False || m == True);
121 s_segment_merging = m;
barta9c37392008-03-22 09:38:48 +0000122}
123
bart8f822af2009-06-08 18:20:42 +0000124/** Get the segment merging interval. */
125int DRD_(thread_get_segment_merge_interval)(void)
126{
127 return s_segment_merge_interval;
128}
129
130/** Set the segment merging interval. */
131void DRD_(thread_set_segment_merge_interval)(const int i)
132{
133 s_segment_merge_interval = i;
134}
135
sewardjaf44c822007-11-25 14:01:38 +0000136/**
bart86a87df2009-03-04 19:26:47 +0000137 * Convert Valgrind's ThreadId into a DrdThreadId.
138 *
139 * @return DRD thread ID upon success and DRD_INVALID_THREADID if the passed
140 * Valgrind ThreadId does not yet exist.
bart324a23b2009-02-15 12:14:52 +0000141 */
bart62a784c2009-02-15 13:11:14 +0000142DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000143{
bartbedfd232009-03-26 19:07:15 +0000144 int i;
sewardjaf44c822007-11-25 14:01:38 +0000145
bartbedfd232009-03-26 19:07:15 +0000146 if (tid == VG_INVALID_THREADID)
147 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000148
bartbedfd232009-03-26 19:07:15 +0000149 for (i = 1; i < DRD_N_THREADS; i++)
150 {
151 if (DRD_(g_threadinfo)[i].vg_thread_exists == True
152 && DRD_(g_threadinfo)[i].vg_threadid == tid)
153 {
154 return i;
155 }
156 }
sewardjaf44c822007-11-25 14:01:38 +0000157
bartbedfd232009-03-26 19:07:15 +0000158 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000159}
160
bart86a87df2009-03-04 19:26:47 +0000161/** Allocate a new DRD thread ID for the specified Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000162static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000163{
bartbedfd232009-03-26 19:07:15 +0000164 int i;
sewardjaf44c822007-11-25 14:01:38 +0000165
bartbedfd232009-03-26 19:07:15 +0000166 tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000167
bartbedfd232009-03-26 19:07:15 +0000168 for (i = 1; i < DRD_N_THREADS; i++)
169 {
170 if (DRD_(g_threadinfo)[i].vg_thread_exists == False
171 && DRD_(g_threadinfo)[i].posix_thread_exists == False
172 && DRD_(g_threadinfo)[i].detached_posix_thread == False)
173 {
174 tl_assert(! DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000175
bartbedfd232009-03-26 19:07:15 +0000176 DRD_(g_threadinfo)[i].vg_thread_exists = True;
177 DRD_(g_threadinfo)[i].vg_threadid = tid;
178 DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID;
179 DRD_(g_threadinfo)[i].stack_min = 0;
180 DRD_(g_threadinfo)[i].stack_min_min = 0;
181 DRD_(g_threadinfo)[i].stack_startup = 0;
182 DRD_(g_threadinfo)[i].stack_max = 0;
bartd45d9952009-05-31 18:53:54 +0000183 DRD_(thread_set_name)(i, "");
bart383d6132010-09-02 14:43:18 +0000184 DRD_(g_threadinfo)[i].on_alt_stack = False;
bartd45d9952009-05-31 18:53:54 +0000185 DRD_(g_threadinfo)[i].is_recording_loads = True;
186 DRD_(g_threadinfo)[i].is_recording_stores = True;
bartdd75cdf2009-07-24 08:20:10 +0000187 DRD_(g_threadinfo)[i].pthread_create_nesting_level = 0;
bartbedfd232009-03-26 19:07:15 +0000188 DRD_(g_threadinfo)[i].synchr_nesting = 0;
189 tl_assert(DRD_(g_threadinfo)[i].first == 0);
190 tl_assert(DRD_(g_threadinfo)[i].last == 0);
bart86a87df2009-03-04 19:26:47 +0000191
bartbedfd232009-03-26 19:07:15 +0000192 tl_assert(DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000193
bartbedfd232009-03-26 19:07:15 +0000194 return i;
195 }
196 }
sewardjaf44c822007-11-25 14:01:38 +0000197
bart3c9afb12009-07-24 11:11:30 +0000198 VG_(printf)(
199"\nSorry, but the maximum number of threads supported by DRD has been exceeded."
200"Aborting.\n");
201
bartbedfd232009-03-26 19:07:15 +0000202 tl_assert(False);
sewardjaf44c822007-11-25 14:01:38 +0000203
bartbedfd232009-03-26 19:07:15 +0000204 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000205}
206
bart86a87df2009-03-04 19:26:47 +0000207/** Convert a POSIX thread ID into a DRD thread ID. */
bart62a784c2009-02-15 13:11:14 +0000208DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000209{
bartbedfd232009-03-26 19:07:15 +0000210 int i;
sewardjaf44c822007-11-25 14:01:38 +0000211
bartb48bde22009-07-31 08:26:17 +0000212 if (tid != INVALID_POSIX_THREADID)
bartbedfd232009-03-26 19:07:15 +0000213 {
bartb48bde22009-07-31 08:26:17 +0000214 for (i = 1; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000215 {
bartb48bde22009-07-31 08:26:17 +0000216 if (DRD_(g_threadinfo)[i].posix_thread_exists
217 && DRD_(g_threadinfo)[i].pt_threadid == tid)
218 {
219 return i;
220 }
bartbedfd232009-03-26 19:07:15 +0000221 }
222 }
223 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000224}
225
bart86a87df2009-03-04 19:26:47 +0000226/** Convert a DRD thread ID into a Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000227ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000228{
bartbedfd232009-03-26 19:07:15 +0000229 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
230 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000231
bartbedfd232009-03-26 19:07:15 +0000232 return (DRD_(g_threadinfo)[tid].vg_thread_exists
233 ? DRD_(g_threadinfo)[tid].vg_threadid
234 : VG_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000235}
236
bart8f822af2009-06-08 18:20:42 +0000237#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart324a23b2009-02-15 12:14:52 +0000238/**
239 * Sanity check of the doubly linked list of segments referenced by a
240 * ThreadInfo struct.
241 * @return True if sane, False if not.
sewardjaf44c822007-11-25 14:01:38 +0000242 */
bart62a784c2009-02-15 13:11:14 +0000243static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti)
sewardjaf44c822007-11-25 14:01:38 +0000244{
bartbedfd232009-03-26 19:07:15 +0000245 Segment* p;
bart8f822af2009-06-08 18:20:42 +0000246
bartbedfd232009-03-26 19:07:15 +0000247 for (p = ti->first; p; p = p->next) {
248 if (p->next && p->next->prev != p)
249 return False;
250 if (p->next == 0 && p != ti->last)
251 return False;
252 }
253 for (p = ti->last; p; p = p->prev) {
254 if (p->prev && p->prev->next != p)
255 return False;
256 if (p->prev == 0 && p != ti->first)
257 return False;
258 }
259 return True;
sewardjaf44c822007-11-25 14:01:38 +0000260}
bart23d3a4e2008-04-05 12:53:00 +0000261#endif
sewardjaf44c822007-11-25 14:01:38 +0000262
bart439c55f2009-02-15 10:38:37 +0000263/**
264 * Create the first segment for a newly started thread.
265 *
266 * This function is called from the handler installed via
267 * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler
268 * from the context of the creator thread, before the new thread has been
269 * created.
bart86a87df2009-03-04 19:26:47 +0000270 *
271 * @param[in] creator DRD thread ID of the creator thread.
272 * @param[in] vg_created Valgrind thread ID of the created thread.
273 *
274 * @return DRD thread ID of the created thread.
bart439c55f2009-02-15 10:38:37 +0000275 */
bart62a784c2009-02-15 13:11:14 +0000276DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
277 const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000278{
bartbedfd232009-03-26 19:07:15 +0000279 DrdThreadId created;
sewardjaf44c822007-11-25 14:01:38 +0000280
bartbedfd232009-03-26 19:07:15 +0000281 tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID);
282 created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created);
283 tl_assert(0 <= (int)created && created < DRD_N_THREADS
284 && created != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000285
bartbedfd232009-03-26 19:07:15 +0000286 tl_assert(DRD_(g_threadinfo)[created].first == 0);
287 tl_assert(DRD_(g_threadinfo)[created].last == 0);
bart8f822af2009-06-08 18:20:42 +0000288 /* Create an initial segment for the newly created thread. */
bartbedfd232009-03-26 19:07:15 +0000289 thread_append_segment(created, DRD_(sg_new)(creator, created));
sewardjaf44c822007-11-25 14:01:38 +0000290
bartbedfd232009-03-26 19:07:15 +0000291 return created;
sewardjaf44c822007-11-25 14:01:38 +0000292}
293
bart439c55f2009-02-15 10:38:37 +0000294/**
bart86a87df2009-03-04 19:26:47 +0000295 * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called
296 * after the thread has been created and before any client instructions are run
bart439c55f2009-02-15 10:38:37 +0000297 * on the newly created thread, e.g. from the handler installed via
298 * VG_(track_pre_thread_first_insn)().
bart86a87df2009-03-04 19:26:47 +0000299 *
300 * @param[in] vg_created Valgrind thread ID of the newly created thread.
301 *
302 * @return DRD thread ID for the new thread.
bart439c55f2009-02-15 10:38:37 +0000303 */
bart62a784c2009-02-15 13:11:14 +0000304DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created)
bart439c55f2009-02-15 10:38:37 +0000305{
bartbedfd232009-03-26 19:07:15 +0000306 const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created);
bart439c55f2009-02-15 10:38:37 +0000307
bartbedfd232009-03-26 19:07:15 +0000308 tl_assert(0 <= (int)created && created < DRD_N_THREADS
309 && created != DRD_INVALID_THREADID);
bart439c55f2009-02-15 10:38:37 +0000310
bartbedfd232009-03-26 19:07:15 +0000311 DRD_(g_threadinfo)[created].stack_max
312 = VG_(thread_get_stack_max)(vg_created);
313 DRD_(g_threadinfo)[created].stack_startup
314 = DRD_(g_threadinfo)[created].stack_max;
315 DRD_(g_threadinfo)[created].stack_min
316 = DRD_(g_threadinfo)[created].stack_max;
317 DRD_(g_threadinfo)[created].stack_min_min
318 = DRD_(g_threadinfo)[created].stack_max;
319 DRD_(g_threadinfo)[created].stack_size
320 = VG_(thread_get_stack_size)(vg_created);
321 tl_assert(DRD_(g_threadinfo)[created].stack_max != 0);
bart439c55f2009-02-15 10:38:37 +0000322
bartbedfd232009-03-26 19:07:15 +0000323 return created;
bart439c55f2009-02-15 10:38:37 +0000324}
bart09dc13f2009-02-14 15:13:31 +0000325
bart324a23b2009-02-15 12:14:52 +0000326/**
327 * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just
328 * after thread drd_joiner joined thread drd_joinee.
329 */
bart09dc13f2009-02-14 15:13:31 +0000330void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
331{
bartbedfd232009-03-26 19:07:15 +0000332 tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner));
333 tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee));
bart7627be32009-06-06 12:26:05 +0000334
bartbedfd232009-03-26 19:07:15 +0000335 DRD_(thread_new_segment)(drd_joiner);
bart8f822af2009-06-08 18:20:42 +0000336 DRD_(thread_combine_vc_join)(drd_joiner, drd_joinee);
bart7627be32009-06-06 12:26:05 +0000337 DRD_(thread_new_segment)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000338
bartbedfd232009-03-26 19:07:15 +0000339 if (s_trace_fork_join)
340 {
341 const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner);
bartbedfd232009-03-26 19:07:15 +0000342 const unsigned msg_size = 256;
343 char* msg;
bart09dc13f2009-02-14 15:13:31 +0000344
bartbedfd232009-03-26 19:07:15 +0000345 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
346 tl_assert(msg);
347 VG_(snprintf)(msg, msg_size,
bart63c92ea2009-07-19 17:53:56 +0000348 "drd_post_thread_join joiner = %d, joinee = %d",
349 drd_joiner, drd_joinee);
bartbedfd232009-03-26 19:07:15 +0000350 if (joiner)
351 {
bart8f822af2009-06-08 18:20:42 +0000352 char* vc;
353
354 vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(drd_joiner));
bartbedfd232009-03-26 19:07:15 +0000355 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart8f822af2009-06-08 18:20:42 +0000356 ", new vc: %s", vc);
357 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000358 }
sewardj1e29ebc2009-07-15 14:49:17 +0000359 VG_(message)(Vg_DebugMsg, "%s\n", msg);
bartbedfd232009-03-26 19:07:15 +0000360 VG_(free)(msg);
361 }
bart09dc13f2009-02-14 15:13:31 +0000362
bartbedfd232009-03-26 19:07:15 +0000363 if (! DRD_(get_check_stack_accesses)())
364 {
365 DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee)
366 - DRD_(thread_get_stack_size)(drd_joinee),
367 DRD_(thread_get_stack_max)(drd_joinee));
368 }
369 DRD_(clientobj_delete_thread)(drd_joinee);
370 DRD_(thread_delete)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000371}
372
bart324a23b2009-02-15 12:14:52 +0000373/**
374 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
375 * and accesses this data structure from multiple threads without locking.
376 * Any conflicting accesses in the range stack_startup..stack_max will be
377 * ignored.
378 */
bart62a784c2009-02-15 13:11:14 +0000379void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
380 const Addr stack_startup)
sewardjaf44c822007-11-25 14:01:38 +0000381{
bartbedfd232009-03-26 19:07:15 +0000382 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
383 && tid != DRD_INVALID_THREADID);
384 tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup);
385 tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max);
386 DRD_(g_threadinfo)[tid].stack_startup = stack_startup;
sewardjaf44c822007-11-25 14:01:38 +0000387}
388
bart86a87df2009-03-04 19:26:47 +0000389/** Return the stack pointer for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000390Addr DRD_(thread_get_stack_min)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000391{
bartbedfd232009-03-26 19:07:15 +0000392 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
393 && tid != DRD_INVALID_THREADID);
394 return DRD_(g_threadinfo)[tid].stack_min;
sewardjaf44c822007-11-25 14:01:38 +0000395}
396
bart86a87df2009-03-04 19:26:47 +0000397/**
398 * Return the lowest value that was ever assigned to the stack pointer
399 * for the specified thread.
400 */
bart62a784c2009-02-15 13:11:14 +0000401Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000402{
bartbedfd232009-03-26 19:07:15 +0000403 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
404 && tid != DRD_INVALID_THREADID);
405 return DRD_(g_threadinfo)[tid].stack_min_min;
bartcac53462008-03-29 09:27:08 +0000406}
407
bart86a87df2009-03-04 19:26:47 +0000408/** Return the top address for the stack of the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000409Addr DRD_(thread_get_stack_max)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000410{
bartbedfd232009-03-26 19:07:15 +0000411 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
412 && tid != DRD_INVALID_THREADID);
413 return DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000414}
415
bart86a87df2009-03-04 19:26:47 +0000416/** Return the maximum stack size for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000417SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000418{
bartbedfd232009-03-26 19:07:15 +0000419 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
420 && tid != DRD_INVALID_THREADID);
421 return DRD_(g_threadinfo)[tid].stack_size;
bartcac53462008-03-29 09:27:08 +0000422}
423
bart383d6132010-09-02 14:43:18 +0000424Bool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid)
425{
426 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
427 && tid != DRD_INVALID_THREADID);
428 return DRD_(g_threadinfo)[tid].on_alt_stack;
429}
430
431void DRD_(thread_set_on_alt_stack)(const DrdThreadId tid,
432 const Bool on_alt_stack)
433{
434 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
435 && tid != DRD_INVALID_THREADID);
436 tl_assert(on_alt_stack == !!on_alt_stack);
437 DRD_(g_threadinfo)[tid].on_alt_stack = on_alt_stack;
438}
439
440Int DRD_(thread_get_threads_on_alt_stack)(void)
441{
442 int i, n = 0;
443
444 for (i = 1; i < DRD_N_THREADS; i++)
445 n += DRD_(g_threadinfo)[i].on_alt_stack;
446 return n;
447}
448
bart09dc13f2009-02-14 15:13:31 +0000449/**
bart31b983d2010-02-21 14:52:59 +0000450 * Clean up thread-specific data structures. Call this just after
bart09dc13f2009-02-14 15:13:31 +0000451 * pthread_join().
sewardjaf44c822007-11-25 14:01:38 +0000452 */
bart62a784c2009-02-15 13:11:14 +0000453void DRD_(thread_delete)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000454{
bartbedfd232009-03-26 19:07:15 +0000455 Segment* sg;
456 Segment* sg_prev;
sewardjaf44c822007-11-25 14:01:38 +0000457
bartbedfd232009-03-26 19:07:15 +0000458 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart86a87df2009-03-04 19:26:47 +0000459
bartbedfd232009-03-26 19:07:15 +0000460 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0);
461 for (sg = DRD_(g_threadinfo)[tid].last; sg; sg = sg_prev)
462 {
463 sg_prev = sg->prev;
464 sg->prev = 0;
465 sg->next = 0;
466 DRD_(sg_put)(sg);
467 }
468 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
469 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
470 tl_assert(DRD_(g_threadinfo)[tid].detached_posix_thread == False);
471 DRD_(g_threadinfo)[tid].first = 0;
472 DRD_(g_threadinfo)[tid].last = 0;
bart86a87df2009-03-04 19:26:47 +0000473
bartbedfd232009-03-26 19:07:15 +0000474 tl_assert(! DRD_(IsValidDrdThreadId)(tid));
sewardjaf44c822007-11-25 14:01:38 +0000475}
476
bart324a23b2009-02-15 12:14:52 +0000477/**
478 * Called after a thread performed its last memory access and before
479 * thread_delete() is called. Note: thread_delete() is only called for
480 * joinable threads, not for detached threads.
481 */
bart62a784c2009-02-15 13:11:14 +0000482void DRD_(thread_finished)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000483{
bartbedfd232009-03-26 19:07:15 +0000484 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
485 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000486
bartbedfd232009-03-26 19:07:15 +0000487 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
sewardjaf44c822007-11-25 14:01:38 +0000488
bartbedfd232009-03-26 19:07:15 +0000489 if (DRD_(g_threadinfo)[tid].detached_posix_thread)
490 {
491 /*
492 * Once a detached thread has finished, its stack is deallocated and
493 * should no longer be taken into account when computing the conflict set.
494 */
495 DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000496
bartbedfd232009-03-26 19:07:15 +0000497 /*
498 * For a detached thread, calling pthread_exit() invalidates the
499 * POSIX thread ID associated with the detached thread. For joinable
500 * POSIX threads however, the POSIX thread ID remains live after the
501 * pthread_exit() call until pthread_join() is called.
502 */
503 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
504 }
sewardjaf44c822007-11-25 14:01:38 +0000505}
506
bart9b2974a2008-09-27 12:35:31 +0000507/** Called just before pthread_cancel(). */
bart62a784c2009-02-15 13:11:14 +0000508void DRD_(thread_pre_cancel)(const DrdThreadId tid)
bartaf0691b2008-09-27 12:26:50 +0000509{
bartbedfd232009-03-26 19:07:15 +0000510 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
511 && tid != DRD_INVALID_THREADID);
512 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bartaf0691b2008-09-27 12:26:50 +0000513
bartbedfd232009-03-26 19:07:15 +0000514 DRD_(g_threadinfo)[tid].synchr_nesting = 0;
bartaf0691b2008-09-27 12:26:50 +0000515}
516
barte7dff242009-04-23 17:12:39 +0000517/**
518 * Store the POSIX thread ID for the specified thread.
519 *
520 * @note This function can be called two times for the same thread -- see also
521 * the comment block preceding the pthread_create() wrapper in
522 * drd_pthread_intercepts.c.
523 */
bart62a784c2009-02-15 13:11:14 +0000524void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
sewardjaf44c822007-11-25 14:01:38 +0000525{
bartbedfd232009-03-26 19:07:15 +0000526 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
527 && tid != DRD_INVALID_THREADID);
barte7dff242009-04-23 17:12:39 +0000528 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID
529 || DRD_(g_threadinfo)[tid].pt_threadid == ptid);
bartbedfd232009-03-26 19:07:15 +0000530 tl_assert(ptid != INVALID_POSIX_THREADID);
531 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
532 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardjaf44c822007-11-25 14:01:38 +0000533}
534
bart86a87df2009-03-04 19:26:47 +0000535/** Returns true for joinable threads and false for detached threads. */
bart62a784c2009-02-15 13:11:14 +0000536Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000537{
bartbedfd232009-03-26 19:07:15 +0000538 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
539 && tid != DRD_INVALID_THREADID);
540 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000541}
542
bart86a87df2009-03-04 19:26:47 +0000543/** Store the thread mode: joinable or detached. */
bart62a784c2009-02-15 13:11:14 +0000544void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000545{
bartbedfd232009-03-26 19:07:15 +0000546 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
547 && tid != DRD_INVALID_THREADID);
548 tl_assert(!! joinable == joinable);
549 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bart8f822af2009-06-08 18:20:42 +0000550
bartbedfd232009-03-26 19:07:15 +0000551 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000552}
553
bartdd75cdf2009-07-24 08:20:10 +0000554/** Tells DRD that the calling thread is about to enter pthread_create(). */
555void DRD_(thread_entering_pthread_create)(const DrdThreadId tid)
556{
557 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
558 && tid != DRD_INVALID_THREADID);
559 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
560 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level >= 0);
561
562 DRD_(g_threadinfo)[tid].pthread_create_nesting_level++;
563}
564
565/** Tells DRD that the calling thread has left pthread_create(). */
566void DRD_(thread_left_pthread_create)(const DrdThreadId tid)
567{
568 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
569 && tid != DRD_INVALID_THREADID);
570 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
571 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level > 0);
572
573 DRD_(g_threadinfo)[tid].pthread_create_nesting_level--;
574}
575
bartd45d9952009-05-31 18:53:54 +0000576/** Obtain the thread number and the user-assigned thread name. */
577const char* DRD_(thread_get_name)(const DrdThreadId tid)
578{
579 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
580 && tid != DRD_INVALID_THREADID);
581
582 return DRD_(g_threadinfo)[tid].name;
583}
584
585/** Set the name of the specified thread. */
586void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name)
587{
588 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
589 && tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000590
bartd45d9952009-05-31 18:53:54 +0000591 if (name == NULL || name[0] == 0)
592 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
593 sizeof(DRD_(g_threadinfo)[tid].name),
594 "Thread %d",
595 tid);
596 else
597 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
598 sizeof(DRD_(g_threadinfo)[tid].name),
599 "Thread %d (%s)",
600 tid, name);
601 DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0;
602}
603
bart86a87df2009-03-04 19:26:47 +0000604/**
605 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
606 * conflict set.
607 */
bart62a784c2009-02-15 13:11:14 +0000608void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000609{
bartbedfd232009-03-26 19:07:15 +0000610 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000611
bartbedfd232009-03-26 19:07:15 +0000612 if (vg_tid != s_vg_running_tid)
613 {
614 DRD_(thread_set_running_tid)(vg_tid,
615 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
616 }
sewardj8b09d4f2007-12-04 21:27:18 +0000617
bartbedfd232009-03-26 19:07:15 +0000618 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
619 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000620}
621
bart86a87df2009-03-04 19:26:47 +0000622/**
623 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
624 * conflict set.
625 */
bart62a784c2009-02-15 13:11:14 +0000626void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
627 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000628{
bartbedfd232009-03-26 19:07:15 +0000629 tl_assert(vg_tid != VG_INVALID_THREADID);
630 tl_assert(drd_tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000631
bartbedfd232009-03-26 19:07:15 +0000632 if (vg_tid != s_vg_running_tid)
633 {
634 if (s_trace_context_switches
635 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
636 {
637 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +0000638 "Context switch from thread %d to thread %d;"
sewardj1e29ebc2009-07-15 14:49:17 +0000639 " segments: %llu\n",
bart63c92ea2009-07-19 17:53:56 +0000640 DRD_(g_drd_running_tid), drd_tid,
bartbedfd232009-03-26 19:07:15 +0000641 DRD_(sg_get_segments_alive_count)());
642 }
643 s_vg_running_tid = vg_tid;
644 DRD_(g_drd_running_tid) = drd_tid;
645 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
646 s_context_switch_count++;
647 }
sewardj8b09d4f2007-12-04 21:27:18 +0000648
bartbedfd232009-03-26 19:07:15 +0000649 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
650 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000651}
652
bart86a87df2009-03-04 19:26:47 +0000653/**
654 * Increase the synchronization nesting counter. Must be called before the
655 * client calls a synchronization function.
656 */
bart62a784c2009-02-15 13:11:14 +0000657int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000658{
bartbedfd232009-03-26 19:07:15 +0000659 tl_assert(DRD_(IsValidDrdThreadId)(tid));
660 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000661}
662
bart86a87df2009-03-04 19:26:47 +0000663/**
664 * Decrease the synchronization nesting counter. Must be called after the
665 * client left a synchronization function.
666 */
bart62a784c2009-02-15 13:11:14 +0000667int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000668{
bartbedfd232009-03-26 19:07:15 +0000669 tl_assert(DRD_(IsValidDrdThreadId)(tid));
670 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
671 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000672}
673
bart86a87df2009-03-04 19:26:47 +0000674/** Returns the synchronization nesting counter. */
bart62a784c2009-02-15 13:11:14 +0000675int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000676{
bartbedfd232009-03-26 19:07:15 +0000677 tl_assert(DRD_(IsValidDrdThreadId)(tid));
678 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000679}
680
bart1a473c72008-03-13 19:03:38 +0000681/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000682static
bart86a87df2009-03-04 19:26:47 +0000683void thread_append_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000684{
bartbedfd232009-03-26 19:07:15 +0000685 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
686 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000687
688#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
689 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
690#endif
691
bartbedfd232009-03-26 19:07:15 +0000692 sg->prev = DRD_(g_threadinfo)[tid].last;
693 sg->next = 0;
694 if (DRD_(g_threadinfo)[tid].last)
695 DRD_(g_threadinfo)[tid].last->next = sg;
696 DRD_(g_threadinfo)[tid].last = sg;
697 if (DRD_(g_threadinfo)[tid].first == 0)
698 DRD_(g_threadinfo)[tid].first = sg;
bart8f822af2009-06-08 18:20:42 +0000699
700#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
701 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
702#endif
sewardjaf44c822007-11-25 14:01:38 +0000703}
704
bart324a23b2009-02-15 12:14:52 +0000705/**
706 * Remove a segment from the segment list of thread threadid, and free the
707 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000708 */
bart62a784c2009-02-15 13:11:14 +0000709static
bart86a87df2009-03-04 19:26:47 +0000710void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000711{
bartbedfd232009-03-26 19:07:15 +0000712 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
713 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000714
715#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
716 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
717#endif
bart26f73e12008-02-24 18:37:08 +0000718
bartbedfd232009-03-26 19:07:15 +0000719 if (sg->prev)
720 sg->prev->next = sg->next;
721 if (sg->next)
722 sg->next->prev = sg->prev;
723 if (sg == DRD_(g_threadinfo)[tid].first)
724 DRD_(g_threadinfo)[tid].first = sg->next;
725 if (sg == DRD_(g_threadinfo)[tid].last)
726 DRD_(g_threadinfo)[tid].last = sg->prev;
727 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000728
bart8f822af2009-06-08 18:20:42 +0000729#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
730 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
731#endif
sewardjaf44c822007-11-25 14:01:38 +0000732}
733
bart86a87df2009-03-04 19:26:47 +0000734/**
735 * Returns a pointer to the vector clock of the most recent segment associated
736 * with thread 'tid'.
737 */
bart62a784c2009-02-15 13:11:14 +0000738VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000739{
bartbedfd232009-03-26 19:07:15 +0000740 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
741 && tid != DRD_INVALID_THREADID);
742 tl_assert(DRD_(g_threadinfo)[tid].last);
743 return &DRD_(g_threadinfo)[tid].last->vc;
sewardjaf44c822007-11-25 14:01:38 +0000744}
745
bart324a23b2009-02-15 12:14:52 +0000746/**
747 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000748 */
bart62a784c2009-02-15 13:11:14 +0000749void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000750{
bartbedfd232009-03-26 19:07:15 +0000751 tl_assert(sg);
752 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
753 && tid != DRD_INVALID_THREADID);
754 tl_assert(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000755
bartbedfd232009-03-26 19:07:15 +0000756 DRD_(sg_put)(*sg);
757 *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000758}
759
sewardjaf44c822007-11-25 14:01:38 +0000760/**
761 * Compute the minimum of all latest vector clocks of all threads
762 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
bart86a87df2009-03-04 19:26:47 +0000763 *
sewardjaf44c822007-11-25 14:01:38 +0000764 * @param vc pointer to a vectorclock, holds result upon return.
765 */
bart62a784c2009-02-15 13:11:14 +0000766static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000767{
bartbedfd232009-03-26 19:07:15 +0000768 unsigned i;
769 Bool first;
770 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000771
bartbedfd232009-03-26 19:07:15 +0000772 first = True;
bart8f822af2009-06-08 18:20:42 +0000773 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000774 {
775 latest_sg = DRD_(g_threadinfo)[i].last;
776 if (latest_sg)
777 {
778 if (first)
779 DRD_(vc_assign)(vc, &latest_sg->vc);
780 else
781 DRD_(vc_min)(vc, &latest_sg->vc);
782 first = False;
783 }
784 }
sewardjaf44c822007-11-25 14:01:38 +0000785}
786
bart86a87df2009-03-04 19:26:47 +0000787/**
788 * Compute the maximum of all latest vector clocks of all threads.
789 *
790 * @param vc pointer to a vectorclock, holds result upon return.
791 */
bart62a784c2009-02-15 13:11:14 +0000792static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000793{
bartbedfd232009-03-26 19:07:15 +0000794 unsigned i;
795 Bool first;
796 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000797
bartbedfd232009-03-26 19:07:15 +0000798 first = True;
bart8f822af2009-06-08 18:20:42 +0000799 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000800 {
801 latest_sg = DRD_(g_threadinfo)[i].last;
802 if (latest_sg)
803 {
804 if (first)
805 DRD_(vc_assign)(vc, &latest_sg->vc);
806 else
807 DRD_(vc_combine)(vc, &latest_sg->vc);
808 first = False;
809 }
810 }
sewardjaf44c822007-11-25 14:01:38 +0000811}
812
813/**
bart5bd9f2d2008-03-03 20:31:58 +0000814 * Discard all segments that have a defined order against the latest vector
bart86a87df2009-03-04 19:26:47 +0000815 * clock of all threads -- these segments can no longer be involved in a
sewardjaf44c822007-11-25 14:01:38 +0000816 * data race.
817 */
bart8f822af2009-06-08 18:20:42 +0000818static void thread_discard_ordered_segments(void)
sewardjaf44c822007-11-25 14:01:38 +0000819{
bartbedfd232009-03-26 19:07:15 +0000820 unsigned i;
821 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000822
bartbedfd232009-03-26 19:07:15 +0000823 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000824
bartbedfd232009-03-26 19:07:15 +0000825 DRD_(vc_init)(&thread_vc_min, 0, 0);
826 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
827 if (DRD_(sg_get_trace)())
828 {
bart8f822af2009-06-08 18:20:42 +0000829 char *vc_min, *vc_max;
bartbedfd232009-03-26 19:07:15 +0000830 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000831
bartbedfd232009-03-26 19:07:15 +0000832 DRD_(vc_init)(&thread_vc_max, 0, 0);
833 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
bart8f822af2009-06-08 18:20:42 +0000834 vc_min = DRD_(vc_aprint)(&thread_vc_min);
835 vc_max = DRD_(vc_aprint)(&thread_vc_max);
836 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000837 "Discarding ordered segments -- min vc is %s, max vc is %s\n",
bart8f822af2009-06-08 18:20:42 +0000838 vc_min, vc_max);
839 VG_(free)(vc_min);
840 VG_(free)(vc_max);
bartbedfd232009-03-26 19:07:15 +0000841 DRD_(vc_cleanup)(&thread_vc_max);
842 }
sewardjaf44c822007-11-25 14:01:38 +0000843
bart8f822af2009-06-08 18:20:42 +0000844 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000845 {
846 Segment* sg;
847 Segment* sg_next;
848 for (sg = DRD_(g_threadinfo)[i].first;
849 sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
850 sg = sg_next)
851 {
852 thread_discard_segment(i, sg);
853 }
854 }
855 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +0000856}
857
bart324a23b2009-02-15 12:14:52 +0000858/**
bart8f822af2009-06-08 18:20:42 +0000859 * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper
860 * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if
861 * all segments in the set CS are ordered consistently against both sg1 and
862 * sg2. The set CS is defined as the set of segments that can immediately
863 * precede future segments via inter-thread synchronization operations. In
864 * DRD the set CS consists of the latest segment of each thread combined with
865 * all segments for which the reference count is strictly greater than one.
866 * The code below is an optimized version of the following:
867 *
868 * for (i = 0; i < DRD_N_THREADS; i++)
869 * {
870 * Segment* sg;
871 *
872 * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
873 * {
874 * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1)
875 * {
876 * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc)
877 * != DRD_(vc_lte)(&sg2->vc, &sg->vc)
878 * || DRD_(vc_lte)(&sg->vc, &sg1->vc)
879 * != DRD_(vc_lte)(&sg->vc, &sg2->vc))
880 * {
881 * return False;
882 * }
883 * }
884 * }
885 * }
886 */
887static Bool thread_consistent_segment_ordering(const DrdThreadId tid,
888 Segment* const sg1,
889 Segment* const sg2)
890{
891 unsigned i;
892
893 tl_assert(sg1->next);
894 tl_assert(sg2->next);
895 tl_assert(sg1->next == sg2);
896 tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc));
897
898 for (i = 0; i < DRD_N_THREADS; i++)
899 {
900 Segment* sg;
901
902 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
903 {
904 if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1)
905 {
906 if (DRD_(vc_lte)(&sg2->vc, &sg->vc))
907 break;
908 if (DRD_(vc_lte)(&sg1->vc, &sg->vc))
909 return False;
910 }
911 }
912 for (sg = DRD_(g_threadinfo)[i].last; sg; sg = sg->prev)
913 {
914 if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1)
915 {
916 if (DRD_(vc_lte)(&sg->vc, &sg1->vc))
917 break;
918 if (DRD_(vc_lte)(&sg->vc, &sg2->vc))
919 return False;
920 }
921 }
922 }
923 return True;
924}
925
926/**
bart324a23b2009-02-15 12:14:52 +0000927 * Merge all segments that may be merged without triggering false positives
928 * or discarding real data races. For the theoretical background of segment
bart8f822af2009-06-08 18:20:42 +0000929 * merging, see also the following paper: Mark Christiaens, Michiel Ronsse
930 * and Koen De Bosschere. Bounding the number of segment histories during
931 * data race detection. Parallel Computing archive, Volume 28, Issue 9,
932 * pp 1221-1238, September 2002. This paper contains a proof that merging
933 * consecutive segments for which the property equiv(s1,s2) holds can be
934 * merged without reducing the accuracy of datarace detection. Furthermore
935 * it is also proven that the total number of all segments will never grow
936 * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged
937 * every time a new segment is created. The property equiv(s1, s2) is defined
938 * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector
939 * clocks of segments s and s1 are ordered in the same way as those of segments
940 * s and s2. The set CS is defined as the set of existing segments s that have
941 * the potential to conflict with not yet created segments, either because the
942 * segment s is the latest segment of a thread or because it can become the
943 * immediate predecessor of a new segment due to a synchronization operation.
barta9c37392008-03-22 09:38:48 +0000944 */
945static void thread_merge_segments(void)
946{
bartbedfd232009-03-26 19:07:15 +0000947 unsigned i;
barta9c37392008-03-22 09:38:48 +0000948
bart8f822af2009-06-08 18:20:42 +0000949 s_new_segments_since_last_merge = 0;
950
951 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000952 {
953 Segment* sg;
barta9c37392008-03-22 09:38:48 +0000954
bart8f822af2009-06-08 18:20:42 +0000955#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
956 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
957#endif
barta9c37392008-03-22 09:38:48 +0000958
bartbedfd232009-03-26 19:07:15 +0000959 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
barta9c37392008-03-22 09:38:48 +0000960 {
bartbedfd232009-03-26 19:07:15 +0000961 if (DRD_(sg_get_refcnt)(sg) == 1
962 && sg->next
963 && DRD_(sg_get_refcnt)(sg->next) == 1
bart8f822af2009-06-08 18:20:42 +0000964 && sg->next->next
965 && thread_consistent_segment_ordering(i, sg, sg->next))
bartbedfd232009-03-26 19:07:15 +0000966 {
967 /* Merge sg and sg->next into sg. */
968 DRD_(sg_merge)(sg, sg->next);
969 thread_discard_segment(i, sg->next);
970 }
barta9c37392008-03-22 09:38:48 +0000971 }
barta9c37392008-03-22 09:38:48 +0000972
bart8f822af2009-06-08 18:20:42 +0000973#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
974 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
975#endif
bartbedfd232009-03-26 19:07:15 +0000976 }
barta9c37392008-03-22 09:38:48 +0000977}
978
bart324a23b2009-02-15 12:14:52 +0000979/**
bart324a23b2009-02-15 12:14:52 +0000980 * Create a new segment for the specified thread, and discard any segments
981 * that cannot cause races anymore.
sewardjaf44c822007-11-25 14:01:38 +0000982 */
bart62a784c2009-02-15 13:11:14 +0000983void DRD_(thread_new_segment)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000984{
bart8f822af2009-06-08 18:20:42 +0000985 Segment* last_sg;
bartbedfd232009-03-26 19:07:15 +0000986 Segment* new_sg;
bartd66e3a82008-04-06 15:02:17 +0000987
bartbedfd232009-03-26 19:07:15 +0000988 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
989 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000990 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +0000991
bart8f822af2009-06-08 18:20:42 +0000992 last_sg = DRD_(g_threadinfo)[tid].last;
bartbedfd232009-03-26 19:07:15 +0000993 new_sg = DRD_(sg_new)(tid, tid);
994 thread_append_segment(tid, new_sg);
bart8f822af2009-06-08 18:20:42 +0000995 if (tid == DRD_(g_drd_running_tid) && last_sg)
barte5214662009-06-21 11:51:23 +0000996 {
bart8f822af2009-06-08 18:20:42 +0000997 DRD_(thread_update_conflict_set)(tid, &last_sg->vc);
barte5214662009-06-21 11:51:23 +0000998 s_update_conflict_set_new_sg_count++;
999 }
bartd66e3a82008-04-06 15:02:17 +00001000
bart8f822af2009-06-08 18:20:42 +00001001 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001002
bart8f822af2009-06-08 18:20:42 +00001003 if (s_segment_merging
1004 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
bartbedfd232009-03-26 19:07:15 +00001005 {
bart8f822af2009-06-08 18:20:42 +00001006 thread_discard_ordered_segments();
bartbedfd232009-03-26 19:07:15 +00001007 thread_merge_segments();
1008 }
sewardjaf44c822007-11-25 14:01:38 +00001009}
1010
bart26f73e12008-02-24 18:37:08 +00001011/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart8f822af2009-06-08 18:20:42 +00001012void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +00001013{
bartbedfd232009-03-26 19:07:15 +00001014 tl_assert(joiner != joinee);
1015 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
1016 && joiner != DRD_INVALID_THREADID);
1017 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
1018 && joinee != DRD_INVALID_THREADID);
1019 tl_assert(DRD_(g_threadinfo)[joiner].last);
1020 tl_assert(DRD_(g_threadinfo)[joinee].last);
bart8f822af2009-06-08 18:20:42 +00001021
1022 if (DRD_(sg_get_trace)())
1023 {
1024 char *str1, *str2;
1025 str1 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc);
1026 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joinee].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001027 VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n",
bart8f822af2009-06-08 18:20:42 +00001028 str1, str2);
1029 VG_(free)(str1);
1030 VG_(free)(str2);
1031 }
barte5214662009-06-21 11:51:23 +00001032 if (joiner == DRD_(g_drd_running_tid))
1033 {
1034 VectorClock old_vc;
1035
1036 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[joiner].last->vc);
1037 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
bartf6ec1fe2009-06-21 18:07:35 +00001038 &DRD_(g_threadinfo)[joinee].last->vc);
barte5214662009-06-21 11:51:23 +00001039 DRD_(thread_update_conflict_set)(joiner, &old_vc);
1040 s_update_conflict_set_join_count++;
1041 DRD_(vc_cleanup)(&old_vc);
1042 }
1043 else
1044 {
1045 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
bartf6ec1fe2009-06-21 18:07:35 +00001046 &DRD_(g_threadinfo)[joinee].last->vc);
barte5214662009-06-21 11:51:23 +00001047 }
1048
1049 thread_discard_ordered_segments();
1050
bart8f822af2009-06-08 18:20:42 +00001051 if (DRD_(sg_get_trace)())
1052 {
1053 char* str;
1054 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001055 VG_(message)(Vg_DebugMsg, "After join: %s\n", str);
bart8f822af2009-06-08 18:20:42 +00001056 VG_(free)(str);
1057 }
sewardjaf44c822007-11-25 14:01:38 +00001058}
1059
bart324a23b2009-02-15 12:14:52 +00001060/**
bart8f822af2009-06-08 18:20:42 +00001061 * Update the vector clock of the last segment of thread tid with the
bartf6ec1fe2009-06-21 18:07:35 +00001062 * the vector clock of segment sg.
bart26f73e12008-02-24 18:37:08 +00001063 */
bartf6ec1fe2009-06-21 18:07:35 +00001064static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg)
sewardjaf44c822007-11-25 14:01:38 +00001065{
bart8f822af2009-06-08 18:20:42 +00001066 const VectorClock* const vc = &sg->vc;
1067
bartbedfd232009-03-26 19:07:15 +00001068 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1069 && tid != DRD_INVALID_THREADID);
1070 tl_assert(DRD_(g_threadinfo)[tid].last);
bart8f822af2009-06-08 18:20:42 +00001071 tl_assert(sg);
bartbedfd232009-03-26 19:07:15 +00001072 tl_assert(vc);
bart8f822af2009-06-08 18:20:42 +00001073
1074 if (tid != sg->tid)
1075 {
1076 VectorClock old_vc;
1077
1078 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc);
1079 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc);
1080 if (DRD_(sg_get_trace)())
1081 {
1082 char *str1, *str2;
1083 str1 = DRD_(vc_aprint)(&old_vc);
1084 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001085 VG_(message)(Vg_DebugMsg, "thread %d: vc %s -> %s\n", tid, str1, str2);
bart8f822af2009-06-08 18:20:42 +00001086 VG_(free)(str1);
1087 VG_(free)(str2);
1088 }
barte5214662009-06-21 11:51:23 +00001089
bart8f822af2009-06-08 18:20:42 +00001090 thread_discard_ordered_segments();
barte5214662009-06-21 11:51:23 +00001091
bart8f822af2009-06-08 18:20:42 +00001092 DRD_(thread_update_conflict_set)(tid, &old_vc);
barte5214662009-06-21 11:51:23 +00001093 s_update_conflict_set_sync_count++;
1094
bart8f822af2009-06-08 18:20:42 +00001095 DRD_(vc_cleanup)(&old_vc);
1096 }
1097 else
1098 {
1099 tl_assert(DRD_(vc_lte)(vc, &DRD_(g_threadinfo)[tid].last->vc));
1100 }
sewardjaf44c822007-11-25 14:01:38 +00001101}
1102
bart324a23b2009-02-15 12:14:52 +00001103/**
bartf6ec1fe2009-06-21 18:07:35 +00001104 * Create a new segment for thread tid and update the vector clock of the last
1105 * segment of this thread with the the vector clock of segment sg. Call this
1106 * function after thread tid had to wait because of thread synchronization
1107 * until the memory accesses in the segment sg finished.
1108 */
1109void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, const Segment* sg)
1110{
1111 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1112 && tid != DRD_INVALID_THREADID);
1113 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1114 tl_assert(sg);
1115
1116 thread_append_segment(tid, DRD_(sg_new)(tid, tid));
1117
1118 thread_combine_vc_sync(tid, sg);
1119
1120 if (s_segment_merging
1121 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
1122 {
1123 thread_discard_ordered_segments();
1124 thread_merge_segments();
1125 }
1126}
1127
1128/**
bart324a23b2009-02-15 12:14:52 +00001129 * Call this function whenever a thread is no longer using the memory
1130 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
1131 * increase.
bart26f73e12008-02-24 18:37:08 +00001132 */
bartf9427fd2010-08-29 09:19:07 +00001133void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2,
1134 const Bool dont_clear_access)
sewardjaf44c822007-11-25 14:01:38 +00001135{
bartbedfd232009-03-26 19:07:15 +00001136 DrdThreadId other_user;
1137 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001138
bartbedfd232009-03-26 19:07:15 +00001139 /* For all threads, mark the range [ a1, a2 [ as no longer in use. */
1140 other_user = DRD_INVALID_THREADID;
bart8f822af2009-06-08 18:20:42 +00001141 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001142 {
1143 Segment* p;
1144 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
sewardjaf44c822007-11-25 14:01:38 +00001145 {
bartbedfd232009-03-26 19:07:15 +00001146 if (other_user == DRD_INVALID_THREADID
1147 && i != DRD_(g_drd_running_tid))
1148 {
bartf9427fd2010-08-29 09:19:07 +00001149 if (UNLIKELY((!dont_clear_access
1150 && DRD_(bm_test_and_clear)(DRD_(sg_bm)(p), a1, a2))
1151 || (dont_clear_access
1152 && DRD_(bm_has_any_access)(DRD_(sg_bm)(p), a1, a2))
1153 ))
bartbedfd232009-03-26 19:07:15 +00001154 {
1155 other_user = i;
1156 }
1157 continue;
1158 }
bartf9427fd2010-08-29 09:19:07 +00001159 if (!dont_clear_access)
1160 DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001161 }
bartbedfd232009-03-26 19:07:15 +00001162 }
sewardjaf44c822007-11-25 14:01:38 +00001163
bartbedfd232009-03-26 19:07:15 +00001164 /*
1165 * If any other thread had accessed memory in [ a1, a2 [, update the
1166 * conflict set.
1167 */
1168 if (other_user != DRD_INVALID_THREADID
1169 && DRD_(bm_has_any_access)(DRD_(g_conflict_set), a1, a2))
1170 {
1171 thread_compute_conflict_set(&DRD_(g_conflict_set),
1172 DRD_(thread_get_running_tid)());
1173 }
sewardjaf44c822007-11-25 14:01:38 +00001174}
1175
bartd45d9952009-05-31 18:53:54 +00001176/** Specify whether memory loads should be recorded. */
1177void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001178{
bartbedfd232009-03-26 19:07:15 +00001179 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1180 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001181 tl_assert(enabled == !! enabled);
1182
1183 DRD_(g_threadinfo)[tid].is_recording_loads = enabled;
bart0268dfa2008-03-11 20:10:21 +00001184}
1185
bartd45d9952009-05-31 18:53:54 +00001186/** Specify whether memory stores should be recorded. */
1187void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001188{
bartbedfd232009-03-26 19:07:15 +00001189 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1190 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001191 tl_assert(enabled == !! enabled);
1192
1193 DRD_(g_threadinfo)[tid].is_recording_stores = enabled;
bart0268dfa2008-03-11 20:10:21 +00001194}
1195
bart86a87df2009-03-04 19:26:47 +00001196/**
1197 * Print the segment information for all threads.
1198 *
1199 * This function is only used for debugging purposes.
1200 */
bart62a784c2009-02-15 13:11:14 +00001201void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +00001202{
bartbedfd232009-03-26 19:07:15 +00001203 unsigned i;
1204 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001205
bart8f822af2009-06-08 18:20:42 +00001206 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001207 {
1208 if (DRD_(g_threadinfo)[i].first)
sewardjaf44c822007-11-25 14:01:38 +00001209 {
bartbedfd232009-03-26 19:07:15 +00001210 VG_(printf)("**************\n"
1211 "* thread %3d (%d/%d/%d/0x%lx/%d) *\n"
1212 "**************\n",
1213 i,
1214 DRD_(g_threadinfo)[i].vg_thread_exists,
1215 DRD_(g_threadinfo)[i].vg_threadid,
1216 DRD_(g_threadinfo)[i].posix_thread_exists,
1217 DRD_(g_threadinfo)[i].pt_threadid,
1218 DRD_(g_threadinfo)[i].detached_posix_thread);
1219 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
1220 {
1221 DRD_(sg_print)(p);
1222 }
sewardjaf44c822007-11-25 14:01:38 +00001223 }
bartbedfd232009-03-26 19:07:15 +00001224 }
sewardjaf44c822007-11-25 14:01:38 +00001225}
1226
bart86a87df2009-03-04 19:26:47 +00001227/** Show a call stack involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001228static void show_call_stack(const DrdThreadId tid,
1229 const Char* const msg,
1230 ExeContext* const callstack)
1231{
bartbedfd232009-03-26 19:07:15 +00001232 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +00001233
bart63c92ea2009-07-19 17:53:56 +00001234 VG_(message)(Vg_UserMsg, "%s (thread %d)\n", msg, tid);
sewardjaf44c822007-11-25 14:01:38 +00001235
bartbedfd232009-03-26 19:07:15 +00001236 if (vg_tid != VG_INVALID_THREADID)
1237 {
1238 if (callstack)
1239 {
1240 VG_(pp_ExeContext)(callstack);
1241 }
1242 else
1243 {
1244 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
1245 }
1246 }
1247 else
1248 {
1249 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001250 " (thread finished, call stack no longer available)\n");
bartbedfd232009-03-26 19:07:15 +00001251 }
sewardjaf44c822007-11-25 14:01:38 +00001252}
1253
bart86a87df2009-03-04 19:26:47 +00001254/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001255static void
1256thread_report_conflicting_segments_segment(const DrdThreadId tid,
1257 const Addr addr,
1258 const SizeT size,
1259 const BmAccessTypeT access_type,
1260 const Segment* const p)
1261{
bartbedfd232009-03-26 19:07:15 +00001262 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001263
bartbedfd232009-03-26 19:07:15 +00001264 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1265 && tid != DRD_INVALID_THREADID);
1266 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +00001267
bart8f822af2009-06-08 18:20:42 +00001268 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001269 {
1270 if (i != tid)
sewardjaf44c822007-11-25 14:01:38 +00001271 {
bartbedfd232009-03-26 19:07:15 +00001272 Segment* q;
1273 for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev)
1274 {
1275 /*
bart31b983d2010-02-21 14:52:59 +00001276 * Since q iterates over the segments of thread i in order of
1277 * decreasing vector clocks, if q->vc <= p->vc, then
bartbedfd232009-03-26 19:07:15 +00001278 * q->next->vc <= p->vc will also hold. Hence, break out of the
1279 * loop once this condition is met.
1280 */
1281 if (DRD_(vc_lte)(&q->vc, &p->vc))
1282 break;
1283 if (! DRD_(vc_lte)(&p->vc, &q->vc))
1284 {
bart8f822af2009-06-08 18:20:42 +00001285 if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size,
bartbedfd232009-03-26 19:07:15 +00001286 access_type))
1287 {
1288 tl_assert(q->stacktrace);
1289 show_call_stack(i, "Other segment start",
1290 q->stacktrace);
1291 show_call_stack(i, "Other segment end",
1292 q->next ? q->next->stacktrace : 0);
1293 }
1294 }
1295 }
sewardjaf44c822007-11-25 14:01:38 +00001296 }
bartbedfd232009-03-26 19:07:15 +00001297 }
sewardjaf44c822007-11-25 14:01:38 +00001298}
1299
bart86a87df2009-03-04 19:26:47 +00001300/** Print information about all segments involved in a data race. */
bart62a784c2009-02-15 13:11:14 +00001301void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1302 const Addr addr,
1303 const SizeT size,
1304 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +00001305{
bartbedfd232009-03-26 19:07:15 +00001306 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001307
bartbedfd232009-03-26 19:07:15 +00001308 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1309 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001310
bartbedfd232009-03-26 19:07:15 +00001311 for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next)
1312 {
bart8f822af2009-06-08 18:20:42 +00001313 if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type))
bartbedfd232009-03-26 19:07:15 +00001314 {
1315 thread_report_conflicting_segments_segment(tid, addr, size,
1316 access_type, p);
1317 }
1318 }
sewardjaf44c822007-11-25 14:01:38 +00001319}
sewardjaf44c822007-11-25 14:01:38 +00001320
bart324a23b2009-02-15 12:14:52 +00001321/**
bart8f822af2009-06-08 18:20:42 +00001322 * Verify whether the conflict set for thread tid is up to date. Only perform
1323 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
1324 */
1325static Bool thread_conflict_set_up_to_date(const DrdThreadId tid)
1326{
1327 static int do_verify_conflict_set = -1;
1328 Bool result;
1329 struct bitmap* computed_conflict_set = 0;
1330
1331 if (do_verify_conflict_set < 0)
1332 do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0;
1333
1334 if (do_verify_conflict_set == 0)
1335 return True;
1336
1337 thread_compute_conflict_set(&computed_conflict_set, tid);
1338 result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set);
1339 if (! result)
1340 {
1341 VG_(printf)("actual conflict set:\n");
1342 DRD_(bm_print)(DRD_(g_conflict_set));
1343 VG_(printf)("\n");
1344 VG_(printf)("computed conflict set:\n");
1345 DRD_(bm_print)(computed_conflict_set);
1346 VG_(printf)("\n");
1347 }
1348 DRD_(bm_delete)(computed_conflict_set);
1349 return result;
1350}
1351
1352/**
1353 * Compute the conflict set: a bitmap that represents the union of all memory
1354 * accesses of all segments that are unordered to the current segment of the
1355 * thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001356 */
bart86a87df2009-03-04 19:26:47 +00001357static void thread_compute_conflict_set(struct bitmap** conflict_set,
1358 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001359{
bartbedfd232009-03-26 19:07:15 +00001360 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001361
bartbedfd232009-03-26 19:07:15 +00001362 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1363 && tid != DRD_INVALID_THREADID);
1364 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001365
bart54803fe2009-06-21 09:26:27 +00001366 s_compute_conflict_set_count++;
bartbedfd232009-03-26 19:07:15 +00001367 s_conflict_set_bitmap_creation_count
1368 -= DRD_(bm_get_bitmap_creation_count)();
1369 s_conflict_set_bitmap2_creation_count
1370 -= DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001371
bartbedfd232009-03-26 19:07:15 +00001372 if (*conflict_set)
1373 {
bartf6ec1fe2009-06-21 18:07:35 +00001374 DRD_(bm_cleanup)(*conflict_set);
1375 DRD_(bm_init)(*conflict_set);
bartbedfd232009-03-26 19:07:15 +00001376 }
bartf6ec1fe2009-06-21 18:07:35 +00001377 else
1378 {
1379 *conflict_set = DRD_(bm_new)();
1380 }
bart26f73e12008-02-24 18:37:08 +00001381
bartbedfd232009-03-26 19:07:15 +00001382 if (s_trace_conflict_set)
1383 {
bart8f822af2009-06-08 18:20:42 +00001384 char* str;
bart26f73e12008-02-24 18:37:08 +00001385
bart8f822af2009-06-08 18:20:42 +00001386 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1387 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +00001388 "computing conflict set for thread %d with vc %s\n",
1389 tid, str);
bart8f822af2009-06-08 18:20:42 +00001390 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001391 }
sewardjaf44c822007-11-25 14:01:38 +00001392
bartbedfd232009-03-26 19:07:15 +00001393 p = DRD_(g_threadinfo)[tid].last;
1394 {
1395 unsigned j;
1396
1397 if (s_trace_conflict_set)
bart26f73e12008-02-24 18:37:08 +00001398 {
bart8f822af2009-06-08 18:20:42 +00001399 char* vc;
bartbedfd232009-03-26 19:07:15 +00001400
bart8f822af2009-06-08 18:20:42 +00001401 vc = DRD_(vc_aprint)(&p->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001402 VG_(message)(Vg_DebugMsg, "conflict set: thread [%d] at vc %s\n",
bart8f822af2009-06-08 18:20:42 +00001403 tid, vc);
1404 VG_(free)(vc);
bart26f73e12008-02-24 18:37:08 +00001405 }
sewardjaf44c822007-11-25 14:01:38 +00001406
bart8f822af2009-06-08 18:20:42 +00001407 for (j = 0; j < DRD_N_THREADS; j++)
bartbedfd232009-03-26 19:07:15 +00001408 {
1409 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1410 {
bart8f822af2009-06-08 18:20:42 +00001411 Segment* q;
bartbedfd232009-03-26 19:07:15 +00001412 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1413 {
1414 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1415 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1416 {
1417 if (s_trace_conflict_set)
1418 {
bart8f822af2009-06-08 18:20:42 +00001419 char* str;
1420
1421 str = DRD_(vc_aprint)(&q->vc);
1422 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001423 "conflict set: [%d] merging segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001424 j, str);
1425 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001426 }
bart8f822af2009-06-08 18:20:42 +00001427 DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q));
bartbedfd232009-03-26 19:07:15 +00001428 }
1429 else
1430 {
1431 if (s_trace_conflict_set)
1432 {
bart8f822af2009-06-08 18:20:42 +00001433 char* str;
1434
1435 str = DRD_(vc_aprint)(&q->vc);
1436 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001437 "conflict set: [%d] ignoring segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001438 j, str);
1439 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001440 }
1441 }
1442 }
1443 }
1444 }
1445 }
sewardjaf44c822007-11-25 14:01:38 +00001446
bartbedfd232009-03-26 19:07:15 +00001447 s_conflict_set_bitmap_creation_count
1448 += DRD_(bm_get_bitmap_creation_count)();
1449 s_conflict_set_bitmap2_creation_count
1450 += DRD_(bm_get_bitmap2_creation_count)();
1451
bart8f822af2009-06-08 18:20:42 +00001452 if (s_trace_conflict_set_bm)
bartbedfd232009-03-26 19:07:15 +00001453 {
sewardj1e29ebc2009-07-15 14:49:17 +00001454 VG_(message)(Vg_DebugMsg, "[%d] new conflict set:\n", tid);
bartbedfd232009-03-26 19:07:15 +00001455 DRD_(bm_print)(*conflict_set);
sewardj1e29ebc2009-07-15 14:49:17 +00001456 VG_(message)(Vg_DebugMsg, "[%d] end of new conflict set.\n", tid);
bartbedfd232009-03-26 19:07:15 +00001457 }
sewardjaf44c822007-11-25 14:01:38 +00001458}
1459
bart8f822af2009-06-08 18:20:42 +00001460/**
1461 * Update the conflict set after the vector clock of thread tid has been
1462 * updated from old_vc to its current value, either because a new segment has
1463 * been created or because of a synchronization operation.
1464 */
1465void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
1466 const VectorClock* const old_vc)
1467{
1468 const VectorClock* new_vc;
1469 Segment* p;
1470 unsigned j;
1471
1472 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1473 && tid != DRD_INVALID_THREADID);
1474 tl_assert(old_vc);
1475 tl_assert(tid == DRD_(g_drd_running_tid));
1476 tl_assert(DRD_(g_conflict_set));
1477
1478 if (s_trace_conflict_set)
1479 {
1480 char* str;
1481
1482 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1483 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +00001484 "updating conflict set for thread %d with vc %s\n",
1485 tid, str);
bart8f822af2009-06-08 18:20:42 +00001486 VG_(free)(str);
1487 }
1488
1489 new_vc = &DRD_(g_threadinfo)[tid].last->vc;
1490
1491 DRD_(bm_unmark)(DRD_(g_conflict_set));
1492
1493 for (j = 0; j < DRD_N_THREADS; j++)
1494 {
1495 Segment* q;
1496
1497 if (j == tid || ! DRD_(IsValidDrdThreadId)(j))
1498 continue;
1499
1500 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1501 {
1502 const int included_in_old_conflict_set
1503 = ! DRD_(vc_lte)(&q->vc, old_vc)
1504 && ! DRD_(vc_lte)(old_vc, &q->vc);
1505 const int included_in_new_conflict_set
1506 = ! DRD_(vc_lte)(&q->vc, new_vc)
1507 && ! DRD_(vc_lte)(new_vc, &q->vc);
1508 if (included_in_old_conflict_set != included_in_new_conflict_set)
1509 {
1510 if (s_trace_conflict_set)
1511 {
1512 char* str;
1513
1514 str = DRD_(vc_aprint)(&q->vc);
1515 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001516 "conflict set: [%d] merging segment %s\n", j, str);
bart8f822af2009-06-08 18:20:42 +00001517 VG_(free)(str);
1518 }
1519 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1520 }
1521 else
1522 {
1523 if (s_trace_conflict_set)
1524 {
1525 char* str;
1526
1527 str = DRD_(vc_aprint)(&q->vc);
1528 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001529 "conflict set: [%d] ignoring segment %s\n", j, str);
bart8f822af2009-06-08 18:20:42 +00001530 VG_(free)(str);
1531 }
1532 }
1533 }
1534 }
1535
1536 DRD_(bm_clear_marked)(DRD_(g_conflict_set));
1537
1538 p = DRD_(g_threadinfo)[tid].last;
1539 {
1540 for (j = 0; j < DRD_N_THREADS; j++)
1541 {
1542 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1543 {
1544 Segment* q;
1545 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1546 {
1547 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1548 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1549 {
1550 DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1551 }
1552 }
1553 }
1554 }
1555 }
1556
1557 DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set));
1558
bart54803fe2009-06-21 09:26:27 +00001559 s_update_conflict_set_count++;
bart8f822af2009-06-08 18:20:42 +00001560
1561 if (s_trace_conflict_set_bm)
1562 {
sewardj1e29ebc2009-07-15 14:49:17 +00001563 VG_(message)(Vg_DebugMsg, "[%d] updated conflict set:\n", tid);
bart8f822af2009-06-08 18:20:42 +00001564 DRD_(bm_print)(DRD_(g_conflict_set));
sewardj1e29ebc2009-07-15 14:49:17 +00001565 VG_(message)(Vg_DebugMsg, "[%d] end of updated conflict set.\n", tid);
bart8f822af2009-06-08 18:20:42 +00001566 }
1567
1568 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1569}
1570
bart86a87df2009-03-04 19:26:47 +00001571/** Report the number of context switches performed. */
bart62a784c2009-02-15 13:11:14 +00001572ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001573{
bartbedfd232009-03-26 19:07:15 +00001574 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +00001575}
1576
bart86a87df2009-03-04 19:26:47 +00001577/** Report the number of ordered segments that have been discarded. */
bart62a784c2009-02-15 13:11:14 +00001578ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001579{
bartbedfd232009-03-26 19:07:15 +00001580 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +00001581}
1582
bart54803fe2009-06-21 09:26:27 +00001583/** Return how many times the conflict set has been updated entirely. */
1584ULong DRD_(thread_get_compute_conflict_set_count)()
sewardjaf44c822007-11-25 14:01:38 +00001585{
bart54803fe2009-06-21 09:26:27 +00001586 return s_compute_conflict_set_count;
1587}
1588
1589/** Return how many times the conflict set has been updated partially. */
1590ULong DRD_(thread_get_update_conflict_set_count)(void)
1591{
bartbedfd232009-03-26 19:07:15 +00001592 return s_update_conflict_set_count;
sewardjaf44c822007-11-25 14:01:38 +00001593}
1594
bart86a87df2009-03-04 19:26:47 +00001595/**
barte5214662009-06-21 11:51:23 +00001596 * Return how many times the conflict set has been updated partially
1597 * because a new segment has been created.
1598 */
1599ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void)
1600{
1601 return s_update_conflict_set_new_sg_count;
1602}
1603
1604/**
1605 * Return how many times the conflict set has been updated partially
1606 * because of combining vector clocks due to synchronization operations
1607 * other than reader/writer lock or barrier operations.
1608 */
1609ULong DRD_(thread_get_update_conflict_set_sync_count)(void)
1610{
1611 return s_update_conflict_set_sync_count;
1612}
1613
1614/**
1615 * Return how many times the conflict set has been updated partially
1616 * because of thread joins.
1617 */
1618ULong DRD_(thread_get_update_conflict_set_join_count)(void)
1619{
1620 return s_update_conflict_set_join_count;
1621}
1622
1623/**
bart86a87df2009-03-04 19:26:47 +00001624 * Return the number of first-level bitmaps that have been created during
1625 * conflict set updates.
1626 */
bart62a784c2009-02-15 13:11:14 +00001627ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001628{
bartbedfd232009-03-26 19:07:15 +00001629 return s_conflict_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001630}
1631
bart86a87df2009-03-04 19:26:47 +00001632/**
1633 * Return the number of second-level bitmaps that have been created during
1634 * conflict set updates.
1635 */
bart62a784c2009-02-15 13:11:14 +00001636ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001637{
bartbedfd232009-03-26 19:07:15 +00001638 return s_conflict_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001639}