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