| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame^] | 2 | This file is part of drd, a thread error detector. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 3 | |
| bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame^] | 4 | Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | |
| 6 | This program is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU General Public License as |
| 8 | published by the Free Software Foundation; either version 2 of the |
| 9 | License, or (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 19 | 02111-1307, USA. |
| 20 | |
| 21 | The GNU General Public License is contained in the file COPYING. |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include "drd_error.h" |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 26 | #include "drd_barrier.h" |
| 27 | #include "drd_cond.h" |
| 28 | #include "drd_mutex.h" |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 29 | #include "drd_segment.h" |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 30 | #include "drd_semaphore.h" |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 31 | #include "drd_suppression.h" |
| 32 | #include "drd_thread.h" |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 33 | #include "pub_tool_vki.h" |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 34 | #include "pub_tool_basics.h" // Addr, SizeT |
| 35 | #include "pub_tool_errormgr.h" // VG_(unique_error)() |
| 36 | #include "pub_tool_libcassert.h" // tl_assert() |
| 37 | #include "pub_tool_libcbase.h" // VG_(strlen)() |
| 38 | #include "pub_tool_libcprint.h" // VG_(printf)() |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 39 | #include "pub_tool_libcproc.h" // VG_(getenv)() |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 40 | #include "pub_tool_machine.h" |
| 41 | #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)() |
| sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 42 | #include "pub_tool_options.h" // VG_(clo_backtrace_size) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 43 | #include "pub_tool_threadstate.h" // VG_(get_pthread_id)() |
| 44 | |
| bart | 32ba208 | 2008-06-05 08:53:42 +0000 | [diff] [blame] | 45 | |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 46 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 47 | /* Local functions. */ |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 48 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 49 | static void DRD_(thread_append_segment)(const DrdThreadId tid, |
| 50 | Segment* const sg); |
| 51 | static void DRD_(thread_discard_segment)(const DrdThreadId tid, |
| 52 | Segment* const sg); |
| 53 | static Bool DRD_(thread_conflict_set_up_to_date)(const DrdThreadId tid); |
| 54 | static void DRD_(thread_compute_conflict_set)(struct bitmap** conflict_set, |
| 55 | const DrdThreadId tid); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 56 | |
| 57 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 58 | /* Local variables. */ |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 59 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 60 | static ULong DRD_(s_context_switch_count); |
| 61 | static ULong DRD_(s_discard_ordered_segments_count); |
| 62 | static ULong DRD_(s_update_conflict_set_count); |
| 63 | static ULong DRD_(s_conflict_set_new_segment_count); |
| 64 | static ULong DRD_(s_conflict_set_combine_vc_count); |
| 65 | static ULong DRD_(s_conflict_set_bitmap_creation_count); |
| 66 | static ULong DRD_(s_conflict_set_bitmap2_creation_count); |
| 67 | static ThreadId DRD_(s_vg_running_tid) = VG_INVALID_THREADID; |
| 68 | DrdThreadId DRD_(g_drd_running_tid) = DRD_INVALID_THREADID; |
| 69 | ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS]; |
| 70 | struct bitmap* DRD_(g_conflict_set); |
| 71 | static Bool DRD_(s_trace_context_switches) = False; |
| 72 | static Bool DRD_(s_trace_conflict_set) = False; |
| 73 | static Bool DRD_(s_trace_fork_join) = False; |
| 74 | static Bool DRD_(s_segment_merging) = True; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 75 | |
| 76 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 77 | /* Function definitions. */ |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 78 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 79 | void DRD_(thread_trace_context_switches)(const Bool t) |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 80 | { |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 81 | tl_assert(t == False || t == True); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 82 | DRD_(s_trace_context_switches) = t; |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 85 | void DRD_(thread_trace_conflict_set)(const Bool t) |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 86 | { |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 87 | tl_assert(t == False || t == True); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 88 | DRD_(s_trace_conflict_set) = t; |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 91 | Bool DRD_(thread_get_trace_fork_join)(void) |
| 92 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 93 | return DRD_(s_trace_fork_join); |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void DRD_(thread_set_trace_fork_join)(const Bool t) |
| 97 | { |
| 98 | tl_assert(t == False || t == True); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 99 | DRD_(s_trace_fork_join) = t; |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 102 | void DRD_(thread_set_segment_merging)(const Bool m) |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 103 | { |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 104 | tl_assert(m == False || m == True); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 105 | DRD_(s_segment_merging) = m; |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 108 | /** |
| 109 | * Convert Valgrind's ThreadId into a DrdThreadId. Report failure if |
| 110 | * Valgrind's ThreadId does not yet exist. |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 111 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 112 | DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 113 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 114 | int i; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 115 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 116 | if (tid == VG_INVALID_THREADID) |
| 117 | return DRD_INVALID_THREADID; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 118 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 119 | for (i = 1; i < DRD_N_THREADS; i++) |
| 120 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 121 | if (DRD_(g_threadinfo)[i].vg_thread_exists == True |
| 122 | && DRD_(g_threadinfo)[i].vg_threadid == tid) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 123 | { |
| 124 | return i; |
| 125 | } |
| 126 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 127 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 128 | return DRD_INVALID_THREADID; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 131 | static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 132 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 133 | int i; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 134 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 135 | tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 136 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 137 | for (i = 1; i < DRD_N_THREADS; i++) |
| 138 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 139 | if (DRD_(g_threadinfo)[i].vg_thread_exists == False |
| 140 | && DRD_(g_threadinfo)[i].posix_thread_exists == False |
| 141 | && DRD_(g_threadinfo)[i].detached_posix_thread == False) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 142 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 143 | DRD_(g_threadinfo)[i].vg_thread_exists = True; |
| 144 | DRD_(g_threadinfo)[i].vg_threadid = tid; |
| 145 | DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID; |
| 146 | DRD_(g_threadinfo)[i].stack_min = 0; |
| 147 | DRD_(g_threadinfo)[i].stack_min_min = 0; |
| 148 | DRD_(g_threadinfo)[i].stack_startup = 0; |
| 149 | DRD_(g_threadinfo)[i].stack_max = 0; |
| 150 | DRD_(g_threadinfo)[i].is_recording = True; |
| 151 | DRD_(g_threadinfo)[i].synchr_nesting = 0; |
| 152 | if (DRD_(g_threadinfo)[i].first != 0) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 153 | VG_(printf)("drd thread id = %d\n", i); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 154 | tl_assert(DRD_(g_threadinfo)[i].first == 0); |
| 155 | tl_assert(DRD_(g_threadinfo)[i].last == 0); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 156 | return i; |
| 157 | } |
| 158 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 159 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 160 | tl_assert(False); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 161 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 162 | return DRD_INVALID_THREADID; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 165 | DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 166 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 167 | int i; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 168 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 169 | tl_assert(tid != INVALID_POSIX_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 170 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 171 | for (i = 1; i < DRD_N_THREADS; i++) |
| 172 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 173 | if (DRD_(g_threadinfo)[i].posix_thread_exists |
| 174 | && DRD_(g_threadinfo)[i].pt_threadid == tid) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 175 | { |
| 176 | return i; |
| 177 | } |
| 178 | } |
| 179 | return DRD_INVALID_THREADID; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 182 | ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 183 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 184 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 185 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 186 | return (DRD_(g_threadinfo)[tid].vg_thread_exists |
| 187 | ? DRD_(g_threadinfo)[tid].vg_threadid |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 188 | : VG_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 191 | #if 0 |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 192 | /** |
| 193 | * Sanity check of the doubly linked list of segments referenced by a |
| 194 | * ThreadInfo struct. |
| 195 | * @return True if sane, False if not. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 196 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 197 | static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 198 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 199 | Segment* p; |
| 200 | for (p = ti->first; p; p = p->next) { |
| 201 | if (p->next && p->next->prev != p) |
| 202 | return False; |
| 203 | if (p->next == 0 && p != ti->last) |
| 204 | return False; |
| 205 | } |
| 206 | for (p = ti->last; p; p = p->prev) { |
| 207 | if (p->prev && p->prev->next != p) |
| 208 | return False; |
| 209 | if (p->prev == 0 && p != ti->first) |
| 210 | return False; |
| 211 | } |
| 212 | return True; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 213 | } |
| bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 214 | #endif |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 215 | |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 216 | /** |
| 217 | * Create the first segment for a newly started thread. |
| 218 | * |
| 219 | * This function is called from the handler installed via |
| 220 | * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler |
| 221 | * from the context of the creator thread, before the new thread has been |
| 222 | * created. |
| 223 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 224 | DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator, |
| 225 | const ThreadId vg_created) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 226 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 227 | DrdThreadId created; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 228 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 229 | tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID); |
| 230 | created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created); |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 231 | tl_assert(0 <= (int)created && created < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 232 | && created != DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 233 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 234 | tl_assert(DRD_(g_threadinfo)[created].first == 0); |
| 235 | tl_assert(DRD_(g_threadinfo)[created].last == 0); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 236 | DRD_(thread_append_segment)(created, DRD_(sg_new)(creator, created)); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 237 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 238 | return created; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 241 | /** |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 242 | * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called after |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 243 | * the thread has been created and before any client instructioins are run |
| 244 | * on the newly created thread, e.g. from the handler installed via |
| 245 | * VG_(track_pre_thread_first_insn)(). |
| 246 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 247 | DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created) |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 248 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 249 | const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created); |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 250 | |
| 251 | tl_assert(0 <= (int)created && created < DRD_N_THREADS |
| 252 | && created != DRD_INVALID_THREADID); |
| 253 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 254 | DRD_(g_threadinfo)[created].stack_max = VG_(thread_get_stack_max)(vg_created); |
| 255 | DRD_(g_threadinfo)[created].stack_startup = DRD_(g_threadinfo)[created].stack_max; |
| 256 | DRD_(g_threadinfo)[created].stack_min = DRD_(g_threadinfo)[created].stack_max; |
| 257 | DRD_(g_threadinfo)[created].stack_min_min = DRD_(g_threadinfo)[created].stack_max; |
| 258 | DRD_(g_threadinfo)[created].stack_size = VG_(thread_get_stack_size)(vg_created); |
| 259 | tl_assert(DRD_(g_threadinfo)[created].stack_max != 0); |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 260 | |
| 261 | return created; |
| 262 | } |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 263 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 264 | /** |
| 265 | * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just |
| 266 | * after thread drd_joiner joined thread drd_joinee. |
| 267 | */ |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 268 | void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee) |
| 269 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 270 | tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner)); |
| 271 | tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee)); |
| 272 | DRD_(thread_new_segment)(drd_joinee); |
| 273 | DRD_(thread_combine_vc)(drd_joiner, drd_joinee); |
| 274 | DRD_(thread_new_segment)(drd_joiner); |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 275 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 276 | if (DRD_(s_trace_fork_join)) |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 277 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 278 | const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner); |
| 279 | const ThreadId joinee = DRD_(DrdThreadIdToVgThreadId)(drd_joinee); |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 280 | const unsigned msg_size = 256; |
| 281 | char* msg; |
| 282 | |
| 283 | msg = VG_(malloc)("drd.main.dptj.1", msg_size); |
| 284 | tl_assert(msg); |
| 285 | VG_(snprintf)(msg, msg_size, |
| 286 | "drd_post_thread_join joiner = %d/%d, joinee = %d/%d", |
| 287 | joiner, drd_joiner, joinee, drd_joinee); |
| 288 | if (joiner) |
| 289 | { |
| 290 | VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg), |
| 291 | ", new vc: "); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 292 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg), |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 293 | DRD_(thread_get_vc)(drd_joiner)); |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 294 | } |
| 295 | VG_(message)(Vg_DebugMsg, "%s", msg); |
| 296 | VG_(free)(msg); |
| 297 | } |
| 298 | |
| 299 | if (! DRD_(get_check_stack_accesses)()) |
| 300 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 301 | DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee) |
| 302 | - DRD_(thread_get_stack_size)(drd_joinee), |
| 303 | DRD_(thread_get_stack_max)(drd_joinee)); |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 304 | } |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 305 | DRD_(thread_delete)(drd_joinee); |
| bart | dc1ef03 | 2009-02-15 14:18:02 +0000 | [diff] [blame] | 306 | DRD_(mutex_thread_delete)(drd_joinee); |
| 307 | DRD_(cond_thread_delete)(drd_joinee); |
| 308 | DRD_(semaphore_thread_delete)(drd_joinee); |
| bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 309 | DRD_(barrier_thread_delete)(drd_joinee); |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 312 | /** |
| 313 | * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack, |
| 314 | * and accesses this data structure from multiple threads without locking. |
| 315 | * Any conflicting accesses in the range stack_startup..stack_max will be |
| 316 | * ignored. |
| 317 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 318 | void DRD_(thread_set_stack_startup)(const DrdThreadId tid, |
| 319 | const Addr stack_startup) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 320 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 321 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 322 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 323 | tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup); |
| 324 | tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max); |
| 325 | DRD_(g_threadinfo)[tid].stack_startup = stack_startup; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 328 | Addr DRD_(thread_get_stack_min)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 329 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 330 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 331 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 332 | return DRD_(g_threadinfo)[tid].stack_min; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 335 | Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid) |
| bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 336 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 337 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 338 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 339 | return DRD_(g_threadinfo)[tid].stack_min_min; |
| bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 342 | Addr DRD_(thread_get_stack_max)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 343 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 344 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 345 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 346 | return DRD_(g_threadinfo)[tid].stack_max; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 349 | SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid) |
| bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 350 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 351 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 352 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 353 | return DRD_(g_threadinfo)[tid].stack_size; |
| bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 356 | /** |
| 357 | * Clean up thread-specific data structures. Call this just after |
| 358 | * pthread_join(). |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 359 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 360 | void DRD_(thread_delete)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 361 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 362 | Segment* sg; |
| 363 | Segment* sg_prev; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 364 | |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 365 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 366 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 367 | tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0); |
| 368 | for (sg = DRD_(g_threadinfo)[tid].last; sg; sg = sg_prev) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 369 | { |
| 370 | sg_prev = sg->prev; |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 371 | sg->prev = 0; |
| 372 | sg->next = 0; |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 373 | DRD_(sg_put)(sg); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 374 | } |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 375 | DRD_(g_threadinfo)[tid].vg_thread_exists = False; |
| 376 | DRD_(g_threadinfo)[tid].posix_thread_exists = False; |
| 377 | tl_assert(DRD_(g_threadinfo)[tid].detached_posix_thread == False); |
| 378 | DRD_(g_threadinfo)[tid].first = 0; |
| 379 | DRD_(g_threadinfo)[tid].last = 0; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 382 | /** |
| 383 | * Called after a thread performed its last memory access and before |
| 384 | * thread_delete() is called. Note: thread_delete() is only called for |
| 385 | * joinable threads, not for detached threads. |
| 386 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 387 | void DRD_(thread_finished)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 388 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 389 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 390 | && tid != DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 391 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 392 | DRD_(g_threadinfo)[tid].vg_thread_exists = False; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 393 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 394 | if (DRD_(g_threadinfo)[tid].detached_posix_thread) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 395 | { |
| 396 | /* Once a detached thread has finished, its stack is deallocated and */ |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 397 | /* should no longer be taken into account when computing the conflict set*/ |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 398 | DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 399 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 400 | /* For a detached thread, calling pthread_exit() invalidates the */ |
| 401 | /* POSIX thread ID associated with the detached thread. For joinable */ |
| 402 | /* POSIX threads however, the POSIX thread ID remains live after the */ |
| 403 | /* pthread_exit() call until pthread_join() is called. */ |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 404 | DRD_(g_threadinfo)[tid].posix_thread_exists = False; |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 405 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| bart | 9b2974a | 2008-09-27 12:35:31 +0000 | [diff] [blame] | 408 | /** Called just before pthread_cancel(). */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 409 | void DRD_(thread_pre_cancel)(const DrdThreadId tid) |
| bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 410 | { |
| 411 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 412 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 413 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
| bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 414 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 415 | DRD_(g_threadinfo)[tid].synchr_nesting = 0; |
| bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 418 | void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 419 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 420 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 421 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 422 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 423 | tl_assert(ptid != INVALID_POSIX_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 424 | DRD_(g_threadinfo)[tid].posix_thread_exists = True; |
| 425 | DRD_(g_threadinfo)[tid].pt_threadid = ptid; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 428 | Bool DRD_(thread_get_joinable)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 429 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 430 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 431 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 432 | return ! DRD_(g_threadinfo)[tid].detached_posix_thread; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 435 | void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 436 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 437 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 438 | && tid != DRD_INVALID_THREADID); |
| 439 | tl_assert(!! joinable == joinable); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 440 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 441 | #if 0 |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 442 | VG_(message)(Vg_DebugMsg, |
| 443 | "thread_set_joinable(%d/%d, %s)", |
| 444 | tid, |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 445 | DRD_(g_threadinfo)[tid].vg_threadid, |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 446 | joinable ? "joinable" : "detached"); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 447 | #endif |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 448 | DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 451 | void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 452 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 453 | tl_assert(vg_tid != VG_INVALID_THREADID); |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 454 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 455 | if (vg_tid != DRD_(s_vg_running_tid)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 456 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 457 | DRD_(thread_set_running_tid)(vg_tid, |
| 458 | DRD_(VgThreadIdToDrdThreadId)(vg_tid)); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 459 | } |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 460 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 461 | tl_assert(DRD_(s_vg_running_tid) != VG_INVALID_THREADID); |
| 462 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 465 | void DRD_(thread_set_running_tid)(const ThreadId vg_tid, |
| 466 | const DrdThreadId drd_tid) |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 467 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 468 | tl_assert(vg_tid != VG_INVALID_THREADID); |
| 469 | tl_assert(drd_tid != DRD_INVALID_THREADID); |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 470 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 471 | if (vg_tid != DRD_(s_vg_running_tid)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 472 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 473 | if (DRD_(s_trace_context_switches) |
| 474 | && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 475 | { |
| 476 | VG_(message)(Vg_DebugMsg, |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 477 | "Context switch from thread %d/%d to thread %d/%d;" |
| 478 | " segments: %llu", |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 479 | DRD_(s_vg_running_tid), DRD_(g_drd_running_tid), |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 480 | DRD_(DrdThreadIdToVgThreadId)(drd_tid), drd_tid, |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 481 | DRD_(sg_get_segments_alive_count)()); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 482 | } |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 483 | DRD_(s_vg_running_tid) = vg_tid; |
| 484 | DRD_(g_drd_running_tid) = drd_tid; |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 485 | DRD_(thread_compute_conflict_set)(&DRD_(g_conflict_set), drd_tid); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 486 | DRD_(s_context_switch_count)++; |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 487 | } |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 488 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 489 | tl_assert(DRD_(s_vg_running_tid) != VG_INVALID_THREADID); |
| 490 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 493 | int DRD_(thread_enter_synchr)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 494 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 495 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 496 | return DRD_(g_threadinfo)[tid].synchr_nesting++; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 499 | int DRD_(thread_leave_synchr)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 500 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 501 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 502 | tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1); |
| 503 | return --DRD_(g_threadinfo)[tid].synchr_nesting; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 506 | int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 507 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 508 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 509 | return DRD_(g_threadinfo)[tid].synchr_nesting; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 512 | /** Append a new segment at the end of the segment list. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 513 | static |
| 514 | void DRD_(thread_append_segment)(const DrdThreadId tid, Segment* const sg) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 515 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 516 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 517 | && tid != DRD_INVALID_THREADID); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 518 | // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 519 | sg->prev = DRD_(g_threadinfo)[tid].last; |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 520 | sg->next = 0; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 521 | if (DRD_(g_threadinfo)[tid].last) |
| 522 | DRD_(g_threadinfo)[tid].last->next = sg; |
| 523 | DRD_(g_threadinfo)[tid].last = sg; |
| 524 | if (DRD_(g_threadinfo)[tid].first == 0) |
| 525 | DRD_(g_threadinfo)[tid].first = sg; |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 526 | // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 529 | /** |
| 530 | * Remove a segment from the segment list of thread threadid, and free the |
| 531 | * associated memory. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 532 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 533 | static |
| 534 | void DRD_(thread_discard_segment)(const DrdThreadId tid, Segment* const sg) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 535 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 536 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 537 | && tid != DRD_INVALID_THREADID); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 538 | //tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 539 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 540 | if (sg->prev) |
| 541 | sg->prev->next = sg->next; |
| 542 | if (sg->next) |
| 543 | sg->next->prev = sg->prev; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 544 | if (sg == DRD_(g_threadinfo)[tid].first) |
| 545 | DRD_(g_threadinfo)[tid].first = sg->next; |
| 546 | if (sg == DRD_(g_threadinfo)[tid].last) |
| 547 | DRD_(g_threadinfo)[tid].last = sg->prev; |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 548 | DRD_(sg_put)(sg); |
| bart | 3f74967 | 2008-03-22 09:49:40 +0000 | [diff] [blame] | 549 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 550 | //tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 553 | VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 554 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 555 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 556 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 557 | tl_assert(DRD_(g_threadinfo)[tid].last); |
| 558 | return &DRD_(g_threadinfo)[tid].last->vc; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 561 | /** |
| 562 | * Return the latest segment of thread 'tid' and increment its reference count. |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 563 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 564 | void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid) |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 565 | { |
| 566 | tl_assert(sg); |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 567 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 568 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 569 | tl_assert(DRD_(g_threadinfo)[tid].last); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 570 | |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 571 | DRD_(sg_put)(*sg); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 572 | *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 575 | /** |
| 576 | * Compute the minimum of all latest vector clocks of all threads |
| 577 | * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA). |
| 578 | * @param vc pointer to a vectorclock, holds result upon return. |
| 579 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 580 | static void DRD_(thread_compute_minimum_vc)(VectorClock* vc) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 581 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 582 | unsigned i; |
| 583 | Bool first; |
| 584 | Segment* latest_sg; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 585 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 586 | first = True; |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 587 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 588 | i++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 589 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 590 | latest_sg = DRD_(g_threadinfo)[i].last; |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 591 | if (latest_sg) |
| 592 | { |
| 593 | if (first) |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 594 | DRD_(vc_assign)(vc, &latest_sg->vc); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 595 | else |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 596 | DRD_(vc_min)(vc, &latest_sg->vc); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 597 | first = False; |
| 598 | } |
| 599 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 602 | static void DRD_(thread_compute_maximum_vc)(VectorClock* vc) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 603 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 604 | unsigned i; |
| 605 | Bool first; |
| 606 | Segment* latest_sg; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 607 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 608 | first = True; |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 609 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 610 | i++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 611 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 612 | latest_sg = DRD_(g_threadinfo)[i].last; |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 613 | if (latest_sg) |
| 614 | { |
| 615 | if (first) |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 616 | DRD_(vc_assign)(vc, &latest_sg->vc); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 617 | else |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 618 | DRD_(vc_combine)(vc, &latest_sg->vc); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 619 | first = False; |
| 620 | } |
| 621 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /** |
| bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 625 | * Discard all segments that have a defined order against the latest vector |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 626 | * clock of every thread -- these segments can no longer be involved in a |
| 627 | * data race. |
| 628 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 629 | static void DRD_(thread_discard_ordered_segments)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 630 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 631 | unsigned i; |
| 632 | VectorClock thread_vc_min; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 633 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 634 | DRD_(s_discard_ordered_segments_count)++; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 635 | |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 636 | DRD_(vc_init)(&thread_vc_min, 0, 0); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 637 | DRD_(thread_compute_minimum_vc)(&thread_vc_min); |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 638 | if (DRD_(sg_get_trace)()) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 639 | { |
| 640 | char msg[256]; |
| 641 | VectorClock thread_vc_max; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 642 | |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 643 | DRD_(vc_init)(&thread_vc_max, 0, 0); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 644 | DRD_(thread_compute_maximum_vc)(&thread_vc_max); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 645 | VG_(snprintf)(msg, sizeof(msg), |
| 646 | "Discarding ordered segments -- min vc is "); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 647 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 648 | &thread_vc_min); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 649 | VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 650 | ", max vc is "); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 651 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 652 | &thread_vc_max); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 653 | VG_(message)(Vg_UserMsg, "%s", msg); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 654 | DRD_(vc_cleanup)(&thread_vc_max); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 655 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 656 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 657 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 658 | i++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 659 | { |
| 660 | Segment* sg; |
| 661 | Segment* sg_next; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 662 | for (sg = DRD_(g_threadinfo)[i].first; |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 663 | sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 664 | sg = sg_next) |
| 665 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 666 | DRD_(thread_discard_segment)(i, sg); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 669 | DRD_(vc_cleanup)(&thread_vc_min); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 672 | /** |
| 673 | * Merge all segments that may be merged without triggering false positives |
| 674 | * or discarding real data races. For the theoretical background of segment |
| 675 | * merging, see also the following paper: |
| 676 | * Mark Christiaens, Michiel Ronsse and Koen De Bosschere. |
| 677 | * Bounding the number of segment histories during data race detection. |
| 678 | * Parallel Computing archive, Volume 28, Issue 9, pp 1221-1238, |
| 679 | * September 2002. |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 680 | */ |
| 681 | static void thread_merge_segments(void) |
| 682 | { |
| 683 | unsigned i; |
| 684 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 685 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 686 | i++) |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 687 | { |
| 688 | Segment* sg; |
| 689 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 690 | // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i])); |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 691 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 692 | for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next) |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 693 | { |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 694 | if (DRD_(sg_get_refcnt)(sg) == 1 |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 695 | && sg->next |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 696 | && DRD_(sg_get_refcnt)(sg->next) == 1 |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 697 | && sg->next->next) |
| 698 | { |
| 699 | /* Merge sg and sg->next into sg. */ |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 700 | DRD_(sg_merge)(sg, sg->next); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 701 | DRD_(thread_discard_segment)(i, sg->next); |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 705 | // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i])); |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 706 | } |
| 707 | } |
| 708 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 709 | /** |
| 710 | * Every change in the vector clock of a thread may cause segments that |
| 711 | * were previously ordered to this thread to become unordered. Hence, |
| 712 | * it may be necessary to recalculate the conflict set if the vector clock |
| 713 | * of the current thread is updated. This function check whether such a |
| 714 | * recalculation is necessary. |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 715 | * |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 716 | * @param tid Thread ID of the thread to which a new segment has been |
| 717 | * appended. |
| 718 | * @param new_sg Pointer to the most recent segment of thread tid. |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 719 | */ |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 720 | static Bool conflict_set_update_needed(const DrdThreadId tid, |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 721 | const Segment* const new_sg) |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 722 | { |
| bart | 5d421ba | 2008-04-19 15:15:12 +0000 | [diff] [blame] | 723 | #if 0 |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 724 | unsigned i; |
| 725 | const Segment* old_sg; |
| 726 | |
| 727 | tl_assert(new_sg); |
| 728 | |
| 729 | /* If a new segment was added to another thread than the running thread, */ |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 730 | /* just tell the caller to update the conflict set. */ |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 731 | if (tid != DRD_(g_drd_running_tid)) |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 732 | return True; |
| 733 | |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 734 | /* Always let the caller update the conflict set after creation of the */ |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 735 | /* first segment. */ |
| 736 | old_sg = new_sg->prev; |
| 737 | if (old_sg == 0) |
| 738 | return True; |
| 739 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 740 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 741 | i++) |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 742 | { |
| 743 | Segment* q; |
| 744 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 745 | if (i == DRD_(g_drd_running_tid)) |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 746 | continue; |
| 747 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 748 | for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev) |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 749 | { |
| 750 | /* If the expression below evaluates to false, this expression will */ |
| 751 | /* also evaluate to false for all subsequent iterations. So stop */ |
| 752 | /* iterating. */ |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 753 | if (DRD_(vc_lte)(&q->vc, &old_sg->vc)) |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 754 | break; |
| 755 | /* If the vector clock of the 2nd the last segment is not ordered */ |
| 756 | /* to the vector clock of segment q, and the last segment is, ask */ |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 757 | /* the caller to update the conflict set. */ |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 758 | if (! DRD_(vc_lte)(&old_sg->vc, &q->vc)) |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 759 | { |
| 760 | return True; |
| 761 | } |
| 762 | /* If the vector clock of the last segment is not ordered to the */ |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 763 | /* vector clock of segment q, ask the caller to update the conflict */ |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 764 | /* set. */ |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 765 | if (! DRD_(vc_lte)(&q->vc, &new_sg->vc) && ! DRD_(vc_lte)(&new_sg->vc, &q->vc)) |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 766 | { |
| 767 | return True; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | return False; |
| bart | 5d421ba | 2008-04-19 15:15:12 +0000 | [diff] [blame] | 773 | #else |
| 774 | return True; |
| 775 | #endif |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 778 | /** |
| 779 | * Create a new segment for the specified thread, and discard any segments |
| 780 | * that cannot cause races anymore. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 781 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 782 | void DRD_(thread_new_segment)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 783 | { |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 784 | Segment* new_sg; |
| 785 | |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 786 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 787 | && tid != DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 788 | |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 789 | new_sg = DRD_(sg_new)(tid, tid); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 790 | DRD_(thread_append_segment)(tid, new_sg); |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 791 | |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 792 | if (conflict_set_update_needed(tid, new_sg)) |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 793 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 794 | DRD_(thread_compute_conflict_set)(&DRD_(g_conflict_set), |
| 795 | DRD_(g_drd_running_tid)); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 796 | DRD_(s_conflict_set_new_segment_count)++; |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 797 | } |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 798 | else if (tid == DRD_(g_drd_running_tid)) |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 799 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 800 | tl_assert(DRD_(thread_conflict_set_up_to_date)(DRD_(g_drd_running_tid))); |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 801 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 802 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 803 | DRD_(thread_discard_ordered_segments)(); |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 804 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 805 | if (DRD_(s_segment_merging)) |
| 806 | { |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 807 | thread_merge_segments(); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 808 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 811 | /** Call this function after thread 'joiner' joined thread 'joinee'. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 812 | void DRD_(thread_combine_vc)(DrdThreadId joiner, DrdThreadId joinee) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 813 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 814 | tl_assert(joiner != joinee); |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 815 | tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 816 | && joiner != DRD_INVALID_THREADID); |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 817 | tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 818 | && joinee != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 819 | tl_assert(DRD_(g_threadinfo)[joiner].last); |
| 820 | tl_assert(DRD_(g_threadinfo)[joinee].last); |
| 821 | DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc, |
| 822 | &DRD_(g_threadinfo)[joinee].last->vc); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 823 | DRD_(thread_discard_ordered_segments)(); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 824 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 825 | if (joiner == DRD_(g_drd_running_tid)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 826 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 827 | DRD_(thread_compute_conflict_set)(&DRD_(g_conflict_set), joiner); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 828 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 831 | /** |
| 832 | * Call this function after thread 'tid' had to wait because of thread |
| 833 | * synchronization until the memory accesses in the segment with vector clock |
| 834 | * 'vc' finished. |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 835 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 836 | void DRD_(thread_combine_vc2)(DrdThreadId tid, const VectorClock* const vc) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 837 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 838 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 839 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 840 | tl_assert(DRD_(g_threadinfo)[tid].last); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 841 | tl_assert(vc); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 842 | DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 843 | DRD_(thread_compute_conflict_set)(&DRD_(g_conflict_set), tid); |
| 844 | DRD_(thread_discard_ordered_segments)(); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 845 | DRD_(s_conflict_set_combine_vc_count)++; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 848 | /** |
| 849 | * Call this function whenever a thread is no longer using the memory |
| 850 | * [ a1, a2 [, e.g. because of a call to free() or a stack pointer |
| 851 | * increase. |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 852 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 853 | void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 854 | { |
| bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 855 | DrdThreadId other_user; |
| 856 | unsigned i; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 857 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 858 | /* For all threads, mark the range [ a1, a2 [ as no longer in use. */ |
| bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 859 | other_user = DRD_INVALID_THREADID; |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 860 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 861 | i++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 862 | { |
| 863 | Segment* p; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 864 | for (p = DRD_(g_threadinfo)[i].first; p; p = p->next) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 865 | { |
| 866 | if (other_user == DRD_INVALID_THREADID |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 867 | && i != DRD_(g_drd_running_tid)) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 868 | { |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 869 | if (UNLIKELY(DRD_(bm_test_and_clear)(p->bm, a1, a2))) |
| bart | 8bf2f8b | 2008-03-30 17:56:43 +0000 | [diff] [blame] | 870 | { |
| 871 | other_user = i; |
| 872 | } |
| 873 | continue; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 874 | } |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 875 | DRD_(bm_clear)(p->bm, a1, a2); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 876 | } |
| 877 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 878 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 879 | /* |
| 880 | * If any other thread had accessed memory in [ a1, a2 [, update the |
| 881 | * conflict set. |
| 882 | */ |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 883 | if (other_user != DRD_INVALID_THREADID |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 884 | && DRD_(bm_has_any_access)(DRD_(g_conflict_set), a1, a2)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 885 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 886 | DRD_(thread_compute_conflict_set)(&DRD_(g_conflict_set), |
| 887 | DRD_(thread_get_running_tid)()); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 888 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 889 | } |
| 890 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 891 | void DRD_(thread_start_recording)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 892 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 893 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 894 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 895 | tl_assert(! DRD_(g_threadinfo)[tid].is_recording); |
| 896 | DRD_(g_threadinfo)[tid].is_recording = True; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 899 | void DRD_(thread_stop_recording)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 900 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 901 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 902 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 903 | tl_assert(DRD_(g_threadinfo)[tid].is_recording); |
| 904 | DRD_(g_threadinfo)[tid].is_recording = False; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 907 | void DRD_(thread_print_all)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 908 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 909 | unsigned i; |
| 910 | Segment* p; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 911 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 912 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 913 | i++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 914 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 915 | if (DRD_(g_threadinfo)[i].first) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 916 | { |
| 917 | VG_(printf)("**************\n" |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 918 | "* thread %3d (%d/%d/%d/0x%lx/%d) *\n" |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 919 | "**************\n", |
| 920 | i, |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 921 | DRD_(g_threadinfo)[i].vg_thread_exists, |
| 922 | DRD_(g_threadinfo)[i].vg_threadid, |
| 923 | DRD_(g_threadinfo)[i].posix_thread_exists, |
| 924 | DRD_(g_threadinfo)[i].pt_threadid, |
| 925 | DRD_(g_threadinfo)[i].detached_posix_thread); |
| 926 | for (p = DRD_(g_threadinfo)[i].first; p; p = p->next) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 927 | { |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 928 | DRD_(sg_print)(p); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 929 | } |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | static void show_call_stack(const DrdThreadId tid, |
| 935 | const Char* const msg, |
| 936 | ExeContext* const callstack) |
| 937 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 938 | const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 939 | |
| bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 940 | VG_(message)(Vg_UserMsg, "%s (thread %d/%d)", msg, vg_tid, tid); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 941 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 942 | if (vg_tid != VG_INVALID_THREADID) |
| 943 | { |
| 944 | if (callstack) |
| 945 | { |
| 946 | VG_(pp_ExeContext)(callstack); |
| 947 | } |
| 948 | else |
| 949 | { |
| 950 | VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size)); |
| 951 | } |
| 952 | } |
| 953 | else |
| 954 | { |
| 955 | VG_(message)(Vg_UserMsg, |
| 956 | " (thread finished, call stack no longer available)"); |
| 957 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 960 | static void |
| 961 | thread_report_conflicting_segments_segment(const DrdThreadId tid, |
| 962 | const Addr addr, |
| 963 | const SizeT size, |
| 964 | const BmAccessTypeT access_type, |
| 965 | const Segment* const p) |
| 966 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 967 | unsigned i; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 968 | |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 969 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 970 | && tid != DRD_INVALID_THREADID); |
| 971 | tl_assert(p); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 972 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 973 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 974 | i++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 975 | { |
| 976 | if (i != tid) |
| 977 | { |
| 978 | Segment* q; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 979 | for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 980 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 981 | /* |
| 982 | * Since q iterates over the segments of thread i in order of |
| 983 | * decreasing vector clocks, if q->vc <= p->vc, then |
| 984 | * q->next->vc <= p->vc will also hold. Hence, break out of the |
| 985 | * loop once this condition is met. |
| 986 | */ |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 987 | if (DRD_(vc_lte)(&q->vc, &p->vc)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 988 | break; |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 989 | if (! DRD_(vc_lte)(&p->vc, &q->vc)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 990 | { |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 991 | if (DRD_(bm_has_conflict_with)(q->bm, addr, addr + size, |
| 992 | access_type)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 993 | { |
| 994 | tl_assert(q->stacktrace); |
| 995 | show_call_stack(i, "Other segment start", |
| 996 | q->stacktrace); |
| 997 | show_call_stack(i, "Other segment end", |
| 998 | q->next ? q->next->stacktrace : 0); |
| 999 | } |
| 1000 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1001 | } |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1006 | void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid, |
| 1007 | const Addr addr, |
| 1008 | const SizeT size, |
| 1009 | const BmAccessTypeT access_type) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1010 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1011 | Segment* p; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1012 | |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 1013 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1014 | && tid != DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1015 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1016 | for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1017 | { |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1018 | if (DRD_(bm_has)(p->bm, addr, addr + size, access_type)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1019 | { |
| 1020 | thread_report_conflicting_segments_segment(tid, addr, size, |
| 1021 | access_type, p); |
| 1022 | } |
| 1023 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1024 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1025 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1026 | /** |
| 1027 | * Verify whether the conflict set for thread tid is up to date. Only perform |
| 1028 | * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set. |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1029 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1030 | static Bool DRD_(thread_conflict_set_up_to_date)(const DrdThreadId tid) |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1031 | { |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1032 | static int do_verify_conflict_set = -1; |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1033 | Bool result; |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1034 | struct bitmap* computed_conflict_set = 0; |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1035 | |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1036 | if (do_verify_conflict_set < 0) |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1037 | { |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1038 | //VG_(message)(Vg_DebugMsg, "%s", VG_(getenv)("DRD_VERIFY_CONFLICT_SET")); |
| 1039 | do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0; |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1040 | } |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1041 | if (do_verify_conflict_set == 0) |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1042 | return True; |
| 1043 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1044 | DRD_(thread_compute_conflict_set)(&computed_conflict_set, tid); |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1045 | result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set); |
| 1046 | DRD_(bm_delete)(computed_conflict_set); |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1047 | return result; |
| 1048 | } |
| 1049 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1050 | /** |
| 1051 | * Compute a bitmap that represents the union of all memory accesses of all |
| 1052 | * segments that are unordered to the current segment of the thread tid. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1053 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1054 | static void DRD_(thread_compute_conflict_set)(struct bitmap** conflict_set, |
| 1055 | const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1056 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1057 | Segment* p; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1058 | |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 1059 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1060 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1061 | tl_assert(tid == DRD_(g_drd_running_tid)); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1062 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1063 | DRD_(s_update_conflict_set_count)++; |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1064 | DRD_(s_conflict_set_bitmap_creation_count) -= DRD_(bm_get_bitmap_creation_count)(); |
| 1065 | DRD_(s_conflict_set_bitmap2_creation_count) -= DRD_(bm_get_bitmap2_creation_count)(); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1066 | |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1067 | if (*conflict_set) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1068 | { |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1069 | DRD_(bm_delete)(*conflict_set); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1070 | } |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1071 | *conflict_set = DRD_(bm_new)(); |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1072 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1073 | if (DRD_(s_trace_conflict_set)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1074 | { |
| 1075 | char msg[256]; |
| 1076 | |
| 1077 | VG_(snprintf)(msg, sizeof(msg), |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1078 | "computing conflict set for thread %d/%d with vc ", |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1079 | DRD_(DrdThreadIdToVgThreadId)(tid), tid); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1080 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), |
| 1081 | sizeof(msg) - VG_(strlen)(msg), |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1082 | &DRD_(g_threadinfo)[tid].last->vc); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1083 | VG_(message)(Vg_UserMsg, "%s", msg); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1086 | p = DRD_(g_threadinfo)[tid].last; |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1087 | { |
| 1088 | unsigned j; |
| 1089 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1090 | if (DRD_(s_trace_conflict_set)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1091 | { |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1092 | char msg[256]; |
| 1093 | |
| 1094 | VG_(snprintf)(msg, sizeof(msg), |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1095 | "conflict set: thread [%d] at vc ", |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1096 | tid); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1097 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), |
| 1098 | sizeof(msg) - VG_(strlen)(msg), |
| 1099 | &p->vc); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1100 | VG_(message)(Vg_UserMsg, "%s", msg); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1101 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1102 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1103 | for (j = 0; j < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); j++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1104 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1105 | if (j != tid && DRD_(IsValidDrdThreadId)(j)) |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1106 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1107 | const Segment* q; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1108 | for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev) |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1109 | { |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1110 | if (! DRD_(vc_lte)(&q->vc, &p->vc) && ! DRD_(vc_lte)(&p->vc, &q->vc)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1111 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1112 | if (DRD_(s_trace_conflict_set)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1113 | { |
| 1114 | char msg[256]; |
| 1115 | VG_(snprintf)(msg, sizeof(msg), |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1116 | "conflict set: [%d] merging segment ", j); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1117 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), |
| 1118 | sizeof(msg) - VG_(strlen)(msg), |
| 1119 | &q->vc); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1120 | VG_(message)(Vg_UserMsg, "%s", msg); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1121 | } |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1122 | DRD_(bm_merge2)(*conflict_set, q->bm); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1123 | } |
| 1124 | else |
| 1125 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1126 | if (DRD_(s_trace_conflict_set)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1127 | { |
| 1128 | char msg[256]; |
| 1129 | VG_(snprintf)(msg, sizeof(msg), |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1130 | "conflict set: [%d] ignoring segment ", j); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1131 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), |
| 1132 | sizeof(msg) - VG_(strlen)(msg), |
| 1133 | &q->vc); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1134 | VG_(message)(Vg_UserMsg, "%s", msg); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1135 | } |
| 1136 | } |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1137 | } |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1138 | } |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1139 | } |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1140 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1141 | |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1142 | DRD_(s_conflict_set_bitmap_creation_count) += DRD_(bm_get_bitmap_creation_count)(); |
| 1143 | DRD_(s_conflict_set_bitmap2_creation_count) += DRD_(bm_get_bitmap2_creation_count)(); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1144 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1145 | if (0 && DRD_(s_trace_conflict_set)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1146 | { |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1147 | VG_(message)(Vg_UserMsg, "[%d] new conflict set:", tid); |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1148 | DRD_(bm_print)(*conflict_set); |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1149 | VG_(message)(Vg_UserMsg, "[%d] end of new conflict set.", tid); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1150 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1153 | ULong DRD_(thread_get_context_switch_count)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1154 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1155 | return DRD_(s_context_switch_count); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1158 | ULong DRD_(thread_get_discard_ordered_segments_count)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1159 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1160 | return DRD_(s_discard_ordered_segments_count); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1163 | ULong DRD_(thread_get_update_conflict_set_count)(ULong* dsnsc, ULong* dscvc) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1164 | { |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1165 | tl_assert(dsnsc); |
| 1166 | tl_assert(dscvc); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1167 | *dsnsc = DRD_(s_conflict_set_new_segment_count); |
| 1168 | *dscvc = DRD_(s_conflict_set_combine_vc_count); |
| 1169 | return DRD_(s_update_conflict_set_count); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1172 | ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1173 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1174 | return DRD_(s_conflict_set_bitmap_creation_count); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1177 | ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1178 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1179 | return DRD_(s_conflict_set_bitmap2_creation_count); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1180 | } |