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