1 //===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Target/Thread.h"
15 #include "Plugins/Process/Utility/UnwindLLDB.h"
16 #include "Plugins/Process/Utility/UnwindMacOSXFrameBackchain.h"
17 #include "lldb/Breakpoint/BreakpointLocation.h"
18 #include "lldb/Core/Debugger.h"
19 #include "lldb/Core/FormatEntity.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/State.h"
22 #include "lldb/Core/ValueObject.h"
23 #include "lldb/Host/Host.h"
24 #include "lldb/Interpreter/OptionValueFileSpecList.h"
25 #include "lldb/Interpreter/OptionValueProperties.h"
26 #include "lldb/Interpreter/Property.h"
27 #include "lldb/Symbol/Function.h"
28 #include "lldb/Target/ABI.h"
29 #include "lldb/Target/DynamicLoader.h"
30 #include "lldb/Target/ExecutionContext.h"
31 #include "lldb/Target/Process.h"
32 #include "lldb/Target/RegisterContext.h"
33 #include "lldb/Target/StopInfo.h"
34 #include "lldb/Target/SystemRuntime.h"
35 #include "lldb/Target/Target.h"
36 #include "lldb/Target/ThreadPlan.h"
37 #include "lldb/Target/ThreadPlanBase.h"
38 #include "lldb/Target/ThreadPlanCallFunction.h"
39 #include "lldb/Target/ThreadPlanPython.h"
40 #include "lldb/Target/ThreadPlanRunToAddress.h"
41 #include "lldb/Target/ThreadPlanStepInRange.h"
42 #include "lldb/Target/ThreadPlanStepInstruction.h"
43 #include "lldb/Target/ThreadPlanStepOut.h"
44 #include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
45 #include "lldb/Target/ThreadPlanStepOverRange.h"
46 #include "lldb/Target/ThreadPlanStepThrough.h"
47 #include "lldb/Target/ThreadPlanStepUntil.h"
48 #include "lldb/Target/ThreadSpec.h"
49 #include "lldb/Target/Unwind.h"
50 #include "lldb/Utility/Log.h"
51 #include "lldb/Utility/RegularExpression.h"
52 #include "lldb/Utility/Stream.h"
53 #include "lldb/Utility/StreamString.h"
56 using namespace lldb_private;
58 const ThreadPropertiesSP &Thread::GetGlobalProperties() {
59 // NOTE: intentional leak so we don't crash if global destructor chain gets
60 // called as other threads still use the result of this function
61 static ThreadPropertiesSP *g_settings_sp_ptr =
62 new ThreadPropertiesSP(new ThreadProperties(true));
63 return *g_settings_sp_ptr;
66 static PropertyDefinition g_properties[] = {
67 {"step-in-avoid-nodebug", OptionValue::eTypeBoolean, true, true, nullptr,
69 "If true, step-in will not stop in functions with no debug information."},
70 {"step-out-avoid-nodebug", OptionValue::eTypeBoolean, true, false, nullptr,
71 nullptr, "If true, when step-in/step-out/step-over leave the current "
72 "frame, they will continue to step out till they come to a "
74 "debug information. Passing a frame argument to step-out will "
75 "override this option."},
76 {"step-avoid-regexp", OptionValue::eTypeRegex, true, 0, "^std::", nullptr,
77 "A regular expression defining functions step-in won't stop in."},
78 {"step-avoid-libraries", OptionValue::eTypeFileSpecList, true, 0, nullptr,
79 nullptr, "A list of libraries that source stepping won't stop in."},
80 {"trace-thread", OptionValue::eTypeBoolean, false, false, nullptr, nullptr,
81 "If true, this thread will single-step and log execution."},
82 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}};
85 ePropertyStepInAvoidsNoDebug,
86 ePropertyStepOutAvoidsNoDebug,
87 ePropertyStepAvoidRegex,
88 ePropertyStepAvoidLibraries,
89 ePropertyEnableThreadTrace
92 class ThreadOptionValueProperties : public OptionValueProperties {
94 ThreadOptionValueProperties(const ConstString &name)
95 : OptionValueProperties(name) {}
97 // This constructor is used when creating ThreadOptionValueProperties when it
98 // is part of a new lldb_private::Thread instance. It will copy all current
99 // global property values as needed
100 ThreadOptionValueProperties(ThreadProperties *global_properties)
101 : OptionValueProperties(*global_properties->GetValueProperties()) {}
103 const Property *GetPropertyAtIndex(const ExecutionContext *exe_ctx,
105 uint32_t idx) const override {
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
109 // use the one from this instance.
111 Thread *thread = exe_ctx->GetThreadPtr();
113 ThreadOptionValueProperties *instance_properties =
114 static_cast<ThreadOptionValueProperties *>(
115 thread->GetValueProperties().get());
116 if (this != instance_properties)
117 return instance_properties->ProtectedGetPropertyAtIndex(idx);
120 return ProtectedGetPropertyAtIndex(idx);
124 ThreadProperties::ThreadProperties(bool is_global) : Properties() {
126 m_collection_sp.reset(
127 new ThreadOptionValueProperties(ConstString("thread")));
128 m_collection_sp->Initialize(g_properties);
130 m_collection_sp.reset(
131 new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
134 ThreadProperties::~ThreadProperties() = default;
136 const RegularExpression *ThreadProperties::GetSymbolsToAvoidRegexp() {
137 const uint32_t idx = ePropertyStepAvoidRegex;
138 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex(nullptr, idx);
141 FileSpecList &ThreadProperties::GetLibrariesToAvoid() const {
142 const uint32_t idx = ePropertyStepAvoidLibraries;
143 OptionValueFileSpecList *option_value =
144 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
146 assert(option_value);
147 return option_value->GetCurrentValue();
150 bool ThreadProperties::GetTraceEnabledState() const {
151 const uint32_t idx = ePropertyEnableThreadTrace;
152 return m_collection_sp->GetPropertyAtIndexAsBoolean(
153 nullptr, idx, g_properties[idx].default_uint_value != 0);
156 bool ThreadProperties::GetStepInAvoidsNoDebug() const {
157 const uint32_t idx = ePropertyStepInAvoidsNoDebug;
158 return m_collection_sp->GetPropertyAtIndexAsBoolean(
159 nullptr, idx, g_properties[idx].default_uint_value != 0);
162 bool ThreadProperties::GetStepOutAvoidsNoDebug() const {
163 const uint32_t idx = ePropertyStepOutAvoidsNoDebug;
164 return m_collection_sp->GetPropertyAtIndexAsBoolean(
165 nullptr, idx, g_properties[idx].default_uint_value != 0);
168 //------------------------------------------------------------------
170 //------------------------------------------------------------------
172 const ConstString &Thread::ThreadEventData::GetFlavorString() {
173 static ConstString g_flavor("Thread::ThreadEventData");
177 Thread::ThreadEventData::ThreadEventData(const lldb::ThreadSP thread_sp)
178 : m_thread_sp(thread_sp), m_stack_id() {}
180 Thread::ThreadEventData::ThreadEventData(const lldb::ThreadSP thread_sp,
181 const StackID &stack_id)
182 : m_thread_sp(thread_sp), m_stack_id(stack_id) {}
184 Thread::ThreadEventData::ThreadEventData() : m_thread_sp(), m_stack_id() {}
186 Thread::ThreadEventData::~ThreadEventData() = default;
188 void Thread::ThreadEventData::Dump(Stream *s) const {}
190 const Thread::ThreadEventData *
191 Thread::ThreadEventData::GetEventDataFromEvent(const Event *event_ptr) {
193 const EventData *event_data = event_ptr->GetData();
195 event_data->GetFlavor() == ThreadEventData::GetFlavorString())
196 return static_cast<const ThreadEventData *>(event_ptr->GetData());
201 ThreadSP Thread::ThreadEventData::GetThreadFromEvent(const Event *event_ptr) {
203 const ThreadEventData *event_data = GetEventDataFromEvent(event_ptr);
205 thread_sp = event_data->GetThread();
209 StackID Thread::ThreadEventData::GetStackIDFromEvent(const Event *event_ptr) {
211 const ThreadEventData *event_data = GetEventDataFromEvent(event_ptr);
213 stack_id = event_data->GetStackID();
218 Thread::ThreadEventData::GetStackFrameFromEvent(const Event *event_ptr) {
219 const ThreadEventData *event_data = GetEventDataFromEvent(event_ptr);
220 StackFrameSP frame_sp;
222 ThreadSP thread_sp = event_data->GetThread();
224 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID(
225 event_data->GetStackID());
231 //------------------------------------------------------------------
233 //------------------------------------------------------------------
235 ConstString &Thread::GetStaticBroadcasterClass() {
236 static ConstString class_name("lldb.thread");
240 Thread::Thread(Process &process, lldb::tid_t tid, bool use_invalid_index_id)
241 : ThreadProperties(false), UserID(tid),
242 Broadcaster(process.GetTarget().GetDebugger().GetBroadcasterManager(),
243 Thread::GetStaticBroadcasterClass().AsCString()),
244 m_process_wp(process.shared_from_this()), m_stop_info_sp(),
245 m_stop_info_stop_id(0), m_stop_info_override_stop_id(0),
246 m_index_id(use_invalid_index_id ? LLDB_INVALID_INDEX32
247 : process.GetNextThreadIndexID(tid)),
248 m_reg_context_sp(), m_state(eStateUnloaded), m_state_mutex(),
249 m_plan_stack(), m_completed_plan_stack(), m_frame_mutex(),
250 m_curr_frames_sp(), m_prev_frames_sp(),
251 m_resume_signal(LLDB_INVALID_SIGNAL_NUMBER),
252 m_resume_state(eStateRunning), m_temporary_resume_state(eStateRunning),
253 m_unwinder_ap(), m_destroy_called(false),
254 m_override_should_notify(eLazyBoolCalculate),
255 m_extended_info_fetched(false), m_extended_info() {
256 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
258 log->Printf("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")",
259 static_cast<void *>(this), GetID());
261 CheckInWithManager();
262 QueueFundamentalPlan(true);
266 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
268 log->Printf("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")",
269 static_cast<void *>(this), GetID());
270 /// If you hit this assert, it means your derived class forgot to call
271 /// DoDestroy in its destructor.
272 assert(m_destroy_called);
275 void Thread::DestroyThread() {
276 // Tell any plans on the plan stacks that the thread is being destroyed since
277 // any plans that have a thread go away in the middle of might need
278 // to do cleanup, or in some cases NOT do cleanup...
279 for (auto plan : m_plan_stack)
280 plan->ThreadDestroyed();
282 for (auto plan : m_discarded_plan_stack)
283 plan->ThreadDestroyed();
285 for (auto plan : m_completed_plan_stack)
286 plan->ThreadDestroyed();
288 m_destroy_called = true;
289 m_plan_stack.clear();
290 m_discarded_plan_stack.clear();
291 m_completed_plan_stack.clear();
293 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming
295 // plan stack is never empty, but if somebody errantly asks questions of a
297 // without checking first whether it is destroyed, they won't crash.
298 ThreadPlanSP null_plan_sp(new ThreadPlanNull(*this));
299 m_plan_stack.push_back(null_plan_sp);
301 m_stop_info_sp.reset();
302 m_reg_context_sp.reset();
303 m_unwinder_ap.reset();
304 std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
305 m_curr_frames_sp.reset();
306 m_prev_frames_sp.reset();
309 void Thread::BroadcastSelectedFrameChange(StackID &new_frame_id) {
310 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
311 BroadcastEvent(eBroadcastBitSelectedFrameChanged,
312 new ThreadEventData(this->shared_from_this(), new_frame_id));
315 lldb::StackFrameSP Thread::GetSelectedFrame() {
316 StackFrameListSP stack_frame_list_sp(GetStackFrameList());
317 StackFrameSP frame_sp = stack_frame_list_sp->GetFrameAtIndex(
318 stack_frame_list_sp->GetSelectedFrameIndex());
319 FunctionOptimizationWarning(frame_sp.get());
323 uint32_t Thread::SetSelectedFrame(lldb_private::StackFrame *frame,
325 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
327 BroadcastSelectedFrameChange(frame->GetStackID());
328 FunctionOptimizationWarning(frame);
332 bool Thread::SetSelectedFrameByIndex(uint32_t frame_idx, bool broadcast) {
333 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex(frame_idx));
335 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
337 BroadcastSelectedFrameChange(frame_sp->GetStackID());
338 FunctionOptimizationWarning(frame_sp.get());
344 bool Thread::SetSelectedFrameByIndexNoisily(uint32_t frame_idx,
345 Stream &output_stream) {
346 const bool broadcast = true;
347 bool success = SetSelectedFrameByIndex(frame_idx, broadcast);
349 StackFrameSP frame_sp = GetSelectedFrame();
351 bool already_shown = false;
352 SymbolContext frame_sc(
353 frame_sp->GetSymbolContext(eSymbolContextLineEntry));
354 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() &&
355 frame_sc.line_entry.file && frame_sc.line_entry.line != 0) {
356 already_shown = Host::OpenFileInExternalEditor(
357 frame_sc.line_entry.file, frame_sc.line_entry.line);
360 bool show_frame_info = true;
361 bool show_source = !already_shown;
362 FunctionOptimizationWarning(frame_sp.get());
363 return frame_sp->GetStatus(output_stream, show_frame_info, show_source);
370 void Thread::FunctionOptimizationWarning(StackFrame *frame) {
371 if (frame && frame->HasDebugInformation() &&
372 GetProcess()->GetWarningsOptimization()) {
374 frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextModule);
375 GetProcess()->PrintWarningOptimization(sc);
379 lldb::StopInfoSP Thread::GetStopInfo() {
380 if (m_destroy_called)
381 return m_stop_info_sp;
383 ThreadPlanSP completed_plan_sp(GetCompletedPlan());
384 ProcessSP process_sp(GetProcess());
385 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
387 // Here we select the stop info according to priorirty:
388 // - m_stop_info_sp (if not trace) - preset value
389 // - completed plan stop info - new value with plan from completed plan stack
390 // - m_stop_info_sp (trace stop reason is OK now)
391 // - ask GetPrivateStopInfo to set stop info
393 bool have_valid_stop_info = m_stop_info_sp &&
394 m_stop_info_sp ->IsValid() &&
395 m_stop_info_stop_id == stop_id;
396 bool have_valid_completed_plan = completed_plan_sp && completed_plan_sp->PlanSucceeded();
397 bool plan_overrides_trace =
398 have_valid_stop_info && have_valid_completed_plan
399 && (m_stop_info_sp->GetStopReason() == eStopReasonTrace);
401 if (have_valid_stop_info && !plan_overrides_trace) {
402 return m_stop_info_sp;
403 } else if (have_valid_completed_plan) {
404 return StopInfo::CreateStopReasonWithPlan(
405 completed_plan_sp, GetReturnValueObject(), GetExpressionVariable());
407 GetPrivateStopInfo();
408 return m_stop_info_sp;
412 lldb::StopInfoSP Thread::GetPrivateStopInfo() {
413 if (m_destroy_called)
414 return m_stop_info_sp;
416 ProcessSP process_sp(GetProcess());
418 const uint32_t process_stop_id = process_sp->GetStopID();
419 if (m_stop_info_stop_id != process_stop_id) {
420 if (m_stop_info_sp) {
421 if (m_stop_info_sp->IsValid() || IsStillAtLastBreakpointHit() ||
422 GetCurrentPlan()->IsVirtualStep())
423 SetStopInfo(m_stop_info_sp);
425 m_stop_info_sp.reset();
428 if (!m_stop_info_sp) {
429 if (!CalculateStopInfo())
430 SetStopInfo(StopInfoSP());
434 // The stop info can be manually set by calling Thread::SetStopInfo()
435 // prior to this function ever getting called, so we can't rely on
436 // "m_stop_info_stop_id != process_stop_id" as the condition for
437 // the if statement below, we must also check the stop info to see
438 // if we need to override it. See the header documentation in
439 // Process::GetStopInfoOverrideCallback() for more information on
440 // the stop info override callback.
441 if (m_stop_info_override_stop_id != process_stop_id) {
442 m_stop_info_override_stop_id = process_stop_id;
443 if (m_stop_info_sp) {
444 ArchSpec::StopInfoOverrideCallbackType callback =
445 GetProcess()->GetStopInfoOverrideCallback();
451 return m_stop_info_sp;
454 lldb::StopReason Thread::GetStopReason() {
455 lldb::StopInfoSP stop_info_sp(GetStopInfo());
457 return stop_info_sp->GetStopReason();
458 return eStopReasonNone;
461 bool Thread::StopInfoIsUpToDate() const {
462 ProcessSP process_sp(GetProcess());
464 return m_stop_info_stop_id == process_sp->GetStopID();
466 return true; // Process is no longer around so stop info is always up to
470 void Thread::ResetStopInfo() {
471 if (m_stop_info_sp) {
472 m_stop_info_sp.reset();
476 void Thread::SetStopInfo(const lldb::StopInfoSP &stop_info_sp) {
477 m_stop_info_sp = stop_info_sp;
478 if (m_stop_info_sp) {
479 m_stop_info_sp->MakeStopInfoValid();
480 // If we are overriding the ShouldReportStop, do that here:
481 if (m_override_should_notify != eLazyBoolCalculate)
482 m_stop_info_sp->OverrideShouldNotify(m_override_should_notify ==
486 ProcessSP process_sp(GetProcess());
488 m_stop_info_stop_id = process_sp->GetStopID();
490 m_stop_info_stop_id = UINT32_MAX;
491 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
493 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)",
494 static_cast<void *>(this), GetID(),
495 stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>",
496 m_stop_info_stop_id);
499 void Thread::SetShouldReportStop(Vote vote) {
500 if (vote == eVoteNoOpinion)
503 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
505 m_stop_info_sp->OverrideShouldNotify(m_override_should_notify ==
510 void Thread::SetStopInfoToNothing() {
511 // Note, we can't just NULL out the private reason, or the native thread
512 // implementation will try to
513 // go calculate it again. For now, just set it to a Unix Signal with an
514 // invalid signal number.
516 StopInfo::CreateStopReasonWithSignal(*this, LLDB_INVALID_SIGNAL_NUMBER));
519 bool Thread::ThreadStoppedForAReason(void) {
520 return (bool)GetPrivateStopInfo();
523 bool Thread::CheckpointThreadState(ThreadStateCheckpoint &saved_state) {
524 saved_state.register_backup_sp.reset();
525 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex(0));
527 lldb::RegisterCheckpointSP reg_checkpoint_sp(
528 new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
529 if (reg_checkpoint_sp) {
530 lldb::RegisterContextSP reg_ctx_sp(frame_sp->GetRegisterContext());
531 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues(*reg_checkpoint_sp))
532 saved_state.register_backup_sp = reg_checkpoint_sp;
535 if (!saved_state.register_backup_sp)
538 saved_state.stop_info_sp = GetStopInfo();
539 ProcessSP process_sp(GetProcess());
541 saved_state.orig_stop_id = process_sp->GetStopID();
542 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
543 saved_state.m_completed_plan_stack = m_completed_plan_stack;
548 bool Thread::RestoreRegisterStateFromCheckpoint(
549 ThreadStateCheckpoint &saved_state) {
550 if (saved_state.register_backup_sp) {
551 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex(0));
553 lldb::RegisterContextSP reg_ctx_sp(frame_sp->GetRegisterContext());
556 reg_ctx_sp->WriteAllRegisterValues(*saved_state.register_backup_sp);
558 // Clear out all stack frames as our world just changed.
560 reg_ctx_sp->InvalidateIfNeeded(true);
561 if (m_unwinder_ap.get())
562 m_unwinder_ap->Clear();
570 bool Thread::RestoreThreadStateFromCheckpoint(
571 ThreadStateCheckpoint &saved_state) {
572 if (saved_state.stop_info_sp)
573 saved_state.stop_info_sp->MakeStopInfoValid();
574 SetStopInfo(saved_state.stop_info_sp);
575 GetStackFrameList()->SetCurrentInlinedDepth(
576 saved_state.current_inlined_depth);
577 m_completed_plan_stack = saved_state.m_completed_plan_stack;
581 StateType Thread::GetState() const {
582 // If any other threads access this we will need a mutex for it
583 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
587 void Thread::SetState(StateType state) {
588 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
592 void Thread::WillStop() {
593 ThreadPlan *current_plan = GetCurrentPlan();
595 // FIXME: I may decide to disallow threads with no plans. In which
596 // case this should go to an assert.
601 current_plan->WillStop();
604 void Thread::SetupForResume() {
605 if (GetResumeState() != eStateSuspended) {
606 // If we're at a breakpoint push the step-over breakpoint plan. Do this
608 // telling the current plan it will resume, since we might change what the
612 lldb::RegisterContextSP reg_ctx_sp(GetRegisterContext());
614 const addr_t thread_pc = reg_ctx_sp->GetPC();
615 BreakpointSiteSP bp_site_sp =
616 GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc);
618 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target
619 // may not require anything
620 // special to step over a breakpoint.
622 ThreadPlan *cur_plan = GetCurrentPlan();
624 bool push_step_over_bp_plan = false;
625 if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint) {
626 ThreadPlanStepOverBreakpoint *bp_plan =
627 (ThreadPlanStepOverBreakpoint *)cur_plan;
628 if (bp_plan->GetBreakpointLoadAddress() != thread_pc)
629 push_step_over_bp_plan = true;
631 push_step_over_bp_plan = true;
633 if (push_step_over_bp_plan) {
634 ThreadPlanSP step_bp_plan_sp(new ThreadPlanStepOverBreakpoint(*this));
635 if (step_bp_plan_sp) {
636 step_bp_plan_sp->SetPrivate(true);
638 if (GetCurrentPlan()->RunState() != eStateStepping) {
639 ThreadPlanStepOverBreakpoint *step_bp_plan =
640 static_cast<ThreadPlanStepOverBreakpoint *>(
641 step_bp_plan_sp.get());
642 step_bp_plan->SetAutoContinue(true);
644 QueueThreadPlan(step_bp_plan_sp, false);
652 bool Thread::ShouldResume(StateType resume_state) {
653 // At this point clear the completed plan stack.
654 m_completed_plan_stack.clear();
655 m_discarded_plan_stack.clear();
656 m_override_should_notify = eLazyBoolCalculate;
658 StateType prev_resume_state = GetTemporaryResumeState();
660 SetTemporaryResumeState(resume_state);
662 lldb::ThreadSP backing_thread_sp(GetBackingThread());
663 if (backing_thread_sp)
664 backing_thread_sp->SetTemporaryResumeState(resume_state);
666 // Make sure m_stop_info_sp is valid. Don't do this for threads we suspended
667 // in the previous run.
668 if (prev_resume_state != eStateSuspended)
669 GetPrivateStopInfo();
671 // This is a little dubious, but we are trying to limit how often we actually
672 // fetch stop info from
673 // the target, 'cause that slows down single stepping. So assume that if we
674 // got to the point where
675 // we're about to resume, and we haven't yet had to fetch the stop reason,
676 // then it doesn't need to know
677 // about the fact that we are resuming...
678 const uint32_t process_stop_id = GetProcess()->GetStopID();
679 if (m_stop_info_stop_id == process_stop_id &&
680 (m_stop_info_sp && m_stop_info_sp->IsValid())) {
681 StopInfo *stop_info = GetPrivateStopInfo().get();
683 stop_info->WillResume(resume_state);
686 // Tell all the plans that we are about to resume in case they need to clear
688 // We distinguish between the plan on the top of the stack and the lower
689 // plans in case a plan needs to do any special business before it runs.
691 bool need_to_resume = false;
692 ThreadPlan *plan_ptr = GetCurrentPlan();
694 need_to_resume = plan_ptr->WillResume(resume_state, true);
696 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != nullptr) {
697 plan_ptr->WillResume(resume_state, false);
700 // If the WillResume for the plan says we are faking a resume, then it will
701 // have set an appropriate stop info.
702 // In that case, don't reset it here.
704 if (need_to_resume && resume_state != eStateSuspended) {
705 m_stop_info_sp.reset();
709 if (need_to_resume) {
711 // Let Thread subclasses do any special work they need to prior to resuming
712 WillResume(resume_state);
715 return need_to_resume;
718 void Thread::DidResume() { SetResumeSignal(LLDB_INVALID_SIGNAL_NUMBER); }
720 void Thread::DidStop() { SetState(eStateStopped); }
722 bool Thread::ShouldStop(Event *event_ptr) {
723 ThreadPlan *current_plan = GetCurrentPlan();
725 bool should_stop = true;
727 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
729 if (GetResumeState() == eStateSuspended) {
731 log->Printf("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64
732 ", should_stop = 0 (ignore since thread was suspended)",
733 __FUNCTION__, GetID(), GetProtocolID());
737 if (GetTemporaryResumeState() == eStateSuspended) {
739 log->Printf("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64
740 ", should_stop = 0 (ignore since thread was suspended)",
741 __FUNCTION__, GetID(), GetProtocolID());
745 // Based on the current thread plan and process stop info, check if this
746 // thread caused the process to stop. NOTE: this must take place before
747 // the plan is moved from the current plan stack to the completed plan
749 if (!ThreadStoppedForAReason()) {
751 log->Printf("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64
752 ", pc = 0x%16.16" PRIx64
753 ", should_stop = 0 (ignore since no stop reason)",
754 __FUNCTION__, GetID(), GetProtocolID(),
755 GetRegisterContext() ? GetRegisterContext()->GetPC()
756 : LLDB_INVALID_ADDRESS);
761 log->Printf("Thread::%s(%p) for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64
762 ", pc = 0x%16.16" PRIx64,
763 __FUNCTION__, static_cast<void *>(this), GetID(),
765 GetRegisterContext() ? GetRegisterContext()->GetPC()
766 : LLDB_INVALID_ADDRESS);
767 log->Printf("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
771 log->Printf("Plan stack initial state:\n%s", s.GetData());
774 // The top most plan always gets to do the trace log...
775 current_plan->DoTraceLog();
777 // First query the stop info's ShouldStopSynchronous. This handles
778 // "synchronous" stop reasons, for example the breakpoint
779 // command on internal breakpoints. If a synchronous stop reason says we
780 // should not stop, then we don't have to
781 // do any more work on this stop.
782 StopInfoSP private_stop_info(GetPrivateStopInfo());
783 if (private_stop_info &&
784 !private_stop_info->ShouldStopSynchronous(event_ptr)) {
786 log->Printf("StopInfo::ShouldStop async callback says we should not "
787 "stop, returning ShouldStop of false.");
791 // If we've already been restarted, don't query the plans since the state they
792 // would examine is not current.
793 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
796 // Before the plans see the state of the world, calculate the current inlined
798 GetStackFrameList()->CalculateCurrentInlinedDepth();
800 // If the base plan doesn't understand why we stopped, then we have to find a
802 // If that plan is still working, then we don't need to do any more work. If
803 // the plan that explains
804 // the stop is done, then we should pop all the plans below it, and pop it,
805 // and then let the plans above it decide
806 // whether they still need to do more work.
808 bool done_processing_current_plan = false;
810 if (!current_plan->PlanExplainsStop(event_ptr)) {
811 if (current_plan->TracerExplainsStop()) {
812 done_processing_current_plan = true;
815 // If the current plan doesn't explain the stop, then find one that
816 // does and let it handle the situation.
817 ThreadPlan *plan_ptr = current_plan;
818 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != nullptr) {
819 if (plan_ptr->PlanExplainsStop(event_ptr)) {
820 should_stop = plan_ptr->ShouldStop(event_ptr);
822 // plan_ptr explains the stop, next check whether plan_ptr is done, if
823 // so, then we should take it
824 // and all the plans below it off the stack.
826 if (plan_ptr->MischiefManaged()) {
827 // We're going to pop the plans up to and including the plan that
828 // explains the stop.
829 ThreadPlan *prev_plan_ptr = GetPreviousPlan(plan_ptr);
833 current_plan->WillStop();
835 } while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
836 // Now, if the responsible plan was not "Okay to discard" then we're
838 // otherwise we forward this to the next plan in the stack below.
839 done_processing_current_plan =
840 (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard());
842 done_processing_current_plan = true;
850 if (!done_processing_current_plan) {
851 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
854 log->Printf("Plan %s explains stop, auto-continue %i.",
855 current_plan->GetName(), over_ride_stop);
857 // We're starting from the base plan, so just let it decide;
858 if (PlanIsBasePlan(current_plan)) {
859 should_stop = current_plan->ShouldStop(event_ptr);
861 log->Printf("Base plan says should stop: %i.", should_stop);
863 // Otherwise, don't let the base plan override what the other plans say to
865 // presumably if there were other plans they would know what to do...
867 if (PlanIsBasePlan(current_plan))
870 should_stop = current_plan->ShouldStop(event_ptr);
872 log->Printf("Plan %s should stop: %d.", current_plan->GetName(),
874 if (current_plan->MischiefManaged()) {
876 current_plan->WillStop();
878 // If a Master Plan wants to stop, and wants to stick on the stack, we
880 // Otherwise, see if the plan's parent wants to stop.
882 if (should_stop && current_plan->IsMasterPlan() &&
883 !current_plan->OkayToDiscard()) {
889 current_plan = GetCurrentPlan();
890 if (current_plan == nullptr) {
904 // One other potential problem is that we set up a master plan, then stop in
905 // before it is complete - for instance
906 // by hitting a breakpoint during a step-over - then do some step/finish/etc
907 // operations that wind up
908 // past the end point condition of the initial plan. We don't want to strand
909 // the original plan on the stack,
910 // This code clears stale plans off the stack.
913 ThreadPlan *plan_ptr = GetCurrentPlan();
915 // Discard the stale plans and all plans below them in the stack,
916 // plus move the completed plans to the completed plan stack
917 while (!PlanIsBasePlan(plan_ptr)) {
918 bool stale = plan_ptr->IsPlanStale();
919 ThreadPlan *examined_plan = plan_ptr;
920 plan_ptr = GetPreviousPlan(examined_plan);
925 "Plan %s being discarded in cleanup, it says it is already done.",
926 examined_plan->GetName());
927 while (GetCurrentPlan() != examined_plan) {
930 if (examined_plan->IsPlanComplete()) {
931 // plan is complete but does not explain the stop (example: step to a line
932 // with breakpoint), let us move the plan to completed_plan_stack anyway
944 log->Printf("Plan stack final state:\n%s", s.GetData());
945 log->Printf("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv",
951 Vote Thread::ShouldReportStop(Event *event_ptr) {
952 StateType thread_state = GetResumeState();
953 StateType temp_thread_state = GetTemporaryResumeState();
955 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
957 if (thread_state == eStateSuspended || thread_state == eStateInvalid) {
959 log->Printf("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
960 ": returning vote %i (state was suspended or invalid)",
961 GetID(), eVoteNoOpinion);
962 return eVoteNoOpinion;
965 if (temp_thread_state == eStateSuspended ||
966 temp_thread_state == eStateInvalid) {
969 "Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
970 ": returning vote %i (temporary state was suspended or invalid)",
971 GetID(), eVoteNoOpinion);
972 return eVoteNoOpinion;
975 if (!ThreadStoppedForAReason()) {
977 log->Printf("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
978 ": returning vote %i (thread didn't stop for a reason.)",
979 GetID(), eVoteNoOpinion);
980 return eVoteNoOpinion;
983 if (m_completed_plan_stack.size() > 0) {
984 // Don't use GetCompletedPlan here, since that suppresses private plans.
986 log->Printf("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
987 ": returning vote for complete stack's back plan",
989 return m_completed_plan_stack.back()->ShouldReportStop(event_ptr);
991 Vote thread_vote = eVoteNoOpinion;
992 ThreadPlan *plan_ptr = GetCurrentPlan();
994 if (plan_ptr->PlanExplainsStop(event_ptr)) {
995 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
998 if (PlanIsBasePlan(plan_ptr))
1001 plan_ptr = GetPreviousPlan(plan_ptr);
1004 log->Printf("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
1005 ": returning vote %i for current plan",
1006 GetID(), thread_vote);
1012 Vote Thread::ShouldReportRun(Event *event_ptr) {
1013 StateType thread_state = GetResumeState();
1015 if (thread_state == eStateSuspended || thread_state == eStateInvalid) {
1016 return eVoteNoOpinion;
1019 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1020 if (m_completed_plan_stack.size() > 0) {
1021 // Don't use GetCompletedPlan here, since that suppresses private plans.
1023 log->Printf("Current Plan for thread %d(%p) (0x%4.4" PRIx64
1024 ", %s): %s being asked whether we should report run.",
1025 GetIndexID(), static_cast<void *>(this), GetID(),
1026 StateAsCString(GetTemporaryResumeState()),
1027 m_completed_plan_stack.back()->GetName());
1029 return m_completed_plan_stack.back()->ShouldReportRun(event_ptr);
1032 log->Printf("Current Plan for thread %d(%p) (0x%4.4" PRIx64
1033 ", %s): %s being asked whether we should report run.",
1034 GetIndexID(), static_cast<void *>(this), GetID(),
1035 StateAsCString(GetTemporaryResumeState()),
1036 GetCurrentPlan()->GetName());
1038 return GetCurrentPlan()->ShouldReportRun(event_ptr);
1042 bool Thread::MatchesSpec(const ThreadSpec *spec) {
1043 return (spec == nullptr) ? true : spec->ThreadPassesBasicTests(*this);
1046 void Thread::PushPlan(ThreadPlanSP &thread_plan_sp) {
1047 if (thread_plan_sp) {
1048 // If the thread plan doesn't already have a tracer, give it its parent's
1050 if (!thread_plan_sp->GetThreadPlanTracer())
1051 thread_plan_sp->SetThreadPlanTracer(
1052 m_plan_stack.back()->GetThreadPlanTracer());
1053 m_plan_stack.push_back(thread_plan_sp);
1055 thread_plan_sp->DidPush();
1057 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1060 thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull);
1061 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
1062 static_cast<void *>(this), s.GetData(),
1063 thread_plan_sp->GetThread().GetID());
1068 void Thread::PopPlan() {
1069 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1071 if (m_plan_stack.size() <= 1)
1074 ThreadPlanSP &plan = m_plan_stack.back();
1076 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
1077 plan->GetName(), plan->GetThread().GetID());
1079 m_completed_plan_stack.push_back(plan);
1081 m_plan_stack.pop_back();
1085 void Thread::DiscardPlan() {
1086 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1087 if (m_plan_stack.size() > 1) {
1088 ThreadPlanSP &plan = m_plan_stack.back();
1090 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
1091 plan->GetName(), plan->GetThread().GetID());
1093 m_discarded_plan_stack.push_back(plan);
1095 m_plan_stack.pop_back();
1099 ThreadPlan *Thread::GetCurrentPlan() {
1100 // There will always be at least the base plan. If somebody is mucking with a
1101 // thread with an empty plan stack, we should assert right away.
1102 return m_plan_stack.empty() ? nullptr : m_plan_stack.back().get();
1105 ThreadPlanSP Thread::GetCompletedPlan() {
1106 ThreadPlanSP empty_plan_sp;
1107 if (!m_completed_plan_stack.empty()) {
1108 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) {
1109 ThreadPlanSP completed_plan_sp;
1110 completed_plan_sp = m_completed_plan_stack[i];
1111 if (!completed_plan_sp->GetPrivate())
1112 return completed_plan_sp;
1115 return empty_plan_sp;
1118 ValueObjectSP Thread::GetReturnValueObject() {
1119 if (!m_completed_plan_stack.empty()) {
1120 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) {
1121 ValueObjectSP return_valobj_sp;
1122 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1123 if (return_valobj_sp)
1124 return return_valobj_sp;
1127 return ValueObjectSP();
1130 ExpressionVariableSP Thread::GetExpressionVariable() {
1131 if (!m_completed_plan_stack.empty()) {
1132 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) {
1133 ExpressionVariableSP expression_variable_sp;
1134 expression_variable_sp =
1135 m_completed_plan_stack[i]->GetExpressionVariable();
1136 if (expression_variable_sp)
1137 return expression_variable_sp;
1140 return ExpressionVariableSP();
1143 bool Thread::IsThreadPlanDone(ThreadPlan *plan) {
1144 if (!m_completed_plan_stack.empty()) {
1145 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) {
1146 if (m_completed_plan_stack[i].get() == plan)
1153 bool Thread::WasThreadPlanDiscarded(ThreadPlan *plan) {
1154 if (!m_discarded_plan_stack.empty()) {
1155 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--) {
1156 if (m_discarded_plan_stack[i].get() == plan)
1163 bool Thread::CompletedPlanOverridesBreakpoint() {
1164 return (!m_completed_plan_stack.empty()) ;
1167 ThreadPlan *Thread::GetPreviousPlan(ThreadPlan *current_plan) {
1168 if (current_plan == nullptr)
1171 int stack_size = m_completed_plan_stack.size();
1172 for (int i = stack_size - 1; i > 0; i--) {
1173 if (current_plan == m_completed_plan_stack[i].get())
1174 return m_completed_plan_stack[i - 1].get();
1177 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan) {
1178 return GetCurrentPlan();
1181 stack_size = m_plan_stack.size();
1182 for (int i = stack_size - 1; i > 0; i--) {
1183 if (current_plan == m_plan_stack[i].get())
1184 return m_plan_stack[i - 1].get();
1189 void Thread::QueueThreadPlan(ThreadPlanSP &thread_plan_sp,
1190 bool abort_other_plans) {
1191 if (abort_other_plans)
1192 DiscardThreadPlans(true);
1194 PushPlan(thread_plan_sp);
1197 void Thread::EnableTracer(bool value, bool single_stepping) {
1198 int stack_size = m_plan_stack.size();
1199 for (int i = 0; i < stack_size; i++) {
1200 if (m_plan_stack[i]->GetThreadPlanTracer()) {
1201 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1202 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1207 void Thread::SetTracer(lldb::ThreadPlanTracerSP &tracer_sp) {
1208 int stack_size = m_plan_stack.size();
1209 for (int i = 0; i < stack_size; i++)
1210 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1213 bool Thread::DiscardUserThreadPlansUpToIndex(uint32_t thread_index) {
1214 // Count the user thread plans from the back end to get the number of the one
1219 ThreadPlan *up_to_plan_ptr = nullptr;
1221 for (ThreadPlanSP plan_sp : m_plan_stack) {
1222 if (plan_sp->GetPrivate())
1224 if (idx == thread_index) {
1225 up_to_plan_ptr = plan_sp.get();
1231 if (up_to_plan_ptr == nullptr)
1234 DiscardThreadPlansUpToPlan(up_to_plan_ptr);
1238 void Thread::DiscardThreadPlansUpToPlan(lldb::ThreadPlanSP &up_to_plan_sp) {
1239 DiscardThreadPlansUpToPlan(up_to_plan_sp.get());
1242 void Thread::DiscardThreadPlansUpToPlan(ThreadPlan *up_to_plan_ptr) {
1243 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1245 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64
1247 GetID(), static_cast<void *>(up_to_plan_ptr));
1249 int stack_size = m_plan_stack.size();
1251 // If the input plan is nullptr, discard all plans. Otherwise make sure this
1253 // stack, and if so discard up to and including it.
1255 if (up_to_plan_ptr == nullptr) {
1256 for (int i = stack_size - 1; i > 0; i--)
1259 bool found_it = false;
1260 for (int i = stack_size - 1; i > 0; i--) {
1261 if (m_plan_stack[i].get() == up_to_plan_ptr)
1265 bool last_one = false;
1266 for (int i = stack_size - 1; i > 0 && !last_one; i--) {
1267 if (GetCurrentPlan() == up_to_plan_ptr)
1275 void Thread::DiscardThreadPlans(bool force) {
1276 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1278 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64
1284 int stack_size = m_plan_stack.size();
1285 for (int i = stack_size - 1; i > 0; i--) {
1292 int master_plan_idx;
1293 bool discard = true;
1295 // Find the first master plan, see if it wants discarding, and if yes
1296 // discard up to it.
1297 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0;
1298 master_plan_idx--) {
1299 if (m_plan_stack[master_plan_idx]->IsMasterPlan()) {
1300 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1306 // First pop all the dependent plans:
1307 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--) {
1308 // FIXME: Do we need a finalize here, or is the rule that
1310 // for the plan leaves it in a state that it is safe to pop the plan
1311 // with no more notice?
1315 // Now discard the master plan itself.
1316 // The bottom-most plan never gets discarded. "OkayToDiscard" for it
1318 // discard it's dependent plans, but not it...
1319 if (master_plan_idx > 0) {
1323 // If the master plan doesn't want to get discarded, then we're done.
1329 bool Thread::PlanIsBasePlan(ThreadPlan *plan_ptr) {
1330 if (plan_ptr->IsBasePlan())
1332 else if (m_plan_stack.size() == 0)
1335 return m_plan_stack[0].get() == plan_ptr;
1338 Error Thread::UnwindInnermostExpression() {
1340 int stack_size = m_plan_stack.size();
1342 // If the input plan is nullptr, discard all plans. Otherwise make sure this
1344 // stack, and if so discard up to and including it.
1346 for (int i = stack_size - 1; i > 0; i--) {
1347 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction) {
1348 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1352 error.SetErrorString("No expressions currently active on this thread");
1356 ThreadPlanSP Thread::QueueFundamentalPlan(bool abort_other_plans) {
1357 ThreadPlanSP thread_plan_sp(new ThreadPlanBase(*this));
1358 QueueThreadPlan(thread_plan_sp, abort_other_plans);
1359 return thread_plan_sp;
1362 ThreadPlanSP Thread::QueueThreadPlanForStepSingleInstruction(
1363 bool step_over, bool abort_other_plans, bool stop_other_threads) {
1364 ThreadPlanSP thread_plan_sp(new ThreadPlanStepInstruction(
1365 *this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1366 QueueThreadPlan(thread_plan_sp, abort_other_plans);
1367 return thread_plan_sp;
1370 ThreadPlanSP Thread::QueueThreadPlanForStepOverRange(
1371 bool abort_other_plans, const AddressRange &range,
1372 const SymbolContext &addr_context, lldb::RunMode stop_other_threads,
1373 LazyBool step_out_avoids_code_withoug_debug_info) {
1374 ThreadPlanSP thread_plan_sp;
1375 thread_plan_sp.reset(new ThreadPlanStepOverRange(
1376 *this, range, addr_context, stop_other_threads,
1377 step_out_avoids_code_withoug_debug_info));
1379 QueueThreadPlan(thread_plan_sp, abort_other_plans);
1380 return thread_plan_sp;
1383 // Call the QueueThreadPlanForStepOverRange method which takes an address range.
1384 ThreadPlanSP Thread::QueueThreadPlanForStepOverRange(
1385 bool abort_other_plans, const LineEntry &line_entry,
1386 const SymbolContext &addr_context, lldb::RunMode stop_other_threads,
1387 LazyBool step_out_avoids_code_withoug_debug_info) {
1388 return QueueThreadPlanForStepOverRange(
1389 abort_other_plans, line_entry.GetSameLineContiguousAddressRange(),
1390 addr_context, stop_other_threads,
1391 step_out_avoids_code_withoug_debug_info);
1394 ThreadPlanSP Thread::QueueThreadPlanForStepInRange(
1395 bool abort_other_plans, const AddressRange &range,
1396 const SymbolContext &addr_context, const char *step_in_target,
1397 lldb::RunMode stop_other_threads,
1398 LazyBool step_in_avoids_code_without_debug_info,
1399 LazyBool step_out_avoids_code_without_debug_info) {
1400 ThreadPlanSP thread_plan_sp(
1401 new ThreadPlanStepInRange(*this, range, addr_context, stop_other_threads,
1402 step_in_avoids_code_without_debug_info,
1403 step_out_avoids_code_without_debug_info));
1404 ThreadPlanStepInRange *plan =
1405 static_cast<ThreadPlanStepInRange *>(thread_plan_sp.get());
1408 plan->SetStepInTarget(step_in_target);
1410 QueueThreadPlan(thread_plan_sp, abort_other_plans);
1411 return thread_plan_sp;
1414 // Call the QueueThreadPlanForStepInRange method which takes an address range.
1415 ThreadPlanSP Thread::QueueThreadPlanForStepInRange(
1416 bool abort_other_plans, const LineEntry &line_entry,
1417 const SymbolContext &addr_context, const char *step_in_target,
1418 lldb::RunMode stop_other_threads,
1419 LazyBool step_in_avoids_code_without_debug_info,
1420 LazyBool step_out_avoids_code_without_debug_info) {
1421 return QueueThreadPlanForStepInRange(
1422 abort_other_plans, line_entry.GetSameLineContiguousAddressRange(),
1423 addr_context, step_in_target, stop_other_threads,
1424 step_in_avoids_code_without_debug_info,
1425 step_out_avoids_code_without_debug_info);
1428 ThreadPlanSP Thread::QueueThreadPlanForStepOut(
1429 bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
1430 bool stop_other_threads, Vote stop_vote, Vote run_vote, uint32_t frame_idx,
1431 LazyBool step_out_avoids_code_without_debug_info) {
1432 ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut(
1433 *this, addr_context, first_insn, stop_other_threads, stop_vote, run_vote,
1434 frame_idx, step_out_avoids_code_without_debug_info));
1436 if (thread_plan_sp->ValidatePlan(nullptr)) {
1437 QueueThreadPlan(thread_plan_sp, abort_other_plans);
1438 return thread_plan_sp;
1440 return ThreadPlanSP();
1444 ThreadPlanSP Thread::QueueThreadPlanForStepOutNoShouldStop(
1445 bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
1446 bool stop_other_threads, Vote stop_vote, Vote run_vote, uint32_t frame_idx,
1447 bool continue_to_next_branch) {
1448 const bool calculate_return_value =
1449 false; // No need to calculate the return value here.
1450 ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut(
1451 *this, addr_context, first_insn, stop_other_threads, stop_vote, run_vote,
1452 frame_idx, eLazyBoolNo, continue_to_next_branch, calculate_return_value));
1454 ThreadPlanStepOut *new_plan =
1455 static_cast<ThreadPlanStepOut *>(thread_plan_sp.get());
1456 new_plan->ClearShouldStopHereCallbacks();
1458 if (thread_plan_sp->ValidatePlan(nullptr)) {
1459 QueueThreadPlan(thread_plan_sp, abort_other_plans);
1460 return thread_plan_sp;
1462 return ThreadPlanSP();
1466 ThreadPlanSP Thread::QueueThreadPlanForStepThrough(StackID &return_stack_id,
1467 bool abort_other_plans,
1468 bool stop_other_threads) {
1469 ThreadPlanSP thread_plan_sp(
1470 new ThreadPlanStepThrough(*this, return_stack_id, stop_other_threads));
1471 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan(nullptr))
1472 return ThreadPlanSP();
1474 QueueThreadPlan(thread_plan_sp, abort_other_plans);
1475 return thread_plan_sp;
1478 ThreadPlanSP Thread::QueueThreadPlanForRunToAddress(bool abort_other_plans,
1479 Address &target_addr,
1480 bool stop_other_threads) {
1481 ThreadPlanSP thread_plan_sp(
1482 new ThreadPlanRunToAddress(*this, target_addr, stop_other_threads));
1483 QueueThreadPlan(thread_plan_sp, abort_other_plans);
1484 return thread_plan_sp;
1487 ThreadPlanSP Thread::QueueThreadPlanForStepUntil(bool abort_other_plans,
1488 lldb::addr_t *address_list,
1489 size_t num_addresses,
1490 bool stop_other_threads,
1491 uint32_t frame_idx) {
1492 ThreadPlanSP thread_plan_sp(new ThreadPlanStepUntil(
1493 *this, address_list, num_addresses, stop_other_threads, frame_idx));
1494 QueueThreadPlan(thread_plan_sp, abort_other_plans);
1495 return thread_plan_sp;
1498 lldb::ThreadPlanSP Thread::QueueThreadPlanForStepScripted(
1499 bool abort_other_plans, const char *class_name, bool stop_other_threads) {
1500 ThreadPlanSP thread_plan_sp(new ThreadPlanPython(*this, class_name));
1501 QueueThreadPlan(thread_plan_sp, abort_other_plans);
1502 // This seems a little funny, but I don't want to have to split up the
1503 // constructor and the
1504 // DidPush in the scripted plan, that seems annoying.
1505 // That means the constructor has to be in DidPush.
1506 // So I have to validate the plan AFTER pushing it, and then take it off
1508 if (!thread_plan_sp->ValidatePlan(nullptr)) {
1509 DiscardThreadPlansUpToPlan(thread_plan_sp);
1510 return ThreadPlanSP();
1512 return thread_plan_sp;
1515 uint32_t Thread::GetIndexID() const { return m_index_id; }
1517 static void PrintPlanElement(Stream *s, const ThreadPlanSP &plan,
1518 lldb::DescriptionLevel desc_level,
1522 s->Printf("Element %d: ", elem_idx);
1523 plan->GetDescription(s, desc_level);
1528 static void PrintPlanStack(Stream *s,
1529 const std::vector<lldb::ThreadPlanSP> &plan_stack,
1530 lldb::DescriptionLevel desc_level,
1531 bool include_internal) {
1532 int32_t print_idx = 0;
1533 for (ThreadPlanSP plan_sp : plan_stack) {
1534 if (include_internal || !plan_sp->GetPrivate()) {
1535 PrintPlanElement(s, plan_sp, desc_level, print_idx++);
1540 void Thread::DumpThreadPlans(Stream *s, lldb::DescriptionLevel desc_level,
1541 bool include_internal,
1542 bool ignore_boring_threads) const {
1543 uint32_t stack_size;
1545 if (ignore_boring_threads) {
1546 uint32_t stack_size = m_plan_stack.size();
1547 uint32_t completed_stack_size = m_completed_plan_stack.size();
1548 uint32_t discarded_stack_size = m_discarded_plan_stack.size();
1549 if (stack_size == 1 && completed_stack_size == 0 &&
1550 discarded_stack_size == 0) {
1551 s->Printf("thread #%u: tid = 0x%4.4" PRIx64 "\n", GetIndexID(), GetID());
1554 s->Printf("No active thread plans\n");
1561 s->Printf("thread #%u: tid = 0x%4.4" PRIx64 ":\n", GetIndexID(), GetID());
1564 s->Printf("Active plan stack:\n");
1565 PrintPlanStack(s, m_plan_stack, desc_level, include_internal);
1567 stack_size = m_completed_plan_stack.size();
1568 if (stack_size > 0) {
1570 s->Printf("Completed Plan Stack:\n");
1571 PrintPlanStack(s, m_completed_plan_stack, desc_level, include_internal);
1574 stack_size = m_discarded_plan_stack.size();
1575 if (stack_size > 0) {
1577 s->Printf("Discarded Plan Stack:\n");
1578 PrintPlanStack(s, m_discarded_plan_stack, desc_level, include_internal);
1584 TargetSP Thread::CalculateTarget() {
1586 ProcessSP process_sp(GetProcess());
1588 target_sp = process_sp->CalculateTarget();
1592 ProcessSP Thread::CalculateProcess() { return GetProcess(); }
1594 ThreadSP Thread::CalculateThread() { return shared_from_this(); }
1596 StackFrameSP Thread::CalculateStackFrame() { return StackFrameSP(); }
1598 void Thread::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1599 exe_ctx.SetContext(shared_from_this());
1602 StackFrameListSP Thread::GetStackFrameList() {
1603 StackFrameListSP frame_list_sp;
1604 std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
1605 if (m_curr_frames_sp) {
1606 frame_list_sp = m_curr_frames_sp;
1608 frame_list_sp.reset(new StackFrameList(*this, m_prev_frames_sp, true));
1609 m_curr_frames_sp = frame_list_sp;
1611 return frame_list_sp;
1614 void Thread::ClearStackFrames() {
1615 std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
1617 Unwind *unwinder = GetUnwinder();
1621 // Only store away the old "reference" StackFrameList if we got all its
1623 // FIXME: At some point we can try to splice in the frames we have fetched
1625 // the new frame as we make it, but let's not try that now.
1626 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
1627 m_prev_frames_sp.swap(m_curr_frames_sp);
1628 m_curr_frames_sp.reset();
1630 m_extended_info.reset();
1631 m_extended_info_fetched = false;
1634 lldb::StackFrameSP Thread::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) {
1635 return GetStackFrameList()->GetFrameWithConcreteFrameIndex(unwind_idx);
1638 Error Thread::ReturnFromFrameWithIndex(uint32_t frame_idx,
1639 lldb::ValueObjectSP return_value_sp,
1641 StackFrameSP frame_sp = GetStackFrameAtIndex(frame_idx);
1645 return_error.SetErrorStringWithFormat(
1646 "Could not find frame with index %d in thread 0x%" PRIx64 ".",
1647 frame_idx, GetID());
1650 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
1653 Error Thread::ReturnFromFrame(lldb::StackFrameSP frame_sp,
1654 lldb::ValueObjectSP return_value_sp,
1659 return_error.SetErrorString("Can't return to a null frame.");
1660 return return_error;
1663 Thread *thread = frame_sp->GetThread().get();
1664 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1665 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
1666 if (!older_frame_sp) {
1667 return_error.SetErrorString("No older frame to return to.");
1668 return return_error;
1671 if (return_value_sp) {
1672 lldb::ABISP abi = thread->GetProcess()->GetABI();
1674 return_error.SetErrorString("Could not find ABI to set return value.");
1675 return return_error;
1677 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1679 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not
1681 // Turn that back on when that works.
1682 if (/* DISABLES CODE */ (0) && sc.function != nullptr) {
1683 Type *function_type = sc.function->GetType();
1684 if (function_type) {
1685 CompilerType return_type =
1686 sc.function->GetCompilerType().GetFunctionReturnType();
1689 return_type.DumpTypeDescription(&s);
1690 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
1691 if (cast_value_sp) {
1692 cast_value_sp->SetFormat(eFormatHex);
1693 return_value_sp = cast_value_sp;
1699 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
1700 if (!return_error.Success())
1701 return return_error;
1704 // Now write the return registers for the chosen frame:
1705 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the
1709 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
1710 if (youngest_frame_sp) {
1711 lldb::RegisterContextSP reg_ctx_sp(youngest_frame_sp->GetRegisterContext());
1713 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(
1714 older_frame_sp->GetRegisterContext());
1716 thread->DiscardThreadPlans(true);
1717 thread->ClearStackFrames();
1718 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1719 BroadcastEvent(eBroadcastBitStackChanged,
1720 new ThreadEventData(this->shared_from_this()));
1722 return_error.SetErrorString("Could not reset register values.");
1725 return_error.SetErrorString("Frame has no register context.");
1728 return_error.SetErrorString("Returned past top frame.");
1730 return return_error;
1733 static void DumpAddressList(Stream &s, const std::vector<Address> &list,
1734 ExecutionContextScope *exe_scope) {
1735 for (size_t n = 0; n < list.size(); n++) {
1737 list[n].Dump(&s, exe_scope, Address::DumpStyleResolvedDescription,
1738 Address::DumpStyleSectionNameOffset);
1743 Error Thread::JumpToLine(const FileSpec &file, uint32_t line,
1744 bool can_leave_function, std::string *warnings) {
1745 ExecutionContext exe_ctx(GetStackFrameAtIndex(0));
1746 Target *target = exe_ctx.GetTargetPtr();
1747 TargetSP target_sp = exe_ctx.GetTargetSP();
1748 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
1749 StackFrame *frame = exe_ctx.GetFramePtr();
1750 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1752 // Find candidate locations.
1753 std::vector<Address> candidates, within_function, outside_function;
1754 target->GetImages().FindAddressesForLine(target_sp, file, line, sc.function,
1755 within_function, outside_function);
1757 // If possible, we try and stay within the current function.
1758 // Within a function, we accept multiple locations (optimized code may do
1760 // there's no solution here so we do the best we can).
1761 // However if we're trying to leave the function, we don't know how to pick
1763 // right location, so if there's more than one then we bail.
1764 if (!within_function.empty())
1765 candidates = within_function;
1766 else if (outside_function.size() == 1 && can_leave_function)
1767 candidates = outside_function;
1769 // Check if we got anything.
1770 if (candidates.empty()) {
1771 if (outside_function.empty()) {
1772 return Error("Cannot locate an address for %s:%i.",
1773 file.GetFilename().AsCString(), line);
1774 } else if (outside_function.size() == 1) {
1775 return Error("%s:%i is outside the current function.",
1776 file.GetFilename().AsCString(), line);
1779 DumpAddressList(sstr, outside_function, target);
1780 return Error("%s:%i has multiple candidate locations:\n%s",
1781 file.GetFilename().AsCString(), line, sstr.GetData());
1785 // Accept the first location, warn about any others.
1786 Address dest = candidates[0];
1787 if (warnings && candidates.size() > 1) {
1789 sstr.Printf("%s:%i appears multiple times in this function, selecting the "
1790 "first location:\n",
1791 file.GetFilename().AsCString(), line);
1792 DumpAddressList(sstr, candidates, target);
1793 *warnings = sstr.GetString();
1796 if (!reg_ctx->SetPC(dest))
1797 return Error("Cannot change PC to target address.");
1802 void Thread::DumpUsingSettingsFormat(Stream &strm, uint32_t frame_idx,
1804 ExecutionContext exe_ctx(shared_from_this());
1805 Process *process = exe_ctx.GetProcessPtr();
1806 if (process == nullptr)
1809 StackFrameSP frame_sp;
1810 SymbolContext frame_sc;
1811 if (frame_idx != LLDB_INVALID_FRAME_ID) {
1812 frame_sp = GetStackFrameAtIndex(frame_idx);
1814 exe_ctx.SetFrameSP(frame_sp);
1815 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
1819 const FormatEntity::Entry *thread_format;
1821 thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadStopFormat();
1823 thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
1825 assert(thread_format);
1827 FormatEntity::Format(*thread_format, strm, frame_sp ? &frame_sc : nullptr,
1828 &exe_ctx, nullptr, nullptr, false, false);
1831 void Thread::SettingsInitialize() {}
1833 void Thread::SettingsTerminate() {}
1835 lldb::addr_t Thread::GetThreadPointer() { return LLDB_INVALID_ADDRESS; }
1837 addr_t Thread::GetThreadLocalData(const ModuleSP module,
1838 lldb::addr_t tls_file_addr) {
1839 // The default implementation is to ask the dynamic loader for it.
1840 // This can be overridden for specific platforms.
1841 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
1843 return loader->GetThreadLocalData(module, shared_from_this(),
1846 return LLDB_INVALID_ADDRESS;
1849 bool Thread::SafeToCallFunctions() {
1850 Process *process = GetProcess().get();
1852 SystemRuntime *runtime = process->GetSystemRuntime();
1854 return runtime->SafeToCallFunctionsOnThisThread(shared_from_this());
1861 Thread::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) {
1862 return GetStackFrameList()->GetStackFrameSPForStackFramePtr(stack_frame_ptr);
1865 const char *Thread::StopReasonAsCString(lldb::StopReason reason) {
1867 case eStopReasonInvalid:
1869 case eStopReasonNone:
1871 case eStopReasonTrace:
1873 case eStopReasonBreakpoint:
1874 return "breakpoint";
1875 case eStopReasonWatchpoint:
1876 return "watchpoint";
1877 case eStopReasonSignal:
1879 case eStopReasonException:
1881 case eStopReasonExec:
1883 case eStopReasonPlanComplete:
1884 return "plan complete";
1885 case eStopReasonThreadExiting:
1886 return "thread exiting";
1887 case eStopReasonInstrumentation:
1888 return "instrumentation break";
1891 static char unknown_state_string[64];
1892 snprintf(unknown_state_string, sizeof(unknown_state_string),
1893 "StopReason = %i", reason);
1894 return unknown_state_string;
1897 const char *Thread::RunModeAsCString(lldb::RunMode mode) {
1899 case eOnlyThisThread:
1900 return "only this thread";
1902 return "all threads";
1903 case eOnlyDuringStepping:
1904 return "only during stepping";
1907 static char unknown_state_string[64];
1908 snprintf(unknown_state_string, sizeof(unknown_state_string), "RunMode = %i",
1910 return unknown_state_string;
1913 size_t Thread::GetStatus(Stream &strm, uint32_t start_frame,
1914 uint32_t num_frames, uint32_t num_frames_with_source,
1916 ExecutionContext exe_ctx(shared_from_this());
1917 Target *target = exe_ctx.GetTargetPtr();
1918 Process *process = exe_ctx.GetProcessPtr();
1919 size_t num_frames_shown = 0;
1921 bool is_selected = false;
1923 if (process->GetThreadList().GetSelectedThread().get() == this)
1926 strm.Printf("%c ", is_selected ? '*' : ' ');
1927 if (target && target->GetDebugger().GetUseExternalEditor()) {
1928 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
1930 SymbolContext frame_sc(
1931 frame_sp->GetSymbolContext(eSymbolContextLineEntry));
1932 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file) {
1933 Host::OpenFileInExternalEditor(frame_sc.line_entry.file,
1934 frame_sc.line_entry.line);
1939 DumpUsingSettingsFormat(strm, start_frame, stop_format);
1941 if (num_frames > 0) {
1944 const bool show_frame_info = true;
1946 const char *selected_frame_marker = nullptr;
1947 if (num_frames == 1 ||
1948 (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
1951 selected_frame_marker = "* ";
1953 num_frames_shown = GetStackFrameList()->GetStatus(
1954 strm, start_frame, num_frames, show_frame_info, num_frames_with_source,
1955 selected_frame_marker);
1956 if (num_frames == 1)
1960 return num_frames_shown;
1963 bool Thread::GetDescription(Stream &strm, lldb::DescriptionLevel level,
1964 bool print_json_thread, bool print_json_stopinfo) {
1965 const bool stop_format = false;
1966 DumpUsingSettingsFormat(strm, 0, stop_format);
1969 StructuredData::ObjectSP thread_info = GetExtendedInfo();
1971 if (print_json_thread || print_json_stopinfo) {
1972 if (thread_info && print_json_thread) {
1973 thread_info->Dump(strm);
1977 if (print_json_stopinfo && m_stop_info_sp) {
1978 StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo();
1980 stop_info->Dump(strm);
1989 StructuredData::ObjectSP activity =
1990 thread_info->GetObjectForDotSeparatedPath("activity");
1991 StructuredData::ObjectSP breadcrumb =
1992 thread_info->GetObjectForDotSeparatedPath("breadcrumb");
1993 StructuredData::ObjectSP messages =
1994 thread_info->GetObjectForDotSeparatedPath("trace_messages");
1996 bool printed_activity = false;
1998 activity->GetType() == StructuredData::Type::eTypeDictionary) {
1999 StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
2000 StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
2001 StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
2002 if (name && name->GetType() == StructuredData::Type::eTypeString && id &&
2003 id->GetType() == StructuredData::Type::eTypeInteger) {
2004 strm.Printf(" Activity '%s', 0x%" PRIx64 "\n",
2005 name->GetAsString()->GetValue().c_str(),
2006 id->GetAsInteger()->GetValue());
2008 printed_activity = true;
2010 bool printed_breadcrumb = false;
2012 breadcrumb->GetType() == StructuredData::Type::eTypeDictionary) {
2013 if (printed_activity)
2015 StructuredData::Dictionary *breadcrumb_dict =
2016 breadcrumb->GetAsDictionary();
2017 StructuredData::ObjectSP breadcrumb_text =
2018 breadcrumb_dict->GetValueForKey("name");
2019 if (breadcrumb_text &&
2020 breadcrumb_text->GetType() == StructuredData::Type::eTypeString) {
2021 strm.Printf(" Current Breadcrumb: %s\n",
2022 breadcrumb_text->GetAsString()->GetValue().c_str());
2024 printed_breadcrumb = true;
2026 if (messages && messages->GetType() == StructuredData::Type::eTypeArray) {
2027 if (printed_breadcrumb)
2029 StructuredData::Array *messages_array = messages->GetAsArray();
2030 const size_t msg_count = messages_array->GetSize();
2031 if (msg_count > 0) {
2032 strm.Printf(" %zu trace messages:\n", msg_count);
2033 for (size_t i = 0; i < msg_count; i++) {
2034 StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
2036 message->GetType() == StructuredData::Type::eTypeDictionary) {
2037 StructuredData::Dictionary *message_dict =
2038 message->GetAsDictionary();
2039 StructuredData::ObjectSP message_text =
2040 message_dict->GetValueForKey("message");
2042 message_text->GetType() == StructuredData::Type::eTypeString) {
2043 strm.Printf(" %s\n",
2044 message_text->GetAsString()->GetValue().c_str());
2055 size_t Thread::GetStackFrameStatus(Stream &strm, uint32_t first_frame,
2056 uint32_t num_frames, bool show_frame_info,
2057 uint32_t num_frames_with_source) {
2058 return GetStackFrameList()->GetStatus(
2059 strm, first_frame, num_frames, show_frame_info, num_frames_with_source);
2062 Unwind *Thread::GetUnwinder() {
2063 if (!m_unwinder_ap) {
2064 const ArchSpec target_arch(CalculateTarget()->GetArchitecture());
2065 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2067 case llvm::Triple::x86_64:
2068 case llvm::Triple::x86:
2069 case llvm::Triple::arm:
2070 case llvm::Triple::aarch64:
2071 case llvm::Triple::thumb:
2072 case llvm::Triple::mips:
2073 case llvm::Triple::mipsel:
2074 case llvm::Triple::mips64:
2075 case llvm::Triple::mips64el:
2076 case llvm::Triple::ppc:
2077 case llvm::Triple::ppc64:
2078 case llvm::Triple::systemz:
2079 case llvm::Triple::hexagon:
2080 m_unwinder_ap.reset(new UnwindLLDB(*this));
2084 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2085 m_unwinder_ap.reset(new UnwindMacOSXFrameBackchain(*this));
2089 return m_unwinder_ap.get();
2092 void Thread::Flush() {
2094 m_reg_context_sp.reset();
2097 bool Thread::IsStillAtLastBreakpointHit() {
2098 // If we are currently stopped at a breakpoint, always return that stopinfo
2099 // and don't reset it.
2100 // This allows threads to maintain their breakpoint stopinfo, such as when
2101 // thread-stepping in
2102 // multithreaded programs.
2103 if (m_stop_info_sp) {
2104 StopReason stop_reason = m_stop_info_sp->GetStopReason();
2105 if (stop_reason == lldb::eStopReasonBreakpoint) {
2106 uint64_t value = m_stop_info_sp->GetValue();
2107 lldb::RegisterContextSP reg_ctx_sp(GetRegisterContext());
2109 lldb::addr_t pc = reg_ctx_sp->GetPC();
2110 BreakpointSiteSP bp_site_sp =
2111 GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
2112 if (bp_site_sp && static_cast<break_id_t>(value) == bp_site_sp->GetID())
2120 Error Thread::StepIn(bool source_step,
2121 LazyBool step_in_avoids_code_without_debug_info,
2122 LazyBool step_out_avoids_code_without_debug_info)
2126 Process *process = GetProcess().get();
2127 if (StateIsStoppedState(process->GetState(), true)) {
2128 StackFrameSP frame_sp = GetStackFrameAtIndex(0);
2129 ThreadPlanSP new_plan_sp;
2130 const lldb::RunMode run_mode = eOnlyThisThread;
2131 const bool abort_other_plans = false;
2133 if (source_step && frame_sp && frame_sp->HasDebugInformation()) {
2134 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2135 new_plan_sp = QueueThreadPlanForStepInRange(
2136 abort_other_plans, sc.line_entry, sc, nullptr, run_mode,
2137 step_in_avoids_code_without_debug_info,
2138 step_out_avoids_code_without_debug_info);
2140 new_plan_sp = QueueThreadPlanForStepSingleInstruction(
2141 false, abort_other_plans, run_mode);
2144 new_plan_sp->SetIsMasterPlan(true);
2145 new_plan_sp->SetOkayToDiscard(false);
2147 // Why do we need to set the current thread by ID here???
2148 process->GetThreadList().SetSelectedThreadByID(GetID());
2149 error = process->Resume();
2151 error.SetErrorString("process not stopped");
2156 Error Thread::StepOver(bool source_step,
2157 LazyBool step_out_avoids_code_without_debug_info) {
2159 Process *process = GetProcess().get();
2160 if (StateIsStoppedState(process->GetState(), true)) {
2161 StackFrameSP frame_sp = GetStackFrameAtIndex(0);
2162 ThreadPlanSP new_plan_sp;
2164 const lldb::RunMode run_mode = eOnlyThisThread;
2165 const bool abort_other_plans = false;
2167 if (source_step && frame_sp && frame_sp->HasDebugInformation()) {
2168 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2169 new_plan_sp = QueueThreadPlanForStepOverRange(
2170 abort_other_plans, sc.line_entry, sc, run_mode,
2171 step_out_avoids_code_without_debug_info);
2173 new_plan_sp = QueueThreadPlanForStepSingleInstruction(
2174 true, abort_other_plans, run_mode);
2177 new_plan_sp->SetIsMasterPlan(true);
2178 new_plan_sp->SetOkayToDiscard(false);
2180 // Why do we need to set the current thread by ID here???
2181 process->GetThreadList().SetSelectedThreadByID(GetID());
2182 error = process->Resume();
2184 error.SetErrorString("process not stopped");
2189 Error Thread::StepOut() {
2191 Process *process = GetProcess().get();
2192 if (StateIsStoppedState(process->GetState(), true)) {
2193 const bool first_instruction = false;
2194 const bool stop_other_threads = false;
2195 const bool abort_other_plans = false;
2197 ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut(
2198 abort_other_plans, nullptr, first_instruction, stop_other_threads,
2199 eVoteYes, eVoteNoOpinion, 0));
2201 new_plan_sp->SetIsMasterPlan(true);
2202 new_plan_sp->SetOkayToDiscard(false);
2204 // Why do we need to set the current thread by ID here???
2205 process->GetThreadList().SetSelectedThreadByID(GetID());
2206 error = process->Resume();
2208 error.SetErrorString("process not stopped");