sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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" |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 30 | #include "pub_tool_vki.h" |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 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)() |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 36 | #include "pub_tool_libcproc.h" // VG_(getenv)() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 37 | #include "pub_tool_machine.h" |
| 38 | #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)() |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 39 | #include "pub_tool_options.h" // VG_(clo_backtrace_size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 40 | #include "pub_tool_threadstate.h" // VG_(get_pthread_id)() |
| 41 | |
bart | 32ba208 | 2008-06-05 08:53:42 +0000 | [diff] [blame^] | 42 | /* Include the drd_bitmap.c source file here to allow the compiler to */ |
| 43 | /* inline the bitmap manipulation functions called from this source file. */ |
| 44 | #include "drd_bitmap.c" |
| 45 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 46 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 47 | // Local functions. |
| 48 | |
| 49 | static void thread_append_segment(const DrdThreadId tid, |
| 50 | Segment* const sg); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 51 | static void thread_discard_segment(const DrdThreadId tid, Segment* const sg); |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 52 | static Bool thread_danger_set_up_to_date(const DrdThreadId tid); |
bart | 4af812e | 2008-04-13 15:39:38 +0000 | [diff] [blame] | 53 | static void thread_compute_danger_set(struct bitmap** danger_set, |
| 54 | const DrdThreadId tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 55 | |
| 56 | |
| 57 | // Local variables. |
| 58 | |
| 59 | static ULong s_context_switch_count; |
| 60 | static ULong s_discard_ordered_segments_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 61 | static ULong s_update_danger_set_count; |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 62 | static ULong s_danger_set_new_segment_count; |
| 63 | static ULong s_danger_set_combine_vc_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 64 | static ULong s_danger_set_bitmap_creation_count; |
| 65 | static ULong s_danger_set_bitmap2_creation_count; |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 66 | static ThreadId s_vg_running_tid = VG_INVALID_THREADID; |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 67 | DrdThreadId s_drd_running_tid = DRD_INVALID_THREADID; |
| 68 | ThreadInfo s_threadinfo[DRD_N_THREADS]; |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 69 | struct bitmap* s_danger_set; |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 70 | static Bool s_trace_context_switches = False; |
| 71 | static Bool s_trace_danger_set = False; |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 72 | static Bool s_segment_merging = True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | // Function definitions. |
| 76 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 77 | void thread_trace_context_switches(const Bool t) |
| 78 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 79 | s_trace_context_switches = t; |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void thread_trace_danger_set(const Bool t) |
| 83 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 84 | s_trace_danger_set = t; |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 85 | } |
| 86 | |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 87 | void thread_set_segment_merging(const Bool m) |
| 88 | { |
| 89 | s_segment_merging = m; |
| 90 | } |
| 91 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 92 | __inline__ Bool IsValidDrdThreadId(const DrdThreadId tid) |
| 93 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 94 | return (0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 95 | && ! (s_threadinfo[tid].vg_thread_exists == False |
| 96 | && s_threadinfo[tid].posix_thread_exists == False |
| 97 | && s_threadinfo[tid].detached_posix_thread == False)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Convert Valgrind's ThreadId into a DrdThreadId. Report failure if |
| 102 | * Valgrind's ThreadId does not yet exist. |
| 103 | **/ |
| 104 | DrdThreadId VgThreadIdToDrdThreadId(const ThreadId tid) |
| 105 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 106 | int i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 107 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 108 | if (tid == VG_INVALID_THREADID) |
| 109 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 110 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 111 | for (i = 1; i < DRD_N_THREADS; i++) |
| 112 | { |
| 113 | if (s_threadinfo[i].vg_thread_exists == True |
| 114 | && s_threadinfo[i].vg_threadid == tid) |
| 115 | { |
| 116 | return i; |
| 117 | } |
| 118 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 119 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 120 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static |
| 124 | DrdThreadId VgThreadIdToNewDrdThreadId(const ThreadId tid) |
| 125 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 126 | int i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 127 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 128 | tl_assert(VgThreadIdToDrdThreadId(tid) == DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 129 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 130 | for (i = 1; i < DRD_N_THREADS; i++) |
| 131 | { |
| 132 | if (s_threadinfo[i].vg_thread_exists == False |
| 133 | && s_threadinfo[i].posix_thread_exists == False |
| 134 | && s_threadinfo[i].detached_posix_thread == False) |
| 135 | { |
| 136 | s_threadinfo[i].vg_thread_exists = True; |
| 137 | s_threadinfo[i].vg_threadid = tid; |
| 138 | s_threadinfo[i].pt_threadid = INVALID_POSIX_THREADID; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 139 | s_threadinfo[i].stack_min = 0; |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 140 | s_threadinfo[i].stack_min_min = 0; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 141 | s_threadinfo[i].stack_startup = 0; |
| 142 | s_threadinfo[i].stack_max = 0; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 143 | s_threadinfo[i].is_recording = True; |
| 144 | s_threadinfo[i].synchr_nesting = 0; |
| 145 | if (s_threadinfo[i].first != 0) |
| 146 | VG_(printf)("drd thread id = %d\n", i); |
| 147 | tl_assert(s_threadinfo[i].first == 0); |
| 148 | tl_assert(s_threadinfo[i].last == 0); |
| 149 | return i; |
| 150 | } |
| 151 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 152 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 153 | tl_assert(False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 154 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 155 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | DrdThreadId PtThreadIdToDrdThreadId(const PThreadId tid) |
| 159 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 160 | int i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 161 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 162 | tl_assert(tid != INVALID_POSIX_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 163 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 164 | for (i = 1; i < DRD_N_THREADS; i++) |
| 165 | { |
| 166 | if (s_threadinfo[i].posix_thread_exists |
| 167 | && s_threadinfo[i].pt_threadid == tid) |
| 168 | { |
| 169 | return i; |
| 170 | } |
| 171 | } |
| 172 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | ThreadId DrdThreadIdToVgThreadId(const DrdThreadId tid) |
| 176 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 177 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 178 | && tid != DRD_INVALID_THREADID); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 179 | return (s_threadinfo[tid].vg_thread_exists |
| 180 | ? s_threadinfo[tid].vg_threadid |
| 181 | : VG_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 182 | } |
| 183 | |
bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 184 | #if 0 |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 185 | /** Sanity check of the doubly linked list of segments referenced by a |
| 186 | * ThreadInfo struct. |
| 187 | * @return True if sane, False if not. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 188 | */ |
| 189 | static Bool sane_ThreadInfo(const ThreadInfo* const ti) |
| 190 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 191 | Segment* p; |
| 192 | for (p = ti->first; p; p = p->next) { |
| 193 | if (p->next && p->next->prev != p) |
| 194 | return False; |
| 195 | if (p->next == 0 && p != ti->last) |
| 196 | return False; |
| 197 | } |
| 198 | for (p = ti->last; p; p = p->prev) { |
| 199 | if (p->prev && p->prev->next != p) |
| 200 | return False; |
| 201 | if (p->prev == 0 && p != ti->first) |
| 202 | return False; |
| 203 | } |
| 204 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 205 | } |
bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 206 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 207 | |
| 208 | DrdThreadId thread_pre_create(const DrdThreadId creator, |
| 209 | const ThreadId vg_created) |
| 210 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 211 | DrdThreadId created; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 212 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 213 | tl_assert(VgThreadIdToDrdThreadId(vg_created) == DRD_INVALID_THREADID); |
| 214 | created = VgThreadIdToNewDrdThreadId(vg_created); |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 215 | tl_assert(0 <= (int)created && created < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 216 | && created != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 217 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 218 | tl_assert(s_threadinfo[created].first == 0); |
| 219 | tl_assert(s_threadinfo[created].last == 0); |
| 220 | thread_append_segment(created, sg_new(creator, created)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 221 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 222 | return created; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 223 | } |
| 224 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 225 | /** Allocate the first segment for a thread. Call this just after |
| 226 | * pthread_create(). |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 227 | */ |
| 228 | DrdThreadId thread_post_create(const ThreadId vg_created) |
| 229 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 230 | const DrdThreadId created = VgThreadIdToDrdThreadId(vg_created); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 231 | |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 232 | tl_assert(0 <= (int)created && created < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 233 | && created != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 234 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 235 | s_threadinfo[created].stack_max = VG_(thread_get_stack_max)(vg_created); |
| 236 | s_threadinfo[created].stack_startup = s_threadinfo[created].stack_max; |
| 237 | s_threadinfo[created].stack_min = s_threadinfo[created].stack_max; |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 238 | s_threadinfo[created].stack_min_min = s_threadinfo[created].stack_max; |
| 239 | s_threadinfo[created].stack_size = VG_(thread_get_stack_size)(vg_created); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 240 | tl_assert(s_threadinfo[created].stack_max != 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 241 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 242 | return created; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* NPTL hack: NPTL allocates the 'struct pthread' on top of the stack, */ |
| 246 | /* and accesses this data structure from multiple threads without locking. */ |
| 247 | /* Any conflicting accesses in the range stack_startup..stack_max will be */ |
| 248 | /* ignored. */ |
| 249 | void thread_set_stack_startup(const DrdThreadId tid, const Addr stack_startup) |
| 250 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 251 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 252 | && tid != DRD_INVALID_THREADID); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 253 | tl_assert(s_threadinfo[tid].stack_min <= stack_startup); |
| 254 | tl_assert(stack_startup <= s_threadinfo[tid].stack_max); |
| 255 | s_threadinfo[tid].stack_startup = stack_startup; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | Addr thread_get_stack_min(const DrdThreadId tid) |
| 259 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 260 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 261 | && tid != DRD_INVALID_THREADID); |
| 262 | return s_threadinfo[tid].stack_min; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 263 | } |
| 264 | |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 265 | Addr thread_get_stack_min_min(const DrdThreadId tid) |
| 266 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 267 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 268 | && tid != DRD_INVALID_THREADID); |
| 269 | return s_threadinfo[tid].stack_min_min; |
| 270 | } |
| 271 | |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 272 | Addr thread_get_stack_max(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 273 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 274 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 275 | && tid != DRD_INVALID_THREADID); |
| 276 | return s_threadinfo[tid].stack_max; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 277 | } |
| 278 | |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 279 | SizeT thread_get_stack_size(const DrdThreadId tid) |
| 280 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 281 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 282 | && tid != DRD_INVALID_THREADID); |
| 283 | return s_threadinfo[tid].stack_size; |
| 284 | } |
| 285 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 286 | /** Clean up thread-specific data structures. Call this just after |
| 287 | * pthread_join(). |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 288 | */ |
| 289 | void thread_delete(const DrdThreadId tid) |
| 290 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 291 | Segment* sg; |
| 292 | Segment* sg_prev; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 293 | |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 294 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 295 | && tid != DRD_INVALID_THREADID); |
| 296 | tl_assert(s_threadinfo[tid].synchr_nesting == 0); |
| 297 | for (sg = s_threadinfo[tid].last; sg; sg = sg_prev) |
| 298 | { |
| 299 | sg_prev = sg->prev; |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 300 | sg->prev = 0; |
| 301 | sg->next = 0; |
| 302 | sg_put(sg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 303 | } |
| 304 | s_threadinfo[tid].vg_thread_exists = False; |
| 305 | s_threadinfo[tid].posix_thread_exists = False; |
| 306 | tl_assert(s_threadinfo[tid].detached_posix_thread == False); |
| 307 | s_threadinfo[tid].first = 0; |
| 308 | s_threadinfo[tid].last = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /* Called after a thread performed its last memory access and before */ |
| 312 | /* thread_delete() is called. Note: thread_delete() is only called for */ |
| 313 | /* joinable threads, not for detached threads. */ |
| 314 | void thread_finished(const DrdThreadId tid) |
| 315 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 316 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 317 | && tid != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 318 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 319 | s_threadinfo[tid].vg_thread_exists = False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 320 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 321 | if (s_threadinfo[tid].detached_posix_thread) |
| 322 | { |
| 323 | /* Once a detached thread has finished, its stack is deallocated and */ |
| 324 | /* should no longer be taken into account when computing the danger set*/ |
| 325 | s_threadinfo[tid].stack_min = s_threadinfo[tid].stack_max; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 326 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 327 | /* For a detached thread, calling pthread_exit() invalidates the */ |
| 328 | /* POSIX thread ID associated with the detached thread. For joinable */ |
| 329 | /* POSIX threads however, the POSIX thread ID remains live after the */ |
| 330 | /* pthread_exit() call until pthread_join() is called. */ |
| 331 | s_threadinfo[tid].posix_thread_exists = False; |
| 332 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void thread_set_pthreadid(const DrdThreadId tid, const PThreadId ptid) |
| 336 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 337 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 338 | && tid != DRD_INVALID_THREADID); |
| 339 | tl_assert(s_threadinfo[tid].pt_threadid == INVALID_POSIX_THREADID); |
| 340 | tl_assert(ptid != INVALID_POSIX_THREADID); |
| 341 | s_threadinfo[tid].posix_thread_exists = True; |
| 342 | s_threadinfo[tid].pt_threadid = ptid; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | Bool thread_get_joinable(const DrdThreadId tid) |
| 346 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 347 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 348 | && tid != DRD_INVALID_THREADID); |
| 349 | return ! s_threadinfo[tid].detached_posix_thread; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void thread_set_joinable(const DrdThreadId tid, const Bool joinable) |
| 353 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 354 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 355 | && tid != DRD_INVALID_THREADID); |
| 356 | tl_assert(!! joinable == joinable); |
| 357 | tl_assert(s_threadinfo[tid].pt_threadid != INVALID_POSIX_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 358 | #if 0 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 359 | VG_(message)(Vg_DebugMsg, |
| 360 | "thread_set_joinable(%d/%d, %s)", |
| 361 | tid, |
| 362 | s_threadinfo[tid].vg_threadid, |
| 363 | joinable ? "joinable" : "detached"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 364 | #endif |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 365 | s_threadinfo[tid].detached_posix_thread = ! joinable; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 366 | } |
| 367 | |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 368 | void thread_set_vg_running_tid(const ThreadId vg_tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 369 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 370 | tl_assert(vg_tid != VG_INVALID_THREADID); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 371 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 372 | if (vg_tid != s_vg_running_tid) |
| 373 | { |
| 374 | thread_set_running_tid(vg_tid, VgThreadIdToDrdThreadId(vg_tid)); |
| 375 | } |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 376 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 377 | tl_assert(s_vg_running_tid != VG_INVALID_THREADID); |
| 378 | tl_assert(s_drd_running_tid != DRD_INVALID_THREADID); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | void thread_set_running_tid(const ThreadId vg_tid, const DrdThreadId drd_tid) |
| 382 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 383 | tl_assert(vg_tid != VG_INVALID_THREADID); |
| 384 | tl_assert(drd_tid != DRD_INVALID_THREADID); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 385 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 386 | if (vg_tid != s_vg_running_tid) |
| 387 | { |
| 388 | if (s_trace_context_switches |
| 389 | && s_drd_running_tid != DRD_INVALID_THREADID) |
| 390 | { |
| 391 | VG_(message)(Vg_DebugMsg, |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 392 | "Context switch from thread %d/%d to thread %d/%d;" |
| 393 | " segments: %llu", |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 394 | s_vg_running_tid, s_drd_running_tid, |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 395 | DrdThreadIdToVgThreadId(drd_tid), drd_tid, |
| 396 | sg_get_alive_segments_count()); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 397 | } |
| 398 | s_vg_running_tid = vg_tid; |
| 399 | s_drd_running_tid = drd_tid; |
bart | 4af812e | 2008-04-13 15:39:38 +0000 | [diff] [blame] | 400 | thread_compute_danger_set(&s_danger_set, drd_tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 401 | s_context_switch_count++; |
| 402 | } |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 403 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 404 | tl_assert(s_vg_running_tid != VG_INVALID_THREADID); |
| 405 | tl_assert(s_drd_running_tid != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 406 | } |
| 407 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 408 | int thread_enter_synchr(const DrdThreadId tid) |
| 409 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 410 | tl_assert(IsValidDrdThreadId(tid)); |
| 411 | return s_threadinfo[tid].synchr_nesting++; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | int thread_leave_synchr(const DrdThreadId tid) |
| 415 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 416 | tl_assert(IsValidDrdThreadId(tid)); |
| 417 | tl_assert(s_threadinfo[tid].synchr_nesting >= 1); |
| 418 | return --s_threadinfo[tid].synchr_nesting; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | int thread_get_synchr_nesting_count(const DrdThreadId tid) |
| 422 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 423 | tl_assert(IsValidDrdThreadId(tid)); |
| 424 | return s_threadinfo[tid].synchr_nesting; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 425 | } |
| 426 | |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 427 | /** Append a new segment at the end of the segment list. */ |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 428 | static void thread_append_segment(const DrdThreadId tid, Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 429 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 430 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 431 | && tid != DRD_INVALID_THREADID); |
bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 432 | // tl_assert(sane_ThreadInfo(&s_threadinfo[tid])); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 433 | sg->prev = s_threadinfo[tid].last; |
| 434 | sg->next = 0; |
| 435 | if (s_threadinfo[tid].last) |
| 436 | s_threadinfo[tid].last->next = sg; |
| 437 | s_threadinfo[tid].last = sg; |
| 438 | if (s_threadinfo[tid].first == 0) |
| 439 | s_threadinfo[tid].first = sg; |
bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 440 | // tl_assert(sane_ThreadInfo(&s_threadinfo[tid])); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 441 | } |
| 442 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 443 | /** Remove a segment from the segment list of thread threadid, and free the |
| 444 | * associated memory. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 445 | */ |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 446 | static void thread_discard_segment(const DrdThreadId tid, Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 447 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 448 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 449 | && tid != DRD_INVALID_THREADID); |
bart | 3f74967 | 2008-03-22 09:49:40 +0000 | [diff] [blame] | 450 | //tl_assert(sane_ThreadInfo(&s_threadinfo[tid])); |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 451 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 452 | if (sg->prev) |
| 453 | sg->prev->next = sg->next; |
| 454 | if (sg->next) |
| 455 | sg->next->prev = sg->prev; |
| 456 | if (sg == s_threadinfo[tid].first) |
| 457 | s_threadinfo[tid].first = sg->next; |
| 458 | if (sg == s_threadinfo[tid].last) |
| 459 | s_threadinfo[tid].last = sg->prev; |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 460 | sg_put(sg); |
bart | 3f74967 | 2008-03-22 09:49:40 +0000 | [diff] [blame] | 461 | |
| 462 | //tl_assert(sane_ThreadInfo(&s_threadinfo[tid])); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | VectorClock* thread_get_vc(const DrdThreadId tid) |
| 466 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 467 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 468 | && tid != DRD_INVALID_THREADID); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 469 | tl_assert(s_threadinfo[tid].last); |
| 470 | return &s_threadinfo[tid].last->vc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 471 | } |
| 472 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 473 | /** Return the latest segment of thread 'tid' and increment its reference |
| 474 | * count. |
| 475 | */ |
| 476 | void thread_get_latest_segment(Segment** sg, const DrdThreadId tid) |
| 477 | { |
| 478 | tl_assert(sg); |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 479 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 480 | && tid != DRD_INVALID_THREADID); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 481 | tl_assert(s_threadinfo[tid].last); |
| 482 | |
| 483 | sg_put(*sg); |
| 484 | *sg = sg_get(s_threadinfo[tid].last); |
| 485 | } |
| 486 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 487 | /** |
| 488 | * Compute the minimum of all latest vector clocks of all threads |
| 489 | * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA). |
| 490 | * @param vc pointer to a vectorclock, holds result upon return. |
| 491 | */ |
| 492 | static void thread_compute_minimum_vc(VectorClock* vc) |
| 493 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 494 | unsigned i; |
| 495 | Bool first; |
| 496 | Segment* latest_sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 497 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 498 | first = True; |
| 499 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 500 | { |
| 501 | latest_sg = s_threadinfo[i].last; |
| 502 | if (latest_sg) |
| 503 | { |
| 504 | if (first) |
| 505 | vc_assign(vc, &latest_sg->vc); |
| 506 | else |
| 507 | vc_min(vc, &latest_sg->vc); |
| 508 | first = False; |
| 509 | } |
| 510 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static void thread_compute_maximum_vc(VectorClock* vc) |
| 514 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 515 | unsigned i; |
| 516 | Bool first; |
| 517 | Segment* latest_sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 518 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 519 | first = True; |
| 520 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 521 | { |
| 522 | latest_sg = s_threadinfo[i].last; |
| 523 | if (latest_sg) |
| 524 | { |
| 525 | if (first) |
| 526 | vc_assign(vc, &latest_sg->vc); |
| 527 | else |
| 528 | vc_combine(vc, &latest_sg->vc); |
| 529 | first = False; |
| 530 | } |
| 531 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /** |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 535 | * Discard all segments that have a defined order against the latest vector |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 536 | * clock of every thread -- these segments can no longer be involved in a |
| 537 | * data race. |
| 538 | */ |
| 539 | static void thread_discard_ordered_segments(void) |
| 540 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 541 | unsigned i; |
| 542 | VectorClock thread_vc_min; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 543 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 544 | s_discard_ordered_segments_count++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 545 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 546 | vc_init(&thread_vc_min, 0, 0); |
| 547 | thread_compute_minimum_vc(&thread_vc_min); |
| 548 | if (sg_get_trace()) |
| 549 | { |
| 550 | char msg[256]; |
| 551 | VectorClock thread_vc_max; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 552 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 553 | vc_init(&thread_vc_max, 0, 0); |
| 554 | thread_compute_maximum_vc(&thread_vc_max); |
| 555 | VG_(snprintf)(msg, sizeof(msg), |
| 556 | "Discarding ordered segments -- min vc is "); |
| 557 | vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 558 | &thread_vc_min); |
| 559 | VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 560 | ", max vc is "); |
| 561 | vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 562 | &thread_vc_max); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 563 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 564 | vc_cleanup(&thread_vc_max); |
| 565 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 566 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 567 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 568 | { |
| 569 | Segment* sg; |
| 570 | Segment* sg_next; |
| 571 | for (sg = s_threadinfo[i].first; |
| 572 | sg && (sg_next = sg->next) && vc_lte(&sg->vc, &thread_vc_min); |
| 573 | sg = sg_next) |
| 574 | { |
| 575 | thread_discard_segment(i, sg); |
| 576 | } |
| 577 | } |
| 578 | vc_cleanup(&thread_vc_min); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 579 | } |
| 580 | |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 581 | /** Merge all segments that may be merged without triggering false positives |
| 582 | * or discarding real data races. For the theoretical background of segment |
| 583 | * merging, see also the following paper: |
| 584 | * Mark Christiaens, Michiel Ronsse and Koen De Bosschere. |
| 585 | * Bounding the number of segment histories during data race detection. |
| 586 | * Parallel Computing archive, Volume 28, Issue 9, pp 1221-1238, |
| 587 | * September 2002. |
| 588 | */ |
| 589 | static void thread_merge_segments(void) |
| 590 | { |
| 591 | unsigned i; |
| 592 | |
| 593 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 594 | { |
| 595 | Segment* sg; |
| 596 | |
bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 597 | // tl_assert(sane_ThreadInfo(&s_threadinfo[i])); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 598 | |
| 599 | for (sg = s_threadinfo[i].first; sg; sg = sg->next) |
| 600 | { |
| 601 | if (sg_get_refcnt(sg) == 1 |
| 602 | && sg->next |
| 603 | && sg_get_refcnt(sg->next) == 1 |
| 604 | && sg->next->next) |
| 605 | { |
| 606 | /* Merge sg and sg->next into sg. */ |
| 607 | sg_merge(sg, sg->next); |
| 608 | thread_discard_segment(i, sg->next); |
| 609 | } |
| 610 | } |
| 611 | |
bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 612 | // tl_assert(sane_ThreadInfo(&s_threadinfo[i])); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 616 | /** Every change in the vector clock of a thread may cause segments that |
| 617 | * were previously ordered to this thread to become unordered. Hence, |
| 618 | * it may be necessary to recalculate the danger set if the vector clock |
| 619 | * of the current thread is updated. This function check whether such a |
| 620 | * recalculation is necessary. |
| 621 | * |
| 622 | * @param tid Thread ID of the thread to which a new segment has been |
| 623 | * appended. |
| 624 | * @param new_sg Pointer to the most recent segment of thread tid. |
| 625 | */ |
| 626 | static Bool danger_set_update_needed(const DrdThreadId tid, |
| 627 | const Segment* const new_sg) |
| 628 | { |
bart | 5d421ba | 2008-04-19 15:15:12 +0000 | [diff] [blame] | 629 | #if 0 |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 630 | unsigned i; |
| 631 | const Segment* old_sg; |
| 632 | |
| 633 | tl_assert(new_sg); |
| 634 | |
| 635 | /* If a new segment was added to another thread than the running thread, */ |
| 636 | /* just tell the caller to update the danger set. */ |
| 637 | if (tid != s_drd_running_tid) |
| 638 | return True; |
| 639 | |
| 640 | /* Always let the caller update the danger set after creation of the */ |
| 641 | /* first segment. */ |
| 642 | old_sg = new_sg->prev; |
| 643 | if (old_sg == 0) |
| 644 | return True; |
| 645 | |
| 646 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 647 | { |
| 648 | Segment* q; |
| 649 | |
| 650 | if (i == s_drd_running_tid) |
| 651 | continue; |
| 652 | |
| 653 | for (q = s_threadinfo[i].last; q; q = q->prev) |
| 654 | { |
| 655 | /* If the expression below evaluates to false, this expression will */ |
| 656 | /* also evaluate to false for all subsequent iterations. So stop */ |
| 657 | /* iterating. */ |
| 658 | if (vc_lte(&q->vc, &old_sg->vc)) |
| 659 | break; |
| 660 | /* If the vector clock of the 2nd the last segment is not ordered */ |
| 661 | /* to the vector clock of segment q, and the last segment is, ask */ |
| 662 | /* the caller to update the danger set. */ |
| 663 | if (! vc_lte(&old_sg->vc, &q->vc)) |
| 664 | { |
| 665 | return True; |
| 666 | } |
| 667 | /* If the vector clock of the last segment is not ordered to the */ |
| 668 | /* vector clock of segment q, ask the caller to update the danger */ |
| 669 | /* set. */ |
| 670 | if (! vc_lte(&q->vc, &new_sg->vc) && ! vc_lte(&new_sg->vc, &q->vc)) |
| 671 | { |
| 672 | return True; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | return False; |
bart | 5d421ba | 2008-04-19 15:15:12 +0000 | [diff] [blame] | 678 | #else |
| 679 | return True; |
| 680 | #endif |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 681 | } |
| 682 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 683 | /** Create a new segment for the specified thread, and discard any segments |
| 684 | * that cannot cause races anymore. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 685 | */ |
| 686 | void thread_new_segment(const DrdThreadId tid) |
| 687 | { |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 688 | Segment* new_sg; |
| 689 | |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 690 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 691 | && tid != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 692 | |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 693 | new_sg = sg_new(tid, tid); |
| 694 | thread_append_segment(tid, new_sg); |
| 695 | |
| 696 | if (danger_set_update_needed(tid, new_sg)) |
| 697 | { |
bart | 4af812e | 2008-04-13 15:39:38 +0000 | [diff] [blame] | 698 | thread_compute_danger_set(&s_danger_set, s_drd_running_tid); |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 699 | s_danger_set_new_segment_count++; |
| 700 | } |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 701 | else if (tid == s_drd_running_tid) |
| 702 | { |
| 703 | tl_assert(thread_danger_set_up_to_date(s_drd_running_tid)); |
| 704 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 705 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 706 | thread_discard_ordered_segments(); |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 707 | |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 708 | if (s_segment_merging) |
| 709 | thread_merge_segments(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 710 | } |
| 711 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 712 | /** Call this function after thread 'joiner' joined thread 'joinee'. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 713 | void thread_combine_vc(DrdThreadId joiner, DrdThreadId joinee) |
| 714 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 715 | tl_assert(joiner != joinee); |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 716 | tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 717 | && joiner != DRD_INVALID_THREADID); |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 718 | tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 719 | && joinee != DRD_INVALID_THREADID); |
| 720 | tl_assert(s_threadinfo[joiner].last); |
| 721 | tl_assert(s_threadinfo[joinee].last); |
| 722 | vc_combine(&s_threadinfo[joiner].last->vc, &s_threadinfo[joinee].last->vc); |
| 723 | thread_discard_ordered_segments(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 724 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 725 | if (joiner == s_drd_running_tid) |
| 726 | { |
bart | 4af812e | 2008-04-13 15:39:38 +0000 | [diff] [blame] | 727 | thread_compute_danger_set(&s_danger_set, joiner); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 728 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 729 | } |
| 730 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 731 | /** Call this function after thread 'tid' had to wait because of thread |
| 732 | * synchronization until the memory accesses in the segment with vector clock |
| 733 | * 'vc' finished. |
| 734 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 735 | void thread_combine_vc2(DrdThreadId tid, const VectorClock* const vc) |
| 736 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 737 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 738 | && tid != DRD_INVALID_THREADID); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 739 | tl_assert(s_threadinfo[tid].last); |
| 740 | tl_assert(vc); |
| 741 | vc_combine(&s_threadinfo[tid].last->vc, vc); |
bart | 4af812e | 2008-04-13 15:39:38 +0000 | [diff] [blame] | 742 | thread_compute_danger_set(&s_danger_set, tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 743 | thread_discard_ordered_segments(); |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 744 | s_danger_set_combine_vc_count++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 745 | } |
| 746 | |
bart | dfbae6e | 2008-06-05 08:29:53 +0000 | [diff] [blame] | 747 | Bool bm_access_load_1_triggers_conflict(const Addr a1) |
| 748 | { |
| 749 | bm_access_load_1(running_thread_get_segment()->bm, a1); |
| 750 | return bm_load_1_has_conflict_with(thread_get_danger_set(), a1); |
| 751 | } |
| 752 | |
| 753 | Bool bm_access_load_2_triggers_conflict(const Addr a1) |
| 754 | { |
bart | 32ba208 | 2008-06-05 08:53:42 +0000 | [diff] [blame^] | 755 | if ((a1 & 1) == 0) |
| 756 | bm_access_aligned_load(running_thread_get_segment()->bm, a1, 2); |
| 757 | else |
| 758 | bm_access_range(running_thread_get_segment()->bm, a1, a1 + 2, eLoad); |
| 759 | if ((a1 & 1) == 0) |
| 760 | return bm_aligned_load_has_conflict_with(thread_get_danger_set(), a1, 2); |
| 761 | else |
| 762 | return bm_has_conflict_with(thread_get_danger_set(), a1, a1 + 2, eLoad); |
bart | dfbae6e | 2008-06-05 08:29:53 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | Bool bm_access_load_4_triggers_conflict(const Addr a1) |
| 766 | { |
bart | 32ba208 | 2008-06-05 08:53:42 +0000 | [diff] [blame^] | 767 | if ((a1 & 3) == 0) |
| 768 | bm_access_aligned_load(running_thread_get_segment()->bm, a1, 4); |
| 769 | else |
| 770 | bm_access_range(running_thread_get_segment()->bm, a1, a1 + 4, eLoad); |
| 771 | if ((a1 & 3) == 0) |
| 772 | return bm_aligned_load_has_conflict_with(thread_get_danger_set(), a1, 4); |
| 773 | else |
| 774 | return bm_has_conflict_with(thread_get_danger_set(), a1, a1 + 4, eLoad); |
bart | dfbae6e | 2008-06-05 08:29:53 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | Bool bm_access_load_8_triggers_conflict(const Addr a1) |
| 778 | { |
bart | 32ba208 | 2008-06-05 08:53:42 +0000 | [diff] [blame^] | 779 | if ((a1 & 7) == 0) |
| 780 | bm_access_aligned_load(running_thread_get_segment()->bm, a1, 8); |
| 781 | else if ((a1 & 3) == 0) |
| 782 | { |
| 783 | bm_access_aligned_load(running_thread_get_segment()->bm, a1 + 0, 4); |
| 784 | bm_access_aligned_load(running_thread_get_segment()->bm, a1 + 4, 4); |
| 785 | } |
| 786 | else |
| 787 | bm_access_range(running_thread_get_segment()->bm, a1, a1 + 8, eLoad); |
| 788 | if ((a1 & 7) == 0) |
| 789 | return bm_aligned_load_has_conflict_with(thread_get_danger_set(), a1, 8); |
| 790 | else |
| 791 | return bm_has_conflict_with(thread_get_danger_set(), a1, a1 + 8, eLoad); |
bart | dfbae6e | 2008-06-05 08:29:53 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | Bool bm_access_load_triggers_conflict(const Addr a1, const Addr a2) |
| 795 | { |
| 796 | bm_access_range_load(running_thread_get_segment()->bm, a1, a2); |
| 797 | return bm_load_has_conflict_with(thread_get_danger_set(), a1, a2); |
| 798 | } |
| 799 | |
| 800 | Bool bm_access_store_1_triggers_conflict(const Addr a1) |
| 801 | { |
| 802 | bm_access_store_1(running_thread_get_segment()->bm, a1); |
| 803 | return bm_store_1_has_conflict_with(thread_get_danger_set(), a1); |
| 804 | } |
| 805 | |
| 806 | Bool bm_access_store_2_triggers_conflict(const Addr a1) |
| 807 | { |
bart | 32ba208 | 2008-06-05 08:53:42 +0000 | [diff] [blame^] | 808 | if ((a1 & 1) == 0) |
| 809 | bm_access_aligned_store(running_thread_get_segment()->bm, a1, 2); |
| 810 | else |
| 811 | bm_access_range(running_thread_get_segment()->bm, a1, a1 + 2, eStore); |
| 812 | if ((a1 & 1) == 0) |
| 813 | return bm_aligned_store_has_conflict_with(thread_get_danger_set(), a1, 2); |
| 814 | else |
| 815 | return bm_has_conflict_with(thread_get_danger_set(), a1, a1 + 2, eStore); |
bart | dfbae6e | 2008-06-05 08:29:53 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | Bool bm_access_store_4_triggers_conflict(const Addr a1) |
| 819 | { |
bart | 32ba208 | 2008-06-05 08:53:42 +0000 | [diff] [blame^] | 820 | if ((a1 & 3) == 0) |
| 821 | bm_access_aligned_store(running_thread_get_segment()->bm, a1, 4); |
| 822 | else |
| 823 | bm_access_range(running_thread_get_segment()->bm, a1, a1 + 4, eStore); |
| 824 | if ((a1 & 3) == 0) |
| 825 | return bm_aligned_store_has_conflict_with(thread_get_danger_set(), a1, 4); |
| 826 | else |
| 827 | return bm_has_conflict_with(thread_get_danger_set(), a1, a1 + 4, eStore); |
bart | dfbae6e | 2008-06-05 08:29:53 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | Bool bm_access_store_8_triggers_conflict(const Addr a1) |
| 831 | { |
bart | 32ba208 | 2008-06-05 08:53:42 +0000 | [diff] [blame^] | 832 | if ((a1 & 7) == 0) |
| 833 | bm_access_aligned_store(running_thread_get_segment()->bm, a1, 8); |
| 834 | else if ((a1 & 3) == 0) |
| 835 | { |
| 836 | bm_access_aligned_store(running_thread_get_segment()->bm, a1 + 0, 4); |
| 837 | bm_access_aligned_store(running_thread_get_segment()->bm, a1 + 4, 4); |
| 838 | } |
| 839 | else |
| 840 | bm_access_range(running_thread_get_segment()->bm, a1, a1 + 8, eStore); |
| 841 | if ((a1 & 7) == 0) |
| 842 | return bm_aligned_store_has_conflict_with(thread_get_danger_set(), a1, 8); |
| 843 | else |
| 844 | return bm_has_conflict_with(thread_get_danger_set(), a1, a1 + 8, eStore); |
bart | dfbae6e | 2008-06-05 08:29:53 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | Bool bm_access_store_triggers_conflict(const Addr a1, const Addr a2) |
| 848 | { |
| 849 | bm_access_range_store(running_thread_get_segment()->bm, a1, a2); |
| 850 | return bm_store_has_conflict_with(thread_get_danger_set(), a1, a2); |
| 851 | } |
| 852 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 853 | /** Call this function whenever a thread is no longer using the memory |
| 854 | * [ a1, a2 [, e.g. because of a call to free() or a stack pointer |
| 855 | * increase. |
| 856 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 857 | void thread_stop_using_mem(const Addr a1, const Addr a2) |
| 858 | { |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 859 | DrdThreadId other_user; |
| 860 | unsigned i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 861 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 862 | /* For all threads, mark the range [ a1, a2 [ as no longer in use. */ |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 863 | other_user = DRD_INVALID_THREADID; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 864 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 865 | { |
| 866 | Segment* p; |
| 867 | for (p = s_threadinfo[i].first; p; p = p->next) |
| 868 | { |
| 869 | if (other_user == DRD_INVALID_THREADID |
bart | 8bf2f8b | 2008-03-30 17:56:43 +0000 | [diff] [blame] | 870 | && i != s_drd_running_tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 871 | { |
bart | 8bf2f8b | 2008-03-30 17:56:43 +0000 | [diff] [blame] | 872 | if (UNLIKELY(bm_test_and_clear(p->bm, a1, a2))) |
| 873 | { |
| 874 | other_user = i; |
| 875 | } |
| 876 | continue; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 877 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 878 | bm_clear(p->bm, a1, a2); |
| 879 | } |
| 880 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 881 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 882 | /* If any other thread had accessed memory in [ a1, a2 [, update the */ |
| 883 | /* danger set. */ |
| 884 | if (other_user != DRD_INVALID_THREADID |
| 885 | && bm_has_any_access(s_danger_set, a1, a2)) |
| 886 | { |
bart | 4af812e | 2008-04-13 15:39:38 +0000 | [diff] [blame] | 887 | thread_compute_danger_set(&s_danger_set, thread_get_running_tid()); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 888 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 889 | } |
| 890 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 891 | void thread_start_recording(const DrdThreadId tid) |
| 892 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 893 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 894 | && tid != DRD_INVALID_THREADID); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 895 | tl_assert(! s_threadinfo[tid].is_recording); |
| 896 | s_threadinfo[tid].is_recording = True; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | void thread_stop_recording(const DrdThreadId tid) |
| 900 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 901 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 902 | && tid != DRD_INVALID_THREADID); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 903 | tl_assert(s_threadinfo[tid].is_recording); |
| 904 | s_threadinfo[tid].is_recording = False; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 905 | } |
| 906 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 907 | void thread_print_all(void) |
| 908 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 909 | unsigned i; |
| 910 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 911 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 912 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 913 | { |
| 914 | if (s_threadinfo[i].first) |
| 915 | { |
| 916 | VG_(printf)("**************\n" |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 917 | "* thread %3d (%d/%d/%d/0x%lx/%d) *\n" |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 918 | "**************\n", |
| 919 | i, |
| 920 | s_threadinfo[i].vg_thread_exists, |
| 921 | s_threadinfo[i].vg_threadid, |
| 922 | s_threadinfo[i].posix_thread_exists, |
| 923 | s_threadinfo[i].pt_threadid, |
bart | 354009c | 2008-03-16 10:42:33 +0000 | [diff] [blame] | 924 | s_threadinfo[i].detached_posix_thread); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 925 | for (p = s_threadinfo[i].first; p; p = p->next) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 926 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 927 | sg_print(p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 928 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 929 | } |
| 930 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | static void show_call_stack(const DrdThreadId tid, |
| 934 | const Char* const msg, |
| 935 | ExeContext* const callstack) |
| 936 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 937 | const ThreadId vg_tid = DrdThreadIdToVgThreadId(tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 938 | |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 939 | VG_(message)(Vg_UserMsg, "%s (thread %d/%d)", msg, vg_tid, tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 940 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 941 | if (vg_tid != VG_INVALID_THREADID) |
| 942 | { |
| 943 | if (callstack) |
| 944 | { |
| 945 | VG_(pp_ExeContext)(callstack); |
| 946 | } |
| 947 | else |
| 948 | { |
| 949 | VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size)); |
| 950 | } |
| 951 | } |
| 952 | else |
| 953 | { |
| 954 | VG_(message)(Vg_UserMsg, |
| 955 | " (thread finished, call stack no longer available)"); |
| 956 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 957 | } |
| 958 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 959 | static void |
| 960 | thread_report_conflicting_segments_segment(const DrdThreadId tid, |
| 961 | const Addr addr, |
| 962 | const SizeT size, |
| 963 | const BmAccessTypeT access_type, |
| 964 | const Segment* const p) |
| 965 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 966 | unsigned i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 967 | |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 968 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 969 | && tid != DRD_INVALID_THREADID); |
| 970 | tl_assert(p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 971 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 972 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 973 | { |
| 974 | if (i != tid) |
| 975 | { |
| 976 | Segment* q; |
| 977 | for (q = s_threadinfo[i].last; q; q = q->prev) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 978 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 979 | // Since q iterates over the segments of thread i in order of |
| 980 | // decreasing vector clocks, if q->vc <= p->vc, then |
| 981 | // q->next->vc <= p->vc will also hold. Hence, break out of the |
| 982 | // loop once this condition is met. |
| 983 | if (vc_lte(&q->vc, &p->vc)) |
| 984 | break; |
| 985 | if (! vc_lte(&p->vc, &q->vc)) |
| 986 | { |
| 987 | if (bm_has_conflict_with(q->bm, addr, addr + size, access_type)) |
| 988 | { |
| 989 | tl_assert(q->stacktrace); |
| 990 | show_call_stack(i, "Other segment start", |
| 991 | q->stacktrace); |
| 992 | show_call_stack(i, "Other segment end", |
| 993 | q->next ? q->next->stacktrace : 0); |
| 994 | } |
| 995 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 996 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 997 | } |
| 998 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | void thread_report_conflicting_segments(const DrdThreadId tid, |
| 1002 | const Addr addr, |
| 1003 | const SizeT size, |
| 1004 | const BmAccessTypeT access_type) |
| 1005 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1006 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1007 | |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 1008 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1009 | && tid != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1010 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1011 | for (p = s_threadinfo[tid].first; p; p = p->next) |
| 1012 | { |
| 1013 | if (bm_has(p->bm, addr, addr + size, access_type)) |
| 1014 | { |
| 1015 | thread_report_conflicting_segments_segment(tid, addr, size, |
| 1016 | access_type, p); |
| 1017 | } |
| 1018 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1019 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1020 | |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1021 | /** Verify whether the danger set for thread tid is up to date. Only perform |
| 1022 | * the check if the environment variable DRD_VERIFY_DANGER_SET has been set. |
| 1023 | */ |
| 1024 | static Bool thread_danger_set_up_to_date(const DrdThreadId tid) |
| 1025 | { |
| 1026 | static int do_verify_danger_set = -1; |
| 1027 | Bool result; |
| 1028 | struct bitmap* computed_danger_set = 0; |
| 1029 | |
| 1030 | if (do_verify_danger_set < 0) |
| 1031 | { |
| 1032 | //VG_(message)(Vg_DebugMsg, "%s", VG_(getenv)("DRD_VERIFY_DANGER_SET")); |
| 1033 | do_verify_danger_set = VG_(getenv)("DRD_VERIFY_DANGER_SET") != 0; |
| 1034 | } |
| 1035 | if (do_verify_danger_set == 0) |
| 1036 | return True; |
| 1037 | |
| 1038 | thread_compute_danger_set(&computed_danger_set, tid); |
bart | a3f6109 | 2008-05-04 07:46:20 +0000 | [diff] [blame] | 1039 | result = bm_equal(s_danger_set, computed_danger_set); |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1040 | bm_delete(computed_danger_set); |
| 1041 | return result; |
| 1042 | } |
| 1043 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1044 | /** Compute a bitmap that represents the union of all memory accesses of all |
| 1045 | * segments that are unordered to the current segment of the thread tid. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1046 | */ |
bart | 4af812e | 2008-04-13 15:39:38 +0000 | [diff] [blame] | 1047 | static void thread_compute_danger_set(struct bitmap** danger_set, |
| 1048 | const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1049 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1050 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1051 | |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 1052 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1053 | && tid != DRD_INVALID_THREADID); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1054 | tl_assert(tid == s_drd_running_tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1055 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1056 | s_update_danger_set_count++; |
| 1057 | s_danger_set_bitmap_creation_count -= bm_get_bitmap_creation_count(); |
| 1058 | s_danger_set_bitmap2_creation_count -= bm_get_bitmap2_creation_count(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1059 | |
bart | 4af812e | 2008-04-13 15:39:38 +0000 | [diff] [blame] | 1060 | if (*danger_set) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1061 | { |
bart | 4af812e | 2008-04-13 15:39:38 +0000 | [diff] [blame] | 1062 | bm_delete(*danger_set); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1063 | } |
bart | 4af812e | 2008-04-13 15:39:38 +0000 | [diff] [blame] | 1064 | *danger_set = bm_new(); |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1065 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1066 | if (s_trace_danger_set) |
| 1067 | { |
| 1068 | char msg[256]; |
| 1069 | |
| 1070 | VG_(snprintf)(msg, sizeof(msg), |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 1071 | "computing danger set for thread %d/%d with vc ", |
| 1072 | DrdThreadIdToVgThreadId(tid), tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1073 | vc_snprint(msg + VG_(strlen)(msg), |
| 1074 | sizeof(msg) - VG_(strlen)(msg), |
| 1075 | &s_threadinfo[tid].last->vc); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1076 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | p = s_threadinfo[tid].last; |
| 1080 | { |
| 1081 | unsigned j; |
| 1082 | |
| 1083 | if (s_trace_danger_set) |
| 1084 | { |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1085 | char msg[256]; |
| 1086 | |
| 1087 | VG_(snprintf)(msg, sizeof(msg), |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1088 | "danger set: thread [%d] at vc ", |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1089 | tid); |
| 1090 | vc_snprint(msg + VG_(strlen)(msg), |
| 1091 | sizeof(msg) - VG_(strlen)(msg), |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1092 | &p->vc); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1093 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1094 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1095 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1096 | for (j = 0; j < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); j++) |
| 1097 | { |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1098 | if (j != tid && IsValidDrdThreadId(j)) |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1099 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1100 | const Segment* q; |
| 1101 | for (q = s_threadinfo[j].last; q; q = q->prev) |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1102 | { |
| 1103 | if (! vc_lte(&q->vc, &p->vc) && ! vc_lte(&p->vc, &q->vc)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1104 | { |
| 1105 | if (s_trace_danger_set) |
| 1106 | { |
| 1107 | char msg[256]; |
| 1108 | VG_(snprintf)(msg, sizeof(msg), |
| 1109 | "danger set: [%d] merging segment ", j); |
| 1110 | vc_snprint(msg + VG_(strlen)(msg), |
| 1111 | sizeof(msg) - VG_(strlen)(msg), |
| 1112 | &q->vc); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1113 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1114 | } |
bart | 4af812e | 2008-04-13 15:39:38 +0000 | [diff] [blame] | 1115 | bm_merge2(*danger_set, q->bm); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1116 | } |
| 1117 | else |
| 1118 | { |
| 1119 | if (s_trace_danger_set) |
| 1120 | { |
| 1121 | char msg[256]; |
| 1122 | VG_(snprintf)(msg, sizeof(msg), |
| 1123 | "danger set: [%d] ignoring segment ", j); |
| 1124 | vc_snprint(msg + VG_(strlen)(msg), |
| 1125 | sizeof(msg) - VG_(strlen)(msg), |
| 1126 | &q->vc); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1127 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1128 | } |
| 1129 | } |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1130 | } |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1131 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1132 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1133 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1134 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1135 | s_danger_set_bitmap_creation_count += bm_get_bitmap_creation_count(); |
| 1136 | s_danger_set_bitmap2_creation_count += bm_get_bitmap2_creation_count(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1137 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1138 | if (0 && s_trace_danger_set) |
| 1139 | { |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1140 | VG_(message)(Vg_UserMsg, "[%d] new danger set:", tid); |
bart | 4af812e | 2008-04-13 15:39:38 +0000 | [diff] [blame] | 1141 | bm_print(*danger_set); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1142 | VG_(message)(Vg_UserMsg, "[%d] end of new danger set.", tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1143 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1146 | ULong thread_get_context_switch_count(void) |
| 1147 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1148 | return s_context_switch_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1151 | ULong thread_get_discard_ordered_segments_count(void) |
| 1152 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1153 | return s_discard_ordered_segments_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1156 | ULong thread_get_update_danger_set_count(ULong* dsnsc, ULong* dscvc) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1157 | { |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1158 | tl_assert(dsnsc); |
| 1159 | tl_assert(dscvc); |
| 1160 | *dsnsc = s_danger_set_new_segment_count; |
| 1161 | *dscvc = s_danger_set_combine_vc_count; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1162 | return s_update_danger_set_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | ULong thread_get_danger_set_bitmap_creation_count(void) |
| 1166 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1167 | return s_danger_set_bitmap_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | ULong thread_get_danger_set_bitmap2_creation_count(void) |
| 1171 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1172 | return s_danger_set_bitmap2_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1173 | } |