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