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