blob: d122978d42a6cea8b04c0f412c3fa24df0009f22 [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];
bart1a473c72008-03-13 19:03:38 +000088extern struct bitmap* s_danger_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);
94void thread_trace_danger_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);
106void thread_set_stack_startup(const DrdThreadId tid, const Addr stack_startup);
107Addr thread_get_stack_min(const DrdThreadId tid);
bartcac53462008-03-29 09:27:08 +0000108Addr thread_get_stack_min_min(const DrdThreadId tid);
bartd43f8d32008-03-16 17:29:20 +0000109Addr thread_get_stack_max(const DrdThreadId tid);
bartcac53462008-03-29 09:27:08 +0000110SizeT thread_get_stack_size(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000111void thread_set_pthreadid(const DrdThreadId tid, const PThreadId ptid);
112Bool thread_get_joinable(const DrdThreadId tid);
113void thread_set_joinable(const DrdThreadId tid, const Bool joinable);
sewardj8b09d4f2007-12-04 21:27:18 +0000114void thread_set_vg_running_tid(const ThreadId vg_tid);
115void thread_set_running_tid(const ThreadId vg_tid,
116 const DrdThreadId drd_tid);
bart0268dfa2008-03-11 20:10:21 +0000117int thread_enter_synchr(const DrdThreadId tid);
118int thread_leave_synchr(const DrdThreadId tid);
119int thread_get_synchr_nesting_count(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000120void thread_new_segment(const DrdThreadId tid);
121VectorClock* thread_get_vc(const DrdThreadId tid);
barta2b6e1b2008-03-17 18:32:39 +0000122void thread_get_latest_segment(Segment** sg, const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000123void thread_combine_vc(const DrdThreadId joiner, const DrdThreadId joinee);
124void thread_combine_vc2(const DrdThreadId tid, const VectorClock* const vc);
bartdfbae6e2008-06-05 08:29:53 +0000125
sewardjaf44c822007-11-25 14:01:38 +0000126void thread_stop_using_mem(const Addr a1, const Addr a2);
bart0268dfa2008-03-11 20:10:21 +0000127void thread_start_recording(const DrdThreadId tid);
128void thread_stop_recording(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000129void thread_print_all(void);
130void thread_report_races(const DrdThreadId tid);
131void thread_report_races_segment(const DrdThreadId tid,
132 const Segment* const p);
133void thread_report_all_races(void);
sewardjaf44c822007-11-25 14:01:38 +0000134void thread_report_conflicting_segments(const DrdThreadId tid,
135 const Addr addr,
136 const SizeT size,
137 const BmAccessTypeT access_type);
138ULong thread_get_context_switch_count(void);
139ULong thread_get_report_races_count(void);
140ULong thread_get_discard_ordered_segments_count(void);
bartd66e3a82008-04-06 15:02:17 +0000141ULong thread_get_update_danger_set_count(ULong* dsnsc, ULong* dscvc);
sewardjaf44c822007-11-25 14:01:38 +0000142ULong thread_get_danger_set_bitmap_creation_count(void);
143ULong thread_get_danger_set_bitmap2_creation_count(void);
144
145
bartbf80e122008-06-06 10:18:24 +0000146static __inline__
147Bool IsValidDrdThreadId(const DrdThreadId tid)
148{
149 return (0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID
150 && ! (s_threadinfo[tid].vg_thread_exists == False
151 && s_threadinfo[tid].posix_thread_exists == False
152 && s_threadinfo[tid].detached_posix_thread == False));
153}
154
bart08865622008-06-06 14:31:36 +0000155static __inline__
bart1ea5fff2008-03-16 08:36:23 +0000156DrdThreadId thread_get_running_tid(void)
157{
158 tl_assert(s_drd_running_tid != DRD_INVALID_THREADID);
159 return s_drd_running_tid;
160}
161
bart08865622008-06-06 14:31:36 +0000162static __inline__
bart1ea5fff2008-03-16 08:36:23 +0000163struct bitmap* thread_get_danger_set(void)
bart1a473c72008-03-13 19:03:38 +0000164{
165 return s_danger_set;
166}
167
bart08865622008-06-06 14:31:36 +0000168static __inline__
bartf00a85b2008-03-13 18:49:23 +0000169Bool running_thread_is_recording(void)
170{
bart74a5f212008-05-11 06:43:07 +0000171 tl_assert(0 <= (int)s_drd_running_tid && s_drd_running_tid < DRD_N_THREADS
bart1a473c72008-03-13 19:03:38 +0000172 && s_drd_running_tid != DRD_INVALID_THREADID);
173 return (s_threadinfo[s_drd_running_tid].synchr_nesting == 0
174 && s_threadinfo[s_drd_running_tid].is_recording);
175}
176
bart08865622008-06-06 14:31:36 +0000177static __inline__
bart1ea5fff2008-03-16 08:36:23 +0000178void thread_set_stack_min(const DrdThreadId tid, const Addr stack_min)
179{
180#if 0
181 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
182#endif
barte773de42008-03-29 12:54:01 +0000183 s_threadinfo[tid].stack_min = stack_min;
bart1ea5fff2008-03-16 08:36:23 +0000184#if 0
bartfda64922008-03-29 13:13:33 +0000185 /* This function can be called after the thread has been created but */
186 /* before drd_post_thread_create() has filled in stack_max. */
187 tl_assert(s_threadinfo[tid].stack_min < s_threadinfo[tid].stack_max
188 || s_threadinfo[tid].stack_max == 0);
bart1ea5fff2008-03-16 08:36:23 +0000189#endif
barte773de42008-03-29 12:54:01 +0000190 if (UNLIKELY(stack_min < s_threadinfo[tid].stack_min_min))
191 {
192 s_threadinfo[tid].stack_min_min = stack_min;
bart1ea5fff2008-03-16 08:36:23 +0000193 }
194}
195
bart08865622008-06-06 14:31:36 +0000196/** Return true if and only if the specified address is on the stack of the
197 * currently scheduled thread.
198 */
199static __inline__
200Bool thread_address_on_stack(const Addr a)
201{
202 return (s_threadinfo[s_drd_running_tid].stack_min <= a
203 && a < s_threadinfo[s_drd_running_tid].stack_max);
204}
205
bart1a473c72008-03-13 19:03:38 +0000206/** Return a pointer to the latest segment for the specified thread. */
bart08865622008-06-06 14:31:36 +0000207static __inline__
bart1a473c72008-03-13 19:03:38 +0000208Segment* thread_get_segment(const DrdThreadId tid)
209{
bart74a5f212008-05-11 06:43:07 +0000210 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart1a473c72008-03-13 19:03:38 +0000211 && tid != DRD_INVALID_THREADID);
212 tl_assert(s_threadinfo[tid].last);
213 return s_threadinfo[tid].last;
bartf00a85b2008-03-13 18:49:23 +0000214}
215
barta79df6e2008-03-14 17:07:51 +0000216/** Return a pointer to the latest segment for the running thread. */
bart08865622008-06-06 14:31:36 +0000217static __inline__
barta79df6e2008-03-14 17:07:51 +0000218Segment* running_thread_get_segment(void)
219{
220 return thread_get_segment(s_drd_running_tid);
221}
bartf00a85b2008-03-13 18:49:23 +0000222
sewardjaf44c822007-11-25 14:01:38 +0000223#endif // __THREAD_H