/* -*- mode: C; c-basic-offset: 3; -*- */ /* This file is part of drd, a thread error detector. Copyright (C) 2006-2010 Bart Van Assche . This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. The GNU General Public License is contained in the file COPYING. */ #ifndef __THREAD_H #define __THREAD_H /* Include directives. */ #include "drd_basics.h" #include "drd_segment.h" #include "pub_drd_bitmap.h" #include "pub_tool_libcassert.h" /* tl_assert() */ #include "pub_tool_stacktrace.h" /* typedef StackTrace */ #include "pub_tool_threadstate.h" /* VG_N_THREADS */ /* Defines. */ /** Maximum number of threads DRD keeps information about. */ #define DRD_N_THREADS VG_N_THREADS /** A number different from any valid DRD thread ID. */ #define DRD_INVALID_THREADID 0 /** * A number different from any valid POSIX thread ID. * * @note The PThreadId typedef and the INVALID_POSIX_THREADID depend on the * operating system and threading library in use. PThreadId must contain at * least as many bits as pthread_t, and INVALID_POSIX_THREADID * must be a value that will never be returned by pthread_self(). */ #define INVALID_POSIX_THREADID ((PThreadId)0) /* Type definitions. */ /** * POSIX thread ID. The type PThreadId must be at least as wide as * pthread_t. */ typedef UWord PThreadId; /** Per-thread information managed by DRD. */ typedef struct { Segment* first; /**< Pointer to first segment. */ Segment* last; /**< Pointer to last segment. */ ThreadId vg_threadid; /**< Valgrind thread ID. */ PThreadId pt_threadid; /**< POSIX thread ID. */ Addr stack_min_min; /**< Lowest value stack pointer ever had. */ Addr stack_min; /**< Current stack pointer. */ Addr stack_startup; /** 0); } /** * Reports whether or not recording of memory loads is enabled for the * currently running client thread. */ static __inline__ Bool DRD_(running_thread_is_recording_loads)(void) { #ifdef ENABLE_DRD_CONSISTENCY_CHECKS tl_assert(0 <= (int)DRD_(g_drd_running_tid) && DRD_(g_drd_running_tid) < DRD_N_THREADS && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); #endif return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0 && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_loads); } /** * Reports whether or not recording memory stores is enabled for the * currently running client thread. */ static __inline__ Bool DRD_(running_thread_is_recording_stores)(void) { #ifdef ENABLE_DRD_CONSISTENCY_CHECKS tl_assert(0 <= (int)DRD_(g_drd_running_tid) && DRD_(g_drd_running_tid) < DRD_N_THREADS && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); #endif return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].synchr_nesting == 0 && DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].is_recording_stores); } /** * Update the information about the lowest stack address that has ever been * accessed by a thread. */ static __inline__ void DRD_(thread_set_stack_min)(const DrdThreadId tid, const Addr stack_min) { #ifdef ENABLE_DRD_CONSISTENCY_CHECKS tl_assert(0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID); #endif DRD_(g_threadinfo)[tid].stack_min = stack_min; #ifdef ENABLE_DRD_CONSISTENCY_CHECKS /* This function can be called after the thread has been created but */ /* before drd_post_thread_create() has filled in stack_max. */ tl_assert(DRD_(g_threadinfo)[tid].stack_min < DRD_(g_threadinfo)[tid].stack_max || DRD_(g_threadinfo)[tid].stack_max == 0); #endif if (UNLIKELY(stack_min < DRD_(g_threadinfo)[tid].stack_min_min)) { DRD_(g_threadinfo)[tid].stack_min_min = stack_min; } } /** * Return true if and only if the specified address is on the stack of the * currently scheduled thread. */ static __inline__ Bool DRD_(thread_address_on_stack)(const Addr a) { return (DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_min <= a && a < DRD_(g_threadinfo)[DRD_(g_drd_running_tid)].stack_max); } /** * Return true if and only if the specified address is on the stack of any * thread. */ static __inline__ Bool DRD_(thread_address_on_any_stack)(const Addr a) { int i; for (i = 1; i < DRD_N_THREADS; i++) { if (DRD_(g_threadinfo)[i].vg_thread_exists && DRD_(g_threadinfo)[i].stack_min <= a && a < DRD_(g_threadinfo)[i].stack_max) { return True; } } return False; } /** Return a pointer to the latest segment for the specified thread. */ static __inline__ Segment* DRD_(thread_get_segment)(const DrdThreadId tid) { #ifdef ENABLE_DRD_CONSISTENCY_CHECKS tl_assert(0 <= (int)tid && tid < DRD_N_THREADS && tid != DRD_INVALID_THREADID); tl_assert(DRD_(g_threadinfo)[tid].last); #endif return DRD_(g_threadinfo)[tid].last; } /** Return a pointer to the latest segment for the running thread. */ static __inline__ Segment* DRD_(running_thread_get_segment)(void) { return DRD_(thread_get_segment)(DRD_(g_drd_running_tid)); } #endif /* __THREAD_H */