1 //===-- Thread.cpp ----------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/lldb-python.h" 11 12 #include "lldb/lldb-private-log.h" 13 #include "lldb/Breakpoint/BreakpointLocation.h" 14 #include "lldb/Core/Debugger.h" 15 #include "lldb/Core/Log.h" 16 #include "lldb/Core/Stream.h" 17 #include "lldb/Core/StreamString.h" 18 #include "lldb/Core/RegularExpression.h" 19 #include "lldb/Host/Host.h" 20 #include "lldb/Symbol/Function.h" 21 #include "lldb/Target/DynamicLoader.h" 22 #include "lldb/Target/ExecutionContext.h" 23 #include "lldb/Target/ObjCLanguageRuntime.h" 24 #include "lldb/Target/Process.h" 25 #include "lldb/Target/RegisterContext.h" 26 #include "lldb/Target/StopInfo.h" 27 #include "lldb/Target/Target.h" 28 #include "lldb/Target/Thread.h" 29 #include "lldb/Target/ThreadPlan.h" 30 #include "lldb/Target/ThreadPlanCallFunction.h" 31 #include "lldb/Target/ThreadPlanBase.h" 32 #include "lldb/Target/ThreadPlanStepInstruction.h" 33 #include "lldb/Target/ThreadPlanStepOut.h" 34 #include "lldb/Target/ThreadPlanStepOverBreakpoint.h" 35 #include "lldb/Target/ThreadPlanStepThrough.h" 36 #include "lldb/Target/ThreadPlanStepInRange.h" 37 #include "lldb/Target/ThreadPlanStepOverRange.h" 38 #include "lldb/Target/ThreadPlanRunToAddress.h" 39 #include "lldb/Target/ThreadPlanStepUntil.h" 40 #include "lldb/Target/ThreadSpec.h" 41 #include "lldb/Target/Unwind.h" 42 #include "Plugins/Process/Utility/UnwindLLDB.h" 43 #include "UnwindMacOSXFrameBackchain.h" 44 45 46 using namespace lldb; 47 using namespace lldb_private; 48 49 50 const ThreadPropertiesSP & 51 Thread::GetGlobalProperties() 52 { 53 static ThreadPropertiesSP g_settings_sp; 54 if (!g_settings_sp) 55 g_settings_sp.reset (new ThreadProperties (true)); 56 return g_settings_sp; 57 } 58 59 static PropertyDefinition 60 g_properties[] = 61 { 62 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." }, 63 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." }, 64 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL } 65 }; 66 67 enum { 68 ePropertyStepAvoidRegex, 69 ePropertyEnableThreadTrace 70 }; 71 72 73 class ThreadOptionValueProperties : public OptionValueProperties 74 { 75 public: 76 ThreadOptionValueProperties (const ConstString &name) : 77 OptionValueProperties (name) 78 { 79 } 80 81 // This constructor is used when creating ThreadOptionValueProperties when it 82 // is part of a new lldb_private::Thread instance. It will copy all current 83 // global property values as needed 84 ThreadOptionValueProperties (ThreadProperties *global_properties) : 85 OptionValueProperties(*global_properties->GetValueProperties()) 86 { 87 } 88 89 virtual const Property * 90 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const 91 { 92 // When gettings the value for a key from the thread options, we will always 93 // try and grab the setting from the current thread if there is one. Else we just 94 // use the one from this instance. 95 if (exe_ctx) 96 { 97 Thread *thread = exe_ctx->GetThreadPtr(); 98 if (thread) 99 { 100 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get()); 101 if (this != instance_properties) 102 return instance_properties->ProtectedGetPropertyAtIndex (idx); 103 } 104 } 105 return ProtectedGetPropertyAtIndex (idx); 106 } 107 }; 108 109 110 111 ThreadProperties::ThreadProperties (bool is_global) : 112 Properties () 113 { 114 if (is_global) 115 { 116 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread"))); 117 m_collection_sp->Initialize(g_properties); 118 } 119 else 120 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get())); 121 } 122 123 ThreadProperties::~ThreadProperties() 124 { 125 } 126 127 const RegularExpression * 128 ThreadProperties::GetSymbolsToAvoidRegexp() 129 { 130 const uint32_t idx = ePropertyStepAvoidRegex; 131 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx); 132 } 133 134 bool 135 ThreadProperties::GetTraceEnabledState() const 136 { 137 const uint32_t idx = ePropertyEnableThreadTrace; 138 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 139 } 140 141 //------------------------------------------------------------------ 142 // Thread Event Data 143 //------------------------------------------------------------------ 144 145 146 const ConstString & 147 Thread::ThreadEventData::GetFlavorString () 148 { 149 static ConstString g_flavor ("Thread::ThreadEventData"); 150 return g_flavor; 151 } 152 153 Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) : 154 m_thread_sp (thread_sp), 155 m_stack_id () 156 { 157 } 158 159 Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) : 160 m_thread_sp (thread_sp), 161 m_stack_id (stack_id) 162 { 163 } 164 165 Thread::ThreadEventData::ThreadEventData () : 166 m_thread_sp (), 167 m_stack_id () 168 { 169 } 170 171 Thread::ThreadEventData::~ThreadEventData () 172 { 173 } 174 175 void 176 Thread::ThreadEventData::Dump (Stream *s) const 177 { 178 179 } 180 181 const Thread::ThreadEventData * 182 Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr) 183 { 184 if (event_ptr) 185 { 186 const EventData *event_data = event_ptr->GetData(); 187 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString()) 188 return static_cast <const ThreadEventData *> (event_ptr->GetData()); 189 } 190 return NULL; 191 } 192 193 ThreadSP 194 Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr) 195 { 196 ThreadSP thread_sp; 197 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr); 198 if (event_data) 199 thread_sp = event_data->GetThread(); 200 return thread_sp; 201 } 202 203 StackID 204 Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr) 205 { 206 StackID stack_id; 207 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr); 208 if (event_data) 209 stack_id = event_data->GetStackID(); 210 return stack_id; 211 } 212 213 StackFrameSP 214 Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr) 215 { 216 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr); 217 StackFrameSP frame_sp; 218 if (event_data) 219 { 220 ThreadSP thread_sp = event_data->GetThread(); 221 if (thread_sp) 222 { 223 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID()); 224 } 225 } 226 return frame_sp; 227 } 228 229 //------------------------------------------------------------------ 230 // Thread class 231 //------------------------------------------------------------------ 232 233 ConstString & 234 Thread::GetStaticBroadcasterClass () 235 { 236 static ConstString class_name ("lldb.thread"); 237 return class_name; 238 } 239 240 Thread::Thread (Process &process, lldb::tid_t tid) : 241 ThreadProperties (false), 242 UserID (tid), 243 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()), 244 m_process_wp (process.shared_from_this()), 245 m_actual_stop_info_sp (), 246 m_index_id (process.GetNextThreadIndexID(tid)), 247 m_reg_context_sp (), 248 m_state (eStateUnloaded), 249 m_state_mutex (Mutex::eMutexTypeRecursive), 250 m_plan_stack (), 251 m_completed_plan_stack(), 252 m_frame_mutex (Mutex::eMutexTypeRecursive), 253 m_curr_frames_sp (), 254 m_prev_frames_sp (), 255 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER), 256 m_resume_state (eStateRunning), 257 m_temporary_resume_state (eStateRunning), 258 m_unwinder_ap (), 259 m_destroy_called (false), 260 m_thread_stop_reason_stop_id (0) 261 { 262 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 263 if (log) 264 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")", this, GetID()); 265 266 CheckInWithManager(); 267 QueueFundamentalPlan(true); 268 } 269 270 271 Thread::~Thread() 272 { 273 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 274 if (log) 275 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")", this, GetID()); 276 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor. 277 assert (m_destroy_called); 278 } 279 280 void 281 Thread::DestroyThread () 282 { 283 m_destroy_called = true; 284 m_plan_stack.clear(); 285 m_discarded_plan_stack.clear(); 286 m_completed_plan_stack.clear(); 287 m_actual_stop_info_sp.reset(); 288 m_reg_context_sp.reset(); 289 m_unwinder_ap.reset(); 290 Mutex::Locker locker(m_frame_mutex); 291 m_curr_frames_sp.reset(); 292 m_prev_frames_sp.reset(); 293 } 294 295 void 296 Thread::BroadcastSelectedFrameChange(StackID &new_frame_id) 297 { 298 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged)) 299 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id)); 300 } 301 302 uint32_t 303 Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast) 304 { 305 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame); 306 if (broadcast) 307 BroadcastSelectedFrameChange(frame->GetStackID()); 308 return ret_value; 309 } 310 311 bool 312 Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast) 313 { 314 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx)); 315 if (frame_sp) 316 { 317 GetStackFrameList()->SetSelectedFrame(frame_sp.get()); 318 if (broadcast) 319 BroadcastSelectedFrameChange(frame_sp->GetStackID()); 320 return true; 321 } 322 else 323 return false; 324 } 325 326 bool 327 Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream) 328 { 329 const bool broadcast = true; 330 bool success = SetSelectedFrameByIndex (frame_idx, broadcast); 331 if (success) 332 { 333 StackFrameSP frame_sp = GetSelectedFrame(); 334 if (frame_sp) 335 { 336 bool already_shown = false; 337 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry)); 338 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0) 339 { 340 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line); 341 } 342 343 bool show_frame_info = true; 344 bool show_source = !already_shown; 345 return frame_sp->GetStatus (output_stream, show_frame_info, show_source); 346 } 347 return false; 348 } 349 else 350 return false; 351 } 352 353 354 lldb::StopInfoSP 355 Thread::GetStopInfo () 356 { 357 ThreadPlanSP plan_sp (GetCompletedPlan()); 358 if (plan_sp && plan_sp->PlanSucceeded()) 359 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject()); 360 else 361 { 362 ProcessSP process_sp (GetProcess()); 363 if (process_sp 364 && m_actual_stop_info_sp 365 && m_actual_stop_info_sp->IsValid() 366 && m_thread_stop_reason_stop_id == process_sp->GetStopID()) 367 return m_actual_stop_info_sp; 368 else 369 return GetPrivateStopReason (); 370 } 371 } 372 373 lldb::StopReason 374 Thread::GetStopReason() 375 { 376 lldb::StopInfoSP stop_info_sp (GetStopInfo ()); 377 if (stop_info_sp) 378 return stop_info_sp->GetStopReason(); 379 return eStopReasonNone; 380 } 381 382 383 384 void 385 Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp) 386 { 387 m_actual_stop_info_sp = stop_info_sp; 388 if (m_actual_stop_info_sp) 389 m_actual_stop_info_sp->MakeStopInfoValid(); 390 ProcessSP process_sp (GetProcess()); 391 if (process_sp) 392 m_thread_stop_reason_stop_id = process_sp->GetStopID(); 393 else 394 m_thread_stop_reason_stop_id = UINT32_MAX; 395 } 396 397 void 398 Thread::SetStopInfoToNothing() 399 { 400 // Note, we can't just NULL out the private reason, or the native thread implementation will try to 401 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number. 402 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER)); 403 } 404 405 bool 406 Thread::ThreadStoppedForAReason (void) 407 { 408 return (bool) GetPrivateStopReason (); 409 } 410 411 bool 412 Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state) 413 { 414 if (!SaveFrameZeroState(saved_state.register_backup)) 415 return false; 416 417 saved_state.stop_info_sp = GetStopInfo(); 418 ProcessSP process_sp (GetProcess()); 419 if (process_sp) 420 saved_state.orig_stop_id = process_sp->GetStopID(); 421 saved_state.current_inlined_depth = GetCurrentInlinedDepth(); 422 423 return true; 424 } 425 426 bool 427 Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state) 428 { 429 RestoreSaveFrameZero(saved_state.register_backup); 430 return true; 431 } 432 433 bool 434 Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state) 435 { 436 if (saved_state.stop_info_sp) 437 saved_state.stop_info_sp->MakeStopInfoValid(); 438 SetStopInfo(saved_state.stop_info_sp); 439 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth); 440 return true; 441 } 442 443 StateType 444 Thread::GetState() const 445 { 446 // If any other threads access this we will need a mutex for it 447 Mutex::Locker locker(m_state_mutex); 448 return m_state; 449 } 450 451 void 452 Thread::SetState(StateType state) 453 { 454 Mutex::Locker locker(m_state_mutex); 455 m_state = state; 456 } 457 458 void 459 Thread::WillStop() 460 { 461 ThreadPlan *current_plan = GetCurrentPlan(); 462 463 // FIXME: I may decide to disallow threads with no plans. In which 464 // case this should go to an assert. 465 466 if (!current_plan) 467 return; 468 469 current_plan->WillStop(); 470 } 471 472 void 473 Thread::SetupForResume () 474 { 475 if (GetResumeState() != eStateSuspended) 476 { 477 478 // If we're at a breakpoint push the step-over breakpoint plan. Do this before 479 // telling the current plan it will resume, since we might change what the current 480 // plan is. 481 482 StopReason stop_reason = lldb::eStopReasonInvalid; 483 StopInfoSP stop_info_sp = GetStopInfo(); 484 if (stop_info_sp.get()) 485 stop_reason = stop_info_sp->GetStopReason(); 486 if (stop_reason == lldb::eStopReasonBreakpoint) 487 { 488 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything 489 // special to step over a breakpoint. 490 491 ThreadPlan *cur_plan = GetCurrentPlan(); 492 493 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint) 494 { 495 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this); 496 if (step_bp_plan) 497 { 498 ThreadPlanSP step_bp_plan_sp; 499 step_bp_plan->SetPrivate (true); 500 501 if (GetCurrentPlan()->RunState() != eStateStepping) 502 { 503 step_bp_plan->SetAutoContinue(true); 504 } 505 step_bp_plan_sp.reset (step_bp_plan); 506 QueueThreadPlan (step_bp_plan_sp, false); 507 } 508 } 509 } 510 } 511 } 512 513 bool 514 Thread::WillResume (StateType resume_state) 515 { 516 // At this point clear the completed plan stack. 517 m_completed_plan_stack.clear(); 518 m_discarded_plan_stack.clear(); 519 520 m_temporary_resume_state = resume_state; 521 522 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from 523 // the target, 'cause that slows down single stepping. So assume that if we got to the point where 524 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know 525 // about the fact that we are resuming... 526 const uint32_t process_stop_id = GetProcess()->GetStopID(); 527 if (m_thread_stop_reason_stop_id == process_stop_id && 528 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid())) 529 { 530 StopInfo *stop_info = GetPrivateStopReason().get(); 531 if (stop_info) 532 stop_info->WillResume (resume_state); 533 } 534 535 // Tell all the plans that we are about to resume in case they need to clear any state. 536 // We distinguish between the plan on the top of the stack and the lower 537 // plans in case a plan needs to do any special business before it runs. 538 539 ThreadPlan *plan_ptr = GetCurrentPlan(); 540 bool need_to_resume = plan_ptr->WillResume(resume_state, true); 541 542 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL) 543 { 544 plan_ptr->WillResume (resume_state, false); 545 } 546 547 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info. 548 // In that case, don't reset it here. 549 550 if (need_to_resume && resume_state != eStateSuspended) 551 { 552 m_actual_stop_info_sp.reset(); 553 } 554 555 return need_to_resume; 556 } 557 558 void 559 Thread::DidResume () 560 { 561 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER); 562 } 563 564 bool 565 Thread::ShouldStop (Event* event_ptr) 566 { 567 ThreadPlan *current_plan = GetCurrentPlan(); 568 bool should_stop = true; 569 570 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 571 572 if (GetResumeState () == eStateSuspended) 573 { 574 if (log) 575 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)", 576 __FUNCTION__, 577 GetID ()); 578 // log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since thread was suspended)", 579 // __FUNCTION__, 580 // GetID (), 581 // GetRegisterContext()->GetPC()); 582 return false; 583 } 584 585 if (GetTemporaryResumeState () == eStateSuspended) 586 { 587 if (log) 588 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)", 589 __FUNCTION__, 590 GetID ()); 591 // log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since thread was suspended)", 592 // __FUNCTION__, 593 // GetID (), 594 // GetRegisterContext()->GetPC()); 595 return false; 596 } 597 598 if (ThreadStoppedForAReason() == false) 599 { 600 if (log) 601 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since no stop reason)", 602 __FUNCTION__, 603 GetID (), 604 GetRegisterContext()->GetPC()); 605 return false; 606 } 607 608 if (log) 609 { 610 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64, 611 __FUNCTION__, 612 GetID (), 613 GetRegisterContext()->GetPC()); 614 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^"); 615 StreamString s; 616 s.IndentMore(); 617 DumpThreadPlans(&s); 618 log->Printf ("Plan stack initial state:\n%s", s.GetData()); 619 } 620 621 // The top most plan always gets to do the trace log... 622 current_plan->DoTraceLog (); 623 624 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint 625 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to 626 // do any more work on this stop. 627 StopInfoSP private_stop_info (GetPrivateStopReason()); 628 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false) 629 { 630 if (log) 631 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false."); 632 return false; 633 } 634 635 // If we've already been restarted, don't query the plans since the state they would examine is not current. 636 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr)) 637 return false; 638 639 // Before the plans see the state of the world, calculate the current inlined depth. 640 GetStackFrameList()->CalculateCurrentInlinedDepth(); 641 642 // If the base plan doesn't understand why we stopped, then we have to find a plan that does. 643 // If that plan is still working, then we don't need to do any more work. If the plan that explains 644 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide 645 // whether they still need to do more work. 646 647 bool done_processing_current_plan = false; 648 649 if (!current_plan->PlanExplainsStop(event_ptr)) 650 { 651 if (current_plan->TracerExplainsStop()) 652 { 653 done_processing_current_plan = true; 654 should_stop = false; 655 } 656 else 657 { 658 // If the current plan doesn't explain the stop, then find one that 659 // does and let it handle the situation. 660 ThreadPlan *plan_ptr = current_plan; 661 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL) 662 { 663 if (plan_ptr->PlanExplainsStop(event_ptr)) 664 { 665 should_stop = plan_ptr->ShouldStop (event_ptr); 666 667 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it 668 // and all the plans below it off the stack. 669 670 if (plan_ptr->MischiefManaged()) 671 { 672 // We're going to pop the plans up to and including the plan that explains the stop. 673 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr); 674 675 do 676 { 677 if (should_stop) 678 current_plan->WillStop(); 679 PopPlan(); 680 } 681 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr); 682 // Now, if the responsible plan was not "Okay to discard" then we're done, 683 // otherwise we forward this to the next plan in the stack below. 684 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard()) 685 done_processing_current_plan = true; 686 else 687 done_processing_current_plan = false; 688 } 689 else 690 done_processing_current_plan = true; 691 692 break; 693 } 694 695 } 696 } 697 } 698 699 if (!done_processing_current_plan) 700 { 701 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr); 702 703 if (log) 704 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop); 705 706 // We're starting from the base plan, so just let it decide; 707 if (PlanIsBasePlan(current_plan)) 708 { 709 should_stop = current_plan->ShouldStop (event_ptr); 710 if (log) 711 log->Printf("Base plan says should stop: %i.", should_stop); 712 } 713 else 714 { 715 // Otherwise, don't let the base plan override what the other plans say to do, since 716 // presumably if there were other plans they would know what to do... 717 while (1) 718 { 719 if (PlanIsBasePlan(current_plan)) 720 break; 721 722 should_stop = current_plan->ShouldStop(event_ptr); 723 if (log) 724 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop); 725 if (current_plan->MischiefManaged()) 726 { 727 if (should_stop) 728 current_plan->WillStop(); 729 730 // If a Master Plan wants to stop, and wants to stick on the stack, we let it. 731 // Otherwise, see if the plan's parent wants to stop. 732 733 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard()) 734 { 735 PopPlan(); 736 break; 737 } 738 else 739 { 740 741 PopPlan(); 742 743 current_plan = GetCurrentPlan(); 744 if (current_plan == NULL) 745 { 746 break; 747 } 748 } 749 } 750 else 751 { 752 break; 753 } 754 } 755 } 756 757 if (over_ride_stop) 758 should_stop = false; 759 760 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance 761 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up 762 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack, 763 // This code clears stale plans off the stack. 764 765 if (should_stop) 766 { 767 ThreadPlan *plan_ptr = GetCurrentPlan(); 768 while (!PlanIsBasePlan(plan_ptr)) 769 { 770 bool stale = plan_ptr->IsPlanStale (); 771 ThreadPlan *examined_plan = plan_ptr; 772 plan_ptr = GetPreviousPlan (examined_plan); 773 774 if (stale) 775 { 776 if (log) 777 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName()); 778 DiscardThreadPlansUpToPlan(examined_plan); 779 } 780 } 781 } 782 783 } 784 785 if (log) 786 { 787 StreamString s; 788 s.IndentMore(); 789 DumpThreadPlans(&s); 790 log->Printf ("Plan stack final state:\n%s", s.GetData()); 791 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop); 792 } 793 return should_stop; 794 } 795 796 Vote 797 Thread::ShouldReportStop (Event* event_ptr) 798 { 799 StateType thread_state = GetResumeState (); 800 StateType temp_thread_state = GetTemporaryResumeState(); 801 802 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 803 804 if (thread_state == eStateSuspended || thread_state == eStateInvalid) 805 { 806 if (log) 807 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (state was suspended or invalid)\n", GetID(), eVoteNoOpinion); 808 return eVoteNoOpinion; 809 } 810 811 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid) 812 { 813 if (log) 814 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion); 815 return eVoteNoOpinion; 816 } 817 818 if (!ThreadStoppedForAReason()) 819 { 820 if (log) 821 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion); 822 return eVoteNoOpinion; 823 } 824 825 if (m_completed_plan_stack.size() > 0) 826 { 827 // Don't use GetCompletedPlan here, since that suppresses private plans. 828 if (log) 829 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote for complete stack's back plan\n", GetID()); 830 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr); 831 } 832 else 833 { 834 Vote thread_vote = eVoteNoOpinion; 835 ThreadPlan *plan_ptr = GetCurrentPlan(); 836 while (1) 837 { 838 if (plan_ptr->PlanExplainsStop(event_ptr)) 839 { 840 thread_vote = plan_ptr->ShouldReportStop(event_ptr); 841 break; 842 } 843 if (PlanIsBasePlan(plan_ptr)) 844 break; 845 else 846 plan_ptr = GetPreviousPlan(plan_ptr); 847 } 848 if (log) 849 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i for current plan\n", GetID(), thread_vote); 850 851 return thread_vote; 852 } 853 } 854 855 Vote 856 Thread::ShouldReportRun (Event* event_ptr) 857 { 858 StateType thread_state = GetResumeState (); 859 860 if (thread_state == eStateSuspended 861 || thread_state == eStateInvalid) 862 { 863 return eVoteNoOpinion; 864 } 865 866 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 867 if (m_completed_plan_stack.size() > 0) 868 { 869 // Don't use GetCompletedPlan here, since that suppresses private plans. 870 if (log) 871 log->Printf ("Current Plan for thread %d (0x%4.4" PRIx64 "): %s being asked whether we should report run.", 872 GetIndexID(), 873 GetID(), 874 m_completed_plan_stack.back()->GetName()); 875 876 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr); 877 } 878 else 879 { 880 if (log) 881 log->Printf ("Current Plan for thread %d (0x%4.4" PRIx64 "): %s being asked whether we should report run.", 882 GetIndexID(), 883 GetID(), 884 GetCurrentPlan()->GetName()); 885 886 return GetCurrentPlan()->ShouldReportRun (event_ptr); 887 } 888 } 889 890 bool 891 Thread::MatchesSpec (const ThreadSpec *spec) 892 { 893 if (spec == NULL) 894 return true; 895 896 return spec->ThreadPassesBasicTests(*this); 897 } 898 899 void 900 Thread::PushPlan (ThreadPlanSP &thread_plan_sp) 901 { 902 if (thread_plan_sp) 903 { 904 // If the thread plan doesn't already have a tracer, give it its parent's tracer: 905 if (!thread_plan_sp->GetThreadPlanTracer()) 906 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer()); 907 m_plan_stack.push_back (thread_plan_sp); 908 909 thread_plan_sp->DidPush(); 910 911 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 912 if (log) 913 { 914 StreamString s; 915 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull); 916 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4" PRIx64 ".", 917 s.GetData(), 918 thread_plan_sp->GetThread().GetID()); 919 } 920 } 921 } 922 923 void 924 Thread::PopPlan () 925 { 926 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 927 928 if (m_plan_stack.size() <= 1) 929 return; 930 else 931 { 932 ThreadPlanSP &plan = m_plan_stack.back(); 933 if (log) 934 { 935 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID()); 936 } 937 m_completed_plan_stack.push_back (plan); 938 plan->WillPop(); 939 m_plan_stack.pop_back(); 940 } 941 } 942 943 void 944 Thread::DiscardPlan () 945 { 946 if (m_plan_stack.size() > 1) 947 { 948 ThreadPlanSP &plan = m_plan_stack.back(); 949 m_discarded_plan_stack.push_back (plan); 950 plan->WillPop(); 951 m_plan_stack.pop_back(); 952 } 953 } 954 955 ThreadPlan * 956 Thread::GetCurrentPlan () 957 { 958 // There will always be at least the base plan. If somebody is mucking with a 959 // thread with an empty plan stack, we should assert right away. 960 assert (!m_plan_stack.empty()); 961 962 return m_plan_stack.back().get(); 963 } 964 965 ThreadPlanSP 966 Thread::GetCompletedPlan () 967 { 968 ThreadPlanSP empty_plan_sp; 969 if (!m_completed_plan_stack.empty()) 970 { 971 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 972 { 973 ThreadPlanSP completed_plan_sp; 974 completed_plan_sp = m_completed_plan_stack[i]; 975 if (!completed_plan_sp->GetPrivate ()) 976 return completed_plan_sp; 977 } 978 } 979 return empty_plan_sp; 980 } 981 982 ValueObjectSP 983 Thread::GetReturnValueObject () 984 { 985 if (!m_completed_plan_stack.empty()) 986 { 987 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 988 { 989 ValueObjectSP return_valobj_sp; 990 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject(); 991 if (return_valobj_sp) 992 return return_valobj_sp; 993 } 994 } 995 return ValueObjectSP(); 996 } 997 998 bool 999 Thread::IsThreadPlanDone (ThreadPlan *plan) 1000 { 1001 if (!m_completed_plan_stack.empty()) 1002 { 1003 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 1004 { 1005 if (m_completed_plan_stack[i].get() == plan) 1006 return true; 1007 } 1008 } 1009 return false; 1010 } 1011 1012 bool 1013 Thread::WasThreadPlanDiscarded (ThreadPlan *plan) 1014 { 1015 if (!m_discarded_plan_stack.empty()) 1016 { 1017 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--) 1018 { 1019 if (m_discarded_plan_stack[i].get() == plan) 1020 return true; 1021 } 1022 } 1023 return false; 1024 } 1025 1026 ThreadPlan * 1027 Thread::GetPreviousPlan (ThreadPlan *current_plan) 1028 { 1029 if (current_plan == NULL) 1030 return NULL; 1031 1032 int stack_size = m_completed_plan_stack.size(); 1033 for (int i = stack_size - 1; i > 0; i--) 1034 { 1035 if (current_plan == m_completed_plan_stack[i].get()) 1036 return m_completed_plan_stack[i-1].get(); 1037 } 1038 1039 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan) 1040 { 1041 if (m_plan_stack.size() > 0) 1042 return m_plan_stack.back().get(); 1043 else 1044 return NULL; 1045 } 1046 1047 stack_size = m_plan_stack.size(); 1048 for (int i = stack_size - 1; i > 0; i--) 1049 { 1050 if (current_plan == m_plan_stack[i].get()) 1051 return m_plan_stack[i-1].get(); 1052 } 1053 return NULL; 1054 } 1055 1056 void 1057 Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans) 1058 { 1059 if (abort_other_plans) 1060 DiscardThreadPlans(true); 1061 1062 PushPlan (thread_plan_sp); 1063 } 1064 1065 1066 void 1067 Thread::EnableTracer (bool value, bool single_stepping) 1068 { 1069 int stack_size = m_plan_stack.size(); 1070 for (int i = 0; i < stack_size; i++) 1071 { 1072 if (m_plan_stack[i]->GetThreadPlanTracer()) 1073 { 1074 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value); 1075 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping); 1076 } 1077 } 1078 } 1079 1080 void 1081 Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp) 1082 { 1083 int stack_size = m_plan_stack.size(); 1084 for (int i = 0; i < stack_size; i++) 1085 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp); 1086 } 1087 1088 void 1089 Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp) 1090 { 1091 DiscardThreadPlansUpToPlan (up_to_plan_sp.get()); 1092 } 1093 1094 void 1095 Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr) 1096 { 1097 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 1098 if (log) 1099 { 1100 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p", GetID(), up_to_plan_ptr); 1101 } 1102 1103 int stack_size = m_plan_stack.size(); 1104 1105 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the 1106 // stack, and if so discard up to and including it. 1107 1108 if (up_to_plan_ptr == NULL) 1109 { 1110 for (int i = stack_size - 1; i > 0; i--) 1111 DiscardPlan(); 1112 } 1113 else 1114 { 1115 bool found_it = false; 1116 for (int i = stack_size - 1; i > 0; i--) 1117 { 1118 if (m_plan_stack[i].get() == up_to_plan_ptr) 1119 found_it = true; 1120 } 1121 if (found_it) 1122 { 1123 bool last_one = false; 1124 for (int i = stack_size - 1; i > 0 && !last_one ; i--) 1125 { 1126 if (GetCurrentPlan() == up_to_plan_ptr) 1127 last_one = true; 1128 DiscardPlan(); 1129 } 1130 } 1131 } 1132 return; 1133 } 1134 1135 void 1136 Thread::DiscardThreadPlans(bool force) 1137 { 1138 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 1139 if (log) 1140 { 1141 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force); 1142 } 1143 1144 if (force) 1145 { 1146 int stack_size = m_plan_stack.size(); 1147 for (int i = stack_size - 1; i > 0; i--) 1148 { 1149 DiscardPlan(); 1150 } 1151 return; 1152 } 1153 1154 while (1) 1155 { 1156 1157 int master_plan_idx; 1158 bool discard = true; 1159 1160 // Find the first master plan, see if it wants discarding, and if yes discard up to it. 1161 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--) 1162 { 1163 if (m_plan_stack[master_plan_idx]->IsMasterPlan()) 1164 { 1165 discard = m_plan_stack[master_plan_idx]->OkayToDiscard(); 1166 break; 1167 } 1168 } 1169 1170 if (discard) 1171 { 1172 // First pop all the dependent plans: 1173 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--) 1174 { 1175 1176 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop" 1177 // for the plan leaves it in a state that it is safe to pop the plan 1178 // with no more notice? 1179 DiscardPlan(); 1180 } 1181 1182 // Now discard the master plan itself. 1183 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means 1184 // discard it's dependent plans, but not it... 1185 if (master_plan_idx > 0) 1186 { 1187 DiscardPlan(); 1188 } 1189 } 1190 else 1191 { 1192 // If the master plan doesn't want to get discarded, then we're done. 1193 break; 1194 } 1195 1196 } 1197 } 1198 1199 bool 1200 Thread::PlanIsBasePlan (ThreadPlan *plan_ptr) 1201 { 1202 if (plan_ptr->IsBasePlan()) 1203 return true; 1204 else if (m_plan_stack.size() == 0) 1205 return false; 1206 else 1207 return m_plan_stack[0].get() == plan_ptr; 1208 } 1209 1210 Error 1211 Thread::UnwindInnermostExpression() 1212 { 1213 Error error; 1214 int stack_size = m_plan_stack.size(); 1215 1216 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the 1217 // stack, and if so discard up to and including it. 1218 1219 for (int i = stack_size - 1; i > 0; i--) 1220 { 1221 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction) 1222 { 1223 DiscardThreadPlansUpToPlan(m_plan_stack[i].get()); 1224 return error; 1225 } 1226 } 1227 error.SetErrorString("No expressions currently active on this thread"); 1228 return error; 1229 } 1230 1231 1232 ThreadPlan * 1233 Thread::QueueFundamentalPlan (bool abort_other_plans) 1234 { 1235 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this)); 1236 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1237 return thread_plan_sp.get(); 1238 } 1239 1240 ThreadPlan * 1241 Thread::QueueThreadPlanForStepSingleInstruction 1242 ( 1243 bool step_over, 1244 bool abort_other_plans, 1245 bool stop_other_threads 1246 ) 1247 { 1248 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion)); 1249 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1250 return thread_plan_sp.get(); 1251 } 1252 1253 ThreadPlan * 1254 Thread::QueueThreadPlanForStepOverRange 1255 ( 1256 bool abort_other_plans, 1257 const AddressRange &range, 1258 const SymbolContext &addr_context, 1259 lldb::RunMode stop_other_threads 1260 ) 1261 { 1262 ThreadPlanSP thread_plan_sp; 1263 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads)); 1264 1265 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1266 return thread_plan_sp.get(); 1267 } 1268 1269 ThreadPlan * 1270 Thread::QueueThreadPlanForStepInRange 1271 ( 1272 bool abort_other_plans, 1273 const AddressRange &range, 1274 const SymbolContext &addr_context, 1275 const char *step_in_target, 1276 lldb::RunMode stop_other_threads, 1277 bool avoid_code_without_debug_info 1278 ) 1279 { 1280 ThreadPlanSP thread_plan_sp; 1281 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads); 1282 if (avoid_code_without_debug_info) 1283 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug); 1284 else 1285 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug); 1286 if (step_in_target) 1287 plan->SetStepInTarget(step_in_target); 1288 thread_plan_sp.reset (plan); 1289 1290 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1291 return thread_plan_sp.get(); 1292 } 1293 1294 1295 ThreadPlan * 1296 Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans) 1297 { 1298 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this)); 1299 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1300 return thread_plan_sp.get(); 1301 } 1302 1303 ThreadPlan * 1304 Thread::QueueThreadPlanForStepOut 1305 ( 1306 bool abort_other_plans, 1307 SymbolContext *addr_context, 1308 bool first_insn, 1309 bool stop_other_threads, 1310 Vote stop_vote, 1311 Vote run_vote, 1312 uint32_t frame_idx 1313 ) 1314 { 1315 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, 1316 addr_context, 1317 first_insn, 1318 stop_other_threads, 1319 stop_vote, 1320 run_vote, 1321 frame_idx)); 1322 1323 if (thread_plan_sp->ValidatePlan(NULL)) 1324 { 1325 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1326 return thread_plan_sp.get(); 1327 } 1328 else 1329 { 1330 return NULL; 1331 } 1332 } 1333 1334 ThreadPlan * 1335 Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads) 1336 { 1337 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads)); 1338 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL)) 1339 return NULL; 1340 1341 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1342 return thread_plan_sp.get(); 1343 } 1344 1345 ThreadPlan * 1346 Thread::QueueThreadPlanForCallFunction (bool abort_other_plans, 1347 Address& function, 1348 lldb::addr_t arg, 1349 bool stop_other_threads, 1350 bool unwind_on_error, 1351 bool ignore_breakpoints) 1352 { 1353 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, 1354 function, 1355 ClangASTType(), 1356 arg, 1357 stop_other_threads, 1358 unwind_on_error, 1359 ignore_breakpoints)); 1360 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1361 return thread_plan_sp.get(); 1362 } 1363 1364 ThreadPlan * 1365 Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans, 1366 Address &target_addr, 1367 bool stop_other_threads) 1368 { 1369 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads)); 1370 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1371 return thread_plan_sp.get(); 1372 } 1373 1374 ThreadPlan * 1375 Thread::QueueThreadPlanForStepUntil (bool abort_other_plans, 1376 lldb::addr_t *address_list, 1377 size_t num_addresses, 1378 bool stop_other_threads, 1379 uint32_t frame_idx) 1380 { 1381 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx)); 1382 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1383 return thread_plan_sp.get(); 1384 1385 } 1386 1387 uint32_t 1388 Thread::GetIndexID () const 1389 { 1390 return m_index_id; 1391 } 1392 1393 void 1394 Thread::DumpThreadPlans (lldb_private::Stream *s) const 1395 { 1396 uint32_t stack_size = m_plan_stack.size(); 1397 int i; 1398 s->Indent(); 1399 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4" PRIx64 ", stack_size = %d\n", GetIndexID(), GetID(), stack_size); 1400 for (i = stack_size - 1; i >= 0; i--) 1401 { 1402 s->IndentMore(); 1403 s->Indent(); 1404 s->Printf ("Element %d: ", i); 1405 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); 1406 s->EOL(); 1407 s->IndentLess(); 1408 } 1409 1410 stack_size = m_completed_plan_stack.size(); 1411 if (stack_size > 0) 1412 { 1413 s->Indent(); 1414 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size); 1415 for (i = stack_size - 1; i >= 0; i--) 1416 { 1417 s->IndentMore(); 1418 s->Indent(); 1419 s->Printf ("Element %d: ", i); 1420 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); 1421 s->EOL(); 1422 s->IndentLess(); 1423 } 1424 } 1425 1426 stack_size = m_discarded_plan_stack.size(); 1427 if (stack_size > 0) 1428 { 1429 s->Indent(); 1430 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size); 1431 for (i = stack_size - 1; i >= 0; i--) 1432 { 1433 s->IndentMore(); 1434 s->Indent(); 1435 s->Printf ("Element %d: ", i); 1436 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull); 1437 s->EOL(); 1438 s->IndentLess(); 1439 } 1440 } 1441 1442 } 1443 1444 TargetSP 1445 Thread::CalculateTarget () 1446 { 1447 TargetSP target_sp; 1448 ProcessSP process_sp(GetProcess()); 1449 if (process_sp) 1450 target_sp = process_sp->CalculateTarget(); 1451 return target_sp; 1452 1453 } 1454 1455 ProcessSP 1456 Thread::CalculateProcess () 1457 { 1458 return GetProcess(); 1459 } 1460 1461 ThreadSP 1462 Thread::CalculateThread () 1463 { 1464 return shared_from_this(); 1465 } 1466 1467 StackFrameSP 1468 Thread::CalculateStackFrame () 1469 { 1470 return StackFrameSP(); 1471 } 1472 1473 void 1474 Thread::CalculateExecutionContext (ExecutionContext &exe_ctx) 1475 { 1476 exe_ctx.SetContext (shared_from_this()); 1477 } 1478 1479 1480 StackFrameListSP 1481 Thread::GetStackFrameList () 1482 { 1483 StackFrameListSP frame_list_sp; 1484 Mutex::Locker locker(m_frame_mutex); 1485 if (m_curr_frames_sp) 1486 { 1487 frame_list_sp = m_curr_frames_sp; 1488 } 1489 else 1490 { 1491 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true)); 1492 m_curr_frames_sp = frame_list_sp; 1493 } 1494 return frame_list_sp; 1495 } 1496 1497 void 1498 Thread::ClearStackFrames () 1499 { 1500 Mutex::Locker locker(m_frame_mutex); 1501 1502 // Only store away the old "reference" StackFrameList if we got all its frames: 1503 // FIXME: At some point we can try to splice in the frames we have fetched into 1504 // the new frame as we make it, but let's not try that now. 1505 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched()) 1506 m_prev_frames_sp.swap (m_curr_frames_sp); 1507 m_curr_frames_sp.reset(); 1508 } 1509 1510 lldb::StackFrameSP 1511 Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx) 1512 { 1513 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx); 1514 } 1515 1516 1517 Error 1518 Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast) 1519 { 1520 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx); 1521 Error return_error; 1522 1523 if (!frame_sp) 1524 { 1525 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID()); 1526 } 1527 1528 return ReturnFromFrame(frame_sp, return_value_sp, broadcast); 1529 } 1530 1531 Error 1532 Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast) 1533 { 1534 Error return_error; 1535 1536 if (!frame_sp) 1537 { 1538 return_error.SetErrorString("Can't return to a null frame."); 1539 return return_error; 1540 } 1541 1542 Thread *thread = frame_sp->GetThread().get(); 1543 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1; 1544 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx); 1545 if (!older_frame_sp) 1546 { 1547 return_error.SetErrorString("No older frame to return to."); 1548 return return_error; 1549 } 1550 1551 if (return_value_sp) 1552 { 1553 lldb::ABISP abi = thread->GetProcess()->GetABI(); 1554 if (!abi) 1555 { 1556 return_error.SetErrorString("Could not find ABI to set return value."); 1557 return return_error; 1558 } 1559 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction); 1560 1561 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars. 1562 // Turn that back on when that works. 1563 if (0 && sc.function != NULL) 1564 { 1565 Type *function_type = sc.function->GetType(); 1566 if (function_type) 1567 { 1568 clang_type_t return_type = sc.function->GetReturnClangType(); 1569 if (return_type) 1570 { 1571 ClangASTType ast_type (function_type->GetClangAST(), return_type); 1572 StreamString s; 1573 ast_type.DumpTypeDescription(&s); 1574 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type); 1575 if (cast_value_sp) 1576 { 1577 cast_value_sp->SetFormat(eFormatHex); 1578 return_value_sp = cast_value_sp; 1579 } 1580 } 1581 } 1582 } 1583 1584 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp); 1585 if (!return_error.Success()) 1586 return return_error; 1587 } 1588 1589 // Now write the return registers for the chosen frame: 1590 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write 1591 // cook their data 1592 1593 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0); 1594 if (youngest_frame_sp) 1595 { 1596 bool copy_success = youngest_frame_sp->GetRegisterContext()->CopyFromRegisterContext(older_frame_sp->GetRegisterContext()); 1597 if (copy_success) 1598 { 1599 thread->DiscardThreadPlans(true); 1600 thread->ClearStackFrames(); 1601 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged)) 1602 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this())); 1603 } 1604 else 1605 { 1606 return_error.SetErrorString("Could not reset register values."); 1607 } 1608 } 1609 else 1610 { 1611 return_error.SetErrorString("Returned past top frame."); 1612 } 1613 return return_error; 1614 } 1615 1616 void 1617 Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx) 1618 { 1619 ExecutionContext exe_ctx (shared_from_this()); 1620 Process *process = exe_ctx.GetProcessPtr(); 1621 if (process == NULL) 1622 return; 1623 1624 StackFrameSP frame_sp; 1625 SymbolContext frame_sc; 1626 if (frame_idx != LLDB_INVALID_INDEX32) 1627 { 1628 frame_sp = GetStackFrameAtIndex (frame_idx); 1629 if (frame_sp) 1630 { 1631 exe_ctx.SetFrameSP(frame_sp); 1632 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything); 1633 } 1634 } 1635 1636 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat(); 1637 assert (thread_format); 1638 const char *end = NULL; 1639 Debugger::FormatPrompt (thread_format, 1640 frame_sp ? &frame_sc : NULL, 1641 &exe_ctx, 1642 NULL, 1643 strm, 1644 &end); 1645 } 1646 1647 void 1648 Thread::SettingsInitialize () 1649 { 1650 } 1651 1652 void 1653 Thread::SettingsTerminate () 1654 { 1655 } 1656 1657 lldb::StackFrameSP 1658 Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr) 1659 { 1660 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr); 1661 } 1662 1663 const char * 1664 Thread::StopReasonAsCString (lldb::StopReason reason) 1665 { 1666 switch (reason) 1667 { 1668 case eStopReasonInvalid: return "invalid"; 1669 case eStopReasonNone: return "none"; 1670 case eStopReasonTrace: return "trace"; 1671 case eStopReasonBreakpoint: return "breakpoint"; 1672 case eStopReasonWatchpoint: return "watchpoint"; 1673 case eStopReasonSignal: return "signal"; 1674 case eStopReasonException: return "exception"; 1675 case eStopReasonExec: return "exec"; 1676 case eStopReasonPlanComplete: return "plan complete"; 1677 case eStopReasonThreadExiting: return "thread exiting"; 1678 } 1679 1680 1681 static char unknown_state_string[64]; 1682 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason); 1683 return unknown_state_string; 1684 } 1685 1686 const char * 1687 Thread::RunModeAsCString (lldb::RunMode mode) 1688 { 1689 switch (mode) 1690 { 1691 case eOnlyThisThread: return "only this thread"; 1692 case eAllThreads: return "all threads"; 1693 case eOnlyDuringStepping: return "only during stepping"; 1694 } 1695 1696 static char unknown_state_string[64]; 1697 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode); 1698 return unknown_state_string; 1699 } 1700 1701 size_t 1702 Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source) 1703 { 1704 ExecutionContext exe_ctx (shared_from_this()); 1705 Target *target = exe_ctx.GetTargetPtr(); 1706 Process *process = exe_ctx.GetProcessPtr(); 1707 size_t num_frames_shown = 0; 1708 strm.Indent(); 1709 bool is_selected = false; 1710 if (process) 1711 { 1712 if (process->GetThreadList().GetSelectedThread().get() == this) 1713 is_selected = true; 1714 } 1715 strm.Printf("%c ", is_selected ? '*' : ' '); 1716 if (target && target->GetDebugger().GetUseExternalEditor()) 1717 { 1718 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame); 1719 if (frame_sp) 1720 { 1721 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry)); 1722 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file) 1723 { 1724 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line); 1725 } 1726 } 1727 } 1728 1729 DumpUsingSettingsFormat (strm, start_frame); 1730 1731 if (num_frames > 0) 1732 { 1733 strm.IndentMore(); 1734 1735 const bool show_frame_info = true; 1736 strm.IndentMore (); 1737 num_frames_shown = GetStackFrameList ()->GetStatus (strm, 1738 start_frame, 1739 num_frames, 1740 show_frame_info, 1741 num_frames_with_source); 1742 strm.IndentLess(); 1743 strm.IndentLess(); 1744 } 1745 return num_frames_shown; 1746 } 1747 1748 size_t 1749 Thread::GetStackFrameStatus (Stream& strm, 1750 uint32_t first_frame, 1751 uint32_t num_frames, 1752 bool show_frame_info, 1753 uint32_t num_frames_with_source) 1754 { 1755 return GetStackFrameList()->GetStatus (strm, 1756 first_frame, 1757 num_frames, 1758 show_frame_info, 1759 num_frames_with_source); 1760 } 1761 1762 bool 1763 Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint) 1764 { 1765 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0)); 1766 if (frame_sp) 1767 { 1768 checkpoint.SetStackID(frame_sp->GetStackID()); 1769 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData()); 1770 } 1771 return false; 1772 } 1773 1774 bool 1775 Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint) 1776 { 1777 return ResetFrameZeroRegisters (checkpoint.GetData()); 1778 } 1779 1780 bool 1781 Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp) 1782 { 1783 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0)); 1784 if (frame_sp) 1785 { 1786 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (register_data_sp); 1787 1788 // Clear out all stack frames as our world just changed. 1789 ClearStackFrames(); 1790 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true); 1791 if (m_unwinder_ap.get()) 1792 m_unwinder_ap->Clear(); 1793 1794 return ret; 1795 } 1796 return false; 1797 } 1798 1799 Unwind * 1800 Thread::GetUnwinder () 1801 { 1802 if (m_unwinder_ap.get() == NULL) 1803 { 1804 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ()); 1805 const llvm::Triple::ArchType machine = target_arch.GetMachine(); 1806 switch (machine) 1807 { 1808 case llvm::Triple::x86_64: 1809 case llvm::Triple::x86: 1810 case llvm::Triple::arm: 1811 case llvm::Triple::thumb: 1812 m_unwinder_ap.reset (new UnwindLLDB (*this)); 1813 break; 1814 1815 default: 1816 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple) 1817 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this)); 1818 break; 1819 } 1820 } 1821 return m_unwinder_ap.get(); 1822 } 1823 1824 1825 void 1826 Thread::Flush () 1827 { 1828 ClearStackFrames (); 1829 m_reg_context_sp.reset(); 1830 } 1831 1832 bool 1833 Thread::IsStillAtLastBreakpointHit () 1834 { 1835 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it. 1836 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in 1837 // multithreaded programs. 1838 if (m_actual_stop_info_sp) { 1839 StopReason stop_reason = m_actual_stop_info_sp->GetStopReason(); 1840 if (stop_reason == lldb::eStopReasonBreakpoint) { 1841 uint64_t value = m_actual_stop_info_sp->GetValue(); 1842 lldb::addr_t pc = GetRegisterContext()->GetPC(); 1843 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc); 1844 if (bp_site_sp && value == bp_site_sp->GetID()) 1845 return true; 1846 } 1847 } 1848 return false; 1849 } 1850