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 | |
sewardj | b3a1e4b | 2015-08-21 11:32:26 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2015 Bart Van Assche <bvanassche@acm.org>. |
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 |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 36 | #include "pub_tool_libcassert.h" // tl_assert() |
| 37 | #include "pub_tool_libcbase.h" // VG_(strlen)() |
| 38 | #include "pub_tool_libcprint.h" // VG_(printf)() |
| 39 | #include "pub_tool_machine.h" |
| 40 | #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)() |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 41 | #include "pub_tool_options.h" // VG_(clo_backtrace_size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 42 | #include "pub_tool_threadstate.h" // VG_(get_pthread_id)() |
| 43 | |
bart | 32ba208 | 2008-06-05 08:53:42 +0000 | [diff] [blame] | 44 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 45 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 46 | /* Local functions. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 47 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 48 | static void thread_append_segment(const DrdThreadId tid, Segment* const sg); |
| 49 | static void thread_discard_segment(const DrdThreadId tid, Segment* const sg); |
| 50 | static void thread_compute_conflict_set(struct bitmap** conflict_set, |
| 51 | const DrdThreadId tid); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 52 | static Bool thread_conflict_set_up_to_date(const DrdThreadId tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 53 | |
| 54 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 55 | /* Local variables. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 56 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 57 | static ULong s_context_switch_count; |
| 58 | static ULong s_discard_ordered_segments_count; |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 59 | static ULong s_compute_conflict_set_count; |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 60 | static ULong s_update_conflict_set_count; |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 61 | static ULong s_update_conflict_set_new_sg_count; |
| 62 | static ULong s_update_conflict_set_sync_count; |
| 63 | static ULong s_update_conflict_set_join_count; |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 64 | static ULong s_conflict_set_bitmap_creation_count; |
| 65 | static ULong s_conflict_set_bitmap2_creation_count; |
| 66 | static ThreadId s_vg_running_tid = VG_INVALID_THREADID; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 67 | DrdThreadId DRD_(g_drd_running_tid) = DRD_INVALID_THREADID; |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 68 | ThreadInfo* DRD_(g_threadinfo); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 69 | struct bitmap* DRD_(g_conflict_set); |
bart | 9cdc083 | 2014-08-09 12:58:17 +0000 | [diff] [blame] | 70 | Bool DRD_(verify_conflict_set); |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 71 | static Bool s_trace_context_switches = False; |
| 72 | static Bool s_trace_conflict_set = False; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 73 | static Bool s_trace_conflict_set_bm = False; |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 74 | static Bool s_trace_fork_join = False; |
| 75 | static Bool s_segment_merging = True; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 76 | static Bool s_new_segments_since_last_merge; |
bart | 6f1d716 | 2009-06-24 18:34:10 +0000 | [diff] [blame] | 77 | static int s_segment_merge_interval = 10; |
bart | 6d956dc | 2011-07-28 09:54:37 +0000 | [diff] [blame] | 78 | static unsigned s_join_list_vol = 10; |
| 79 | static unsigned s_deletion_head; |
| 80 | static unsigned s_deletion_tail; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 81 | #if defined(VGO_solaris) |
| 82 | Bool DRD_(ignore_thread_creation) = True; |
| 83 | #else |
| 84 | Bool DRD_(ignore_thread_creation) = False; |
| 85 | #endif /* VGO_solaris */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 86 | |
| 87 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 88 | /* Function definitions. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 89 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 90 | /** Enables/disables context switch tracing. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 91 | void DRD_(thread_trace_context_switches)(const Bool t) |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 92 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 93 | tl_assert(t == False || t == True); |
| 94 | s_trace_context_switches = t; |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 95 | } |
| 96 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 97 | /** Enables/disables conflict set tracing. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 98 | void DRD_(thread_trace_conflict_set)(const Bool t) |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 99 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 100 | tl_assert(t == False || t == True); |
| 101 | s_trace_conflict_set = t; |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 102 | } |
| 103 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 104 | /** Enables/disables conflict set bitmap tracing. */ |
| 105 | void DRD_(thread_trace_conflict_set_bm)(const Bool t) |
| 106 | { |
| 107 | tl_assert(t == False || t == True); |
| 108 | s_trace_conflict_set_bm = t; |
| 109 | } |
| 110 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 111 | /** Report whether fork/join tracing is enabled. */ |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 112 | Bool DRD_(thread_get_trace_fork_join)(void) |
| 113 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 114 | return s_trace_fork_join; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 115 | } |
| 116 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 117 | /** Enables/disables fork/join tracing. */ |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 118 | void DRD_(thread_set_trace_fork_join)(const Bool t) |
| 119 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 120 | tl_assert(t == False || t == True); |
| 121 | s_trace_fork_join = t; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 122 | } |
| 123 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 124 | /** Enables/disables segment merging. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 125 | void DRD_(thread_set_segment_merging)(const Bool m) |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 126 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 127 | tl_assert(m == False || m == True); |
| 128 | s_segment_merging = m; |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 129 | } |
| 130 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 131 | /** Get the segment merging interval. */ |
| 132 | int DRD_(thread_get_segment_merge_interval)(void) |
| 133 | { |
| 134 | return s_segment_merge_interval; |
| 135 | } |
| 136 | |
| 137 | /** Set the segment merging interval. */ |
| 138 | void DRD_(thread_set_segment_merge_interval)(const int i) |
| 139 | { |
| 140 | s_segment_merge_interval = i; |
| 141 | } |
| 142 | |
bart | 6d956dc | 2011-07-28 09:54:37 +0000 | [diff] [blame] | 143 | void DRD_(thread_set_join_list_vol)(const int jlv) |
| 144 | { |
| 145 | s_join_list_vol = jlv; |
| 146 | } |
| 147 | |
bart | e278ab5 | 2012-01-24 18:28:55 +0000 | [diff] [blame] | 148 | void DRD_(thread_init)(void) |
| 149 | { |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 150 | DRD_(g_threadinfo) = VG_(malloc)("drd.main.ti.1", |
| 151 | DRD_N_THREADS * sizeof DRD_(g_threadinfo)[0]); |
| 152 | for (UInt i = 0; i < DRD_N_THREADS; ++i) { |
| 153 | static ThreadInfo initval; |
| 154 | DRD_(g_threadinfo)[i] = initval; |
| 155 | } |
bart | e278ab5 | 2012-01-24 18:28:55 +0000 | [diff] [blame] | 156 | } |
| 157 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 158 | /** |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 159 | * Convert Valgrind's ThreadId into a DrdThreadId. |
| 160 | * |
| 161 | * @return DRD thread ID upon success and DRD_INVALID_THREADID if the passed |
| 162 | * Valgrind ThreadId does not yet exist. |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 163 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 164 | DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 165 | { |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 166 | UInt i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 167 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 168 | if (tid == VG_INVALID_THREADID) |
| 169 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 170 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 171 | for (i = 1; i < DRD_N_THREADS; i++) |
| 172 | { |
| 173 | if (DRD_(g_threadinfo)[i].vg_thread_exists == True |
| 174 | && DRD_(g_threadinfo)[i].vg_threadid == tid) |
| 175 | { |
| 176 | return i; |
| 177 | } |
| 178 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 179 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 180 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 181 | } |
| 182 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 183 | /** Allocate a new DRD thread ID for the specified Valgrind thread ID. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 184 | static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 185 | { |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 186 | UInt i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 187 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 188 | tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 189 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 190 | for (i = 1; i < DRD_N_THREADS; i++) |
| 191 | { |
bart | 6d956dc | 2011-07-28 09:54:37 +0000 | [diff] [blame] | 192 | if (!DRD_(g_threadinfo)[i].valid) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 193 | { |
| 194 | tl_assert(! DRD_(IsValidDrdThreadId)(i)); |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 195 | |
bart | 6d956dc | 2011-07-28 09:54:37 +0000 | [diff] [blame] | 196 | DRD_(g_threadinfo)[i].valid = True; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 197 | DRD_(g_threadinfo)[i].vg_thread_exists = True; |
| 198 | DRD_(g_threadinfo)[i].vg_threadid = tid; |
| 199 | DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID; |
| 200 | DRD_(g_threadinfo)[i].stack_min = 0; |
| 201 | DRD_(g_threadinfo)[i].stack_min_min = 0; |
| 202 | DRD_(g_threadinfo)[i].stack_startup = 0; |
| 203 | DRD_(g_threadinfo)[i].stack_max = 0; |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 204 | DRD_(thread_set_name)(i, ""); |
bart | 383d613 | 2010-09-02 14:43:18 +0000 | [diff] [blame] | 205 | DRD_(g_threadinfo)[i].on_alt_stack = False; |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 206 | DRD_(g_threadinfo)[i].is_recording_loads = True; |
| 207 | DRD_(g_threadinfo)[i].is_recording_stores = True; |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 208 | DRD_(g_threadinfo)[i].pthread_create_nesting_level = 0; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 209 | DRD_(g_threadinfo)[i].synchr_nesting = 0; |
bart | 6d956dc | 2011-07-28 09:54:37 +0000 | [diff] [blame] | 210 | DRD_(g_threadinfo)[i].deletion_seq = s_deletion_tail - 1; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 211 | DRD_(g_threadinfo)[i].creator_thread = DRD_INVALID_THREADID; |
| 212 | #if defined (VGO_solaris) |
| 213 | DRD_(g_threadinfo)[i].bind_guard_flag = 0; |
| 214 | #endif /* VGO_solaris */ |
| 215 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 216 | tl_assert(DRD_(g_threadinfo)[i].sg_first == NULL); |
| 217 | tl_assert(DRD_(g_threadinfo)[i].sg_last == NULL); |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 218 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 219 | tl_assert(DRD_(IsValidDrdThreadId)(i)); |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 220 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 221 | return i; |
| 222 | } |
| 223 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 224 | |
bart | 3c9afb1 | 2009-07-24 11:11:30 +0000 | [diff] [blame] | 225 | VG_(printf)( |
| 226 | "\nSorry, but the maximum number of threads supported by DRD has been exceeded." |
| 227 | "Aborting.\n"); |
| 228 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 229 | tl_assert(False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 230 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 231 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 232 | } |
| 233 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 234 | /** Convert a POSIX thread ID into a DRD thread ID. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 235 | DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 236 | { |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 237 | UInt i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 238 | |
bart | b48bde2 | 2009-07-31 08:26:17 +0000 | [diff] [blame] | 239 | if (tid != INVALID_POSIX_THREADID) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 240 | { |
bart | b48bde2 | 2009-07-31 08:26:17 +0000 | [diff] [blame] | 241 | for (i = 1; i < DRD_N_THREADS; i++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 242 | { |
bart | b48bde2 | 2009-07-31 08:26:17 +0000 | [diff] [blame] | 243 | if (DRD_(g_threadinfo)[i].posix_thread_exists |
| 244 | && DRD_(g_threadinfo)[i].pt_threadid == tid) |
| 245 | { |
| 246 | return i; |
| 247 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 251 | } |
| 252 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 253 | /** Convert a DRD thread ID into a Valgrind thread ID. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 254 | ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 255 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 256 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 257 | && tid != DRD_INVALID_THREADID); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 258 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 259 | return (DRD_(g_threadinfo)[tid].vg_thread_exists |
| 260 | ? DRD_(g_threadinfo)[tid].vg_threadid |
| 261 | : VG_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 262 | } |
| 263 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 264 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 265 | /** |
| 266 | * Sanity check of the doubly linked list of segments referenced by a |
| 267 | * ThreadInfo struct. |
| 268 | * @return True if sane, False if not. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 269 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 270 | static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 271 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 272 | Segment* p; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 273 | |
bart | 2bf4bb9 | 2014-03-10 18:58:19 +0000 | [diff] [blame] | 274 | for (p = ti->sg_first; p; p = p->thr_next) { |
| 275 | if (p->thr_next && p->thr_next->thr_prev != p) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 276 | return False; |
bart | 2bf4bb9 | 2014-03-10 18:58:19 +0000 | [diff] [blame] | 277 | if (p->thr_next == 0 && p != ti->sg_last) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 278 | return False; |
| 279 | } |
bart | 2bf4bb9 | 2014-03-10 18:58:19 +0000 | [diff] [blame] | 280 | for (p = ti->sg_last; p; p = p->thr_prev) { |
| 281 | if (p->thr_prev && p->thr_prev->thr_next != p) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 282 | return False; |
bart | 2bf4bb9 | 2014-03-10 18:58:19 +0000 | [diff] [blame] | 283 | if (p->thr_prev == 0 && p != ti->sg_first) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 284 | return False; |
| 285 | } |
| 286 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 287 | } |
bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 288 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 289 | |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 290 | /** |
| 291 | * Create the first segment for a newly started thread. |
| 292 | * |
| 293 | * This function is called from the handler installed via |
| 294 | * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler |
| 295 | * from the context of the creator thread, before the new thread has been |
| 296 | * created. |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 297 | * |
| 298 | * @param[in] creator DRD thread ID of the creator thread. |
| 299 | * @param[in] vg_created Valgrind thread ID of the created thread. |
| 300 | * |
| 301 | * @return DRD thread ID of the created thread. |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 302 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 303 | DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator, |
| 304 | const ThreadId vg_created) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 305 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 306 | DrdThreadId created; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 307 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 308 | tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID); |
| 309 | created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created); |
| 310 | tl_assert(0 <= (int)created && created < DRD_N_THREADS |
| 311 | && created != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 312 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 313 | tl_assert(DRD_(g_threadinfo)[created].sg_first == NULL); |
| 314 | tl_assert(DRD_(g_threadinfo)[created].sg_last == NULL); |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 315 | |
| 316 | if (creator != DRD_INVALID_THREADID) { |
| 317 | if (DRD_(ignore_thread_creation)) { |
| 318 | tl_assert(DRD_(thread_get_synchr_nesting_count)(created) == 0); |
| 319 | DRD_(thread_enter_synchr)(created); |
| 320 | /* Counterpart in DRD_(thread_set_pthreadid)(). */ |
| 321 | } |
| 322 | } |
| 323 | DRD_(g_threadinfo)[created].creator_thread = creator; |
| 324 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 325 | /* Create an initial segment for the newly created thread. */ |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 326 | thread_append_segment(created, DRD_(sg_new)(creator, created)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 327 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 328 | return created; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 329 | } |
| 330 | |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 331 | /** |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 332 | * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called |
| 333 | * after the thread has been created and before any client instructions are run |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 334 | * on the newly created thread, e.g. from the handler installed via |
| 335 | * VG_(track_pre_thread_first_insn)(). |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 336 | * |
| 337 | * @param[in] vg_created Valgrind thread ID of the newly created thread. |
| 338 | * |
| 339 | * @return DRD thread ID for the new thread. |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 340 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 341 | DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created) |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 342 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 343 | const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created); |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 344 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 345 | tl_assert(0 <= (int)created && created < DRD_N_THREADS |
| 346 | && created != DRD_INVALID_THREADID); |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 347 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 348 | DRD_(g_threadinfo)[created].stack_max |
| 349 | = VG_(thread_get_stack_max)(vg_created); |
| 350 | DRD_(g_threadinfo)[created].stack_startup |
| 351 | = DRD_(g_threadinfo)[created].stack_max; |
| 352 | DRD_(g_threadinfo)[created].stack_min |
| 353 | = DRD_(g_threadinfo)[created].stack_max; |
| 354 | DRD_(g_threadinfo)[created].stack_min_min |
| 355 | = DRD_(g_threadinfo)[created].stack_max; |
| 356 | DRD_(g_threadinfo)[created].stack_size |
| 357 | = VG_(thread_get_stack_size)(vg_created); |
| 358 | tl_assert(DRD_(g_threadinfo)[created].stack_max != 0); |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 359 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 360 | return created; |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 361 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 362 | |
bart | 6d956dc | 2011-07-28 09:54:37 +0000 | [diff] [blame] | 363 | static void DRD_(thread_delayed_delete)(const DrdThreadId tid) |
| 364 | { |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 365 | UInt j; |
bart | 6d956dc | 2011-07-28 09:54:37 +0000 | [diff] [blame] | 366 | |
| 367 | DRD_(g_threadinfo)[tid].vg_thread_exists = False; |
| 368 | DRD_(g_threadinfo)[tid].posix_thread_exists = False; |
| 369 | DRD_(g_threadinfo)[tid].deletion_seq = s_deletion_head++; |
| 370 | #if 0 |
| 371 | VG_(message)(Vg_DebugMsg, "Adding thread %d to the deletion list\n", tid); |
| 372 | #endif |
| 373 | if (s_deletion_head - s_deletion_tail >= s_join_list_vol) { |
| 374 | for (j = 0; j < DRD_N_THREADS; ++j) { |
| 375 | if (DRD_(IsValidDrdThreadId)(j) |
| 376 | && DRD_(g_threadinfo)[j].deletion_seq == s_deletion_tail) |
| 377 | { |
| 378 | s_deletion_tail++; |
| 379 | #if 0 |
| 380 | VG_(message)(Vg_DebugMsg, "Delayed delete of thread %d\n", j); |
| 381 | #endif |
| 382 | DRD_(thread_delete)(j, False); |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 389 | /** |
| 390 | * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just |
| 391 | * after thread drd_joiner joined thread drd_joinee. |
| 392 | */ |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 393 | void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee) |
| 394 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 395 | tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner)); |
| 396 | tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee)); |
bart | 7627be3 | 2009-06-06 12:26:05 +0000 | [diff] [blame] | 397 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 398 | DRD_(thread_new_segment)(drd_joiner); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 399 | DRD_(thread_combine_vc_join)(drd_joiner, drd_joinee); |
bart | 7627be3 | 2009-06-06 12:26:05 +0000 | [diff] [blame] | 400 | DRD_(thread_new_segment)(drd_joinee); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 401 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 402 | if (s_trace_fork_join) |
| 403 | { |
| 404 | const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 405 | const unsigned msg_size = 256; |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 406 | HChar* msg; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 407 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 408 | msg = VG_(malloc)("drd.main.dptj.1", msg_size); |
florian | f5d8e65 | 2014-09-11 22:15:39 +0000 | [diff] [blame] | 409 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 410 | VG_(snprintf)(msg, msg_size, |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 411 | "drd_post_thread_join joiner = %u, joinee = %u", |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 412 | drd_joiner, drd_joinee); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 413 | if (joiner) |
| 414 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 415 | HChar* vc; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 416 | |
| 417 | vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(drd_joiner)); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 418 | VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg), |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 419 | ", new vc: %s", vc); |
| 420 | VG_(free)(vc); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 421 | } |
bart | ad994e8 | 2011-10-13 18:04:30 +0000 | [diff] [blame] | 422 | DRD_(trace_msg)("%pS", msg); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 423 | VG_(free)(msg); |
| 424 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 425 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 426 | if (! DRD_(get_check_stack_accesses)()) |
| 427 | { |
| 428 | DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee) |
| 429 | - DRD_(thread_get_stack_size)(drd_joinee), |
| 430 | DRD_(thread_get_stack_max)(drd_joinee)); |
| 431 | } |
| 432 | DRD_(clientobj_delete_thread)(drd_joinee); |
bart | 6d956dc | 2011-07-28 09:54:37 +0000 | [diff] [blame] | 433 | DRD_(thread_delayed_delete)(drd_joinee); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 434 | } |
| 435 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 436 | /** |
| 437 | * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack, |
| 438 | * and accesses this data structure from multiple threads without locking. |
| 439 | * Any conflicting accesses in the range stack_startup..stack_max will be |
| 440 | * ignored. |
| 441 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 442 | void DRD_(thread_set_stack_startup)(const DrdThreadId tid, |
| 443 | const Addr stack_startup) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 444 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 445 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 446 | && tid != DRD_INVALID_THREADID); |
| 447 | tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup); |
| 448 | tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max); |
| 449 | DRD_(g_threadinfo)[tid].stack_startup = stack_startup; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 450 | } |
| 451 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 452 | /** Return the stack pointer for the specified thread. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 453 | Addr DRD_(thread_get_stack_min)(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 454 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 455 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 456 | && tid != DRD_INVALID_THREADID); |
| 457 | return DRD_(g_threadinfo)[tid].stack_min; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 458 | } |
| 459 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 460 | /** |
| 461 | * Return the lowest value that was ever assigned to the stack pointer |
| 462 | * for the specified thread. |
| 463 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 464 | Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid) |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 465 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 466 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 467 | && tid != DRD_INVALID_THREADID); |
| 468 | return DRD_(g_threadinfo)[tid].stack_min_min; |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 469 | } |
| 470 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 471 | /** Return the top address for the stack of the specified thread. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 472 | Addr DRD_(thread_get_stack_max)(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 473 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 474 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 475 | && tid != DRD_INVALID_THREADID); |
| 476 | return DRD_(g_threadinfo)[tid].stack_max; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 477 | } |
| 478 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 479 | /** Return the maximum stack size for the specified thread. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 480 | SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid) |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 481 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 482 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 483 | && tid != DRD_INVALID_THREADID); |
| 484 | return DRD_(g_threadinfo)[tid].stack_size; |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 485 | } |
| 486 | |
bart | 383d613 | 2010-09-02 14:43:18 +0000 | [diff] [blame] | 487 | Bool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid) |
| 488 | { |
| 489 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 490 | && tid != DRD_INVALID_THREADID); |
| 491 | return DRD_(g_threadinfo)[tid].on_alt_stack; |
| 492 | } |
| 493 | |
| 494 | void DRD_(thread_set_on_alt_stack)(const DrdThreadId tid, |
| 495 | const Bool on_alt_stack) |
| 496 | { |
| 497 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 498 | && tid != DRD_INVALID_THREADID); |
| 499 | tl_assert(on_alt_stack == !!on_alt_stack); |
| 500 | DRD_(g_threadinfo)[tid].on_alt_stack = on_alt_stack; |
| 501 | } |
| 502 | |
| 503 | Int DRD_(thread_get_threads_on_alt_stack)(void) |
| 504 | { |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 505 | int n = 0; |
bart | 383d613 | 2010-09-02 14:43:18 +0000 | [diff] [blame] | 506 | |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 507 | for (UInt i = 1; i < DRD_N_THREADS; i++) |
bart | 383d613 | 2010-09-02 14:43:18 +0000 | [diff] [blame] | 508 | n += DRD_(g_threadinfo)[i].on_alt_stack; |
| 509 | return n; |
| 510 | } |
| 511 | |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 512 | /** |
bart | 6d956dc | 2011-07-28 09:54:37 +0000 | [diff] [blame] | 513 | * Clean up thread-specific data structures. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 514 | */ |
bart | 9194e93 | 2011-02-09 11:55:12 +0000 | [diff] [blame] | 515 | void DRD_(thread_delete)(const DrdThreadId tid, const Bool detached) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 516 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 517 | Segment* sg; |
| 518 | Segment* sg_prev; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 519 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 520 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 521 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 522 | tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0); |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 523 | for (sg = DRD_(g_threadinfo)[tid].sg_last; sg; sg = sg_prev) { |
| 524 | sg_prev = sg->thr_prev; |
| 525 | sg->thr_next = NULL; |
| 526 | sg->thr_prev = NULL; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 527 | DRD_(sg_put)(sg); |
| 528 | } |
bart | 6d956dc | 2011-07-28 09:54:37 +0000 | [diff] [blame] | 529 | DRD_(g_threadinfo)[tid].valid = False; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 530 | DRD_(g_threadinfo)[tid].vg_thread_exists = False; |
| 531 | DRD_(g_threadinfo)[tid].posix_thread_exists = False; |
bart | 9194e93 | 2011-02-09 11:55:12 +0000 | [diff] [blame] | 532 | if (detached) |
| 533 | DRD_(g_threadinfo)[tid].detached_posix_thread = False; |
| 534 | else |
| 535 | tl_assert(!DRD_(g_threadinfo)[tid].detached_posix_thread); |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 536 | DRD_(g_threadinfo)[tid].sg_first = NULL; |
| 537 | DRD_(g_threadinfo)[tid].sg_last = NULL; |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 538 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 539 | tl_assert(!DRD_(IsValidDrdThreadId)(tid)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 540 | } |
| 541 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 542 | /** |
| 543 | * Called after a thread performed its last memory access and before |
| 544 | * thread_delete() is called. Note: thread_delete() is only called for |
| 545 | * joinable threads, not for detached threads. |
| 546 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 547 | void DRD_(thread_finished)(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 548 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 549 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 550 | && tid != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 551 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 552 | DRD_(g_threadinfo)[tid].vg_thread_exists = False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 553 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 554 | if (DRD_(g_threadinfo)[tid].detached_posix_thread) |
| 555 | { |
| 556 | /* |
| 557 | * Once a detached thread has finished, its stack is deallocated and |
| 558 | * should no longer be taken into account when computing the conflict set. |
| 559 | */ |
| 560 | DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 561 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 562 | /* |
| 563 | * For a detached thread, calling pthread_exit() invalidates the |
| 564 | * POSIX thread ID associated with the detached thread. For joinable |
| 565 | * POSIX threads however, the POSIX thread ID remains live after the |
| 566 | * pthread_exit() call until pthread_join() is called. |
| 567 | */ |
| 568 | DRD_(g_threadinfo)[tid].posix_thread_exists = False; |
| 569 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 570 | } |
| 571 | |
bart | 5c7e6b6 | 2011-02-03 17:47:50 +0000 | [diff] [blame] | 572 | /** Called just after fork() in the child process. */ |
| 573 | void DRD_(drd_thread_atfork_child)(const DrdThreadId tid) |
| 574 | { |
| 575 | unsigned i; |
| 576 | |
| 577 | for (i = 1; i < DRD_N_THREADS; i++) |
| 578 | { |
| 579 | if (i == tid) |
| 580 | continue; |
| 581 | if (DRD_(IsValidDrdThreadId(i))) |
bart | 9194e93 | 2011-02-09 11:55:12 +0000 | [diff] [blame] | 582 | DRD_(thread_delete)(i, True); |
bart | 5c7e6b6 | 2011-02-03 17:47:50 +0000 | [diff] [blame] | 583 | tl_assert(!DRD_(IsValidDrdThreadId(i))); |
bart | f7a5b3f | 2011-12-11 20:34:03 +0000 | [diff] [blame] | 584 | } |
bart | 7692f16 | 2014-08-08 16:27:30 +0000 | [diff] [blame] | 585 | |
| 586 | DRD_(bm_cleanup)(DRD_(g_conflict_set)); |
| 587 | DRD_(bm_init)(DRD_(g_conflict_set)); |
bart | 5c7e6b6 | 2011-02-03 17:47:50 +0000 | [diff] [blame] | 588 | } |
| 589 | |
bart | 9b2974a | 2008-09-27 12:35:31 +0000 | [diff] [blame] | 590 | /** Called just before pthread_cancel(). */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 591 | void DRD_(thread_pre_cancel)(const DrdThreadId tid) |
bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 592 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 593 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 594 | && tid != DRD_INVALID_THREADID); |
| 595 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 596 | |
bart | 0c0cd77 | 2011-03-03 19:59:20 +0000 | [diff] [blame] | 597 | if (DRD_(thread_get_trace_fork_join)()) |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 598 | DRD_(trace_msg)("[%u] drd_thread_pre_cancel %u", |
bart | b92ff0f | 2011-10-08 08:29:29 +0000 | [diff] [blame] | 599 | DRD_(g_drd_running_tid), tid); |
bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 600 | } |
| 601 | |
bart | e7dff24 | 2009-04-23 17:12:39 +0000 | [diff] [blame] | 602 | /** |
| 603 | * Store the POSIX thread ID for the specified thread. |
| 604 | * |
| 605 | * @note This function can be called two times for the same thread -- see also |
| 606 | * the comment block preceding the pthread_create() wrapper in |
| 607 | * drd_pthread_intercepts.c. |
| 608 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 609 | void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 610 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 611 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 612 | && tid != DRD_INVALID_THREADID); |
bart | e7dff24 | 2009-04-23 17:12:39 +0000 | [diff] [blame] | 613 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID |
| 614 | || DRD_(g_threadinfo)[tid].pt_threadid == ptid); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 615 | tl_assert(ptid != INVALID_POSIX_THREADID); |
| 616 | DRD_(g_threadinfo)[tid].posix_thread_exists = True; |
| 617 | DRD_(g_threadinfo)[tid].pt_threadid = ptid; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 618 | |
| 619 | if (DRD_(g_threadinfo)[tid].creator_thread != DRD_INVALID_THREADID) { |
| 620 | if (DRD_(ignore_thread_creation)) { |
| 621 | DRD_(thread_leave_synchr)(tid); |
| 622 | tl_assert(DRD_(thread_get_synchr_nesting_count)(tid) == 0); |
| 623 | } |
| 624 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 625 | } |
| 626 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 627 | /** Returns true for joinable threads and false for detached threads. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 628 | Bool DRD_(thread_get_joinable)(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 629 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 630 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 631 | && tid != DRD_INVALID_THREADID); |
| 632 | return ! DRD_(g_threadinfo)[tid].detached_posix_thread; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 633 | } |
| 634 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 635 | /** Store the thread mode: joinable or detached. */ |
petarj | 4df0bfc | 2013-02-27 23:17:33 +0000 | [diff] [blame] | 636 | #if defined(VGP_mips32_linux) || defined(VGP_mips64_linux) |
sewardj | 5db1540 | 2012-06-07 09:13:21 +0000 | [diff] [blame] | 637 | /* There is a cse related issue in gcc for MIPS. Optimization level |
| 638 | has to be lowered, so cse related optimizations are not |
| 639 | included.*/ |
| 640 | __attribute__((optimize("O1"))) |
| 641 | #endif |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 642 | void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 643 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 644 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 645 | && tid != DRD_INVALID_THREADID); |
sewardj | d56b7e2 | 2015-01-20 00:12:18 +0000 | [diff] [blame] | 646 | tl_assert((!! joinable) == joinable); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 647 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 648 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 649 | DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 650 | } |
| 651 | |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 652 | /** Tells DRD that the calling thread is about to enter pthread_create(). */ |
| 653 | void DRD_(thread_entering_pthread_create)(const DrdThreadId tid) |
| 654 | { |
| 655 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 656 | && tid != DRD_INVALID_THREADID); |
| 657 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
| 658 | tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level >= 0); |
| 659 | |
| 660 | DRD_(g_threadinfo)[tid].pthread_create_nesting_level++; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 661 | |
| 662 | if (DRD_(ignore_thread_creation)) { |
| 663 | tl_assert(DRD_(thread_get_synchr_nesting_count)(tid) == 0); |
| 664 | DRD_(thread_enter_synchr)(tid); |
| 665 | } |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /** Tells DRD that the calling thread has left pthread_create(). */ |
| 669 | void DRD_(thread_left_pthread_create)(const DrdThreadId tid) |
| 670 | { |
| 671 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 672 | && tid != DRD_INVALID_THREADID); |
| 673 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
| 674 | tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level > 0); |
| 675 | |
| 676 | DRD_(g_threadinfo)[tid].pthread_create_nesting_level--; |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 677 | |
| 678 | if (DRD_(ignore_thread_creation)) { |
| 679 | DRD_(thread_leave_synchr)(tid); |
| 680 | tl_assert(DRD_(thread_get_synchr_nesting_count)(tid) == 0); |
| 681 | } |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 682 | } |
| 683 | |
sewardj | 8eb8bab | 2015-07-21 14:44:28 +0000 | [diff] [blame] | 684 | #if defined(VGO_solaris) |
| 685 | /** Handles the bind_guard() intercept. */ |
| 686 | void DRD_(thread_entering_rtld_bind_guard)(const DrdThreadId tid, int flags) |
| 687 | { |
| 688 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 689 | && tid != DRD_INVALID_THREADID); |
| 690 | |
| 691 | Int bindflag = (flags & VKI_THR_FLG_RTLD); |
| 692 | if ((bindflag & DRD_(g_threadinfo)[tid].bind_guard_flag) == 0) { |
| 693 | DRD_(g_threadinfo)[tid].bind_guard_flag |= bindflag; |
| 694 | DRD_(thread_enter_synchr)(tid); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Handles the bind_clear() intercept. |
| 700 | * Call to bind_clear(0) is typically used to determine value of bind_flags. |
| 701 | */ |
| 702 | void DRD_(thread_leaving_rtld_bind_clear)(const DrdThreadId tid, int flags) |
| 703 | { |
| 704 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 705 | && tid != DRD_INVALID_THREADID); |
| 706 | |
| 707 | Int bindflag = (flags & VKI_THR_FLG_RTLD); |
| 708 | if ((DRD_(g_threadinfo)[tid].bind_guard_flag & bindflag) != 0) { |
| 709 | DRD_(g_threadinfo)[tid].bind_guard_flag &= ~bindflag; |
| 710 | DRD_(thread_leave_synchr)(tid); |
| 711 | } |
| 712 | } |
| 713 | #endif /* VGO_solaris */ |
| 714 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 715 | /** Obtain the thread number and the user-assigned thread name. */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 716 | const HChar* DRD_(thread_get_name)(const DrdThreadId tid) |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 717 | { |
| 718 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 719 | && tid != DRD_INVALID_THREADID); |
| 720 | |
| 721 | return DRD_(g_threadinfo)[tid].name; |
| 722 | } |
| 723 | |
| 724 | /** Set the name of the specified thread. */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 725 | void DRD_(thread_set_name)(const DrdThreadId tid, const HChar* const name) |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 726 | { |
| 727 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 728 | && tid != DRD_INVALID_THREADID); |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 729 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 730 | if (name == NULL || name[0] == 0) |
| 731 | VG_(snprintf)(DRD_(g_threadinfo)[tid].name, |
| 732 | sizeof(DRD_(g_threadinfo)[tid].name), |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 733 | "Thread %u", |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 734 | tid); |
| 735 | else |
| 736 | VG_(snprintf)(DRD_(g_threadinfo)[tid].name, |
| 737 | sizeof(DRD_(g_threadinfo)[tid].name), |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 738 | "Thread %u (%s)", |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 739 | tid, name); |
| 740 | DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0; |
| 741 | } |
| 742 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 743 | /** |
| 744 | * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the |
| 745 | * conflict set. |
| 746 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 747 | void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 748 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 749 | tl_assert(vg_tid != VG_INVALID_THREADID); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 750 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 751 | if (vg_tid != s_vg_running_tid) |
| 752 | { |
| 753 | DRD_(thread_set_running_tid)(vg_tid, |
| 754 | DRD_(VgThreadIdToDrdThreadId)(vg_tid)); |
| 755 | } |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 756 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 757 | tl_assert(s_vg_running_tid != VG_INVALID_THREADID); |
| 758 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 759 | } |
| 760 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 761 | /** |
| 762 | * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the |
| 763 | * conflict set. |
| 764 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 765 | void DRD_(thread_set_running_tid)(const ThreadId vg_tid, |
| 766 | const DrdThreadId drd_tid) |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 767 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 768 | tl_assert(vg_tid != VG_INVALID_THREADID); |
| 769 | tl_assert(drd_tid != DRD_INVALID_THREADID); |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 770 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 771 | if (vg_tid != s_vg_running_tid) |
| 772 | { |
| 773 | if (s_trace_context_switches |
| 774 | && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID) |
| 775 | { |
| 776 | VG_(message)(Vg_DebugMsg, |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 777 | "Context switch from thread %u to thread %u;" |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 778 | " segments: %llu\n", |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 779 | DRD_(g_drd_running_tid), drd_tid, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 780 | DRD_(sg_get_segments_alive_count)()); |
| 781 | } |
| 782 | s_vg_running_tid = vg_tid; |
| 783 | DRD_(g_drd_running_tid) = drd_tid; |
| 784 | thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid); |
| 785 | s_context_switch_count++; |
| 786 | } |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 787 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 788 | tl_assert(s_vg_running_tid != VG_INVALID_THREADID); |
| 789 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 790 | } |
| 791 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 792 | /** |
| 793 | * Increase the synchronization nesting counter. Must be called before the |
| 794 | * client calls a synchronization function. |
| 795 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 796 | int DRD_(thread_enter_synchr)(const DrdThreadId tid) |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 797 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 798 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| 799 | return DRD_(g_threadinfo)[tid].synchr_nesting++; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 800 | } |
| 801 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 802 | /** |
| 803 | * Decrease the synchronization nesting counter. Must be called after the |
| 804 | * client left a synchronization function. |
| 805 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 806 | int DRD_(thread_leave_synchr)(const DrdThreadId tid) |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 807 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 808 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| 809 | tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1); |
| 810 | return --DRD_(g_threadinfo)[tid].synchr_nesting; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 811 | } |
| 812 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 813 | /** Returns the synchronization nesting counter. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 814 | int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid) |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 815 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 816 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| 817 | return DRD_(g_threadinfo)[tid].synchr_nesting; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 818 | } |
| 819 | |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 820 | /** Append a new segment at the end of the segment list. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 821 | static |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 822 | void thread_append_segment(const DrdThreadId tid, Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 823 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 824 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 825 | && tid != DRD_INVALID_THREADID); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 826 | |
| 827 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 828 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| 829 | #endif |
| 830 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 831 | // add at tail |
| 832 | sg->thr_prev = DRD_(g_threadinfo)[tid].sg_last; |
| 833 | sg->thr_next = NULL; |
| 834 | if (DRD_(g_threadinfo)[tid].sg_last) |
| 835 | DRD_(g_threadinfo)[tid].sg_last->thr_next = sg; |
| 836 | DRD_(g_threadinfo)[tid].sg_last = sg; |
| 837 | if (DRD_(g_threadinfo)[tid].sg_first == NULL) |
| 838 | DRD_(g_threadinfo)[tid].sg_first = sg; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 839 | |
| 840 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 841 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| 842 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 843 | } |
| 844 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 845 | /** |
| 846 | * Remove a segment from the segment list of thread threadid, and free the |
| 847 | * associated memory. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 848 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 849 | static |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 850 | void thread_discard_segment(const DrdThreadId tid, Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 851 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 852 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 853 | && tid != DRD_INVALID_THREADID); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 854 | |
| 855 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 856 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| 857 | #endif |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 858 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 859 | if (sg->thr_prev) |
| 860 | sg->thr_prev->thr_next = sg->thr_next; |
| 861 | if (sg->thr_next) |
| 862 | sg->thr_next->thr_prev = sg->thr_prev; |
| 863 | if (sg == DRD_(g_threadinfo)[tid].sg_first) |
| 864 | DRD_(g_threadinfo)[tid].sg_first = sg->thr_next; |
| 865 | if (sg == DRD_(g_threadinfo)[tid].sg_last) |
| 866 | DRD_(g_threadinfo)[tid].sg_last = sg->thr_prev; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 867 | DRD_(sg_put)(sg); |
bart | 3f74967 | 2008-03-22 09:49:40 +0000 | [diff] [blame] | 868 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 869 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 870 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| 871 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 872 | } |
| 873 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 874 | /** |
| 875 | * Returns a pointer to the vector clock of the most recent segment associated |
| 876 | * with thread 'tid'. |
| 877 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 878 | VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 879 | { |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 880 | Segment* latest_sg; |
bart | e278ab5 | 2012-01-24 18:28:55 +0000 | [diff] [blame] | 881 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 882 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 883 | && tid != DRD_INVALID_THREADID); |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 884 | latest_sg = DRD_(g_threadinfo)[tid].sg_last; |
| 885 | tl_assert(latest_sg); |
| 886 | return &latest_sg->vc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 887 | } |
| 888 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 889 | /** |
| 890 | * Return the latest segment of thread 'tid' and increment its reference count. |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 891 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 892 | void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid) |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 893 | { |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 894 | Segment* latest_sg; |
bart | e278ab5 | 2012-01-24 18:28:55 +0000 | [diff] [blame] | 895 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 896 | tl_assert(sg); |
| 897 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 898 | && tid != DRD_INVALID_THREADID); |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 899 | latest_sg = DRD_(g_threadinfo)[tid].sg_last; |
| 900 | tl_assert(latest_sg); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 901 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 902 | DRD_(sg_put)(*sg); |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 903 | *sg = DRD_(sg_get)(latest_sg); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 904 | } |
| 905 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 906 | /** |
| 907 | * Compute the minimum of all latest vector clocks of all threads |
| 908 | * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA). |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 909 | * |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 910 | * @param vc pointer to a vectorclock, holds result upon return. |
| 911 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 912 | static void DRD_(thread_compute_minimum_vc)(VectorClock* vc) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 913 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 914 | unsigned i; |
| 915 | Bool first; |
| 916 | Segment* latest_sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 917 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 918 | first = True; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 919 | for (i = 0; i < DRD_N_THREADS; i++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 920 | { |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 921 | latest_sg = DRD_(g_threadinfo)[i].sg_last; |
| 922 | if (latest_sg) { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 923 | if (first) |
| 924 | DRD_(vc_assign)(vc, &latest_sg->vc); |
| 925 | else |
| 926 | DRD_(vc_min)(vc, &latest_sg->vc); |
| 927 | first = False; |
| 928 | } |
| 929 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 930 | } |
| 931 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 932 | /** |
| 933 | * Compute the maximum of all latest vector clocks of all threads. |
| 934 | * |
| 935 | * @param vc pointer to a vectorclock, holds result upon return. |
| 936 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 937 | static void DRD_(thread_compute_maximum_vc)(VectorClock* vc) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 938 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 939 | unsigned i; |
| 940 | Bool first; |
| 941 | Segment* latest_sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 942 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 943 | first = True; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 944 | for (i = 0; i < DRD_N_THREADS; i++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 945 | { |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 946 | latest_sg = DRD_(g_threadinfo)[i].sg_last; |
| 947 | if (latest_sg) { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 948 | if (first) |
| 949 | DRD_(vc_assign)(vc, &latest_sg->vc); |
| 950 | else |
| 951 | DRD_(vc_combine)(vc, &latest_sg->vc); |
| 952 | first = False; |
| 953 | } |
| 954 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | /** |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 958 | * Discard all segments that have a defined order against the latest vector |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 959 | * clock of all threads -- these segments can no longer be involved in a |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 960 | * data race. |
| 961 | */ |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 962 | static void thread_discard_ordered_segments(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 963 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 964 | unsigned i; |
| 965 | VectorClock thread_vc_min; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 966 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 967 | s_discard_ordered_segments_count++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 968 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 969 | DRD_(vc_init)(&thread_vc_min, 0, 0); |
| 970 | DRD_(thread_compute_minimum_vc)(&thread_vc_min); |
| 971 | if (DRD_(sg_get_trace)()) |
| 972 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 973 | HChar *vc_min, *vc_max; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 974 | VectorClock thread_vc_max; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 975 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 976 | DRD_(vc_init)(&thread_vc_max, 0, 0); |
| 977 | DRD_(thread_compute_maximum_vc)(&thread_vc_max); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 978 | vc_min = DRD_(vc_aprint)(&thread_vc_min); |
| 979 | vc_max = DRD_(vc_aprint)(&thread_vc_max); |
| 980 | VG_(message)(Vg_DebugMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 981 | "Discarding ordered segments -- min vc is %s, max vc is %s\n", |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 982 | vc_min, vc_max); |
| 983 | VG_(free)(vc_min); |
| 984 | VG_(free)(vc_max); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 985 | DRD_(vc_cleanup)(&thread_vc_max); |
| 986 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 987 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 988 | for (i = 0; i < DRD_N_THREADS; i++) { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 989 | Segment* sg; |
| 990 | Segment* sg_next; |
bart | e278ab5 | 2012-01-24 18:28:55 +0000 | [diff] [blame] | 991 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 992 | for (sg = DRD_(g_threadinfo)[i].sg_first; |
| 993 | sg && (sg_next = sg->thr_next) |
| 994 | && DRD_(vc_lte)(&sg->vc, &thread_vc_min); |
| 995 | sg = sg_next) |
| 996 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 997 | thread_discard_segment(i, sg); |
| 998 | } |
| 999 | } |
| 1000 | DRD_(vc_cleanup)(&thread_vc_min); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1003 | /** |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1004 | * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper |
| 1005 | * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if |
| 1006 | * all segments in the set CS are ordered consistently against both sg1 and |
| 1007 | * sg2. The set CS is defined as the set of segments that can immediately |
| 1008 | * precede future segments via inter-thread synchronization operations. In |
| 1009 | * DRD the set CS consists of the latest segment of each thread combined with |
| 1010 | * all segments for which the reference count is strictly greater than one. |
| 1011 | * The code below is an optimized version of the following: |
| 1012 | * |
| 1013 | * for (i = 0; i < DRD_N_THREADS; i++) |
| 1014 | * { |
| 1015 | * Segment* sg; |
| 1016 | * |
| 1017 | * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next) |
| 1018 | * { |
| 1019 | * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1) |
| 1020 | * { |
| 1021 | * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc) |
| 1022 | * != DRD_(vc_lte)(&sg2->vc, &sg->vc) |
| 1023 | * || DRD_(vc_lte)(&sg->vc, &sg1->vc) |
| 1024 | * != DRD_(vc_lte)(&sg->vc, &sg2->vc)) |
| 1025 | * { |
| 1026 | * return False; |
| 1027 | * } |
| 1028 | * } |
| 1029 | * } |
| 1030 | * } |
| 1031 | */ |
| 1032 | static Bool thread_consistent_segment_ordering(const DrdThreadId tid, |
| 1033 | Segment* const sg1, |
| 1034 | Segment* const sg2) |
| 1035 | { |
| 1036 | unsigned i; |
| 1037 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1038 | tl_assert(sg1->thr_next); |
| 1039 | tl_assert(sg2->thr_next); |
| 1040 | tl_assert(sg1->thr_next == sg2); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1041 | tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc)); |
| 1042 | |
| 1043 | for (i = 0; i < DRD_N_THREADS; i++) |
| 1044 | { |
| 1045 | Segment* sg; |
| 1046 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1047 | for (sg = DRD_(g_threadinfo)[i].sg_first; sg; sg = sg->thr_next) { |
| 1048 | if (!sg->thr_next || DRD_(sg_get_refcnt)(sg) > 1) { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1049 | if (DRD_(vc_lte)(&sg2->vc, &sg->vc)) |
| 1050 | break; |
| 1051 | if (DRD_(vc_lte)(&sg1->vc, &sg->vc)) |
| 1052 | return False; |
| 1053 | } |
| 1054 | } |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1055 | for (sg = DRD_(g_threadinfo)[i].sg_last; sg; sg = sg->thr_prev) { |
| 1056 | if (!sg->thr_next || DRD_(sg_get_refcnt)(sg) > 1) { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1057 | if (DRD_(vc_lte)(&sg->vc, &sg1->vc)) |
| 1058 | break; |
| 1059 | if (DRD_(vc_lte)(&sg->vc, &sg2->vc)) |
| 1060 | return False; |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | return True; |
| 1065 | } |
| 1066 | |
| 1067 | /** |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1068 | * Merge all segments that may be merged without triggering false positives |
| 1069 | * or discarding real data races. For the theoretical background of segment |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1070 | * merging, see also the following paper: Mark Christiaens, Michiel Ronsse |
| 1071 | * and Koen De Bosschere. Bounding the number of segment histories during |
| 1072 | * data race detection. Parallel Computing archive, Volume 28, Issue 9, |
| 1073 | * pp 1221-1238, September 2002. This paper contains a proof that merging |
| 1074 | * consecutive segments for which the property equiv(s1,s2) holds can be |
| 1075 | * merged without reducing the accuracy of datarace detection. Furthermore |
| 1076 | * it is also proven that the total number of all segments will never grow |
| 1077 | * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged |
| 1078 | * every time a new segment is created. The property equiv(s1, s2) is defined |
| 1079 | * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector |
| 1080 | * clocks of segments s and s1 are ordered in the same way as those of segments |
| 1081 | * s and s2. The set CS is defined as the set of existing segments s that have |
| 1082 | * the potential to conflict with not yet created segments, either because the |
| 1083 | * segment s is the latest segment of a thread or because it can become the |
| 1084 | * immediate predecessor of a new segment due to a synchronization operation. |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 1085 | */ |
| 1086 | static void thread_merge_segments(void) |
| 1087 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1088 | unsigned i; |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 1089 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1090 | s_new_segments_since_last_merge = 0; |
| 1091 | |
| 1092 | for (i = 0; i < DRD_N_THREADS; i++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1093 | { |
| 1094 | Segment* sg; |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 1095 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1096 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 1097 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i])); |
| 1098 | #endif |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 1099 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1100 | for (sg = DRD_(g_threadinfo)[i].sg_first; sg; sg = sg->thr_next) { |
| 1101 | if (DRD_(sg_get_refcnt)(sg) == 1 && sg->thr_next) { |
| 1102 | Segment* const sg_next = sg->thr_next; |
bart | e278ab5 | 2012-01-24 18:28:55 +0000 | [diff] [blame] | 1103 | if (DRD_(sg_get_refcnt)(sg_next) == 1 |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1104 | && sg_next->thr_next |
bart | e278ab5 | 2012-01-24 18:28:55 +0000 | [diff] [blame] | 1105 | && thread_consistent_segment_ordering(i, sg, sg_next)) |
| 1106 | { |
| 1107 | /* Merge sg and sg_next into sg. */ |
| 1108 | DRD_(sg_merge)(sg, sg_next); |
| 1109 | thread_discard_segment(i, sg_next); |
| 1110 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1111 | } |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 1112 | } |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 1113 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1114 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 1115 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i])); |
| 1116 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1117 | } |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1120 | /** |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1121 | * Create a new segment for the specified thread, and discard any segments |
| 1122 | * that cannot cause races anymore. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1123 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1124 | void DRD_(thread_new_segment)(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1125 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1126 | Segment* last_sg; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1127 | Segment* new_sg; |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1128 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1129 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1130 | && tid != DRD_INVALID_THREADID); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1131 | tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid))); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1132 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1133 | last_sg = DRD_(g_threadinfo)[tid].sg_last; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1134 | new_sg = DRD_(sg_new)(tid, tid); |
| 1135 | thread_append_segment(tid, new_sg); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1136 | if (tid == DRD_(g_drd_running_tid) && last_sg) |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1137 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1138 | DRD_(thread_update_conflict_set)(tid, &last_sg->vc); |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1139 | s_update_conflict_set_new_sg_count++; |
| 1140 | } |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1141 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1142 | tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid))); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1143 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1144 | if (s_segment_merging |
| 1145 | && ++s_new_segments_since_last_merge >= s_segment_merge_interval) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1146 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1147 | thread_discard_ordered_segments(); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1148 | thread_merge_segments(); |
| 1149 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1152 | /** Call this function after thread 'joiner' joined thread 'joinee'. */ |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1153 | void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1154 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1155 | tl_assert(joiner != joinee); |
| 1156 | tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS |
| 1157 | && joiner != DRD_INVALID_THREADID); |
| 1158 | tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS |
| 1159 | && joinee != DRD_INVALID_THREADID); |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1160 | tl_assert(DRD_(g_threadinfo)[joiner].sg_first); |
| 1161 | tl_assert(DRD_(g_threadinfo)[joiner].sg_last); |
| 1162 | tl_assert(DRD_(g_threadinfo)[joinee].sg_first); |
| 1163 | tl_assert(DRD_(g_threadinfo)[joinee].sg_last); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1164 | |
| 1165 | if (DRD_(sg_get_trace)()) |
| 1166 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1167 | HChar *str1, *str2; |
bart | c6bf184 | 2012-01-22 08:40:42 +0000 | [diff] [blame] | 1168 | str1 = DRD_(vc_aprint)(DRD_(thread_get_vc)(joiner)); |
| 1169 | str2 = DRD_(vc_aprint)(DRD_(thread_get_vc)(joinee)); |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1170 | VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n", |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1171 | str1, str2); |
| 1172 | VG_(free)(str1); |
| 1173 | VG_(free)(str2); |
| 1174 | } |
bart | ae37e6d | 2012-01-22 08:58:31 +0000 | [diff] [blame] | 1175 | if (joiner == DRD_(g_drd_running_tid)) { |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1176 | VectorClock old_vc; |
| 1177 | |
bart | c6bf184 | 2012-01-22 08:40:42 +0000 | [diff] [blame] | 1178 | DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(joiner)); |
| 1179 | DRD_(vc_combine)(DRD_(thread_get_vc)(joiner), |
| 1180 | DRD_(thread_get_vc)(joinee)); |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1181 | DRD_(thread_update_conflict_set)(joiner, &old_vc); |
| 1182 | s_update_conflict_set_join_count++; |
| 1183 | DRD_(vc_cleanup)(&old_vc); |
bart | ae37e6d | 2012-01-22 08:58:31 +0000 | [diff] [blame] | 1184 | } else { |
bart | c6bf184 | 2012-01-22 08:40:42 +0000 | [diff] [blame] | 1185 | DRD_(vc_combine)(DRD_(thread_get_vc)(joiner), |
| 1186 | DRD_(thread_get_vc)(joinee)); |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | thread_discard_ordered_segments(); |
| 1190 | |
bart | ae37e6d | 2012-01-22 08:58:31 +0000 | [diff] [blame] | 1191 | if (DRD_(sg_get_trace)()) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1192 | HChar* str; |
bart | ae37e6d | 2012-01-22 08:58:31 +0000 | [diff] [blame] | 1193 | |
bart | c6bf184 | 2012-01-22 08:40:42 +0000 | [diff] [blame] | 1194 | str = DRD_(vc_aprint)(DRD_(thread_get_vc)(joiner)); |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1195 | VG_(message)(Vg_DebugMsg, "After join: %s\n", str); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1196 | VG_(free)(str); |
| 1197 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1200 | /** |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1201 | * Update the vector clock of the last segment of thread tid with the |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1202 | * the vector clock of segment sg. |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1203 | */ |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1204 | static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1205 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1206 | const VectorClock* const vc = &sg->vc; |
| 1207 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1208 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1209 | && tid != DRD_INVALID_THREADID); |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1210 | tl_assert(DRD_(g_threadinfo)[tid].sg_first); |
| 1211 | tl_assert(DRD_(g_threadinfo)[tid].sg_last); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1212 | tl_assert(sg); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1213 | tl_assert(vc); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1214 | |
bart | ae37e6d | 2012-01-22 08:58:31 +0000 | [diff] [blame] | 1215 | if (tid != sg->tid) { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1216 | VectorClock old_vc; |
| 1217 | |
bart | c6bf184 | 2012-01-22 08:40:42 +0000 | [diff] [blame] | 1218 | DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(tid)); |
| 1219 | DRD_(vc_combine)(DRD_(thread_get_vc)(tid), vc); |
bart | ae37e6d | 2012-01-22 08:58:31 +0000 | [diff] [blame] | 1220 | if (DRD_(sg_get_trace)()) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1221 | HChar *str1, *str2; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1222 | str1 = DRD_(vc_aprint)(&old_vc); |
bart | c6bf184 | 2012-01-22 08:40:42 +0000 | [diff] [blame] | 1223 | str2 = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid)); |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1224 | VG_(message)(Vg_DebugMsg, "thread %u: vc %s -> %s\n", tid, str1, str2); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1225 | VG_(free)(str1); |
| 1226 | VG_(free)(str2); |
| 1227 | } |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1228 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1229 | thread_discard_ordered_segments(); |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1230 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1231 | DRD_(thread_update_conflict_set)(tid, &old_vc); |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1232 | s_update_conflict_set_sync_count++; |
| 1233 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1234 | DRD_(vc_cleanup)(&old_vc); |
bart | ae37e6d | 2012-01-22 08:58:31 +0000 | [diff] [blame] | 1235 | } else { |
bart | c6bf184 | 2012-01-22 08:40:42 +0000 | [diff] [blame] | 1236 | tl_assert(DRD_(vc_lte)(vc, DRD_(thread_get_vc)(tid))); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1237 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1240 | /** |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1241 | * Create a new segment for thread tid and update the vector clock of the last |
florian | ad4e979 | 2015-07-05 21:53:33 +0000 | [diff] [blame] | 1242 | * segment of this thread with the vector clock of segment sg. Call this |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1243 | * function after thread tid had to wait because of thread synchronization |
| 1244 | * until the memory accesses in the segment sg finished. |
| 1245 | */ |
| 1246 | void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, const Segment* sg) |
| 1247 | { |
| 1248 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1249 | && tid != DRD_INVALID_THREADID); |
| 1250 | tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid))); |
| 1251 | tl_assert(sg); |
| 1252 | |
| 1253 | thread_append_segment(tid, DRD_(sg_new)(tid, tid)); |
| 1254 | |
| 1255 | thread_combine_vc_sync(tid, sg); |
| 1256 | |
| 1257 | if (s_segment_merging |
| 1258 | && ++s_new_segments_since_last_merge >= s_segment_merge_interval) |
| 1259 | { |
| 1260 | thread_discard_ordered_segments(); |
| 1261 | thread_merge_segments(); |
| 1262 | } |
| 1263 | } |
| 1264 | |
| 1265 | /** |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1266 | * Call this function whenever a thread is no longer using the memory |
| 1267 | * [ a1, a2 [, e.g. because of a call to free() or a stack pointer |
| 1268 | * increase. |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1269 | */ |
bart | 23ef19d | 2011-03-12 12:34:44 +0000 | [diff] [blame] | 1270 | void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1271 | { |
bart | 178b686 | 2011-07-29 12:30:43 +0000 | [diff] [blame] | 1272 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1273 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1274 | for (p = DRD_(g_sg_list); p; p = p->g_next) |
bart | e278ab5 | 2012-01-24 18:28:55 +0000 | [diff] [blame] | 1275 | DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1276 | |
bart | 178b686 | 2011-07-29 12:30:43 +0000 | [diff] [blame] | 1277 | DRD_(bm_clear)(DRD_(g_conflict_set), a1, a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1280 | /** Specify whether memory loads should be recorded. */ |
| 1281 | void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled) |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 1282 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1283 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1284 | && tid != DRD_INVALID_THREADID); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1285 | tl_assert(enabled == !! enabled); |
| 1286 | |
| 1287 | DRD_(g_threadinfo)[tid].is_recording_loads = enabled; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1290 | /** Specify whether memory stores should be recorded. */ |
| 1291 | void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled) |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 1292 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1293 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1294 | && tid != DRD_INVALID_THREADID); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1295 | tl_assert(enabled == !! enabled); |
| 1296 | |
| 1297 | DRD_(g_threadinfo)[tid].is_recording_stores = enabled; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1300 | /** |
| 1301 | * Print the segment information for all threads. |
| 1302 | * |
| 1303 | * This function is only used for debugging purposes. |
| 1304 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1305 | void DRD_(thread_print_all)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1306 | { |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1307 | UInt i; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1308 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1309 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1310 | for (i = 0; i < DRD_N_THREADS; i++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1311 | { |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1312 | p = DRD_(g_threadinfo)[i].sg_first; |
| 1313 | if (p) { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1314 | VG_(printf)("**************\n" |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1315 | "* thread %3u (%d/%u/%u/%u/0x%lx/%d) *\n" |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1316 | "**************\n", |
| 1317 | i, |
bart | 6d956dc | 2011-07-28 09:54:37 +0000 | [diff] [blame] | 1318 | DRD_(g_threadinfo)[i].valid, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1319 | DRD_(g_threadinfo)[i].vg_thread_exists, |
| 1320 | DRD_(g_threadinfo)[i].vg_threadid, |
| 1321 | DRD_(g_threadinfo)[i].posix_thread_exists, |
| 1322 | DRD_(g_threadinfo)[i].pt_threadid, |
| 1323 | DRD_(g_threadinfo)[i].detached_posix_thread); |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1324 | for ( ; p; p = p->thr_next) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1325 | DRD_(sg_print)(p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1326 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1327 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1330 | /** Show a call stack involved in a data race. */ |
bart | e708600 | 2011-10-11 19:08:39 +0000 | [diff] [blame] | 1331 | static void show_call_stack(const DrdThreadId tid, ExeContext* const callstack) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1332 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1333 | const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1334 | |
bart | e708600 | 2011-10-11 19:08:39 +0000 | [diff] [blame] | 1335 | if (vg_tid != VG_INVALID_THREADID) { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1336 | if (callstack) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1337 | VG_(pp_ExeContext)(callstack); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1338 | else |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1339 | VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size)); |
bart | e708600 | 2011-10-11 19:08:39 +0000 | [diff] [blame] | 1340 | } else { |
| 1341 | if (!VG_(clo_xml)) |
| 1342 | VG_(message)(Vg_UserMsg, |
| 1343 | " (thread finished, call stack no longer available)\n"); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1344 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1347 | /** Print information about the segments involved in a data race. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1348 | static void |
| 1349 | thread_report_conflicting_segments_segment(const DrdThreadId tid, |
| 1350 | const Addr addr, |
| 1351 | const SizeT size, |
| 1352 | const BmAccessTypeT access_type, |
| 1353 | const Segment* const p) |
| 1354 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1355 | unsigned i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1356 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1357 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1358 | && tid != DRD_INVALID_THREADID); |
| 1359 | tl_assert(p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1360 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1361 | for (i = 0; i < DRD_N_THREADS; i++) { |
| 1362 | if (i != tid) { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1363 | Segment* q; |
bart | e278ab5 | 2012-01-24 18:28:55 +0000 | [diff] [blame] | 1364 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1365 | for (q = DRD_(g_threadinfo)[i].sg_last; q; q = q->thr_prev) { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1366 | /* |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 1367 | * Since q iterates over the segments of thread i in order of |
| 1368 | * decreasing vector clocks, if q->vc <= p->vc, then |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1369 | * q->next->vc <= p->vc will also hold. Hence, break out of the |
| 1370 | * loop once this condition is met. |
| 1371 | */ |
| 1372 | if (DRD_(vc_lte)(&q->vc, &p->vc)) |
| 1373 | break; |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1374 | if (!DRD_(vc_lte)(&p->vc, &q->vc)) { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1375 | if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size, |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1376 | access_type)) { |
bart | e278ab5 | 2012-01-24 18:28:55 +0000 | [diff] [blame] | 1377 | Segment* q_next; |
| 1378 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1379 | tl_assert(q->stacktrace); |
bart | e708600 | 2011-10-11 19:08:39 +0000 | [diff] [blame] | 1380 | if (VG_(clo_xml)) |
| 1381 | VG_(printf_xml)(" <other_segment_start>\n"); |
| 1382 | else |
| 1383 | VG_(message)(Vg_UserMsg, |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1384 | "Other segment start (thread %u)\n", i); |
bart | e708600 | 2011-10-11 19:08:39 +0000 | [diff] [blame] | 1385 | show_call_stack(i, q->stacktrace); |
| 1386 | if (VG_(clo_xml)) |
| 1387 | VG_(printf_xml)(" </other_segment_start>\n" |
| 1388 | " <other_segment_end>\n"); |
| 1389 | else |
| 1390 | VG_(message)(Vg_UserMsg, |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1391 | "Other segment end (thread %u)\n", i); |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1392 | q_next = q->thr_next; |
bart | e278ab5 | 2012-01-24 18:28:55 +0000 | [diff] [blame] | 1393 | show_call_stack(i, q_next ? q_next->stacktrace : 0); |
bart | e708600 | 2011-10-11 19:08:39 +0000 | [diff] [blame] | 1394 | if (VG_(clo_xml)) |
| 1395 | VG_(printf_xml)(" </other_segment_end>\n"); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1399 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1400 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1403 | /** Print information about all segments involved in a data race. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1404 | void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid, |
| 1405 | const Addr addr, |
| 1406 | const SizeT size, |
| 1407 | const BmAccessTypeT access_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1408 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1409 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1410 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1411 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1412 | && tid != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1413 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1414 | for (p = DRD_(g_threadinfo)[tid].sg_first; p; p = p->thr_next) { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1415 | if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1416 | thread_report_conflicting_segments_segment(tid, addr, size, |
| 1417 | access_type, p); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1418 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1419 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1420 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1421 | /** |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1422 | * Verify whether the conflict set for thread tid is up to date. Only perform |
| 1423 | * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set. |
| 1424 | */ |
| 1425 | static Bool thread_conflict_set_up_to_date(const DrdThreadId tid) |
| 1426 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1427 | Bool result; |
| 1428 | struct bitmap* computed_conflict_set = 0; |
| 1429 | |
bart | 9cdc083 | 2014-08-09 12:58:17 +0000 | [diff] [blame] | 1430 | if (!DRD_(verify_conflict_set)) |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1431 | return True; |
| 1432 | |
| 1433 | thread_compute_conflict_set(&computed_conflict_set, tid); |
| 1434 | result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set); |
| 1435 | if (! result) |
| 1436 | { |
| 1437 | VG_(printf)("actual conflict set:\n"); |
| 1438 | DRD_(bm_print)(DRD_(g_conflict_set)); |
| 1439 | VG_(printf)("\n"); |
| 1440 | VG_(printf)("computed conflict set:\n"); |
| 1441 | DRD_(bm_print)(computed_conflict_set); |
| 1442 | VG_(printf)("\n"); |
| 1443 | } |
| 1444 | DRD_(bm_delete)(computed_conflict_set); |
| 1445 | return result; |
| 1446 | } |
| 1447 | |
| 1448 | /** |
| 1449 | * Compute the conflict set: a bitmap that represents the union of all memory |
| 1450 | * accesses of all segments that are unordered to the current segment of the |
| 1451 | * thread tid. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1452 | */ |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1453 | static void thread_compute_conflict_set(struct bitmap** conflict_set, |
| 1454 | const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1455 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1456 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1457 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1458 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1459 | && tid != DRD_INVALID_THREADID); |
| 1460 | tl_assert(tid == DRD_(g_drd_running_tid)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1461 | |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 1462 | s_compute_conflict_set_count++; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1463 | s_conflict_set_bitmap_creation_count |
| 1464 | -= DRD_(bm_get_bitmap_creation_count)(); |
| 1465 | s_conflict_set_bitmap2_creation_count |
| 1466 | -= DRD_(bm_get_bitmap2_creation_count)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1467 | |
bart | ae37e6d | 2012-01-22 08:58:31 +0000 | [diff] [blame] | 1468 | if (*conflict_set) { |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1469 | DRD_(bm_cleanup)(*conflict_set); |
| 1470 | DRD_(bm_init)(*conflict_set); |
bart | ae37e6d | 2012-01-22 08:58:31 +0000 | [diff] [blame] | 1471 | } else { |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1472 | *conflict_set = DRD_(bm_new)(); |
| 1473 | } |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1474 | |
bart | ae37e6d | 2012-01-22 08:58:31 +0000 | [diff] [blame] | 1475 | if (s_trace_conflict_set) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1476 | HChar* str; |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1477 | |
bart | c6bf184 | 2012-01-22 08:40:42 +0000 | [diff] [blame] | 1478 | str = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid)); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1479 | VG_(message)(Vg_DebugMsg, |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1480 | "computing conflict set for thread %u with vc %s\n", |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 1481 | tid, str); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1482 | VG_(free)(str); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1483 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1484 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1485 | p = DRD_(g_threadinfo)[tid].sg_last; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1486 | { |
| 1487 | unsigned j; |
| 1488 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1489 | if (s_trace_conflict_set) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1490 | HChar* vc; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1491 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1492 | vc = DRD_(vc_aprint)(&p->vc); |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1493 | VG_(message)(Vg_DebugMsg, "conflict set: thread [%u] at vc %s\n", |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1494 | tid, vc); |
| 1495 | VG_(free)(vc); |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1496 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1497 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1498 | for (j = 0; j < DRD_N_THREADS; j++) { |
| 1499 | if (j != tid && DRD_(IsValidDrdThreadId)(j)) { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1500 | Segment* q; |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1501 | |
| 1502 | for (q = DRD_(g_threadinfo)[j].sg_last; q; q = q->thr_prev) { |
| 1503 | if (!DRD_(vc_lte)(&q->vc, &p->vc) |
| 1504 | && !DRD_(vc_lte)(&p->vc, &q->vc)) { |
| 1505 | if (s_trace_conflict_set) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1506 | HChar* str; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1507 | |
| 1508 | str = DRD_(vc_aprint)(&q->vc); |
| 1509 | VG_(message)(Vg_DebugMsg, |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1510 | "conflict set: [%u] merging segment %s\n", |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1511 | j, str); |
| 1512 | VG_(free)(str); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1513 | } |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1514 | DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q)); |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1515 | } else { |
| 1516 | if (s_trace_conflict_set) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1517 | HChar* str; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1518 | |
| 1519 | str = DRD_(vc_aprint)(&q->vc); |
| 1520 | VG_(message)(Vg_DebugMsg, |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1521 | "conflict set: [%u] ignoring segment %s\n", |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1522 | j, str); |
| 1523 | VG_(free)(str); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1530 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1531 | s_conflict_set_bitmap_creation_count |
| 1532 | += DRD_(bm_get_bitmap_creation_count)(); |
| 1533 | s_conflict_set_bitmap2_creation_count |
| 1534 | += DRD_(bm_get_bitmap2_creation_count)(); |
| 1535 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1536 | if (s_trace_conflict_set_bm) { |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1537 | VG_(message)(Vg_DebugMsg, "[%u] new conflict set:\n", tid); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1538 | DRD_(bm_print)(*conflict_set); |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1539 | VG_(message)(Vg_DebugMsg, "[%u] end of new conflict set.\n", tid); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1540 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1543 | /** |
| 1544 | * Update the conflict set after the vector clock of thread tid has been |
| 1545 | * updated from old_vc to its current value, either because a new segment has |
| 1546 | * been created or because of a synchronization operation. |
| 1547 | */ |
| 1548 | void DRD_(thread_update_conflict_set)(const DrdThreadId tid, |
| 1549 | const VectorClock* const old_vc) |
| 1550 | { |
| 1551 | const VectorClock* new_vc; |
| 1552 | Segment* p; |
| 1553 | unsigned j; |
| 1554 | |
| 1555 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1556 | && tid != DRD_INVALID_THREADID); |
| 1557 | tl_assert(old_vc); |
| 1558 | tl_assert(tid == DRD_(g_drd_running_tid)); |
| 1559 | tl_assert(DRD_(g_conflict_set)); |
| 1560 | |
bart | ae37e6d | 2012-01-22 08:58:31 +0000 | [diff] [blame] | 1561 | if (s_trace_conflict_set) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1562 | HChar* str; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1563 | |
bart | c6bf184 | 2012-01-22 08:40:42 +0000 | [diff] [blame] | 1564 | str = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid)); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1565 | VG_(message)(Vg_DebugMsg, |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1566 | "updating conflict set for thread %u with vc %s\n", |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 1567 | tid, str); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1568 | VG_(free)(str); |
| 1569 | } |
| 1570 | |
bart | c6bf184 | 2012-01-22 08:40:42 +0000 | [diff] [blame] | 1571 | new_vc = DRD_(thread_get_vc)(tid); |
bart | f5fe4b6 | 2011-07-03 11:39:30 +0000 | [diff] [blame] | 1572 | tl_assert(DRD_(vc_lte)(old_vc, new_vc)); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1573 | |
| 1574 | DRD_(bm_unmark)(DRD_(g_conflict_set)); |
| 1575 | |
| 1576 | for (j = 0; j < DRD_N_THREADS; j++) |
| 1577 | { |
| 1578 | Segment* q; |
| 1579 | |
| 1580 | if (j == tid || ! DRD_(IsValidDrdThreadId)(j)) |
| 1581 | continue; |
| 1582 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1583 | for (q = DRD_(g_threadinfo)[j].sg_last; |
| 1584 | q && !DRD_(vc_lte)(&q->vc, new_vc); |
| 1585 | q = q->thr_prev) { |
| 1586 | const Bool included_in_old_conflict_set |
| 1587 | = !DRD_(vc_lte)(old_vc, &q->vc); |
| 1588 | const Bool included_in_new_conflict_set |
| 1589 | = !DRD_(vc_lte)(new_vc, &q->vc); |
bart | 178b686 | 2011-07-29 12:30:43 +0000 | [diff] [blame] | 1590 | |
| 1591 | if (UNLIKELY(s_trace_conflict_set)) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1592 | HChar* str; |
bart | 178b686 | 2011-07-29 12:30:43 +0000 | [diff] [blame] | 1593 | |
| 1594 | str = DRD_(vc_aprint)(&q->vc); |
| 1595 | VG_(message)(Vg_DebugMsg, |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1596 | "conflict set: [%u] %s segment %s\n", j, |
bart | 178b686 | 2011-07-29 12:30:43 +0000 | [diff] [blame] | 1597 | included_in_old_conflict_set |
| 1598 | != included_in_new_conflict_set |
| 1599 | ? "merging" : "ignoring", str); |
| 1600 | VG_(free)(str); |
| 1601 | } |
| 1602 | if (included_in_old_conflict_set != included_in_new_conflict_set) |
| 1603 | DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q)); |
| 1604 | } |
| 1605 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1606 | for ( ; q && !DRD_(vc_lte)(&q->vc, old_vc); q = q->thr_prev) { |
| 1607 | const Bool included_in_old_conflict_set |
| 1608 | = !DRD_(vc_lte)(old_vc, &q->vc); |
| 1609 | const Bool included_in_new_conflict_set |
| 1610 | = !DRD_(vc_lte)(&q->vc, new_vc) |
| 1611 | && !DRD_(vc_lte)(new_vc, &q->vc); |
bart | 178b686 | 2011-07-29 12:30:43 +0000 | [diff] [blame] | 1612 | |
| 1613 | if (UNLIKELY(s_trace_conflict_set)) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1614 | HChar* str; |
bart | ac5b95b | 2011-07-03 11:43:45 +0000 | [diff] [blame] | 1615 | |
| 1616 | str = DRD_(vc_aprint)(&q->vc); |
| 1617 | VG_(message)(Vg_DebugMsg, |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1618 | "conflict set: [%u] %s segment %s\n", j, |
bart | ac5b95b | 2011-07-03 11:43:45 +0000 | [diff] [blame] | 1619 | included_in_old_conflict_set |
| 1620 | != included_in_new_conflict_set |
| 1621 | ? "merging" : "ignoring", str); |
| 1622 | VG_(free)(str); |
| 1623 | } |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1624 | if (included_in_old_conflict_set != included_in_new_conflict_set) |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1625 | DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q)); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | DRD_(bm_clear_marked)(DRD_(g_conflict_set)); |
| 1630 | |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1631 | p = DRD_(g_threadinfo)[tid].sg_last; |
| 1632 | for (j = 0; j < DRD_N_THREADS; j++) { |
| 1633 | if (j != tid && DRD_(IsValidDrdThreadId)(j)) { |
bart | 4b3fdb2 | 2011-07-03 11:40:49 +0000 | [diff] [blame] | 1634 | Segment* q; |
bart | 91b7ec3 | 2012-01-25 20:36:27 +0000 | [diff] [blame] | 1635 | for (q = DRD_(g_threadinfo)[j].sg_last; |
| 1636 | q && !DRD_(vc_lte)(&q->vc, &p->vc); |
| 1637 | q = q->thr_prev) { |
bart | 178b686 | 2011-07-29 12:30:43 +0000 | [diff] [blame] | 1638 | if (!DRD_(vc_lte)(&p->vc, &q->vc)) |
bart | 4b3fdb2 | 2011-07-03 11:40:49 +0000 | [diff] [blame] | 1639 | DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q)); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1640 | } |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set)); |
| 1645 | |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 1646 | s_update_conflict_set_count++; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1647 | |
| 1648 | if (s_trace_conflict_set_bm) |
| 1649 | { |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1650 | VG_(message)(Vg_DebugMsg, "[%u] updated conflict set:\n", tid); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1651 | DRD_(bm_print)(DRD_(g_conflict_set)); |
florian | ea71ffb | 2015-08-05 14:38:57 +0000 | [diff] [blame] | 1652 | VG_(message)(Vg_DebugMsg, "[%u] end of updated conflict set.\n", tid); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid))); |
| 1656 | } |
| 1657 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1658 | /** Report the number of context switches performed. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1659 | ULong DRD_(thread_get_context_switch_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1660 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1661 | return s_context_switch_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1664 | /** Report the number of ordered segments that have been discarded. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1665 | ULong DRD_(thread_get_discard_ordered_segments_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1666 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1667 | return s_discard_ordered_segments_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 1670 | /** Return how many times the conflict set has been updated entirely. */ |
| 1671 | ULong DRD_(thread_get_compute_conflict_set_count)() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1672 | { |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 1673 | return s_compute_conflict_set_count; |
| 1674 | } |
| 1675 | |
| 1676 | /** Return how many times the conflict set has been updated partially. */ |
| 1677 | ULong DRD_(thread_get_update_conflict_set_count)(void) |
| 1678 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1679 | return s_update_conflict_set_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1682 | /** |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1683 | * Return how many times the conflict set has been updated partially |
| 1684 | * because a new segment has been created. |
| 1685 | */ |
| 1686 | ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void) |
| 1687 | { |
| 1688 | return s_update_conflict_set_new_sg_count; |
| 1689 | } |
| 1690 | |
| 1691 | /** |
| 1692 | * Return how many times the conflict set has been updated partially |
| 1693 | * because of combining vector clocks due to synchronization operations |
| 1694 | * other than reader/writer lock or barrier operations. |
| 1695 | */ |
| 1696 | ULong DRD_(thread_get_update_conflict_set_sync_count)(void) |
| 1697 | { |
| 1698 | return s_update_conflict_set_sync_count; |
| 1699 | } |
| 1700 | |
| 1701 | /** |
| 1702 | * Return how many times the conflict set has been updated partially |
| 1703 | * because of thread joins. |
| 1704 | */ |
| 1705 | ULong DRD_(thread_get_update_conflict_set_join_count)(void) |
| 1706 | { |
| 1707 | return s_update_conflict_set_join_count; |
| 1708 | } |
| 1709 | |
| 1710 | /** |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1711 | * Return the number of first-level bitmaps that have been created during |
| 1712 | * conflict set updates. |
| 1713 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1714 | ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1715 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1716 | return s_conflict_set_bitmap_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1719 | /** |
| 1720 | * Return the number of second-level bitmaps that have been created during |
| 1721 | * conflict set updates. |
| 1722 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1723 | ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1724 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1725 | return s_conflict_set_bitmap2_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1726 | } |