Bug 471036 - disInstr_AMD64: disInstr miscalculated next %rip on RORX imm8, m32/64...
[valgrind.git] / drd / drd_thread.h
blob0f7d783733d512d6d5f9b3ed11f0e5a66a88229a
1 /*
2 This file is part of drd, a thread error detector.
4 Copyright (C) 2006-2020 Bart Van Assche <bvanassche@acm.org>.
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.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
19 The GNU General Public License is contained in the file COPYING.
23 #ifndef __THREAD_H
24 #define __THREAD_H
27 /* Include directives. */
29 #include "drd_basics.h"
30 #include "drd_segment.h"
31 #include "pub_drd_bitmap.h"
32 #include "pub_tool_libcassert.h" /* tl_assert() */
33 #include "pub_tool_stacktrace.h" /* typedef StackTrace */
34 #include "pub_tool_threadstate.h" /* VG_N_THREADS */
37 /* Defines. */
39 /** Maximum number of threads DRD keeps information about. */
40 #define DRD_N_THREADS VG_N_THREADS
42 /** A number different from any valid DRD thread ID. */
43 #define DRD_INVALID_THREADID 0
45 /**
46 * A number different from any valid POSIX thread ID.
48 * @note The PThreadId typedef and the INVALID_POSIX_THREADID depend on the
49 * operating system and threading library in use. PThreadId must contain at
50 * least as many bits as pthread_t, and INVALID_POSIX_THREADID
51 * must be a value that will never be returned by pthread_self().
53 #define INVALID_POSIX_THREADID ((PThreadId)0)
56 /* Type definitions. */
58 /**
59 * POSIX thread ID. The type PThreadId must be at least as wide as
60 * pthread_t.
62 typedef UWord PThreadId;
64 /** Per-thread information managed by DRD. */
65 typedef struct
67 struct segment* sg_first;/**< Segment list. */
68 struct segment* sg_last;
69 ThreadId vg_threadid; /**< Valgrind thread ID. */
70 PThreadId pt_threadid; /**< POSIX thread ID. */
71 Addr stack_min_min; /**< Lowest value stack pointer ever had. */
72 Addr stack_min; /**< Current stack pointer. */
73 Addr stack_startup; /**<Stack pointer after pthread_create() finished.*/
74 Addr stack_max; /**< Top of stack. */
75 SizeT stack_size; /**< Maximum size of stack. */
76 HChar name[64]; /**< User-assigned thread name. */
77 Bool on_alt_stack;
78 /** Whether this structure contains valid information. */
79 Bool valid;
80 /** Indicates whether the Valgrind core knows about this thread. */
81 Bool vg_thread_exists;
82 /** Indicates whether there is an associated POSIX thread ID. */
83 Bool posix_thread_exists;
84 /**
85 * If true, indicates that there is a corresponding POSIX thread ID and
86 * a corresponding OS thread that is detached.
88 Bool detached_posix_thread;
89 /** Whether recording of memory load accesses is currently enabled. */
90 Bool is_recording_loads;
91 /** Whether recording of memory load accesses is currently enabled. */
92 Bool is_recording_stores;
93 /** pthread_create() nesting level. */
94 Int pthread_create_nesting_level;
95 /** Nesting level of synchronization functions called by the client. */
96 Int synchr_nesting;
97 /** Delayed thread deletion sequence number. */
98 unsigned deletion_seq;
99 /**
100 * ID of the creator thread. It can be safely accessed only until the
101 * thread is fully created. Then the creator thread lives its own life again.
103 DrdThreadId creator_thread;
105 #if defined(VGO_solaris)
106 Int bind_guard_flag; /**< Bind flag from the runtime linker. */
107 #endif /* VGO_solaris */
108 } ThreadInfo;
112 * Local variables of drd_thread.c that are declared here such that these
113 * can be accessed by inline functions.
117 * DRD thread ID of the currently running thread. It is crucial for correct
118 * operation of DRD that this number is always in sync with
119 * VG_(get_running_tid)().
121 extern DrdThreadId DRD_(g_drd_running_tid);
122 /** Per-thread information managed by DRD. */
123 extern ThreadInfo* DRD_(g_threadinfo);
124 /** Conflict set for the currently running thread. */
125 extern struct bitmap* DRD_(g_conflict_set);
126 extern Bool DRD_(verify_conflict_set);
127 /** Whether activities during thread creation should be ignored. */
128 extern Bool DRD_(ignore_thread_creation);
131 /* Function declarations. */
133 void DRD_(thread_trace_context_switches)(const Bool t);
134 void DRD_(thread_trace_conflict_set)(const Bool t);
135 void DRD_(thread_trace_conflict_set_bm)(const Bool t);
136 Bool DRD_(thread_get_trace_fork_join)(void);
137 void DRD_(thread_set_trace_fork_join)(const Bool t);
138 void DRD_(thread_set_segment_merging)(const Bool m);
139 int DRD_(thread_get_segment_merge_interval)(void);
140 void DRD_(thread_set_segment_merge_interval)(const int i);
141 void DRD_(thread_set_join_list_vol)(const int jlv);
143 void DRD_(thread_init)(void);
144 DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid);
145 DrdThreadId DRD_(NewVgThreadIdToDrdThreadId)(const ThreadId tid);
146 DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid);
147 ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid);
148 DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
149 const ThreadId vg_created);
150 DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created);
151 void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee);
152 void DRD_(thread_delete)(const DrdThreadId tid, Bool detached);
153 void DRD_(thread_finished)(const DrdThreadId tid);
154 void DRD_(drd_thread_atfork_child)(const DrdThreadId tid);
155 void DRD_(thread_pre_cancel)(const DrdThreadId tid);
156 void DRD_(thread_register_stack)(DrdThreadId tid, Addr addr1, Addr addr2);
157 void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
158 const Addr stack_startup);
159 Addr DRD_(thread_get_stack_min)(const DrdThreadId tid);
160 Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid);
161 Addr DRD_(thread_get_stack_max)(const DrdThreadId tid);
162 SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid);
163 Bool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid);
164 void DRD_(thread_set_on_alt_stack)(const DrdThreadId tid,
165 const Bool on_alt_stack);
166 Int DRD_(thread_get_threads_on_alt_stack)(void);
167 void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid);
168 Bool DRD_(thread_get_joinable)(const DrdThreadId tid);
169 void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable);
170 void DRD_(thread_entering_pthread_create)(const DrdThreadId tid);
171 void DRD_(thread_left_pthread_create)(const DrdThreadId tid);
172 #if defined(VGO_solaris)
173 void DRD_(thread_entering_rtld_bind_guard)(const DrdThreadId tid, int flags);
174 void DRD_(thread_leaving_rtld_bind_clear)(const DrdThreadId tid, int flags);
175 #endif /* VGO_solaris */
176 const HChar* DRD_(thread_get_name)(const DrdThreadId tid);
177 void DRD_(thread_set_name)(const DrdThreadId tid, const HChar* const name);
178 void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid);
179 void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
180 const DrdThreadId drd_tid);
181 int DRD_(thread_enter_synchr)(const DrdThreadId tid);
182 int DRD_(thread_leave_synchr)(const DrdThreadId tid);
183 int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid);
184 void DRD_(thread_new_segment)(const DrdThreadId tid);
185 VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid);
186 void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid);
187 void DRD_(thread_combine_vc_join)(const DrdThreadId joiner,
188 const DrdThreadId joinee);
189 void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid,
190 const Segment* sg);
191 void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
192 const VectorClock* const old_vc);
194 void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2);
195 void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled);
196 void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled);
197 void DRD_(thread_print_all)(void);
198 void DRD_(thread_report_races)(const DrdThreadId tid);
199 void DRD_(thread_report_races_segment)(const DrdThreadId tid,
200 const Segment* const p);
201 void DRD_(thread_report_all_races)(void);
202 void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
203 const Addr addr,
204 const SizeT size,
205 const BmAccessTypeT access_type);
206 ULong DRD_(thread_get_context_switch_count)(void);
207 ULong DRD_(thread_get_report_races_count)(void);
208 ULong DRD_(thread_get_discard_ordered_segments_count)(void);
209 ULong DRD_(thread_get_compute_conflict_set_count)(void);
210 ULong DRD_(thread_get_update_conflict_set_count)(void);
211 ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void);
212 ULong DRD_(thread_get_update_conflict_set_sync_count)(void);
213 ULong DRD_(thread_get_update_conflict_set_join_count)(void);
214 ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void);
215 ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void);
218 /* Inline function definitions. */
221 * Whether or not the specified DRD thread ID is valid.
223 * A DRD thread ID is valid if and only if the following conditions are met:
224 * - The ID is a valid index of the DRD_(g_threadinfo)[] array.
225 * - The ID is not equal to DRD_INVALID_THREADID.
226 * - The ID refers either to a thread known by the Valgrind core, a joinable
227 * thread that has not yet been joined or a detached thread.
229 static __inline__
230 Bool DRD_(IsValidDrdThreadId)(const DrdThreadId tid)
232 return (0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID
233 && (DRD_(g_threadinfo)[tid].valid));
236 /** Returns the DRD thread ID of the currently running thread. */
237 static __inline__
238 DrdThreadId DRD_(thread_get_running_tid)(void)
240 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
241 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
242 #endif
243 return DRD_(g_drd_running_tid);
246 /** Returns a pointer to the conflict set for the currently running thread. */
247 static __inline__
248 struct bitmap* DRD_(thread_get_conflict_set)(void)
250 return DRD_(g_conflict_set);
254 * Reports whether or not the currently running client thread is executing code
255 * inside the pthread_create() function.
257 static __inline__
258 Bool DRD_(running_thread_inside_pthread_create)(void)
260 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)]
261 .pthread_create_nesting_level > 0);
265 * Reports whether or not recording of memory loads is enabled for the
266 * currently running client thread.
268 static __inline__
269 Bool DRD_(running_thread_is_recording_loads)(void)
271 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
272 tl_assert(0 <= (int)DRD_(g_drd_running_tid)
273 && DRD_(g_drd_running_tid) < DRD_N_THREADS
274 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
275 #endif
276 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0
277 && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_loads);
281 * Reports whether or not recording memory stores is enabled for the
282 * currently running client thread.
284 static __inline__
285 Bool DRD_(running_thread_is_recording_stores)(void)
287 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
288 tl_assert(0 <= (int)DRD_(g_drd_running_tid)
289 && DRD_(g_drd_running_tid) < DRD_N_THREADS
290 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
291 #endif
292 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0
293 && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_stores);
297 * Update the information about the lowest stack address that has ever been
298 * accessed by a thread.
300 static __inline__
301 void DRD_(thread_set_stack_min)(const DrdThreadId tid, const Addr stack_min)
303 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
304 tl_assert(0 <= (int)tid
305 && tid < DRD_N_THREADS
306 && tid != DRD_INVALID_THREADID);
307 #endif
308 DRD_(g_threadinfo)[tid].stack_min = stack_min;
309 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
310 /* This function can be called after the thread has been created but */
311 /* before drd_post_thread_create() has filled in stack_max. */
312 tl_assert(DRD_(g_threadinfo)[tid].stack_min
313 <= DRD_(g_threadinfo)[tid].stack_max
314 || DRD_(g_threadinfo)[tid].stack_max == 0);
315 #endif
316 if (UNLIKELY(stack_min < DRD_(g_threadinfo)[tid].stack_min_min))
318 DRD_(g_threadinfo)[tid].stack_min_min = stack_min;
323 * Return true if and only if the specified address is on the stack of the
324 * currently scheduled thread.
326 static __inline__
327 Bool DRD_(thread_address_on_stack)(const Addr a)
329 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_min <= a
330 && a < DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_max);
334 * Return true if and only if the specified address is on the stack of any
335 * thread.
337 static __inline__
338 Bool DRD_(thread_address_on_any_stack)(const Addr a)
340 UInt i;
342 for (i = 1; i < DRD_N_THREADS; i++)
344 if (DRD_(g_threadinfo)[i].vg_thread_exists
345 && DRD_(g_threadinfo)[i].stack_min <= a
346 && a < DRD_(g_threadinfo)[i].stack_max)
348 return True;
351 return False;
354 /** Return a pointer to the latest segment for the specified thread. */
355 static __inline__
356 Segment* DRD_(thread_get_segment)(const DrdThreadId tid)
358 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
359 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
360 && tid != DRD_INVALID_THREADID);
361 tl_assert(DRD_(g_threadinfo)[tid].sg_last);
362 #endif
363 return DRD_(g_threadinfo)[tid].sg_last;
366 /** Return a pointer to the latest segment for the running thread. */
367 static __inline__
368 Segment* DRD_(running_thread_get_segment)(void)
370 return DRD_(thread_get_segment)(DRD_(g_drd_running_tid));
373 #endif /* __THREAD_H */