blob: 72a92dc02bb40576d24294fcbb09417603684edd [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
4 Copyright (C) 2006-2007 Bart Van Assche
5 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"
30#include "pthread_object_size.h"
31#include "pub_core_options.h" // VG_(clo_backtrace_size)
32#include "pub_tool_basics.h" // Addr, SizeT
33#include "pub_tool_errormgr.h" // VG_(unique_error)()
34#include "pub_tool_libcassert.h" // tl_assert()
35#include "pub_tool_libcbase.h" // VG_(strlen)()
36#include "pub_tool_libcprint.h" // VG_(printf)()
37#include "pub_tool_machine.h"
38#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
39#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
40
41
42// Defines.
43
44#define DRD_N_THREADS VG_N_THREADS
45
46
47// Type definitions.
48
49typedef struct
50{
51 Segment* first;
52 Segment* last;
53 ThreadId vg_threadid;
54 PThreadId pt_threadid;
55 Addr stack_min_min;
56 Addr stack_min;
57 Addr stack_startup;
58 Addr stack_max;
59 char name[32];
60 /// Indicates whether the Valgrind core knows about this thread.
61 Bool vg_thread_exists;
62 /// Indicates whether there is an associated POSIX thread ID.
63 Bool posix_thread_exists;
64 /// If true, indicates that there is a corresponding POSIX thread ID and
65 /// a corresponding OS thread that is detached.
66 Bool detached_posix_thread;
67 Bool is_recording;
68} ThreadInfo;
69
70
71// Local functions.
72
73static void thread_append_segment(const DrdThreadId tid,
74 Segment* const sg);
75static void thread_update_danger_set(const DrdThreadId tid);
76
77
78// Local variables.
79
80static ULong s_context_switch_count;
81static ULong s_discard_ordered_segments_count;
82#ifdef OLD_RACE_DETECTION_ALGORITHM
83static ULong s_report_races_count;
84#endif
85static ULong s_update_danger_set_count;
86static ULong s_danger_set_bitmap_creation_count;
87static ULong s_danger_set_bitmap2_creation_count;
88static DrdThreadId s_running_tid = DRD_INVALID_THREADID;
89static ThreadInfo s_threadinfo[DRD_N_THREADS];
90static struct bitmap* s_danger_set;
91
92
93// Function definitions.
94
95__inline__ Bool IsValidDrdThreadId(const DrdThreadId tid)
96{
97 return (0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID
98 && ! (s_threadinfo[tid].vg_thread_exists == False
99 && s_threadinfo[tid].posix_thread_exists == False
100 && s_threadinfo[tid].detached_posix_thread == False));
101}
102
103/**
104 * Convert Valgrind's ThreadId into a DrdThreadId. Report failure if
105 * Valgrind's ThreadId does not yet exist.
106 **/
107DrdThreadId VgThreadIdToDrdThreadId(const ThreadId tid)
108{
109 int i;
110
111 if (tid == VG_INVALID_THREADID)
112 return DRD_INVALID_THREADID;
113
114 for (i = 1; i < DRD_N_THREADS; i++)
115 {
116 if (s_threadinfo[i].vg_thread_exists == True
117 && s_threadinfo[i].vg_threadid == tid)
118 {
119 return i;
120 }
121 }
122
123 return DRD_INVALID_THREADID;
124}
125
126static
127DrdThreadId VgThreadIdToNewDrdThreadId(const ThreadId tid)
128{
129 int i;
130
131 tl_assert(VgThreadIdToDrdThreadId(tid) == DRD_INVALID_THREADID);
132
133 for (i = 1; i < DRD_N_THREADS; i++)
134 {
135 if (s_threadinfo[i].vg_thread_exists == False
136 && s_threadinfo[i].posix_thread_exists == False
137 && s_threadinfo[i].detached_posix_thread == False)
138 {
139 s_threadinfo[i].vg_thread_exists = True;
140 s_threadinfo[i].vg_threadid = tid;
141 s_threadinfo[i].pt_threadid = INVALID_POSIX_THREADID;
142 s_threadinfo[i].stack_min_min = 0;
143 s_threadinfo[i].stack_min = 0;
144 s_threadinfo[i].stack_startup = 0;
145 s_threadinfo[i].stack_max = 0;
146 VG_(snprintf)(s_threadinfo[i].name, sizeof(s_threadinfo[i].name),
147 "thread %d", tid);
148 s_threadinfo[i].name[sizeof(s_threadinfo[i].name) - 1] = 0;
149 s_threadinfo[i].is_recording = True;
150 if (s_threadinfo[i].first != 0)
151 VG_(printf)("drd thread id = %d\n", i);
152 tl_assert(s_threadinfo[i].first == 0);
153 tl_assert(s_threadinfo[i].last == 0);
154 return i;
155 }
156 }
157
158 tl_assert(False);
159
160 return DRD_INVALID_THREADID;
161}
162
163DrdThreadId PtThreadIdToDrdThreadId(const PThreadId tid)
164{
165 int i;
166
167 tl_assert(tid != INVALID_POSIX_THREADID);
168
169 for (i = 1; i < DRD_N_THREADS; i++)
170 {
171 if (s_threadinfo[i].posix_thread_exists
172 && s_threadinfo[i].pt_threadid == tid)
173 {
174 return i;
175 }
176 }
177 return DRD_INVALID_THREADID;
178}
179
180ThreadId DrdThreadIdToVgThreadId(const DrdThreadId tid)
181{
182 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
183 return (s_threadinfo[tid].vg_thread_exists
184 ? s_threadinfo[tid].vg_threadid
185 : VG_INVALID_THREADID);
186}
187
188/**
189 * Sanity check of the doubly linked list of segments referenced by a ThreadInfo struct.
190 * @return True if sane, False if not.
191 */
192static Bool sane_ThreadInfo(const ThreadInfo* const ti)
193{
194 Segment* p;
195 for (p = ti->first; p; p = p->next) {
196 if (p->next && p->next->prev != p)
197 return False;
198 if (p->next == 0 && p != ti->last)
199 return False;
200 }
201 for (p = ti->last; p; p = p->prev) {
202 if (p->prev && p->prev->next != p)
203 return False;
204 if (p->prev == 0 && p != ti->first)
205 return False;
206 }
207 return True;
208}
209
210DrdThreadId thread_pre_create(const DrdThreadId creator,
211 const ThreadId vg_created)
212{
213 DrdThreadId created;
214
215 tl_assert(VgThreadIdToDrdThreadId(vg_created) == DRD_INVALID_THREADID);
216 created = VgThreadIdToNewDrdThreadId(vg_created);
217 tl_assert(0 <= created && created < DRD_N_THREADS
218 && created != DRD_INVALID_THREADID);
219
220 tl_assert(s_threadinfo[created].first == 0);
221 tl_assert(s_threadinfo[created].last == 0);
222 thread_append_segment(created, sg_new(creator, created));
223
224 return created;
225}
226
227/**
228 * Allocate the first segment for a thread. Call this just after
229 * pthread_create().
230 */
231DrdThreadId thread_post_create(const ThreadId vg_created)
232{
233 const DrdThreadId created = VgThreadIdToDrdThreadId(vg_created);
234
235 tl_assert(0 <= created && created < DRD_N_THREADS
236 && created != DRD_INVALID_THREADID);
237
238 s_threadinfo[created].stack_max = VG_(thread_get_stack_max)(vg_created);
239 s_threadinfo[created].stack_startup = s_threadinfo[created].stack_max;
240 s_threadinfo[created].stack_min = s_threadinfo[created].stack_max;
241 s_threadinfo[created].stack_min_min = s_threadinfo[created].stack_max;
242 tl_assert(s_threadinfo[created].stack_max != 0);
243
244 return created;
245}
246
247/* NPTL hack: NPTL allocates the 'struct pthread' on top of the stack, */
248/* and accesses this data structure from multiple threads without locking. */
249/* Any conflicting accesses in the range stack_startup..stack_max will be */
250/* ignored. */
251void thread_set_stack_startup(const DrdThreadId tid, const Addr stack_startup)
252{
253#if 0
254 VG_(message)(Vg_DebugMsg, "thread_set_stack_startup: thread %d (%d)"
255 " stack 0x%x .. 0x%lx (size %d)",
256 s_threadinfo[tid].vg_threadid, tid,
257 stack_startup,
258 s_threadinfo[tid].stack_max,
259 s_threadinfo[tid].stack_max - stack_startup);
260#endif
261 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
262 tl_assert(s_threadinfo[tid].stack_min <= stack_startup);
263 tl_assert(stack_startup <= s_threadinfo[tid].stack_max);
264 s_threadinfo[tid].stack_startup = stack_startup;
265}
266
267Addr thread_get_stack_min(const DrdThreadId tid)
268{
269 tl_assert(0 <= tid && tid < DRD_N_THREADS
270 && tid != DRD_INVALID_THREADID);
271 return s_threadinfo[tid].stack_min;
272}
273
274void thread_set_stack_min(const DrdThreadId tid, const Addr stack_min)
275{
276#if 0
277 VG_(message)(Vg_DebugMsg, "thread %d (%d) stack_min = 0x%x"
278 " (size %d, max %d, delta %d)",
279 s_threadinfo[tid].vg_threadid, tid,
280 stack_min,
281 s_threadinfo[tid].stack_max - stack_min,
282 s_threadinfo[tid].stack_max - s_threadinfo[tid].stack_min_min,
283 s_threadinfo[tid].stack_min - stack_min);
284#endif
285 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
286 if (s_threadinfo[tid].stack_max)
287 {
288 s_threadinfo[tid].stack_min = stack_min;
289 if (stack_min < s_threadinfo[tid].stack_min_min)
290 {
291 s_threadinfo[tid].stack_min_min = stack_min;
292 }
293 tl_assert(s_threadinfo[tid].stack_min_min
294 <= s_threadinfo[tid].stack_min);
295 tl_assert(s_threadinfo[tid].stack_min < s_threadinfo[tid].stack_max);
296 }
297}
298
299DrdThreadId thread_lookup_stackaddr(const Addr a,
300 Addr* const stack_min,
301 Addr* const stack_max)
302{
303 unsigned i;
304 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
305 {
306 if (s_threadinfo[i].stack_min <= a && a <= s_threadinfo[i].stack_max)
307 {
308 *stack_min = s_threadinfo[i].stack_min;
309 *stack_max = s_threadinfo[i].stack_max;
310 return i;
311 }
312 }
313 return DRD_INVALID_THREADID;
314}
315
316/**
317 * Clean up thread-specific data structures. Call this just after
318 * pthread_join().
319 */
320void thread_delete(const DrdThreadId tid)
321{
322 Segment* sg;
323 Segment* sg_prev;
324
325 tl_assert(0 <= tid && tid < DRD_N_THREADS
326 && tid != DRD_INVALID_THREADID);
327 for (sg = s_threadinfo[tid].last; sg; sg = sg_prev)
328 {
329 sg_prev = sg->prev;
330 sg_delete(sg);
331 }
332 s_threadinfo[tid].vg_thread_exists = False;
333 s_threadinfo[tid].posix_thread_exists = False;
334 tl_assert(s_threadinfo[tid].detached_posix_thread == False);
335 s_threadinfo[tid].first = 0;
336 s_threadinfo[tid].last = 0;
337}
338
339/* Called after a thread performed its last memory access and before */
340/* thread_delete() is called. Note: thread_delete() is only called for */
341/* joinable threads, not for detached threads. */
342void thread_finished(const DrdThreadId tid)
343{
344 tl_assert(0 <= tid && tid < DRD_N_THREADS
345 && tid != DRD_INVALID_THREADID);
346
347 thread_stop_using_mem(s_threadinfo[tid].stack_min,
348 s_threadinfo[tid].stack_max);
349
350 s_threadinfo[tid].vg_thread_exists = False;
351
352 if (s_threadinfo[tid].detached_posix_thread)
353 {
354 /* Once a detached thread has finished, its stack is deallocated and */
355 /* should no longer be taken into account when computing the danger set*/
356 s_threadinfo[tid].stack_min = s_threadinfo[tid].stack_max;
357
358 /* For a detached thread, calling pthread_exit() invalidates the */
359 /* POSIX thread ID associated with the detached thread. For joinable */
360 /* POSIX threads however, the POSIX thread ID remains live after the */
361 /* pthread_exit() call until pthread_join() is called. */
362 s_threadinfo[tid].posix_thread_exists = False;
363 }
364}
365
366void thread_set_pthreadid(const DrdThreadId tid, const PThreadId ptid)
367{
368 tl_assert(0 <= tid && tid < DRD_N_THREADS
369 && tid != DRD_INVALID_THREADID);
370 tl_assert(s_threadinfo[tid].pt_threadid == INVALID_POSIX_THREADID);
371 tl_assert(ptid != INVALID_POSIX_THREADID);
372 s_threadinfo[tid].posix_thread_exists = True;
373 s_threadinfo[tid].pt_threadid = ptid;
374}
375
376Bool thread_get_joinable(const DrdThreadId tid)
377{
378 tl_assert(0 <= tid && tid < DRD_N_THREADS
379 && tid != DRD_INVALID_THREADID);
380 return ! s_threadinfo[tid].detached_posix_thread;
381}
382
383void thread_set_joinable(const DrdThreadId tid, const Bool joinable)
384{
385 tl_assert(0 <= tid && tid < DRD_N_THREADS
386 && tid != DRD_INVALID_THREADID);
387 tl_assert(!! joinable == joinable);
388 tl_assert(s_threadinfo[tid].pt_threadid != INVALID_POSIX_THREADID);
389#if 0
390 VG_(message)(Vg_DebugMsg,
391 "thread_set_joinable(%d/%d, %s)",
392 tid,
393 s_threadinfo[tid].vg_threadid,
394 joinable ? "joinable" : "detached");
395#endif
396 s_threadinfo[tid].detached_posix_thread = ! joinable;
397}
398
399const char* thread_get_name(const DrdThreadId tid)
400{
401 tl_assert(0 <= tid && tid < DRD_N_THREADS
402 && tid != DRD_INVALID_THREADID);
403 return s_threadinfo[tid].name;
404}
405
406void thread_set_name(const DrdThreadId tid, const char* const name)
407{
408 tl_assert(0 <= tid && tid < DRD_N_THREADS
409 && tid != DRD_INVALID_THREADID);
410 VG_(strncpy)(s_threadinfo[tid].name, name,
411 sizeof(s_threadinfo[tid].name));
412 s_threadinfo[tid].name[sizeof(s_threadinfo[tid].name) - 1] = 0;
413}
414
415void thread_set_name_fmt(const DrdThreadId tid, const char* const fmt,
416 const UWord arg)
417{
418 tl_assert(0 <= tid && tid < DRD_N_THREADS
419 && tid != DRD_INVALID_THREADID);
420 VG_(snprintf)(s_threadinfo[tid].name, sizeof(s_threadinfo[tid].name),
421 fmt, arg);
422 s_threadinfo[tid].name[sizeof(s_threadinfo[tid].name) - 1] = 0;
423}
424DrdThreadId thread_get_running_tid(void)
425{
426 tl_assert(s_running_tid != DRD_INVALID_THREADID);
427 return s_running_tid;
428}
429
430void thread_set_running_tid(const DrdThreadId tid)
431{
432 s_running_tid = tid;
433 thread_update_danger_set(tid);
434 s_context_switch_count++;
435}
436
437/**
438 * Return a pointer to the latest segment for the specified thread.
439 */
440Segment* thread_get_segment(const DrdThreadId tid)
441{
442 tl_assert(0 <= tid && tid < DRD_N_THREADS
443 && tid != DRD_INVALID_THREADID);
444 if (s_threadinfo[tid].last == 0)
445 {
446 VG_(message)(Vg_DebugMsg, "threadid = %d", tid);
447 thread_print_all();
448 }
449 tl_assert(s_threadinfo[tid].last);
450 return s_threadinfo[tid].last;
451}
452
453/**
454 * Insert a new segment at the end of the segment list.
455 */
456static void thread_append_segment(const DrdThreadId tid,
457 Segment* const sg)
458{
459 tl_assert(0 <= tid && tid < DRD_N_THREADS
460 && tid != DRD_INVALID_THREADID);
461 tl_assert(sane_ThreadInfo(&s_threadinfo[tid]));
462 sg->prev = s_threadinfo[tid].last;
463 sg->next = 0;
464 if (s_threadinfo[tid].last)
465 s_threadinfo[tid].last->next = sg;
466 s_threadinfo[tid].last = sg;
467 if (s_threadinfo[tid].first == 0)
468 s_threadinfo[tid].first = sg;
469 tl_assert(sane_ThreadInfo(&s_threadinfo[tid]));
470}
471
472/**
473 * Remove a segment from the segment list of thread threadid, and free the
474 * associated memory.
475 */
476static void thread_discard_segment(const DrdThreadId tid,
477 Segment* const sg)
478{
479 tl_assert(0 <= tid && tid < DRD_N_THREADS
480 && tid != DRD_INVALID_THREADID);
481 tl_assert(sane_ThreadInfo(&s_threadinfo[tid]));
482 if (sg->prev)
483 sg->prev->next = sg->next;
484 if (sg->next)
485 sg->next->prev = sg->prev;
486 if (sg == s_threadinfo[tid].first)
487 s_threadinfo[tid].first = sg->next;
488 if (sg == s_threadinfo[tid].last)
489 s_threadinfo[tid].last = sg->prev;
490 sg_delete(sg);
491 tl_assert(sane_ThreadInfo(&s_threadinfo[tid]));
492}
493
494VectorClock* thread_get_vc(const DrdThreadId tid)
495{
496 tl_assert(0 <= tid && tid < DRD_N_THREADS
497 && tid != DRD_INVALID_THREADID);
498 tl_assert(s_threadinfo[tid].last);
499 return &s_threadinfo[tid].last->vc;
500}
501
502/**
503 * Compute the minimum of all latest vector clocks of all threads
504 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
505 * @param vc pointer to a vectorclock, holds result upon return.
506 */
507static void thread_compute_minimum_vc(VectorClock* vc)
508{
509 int i;
510 Bool first;
511 Segment* latest_sg;
512
513 first = True;
514 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
515 {
516 latest_sg = s_threadinfo[i].last;
517 if (latest_sg)
518 {
519 if (first)
520 {
521 vc_cleanup(vc);
522 vc_copy(vc, &latest_sg->vc);
523 }
524 else
525 vc_min(vc, &latest_sg->vc);
526 first = False;
527 }
528 }
529}
530
531static void thread_compute_maximum_vc(VectorClock* vc)
532{
533 int i;
534 Bool first;
535 Segment* latest_sg;
536
537 first = True;
538 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
539 {
540 latest_sg = s_threadinfo[i].last;
541 if (latest_sg)
542 {
543 if (first)
544 {
545 vc_cleanup(vc);
546 vc_copy(vc, &latest_sg->vc);
547 }
548 else
549 vc_combine(vc, &latest_sg->vc);
550 first = False;
551 }
552 }
553}
554
555/**
556 * Discard all segments that have a defined ordered against the latest vector
557 * clock of every thread -- these segments can no longer be involved in a
558 * data race.
559 */
560static void thread_discard_ordered_segments(void)
561{
562 VectorClock thread_vc_min;
563 int i;
564
565 s_discard_ordered_segments_count++;
566
567 vc_init(&thread_vc_min, 0, 0);
568 thread_compute_minimum_vc(&thread_vc_min);
569 if (sg_get_trace())
570 {
571 char msg[256];
572 VectorClock thread_vc_max;
573
574 vc_init(&thread_vc_max, 0, 0);
575 thread_compute_maximum_vc(&thread_vc_max);
576 VG_(snprintf)(msg, sizeof(msg),
577 "Discarding ordered segments -- min vc is ");
578 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
579 &thread_vc_min);
580 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
581 ", max vc is ");
582 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
583 &thread_vc_max);
584 VG_(message)(Vg_DebugMsg, "%s", msg);
585 vc_cleanup(&thread_vc_max);
586 }
587
588 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
589 {
590 Segment* sg;
591 Segment* sg_next;
592 for (sg = s_threadinfo[i].first;
593 sg && (sg_next = sg->next) && vc_lte(&sg->vc, &thread_vc_min);
594 sg = sg_next)
595 {
596#if 0
597 VG_(printf)("Discarding a segment of thread %d: ", i);
598 vc_print(&sg->vc);
599 VG_(printf)("\n");
600#endif
601 thread_discard_segment(i, sg);
602 }
603 }
604 vc_cleanup(&thread_vc_min);
605}
606
607/**
608 * Create a new segment for the specified thread, and report all data races
609 * of the most recent thread segment with other threads.
610 */
611void thread_new_segment(const DrdThreadId tid)
612{
613 //static int s_calls_since_last_discard = 0;
614 Segment* sg;
615
616 tl_assert(0 <= tid && tid < DRD_N_THREADS
617 && tid != DRD_INVALID_THREADID);
618
619#ifdef OLD_RACE_DETECTION_ALGORITHM
620 if (s_threadinfo[tid].last)
621 {
622 thread_report_races_segment(tid, s_threadinfo[tid].last);
623 }
624#endif
625
626 sg = sg_new(tid, tid);
627 thread_append_segment(tid, sg);
628
629 thread_discard_ordered_segments();
630}
631
632void thread_combine_vc(DrdThreadId joiner, DrdThreadId joinee)
633{
634 tl_assert(joiner != joinee);
635 tl_assert(0 <= joiner && joiner < DRD_N_THREADS
636 && joiner != DRD_INVALID_THREADID);
637 tl_assert(0 <= joinee && joinee < DRD_N_THREADS
638 && joinee != DRD_INVALID_THREADID);
639 tl_assert(s_threadinfo[joiner].last);
640 tl_assert(s_threadinfo[joinee].last);
641 vc_combine(&s_threadinfo[joiner].last->vc, &s_threadinfo[joinee].last->vc);
642 thread_discard_ordered_segments();
643
644 if (joiner == s_running_tid)
645 {
646 thread_update_danger_set(joiner);
647 }
648}
649
650void thread_combine_vc2(DrdThreadId tid, const VectorClock* const vc)
651{
652 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
653 tl_assert(s_threadinfo[tid].last);
654 tl_assert(vc);
655 vc_combine(&s_threadinfo[tid].last->vc, vc);
656 thread_discard_ordered_segments();
657}
658
659void thread_stop_using_mem(const Addr a1, const Addr a2)
660{
661 DrdThreadId other_user = DRD_INVALID_THREADID;
662
663 /* For all threads, mark the range [a,a+size[ as no longer in use. */
664
665 unsigned i;
666 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
667 {
668 Segment* p;
669 for (p = s_threadinfo[i].first; p; p = p->next)
670 {
671 if (other_user == DRD_INVALID_THREADID
672 && i != s_running_tid
673 && bm_has_any_access(p->bm, a1, a2))
674 {
675 other_user = i;
676 }
677 bm_clear(p->bm, a1, a2);
678 }
679 }
680
681 /* If any other thread had accessed memory in [a,a+size[, update the */
682 /* danger set. */
683 if (other_user != DRD_INVALID_THREADID
684 && bm_has_any_access(s_danger_set, a1, a2))
685 {
686#if 0
687 VG_(message)(Vg_DebugMsg,
688 "recalculating danger set because thread %d / %d stopped"
689 " using memory at 0x%x sz %d",
690 other_user,
691 s_threadinfo[other_user].vg_threadid,
692 a1,
693 a2 - a1);
694#endif
695 thread_update_danger_set(thread_get_running_tid());
696 }
697}
698
699void thread_start_recording(const DrdThreadId tid)
700{
701 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
702 tl_assert(! s_threadinfo[tid].is_recording);
703 s_threadinfo[tid].is_recording = True;
704}
705
706void thread_stop_recording(const DrdThreadId tid)
707{
708 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
709 tl_assert(s_threadinfo[tid].is_recording);
710 s_threadinfo[tid].is_recording = False;
711}
712
713Bool thread_is_recording(const DrdThreadId tid)
714{
715 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
716 return s_threadinfo[tid].is_recording;
717}
718
719void thread_print_all(void)
720{
721 unsigned i;
722 Segment* p;
723
724 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
725 {
726 if (s_threadinfo[i].first)
727 {
728 VG_(printf)("**************\n"
729 "* thread %3d (%d/%d/%d/0x%x/%d/%s) *\n"
730 "**************\n",
731 i,
732 s_threadinfo[i].vg_thread_exists,
733 s_threadinfo[i].vg_threadid,
734 s_threadinfo[i].posix_thread_exists,
735 s_threadinfo[i].pt_threadid,
736 s_threadinfo[i].detached_posix_thread,
737 s_threadinfo[i].name);
738 for (p = s_threadinfo[i].first; p; p = p->next)
739 {
740 sg_print(p);
741 }
742 }
743 }
744}
745
746static void show_call_stack(const DrdThreadId tid,
747 const Char* const msg,
748 ExeContext* const callstack)
749{
750 const ThreadId vg_tid = DrdThreadIdToVgThreadId(tid);
751
752 VG_(message)(Vg_UserMsg,
753 "%s (%s)",
754 msg,
755 thread_get_name(tid));
756
757 if (vg_tid != VG_INVALID_THREADID)
758 {
759 if (callstack)
760 {
761 VG_(pp_ExeContext)(callstack);
762 }
763 else
764 {
765 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
766 }
767 }
768 else
769 {
770 VG_(message)(Vg_UserMsg,
771 " (thread finished, call stack no longer available)");
772 }
773}
774
775#ifdef OLD_RACE_DETECTION_ALGORITHM
776void thread_report_races(const DrdThreadId threadid)
777{
778 Segment* p;
779
780 s_report_races_count++;
781
782 tl_assert(0 <= threadid && threadid < DRD_N_THREADS
783 && threadid != DRD_INVALID_THREADID);
784
785 for (p = s_threadinfo[threadid].first; p; p = p->next)
786 {
787 thread_report_races_segment(threadid, p);
788 }
789}
790
791/**
792 * Report all data races for segment p of thread threadid against other
793 * threads.
794 */
795void thread_report_races_segment(const DrdThreadId threadid,
796 Segment* const p)
797{
798 unsigned i;
799
800 tl_assert(0 <= threadid && threadid < DRD_N_THREADS
801 && threadid != DRD_INVALID_THREADID);
802 tl_assert(p);
803
804 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
805 {
806 if (i != threadid)
807 {
808 Segment* q;
809 for (q = s_threadinfo[i].last; q; q = q->prev)
810 {
811#if 0
812 char msg[256];
813 VG_(snprintf)(msg, sizeof(msg), "Examining thread %d (vc ", threadid);
814 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
815 &p->vc);
816 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
817 ") versus thread %d (vc ", i);
818 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
819 &q->vc);
820 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
821 ") %d %d",
822 vc_lte(&p->vc, &q->vc), vc_lte(&q->vc, &p->vc));
823 VG_(message)(Vg_DebugMsg, "%s", msg);
824#endif
825 // Since q iterates over the segments of thread i in order of
826 // decreasing vector clocks, if q->vc <= p->vc, then
827 // q->next->vc <= p->vc will also hold. Hence, break out of the
828 // loop once this condition is met.
829 if (vc_lte(&q->vc, &p->vc))
830 break;
831 if (! vc_lte(&p->vc, &q->vc))
832 {
833 if (bm_has_races(p->bm, q->bm))
834 {
835 VG_(message)(Vg_UserMsg, "----------------------------------------------------------------------");
836 tl_assert(p->stacktrace);
837 show_call_stack(threadid, "1st segment start",
838 p->stacktrace);
839 show_call_stack(threadid, "1st segment end",
840 p->next ? p->next->stacktrace : 0);
841 tl_assert(q->stacktrace);
842 show_call_stack(i, "2nd segment start",
843 q->stacktrace);
844 show_call_stack(i, "2nd segment end",
845 q->next ? q->next->stacktrace : 0);
846 bm_report_races(threadid, i, p->bm, q->bm);
847 }
848 }
849 }
850 }
851 }
852}
853
854/**
855 * Report all detected data races for all threads.
856 */
857void thread_report_all_races(void)
858{
859 unsigned i;
860
861 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
862 {
863 if (s_threadinfo[i].last)
864 {
865 thread_report_races(i);
866 }
867 }
868}
869#else
870static void
871thread_report_conflicting_segments_segment(const DrdThreadId tid,
872 const Addr addr,
873 const SizeT size,
874 const BmAccessTypeT access_type,
875 const Segment* const p)
876{
877 unsigned i;
878
879 tl_assert(0 <= tid && tid < DRD_N_THREADS
880 && tid != DRD_INVALID_THREADID);
881 tl_assert(p);
882
883 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
884 {
885 if (i != tid)
886 {
887 Segment* q;
888 for (q = s_threadinfo[i].last; q; q = q->prev)
889 {
890 // Since q iterates over the segments of thread i in order of
891 // decreasing vector clocks, if q->vc <= p->vc, then
892 // q->next->vc <= p->vc will also hold. Hence, break out of the
893 // loop once this condition is met.
894 if (vc_lte(&q->vc, &p->vc))
895 break;
896 if (! vc_lte(&p->vc, &q->vc))
897 {
898 if (bm_has_conflict_with(q->bm, addr, addr + size, access_type))
899 {
900 tl_assert(q->stacktrace);
901 show_call_stack(i, "Other segment start",
902 q->stacktrace);
903 show_call_stack(i, "Other segment end",
904 q->next ? q->next->stacktrace : 0);
905 }
906 }
907 }
908 }
909 }
910}
911
912void thread_report_conflicting_segments(const DrdThreadId tid,
913 const Addr addr,
914 const SizeT size,
915 const BmAccessTypeT access_type)
916{
917 Segment* p;
918
919 tl_assert(0 <= tid && tid < DRD_N_THREADS
920 && tid != DRD_INVALID_THREADID);
921
922 for (p = s_threadinfo[tid].first; p; p = p->next)
923 {
924 if (bm_has(p->bm, addr, addr + size, access_type))
925 {
926 thread_report_conflicting_segments_segment(tid, addr, size,
927 access_type, p);
928 }
929 }
930}
931#endif
932
933/**
934 * Compute a bitmap that represents the union of all memory accesses of all
935 * segments that are unordered to the current segment of the thread tid.
936 */
937static void thread_update_danger_set(const DrdThreadId tid)
938{
939 Segment* p;
940
941 tl_assert(0 <= tid && tid < DRD_N_THREADS
942 && tid != DRD_INVALID_THREADID);
943 tl_assert(tid == s_running_tid);
944
945 s_update_danger_set_count++;
946 s_danger_set_bitmap_creation_count -= bm_get_bitmap_creation_count();
947 s_danger_set_bitmap2_creation_count -= bm_get_bitmap2_creation_count();
948
949#if 0
950 if (s_danger_set)
951 {
952 bm_delete(s_danger_set);
953 s_danger_set = 0;
954 }
955 s_danger_set = bm_new();
956#else
957 // Marginally faster than the above code.
958 if (s_danger_set)
959 {
960 bm_clear_all(s_danger_set);
961 }
962 else
963 {
964 s_danger_set = bm_new();
965 }
966#endif
967
968 for (p = s_threadinfo[tid].first; p; p = p->next)
969 {
970 unsigned j;
971
972 for (j = 0; j < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); j++)
973 {
974 if (IsValidDrdThreadId(j))
975 {
976 const Segment* const q = s_threadinfo[j].last;
977 if (j != tid && q != 0
978 && ! vc_lte(&q->vc, &p->vc) && ! vc_lte(&p->vc, &q->vc))
979 {
980 bm_merge2(s_danger_set, q->bm);
981 }
982
983 }
984 }
985
986 for (j = 0; j < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); j++)
987 {
988 if (IsValidDrdThreadId(j))
989 {
990 // NPTL hack: don't report data races on sizeof(struct pthread)
991 // bytes at the top of the stack, since the NPTL functions access
992 // this data without locking.
993 if (s_threadinfo[j].stack_min != 0)
994 {
995 tl_assert(s_threadinfo[j].stack_startup != 0);
996 if (s_threadinfo[j].stack_min < s_threadinfo[j].stack_startup)
997 {
998 bm_clear(s_danger_set,
999 s_threadinfo[j].stack_min,
1000 s_threadinfo[j].stack_startup);
1001 }
1002 }
1003 }
1004 }
1005 }
1006
1007 s_danger_set_bitmap_creation_count += bm_get_bitmap_creation_count();
1008 s_danger_set_bitmap2_creation_count += bm_get_bitmap2_creation_count();
1009
1010#if 0
1011 VG_(message)(Vg_DebugMsg, "[%d] new danger set:", tid);
1012 bm_print(s_danger_set);
1013 VG_(message)(Vg_DebugMsg, "[%d] end of new danger set.", tid);
1014#endif
1015}
1016
1017Bool thread_conflicting_access(const Addr a,
1018 const SizeT size,
1019 const BmAccessTypeT access_type)
1020{
1021 tl_assert(s_danger_set);
1022 return (bm_has_conflict_with(s_danger_set, a, a + size, access_type)
1023 && ! drd_is_suppressed(a, a + size));
1024}
1025
1026ULong thread_get_context_switch_count(void)
1027{
1028 return s_context_switch_count;
1029}
1030
1031#ifdef OLD_RACE_DETECTION_ALGORITHM
1032ULong thread_get_report_races_count(void)
1033{
1034 return s_report_races_count;
1035}
1036#endif
1037
1038ULong thread_get_discard_ordered_segments_count(void)
1039{
1040 return s_discard_ordered_segments_count;
1041}
1042
1043ULong thread_get_update_danger_set_count(void)
1044{
1045 return s_update_danger_set_count;
1046}
1047
1048ULong thread_get_danger_set_bitmap_creation_count(void)
1049{
1050 return s_danger_set_bitmap_creation_count;
1051}
1052
1053ULong thread_get_danger_set_bitmap2_creation_count(void)
1054{
1055 return s_danger_set_bitmap2_creation_count;
1056}
1057
1058/*
1059 * Local variables:
1060 * c-basic-offset: 3
1061 * End:
1062 */