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