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