blob: f85a5b0923e525b41769e71c132c14fb1d21eb8b [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
bart5c7e6b62011-02-03 17:47:50 +0000507/** Called just after fork() in the child process. */
508void DRD_(drd_thread_atfork_child)(const DrdThreadId tid)
509{
510 unsigned i;
511
512 for (i = 1; i < DRD_N_THREADS; i++)
513 {
514 if (i == tid)
515 continue;
516 if (DRD_(IsValidDrdThreadId(i)))
517 DRD_(thread_delete)(i);
518 tl_assert(!DRD_(IsValidDrdThreadId(i)));
519 }
520}
521
bart9b2974a2008-09-27 12:35:31 +0000522/** Called just before pthread_cancel(). */
bart62a784c2009-02-15 13:11:14 +0000523void DRD_(thread_pre_cancel)(const DrdThreadId tid)
bartaf0691b2008-09-27 12:26:50 +0000524{
bartbedfd232009-03-26 19:07:15 +0000525 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
526 && tid != DRD_INVALID_THREADID);
527 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bartaf0691b2008-09-27 12:26:50 +0000528
bartbedfd232009-03-26 19:07:15 +0000529 DRD_(g_threadinfo)[tid].synchr_nesting = 0;
bartaf0691b2008-09-27 12:26:50 +0000530}
531
barte7dff242009-04-23 17:12:39 +0000532/**
533 * Store the POSIX thread ID for the specified thread.
534 *
535 * @note This function can be called two times for the same thread -- see also
536 * the comment block preceding the pthread_create() wrapper in
537 * drd_pthread_intercepts.c.
538 */
bart62a784c2009-02-15 13:11:14 +0000539void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
sewardjaf44c822007-11-25 14:01:38 +0000540{
bartbedfd232009-03-26 19:07:15 +0000541 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
542 && tid != DRD_INVALID_THREADID);
barte7dff242009-04-23 17:12:39 +0000543 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID
544 || DRD_(g_threadinfo)[tid].pt_threadid == ptid);
bartbedfd232009-03-26 19:07:15 +0000545 tl_assert(ptid != INVALID_POSIX_THREADID);
546 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
547 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardjaf44c822007-11-25 14:01:38 +0000548}
549
bart86a87df2009-03-04 19:26:47 +0000550/** Returns true for joinable threads and false for detached threads. */
bart62a784c2009-02-15 13:11:14 +0000551Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000552{
bartbedfd232009-03-26 19:07:15 +0000553 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
554 && tid != DRD_INVALID_THREADID);
555 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000556}
557
bart86a87df2009-03-04 19:26:47 +0000558/** Store the thread mode: joinable or detached. */
bart62a784c2009-02-15 13:11:14 +0000559void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000560{
bartbedfd232009-03-26 19:07:15 +0000561 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
562 && tid != DRD_INVALID_THREADID);
563 tl_assert(!! joinable == joinable);
564 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bart8f822af2009-06-08 18:20:42 +0000565
bartbedfd232009-03-26 19:07:15 +0000566 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000567}
568
bartdd75cdf2009-07-24 08:20:10 +0000569/** Tells DRD that the calling thread is about to enter pthread_create(). */
570void DRD_(thread_entering_pthread_create)(const DrdThreadId tid)
571{
572 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
573 && tid != DRD_INVALID_THREADID);
574 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
575 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level >= 0);
576
577 DRD_(g_threadinfo)[tid].pthread_create_nesting_level++;
578}
579
580/** Tells DRD that the calling thread has left pthread_create(). */
581void DRD_(thread_left_pthread_create)(const DrdThreadId tid)
582{
583 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
584 && tid != DRD_INVALID_THREADID);
585 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
586 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level > 0);
587
588 DRD_(g_threadinfo)[tid].pthread_create_nesting_level--;
589}
590
bartd45d9952009-05-31 18:53:54 +0000591/** Obtain the thread number and the user-assigned thread name. */
592const char* DRD_(thread_get_name)(const DrdThreadId tid)
593{
594 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
595 && tid != DRD_INVALID_THREADID);
596
597 return DRD_(g_threadinfo)[tid].name;
598}
599
600/** Set the name of the specified thread. */
601void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name)
602{
603 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
604 && tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000605
bartd45d9952009-05-31 18:53:54 +0000606 if (name == NULL || name[0] == 0)
607 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
608 sizeof(DRD_(g_threadinfo)[tid].name),
609 "Thread %d",
610 tid);
611 else
612 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
613 sizeof(DRD_(g_threadinfo)[tid].name),
614 "Thread %d (%s)",
615 tid, name);
616 DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0;
617}
618
bart86a87df2009-03-04 19:26:47 +0000619/**
620 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
621 * conflict set.
622 */
bart62a784c2009-02-15 13:11:14 +0000623void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000624{
bartbedfd232009-03-26 19:07:15 +0000625 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000626
bartbedfd232009-03-26 19:07:15 +0000627 if (vg_tid != s_vg_running_tid)
628 {
629 DRD_(thread_set_running_tid)(vg_tid,
630 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
631 }
sewardj8b09d4f2007-12-04 21:27:18 +0000632
bartbedfd232009-03-26 19:07:15 +0000633 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
634 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000635}
636
bart86a87df2009-03-04 19:26:47 +0000637/**
638 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
639 * conflict set.
640 */
bart62a784c2009-02-15 13:11:14 +0000641void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
642 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000643{
bartbedfd232009-03-26 19:07:15 +0000644 tl_assert(vg_tid != VG_INVALID_THREADID);
645 tl_assert(drd_tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000646
bartbedfd232009-03-26 19:07:15 +0000647 if (vg_tid != s_vg_running_tid)
648 {
649 if (s_trace_context_switches
650 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
651 {
652 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +0000653 "Context switch from thread %d to thread %d;"
sewardj1e29ebc2009-07-15 14:49:17 +0000654 " segments: %llu\n",
bart63c92ea2009-07-19 17:53:56 +0000655 DRD_(g_drd_running_tid), drd_tid,
bartbedfd232009-03-26 19:07:15 +0000656 DRD_(sg_get_segments_alive_count)());
657 }
658 s_vg_running_tid = vg_tid;
659 DRD_(g_drd_running_tid) = drd_tid;
660 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
661 s_context_switch_count++;
662 }
sewardj8b09d4f2007-12-04 21:27:18 +0000663
bartbedfd232009-03-26 19:07:15 +0000664 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
665 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000666}
667
bart86a87df2009-03-04 19:26:47 +0000668/**
669 * Increase the synchronization nesting counter. Must be called before the
670 * client calls a synchronization function.
671 */
bart62a784c2009-02-15 13:11:14 +0000672int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000673{
bartbedfd232009-03-26 19:07:15 +0000674 tl_assert(DRD_(IsValidDrdThreadId)(tid));
675 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000676}
677
bart86a87df2009-03-04 19:26:47 +0000678/**
679 * Decrease the synchronization nesting counter. Must be called after the
680 * client left a synchronization function.
681 */
bart62a784c2009-02-15 13:11:14 +0000682int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000683{
bartbedfd232009-03-26 19:07:15 +0000684 tl_assert(DRD_(IsValidDrdThreadId)(tid));
685 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
686 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000687}
688
bart86a87df2009-03-04 19:26:47 +0000689/** Returns the synchronization nesting counter. */
bart62a784c2009-02-15 13:11:14 +0000690int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000691{
bartbedfd232009-03-26 19:07:15 +0000692 tl_assert(DRD_(IsValidDrdThreadId)(tid));
693 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000694}
695
bart1a473c72008-03-13 19:03:38 +0000696/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000697static
bart86a87df2009-03-04 19:26:47 +0000698void thread_append_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000699{
bartbedfd232009-03-26 19:07:15 +0000700 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
701 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000702
703#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
704 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
705#endif
706
bartbedfd232009-03-26 19:07:15 +0000707 sg->prev = DRD_(g_threadinfo)[tid].last;
708 sg->next = 0;
709 if (DRD_(g_threadinfo)[tid].last)
710 DRD_(g_threadinfo)[tid].last->next = sg;
711 DRD_(g_threadinfo)[tid].last = sg;
712 if (DRD_(g_threadinfo)[tid].first == 0)
713 DRD_(g_threadinfo)[tid].first = sg;
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
sewardjaf44c822007-11-25 14:01:38 +0000718}
719
bart324a23b2009-02-15 12:14:52 +0000720/**
721 * Remove a segment from the segment list of thread threadid, and free the
722 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000723 */
bart62a784c2009-02-15 13:11:14 +0000724static
bart86a87df2009-03-04 19:26:47 +0000725void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000726{
bartbedfd232009-03-26 19:07:15 +0000727 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
728 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000729
730#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
731 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
732#endif
bart26f73e12008-02-24 18:37:08 +0000733
bartbedfd232009-03-26 19:07:15 +0000734 if (sg->prev)
735 sg->prev->next = sg->next;
736 if (sg->next)
737 sg->next->prev = sg->prev;
738 if (sg == DRD_(g_threadinfo)[tid].first)
739 DRD_(g_threadinfo)[tid].first = sg->next;
740 if (sg == DRD_(g_threadinfo)[tid].last)
741 DRD_(g_threadinfo)[tid].last = sg->prev;
742 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000743
bart8f822af2009-06-08 18:20:42 +0000744#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
745 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
746#endif
sewardjaf44c822007-11-25 14:01:38 +0000747}
748
bart86a87df2009-03-04 19:26:47 +0000749/**
750 * Returns a pointer to the vector clock of the most recent segment associated
751 * with thread 'tid'.
752 */
bart62a784c2009-02-15 13:11:14 +0000753VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000754{
bartbedfd232009-03-26 19:07:15 +0000755 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
756 && tid != DRD_INVALID_THREADID);
757 tl_assert(DRD_(g_threadinfo)[tid].last);
758 return &DRD_(g_threadinfo)[tid].last->vc;
sewardjaf44c822007-11-25 14:01:38 +0000759}
760
bart324a23b2009-02-15 12:14:52 +0000761/**
762 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000763 */
bart62a784c2009-02-15 13:11:14 +0000764void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000765{
bartbedfd232009-03-26 19:07:15 +0000766 tl_assert(sg);
767 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
768 && tid != DRD_INVALID_THREADID);
769 tl_assert(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000770
bartbedfd232009-03-26 19:07:15 +0000771 DRD_(sg_put)(*sg);
772 *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000773}
774
sewardjaf44c822007-11-25 14:01:38 +0000775/**
776 * Compute the minimum of all latest vector clocks of all threads
777 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
bart86a87df2009-03-04 19:26:47 +0000778 *
sewardjaf44c822007-11-25 14:01:38 +0000779 * @param vc pointer to a vectorclock, holds result upon return.
780 */
bart62a784c2009-02-15 13:11:14 +0000781static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000782{
bartbedfd232009-03-26 19:07:15 +0000783 unsigned i;
784 Bool first;
785 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000786
bartbedfd232009-03-26 19:07:15 +0000787 first = True;
bart8f822af2009-06-08 18:20:42 +0000788 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000789 {
790 latest_sg = DRD_(g_threadinfo)[i].last;
791 if (latest_sg)
792 {
793 if (first)
794 DRD_(vc_assign)(vc, &latest_sg->vc);
795 else
796 DRD_(vc_min)(vc, &latest_sg->vc);
797 first = False;
798 }
799 }
sewardjaf44c822007-11-25 14:01:38 +0000800}
801
bart86a87df2009-03-04 19:26:47 +0000802/**
803 * Compute the maximum of all latest vector clocks of all threads.
804 *
805 * @param vc pointer to a vectorclock, holds result upon return.
806 */
bart62a784c2009-02-15 13:11:14 +0000807static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000808{
bartbedfd232009-03-26 19:07:15 +0000809 unsigned i;
810 Bool first;
811 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000812
bartbedfd232009-03-26 19:07:15 +0000813 first = True;
bart8f822af2009-06-08 18:20:42 +0000814 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000815 {
816 latest_sg = DRD_(g_threadinfo)[i].last;
817 if (latest_sg)
818 {
819 if (first)
820 DRD_(vc_assign)(vc, &latest_sg->vc);
821 else
822 DRD_(vc_combine)(vc, &latest_sg->vc);
823 first = False;
824 }
825 }
sewardjaf44c822007-11-25 14:01:38 +0000826}
827
828/**
bart5bd9f2d2008-03-03 20:31:58 +0000829 * Discard all segments that have a defined order against the latest vector
bart86a87df2009-03-04 19:26:47 +0000830 * clock of all threads -- these segments can no longer be involved in a
sewardjaf44c822007-11-25 14:01:38 +0000831 * data race.
832 */
bart8f822af2009-06-08 18:20:42 +0000833static void thread_discard_ordered_segments(void)
sewardjaf44c822007-11-25 14:01:38 +0000834{
bartbedfd232009-03-26 19:07:15 +0000835 unsigned i;
836 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000837
bartbedfd232009-03-26 19:07:15 +0000838 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000839
bartbedfd232009-03-26 19:07:15 +0000840 DRD_(vc_init)(&thread_vc_min, 0, 0);
841 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
842 if (DRD_(sg_get_trace)())
843 {
bart8f822af2009-06-08 18:20:42 +0000844 char *vc_min, *vc_max;
bartbedfd232009-03-26 19:07:15 +0000845 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000846
bartbedfd232009-03-26 19:07:15 +0000847 DRD_(vc_init)(&thread_vc_max, 0, 0);
848 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
bart8f822af2009-06-08 18:20:42 +0000849 vc_min = DRD_(vc_aprint)(&thread_vc_min);
850 vc_max = DRD_(vc_aprint)(&thread_vc_max);
851 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000852 "Discarding ordered segments -- min vc is %s, max vc is %s\n",
bart8f822af2009-06-08 18:20:42 +0000853 vc_min, vc_max);
854 VG_(free)(vc_min);
855 VG_(free)(vc_max);
bartbedfd232009-03-26 19:07:15 +0000856 DRD_(vc_cleanup)(&thread_vc_max);
857 }
sewardjaf44c822007-11-25 14:01:38 +0000858
bart8f822af2009-06-08 18:20:42 +0000859 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000860 {
861 Segment* sg;
862 Segment* sg_next;
863 for (sg = DRD_(g_threadinfo)[i].first;
864 sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
865 sg = sg_next)
866 {
867 thread_discard_segment(i, sg);
868 }
869 }
870 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +0000871}
872
bart324a23b2009-02-15 12:14:52 +0000873/**
bart8f822af2009-06-08 18:20:42 +0000874 * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper
875 * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if
876 * all segments in the set CS are ordered consistently against both sg1 and
877 * sg2. The set CS is defined as the set of segments that can immediately
878 * precede future segments via inter-thread synchronization operations. In
879 * DRD the set CS consists of the latest segment of each thread combined with
880 * all segments for which the reference count is strictly greater than one.
881 * The code below is an optimized version of the following:
882 *
883 * for (i = 0; i < DRD_N_THREADS; i++)
884 * {
885 * Segment* sg;
886 *
887 * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
888 * {
889 * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1)
890 * {
891 * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc)
892 * != DRD_(vc_lte)(&sg2->vc, &sg->vc)
893 * || DRD_(vc_lte)(&sg->vc, &sg1->vc)
894 * != DRD_(vc_lte)(&sg->vc, &sg2->vc))
895 * {
896 * return False;
897 * }
898 * }
899 * }
900 * }
901 */
902static Bool thread_consistent_segment_ordering(const DrdThreadId tid,
903 Segment* const sg1,
904 Segment* const sg2)
905{
906 unsigned i;
907
908 tl_assert(sg1->next);
909 tl_assert(sg2->next);
910 tl_assert(sg1->next == sg2);
911 tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc));
912
913 for (i = 0; i < DRD_N_THREADS; i++)
914 {
915 Segment* sg;
916
917 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
918 {
919 if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1)
920 {
921 if (DRD_(vc_lte)(&sg2->vc, &sg->vc))
922 break;
923 if (DRD_(vc_lte)(&sg1->vc, &sg->vc))
924 return False;
925 }
926 }
927 for (sg = DRD_(g_threadinfo)[i].last; sg; sg = sg->prev)
928 {
929 if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1)
930 {
931 if (DRD_(vc_lte)(&sg->vc, &sg1->vc))
932 break;
933 if (DRD_(vc_lte)(&sg->vc, &sg2->vc))
934 return False;
935 }
936 }
937 }
938 return True;
939}
940
941/**
bart324a23b2009-02-15 12:14:52 +0000942 * Merge all segments that may be merged without triggering false positives
943 * or discarding real data races. For the theoretical background of segment
bart8f822af2009-06-08 18:20:42 +0000944 * merging, see also the following paper: Mark Christiaens, Michiel Ronsse
945 * and Koen De Bosschere. Bounding the number of segment histories during
946 * data race detection. Parallel Computing archive, Volume 28, Issue 9,
947 * pp 1221-1238, September 2002. This paper contains a proof that merging
948 * consecutive segments for which the property equiv(s1,s2) holds can be
949 * merged without reducing the accuracy of datarace detection. Furthermore
950 * it is also proven that the total number of all segments will never grow
951 * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged
952 * every time a new segment is created. The property equiv(s1, s2) is defined
953 * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector
954 * clocks of segments s and s1 are ordered in the same way as those of segments
955 * s and s2. The set CS is defined as the set of existing segments s that have
956 * the potential to conflict with not yet created segments, either because the
957 * segment s is the latest segment of a thread or because it can become the
958 * immediate predecessor of a new segment due to a synchronization operation.
barta9c37392008-03-22 09:38:48 +0000959 */
960static void thread_merge_segments(void)
961{
bartbedfd232009-03-26 19:07:15 +0000962 unsigned i;
barta9c37392008-03-22 09:38:48 +0000963
bart8f822af2009-06-08 18:20:42 +0000964 s_new_segments_since_last_merge = 0;
965
966 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000967 {
968 Segment* sg;
barta9c37392008-03-22 09:38:48 +0000969
bart8f822af2009-06-08 18:20:42 +0000970#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
971 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
972#endif
barta9c37392008-03-22 09:38:48 +0000973
bartbedfd232009-03-26 19:07:15 +0000974 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
barta9c37392008-03-22 09:38:48 +0000975 {
bartbedfd232009-03-26 19:07:15 +0000976 if (DRD_(sg_get_refcnt)(sg) == 1
977 && sg->next
978 && DRD_(sg_get_refcnt)(sg->next) == 1
bart8f822af2009-06-08 18:20:42 +0000979 && sg->next->next
980 && thread_consistent_segment_ordering(i, sg, sg->next))
bartbedfd232009-03-26 19:07:15 +0000981 {
982 /* Merge sg and sg->next into sg. */
983 DRD_(sg_merge)(sg, sg->next);
984 thread_discard_segment(i, sg->next);
985 }
barta9c37392008-03-22 09:38:48 +0000986 }
barta9c37392008-03-22 09:38:48 +0000987
bart8f822af2009-06-08 18:20:42 +0000988#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
989 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
990#endif
bartbedfd232009-03-26 19:07:15 +0000991 }
barta9c37392008-03-22 09:38:48 +0000992}
993
bart324a23b2009-02-15 12:14:52 +0000994/**
bart324a23b2009-02-15 12:14:52 +0000995 * Create a new segment for the specified thread, and discard any segments
996 * that cannot cause races anymore.
sewardjaf44c822007-11-25 14:01:38 +0000997 */
bart62a784c2009-02-15 13:11:14 +0000998void DRD_(thread_new_segment)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000999{
bart8f822af2009-06-08 18:20:42 +00001000 Segment* last_sg;
bartbedfd232009-03-26 19:07:15 +00001001 Segment* new_sg;
bartd66e3a82008-04-06 15:02:17 +00001002
bartbedfd232009-03-26 19:07:15 +00001003 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1004 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +00001005 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001006
bart8f822af2009-06-08 18:20:42 +00001007 last_sg = DRD_(g_threadinfo)[tid].last;
bartbedfd232009-03-26 19:07:15 +00001008 new_sg = DRD_(sg_new)(tid, tid);
1009 thread_append_segment(tid, new_sg);
bart8f822af2009-06-08 18:20:42 +00001010 if (tid == DRD_(g_drd_running_tid) && last_sg)
barte5214662009-06-21 11:51:23 +00001011 {
bart8f822af2009-06-08 18:20:42 +00001012 DRD_(thread_update_conflict_set)(tid, &last_sg->vc);
barte5214662009-06-21 11:51:23 +00001013 s_update_conflict_set_new_sg_count++;
1014 }
bartd66e3a82008-04-06 15:02:17 +00001015
bart8f822af2009-06-08 18:20:42 +00001016 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001017
bart8f822af2009-06-08 18:20:42 +00001018 if (s_segment_merging
1019 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
bartbedfd232009-03-26 19:07:15 +00001020 {
bart8f822af2009-06-08 18:20:42 +00001021 thread_discard_ordered_segments();
bartbedfd232009-03-26 19:07:15 +00001022 thread_merge_segments();
1023 }
sewardjaf44c822007-11-25 14:01:38 +00001024}
1025
bart26f73e12008-02-24 18:37:08 +00001026/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart8f822af2009-06-08 18:20:42 +00001027void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +00001028{
bartbedfd232009-03-26 19:07:15 +00001029 tl_assert(joiner != joinee);
1030 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
1031 && joiner != DRD_INVALID_THREADID);
1032 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
1033 && joinee != DRD_INVALID_THREADID);
1034 tl_assert(DRD_(g_threadinfo)[joiner].last);
1035 tl_assert(DRD_(g_threadinfo)[joinee].last);
bart8f822af2009-06-08 18:20:42 +00001036
1037 if (DRD_(sg_get_trace)())
1038 {
1039 char *str1, *str2;
1040 str1 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc);
1041 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joinee].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001042 VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n",
bart8f822af2009-06-08 18:20:42 +00001043 str1, str2);
1044 VG_(free)(str1);
1045 VG_(free)(str2);
1046 }
barte5214662009-06-21 11:51:23 +00001047 if (joiner == DRD_(g_drd_running_tid))
1048 {
1049 VectorClock old_vc;
1050
1051 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[joiner].last->vc);
1052 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
bartf6ec1fe2009-06-21 18:07:35 +00001053 &DRD_(g_threadinfo)[joinee].last->vc);
barte5214662009-06-21 11:51:23 +00001054 DRD_(thread_update_conflict_set)(joiner, &old_vc);
1055 s_update_conflict_set_join_count++;
1056 DRD_(vc_cleanup)(&old_vc);
1057 }
1058 else
1059 {
1060 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
bartf6ec1fe2009-06-21 18:07:35 +00001061 &DRD_(g_threadinfo)[joinee].last->vc);
barte5214662009-06-21 11:51:23 +00001062 }
1063
1064 thread_discard_ordered_segments();
1065
bart8f822af2009-06-08 18:20:42 +00001066 if (DRD_(sg_get_trace)())
1067 {
1068 char* str;
1069 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001070 VG_(message)(Vg_DebugMsg, "After join: %s\n", str);
bart8f822af2009-06-08 18:20:42 +00001071 VG_(free)(str);
1072 }
sewardjaf44c822007-11-25 14:01:38 +00001073}
1074
bart324a23b2009-02-15 12:14:52 +00001075/**
bart8f822af2009-06-08 18:20:42 +00001076 * Update the vector clock of the last segment of thread tid with the
bartf6ec1fe2009-06-21 18:07:35 +00001077 * the vector clock of segment sg.
bart26f73e12008-02-24 18:37:08 +00001078 */
bartf6ec1fe2009-06-21 18:07:35 +00001079static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg)
sewardjaf44c822007-11-25 14:01:38 +00001080{
bart8f822af2009-06-08 18:20:42 +00001081 const VectorClock* const vc = &sg->vc;
1082
bartbedfd232009-03-26 19:07:15 +00001083 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1084 && tid != DRD_INVALID_THREADID);
1085 tl_assert(DRD_(g_threadinfo)[tid].last);
bart8f822af2009-06-08 18:20:42 +00001086 tl_assert(sg);
bartbedfd232009-03-26 19:07:15 +00001087 tl_assert(vc);
bart8f822af2009-06-08 18:20:42 +00001088
1089 if (tid != sg->tid)
1090 {
1091 VectorClock old_vc;
1092
1093 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc);
1094 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc);
1095 if (DRD_(sg_get_trace)())
1096 {
1097 char *str1, *str2;
1098 str1 = DRD_(vc_aprint)(&old_vc);
1099 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001100 VG_(message)(Vg_DebugMsg, "thread %d: vc %s -> %s\n", tid, str1, str2);
bart8f822af2009-06-08 18:20:42 +00001101 VG_(free)(str1);
1102 VG_(free)(str2);
1103 }
barte5214662009-06-21 11:51:23 +00001104
bart8f822af2009-06-08 18:20:42 +00001105 thread_discard_ordered_segments();
barte5214662009-06-21 11:51:23 +00001106
bart8f822af2009-06-08 18:20:42 +00001107 DRD_(thread_update_conflict_set)(tid, &old_vc);
barte5214662009-06-21 11:51:23 +00001108 s_update_conflict_set_sync_count++;
1109
bart8f822af2009-06-08 18:20:42 +00001110 DRD_(vc_cleanup)(&old_vc);
1111 }
1112 else
1113 {
1114 tl_assert(DRD_(vc_lte)(vc, &DRD_(g_threadinfo)[tid].last->vc));
1115 }
sewardjaf44c822007-11-25 14:01:38 +00001116}
1117
bart324a23b2009-02-15 12:14:52 +00001118/**
bartf6ec1fe2009-06-21 18:07:35 +00001119 * Create a new segment for thread tid and update the vector clock of the last
1120 * segment of this thread with the the vector clock of segment sg. Call this
1121 * function after thread tid had to wait because of thread synchronization
1122 * until the memory accesses in the segment sg finished.
1123 */
1124void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, const Segment* sg)
1125{
1126 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1127 && tid != DRD_INVALID_THREADID);
1128 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1129 tl_assert(sg);
1130
1131 thread_append_segment(tid, DRD_(sg_new)(tid, tid));
1132
1133 thread_combine_vc_sync(tid, sg);
1134
1135 if (s_segment_merging
1136 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
1137 {
1138 thread_discard_ordered_segments();
1139 thread_merge_segments();
1140 }
1141}
1142
1143/**
bart324a23b2009-02-15 12:14:52 +00001144 * Call this function whenever a thread is no longer using the memory
1145 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
1146 * increase.
bart26f73e12008-02-24 18:37:08 +00001147 */
bartf9427fd2010-08-29 09:19:07 +00001148void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2,
1149 const Bool dont_clear_access)
sewardjaf44c822007-11-25 14:01:38 +00001150{
bartbedfd232009-03-26 19:07:15 +00001151 DrdThreadId other_user;
1152 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001153
bartbedfd232009-03-26 19:07:15 +00001154 /* For all threads, mark the range [ a1, a2 [ as no longer in use. */
1155 other_user = DRD_INVALID_THREADID;
bart8f822af2009-06-08 18:20:42 +00001156 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001157 {
1158 Segment* p;
1159 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
sewardjaf44c822007-11-25 14:01:38 +00001160 {
bartbedfd232009-03-26 19:07:15 +00001161 if (other_user == DRD_INVALID_THREADID
1162 && i != DRD_(g_drd_running_tid))
1163 {
bartf9427fd2010-08-29 09:19:07 +00001164 if (UNLIKELY((!dont_clear_access
1165 && DRD_(bm_test_and_clear)(DRD_(sg_bm)(p), a1, a2))
1166 || (dont_clear_access
1167 && DRD_(bm_has_any_access)(DRD_(sg_bm)(p), a1, a2))
1168 ))
bartbedfd232009-03-26 19:07:15 +00001169 {
1170 other_user = i;
1171 }
1172 continue;
1173 }
bartf9427fd2010-08-29 09:19:07 +00001174 if (!dont_clear_access)
1175 DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001176 }
bartbedfd232009-03-26 19:07:15 +00001177 }
sewardjaf44c822007-11-25 14:01:38 +00001178
bartbedfd232009-03-26 19:07:15 +00001179 /*
1180 * If any other thread had accessed memory in [ a1, a2 [, update the
1181 * conflict set.
1182 */
1183 if (other_user != DRD_INVALID_THREADID
1184 && DRD_(bm_has_any_access)(DRD_(g_conflict_set), a1, a2))
1185 {
1186 thread_compute_conflict_set(&DRD_(g_conflict_set),
1187 DRD_(thread_get_running_tid)());
1188 }
sewardjaf44c822007-11-25 14:01:38 +00001189}
1190
bartd45d9952009-05-31 18:53:54 +00001191/** Specify whether memory loads should be recorded. */
1192void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001193{
bartbedfd232009-03-26 19:07:15 +00001194 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1195 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001196 tl_assert(enabled == !! enabled);
1197
1198 DRD_(g_threadinfo)[tid].is_recording_loads = enabled;
bart0268dfa2008-03-11 20:10:21 +00001199}
1200
bartd45d9952009-05-31 18:53:54 +00001201/** Specify whether memory stores should be recorded. */
1202void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001203{
bartbedfd232009-03-26 19:07:15 +00001204 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1205 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001206 tl_assert(enabled == !! enabled);
1207
1208 DRD_(g_threadinfo)[tid].is_recording_stores = enabled;
bart0268dfa2008-03-11 20:10:21 +00001209}
1210
bart86a87df2009-03-04 19:26:47 +00001211/**
1212 * Print the segment information for all threads.
1213 *
1214 * This function is only used for debugging purposes.
1215 */
bart62a784c2009-02-15 13:11:14 +00001216void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +00001217{
bartbedfd232009-03-26 19:07:15 +00001218 unsigned i;
1219 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001220
bart8f822af2009-06-08 18:20:42 +00001221 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001222 {
1223 if (DRD_(g_threadinfo)[i].first)
sewardjaf44c822007-11-25 14:01:38 +00001224 {
bartbedfd232009-03-26 19:07:15 +00001225 VG_(printf)("**************\n"
1226 "* thread %3d (%d/%d/%d/0x%lx/%d) *\n"
1227 "**************\n",
1228 i,
1229 DRD_(g_threadinfo)[i].vg_thread_exists,
1230 DRD_(g_threadinfo)[i].vg_threadid,
1231 DRD_(g_threadinfo)[i].posix_thread_exists,
1232 DRD_(g_threadinfo)[i].pt_threadid,
1233 DRD_(g_threadinfo)[i].detached_posix_thread);
1234 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
1235 {
1236 DRD_(sg_print)(p);
1237 }
sewardjaf44c822007-11-25 14:01:38 +00001238 }
bartbedfd232009-03-26 19:07:15 +00001239 }
sewardjaf44c822007-11-25 14:01:38 +00001240}
1241
bart86a87df2009-03-04 19:26:47 +00001242/** Show a call stack involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001243static void show_call_stack(const DrdThreadId tid,
1244 const Char* const msg,
1245 ExeContext* const callstack)
1246{
bartbedfd232009-03-26 19:07:15 +00001247 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +00001248
bart63c92ea2009-07-19 17:53:56 +00001249 VG_(message)(Vg_UserMsg, "%s (thread %d)\n", msg, tid);
sewardjaf44c822007-11-25 14:01:38 +00001250
bartbedfd232009-03-26 19:07:15 +00001251 if (vg_tid != VG_INVALID_THREADID)
1252 {
1253 if (callstack)
1254 {
1255 VG_(pp_ExeContext)(callstack);
1256 }
1257 else
1258 {
1259 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
1260 }
1261 }
1262 else
1263 {
1264 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001265 " (thread finished, call stack no longer available)\n");
bartbedfd232009-03-26 19:07:15 +00001266 }
sewardjaf44c822007-11-25 14:01:38 +00001267}
1268
bart86a87df2009-03-04 19:26:47 +00001269/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001270static void
1271thread_report_conflicting_segments_segment(const DrdThreadId tid,
1272 const Addr addr,
1273 const SizeT size,
1274 const BmAccessTypeT access_type,
1275 const Segment* const p)
1276{
bartbedfd232009-03-26 19:07:15 +00001277 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001278
bartbedfd232009-03-26 19:07:15 +00001279 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1280 && tid != DRD_INVALID_THREADID);
1281 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +00001282
bart8f822af2009-06-08 18:20:42 +00001283 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001284 {
1285 if (i != tid)
sewardjaf44c822007-11-25 14:01:38 +00001286 {
bartbedfd232009-03-26 19:07:15 +00001287 Segment* q;
1288 for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev)
1289 {
1290 /*
bart31b983d2010-02-21 14:52:59 +00001291 * Since q iterates over the segments of thread i in order of
1292 * decreasing vector clocks, if q->vc <= p->vc, then
bartbedfd232009-03-26 19:07:15 +00001293 * q->next->vc <= p->vc will also hold. Hence, break out of the
1294 * loop once this condition is met.
1295 */
1296 if (DRD_(vc_lte)(&q->vc, &p->vc))
1297 break;
1298 if (! DRD_(vc_lte)(&p->vc, &q->vc))
1299 {
bart8f822af2009-06-08 18:20:42 +00001300 if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size,
bartbedfd232009-03-26 19:07:15 +00001301 access_type))
1302 {
1303 tl_assert(q->stacktrace);
1304 show_call_stack(i, "Other segment start",
1305 q->stacktrace);
1306 show_call_stack(i, "Other segment end",
1307 q->next ? q->next->stacktrace : 0);
1308 }
1309 }
1310 }
sewardjaf44c822007-11-25 14:01:38 +00001311 }
bartbedfd232009-03-26 19:07:15 +00001312 }
sewardjaf44c822007-11-25 14:01:38 +00001313}
1314
bart86a87df2009-03-04 19:26:47 +00001315/** Print information about all segments involved in a data race. */
bart62a784c2009-02-15 13:11:14 +00001316void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1317 const Addr addr,
1318 const SizeT size,
1319 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +00001320{
bartbedfd232009-03-26 19:07:15 +00001321 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001322
bartbedfd232009-03-26 19:07:15 +00001323 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1324 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001325
bartbedfd232009-03-26 19:07:15 +00001326 for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next)
1327 {
bart8f822af2009-06-08 18:20:42 +00001328 if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type))
bartbedfd232009-03-26 19:07:15 +00001329 {
1330 thread_report_conflicting_segments_segment(tid, addr, size,
1331 access_type, p);
1332 }
1333 }
sewardjaf44c822007-11-25 14:01:38 +00001334}
sewardjaf44c822007-11-25 14:01:38 +00001335
bart324a23b2009-02-15 12:14:52 +00001336/**
bart8f822af2009-06-08 18:20:42 +00001337 * Verify whether the conflict set for thread tid is up to date. Only perform
1338 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
1339 */
1340static Bool thread_conflict_set_up_to_date(const DrdThreadId tid)
1341{
1342 static int do_verify_conflict_set = -1;
1343 Bool result;
1344 struct bitmap* computed_conflict_set = 0;
1345
1346 if (do_verify_conflict_set < 0)
1347 do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0;
1348
1349 if (do_verify_conflict_set == 0)
1350 return True;
1351
1352 thread_compute_conflict_set(&computed_conflict_set, tid);
1353 result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set);
1354 if (! result)
1355 {
1356 VG_(printf)("actual conflict set:\n");
1357 DRD_(bm_print)(DRD_(g_conflict_set));
1358 VG_(printf)("\n");
1359 VG_(printf)("computed conflict set:\n");
1360 DRD_(bm_print)(computed_conflict_set);
1361 VG_(printf)("\n");
1362 }
1363 DRD_(bm_delete)(computed_conflict_set);
1364 return result;
1365}
1366
1367/**
1368 * Compute the conflict set: a bitmap that represents the union of all memory
1369 * accesses of all segments that are unordered to the current segment of the
1370 * thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001371 */
bart86a87df2009-03-04 19:26:47 +00001372static void thread_compute_conflict_set(struct bitmap** conflict_set,
1373 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001374{
bartbedfd232009-03-26 19:07:15 +00001375 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001376
bartbedfd232009-03-26 19:07:15 +00001377 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1378 && tid != DRD_INVALID_THREADID);
1379 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001380
bart54803fe2009-06-21 09:26:27 +00001381 s_compute_conflict_set_count++;
bartbedfd232009-03-26 19:07:15 +00001382 s_conflict_set_bitmap_creation_count
1383 -= DRD_(bm_get_bitmap_creation_count)();
1384 s_conflict_set_bitmap2_creation_count
1385 -= DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001386
bartbedfd232009-03-26 19:07:15 +00001387 if (*conflict_set)
1388 {
bartf6ec1fe2009-06-21 18:07:35 +00001389 DRD_(bm_cleanup)(*conflict_set);
1390 DRD_(bm_init)(*conflict_set);
bartbedfd232009-03-26 19:07:15 +00001391 }
bartf6ec1fe2009-06-21 18:07:35 +00001392 else
1393 {
1394 *conflict_set = DRD_(bm_new)();
1395 }
bart26f73e12008-02-24 18:37:08 +00001396
bartbedfd232009-03-26 19:07:15 +00001397 if (s_trace_conflict_set)
1398 {
bart8f822af2009-06-08 18:20:42 +00001399 char* str;
bart26f73e12008-02-24 18:37:08 +00001400
bart8f822af2009-06-08 18:20:42 +00001401 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1402 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +00001403 "computing conflict set for thread %d with vc %s\n",
1404 tid, str);
bart8f822af2009-06-08 18:20:42 +00001405 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001406 }
sewardjaf44c822007-11-25 14:01:38 +00001407
bartbedfd232009-03-26 19:07:15 +00001408 p = DRD_(g_threadinfo)[tid].last;
1409 {
1410 unsigned j;
1411
1412 if (s_trace_conflict_set)
bart26f73e12008-02-24 18:37:08 +00001413 {
bart8f822af2009-06-08 18:20:42 +00001414 char* vc;
bartbedfd232009-03-26 19:07:15 +00001415
bart8f822af2009-06-08 18:20:42 +00001416 vc = DRD_(vc_aprint)(&p->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001417 VG_(message)(Vg_DebugMsg, "conflict set: thread [%d] at vc %s\n",
bart8f822af2009-06-08 18:20:42 +00001418 tid, vc);
1419 VG_(free)(vc);
bart26f73e12008-02-24 18:37:08 +00001420 }
sewardjaf44c822007-11-25 14:01:38 +00001421
bart8f822af2009-06-08 18:20:42 +00001422 for (j = 0; j < DRD_N_THREADS; j++)
bartbedfd232009-03-26 19:07:15 +00001423 {
1424 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1425 {
bart8f822af2009-06-08 18:20:42 +00001426 Segment* q;
bartbedfd232009-03-26 19:07:15 +00001427 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1428 {
1429 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1430 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1431 {
1432 if (s_trace_conflict_set)
1433 {
bart8f822af2009-06-08 18:20:42 +00001434 char* str;
1435
1436 str = DRD_(vc_aprint)(&q->vc);
1437 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001438 "conflict set: [%d] merging segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001439 j, str);
1440 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001441 }
bart8f822af2009-06-08 18:20:42 +00001442 DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q));
bartbedfd232009-03-26 19:07:15 +00001443 }
1444 else
1445 {
1446 if (s_trace_conflict_set)
1447 {
bart8f822af2009-06-08 18:20:42 +00001448 char* str;
1449
1450 str = DRD_(vc_aprint)(&q->vc);
1451 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001452 "conflict set: [%d] ignoring segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001453 j, str);
1454 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001455 }
1456 }
1457 }
1458 }
1459 }
1460 }
sewardjaf44c822007-11-25 14:01:38 +00001461
bartbedfd232009-03-26 19:07:15 +00001462 s_conflict_set_bitmap_creation_count
1463 += DRD_(bm_get_bitmap_creation_count)();
1464 s_conflict_set_bitmap2_creation_count
1465 += DRD_(bm_get_bitmap2_creation_count)();
1466
bart8f822af2009-06-08 18:20:42 +00001467 if (s_trace_conflict_set_bm)
bartbedfd232009-03-26 19:07:15 +00001468 {
sewardj1e29ebc2009-07-15 14:49:17 +00001469 VG_(message)(Vg_DebugMsg, "[%d] new conflict set:\n", tid);
bartbedfd232009-03-26 19:07:15 +00001470 DRD_(bm_print)(*conflict_set);
sewardj1e29ebc2009-07-15 14:49:17 +00001471 VG_(message)(Vg_DebugMsg, "[%d] end of new conflict set.\n", tid);
bartbedfd232009-03-26 19:07:15 +00001472 }
sewardjaf44c822007-11-25 14:01:38 +00001473}
1474
bart8f822af2009-06-08 18:20:42 +00001475/**
1476 * Update the conflict set after the vector clock of thread tid has been
1477 * updated from old_vc to its current value, either because a new segment has
1478 * been created or because of a synchronization operation.
1479 */
1480void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
1481 const VectorClock* const old_vc)
1482{
1483 const VectorClock* new_vc;
1484 Segment* p;
1485 unsigned j;
1486
1487 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1488 && tid != DRD_INVALID_THREADID);
1489 tl_assert(old_vc);
1490 tl_assert(tid == DRD_(g_drd_running_tid));
1491 tl_assert(DRD_(g_conflict_set));
1492
1493 if (s_trace_conflict_set)
1494 {
1495 char* str;
1496
1497 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1498 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +00001499 "updating conflict set for thread %d with vc %s\n",
1500 tid, str);
bart8f822af2009-06-08 18:20:42 +00001501 VG_(free)(str);
1502 }
1503
1504 new_vc = &DRD_(g_threadinfo)[tid].last->vc;
1505
1506 DRD_(bm_unmark)(DRD_(g_conflict_set));
1507
1508 for (j = 0; j < DRD_N_THREADS; j++)
1509 {
1510 Segment* q;
1511
1512 if (j == tid || ! DRD_(IsValidDrdThreadId)(j))
1513 continue;
1514
1515 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1516 {
1517 const int included_in_old_conflict_set
1518 = ! DRD_(vc_lte)(&q->vc, old_vc)
1519 && ! DRD_(vc_lte)(old_vc, &q->vc);
1520 const int included_in_new_conflict_set
1521 = ! DRD_(vc_lte)(&q->vc, new_vc)
1522 && ! DRD_(vc_lte)(new_vc, &q->vc);
1523 if (included_in_old_conflict_set != included_in_new_conflict_set)
1524 {
1525 if (s_trace_conflict_set)
1526 {
1527 char* str;
1528
1529 str = DRD_(vc_aprint)(&q->vc);
1530 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001531 "conflict set: [%d] merging segment %s\n", j, str);
bart8f822af2009-06-08 18:20:42 +00001532 VG_(free)(str);
1533 }
1534 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1535 }
1536 else
1537 {
1538 if (s_trace_conflict_set)
1539 {
1540 char* str;
1541
1542 str = DRD_(vc_aprint)(&q->vc);
1543 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001544 "conflict set: [%d] ignoring segment %s\n", j, str);
bart8f822af2009-06-08 18:20:42 +00001545 VG_(free)(str);
1546 }
1547 }
1548 }
1549 }
1550
1551 DRD_(bm_clear_marked)(DRD_(g_conflict_set));
1552
1553 p = DRD_(g_threadinfo)[tid].last;
1554 {
1555 for (j = 0; j < DRD_N_THREADS; j++)
1556 {
1557 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1558 {
1559 Segment* q;
1560 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1561 {
1562 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1563 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1564 {
1565 DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1566 }
1567 }
1568 }
1569 }
1570 }
1571
1572 DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set));
1573
bart54803fe2009-06-21 09:26:27 +00001574 s_update_conflict_set_count++;
bart8f822af2009-06-08 18:20:42 +00001575
1576 if (s_trace_conflict_set_bm)
1577 {
sewardj1e29ebc2009-07-15 14:49:17 +00001578 VG_(message)(Vg_DebugMsg, "[%d] updated conflict set:\n", tid);
bart8f822af2009-06-08 18:20:42 +00001579 DRD_(bm_print)(DRD_(g_conflict_set));
sewardj1e29ebc2009-07-15 14:49:17 +00001580 VG_(message)(Vg_DebugMsg, "[%d] end of updated conflict set.\n", tid);
bart8f822af2009-06-08 18:20:42 +00001581 }
1582
1583 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1584}
1585
bart86a87df2009-03-04 19:26:47 +00001586/** Report the number of context switches performed. */
bart62a784c2009-02-15 13:11:14 +00001587ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001588{
bartbedfd232009-03-26 19:07:15 +00001589 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +00001590}
1591
bart86a87df2009-03-04 19:26:47 +00001592/** Report the number of ordered segments that have been discarded. */
bart62a784c2009-02-15 13:11:14 +00001593ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001594{
bartbedfd232009-03-26 19:07:15 +00001595 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +00001596}
1597
bart54803fe2009-06-21 09:26:27 +00001598/** Return how many times the conflict set has been updated entirely. */
1599ULong DRD_(thread_get_compute_conflict_set_count)()
sewardjaf44c822007-11-25 14:01:38 +00001600{
bart54803fe2009-06-21 09:26:27 +00001601 return s_compute_conflict_set_count;
1602}
1603
1604/** Return how many times the conflict set has been updated partially. */
1605ULong DRD_(thread_get_update_conflict_set_count)(void)
1606{
bartbedfd232009-03-26 19:07:15 +00001607 return s_update_conflict_set_count;
sewardjaf44c822007-11-25 14:01:38 +00001608}
1609
bart86a87df2009-03-04 19:26:47 +00001610/**
barte5214662009-06-21 11:51:23 +00001611 * Return how many times the conflict set has been updated partially
1612 * because a new segment has been created.
1613 */
1614ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void)
1615{
1616 return s_update_conflict_set_new_sg_count;
1617}
1618
1619/**
1620 * Return how many times the conflict set has been updated partially
1621 * because of combining vector clocks due to synchronization operations
1622 * other than reader/writer lock or barrier operations.
1623 */
1624ULong DRD_(thread_get_update_conflict_set_sync_count)(void)
1625{
1626 return s_update_conflict_set_sync_count;
1627}
1628
1629/**
1630 * Return how many times the conflict set has been updated partially
1631 * because of thread joins.
1632 */
1633ULong DRD_(thread_get_update_conflict_set_join_count)(void)
1634{
1635 return s_update_conflict_set_join_count;
1636}
1637
1638/**
bart86a87df2009-03-04 19:26:47 +00001639 * Return the number of first-level bitmaps that have been created during
1640 * conflict set updates.
1641 */
bart62a784c2009-02-15 13:11:14 +00001642ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001643{
bartbedfd232009-03-26 19:07:15 +00001644 return s_conflict_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001645}
1646
bart86a87df2009-03-04 19:26:47 +00001647/**
1648 * Return the number of second-level bitmaps that have been created during
1649 * conflict set updates.
1650 */
bart62a784c2009-02-15 13:11:14 +00001651ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001652{
bartbedfd232009-03-26 19:07:15 +00001653 return s_conflict_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001654}