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