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