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