blob: 6b73d8c0bcad848139596ba2291127d0251fa0f6 [file] [log] [blame]
bartbedfd232009-03-26 19:07:15 +00001/* -*- mode: C; c-basic-offset: 3; -*- */
sewardjaf44c822007-11-25 14:01:38 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00004
bart876cafd2010-10-10 18:07:31 +00005 Copyright (C) 2006-2010 Bart Van Assche <bvanassche@acm.org>.
sewardjaf44c822007-11-25 14:01:38 +00006
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
bart324a23b2009-02-15 12:14:52 +000030/* Include directives. */
sewardjaf44c822007-11-25 14:01:38 +000031
bart09dc13f2009-02-14 15:13:31 +000032#include "drd_basics.h"
bartf00a85b2008-03-13 18:49:23 +000033#include "drd_segment.h"
bartd59bb0f2008-06-08 08:08:31 +000034#include "pub_drd_bitmap.h"
bart86a87df2009-03-04 19:26:47 +000035#include "pub_tool_libcassert.h" /* tl_assert() */
36#include "pub_tool_stacktrace.h" /* typedef StackTrace */
37#include "pub_tool_threadstate.h" /* VG_N_THREADS */
bartf00a85b2008-03-13 18:49:23 +000038
39
bart324a23b2009-02-15 12:14:52 +000040/* Defines. */
bartf00a85b2008-03-13 18:49:23 +000041
bart86a87df2009-03-04 19:26:47 +000042/** Maximum number of threads DRD keeps information about. */
bartf00a85b2008-03-13 18:49:23 +000043#define DRD_N_THREADS VG_N_THREADS
sewardjaf44c822007-11-25 14:01:38 +000044
bart86a87df2009-03-04 19:26:47 +000045/** A number different from any valid DRD thread ID. */
sewardjaf44c822007-11-25 14:01:38 +000046#define DRD_INVALID_THREADID 0
47
bart86a87df2009-03-04 19:26:47 +000048/**
49 * A number different from any valid POSIX thread ID.
50 *
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().
55 */
sewardjaf44c822007-11-25 14:01:38 +000056#define INVALID_POSIX_THREADID ((PThreadId)0)
57
58
bart324a23b2009-02-15 12:14:52 +000059/* Type definitions. */
bartf00a85b2008-03-13 18:49:23 +000060
bart86a87df2009-03-04 19:26:47 +000061/**
62 * POSIX thread ID. The type PThreadId must be at least as wide as
63 * pthread_t.
64 */
sewardjaf44c822007-11-25 14:01:38 +000065typedef UWord PThreadId;
66
bart86a87df2009-03-04 19:26:47 +000067/** Per-thread information managed by DRD. */
bartf00a85b2008-03-13 18:49:23 +000068typedef struct
69{
bartbedfd232009-03-26 19:07:15 +000070 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. */
bartd45d9952009-05-31 18:53:54 +000079 char name[64]; /**< User-assigned thread name. */
bart383d6132010-09-02 14:43:18 +000080 Bool on_alt_stack;
bartbedfd232009-03-26 19:07:15 +000081 /** Indicates whether the Valgrind core knows about this thread. */
82 Bool vg_thread_exists;
83 /** Indicates whether there is an associated POSIX thread ID. */
84 Bool posix_thread_exists;
85 /**
86 * If true, indicates that there is a corresponding POSIX thread ID and
87 * a corresponding OS thread that is detached.
88 */
89 Bool detached_posix_thread;
bartd45d9952009-05-31 18:53:54 +000090 /** Wether recording of memory load accesses is currently enabled. */
91 Bool is_recording_loads;
92 /** Wether recording of memory load accesses is currently enabled. */
93 Bool is_recording_stores;
bartdd75cdf2009-07-24 08:20:10 +000094 /** pthread_create() nesting level. */
95 Int pthread_create_nesting_level;
bartbedfd232009-03-26 19:07:15 +000096 /** Nesting level of synchronization functions called by the client. */
97 Int synchr_nesting;
bartf00a85b2008-03-13 18:49:23 +000098} ThreadInfo;
99
100
bart324a23b2009-02-15 12:14:52 +0000101/*
102 * Local variables of drd_thread.c that are declared here such that these
103 * can be accessed by inline functions.
104 */
bartf00a85b2008-03-13 18:49:23 +0000105
bart86a87df2009-03-04 19:26:47 +0000106/**
107 * DRD thread ID of the currently running thread. It is crucial for correct
108 * operation of DRD that this number is always in sync with
109 * VG_(get_running_tid)().
110 */
bart324a23b2009-02-15 12:14:52 +0000111extern DrdThreadId DRD_(g_drd_running_tid);
bart86a87df2009-03-04 19:26:47 +0000112/** Per-thread information managed by DRD. */
bart324a23b2009-02-15 12:14:52 +0000113extern ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS];
bart86a87df2009-03-04 19:26:47 +0000114/** Conflict set for the currently running thread. */
bart324a23b2009-02-15 12:14:52 +0000115extern struct bitmap* DRD_(g_conflict_set);
bartf00a85b2008-03-13 18:49:23 +0000116
117
bart324a23b2009-02-15 12:14:52 +0000118/* Function declarations. */
sewardjaf44c822007-11-25 14:01:38 +0000119
bart62a784c2009-02-15 13:11:14 +0000120void DRD_(thread_trace_context_switches)(const Bool t);
121void DRD_(thread_trace_conflict_set)(const Bool t);
bart8f822af2009-06-08 18:20:42 +0000122void DRD_(thread_trace_conflict_set_bm)(const Bool t);
bart09dc13f2009-02-14 15:13:31 +0000123Bool DRD_(thread_get_trace_fork_join)(void);
124void DRD_(thread_set_trace_fork_join)(const Bool t);
bart62a784c2009-02-15 13:11:14 +0000125void DRD_(thread_set_segment_merging)(const Bool m);
bart8f822af2009-06-08 18:20:42 +0000126int DRD_(thread_get_segment_merge_interval)(void);
127void DRD_(thread_set_segment_merge_interval)(const int i);
sewardjaf44c822007-11-25 14:01:38 +0000128
bart62a784c2009-02-15 13:11:14 +0000129DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid);
130DrdThreadId DRD_(NewVgThreadIdToDrdThreadId)(const ThreadId tid);
131DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid);
132ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid);
133DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
bartbedfd232009-03-26 19:07:15 +0000134 const ThreadId vg_created);
bart62a784c2009-02-15 13:11:14 +0000135DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created);
bart09dc13f2009-02-14 15:13:31 +0000136void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee);
bart9194e932011-02-09 11:55:12 +0000137void DRD_(thread_delete)(const DrdThreadId tid, Bool detached);
bart62a784c2009-02-15 13:11:14 +0000138void DRD_(thread_finished)(const DrdThreadId tid);
bart5c7e6b62011-02-03 17:47:50 +0000139void DRD_(drd_thread_atfork_child)(const DrdThreadId tid);
bart62a784c2009-02-15 13:11:14 +0000140void DRD_(thread_pre_cancel)(const DrdThreadId tid);
bartbedfd232009-03-26 19:07:15 +0000141void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
142 const Addr stack_startup);
bart62a784c2009-02-15 13:11:14 +0000143Addr DRD_(thread_get_stack_min)(const DrdThreadId tid);
144Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid);
145Addr DRD_(thread_get_stack_max)(const DrdThreadId tid);
146SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid);
bart383d6132010-09-02 14:43:18 +0000147Bool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid);
148void DRD_(thread_set_on_alt_stack)(const DrdThreadId tid,
149 const Bool on_alt_stack);
150Int DRD_(thread_get_threads_on_alt_stack)(void);
bart62a784c2009-02-15 13:11:14 +0000151void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid);
152Bool DRD_(thread_get_joinable)(const DrdThreadId tid);
153void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable);
bartdd75cdf2009-07-24 08:20:10 +0000154void DRD_(thread_entering_pthread_create)(const DrdThreadId tid);
155void DRD_(thread_left_pthread_create)(const DrdThreadId tid);
bartd45d9952009-05-31 18:53:54 +0000156const char* DRD_(thread_get_name)(const DrdThreadId tid);
157void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name);
bart62a784c2009-02-15 13:11:14 +0000158void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid);
159void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
bartbedfd232009-03-26 19:07:15 +0000160 const DrdThreadId drd_tid);
bart62a784c2009-02-15 13:11:14 +0000161int DRD_(thread_enter_synchr)(const DrdThreadId tid);
162int DRD_(thread_leave_synchr)(const DrdThreadId tid);
163int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid);
164void DRD_(thread_new_segment)(const DrdThreadId tid);
165VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid);
166void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid);
bart8f822af2009-06-08 18:20:42 +0000167void DRD_(thread_combine_vc_join)(const DrdThreadId joiner,
168 const DrdThreadId joinee);
bartf6ec1fe2009-06-21 18:07:35 +0000169void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid,
170 const Segment* sg);
bart8f822af2009-06-08 18:20:42 +0000171void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
172 const VectorClock* const old_vc);
bartdfbae6e2008-06-05 08:29:53 +0000173
bart23ef19d2011-03-12 12:34:44 +0000174void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2);
bartd45d9952009-05-31 18:53:54 +0000175void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled);
176void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled);
bart62a784c2009-02-15 13:11:14 +0000177void DRD_(thread_print_all)(void);
178void DRD_(thread_report_races)(const DrdThreadId tid);
179void DRD_(thread_report_races_segment)(const DrdThreadId tid,
bartbedfd232009-03-26 19:07:15 +0000180 const Segment* const p);
bart62a784c2009-02-15 13:11:14 +0000181void DRD_(thread_report_all_races)(void);
182void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
bartbedfd232009-03-26 19:07:15 +0000183 const Addr addr,
184 const SizeT size,
185 const BmAccessTypeT access_type);
bart62a784c2009-02-15 13:11:14 +0000186ULong DRD_(thread_get_context_switch_count)(void);
187ULong DRD_(thread_get_report_races_count)(void);
188ULong DRD_(thread_get_discard_ordered_segments_count)(void);
bart54803fe2009-06-21 09:26:27 +0000189ULong DRD_(thread_get_compute_conflict_set_count)(void);
190ULong DRD_(thread_get_update_conflict_set_count)(void);
barte5214662009-06-21 11:51:23 +0000191ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void);
192ULong DRD_(thread_get_update_conflict_set_sync_count)(void);
193ULong DRD_(thread_get_update_conflict_set_join_count)(void);
bart62a784c2009-02-15 13:11:14 +0000194ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void);
195ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void);
sewardjaf44c822007-11-25 14:01:38 +0000196
197
bart324a23b2009-02-15 12:14:52 +0000198/* Inline function definitions. */
199
bart86a87df2009-03-04 19:26:47 +0000200/**
201 * Whether or not the specified DRD thread ID is valid.
202 *
203 * A DRD thread ID is valid if and only if the following conditions are met:
204 * - The ID is a valid index of the DRD_(g_threadinfo)[] array.
205 * - The ID is not equal to DRD_INVALID_THREADID.
206 * - The ID refers either to a thread known by the Valgrind core, a joinable
207 * thread that has not yet been joined or a detached thread.
208 */
bartbf80e122008-06-06 10:18:24 +0000209static __inline__
bart62a784c2009-02-15 13:11:14 +0000210Bool DRD_(IsValidDrdThreadId)(const DrdThreadId tid)
bartbf80e122008-06-06 10:18:24 +0000211{
bartbedfd232009-03-26 19:07:15 +0000212 return (0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID
213 && ! (DRD_(g_threadinfo)[tid].vg_thread_exists == False
214 && DRD_(g_threadinfo)[tid].posix_thread_exists == False
215 && DRD_(g_threadinfo)[tid].detached_posix_thread == False));
bartbf80e122008-06-06 10:18:24 +0000216}
217
bart86a87df2009-03-04 19:26:47 +0000218/** Returns the DRD thread ID of the currently running thread. */
bart08865622008-06-06 14:31:36 +0000219static __inline__
bart62a784c2009-02-15 13:11:14 +0000220DrdThreadId DRD_(thread_get_running_tid)(void)
bart1ea5fff2008-03-16 08:36:23 +0000221{
bartdd75cdf2009-07-24 08:20:10 +0000222#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000223 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
bartdd75cdf2009-07-24 08:20:10 +0000224#endif
bartbedfd232009-03-26 19:07:15 +0000225 return DRD_(g_drd_running_tid);
bart1ea5fff2008-03-16 08:36:23 +0000226}
227
bart86a87df2009-03-04 19:26:47 +0000228/** Returns a pointer to the conflict set for the currently running thread. */
bart08865622008-06-06 14:31:36 +0000229static __inline__
bart62a784c2009-02-15 13:11:14 +0000230struct bitmap* DRD_(thread_get_conflict_set)(void)
bart1a473c72008-03-13 19:03:38 +0000231{
bartbedfd232009-03-26 19:07:15 +0000232 return DRD_(g_conflict_set);
bart1a473c72008-03-13 19:03:38 +0000233}
234
bart86a87df2009-03-04 19:26:47 +0000235/**
bartdd75cdf2009-07-24 08:20:10 +0000236 * Reports whether or not the currently running client thread is executing code
237 * inside the pthread_create() function.
238 */
239static __inline__
240Bool DRD_(running_thread_inside_pthread_create)(void)
241{
242 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)]
243 .pthread_create_nesting_level > 0);
244}
245
246/**
bart31b983d2010-02-21 14:52:59 +0000247 * Reports whether or not recording of memory loads is enabled for the
bartdd75cdf2009-07-24 08:20:10 +0000248 * currently running client thread.
bart86a87df2009-03-04 19:26:47 +0000249 */
bart08865622008-06-06 14:31:36 +0000250static __inline__
bartd45d9952009-05-31 18:53:54 +0000251Bool DRD_(running_thread_is_recording_loads)(void)
bartf00a85b2008-03-13 18:49:23 +0000252{
bart8b4b2ee2008-06-11 13:17:56 +0000253#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000254 tl_assert(0 <= (int)DRD_(g_drd_running_tid)
255 && DRD_(g_drd_running_tid) < DRD_N_THREADS
256 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
bart589f9482008-06-09 15:08:22 +0000257#endif
bartbedfd232009-03-26 19:07:15 +0000258 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0
bartd45d9952009-05-31 18:53:54 +0000259 && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_loads);
260}
261
bartdd75cdf2009-07-24 08:20:10 +0000262/**
bart31b983d2010-02-21 14:52:59 +0000263 * Reports whether or not recording memory stores is enabled for the
bartdd75cdf2009-07-24 08:20:10 +0000264 * currently running client thread.
265 */
bartd45d9952009-05-31 18:53:54 +0000266static __inline__
267Bool DRD_(running_thread_is_recording_stores)(void)
268{
269#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
270 tl_assert(0 <= (int)DRD_(g_drd_running_tid)
271 && DRD_(g_drd_running_tid) < DRD_N_THREADS
272 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
273#endif
274 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0
275 && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_stores);
bart1a473c72008-03-13 19:03:38 +0000276}
277
bart86a87df2009-03-04 19:26:47 +0000278/**
279 * Update the information about the lowest stack address that has ever been
280 * accessed by a thread.
281 */
bart08865622008-06-06 14:31:36 +0000282static __inline__
bart62a784c2009-02-15 13:11:14 +0000283void DRD_(thread_set_stack_min)(const DrdThreadId tid, const Addr stack_min)
bart1ea5fff2008-03-16 08:36:23 +0000284{
bart8b4b2ee2008-06-11 13:17:56 +0000285#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000286 tl_assert(0 <= (int)tid
287 && tid < DRD_N_THREADS
288 && tid != DRD_INVALID_THREADID);
bart1ea5fff2008-03-16 08:36:23 +0000289#endif
bartbedfd232009-03-26 19:07:15 +0000290 DRD_(g_threadinfo)[tid].stack_min = stack_min;
bart8b4b2ee2008-06-11 13:17:56 +0000291#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000292 /* This function can be called after the thread has been created but */
293 /* before drd_post_thread_create() has filled in stack_max. */
294 tl_assert(DRD_(g_threadinfo)[tid].stack_min
295 < DRD_(g_threadinfo)[tid].stack_max
296 || DRD_(g_threadinfo)[tid].stack_max == 0);
bart1ea5fff2008-03-16 08:36:23 +0000297#endif
bartbedfd232009-03-26 19:07:15 +0000298 if (UNLIKELY(stack_min < DRD_(g_threadinfo)[tid].stack_min_min))
299 {
300 DRD_(g_threadinfo)[tid].stack_min_min = stack_min;
301 }
bart1ea5fff2008-03-16 08:36:23 +0000302}
303
bart324a23b2009-02-15 12:14:52 +0000304/**
305 * Return true if and only if the specified address is on the stack of the
306 * currently scheduled thread.
bart08865622008-06-06 14:31:36 +0000307 */
308static __inline__
bart62a784c2009-02-15 13:11:14 +0000309Bool DRD_(thread_address_on_stack)(const Addr a)
bart08865622008-06-06 14:31:36 +0000310{
bartbedfd232009-03-26 19:07:15 +0000311 return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_min <= a
312 && a < DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_max);
bart08865622008-06-06 14:31:36 +0000313}
314
bart0d6d5c52009-03-10 09:22:13 +0000315/**
316 * Return true if and only if the specified address is on the stack of any
317 * thread.
318 */
319static __inline__
320Bool DRD_(thread_address_on_any_stack)(const Addr a)
321{
bartbedfd232009-03-26 19:07:15 +0000322 int i;
bart0d6d5c52009-03-10 09:22:13 +0000323
bartbedfd232009-03-26 19:07:15 +0000324 for (i = 1; i < DRD_N_THREADS; i++)
325 {
326 if (DRD_(g_threadinfo)[i].vg_thread_exists
327 && DRD_(g_threadinfo)[i].stack_min <= a
328 && a < DRD_(g_threadinfo)[i].stack_max)
329 {
330 return True;
331 }
332 }
333 return False;
bart0d6d5c52009-03-10 09:22:13 +0000334}
335
bart1a473c72008-03-13 19:03:38 +0000336/** Return a pointer to the latest segment for the specified thread. */
bart08865622008-06-06 14:31:36 +0000337static __inline__
bart62a784c2009-02-15 13:11:14 +0000338Segment* DRD_(thread_get_segment)(const DrdThreadId tid)
bart1a473c72008-03-13 19:03:38 +0000339{
bart8b4b2ee2008-06-11 13:17:56 +0000340#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartbedfd232009-03-26 19:07:15 +0000341 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
342 && tid != DRD_INVALID_THREADID);
343 tl_assert(DRD_(g_threadinfo)[tid].last);
bart589f9482008-06-09 15:08:22 +0000344#endif
bartbedfd232009-03-26 19:07:15 +0000345 return DRD_(g_threadinfo)[tid].last;
bartf00a85b2008-03-13 18:49:23 +0000346}
347
barta79df6e2008-03-14 17:07:51 +0000348/** Return a pointer to the latest segment for the running thread. */
bart08865622008-06-06 14:31:36 +0000349static __inline__
bart62a784c2009-02-15 13:11:14 +0000350Segment* DRD_(running_thread_get_segment)(void)
barta79df6e2008-03-14 17:07:51 +0000351{
bartbedfd232009-03-26 19:07:15 +0000352 return DRD_(thread_get_segment)(DRD_(g_drd_running_tid));
barta79df6e2008-03-14 17:07:51 +0000353}
bartf00a85b2008-03-13 18:49:23 +0000354
bart86a87df2009-03-04 19:26:47 +0000355#endif /* __THREAD_H */