1 /* -*- mode: C; c-basic-offset: 3; -*- */
3 This file is part of drd, a thread error detector.
5 Copyright (C) 2006-2010 Bart Van Assche <bart.vanassche@gmail.com>.
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.
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.
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
22 The GNU General Public License is contained in the file COPYING.
30 /* Include directives. */
32 #include "drd_basics.h"
33 #include "drd_segment.h"
34 #include "pub_drd_bitmap.h"
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 */
42 /** Maximum number of threads DRD keeps information about. */
43 #define DRD_N_THREADS VG_N_THREADS
45 /** A number different from any valid DRD thread ID. */
46 #define DRD_INVALID_THREADID 0
49 * A number different from any valid POSIX thread ID.
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().
56 #define INVALID_POSIX_THREADID ((PThreadId)0)
59 /* Type definitions. */
62 * POSIX thread ID. The type PThreadId must be at least as wide as
65 typedef UWord PThreadId;
67 /** Per-thread information managed by DRD. */
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. */
79 char name[64]; /**< User-assigned thread name. */
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;
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 /** Wether recording of memory load accesses is currently enabled. */
90 Bool is_recording_loads;
91 /** Wether 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. */
101 * Local variables of drd_thread.c that are declared here such that these
102 * can be accessed by inline functions.
106 * DRD thread ID of the currently running thread. It is crucial for correct
107 * operation of DRD that this number is always in sync with
108 * VG_(get_running_tid)().
110 extern DrdThreadId DRD_(g_drd_running_tid);
111 /** Per-thread information managed by DRD. */
112 extern ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS];
113 /** Conflict set for the currently running thread. */
114 extern struct bitmap* DRD_(g_conflict_set);
117 /* Function declarations. */
119 void DRD_(thread_trace_context_switches)(const Bool t);
120 void DRD_(thread_trace_conflict_set)(const Bool t);
121 void DRD_(thread_trace_conflict_set_bm)(const Bool t);
122 Bool DRD_(thread_get_trace_fork_join)(void);
123 void DRD_(thread_set_trace_fork_join)(const Bool t);
124 void DRD_(thread_set_segment_merging)(const Bool m);
125 int DRD_(thread_get_segment_merge_interval)(void);
126 void DRD_(thread_set_segment_merge_interval)(const int i);
128 DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid);
129 DrdThreadId DRD_(NewVgThreadIdToDrdThreadId)(const ThreadId tid);
130 DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid);
131 ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid);
132 DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
133 const ThreadId vg_created);
134 DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created);
135 void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee);
136 void DRD_(thread_delete)(const DrdThreadId tid);
137 void DRD_(thread_finished)(const DrdThreadId tid);
138 void DRD_(thread_pre_cancel)(const DrdThreadId tid);
139 void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
140 const Addr stack_startup);
141 Addr DRD_(thread_get_stack_min)(const DrdThreadId tid);
142 Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid);
143 Addr DRD_(thread_get_stack_max)(const DrdThreadId tid);
144 SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid);
145 void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid);
146 Bool DRD_(thread_get_joinable)(const DrdThreadId tid);
147 void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable);
148 void DRD_(thread_entering_pthread_create)(const DrdThreadId tid);
149 void DRD_(thread_left_pthread_create)(const DrdThreadId tid);
150 const char* DRD_(thread_get_name)(const DrdThreadId tid);
151 void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name);
152 void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid);
153 void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
154 const DrdThreadId drd_tid);
155 int DRD_(thread_enter_synchr)(const DrdThreadId tid);
156 int DRD_(thread_leave_synchr)(const DrdThreadId tid);
157 int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid);
158 void DRD_(thread_new_segment)(const DrdThreadId tid);
159 VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid);
160 void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid);
161 void DRD_(thread_combine_vc_join)(const DrdThreadId joiner,
162 const DrdThreadId joinee);
163 void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid,
165 void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
166 const VectorClock* const old_vc);
168 void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2);
169 void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled);
170 void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled);
171 void DRD_(thread_print_all)(void);
172 void DRD_(thread_report_races)(const DrdThreadId tid);
173 void DRD_(thread_report_races_segment)(const DrdThreadId tid,
174 const Segment* const p);
175 void DRD_(thread_report_all_races)(void);
176 void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
179 const BmAccessTypeT access_type);
180 ULong DRD_(thread_get_context_switch_count)(void);
181 ULong DRD_(thread_get_report_races_count)(void);
182 ULong DRD_(thread_get_discard_ordered_segments_count)(void);
183 ULong DRD_(thread_get_compute_conflict_set_count)(void);
184 ULong DRD_(thread_get_update_conflict_set_count)(void);
185 ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void);
186 ULong DRD_(thread_get_update_conflict_set_sync_count)(void);
187 ULong DRD_(thread_get_update_conflict_set_join_count)(void);
188 ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void);
189 ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void);
192 /* Inline function definitions. */
195 * Whether or not the specified DRD thread ID is valid.
197 * A DRD thread ID is valid if and only if the following conditions are met:
198 * - The ID is a valid index of the DRD_(g_threadinfo)[] array.
199 * - The ID is not equal to DRD_INVALID_THREADID.
200 * - The ID refers either to a thread known by the Valgrind core, a joinable
201 * thread that has not yet been joined or a detached thread.
204 Bool DRD_(IsValidDrdThreadId)(const DrdThreadId tid)
206 return (0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID
207 && ! (DRD_(g_threadinfo)[tid].vg_thread_exists == False
208 && DRD_(g_threadinfo)[tid].posix_thread_exists == False
209 && DRD_(g_threadinfo)[tid].detached_posix_thread == False));
212 /** Returns the DRD thread ID of the currently running thread. */
214 DrdThreadId DRD_(thread_get_running_tid)(void)
216 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
217 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
219 return DRD_(g_drd_running_tid);
222 /** Returns a pointer to the conflict set for the currently running thread. */
224 struct bitmap* DRD_(thread_get_conflict_set)(void)
226 return DRD_(g_conflict_set);
230 * Reports whether or not the currently running client thread is executing code
231 * inside the pthread_create() function.
234 Bool DRD_(running_thread_inside_pthread_create)(void)
236 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)]
237 .pthread_create_nesting_level > 0);
241 * Reports whether or not recording of memory loads is enabled for the
242 * currently running client thread.
245 Bool DRD_(running_thread_is_recording_loads)(void)
247 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
248 tl_assert(0 <= (int)DRD_(g_drd_running_tid)
249 && DRD_(g_drd_running_tid) < DRD_N_THREADS
250 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
252 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0
253 && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_loads);
257 * Reports whether or not recording memory stores is enabled for the
258 * currently running client thread.
261 Bool DRD_(running_thread_is_recording_stores)(void)
263 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
264 tl_assert(0 <= (int)DRD_(g_drd_running_tid)
265 && DRD_(g_drd_running_tid) < DRD_N_THREADS
266 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
268 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0
269 && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_stores);
273 * Update the information about the lowest stack address that has ever been
274 * accessed by a thread.
277 void DRD_(thread_set_stack_min)(const DrdThreadId tid, const Addr stack_min)
279 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
280 tl_assert(0 <= (int)tid
281 && tid < DRD_N_THREADS
282 && tid != DRD_INVALID_THREADID);
284 DRD_(g_threadinfo)[tid].stack_min = stack_min;
285 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
286 /* This function can be called after the thread has been created but */
287 /* before drd_post_thread_create() has filled in stack_max. */
288 tl_assert(DRD_(g_threadinfo)[tid].stack_min
289 < DRD_(g_threadinfo)[tid].stack_max
290 || DRD_(g_threadinfo)[tid].stack_max == 0);
292 if (UNLIKELY(stack_min < DRD_(g_threadinfo)[tid].stack_min_min))
294 DRD_(g_threadinfo)[tid].stack_min_min = stack_min;
299 * Return true if and only if the specified address is on the stack of the
300 * currently scheduled thread.
303 Bool DRD_(thread_address_on_stack)(const Addr a)
305 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_min <= a
306 && a < DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_max);
310 * Return true if and only if the specified address is on the stack of any
314 Bool DRD_(thread_address_on_any_stack)(const Addr a)
318 for (i = 1; i < DRD_N_THREADS; i++)
320 if (DRD_(g_threadinfo)[i].vg_thread_exists
321 && DRD_(g_threadinfo)[i].stack_min <= a
322 && a < DRD_(g_threadinfo)[i].stack_max)
330 /** Return a pointer to the latest segment for the specified thread. */
332 Segment* DRD_(thread_get_segment)(const DrdThreadId tid)
334 #ifdef ENABLE_DRD_CONSISTENCY_CHECKS
335 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
336 && tid != DRD_INVALID_THREADID);
337 tl_assert(DRD_(g_threadinfo)[tid].last);
339 return DRD_(g_threadinfo)[tid].last;
342 /** Return a pointer to the latest segment for the running thread. */
344 Segment* DRD_(running_thread_get_segment)(void)
346 return DRD_(thread_get_segment)(DRD_(g_drd_running_tid));
349 #endif /* __THREAD_H */