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