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