Add statistics printing for the new trace construction algorithm.
[valgrind.git] / drd / drd_thread.h
blobb42b6d85892bf63d47b5499ad430bb77cc8925d8
1 /*
2 This file is part of drd, a thread error detector.
4 Copyright (C) 2006-2017 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_set_stack_startup)(const DrdThreadId tid,
157 const Addr stack_startup);
158 Addr DRD_(thread_get_stack_min)(const DrdThreadId tid);
159 Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid);
160 Addr DRD_(thread_get_stack_max)(const DrdThreadId tid);
161 SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid);
162 Bool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid);
163 void DRD_(thread_set_on_alt_stack)(const DrdThreadId tid,
164 const Bool on_alt_stack);
165 Int DRD_(thread_get_threads_on_alt_stack)(void);
166 void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid);
167 Bool DRD_(thread_get_joinable)(const DrdThreadId tid);
168 void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable);
169 void DRD_(thread_entering_pthread_create)(const DrdThreadId tid);
170 void DRD_(thread_left_pthread_create)(const DrdThreadId tid);
171 #if defined(VGO_solaris)
172 void DRD_(thread_entering_rtld_bind_guard)(const DrdThreadId tid, int flags);
173 void DRD_(thread_leaving_rtld_bind_clear)(const DrdThreadId tid, int flags);
174 #endif /* VGO_solaris */
175 const HChar* DRD_(thread_get_name)(const DrdThreadId tid);
176 void DRD_(thread_set_name)(const DrdThreadId tid, const HChar* const name);
177 void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid);
178 void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
179 const DrdThreadId drd_tid);
180 int DRD_(thread_enter_synchr)(const DrdThreadId tid);
181 int DRD_(thread_leave_synchr)(const DrdThreadId tid);
182 int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid);
183 void DRD_(thread_new_segment)(const DrdThreadId tid);
184 VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid);
185 void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid);
186 void DRD_(thread_combine_vc_join)(const DrdThreadId joiner,
187 const DrdThreadId joinee);
188 void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid,
189 const Segment* sg);
190 void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
191 const VectorClock* const old_vc);
193 void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2);
194 void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled);
195 void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled);
196 void DRD_(thread_print_all)(void);
197 void DRD_(thread_report_races)(const DrdThreadId tid);
198 void DRD_(thread_report_races_segment)(const DrdThreadId tid,
199 const Segment* const p);
200 void DRD_(thread_report_all_races)(void);
201 void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
202 const Addr addr,
203 const SizeT size,
204 const BmAccessTypeT access_type);
205 ULong DRD_(thread_get_context_switch_count)(void);
206 ULong DRD_(thread_get_report_races_count)(void);
207 ULong DRD_(thread_get_discard_ordered_segments_count)(void);
208 ULong DRD_(thread_get_compute_conflict_set_count)(void);
209 ULong DRD_(thread_get_update_conflict_set_count)(void);
210 ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void);
211 ULong DRD_(thread_get_update_conflict_set_sync_count)(void);
212 ULong DRD_(thread_get_update_conflict_set_join_count)(void);
213 ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void);
214 ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void);
217 /* Inline function definitions. */
220 * Whether or not the specified DRD thread ID is valid.
222 * A DRD thread ID is valid if and only if the following conditions are met:
223 * - The ID is a valid index of the DRD_(g_threadinfo)[] array.
224 * - The ID is not equal to DRD_INVALID_THREADID.
225 * - The ID refers either to a thread known by the Valgrind core, a joinable
226 * thread that has not yet been joined or a detached thread.
228 static __inline__
229 Bool DRD_(IsValidDrdThreadId)(const DrdThreadId tid)
231 return (0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID
232 && (DRD_(g_threadinfo)[tid].valid));
235 /** Returns the DRD thread ID of the currently running thread. */
236 static __inline__
237 DrdThreadId DRD_(thread_get_running_tid)(void)
239 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
240 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
241 #endif
242 return DRD_(g_drd_running_tid);
245 /** Returns a pointer to the conflict set for the currently running thread. */
246 static __inline__
247 struct bitmap* DRD_(thread_get_conflict_set)(void)
249 return DRD_(g_conflict_set);
253 * Reports whether or not the currently running client thread is executing code
254 * inside the pthread_create() function.
256 static __inline__
257 Bool DRD_(running_thread_inside_pthread_create)(void)
259 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)]
260 .pthread_create_nesting_level > 0);
264 * Reports whether or not recording of memory loads is enabled for the
265 * currently running client thread.
267 static __inline__
268 Bool DRD_(running_thread_is_recording_loads)(void)
270 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
271 tl_assert(0 <= (int)DRD_(g_drd_running_tid)
272 && DRD_(g_drd_running_tid) < DRD_N_THREADS
273 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
274 #endif
275 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0
276 && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_loads);
280 * Reports whether or not recording memory stores is enabled for the
281 * currently running client thread.
283 static __inline__
284 Bool DRD_(running_thread_is_recording_stores)(void)
286 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
287 tl_assert(0 <= (int)DRD_(g_drd_running_tid)
288 && DRD_(g_drd_running_tid) < DRD_N_THREADS
289 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
290 #endif
291 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0
292 && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_stores);
296 * Update the information about the lowest stack address that has ever been
297 * accessed by a thread.
299 static __inline__
300 void DRD_(thread_set_stack_min)(const DrdThreadId tid, const Addr stack_min)
302 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
303 tl_assert(0 <= (int)tid
304 && tid < DRD_N_THREADS
305 && tid != DRD_INVALID_THREADID);
306 #endif
307 DRD_(g_threadinfo)[tid].stack_min = stack_min;
308 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
309 /* This function can be called after the thread has been created but */
310 /* before drd_post_thread_create() has filled in stack_max. */
311 tl_assert(DRD_(g_threadinfo)[tid].stack_min
312 <= DRD_(g_threadinfo)[tid].stack_max
313 || DRD_(g_threadinfo)[tid].stack_max == 0);
314 #endif
315 if (UNLIKELY(stack_min < DRD_(g_threadinfo)[tid].stack_min_min))
317 DRD_(g_threadinfo)[tid].stack_min_min = stack_min;
322 * Return true if and only if the specified address is on the stack of the
323 * currently scheduled thread.
325 static __inline__
326 Bool DRD_(thread_address_on_stack)(const Addr a)
328 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_min <= a
329 && a < DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_max);
333 * Return true if and only if the specified address is on the stack of any
334 * thread.
336 static __inline__
337 Bool DRD_(thread_address_on_any_stack)(const Addr a)
339 UInt i;
341 for (i = 1; i < DRD_N_THREADS; i++)
343 if (DRD_(g_threadinfo)[i].vg_thread_exists
344 && DRD_(g_threadinfo)[i].stack_min <= a
345 && a < DRD_(g_threadinfo)[i].stack_max)
347 return True;
350 return False;
353 /** Return a pointer to the latest segment for the specified thread. */
354 static __inline__
355 Segment* DRD_(thread_get_segment)(const DrdThreadId tid)
357 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
358 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
359 && tid != DRD_INVALID_THREADID);
360 tl_assert(DRD_(g_threadinfo)[tid].sg_last);
361 #endif
362 return DRD_(g_threadinfo)[tid].sg_last;
365 /** Return a pointer to the latest segment for the running thread. */
366 static __inline__
367 Segment* DRD_(running_thread_get_segment)(void)
369 return DRD_(thread_get_segment)(DRD_(g_drd_running_tid));
372 #endif /* __THREAD_H */