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