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