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