| 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 | { |
| bart | 0d3bbd3 | 2009-03-12 18:35:44 +0000 | [diff] [blame] | 422 | /* |
| 423 | * Once a detached thread has finished, its stack is deallocated and |
| 424 | * should no longer be taken into account when computing the conflict set. |
| 425 | */ |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 426 | DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 427 | |
| bart | 0d3bbd3 | 2009-03-12 18:35:44 +0000 | [diff] [blame] | 428 | /* |
| 429 | * For a detached thread, calling pthread_exit() invalidates the |
| 430 | * POSIX thread ID associated with the detached thread. For joinable |
| 431 | * POSIX threads however, the POSIX thread ID remains live after the |
| 432 | * pthread_exit() call until pthread_join() is called. |
| 433 | */ |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 434 | DRD_(g_threadinfo)[tid].posix_thread_exists = False; |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 435 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| bart | 9b2974a | 2008-09-27 12:35:31 +0000 | [diff] [blame] | 438 | /** Called just before pthread_cancel(). */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 439 | void DRD_(thread_pre_cancel)(const DrdThreadId tid) |
| bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 440 | { |
| 441 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 442 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 443 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
| bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 444 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 445 | DRD_(g_threadinfo)[tid].synchr_nesting = 0; |
| bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 448 | /** Store the POSIX thread ID for the specified thread. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 449 | void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 450 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 451 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 452 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 453 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 454 | tl_assert(ptid != INVALID_POSIX_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 455 | DRD_(g_threadinfo)[tid].posix_thread_exists = True; |
| 456 | DRD_(g_threadinfo)[tid].pt_threadid = ptid; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 459 | /** Returns true for joinable threads and false for detached threads. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 460 | Bool DRD_(thread_get_joinable)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 461 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 462 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 463 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 464 | return ! DRD_(g_threadinfo)[tid].detached_posix_thread; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 467 | /** Store the thread mode: joinable or detached. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 468 | void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 469 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 470 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 471 | && tid != DRD_INVALID_THREADID); |
| 472 | tl_assert(!! joinable == joinable); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 473 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 474 | #if 0 |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 475 | VG_(message)(Vg_DebugMsg, |
| 476 | "thread_set_joinable(%d/%d, %s)", |
| 477 | tid, |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 478 | DRD_(g_threadinfo)[tid].vg_threadid, |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 479 | joinable ? "joinable" : "detached"); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 480 | #endif |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 481 | DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 484 | /** |
| 485 | * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the |
| 486 | * conflict set. |
| 487 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 488 | void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 489 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 490 | tl_assert(vg_tid != VG_INVALID_THREADID); |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 491 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 492 | if (vg_tid != s_vg_running_tid) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 493 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 494 | DRD_(thread_set_running_tid)(vg_tid, |
| 495 | DRD_(VgThreadIdToDrdThreadId)(vg_tid)); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 496 | } |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 497 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 498 | tl_assert(s_vg_running_tid != VG_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 499 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 502 | /** |
| 503 | * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the |
| 504 | * conflict set. |
| 505 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 506 | void DRD_(thread_set_running_tid)(const ThreadId vg_tid, |
| 507 | const DrdThreadId drd_tid) |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 508 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 509 | tl_assert(vg_tid != VG_INVALID_THREADID); |
| 510 | tl_assert(drd_tid != DRD_INVALID_THREADID); |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 511 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 512 | if (vg_tid != s_vg_running_tid) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 513 | { |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 514 | if (s_trace_context_switches |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 515 | && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 516 | { |
| 517 | VG_(message)(Vg_DebugMsg, |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 518 | "Context switch from thread %d/%d to thread %d/%d;" |
| 519 | " segments: %llu", |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 520 | s_vg_running_tid, DRD_(g_drd_running_tid), |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 521 | DRD_(DrdThreadIdToVgThreadId)(drd_tid), drd_tid, |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 522 | DRD_(sg_get_segments_alive_count)()); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 523 | } |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 524 | s_vg_running_tid = vg_tid; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 525 | DRD_(g_drd_running_tid) = drd_tid; |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 526 | thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid); |
| 527 | s_context_switch_count++; |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 528 | } |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 529 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 530 | tl_assert(s_vg_running_tid != VG_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 531 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 534 | /** |
| 535 | * Increase the synchronization nesting counter. Must be called before the |
| 536 | * client calls a synchronization function. |
| 537 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 538 | int DRD_(thread_enter_synchr)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 539 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 540 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 541 | return DRD_(g_threadinfo)[tid].synchr_nesting++; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 544 | /** |
| 545 | * Decrease the synchronization nesting counter. Must be called after the |
| 546 | * client left a synchronization function. |
| 547 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 548 | int DRD_(thread_leave_synchr)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 549 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 550 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 551 | tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1); |
| 552 | return --DRD_(g_threadinfo)[tid].synchr_nesting; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 555 | /** Returns the synchronization nesting counter. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 556 | int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 557 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 558 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 559 | return DRD_(g_threadinfo)[tid].synchr_nesting; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 560 | } |
| 561 | |
| bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 562 | /** Append a new segment at the end of the segment list. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 563 | static |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 564 | void thread_append_segment(const DrdThreadId tid, Segment* const sg) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 565 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 566 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 567 | && tid != DRD_INVALID_THREADID); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 568 | // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 569 | sg->prev = DRD_(g_threadinfo)[tid].last; |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 570 | sg->next = 0; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 571 | if (DRD_(g_threadinfo)[tid].last) |
| 572 | DRD_(g_threadinfo)[tid].last->next = sg; |
| 573 | DRD_(g_threadinfo)[tid].last = sg; |
| 574 | if (DRD_(g_threadinfo)[tid].first == 0) |
| 575 | DRD_(g_threadinfo)[tid].first = sg; |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 576 | // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 579 | /** |
| 580 | * Remove a segment from the segment list of thread threadid, and free the |
| 581 | * associated memory. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 582 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 583 | static |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 584 | void thread_discard_segment(const DrdThreadId tid, Segment* const sg) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 585 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 586 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 587 | && tid != DRD_INVALID_THREADID); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 588 | //tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 589 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 590 | if (sg->prev) |
| 591 | sg->prev->next = sg->next; |
| 592 | if (sg->next) |
| 593 | sg->next->prev = sg->prev; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 594 | if (sg == DRD_(g_threadinfo)[tid].first) |
| 595 | DRD_(g_threadinfo)[tid].first = sg->next; |
| 596 | if (sg == DRD_(g_threadinfo)[tid].last) |
| 597 | DRD_(g_threadinfo)[tid].last = sg->prev; |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 598 | DRD_(sg_put)(sg); |
| bart | 3f74967 | 2008-03-22 09:49:40 +0000 | [diff] [blame] | 599 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 600 | //tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 603 | /** |
| 604 | * Returns a pointer to the vector clock of the most recent segment associated |
| 605 | * with thread 'tid'. |
| 606 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 607 | VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 608 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 609 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 610 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 611 | tl_assert(DRD_(g_threadinfo)[tid].last); |
| 612 | return &DRD_(g_threadinfo)[tid].last->vc; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 615 | /** |
| 616 | * Return the latest segment of thread 'tid' and increment its reference count. |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 617 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 618 | void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid) |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 619 | { |
| 620 | tl_assert(sg); |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 621 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 622 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 623 | tl_assert(DRD_(g_threadinfo)[tid].last); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 624 | |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 625 | DRD_(sg_put)(*sg); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 626 | *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 629 | /** |
| 630 | * Compute the minimum of all latest vector clocks of all threads |
| 631 | * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA). |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 632 | * |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 633 | * @param vc pointer to a vectorclock, holds result upon return. |
| 634 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 635 | static void DRD_(thread_compute_minimum_vc)(VectorClock* vc) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 636 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 637 | unsigned i; |
| 638 | Bool first; |
| 639 | Segment* latest_sg; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 640 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 641 | first = True; |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 642 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 643 | i++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 644 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 645 | latest_sg = DRD_(g_threadinfo)[i].last; |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 646 | if (latest_sg) |
| 647 | { |
| 648 | if (first) |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 649 | DRD_(vc_assign)(vc, &latest_sg->vc); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 650 | else |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 651 | DRD_(vc_min)(vc, &latest_sg->vc); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 652 | first = False; |
| 653 | } |
| 654 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 657 | /** |
| 658 | * Compute the maximum of all latest vector clocks of all threads. |
| 659 | * |
| 660 | * @param vc pointer to a vectorclock, holds result upon return. |
| 661 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 662 | static void DRD_(thread_compute_maximum_vc)(VectorClock* vc) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 663 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 664 | unsigned i; |
| 665 | Bool first; |
| 666 | Segment* latest_sg; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 667 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 668 | first = True; |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 669 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 670 | i++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 671 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 672 | latest_sg = DRD_(g_threadinfo)[i].last; |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 673 | if (latest_sg) |
| 674 | { |
| 675 | if (first) |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 676 | DRD_(vc_assign)(vc, &latest_sg->vc); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 677 | else |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 678 | DRD_(vc_combine)(vc, &latest_sg->vc); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 679 | first = False; |
| 680 | } |
| 681 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /** |
| bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 685 | * Discard all segments that have a defined order against the latest vector |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 686 | * clock of all threads -- these segments can no longer be involved in a |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 687 | * data race. |
| 688 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 689 | static void DRD_(thread_discard_ordered_segments)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 690 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 691 | unsigned i; |
| 692 | VectorClock thread_vc_min; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 693 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 694 | s_discard_ordered_segments_count++; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 695 | |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 696 | DRD_(vc_init)(&thread_vc_min, 0, 0); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 697 | DRD_(thread_compute_minimum_vc)(&thread_vc_min); |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 698 | if (DRD_(sg_get_trace)()) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 699 | { |
| 700 | char msg[256]; |
| 701 | VectorClock thread_vc_max; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 702 | |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 703 | DRD_(vc_init)(&thread_vc_max, 0, 0); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 704 | DRD_(thread_compute_maximum_vc)(&thread_vc_max); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 705 | VG_(snprintf)(msg, sizeof(msg), |
| 706 | "Discarding ordered segments -- min 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_min); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 709 | VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 710 | ", max vc is "); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 711 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 712 | &thread_vc_max); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 713 | VG_(message)(Vg_UserMsg, "%s", msg); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 714 | DRD_(vc_cleanup)(&thread_vc_max); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 715 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 716 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 717 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 718 | i++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 719 | { |
| 720 | Segment* sg; |
| 721 | Segment* sg_next; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 722 | for (sg = DRD_(g_threadinfo)[i].first; |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 723 | sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 724 | sg = sg_next) |
| 725 | { |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 726 | thread_discard_segment(i, sg); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 727 | } |
| 728 | } |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 729 | DRD_(vc_cleanup)(&thread_vc_min); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 732 | /** |
| 733 | * Merge all segments that may be merged without triggering false positives |
| 734 | * or discarding real data races. For the theoretical background of segment |
| 735 | * merging, see also the following paper: |
| 736 | * Mark Christiaens, Michiel Ronsse and Koen De Bosschere. |
| 737 | * Bounding the number of segment histories during data race detection. |
| 738 | * Parallel Computing archive, Volume 28, Issue 9, pp 1221-1238, |
| 739 | * September 2002. |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 740 | */ |
| 741 | static void thread_merge_segments(void) |
| 742 | { |
| 743 | unsigned i; |
| 744 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 745 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 746 | i++) |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 747 | { |
| 748 | Segment* sg; |
| 749 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 750 | // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i])); |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 751 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 752 | for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next) |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 753 | { |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 754 | if (DRD_(sg_get_refcnt)(sg) == 1 |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 755 | && sg->next |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 756 | && DRD_(sg_get_refcnt)(sg->next) == 1 |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 757 | && sg->next->next) |
| 758 | { |
| 759 | /* Merge sg and sg->next into sg. */ |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 760 | DRD_(sg_merge)(sg, sg->next); |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 761 | thread_discard_segment(i, sg->next); |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 765 | // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i])); |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 769 | /** |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 770 | * Create a new segment for the specified thread, and discard any segments |
| 771 | * that cannot cause races anymore. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 772 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 773 | void DRD_(thread_new_segment)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 774 | { |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 775 | Segment* new_sg; |
| 776 | |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 777 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 778 | && tid != DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 779 | |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 780 | new_sg = DRD_(sg_new)(tid, tid); |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 781 | thread_append_segment(tid, new_sg); |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 782 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 783 | thread_compute_conflict_set(&DRD_(g_conflict_set), DRD_(g_drd_running_tid)); |
| 784 | s_conflict_set_new_segment_count++; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 785 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 786 | DRD_(thread_discard_ordered_segments)(); |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 787 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 788 | if (s_segment_merging) |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 789 | { |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 790 | thread_merge_segments(); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 791 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 794 | /** Call this function after thread 'joiner' joined thread 'joinee'. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 795 | void DRD_(thread_combine_vc)(DrdThreadId joiner, DrdThreadId joinee) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 796 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 797 | tl_assert(joiner != joinee); |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 798 | tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 799 | && joiner != DRD_INVALID_THREADID); |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 800 | tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 801 | && joinee != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 802 | tl_assert(DRD_(g_threadinfo)[joiner].last); |
| 803 | tl_assert(DRD_(g_threadinfo)[joinee].last); |
| 804 | DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc, |
| 805 | &DRD_(g_threadinfo)[joinee].last->vc); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 806 | DRD_(thread_discard_ordered_segments)(); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 807 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 808 | if (joiner == DRD_(g_drd_running_tid)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 809 | { |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 810 | thread_compute_conflict_set(&DRD_(g_conflict_set), joiner); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 811 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 814 | /** |
| 815 | * Call this function after thread 'tid' had to wait because of thread |
| 816 | * synchronization until the memory accesses in the segment with vector clock |
| 817 | * 'vc' finished. |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 818 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 819 | void DRD_(thread_combine_vc2)(DrdThreadId tid, const VectorClock* const vc) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 820 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 821 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 822 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 823 | tl_assert(DRD_(g_threadinfo)[tid].last); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 824 | tl_assert(vc); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 825 | DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc); |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 826 | thread_compute_conflict_set(&DRD_(g_conflict_set), tid); |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 827 | DRD_(thread_discard_ordered_segments)(); |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 828 | s_conflict_set_combine_vc_count++; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 831 | /** |
| 832 | * Call this function whenever a thread is no longer using the memory |
| 833 | * [ a1, a2 [, e.g. because of a call to free() or a stack pointer |
| 834 | * increase. |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 835 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 836 | void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 837 | { |
| bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 838 | DrdThreadId other_user; |
| 839 | unsigned i; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 840 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 841 | /* For all threads, mark the range [ a1, a2 [ as no longer in use. */ |
| bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 842 | other_user = DRD_INVALID_THREADID; |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 843 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 844 | i++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 845 | { |
| 846 | Segment* p; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 847 | for (p = DRD_(g_threadinfo)[i].first; p; p = p->next) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 848 | { |
| 849 | if (other_user == DRD_INVALID_THREADID |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 850 | && i != DRD_(g_drd_running_tid)) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 851 | { |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 852 | if (UNLIKELY(DRD_(bm_test_and_clear)(p->bm, a1, a2))) |
| bart | 8bf2f8b | 2008-03-30 17:56:43 +0000 | [diff] [blame] | 853 | { |
| 854 | other_user = i; |
| 855 | } |
| 856 | continue; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 857 | } |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 858 | DRD_(bm_clear)(p->bm, a1, a2); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 861 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 862 | /* |
| 863 | * If any other thread had accessed memory in [ a1, a2 [, update the |
| 864 | * conflict set. |
| 865 | */ |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 866 | if (other_user != DRD_INVALID_THREADID |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 867 | && DRD_(bm_has_any_access)(DRD_(g_conflict_set), a1, a2)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 868 | { |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 869 | thread_compute_conflict_set(&DRD_(g_conflict_set), |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 870 | DRD_(thread_get_running_tid)()); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 871 | } |
| 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 | /** Start recording memory access information. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 875 | void DRD_(thread_start_recording)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 876 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 877 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 878 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 879 | tl_assert(! DRD_(g_threadinfo)[tid].is_recording); |
| 880 | DRD_(g_threadinfo)[tid].is_recording = True; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 883 | /** Stop recording memory access information. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 884 | void DRD_(thread_stop_recording)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 885 | { |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 886 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 887 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 888 | tl_assert(DRD_(g_threadinfo)[tid].is_recording); |
| 889 | DRD_(g_threadinfo)[tid].is_recording = False; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 892 | /** |
| 893 | * Print the segment information for all threads. |
| 894 | * |
| 895 | * This function is only used for debugging purposes. |
| 896 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 897 | void DRD_(thread_print_all)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 898 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 899 | unsigned i; |
| 900 | Segment* p; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 901 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 902 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 903 | i++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 904 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 905 | if (DRD_(g_threadinfo)[i].first) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 906 | { |
| 907 | VG_(printf)("**************\n" |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 908 | "* thread %3d (%d/%d/%d/0x%lx/%d) *\n" |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 909 | "**************\n", |
| 910 | i, |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 911 | DRD_(g_threadinfo)[i].vg_thread_exists, |
| 912 | DRD_(g_threadinfo)[i].vg_threadid, |
| 913 | DRD_(g_threadinfo)[i].posix_thread_exists, |
| 914 | DRD_(g_threadinfo)[i].pt_threadid, |
| 915 | DRD_(g_threadinfo)[i].detached_posix_thread); |
| 916 | for (p = DRD_(g_threadinfo)[i].first; p; p = p->next) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 917 | { |
| bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 918 | DRD_(sg_print)(p); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 919 | } |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 920 | } |
| 921 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 924 | /** Show a call stack involved in a data race. */ |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 925 | static void show_call_stack(const DrdThreadId tid, |
| 926 | const Char* const msg, |
| 927 | ExeContext* const callstack) |
| 928 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 929 | const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 930 | |
| bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 931 | VG_(message)(Vg_UserMsg, "%s (thread %d/%d)", msg, vg_tid, tid); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 932 | |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 933 | if (vg_tid != VG_INVALID_THREADID) |
| 934 | { |
| 935 | if (callstack) |
| 936 | { |
| 937 | VG_(pp_ExeContext)(callstack); |
| 938 | } |
| 939 | else |
| 940 | { |
| 941 | VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size)); |
| 942 | } |
| 943 | } |
| 944 | else |
| 945 | { |
| 946 | VG_(message)(Vg_UserMsg, |
| 947 | " (thread finished, call stack no longer available)"); |
| 948 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 951 | /** Print information about the segments involved in a data race. */ |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 952 | static void |
| 953 | thread_report_conflicting_segments_segment(const DrdThreadId tid, |
| 954 | const Addr addr, |
| 955 | const SizeT size, |
| 956 | const BmAccessTypeT access_type, |
| 957 | const Segment* const p) |
| 958 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 959 | unsigned i; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 960 | |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 961 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 962 | && tid != DRD_INVALID_THREADID); |
| 963 | tl_assert(p); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 964 | |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 965 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 966 | i++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 967 | { |
| 968 | if (i != tid) |
| 969 | { |
| 970 | Segment* q; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 971 | for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 972 | { |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 973 | /* |
| 974 | * Since q iterates over the segments of thread i in order of |
| 975 | * decreasing vector clocks, if q->vc <= p->vc, then |
| 976 | * q->next->vc <= p->vc will also hold. Hence, break out of the |
| 977 | * loop once this condition is met. |
| 978 | */ |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 979 | if (DRD_(vc_lte)(&q->vc, &p->vc)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 980 | break; |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 981 | if (! DRD_(vc_lte)(&p->vc, &q->vc)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 982 | { |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 983 | if (DRD_(bm_has_conflict_with)(q->bm, addr, addr + size, |
| 984 | access_type)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 985 | { |
| 986 | tl_assert(q->stacktrace); |
| 987 | show_call_stack(i, "Other segment start", |
| 988 | q->stacktrace); |
| 989 | show_call_stack(i, "Other segment end", |
| 990 | q->next ? q->next->stacktrace : 0); |
| 991 | } |
| 992 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 993 | } |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 994 | } |
| 995 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 998 | /** Print information about all segments involved in a data race. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 999 | void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid, |
| 1000 | const Addr addr, |
| 1001 | const SizeT size, |
| 1002 | const BmAccessTypeT access_type) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1003 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1004 | Segment* p; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1005 | |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 1006 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1007 | && tid != DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1008 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1009 | for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1010 | { |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1011 | if (DRD_(bm_has)(p->bm, addr, addr + size, access_type)) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1012 | { |
| 1013 | thread_report_conflicting_segments_segment(tid, addr, size, |
| 1014 | access_type, p); |
| 1015 | } |
| 1016 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1017 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1018 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1019 | /** |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1020 | * Compute a bitmap that represents the union of all memory accesses of all |
| 1021 | * segments that are unordered to the current segment of the thread tid. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1022 | */ |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1023 | static void thread_compute_conflict_set(struct bitmap** conflict_set, |
| 1024 | const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1025 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1026 | Segment* p; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1027 | |
| bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 1028 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1029 | && tid != DRD_INVALID_THREADID); |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1030 | tl_assert(tid == DRD_(g_drd_running_tid)); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1031 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1032 | s_update_conflict_set_count++; |
| 1033 | s_conflict_set_bitmap_creation_count -= DRD_(bm_get_bitmap_creation_count)(); |
| 1034 | s_conflict_set_bitmap2_creation_count -= DRD_(bm_get_bitmap2_creation_count)(); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1035 | |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1036 | if (*conflict_set) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1037 | { |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1038 | DRD_(bm_delete)(*conflict_set); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1039 | } |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1040 | *conflict_set = DRD_(bm_new)(); |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1041 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1042 | if (s_trace_conflict_set) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1043 | { |
| 1044 | char msg[256]; |
| 1045 | |
| 1046 | VG_(snprintf)(msg, sizeof(msg), |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1047 | "computing conflict set for thread %d/%d with vc ", |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1048 | DRD_(DrdThreadIdToVgThreadId)(tid), tid); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1049 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), |
| 1050 | sizeof(msg) - VG_(strlen)(msg), |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1051 | &DRD_(g_threadinfo)[tid].last->vc); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1052 | VG_(message)(Vg_UserMsg, "%s", msg); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1055 | p = DRD_(g_threadinfo)[tid].last; |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1056 | { |
| 1057 | unsigned j; |
| 1058 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1059 | if (s_trace_conflict_set) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1060 | { |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1061 | char msg[256]; |
| 1062 | |
| 1063 | VG_(snprintf)(msg, sizeof(msg), |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1064 | "conflict set: thread [%d] at vc ", |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1065 | tid); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1066 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), |
| 1067 | sizeof(msg) - VG_(strlen)(msg), |
| 1068 | &p->vc); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1069 | VG_(message)(Vg_UserMsg, "%s", msg); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1070 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1071 | |
| bart | 0d3bbd3 | 2009-03-12 18:35:44 +0000 | [diff] [blame] | 1072 | for (j = 0; j < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); |
| 1073 | j++) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1074 | { |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1075 | if (j != tid && DRD_(IsValidDrdThreadId)(j)) |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1076 | { |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1077 | const Segment* q; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1078 | for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev) |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1079 | { |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1080 | 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] | 1081 | { |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1082 | if (s_trace_conflict_set) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1083 | { |
| 1084 | char msg[256]; |
| 1085 | VG_(snprintf)(msg, sizeof(msg), |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1086 | "conflict set: [%d] merging segment ", j); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1087 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), |
| 1088 | sizeof(msg) - VG_(strlen)(msg), |
| 1089 | &q->vc); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1090 | VG_(message)(Vg_UserMsg, "%s", msg); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1091 | } |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1092 | DRD_(bm_merge2)(*conflict_set, q->bm); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1093 | } |
| 1094 | else |
| 1095 | { |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1096 | if (s_trace_conflict_set) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1097 | { |
| 1098 | char msg[256]; |
| 1099 | VG_(snprintf)(msg, sizeof(msg), |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1100 | "conflict set: [%d] ignoring segment ", j); |
| bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1101 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), |
| 1102 | sizeof(msg) - VG_(strlen)(msg), |
| 1103 | &q->vc); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1104 | VG_(message)(Vg_UserMsg, "%s", msg); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1105 | } |
| 1106 | } |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1107 | } |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1108 | } |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1109 | } |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1110 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1111 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1112 | s_conflict_set_bitmap_creation_count += DRD_(bm_get_bitmap_creation_count)(); |
| 1113 | s_conflict_set_bitmap2_creation_count += DRD_(bm_get_bitmap2_creation_count)(); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1114 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1115 | if (0 && s_trace_conflict_set) |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1116 | { |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1117 | VG_(message)(Vg_UserMsg, "[%d] new conflict set:", tid); |
| bart | 99edb29 | 2009-02-15 15:59:20 +0000 | [diff] [blame] | 1118 | DRD_(bm_print)(*conflict_set); |
| bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1119 | VG_(message)(Vg_UserMsg, "[%d] end of new conflict set.", tid); |
| bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1120 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1123 | /** Report the number of context switches performed. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1124 | ULong DRD_(thread_get_context_switch_count)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1125 | { |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1126 | return s_context_switch_count; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1129 | /** Report the number of ordered segments that have been discarded. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1130 | ULong DRD_(thread_get_discard_ordered_segments_count)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1131 | { |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1132 | return s_discard_ordered_segments_count; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1135 | /** Return how many times the conflict set has been updated. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1136 | ULong DRD_(thread_get_update_conflict_set_count)(ULong* dsnsc, ULong* dscvc) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1137 | { |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1138 | tl_assert(dsnsc); |
| 1139 | tl_assert(dscvc); |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1140 | *dsnsc = s_conflict_set_new_segment_count; |
| 1141 | *dscvc = s_conflict_set_combine_vc_count; |
| 1142 | return s_update_conflict_set_count; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1145 | /** |
| 1146 | * Return the number of first-level bitmaps that have been created during |
| 1147 | * conflict set updates. |
| 1148 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1149 | ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1150 | { |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1151 | return s_conflict_set_bitmap_creation_count; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1154 | /** |
| 1155 | * Return the number of second-level bitmaps that have been created during |
| 1156 | * conflict set updates. |
| 1157 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1158 | ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1159 | { |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1160 | return s_conflict_set_bitmap2_creation_count; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1161 | } |