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