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