sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | 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 | |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 30 | // Includes. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 31 | |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 32 | #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 |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 41 | |
| 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 | |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 52 | // Type definitions. |
| 53 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 54 | typedef UInt DrdThreadId; |
| 55 | typedef UWord PThreadId; |
| 56 | |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 57 | typedef struct |
| 58 | { |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 59 | Segment* first; |
| 60 | Segment* last; |
| 61 | ThreadId vg_threadid; |
| 62 | PThreadId pt_threadid; |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 63 | 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. */ |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 68 | /// 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; |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 79 | } 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 | |
| 85 | extern DrdThreadId s_drd_running_tid; |
| 86 | extern ThreadInfo s_threadinfo[DRD_N_THREADS]; |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 87 | extern struct bitmap* s_danger_set; |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 88 | |
| 89 | |
| 90 | // Function declarations. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 91 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 92 | void thread_trace_context_switches(const Bool t); |
| 93 | void thread_trace_danger_set(const Bool t); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 94 | void thread_set_segment_merging(const Bool m); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 95 | Bool IsValidDrdThreadId(const DrdThreadId tid); |
| 96 | |
| 97 | DrdThreadId VgThreadIdToDrdThreadId(const ThreadId tid); |
| 98 | DrdThreadId NewVgThreadIdToDrdThreadId(const ThreadId tid); |
| 99 | DrdThreadId PtThreadIdToDrdThreadId(const PThreadId tid); |
| 100 | ThreadId DrdThreadIdToVgThreadId(const DrdThreadId tid); |
| 101 | DrdThreadId thread_pre_create(const DrdThreadId creator, |
| 102 | const ThreadId vg_created); |
| 103 | DrdThreadId thread_post_create(const ThreadId vg_created); |
| 104 | void thread_delete(const DrdThreadId tid); |
| 105 | void thread_finished(const DrdThreadId tid); |
| 106 | void thread_set_stack_startup(const DrdThreadId tid, const Addr stack_startup); |
| 107 | Addr thread_get_stack_min(const DrdThreadId tid); |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 108 | Addr thread_get_stack_min_min(const DrdThreadId tid); |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 109 | Addr thread_get_stack_max(const DrdThreadId tid); |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 110 | SizeT thread_get_stack_size(const DrdThreadId tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 111 | void thread_set_pthreadid(const DrdThreadId tid, const PThreadId ptid); |
| 112 | Bool thread_get_joinable(const DrdThreadId tid); |
| 113 | void thread_set_joinable(const DrdThreadId tid, const Bool joinable); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 114 | void thread_set_vg_running_tid(const ThreadId vg_tid); |
| 115 | void thread_set_running_tid(const ThreadId vg_tid, |
| 116 | const DrdThreadId drd_tid); |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 117 | int thread_enter_synchr(const DrdThreadId tid); |
| 118 | int thread_leave_synchr(const DrdThreadId tid); |
| 119 | int thread_get_synchr_nesting_count(const DrdThreadId tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 120 | void thread_new_segment(const DrdThreadId tid); |
| 121 | VectorClock* thread_get_vc(const DrdThreadId tid); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 122 | void thread_get_latest_segment(Segment** sg, const DrdThreadId tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 123 | void thread_combine_vc(const DrdThreadId joiner, const DrdThreadId joinee); |
| 124 | void thread_combine_vc2(const DrdThreadId tid, const VectorClock* const vc); |
| 125 | void thread_stop_using_mem(const Addr a1, const Addr a2); |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 126 | void thread_start_recording(const DrdThreadId tid); |
| 127 | void thread_stop_recording(const DrdThreadId tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 128 | void thread_print_all(void); |
| 129 | void thread_report_races(const DrdThreadId tid); |
| 130 | void thread_report_races_segment(const DrdThreadId tid, |
| 131 | const Segment* const p); |
| 132 | void thread_report_all_races(void); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 133 | void thread_report_conflicting_segments(const DrdThreadId tid, |
| 134 | const Addr addr, |
| 135 | const SizeT size, |
| 136 | const BmAccessTypeT access_type); |
| 137 | ULong thread_get_context_switch_count(void); |
| 138 | ULong thread_get_report_races_count(void); |
| 139 | ULong thread_get_discard_ordered_segments_count(void); |
| 140 | ULong thread_get_update_danger_set_count(void); |
| 141 | ULong thread_get_danger_set_bitmap_creation_count(void); |
| 142 | ULong thread_get_danger_set_bitmap2_creation_count(void); |
| 143 | |
| 144 | |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 145 | static inline |
| 146 | DrdThreadId thread_get_running_tid(void) |
| 147 | { |
| 148 | tl_assert(s_drd_running_tid != DRD_INVALID_THREADID); |
| 149 | return s_drd_running_tid; |
| 150 | } |
| 151 | |
| 152 | static inline |
| 153 | struct bitmap* thread_get_danger_set(void) |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 154 | { |
| 155 | return s_danger_set; |
| 156 | } |
| 157 | |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 158 | static inline |
| 159 | Bool running_thread_is_recording(void) |
| 160 | { |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 161 | tl_assert(0 <= s_drd_running_tid && s_drd_running_tid < DRD_N_THREADS |
| 162 | && s_drd_running_tid != DRD_INVALID_THREADID); |
| 163 | return (s_threadinfo[s_drd_running_tid].synchr_nesting == 0 |
| 164 | && s_threadinfo[s_drd_running_tid].is_recording); |
| 165 | } |
| 166 | |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 167 | static inline |
| 168 | void thread_set_stack_min(const DrdThreadId tid, const Addr stack_min) |
| 169 | { |
| 170 | #if 0 |
| 171 | tl_assert(0 <= tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID); |
| 172 | #endif |
bart | e773de4 | 2008-03-29 12:54:01 +0000 | [diff] [blame] | 173 | s_threadinfo[tid].stack_min = stack_min; |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 174 | #if 0 |
bart | fda6492 | 2008-03-29 13:13:33 +0000 | [diff] [blame^] | 175 | /* This function can be called after the thread has been created but */ |
| 176 | /* before drd_post_thread_create() has filled in stack_max. */ |
| 177 | tl_assert(s_threadinfo[tid].stack_min < s_threadinfo[tid].stack_max |
| 178 | || s_threadinfo[tid].stack_max == 0); |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 179 | #endif |
bart | e773de4 | 2008-03-29 12:54:01 +0000 | [diff] [blame] | 180 | if (UNLIKELY(stack_min < s_threadinfo[tid].stack_min_min)) |
| 181 | { |
| 182 | s_threadinfo[tid].stack_min_min = stack_min; |
bart | 1ea5fff | 2008-03-16 08:36:23 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 186 | /** Return a pointer to the latest segment for the specified thread. */ |
| 187 | static inline |
| 188 | Segment* thread_get_segment(const DrdThreadId tid) |
| 189 | { |
| 190 | tl_assert(0 <= tid && tid < DRD_N_THREADS |
| 191 | && tid != DRD_INVALID_THREADID); |
| 192 | tl_assert(s_threadinfo[tid].last); |
| 193 | return s_threadinfo[tid].last; |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 194 | } |
| 195 | |
bart | a79df6e | 2008-03-14 17:07:51 +0000 | [diff] [blame] | 196 | /** Return a pointer to the latest segment for the running thread. */ |
| 197 | static inline |
| 198 | Segment* running_thread_get_segment(void) |
| 199 | { |
| 200 | return thread_get_segment(s_drd_running_tid); |
| 201 | } |
bart | f00a85b | 2008-03-13 18:49:23 +0000 | [diff] [blame] | 202 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 203 | #endif // __THREAD_H |