njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- The thread state. m_threadstate.c ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
| 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
| 9 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 10 | Copyright (C) 2000-2017 Julian Seward |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 11 | jseward@acm.org |
| 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
| 31 | #include "pub_core_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 32 | #include "pub_core_vki.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 33 | #include "pub_core_threadstate.h" |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 34 | #include "pub_core_mallocfree.h" // VG_(malloc) |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 35 | #include "pub_core_libcassert.h" |
florian | c91f584 | 2013-09-15 10:42:26 +0000 | [diff] [blame] | 36 | #include "pub_core_inner.h" |
bart | 27233e9 | 2012-03-08 14:59:25 +0000 | [diff] [blame] | 37 | #if defined(ENABLE_INNER_CLIENT_REQUEST) |
| 38 | #include "helgrind/helgrind.h" |
bart | 27233e9 | 2012-03-08 14:59:25 +0000 | [diff] [blame] | 39 | #endif |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 40 | |
| 41 | /*------------------------------------------------------------*/ |
| 42 | /*--- Data structures. ---*/ |
| 43 | /*------------------------------------------------------------*/ |
| 44 | |
| 45 | ThreadId VG_(running_tid) = VG_INVALID_THREADID; |
| 46 | |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 47 | ThreadState *VG_(threads); |
| 48 | UInt VG_N_THREADS; |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 49 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 50 | ThreadState *VG_(inner_threads); |
| 51 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 52 | /*------------------------------------------------------------*/ |
| 53 | /*--- Operations. ---*/ |
| 54 | /*------------------------------------------------------------*/ |
| 55 | |
bart | 27233e9 | 2012-03-08 14:59:25 +0000 | [diff] [blame] | 56 | void VG_(init_Threads)(void) |
| 57 | { |
| 58 | ThreadId tid; |
florian | 1e802b6 | 2015-02-13 19:08:26 +0000 | [diff] [blame] | 59 | |
philippe | 4e4ded7 | 2015-08-21 22:22:27 +0000 | [diff] [blame] | 60 | VG_(threads) = VG_(arena_memalign) (VG_AR_CORE, "init_Threads", |
| 61 | LibVEX_GUEST_STATE_ALIGN, |
| 62 | VG_N_THREADS * sizeof VG_(threads)[0]); |
bart | 27233e9 | 2012-03-08 14:59:25 +0000 | [diff] [blame] | 63 | |
bart | 92e2c64 | 2012-03-08 19:07:08 +0000 | [diff] [blame] | 64 | for (tid = 1; tid < VG_N_THREADS; tid++) { |
| 65 | INNER_REQUEST( |
| 66 | ANNOTATE_BENIGN_RACE_SIZED(&VG_(threads)[tid].status, |
| 67 | sizeof(VG_(threads)[tid].status), "")); |
| 68 | INNER_REQUEST( |
| 69 | ANNOTATE_BENIGN_RACE_SIZED(&VG_(threads)[tid].os_state.exitcode, |
| 70 | sizeof(VG_(threads)[tid].os_state.exitcode), |
| 71 | "")); |
| 72 | } |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 73 | INNER_REQUEST(VALGRIND_INNER_THREADS(VG_(threads))); |
bart | 27233e9 | 2012-03-08 14:59:25 +0000 | [diff] [blame] | 74 | } |
| 75 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 76 | const HChar* VG_(name_of_ThreadStatus) ( ThreadStatus status ) |
| 77 | { |
| 78 | switch (status) { |
| 79 | case VgTs_Empty: return "VgTs_Empty"; |
| 80 | case VgTs_Init: return "VgTs_Init"; |
| 81 | case VgTs_Runnable: return "VgTs_Runnable"; |
| 82 | case VgTs_WaitSys: return "VgTs_WaitSys"; |
| 83 | case VgTs_Yielding: return "VgTs_Yielding"; |
| 84 | case VgTs_Zombie: return "VgTs_Zombie"; |
| 85 | default: return "VgTs_???"; |
| 86 | } |
| 87 | } |
| 88 | |
philippe | b8ba031 | 2013-10-21 19:57:08 +0000 | [diff] [blame] | 89 | const HChar* VG_(name_of_VgSchedReturnCode) ( VgSchedReturnCode retcode ) |
| 90 | { |
| 91 | switch (retcode) { |
| 92 | case VgSrc_None: return "VgSrc_None"; |
| 93 | case VgSrc_ExitThread: return "VgSrc_ExitThread"; |
| 94 | case VgSrc_ExitProcess: return "VgSrc_ExitProcess"; |
| 95 | case VgSrc_FatalSig: return "VgSrc_FatalSig"; |
| 96 | default: return "VgSrc_???"; |
| 97 | } |
| 98 | } |
| 99 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 100 | ThreadState *VG_(get_ThreadState)(ThreadId tid) |
| 101 | { |
| 102 | vg_assert(tid >= 0 && tid < VG_N_THREADS); |
sewardj | 13ac35b | 2005-07-08 18:23:40 +0000 | [diff] [blame] | 103 | vg_assert(VG_(threads)[tid].tid == tid); |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 104 | return &VG_(threads)[tid]; |
| 105 | } |
| 106 | |
| 107 | Bool VG_(is_valid_tid) ( ThreadId tid ) |
| 108 | { |
| 109 | /* tid is unsigned, hence no < 0 test. */ |
| 110 | if (tid == 0) return False; |
| 111 | if (tid >= VG_N_THREADS) return False; |
| 112 | if (VG_(threads)[tid].status == VgTs_Empty) return False; |
| 113 | return True; |
| 114 | } |
| 115 | |
| 116 | // This function is for tools to call. |
| 117 | ThreadId VG_(get_running_tid)(void) |
| 118 | { |
| 119 | return VG_(running_tid); |
| 120 | } |
| 121 | |
| 122 | Bool VG_(is_running_thread)(ThreadId tid) |
| 123 | { |
| 124 | ThreadState *tst = VG_(get_ThreadState)(tid); |
| 125 | |
| 126 | return |
| 127 | // tst->os_state.lwpid == VG_(gettid)() && // check we're this tid |
| 128 | VG_(running_tid) == tid && // and that we've got the lock |
| 129 | tst->status == VgTs_Runnable; // and we're runnable |
| 130 | } |
| 131 | |
| 132 | /* Return true if the thread is still alive but in the process of exiting. */ |
| 133 | inline Bool VG_(is_exiting)(ThreadId tid) |
| 134 | { |
| 135 | vg_assert(VG_(is_valid_tid)(tid)); |
| 136 | return VG_(threads)[tid].exitreason != VgSrc_None; |
| 137 | } |
| 138 | |
| 139 | /* Return the number of non-dead Threads */ |
| 140 | Int VG_(count_living_threads)(void) |
| 141 | { |
| 142 | Int count = 0; |
| 143 | ThreadId tid; |
| 144 | |
| 145 | for(tid = 1; tid < VG_N_THREADS; tid++) |
| 146 | if (VG_(threads)[tid].status != VgTs_Empty && |
| 147 | VG_(threads)[tid].status != VgTs_Zombie) |
| 148 | count++; |
| 149 | |
| 150 | return count; |
| 151 | } |
| 152 | |
sewardj | d8a725e | 2006-10-17 02:01:12 +0000 | [diff] [blame] | 153 | /* Return the number of threads in VgTs_Runnable state */ |
| 154 | Int VG_(count_runnable_threads)(void) |
| 155 | { |
| 156 | Int count = 0; |
| 157 | ThreadId tid; |
| 158 | |
| 159 | for(tid = 1; tid < VG_N_THREADS; tid++) |
| 160 | if (VG_(threads)[tid].status == VgTs_Runnable) |
| 161 | count++; |
| 162 | |
| 163 | return count; |
| 164 | } |
| 165 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 166 | /* Given an LWP id (ie, real kernel thread id), find the corresponding |
| 167 | ThreadId */ |
sewardj | 4278172 | 2006-12-17 19:36:06 +0000 | [diff] [blame] | 168 | ThreadId VG_(lwpid_to_vgtid)(Int lwp) |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 169 | { |
| 170 | ThreadId tid; |
| 171 | |
| 172 | for(tid = 1; tid < VG_N_THREADS; tid++) |
sewardj | d8a725e | 2006-10-17 02:01:12 +0000 | [diff] [blame] | 173 | if (VG_(threads)[tid].status != VgTs_Empty |
| 174 | && VG_(threads)[tid].os_state.lwpid == lwp) |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 175 | return tid; |
| 176 | |
| 177 | return VG_INVALID_THREADID; |
| 178 | } |
| 179 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 180 | /*--------------------------------------------------------------------*/ |
| 181 | /*--- end ---*/ |
| 182 | /*--------------------------------------------------------------------*/ |