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); |
bart | 3f74967 | 2008-03-22 09:49:40 +0000 | [diff] [blame^] | 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 | 3f74967 | 2008-03-22 09:49:40 +0000 | [diff] [blame^] | 430 | |
| 431 | //tl_assert(sane_ThreadInfo(&s_threadinfo[tid])); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | VectorClock* thread_get_vc(const DrdThreadId tid) |
| 435 | { |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 436 | tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 437 | tl_assert(s_threadinfo[tid].last); |
| 438 | return &s_threadinfo[tid].last->vc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 439 | } |
| 440 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 441 | /** Return the latest segment of thread 'tid' and increment its reference |
| 442 | * count. |
| 443 | */ |
| 444 | void thread_get_latest_segment(Segment** sg, const DrdThreadId tid) |
| 445 | { |
| 446 | tl_assert(sg); |
| 447 | tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID); |
| 448 | tl_assert(s_threadinfo[tid].last); |
| 449 | |
| 450 | sg_put(*sg); |
| 451 | *sg = sg_get(s_threadinfo[tid].last); |
| 452 | } |
| 453 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 454 | /** |
| 455 | * Compute the minimum of all latest vector clocks of all threads |
| 456 | * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA). |
| 457 | * @param vc pointer to a vectorclock, holds result upon return. |
| 458 | */ |
| 459 | static void thread_compute_minimum_vc(VectorClock* vc) |
| 460 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 461 | unsigned i; |
| 462 | Bool first; |
| 463 | Segment* latest_sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 464 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 465 | first = True; |
| 466 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 467 | { |
| 468 | latest_sg = s_threadinfo[i].last; |
| 469 | if (latest_sg) |
| 470 | { |
| 471 | if (first) |
| 472 | vc_assign(vc, &latest_sg->vc); |
| 473 | else |
| 474 | vc_min(vc, &latest_sg->vc); |
| 475 | first = False; |
| 476 | } |
| 477 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | static void thread_compute_maximum_vc(VectorClock* vc) |
| 481 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 482 | unsigned i; |
| 483 | Bool first; |
| 484 | Segment* latest_sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 485 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 486 | first = True; |
| 487 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 488 | { |
| 489 | latest_sg = s_threadinfo[i].last; |
| 490 | if (latest_sg) |
| 491 | { |
| 492 | if (first) |
| 493 | vc_assign(vc, &latest_sg->vc); |
| 494 | else |
| 495 | vc_combine(vc, &latest_sg->vc); |
| 496 | first = False; |
| 497 | } |
| 498 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /** |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 502 | * Discard all segments that have a defined order against the latest vector |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 503 | * clock of every thread -- these segments can no longer be involved in a |
| 504 | * data race. |
| 505 | */ |
| 506 | static void thread_discard_ordered_segments(void) |
| 507 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 508 | unsigned i; |
| 509 | VectorClock thread_vc_min; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 510 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 511 | s_discard_ordered_segments_count++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 512 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 513 | vc_init(&thread_vc_min, 0, 0); |
| 514 | thread_compute_minimum_vc(&thread_vc_min); |
| 515 | if (sg_get_trace()) |
| 516 | { |
| 517 | char msg[256]; |
| 518 | VectorClock thread_vc_max; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 519 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 520 | vc_init(&thread_vc_max, 0, 0); |
| 521 | thread_compute_maximum_vc(&thread_vc_max); |
| 522 | VG_(snprintf)(msg, sizeof(msg), |
| 523 | "Discarding ordered segments -- min vc is "); |
| 524 | vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 525 | &thread_vc_min); |
| 526 | VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 527 | ", max vc is "); |
| 528 | vc_snprint(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 529 | &thread_vc_max); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 530 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 531 | vc_cleanup(&thread_vc_max); |
| 532 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 533 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 534 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 535 | { |
| 536 | Segment* sg; |
| 537 | Segment* sg_next; |
| 538 | for (sg = s_threadinfo[i].first; |
| 539 | sg && (sg_next = sg->next) && vc_lte(&sg->vc, &thread_vc_min); |
| 540 | sg = sg_next) |
| 541 | { |
| 542 | thread_discard_segment(i, sg); |
| 543 | } |
| 544 | } |
| 545 | vc_cleanup(&thread_vc_min); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 546 | } |
| 547 | |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 548 | /** Merge all segments that may be merged without triggering false positives |
| 549 | * or discarding real data races. For the theoretical background of segment |
| 550 | * merging, see also the following paper: |
| 551 | * Mark Christiaens, Michiel Ronsse and Koen De Bosschere. |
| 552 | * Bounding the number of segment histories during data race detection. |
| 553 | * Parallel Computing archive, Volume 28, Issue 9, pp 1221-1238, |
| 554 | * September 2002. |
| 555 | */ |
| 556 | static void thread_merge_segments(void) |
| 557 | { |
| 558 | unsigned i; |
| 559 | |
| 560 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 561 | { |
| 562 | Segment* sg; |
| 563 | |
| 564 | tl_assert(sane_ThreadInfo(&s_threadinfo[i])); |
| 565 | |
| 566 | for (sg = s_threadinfo[i].first; sg; sg = sg->next) |
| 567 | { |
| 568 | if (sg_get_refcnt(sg) == 1 |
| 569 | && sg->next |
| 570 | && sg_get_refcnt(sg->next) == 1 |
| 571 | && sg->next->next) |
| 572 | { |
| 573 | /* Merge sg and sg->next into sg. */ |
| 574 | sg_merge(sg, sg->next); |
| 575 | thread_discard_segment(i, sg->next); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | tl_assert(sane_ThreadInfo(&s_threadinfo[i])); |
| 580 | } |
| 581 | } |
| 582 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 583 | /** Create a new segment for the specified thread, and discard any segments |
| 584 | * that cannot cause races anymore. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 585 | */ |
| 586 | void thread_new_segment(const DrdThreadId tid) |
| 587 | { |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 588 | tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 589 | |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 590 | thread_append_segment(tid, sg_new(tid, tid)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 591 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 592 | thread_discard_ordered_segments(); |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 593 | |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 594 | if (s_segment_merging) |
| 595 | thread_merge_segments(); |
| 596 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 597 | if (tid == s_drd_running_tid) |
| 598 | { |
| 599 | /* Every change in the vector clock of the current thread may cause */ |
| 600 | /* segments that were previously ordered to this thread to become */ |
| 601 | /* unordered. Hence, recalculate the danger set if the vector clock */ |
| 602 | /* of the current thread is updated. */ |
| 603 | thread_update_danger_set(tid); |
| 604 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 605 | } |
| 606 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 607 | /** Call this function after thread 'joiner' joined thread 'joinee'. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 608 | void thread_combine_vc(DrdThreadId joiner, DrdThreadId joinee) |
| 609 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 610 | tl_assert(joiner != joinee); |
| 611 | tl_assert(0 <= joiner && joiner < DRD_N_THREADS |
| 612 | && joiner != DRD_INVALID_THREADID); |
| 613 | tl_assert(0 <= joinee && joinee < DRD_N_THREADS |
| 614 | && joinee != DRD_INVALID_THREADID); |
| 615 | tl_assert(s_threadinfo[joiner].last); |
| 616 | tl_assert(s_threadinfo[joinee].last); |
| 617 | vc_combine(&s_threadinfo[joiner].last->vc, &s_threadinfo[joinee].last->vc); |
| 618 | thread_discard_ordered_segments(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 619 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 620 | if (joiner == s_drd_running_tid) |
| 621 | { |
| 622 | thread_update_danger_set(joiner); |
| 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 'tid' had to wait because of thread |
| 627 | * synchronization until the memory accesses in the segment with vector clock |
| 628 | * 'vc' finished. |
| 629 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 630 | void thread_combine_vc2(DrdThreadId tid, const VectorClock* const vc) |
| 631 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 632 | tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID); |
| 633 | tl_assert(s_threadinfo[tid].last); |
| 634 | tl_assert(vc); |
| 635 | vc_combine(&s_threadinfo[tid].last->vc, vc); |
| 636 | thread_discard_ordered_segments(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 637 | } |
| 638 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 639 | /** Call this function whenever a thread is no longer using the memory |
| 640 | * [ a1, a2 [, e.g. because of a call to free() or a stack pointer |
| 641 | * increase. |
| 642 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 643 | void thread_stop_using_mem(const Addr a1, const Addr a2) |
| 644 | { |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 645 | DrdThreadId other_user; |
| 646 | unsigned i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 647 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 648 | /* For all threads, mark the range [ a1, a2 [ as no longer in use. */ |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 649 | other_user = DRD_INVALID_THREADID; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 650 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 651 | { |
| 652 | Segment* p; |
| 653 | for (p = s_threadinfo[i].first; p; p = p->next) |
| 654 | { |
| 655 | if (other_user == DRD_INVALID_THREADID |
| 656 | && i != s_drd_running_tid |
| 657 | && bm_has_any_access(p->bm, a1, a2)) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 658 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 659 | other_user = i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 660 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 661 | bm_clear(p->bm, a1, a2); |
| 662 | } |
| 663 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 664 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 665 | /* If any other thread had accessed memory in [ a1, a2 [, update the */ |
| 666 | /* danger set. */ |
| 667 | if (other_user != DRD_INVALID_THREADID |
| 668 | && bm_has_any_access(s_danger_set, a1, a2)) |
| 669 | { |
| 670 | thread_update_danger_set(thread_get_running_tid()); |
| 671 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 672 | } |
| 673 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 674 | void thread_start_recording(const DrdThreadId tid) |
| 675 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 676 | tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID); |
| 677 | tl_assert(! s_threadinfo[tid].is_recording); |
| 678 | s_threadinfo[tid].is_recording = True; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | void thread_stop_recording(const DrdThreadId tid) |
| 682 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 683 | tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID); |
| 684 | tl_assert(s_threadinfo[tid].is_recording); |
| 685 | s_threadinfo[tid].is_recording = False; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 686 | } |
| 687 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 688 | void thread_print_all(void) |
| 689 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 690 | unsigned i; |
| 691 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 692 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 693 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 694 | { |
| 695 | if (s_threadinfo[i].first) |
| 696 | { |
| 697 | VG_(printf)("**************\n" |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 698 | "* thread %3d (%d/%d/%d/0x%lx/%d) *\n" |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 699 | "**************\n", |
| 700 | i, |
| 701 | s_threadinfo[i].vg_thread_exists, |
| 702 | s_threadinfo[i].vg_threadid, |
| 703 | s_threadinfo[i].posix_thread_exists, |
| 704 | s_threadinfo[i].pt_threadid, |
bart | 354009c | 2008-03-16 10:42:33 +0000 | [diff] [blame] | 705 | s_threadinfo[i].detached_posix_thread); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 706 | for (p = s_threadinfo[i].first; p; p = p->next) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 707 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 708 | sg_print(p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 709 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static void show_call_stack(const DrdThreadId tid, |
| 715 | const Char* const msg, |
| 716 | ExeContext* const callstack) |
| 717 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 718 | const ThreadId vg_tid = DrdThreadIdToVgThreadId(tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 719 | |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 720 | VG_(message)(Vg_UserMsg, "%s (thread %d/%d)", msg, vg_tid, tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 721 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 722 | if (vg_tid != VG_INVALID_THREADID) |
| 723 | { |
| 724 | if (callstack) |
| 725 | { |
| 726 | VG_(pp_ExeContext)(callstack); |
| 727 | } |
| 728 | else |
| 729 | { |
| 730 | VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size)); |
| 731 | } |
| 732 | } |
| 733 | else |
| 734 | { |
| 735 | VG_(message)(Vg_UserMsg, |
| 736 | " (thread finished, call stack no longer available)"); |
| 737 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 738 | } |
| 739 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 740 | static void |
| 741 | thread_report_conflicting_segments_segment(const DrdThreadId tid, |
| 742 | const Addr addr, |
| 743 | const SizeT size, |
| 744 | const BmAccessTypeT access_type, |
| 745 | const Segment* const p) |
| 746 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 747 | unsigned i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 748 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 749 | tl_assert(0 <= tid && tid < DRD_N_THREADS |
| 750 | && tid != DRD_INVALID_THREADID); |
| 751 | tl_assert(p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 752 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 753 | for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++) |
| 754 | { |
| 755 | if (i != tid) |
| 756 | { |
| 757 | Segment* q; |
| 758 | for (q = s_threadinfo[i].last; q; q = q->prev) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 759 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 760 | // Since q iterates over the segments of thread i in order of |
| 761 | // decreasing vector clocks, if q->vc <= p->vc, then |
| 762 | // q->next->vc <= p->vc will also hold. Hence, break out of the |
| 763 | // loop once this condition is met. |
| 764 | if (vc_lte(&q->vc, &p->vc)) |
| 765 | break; |
| 766 | if (! vc_lte(&p->vc, &q->vc)) |
| 767 | { |
| 768 | if (bm_has_conflict_with(q->bm, addr, addr + size, access_type)) |
| 769 | { |
| 770 | tl_assert(q->stacktrace); |
| 771 | show_call_stack(i, "Other segment start", |
| 772 | q->stacktrace); |
| 773 | show_call_stack(i, "Other segment end", |
| 774 | q->next ? q->next->stacktrace : 0); |
| 775 | } |
| 776 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 777 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 778 | } |
| 779 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | void thread_report_conflicting_segments(const DrdThreadId tid, |
| 783 | const Addr addr, |
| 784 | const SizeT size, |
| 785 | const BmAccessTypeT access_type) |
| 786 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 787 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 788 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 789 | tl_assert(0 <= tid && tid < DRD_N_THREADS |
| 790 | && tid != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 791 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 792 | for (p = s_threadinfo[tid].first; p; p = p->next) |
| 793 | { |
| 794 | if (bm_has(p->bm, addr, addr + size, access_type)) |
| 795 | { |
| 796 | thread_report_conflicting_segments_segment(tid, addr, size, |
| 797 | access_type, p); |
| 798 | } |
| 799 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 800 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 801 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 802 | /** Compute a bitmap that represents the union of all memory accesses of all |
| 803 | * segments that are unordered to the current segment of the thread tid. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 804 | */ |
| 805 | static void thread_update_danger_set(const DrdThreadId tid) |
| 806 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 807 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 808 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 809 | tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID); |
| 810 | tl_assert(tid == s_drd_running_tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 811 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 812 | s_update_danger_set_count++; |
| 813 | s_danger_set_bitmap_creation_count -= bm_get_bitmap_creation_count(); |
| 814 | s_danger_set_bitmap2_creation_count -= bm_get_bitmap2_creation_count(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 815 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 816 | if (s_danger_set) |
| 817 | { |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 818 | bm_delete(s_danger_set); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 819 | } |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 820 | s_danger_set = bm_new(); |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 821 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 822 | if (s_trace_danger_set) |
| 823 | { |
| 824 | char msg[256]; |
| 825 | |
| 826 | VG_(snprintf)(msg, sizeof(msg), |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 827 | "computing danger set for thread %d/%d with vc ", |
| 828 | DrdThreadIdToVgThreadId(tid), tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 829 | vc_snprint(msg + VG_(strlen)(msg), |
| 830 | sizeof(msg) - VG_(strlen)(msg), |
| 831 | &s_threadinfo[tid].last->vc); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 832 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | p = s_threadinfo[tid].last; |
| 836 | { |
| 837 | unsigned j; |
| 838 | |
| 839 | if (s_trace_danger_set) |
| 840 | { |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 841 | char msg[256]; |
| 842 | |
| 843 | VG_(snprintf)(msg, sizeof(msg), |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 844 | "danger set: thread [%d] at vc ", |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 845 | tid); |
| 846 | vc_snprint(msg + VG_(strlen)(msg), |
| 847 | sizeof(msg) - VG_(strlen)(msg), |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 848 | &p->vc); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 849 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 850 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 851 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 852 | for (j = 0; j < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); j++) |
| 853 | { |
| 854 | if (IsValidDrdThreadId(j)) |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 855 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 856 | const Segment* q; |
| 857 | for (q = s_threadinfo[j].last; q; q = q->prev) |
| 858 | if (j != tid && q != 0 |
| 859 | && ! vc_lte(&q->vc, &p->vc) && ! vc_lte(&p->vc, &q->vc)) |
| 860 | { |
| 861 | if (s_trace_danger_set) |
| 862 | { |
| 863 | char msg[256]; |
| 864 | VG_(snprintf)(msg, sizeof(msg), |
| 865 | "danger set: [%d] merging segment ", j); |
| 866 | vc_snprint(msg + VG_(strlen)(msg), |
| 867 | sizeof(msg) - VG_(strlen)(msg), |
| 868 | &q->vc); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 869 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 870 | } |
| 871 | bm_merge2(s_danger_set, q->bm); |
| 872 | } |
| 873 | else |
| 874 | { |
| 875 | if (s_trace_danger_set) |
| 876 | { |
| 877 | char msg[256]; |
| 878 | VG_(snprintf)(msg, sizeof(msg), |
| 879 | "danger set: [%d] ignoring segment ", j); |
| 880 | vc_snprint(msg + VG_(strlen)(msg), |
| 881 | sizeof(msg) - VG_(strlen)(msg), |
| 882 | &q->vc); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 883 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 884 | } |
| 885 | } |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 886 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 887 | } |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 888 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 889 | for (j = 0; j < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); j++) |
| 890 | { |
| 891 | if (IsValidDrdThreadId(j)) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 892 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 893 | // NPTL hack: don't report data races on sizeof(struct pthread) |
| 894 | // bytes at the top of the stack, since the NPTL functions access |
| 895 | // this data without locking. |
| 896 | if (s_threadinfo[j].stack_min != 0) |
| 897 | { |
| 898 | tl_assert(s_threadinfo[j].stack_startup != 0); |
| 899 | if (s_threadinfo[j].stack_min < s_threadinfo[j].stack_startup) |
| 900 | { |
| 901 | bm_clear(s_danger_set, |
| 902 | s_threadinfo[j].stack_min, |
| 903 | s_threadinfo[j].stack_startup); |
| 904 | } |
| 905 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 906 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 907 | } |
| 908 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 909 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 910 | s_danger_set_bitmap_creation_count += bm_get_bitmap_creation_count(); |
| 911 | s_danger_set_bitmap2_creation_count += bm_get_bitmap2_creation_count(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 912 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 913 | if (0 && s_trace_danger_set) |
| 914 | { |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 915 | VG_(message)(Vg_UserMsg, "[%d] new danger set:", tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 916 | bm_print(s_danger_set); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 917 | VG_(message)(Vg_UserMsg, "[%d] end of new danger set.", tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 918 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 919 | } |
| 920 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 921 | ULong thread_get_context_switch_count(void) |
| 922 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 923 | return s_context_switch_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 924 | } |
| 925 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 926 | ULong thread_get_discard_ordered_segments_count(void) |
| 927 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 928 | return s_discard_ordered_segments_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | ULong thread_get_update_danger_set_count(void) |
| 932 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 933 | return s_update_danger_set_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | ULong thread_get_danger_set_bitmap_creation_count(void) |
| 937 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 938 | return s_danger_set_bitmap_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | ULong thread_get_danger_set_bitmap2_creation_count(void) |
| 942 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 943 | return s_danger_set_bitmap2_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 944 | } |