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