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