bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 2 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 3 | This file is part of drd, a thread error detector. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 4 | |
bart | 876cafd | 2010-10-10 18:07:31 +0000 | [diff] [blame] | 5 | Copyright (C) 2006-2010 Bart Van Assche <bvanassche@acm.org>. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 6 | |
| 7 | This program is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU General Public License as |
| 9 | published by the Free Software Foundation; either version 2 of the |
| 10 | License, or (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307, USA. |
| 21 | |
| 22 | The GNU General Public License is contained in the file COPYING. |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #ifndef __THREAD_H |
| 27 | #define __THREAD_H |
| 28 | |
| 29 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 30 | /* Include directives. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 31 | |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 32 | #include "drd_basics.h" |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 33 | #include "drd_segment.h" |
bart | d59bb0f | 2008-06-08 08:08:31 +0000 | [diff] [blame] | 34 | #include "pub_drd_bitmap.h" |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 35 | #include "pub_tool_libcassert.h" /* tl_assert() */ |
| 36 | #include "pub_tool_stacktrace.h" /* typedef StackTrace */ |
| 37 | #include "pub_tool_threadstate.h" /* VG_N_THREADS */ |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 38 | |
| 39 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 40 | /* Defines. */ |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 41 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 42 | /** Maximum number of threads DRD keeps information about. */ |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 43 | #define DRD_N_THREADS VG_N_THREADS |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 44 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 45 | /** A number different from any valid DRD thread ID. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 46 | #define DRD_INVALID_THREADID 0 |
| 47 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 48 | /** |
| 49 | * A number different from any valid POSIX thread ID. |
| 50 | * |
| 51 | * @note The PThreadId typedef and the INVALID_POSIX_THREADID depend on the |
| 52 | * operating system and threading library in use. PThreadId must contain at |
| 53 | * least as many bits as pthread_t, and INVALID_POSIX_THREADID |
| 54 | * must be a value that will never be returned by pthread_self(). |
| 55 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 56 | #define INVALID_POSIX_THREADID ((PThreadId)0) |
| 57 | |
| 58 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 59 | /* Type definitions. */ |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 60 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 61 | /** |
| 62 | * POSIX thread ID. The type PThreadId must be at least as wide as |
| 63 | * pthread_t. |
| 64 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 65 | typedef UWord PThreadId; |
| 66 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 67 | /** Per-thread information managed by DRD. */ |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 68 | typedef struct |
| 69 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 70 | Segment* first; /**< Pointer to first segment. */ |
| 71 | Segment* last; /**< Pointer to last segment. */ |
| 72 | ThreadId vg_threadid; /**< Valgrind thread ID. */ |
| 73 | PThreadId pt_threadid; /**< POSIX thread ID. */ |
| 74 | Addr stack_min_min; /**< Lowest value stack pointer ever had. */ |
| 75 | Addr stack_min; /**< Current stack pointer. */ |
| 76 | Addr stack_startup; /**<Stack pointer after pthread_create() finished.*/ |
| 77 | Addr stack_max; /**< Top of stack. */ |
| 78 | SizeT stack_size; /**< Maximum size of stack. */ |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 79 | char name[64]; /**< User-assigned thread name. */ |
bart | 383d613 | 2010-09-02 14:43:18 +0000 | [diff] [blame] | 80 | Bool on_alt_stack; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 81 | /** Indicates whether the Valgrind core knows about this thread. */ |
| 82 | Bool vg_thread_exists; |
| 83 | /** Indicates whether there is an associated POSIX thread ID. */ |
| 84 | Bool posix_thread_exists; |
| 85 | /** |
| 86 | * If true, indicates that there is a corresponding POSIX thread ID and |
| 87 | * a corresponding OS thread that is detached. |
| 88 | */ |
| 89 | Bool detached_posix_thread; |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 90 | /** Wether recording of memory load accesses is currently enabled. */ |
| 91 | Bool is_recording_loads; |
| 92 | /** Wether recording of memory load accesses is currently enabled. */ |
| 93 | Bool is_recording_stores; |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 94 | /** pthread_create() nesting level. */ |
| 95 | Int pthread_create_nesting_level; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 96 | /** Nesting level of synchronization functions called by the client. */ |
| 97 | Int synchr_nesting; |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 98 | } ThreadInfo; |
| 99 | |
| 100 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 101 | /* |
| 102 | * Local variables of drd_thread.c that are declared here such that these |
| 103 | * can be accessed by inline functions. |
| 104 | */ |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 105 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 106 | /** |
| 107 | * DRD thread ID of the currently running thread. It is crucial for correct |
| 108 | * operation of DRD that this number is always in sync with |
| 109 | * VG_(get_running_tid)(). |
| 110 | */ |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 111 | extern DrdThreadId DRD_(g_drd_running_tid); |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 112 | /** Per-thread information managed by DRD. */ |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 113 | extern ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS]; |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 114 | /** Conflict set for the currently running thread. */ |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 115 | extern struct bitmap* DRD_(g_conflict_set); |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 116 | |
| 117 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 118 | /* Function declarations. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 119 | |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 120 | void DRD_(thread_trace_context_switches)(const Bool t); |
| 121 | void DRD_(thread_trace_conflict_set)(const Bool t); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 122 | void DRD_(thread_trace_conflict_set_bm)(const Bool t); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 123 | Bool DRD_(thread_get_trace_fork_join)(void); |
| 124 | void DRD_(thread_set_trace_fork_join)(const Bool t); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 125 | void DRD_(thread_set_segment_merging)(const Bool m); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 126 | int DRD_(thread_get_segment_merge_interval)(void); |
| 127 | void DRD_(thread_set_segment_merge_interval)(const int i); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 128 | |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 129 | DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid); |
| 130 | DrdThreadId DRD_(NewVgThreadIdToDrdThreadId)(const ThreadId tid); |
| 131 | DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid); |
| 132 | ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid); |
| 133 | DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 134 | const ThreadId vg_created); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 135 | DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 136 | void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee); |
bart | 9194e93 | 2011-02-09 11:55:12 +0000 | [diff] [blame] | 137 | void DRD_(thread_delete)(const DrdThreadId tid, Bool detached); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 138 | void DRD_(thread_finished)(const DrdThreadId tid); |
bart | 5c7e6b6 | 2011-02-03 17:47:50 +0000 | [diff] [blame] | 139 | void DRD_(drd_thread_atfork_child)(const DrdThreadId tid); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 140 | void DRD_(thread_pre_cancel)(const DrdThreadId tid); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 141 | void DRD_(thread_set_stack_startup)(const DrdThreadId tid, |
| 142 | const Addr stack_startup); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 143 | Addr DRD_(thread_get_stack_min)(const DrdThreadId tid); |
| 144 | Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid); |
| 145 | Addr DRD_(thread_get_stack_max)(const DrdThreadId tid); |
| 146 | SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid); |
bart | 383d613 | 2010-09-02 14:43:18 +0000 | [diff] [blame] | 147 | Bool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid); |
| 148 | void DRD_(thread_set_on_alt_stack)(const DrdThreadId tid, |
| 149 | const Bool on_alt_stack); |
| 150 | Int DRD_(thread_get_threads_on_alt_stack)(void); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 151 | void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid); |
| 152 | Bool DRD_(thread_get_joinable)(const DrdThreadId tid); |
| 153 | void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable); |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 154 | void DRD_(thread_entering_pthread_create)(const DrdThreadId tid); |
| 155 | void DRD_(thread_left_pthread_create)(const DrdThreadId tid); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 156 | const char* DRD_(thread_get_name)(const DrdThreadId tid); |
| 157 | void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 158 | void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid); |
| 159 | void DRD_(thread_set_running_tid)(const ThreadId vg_tid, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 160 | const DrdThreadId drd_tid); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 161 | int DRD_(thread_enter_synchr)(const DrdThreadId tid); |
| 162 | int DRD_(thread_leave_synchr)(const DrdThreadId tid); |
| 163 | int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid); |
| 164 | void DRD_(thread_new_segment)(const DrdThreadId tid); |
| 165 | VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid); |
| 166 | void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 167 | void DRD_(thread_combine_vc_join)(const DrdThreadId joiner, |
| 168 | const DrdThreadId joinee); |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 169 | void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, |
| 170 | const Segment* sg); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 171 | void DRD_(thread_update_conflict_set)(const DrdThreadId tid, |
| 172 | const VectorClock* const old_vc); |
bart | dfbae6e | 2008-06-05 08:29:53 +0000 | [diff] [blame] | 173 | |
bart | 23ef19d | 2011-03-12 12:34:44 +0000 | [diff] [blame^] | 174 | void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 175 | void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled); |
| 176 | void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 177 | void DRD_(thread_print_all)(void); |
| 178 | void DRD_(thread_report_races)(const DrdThreadId tid); |
| 179 | void DRD_(thread_report_races_segment)(const DrdThreadId tid, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 180 | const Segment* const p); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 181 | void DRD_(thread_report_all_races)(void); |
| 182 | void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 183 | const Addr addr, |
| 184 | const SizeT size, |
| 185 | const BmAccessTypeT access_type); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 186 | ULong DRD_(thread_get_context_switch_count)(void); |
| 187 | ULong DRD_(thread_get_report_races_count)(void); |
| 188 | ULong DRD_(thread_get_discard_ordered_segments_count)(void); |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 189 | ULong DRD_(thread_get_compute_conflict_set_count)(void); |
| 190 | ULong DRD_(thread_get_update_conflict_set_count)(void); |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 191 | ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void); |
| 192 | ULong DRD_(thread_get_update_conflict_set_sync_count)(void); |
| 193 | ULong DRD_(thread_get_update_conflict_set_join_count)(void); |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 194 | ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void); |
| 195 | ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 196 | |
| 197 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 198 | /* Inline function definitions. */ |
| 199 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 200 | /** |
| 201 | * Whether or not the specified DRD thread ID is valid. |
| 202 | * |
| 203 | * A DRD thread ID is valid if and only if the following conditions are met: |
| 204 | * - The ID is a valid index of the DRD_(g_threadinfo)[] array. |
| 205 | * - The ID is not equal to DRD_INVALID_THREADID. |
| 206 | * - The ID refers either to a thread known by the Valgrind core, a joinable |
| 207 | * thread that has not yet been joined or a detached thread. |
| 208 | */ |
bart | bf80e12 | 2008-06-06 10:18:24 +0000 | [diff] [blame] | 209 | static __inline__ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 210 | Bool DRD_(IsValidDrdThreadId)(const DrdThreadId tid) |
bart | bf80e12 | 2008-06-06 10:18:24 +0000 | [diff] [blame] | 211 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 212 | return (0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID |
| 213 | && ! (DRD_(g_threadinfo)[tid].vg_thread_exists == False |
| 214 | && DRD_(g_threadinfo)[tid].posix_thread_exists == False |
| 215 | && DRD_(g_threadinfo)[tid].detached_posix_thread == False)); |
bart | bf80e12 | 2008-06-06 10:18:24 +0000 | [diff] [blame] | 216 | } |
| 217 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 218 | /** Returns the DRD thread ID of the currently running thread. */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 219 | static __inline__ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 220 | DrdThreadId DRD_(thread_get_running_tid)(void) |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 221 | { |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 222 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 223 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 224 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 225 | return DRD_(g_drd_running_tid); |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 226 | } |
| 227 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 228 | /** Returns a pointer to the conflict set for the currently running thread. */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 229 | static __inline__ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 230 | struct bitmap* DRD_(thread_get_conflict_set)(void) |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 231 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 232 | return DRD_(g_conflict_set); |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 233 | } |
| 234 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 235 | /** |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 236 | * Reports whether or not the currently running client thread is executing code |
| 237 | * inside the pthread_create() function. |
| 238 | */ |
| 239 | static __inline__ |
| 240 | Bool DRD_(running_thread_inside_pthread_create)(void) |
| 241 | { |
| 242 | return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)] |
| 243 | .pthread_create_nesting_level > 0); |
| 244 | } |
| 245 | |
| 246 | /** |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 247 | * Reports whether or not recording of memory loads is enabled for the |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 248 | * currently running client thread. |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 249 | */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 250 | static __inline__ |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 251 | Bool DRD_(running_thread_is_recording_loads)(void) |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 252 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 253 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 254 | tl_assert(0 <= (int)DRD_(g_drd_running_tid) |
| 255 | && DRD_(g_drd_running_tid) < DRD_N_THREADS |
| 256 | && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
bart | 589f948 | 2008-06-09 15:08:22 +0000 | [diff] [blame] | 257 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 258 | return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0 |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 259 | && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_loads); |
| 260 | } |
| 261 | |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 262 | /** |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 263 | * Reports whether or not recording memory stores is enabled for the |
bart | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame] | 264 | * currently running client thread. |
| 265 | */ |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 266 | static __inline__ |
| 267 | Bool DRD_(running_thread_is_recording_stores)(void) |
| 268 | { |
| 269 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 270 | tl_assert(0 <= (int)DRD_(g_drd_running_tid) |
| 271 | && DRD_(g_drd_running_tid) < DRD_N_THREADS |
| 272 | && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
| 273 | #endif |
| 274 | return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0 |
| 275 | && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_stores); |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 276 | } |
| 277 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 278 | /** |
| 279 | * Update the information about the lowest stack address that has ever been |
| 280 | * accessed by a thread. |
| 281 | */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 282 | static __inline__ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 283 | void DRD_(thread_set_stack_min)(const DrdThreadId tid, const Addr stack_min) |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 284 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 285 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 286 | tl_assert(0 <= (int)tid |
| 287 | && tid < DRD_N_THREADS |
| 288 | && tid != DRD_INVALID_THREADID); |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 289 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 290 | DRD_(g_threadinfo)[tid].stack_min = stack_min; |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 291 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 292 | /* This function can be called after the thread has been created but */ |
| 293 | /* before drd_post_thread_create() has filled in stack_max. */ |
| 294 | tl_assert(DRD_(g_threadinfo)[tid].stack_min |
| 295 | < DRD_(g_threadinfo)[tid].stack_max |
| 296 | || DRD_(g_threadinfo)[tid].stack_max == 0); |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 297 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 298 | if (UNLIKELY(stack_min < DRD_(g_threadinfo)[tid].stack_min_min)) |
| 299 | { |
| 300 | DRD_(g_threadinfo)[tid].stack_min_min = stack_min; |
| 301 | } |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 302 | } |
| 303 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 304 | /** |
| 305 | * Return true if and only if the specified address is on the stack of the |
| 306 | * currently scheduled thread. |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 307 | */ |
| 308 | static __inline__ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 309 | Bool DRD_(thread_address_on_stack)(const Addr a) |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 310 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 311 | return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_min <= a |
| 312 | && a < DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_max); |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 313 | } |
| 314 | |
bart | 0d6d5c5 | 2009-03-10 09:22:13 +0000 | [diff] [blame] | 315 | /** |
| 316 | * Return true if and only if the specified address is on the stack of any |
| 317 | * thread. |
| 318 | */ |
| 319 | static __inline__ |
| 320 | Bool DRD_(thread_address_on_any_stack)(const Addr a) |
| 321 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 322 | int i; |
bart | 0d6d5c5 | 2009-03-10 09:22:13 +0000 | [diff] [blame] | 323 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 324 | for (i = 1; i < DRD_N_THREADS; i++) |
| 325 | { |
| 326 | if (DRD_(g_threadinfo)[i].vg_thread_exists |
| 327 | && DRD_(g_threadinfo)[i].stack_min <= a |
| 328 | && a < DRD_(g_threadinfo)[i].stack_max) |
| 329 | { |
| 330 | return True; |
| 331 | } |
| 332 | } |
| 333 | return False; |
bart | 0d6d5c5 | 2009-03-10 09:22:13 +0000 | [diff] [blame] | 334 | } |
| 335 | |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 336 | /** Return a pointer to the latest segment for the specified thread. */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 337 | static __inline__ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 338 | Segment* DRD_(thread_get_segment)(const DrdThreadId tid) |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 339 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 340 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 341 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 342 | && tid != DRD_INVALID_THREADID); |
| 343 | tl_assert(DRD_(g_threadinfo)[tid].last); |
bart | 589f948 | 2008-06-09 15:08:22 +0000 | [diff] [blame] | 344 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 345 | return DRD_(g_threadinfo)[tid].last; |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 346 | } |
| 347 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 348 | /** Return a pointer to the latest segment for the running thread. */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 349 | static __inline__ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 350 | Segment* DRD_(running_thread_get_segment)(void) |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 351 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 352 | return DRD_(thread_get_segment)(DRD_(g_drd_running_tid)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 353 | } |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 354 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 355 | #endif /* __THREAD_H */ |