blob: 2b692b143db1879c0244acc7f5814b8f7e4eb2d2 [file] [log] [blame]
bartbedfd232009-03-26 19:07:15 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
sewardjaf44c822007-11-25 14:01:38 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00004
bart86562bd2009-02-16 19:43:56 +00005 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
sewardjaf44c822007-11-25 14:01:38 +00006
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
26#include "drd_error.h"
bart09dc13f2009-02-14 15:13:31 +000027#include "drd_barrier.h"
bartd2c5eae2009-02-21 15:27:04 +000028#include "drd_clientobj.h"
bart09dc13f2009-02-14 15:13:31 +000029#include "drd_cond.h"
30#include "drd_mutex.h"
sewardjaf44c822007-11-25 14:01:38 +000031#include "drd_segment.h"
bart09dc13f2009-02-14 15:13:31 +000032#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000033#include "drd_suppression.h"
34#include "drd_thread.h"
bart82195c12008-04-13 17:35:08 +000035#include "pub_tool_vki.h"
sewardjaf44c822007-11-25 14:01:38 +000036#include "pub_tool_basics.h" // Addr, SizeT
37#include "pub_tool_errormgr.h" // VG_(unique_error)()
38#include "pub_tool_libcassert.h" // tl_assert()
39#include "pub_tool_libcbase.h" // VG_(strlen)()
40#include "pub_tool_libcprint.h" // VG_(printf)()
bart82195c12008-04-13 17:35:08 +000041#include "pub_tool_libcproc.h" // VG_(getenv)()
sewardjaf44c822007-11-25 14:01:38 +000042#include "pub_tool_machine.h"
43#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardj85642922008-01-14 11:54:56 +000044#include "pub_tool_options.h" // VG_(clo_backtrace_size)
sewardjaf44c822007-11-25 14:01:38 +000045#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
46
bart32ba2082008-06-05 08:53:42 +000047
sewardjaf44c822007-11-25 14:01:38 +000048
bart324a23b2009-02-15 12:14:52 +000049/* Local functions. */
sewardjaf44c822007-11-25 14:01:38 +000050
bart86a87df2009-03-04 19:26:47 +000051static void thread_append_segment(const DrdThreadId tid, Segment* const sg);
52static void thread_discard_segment(const DrdThreadId tid, Segment* const sg);
53static void thread_compute_conflict_set(struct bitmap** conflict_set,
54 const DrdThreadId tid);
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;
61static ULong s_update_conflict_set_count;
62static ULong s_conflict_set_new_segment_count;
63static ULong s_conflict_set_combine_vc_count;
64static ULong s_conflict_set_bitmap_creation_count;
65static ULong s_conflict_set_bitmap2_creation_count;
66static ThreadId s_vg_running_tid = VG_INVALID_THREADID;
bart324a23b2009-02-15 12:14:52 +000067DrdThreadId DRD_(g_drd_running_tid) = DRD_INVALID_THREADID;
68ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS];
69struct bitmap* DRD_(g_conflict_set);
bart86a87df2009-03-04 19:26:47 +000070static Bool s_trace_context_switches = False;
71static Bool s_trace_conflict_set = False;
72static Bool s_trace_fork_join = False;
73static Bool s_segment_merging = True;
sewardjaf44c822007-11-25 14:01:38 +000074
75
bart324a23b2009-02-15 12:14:52 +000076/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000077
bart86a87df2009-03-04 19:26:47 +000078/** Enables/disables context switch tracing. */
bart62a784c2009-02-15 13:11:14 +000079void DRD_(thread_trace_context_switches)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000080{
bartbedfd232009-03-26 19:07:15 +000081 tl_assert(t == False || t == True);
82 s_trace_context_switches = t;
bart26f73e12008-02-24 18:37:08 +000083}
84
bart86a87df2009-03-04 19:26:47 +000085/** Enables/disables conflict set tracing. */
bart62a784c2009-02-15 13:11:14 +000086void DRD_(thread_trace_conflict_set)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000087{
bartbedfd232009-03-26 19:07:15 +000088 tl_assert(t == False || t == True);
89 s_trace_conflict_set = t;
bart26f73e12008-02-24 18:37:08 +000090}
91
bart86a87df2009-03-04 19:26:47 +000092/** Report whether fork/join tracing is enabled. */
bart09dc13f2009-02-14 15:13:31 +000093Bool DRD_(thread_get_trace_fork_join)(void)
94{
bartbedfd232009-03-26 19:07:15 +000095 return s_trace_fork_join;
bart09dc13f2009-02-14 15:13:31 +000096}
97
bart86a87df2009-03-04 19:26:47 +000098/** Enables/disables fork/join tracing. */
bart09dc13f2009-02-14 15:13:31 +000099void DRD_(thread_set_trace_fork_join)(const Bool t)
100{
bartbedfd232009-03-26 19:07:15 +0000101 tl_assert(t == False || t == True);
102 s_trace_fork_join = t;
bart09dc13f2009-02-14 15:13:31 +0000103}
104
bart86a87df2009-03-04 19:26:47 +0000105/** Enables/disables segment merging. */
bart62a784c2009-02-15 13:11:14 +0000106void DRD_(thread_set_segment_merging)(const Bool m)
barta9c37392008-03-22 09:38:48 +0000107{
bartbedfd232009-03-26 19:07:15 +0000108 tl_assert(m == False || m == True);
109 s_segment_merging = m;
barta9c37392008-03-22 09:38:48 +0000110}
111
sewardjaf44c822007-11-25 14:01:38 +0000112/**
bart86a87df2009-03-04 19:26:47 +0000113 * Convert Valgrind's ThreadId into a DrdThreadId.
114 *
115 * @return DRD thread ID upon success and DRD_INVALID_THREADID if the passed
116 * Valgrind ThreadId does not yet exist.
bart324a23b2009-02-15 12:14:52 +0000117 */
bart62a784c2009-02-15 13:11:14 +0000118DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000119{
bartbedfd232009-03-26 19:07:15 +0000120 int i;
sewardjaf44c822007-11-25 14:01:38 +0000121
bartbedfd232009-03-26 19:07:15 +0000122 if (tid == VG_INVALID_THREADID)
123 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000124
bartbedfd232009-03-26 19:07:15 +0000125 for (i = 1; i < DRD_N_THREADS; i++)
126 {
127 if (DRD_(g_threadinfo)[i].vg_thread_exists == True
128 && DRD_(g_threadinfo)[i].vg_threadid == tid)
129 {
130 return i;
131 }
132 }
sewardjaf44c822007-11-25 14:01:38 +0000133
bartbedfd232009-03-26 19:07:15 +0000134 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000135}
136
bart86a87df2009-03-04 19:26:47 +0000137/** Allocate a new DRD thread ID for the specified Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000138static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000139{
bartbedfd232009-03-26 19:07:15 +0000140 int i;
sewardjaf44c822007-11-25 14:01:38 +0000141
bartbedfd232009-03-26 19:07:15 +0000142 tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000143
bartbedfd232009-03-26 19:07:15 +0000144 for (i = 1; i < DRD_N_THREADS; i++)
145 {
146 if (DRD_(g_threadinfo)[i].vg_thread_exists == False
147 && DRD_(g_threadinfo)[i].posix_thread_exists == False
148 && DRD_(g_threadinfo)[i].detached_posix_thread == False)
149 {
150 tl_assert(! DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000151
bartbedfd232009-03-26 19:07:15 +0000152 DRD_(g_threadinfo)[i].vg_thread_exists = True;
153 DRD_(g_threadinfo)[i].vg_threadid = tid;
154 DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID;
155 DRD_(g_threadinfo)[i].stack_min = 0;
156 DRD_(g_threadinfo)[i].stack_min_min = 0;
157 DRD_(g_threadinfo)[i].stack_startup = 0;
158 DRD_(g_threadinfo)[i].stack_max = 0;
159 DRD_(g_threadinfo)[i].is_recording = True;
160 DRD_(g_threadinfo)[i].synchr_nesting = 0;
161 tl_assert(DRD_(g_threadinfo)[i].first == 0);
162 tl_assert(DRD_(g_threadinfo)[i].last == 0);
bart86a87df2009-03-04 19:26:47 +0000163
bartbedfd232009-03-26 19:07:15 +0000164 tl_assert(DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000165
bartbedfd232009-03-26 19:07:15 +0000166 return i;
167 }
168 }
sewardjaf44c822007-11-25 14:01:38 +0000169
bartbedfd232009-03-26 19:07:15 +0000170 tl_assert(False);
sewardjaf44c822007-11-25 14:01:38 +0000171
bartbedfd232009-03-26 19:07:15 +0000172 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000173}
174
bart86a87df2009-03-04 19:26:47 +0000175/** Convert a POSIX thread ID into a DRD thread ID. */
bart62a784c2009-02-15 13:11:14 +0000176DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000177{
bartbedfd232009-03-26 19:07:15 +0000178 int i;
sewardjaf44c822007-11-25 14:01:38 +0000179
bartbedfd232009-03-26 19:07:15 +0000180 tl_assert(tid != INVALID_POSIX_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000181
bartbedfd232009-03-26 19:07:15 +0000182 for (i = 1; i < DRD_N_THREADS; i++)
183 {
184 if (DRD_(g_threadinfo)[i].posix_thread_exists
185 && DRD_(g_threadinfo)[i].pt_threadid == tid)
186 {
187 return i;
188 }
189 }
190 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000191}
192
bart86a87df2009-03-04 19:26:47 +0000193/** Convert a DRD thread ID into a Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000194ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000195{
bartbedfd232009-03-26 19:07:15 +0000196 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
197 && tid != DRD_INVALID_THREADID);
198 return (DRD_(g_threadinfo)[tid].vg_thread_exists
199 ? DRD_(g_threadinfo)[tid].vg_threadid
200 : VG_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000201}
202
bart23d3a4e2008-04-05 12:53:00 +0000203#if 0
bart324a23b2009-02-15 12:14:52 +0000204/**
205 * Sanity check of the doubly linked list of segments referenced by a
206 * ThreadInfo struct.
207 * @return True if sane, False if not.
sewardjaf44c822007-11-25 14:01:38 +0000208 */
bart62a784c2009-02-15 13:11:14 +0000209static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti)
sewardjaf44c822007-11-25 14:01:38 +0000210{
bartbedfd232009-03-26 19:07:15 +0000211 Segment* p;
212 for (p = ti->first; p; p = p->next) {
213 if (p->next && p->next->prev != p)
214 return False;
215 if (p->next == 0 && p != ti->last)
216 return False;
217 }
218 for (p = ti->last; p; p = p->prev) {
219 if (p->prev && p->prev->next != p)
220 return False;
221 if (p->prev == 0 && p != ti->first)
222 return False;
223 }
224 return True;
sewardjaf44c822007-11-25 14:01:38 +0000225}
bart23d3a4e2008-04-05 12:53:00 +0000226#endif
sewardjaf44c822007-11-25 14:01:38 +0000227
bart439c55f2009-02-15 10:38:37 +0000228/**
229 * Create the first segment for a newly started thread.
230 *
231 * This function is called from the handler installed via
232 * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler
233 * from the context of the creator thread, before the new thread has been
234 * created.
bart86a87df2009-03-04 19:26:47 +0000235 *
236 * @param[in] creator DRD thread ID of the creator thread.
237 * @param[in] vg_created Valgrind thread ID of the created thread.
238 *
239 * @return DRD thread ID of the created thread.
bart439c55f2009-02-15 10:38:37 +0000240 */
bart62a784c2009-02-15 13:11:14 +0000241DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
242 const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000243{
bartbedfd232009-03-26 19:07:15 +0000244 DrdThreadId created;
sewardjaf44c822007-11-25 14:01:38 +0000245
bartbedfd232009-03-26 19:07:15 +0000246 tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID);
247 created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created);
248 tl_assert(0 <= (int)created && created < DRD_N_THREADS
249 && created != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000250
bartbedfd232009-03-26 19:07:15 +0000251 tl_assert(DRD_(g_threadinfo)[created].first == 0);
252 tl_assert(DRD_(g_threadinfo)[created].last == 0);
253 thread_append_segment(created, DRD_(sg_new)(creator, created));
sewardjaf44c822007-11-25 14:01:38 +0000254
bartbedfd232009-03-26 19:07:15 +0000255 return created;
sewardjaf44c822007-11-25 14:01:38 +0000256}
257
bart439c55f2009-02-15 10:38:37 +0000258/**
bart86a87df2009-03-04 19:26:47 +0000259 * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called
260 * after the thread has been created and before any client instructions are run
bart439c55f2009-02-15 10:38:37 +0000261 * on the newly created thread, e.g. from the handler installed via
262 * VG_(track_pre_thread_first_insn)().
bart86a87df2009-03-04 19:26:47 +0000263 *
264 * @param[in] vg_created Valgrind thread ID of the newly created thread.
265 *
266 * @return DRD thread ID for the new thread.
bart439c55f2009-02-15 10:38:37 +0000267 */
bart62a784c2009-02-15 13:11:14 +0000268DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created)
bart439c55f2009-02-15 10:38:37 +0000269{
bartbedfd232009-03-26 19:07:15 +0000270 const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created);
bart439c55f2009-02-15 10:38:37 +0000271
bartbedfd232009-03-26 19:07:15 +0000272 tl_assert(0 <= (int)created && created < DRD_N_THREADS
273 && created != DRD_INVALID_THREADID);
bart439c55f2009-02-15 10:38:37 +0000274
bartbedfd232009-03-26 19:07:15 +0000275 DRD_(g_threadinfo)[created].stack_max
276 = VG_(thread_get_stack_max)(vg_created);
277 DRD_(g_threadinfo)[created].stack_startup
278 = DRD_(g_threadinfo)[created].stack_max;
279 DRD_(g_threadinfo)[created].stack_min
280 = DRD_(g_threadinfo)[created].stack_max;
281 DRD_(g_threadinfo)[created].stack_min_min
282 = DRD_(g_threadinfo)[created].stack_max;
283 DRD_(g_threadinfo)[created].stack_size
284 = VG_(thread_get_stack_size)(vg_created);
285 tl_assert(DRD_(g_threadinfo)[created].stack_max != 0);
bart439c55f2009-02-15 10:38:37 +0000286
bartbedfd232009-03-26 19:07:15 +0000287 return created;
bart439c55f2009-02-15 10:38:37 +0000288}
bart09dc13f2009-02-14 15:13:31 +0000289
bart324a23b2009-02-15 12:14:52 +0000290/**
291 * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just
292 * after thread drd_joiner joined thread drd_joinee.
293 */
bart09dc13f2009-02-14 15:13:31 +0000294void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
295{
bartbedfd232009-03-26 19:07:15 +0000296 tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner));
297 tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee));
298 DRD_(thread_new_segment)(drd_joinee);
299 DRD_(thread_combine_vc)(drd_joiner, drd_joinee);
300 DRD_(thread_new_segment)(drd_joiner);
bart09dc13f2009-02-14 15:13:31 +0000301
bartbedfd232009-03-26 19:07:15 +0000302 if (s_trace_fork_join)
303 {
304 const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner);
305 const ThreadId joinee = DRD_(DrdThreadIdToVgThreadId)(drd_joinee);
306 const unsigned msg_size = 256;
307 char* msg;
bart09dc13f2009-02-14 15:13:31 +0000308
bartbedfd232009-03-26 19:07:15 +0000309 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
310 tl_assert(msg);
311 VG_(snprintf)(msg, msg_size,
312 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
313 joiner, drd_joiner, joinee, drd_joinee);
314 if (joiner)
315 {
316 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
317 ", new vc: ");
318 DRD_(vc_snprint)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
319 DRD_(thread_get_vc)(drd_joiner));
320 }
321 VG_(message)(Vg_DebugMsg, "%s", msg);
322 VG_(free)(msg);
323 }
bart09dc13f2009-02-14 15:13:31 +0000324
bartbedfd232009-03-26 19:07:15 +0000325 if (! DRD_(get_check_stack_accesses)())
326 {
327 DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee)
328 - DRD_(thread_get_stack_size)(drd_joinee),
329 DRD_(thread_get_stack_max)(drd_joinee));
330 }
331 DRD_(clientobj_delete_thread)(drd_joinee);
332 DRD_(thread_delete)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000333}
334
bart324a23b2009-02-15 12:14:52 +0000335/**
336 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
337 * and accesses this data structure from multiple threads without locking.
338 * Any conflicting accesses in the range stack_startup..stack_max will be
339 * ignored.
340 */
bart62a784c2009-02-15 13:11:14 +0000341void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
342 const Addr stack_startup)
sewardjaf44c822007-11-25 14:01:38 +0000343{
bartbedfd232009-03-26 19:07:15 +0000344 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
345 && tid != DRD_INVALID_THREADID);
346 tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup);
347 tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max);
348 DRD_(g_threadinfo)[tid].stack_startup = stack_startup;
sewardjaf44c822007-11-25 14:01:38 +0000349}
350
bart86a87df2009-03-04 19:26:47 +0000351/** Return the stack pointer for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000352Addr DRD_(thread_get_stack_min)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000353{
bartbedfd232009-03-26 19:07:15 +0000354 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
355 && tid != DRD_INVALID_THREADID);
356 return DRD_(g_threadinfo)[tid].stack_min;
sewardjaf44c822007-11-25 14:01:38 +0000357}
358
bart86a87df2009-03-04 19:26:47 +0000359/**
360 * Return the lowest value that was ever assigned to the stack pointer
361 * for the specified thread.
362 */
bart62a784c2009-02-15 13:11:14 +0000363Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000364{
bartbedfd232009-03-26 19:07:15 +0000365 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
366 && tid != DRD_INVALID_THREADID);
367 return DRD_(g_threadinfo)[tid].stack_min_min;
bartcac53462008-03-29 09:27:08 +0000368}
369
bart86a87df2009-03-04 19:26:47 +0000370/** Return the top address for the stack of the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000371Addr DRD_(thread_get_stack_max)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000372{
bartbedfd232009-03-26 19:07:15 +0000373 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
374 && tid != DRD_INVALID_THREADID);
375 return DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000376}
377
bart86a87df2009-03-04 19:26:47 +0000378/** Return the maximum stack size for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000379SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000380{
bartbedfd232009-03-26 19:07:15 +0000381 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
382 && tid != DRD_INVALID_THREADID);
383 return DRD_(g_threadinfo)[tid].stack_size;
bartcac53462008-03-29 09:27:08 +0000384}
385
bart09dc13f2009-02-14 15:13:31 +0000386/**
387 * Clean up thread-specific data structures. Call this just after
388 * pthread_join().
sewardjaf44c822007-11-25 14:01:38 +0000389 */
bart62a784c2009-02-15 13:11:14 +0000390void DRD_(thread_delete)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000391{
bartbedfd232009-03-26 19:07:15 +0000392 Segment* sg;
393 Segment* sg_prev;
sewardjaf44c822007-11-25 14:01:38 +0000394
bartbedfd232009-03-26 19:07:15 +0000395 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart86a87df2009-03-04 19:26:47 +0000396
bartbedfd232009-03-26 19:07:15 +0000397 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0);
398 for (sg = DRD_(g_threadinfo)[tid].last; sg; sg = sg_prev)
399 {
400 sg_prev = sg->prev;
401 sg->prev = 0;
402 sg->next = 0;
403 DRD_(sg_put)(sg);
404 }
405 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
406 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
407 tl_assert(DRD_(g_threadinfo)[tid].detached_posix_thread == False);
408 DRD_(g_threadinfo)[tid].first = 0;
409 DRD_(g_threadinfo)[tid].last = 0;
bart86a87df2009-03-04 19:26:47 +0000410
bartbedfd232009-03-26 19:07:15 +0000411 tl_assert(! DRD_(IsValidDrdThreadId)(tid));
sewardjaf44c822007-11-25 14:01:38 +0000412}
413
bart324a23b2009-02-15 12:14:52 +0000414/**
415 * Called after a thread performed its last memory access and before
416 * thread_delete() is called. Note: thread_delete() is only called for
417 * joinable threads, not for detached threads.
418 */
bart62a784c2009-02-15 13:11:14 +0000419void DRD_(thread_finished)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000420{
bartbedfd232009-03-26 19:07:15 +0000421 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
422 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000423
bartbedfd232009-03-26 19:07:15 +0000424 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
sewardjaf44c822007-11-25 14:01:38 +0000425
bartbedfd232009-03-26 19:07:15 +0000426 if (DRD_(g_threadinfo)[tid].detached_posix_thread)
427 {
428 /*
429 * Once a detached thread has finished, its stack is deallocated and
430 * should no longer be taken into account when computing the conflict set.
431 */
432 DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000433
bartbedfd232009-03-26 19:07:15 +0000434 /*
435 * For a detached thread, calling pthread_exit() invalidates the
436 * POSIX thread ID associated with the detached thread. For joinable
437 * POSIX threads however, the POSIX thread ID remains live after the
438 * pthread_exit() call until pthread_join() is called.
439 */
440 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
441 }
sewardjaf44c822007-11-25 14:01:38 +0000442}
443
bart9b2974a2008-09-27 12:35:31 +0000444/** Called just before pthread_cancel(). */
bart62a784c2009-02-15 13:11:14 +0000445void DRD_(thread_pre_cancel)(const DrdThreadId tid)
bartaf0691b2008-09-27 12:26:50 +0000446{
bartbedfd232009-03-26 19:07:15 +0000447 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
448 && tid != DRD_INVALID_THREADID);
449 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bartaf0691b2008-09-27 12:26:50 +0000450
bartbedfd232009-03-26 19:07:15 +0000451 DRD_(g_threadinfo)[tid].synchr_nesting = 0;
bartaf0691b2008-09-27 12:26:50 +0000452}
453
bart86a87df2009-03-04 19:26:47 +0000454/** Store the POSIX thread ID for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000455void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
sewardjaf44c822007-11-25 14:01:38 +0000456{
bartbedfd232009-03-26 19:07:15 +0000457 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
458 && tid != DRD_INVALID_THREADID);
459 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID);
460 tl_assert(ptid != INVALID_POSIX_THREADID);
461 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
462 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardjaf44c822007-11-25 14:01:38 +0000463}
464
bart86a87df2009-03-04 19:26:47 +0000465/** Returns true for joinable threads and false for detached threads. */
bart62a784c2009-02-15 13:11:14 +0000466Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000467{
bartbedfd232009-03-26 19:07:15 +0000468 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
469 && tid != DRD_INVALID_THREADID);
470 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000471}
472
bart86a87df2009-03-04 19:26:47 +0000473/** Store the thread mode: joinable or detached. */
bart62a784c2009-02-15 13:11:14 +0000474void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000475{
bartbedfd232009-03-26 19:07:15 +0000476 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
477 && tid != DRD_INVALID_THREADID);
478 tl_assert(!! joinable == joinable);
479 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000480#if 0
bartbedfd232009-03-26 19:07:15 +0000481 VG_(message)(Vg_DebugMsg,
482 "thread_set_joinable(%d/%d, %s)",
483 tid,
484 DRD_(g_threadinfo)[tid].vg_threadid,
485 joinable ? "joinable" : "detached");
sewardjaf44c822007-11-25 14:01:38 +0000486#endif
bartbedfd232009-03-26 19:07:15 +0000487 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000488}
489
bart86a87df2009-03-04 19:26:47 +0000490/**
491 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
492 * conflict set.
493 */
bart62a784c2009-02-15 13:11:14 +0000494void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000495{
bartbedfd232009-03-26 19:07:15 +0000496 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000497
bartbedfd232009-03-26 19:07:15 +0000498 if (vg_tid != s_vg_running_tid)
499 {
500 DRD_(thread_set_running_tid)(vg_tid,
501 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
502 }
sewardj8b09d4f2007-12-04 21:27:18 +0000503
bartbedfd232009-03-26 19:07:15 +0000504 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
505 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000506}
507
bart86a87df2009-03-04 19:26:47 +0000508/**
509 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
510 * conflict set.
511 */
bart62a784c2009-02-15 13:11:14 +0000512void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
513 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000514{
bartbedfd232009-03-26 19:07:15 +0000515 tl_assert(vg_tid != VG_INVALID_THREADID);
516 tl_assert(drd_tid != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000517
bartbedfd232009-03-26 19:07:15 +0000518 if (vg_tid != s_vg_running_tid)
519 {
520 if (s_trace_context_switches
521 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
522 {
523 VG_(message)(Vg_DebugMsg,
524 "Context switch from thread %d/%d to thread %d/%d;"
525 " segments: %llu",
526 s_vg_running_tid, DRD_(g_drd_running_tid),
527 DRD_(DrdThreadIdToVgThreadId)(drd_tid), drd_tid,
528 DRD_(sg_get_segments_alive_count)());
529 }
530 s_vg_running_tid = vg_tid;
531 DRD_(g_drd_running_tid) = drd_tid;
532 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
533 s_context_switch_count++;
534 }
sewardj8b09d4f2007-12-04 21:27:18 +0000535
bartbedfd232009-03-26 19:07:15 +0000536 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
537 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000538}
539
bart86a87df2009-03-04 19:26:47 +0000540/**
541 * Increase the synchronization nesting counter. Must be called before the
542 * client calls a synchronization function.
543 */
bart62a784c2009-02-15 13:11:14 +0000544int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000545{
bartbedfd232009-03-26 19:07:15 +0000546 tl_assert(DRD_(IsValidDrdThreadId)(tid));
547 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000548}
549
bart86a87df2009-03-04 19:26:47 +0000550/**
551 * Decrease the synchronization nesting counter. Must be called after the
552 * client left a synchronization function.
553 */
bart62a784c2009-02-15 13:11:14 +0000554int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000555{
bartbedfd232009-03-26 19:07:15 +0000556 tl_assert(DRD_(IsValidDrdThreadId)(tid));
557 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
558 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000559}
560
bart86a87df2009-03-04 19:26:47 +0000561/** Returns the synchronization nesting counter. */
bart62a784c2009-02-15 13:11:14 +0000562int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000563{
bartbedfd232009-03-26 19:07:15 +0000564 tl_assert(DRD_(IsValidDrdThreadId)(tid));
565 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000566}
567
bart1a473c72008-03-13 19:03:38 +0000568/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000569static
bart86a87df2009-03-04 19:26:47 +0000570void thread_append_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000571{
bartbedfd232009-03-26 19:07:15 +0000572 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
573 && tid != DRD_INVALID_THREADID);
574 // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
575 sg->prev = DRD_(g_threadinfo)[tid].last;
576 sg->next = 0;
577 if (DRD_(g_threadinfo)[tid].last)
578 DRD_(g_threadinfo)[tid].last->next = sg;
579 DRD_(g_threadinfo)[tid].last = sg;
580 if (DRD_(g_threadinfo)[tid].first == 0)
581 DRD_(g_threadinfo)[tid].first = sg;
582 // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
sewardjaf44c822007-11-25 14:01:38 +0000583}
584
bart324a23b2009-02-15 12:14:52 +0000585/**
586 * Remove a segment from the segment list of thread threadid, and free the
587 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000588 */
bart62a784c2009-02-15 13:11:14 +0000589static
bart86a87df2009-03-04 19:26:47 +0000590void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000591{
bartbedfd232009-03-26 19:07:15 +0000592 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
593 && tid != DRD_INVALID_THREADID);
594 //tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
bart26f73e12008-02-24 18:37:08 +0000595
bartbedfd232009-03-26 19:07:15 +0000596 if (sg->prev)
597 sg->prev->next = sg->next;
598 if (sg->next)
599 sg->next->prev = sg->prev;
600 if (sg == DRD_(g_threadinfo)[tid].first)
601 DRD_(g_threadinfo)[tid].first = sg->next;
602 if (sg == DRD_(g_threadinfo)[tid].last)
603 DRD_(g_threadinfo)[tid].last = sg->prev;
604 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000605
bartbedfd232009-03-26 19:07:15 +0000606 //tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
sewardjaf44c822007-11-25 14:01:38 +0000607}
608
bart86a87df2009-03-04 19:26:47 +0000609/**
610 * Returns a pointer to the vector clock of the most recent segment associated
611 * with thread 'tid'.
612 */
bart62a784c2009-02-15 13:11:14 +0000613VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000614{
bartbedfd232009-03-26 19:07:15 +0000615 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
616 && tid != DRD_INVALID_THREADID);
617 tl_assert(DRD_(g_threadinfo)[tid].last);
618 return &DRD_(g_threadinfo)[tid].last->vc;
sewardjaf44c822007-11-25 14:01:38 +0000619}
620
bart324a23b2009-02-15 12:14:52 +0000621/**
622 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000623 */
bart62a784c2009-02-15 13:11:14 +0000624void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000625{
bartbedfd232009-03-26 19:07:15 +0000626 tl_assert(sg);
627 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
628 && tid != DRD_INVALID_THREADID);
629 tl_assert(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000630
bartbedfd232009-03-26 19:07:15 +0000631 DRD_(sg_put)(*sg);
632 *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000633}
634
sewardjaf44c822007-11-25 14:01:38 +0000635/**
636 * Compute the minimum of all latest vector clocks of all threads
637 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
bart86a87df2009-03-04 19:26:47 +0000638 *
sewardjaf44c822007-11-25 14:01:38 +0000639 * @param vc pointer to a vectorclock, holds result upon return.
640 */
bart62a784c2009-02-15 13:11:14 +0000641static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000642{
bartbedfd232009-03-26 19:07:15 +0000643 unsigned i;
644 Bool first;
645 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000646
bartbedfd232009-03-26 19:07:15 +0000647 first = True;
648 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
649 i++)
650 {
651 latest_sg = DRD_(g_threadinfo)[i].last;
652 if (latest_sg)
653 {
654 if (first)
655 DRD_(vc_assign)(vc, &latest_sg->vc);
656 else
657 DRD_(vc_min)(vc, &latest_sg->vc);
658 first = False;
659 }
660 }
sewardjaf44c822007-11-25 14:01:38 +0000661}
662
bart86a87df2009-03-04 19:26:47 +0000663/**
664 * Compute the maximum of all latest vector clocks of all threads.
665 *
666 * @param vc pointer to a vectorclock, holds result upon return.
667 */
bart62a784c2009-02-15 13:11:14 +0000668static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000669{
bartbedfd232009-03-26 19:07:15 +0000670 unsigned i;
671 Bool first;
672 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000673
bartbedfd232009-03-26 19:07:15 +0000674 first = True;
675 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
676 i++)
677 {
678 latest_sg = DRD_(g_threadinfo)[i].last;
679 if (latest_sg)
680 {
681 if (first)
682 DRD_(vc_assign)(vc, &latest_sg->vc);
683 else
684 DRD_(vc_combine)(vc, &latest_sg->vc);
685 first = False;
686 }
687 }
sewardjaf44c822007-11-25 14:01:38 +0000688}
689
690/**
bart5bd9f2d2008-03-03 20:31:58 +0000691 * Discard all segments that have a defined order against the latest vector
bart86a87df2009-03-04 19:26:47 +0000692 * clock of all threads -- these segments can no longer be involved in a
sewardjaf44c822007-11-25 14:01:38 +0000693 * data race.
694 */
bart62a784c2009-02-15 13:11:14 +0000695static void DRD_(thread_discard_ordered_segments)(void)
sewardjaf44c822007-11-25 14:01:38 +0000696{
bartbedfd232009-03-26 19:07:15 +0000697 unsigned i;
698 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000699
bartbedfd232009-03-26 19:07:15 +0000700 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000701
bartbedfd232009-03-26 19:07:15 +0000702 DRD_(vc_init)(&thread_vc_min, 0, 0);
703 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
704 if (DRD_(sg_get_trace)())
705 {
706 char msg[256];
707 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000708
bartbedfd232009-03-26 19:07:15 +0000709 DRD_(vc_init)(&thread_vc_max, 0, 0);
710 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
711 VG_(snprintf)(msg, sizeof(msg),
712 "Discarding ordered segments -- min vc is ");
713 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
714 &thread_vc_min);
715 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
716 ", max vc is ");
717 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
718 &thread_vc_max);
719 VG_(message)(Vg_UserMsg, "%s", msg);
720 DRD_(vc_cleanup)(&thread_vc_max);
721 }
sewardjaf44c822007-11-25 14:01:38 +0000722
bartbedfd232009-03-26 19:07:15 +0000723 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
724 i++)
725 {
726 Segment* sg;
727 Segment* sg_next;
728 for (sg = DRD_(g_threadinfo)[i].first;
729 sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
730 sg = sg_next)
731 {
732 thread_discard_segment(i, sg);
733 }
734 }
735 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +0000736}
737
bart324a23b2009-02-15 12:14:52 +0000738/**
739 * Merge all segments that may be merged without triggering false positives
740 * or discarding real data races. For the theoretical background of segment
741 * merging, see also the following paper:
742 * Mark Christiaens, Michiel Ronsse and Koen De Bosschere.
743 * Bounding the number of segment histories during data race detection.
744 * Parallel Computing archive, Volume 28, Issue 9, pp 1221-1238,
745 * September 2002.
barta9c37392008-03-22 09:38:48 +0000746 */
747static void thread_merge_segments(void)
748{
bartbedfd232009-03-26 19:07:15 +0000749 unsigned i;
barta9c37392008-03-22 09:38:48 +0000750
bartbedfd232009-03-26 19:07:15 +0000751 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
752 i++)
753 {
754 Segment* sg;
barta9c37392008-03-22 09:38:48 +0000755
bartbedfd232009-03-26 19:07:15 +0000756 // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
barta9c37392008-03-22 09:38:48 +0000757
bartbedfd232009-03-26 19:07:15 +0000758 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
barta9c37392008-03-22 09:38:48 +0000759 {
bartbedfd232009-03-26 19:07:15 +0000760 if (DRD_(sg_get_refcnt)(sg) == 1
761 && sg->next
762 && DRD_(sg_get_refcnt)(sg->next) == 1
763 && sg->next->next)
764 {
765 /* Merge sg and sg->next into sg. */
766 DRD_(sg_merge)(sg, sg->next);
767 thread_discard_segment(i, sg->next);
768 }
barta9c37392008-03-22 09:38:48 +0000769 }
barta9c37392008-03-22 09:38:48 +0000770
bartbedfd232009-03-26 19:07:15 +0000771 // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
772 }
barta9c37392008-03-22 09:38:48 +0000773}
774
bart324a23b2009-02-15 12:14:52 +0000775/**
bart324a23b2009-02-15 12:14:52 +0000776 * Create a new segment for the specified thread, and discard any segments
777 * that cannot cause races anymore.
sewardjaf44c822007-11-25 14:01:38 +0000778 */
bart62a784c2009-02-15 13:11:14 +0000779void DRD_(thread_new_segment)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000780{
bartbedfd232009-03-26 19:07:15 +0000781 Segment* new_sg;
bartd66e3a82008-04-06 15:02:17 +0000782
bartbedfd232009-03-26 19:07:15 +0000783 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
784 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000785
bartbedfd232009-03-26 19:07:15 +0000786 new_sg = DRD_(sg_new)(tid, tid);
787 thread_append_segment(tid, new_sg);
bartd66e3a82008-04-06 15:02:17 +0000788
bartbedfd232009-03-26 19:07:15 +0000789 thread_compute_conflict_set(&DRD_(g_conflict_set), DRD_(g_drd_running_tid));
790 s_conflict_set_new_segment_count++;
sewardjaf44c822007-11-25 14:01:38 +0000791
bartbedfd232009-03-26 19:07:15 +0000792 DRD_(thread_discard_ordered_segments)();
bart26f73e12008-02-24 18:37:08 +0000793
bartbedfd232009-03-26 19:07:15 +0000794 if (s_segment_merging)
795 {
796 thread_merge_segments();
797 }
sewardjaf44c822007-11-25 14:01:38 +0000798}
799
bart26f73e12008-02-24 18:37:08 +0000800/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart62a784c2009-02-15 13:11:14 +0000801void DRD_(thread_combine_vc)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +0000802{
bartbedfd232009-03-26 19:07:15 +0000803 tl_assert(joiner != joinee);
804 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
805 && joiner != DRD_INVALID_THREADID);
806 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
807 && joinee != DRD_INVALID_THREADID);
808 tl_assert(DRD_(g_threadinfo)[joiner].last);
809 tl_assert(DRD_(g_threadinfo)[joinee].last);
810 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
811 &DRD_(g_threadinfo)[joinee].last->vc);
812 DRD_(thread_discard_ordered_segments)();
sewardjaf44c822007-11-25 14:01:38 +0000813
bartbedfd232009-03-26 19:07:15 +0000814 if (joiner == DRD_(g_drd_running_tid))
815 {
816 thread_compute_conflict_set(&DRD_(g_conflict_set), joiner);
817 }
sewardjaf44c822007-11-25 14:01:38 +0000818}
819
bart324a23b2009-02-15 12:14:52 +0000820/**
821 * Call this function after thread 'tid' had to wait because of thread
822 * synchronization until the memory accesses in the segment with vector clock
823 * 'vc' finished.
bart26f73e12008-02-24 18:37:08 +0000824 */
bart62a784c2009-02-15 13:11:14 +0000825void DRD_(thread_combine_vc2)(DrdThreadId tid, const VectorClock* const vc)
sewardjaf44c822007-11-25 14:01:38 +0000826{
bartbedfd232009-03-26 19:07:15 +0000827 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
828 && tid != DRD_INVALID_THREADID);
829 tl_assert(DRD_(g_threadinfo)[tid].last);
830 tl_assert(vc);
831 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc);
832 thread_compute_conflict_set(&DRD_(g_conflict_set), tid);
833 DRD_(thread_discard_ordered_segments)();
834 s_conflict_set_combine_vc_count++;
sewardjaf44c822007-11-25 14:01:38 +0000835}
836
bart324a23b2009-02-15 12:14:52 +0000837/**
838 * Call this function whenever a thread is no longer using the memory
839 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
840 * increase.
bart26f73e12008-02-24 18:37:08 +0000841 */
bart62a784c2009-02-15 13:11:14 +0000842void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +0000843{
bartbedfd232009-03-26 19:07:15 +0000844 DrdThreadId other_user;
845 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +0000846
bartbedfd232009-03-26 19:07:15 +0000847 /* For all threads, mark the range [ a1, a2 [ as no longer in use. */
848 other_user = DRD_INVALID_THREADID;
849 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
850 i++)
851 {
852 Segment* p;
853 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
sewardjaf44c822007-11-25 14:01:38 +0000854 {
bartbedfd232009-03-26 19:07:15 +0000855 if (other_user == DRD_INVALID_THREADID
856 && i != DRD_(g_drd_running_tid))
857 {
858 if (UNLIKELY(DRD_(bm_test_and_clear)(p->bm, a1, a2)))
859 {
860 other_user = i;
861 }
862 continue;
863 }
864 DRD_(bm_clear)(p->bm, a1, a2);
sewardjaf44c822007-11-25 14:01:38 +0000865 }
bartbedfd232009-03-26 19:07:15 +0000866 }
sewardjaf44c822007-11-25 14:01:38 +0000867
bartbedfd232009-03-26 19:07:15 +0000868 /*
869 * If any other thread had accessed memory in [ a1, a2 [, update the
870 * conflict set.
871 */
872 if (other_user != DRD_INVALID_THREADID
873 && DRD_(bm_has_any_access)(DRD_(g_conflict_set), a1, a2))
874 {
875 thread_compute_conflict_set(&DRD_(g_conflict_set),
876 DRD_(thread_get_running_tid)());
877 }
sewardjaf44c822007-11-25 14:01:38 +0000878}
879
bart86a87df2009-03-04 19:26:47 +0000880/** Start recording memory access information. */
bart62a784c2009-02-15 13:11:14 +0000881void DRD_(thread_start_recording)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000882{
bartbedfd232009-03-26 19:07:15 +0000883 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
884 && tid != DRD_INVALID_THREADID);
885 tl_assert(! DRD_(g_threadinfo)[tid].is_recording);
886 DRD_(g_threadinfo)[tid].is_recording = True;
bart0268dfa2008-03-11 20:10:21 +0000887}
888
bart86a87df2009-03-04 19:26:47 +0000889/** Stop recording memory access information. */
bart62a784c2009-02-15 13:11:14 +0000890void DRD_(thread_stop_recording)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000891{
bartbedfd232009-03-26 19:07:15 +0000892 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
893 && tid != DRD_INVALID_THREADID);
894 tl_assert(DRD_(g_threadinfo)[tid].is_recording);
895 DRD_(g_threadinfo)[tid].is_recording = False;
bart0268dfa2008-03-11 20:10:21 +0000896}
897
bart86a87df2009-03-04 19:26:47 +0000898/**
899 * Print the segment information for all threads.
900 *
901 * This function is only used for debugging purposes.
902 */
bart62a784c2009-02-15 13:11:14 +0000903void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +0000904{
bartbedfd232009-03-26 19:07:15 +0000905 unsigned i;
906 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +0000907
bartbedfd232009-03-26 19:07:15 +0000908 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
909 i++)
910 {
911 if (DRD_(g_threadinfo)[i].first)
sewardjaf44c822007-11-25 14:01:38 +0000912 {
bartbedfd232009-03-26 19:07:15 +0000913 VG_(printf)("**************\n"
914 "* thread %3d (%d/%d/%d/0x%lx/%d) *\n"
915 "**************\n",
916 i,
917 DRD_(g_threadinfo)[i].vg_thread_exists,
918 DRD_(g_threadinfo)[i].vg_threadid,
919 DRD_(g_threadinfo)[i].posix_thread_exists,
920 DRD_(g_threadinfo)[i].pt_threadid,
921 DRD_(g_threadinfo)[i].detached_posix_thread);
922 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
923 {
924 DRD_(sg_print)(p);
925 }
sewardjaf44c822007-11-25 14:01:38 +0000926 }
bartbedfd232009-03-26 19:07:15 +0000927 }
sewardjaf44c822007-11-25 14:01:38 +0000928}
929
bart86a87df2009-03-04 19:26:47 +0000930/** Show a call stack involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +0000931static void show_call_stack(const DrdThreadId tid,
932 const Char* const msg,
933 ExeContext* const callstack)
934{
bartbedfd232009-03-26 19:07:15 +0000935 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +0000936
bartbedfd232009-03-26 19:07:15 +0000937 VG_(message)(Vg_UserMsg, "%s (thread %d/%d)", msg, vg_tid, tid);
sewardjaf44c822007-11-25 14:01:38 +0000938
bartbedfd232009-03-26 19:07:15 +0000939 if (vg_tid != VG_INVALID_THREADID)
940 {
941 if (callstack)
942 {
943 VG_(pp_ExeContext)(callstack);
944 }
945 else
946 {
947 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
948 }
949 }
950 else
951 {
952 VG_(message)(Vg_UserMsg,
953 " (thread finished, call stack no longer available)");
954 }
sewardjaf44c822007-11-25 14:01:38 +0000955}
956
bart86a87df2009-03-04 19:26:47 +0000957/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +0000958static void
959thread_report_conflicting_segments_segment(const DrdThreadId tid,
960 const Addr addr,
961 const SizeT size,
962 const BmAccessTypeT access_type,
963 const Segment* const p)
964{
bartbedfd232009-03-26 19:07:15 +0000965 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +0000966
bartbedfd232009-03-26 19:07:15 +0000967 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
968 && tid != DRD_INVALID_THREADID);
969 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +0000970
bartbedfd232009-03-26 19:07:15 +0000971 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
972 i++)
973 {
974 if (i != tid)
sewardjaf44c822007-11-25 14:01:38 +0000975 {
bartbedfd232009-03-26 19:07:15 +0000976 Segment* q;
977 for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev)
978 {
979 /*
980 * Since q iterates over the segments of thread i in order of
981 * decreasing vector clocks, if q->vc <= p->vc, then
982 * q->next->vc <= p->vc will also hold. Hence, break out of the
983 * loop once this condition is met.
984 */
985 if (DRD_(vc_lte)(&q->vc, &p->vc))
986 break;
987 if (! DRD_(vc_lte)(&p->vc, &q->vc))
988 {
989 if (DRD_(bm_has_conflict_with)(q->bm, addr, addr + size,
990 access_type))
991 {
992 tl_assert(q->stacktrace);
993 show_call_stack(i, "Other segment start",
994 q->stacktrace);
995 show_call_stack(i, "Other segment end",
996 q->next ? q->next->stacktrace : 0);
997 }
998 }
999 }
sewardjaf44c822007-11-25 14:01:38 +00001000 }
bartbedfd232009-03-26 19:07:15 +00001001 }
sewardjaf44c822007-11-25 14:01:38 +00001002}
1003
bart86a87df2009-03-04 19:26:47 +00001004/** Print information about all segments involved in a data race. */
bart62a784c2009-02-15 13:11:14 +00001005void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1006 const Addr addr,
1007 const SizeT size,
1008 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +00001009{
bartbedfd232009-03-26 19:07:15 +00001010 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001011
bartbedfd232009-03-26 19:07:15 +00001012 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1013 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001014
bartbedfd232009-03-26 19:07:15 +00001015 for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next)
1016 {
1017 if (DRD_(bm_has)(p->bm, addr, addr + size, access_type))
1018 {
1019 thread_report_conflicting_segments_segment(tid, addr, size,
1020 access_type, p);
1021 }
1022 }
sewardjaf44c822007-11-25 14:01:38 +00001023}
sewardjaf44c822007-11-25 14:01:38 +00001024
bart324a23b2009-02-15 12:14:52 +00001025/**
bart324a23b2009-02-15 12:14:52 +00001026 * Compute a bitmap that represents the union of all memory accesses of all
1027 * segments that are unordered to the current segment of the thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001028 */
bart86a87df2009-03-04 19:26:47 +00001029static void thread_compute_conflict_set(struct bitmap** conflict_set,
1030 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001031{
bartbedfd232009-03-26 19:07:15 +00001032 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001033
bartbedfd232009-03-26 19:07:15 +00001034 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1035 && tid != DRD_INVALID_THREADID);
1036 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001037
bartbedfd232009-03-26 19:07:15 +00001038 s_update_conflict_set_count++;
1039 s_conflict_set_bitmap_creation_count
1040 -= DRD_(bm_get_bitmap_creation_count)();
1041 s_conflict_set_bitmap2_creation_count
1042 -= DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001043
bartbedfd232009-03-26 19:07:15 +00001044 if (*conflict_set)
1045 {
1046 DRD_(bm_delete)(*conflict_set);
1047 }
1048 *conflict_set = DRD_(bm_new)();
bart26f73e12008-02-24 18:37:08 +00001049
bartbedfd232009-03-26 19:07:15 +00001050 if (s_trace_conflict_set)
1051 {
bart26f73e12008-02-24 18:37:08 +00001052 char msg[256];
1053
1054 VG_(snprintf)(msg, sizeof(msg),
bartbedfd232009-03-26 19:07:15 +00001055 "computing conflict set for thread %d/%d with vc ",
1056 DRD_(DrdThreadIdToVgThreadId)(tid), tid);
bart41b226c2009-02-14 16:55:19 +00001057 DRD_(vc_snprint)(msg + VG_(strlen)(msg),
1058 sizeof(msg) - VG_(strlen)(msg),
bartbedfd232009-03-26 19:07:15 +00001059 &DRD_(g_threadinfo)[tid].last->vc);
barta2b6e1b2008-03-17 18:32:39 +00001060 VG_(message)(Vg_UserMsg, "%s", msg);
bartbedfd232009-03-26 19:07:15 +00001061 }
sewardjaf44c822007-11-25 14:01:38 +00001062
bartbedfd232009-03-26 19:07:15 +00001063 p = DRD_(g_threadinfo)[tid].last;
1064 {
1065 unsigned j;
1066
1067 if (s_trace_conflict_set)
bart26f73e12008-02-24 18:37:08 +00001068 {
bartbedfd232009-03-26 19:07:15 +00001069 char msg[256];
1070
1071 VG_(snprintf)(msg, sizeof(msg),
1072 "conflict set: thread [%d] at vc ",
1073 tid);
1074 DRD_(vc_snprint)(msg + VG_(strlen)(msg),
1075 sizeof(msg) - VG_(strlen)(msg),
1076 &p->vc);
1077 VG_(message)(Vg_UserMsg, "%s", msg);
bart26f73e12008-02-24 18:37:08 +00001078 }
sewardjaf44c822007-11-25 14:01:38 +00001079
bartbedfd232009-03-26 19:07:15 +00001080 for (j = 0;
1081 j < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
1082 j++)
1083 {
1084 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1085 {
1086 const Segment* q;
1087 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1088 {
1089 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1090 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1091 {
1092 if (s_trace_conflict_set)
1093 {
1094 char msg[256];
1095 VG_(snprintf)(msg, sizeof(msg),
1096 "conflict set: [%d] merging segment ", j);
1097 DRD_(vc_snprint)(msg + VG_(strlen)(msg),
1098 sizeof(msg) - VG_(strlen)(msg),
1099 &q->vc);
1100 VG_(message)(Vg_UserMsg, "%s", msg);
1101 }
1102 DRD_(bm_merge2)(*conflict_set, q->bm);
1103 }
1104 else
1105 {
1106 if (s_trace_conflict_set)
1107 {
1108 char msg[256];
1109 VG_(snprintf)(msg, sizeof(msg),
1110 "conflict set: [%d] ignoring segment ", j);
1111 DRD_(vc_snprint)(msg + VG_(strlen)(msg),
1112 sizeof(msg) - VG_(strlen)(msg),
1113 &q->vc);
1114 VG_(message)(Vg_UserMsg, "%s", msg);
1115 }
1116 }
1117 }
1118 }
1119 }
1120 }
sewardjaf44c822007-11-25 14:01:38 +00001121
bartbedfd232009-03-26 19:07:15 +00001122 s_conflict_set_bitmap_creation_count
1123 += DRD_(bm_get_bitmap_creation_count)();
1124 s_conflict_set_bitmap2_creation_count
1125 += DRD_(bm_get_bitmap2_creation_count)();
1126
1127 if (0 && s_trace_conflict_set)
1128 {
1129 VG_(message)(Vg_UserMsg, "[%d] new conflict set:", tid);
1130 DRD_(bm_print)(*conflict_set);
1131 VG_(message)(Vg_UserMsg, "[%d] end of new conflict set.", tid);
1132 }
sewardjaf44c822007-11-25 14:01:38 +00001133}
1134
bart86a87df2009-03-04 19:26:47 +00001135/** Report the number of context switches performed. */
bart62a784c2009-02-15 13:11:14 +00001136ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001137{
bartbedfd232009-03-26 19:07:15 +00001138 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +00001139}
1140
bart86a87df2009-03-04 19:26:47 +00001141/** Report the number of ordered segments that have been discarded. */
bart62a784c2009-02-15 13:11:14 +00001142ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001143{
bartbedfd232009-03-26 19:07:15 +00001144 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +00001145}
1146
bart86a87df2009-03-04 19:26:47 +00001147/** Return how many times the conflict set has been updated. */
bart62a784c2009-02-15 13:11:14 +00001148ULong DRD_(thread_get_update_conflict_set_count)(ULong* dsnsc, ULong* dscvc)
sewardjaf44c822007-11-25 14:01:38 +00001149{
bartbedfd232009-03-26 19:07:15 +00001150 tl_assert(dsnsc);
1151 tl_assert(dscvc);
1152 *dsnsc = s_conflict_set_new_segment_count;
1153 *dscvc = s_conflict_set_combine_vc_count;
1154 return s_update_conflict_set_count;
sewardjaf44c822007-11-25 14:01:38 +00001155}
1156
bart86a87df2009-03-04 19:26:47 +00001157/**
1158 * Return the number of first-level bitmaps that have been created during
1159 * conflict set updates.
1160 */
bart62a784c2009-02-15 13:11:14 +00001161ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001162{
bartbedfd232009-03-26 19:07:15 +00001163 return s_conflict_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001164}
1165
bart86a87df2009-03-04 19:26:47 +00001166/**
1167 * Return the number of second-level bitmaps that have been created during
1168 * conflict set updates.
1169 */
bart62a784c2009-02-15 13:11:14 +00001170ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001171{
bartbedfd232009-03-26 19:07:15 +00001172 return s_conflict_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001173}