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