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