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