blob: 525c9452eef888c0e723bad3ffcf928976daa594 [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
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"
bartf00a85b2008-03-13 18:49:23 +000035#include "pub_tool_libcassert.h" // tl_assert()
36#include "pub_tool_stacktrace.h" // StackTrace
37#include "pub_tool_threadstate.h" // VG_N_THREADS
38
39
40// Defines.
41
42#define DRD_N_THREADS VG_N_THREADS
sewardjaf44c822007-11-25 14:01:38 +000043
44#define DRD_INVALID_THREADID 0
45
46/* Note: the PThreadId typedef and the INVALID_POSIX_THREADID depend on the */
47/* operating system and threading library in use. PThreadId must contain at */
48/* least the same number of bits as pthread_t, and INVALID_POSIX_THREADID */
49/* must be a value that will never be returned by pthread_self(). */
50
51#define INVALID_POSIX_THREADID ((PThreadId)0)
52
53
bartf00a85b2008-03-13 18:49:23 +000054// Type definitions.
55
sewardjaf44c822007-11-25 14:01:38 +000056typedef UInt DrdThreadId;
57typedef UWord PThreadId;
58
bartf00a85b2008-03-13 18:49:23 +000059typedef struct
60{
bart1a473c72008-03-13 19:03:38 +000061 Segment* first;
62 Segment* last;
63 ThreadId vg_threadid;
64 PThreadId pt_threadid;
bartcac53462008-03-29 09:27:08 +000065 Addr stack_min_min; /** Lowest value stack pointer ever had. */
66 Addr stack_min; /** Current stack pointer. */
67 Addr stack_startup; /** Stack pointer after pthread_create() finished.*/
68 Addr stack_max; /** Top of stack. */
69 SizeT stack_size; /** Maximum size of stack. */
bart1a473c72008-03-13 19:03:38 +000070 /// Indicates whether the Valgrind core knows about this thread.
71 Bool vg_thread_exists;
72 /// Indicates whether there is an associated POSIX thread ID.
73 Bool posix_thread_exists;
74 /// If true, indicates that there is a corresponding POSIX thread ID and
75 /// a corresponding OS thread that is detached.
76 Bool detached_posix_thread;
77 /// Wether recording of memory accesses is active.
78 Bool is_recording;
79 /// Nesting level of synchronization functions called by the client.
80 Int synchr_nesting;
bartf00a85b2008-03-13 18:49:23 +000081} ThreadInfo;
82
83
84// Local variables of drd_thread.c that are declared here such that these
85// can be accessed by inline functions.
86
87extern DrdThreadId s_drd_running_tid;
88extern ThreadInfo s_threadinfo[DRD_N_THREADS];
barte73b0aa2008-06-28 07:19:56 +000089extern struct bitmap* s_conflict_set;
bartf00a85b2008-03-13 18:49:23 +000090
91
92// Function declarations.
sewardjaf44c822007-11-25 14:01:38 +000093
bart26f73e12008-02-24 18:37:08 +000094void thread_trace_context_switches(const Bool t);
barte73b0aa2008-06-28 07:19:56 +000095void thread_trace_conflict_set(const Bool t);
bart09dc13f2009-02-14 15:13:31 +000096Bool DRD_(thread_get_trace_fork_join)(void);
97void DRD_(thread_set_trace_fork_join)(const Bool t);
barta9c37392008-03-22 09:38:48 +000098void thread_set_segment_merging(const Bool m);
sewardjaf44c822007-11-25 14:01:38 +000099
100DrdThreadId VgThreadIdToDrdThreadId(const ThreadId tid);
101DrdThreadId NewVgThreadIdToDrdThreadId(const ThreadId tid);
102DrdThreadId PtThreadIdToDrdThreadId(const PThreadId tid);
103ThreadId DrdThreadIdToVgThreadId(const DrdThreadId tid);
104DrdThreadId thread_pre_create(const DrdThreadId creator,
105 const ThreadId vg_created);
106DrdThreadId thread_post_create(const ThreadId vg_created);
bart09dc13f2009-02-14 15:13:31 +0000107void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee);
sewardjaf44c822007-11-25 14:01:38 +0000108void thread_delete(const DrdThreadId tid);
109void thread_finished(const DrdThreadId tid);
bartaf0691b2008-09-27 12:26:50 +0000110void thread_pre_cancel(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000111void thread_set_stack_startup(const DrdThreadId tid, const Addr stack_startup);
112Addr thread_get_stack_min(const DrdThreadId tid);
bartcac53462008-03-29 09:27:08 +0000113Addr thread_get_stack_min_min(const DrdThreadId tid);
bartd43f8d32008-03-16 17:29:20 +0000114Addr thread_get_stack_max(const DrdThreadId tid);
bartcac53462008-03-29 09:27:08 +0000115SizeT thread_get_stack_size(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000116void thread_set_pthreadid(const DrdThreadId tid, const PThreadId ptid);
117Bool thread_get_joinable(const DrdThreadId tid);
118void thread_set_joinable(const DrdThreadId tid, const Bool joinable);
sewardj8b09d4f2007-12-04 21:27:18 +0000119void thread_set_vg_running_tid(const ThreadId vg_tid);
120void thread_set_running_tid(const ThreadId vg_tid,
121 const DrdThreadId drd_tid);
bart0268dfa2008-03-11 20:10:21 +0000122int thread_enter_synchr(const DrdThreadId tid);
123int thread_leave_synchr(const DrdThreadId tid);
124int thread_get_synchr_nesting_count(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000125void thread_new_segment(const DrdThreadId tid);
126VectorClock* thread_get_vc(const DrdThreadId tid);
barta2b6e1b2008-03-17 18:32:39 +0000127void thread_get_latest_segment(Segment** sg, const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000128void thread_combine_vc(const DrdThreadId joiner, const DrdThreadId joinee);
129void thread_combine_vc2(const DrdThreadId tid, const VectorClock* const vc);
bartdfbae6e2008-06-05 08:29:53 +0000130
sewardjaf44c822007-11-25 14:01:38 +0000131void thread_stop_using_mem(const Addr a1, const Addr a2);
bart0268dfa2008-03-11 20:10:21 +0000132void thread_start_recording(const DrdThreadId tid);
133void thread_stop_recording(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000134void thread_print_all(void);
135void thread_report_races(const DrdThreadId tid);
136void thread_report_races_segment(const DrdThreadId tid,
137 const Segment* const p);
138void thread_report_all_races(void);
sewardjaf44c822007-11-25 14:01:38 +0000139void thread_report_conflicting_segments(const DrdThreadId tid,
140 const Addr addr,
141 const SizeT size,
142 const BmAccessTypeT access_type);
143ULong thread_get_context_switch_count(void);
144ULong thread_get_report_races_count(void);
145ULong thread_get_discard_ordered_segments_count(void);
barte73b0aa2008-06-28 07:19:56 +0000146ULong thread_get_update_conflict_set_count(ULong* dsnsc, ULong* dscvc);
147ULong thread_get_conflict_set_bitmap_creation_count(void);
148ULong thread_get_conflict_set_bitmap2_creation_count(void);
sewardjaf44c822007-11-25 14:01:38 +0000149
150
bartbf80e122008-06-06 10:18:24 +0000151static __inline__
152Bool IsValidDrdThreadId(const DrdThreadId tid)
153{
154 return (0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID
155 && ! (s_threadinfo[tid].vg_thread_exists == False
156 && s_threadinfo[tid].posix_thread_exists == False
157 && s_threadinfo[tid].detached_posix_thread == False));
158}
159
bart08865622008-06-06 14:31:36 +0000160static __inline__
bart1ea5fff2008-03-16 08:36:23 +0000161DrdThreadId thread_get_running_tid(void)
162{
163 tl_assert(s_drd_running_tid != DRD_INVALID_THREADID);
164 return s_drd_running_tid;
165}
166
bart08865622008-06-06 14:31:36 +0000167static __inline__
barte73b0aa2008-06-28 07:19:56 +0000168struct bitmap* thread_get_conflict_set(void)
bart1a473c72008-03-13 19:03:38 +0000169{
barte73b0aa2008-06-28 07:19:56 +0000170 return s_conflict_set;
bart1a473c72008-03-13 19:03:38 +0000171}
172
bart08865622008-06-06 14:31:36 +0000173static __inline__
bartf00a85b2008-03-13 18:49:23 +0000174Bool running_thread_is_recording(void)
175{
bart8b4b2ee2008-06-11 13:17:56 +0000176#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart74a5f212008-05-11 06:43:07 +0000177 tl_assert(0 <= (int)s_drd_running_tid && s_drd_running_tid < DRD_N_THREADS
bart1a473c72008-03-13 19:03:38 +0000178 && s_drd_running_tid != DRD_INVALID_THREADID);
bart589f9482008-06-09 15:08:22 +0000179#endif
bart1a473c72008-03-13 19:03:38 +0000180 return (s_threadinfo[s_drd_running_tid].synchr_nesting == 0
181 && s_threadinfo[s_drd_running_tid].is_recording);
182}
183
bart08865622008-06-06 14:31:36 +0000184static __inline__
bart1ea5fff2008-03-16 08:36:23 +0000185void thread_set_stack_min(const DrdThreadId tid, const Addr stack_min)
186{
bart8b4b2ee2008-06-11 13:17:56 +0000187#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartf0cdc602008-06-11 18:37:59 +0000188 tl_assert(0 <= (int)tid
189 && tid < DRD_N_THREADS
190 && tid != DRD_INVALID_THREADID);
bart1ea5fff2008-03-16 08:36:23 +0000191#endif
barte773de42008-03-29 12:54:01 +0000192 s_threadinfo[tid].stack_min = stack_min;
bart8b4b2ee2008-06-11 13:17:56 +0000193#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bartfda64922008-03-29 13:13:33 +0000194 /* This function can be called after the thread has been created but */
195 /* before drd_post_thread_create() has filled in stack_max. */
196 tl_assert(s_threadinfo[tid].stack_min < s_threadinfo[tid].stack_max
197 || s_threadinfo[tid].stack_max == 0);
bart1ea5fff2008-03-16 08:36:23 +0000198#endif
barte773de42008-03-29 12:54:01 +0000199 if (UNLIKELY(stack_min < s_threadinfo[tid].stack_min_min))
200 {
201 s_threadinfo[tid].stack_min_min = stack_min;
bart1ea5fff2008-03-16 08:36:23 +0000202 }
203}
204
bart08865622008-06-06 14:31:36 +0000205/** Return true if and only if the specified address is on the stack of the
206 * currently scheduled thread.
207 */
208static __inline__
209Bool thread_address_on_stack(const Addr a)
210{
211 return (s_threadinfo[s_drd_running_tid].stack_min <= a
212 && a < s_threadinfo[s_drd_running_tid].stack_max);
213}
214
bart1a473c72008-03-13 19:03:38 +0000215/** Return a pointer to the latest segment for the specified thread. */
bart08865622008-06-06 14:31:36 +0000216static __inline__
bart1a473c72008-03-13 19:03:38 +0000217Segment* thread_get_segment(const DrdThreadId tid)
218{
bart8b4b2ee2008-06-11 13:17:56 +0000219#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart74a5f212008-05-11 06:43:07 +0000220 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart1a473c72008-03-13 19:03:38 +0000221 && tid != DRD_INVALID_THREADID);
222 tl_assert(s_threadinfo[tid].last);
bart589f9482008-06-09 15:08:22 +0000223#endif
bart1a473c72008-03-13 19:03:38 +0000224 return s_threadinfo[tid].last;
bartf00a85b2008-03-13 18:49:23 +0000225}
226
barta79df6e2008-03-14 17:07:51 +0000227/** Return a pointer to the latest segment for the running thread. */
bart08865622008-06-06 14:31:36 +0000228static __inline__
barta79df6e2008-03-14 17:07:51 +0000229Segment* running_thread_get_segment(void)
230{
231 return thread_get_segment(s_drd_running_tid);
232}
bartf00a85b2008-03-13 18:49:23 +0000233
sewardjaf44c822007-11-25 14:01:38 +0000234#endif // __THREAD_H