blob: 299bed321064744fd2d9157a72df8ac03b54ac0e [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"
33#include "pub_tool_libcassert.h" // tl_assert()
34#include "pub_tool_stacktrace.h" // StackTrace
35#include "pub_tool_threadstate.h" // VG_N_THREADS
36
37
38// Defines.
39
40#define DRD_N_THREADS VG_N_THREADS
sewardjaf44c822007-11-25 14:01:38 +000041
42#define DRD_INVALID_THREADID 0
43
44/* Note: the PThreadId typedef and the INVALID_POSIX_THREADID depend on the */
45/* operating system and threading library in use. PThreadId must contain at */
46/* least the same number of bits as pthread_t, and INVALID_POSIX_THREADID */
47/* must be a value that will never be returned by pthread_self(). */
48
49#define INVALID_POSIX_THREADID ((PThreadId)0)
50
51
bartf00a85b2008-03-13 18:49:23 +000052// Type definitions.
53
sewardjaf44c822007-11-25 14:01:38 +000054typedef UInt DrdThreadId;
55typedef UWord PThreadId;
56
bartf00a85b2008-03-13 18:49:23 +000057typedef struct
58{
bart1a473c72008-03-13 19:03:38 +000059 Segment* first;
60 Segment* last;
61 ThreadId vg_threadid;
62 PThreadId pt_threadid;
bartcac53462008-03-29 09:27:08 +000063 Addr stack_min_min; /** Lowest value stack pointer ever had. */
64 Addr stack_min; /** Current stack pointer. */
65 Addr stack_startup; /** Stack pointer after pthread_create() finished.*/
66 Addr stack_max; /** Top of stack. */
67 SizeT stack_size; /** Maximum size of stack. */
bart1a473c72008-03-13 19:03:38 +000068 /// Indicates whether the Valgrind core knows about this thread.
69 Bool vg_thread_exists;
70 /// Indicates whether there is an associated POSIX thread ID.
71 Bool posix_thread_exists;
72 /// If true, indicates that there is a corresponding POSIX thread ID and
73 /// a corresponding OS thread that is detached.
74 Bool detached_posix_thread;
75 /// Wether recording of memory accesses is active.
76 Bool is_recording;
77 /// Nesting level of synchronization functions called by the client.
78 Int synchr_nesting;
bartf00a85b2008-03-13 18:49:23 +000079} ThreadInfo;
80
81
82// Local variables of drd_thread.c that are declared here such that these
83// can be accessed by inline functions.
84
85extern DrdThreadId s_drd_running_tid;
86extern ThreadInfo s_threadinfo[DRD_N_THREADS];
bart1a473c72008-03-13 19:03:38 +000087extern struct bitmap* s_danger_set;
bartf00a85b2008-03-13 18:49:23 +000088
89
90// Function declarations.
sewardjaf44c822007-11-25 14:01:38 +000091
bart26f73e12008-02-24 18:37:08 +000092void thread_trace_context_switches(const Bool t);
93void thread_trace_danger_set(const Bool t);
barta9c37392008-03-22 09:38:48 +000094void thread_set_segment_merging(const Bool m);
sewardjaf44c822007-11-25 14:01:38 +000095
96DrdThreadId VgThreadIdToDrdThreadId(const ThreadId tid);
97DrdThreadId NewVgThreadIdToDrdThreadId(const ThreadId tid);
98DrdThreadId PtThreadIdToDrdThreadId(const PThreadId tid);
99ThreadId DrdThreadIdToVgThreadId(const DrdThreadId tid);
100DrdThreadId thread_pre_create(const DrdThreadId creator,
101 const ThreadId vg_created);
102DrdThreadId thread_post_create(const ThreadId vg_created);
103void thread_delete(const DrdThreadId tid);
104void thread_finished(const DrdThreadId tid);
105void thread_set_stack_startup(const DrdThreadId tid, const Addr stack_startup);
106Addr thread_get_stack_min(const DrdThreadId tid);
bartcac53462008-03-29 09:27:08 +0000107Addr thread_get_stack_min_min(const DrdThreadId tid);
bartd43f8d32008-03-16 17:29:20 +0000108Addr thread_get_stack_max(const DrdThreadId tid);
bartcac53462008-03-29 09:27:08 +0000109SizeT thread_get_stack_size(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000110void thread_set_pthreadid(const DrdThreadId tid, const PThreadId ptid);
111Bool thread_get_joinable(const DrdThreadId tid);
112void thread_set_joinable(const DrdThreadId tid, const Bool joinable);
sewardj8b09d4f2007-12-04 21:27:18 +0000113void thread_set_vg_running_tid(const ThreadId vg_tid);
114void thread_set_running_tid(const ThreadId vg_tid,
115 const DrdThreadId drd_tid);
bart0268dfa2008-03-11 20:10:21 +0000116int thread_enter_synchr(const DrdThreadId tid);
117int thread_leave_synchr(const DrdThreadId tid);
118int thread_get_synchr_nesting_count(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000119void thread_new_segment(const DrdThreadId tid);
120VectorClock* thread_get_vc(const DrdThreadId tid);
barta2b6e1b2008-03-17 18:32:39 +0000121void thread_get_latest_segment(Segment** sg, const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000122void thread_combine_vc(const DrdThreadId joiner, const DrdThreadId joinee);
123void thread_combine_vc2(const DrdThreadId tid, const VectorClock* const vc);
bartdfbae6e2008-06-05 08:29:53 +0000124
125Bool bm_access_load_1_triggers_conflict(const Addr a1);
126Bool bm_access_load_2_triggers_conflict(const Addr a1);
127Bool bm_access_load_4_triggers_conflict(const Addr a1);
128Bool bm_access_load_8_triggers_conflict(const Addr a1);
129Bool bm_access_load_triggers_conflict(const Addr a1, const Addr a2);
130Bool bm_access_store_1_triggers_conflict(const Addr a1);
131Bool bm_access_store_2_triggers_conflict(const Addr a1);
132Bool bm_access_store_4_triggers_conflict(const Addr a1);
133Bool bm_access_store_8_triggers_conflict(const Addr a1);
134Bool bm_access_store_triggers_conflict(const Addr a1, const Addr a2);
135
sewardjaf44c822007-11-25 14:01:38 +0000136void thread_stop_using_mem(const Addr a1, const Addr a2);
bart0268dfa2008-03-11 20:10:21 +0000137void thread_start_recording(const DrdThreadId tid);
138void thread_stop_recording(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +0000139void thread_print_all(void);
140void thread_report_races(const DrdThreadId tid);
141void thread_report_races_segment(const DrdThreadId tid,
142 const Segment* const p);
143void thread_report_all_races(void);
sewardjaf44c822007-11-25 14:01:38 +0000144void thread_report_conflicting_segments(const DrdThreadId tid,
145 const Addr addr,
146 const SizeT size,
147 const BmAccessTypeT access_type);
148ULong thread_get_context_switch_count(void);
149ULong thread_get_report_races_count(void);
150ULong thread_get_discard_ordered_segments_count(void);
bartd66e3a82008-04-06 15:02:17 +0000151ULong thread_get_update_danger_set_count(ULong* dsnsc, ULong* dscvc);
sewardjaf44c822007-11-25 14:01:38 +0000152ULong thread_get_danger_set_bitmap_creation_count(void);
153ULong thread_get_danger_set_bitmap2_creation_count(void);
154
155
bartbf80e122008-06-06 10:18:24 +0000156static __inline__
157Bool IsValidDrdThreadId(const DrdThreadId tid)
158{
159 return (0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID
160 && ! (s_threadinfo[tid].vg_thread_exists == False
161 && s_threadinfo[tid].posix_thread_exists == False
162 && s_threadinfo[tid].detached_posix_thread == False));
163}
164
bart1ea5fff2008-03-16 08:36:23 +0000165static inline
166DrdThreadId thread_get_running_tid(void)
167{
168 tl_assert(s_drd_running_tid != DRD_INVALID_THREADID);
169 return s_drd_running_tid;
170}
171
172static inline
173struct bitmap* thread_get_danger_set(void)
bart1a473c72008-03-13 19:03:38 +0000174{
175 return s_danger_set;
176}
177
bartf00a85b2008-03-13 18:49:23 +0000178static inline
179Bool running_thread_is_recording(void)
180{
bart74a5f212008-05-11 06:43:07 +0000181 tl_assert(0 <= (int)s_drd_running_tid && s_drd_running_tid < DRD_N_THREADS
bart1a473c72008-03-13 19:03:38 +0000182 && s_drd_running_tid != DRD_INVALID_THREADID);
183 return (s_threadinfo[s_drd_running_tid].synchr_nesting == 0
184 && s_threadinfo[s_drd_running_tid].is_recording);
185}
186
bart1ea5fff2008-03-16 08:36:23 +0000187static inline
188void thread_set_stack_min(const DrdThreadId tid, const Addr stack_min)
189{
190#if 0
191 tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID);
192#endif
barte773de42008-03-29 12:54:01 +0000193 s_threadinfo[tid].stack_min = stack_min;
bart1ea5fff2008-03-16 08:36:23 +0000194#if 0
bartfda64922008-03-29 13:13:33 +0000195 /* This function can be called after the thread has been created but */
196 /* before drd_post_thread_create() has filled in stack_max. */
197 tl_assert(s_threadinfo[tid].stack_min < s_threadinfo[tid].stack_max
198 || s_threadinfo[tid].stack_max == 0);
bart1ea5fff2008-03-16 08:36:23 +0000199#endif
barte773de42008-03-29 12:54:01 +0000200 if (UNLIKELY(stack_min < s_threadinfo[tid].stack_min_min))
201 {
202 s_threadinfo[tid].stack_min_min = stack_min;
bart1ea5fff2008-03-16 08:36:23 +0000203 }
204}
205
bart1a473c72008-03-13 19:03:38 +0000206/** Return a pointer to the latest segment for the specified thread. */
207static inline
208Segment* 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. */
217static inline
218Segment* 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