blob: 895502f8332b0e81f321e36a76dab572d7f6ecf8 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 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
bartf00a85b2008-03-13 18:49:23 +000030// Includes.
sewardjaf44c822007-11-25 14:01:38 +000031
bartf00a85b2008-03-13 18:49:23 +000032#include "drd_segment.h"
bartd59bb0f2008-06-08 08:08:31 +000033#include "pub_drd_bitmap.h"
bartf00a85b2008-03-13 18:49:23 +000034#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
sewardjaf44c822007-11-25 14:01:38 +000042
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
bartf00a85b2008-03-13 18:49:23 +000053// Type definitions.
54
sewardjaf44c822007-11-25 14:01:38 +000055typedef UInt DrdThreadId;
56typedef UWord PThreadId;
57
bartf00a85b2008-03-13 18:49:23 +000058typedef struct
59{
bart1a473c72008-03-13 19:03:38 +000060 Segment* first;
61 Segment* last;
62 ThreadId vg_threadid;
63 PThreadId pt_threadid;
bartcac53462008-03-29 09:27:08 +000064 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. */
bart1a473c72008-03-13 19:03:38 +000069 /// 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;
bartf00a85b2008-03-13 18:49:23 +000080} 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
86extern DrdThreadId s_drd_running_tid;
87extern ThreadInfo s_threadinfo[DRD_N_THREADS];
barte73b0aa2008-06-28 07:19:56 +000088extern struct bitmap* s_conflict_set;
bartf00a85b2008-03-13 18:49:23 +000089
90
91// Function declarations.
sewardjaf44c822007-11-25 14:01:38 +000092
bart26f73e12008-02-24 18:37:08 +000093void thread_trace_context_switches(const Bool t);
barte73b0aa2008-06-28 07:19:56 +000094void thread_trace_conflict_set(const Bool t);
barta9c37392008-03-22 09:38:48 +000095void thread_set_segment_merging(const Bool m);
sewardjaf44c822007-11-25 14:01:38 +000096
97DrdThreadId VgThreadIdToDrdThreadId(const ThreadId tid);
98DrdThreadId NewVgThreadIdToDrdThreadId(const ThreadId tid);
99DrdThreadId PtThreadIdToDrdThreadId(const PThreadId tid);
100ThreadId DrdThreadIdToVgThreadId(const DrdThreadId tid);
101DrdThreadId thread_pre_create(const DrdThreadId creator,
102 const ThreadId vg_created);
103DrdThreadId thread_post_create(const ThreadId vg_created);
104void thread_delete(const DrdThreadId tid);
105void thread_finished(const DrdThreadId tid);
bartaf0691b2008-09-27 12:26:50 +0000106void thread_pre_cancel(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000107void thread_set_stack_startup(const DrdThreadId tid, const Addr stack_startup);
108Addr thread_get_stack_min(const DrdThreadId tid);
bartcac53462008-03-29 09:27:08 +0000109Addr thread_get_stack_min_min(const DrdThreadId tid);
bartd43f8d32008-03-16 17:29:20 +0000110Addr thread_get_stack_max(const DrdThreadId tid);
bartcac53462008-03-29 09:27:08 +0000111SizeT thread_get_stack_size(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000112void thread_set_pthreadid(const DrdThreadId tid, const PThreadId ptid);
113Bool thread_get_joinable(const DrdThreadId tid);
114void thread_set_joinable(const DrdThreadId tid, const Bool joinable);
sewardj8b09d4f2007-12-04 21:27:18 +0000115void thread_set_vg_running_tid(const ThreadId vg_tid);
116void thread_set_running_tid(const ThreadId vg_tid,
117 const DrdThreadId drd_tid);
bart0268dfa2008-03-11 20:10:21 +0000118int thread_enter_synchr(const DrdThreadId tid);
119int thread_leave_synchr(const DrdThreadId tid);
120int thread_get_synchr_nesting_count(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000121void thread_new_segment(const DrdThreadId tid);
122VectorClock* thread_get_vc(const DrdThreadId tid);
barta2b6e1b2008-03-17 18:32:39 +0000123void thread_get_latest_segment(Segment** sg, const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000124void thread_combine_vc(const DrdThreadId joiner, const DrdThreadId joinee);
125void thread_combine_vc2(const DrdThreadId tid, const VectorClock* const vc);
bartdfbae6e2008-06-05 08:29:53 +0000126
sewardjaf44c822007-11-25 14:01:38 +0000127void thread_stop_using_mem(const Addr a1, const Addr a2);
bart0268dfa2008-03-11 20:10:21 +0000128void thread_start_recording(const DrdThreadId tid);
129void thread_stop_recording(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000130void thread_print_all(void);
131void thread_report_races(const DrdThreadId tid);
132void thread_report_races_segment(const DrdThreadId tid,
133 const Segment* const p);
134void thread_report_all_races(void);
sewardjaf44c822007-11-25 14:01:38 +0000135void thread_report_conflicting_segments(const DrdThreadId tid,
136 const Addr addr,
137 const SizeT size,
138 const BmAccessTypeT access_type);
139ULong thread_get_context_switch_count(void);
140ULong thread_get_report_races_count(void);
141ULong thread_get_discard_ordered_segments_count(void);
barte73b0aa2008-06-28 07:19:56 +0000142ULong thread_get_update_conflict_set_count(ULong* dsnsc, ULong* dscvc);
143ULong thread_get_conflict_set_bitmap_creation_count(void);
144ULong thread_get_conflict_set_bitmap2_creation_count(void);
sewardjaf44c822007-11-25 14:01:38 +0000145
146
bartbf80e122008-06-06 10:18:24 +0000147static __inline__
148Bool 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
bart08865622008-06-06 14:31:36 +0000156static __inline__
bart1ea5fff2008-03-16 08:36:23 +0000157DrdThreadId thread_get_running_tid(void)
158{
159 tl_assert(s_drd_running_tid != DRD_INVALID_THREADID);
160 return s_drd_running_tid;
161}
162
bart08865622008-06-06 14:31:36 +0000163static __inline__
barte73b0aa2008-06-28 07:19:56 +0000164struct bitmap* thread_get_conflict_set(void)
bart1a473c72008-03-13 19:03:38 +0000165{
barte73b0aa2008-06-28 07:19:56 +0000166 return s_conflict_set;
bart1a473c72008-03-13 19:03:38 +0000167}
168
bart08865622008-06-06 14:31:36 +0000169static __inline__
bartf00a85b2008-03-13 18:49:23 +0000170Bool running_thread_is_recording(void)
171{
bart8b4b2ee2008-06-11 13:17:56 +0000172#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart74a5f212008-05-11 06:43:07 +0000173 tl_assert(0 <= (int)s_drd_running_tid && s_drd_running_tid < DRD_N_THREADS
bart1a473c72008-03-13 19:03:38 +0000174 && s_drd_running_tid != DRD_INVALID_THREADID);
bart589f9482008-06-09 15:08:22 +0000175#endif
bart1a473c72008-03-13 19:03:38 +0000176 return (s_threadinfo[s_drd_running_tid].synchr_nesting == 0
177 && s_threadinfo[s_drd_running_tid].is_recording);
178}
179
bart08865622008-06-06 14:31:36 +0000180static __inline__
bart1ea5fff2008-03-16 08:36:23 +0000181void thread_set_stack_min(const DrdThreadId tid, const Addr stack_min)
182{
bart8b4b2ee2008-06-11 13:17:56 +0000183#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartf0cdc602008-06-11 18:37:59 +0000184 tl_assert(0 <= (int)tid
185 && tid < DRD_N_THREADS
186 && tid != DRD_INVALID_THREADID);
bart1ea5fff2008-03-16 08:36:23 +0000187#endif
barte773de42008-03-29 12:54:01 +0000188 s_threadinfo[tid].stack_min = stack_min;
bart8b4b2ee2008-06-11 13:17:56 +0000189#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartfda64922008-03-29 13:13:33 +0000190 /* 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);
bart1ea5fff2008-03-16 08:36:23 +0000194#endif
barte773de42008-03-29 12:54:01 +0000195 if (UNLIKELY(stack_min < s_threadinfo[tid].stack_min_min))
196 {
197 s_threadinfo[tid].stack_min_min = stack_min;
bart1ea5fff2008-03-16 08:36:23 +0000198 }
199}
200
bart08865622008-06-06 14:31:36 +0000201/** Return true if and only if the specified address is on the stack of the
202 * currently scheduled thread.
203 */
204static __inline__
205Bool 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
bart1a473c72008-03-13 19:03:38 +0000211/** Return a pointer to the latest segment for the specified thread. */
bart08865622008-06-06 14:31:36 +0000212static __inline__
bart1a473c72008-03-13 19:03:38 +0000213Segment* thread_get_segment(const DrdThreadId tid)
214{
bart8b4b2ee2008-06-11 13:17:56 +0000215#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart74a5f212008-05-11 06:43:07 +0000216 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart1a473c72008-03-13 19:03:38 +0000217 && tid != DRD_INVALID_THREADID);
218 tl_assert(s_threadinfo[tid].last);
bart589f9482008-06-09 15:08:22 +0000219#endif
bart1a473c72008-03-13 19:03:38 +0000220 return s_threadinfo[tid].last;
bartf00a85b2008-03-13 18:49:23 +0000221}
222
barta79df6e2008-03-14 17:07:51 +0000223/** Return a pointer to the latest segment for the running thread. */
bart08865622008-06-06 14:31:36 +0000224static __inline__
barta79df6e2008-03-14 17:07:51 +0000225Segment* running_thread_get_segment(void)
226{
227 return thread_get_segment(s_drd_running_tid);
228}
bartf00a85b2008-03-13 18:49:23 +0000229
sewardjaf44c822007-11-25 14:01:38 +0000230#endif // __THREAD_H