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