blob: 8ab9938f2e64a0700d147ecf6fb59f8843062ee3 [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"
sewardjaf44c822007-11-25 14:01:38 +000030#include "pub_core_options.h" // VG_(clo_backtrace_size)
31#include "pub_tool_basics.h" // Addr, SizeT
32#include "pub_tool_errormgr.h" // VG_(unique_error)()
33#include "pub_tool_libcassert.h" // tl_assert()
34#include "pub_tool_libcbase.h" // VG_(strlen)()
35#include "pub_tool_libcprint.h" // VG_(printf)()
36#include "pub_tool_machine.h"
37#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
38#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
39
40
41// Defines.
42
43#define DRD_N_THREADS VG_N_THREADS
44
45
46// Type definitions.
47
48typedef struct
49{
50 Segment* first;
51 Segment* last;
52 ThreadId vg_threadid;
53 PThreadId pt_threadid;
54 Addr stack_min_min;
55 Addr stack_min;
56 Addr stack_startup;
57 Addr stack_max;
58 char name[32];
59 /// Indicates whether the Valgrind core knows about this thread.
60 Bool vg_thread_exists;
61 /// Indicates whether there is an associated POSIX thread ID.
62 Bool posix_thread_exists;
63 /// If true, indicates that there is a corresponding POSIX thread ID and
64 /// a corresponding OS thread that is detached.
65 Bool detached_posix_thread;
66 Bool is_recording;
67} ThreadInfo;
68
69
70// Local functions.
71
72static void thread_append_segment(const DrdThreadId tid,
73 Segment* const sg);
74static void thread_update_danger_set(const DrdThreadId tid);
75
76
77// Local variables.
78
79static ULong s_context_switch_count;
80static ULong s_discard_ordered_segments_count;
81#ifdef OLD_RACE_DETECTION_ALGORITHM
82static ULong s_report_races_count;
83#endif
84static ULong s_update_danger_set_count;
85static ULong s_danger_set_bitmap_creation_count;
86static ULong s_danger_set_bitmap2_creation_count;
sewardj8b09d4f2007-12-04 21:27:18 +000087static ThreadId s_vg_running_tid = VG_INVALID_THREADID;
88static DrdThreadId s_drd_running_tid = DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +000089static 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}
sewardj8b09d4f2007-12-04 21:27:18 +0000424
sewardjaf44c822007-11-25 14:01:38 +0000425DrdThreadId thread_get_running_tid(void)
426{
sewardj8b09d4f2007-12-04 21:27:18 +0000427 // HACK. To do: remove the if-statement and keep the assert.
428 if (VG_(get_running_tid)() != VG_INVALID_THREADID)
429 tl_assert(VG_(get_running_tid)() == s_vg_running_tid);
430 tl_assert(s_drd_running_tid != DRD_INVALID_THREADID);
431 return s_drd_running_tid;
sewardjaf44c822007-11-25 14:01:38 +0000432}
433
sewardj8b09d4f2007-12-04 21:27:18 +0000434void thread_set_vg_running_tid(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000435{
sewardj8b09d4f2007-12-04 21:27:18 +0000436 // HACK. To do: uncomment the line below.
437 // tl_assert(vg_tid != VG_INVALID_THREADID);
438
439 if (vg_tid != s_vg_running_tid)
440 {
441 thread_set_running_tid(vg_tid, VgThreadIdToDrdThreadId(vg_tid));
442 }
443
444 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
445 tl_assert(s_drd_running_tid != DRD_INVALID_THREADID);
446}
447
448void thread_set_running_tid(const ThreadId vg_tid, const DrdThreadId drd_tid)
449{
450 // HACK. To do: remove the next two lines.
451 if (vg_tid == VG_INVALID_THREADID)
452 return;
453
454 tl_assert(vg_tid != VG_INVALID_THREADID);
455 tl_assert(drd_tid != DRD_INVALID_THREADID);
456
457 if (vg_tid != s_vg_running_tid)
458 {
459 s_vg_running_tid = vg_tid;
460 s_drd_running_tid = drd_tid;
461 thread_update_danger_set(drd_tid);
462 s_context_switch_count++;
463 }
464
465 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
466 tl_assert(s_drd_running_tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000467}
468
469/**
470 * Return a pointer to the latest segment for the specified thread.
471 */
472Segment* thread_get_segment(const DrdThreadId tid)
473{
474 tl_assert(0 <= tid && tid < DRD_N_THREADS
475 && tid != DRD_INVALID_THREADID);
476 if (s_threadinfo[tid].last == 0)
477 {
478 VG_(message)(Vg_DebugMsg, "threadid = %d", tid);
479 thread_print_all();
480 }
481 tl_assert(s_threadinfo[tid].last);
482 return s_threadinfo[tid].last;
483}
484
485/**
486 * Insert a new segment at the end of the segment list.
487 */
488static void thread_append_segment(const DrdThreadId tid,
489 Segment* const sg)
490{
491 tl_assert(0 <= tid && tid < DRD_N_THREADS
492 && tid != DRD_INVALID_THREADID);
493 tl_assert(sane_ThreadInfo(&s_threadinfo[tid]));
494 sg->prev = s_threadinfo[tid].last;
495 sg->next = 0;
496 if (s_threadinfo[tid].last)
497 s_threadinfo[tid].last->next = sg;
498 s_threadinfo[tid].last = sg;
499 if (s_threadinfo[tid].first == 0)
500 s_threadinfo[tid].first = sg;
501 tl_assert(sane_ThreadInfo(&s_threadinfo[tid]));
502}
503
504/**
505 * Remove a segment from the segment list of thread threadid, and free the
506 * associated memory.
507 */
508static void thread_discard_segment(const DrdThreadId tid,
509 Segment* const sg)
510{
511 tl_assert(0 <= tid && tid < DRD_N_THREADS
512 && tid != DRD_INVALID_THREADID);
513 tl_assert(sane_ThreadInfo(&s_threadinfo[tid]));
514 if (sg->prev)
515 sg->prev->next = sg->next;
516 if (sg->next)
517 sg->next->prev = sg->prev;
518 if (sg == s_threadinfo[tid].first)
519 s_threadinfo[tid].first = sg->next;
520 if (sg == s_threadinfo[tid].last)
521 s_threadinfo[tid].last = sg->prev;
522 sg_delete(sg);
523 tl_assert(sane_ThreadInfo(&s_threadinfo[tid]));
524}
525
526VectorClock* thread_get_vc(const DrdThreadId tid)
527{
528 tl_assert(0 <= tid && tid < DRD_N_THREADS
529 && tid != DRD_INVALID_THREADID);
530 tl_assert(s_threadinfo[tid].last);
531 return &s_threadinfo[tid].last->vc;
532}
533
534/**
535 * Compute the minimum of all latest vector clocks of all threads
536 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
537 * @param vc pointer to a vectorclock, holds result upon return.
538 */
539static void thread_compute_minimum_vc(VectorClock* vc)
540{
541 int i;
542 Bool first;
543 Segment* latest_sg;
544
545 first = True;
546 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
547 {
548 latest_sg = s_threadinfo[i].last;
549 if (latest_sg)
550 {
551 if (first)
552 {
553 vc_cleanup(vc);
554 vc_copy(vc, &latest_sg->vc);
555 }
556 else
557 vc_min(vc, &latest_sg->vc);
558 first = False;
559 }
560 }
561}
562
563static void thread_compute_maximum_vc(VectorClock* vc)
564{
565 int i;
566 Bool first;
567 Segment* latest_sg;
568
569 first = True;
570 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
571 {
572 latest_sg = s_threadinfo[i].last;
573 if (latest_sg)
574 {
575 if (first)
576 {
577 vc_cleanup(vc);
578 vc_copy(vc, &latest_sg->vc);
579 }
580 else
581 vc_combine(vc, &latest_sg->vc);
582 first = False;
583 }
584 }
585}
586
587/**
588 * Discard all segments that have a defined ordered against the latest vector
589 * clock of every thread -- these segments can no longer be involved in a
590 * data race.
591 */
592static void thread_discard_ordered_segments(void)
593{
594 VectorClock thread_vc_min;
595 int i;
596
597 s_discard_ordered_segments_count++;
598
599 vc_init(&thread_vc_min, 0, 0);
600 thread_compute_minimum_vc(&thread_vc_min);
601 if (sg_get_trace())
602 {
603 char msg[256];
604 VectorClock thread_vc_max;
605
606 vc_init(&thread_vc_max, 0, 0);
607 thread_compute_maximum_vc(&thread_vc_max);
608 VG_(snprintf)(msg, sizeof(msg),
609 "Discarding ordered segments -- min vc is ");
610 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
611 &thread_vc_min);
612 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
613 ", max vc is ");
614 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
615 &thread_vc_max);
616 VG_(message)(Vg_DebugMsg, "%s", msg);
617 vc_cleanup(&thread_vc_max);
618 }
619
620 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
621 {
622 Segment* sg;
623 Segment* sg_next;
624 for (sg = s_threadinfo[i].first;
625 sg && (sg_next = sg->next) && vc_lte(&sg->vc, &thread_vc_min);
626 sg = sg_next)
627 {
628#if 0
629 VG_(printf)("Discarding a segment of thread %d: ", i);
630 vc_print(&sg->vc);
631 VG_(printf)("\n");
632#endif
633 thread_discard_segment(i, sg);
634 }
635 }
636 vc_cleanup(&thread_vc_min);
637}
638
639/**
640 * Create a new segment for the specified thread, and report all data races
641 * of the most recent thread segment with other threads.
642 */
643void thread_new_segment(const DrdThreadId tid)
644{
645 //static int s_calls_since_last_discard = 0;
646 Segment* sg;
647
648 tl_assert(0 <= tid && tid < DRD_N_THREADS
649 && tid != DRD_INVALID_THREADID);
650
651#ifdef OLD_RACE_DETECTION_ALGORITHM
652 if (s_threadinfo[tid].last)
653 {
654 thread_report_races_segment(tid, s_threadinfo[tid].last);
655 }
656#endif
657
658 sg = sg_new(tid, tid);
659 thread_append_segment(tid, sg);
660
661 thread_discard_ordered_segments();
662}
663
664void thread_combine_vc(DrdThreadId joiner, DrdThreadId joinee)
665{
666 tl_assert(joiner != joinee);
667 tl_assert(0 <= joiner && joiner < DRD_N_THREADS
668 && joiner != DRD_INVALID_THREADID);
669 tl_assert(0 <= joinee && joinee < DRD_N_THREADS
670 && joinee != DRD_INVALID_THREADID);
671 tl_assert(s_threadinfo[joiner].last);
672 tl_assert(s_threadinfo[joinee].last);
673 vc_combine(&s_threadinfo[joiner].last->vc, &s_threadinfo[joinee].last->vc);
674 thread_discard_ordered_segments();
675
sewardj8b09d4f2007-12-04 21:27:18 +0000676 if (joiner == s_drd_running_tid)
sewardjaf44c822007-11-25 14:01:38 +0000677 {
678 thread_update_danger_set(joiner);
679 }
680}
681
682void thread_combine_vc2(DrdThreadId tid, const VectorClock* const vc)
683{
684 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
685 tl_assert(s_threadinfo[tid].last);
686 tl_assert(vc);
687 vc_combine(&s_threadinfo[tid].last->vc, vc);
688 thread_discard_ordered_segments();
689}
690
691void thread_stop_using_mem(const Addr a1, const Addr a2)
692{
693 DrdThreadId other_user = DRD_INVALID_THREADID;
694
695 /* For all threads, mark the range [a,a+size[ as no longer in use. */
696
697 unsigned i;
698 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
699 {
700 Segment* p;
701 for (p = s_threadinfo[i].first; p; p = p->next)
702 {
703 if (other_user == DRD_INVALID_THREADID
sewardj8b09d4f2007-12-04 21:27:18 +0000704 && i != s_drd_running_tid
sewardjaf44c822007-11-25 14:01:38 +0000705 && bm_has_any_access(p->bm, a1, a2))
706 {
707 other_user = i;
708 }
709 bm_clear(p->bm, a1, a2);
710 }
711 }
712
713 /* If any other thread had accessed memory in [a,a+size[, update the */
714 /* danger set. */
715 if (other_user != DRD_INVALID_THREADID
716 && bm_has_any_access(s_danger_set, a1, a2))
717 {
718#if 0
719 VG_(message)(Vg_DebugMsg,
720 "recalculating danger set because thread %d / %d stopped"
721 " using memory at 0x%x sz %d",
722 other_user,
723 s_threadinfo[other_user].vg_threadid,
724 a1,
725 a2 - a1);
726#endif
727 thread_update_danger_set(thread_get_running_tid());
728 }
729}
730
731void thread_start_recording(const DrdThreadId tid)
732{
733 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
734 tl_assert(! s_threadinfo[tid].is_recording);
735 s_threadinfo[tid].is_recording = True;
736}
737
738void thread_stop_recording(const DrdThreadId tid)
739{
740 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
741 tl_assert(s_threadinfo[tid].is_recording);
742 s_threadinfo[tid].is_recording = False;
743}
744
745Bool thread_is_recording(const DrdThreadId tid)
746{
747 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
748 return s_threadinfo[tid].is_recording;
749}
750
751void thread_print_all(void)
752{
753 unsigned i;
754 Segment* p;
755
756 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
757 {
758 if (s_threadinfo[i].first)
759 {
760 VG_(printf)("**************\n"
761 "* thread %3d (%d/%d/%d/0x%x/%d/%s) *\n"
762 "**************\n",
763 i,
764 s_threadinfo[i].vg_thread_exists,
765 s_threadinfo[i].vg_threadid,
766 s_threadinfo[i].posix_thread_exists,
767 s_threadinfo[i].pt_threadid,
768 s_threadinfo[i].detached_posix_thread,
769 s_threadinfo[i].name);
770 for (p = s_threadinfo[i].first; p; p = p->next)
771 {
772 sg_print(p);
773 }
774 }
775 }
776}
777
778static void show_call_stack(const DrdThreadId tid,
779 const Char* const msg,
780 ExeContext* const callstack)
781{
782 const ThreadId vg_tid = DrdThreadIdToVgThreadId(tid);
783
784 VG_(message)(Vg_UserMsg,
785 "%s (%s)",
786 msg,
787 thread_get_name(tid));
788
789 if (vg_tid != VG_INVALID_THREADID)
790 {
791 if (callstack)
792 {
793 VG_(pp_ExeContext)(callstack);
794 }
795 else
796 {
797 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
798 }
799 }
800 else
801 {
802 VG_(message)(Vg_UserMsg,
803 " (thread finished, call stack no longer available)");
804 }
805}
806
807#ifdef OLD_RACE_DETECTION_ALGORITHM
808void thread_report_races(const DrdThreadId threadid)
809{
810 Segment* p;
811
812 s_report_races_count++;
813
814 tl_assert(0 <= threadid && threadid < DRD_N_THREADS
815 && threadid != DRD_INVALID_THREADID);
816
817 for (p = s_threadinfo[threadid].first; p; p = p->next)
818 {
819 thread_report_races_segment(threadid, p);
820 }
821}
822
823/**
824 * Report all data races for segment p of thread threadid against other
825 * threads.
826 */
827void thread_report_races_segment(const DrdThreadId threadid,
828 Segment* const p)
829{
830 unsigned i;
831
832 tl_assert(0 <= threadid && threadid < DRD_N_THREADS
833 && threadid != DRD_INVALID_THREADID);
834 tl_assert(p);
835
836 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
837 {
838 if (i != threadid)
839 {
840 Segment* q;
841 for (q = s_threadinfo[i].last; q; q = q->prev)
842 {
843#if 0
844 char msg[256];
845 VG_(snprintf)(msg, sizeof(msg), "Examining thread %d (vc ", threadid);
846 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
847 &p->vc);
848 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
849 ") versus thread %d (vc ", i);
850 vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
851 &q->vc);
852 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
853 ") %d %d",
854 vc_lte(&p->vc, &q->vc), vc_lte(&q->vc, &p->vc));
855 VG_(message)(Vg_DebugMsg, "%s", msg);
856#endif
857 // Since q iterates over the segments of thread i in order of
858 // decreasing vector clocks, if q->vc <= p->vc, then
859 // q->next->vc <= p->vc will also hold. Hence, break out of the
860 // loop once this condition is met.
861 if (vc_lte(&q->vc, &p->vc))
862 break;
863 if (! vc_lte(&p->vc, &q->vc))
864 {
865 if (bm_has_races(p->bm, q->bm))
866 {
867 VG_(message)(Vg_UserMsg, "----------------------------------------------------------------------");
868 tl_assert(p->stacktrace);
869 show_call_stack(threadid, "1st segment start",
870 p->stacktrace);
871 show_call_stack(threadid, "1st segment end",
872 p->next ? p->next->stacktrace : 0);
873 tl_assert(q->stacktrace);
874 show_call_stack(i, "2nd segment start",
875 q->stacktrace);
876 show_call_stack(i, "2nd segment end",
877 q->next ? q->next->stacktrace : 0);
878 bm_report_races(threadid, i, p->bm, q->bm);
879 }
880 }
881 }
882 }
883 }
884}
885
886/**
887 * Report all detected data races for all threads.
888 */
889void thread_report_all_races(void)
890{
891 unsigned i;
892
893 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
894 {
895 if (s_threadinfo[i].last)
896 {
897 thread_report_races(i);
898 }
899 }
900}
901#else
902static void
903thread_report_conflicting_segments_segment(const DrdThreadId tid,
904 const Addr addr,
905 const SizeT size,
906 const BmAccessTypeT access_type,
907 const Segment* const p)
908{
909 unsigned i;
910
911 tl_assert(0 <= tid && tid < DRD_N_THREADS
912 && tid != DRD_INVALID_THREADID);
913 tl_assert(p);
914
915 for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
916 {
917 if (i != tid)
918 {
919 Segment* q;
920 for (q = s_threadinfo[i].last; q; q = q->prev)
921 {
922 // Since q iterates over the segments of thread i in order of
923 // decreasing vector clocks, if q->vc <= p->vc, then
924 // q->next->vc <= p->vc will also hold. Hence, break out of the
925 // loop once this condition is met.
926 if (vc_lte(&q->vc, &p->vc))
927 break;
928 if (! vc_lte(&p->vc, &q->vc))
929 {
930 if (bm_has_conflict_with(q->bm, addr, addr + size, access_type))
931 {
932 tl_assert(q->stacktrace);
933 show_call_stack(i, "Other segment start",
934 q->stacktrace);
935 show_call_stack(i, "Other segment end",
936 q->next ? q->next->stacktrace : 0);
937 }
938 }
939 }
940 }
941 }
942}
943
944void thread_report_conflicting_segments(const DrdThreadId tid,
945 const Addr addr,
946 const SizeT size,
947 const BmAccessTypeT access_type)
948{
949 Segment* p;
950
951 tl_assert(0 <= tid && tid < DRD_N_THREADS
952 && tid != DRD_INVALID_THREADID);
953
954 for (p = s_threadinfo[tid].first; p; p = p->next)
955 {
956 if (bm_has(p->bm, addr, addr + size, access_type))
957 {
958 thread_report_conflicting_segments_segment(tid, addr, size,
959 access_type, p);
960 }
961 }
962}
963#endif
964
965/**
966 * Compute a bitmap that represents the union of all memory accesses of all
967 * segments that are unordered to the current segment of the thread tid.
968 */
969static void thread_update_danger_set(const DrdThreadId tid)
970{
971 Segment* p;
972
973 tl_assert(0 <= tid && tid < DRD_N_THREADS
974 && tid != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000975 tl_assert(tid == s_drd_running_tid);
sewardjaf44c822007-11-25 14:01:38 +0000976
977 s_update_danger_set_count++;
978 s_danger_set_bitmap_creation_count -= bm_get_bitmap_creation_count();
979 s_danger_set_bitmap2_creation_count -= bm_get_bitmap2_creation_count();
980
981#if 0
982 if (s_danger_set)
983 {
984 bm_delete(s_danger_set);
985 s_danger_set = 0;
986 }
987 s_danger_set = bm_new();
988#else
989 // Marginally faster than the above code.
990 if (s_danger_set)
991 {
992 bm_clear_all(s_danger_set);
993 }
994 else
995 {
996 s_danger_set = bm_new();
997 }
998#endif
999
1000 for (p = s_threadinfo[tid].first; p; p = p->next)
1001 {
1002 unsigned j;
1003
1004 for (j = 0; j < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); j++)
1005 {
1006 if (IsValidDrdThreadId(j))
1007 {
1008 const Segment* const q = s_threadinfo[j].last;
1009 if (j != tid && q != 0
1010 && ! vc_lte(&q->vc, &p->vc) && ! vc_lte(&p->vc, &q->vc))
1011 {
1012 bm_merge2(s_danger_set, q->bm);
1013 }
1014
1015 }
1016 }
1017
1018 for (j = 0; j < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); j++)
1019 {
1020 if (IsValidDrdThreadId(j))
1021 {
1022 // NPTL hack: don't report data races on sizeof(struct pthread)
1023 // bytes at the top of the stack, since the NPTL functions access
1024 // this data without locking.
1025 if (s_threadinfo[j].stack_min != 0)
1026 {
1027 tl_assert(s_threadinfo[j].stack_startup != 0);
1028 if (s_threadinfo[j].stack_min < s_threadinfo[j].stack_startup)
1029 {
1030 bm_clear(s_danger_set,
1031 s_threadinfo[j].stack_min,
1032 s_threadinfo[j].stack_startup);
1033 }
1034 }
1035 }
1036 }
1037 }
1038
1039 s_danger_set_bitmap_creation_count += bm_get_bitmap_creation_count();
1040 s_danger_set_bitmap2_creation_count += bm_get_bitmap2_creation_count();
1041
1042#if 0
1043 VG_(message)(Vg_DebugMsg, "[%d] new danger set:", tid);
1044 bm_print(s_danger_set);
1045 VG_(message)(Vg_DebugMsg, "[%d] end of new danger set.", tid);
1046#endif
1047}
1048
1049Bool thread_conflicting_access(const Addr a,
1050 const SizeT size,
1051 const BmAccessTypeT access_type)
1052{
1053 tl_assert(s_danger_set);
1054 return (bm_has_conflict_with(s_danger_set, a, a + size, access_type)
1055 && ! drd_is_suppressed(a, a + size));
1056}
1057
1058ULong thread_get_context_switch_count(void)
1059{
1060 return s_context_switch_count;
1061}
1062
1063#ifdef OLD_RACE_DETECTION_ALGORITHM
1064ULong thread_get_report_races_count(void)
1065{
1066 return s_report_races_count;
1067}
1068#endif
1069
1070ULong thread_get_discard_ordered_segments_count(void)
1071{
1072 return s_discard_ordered_segments_count;
1073}
1074
1075ULong thread_get_update_danger_set_count(void)
1076{
1077 return s_update_danger_set_count;
1078}
1079
1080ULong thread_get_danger_set_bitmap_creation_count(void)
1081{
1082 return s_danger_set_bitmap_creation_count;
1083}
1084
1085ULong thread_get_danger_set_bitmap2_creation_count(void)
1086{
1087 return s_danger_set_bitmap2_creation_count;
1088}
1089
1090/*
1091 * Local variables:
1092 * c-basic-offset: 3
1093 * End:
1094 */