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