sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | bart.vanassche@gmail.com |
| 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 | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 35 | #include "pub_tool_libcassert.h" // tl_assert() |
| 36 | #include "pub_tool_stacktrace.h" // StackTrace |
| 37 | #include "pub_tool_threadstate.h" // VG_N_THREADS |
| 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 | |
| 42 | #define DRD_N_THREADS VG_N_THREADS |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 43 | |
| 44 | #define DRD_INVALID_THREADID 0 |
| 45 | |
| 46 | /* Note: the PThreadId typedef and the INVALID_POSIX_THREADID depend on the */ |
| 47 | /* operating system and threading library in use. PThreadId must contain at */ |
| 48 | /* least the same number of bits as pthread_t, and INVALID_POSIX_THREADID */ |
| 49 | /* must be a value that will never be returned by pthread_self(). */ |
| 50 | |
| 51 | #define INVALID_POSIX_THREADID ((PThreadId)0) |
| 52 | |
| 53 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 54 | /* Type definitions. */ |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 55 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 56 | typedef UWord PThreadId; |
| 57 | |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 58 | typedef struct |
| 59 | { |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 60 | Segment* first; |
| 61 | Segment* last; |
| 62 | ThreadId vg_threadid; |
| 63 | PThreadId pt_threadid; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 64 | Addr stack_min_min; /**< Lowest value stack pointer ever had. */ |
| 65 | Addr stack_min; /**< Current stack pointer. */ |
| 66 | Addr stack_startup; /**<Stack pointer after pthread_create() finished.*/ |
| 67 | Addr stack_max; /**< Top of stack. */ |
| 68 | SizeT stack_size; /**< Maximum size of stack. */ |
| 69 | /** Indicates whether the Valgrind core knows about this thread. */ |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 70 | Bool vg_thread_exists; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 71 | /** Indicates whether there is an associated POSIX thread ID. */ |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 72 | Bool posix_thread_exists; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 73 | /** |
| 74 | * If true, indicates that there is a corresponding POSIX thread ID and |
| 75 | * a corresponding OS thread that is detached. |
| 76 | */ |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 77 | Bool detached_posix_thread; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 78 | /** Wether recording of memory accesses is active. */ |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 79 | Bool is_recording; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 80 | /** Nesting level of synchronization functions called by the client. */ |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 81 | Int synchr_nesting; |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 82 | } ThreadInfo; |
| 83 | |
| 84 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 85 | /* |
| 86 | * Local variables of drd_thread.c that are declared here such that these |
| 87 | * can be accessed by inline functions. |
| 88 | */ |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 89 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 90 | extern DrdThreadId DRD_(g_drd_running_tid); |
| 91 | extern ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS]; |
| 92 | extern struct bitmap* DRD_(g_conflict_set); |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 93 | |
| 94 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 95 | /* Function declarations. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 96 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 97 | void thread_trace_context_switches(const Bool t); |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 98 | void thread_trace_conflict_set(const Bool t); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 99 | Bool DRD_(thread_get_trace_fork_join)(void); |
| 100 | void DRD_(thread_set_trace_fork_join)(const Bool t); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 101 | void thread_set_segment_merging(const Bool m); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 102 | |
| 103 | DrdThreadId VgThreadIdToDrdThreadId(const ThreadId tid); |
| 104 | DrdThreadId NewVgThreadIdToDrdThreadId(const ThreadId tid); |
| 105 | DrdThreadId PtThreadIdToDrdThreadId(const PThreadId tid); |
| 106 | ThreadId DrdThreadIdToVgThreadId(const DrdThreadId tid); |
| 107 | DrdThreadId thread_pre_create(const DrdThreadId creator, |
| 108 | const ThreadId vg_created); |
| 109 | DrdThreadId thread_post_create(const ThreadId vg_created); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 110 | void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 111 | void thread_delete(const DrdThreadId tid); |
| 112 | void thread_finished(const DrdThreadId tid); |
bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 113 | void thread_pre_cancel(const DrdThreadId tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 114 | void thread_set_stack_startup(const DrdThreadId tid, const Addr stack_startup); |
| 115 | Addr thread_get_stack_min(const DrdThreadId tid); |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 116 | Addr thread_get_stack_min_min(const DrdThreadId tid); |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 117 | Addr thread_get_stack_max(const DrdThreadId tid); |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 118 | SizeT thread_get_stack_size(const DrdThreadId tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 119 | void thread_set_pthreadid(const DrdThreadId tid, const PThreadId ptid); |
| 120 | Bool thread_get_joinable(const DrdThreadId tid); |
| 121 | void thread_set_joinable(const DrdThreadId tid, const Bool joinable); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 122 | void thread_set_vg_running_tid(const ThreadId vg_tid); |
| 123 | void thread_set_running_tid(const ThreadId vg_tid, |
| 124 | const DrdThreadId drd_tid); |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 125 | int thread_enter_synchr(const DrdThreadId tid); |
| 126 | int thread_leave_synchr(const DrdThreadId tid); |
| 127 | int thread_get_synchr_nesting_count(const DrdThreadId tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 128 | void thread_new_segment(const DrdThreadId tid); |
| 129 | VectorClock* thread_get_vc(const DrdThreadId tid); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 130 | void thread_get_latest_segment(Segment** sg, const DrdThreadId tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 131 | void thread_combine_vc(const DrdThreadId joiner, const DrdThreadId joinee); |
| 132 | void thread_combine_vc2(const DrdThreadId tid, const VectorClock* const vc); |
bart | dfbae6e | 2008-06-05 08:29:53 +0000 | [diff] [blame] | 133 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 134 | void thread_stop_using_mem(const Addr a1, const Addr a2); |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 135 | void thread_start_recording(const DrdThreadId tid); |
| 136 | void thread_stop_recording(const DrdThreadId tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 137 | void thread_print_all(void); |
| 138 | void thread_report_races(const DrdThreadId tid); |
| 139 | void thread_report_races_segment(const DrdThreadId tid, |
| 140 | const Segment* const p); |
| 141 | void thread_report_all_races(void); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 142 | void thread_report_conflicting_segments(const DrdThreadId tid, |
| 143 | const Addr addr, |
| 144 | const SizeT size, |
| 145 | const BmAccessTypeT access_type); |
| 146 | ULong thread_get_context_switch_count(void); |
| 147 | ULong thread_get_report_races_count(void); |
| 148 | ULong thread_get_discard_ordered_segments_count(void); |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 149 | ULong thread_get_update_conflict_set_count(ULong* dsnsc, ULong* dscvc); |
| 150 | ULong thread_get_conflict_set_bitmap_creation_count(void); |
| 151 | ULong thread_get_conflict_set_bitmap2_creation_count(void); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 152 | |
| 153 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 154 | /* Inline function definitions. */ |
| 155 | |
bart | bf80e12 | 2008-06-06 10:18:24 +0000 | [diff] [blame] | 156 | static __inline__ |
| 157 | Bool IsValidDrdThreadId(const DrdThreadId tid) |
| 158 | { |
| 159 | return (0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 160 | && ! (DRD_(g_threadinfo)[tid].vg_thread_exists == False |
| 161 | && DRD_(g_threadinfo)[tid].posix_thread_exists == False |
| 162 | && DRD_(g_threadinfo)[tid].detached_posix_thread == False)); |
bart | bf80e12 | 2008-06-06 10:18:24 +0000 | [diff] [blame] | 163 | } |
| 164 | |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 165 | static __inline__ |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 166 | DrdThreadId thread_get_running_tid(void) |
| 167 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 168 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
| 169 | return DRD_(g_drd_running_tid); |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 170 | } |
| 171 | |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 172 | static __inline__ |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 173 | struct bitmap* thread_get_conflict_set(void) |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 174 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 175 | return DRD_(g_conflict_set); |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 176 | } |
| 177 | |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 178 | static __inline__ |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 179 | Bool running_thread_is_recording(void) |
| 180 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 181 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 182 | tl_assert(0 <= (int)DRD_(g_drd_running_tid) && DRD_(g_drd_running_tid) < DRD_N_THREADS |
| 183 | && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
bart | 589f948 | 2008-06-09 15:08:22 +0000 | [diff] [blame] | 184 | #endif |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 185 | return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0 |
| 186 | && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording); |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 187 | } |
| 188 | |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 189 | static __inline__ |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 190 | void thread_set_stack_min(const DrdThreadId tid, const Addr stack_min) |
| 191 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 192 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | f0cdc60 | 2008-06-11 18:37:59 +0000 | [diff] [blame] | 193 | tl_assert(0 <= (int)tid |
| 194 | && tid < DRD_N_THREADS |
| 195 | && tid != DRD_INVALID_THREADID); |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 196 | #endif |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 197 | DRD_(g_threadinfo)[tid].stack_min = stack_min; |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 198 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | fda6492 | 2008-03-29 13:13:33 +0000 | [diff] [blame] | 199 | /* This function can be called after the thread has been created but */ |
| 200 | /* before drd_post_thread_create() has filled in stack_max. */ |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 201 | tl_assert(DRD_(g_threadinfo)[tid].stack_min < DRD_(g_threadinfo)[tid].stack_max |
| 202 | || DRD_(g_threadinfo)[tid].stack_max == 0); |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 203 | #endif |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 204 | if (UNLIKELY(stack_min < DRD_(g_threadinfo)[tid].stack_min_min)) |
bart | e773de4 | 2008-03-29 12:54:01 +0000 | [diff] [blame] | 205 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 206 | DRD_(g_threadinfo)[tid].stack_min_min = stack_min; |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 210 | /** |
| 211 | * Return true if and only if the specified address is on the stack of the |
| 212 | * currently scheduled thread. |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 213 | */ |
| 214 | static __inline__ |
| 215 | Bool thread_address_on_stack(const Addr a) |
| 216 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 217 | return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_min <= a |
| 218 | && a < DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_max); |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 219 | } |
| 220 | |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 221 | /** Return a pointer to the latest segment for the specified thread. */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 222 | static __inline__ |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 223 | Segment* thread_get_segment(const DrdThreadId tid) |
| 224 | { |
bart | 8b4b2ee | 2008-06-11 13:17:56 +0000 | [diff] [blame] | 225 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 226 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 227 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 228 | tl_assert(DRD_(g_threadinfo)[tid].last); |
bart | 589f948 | 2008-06-09 15:08:22 +0000 | [diff] [blame] | 229 | #endif |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 230 | return DRD_(g_threadinfo)[tid].last; |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 231 | } |
| 232 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 233 | /** Return a pointer to the latest segment for the running thread. */ |
bart | 0886562 | 2008-06-06 14:31:36 +0000 | [diff] [blame] | 234 | static __inline__ |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 235 | Segment* running_thread_get_segment(void) |
| 236 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 237 | return thread_get_segment(DRD_(g_drd_running_tid)); |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 238 | } |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 239 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 240 | #endif // __THREAD_H |