blob: 17cf5a761c064c28296f21a1f1c549bbb0d9a847 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 bart.vanassche@gmail.com
6
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"
27#include "drd_segment.h"
28#include "drd_suppression.h"
29#include "drd_thread.h"
sewardjaf44c822007-11-25 14:01:38 +000030#include "pub_tool_basics.h" // Addr, SizeT
31#include "pub_tool_errormgr.h" // VG_(unique_error)()
32#include "pub_tool_libcassert.h" // tl_assert()
33#include "pub_tool_libcbase.h" // VG_(strlen)()
34#include "pub_tool_libcprint.h" // VG_(printf)()
35#include "pub_tool_machine.h"
36#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardj85642922008-01-14 11:54:56 +000037#include "pub_tool_options.h" // VG_(clo_backtrace_size)
sewardjaf44c822007-11-25 14:01:38 +000038#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
39
40
sewardjaf44c822007-11-25 14:01:38 +000041// Local functions.
42
43static void thread_append_segment(const DrdThreadId tid,
44 Segment* const sg);
45static void thread_update_danger_set(const DrdThreadId tid);
46
47
48// Local variables.
49
50static ULong s_context_switch_count;
51static ULong s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +000052static ULong s_update_danger_set_count;
53static ULong s_danger_set_bitmap_creation_count;
54static ULong s_danger_set_bitmap2_creation_count;
sewardj8b09d4f2007-12-04 21:27:18 +000055static ThreadId s_vg_running_tid = VG_INVALID_THREADID;
bartf00a85b2008-03-13 18:49:23 +000056DrdThreadId s_drd_running_tid = DRD_INVALID_THREADID;
57ThreadInfo s_threadinfo[DRD_N_THREADS];
bart1a473c72008-03-13 19:03:38 +000058struct bitmap* s_danger_set;
bart26f73e12008-02-24 18:37:08 +000059static Bool s_trace_context_switches = False;
60static Bool s_trace_danger_set = False;
sewardjaf44c822007-11-25 14:01:38 +000061
62
63// Function definitions.
64
bart26f73e12008-02-24 18:37:08 +000065void thread_trace_context_switches(const Bool t)
66{
bart3772a982008-03-15 08:11:03 +000067 s_trace_context_switches = t;
bart26f73e12008-02-24 18:37:08 +000068}
69
70void thread_trace_danger_set(const Bool t)
71{
bart3772a982008-03-15 08:11:03 +000072 s_trace_danger_set = t;
bart26f73e12008-02-24 18:37:08 +000073}
74
sewardjaf44c822007-11-25 14:01:38 +000075__inline__ Bool IsValidDrdThreadId(const DrdThreadId tid)
76{
bart3772a982008-03-15 08:11:03 +000077 return (0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID
78 && ! (s_threadinfo[tid].vg_thread_exists == False
79 && s_threadinfo[tid].posix_thread_exists == False
80 && s_threadinfo[tid].detached_posix_thread == False));
sewardjaf44c822007-11-25 14:01:38 +000081}
82
83/**
84 * Convert Valgrind's ThreadId into a DrdThreadId. Report failure if
85 * Valgrind's ThreadId does not yet exist.
86 **/
87DrdThreadId VgThreadIdToDrdThreadId(const ThreadId tid)
88{
bart3772a982008-03-15 08:11:03 +000089 int i;
sewardjaf44c822007-11-25 14:01:38 +000090
bart3772a982008-03-15 08:11:03 +000091 if (tid == VG_INVALID_THREADID)
92 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +000093
bart3772a982008-03-15 08:11:03 +000094 for (i = 1; i < DRD_N_THREADS; i++)
95 {
96 if (s_threadinfo[i].vg_thread_exists == True
97 && s_threadinfo[i].vg_threadid == tid)
98 {
99 return i;
100 }
101 }
sewardjaf44c822007-11-25 14:01:38 +0000102
bart3772a982008-03-15 08:11:03 +0000103 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000104}
105
106static
107DrdThreadId VgThreadIdToNewDrdThreadId(const ThreadId tid)
108{
bart3772a982008-03-15 08:11:03 +0000109 int i;
sewardjaf44c822007-11-25 14:01:38 +0000110
bart3772a982008-03-15 08:11:03 +0000111 tl_assert(VgThreadIdToDrdThreadId(tid) == DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000112
bart3772a982008-03-15 08:11:03 +0000113 for (i = 1; i < DRD_N_THREADS; i++)
114 {
115 if (s_threadinfo[i].vg_thread_exists == False
116 && s_threadinfo[i].posix_thread_exists == False
117 && s_threadinfo[i].detached_posix_thread == False)
118 {
119 s_threadinfo[i].vg_thread_exists = True;
120 s_threadinfo[i].vg_threadid = tid;
121 s_threadinfo[i].pt_threadid = INVALID_POSIX_THREADID;
bart3772a982008-03-15 08:11:03 +0000122 s_threadinfo[i].stack_min = 0;
123 s_threadinfo[i].stack_startup = 0;
124 s_threadinfo[i].stack_max = 0;
bart3772a982008-03-15 08:11:03 +0000125 s_threadinfo[i].is_recording = True;
126 s_threadinfo[i].synchr_nesting = 0;
127 if (s_threadinfo[i].first != 0)
128 VG_(printf)("drd thread id = %d\n", i);
129 tl_assert(s_threadinfo[i].first == 0);
130 tl_assert(s_threadinfo[i].last == 0);
131 return i;
132 }
133 }
sewardjaf44c822007-11-25 14:01:38 +0000134
bart3772a982008-03-15 08:11:03 +0000135 tl_assert(False);
sewardjaf44c822007-11-25 14:01:38 +0000136
bart3772a982008-03-15 08:11:03 +0000137 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000138}
139
140DrdThreadId PtThreadIdToDrdThreadId(const PThreadId tid)
141{
bart3772a982008-03-15 08:11:03 +0000142 int i;
sewardjaf44c822007-11-25 14:01:38 +0000143
bart3772a982008-03-15 08:11:03 +0000144 tl_assert(tid != INVALID_POSIX_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000145
bart3772a982008-03-15 08:11:03 +0000146 for (i = 1; i < DRD_N_THREADS; i++)
147 {
148 if (s_threadinfo[i].posix_thread_exists
149 && s_threadinfo[i].pt_threadid == tid)
150 {
151 return i;
152 }
153 }
154 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000155}
156
157ThreadId DrdThreadIdToVgThreadId(const DrdThreadId tid)
158{
bart3772a982008-03-15 08:11:03 +0000159 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
160 return (s_threadinfo[tid].vg_thread_exists
161 ? s_threadinfo[tid].vg_threadid
162 : VG_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000163}
164
bart26f73e12008-02-24 18:37:08 +0000165/** Sanity check of the doubly linked list of segments referenced by a
166 * ThreadInfo struct.
167 * @return True if sane, False if not.
sewardjaf44c822007-11-25 14:01:38 +0000168 */
169static Bool sane_ThreadInfo(const ThreadInfo* const ti)
170{
bart3772a982008-03-15 08:11:03 +0000171 Segment* p;
172 for (p = ti->first; p; p = p->next) {
173 if (p->next && p->next->prev != p)
174 return False;
175 if (p->next == 0 && p != ti->last)
176 return False;
177 }
178 for (p = ti->last; p; p = p->prev) {
179 if (p->prev && p->prev->next != p)
180 return False;
181 if (p->prev == 0 && p != ti->first)
182 return False;
183 }
184 return True;
sewardjaf44c822007-11-25 14:01:38 +0000185}
186
187DrdThreadId thread_pre_create(const DrdThreadId creator,
188 const ThreadId vg_created)
189{
bart3772a982008-03-15 08:11:03 +0000190 DrdThreadId created;
sewardjaf44c822007-11-25 14:01:38 +0000191
bart3772a982008-03-15 08:11:03 +0000192 tl_assert(VgThreadIdToDrdThreadId(vg_created) == DRD_INVALID_THREADID);
193 created = VgThreadIdToNewDrdThreadId(vg_created);
194 tl_assert(0 <= created && created < DRD_N_THREADS
195 && created != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000196
bart3772a982008-03-15 08:11:03 +0000197 tl_assert(s_threadinfo[created].first == 0);
198 tl_assert(s_threadinfo[created].last == 0);
199 thread_append_segment(created, sg_new(creator, created));
sewardjaf44c822007-11-25 14:01:38 +0000200
bart3772a982008-03-15 08:11:03 +0000201 return created;
sewardjaf44c822007-11-25 14:01:38 +0000202}
203
bart26f73e12008-02-24 18:37:08 +0000204/** Allocate the first segment for a thread. Call this just after
205 * pthread_create().
sewardjaf44c822007-11-25 14:01:38 +0000206 */
207DrdThreadId thread_post_create(const ThreadId vg_created)
208{
bart3772a982008-03-15 08:11:03 +0000209 const DrdThreadId created = VgThreadIdToDrdThreadId(vg_created);
sewardjaf44c822007-11-25 14:01:38 +0000210
bart3772a982008-03-15 08:11:03 +0000211 tl_assert(0 <= created && created < DRD_N_THREADS
212 && created != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000213
bart3772a982008-03-15 08:11:03 +0000214 s_threadinfo[created].stack_max = VG_(thread_get_stack_max)(vg_created);
215 s_threadinfo[created].stack_startup = s_threadinfo[created].stack_max;
216 s_threadinfo[created].stack_min = s_threadinfo[created].stack_max;
bart3772a982008-03-15 08:11:03 +0000217 tl_assert(s_threadinfo[created].stack_max != 0);
sewardjaf44c822007-11-25 14:01:38 +0000218
bart3772a982008-03-15 08:11:03 +0000219 return created;
sewardjaf44c822007-11-25 14:01:38 +0000220}
221
222/* NPTL hack: NPTL allocates the 'struct pthread' on top of the stack, */
223/* and accesses this data structure from multiple threads without locking. */
224/* Any conflicting accesses in the range stack_startup..stack_max will be */
225/* ignored. */
226void thread_set_stack_startup(const DrdThreadId tid, const Addr stack_startup)
227{
bart3772a982008-03-15 08:11:03 +0000228 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
229 tl_assert(s_threadinfo[tid].stack_min <= stack_startup);
230 tl_assert(stack_startup <= s_threadinfo[tid].stack_max);
231 s_threadinfo[tid].stack_startup = stack_startup;
sewardjaf44c822007-11-25 14:01:38 +0000232}
233
234Addr thread_get_stack_min(const DrdThreadId tid)
235{
bart3772a982008-03-15 08:11:03 +0000236 tl_assert(0 <= tid && tid < DRD_N_THREADS
237 && tid != DRD_INVALID_THREADID);
238 return s_threadinfo[tid].stack_min;
sewardjaf44c822007-11-25 14:01:38 +0000239}
240
bartd43f8d32008-03-16 17:29:20 +0000241Addr thread_get_stack_max(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000242{
bartd43f8d32008-03-16 17:29:20 +0000243 tl_assert(0 <= tid && tid < DRD_N_THREADS
244 && tid != DRD_INVALID_THREADID);
245 return s_threadinfo[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000246}
247
248/**
249 * Clean up thread-specific data structures. Call this just after
250 * pthread_join().
251 */
252void thread_delete(const DrdThreadId tid)
253{
bart3772a982008-03-15 08:11:03 +0000254 Segment* sg;
255 Segment* sg_prev;
sewardjaf44c822007-11-25 14:01:38 +0000256
bart3772a982008-03-15 08:11:03 +0000257 tl_assert(0 <= tid && tid < DRD_N_THREADS
258 && tid != DRD_INVALID_THREADID);
259 tl_assert(s_threadinfo[tid].synchr_nesting == 0);
260 for (sg = s_threadinfo[tid].last; sg; sg = sg_prev)
261 {
262 sg_prev = sg->prev;
263 sg_delete(sg);
264 }
265 s_threadinfo[tid].vg_thread_exists = False;
266 s_threadinfo[tid].posix_thread_exists = False;
267 tl_assert(s_threadinfo[tid].detached_posix_thread == False);
268 s_threadinfo[tid].first = 0;
269 s_threadinfo[tid].last = 0;
sewardjaf44c822007-11-25 14:01:38 +0000270}
271
272/* Called after a thread performed its last memory access and before */
273/* thread_delete() is called. Note: thread_delete() is only called for */
274/* joinable threads, not for detached threads. */
275void thread_finished(const DrdThreadId tid)
276{
bart3772a982008-03-15 08:11:03 +0000277 tl_assert(0 <= tid && tid < DRD_N_THREADS
278 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000279
bart3772a982008-03-15 08:11:03 +0000280 s_threadinfo[tid].vg_thread_exists = False;
sewardjaf44c822007-11-25 14:01:38 +0000281
bart3772a982008-03-15 08:11:03 +0000282 if (s_threadinfo[tid].detached_posix_thread)
283 {
284 /* Once a detached thread has finished, its stack is deallocated and */
285 /* should no longer be taken into account when computing the danger set*/
286 s_threadinfo[tid].stack_min = s_threadinfo[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000287
bart3772a982008-03-15 08:11:03 +0000288 /* For a detached thread, calling pthread_exit() invalidates the */
289 /* POSIX thread ID associated with the detached thread. For joinable */
290 /* POSIX threads however, the POSIX thread ID remains live after the */
291 /* pthread_exit() call until pthread_join() is called. */
292 s_threadinfo[tid].posix_thread_exists = False;
293 }
sewardjaf44c822007-11-25 14:01:38 +0000294}
295
296void thread_set_pthreadid(const DrdThreadId tid, const PThreadId ptid)
297{
bart3772a982008-03-15 08:11:03 +0000298 tl_assert(0 <= tid && tid < DRD_N_THREADS
299 && tid != DRD_INVALID_THREADID);
300 tl_assert(s_threadinfo[tid].pt_threadid == INVALID_POSIX_THREADID);
301 tl_assert(ptid != INVALID_POSIX_THREADID);
302 s_threadinfo[tid].posix_thread_exists = True;
303 s_threadinfo[tid].pt_threadid = ptid;
sewardjaf44c822007-11-25 14:01:38 +0000304}
305
306Bool thread_get_joinable(const DrdThreadId tid)
307{
bart3772a982008-03-15 08:11:03 +0000308 tl_assert(0 <= tid && tid < DRD_N_THREADS
309 && tid != DRD_INVALID_THREADID);
310 return ! s_threadinfo[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000311}
312
313void thread_set_joinable(const DrdThreadId tid, const Bool joinable)
314{
bart3772a982008-03-15 08:11:03 +0000315 tl_assert(0 <= tid && tid < DRD_N_THREADS
316 && tid != DRD_INVALID_THREADID);
317 tl_assert(!! joinable == joinable);
318 tl_assert(s_threadinfo[tid].pt_threadid != INVALID_POSIX_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000319#if 0
bart3772a982008-03-15 08:11:03 +0000320 VG_(message)(Vg_DebugMsg,
321 "thread_set_joinable(%d/%d, %s)",
322 tid,
323 s_threadinfo[tid].vg_threadid,
324 joinable ? "joinable" : "detached");
sewardjaf44c822007-11-25 14:01:38 +0000325#endif
bart3772a982008-03-15 08:11:03 +0000326 s_threadinfo[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000327}
328
sewardj8b09d4f2007-12-04 21:27:18 +0000329void thread_set_vg_running_tid(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000330{
bart3772a982008-03-15 08:11:03 +0000331 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000332
bart3772a982008-03-15 08:11:03 +0000333 if (vg_tid != s_vg_running_tid)
334 {
335 thread_set_running_tid(vg_tid, VgThreadIdToDrdThreadId(vg_tid));
336 }
sewardj8b09d4f2007-12-04 21:27:18 +0000337
bart3772a982008-03-15 08:11:03 +0000338 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
339 tl_assert(s_drd_running_tid != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000340}
341
342void thread_set_running_tid(const ThreadId vg_tid, const DrdThreadId drd_tid)
343{
bart3772a982008-03-15 08:11:03 +0000344 tl_assert(vg_tid != VG_INVALID_THREADID);
345 tl_assert(drd_tid != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000346
bart3772a982008-03-15 08:11:03 +0000347 if (vg_tid != s_vg_running_tid)
348 {
349 if (s_trace_context_switches
350 && s_drd_running_tid != DRD_INVALID_THREADID)
351 {
352 VG_(message)(Vg_DebugMsg,
bartaa97a542008-03-16 17:57:01 +0000353 "Context switch from thread %d/%d to thread %d/%d",
354 s_vg_running_tid, s_drd_running_tid,
355 DrdThreadIdToVgThreadId(drd_tid), drd_tid);
bart3772a982008-03-15 08:11:03 +0000356 }
357 s_vg_running_tid = vg_tid;
358 s_drd_running_tid = drd_tid;
359 thread_update_danger_set(drd_tid);
360 s_context_switch_count++;
361 }
sewardj8b09d4f2007-12-04 21:27:18 +0000362
bart3772a982008-03-15 08:11:03 +0000363 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
364 tl_assert(s_drd_running_tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000365}
366
bart0268dfa2008-03-11 20:10:21 +0000367int thread_enter_synchr(const DrdThreadId tid)
368{
bart3772a982008-03-15 08:11:03 +0000369 tl_assert(IsValidDrdThreadId(tid));
370 return s_threadinfo[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000371}
372
373int thread_leave_synchr(const DrdThreadId tid)
374{
bart3772a982008-03-15 08:11:03 +0000375 tl_assert(IsValidDrdThreadId(tid));
376 tl_assert(s_threadinfo[tid].synchr_nesting >= 1);
377 return --s_threadinfo[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000378}
379
380int thread_get_synchr_nesting_count(const DrdThreadId tid)
381{
bart3772a982008-03-15 08:11:03 +0000382 tl_assert(IsValidDrdThreadId(tid));
383 return s_threadinfo[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000384}
385
bart1a473c72008-03-13 19:03:38 +0000386/** Append a new segment at the end of the segment list. */
bart26f73e12008-02-24 18:37:08 +0000387static void thread_append_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000388{
bart3772a982008-03-15 08:11:03 +0000389 tl_assert(0 <= tid && tid < DRD_N_THREADS
390 && tid != DRD_INVALID_THREADID);
391 tl_assert(sane_ThreadInfo(&s_threadinfo[tid]));
392 sg->prev = s_threadinfo[tid].last;
393 sg->next = 0;
394 if (s_threadinfo[tid].last)
395 s_threadinfo[tid].last->next = sg;
396 s_threadinfo[tid].last = sg;
397 if (s_threadinfo[tid].first == 0)
398 s_threadinfo[tid].first = sg;
399 tl_assert(sane_ThreadInfo(&s_threadinfo[tid]));
sewardjaf44c822007-11-25 14:01:38 +0000400}
401
bart26f73e12008-02-24 18:37:08 +0000402/** Remove a segment from the segment list of thread threadid, and free the
403 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000404 */
bart26f73e12008-02-24 18:37:08 +0000405static void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000406{
bart3772a982008-03-15 08:11:03 +0000407 tl_assert(0 <= tid && tid < DRD_N_THREADS
408 && tid != DRD_INVALID_THREADID);
409 tl_assert(sane_ThreadInfo(&s_threadinfo[tid]));
bart26f73e12008-02-24 18:37:08 +0000410
bart3772a982008-03-15 08:11:03 +0000411 if (sg->prev)
412 sg->prev->next = sg->next;
413 if (sg->next)
414 sg->next->prev = sg->prev;
415 if (sg == s_threadinfo[tid].first)
416 s_threadinfo[tid].first = sg->next;
417 if (sg == s_threadinfo[tid].last)
418 s_threadinfo[tid].last = sg->prev;
419 sg_delete(sg);
420 tl_assert(sane_ThreadInfo(&s_threadinfo[tid]));
sewardjaf44c822007-11-25 14:01:38 +0000421}
422
423VectorClock* thread_get_vc(const DrdThreadId tid)
424{
bart3772a982008-03-15 08:11:03 +0000425 tl_assert(0 <= tid && tid < DRD_N_THREADS
426 && tid != DRD_INVALID_THREADID);
427 tl_assert(s_threadinfo[tid].last);
428 return &s_threadinfo[tid].last->vc;
sewardjaf44c822007-11-25 14:01:38 +0000429}
430
431/**
432 * Compute the minimum of all latest vector clocks of all threads
433 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
434 * @param vc pointer to a vectorclock, holds result upon return.
435 */
436static void thread_compute_minimum_vc(VectorClock* vc)
437{
bart3772a982008-03-15 08:11:03 +0000438 unsigned i;
439 Bool first;
440 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000441
bart3772a982008-03-15 08:11:03 +0000442 first = True;
443 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
444 {
445 latest_sg = s_threadinfo[i].last;
446 if (latest_sg)
447 {
448 if (first)
449 vc_assign(vc, &latest_sg->vc);
450 else
451 vc_min(vc, &latest_sg->vc);
452 first = False;
453 }
454 }
sewardjaf44c822007-11-25 14:01:38 +0000455}
456
457static void thread_compute_maximum_vc(VectorClock* vc)
458{
bart3772a982008-03-15 08:11:03 +0000459 unsigned i;
460 Bool first;
461 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000462
bart3772a982008-03-15 08:11:03 +0000463 first = True;
464 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
465 {
466 latest_sg = s_threadinfo[i].last;
467 if (latest_sg)
468 {
469 if (first)
470 vc_assign(vc, &latest_sg->vc);
471 else
472 vc_combine(vc, &latest_sg->vc);
473 first = False;
474 }
475 }
sewardjaf44c822007-11-25 14:01:38 +0000476}
477
478/**
bart5bd9f2d2008-03-03 20:31:58 +0000479 * Discard all segments that have a defined order against the latest vector
sewardjaf44c822007-11-25 14:01:38 +0000480 * clock of every thread -- these segments can no longer be involved in a
481 * data race.
482 */
483static void thread_discard_ordered_segments(void)
484{
bart3772a982008-03-15 08:11:03 +0000485 unsigned i;
486 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000487
bart3772a982008-03-15 08:11:03 +0000488 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000489
bart3772a982008-03-15 08:11:03 +0000490 vc_init(&thread_vc_min, 0, 0);
491 thread_compute_minimum_vc(&thread_vc_min);
492 if (sg_get_trace())
493 {
494 char msg[256];
495 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000496
bart3772a982008-03-15 08:11:03 +0000497 vc_init(&thread_vc_max, 0, 0);
498 thread_compute_maximum_vc(&thread_vc_max);
499 VG_(snprintf)(msg, sizeof(msg),
500 "Discarding ordered segments -- min vc is ");
501 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
502 &thread_vc_min);
503 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
504 ", max vc is ");
505 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
506 &thread_vc_max);
507 VG_(message)(Vg_DebugMsg, "%s", msg);
508 vc_cleanup(&thread_vc_max);
509 }
sewardjaf44c822007-11-25 14:01:38 +0000510
bart3772a982008-03-15 08:11:03 +0000511 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
512 {
513 Segment* sg;
514 Segment* sg_next;
515 for (sg = s_threadinfo[i].first;
516 sg && (sg_next = sg->next) && vc_lte(&sg->vc, &thread_vc_min);
517 sg = sg_next)
518 {
519 thread_discard_segment(i, sg);
520 }
521 }
522 vc_cleanup(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +0000523}
524
525/**
526 * Create a new segment for the specified thread, and report all data races
527 * of the most recent thread segment with other threads.
528 */
529void thread_new_segment(const DrdThreadId tid)
530{
bart3772a982008-03-15 08:11:03 +0000531 Segment* sg;
sewardjaf44c822007-11-25 14:01:38 +0000532
bart3772a982008-03-15 08:11:03 +0000533 tl_assert(0 <= tid && tid < DRD_N_THREADS
534 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000535
bart3772a982008-03-15 08:11:03 +0000536 sg = sg_new(tid, tid);
537 thread_append_segment(tid, sg);
sewardjaf44c822007-11-25 14:01:38 +0000538
bart3772a982008-03-15 08:11:03 +0000539 thread_discard_ordered_segments();
bart26f73e12008-02-24 18:37:08 +0000540
bart3772a982008-03-15 08:11:03 +0000541 if (tid == s_drd_running_tid)
542 {
543 /* Every change in the vector clock of the current thread may cause */
544 /* segments that were previously ordered to this thread to become */
545 /* unordered. Hence, recalculate the danger set if the vector clock */
546 /* of the current thread is updated. */
547 thread_update_danger_set(tid);
548 }
sewardjaf44c822007-11-25 14:01:38 +0000549}
550
bart26f73e12008-02-24 18:37:08 +0000551/** Call this function after thread 'joiner' joined thread 'joinee'. */
sewardjaf44c822007-11-25 14:01:38 +0000552void thread_combine_vc(DrdThreadId joiner, DrdThreadId joinee)
553{
bart3772a982008-03-15 08:11:03 +0000554 tl_assert(joiner != joinee);
555 tl_assert(0 <= joiner && joiner < DRD_N_THREADS
556 && joiner != DRD_INVALID_THREADID);
557 tl_assert(0 <= joinee && joinee < DRD_N_THREADS
558 && joinee != DRD_INVALID_THREADID);
559 tl_assert(s_threadinfo[joiner].last);
560 tl_assert(s_threadinfo[joinee].last);
561 vc_combine(&s_threadinfo[joiner].last->vc, &s_threadinfo[joinee].last->vc);
562 thread_discard_ordered_segments();
sewardjaf44c822007-11-25 14:01:38 +0000563
bart3772a982008-03-15 08:11:03 +0000564 if (joiner == s_drd_running_tid)
565 {
566 thread_update_danger_set(joiner);
567 }
sewardjaf44c822007-11-25 14:01:38 +0000568}
569
bart26f73e12008-02-24 18:37:08 +0000570/** Call this function after thread 'tid' had to wait because of thread
571 * synchronization until the memory accesses in the segment with vector clock
572 * 'vc' finished.
573 */
sewardjaf44c822007-11-25 14:01:38 +0000574void thread_combine_vc2(DrdThreadId tid, const VectorClock* const vc)
575{
bart3772a982008-03-15 08:11:03 +0000576 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
577 tl_assert(s_threadinfo[tid].last);
578 tl_assert(vc);
579 vc_combine(&s_threadinfo[tid].last->vc, vc);
580 thread_discard_ordered_segments();
sewardjaf44c822007-11-25 14:01:38 +0000581}
582
bart26f73e12008-02-24 18:37:08 +0000583/** Call this function whenever a thread is no longer using the memory
584 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
585 * increase.
586 */
sewardjaf44c822007-11-25 14:01:38 +0000587void thread_stop_using_mem(const Addr a1, const Addr a2)
588{
bartd43f8d32008-03-16 17:29:20 +0000589 DrdThreadId other_user;
590 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +0000591
bart3772a982008-03-15 08:11:03 +0000592 /* For all threads, mark the range [ a1, a2 [ as no longer in use. */
bartd43f8d32008-03-16 17:29:20 +0000593 other_user = DRD_INVALID_THREADID;
bart3772a982008-03-15 08:11:03 +0000594 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
595 {
596 Segment* p;
597 for (p = s_threadinfo[i].first; p; p = p->next)
598 {
599 if (other_user == DRD_INVALID_THREADID
600 && i != s_drd_running_tid
601 && bm_has_any_access(p->bm, a1, a2))
sewardjaf44c822007-11-25 14:01:38 +0000602 {
bart3772a982008-03-15 08:11:03 +0000603 other_user = i;
sewardjaf44c822007-11-25 14:01:38 +0000604 }
bart3772a982008-03-15 08:11:03 +0000605 bm_clear(p->bm, a1, a2);
606 }
607 }
sewardjaf44c822007-11-25 14:01:38 +0000608
bart3772a982008-03-15 08:11:03 +0000609 /* If any other thread had accessed memory in [ a1, a2 [, update the */
610 /* danger set. */
611 if (other_user != DRD_INVALID_THREADID
612 && bm_has_any_access(s_danger_set, a1, a2))
613 {
614 thread_update_danger_set(thread_get_running_tid());
615 }
sewardjaf44c822007-11-25 14:01:38 +0000616}
617
bart0268dfa2008-03-11 20:10:21 +0000618void thread_start_recording(const DrdThreadId tid)
619{
bart3772a982008-03-15 08:11:03 +0000620 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
621 tl_assert(! s_threadinfo[tid].is_recording);
622 s_threadinfo[tid].is_recording = True;
bart0268dfa2008-03-11 20:10:21 +0000623}
624
625void thread_stop_recording(const DrdThreadId tid)
626{
bart3772a982008-03-15 08:11:03 +0000627 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
628 tl_assert(s_threadinfo[tid].is_recording);
629 s_threadinfo[tid].is_recording = False;
bart0268dfa2008-03-11 20:10:21 +0000630}
631
sewardjaf44c822007-11-25 14:01:38 +0000632void thread_print_all(void)
633{
bart3772a982008-03-15 08:11:03 +0000634 unsigned i;
635 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +0000636
bart3772a982008-03-15 08:11:03 +0000637 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
638 {
639 if (s_threadinfo[i].first)
640 {
641 VG_(printf)("**************\n"
bart354009c2008-03-16 10:42:33 +0000642 "* thread %3d (%d/%d/%d/0x%x/%d) *\n"
bart3772a982008-03-15 08:11:03 +0000643 "**************\n",
644 i,
645 s_threadinfo[i].vg_thread_exists,
646 s_threadinfo[i].vg_threadid,
647 s_threadinfo[i].posix_thread_exists,
648 s_threadinfo[i].pt_threadid,
bart354009c2008-03-16 10:42:33 +0000649 s_threadinfo[i].detached_posix_thread);
bart3772a982008-03-15 08:11:03 +0000650 for (p = s_threadinfo[i].first; p; p = p->next)
sewardjaf44c822007-11-25 14:01:38 +0000651 {
bart3772a982008-03-15 08:11:03 +0000652 sg_print(p);
sewardjaf44c822007-11-25 14:01:38 +0000653 }
bart3772a982008-03-15 08:11:03 +0000654 }
655 }
sewardjaf44c822007-11-25 14:01:38 +0000656}
657
658static void show_call_stack(const DrdThreadId tid,
659 const Char* const msg,
660 ExeContext* const callstack)
661{
bart3772a982008-03-15 08:11:03 +0000662 const ThreadId vg_tid = DrdThreadIdToVgThreadId(tid);
sewardjaf44c822007-11-25 14:01:38 +0000663
bartaa97a542008-03-16 17:57:01 +0000664 VG_(message)(Vg_UserMsg, "%s (thread %d/%d)", msg, vg_tid, tid);
sewardjaf44c822007-11-25 14:01:38 +0000665
bart3772a982008-03-15 08:11:03 +0000666 if (vg_tid != VG_INVALID_THREADID)
667 {
668 if (callstack)
669 {
670 VG_(pp_ExeContext)(callstack);
671 }
672 else
673 {
674 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
675 }
676 }
677 else
678 {
679 VG_(message)(Vg_UserMsg,
680 " (thread finished, call stack no longer available)");
681 }
sewardjaf44c822007-11-25 14:01:38 +0000682}
683
sewardjaf44c822007-11-25 14:01:38 +0000684static void
685thread_report_conflicting_segments_segment(const DrdThreadId tid,
686 const Addr addr,
687 const SizeT size,
688 const BmAccessTypeT access_type,
689 const Segment* const p)
690{
bart3772a982008-03-15 08:11:03 +0000691 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +0000692
bart3772a982008-03-15 08:11:03 +0000693 tl_assert(0 <= tid && tid < DRD_N_THREADS
694 && tid != DRD_INVALID_THREADID);
695 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +0000696
bart3772a982008-03-15 08:11:03 +0000697 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
698 {
699 if (i != tid)
700 {
701 Segment* q;
702 for (q = s_threadinfo[i].last; q; q = q->prev)
sewardjaf44c822007-11-25 14:01:38 +0000703 {
bart3772a982008-03-15 08:11:03 +0000704 // Since q iterates over the segments of thread i in order of
705 // decreasing vector clocks, if q->vc <= p->vc, then
706 // q->next->vc <= p->vc will also hold. Hence, break out of the
707 // loop once this condition is met.
708 if (vc_lte(&q->vc, &p->vc))
709 break;
710 if (! vc_lte(&p->vc, &q->vc))
711 {
712 if (bm_has_conflict_with(q->bm, addr, addr + size, access_type))
713 {
714 tl_assert(q->stacktrace);
715 show_call_stack(i, "Other segment start",
716 q->stacktrace);
717 show_call_stack(i, "Other segment end",
718 q->next ? q->next->stacktrace : 0);
719 }
720 }
sewardjaf44c822007-11-25 14:01:38 +0000721 }
bart3772a982008-03-15 08:11:03 +0000722 }
723 }
sewardjaf44c822007-11-25 14:01:38 +0000724}
725
726void thread_report_conflicting_segments(const DrdThreadId tid,
727 const Addr addr,
728 const SizeT size,
729 const BmAccessTypeT access_type)
730{
bart3772a982008-03-15 08:11:03 +0000731 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +0000732
bart3772a982008-03-15 08:11:03 +0000733 tl_assert(0 <= tid && tid < DRD_N_THREADS
734 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000735
bart3772a982008-03-15 08:11:03 +0000736 for (p = s_threadinfo[tid].first; p; p = p->next)
737 {
738 if (bm_has(p->bm, addr, addr + size, access_type))
739 {
740 thread_report_conflicting_segments_segment(tid, addr, size,
741 access_type, p);
742 }
743 }
sewardjaf44c822007-11-25 14:01:38 +0000744}
sewardjaf44c822007-11-25 14:01:38 +0000745
bart26f73e12008-02-24 18:37:08 +0000746/** Compute a bitmap that represents the union of all memory accesses of all
747 * segments that are unordered to the current segment of the thread tid.
sewardjaf44c822007-11-25 14:01:38 +0000748 */
749static void thread_update_danger_set(const DrdThreadId tid)
750{
bart3772a982008-03-15 08:11:03 +0000751 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +0000752
bart3772a982008-03-15 08:11:03 +0000753 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
754 tl_assert(tid == s_drd_running_tid);
sewardjaf44c822007-11-25 14:01:38 +0000755
bart3772a982008-03-15 08:11:03 +0000756 s_update_danger_set_count++;
757 s_danger_set_bitmap_creation_count -= bm_get_bitmap_creation_count();
758 s_danger_set_bitmap2_creation_count -= bm_get_bitmap2_creation_count();
sewardjaf44c822007-11-25 14:01:38 +0000759
bart3772a982008-03-15 08:11:03 +0000760 if (s_danger_set)
761 {
bart1ea5fff2008-03-16 08:36:23 +0000762 bm_delete(s_danger_set);
bart3772a982008-03-15 08:11:03 +0000763 }
bart1ea5fff2008-03-16 08:36:23 +0000764 s_danger_set = bm_new();
bart26f73e12008-02-24 18:37:08 +0000765
bart3772a982008-03-15 08:11:03 +0000766 if (s_trace_danger_set)
767 {
768 char msg[256];
769
770 VG_(snprintf)(msg, sizeof(msg),
bartaa97a542008-03-16 17:57:01 +0000771 "computing danger set for thread %d/%d with vc ",
772 DrdThreadIdToVgThreadId(tid), tid);
bart3772a982008-03-15 08:11:03 +0000773 vc_snprint(msg + VG_(strlen)(msg),
774 sizeof(msg) - VG_(strlen)(msg),
775 &s_threadinfo[tid].last->vc);
776 VG_(message)(Vg_DebugMsg, "%s", msg);
777 }
778
779 p = s_threadinfo[tid].last;
780 {
781 unsigned j;
782
783 if (s_trace_danger_set)
784 {
bart26f73e12008-02-24 18:37:08 +0000785 char msg[256];
786
787 VG_(snprintf)(msg, sizeof(msg),
bart3772a982008-03-15 08:11:03 +0000788 "danger set: thread [%d] at vc ",
bart26f73e12008-02-24 18:37:08 +0000789 tid);
790 vc_snprint(msg + VG_(strlen)(msg),
791 sizeof(msg) - VG_(strlen)(msg),
bart3772a982008-03-15 08:11:03 +0000792 &p->vc);
bart26f73e12008-02-24 18:37:08 +0000793 VG_(message)(Vg_DebugMsg, "%s", msg);
bart3772a982008-03-15 08:11:03 +0000794 }
sewardjaf44c822007-11-25 14:01:38 +0000795
bart3772a982008-03-15 08:11:03 +0000796 for (j = 0; j < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); j++)
797 {
798 if (IsValidDrdThreadId(j))
bart26f73e12008-02-24 18:37:08 +0000799 {
bart3772a982008-03-15 08:11:03 +0000800 const Segment* q;
801 for (q = s_threadinfo[j].last; q; q = q->prev)
802 if (j != tid && q != 0
803 && ! vc_lte(&q->vc, &p->vc) && ! vc_lte(&p->vc, &q->vc))
804 {
805 if (s_trace_danger_set)
806 {
807 char msg[256];
808 VG_(snprintf)(msg, sizeof(msg),
809 "danger set: [%d] merging segment ", j);
810 vc_snprint(msg + VG_(strlen)(msg),
811 sizeof(msg) - VG_(strlen)(msg),
812 &q->vc);
813 VG_(message)(Vg_DebugMsg, "%s", msg);
814 }
815 bm_merge2(s_danger_set, q->bm);
816 }
817 else
818 {
819 if (s_trace_danger_set)
820 {
821 char msg[256];
822 VG_(snprintf)(msg, sizeof(msg),
823 "danger set: [%d] ignoring segment ", j);
824 vc_snprint(msg + VG_(strlen)(msg),
825 sizeof(msg) - VG_(strlen)(msg),
826 &q->vc);
827 VG_(message)(Vg_DebugMsg, "%s", msg);
828 }
829 }
bart26f73e12008-02-24 18:37:08 +0000830 }
bart3772a982008-03-15 08:11:03 +0000831 }
bart26f73e12008-02-24 18:37:08 +0000832
bart3772a982008-03-15 08:11:03 +0000833 for (j = 0; j < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); j++)
834 {
835 if (IsValidDrdThreadId(j))
sewardjaf44c822007-11-25 14:01:38 +0000836 {
bart3772a982008-03-15 08:11:03 +0000837 // NPTL hack: don't report data races on sizeof(struct pthread)
838 // bytes at the top of the stack, since the NPTL functions access
839 // this data without locking.
840 if (s_threadinfo[j].stack_min != 0)
841 {
842 tl_assert(s_threadinfo[j].stack_startup != 0);
843 if (s_threadinfo[j].stack_min < s_threadinfo[j].stack_startup)
844 {
845 bm_clear(s_danger_set,
846 s_threadinfo[j].stack_min,
847 s_threadinfo[j].stack_startup);
848 }
849 }
sewardjaf44c822007-11-25 14:01:38 +0000850 }
bart3772a982008-03-15 08:11:03 +0000851 }
852 }
sewardjaf44c822007-11-25 14:01:38 +0000853
bart3772a982008-03-15 08:11:03 +0000854 s_danger_set_bitmap_creation_count += bm_get_bitmap_creation_count();
855 s_danger_set_bitmap2_creation_count += bm_get_bitmap2_creation_count();
sewardjaf44c822007-11-25 14:01:38 +0000856
bart3772a982008-03-15 08:11:03 +0000857 if (0 && s_trace_danger_set)
858 {
859 VG_(message)(Vg_DebugMsg, "[%d] new danger set:", tid);
860 bm_print(s_danger_set);
861 VG_(message)(Vg_DebugMsg, "[%d] end of new danger set.", tid);
862 }
sewardjaf44c822007-11-25 14:01:38 +0000863}
864
sewardjaf44c822007-11-25 14:01:38 +0000865ULong thread_get_context_switch_count(void)
866{
bart3772a982008-03-15 08:11:03 +0000867 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +0000868}
869
sewardjaf44c822007-11-25 14:01:38 +0000870ULong thread_get_discard_ordered_segments_count(void)
871{
bart3772a982008-03-15 08:11:03 +0000872 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +0000873}
874
875ULong thread_get_update_danger_set_count(void)
876{
bart3772a982008-03-15 08:11:03 +0000877 return s_update_danger_set_count;
sewardjaf44c822007-11-25 14:01:38 +0000878}
879
880ULong thread_get_danger_set_bitmap_creation_count(void)
881{
bart3772a982008-03-15 08:11:03 +0000882 return s_danger_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +0000883}
884
885ULong thread_get_danger_set_bitmap2_creation_count(void)
886{
bart3772a982008-03-15 08:11:03 +0000887 return s_danger_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +0000888}