Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ThreadList.cpp ------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 +0000 | [diff] [blame] | 9 | |
| 10 | // C Includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | #include <stdlib.h> |
| 12 | |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 +0000 | [diff] [blame] | 13 | // C++ Includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 14 | #include <algorithm> |
| 15 | |
Eugene Zelenko | e65b2cf | 2015-12-15 01:33:19 +0000 | [diff] [blame] | 16 | // Other libraries and framework includes |
| 17 | // Project includes |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Log.h" |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 19 | #include "lldb/Core/State.h" |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 20 | #include "lldb/Target/RegisterContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Target/ThreadList.h" |
| 22 | #include "lldb/Target/Thread.h" |
| 23 | #include "lldb/Target/ThreadPlan.h" |
| 24 | #include "lldb/Target/Process.h" |
Zachary Turner | 5023257 | 2015-03-18 21:31:45 +0000 | [diff] [blame] | 25 | #include "lldb/Utility/ConvertEnum.h" |
Zachary Turner | 8f186f8 | 2015-11-13 21:53:03 +0000 | [diff] [blame] | 26 | #include "lldb/Utility/LLDBAssert.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
| 28 | using namespace lldb; |
| 29 | using namespace lldb_private; |
| 30 | |
| 31 | ThreadList::ThreadList (Process *process) : |
Kuba Brecka | e4d4801 | 2014-09-05 19:13:15 +0000 | [diff] [blame] | 32 | ThreadCollection(), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | m_process (process), |
| 34 | m_stop_id (0), |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 35 | m_selected_tid (LLDB_INVALID_THREAD_ID) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | ThreadList::ThreadList (const ThreadList &rhs) : |
Kuba Brecka | e4d4801 | 2014-09-05 19:13:15 +0000 | [diff] [blame] | 40 | ThreadCollection(), |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 41 | m_process (rhs.m_process), |
| 42 | m_stop_id (rhs.m_stop_id), |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 43 | m_selected_tid () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | { |
| 45 | // Use the assignment operator since it uses the mutex |
| 46 | *this = rhs; |
| 47 | } |
| 48 | |
| 49 | const ThreadList& |
| 50 | ThreadList::operator = (const ThreadList& rhs) |
| 51 | { |
| 52 | if (this != &rhs) |
| 53 | { |
| 54 | // Lock both mutexes to make sure neither side changes anyone on us |
Bruce Mitchener | e171da5 | 2015-07-22 00:16:02 +0000 | [diff] [blame] | 55 | // while the assignment occurs |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 56 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | m_process = rhs.m_process; |
| 58 | m_stop_id = rhs.m_stop_id; |
| 59 | m_threads = rhs.m_threads; |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 60 | m_selected_tid = rhs.m_selected_tid; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | } |
| 62 | return *this; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | ThreadList::~ThreadList() |
| 67 | { |
Greg Clayton | ac358da | 2013-03-28 18:33:53 +0000 | [diff] [blame] | 68 | // Clear the thread list. Clear will take the mutex lock |
| 69 | // which will ensure that if anyone is using the list |
| 70 | // they won't get it removed while using it. |
| 71 | Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Jim Ingham | 8d94ba0 | 2016-03-12 02:45:34 +0000 | [diff] [blame^] | 74 | lldb::ThreadSP |
| 75 | ThreadList::GetExpressionExecutionThread() |
| 76 | { |
| 77 | if (m_expression_tid_stack.empty()) |
| 78 | return GetSelectedThread(); |
| 79 | ThreadSP expr_thread_sp = FindThreadByID(m_expression_tid_stack.back()); |
| 80 | if (expr_thread_sp) |
| 81 | return expr_thread_sp; |
| 82 | else |
| 83 | return GetSelectedThread(); |
| 84 | } |
| 85 | |
| 86 | void |
| 87 | ThreadList::PushExpressionExecutionThread(lldb::tid_t tid) |
| 88 | { |
| 89 | m_expression_tid_stack.push_back(tid); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | ThreadList::PopExpressionExecutionThread(lldb::tid_t tid) |
| 94 | { |
| 95 | assert(m_expression_tid_stack.back() == tid); |
| 96 | m_expression_tid_stack.pop_back(); |
| 97 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | |
| 99 | uint32_t |
| 100 | ThreadList::GetStopID () const |
| 101 | { |
| 102 | return m_stop_id; |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | ThreadList::SetStopID (uint32_t stop_id) |
| 107 | { |
| 108 | m_stop_id = stop_id; |
| 109 | } |
| 110 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 111 | uint32_t |
| 112 | ThreadList::GetSize (bool can_update) |
| 113 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 114 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | if (can_update) |
| 116 | m_process->UpdateThreadListIfNeeded(); |
| 117 | return m_threads.size(); |
| 118 | } |
| 119 | |
| 120 | ThreadSP |
| 121 | ThreadList::GetThreadAtIndex (uint32_t idx, bool can_update) |
| 122 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 123 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 124 | if (can_update) |
| 125 | m_process->UpdateThreadListIfNeeded(); |
| 126 | |
| 127 | ThreadSP thread_sp; |
| 128 | if (idx < m_threads.size()) |
| 129 | thread_sp = m_threads[idx]; |
| 130 | return thread_sp; |
| 131 | } |
| 132 | |
| 133 | ThreadSP |
| 134 | ThreadList::FindThreadByID (lldb::tid_t tid, bool can_update) |
| 135 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 136 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 137 | |
| 138 | if (can_update) |
| 139 | m_process->UpdateThreadListIfNeeded(); |
| 140 | |
| 141 | ThreadSP thread_sp; |
| 142 | uint32_t idx = 0; |
| 143 | const uint32_t num_threads = m_threads.size(); |
| 144 | for (idx = 0; idx < num_threads; ++idx) |
| 145 | { |
| 146 | if (m_threads[idx]->GetID() == tid) |
| 147 | { |
| 148 | thread_sp = m_threads[idx]; |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | return thread_sp; |
| 153 | } |
| 154 | |
| 155 | ThreadSP |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 156 | ThreadList::FindThreadByProtocolID (lldb::tid_t tid, bool can_update) |
| 157 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 158 | Mutex::Locker locker(GetMutex()); |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 159 | |
| 160 | if (can_update) |
| 161 | m_process->UpdateThreadListIfNeeded(); |
| 162 | |
| 163 | ThreadSP thread_sp; |
| 164 | uint32_t idx = 0; |
| 165 | const uint32_t num_threads = m_threads.size(); |
| 166 | for (idx = 0; idx < num_threads; ++idx) |
| 167 | { |
| 168 | if (m_threads[idx]->GetProtocolID() == tid) |
| 169 | { |
| 170 | thread_sp = m_threads[idx]; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | return thread_sp; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | ThreadSP |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 179 | ThreadList::RemoveThreadByID (lldb::tid_t tid, bool can_update) |
| 180 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 181 | Mutex::Locker locker(GetMutex()); |
Han Ming Ong | c2c423e | 2013-01-08 22:10:01 +0000 | [diff] [blame] | 182 | |
| 183 | if (can_update) |
| 184 | m_process->UpdateThreadListIfNeeded(); |
| 185 | |
| 186 | ThreadSP thread_sp; |
| 187 | uint32_t idx = 0; |
| 188 | const uint32_t num_threads = m_threads.size(); |
| 189 | for (idx = 0; idx < num_threads; ++idx) |
| 190 | { |
| 191 | if (m_threads[idx]->GetID() == tid) |
| 192 | { |
| 193 | thread_sp = m_threads[idx]; |
| 194 | m_threads.erase(m_threads.begin()+idx); |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | return thread_sp; |
| 199 | } |
| 200 | |
| 201 | ThreadSP |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 202 | ThreadList::RemoveThreadByProtocolID (lldb::tid_t tid, bool can_update) |
| 203 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 204 | Mutex::Locker locker(GetMutex()); |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 205 | |
| 206 | if (can_update) |
| 207 | m_process->UpdateThreadListIfNeeded(); |
| 208 | |
| 209 | ThreadSP thread_sp; |
| 210 | uint32_t idx = 0; |
| 211 | const uint32_t num_threads = m_threads.size(); |
| 212 | for (idx = 0; idx < num_threads; ++idx) |
| 213 | { |
| 214 | if (m_threads[idx]->GetProtocolID() == tid) |
| 215 | { |
| 216 | thread_sp = m_threads[idx]; |
| 217 | m_threads.erase(m_threads.begin()+idx); |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | return thread_sp; |
| 222 | } |
| 223 | |
| 224 | ThreadSP |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 225 | ThreadList::GetThreadSPForThreadPtr (Thread *thread_ptr) |
| 226 | { |
| 227 | ThreadSP thread_sp; |
| 228 | if (thread_ptr) |
| 229 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 230 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 231 | |
| 232 | uint32_t idx = 0; |
| 233 | const uint32_t num_threads = m_threads.size(); |
| 234 | for (idx = 0; idx < num_threads; ++idx) |
| 235 | { |
| 236 | if (m_threads[idx].get() == thread_ptr) |
| 237 | { |
| 238 | thread_sp = m_threads[idx]; |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | return thread_sp; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
| 248 | ThreadSP |
| 249 | ThreadList::FindThreadByIndexID (uint32_t index_id, bool can_update) |
| 250 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 251 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | |
| 253 | if (can_update) |
| 254 | m_process->UpdateThreadListIfNeeded(); |
| 255 | |
| 256 | ThreadSP thread_sp; |
| 257 | const uint32_t num_threads = m_threads.size(); |
| 258 | for (uint32_t idx = 0; idx < num_threads; ++idx) |
| 259 | { |
| 260 | if (m_threads[idx]->GetIndexID() == index_id) |
| 261 | { |
| 262 | thread_sp = m_threads[idx]; |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | return thread_sp; |
| 267 | } |
| 268 | |
| 269 | bool |
| 270 | ThreadList::ShouldStop (Event *event_ptr) |
| 271 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 272 | // Running events should never stop, obviously... |
| 273 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 274 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 276 | // The ShouldStop method of the threads can do a whole lot of work, |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 277 | // figuring out whether the thread plan conditions are met. So we don't want |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 278 | // to keep the ThreadList locked the whole time we are doing this. |
| 279 | // FIXME: It is possible that running code could cause new threads |
Adrian McCarthy | 2f8e4c3 | 2015-05-18 23:24:32 +0000 | [diff] [blame] | 280 | // to be created. If that happens, we will miss asking them whether |
| 281 | // they should stop. This is not a big deal since we haven't had |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 282 | // a chance to hang any interesting operations on those threads yet. |
| 283 | |
| 284 | collection threads_copy; |
| 285 | { |
| 286 | // Scope for locker |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 287 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 288 | |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 289 | m_process->UpdateThreadListIfNeeded(); |
Jim Ingham | 569aaf9 | 2015-11-06 22:45:57 +0000 | [diff] [blame] | 290 | for (lldb::ThreadSP thread_sp : m_threads) |
| 291 | { |
| 292 | // This is an optimization... If we didn't let a thread run in between the previous stop and this |
| 293 | // one, we shouldn't have to consult it for ShouldStop. So just leave it off the list we are going to |
| 294 | // inspect. |
Chaoren Lin | 6e8fbc6 | 2015-11-07 02:16:31 +0000 | [diff] [blame] | 295 | // On Linux, if a thread-specific conditional breakpoint was hit, it won't necessarily be the thread |
| 296 | // that hit the breakpoint itself that evaluates the conditional expression, so the thread that hit |
| 297 | // the breakpoint could still be asked to stop, even though it hasn't been allowed to run since the |
| 298 | // previous stop. |
| 299 | if (thread_sp->GetTemporaryResumeState () != eStateSuspended || thread_sp->IsStillAtLastBreakpointHit()) |
Jim Ingham | 569aaf9 | 2015-11-06 22:45:57 +0000 | [diff] [blame] | 300 | threads_copy.push_back(thread_sp); |
| 301 | } |
| 302 | |
| 303 | // It is possible the threads we were allowing to run all exited and then maybe the user interrupted |
| 304 | // or something, then fall back on looking at all threads: |
| 305 | |
| 306 | if (threads_copy.size() == 0) |
| 307 | threads_copy = m_threads; |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | collection::iterator pos, end = threads_copy.end(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 311 | |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 312 | if (log) |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 313 | { |
| 314 | log->PutCString(""); |
Jim Ingham | 569aaf9 | 2015-11-06 22:45:57 +0000 | [diff] [blame] | 315 | log->Printf ("ThreadList::%s: %" PRIu64 " threads, %" PRIu64 " unsuspended threads", |
| 316 | __FUNCTION__, |
| 317 | (uint64_t)m_threads.size(), |
| 318 | (uint64_t)threads_copy.size()); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 319 | } |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 320 | |
Jim Ingham | a007904 | 2013-03-21 21:46:56 +0000 | [diff] [blame] | 321 | bool did_anybody_stop_for_a_reason = false; |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 322 | |
| 323 | // If the event is an Interrupt event, then we're going to stop no matter what. Otherwise, presume we won't stop. |
Jim Ingham | 7bc3465 | 2013-04-16 22:53:24 +0000 | [diff] [blame] | 324 | bool should_stop = false; |
Jim Ingham | 35878c4 | 2014-04-08 21:33:21 +0000 | [diff] [blame] | 325 | if (Process::ProcessEventData::GetInterruptedFromEvent(event_ptr)) |
| 326 | { |
| 327 | if (log) |
| 328 | log->Printf("ThreadList::%s handling interrupt event, should stop set to true", __FUNCTION__); |
| 329 | |
| 330 | should_stop = true; |
| 331 | } |
Jim Ingham | 7bc3465 | 2013-04-16 22:53:24 +0000 | [diff] [blame] | 332 | |
| 333 | // Now we run through all the threads and get their stop info's. We want to make sure to do this first before |
| 334 | // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a |
| 335 | // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly. |
| 336 | // We don't need to use it here, we just want to make sure it gets computed. |
| 337 | |
| 338 | for (pos = threads_copy.begin(); pos != end; ++pos) |
| 339 | { |
| 340 | ThreadSP thread_sp(*pos); |
| 341 | thread_sp->GetStopInfo(); |
| 342 | } |
Jim Ingham | a007904 | 2013-03-21 21:46:56 +0000 | [diff] [blame] | 343 | |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 344 | for (pos = threads_copy.begin(); pos != end; ++pos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 345 | { |
| 346 | ThreadSP thread_sp(*pos); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 347 | |
Jim Ingham | 39fdae7 | 2014-01-15 03:32:42 +0000 | [diff] [blame] | 348 | // We should never get a stop for which no thread had a stop reason, but sometimes we do see this - |
| 349 | // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out |
| 350 | // the right thing to do and stopping gives the user control over what to do in this instance. |
| 351 | // |
| 352 | // Note, this causes a problem when you have a thread specific breakpoint, and a bunch of threads hit the breakpoint, |
| 353 | // but not the thread which we are waiting for. All the threads that are not "supposed" to hit the breakpoint |
| 354 | // are marked as having no stop reason, which is right, they should not show a stop reason. But that triggers this |
| 355 | // code and causes us to stop seemingly for no reason. |
| 356 | // |
| 357 | // Since the only way we ever saw this error was on first attach, I'm only going to trigger set did_anybody_stop_for_a_reason |
| 358 | // to true unless this is the first stop. |
| 359 | // |
| 360 | // If this becomes a problem, we'll have to have another StopReason like "StopInfoHidden" which will look invalid |
| 361 | // everywhere but at this check. |
| 362 | |
Ed Maste | 21afbe0 | 2014-01-17 17:31:06 +0000 | [diff] [blame] | 363 | if (thread_sp->GetProcess()->GetStopID() > 1) |
Jim Ingham | 39fdae7 | 2014-01-15 03:32:42 +0000 | [diff] [blame] | 364 | did_anybody_stop_for_a_reason = true; |
| 365 | else |
| 366 | did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason(); |
Jim Ingham | a007904 | 2013-03-21 21:46:56 +0000 | [diff] [blame] | 367 | |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 368 | const bool thread_should_stop = thread_sp->ShouldStop(event_ptr); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 369 | if (thread_should_stop) |
| 370 | should_stop |= true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 371 | } |
Jim Ingham | b01e742 | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 372 | |
Jim Ingham | a007904 | 2013-03-21 21:46:56 +0000 | [diff] [blame] | 373 | if (!should_stop && !did_anybody_stop_for_a_reason) |
| 374 | { |
| 375 | should_stop = true; |
| 376 | if (log) |
| 377 | log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__); |
| 378 | } |
| 379 | |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 380 | if (log) |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 381 | log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 383 | if (should_stop) |
| 384 | { |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 385 | for (pos = threads_copy.begin(); pos != end; ++pos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | { |
| 387 | ThreadSP thread_sp(*pos); |
| 388 | thread_sp->WillStop (); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | return should_stop; |
| 393 | } |
| 394 | |
| 395 | Vote |
| 396 | ThreadList::ShouldReportStop (Event *event_ptr) |
| 397 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 398 | Mutex::Locker locker(GetMutex()); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 399 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 400 | Vote result = eVoteNoOpinion; |
| 401 | m_process->UpdateThreadListIfNeeded(); |
| 402 | collection::iterator pos, end = m_threads.end(); |
| 403 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 404 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 405 | |
| 406 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 407 | log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size()); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 409 | // Run through the threads and ask whether we should report this event. |
| 410 | // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion. |
| 411 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 412 | { |
| 413 | ThreadSP thread_sp(*pos); |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 414 | const Vote vote = thread_sp->ShouldReportStop (event_ptr); |
| 415 | switch (vote) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 416 | { |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 417 | case eVoteNoOpinion: |
| 418 | continue; |
| 419 | |
| 420 | case eVoteYes: |
| 421 | result = eVoteYes; |
| 422 | break; |
| 423 | |
| 424 | case eVoteNo: |
| 425 | if (result == eVoteNoOpinion) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 426 | { |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 427 | result = eVoteNo; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 428 | } |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 429 | else |
| 430 | { |
| 431 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 432 | log->Printf ("ThreadList::%s thread 0x%4.4" PRIx64 ": voted %s, but lost out because result was %s", |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 433 | __FUNCTION__, |
| 434 | thread_sp->GetID (), |
| 435 | GetVoteAsCString (vote), |
| 436 | GetVoteAsCString (result)); |
| 437 | } |
| 438 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 441 | if (log) |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 442 | log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 443 | return result; |
| 444 | } |
| 445 | |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame] | 446 | void |
| 447 | ThreadList::SetShouldReportStop (Vote vote) |
| 448 | { |
| 449 | Mutex::Locker locker(GetMutex()); |
| 450 | m_process->UpdateThreadListIfNeeded(); |
| 451 | collection::iterator pos, end = m_threads.end(); |
| 452 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 453 | { |
| 454 | ThreadSP thread_sp(*pos); |
| 455 | thread_sp->SetShouldReportStop (vote); |
| 456 | } |
| 457 | } |
| 458 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 459 | Vote |
| 460 | ThreadList::ShouldReportRun (Event *event_ptr) |
| 461 | { |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 462 | |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 463 | Mutex::Locker locker(GetMutex()); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 465 | Vote result = eVoteNoOpinion; |
| 466 | m_process->UpdateThreadListIfNeeded(); |
| 467 | collection::iterator pos, end = m_threads.end(); |
| 468 | |
| 469 | // Run through the threads and ask whether we should report this event. |
| 470 | // The rule is NO vote wins over everything, a YES vote wins over no opinion. |
| 471 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 472 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | ce57983 | 2011-01-24 04:11:25 +0000 | [diff] [blame] | 473 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 474 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 475 | { |
Jim Ingham | ce57983 | 2011-01-24 04:11:25 +0000 | [diff] [blame] | 476 | if ((*pos)->GetResumeState () != eStateSuspended) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 477 | { |
Jim Ingham | ce57983 | 2011-01-24 04:11:25 +0000 | [diff] [blame] | 478 | switch ((*pos)->ShouldReportRun (event_ptr)) |
| 479 | { |
| 480 | case eVoteNoOpinion: |
| 481 | continue; |
| 482 | case eVoteYes: |
| 483 | if (result == eVoteNoOpinion) |
| 484 | result = eVoteYes; |
| 485 | break; |
| 486 | case eVoteNo: |
Greg Clayton | abcbc8a | 2011-01-24 05:36:47 +0000 | [diff] [blame] | 487 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 488 | log->Printf ("ThreadList::ShouldReportRun() thread %d (0x%4.4" PRIx64 ") says don't report.", |
Greg Clayton | abcbc8a | 2011-01-24 05:36:47 +0000 | [diff] [blame] | 489 | (*pos)->GetIndexID(), |
| 490 | (*pos)->GetID()); |
Jim Ingham | ce57983 | 2011-01-24 04:11:25 +0000 | [diff] [blame] | 491 | result = eVoteNo; |
| 492 | break; |
| 493 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | return result; |
| 497 | } |
| 498 | |
| 499 | void |
| 500 | ThreadList::Clear() |
| 501 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 502 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 503 | m_stop_id = 0; |
| 504 | m_threads.clear(); |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 505 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 509 | ThreadList::Destroy() |
| 510 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 511 | Mutex::Locker locker(GetMutex()); |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 512 | const uint32_t num_threads = m_threads.size(); |
| 513 | for (uint32_t idx = 0; idx < num_threads; ++idx) |
| 514 | { |
| 515 | m_threads[idx]->DestroyThread(); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 520 | ThreadList::RefreshStateAfterStop () |
| 521 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 522 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 523 | |
| 524 | m_process->UpdateThreadListIfNeeded(); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 525 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 526 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 527 | if (log && log->GetVerbose()) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 528 | log->Printf ("Turning off notification of new threads while single stepping a thread."); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 529 | |
| 530 | collection::iterator pos, end = m_threads.end(); |
| 531 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 532 | (*pos)->RefreshStateAfterStop (); |
| 533 | } |
| 534 | |
| 535 | void |
| 536 | ThreadList::DiscardThreadPlans () |
| 537 | { |
| 538 | // You don't need to update the thread list here, because only threads |
| 539 | // that you currently know about have any thread plans. |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 540 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 541 | |
| 542 | collection::iterator pos, end = m_threads.end(); |
| 543 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 544 | (*pos)->DiscardThreadPlans (true); |
| 545 | |
| 546 | } |
| 547 | |
| 548 | bool |
| 549 | ThreadList::WillResume () |
| 550 | { |
| 551 | // Run through the threads and perform their momentary actions. |
| 552 | // But we only do this for threads that are running, user suspended |
| 553 | // threads stay where they are. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 554 | |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 555 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 556 | m_process->UpdateThreadListIfNeeded(); |
| 557 | |
| 558 | collection::iterator pos, end = m_threads.end(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 559 | |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 560 | // See if any thread wants to run stopping others. If it does, then we won't |
| 561 | // setup the other threads for resume, since they aren't going to get a chance |
| 562 | // to run. This is necessary because the SetupForResume might add "StopOthers" |
| 563 | // plans which would then get to be part of the who-gets-to-run negotiation, but |
| 564 | // they're coming in after the fact, and the threads that are already set up should |
| 565 | // take priority. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 566 | |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 567 | bool wants_solo_run = false; |
| 568 | |
| 569 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 570 | { |
Zachary Turner | 8f186f8 | 2015-11-13 21:53:03 +0000 | [diff] [blame] | 571 | lldbassert((*pos)->GetCurrentPlan() && "thread should not have null thread plan"); |
| 572 | if ((*pos)->GetResumeState() != eStateSuspended && |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 573 | (*pos)->GetCurrentPlan()->StopOthers()) |
| 574 | { |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 575 | if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread()) |
| 576 | continue; |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 577 | wants_solo_run = true; |
| 578 | break; |
| 579 | } |
| 580 | } |
| 581 | |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 582 | if (wants_solo_run) |
| 583 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 584 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 585 | if (log && log->GetVerbose()) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 586 | log->Printf ("Turning on notification of new threads while single stepping a thread."); |
| 587 | m_process->StartNoticingNewThreads(); |
| 588 | } |
| 589 | else |
| 590 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 591 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 592 | if (log && log->GetVerbose()) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 593 | log->Printf ("Turning off notification of new threads while single stepping a thread."); |
| 594 | m_process->StopNoticingNewThreads(); |
| 595 | } |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 596 | |
| 597 | // Give all the threads that are likely to run a last chance to set up their state before we |
| 598 | // negotiate who is actually going to get a chance to run... |
| 599 | // Don't set to resume suspended threads, and if any thread wanted to stop others, only |
| 600 | // call setup on the threads that request StopOthers... |
| 601 | |
| 602 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 603 | { |
| 604 | if ((*pos)->GetResumeState() != eStateSuspended |
| 605 | && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers())) |
| 606 | { |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 607 | if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread()) |
| 608 | continue; |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 609 | (*pos)->SetupForResume (); |
| 610 | } |
| 611 | } |
| 612 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 613 | // Now go through the threads and see if any thread wants to run just itself. |
| 614 | // if so then pick one and run it. |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 615 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 616 | ThreadList run_me_only_list (m_process); |
| 617 | |
| 618 | run_me_only_list.SetStopID(m_process->GetStopID()); |
| 619 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 620 | bool run_only_current_thread = false; |
| 621 | |
| 622 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 623 | { |
| 624 | ThreadSP thread_sp(*pos); |
Jim Ingham | b15bfc7 | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 625 | if (thread_sp->GetResumeState() != eStateSuspended && |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 626 | thread_sp->GetCurrentPlan()->StopOthers()) |
| 627 | { |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 628 | if ((*pos)->IsOperatingSystemPluginThread() && !(*pos)->GetBackingThread()) |
| 629 | continue; |
| 630 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 631 | // You can't say "stop others" and also want yourself to be suspended. |
| 632 | assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended); |
| 633 | |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 634 | if (thread_sp == GetSelectedThread()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 635 | { |
Jim Ingham | acbea8f | 2015-06-02 20:26:13 +0000 | [diff] [blame] | 636 | // If the currently selected thread wants to run on its own, always let it. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 637 | run_only_current_thread = true; |
| 638 | run_me_only_list.Clear(); |
| 639 | run_me_only_list.AddThread (thread_sp); |
| 640 | break; |
| 641 | } |
| 642 | |
| 643 | run_me_only_list.AddThread (thread_sp); |
| 644 | } |
| 645 | |
| 646 | } |
| 647 | |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 648 | bool need_to_resume = true; |
| 649 | |
Greg Clayton | 6e0ff1a | 2013-05-09 01:55:29 +0000 | [diff] [blame] | 650 | if (run_me_only_list.GetSize (false) == 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 651 | { |
| 652 | // Everybody runs as they wish: |
| 653 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 654 | { |
| 655 | ThreadSP thread_sp(*pos); |
Jim Ingham | cb5d5a5 | 2012-05-31 20:47:56 +0000 | [diff] [blame] | 656 | StateType run_state; |
| 657 | if (thread_sp->GetResumeState() != eStateSuspended) |
| 658 | run_state = thread_sp->GetCurrentPlan()->RunState(); |
| 659 | else |
| 660 | run_state = eStateSuspended; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 661 | if (!thread_sp->ShouldResume(run_state)) |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 662 | need_to_resume = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | else |
| 666 | { |
| 667 | ThreadSP thread_to_run; |
| 668 | |
| 669 | if (run_only_current_thread) |
| 670 | { |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 671 | thread_to_run = GetSelectedThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 672 | } |
| 673 | else if (run_me_only_list.GetSize (false) == 1) |
| 674 | { |
| 675 | thread_to_run = run_me_only_list.GetThreadAtIndex (0); |
| 676 | } |
| 677 | else |
| 678 | { |
| 679 | int random_thread = (int) |
| 680 | ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0)); |
| 681 | thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread); |
| 682 | } |
| 683 | |
| 684 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 685 | { |
| 686 | ThreadSP thread_sp(*pos); |
| 687 | if (thread_sp == thread_to_run) |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 688 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 689 | if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState())) |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 690 | need_to_resume = false; |
| 691 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 692 | else |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 693 | thread_sp->ShouldResume (eStateSuspended); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 697 | return need_to_resume; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | void |
| 701 | ThreadList::DidResume () |
| 702 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 703 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 704 | collection::iterator pos, end = m_threads.end(); |
| 705 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 706 | { |
| 707 | // Don't clear out threads that aren't going to get a chance to run, rather |
| 708 | // leave their state for the next time around. |
| 709 | ThreadSP thread_sp(*pos); |
| 710 | if (thread_sp->GetResumeState() != eStateSuspended) |
| 711 | thread_sp->DidResume (); |
| 712 | } |
| 713 | } |
| 714 | |
Andrew Kaylor | 29d6574 | 2013-05-10 17:19:04 +0000 | [diff] [blame] | 715 | void |
| 716 | ThreadList::DidStop () |
| 717 | { |
| 718 | Mutex::Locker locker(GetMutex()); |
| 719 | collection::iterator pos, end = m_threads.end(); |
| 720 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 721 | { |
| 722 | // Notify threads that the process just stopped. |
| 723 | // Note, this currently assumes that all threads in the list |
| 724 | // stop when the process stops. In the future we will want to support |
| 725 | // a debugging model where some threads continue to run while others |
| 726 | // are stopped. We either need to handle that somehow here or |
| 727 | // create a special thread list containing only threads which will |
| 728 | // stop in the code that calls this method (currently |
| 729 | // Process::SetPrivateState). |
| 730 | ThreadSP thread_sp(*pos); |
| 731 | if (StateIsRunningState(thread_sp->GetState())) |
| 732 | thread_sp->DidStop (); |
| 733 | } |
| 734 | } |
| 735 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 736 | ThreadSP |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 737 | ThreadList::GetSelectedThread () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 738 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 739 | Mutex::Locker locker(GetMutex()); |
Johnny Chen | 943ddb7 | 2011-08-25 21:59:59 +0000 | [diff] [blame] | 740 | ThreadSP thread_sp = FindThreadByID(m_selected_tid); |
| 741 | if (!thread_sp.get()) |
| 742 | { |
Jason Molenda | 354b9a6 | 2011-09-13 01:13:16 +0000 | [diff] [blame] | 743 | if (m_threads.size() == 0) |
| 744 | return thread_sp; |
Johnny Chen | 943ddb7 | 2011-08-25 21:59:59 +0000 | [diff] [blame] | 745 | m_selected_tid = m_threads[0]->GetID(); |
| 746 | thread_sp = m_threads[0]; |
| 747 | } |
| 748 | return thread_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | bool |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 752 | ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 753 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 754 | Mutex::Locker locker(GetMutex()); |
Jim Ingham | b7f6b2f | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 755 | ThreadSP selected_thread_sp(FindThreadByID(tid)); |
| 756 | if (selected_thread_sp) |
| 757 | { |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 758 | m_selected_tid = tid; |
Jim Ingham | b7f6b2f | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 759 | selected_thread_sp->SetDefaultFileAndLineToSelectedFrame(); |
| 760 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 761 | else |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 762 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 763 | |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 764 | if (notify) |
| 765 | NotifySelectedThreadChanged(m_selected_tid); |
| 766 | |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 767 | return m_selected_tid != LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | bool |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 771 | ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 772 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 773 | Mutex::Locker locker(GetMutex()); |
Jim Ingham | b7f6b2f | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 774 | ThreadSP selected_thread_sp (FindThreadByIndexID(index_id)); |
| 775 | if (selected_thread_sp.get()) |
| 776 | { |
| 777 | m_selected_tid = selected_thread_sp->GetID(); |
| 778 | selected_thread_sp->SetDefaultFileAndLineToSelectedFrame(); |
| 779 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 780 | else |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 781 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 782 | |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 783 | if (notify) |
| 784 | NotifySelectedThreadChanged(m_selected_tid); |
| 785 | |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 786 | return m_selected_tid != LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 789 | void |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 790 | ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid) |
| 791 | { |
| 792 | ThreadSP selected_thread_sp (FindThreadByID(tid)); |
| 793 | if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected)) |
| 794 | selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected, |
| 795 | new Thread::ThreadEventData(selected_thread_sp)); |
| 796 | } |
| 797 | |
| 798 | void |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 799 | ThreadList::Update (ThreadList &rhs) |
| 800 | { |
| 801 | if (this != &rhs) |
| 802 | { |
| 803 | // Lock both mutexes to make sure neither side changes anyone on us |
Bruce Mitchener | e171da5 | 2015-07-22 00:16:02 +0000 | [diff] [blame] | 804 | // while the assignment occurs |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 805 | Mutex::Locker locker(GetMutex()); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 806 | m_process = rhs.m_process; |
| 807 | m_stop_id = rhs.m_stop_id; |
| 808 | m_threads.swap(rhs.m_threads); |
| 809 | m_selected_tid = rhs.m_selected_tid; |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 810 | |
| 811 | |
| 812 | // Now we look for threads that we are done with and |
| 813 | // make sure to clear them up as much as possible so |
| 814 | // anyone with a shared pointer will still have a reference, |
| 815 | // but the thread won't be of much use. Using std::weak_ptr |
| 816 | // for all backward references (such as a thread to a process) |
| 817 | // will eventually solve this issue for us, but for now, we |
| 818 | // need to work around the issue |
| 819 | collection::iterator rhs_pos, rhs_end = rhs.m_threads.end(); |
| 820 | for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos) |
| 821 | { |
| 822 | const lldb::tid_t tid = (*rhs_pos)->GetID(); |
| 823 | bool thread_is_alive = false; |
| 824 | const uint32_t num_threads = m_threads.size(); |
| 825 | for (uint32_t idx = 0; idx < num_threads; ++idx) |
| 826 | { |
Ryan Brown | 65d4d5c | 2015-09-16 21:20:44 +0000 | [diff] [blame] | 827 | ThreadSP backing_thread = m_threads[idx]->GetBackingThread(); |
| 828 | if (m_threads[idx]->GetID() == tid || (backing_thread && backing_thread->GetID() == tid)) |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 829 | { |
| 830 | thread_is_alive = true; |
| 831 | break; |
| 832 | } |
| 833 | } |
| 834 | if (!thread_is_alive) |
| 835 | (*rhs_pos)->DestroyThread(); |
| 836 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 840 | void |
| 841 | ThreadList::Flush () |
| 842 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 843 | Mutex::Locker locker(GetMutex()); |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 844 | collection::iterator pos, end = m_threads.end(); |
| 845 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 846 | (*pos)->Flush (); |
| 847 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 848 | |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 849 | Mutex & |
| 850 | ThreadList::GetMutex () |
| 851 | { |
| 852 | return m_process->m_thread_mutex; |
| 853 | } |
| 854 | |
Jim Ingham | 8d94ba0 | 2016-03-12 02:45:34 +0000 | [diff] [blame^] | 855 | ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher (lldb::ThreadSP thread_sp) : |
| 856 | m_thread_list(nullptr), |
| 857 | m_tid(LLDB_INVALID_THREAD_ID) |
| 858 | { |
| 859 | if (thread_sp) |
| 860 | { |
| 861 | m_tid = thread_sp->GetID(); |
| 862 | m_thread_list = &thread_sp->GetProcess()->GetThreadList(); |
| 863 | m_thread_list->PushExpressionExecutionThread(m_tid); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | |