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