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