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/FormatEntity.h" 17 #include "lldb/Core/State.h" 18 #include "lldb/Core/Stream.h" 19 #include "lldb/Core/StreamString.h" 20 #include "lldb/Core/RegularExpression.h" 21 #include "lldb/Host/Host.h" 22 #include "lldb/Interpreter/OptionValueFileSpecList.h" 23 #include "lldb/Interpreter/OptionValueProperties.h" 24 #include "lldb/Interpreter/Property.h" 25 #include "lldb/Symbol/Function.h" 26 #include "lldb/Target/ABI.h" 27 #include "lldb/Target/DynamicLoader.h" 28 #include "lldb/Target/ExecutionContext.h" 29 #include "lldb/Target/ObjCLanguageRuntime.h" 30 #include "lldb/Target/Process.h" 31 #include "lldb/Target/RegisterContext.h" 32 #include "lldb/Target/StopInfo.h" 33 #include "lldb/Target/SystemRuntime.h" 34 #include "lldb/Target/Target.h" 35 #include "lldb/Target/Thread.h" 36 #include "lldb/Target/ThreadPlan.h" 37 #include "lldb/Target/ThreadPlanCallFunction.h" 38 #include "lldb/Target/ThreadPlanBase.h" 39 #include "lldb/Target/ThreadPlanPython.h" 40 #include "lldb/Target/ThreadPlanStepInstruction.h" 41 #include "lldb/Target/ThreadPlanStepOut.h" 42 #include "lldb/Target/ThreadPlanStepOverBreakpoint.h" 43 #include "lldb/Target/ThreadPlanStepThrough.h" 44 #include "lldb/Target/ThreadPlanStepInRange.h" 45 #include "lldb/Target/ThreadPlanStepOverRange.h" 46 #include "lldb/Target/ThreadPlanRunToAddress.h" 47 #include "lldb/Target/ThreadPlanStepUntil.h" 48 #include "lldb/Target/ThreadSpec.h" 49 #include "lldb/Target/Unwind.h" 50 #include "Plugins/Process/Utility/UnwindLLDB.h" 51 #include "Plugins/Process/Utility/UnwindMacOSXFrameBackchain.h" 52 53 54 using namespace lldb; 55 using namespace lldb_private; 56 57 58 const ThreadPropertiesSP & 59 Thread::GetGlobalProperties() 60 { 61 static ThreadPropertiesSP g_settings_sp; 62 if (!g_settings_sp) 63 g_settings_sp.reset (new ThreadProperties (true)); 64 return g_settings_sp; 65 } 66 67 static PropertyDefinition 68 g_properties[] = 69 { 70 { "step-in-avoid-nodebug", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, step-in will not stop in functions with no debug information." }, 71 { "step-out-avoid-nodebug", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true, when step-in/step-out/step-over leave the current frame, they will continue to step out till they come to a function with " 72 "debug information. Passing a frame argument to step-out will override this option." }, 73 { "step-avoid-regexp", OptionValue::eTypeRegex , true , 0, "^std::", NULL, "A regular expression defining functions step-in won't stop in." }, 74 { "step-avoid-libraries", OptionValue::eTypeFileSpecList , true , 0, NULL, NULL, "A list of libraries that source stepping won't stop in." }, 75 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." }, 76 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL } 77 }; 78 79 enum { 80 ePropertyStepInAvoidsNoDebug, 81 ePropertyStepOutAvoidsNoDebug, 82 ePropertyStepAvoidRegex, 83 ePropertyStepAvoidLibraries, 84 ePropertyEnableThreadTrace 85 }; 86 87 88 class ThreadOptionValueProperties : public OptionValueProperties 89 { 90 public: 91 ThreadOptionValueProperties (const ConstString &name) : 92 OptionValueProperties (name) 93 { 94 } 95 96 // This constructor is used when creating ThreadOptionValueProperties when it 97 // is part of a new lldb_private::Thread instance. It will copy all current 98 // global property values as needed 99 ThreadOptionValueProperties (ThreadProperties *global_properties) : 100 OptionValueProperties(*global_properties->GetValueProperties()) 101 { 102 } 103 104 virtual const Property * 105 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const 106 { 107 // When getting the value for a key from the thread options, we will always 108 // try and grab the setting from the current thread if there is one. Else we just 109 // use the one from this instance. 110 if (exe_ctx) 111 { 112 Thread *thread = exe_ctx->GetThreadPtr(); 113 if (thread) 114 { 115 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get()); 116 if (this != instance_properties) 117 return instance_properties->ProtectedGetPropertyAtIndex (idx); 118 } 119 } 120 return ProtectedGetPropertyAtIndex (idx); 121 } 122 }; 123 124 125 126 ThreadProperties::ThreadProperties (bool is_global) : 127 Properties () 128 { 129 if (is_global) 130 { 131 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread"))); 132 m_collection_sp->Initialize(g_properties); 133 } 134 else 135 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get())); 136 } 137 138 ThreadProperties::~ThreadProperties() 139 { 140 } 141 142 const RegularExpression * 143 ThreadProperties::GetSymbolsToAvoidRegexp() 144 { 145 const uint32_t idx = ePropertyStepAvoidRegex; 146 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx); 147 } 148 149 FileSpecList & 150 ThreadProperties::GetLibrariesToAvoid() const 151 { 152 const uint32_t idx = ePropertyStepAvoidLibraries; 153 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx); 154 assert(option_value); 155 return option_value->GetCurrentValue(); 156 } 157 158 bool 159 ThreadProperties::GetTraceEnabledState() const 160 { 161 const uint32_t idx = ePropertyEnableThreadTrace; 162 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 163 } 164 165 bool 166 ThreadProperties::GetStepInAvoidsNoDebug() const 167 { 168 const uint32_t idx = ePropertyStepInAvoidsNoDebug; 169 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 170 } 171 172 bool 173 ThreadProperties::GetStepOutAvoidsNoDebug() const 174 { 175 const uint32_t idx = ePropertyStepOutAvoidsNoDebug; 176 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0); 177 } 178 179 180 //------------------------------------------------------------------ 181 // Thread Event Data 182 //------------------------------------------------------------------ 183 184 185 const ConstString & 186 Thread::ThreadEventData::GetFlavorString () 187 { 188 static ConstString g_flavor ("Thread::ThreadEventData"); 189 return g_flavor; 190 } 191 192 Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) : 193 m_thread_sp (thread_sp), 194 m_stack_id () 195 { 196 } 197 198 Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) : 199 m_thread_sp (thread_sp), 200 m_stack_id (stack_id) 201 { 202 } 203 204 Thread::ThreadEventData::ThreadEventData () : 205 m_thread_sp (), 206 m_stack_id () 207 { 208 } 209 210 Thread::ThreadEventData::~ThreadEventData () 211 { 212 } 213 214 void 215 Thread::ThreadEventData::Dump (Stream *s) const 216 { 217 218 } 219 220 const Thread::ThreadEventData * 221 Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr) 222 { 223 if (event_ptr) 224 { 225 const EventData *event_data = event_ptr->GetData(); 226 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString()) 227 return static_cast <const ThreadEventData *> (event_ptr->GetData()); 228 } 229 return NULL; 230 } 231 232 ThreadSP 233 Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr) 234 { 235 ThreadSP thread_sp; 236 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr); 237 if (event_data) 238 thread_sp = event_data->GetThread(); 239 return thread_sp; 240 } 241 242 StackID 243 Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr) 244 { 245 StackID stack_id; 246 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr); 247 if (event_data) 248 stack_id = event_data->GetStackID(); 249 return stack_id; 250 } 251 252 StackFrameSP 253 Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr) 254 { 255 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr); 256 StackFrameSP frame_sp; 257 if (event_data) 258 { 259 ThreadSP thread_sp = event_data->GetThread(); 260 if (thread_sp) 261 { 262 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID()); 263 } 264 } 265 return frame_sp; 266 } 267 268 //------------------------------------------------------------------ 269 // Thread class 270 //------------------------------------------------------------------ 271 272 ConstString & 273 Thread::GetStaticBroadcasterClass () 274 { 275 static ConstString class_name ("lldb.thread"); 276 return class_name; 277 } 278 279 Thread::Thread (Process &process, lldb::tid_t tid, bool use_invalid_index_id) : 280 ThreadProperties (false), 281 UserID (tid), 282 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()), 283 m_process_wp (process.shared_from_this()), 284 m_stop_info_sp (), 285 m_stop_info_stop_id (0), 286 m_stop_info_override_stop_id (0), 287 m_index_id (use_invalid_index_id ? LLDB_INVALID_INDEX32 : process.GetNextThreadIndexID(tid)), 288 m_reg_context_sp (), 289 m_state (eStateUnloaded), 290 m_state_mutex (Mutex::eMutexTypeRecursive), 291 m_plan_stack (), 292 m_completed_plan_stack(), 293 m_frame_mutex (Mutex::eMutexTypeRecursive), 294 m_curr_frames_sp (), 295 m_prev_frames_sp (), 296 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER), 297 m_resume_state (eStateRunning), 298 m_temporary_resume_state (eStateRunning), 299 m_unwinder_ap (), 300 m_destroy_called (false), 301 m_override_should_notify (eLazyBoolCalculate), 302 m_extended_info_fetched (false), 303 m_extended_info () 304 { 305 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 306 if (log) 307 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")", 308 static_cast<void*>(this), GetID()); 309 310 CheckInWithManager(); 311 QueueFundamentalPlan(true); 312 } 313 314 315 Thread::~Thread() 316 { 317 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT)); 318 if (log) 319 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")", 320 static_cast<void*>(this), GetID()); 321 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor. 322 assert (m_destroy_called); 323 } 324 325 void 326 Thread::DestroyThread () 327 { 328 // Tell any plans on the plan stacks that the thread is being destroyed since 329 // any plans that have a thread go away in the middle of might need 330 // to do cleanup, or in some cases NOT do cleanup... 331 for (auto plan : m_plan_stack) 332 plan->ThreadDestroyed(); 333 334 for (auto plan : m_discarded_plan_stack) 335 plan->ThreadDestroyed(); 336 337 for (auto plan : m_completed_plan_stack) 338 plan->ThreadDestroyed(); 339 340 m_destroy_called = true; 341 m_plan_stack.clear(); 342 m_discarded_plan_stack.clear(); 343 m_completed_plan_stack.clear(); 344 345 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the 346 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread 347 // without checking first whether it is destroyed, they won't crash. 348 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this)); 349 m_plan_stack.push_back (null_plan_sp); 350 351 m_stop_info_sp.reset(); 352 m_reg_context_sp.reset(); 353 m_unwinder_ap.reset(); 354 Mutex::Locker locker(m_frame_mutex); 355 m_curr_frames_sp.reset(); 356 m_prev_frames_sp.reset(); 357 } 358 359 void 360 Thread::BroadcastSelectedFrameChange(StackID &new_frame_id) 361 { 362 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged)) 363 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id)); 364 } 365 366 uint32_t 367 Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast) 368 { 369 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame); 370 if (broadcast) 371 BroadcastSelectedFrameChange(frame->GetStackID()); 372 return ret_value; 373 } 374 375 bool 376 Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast) 377 { 378 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx)); 379 if (frame_sp) 380 { 381 GetStackFrameList()->SetSelectedFrame(frame_sp.get()); 382 if (broadcast) 383 BroadcastSelectedFrameChange(frame_sp->GetStackID()); 384 return true; 385 } 386 else 387 return false; 388 } 389 390 bool 391 Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream) 392 { 393 const bool broadcast = true; 394 bool success = SetSelectedFrameByIndex (frame_idx, broadcast); 395 if (success) 396 { 397 StackFrameSP frame_sp = GetSelectedFrame(); 398 if (frame_sp) 399 { 400 bool already_shown = false; 401 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry)); 402 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0) 403 { 404 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line); 405 } 406 407 bool show_frame_info = true; 408 bool show_source = !already_shown; 409 return frame_sp->GetStatus (output_stream, show_frame_info, show_source); 410 } 411 return false; 412 } 413 else 414 return false; 415 } 416 417 418 lldb::StopInfoSP 419 Thread::GetStopInfo () 420 { 421 if (m_destroy_called) 422 return m_stop_info_sp; 423 424 ThreadPlanSP plan_sp (GetCompletedPlan()); 425 ProcessSP process_sp (GetProcess()); 426 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX; 427 if (plan_sp && plan_sp->PlanSucceeded()) 428 { 429 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject(), GetExpressionVariable()); 430 } 431 else 432 { 433 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty) 434 (m_stop_info_sp && m_stop_info_sp->IsValid())) // Stop info is valid, just return what we have 435 { 436 return m_stop_info_sp; 437 } 438 else 439 { 440 GetPrivateStopInfo (); 441 return m_stop_info_sp; 442 } 443 } 444 } 445 446 lldb::StopInfoSP 447 Thread::GetPrivateStopInfo () 448 { 449 if (m_destroy_called) 450 return m_stop_info_sp; 451 452 ProcessSP process_sp (GetProcess()); 453 if (process_sp) 454 { 455 const uint32_t process_stop_id = process_sp->GetStopID(); 456 if (m_stop_info_stop_id != process_stop_id) 457 { 458 if (m_stop_info_sp) 459 { 460 if (m_stop_info_sp->IsValid() 461 || IsStillAtLastBreakpointHit() 462 || GetCurrentPlan()->IsVirtualStep()) 463 SetStopInfo (m_stop_info_sp); 464 else 465 m_stop_info_sp.reset(); 466 } 467 468 if (!m_stop_info_sp) 469 { 470 if (CalculateStopInfo() == false) 471 SetStopInfo (StopInfoSP()); 472 } 473 } 474 475 // The stop info can be manually set by calling Thread::SetStopInfo() 476 // prior to this function ever getting called, so we can't rely on 477 // "m_stop_info_stop_id != process_stop_id" as the condition for 478 // the if statement below, we must also check the stop info to see 479 // if we need to override it. See the header documentation in 480 // Process::GetStopInfoOverrideCallback() for more information on 481 // the stop info override callback. 482 if (m_stop_info_override_stop_id != process_stop_id) 483 { 484 m_stop_info_override_stop_id = process_stop_id; 485 if (m_stop_info_sp) 486 { 487 ArchSpec::StopInfoOverrideCallbackType callback = GetProcess()->GetStopInfoOverrideCallback(); 488 if (callback) 489 callback(*this); 490 } 491 } 492 } 493 return m_stop_info_sp; 494 } 495 496 497 lldb::StopReason 498 Thread::GetStopReason() 499 { 500 lldb::StopInfoSP stop_info_sp (GetStopInfo ()); 501 if (stop_info_sp) 502 return stop_info_sp->GetStopReason(); 503 return eStopReasonNone; 504 } 505 506 507 508 void 509 Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp) 510 { 511 m_stop_info_sp = stop_info_sp; 512 if (m_stop_info_sp) 513 { 514 m_stop_info_sp->MakeStopInfoValid(); 515 // If we are overriding the ShouldReportStop, do that here: 516 if (m_override_should_notify != eLazyBoolCalculate) 517 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes); 518 } 519 520 ProcessSP process_sp (GetProcess()); 521 if (process_sp) 522 m_stop_info_stop_id = process_sp->GetStopID(); 523 else 524 m_stop_info_stop_id = UINT32_MAX; 525 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 526 if (log) 527 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)", 528 static_cast<void*>(this), GetID(), 529 stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>", 530 m_stop_info_stop_id); 531 } 532 533 void 534 Thread::SetShouldReportStop (Vote vote) 535 { 536 if (vote == eVoteNoOpinion) 537 return; 538 else 539 { 540 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo); 541 if (m_stop_info_sp) 542 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes); 543 } 544 } 545 546 void 547 Thread::SetStopInfoToNothing() 548 { 549 // Note, we can't just NULL out the private reason, or the native thread implementation will try to 550 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number. 551 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER)); 552 } 553 554 bool 555 Thread::ThreadStoppedForAReason (void) 556 { 557 return (bool) GetPrivateStopInfo (); 558 } 559 560 bool 561 Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state) 562 { 563 saved_state.register_backup_sp.reset(); 564 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0)); 565 if (frame_sp) 566 { 567 lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression)); 568 if (reg_checkpoint_sp) 569 { 570 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext()); 571 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp)) 572 saved_state.register_backup_sp = reg_checkpoint_sp; 573 } 574 } 575 if (!saved_state.register_backup_sp) 576 return false; 577 578 saved_state.stop_info_sp = GetStopInfo(); 579 ProcessSP process_sp (GetProcess()); 580 if (process_sp) 581 saved_state.orig_stop_id = process_sp->GetStopID(); 582 saved_state.current_inlined_depth = GetCurrentInlinedDepth(); 583 584 return true; 585 } 586 587 bool 588 Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state) 589 { 590 if (saved_state.register_backup_sp) 591 { 592 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0)); 593 if (frame_sp) 594 { 595 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext()); 596 if (reg_ctx_sp) 597 { 598 bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp); 599 600 // Clear out all stack frames as our world just changed. 601 ClearStackFrames(); 602 reg_ctx_sp->InvalidateIfNeeded(true); 603 if (m_unwinder_ap.get()) 604 m_unwinder_ap->Clear(); 605 return ret; 606 } 607 } 608 } 609 return false; 610 } 611 612 bool 613 Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state) 614 { 615 if (saved_state.stop_info_sp) 616 saved_state.stop_info_sp->MakeStopInfoValid(); 617 SetStopInfo(saved_state.stop_info_sp); 618 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth); 619 return true; 620 } 621 622 StateType 623 Thread::GetState() const 624 { 625 // If any other threads access this we will need a mutex for it 626 Mutex::Locker locker(m_state_mutex); 627 return m_state; 628 } 629 630 void 631 Thread::SetState(StateType state) 632 { 633 Mutex::Locker locker(m_state_mutex); 634 m_state = state; 635 } 636 637 void 638 Thread::WillStop() 639 { 640 ThreadPlan *current_plan = GetCurrentPlan(); 641 642 // FIXME: I may decide to disallow threads with no plans. In which 643 // case this should go to an assert. 644 645 if (!current_plan) 646 return; 647 648 current_plan->WillStop(); 649 } 650 651 void 652 Thread::SetupForResume () 653 { 654 if (GetResumeState() != eStateSuspended) 655 { 656 657 // If we're at a breakpoint push the step-over breakpoint plan. Do this before 658 // telling the current plan it will resume, since we might change what the current 659 // plan is. 660 661 // StopReason stop_reason = lldb::eStopReasonInvalid; 662 // StopInfoSP stop_info_sp = GetStopInfo(); 663 // if (stop_info_sp.get()) 664 // stop_reason = stop_info_sp->GetStopReason(); 665 // if (stop_reason == lldb::eStopReasonBreakpoint) 666 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext()); 667 if (reg_ctx_sp) 668 { 669 const addr_t thread_pc = reg_ctx_sp->GetPC(); 670 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc); 671 if (bp_site_sp) 672 { 673 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything 674 // special to step over a breakpoint. 675 676 ThreadPlan *cur_plan = GetCurrentPlan(); 677 678 bool push_step_over_bp_plan = false; 679 if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint) 680 { 681 ThreadPlanStepOverBreakpoint *bp_plan = (ThreadPlanStepOverBreakpoint *)cur_plan; 682 if (bp_plan->GetBreakpointLoadAddress() != thread_pc) 683 push_step_over_bp_plan = true; 684 } 685 else 686 push_step_over_bp_plan = true; 687 688 if (push_step_over_bp_plan) 689 { 690 ThreadPlanSP step_bp_plan_sp (new ThreadPlanStepOverBreakpoint (*this)); 691 if (step_bp_plan_sp) 692 { 693 ; 694 step_bp_plan_sp->SetPrivate (true); 695 696 if (GetCurrentPlan()->RunState() != eStateStepping) 697 { 698 ThreadPlanStepOverBreakpoint *step_bp_plan 699 = static_cast<ThreadPlanStepOverBreakpoint *>(step_bp_plan_sp.get()); 700 step_bp_plan->SetAutoContinue(true); 701 } 702 QueueThreadPlan (step_bp_plan_sp, false); 703 } 704 } 705 } 706 } 707 } 708 } 709 710 bool 711 Thread::ShouldResume (StateType resume_state) 712 { 713 // At this point clear the completed plan stack. 714 m_completed_plan_stack.clear(); 715 m_discarded_plan_stack.clear(); 716 m_override_should_notify = eLazyBoolCalculate; 717 718 m_temporary_resume_state = resume_state; 719 720 lldb::ThreadSP backing_thread_sp (GetBackingThread ()); 721 if (backing_thread_sp) 722 backing_thread_sp->m_temporary_resume_state = resume_state; 723 724 // Make sure m_stop_info_sp is valid 725 GetPrivateStopInfo(); 726 727 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from 728 // the target, 'cause that slows down single stepping. So assume that if we got to the point where 729 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know 730 // about the fact that we are resuming... 731 const uint32_t process_stop_id = GetProcess()->GetStopID(); 732 if (m_stop_info_stop_id == process_stop_id && 733 (m_stop_info_sp && m_stop_info_sp->IsValid())) 734 { 735 StopInfo *stop_info = GetPrivateStopInfo().get(); 736 if (stop_info) 737 stop_info->WillResume (resume_state); 738 } 739 740 // Tell all the plans that we are about to resume in case they need to clear any state. 741 // We distinguish between the plan on the top of the stack and the lower 742 // plans in case a plan needs to do any special business before it runs. 743 744 bool need_to_resume = false; 745 ThreadPlan *plan_ptr = GetCurrentPlan(); 746 if (plan_ptr) 747 { 748 need_to_resume = plan_ptr->WillResume(resume_state, true); 749 750 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL) 751 { 752 plan_ptr->WillResume (resume_state, false); 753 } 754 755 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info. 756 // In that case, don't reset it here. 757 758 if (need_to_resume && resume_state != eStateSuspended) 759 { 760 m_stop_info_sp.reset(); 761 } 762 } 763 764 if (need_to_resume) 765 { 766 ClearStackFrames(); 767 // Let Thread subclasses do any special work they need to prior to resuming 768 WillResume (resume_state); 769 } 770 771 return need_to_resume; 772 } 773 774 void 775 Thread::DidResume () 776 { 777 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER); 778 } 779 780 void 781 Thread::DidStop () 782 { 783 SetState (eStateStopped); 784 } 785 786 bool 787 Thread::ShouldStop (Event* event_ptr) 788 { 789 ThreadPlan *current_plan = GetCurrentPlan(); 790 791 bool should_stop = true; 792 793 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 794 795 if (GetResumeState () == eStateSuspended) 796 { 797 if (log) 798 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)", 799 __FUNCTION__, GetID (), GetProtocolID()); 800 return false; 801 } 802 803 if (GetTemporaryResumeState () == eStateSuspended) 804 { 805 if (log) 806 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)", 807 __FUNCTION__, GetID (), GetProtocolID()); 808 return false; 809 } 810 811 // Based on the current thread plan and process stop info, check if this 812 // thread caused the process to stop. NOTE: this must take place before 813 // the plan is moved from the current plan stack to the completed plan 814 // stack. 815 if (ThreadStoppedForAReason() == false) 816 { 817 if (log) 818 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since no stop reason)", 819 __FUNCTION__, GetID (), GetProtocolID(), 820 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS); 821 return false; 822 } 823 824 if (log) 825 { 826 log->Printf ("Thread::%s(%p) for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64, 827 __FUNCTION__, static_cast<void*>(this), GetID (), 828 GetProtocolID (), 829 GetRegisterContext() 830 ? GetRegisterContext()->GetPC() 831 : LLDB_INVALID_ADDRESS); 832 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^"); 833 StreamString s; 834 s.IndentMore(); 835 DumpThreadPlans(&s); 836 log->Printf ("Plan stack initial state:\n%s", s.GetData()); 837 } 838 839 // The top most plan always gets to do the trace log... 840 current_plan->DoTraceLog (); 841 842 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint 843 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to 844 // do any more work on this stop. 845 StopInfoSP private_stop_info (GetPrivateStopInfo()); 846 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false) 847 { 848 if (log) 849 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false."); 850 return false; 851 } 852 853 // If we've already been restarted, don't query the plans since the state they would examine is not current. 854 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr)) 855 return false; 856 857 // Before the plans see the state of the world, calculate the current inlined depth. 858 GetStackFrameList()->CalculateCurrentInlinedDepth(); 859 860 // If the base plan doesn't understand why we stopped, then we have to find a plan that does. 861 // If that plan is still working, then we don't need to do any more work. If the plan that explains 862 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide 863 // whether they still need to do more work. 864 865 bool done_processing_current_plan = false; 866 867 if (!current_plan->PlanExplainsStop(event_ptr)) 868 { 869 if (current_plan->TracerExplainsStop()) 870 { 871 done_processing_current_plan = true; 872 should_stop = false; 873 } 874 else 875 { 876 // If the current plan doesn't explain the stop, then find one that 877 // does and let it handle the situation. 878 ThreadPlan *plan_ptr = current_plan; 879 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL) 880 { 881 if (plan_ptr->PlanExplainsStop(event_ptr)) 882 { 883 should_stop = plan_ptr->ShouldStop (event_ptr); 884 885 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it 886 // and all the plans below it off the stack. 887 888 if (plan_ptr->MischiefManaged()) 889 { 890 // We're going to pop the plans up to and including the plan that explains the stop. 891 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr); 892 893 do 894 { 895 if (should_stop) 896 current_plan->WillStop(); 897 PopPlan(); 898 } 899 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr); 900 // Now, if the responsible plan was not "Okay to discard" then we're done, 901 // otherwise we forward this to the next plan in the stack below. 902 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard()) 903 done_processing_current_plan = true; 904 else 905 done_processing_current_plan = false; 906 } 907 else 908 done_processing_current_plan = true; 909 910 break; 911 } 912 913 } 914 } 915 } 916 917 if (!done_processing_current_plan) 918 { 919 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr); 920 921 if (log) 922 log->Printf("Plan %s explains stop, auto-continue %i.", 923 current_plan->GetName(), over_ride_stop); 924 925 // We're starting from the base plan, so just let it decide; 926 if (PlanIsBasePlan(current_plan)) 927 { 928 should_stop = current_plan->ShouldStop (event_ptr); 929 if (log) 930 log->Printf("Base plan says should stop: %i.", should_stop); 931 } 932 else 933 { 934 // Otherwise, don't let the base plan override what the other plans say to do, since 935 // presumably if there were other plans they would know what to do... 936 while (1) 937 { 938 if (PlanIsBasePlan(current_plan)) 939 break; 940 941 should_stop = current_plan->ShouldStop(event_ptr); 942 if (log) 943 log->Printf("Plan %s should stop: %d.", 944 current_plan->GetName(), should_stop); 945 if (current_plan->MischiefManaged()) 946 { 947 if (should_stop) 948 current_plan->WillStop(); 949 950 // If a Master Plan wants to stop, and wants to stick on the stack, we let it. 951 // Otherwise, see if the plan's parent wants to stop. 952 953 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard()) 954 { 955 PopPlan(); 956 break; 957 } 958 else 959 { 960 961 PopPlan(); 962 963 current_plan = GetCurrentPlan(); 964 if (current_plan == NULL) 965 { 966 break; 967 } 968 } 969 } 970 else 971 { 972 break; 973 } 974 } 975 } 976 977 if (over_ride_stop) 978 should_stop = false; 979 980 } 981 982 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance 983 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up 984 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack, 985 // This code clears stale plans off the stack. 986 987 if (should_stop) 988 { 989 ThreadPlan *plan_ptr = GetCurrentPlan(); 990 while (!PlanIsBasePlan(plan_ptr)) 991 { 992 bool stale = plan_ptr->IsPlanStale (); 993 ThreadPlan *examined_plan = plan_ptr; 994 plan_ptr = GetPreviousPlan (examined_plan); 995 996 if (stale) 997 { 998 if (log) 999 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", 1000 examined_plan->GetName()); 1001 DiscardThreadPlansUpToPlan(examined_plan); 1002 } 1003 } 1004 } 1005 1006 if (log) 1007 { 1008 StreamString s; 1009 s.IndentMore(); 1010 DumpThreadPlans(&s); 1011 log->Printf ("Plan stack final state:\n%s", s.GetData()); 1012 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop); 1013 } 1014 return should_stop; 1015 } 1016 1017 Vote 1018 Thread::ShouldReportStop (Event* event_ptr) 1019 { 1020 StateType thread_state = GetResumeState (); 1021 StateType temp_thread_state = GetTemporaryResumeState(); 1022 1023 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 1024 1025 if (thread_state == eStateSuspended || thread_state == eStateInvalid) 1026 { 1027 if (log) 1028 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (state was suspended or invalid)", GetID(), eVoteNoOpinion); 1029 return eVoteNoOpinion; 1030 } 1031 1032 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid) 1033 { 1034 if (log) 1035 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (temporary state was suspended or invalid)", GetID(), eVoteNoOpinion); 1036 return eVoteNoOpinion; 1037 } 1038 1039 if (!ThreadStoppedForAReason()) 1040 { 1041 if (log) 1042 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (thread didn't stop for a reason.)", GetID(), eVoteNoOpinion); 1043 return eVoteNoOpinion; 1044 } 1045 1046 if (m_completed_plan_stack.size() > 0) 1047 { 1048 // Don't use GetCompletedPlan here, since that suppresses private plans. 1049 if (log) 1050 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote for complete stack's back plan", GetID()); 1051 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr); 1052 } 1053 else 1054 { 1055 Vote thread_vote = eVoteNoOpinion; 1056 ThreadPlan *plan_ptr = GetCurrentPlan(); 1057 while (1) 1058 { 1059 if (plan_ptr->PlanExplainsStop(event_ptr)) 1060 { 1061 thread_vote = plan_ptr->ShouldReportStop(event_ptr); 1062 break; 1063 } 1064 if (PlanIsBasePlan(plan_ptr)) 1065 break; 1066 else 1067 plan_ptr = GetPreviousPlan(plan_ptr); 1068 } 1069 if (log) 1070 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i for current plan", GetID(), thread_vote); 1071 1072 return thread_vote; 1073 } 1074 } 1075 1076 Vote 1077 Thread::ShouldReportRun (Event* event_ptr) 1078 { 1079 StateType thread_state = GetResumeState (); 1080 1081 if (thread_state == eStateSuspended 1082 || thread_state == eStateInvalid) 1083 { 1084 return eVoteNoOpinion; 1085 } 1086 1087 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 1088 if (m_completed_plan_stack.size() > 0) 1089 { 1090 // Don't use GetCompletedPlan here, since that suppresses private plans. 1091 if (log) 1092 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.", 1093 GetIndexID(), static_cast<void*>(this), GetID(), 1094 StateAsCString(GetTemporaryResumeState()), 1095 m_completed_plan_stack.back()->GetName()); 1096 1097 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr); 1098 } 1099 else 1100 { 1101 if (log) 1102 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.", 1103 GetIndexID(), static_cast<void*>(this), GetID(), 1104 StateAsCString(GetTemporaryResumeState()), 1105 GetCurrentPlan()->GetName()); 1106 1107 return GetCurrentPlan()->ShouldReportRun (event_ptr); 1108 } 1109 } 1110 1111 bool 1112 Thread::MatchesSpec (const ThreadSpec *spec) 1113 { 1114 if (spec == NULL) 1115 return true; 1116 1117 return spec->ThreadPassesBasicTests(*this); 1118 } 1119 1120 void 1121 Thread::PushPlan (ThreadPlanSP &thread_plan_sp) 1122 { 1123 if (thread_plan_sp) 1124 { 1125 // If the thread plan doesn't already have a tracer, give it its parent's tracer: 1126 if (!thread_plan_sp->GetThreadPlanTracer()) 1127 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer()); 1128 m_plan_stack.push_back (thread_plan_sp); 1129 1130 thread_plan_sp->DidPush(); 1131 1132 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 1133 if (log) 1134 { 1135 StreamString s; 1136 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull); 1137 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".", 1138 static_cast<void*>(this), s.GetData(), 1139 thread_plan_sp->GetThread().GetID()); 1140 } 1141 } 1142 } 1143 1144 void 1145 Thread::PopPlan () 1146 { 1147 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 1148 1149 if (m_plan_stack.size() <= 1) 1150 return; 1151 else 1152 { 1153 ThreadPlanSP &plan = m_plan_stack.back(); 1154 if (log) 1155 { 1156 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID()); 1157 } 1158 m_completed_plan_stack.push_back (plan); 1159 plan->WillPop(); 1160 m_plan_stack.pop_back(); 1161 } 1162 } 1163 1164 void 1165 Thread::DiscardPlan () 1166 { 1167 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 1168 if (m_plan_stack.size() > 1) 1169 { 1170 ThreadPlanSP &plan = m_plan_stack.back(); 1171 if (log) 1172 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID()); 1173 1174 m_discarded_plan_stack.push_back (plan); 1175 plan->WillPop(); 1176 m_plan_stack.pop_back(); 1177 } 1178 } 1179 1180 ThreadPlan * 1181 Thread::GetCurrentPlan () 1182 { 1183 // There will always be at least the base plan. If somebody is mucking with a 1184 // thread with an empty plan stack, we should assert right away. 1185 if (m_plan_stack.empty()) 1186 return NULL; 1187 return m_plan_stack.back().get(); 1188 } 1189 1190 ThreadPlanSP 1191 Thread::GetCompletedPlan () 1192 { 1193 ThreadPlanSP empty_plan_sp; 1194 if (!m_completed_plan_stack.empty()) 1195 { 1196 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 1197 { 1198 ThreadPlanSP completed_plan_sp; 1199 completed_plan_sp = m_completed_plan_stack[i]; 1200 if (!completed_plan_sp->GetPrivate ()) 1201 return completed_plan_sp; 1202 } 1203 } 1204 return empty_plan_sp; 1205 } 1206 1207 ValueObjectSP 1208 Thread::GetReturnValueObject () 1209 { 1210 if (!m_completed_plan_stack.empty()) 1211 { 1212 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 1213 { 1214 ValueObjectSP return_valobj_sp; 1215 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject(); 1216 if (return_valobj_sp) 1217 return return_valobj_sp; 1218 } 1219 } 1220 return ValueObjectSP(); 1221 } 1222 1223 ClangExpressionVariableSP 1224 Thread::GetExpressionVariable () 1225 { 1226 if (!m_completed_plan_stack.empty()) 1227 { 1228 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 1229 { 1230 ClangExpressionVariableSP expression_variable_sp; 1231 expression_variable_sp = m_completed_plan_stack[i]->GetExpressionVariable(); 1232 if (expression_variable_sp) 1233 return expression_variable_sp; 1234 } 1235 } 1236 return ClangExpressionVariableSP(); 1237 } 1238 1239 bool 1240 Thread::IsThreadPlanDone (ThreadPlan *plan) 1241 { 1242 if (!m_completed_plan_stack.empty()) 1243 { 1244 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) 1245 { 1246 if (m_completed_plan_stack[i].get() == plan) 1247 return true; 1248 } 1249 } 1250 return false; 1251 } 1252 1253 bool 1254 Thread::WasThreadPlanDiscarded (ThreadPlan *plan) 1255 { 1256 if (!m_discarded_plan_stack.empty()) 1257 { 1258 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--) 1259 { 1260 if (m_discarded_plan_stack[i].get() == plan) 1261 return true; 1262 } 1263 } 1264 return false; 1265 } 1266 1267 ThreadPlan * 1268 Thread::GetPreviousPlan (ThreadPlan *current_plan) 1269 { 1270 if (current_plan == NULL) 1271 return NULL; 1272 1273 int stack_size = m_completed_plan_stack.size(); 1274 for (int i = stack_size - 1; i > 0; i--) 1275 { 1276 if (current_plan == m_completed_plan_stack[i].get()) 1277 return m_completed_plan_stack[i-1].get(); 1278 } 1279 1280 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan) 1281 { 1282 if (m_plan_stack.size() > 0) 1283 return m_plan_stack.back().get(); 1284 else 1285 return NULL; 1286 } 1287 1288 stack_size = m_plan_stack.size(); 1289 for (int i = stack_size - 1; i > 0; i--) 1290 { 1291 if (current_plan == m_plan_stack[i].get()) 1292 return m_plan_stack[i-1].get(); 1293 } 1294 return NULL; 1295 } 1296 1297 void 1298 Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans) 1299 { 1300 if (abort_other_plans) 1301 DiscardThreadPlans(true); 1302 1303 PushPlan (thread_plan_sp); 1304 } 1305 1306 1307 void 1308 Thread::EnableTracer (bool value, bool single_stepping) 1309 { 1310 int stack_size = m_plan_stack.size(); 1311 for (int i = 0; i < stack_size; i++) 1312 { 1313 if (m_plan_stack[i]->GetThreadPlanTracer()) 1314 { 1315 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value); 1316 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping); 1317 } 1318 } 1319 } 1320 1321 void 1322 Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp) 1323 { 1324 int stack_size = m_plan_stack.size(); 1325 for (int i = 0; i < stack_size; i++) 1326 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp); 1327 } 1328 1329 bool 1330 Thread::DiscardUserThreadPlansUpToIndex (uint32_t thread_index) 1331 { 1332 // Count the user thread plans from the back end to get the number of the one we want 1333 // to discard: 1334 1335 uint32_t idx = 0; 1336 ThreadPlan *up_to_plan_ptr = nullptr; 1337 1338 for (ThreadPlanSP plan_sp : m_plan_stack) 1339 { 1340 if (plan_sp->GetPrivate()) 1341 continue; 1342 if (idx == thread_index) 1343 { 1344 up_to_plan_ptr = plan_sp.get(); 1345 break; 1346 } 1347 else 1348 idx++; 1349 } 1350 1351 if (up_to_plan_ptr == nullptr) 1352 return false; 1353 1354 DiscardThreadPlansUpToPlan(up_to_plan_ptr); 1355 return true; 1356 } 1357 1358 1359 void 1360 Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp) 1361 { 1362 DiscardThreadPlansUpToPlan (up_to_plan_sp.get()); 1363 } 1364 1365 void 1366 Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr) 1367 { 1368 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 1369 if (log) 1370 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p", 1371 GetID(), static_cast<void*>(up_to_plan_ptr)); 1372 1373 int stack_size = m_plan_stack.size(); 1374 1375 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the 1376 // stack, and if so discard up to and including it. 1377 1378 if (up_to_plan_ptr == NULL) 1379 { 1380 for (int i = stack_size - 1; i > 0; i--) 1381 DiscardPlan(); 1382 } 1383 else 1384 { 1385 bool found_it = false; 1386 for (int i = stack_size - 1; i > 0; i--) 1387 { 1388 if (m_plan_stack[i].get() == up_to_plan_ptr) 1389 found_it = true; 1390 } 1391 if (found_it) 1392 { 1393 bool last_one = false; 1394 for (int i = stack_size - 1; i > 0 && !last_one ; i--) 1395 { 1396 if (GetCurrentPlan() == up_to_plan_ptr) 1397 last_one = true; 1398 DiscardPlan(); 1399 } 1400 } 1401 } 1402 return; 1403 } 1404 1405 void 1406 Thread::DiscardThreadPlans(bool force) 1407 { 1408 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP)); 1409 if (log) 1410 { 1411 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force); 1412 } 1413 1414 if (force) 1415 { 1416 int stack_size = m_plan_stack.size(); 1417 for (int i = stack_size - 1; i > 0; i--) 1418 { 1419 DiscardPlan(); 1420 } 1421 return; 1422 } 1423 1424 while (1) 1425 { 1426 1427 int master_plan_idx; 1428 bool discard = true; 1429 1430 // Find the first master plan, see if it wants discarding, and if yes discard up to it. 1431 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--) 1432 { 1433 if (m_plan_stack[master_plan_idx]->IsMasterPlan()) 1434 { 1435 discard = m_plan_stack[master_plan_idx]->OkayToDiscard(); 1436 break; 1437 } 1438 } 1439 1440 if (discard) 1441 { 1442 // First pop all the dependent plans: 1443 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--) 1444 { 1445 1446 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop" 1447 // for the plan leaves it in a state that it is safe to pop the plan 1448 // with no more notice? 1449 DiscardPlan(); 1450 } 1451 1452 // Now discard the master plan itself. 1453 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means 1454 // discard it's dependent plans, but not it... 1455 if (master_plan_idx > 0) 1456 { 1457 DiscardPlan(); 1458 } 1459 } 1460 else 1461 { 1462 // If the master plan doesn't want to get discarded, then we're done. 1463 break; 1464 } 1465 1466 } 1467 } 1468 1469 bool 1470 Thread::PlanIsBasePlan (ThreadPlan *plan_ptr) 1471 { 1472 if (plan_ptr->IsBasePlan()) 1473 return true; 1474 else if (m_plan_stack.size() == 0) 1475 return false; 1476 else 1477 return m_plan_stack[0].get() == plan_ptr; 1478 } 1479 1480 Error 1481 Thread::UnwindInnermostExpression() 1482 { 1483 Error error; 1484 int stack_size = m_plan_stack.size(); 1485 1486 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the 1487 // stack, and if so discard up to and including it. 1488 1489 for (int i = stack_size - 1; i > 0; i--) 1490 { 1491 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction) 1492 { 1493 DiscardThreadPlansUpToPlan(m_plan_stack[i].get()); 1494 return error; 1495 } 1496 } 1497 error.SetErrorString("No expressions currently active on this thread"); 1498 return error; 1499 } 1500 1501 1502 ThreadPlanSP 1503 Thread::QueueFundamentalPlan (bool abort_other_plans) 1504 { 1505 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this)); 1506 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1507 return thread_plan_sp; 1508 } 1509 1510 ThreadPlanSP 1511 Thread::QueueThreadPlanForStepSingleInstruction 1512 ( 1513 bool step_over, 1514 bool abort_other_plans, 1515 bool stop_other_threads 1516 ) 1517 { 1518 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion)); 1519 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1520 return thread_plan_sp; 1521 } 1522 1523 ThreadPlanSP 1524 Thread::QueueThreadPlanForStepOverRange 1525 ( 1526 bool abort_other_plans, 1527 const AddressRange &range, 1528 const SymbolContext &addr_context, 1529 lldb::RunMode stop_other_threads, 1530 LazyBool step_out_avoids_code_withoug_debug_info 1531 ) 1532 { 1533 ThreadPlanSP thread_plan_sp; 1534 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads, step_out_avoids_code_withoug_debug_info)); 1535 1536 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1537 return thread_plan_sp; 1538 } 1539 1540 ThreadPlanSP 1541 Thread::QueueThreadPlanForStepInRange 1542 ( 1543 bool abort_other_plans, 1544 const AddressRange &range, 1545 const SymbolContext &addr_context, 1546 const char *step_in_target, 1547 lldb::RunMode stop_other_threads, 1548 LazyBool step_in_avoids_code_without_debug_info, 1549 LazyBool step_out_avoids_code_without_debug_info 1550 ) 1551 { 1552 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInRange (*this, 1553 range, 1554 addr_context, 1555 stop_other_threads, 1556 step_in_avoids_code_without_debug_info, 1557 step_out_avoids_code_without_debug_info)); 1558 ThreadPlanStepInRange *plan = static_cast<ThreadPlanStepInRange *>(thread_plan_sp.get()); 1559 1560 if (step_in_target) 1561 plan->SetStepInTarget(step_in_target); 1562 1563 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1564 return thread_plan_sp; 1565 } 1566 1567 1568 ThreadPlanSP 1569 Thread::QueueThreadPlanForStepOut 1570 ( 1571 bool abort_other_plans, 1572 SymbolContext *addr_context, 1573 bool first_insn, 1574 bool stop_other_threads, 1575 Vote stop_vote, 1576 Vote run_vote, 1577 uint32_t frame_idx, 1578 LazyBool step_out_avoids_code_withoug_debug_info 1579 ) 1580 { 1581 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this, 1582 addr_context, 1583 first_insn, 1584 stop_other_threads, 1585 stop_vote, 1586 run_vote, 1587 frame_idx, 1588 step_out_avoids_code_withoug_debug_info)); 1589 1590 if (thread_plan_sp->ValidatePlan(NULL)) 1591 { 1592 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1593 return thread_plan_sp; 1594 } 1595 else 1596 { 1597 return ThreadPlanSP(); 1598 } 1599 } 1600 1601 ThreadPlanSP 1602 Thread::QueueThreadPlanForStepOutNoShouldStop 1603 ( 1604 bool abort_other_plans, 1605 SymbolContext *addr_context, 1606 bool first_insn, 1607 bool stop_other_threads, 1608 Vote stop_vote, 1609 Vote run_vote, 1610 uint32_t frame_idx 1611 ) 1612 { 1613 ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut (*this, 1614 addr_context, 1615 first_insn, 1616 stop_other_threads, 1617 stop_vote, 1618 run_vote, 1619 frame_idx, 1620 eLazyBoolNo)); 1621 1622 ThreadPlanStepOut *new_plan = static_cast<ThreadPlanStepOut *>(thread_plan_sp.get()); 1623 new_plan->ClearShouldStopHereCallbacks(); 1624 1625 if (thread_plan_sp->ValidatePlan(NULL)) 1626 { 1627 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1628 return thread_plan_sp; 1629 } 1630 else 1631 { 1632 return ThreadPlanSP(); 1633 } 1634 } 1635 1636 ThreadPlanSP 1637 Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads) 1638 { 1639 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads)); 1640 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL)) 1641 return ThreadPlanSP(); 1642 1643 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1644 return thread_plan_sp; 1645 } 1646 1647 ThreadPlanSP 1648 Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans, 1649 Address &target_addr, 1650 bool stop_other_threads) 1651 { 1652 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads)); 1653 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1654 return thread_plan_sp; 1655 } 1656 1657 ThreadPlanSP 1658 Thread::QueueThreadPlanForStepUntil (bool abort_other_plans, 1659 lldb::addr_t *address_list, 1660 size_t num_addresses, 1661 bool stop_other_threads, 1662 uint32_t frame_idx) 1663 { 1664 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx)); 1665 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1666 return thread_plan_sp; 1667 1668 } 1669 1670 lldb::ThreadPlanSP 1671 Thread::QueueThreadPlanForStepScripted (bool abort_other_plans, 1672 const char *class_name, 1673 bool stop_other_threads) 1674 { 1675 ThreadPlanSP thread_plan_sp (new ThreadPlanPython (*this, class_name)); 1676 QueueThreadPlan (thread_plan_sp, abort_other_plans); 1677 // This seems a little funny, but I don't want to have to split up the constructor and the 1678 // DidPush in the scripted plan, that seems annoying. 1679 // That means the constructor has to be in DidPush. 1680 // So I have to validate the plan AFTER pushing it, and then take it off again... 1681 if (!thread_plan_sp->ValidatePlan(nullptr)) 1682 { 1683 DiscardThreadPlansUpToPlan(thread_plan_sp); 1684 return ThreadPlanSP(); 1685 } 1686 else 1687 return thread_plan_sp; 1688 1689 } 1690 1691 uint32_t 1692 Thread::GetIndexID () const 1693 { 1694 return m_index_id; 1695 } 1696 1697 static void 1698 PrintPlanElement (Stream *s, const ThreadPlanSP &plan, lldb::DescriptionLevel desc_level, int32_t elem_idx) 1699 { 1700 s->IndentMore(); 1701 s->Indent(); 1702 s->Printf ("Element %d: ", elem_idx); 1703 plan->GetDescription (s, desc_level); 1704 s->EOL(); 1705 s->IndentLess(); 1706 } 1707 1708 static void 1709 PrintPlanStack (Stream *s, const std::vector<lldb::ThreadPlanSP> &plan_stack, lldb::DescriptionLevel desc_level, bool include_internal) 1710 { 1711 int32_t print_idx = 0; 1712 for (ThreadPlanSP plan_sp : plan_stack) 1713 { 1714 if (include_internal || !plan_sp->GetPrivate()) 1715 { 1716 PrintPlanElement (s, plan_sp, desc_level, print_idx++); 1717 } 1718 } 1719 } 1720 1721 void 1722 Thread::DumpThreadPlans (Stream *s, 1723 lldb::DescriptionLevel desc_level, 1724 bool include_internal, 1725 bool ignore_boring_threads) const 1726 { 1727 uint32_t stack_size; 1728 1729 if (ignore_boring_threads) 1730 { 1731 uint32_t stack_size = m_plan_stack.size(); 1732 uint32_t completed_stack_size = m_completed_plan_stack.size(); 1733 uint32_t discarded_stack_size = m_discarded_plan_stack.size(); 1734 if (stack_size == 1 && completed_stack_size == 0 && discarded_stack_size == 0) 1735 { 1736 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 "\n", GetIndexID(), GetID()); 1737 s->IndentMore(); 1738 s->Indent(); 1739 s->Printf("No active thread plans\n"); 1740 s->IndentLess(); 1741 return; 1742 } 1743 } 1744 1745 s->Indent(); 1746 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 ":\n", GetIndexID(), GetID()); 1747 s->IndentMore(); 1748 s->Indent(); 1749 s->Printf ("Active plan stack:\n"); 1750 PrintPlanStack (s, m_plan_stack, desc_level, include_internal); 1751 1752 stack_size = m_completed_plan_stack.size(); 1753 if (stack_size > 0) 1754 { 1755 s->Indent(); 1756 s->Printf ("Completed Plan Stack:\n"); 1757 PrintPlanStack (s, m_completed_plan_stack, desc_level, include_internal); 1758 } 1759 1760 stack_size = m_discarded_plan_stack.size(); 1761 if (stack_size > 0) 1762 { 1763 s->Indent(); 1764 s->Printf ("Discarded Plan Stack:\n"); 1765 PrintPlanStack (s, m_discarded_plan_stack, desc_level, include_internal); 1766 } 1767 1768 s->IndentLess(); 1769 } 1770 1771 TargetSP 1772 Thread::CalculateTarget () 1773 { 1774 TargetSP target_sp; 1775 ProcessSP process_sp(GetProcess()); 1776 if (process_sp) 1777 target_sp = process_sp->CalculateTarget(); 1778 return target_sp; 1779 1780 } 1781 1782 ProcessSP 1783 Thread::CalculateProcess () 1784 { 1785 return GetProcess(); 1786 } 1787 1788 ThreadSP 1789 Thread::CalculateThread () 1790 { 1791 return shared_from_this(); 1792 } 1793 1794 StackFrameSP 1795 Thread::CalculateStackFrame () 1796 { 1797 return StackFrameSP(); 1798 } 1799 1800 void 1801 Thread::CalculateExecutionContext (ExecutionContext &exe_ctx) 1802 { 1803 exe_ctx.SetContext (shared_from_this()); 1804 } 1805 1806 1807 StackFrameListSP 1808 Thread::GetStackFrameList () 1809 { 1810 StackFrameListSP frame_list_sp; 1811 Mutex::Locker locker(m_frame_mutex); 1812 if (m_curr_frames_sp) 1813 { 1814 frame_list_sp = m_curr_frames_sp; 1815 } 1816 else 1817 { 1818 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true)); 1819 m_curr_frames_sp = frame_list_sp; 1820 } 1821 return frame_list_sp; 1822 } 1823 1824 void 1825 Thread::ClearStackFrames () 1826 { 1827 Mutex::Locker locker(m_frame_mutex); 1828 1829 Unwind *unwinder = GetUnwinder (); 1830 if (unwinder) 1831 unwinder->Clear(); 1832 1833 // Only store away the old "reference" StackFrameList if we got all its frames: 1834 // FIXME: At some point we can try to splice in the frames we have fetched into 1835 // the new frame as we make it, but let's not try that now. 1836 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched()) 1837 m_prev_frames_sp.swap (m_curr_frames_sp); 1838 m_curr_frames_sp.reset(); 1839 1840 m_extended_info.reset(); 1841 m_extended_info_fetched = false; 1842 } 1843 1844 lldb::StackFrameSP 1845 Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx) 1846 { 1847 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx); 1848 } 1849 1850 1851 Error 1852 Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast) 1853 { 1854 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx); 1855 Error return_error; 1856 1857 if (!frame_sp) 1858 { 1859 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID()); 1860 } 1861 1862 return ReturnFromFrame(frame_sp, return_value_sp, broadcast); 1863 } 1864 1865 Error 1866 Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast) 1867 { 1868 Error return_error; 1869 1870 if (!frame_sp) 1871 { 1872 return_error.SetErrorString("Can't return to a null frame."); 1873 return return_error; 1874 } 1875 1876 Thread *thread = frame_sp->GetThread().get(); 1877 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1; 1878 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx); 1879 if (!older_frame_sp) 1880 { 1881 return_error.SetErrorString("No older frame to return to."); 1882 return return_error; 1883 } 1884 1885 if (return_value_sp) 1886 { 1887 lldb::ABISP abi = thread->GetProcess()->GetABI(); 1888 if (!abi) 1889 { 1890 return_error.SetErrorString("Could not find ABI to set return value."); 1891 return return_error; 1892 } 1893 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction); 1894 1895 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars. 1896 // Turn that back on when that works. 1897 if (/* DISABLES CODE */ (0) && sc.function != NULL) 1898 { 1899 Type *function_type = sc.function->GetType(); 1900 if (function_type) 1901 { 1902 ClangASTType return_type = sc.function->GetClangType().GetFunctionReturnType(); 1903 if (return_type) 1904 { 1905 StreamString s; 1906 return_type.DumpTypeDescription(&s); 1907 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type); 1908 if (cast_value_sp) 1909 { 1910 cast_value_sp->SetFormat(eFormatHex); 1911 return_value_sp = cast_value_sp; 1912 } 1913 } 1914 } 1915 } 1916 1917 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp); 1918 if (!return_error.Success()) 1919 return return_error; 1920 } 1921 1922 // Now write the return registers for the chosen frame: 1923 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write 1924 // cook their data 1925 1926 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0); 1927 if (youngest_frame_sp) 1928 { 1929 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext()); 1930 if (reg_ctx_sp) 1931 { 1932 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext()); 1933 if (copy_success) 1934 { 1935 thread->DiscardThreadPlans(true); 1936 thread->ClearStackFrames(); 1937 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged)) 1938 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this())); 1939 } 1940 else 1941 { 1942 return_error.SetErrorString("Could not reset register values."); 1943 } 1944 } 1945 else 1946 { 1947 return_error.SetErrorString("Frame has no register context."); 1948 } 1949 } 1950 else 1951 { 1952 return_error.SetErrorString("Returned past top frame."); 1953 } 1954 return return_error; 1955 } 1956 1957 static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope) 1958 { 1959 for (size_t n=0;n<list.size();n++) 1960 { 1961 s << "\t"; 1962 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset); 1963 s << "\n"; 1964 } 1965 } 1966 1967 Error 1968 Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings) 1969 { 1970 ExecutionContext exe_ctx (GetStackFrameAtIndex(0)); 1971 Target *target = exe_ctx.GetTargetPtr(); 1972 TargetSP target_sp = exe_ctx.GetTargetSP(); 1973 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext(); 1974 StackFrame *frame = exe_ctx.GetFramePtr(); 1975 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction); 1976 1977 // Find candidate locations. 1978 std::vector<Address> candidates, within_function, outside_function; 1979 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function); 1980 1981 // If possible, we try and stay within the current function. 1982 // Within a function, we accept multiple locations (optimized code may do this, 1983 // there's no solution here so we do the best we can). 1984 // However if we're trying to leave the function, we don't know how to pick the 1985 // right location, so if there's more than one then we bail. 1986 if (!within_function.empty()) 1987 candidates = within_function; 1988 else if (outside_function.size() == 1 && can_leave_function) 1989 candidates = outside_function; 1990 1991 // Check if we got anything. 1992 if (candidates.empty()) 1993 { 1994 if (outside_function.empty()) 1995 { 1996 return Error("Cannot locate an address for %s:%i.", 1997 file.GetFilename().AsCString(), line); 1998 } 1999 else if (outside_function.size() == 1) 2000 { 2001 return Error("%s:%i is outside the current function.", 2002 file.GetFilename().AsCString(), line); 2003 } 2004 else 2005 { 2006 StreamString sstr; 2007 DumpAddressList(sstr, outside_function, target); 2008 return Error("%s:%i has multiple candidate locations:\n%s", 2009 file.GetFilename().AsCString(), line, sstr.GetString().c_str()); 2010 } 2011 } 2012 2013 // Accept the first location, warn about any others. 2014 Address dest = candidates[0]; 2015 if (warnings && candidates.size() > 1) 2016 { 2017 StreamString sstr; 2018 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n", 2019 file.GetFilename().AsCString(), line); 2020 DumpAddressList(sstr, candidates, target); 2021 *warnings = sstr.GetString(); 2022 } 2023 2024 if (!reg_ctx->SetPC (dest)) 2025 return Error("Cannot change PC to target address."); 2026 2027 return Error(); 2028 } 2029 2030 void 2031 Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx) 2032 { 2033 ExecutionContext exe_ctx (shared_from_this()); 2034 Process *process = exe_ctx.GetProcessPtr(); 2035 if (process == NULL) 2036 return; 2037 2038 StackFrameSP frame_sp; 2039 SymbolContext frame_sc; 2040 if (frame_idx != LLDB_INVALID_FRAME_ID) 2041 { 2042 frame_sp = GetStackFrameAtIndex (frame_idx); 2043 if (frame_sp) 2044 { 2045 exe_ctx.SetFrameSP(frame_sp); 2046 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything); 2047 } 2048 } 2049 2050 const FormatEntity::Entry *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat(); 2051 assert (thread_format); 2052 2053 FormatEntity::Format(*thread_format, 2054 strm, 2055 frame_sp ? &frame_sc : NULL, 2056 &exe_ctx, 2057 NULL, 2058 NULL, 2059 false, 2060 false); 2061 } 2062 2063 void 2064 Thread::SettingsInitialize () 2065 { 2066 } 2067 2068 void 2069 Thread::SettingsTerminate () 2070 { 2071 } 2072 2073 lldb::addr_t 2074 Thread::GetThreadPointer () 2075 { 2076 return LLDB_INVALID_ADDRESS; 2077 } 2078 2079 addr_t 2080 Thread::GetThreadLocalData (const ModuleSP module) 2081 { 2082 // The default implementation is to ask the dynamic loader for it. 2083 // This can be overridden for specific platforms. 2084 DynamicLoader *loader = GetProcess()->GetDynamicLoader(); 2085 if (loader) 2086 return loader->GetThreadLocalData (module, shared_from_this()); 2087 else 2088 return LLDB_INVALID_ADDRESS; 2089 } 2090 2091 bool 2092 Thread::SafeToCallFunctions () 2093 { 2094 Process *process = GetProcess().get(); 2095 if (process) 2096 { 2097 SystemRuntime *runtime = process->GetSystemRuntime (); 2098 if (runtime) 2099 { 2100 return runtime->SafeToCallFunctionsOnThisThread (shared_from_this()); 2101 } 2102 } 2103 return true; 2104 } 2105 2106 lldb::StackFrameSP 2107 Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr) 2108 { 2109 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr); 2110 } 2111 2112 const char * 2113 Thread::StopReasonAsCString (lldb::StopReason reason) 2114 { 2115 switch (reason) 2116 { 2117 case eStopReasonInvalid: return "invalid"; 2118 case eStopReasonNone: return "none"; 2119 case eStopReasonTrace: return "trace"; 2120 case eStopReasonBreakpoint: return "breakpoint"; 2121 case eStopReasonWatchpoint: return "watchpoint"; 2122 case eStopReasonSignal: return "signal"; 2123 case eStopReasonException: return "exception"; 2124 case eStopReasonExec: return "exec"; 2125 case eStopReasonPlanComplete: return "plan complete"; 2126 case eStopReasonThreadExiting: return "thread exiting"; 2127 case eStopReasonInstrumentation: return "instrumentation break"; 2128 } 2129 2130 2131 static char unknown_state_string[64]; 2132 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason); 2133 return unknown_state_string; 2134 } 2135 2136 const char * 2137 Thread::RunModeAsCString (lldb::RunMode mode) 2138 { 2139 switch (mode) 2140 { 2141 case eOnlyThisThread: return "only this thread"; 2142 case eAllThreads: return "all threads"; 2143 case eOnlyDuringStepping: return "only during stepping"; 2144 } 2145 2146 static char unknown_state_string[64]; 2147 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode); 2148 return unknown_state_string; 2149 } 2150 2151 size_t 2152 Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source) 2153 { 2154 ExecutionContext exe_ctx (shared_from_this()); 2155 Target *target = exe_ctx.GetTargetPtr(); 2156 Process *process = exe_ctx.GetProcessPtr(); 2157 size_t num_frames_shown = 0; 2158 strm.Indent(); 2159 bool is_selected = false; 2160 if (process) 2161 { 2162 if (process->GetThreadList().GetSelectedThread().get() == this) 2163 is_selected = true; 2164 } 2165 strm.Printf("%c ", is_selected ? '*' : ' '); 2166 if (target && target->GetDebugger().GetUseExternalEditor()) 2167 { 2168 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame); 2169 if (frame_sp) 2170 { 2171 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry)); 2172 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file) 2173 { 2174 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line); 2175 } 2176 } 2177 } 2178 2179 DumpUsingSettingsFormat (strm, start_frame); 2180 2181 if (num_frames > 0) 2182 { 2183 strm.IndentMore(); 2184 2185 const bool show_frame_info = true; 2186 2187 const char *selected_frame_marker = NULL; 2188 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID())) 2189 strm.IndentMore (); 2190 else 2191 selected_frame_marker = "* "; 2192 2193 num_frames_shown = GetStackFrameList ()->GetStatus (strm, 2194 start_frame, 2195 num_frames, 2196 show_frame_info, 2197 num_frames_with_source, 2198 selected_frame_marker); 2199 if (num_frames == 1) 2200 strm.IndentLess(); 2201 strm.IndentLess(); 2202 } 2203 return num_frames_shown; 2204 } 2205 2206 bool 2207 Thread::GetDescription (Stream &strm, lldb::DescriptionLevel level, bool print_json_thread, bool print_json_stopinfo) 2208 { 2209 DumpUsingSettingsFormat (strm, 0); 2210 strm.Printf("\n"); 2211 2212 StructuredData::ObjectSP thread_info = GetExtendedInfo(); 2213 StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo(); 2214 2215 if (print_json_thread || print_json_stopinfo) 2216 { 2217 if (thread_info && print_json_thread) 2218 { 2219 thread_info->Dump (strm); 2220 strm.Printf("\n"); 2221 } 2222 2223 if (stop_info && print_json_stopinfo) 2224 { 2225 stop_info->Dump (strm); 2226 strm.Printf("\n"); 2227 } 2228 2229 return true; 2230 } 2231 2232 if (thread_info) 2233 { 2234 StructuredData::ObjectSP activity = thread_info->GetObjectForDotSeparatedPath("activity"); 2235 StructuredData::ObjectSP breadcrumb = thread_info->GetObjectForDotSeparatedPath("breadcrumb"); 2236 StructuredData::ObjectSP messages = thread_info->GetObjectForDotSeparatedPath("trace_messages"); 2237 2238 bool printed_activity = false; 2239 if (activity && activity->GetType() == StructuredData::Type::eTypeDictionary) 2240 { 2241 StructuredData::Dictionary *activity_dict = activity->GetAsDictionary(); 2242 StructuredData::ObjectSP id = activity_dict->GetValueForKey("id"); 2243 StructuredData::ObjectSP name = activity_dict->GetValueForKey("name"); 2244 if (name && name->GetType() == StructuredData::Type::eTypeString 2245 && id && id->GetType() == StructuredData::Type::eTypeInteger) 2246 { 2247 strm.Printf(" Activity '%s', 0x%" PRIx64 "\n", name->GetAsString()->GetValue().c_str(), id->GetAsInteger()->GetValue()); 2248 } 2249 printed_activity = true; 2250 } 2251 bool printed_breadcrumb = false; 2252 if (breadcrumb && breadcrumb->GetType() == StructuredData::Type::eTypeDictionary) 2253 { 2254 if (printed_activity) 2255 strm.Printf ("\n"); 2256 StructuredData::Dictionary *breadcrumb_dict = breadcrumb->GetAsDictionary(); 2257 StructuredData::ObjectSP breadcrumb_text = breadcrumb_dict->GetValueForKey ("name"); 2258 if (breadcrumb_text && breadcrumb_text->GetType() == StructuredData::Type::eTypeString) 2259 { 2260 strm.Printf (" Current Breadcrumb: %s\n", breadcrumb_text->GetAsString()->GetValue().c_str()); 2261 } 2262 printed_breadcrumb = true; 2263 } 2264 if (messages && messages->GetType() == StructuredData::Type::eTypeArray) 2265 { 2266 if (printed_breadcrumb) 2267 strm.Printf("\n"); 2268 StructuredData::Array *messages_array = messages->GetAsArray(); 2269 const size_t msg_count = messages_array->GetSize(); 2270 if (msg_count > 0) 2271 { 2272 strm.Printf (" %zu trace messages:\n", msg_count); 2273 for (size_t i = 0; i < msg_count; i++) 2274 { 2275 StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i); 2276 if (message && message->GetType() == StructuredData::Type::eTypeDictionary) 2277 { 2278 StructuredData::Dictionary *message_dict = message->GetAsDictionary(); 2279 StructuredData::ObjectSP message_text = message_dict->GetValueForKey ("message"); 2280 if (message_text && message_text->GetType() == StructuredData::Type::eTypeString) 2281 { 2282 strm.Printf (" %s\n", message_text->GetAsString()->GetValue().c_str()); 2283 } 2284 } 2285 } 2286 } 2287 } 2288 } 2289 2290 return true; 2291 } 2292 2293 size_t 2294 Thread::GetStackFrameStatus (Stream& strm, 2295 uint32_t first_frame, 2296 uint32_t num_frames, 2297 bool show_frame_info, 2298 uint32_t num_frames_with_source) 2299 { 2300 return GetStackFrameList()->GetStatus (strm, 2301 first_frame, 2302 num_frames, 2303 show_frame_info, 2304 num_frames_with_source); 2305 } 2306 2307 Unwind * 2308 Thread::GetUnwinder () 2309 { 2310 if (m_unwinder_ap.get() == NULL) 2311 { 2312 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ()); 2313 const llvm::Triple::ArchType machine = target_arch.GetMachine(); 2314 switch (machine) 2315 { 2316 case llvm::Triple::x86_64: 2317 case llvm::Triple::x86: 2318 case llvm::Triple::arm: 2319 case llvm::Triple::aarch64: 2320 case llvm::Triple::thumb: 2321 case llvm::Triple::mips64: 2322 case llvm::Triple::ppc: 2323 case llvm::Triple::ppc64: 2324 case llvm::Triple::hexagon: 2325 m_unwinder_ap.reset (new UnwindLLDB (*this)); 2326 break; 2327 2328 default: 2329 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple) 2330 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this)); 2331 break; 2332 } 2333 } 2334 return m_unwinder_ap.get(); 2335 } 2336 2337 2338 void 2339 Thread::Flush () 2340 { 2341 ClearStackFrames (); 2342 m_reg_context_sp.reset(); 2343 } 2344 2345 bool 2346 Thread::IsStillAtLastBreakpointHit () 2347 { 2348 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it. 2349 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in 2350 // multithreaded programs. 2351 if (m_stop_info_sp) { 2352 StopReason stop_reason = m_stop_info_sp->GetStopReason(); 2353 if (stop_reason == lldb::eStopReasonBreakpoint) { 2354 uint64_t value = m_stop_info_sp->GetValue(); 2355 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext()); 2356 if (reg_ctx_sp) 2357 { 2358 lldb::addr_t pc = reg_ctx_sp->GetPC(); 2359 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc); 2360 if (bp_site_sp && 2361 static_cast<break_id_t>(value) == bp_site_sp->GetID()) 2362 return true; 2363 } 2364 } 2365 } 2366 return false; 2367 } 2368 2369 2370 Error 2371 Thread::StepIn (bool source_step, 2372 LazyBool step_in_avoids_code_without_debug_info, 2373 LazyBool step_out_avoids_code_without_debug_info) 2374 2375 { 2376 Error error; 2377 Process *process = GetProcess().get(); 2378 if (StateIsStoppedState (process->GetState(), true)) 2379 { 2380 StackFrameSP frame_sp = GetStackFrameAtIndex (0); 2381 ThreadPlanSP new_plan_sp; 2382 const lldb::RunMode run_mode = eOnlyThisThread; 2383 const bool abort_other_plans = false; 2384 2385 if (source_step && frame_sp && frame_sp->HasDebugInformation ()) 2386 { 2387 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything)); 2388 new_plan_sp = QueueThreadPlanForStepInRange (abort_other_plans, 2389 sc.line_entry.range, 2390 sc, 2391 NULL, 2392 run_mode, 2393 step_in_avoids_code_without_debug_info, 2394 step_out_avoids_code_without_debug_info); 2395 } 2396 else 2397 { 2398 new_plan_sp = QueueThreadPlanForStepSingleInstruction (false, 2399 abort_other_plans, 2400 run_mode); 2401 } 2402 2403 new_plan_sp->SetIsMasterPlan(true); 2404 new_plan_sp->SetOkayToDiscard(false); 2405 2406 // Why do we need to set the current thread by ID here??? 2407 process->GetThreadList().SetSelectedThreadByID (GetID()); 2408 error = process->Resume(); 2409 } 2410 else 2411 { 2412 error.SetErrorString("process not stopped"); 2413 } 2414 return error; 2415 } 2416 2417 Error 2418 Thread::StepOver (bool source_step, 2419 LazyBool step_out_avoids_code_without_debug_info) 2420 { 2421 Error error; 2422 Process *process = GetProcess().get(); 2423 if (StateIsStoppedState (process->GetState(), true)) 2424 { 2425 StackFrameSP frame_sp = GetStackFrameAtIndex (0); 2426 ThreadPlanSP new_plan_sp; 2427 2428 const lldb::RunMode run_mode = eOnlyThisThread; 2429 const bool abort_other_plans = false; 2430 2431 if (source_step && frame_sp && frame_sp->HasDebugInformation ()) 2432 { 2433 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything)); 2434 new_plan_sp = QueueThreadPlanForStepOverRange (abort_other_plans, 2435 sc.line_entry.range, 2436 sc, 2437 run_mode, 2438 step_out_avoids_code_without_debug_info); 2439 } 2440 else 2441 { 2442 new_plan_sp = QueueThreadPlanForStepSingleInstruction (true, 2443 abort_other_plans, 2444 run_mode); 2445 } 2446 2447 new_plan_sp->SetIsMasterPlan(true); 2448 new_plan_sp->SetOkayToDiscard(false); 2449 2450 // Why do we need to set the current thread by ID here??? 2451 process->GetThreadList().SetSelectedThreadByID (GetID()); 2452 error = process->Resume(); 2453 } 2454 else 2455 { 2456 error.SetErrorString("process not stopped"); 2457 } 2458 return error; 2459 } 2460 2461 Error 2462 Thread::StepOut () 2463 { 2464 Error error; 2465 Process *process = GetProcess().get(); 2466 if (StateIsStoppedState (process->GetState(), true)) 2467 { 2468 const bool first_instruction = false; 2469 const bool stop_other_threads = false; 2470 const bool abort_other_plans = false; 2471 2472 ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut (abort_other_plans, 2473 NULL, 2474 first_instruction, 2475 stop_other_threads, 2476 eVoteYes, 2477 eVoteNoOpinion, 2478 0)); 2479 2480 new_plan_sp->SetIsMasterPlan(true); 2481 new_plan_sp->SetOkayToDiscard(false); 2482 2483 // Why do we need to set the current thread by ID here??? 2484 process->GetThreadList().SetSelectedThreadByID (GetID()); 2485 error = process->Resume(); 2486 } 2487 else 2488 { 2489 error.SetErrorString("process not stopped"); 2490 } 2491 return error; 2492 } 2493