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" |
| 14 | #include "lldb/Target/RegisterContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "lldb/Target/ThreadList.h" |
| 16 | #include "lldb/Target/Thread.h" |
| 17 | #include "lldb/Target/ThreadPlan.h" |
| 18 | #include "lldb/Target/Process.h" |
| 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | |
| 23 | ThreadList::ThreadList (Process *process) : |
| 24 | m_process (process), |
| 25 | m_stop_id (0), |
| 26 | m_threads(), |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 27 | m_selected_tid (LLDB_INVALID_THREAD_ID) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | { |
| 29 | } |
| 30 | |
| 31 | ThreadList::ThreadList (const ThreadList &rhs) : |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 32 | m_process (rhs.m_process), |
| 33 | m_stop_id (rhs.m_stop_id), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | m_threads (), |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 35 | m_selected_tid () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | { |
| 37 | // Use the assignment operator since it uses the mutex |
| 38 | *this = rhs; |
| 39 | } |
| 40 | |
| 41 | const ThreadList& |
| 42 | ThreadList::operator = (const ThreadList& rhs) |
| 43 | { |
| 44 | if (this != &rhs) |
| 45 | { |
| 46 | // Lock both mutexes to make sure neither side changes anyone on us |
| 47 | // while the assignement occurs |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 48 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | m_process = rhs.m_process; |
| 50 | m_stop_id = rhs.m_stop_id; |
| 51 | m_threads = rhs.m_threads; |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 52 | m_selected_tid = rhs.m_selected_tid; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | } |
| 54 | return *this; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | ThreadList::~ThreadList() |
| 59 | { |
Greg Clayton | ac358da | 2013-03-28 18:33:53 +0000 | [diff] [blame] | 60 | // Clear the thread list. Clear will take the mutex lock |
| 61 | // which will ensure that if anyone is using the list |
| 62 | // they won't get it removed while using it. |
| 63 | Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
| 67 | uint32_t |
| 68 | ThreadList::GetStopID () const |
| 69 | { |
| 70 | return m_stop_id; |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | ThreadList::SetStopID (uint32_t stop_id) |
| 75 | { |
| 76 | m_stop_id = stop_id; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | void |
Greg Clayton | c3776bf | 2012-02-09 06:16:32 +0000 | [diff] [blame] | 81 | ThreadList::AddThread (const ThreadSP &thread_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 83 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | m_threads.push_back(thread_sp); |
| 85 | } |
| 86 | |
| 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, |
| 253 | // running breakpoint commands & conditions, etc. So we don't want |
| 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 |
| 256 | // to be created. If that happens we will miss asking them whether |
| 257 | // then should stop. This is not a big deal, since we haven't had |
| 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(); |
| 266 | threads_copy = m_threads; |
| 267 | } |
| 268 | |
| 269 | collection::iterator pos, end = threads_copy.end(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 270 | |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 271 | if (log) |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 272 | { |
| 273 | log->PutCString(""); |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 274 | log->Printf ("ThreadList::%s: %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size()); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 275 | } |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 276 | |
Jim Ingham | a007904 | 2013-03-21 21:46:56 +0000 | [diff] [blame] | 277 | bool did_anybody_stop_for_a_reason = false; |
Jim Ingham | 7bc3465 | 2013-04-16 22:53:24 +0000 | [diff] [blame] | 278 | bool should_stop = false; |
| 279 | |
| 280 | // Now we run through all the threads and get their stop info's. We want to make sure to do this first before |
| 281 | // we start running the ShouldStop, because one thread's ShouldStop could destroy information (like deleting a |
| 282 | // thread specific breakpoint another thread had stopped at) which could lead us to compute the StopInfo incorrectly. |
| 283 | // We don't need to use it here, we just want to make sure it gets computed. |
| 284 | |
| 285 | for (pos = threads_copy.begin(); pos != end; ++pos) |
| 286 | { |
| 287 | ThreadSP thread_sp(*pos); |
| 288 | thread_sp->GetStopInfo(); |
| 289 | } |
Jim Ingham | a007904 | 2013-03-21 21:46:56 +0000 | [diff] [blame] | 290 | |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 291 | for (pos = threads_copy.begin(); pos != end; ++pos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | { |
| 293 | ThreadSP thread_sp(*pos); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 294 | |
Jim Ingham | a007904 | 2013-03-21 21:46:56 +0000 | [diff] [blame] | 295 | did_anybody_stop_for_a_reason |= thread_sp->ThreadStoppedForAReason(); |
| 296 | |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 297 | const bool thread_should_stop = thread_sp->ShouldStop(event_ptr); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 298 | if (thread_should_stop) |
| 299 | should_stop |= true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 300 | } |
Jim Ingham | b01e742 | 2010-06-19 04:45:32 +0000 | [diff] [blame] | 301 | |
Jim Ingham | a007904 | 2013-03-21 21:46:56 +0000 | [diff] [blame] | 302 | // We should never get a stop for which no thread had a stop reason, but sometimes we do see this - |
| 303 | // for instance when we first connect to a remote stub. In that case we should stop, since we can't figure out |
| 304 | // the right thing to do and stopping gives the user control over what to do in this instance. |
| 305 | |
| 306 | if (!should_stop && !did_anybody_stop_for_a_reason) |
| 307 | { |
| 308 | should_stop = true; |
| 309 | if (log) |
| 310 | log->Printf ("ThreadList::%s we stopped but no threads had a stop reason, overriding should_stop and stopping.", __FUNCTION__); |
| 311 | } |
| 312 | |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 313 | if (log) |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 314 | log->Printf ("ThreadList::%s overall should_stop = %i", __FUNCTION__, should_stop); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 316 | if (should_stop) |
| 317 | { |
Jim Ingham | b42f3af | 2012-11-15 22:44:04 +0000 | [diff] [blame] | 318 | for (pos = threads_copy.begin(); pos != end; ++pos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 319 | { |
| 320 | ThreadSP thread_sp(*pos); |
| 321 | thread_sp->WillStop (); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return should_stop; |
| 326 | } |
| 327 | |
| 328 | Vote |
| 329 | ThreadList::ShouldReportStop (Event *event_ptr) |
| 330 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 331 | Mutex::Locker locker(GetMutex()); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 332 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 333 | Vote result = eVoteNoOpinion; |
| 334 | m_process->UpdateThreadListIfNeeded(); |
| 335 | collection::iterator pos, end = m_threads.end(); |
| 336 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 337 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 338 | |
| 339 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 340 | log->Printf ("ThreadList::%s %" PRIu64 " threads", __FUNCTION__, (uint64_t)m_threads.size()); |
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 | // Run through the threads and ask whether we should report this event. |
| 343 | // For stopping, a YES vote wins over everything. A NO vote wins over NO opinion. |
| 344 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 345 | { |
| 346 | ThreadSP thread_sp(*pos); |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 347 | const Vote vote = thread_sp->ShouldReportStop (event_ptr); |
| 348 | switch (vote) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 349 | { |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 350 | case eVoteNoOpinion: |
| 351 | continue; |
| 352 | |
| 353 | case eVoteYes: |
| 354 | result = eVoteYes; |
| 355 | break; |
| 356 | |
| 357 | case eVoteNo: |
| 358 | if (result == eVoteNoOpinion) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 359 | { |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 360 | result = eVoteNo; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 361 | } |
Jim Ingham | 92087d8 | 2012-01-31 23:09:20 +0000 | [diff] [blame] | 362 | else |
| 363 | { |
| 364 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 365 | 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] | 366 | __FUNCTION__, |
| 367 | thread_sp->GetID (), |
| 368 | GetVoteAsCString (vote), |
| 369 | GetVoteAsCString (result)); |
| 370 | } |
| 371 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 374 | if (log) |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 375 | log->Printf ("ThreadList::%s returning %s", __FUNCTION__, GetVoteAsCString (result)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 376 | return result; |
| 377 | } |
| 378 | |
Jim Ingham | 221d51c | 2013-05-08 00:35:16 +0000 | [diff] [blame^] | 379 | void |
| 380 | ThreadList::SetShouldReportStop (Vote vote) |
| 381 | { |
| 382 | Mutex::Locker locker(GetMutex()); |
| 383 | m_process->UpdateThreadListIfNeeded(); |
| 384 | collection::iterator pos, end = m_threads.end(); |
| 385 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 386 | { |
| 387 | ThreadSP thread_sp(*pos); |
| 388 | thread_sp->SetShouldReportStop (vote); |
| 389 | } |
| 390 | } |
| 391 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 392 | Vote |
| 393 | ThreadList::ShouldReportRun (Event *event_ptr) |
| 394 | { |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 395 | |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 396 | Mutex::Locker locker(GetMutex()); |
Greg Clayton | 2cad65a | 2010-09-03 17:10:42 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 398 | Vote result = eVoteNoOpinion; |
| 399 | m_process->UpdateThreadListIfNeeded(); |
| 400 | collection::iterator pos, end = m_threads.end(); |
| 401 | |
| 402 | // Run through the threads and ask whether we should report this event. |
| 403 | // The rule is NO vote wins over everything, a YES vote wins over no opinion. |
| 404 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 405 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | ce57983 | 2011-01-24 04:11:25 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 407 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 408 | { |
Jim Ingham | ce57983 | 2011-01-24 04:11:25 +0000 | [diff] [blame] | 409 | if ((*pos)->GetResumeState () != eStateSuspended) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 410 | { |
Jim Ingham | ce57983 | 2011-01-24 04:11:25 +0000 | [diff] [blame] | 411 | switch ((*pos)->ShouldReportRun (event_ptr)) |
| 412 | { |
| 413 | case eVoteNoOpinion: |
| 414 | continue; |
| 415 | case eVoteYes: |
| 416 | if (result == eVoteNoOpinion) |
| 417 | result = eVoteYes; |
| 418 | break; |
| 419 | case eVoteNo: |
Greg Clayton | abcbc8a | 2011-01-24 05:36:47 +0000 | [diff] [blame] | 420 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 421 | 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] | 422 | (*pos)->GetIndexID(), |
| 423 | (*pos)->GetID()); |
Jim Ingham | ce57983 | 2011-01-24 04:11:25 +0000 | [diff] [blame] | 424 | result = eVoteNo; |
| 425 | break; |
| 426 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | return result; |
| 430 | } |
| 431 | |
| 432 | void |
| 433 | ThreadList::Clear() |
| 434 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 435 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 436 | m_stop_id = 0; |
| 437 | m_threads.clear(); |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 438 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 442 | ThreadList::Destroy() |
| 443 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 444 | Mutex::Locker locker(GetMutex()); |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 445 | const uint32_t num_threads = m_threads.size(); |
| 446 | for (uint32_t idx = 0; idx < num_threads; ++idx) |
| 447 | { |
| 448 | m_threads[idx]->DestroyThread(); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 453 | ThreadList::RefreshStateAfterStop () |
| 454 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 455 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 456 | |
| 457 | m_process->UpdateThreadListIfNeeded(); |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 458 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 459 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 460 | if (log && log->GetVerbose()) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 461 | 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] | 462 | |
| 463 | collection::iterator pos, end = m_threads.end(); |
| 464 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 465 | (*pos)->RefreshStateAfterStop (); |
| 466 | } |
| 467 | |
| 468 | void |
| 469 | ThreadList::DiscardThreadPlans () |
| 470 | { |
| 471 | // You don't need to update the thread list here, because only threads |
| 472 | // that you currently know about have any thread plans. |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 473 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 474 | |
| 475 | collection::iterator pos, end = m_threads.end(); |
| 476 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 477 | (*pos)->DiscardThreadPlans (true); |
| 478 | |
| 479 | } |
| 480 | |
| 481 | bool |
| 482 | ThreadList::WillResume () |
| 483 | { |
| 484 | // Run through the threads and perform their momentary actions. |
| 485 | // But we only do this for threads that are running, user suspended |
| 486 | // threads stay where they are. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | m_process->UpdateThreadListIfNeeded(); |
| 490 | |
| 491 | collection::iterator pos, end = m_threads.end(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 492 | |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 493 | // See if any thread wants to run stopping others. If it does, then we won't |
| 494 | // setup the other threads for resume, since they aren't going to get a chance |
| 495 | // to run. This is necessary because the SetupForResume might add "StopOthers" |
| 496 | // plans which would then get to be part of the who-gets-to-run negotiation, but |
| 497 | // they're coming in after the fact, and the threads that are already set up should |
| 498 | // take priority. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 499 | |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 500 | bool wants_solo_run = false; |
| 501 | |
| 502 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 503 | { |
| 504 | if ((*pos)->GetResumeState() != eStateSuspended && |
| 505 | (*pos)->GetCurrentPlan()->StopOthers()) |
| 506 | { |
| 507 | wants_solo_run = true; |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 512 | if (wants_solo_run) |
| 513 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 514 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 515 | if (log && log->GetVerbose()) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 516 | log->Printf ("Turning on notification of new threads while single stepping a thread."); |
| 517 | m_process->StartNoticingNewThreads(); |
| 518 | } |
| 519 | else |
| 520 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 521 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); |
Jim Ingham | 10c4b24 | 2011-10-15 00:23:43 +0000 | [diff] [blame] | 522 | if (log && log->GetVerbose()) |
Jim Ingham | 1c823b4 | 2011-01-22 01:33:44 +0000 | [diff] [blame] | 523 | log->Printf ("Turning off notification of new threads while single stepping a thread."); |
| 524 | m_process->StopNoticingNewThreads(); |
| 525 | } |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 526 | |
| 527 | // Give all the threads that are likely to run a last chance to set up their state before we |
| 528 | // negotiate who is actually going to get a chance to run... |
| 529 | // Don't set to resume suspended threads, and if any thread wanted to stop others, only |
| 530 | // call setup on the threads that request StopOthers... |
| 531 | |
| 532 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 533 | { |
| 534 | if ((*pos)->GetResumeState() != eStateSuspended |
| 535 | && (!wants_solo_run || (*pos)->GetCurrentPlan()->StopOthers())) |
| 536 | { |
| 537 | (*pos)->SetupForResume (); |
| 538 | } |
| 539 | } |
| 540 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 541 | // Now go through the threads and see if any thread wants to run just itself. |
| 542 | // if so then pick one and run it. |
Jim Ingham | a3241c1 | 2010-07-14 02:27:20 +0000 | [diff] [blame] | 543 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 544 | ThreadList run_me_only_list (m_process); |
| 545 | |
| 546 | run_me_only_list.SetStopID(m_process->GetStopID()); |
| 547 | |
| 548 | ThreadSP immediate_thread_sp; |
| 549 | bool run_only_current_thread = false; |
| 550 | |
| 551 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 552 | { |
| 553 | ThreadSP thread_sp(*pos); |
Jim Ingham | b15bfc7 | 2010-10-20 00:39:53 +0000 | [diff] [blame] | 554 | if (thread_sp->GetResumeState() != eStateSuspended && |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 555 | thread_sp->GetCurrentPlan()->StopOthers()) |
| 556 | { |
| 557 | // You can't say "stop others" and also want yourself to be suspended. |
| 558 | assert (thread_sp->GetCurrentPlan()->RunState() != eStateSuspended); |
| 559 | |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 560 | if (thread_sp == GetSelectedThread()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 561 | { |
| 562 | run_only_current_thread = true; |
| 563 | run_me_only_list.Clear(); |
| 564 | run_me_only_list.AddThread (thread_sp); |
| 565 | break; |
| 566 | } |
| 567 | |
| 568 | run_me_only_list.AddThread (thread_sp); |
| 569 | } |
| 570 | |
| 571 | } |
| 572 | |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 573 | bool need_to_resume = true; |
| 574 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 575 | if (immediate_thread_sp) |
| 576 | { |
| 577 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 578 | { |
| 579 | ThreadSP thread_sp(*pos); |
| 580 | if (thread_sp.get() == immediate_thread_sp.get()) |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 581 | thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 582 | else |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 583 | thread_sp->ShouldResume (eStateSuspended); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | else if (run_me_only_list.GetSize (false) == 0) |
| 587 | { |
| 588 | // Everybody runs as they wish: |
| 589 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 590 | { |
| 591 | ThreadSP thread_sp(*pos); |
Jim Ingham | cb5d5a5 | 2012-05-31 20:47:56 +0000 | [diff] [blame] | 592 | StateType run_state; |
| 593 | if (thread_sp->GetResumeState() != eStateSuspended) |
| 594 | run_state = thread_sp->GetCurrentPlan()->RunState(); |
| 595 | else |
| 596 | run_state = eStateSuspended; |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 597 | if (!thread_sp->ShouldResume(run_state)) |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 598 | need_to_resume = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | else |
| 602 | { |
| 603 | ThreadSP thread_to_run; |
| 604 | |
| 605 | if (run_only_current_thread) |
| 606 | { |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 607 | thread_to_run = GetSelectedThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 608 | } |
| 609 | else if (run_me_only_list.GetSize (false) == 1) |
| 610 | { |
| 611 | thread_to_run = run_me_only_list.GetThreadAtIndex (0); |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | int random_thread = (int) |
| 616 | ((run_me_only_list.GetSize (false) * (double) rand ()) / (RAND_MAX + 1.0)); |
| 617 | thread_to_run = run_me_only_list.GetThreadAtIndex (random_thread); |
| 618 | } |
| 619 | |
| 620 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 621 | { |
| 622 | ThreadSP thread_sp(*pos); |
| 623 | if (thread_sp == thread_to_run) |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 624 | { |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 625 | if (!thread_sp->ShouldResume(thread_sp->GetCurrentPlan()->RunState())) |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 626 | need_to_resume = false; |
| 627 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 628 | else |
Greg Clayton | 160c9d8 | 2013-05-01 21:54:04 +0000 | [diff] [blame] | 629 | thread_sp->ShouldResume (eStateSuspended); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Jim Ingham | 513c6bb | 2012-09-01 01:02:41 +0000 | [diff] [blame] | 633 | return need_to_resume; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | void |
| 637 | ThreadList::DidResume () |
| 638 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 639 | Mutex::Locker locker(GetMutex()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 640 | collection::iterator pos, end = m_threads.end(); |
| 641 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 642 | { |
| 643 | // Don't clear out threads that aren't going to get a chance to run, rather |
| 644 | // leave their state for the next time around. |
| 645 | ThreadSP thread_sp(*pos); |
| 646 | if (thread_sp->GetResumeState() != eStateSuspended) |
| 647 | thread_sp->DidResume (); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | ThreadSP |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 652 | ThreadList::GetSelectedThread () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 653 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 654 | Mutex::Locker locker(GetMutex()); |
Johnny Chen | 943ddb7 | 2011-08-25 21:59:59 +0000 | [diff] [blame] | 655 | ThreadSP thread_sp = FindThreadByID(m_selected_tid); |
| 656 | if (!thread_sp.get()) |
| 657 | { |
Jason Molenda | 354b9a6 | 2011-09-13 01:13:16 +0000 | [diff] [blame] | 658 | if (m_threads.size() == 0) |
| 659 | return thread_sp; |
Johnny Chen | 943ddb7 | 2011-08-25 21:59:59 +0000 | [diff] [blame] | 660 | m_selected_tid = m_threads[0]->GetID(); |
| 661 | thread_sp = m_threads[0]; |
| 662 | } |
| 663 | return thread_sp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | bool |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 667 | ThreadList::SetSelectedThreadByID (lldb::tid_t tid, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 668 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 669 | Mutex::Locker locker(GetMutex()); |
Jim Ingham | b7f6b2f | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 670 | ThreadSP selected_thread_sp(FindThreadByID(tid)); |
| 671 | if (selected_thread_sp) |
| 672 | { |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 673 | m_selected_tid = tid; |
Jim Ingham | b7f6b2f | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 674 | selected_thread_sp->SetDefaultFileAndLineToSelectedFrame(); |
| 675 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 676 | else |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 677 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 678 | |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 679 | if (notify) |
| 680 | NotifySelectedThreadChanged(m_selected_tid); |
| 681 | |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 682 | return m_selected_tid != LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | bool |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 686 | ThreadList::SetSelectedThreadByIndexID (uint32_t index_id, bool notify) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 687 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 688 | Mutex::Locker locker(GetMutex()); |
Jim Ingham | b7f6b2f | 2011-09-08 22:13:49 +0000 | [diff] [blame] | 689 | ThreadSP selected_thread_sp (FindThreadByIndexID(index_id)); |
| 690 | if (selected_thread_sp.get()) |
| 691 | { |
| 692 | m_selected_tid = selected_thread_sp->GetID(); |
| 693 | selected_thread_sp->SetDefaultFileAndLineToSelectedFrame(); |
| 694 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 695 | else |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 696 | m_selected_tid = LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 697 | |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 698 | if (notify) |
| 699 | NotifySelectedThreadChanged(m_selected_tid); |
| 700 | |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 701 | return m_selected_tid != LLDB_INVALID_THREAD_ID; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 704 | void |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 705 | ThreadList::NotifySelectedThreadChanged (lldb::tid_t tid) |
| 706 | { |
| 707 | ThreadSP selected_thread_sp (FindThreadByID(tid)); |
| 708 | if (selected_thread_sp->EventTypeHasListeners(Thread::eBroadcastBitThreadSelected)) |
| 709 | selected_thread_sp->BroadcastEvent(Thread::eBroadcastBitThreadSelected, |
| 710 | new Thread::ThreadEventData(selected_thread_sp)); |
| 711 | } |
| 712 | |
| 713 | void |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 714 | ThreadList::Update (ThreadList &rhs) |
| 715 | { |
| 716 | if (this != &rhs) |
| 717 | { |
| 718 | // Lock both mutexes to make sure neither side changes anyone on us |
| 719 | // while the assignement occurs |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 720 | Mutex::Locker locker(GetMutex()); |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 721 | m_process = rhs.m_process; |
| 722 | m_stop_id = rhs.m_stop_id; |
| 723 | m_threads.swap(rhs.m_threads); |
| 724 | m_selected_tid = rhs.m_selected_tid; |
Greg Clayton | e1cd1be | 2012-01-29 20:56:30 +0000 | [diff] [blame] | 725 | |
| 726 | |
| 727 | // Now we look for threads that we are done with and |
| 728 | // make sure to clear them up as much as possible so |
| 729 | // anyone with a shared pointer will still have a reference, |
| 730 | // but the thread won't be of much use. Using std::weak_ptr |
| 731 | // for all backward references (such as a thread to a process) |
| 732 | // will eventually solve this issue for us, but for now, we |
| 733 | // need to work around the issue |
| 734 | collection::iterator rhs_pos, rhs_end = rhs.m_threads.end(); |
| 735 | for (rhs_pos = rhs.m_threads.begin(); rhs_pos != rhs_end; ++rhs_pos) |
| 736 | { |
| 737 | const lldb::tid_t tid = (*rhs_pos)->GetID(); |
| 738 | bool thread_is_alive = false; |
| 739 | const uint32_t num_threads = m_threads.size(); |
| 740 | for (uint32_t idx = 0; idx < num_threads; ++idx) |
| 741 | { |
| 742 | if (m_threads[idx]->GetID() == tid) |
| 743 | { |
| 744 | thread_is_alive = true; |
| 745 | break; |
| 746 | } |
| 747 | } |
| 748 | if (!thread_is_alive) |
| 749 | (*rhs_pos)->DestroyThread(); |
| 750 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 754 | void |
| 755 | ThreadList::Flush () |
| 756 | { |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 757 | Mutex::Locker locker(GetMutex()); |
Greg Clayton | fa559e5 | 2012-05-18 02:38:05 +0000 | [diff] [blame] | 758 | collection::iterator pos, end = m_threads.end(); |
| 759 | for (pos = m_threads.begin(); pos != end; ++pos) |
| 760 | (*pos)->Flush (); |
| 761 | } |
Greg Clayton | 56d9a1b | 2011-08-22 02:49:39 +0000 | [diff] [blame] | 762 | |
Andrew Kaylor | ba4e61d | 2013-05-07 18:35:34 +0000 | [diff] [blame] | 763 | Mutex & |
| 764 | ThreadList::GetMutex () |
| 765 | { |
| 766 | return m_process->m_thread_mutex; |
| 767 | } |
| 768 | |