1dda28197Spatrick //===-- Thread.cpp --------------------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "lldb/Target/Thread.h"
10061da546Spatrick #include "lldb/Breakpoint/BreakpointLocation.h"
11061da546Spatrick #include "lldb/Core/Debugger.h"
12061da546Spatrick #include "lldb/Core/FormatEntity.h"
13061da546Spatrick #include "lldb/Core/Module.h"
14061da546Spatrick #include "lldb/Core/StructuredDataImpl.h"
15061da546Spatrick #include "lldb/Core/ValueObject.h"
16*f6aab3d8Srobert #include "lldb/Core/ValueObjectConstResult.h"
17061da546Spatrick #include "lldb/Host/Host.h"
18061da546Spatrick #include "lldb/Interpreter/OptionValueFileSpecList.h"
19061da546Spatrick #include "lldb/Interpreter/OptionValueProperties.h"
20061da546Spatrick #include "lldb/Interpreter/Property.h"
21061da546Spatrick #include "lldb/Symbol/Function.h"
22061da546Spatrick #include "lldb/Target/ABI.h"
23061da546Spatrick #include "lldb/Target/DynamicLoader.h"
24061da546Spatrick #include "lldb/Target/ExecutionContext.h"
25061da546Spatrick #include "lldb/Target/LanguageRuntime.h"
26061da546Spatrick #include "lldb/Target/Process.h"
27061da546Spatrick #include "lldb/Target/RegisterContext.h"
28061da546Spatrick #include "lldb/Target/StackFrameRecognizer.h"
29061da546Spatrick #include "lldb/Target/StopInfo.h"
30061da546Spatrick #include "lldb/Target/SystemRuntime.h"
31061da546Spatrick #include "lldb/Target/Target.h"
32061da546Spatrick #include "lldb/Target/ThreadPlan.h"
33061da546Spatrick #include "lldb/Target/ThreadPlanBase.h"
34061da546Spatrick #include "lldb/Target/ThreadPlanCallFunction.h"
35061da546Spatrick #include "lldb/Target/ThreadPlanPython.h"
36061da546Spatrick #include "lldb/Target/ThreadPlanRunToAddress.h"
37dda28197Spatrick #include "lldb/Target/ThreadPlanStack.h"
38061da546Spatrick #include "lldb/Target/ThreadPlanStepInRange.h"
39061da546Spatrick #include "lldb/Target/ThreadPlanStepInstruction.h"
40061da546Spatrick #include "lldb/Target/ThreadPlanStepOut.h"
41061da546Spatrick #include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
42061da546Spatrick #include "lldb/Target/ThreadPlanStepOverRange.h"
43061da546Spatrick #include "lldb/Target/ThreadPlanStepThrough.h"
44061da546Spatrick #include "lldb/Target/ThreadPlanStepUntil.h"
45061da546Spatrick #include "lldb/Target/ThreadSpec.h"
46dda28197Spatrick #include "lldb/Target/UnwindLLDB.h"
47*f6aab3d8Srobert #include "lldb/Utility/LLDBLog.h"
48061da546Spatrick #include "lldb/Utility/Log.h"
49061da546Spatrick #include "lldb/Utility/RegularExpression.h"
50061da546Spatrick #include "lldb/Utility/State.h"
51061da546Spatrick #include "lldb/Utility/Stream.h"
52061da546Spatrick #include "lldb/Utility/StreamString.h"
53061da546Spatrick #include "lldb/lldb-enumerations.h"
54061da546Spatrick
55061da546Spatrick #include <memory>
56*f6aab3d8Srobert #include <optional>
57061da546Spatrick
58061da546Spatrick using namespace lldb;
59061da546Spatrick using namespace lldb_private;
60061da546Spatrick
GetGlobalProperties()61*f6aab3d8Srobert ThreadProperties &Thread::GetGlobalProperties() {
62061da546Spatrick // NOTE: intentional leak so we don't crash if global destructor chain gets
63061da546Spatrick // called as other threads still use the result of this function
64*f6aab3d8Srobert static ThreadProperties *g_settings_ptr = new ThreadProperties(true);
65*f6aab3d8Srobert return *g_settings_ptr;
66061da546Spatrick }
67061da546Spatrick
68061da546Spatrick #define LLDB_PROPERTIES_thread
69061da546Spatrick #include "TargetProperties.inc"
70061da546Spatrick
71061da546Spatrick enum {
72061da546Spatrick #define LLDB_PROPERTIES_thread
73061da546Spatrick #include "TargetPropertiesEnum.inc"
74061da546Spatrick };
75061da546Spatrick
76be691f3bSpatrick class ThreadOptionValueProperties
77be691f3bSpatrick : public Cloneable<ThreadOptionValueProperties, OptionValueProperties> {
78061da546Spatrick public:
ThreadOptionValueProperties(ConstString name)79be691f3bSpatrick ThreadOptionValueProperties(ConstString name) : Cloneable(name) {}
80061da546Spatrick
GetPropertyAtIndex(const ExecutionContext * exe_ctx,bool will_modify,uint32_t idx) const81061da546Spatrick const Property *GetPropertyAtIndex(const ExecutionContext *exe_ctx,
82061da546Spatrick bool will_modify,
83061da546Spatrick uint32_t idx) const override {
84061da546Spatrick // When getting the value for a key from the thread options, we will always
85061da546Spatrick // try and grab the setting from the current thread if there is one. Else
86061da546Spatrick // we just use the one from this instance.
87061da546Spatrick if (exe_ctx) {
88061da546Spatrick Thread *thread = exe_ctx->GetThreadPtr();
89061da546Spatrick if (thread) {
90061da546Spatrick ThreadOptionValueProperties *instance_properties =
91061da546Spatrick static_cast<ThreadOptionValueProperties *>(
92061da546Spatrick thread->GetValueProperties().get());
93061da546Spatrick if (this != instance_properties)
94061da546Spatrick return instance_properties->ProtectedGetPropertyAtIndex(idx);
95061da546Spatrick }
96061da546Spatrick }
97061da546Spatrick return ProtectedGetPropertyAtIndex(idx);
98061da546Spatrick }
99061da546Spatrick };
100061da546Spatrick
ThreadProperties(bool is_global)101061da546Spatrick ThreadProperties::ThreadProperties(bool is_global) : Properties() {
102061da546Spatrick if (is_global) {
103061da546Spatrick m_collection_sp =
104061da546Spatrick std::make_shared<ThreadOptionValueProperties>(ConstString("thread"));
105061da546Spatrick m_collection_sp->Initialize(g_thread_properties);
106061da546Spatrick } else
107be691f3bSpatrick m_collection_sp =
108*f6aab3d8Srobert OptionValueProperties::CreateLocalCopy(Thread::GetGlobalProperties());
109061da546Spatrick }
110061da546Spatrick
111061da546Spatrick ThreadProperties::~ThreadProperties() = default;
112061da546Spatrick
GetSymbolsToAvoidRegexp()113061da546Spatrick const RegularExpression *ThreadProperties::GetSymbolsToAvoidRegexp() {
114061da546Spatrick const uint32_t idx = ePropertyStepAvoidRegex;
115061da546Spatrick return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex(nullptr, idx);
116061da546Spatrick }
117061da546Spatrick
GetLibrariesToAvoid() const118061da546Spatrick FileSpecList ThreadProperties::GetLibrariesToAvoid() const {
119061da546Spatrick const uint32_t idx = ePropertyStepAvoidLibraries;
120061da546Spatrick const OptionValueFileSpecList *option_value =
121061da546Spatrick m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
122061da546Spatrick false, idx);
123061da546Spatrick assert(option_value);
124061da546Spatrick return option_value->GetCurrentValue();
125061da546Spatrick }
126061da546Spatrick
GetTraceEnabledState() const127061da546Spatrick bool ThreadProperties::GetTraceEnabledState() const {
128061da546Spatrick const uint32_t idx = ePropertyEnableThreadTrace;
129061da546Spatrick return m_collection_sp->GetPropertyAtIndexAsBoolean(
130061da546Spatrick nullptr, idx, g_thread_properties[idx].default_uint_value != 0);
131061da546Spatrick }
132061da546Spatrick
GetStepInAvoidsNoDebug() const133061da546Spatrick bool ThreadProperties::GetStepInAvoidsNoDebug() const {
134061da546Spatrick const uint32_t idx = ePropertyStepInAvoidsNoDebug;
135061da546Spatrick return m_collection_sp->GetPropertyAtIndexAsBoolean(
136061da546Spatrick nullptr, idx, g_thread_properties[idx].default_uint_value != 0);
137061da546Spatrick }
138061da546Spatrick
GetStepOutAvoidsNoDebug() const139061da546Spatrick bool ThreadProperties::GetStepOutAvoidsNoDebug() const {
140061da546Spatrick const uint32_t idx = ePropertyStepOutAvoidsNoDebug;
141061da546Spatrick return m_collection_sp->GetPropertyAtIndexAsBoolean(
142061da546Spatrick nullptr, idx, g_thread_properties[idx].default_uint_value != 0);
143061da546Spatrick }
144061da546Spatrick
GetMaxBacktraceDepth() const145061da546Spatrick uint64_t ThreadProperties::GetMaxBacktraceDepth() const {
146061da546Spatrick const uint32_t idx = ePropertyMaxBacktraceDepth;
147061da546Spatrick return m_collection_sp->GetPropertyAtIndexAsUInt64(
148061da546Spatrick nullptr, idx, g_thread_properties[idx].default_uint_value != 0);
149061da546Spatrick }
150061da546Spatrick
151061da546Spatrick // Thread Event Data
152061da546Spatrick
GetFlavorString()153061da546Spatrick ConstString Thread::ThreadEventData::GetFlavorString() {
154061da546Spatrick static ConstString g_flavor("Thread::ThreadEventData");
155061da546Spatrick return g_flavor;
156061da546Spatrick }
157061da546Spatrick
ThreadEventData(const lldb::ThreadSP thread_sp)158061da546Spatrick Thread::ThreadEventData::ThreadEventData(const lldb::ThreadSP thread_sp)
159061da546Spatrick : m_thread_sp(thread_sp), m_stack_id() {}
160061da546Spatrick
ThreadEventData(const lldb::ThreadSP thread_sp,const StackID & stack_id)161061da546Spatrick Thread::ThreadEventData::ThreadEventData(const lldb::ThreadSP thread_sp,
162061da546Spatrick const StackID &stack_id)
163061da546Spatrick : m_thread_sp(thread_sp), m_stack_id(stack_id) {}
164061da546Spatrick
ThreadEventData()165061da546Spatrick Thread::ThreadEventData::ThreadEventData() : m_thread_sp(), m_stack_id() {}
166061da546Spatrick
167061da546Spatrick Thread::ThreadEventData::~ThreadEventData() = default;
168061da546Spatrick
Dump(Stream * s) const169061da546Spatrick void Thread::ThreadEventData::Dump(Stream *s) const {}
170061da546Spatrick
171061da546Spatrick const Thread::ThreadEventData *
GetEventDataFromEvent(const Event * event_ptr)172061da546Spatrick Thread::ThreadEventData::GetEventDataFromEvent(const Event *event_ptr) {
173061da546Spatrick if (event_ptr) {
174061da546Spatrick const EventData *event_data = event_ptr->GetData();
175061da546Spatrick if (event_data &&
176061da546Spatrick event_data->GetFlavor() == ThreadEventData::GetFlavorString())
177061da546Spatrick return static_cast<const ThreadEventData *>(event_ptr->GetData());
178061da546Spatrick }
179061da546Spatrick return nullptr;
180061da546Spatrick }
181061da546Spatrick
GetThreadFromEvent(const Event * event_ptr)182061da546Spatrick ThreadSP Thread::ThreadEventData::GetThreadFromEvent(const Event *event_ptr) {
183061da546Spatrick ThreadSP thread_sp;
184061da546Spatrick const ThreadEventData *event_data = GetEventDataFromEvent(event_ptr);
185061da546Spatrick if (event_data)
186061da546Spatrick thread_sp = event_data->GetThread();
187061da546Spatrick return thread_sp;
188061da546Spatrick }
189061da546Spatrick
GetStackIDFromEvent(const Event * event_ptr)190061da546Spatrick StackID Thread::ThreadEventData::GetStackIDFromEvent(const Event *event_ptr) {
191061da546Spatrick StackID stack_id;
192061da546Spatrick const ThreadEventData *event_data = GetEventDataFromEvent(event_ptr);
193061da546Spatrick if (event_data)
194061da546Spatrick stack_id = event_data->GetStackID();
195061da546Spatrick return stack_id;
196061da546Spatrick }
197061da546Spatrick
198061da546Spatrick StackFrameSP
GetStackFrameFromEvent(const Event * event_ptr)199061da546Spatrick Thread::ThreadEventData::GetStackFrameFromEvent(const Event *event_ptr) {
200061da546Spatrick const ThreadEventData *event_data = GetEventDataFromEvent(event_ptr);
201061da546Spatrick StackFrameSP frame_sp;
202061da546Spatrick if (event_data) {
203061da546Spatrick ThreadSP thread_sp = event_data->GetThread();
204061da546Spatrick if (thread_sp) {
205061da546Spatrick frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID(
206061da546Spatrick event_data->GetStackID());
207061da546Spatrick }
208061da546Spatrick }
209061da546Spatrick return frame_sp;
210061da546Spatrick }
211061da546Spatrick
212061da546Spatrick // Thread class
213061da546Spatrick
GetStaticBroadcasterClass()214061da546Spatrick ConstString &Thread::GetStaticBroadcasterClass() {
215061da546Spatrick static ConstString class_name("lldb.thread");
216061da546Spatrick return class_name;
217061da546Spatrick }
218061da546Spatrick
Thread(Process & process,lldb::tid_t tid,bool use_invalid_index_id)219061da546Spatrick Thread::Thread(Process &process, lldb::tid_t tid, bool use_invalid_index_id)
220061da546Spatrick : ThreadProperties(false), UserID(tid),
221061da546Spatrick Broadcaster(process.GetTarget().GetDebugger().GetBroadcasterManager(),
222061da546Spatrick Thread::GetStaticBroadcasterClass().AsCString()),
223061da546Spatrick m_process_wp(process.shared_from_this()), m_stop_info_sp(),
224061da546Spatrick m_stop_info_stop_id(0), m_stop_info_override_stop_id(0),
225*f6aab3d8Srobert m_should_run_before_public_stop(false),
226061da546Spatrick m_index_id(use_invalid_index_id ? LLDB_INVALID_INDEX32
227061da546Spatrick : process.GetNextThreadIndexID(tid)),
228061da546Spatrick m_reg_context_sp(), m_state(eStateUnloaded), m_state_mutex(),
229dda28197Spatrick m_frame_mutex(), m_curr_frames_sp(), m_prev_frames_sp(),
230061da546Spatrick m_resume_signal(LLDB_INVALID_SIGNAL_NUMBER),
231061da546Spatrick m_resume_state(eStateRunning), m_temporary_resume_state(eStateRunning),
232061da546Spatrick m_unwinder_up(), m_destroy_called(false),
233061da546Spatrick m_override_should_notify(eLazyBoolCalculate),
234061da546Spatrick m_extended_info_fetched(false), m_extended_info() {
235*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Object);
236061da546Spatrick LLDB_LOGF(log, "%p Thread::Thread(tid = 0x%4.4" PRIx64 ")",
237061da546Spatrick static_cast<void *>(this), GetID());
238061da546Spatrick
239061da546Spatrick CheckInWithManager();
240061da546Spatrick }
241061da546Spatrick
~Thread()242061da546Spatrick Thread::~Thread() {
243*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Object);
244061da546Spatrick LLDB_LOGF(log, "%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")",
245061da546Spatrick static_cast<void *>(this), GetID());
246061da546Spatrick /// If you hit this assert, it means your derived class forgot to call
247061da546Spatrick /// DoDestroy in its destructor.
248061da546Spatrick assert(m_destroy_called);
249061da546Spatrick }
250061da546Spatrick
DestroyThread()251061da546Spatrick void Thread::DestroyThread() {
252061da546Spatrick m_destroy_called = true;
253061da546Spatrick m_stop_info_sp.reset();
254061da546Spatrick m_reg_context_sp.reset();
255061da546Spatrick m_unwinder_up.reset();
256061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
257061da546Spatrick m_curr_frames_sp.reset();
258061da546Spatrick m_prev_frames_sp.reset();
259061da546Spatrick }
260061da546Spatrick
BroadcastSelectedFrameChange(StackID & new_frame_id)261061da546Spatrick void Thread::BroadcastSelectedFrameChange(StackID &new_frame_id) {
262061da546Spatrick if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
263061da546Spatrick BroadcastEvent(eBroadcastBitSelectedFrameChanged,
264061da546Spatrick new ThreadEventData(this->shared_from_this(), new_frame_id));
265061da546Spatrick }
266061da546Spatrick
GetSelectedFrame()267061da546Spatrick lldb::StackFrameSP Thread::GetSelectedFrame() {
268061da546Spatrick StackFrameListSP stack_frame_list_sp(GetStackFrameList());
269061da546Spatrick StackFrameSP frame_sp = stack_frame_list_sp->GetFrameAtIndex(
270061da546Spatrick stack_frame_list_sp->GetSelectedFrameIndex());
271dda28197Spatrick FrameSelectedCallback(frame_sp.get());
272061da546Spatrick return frame_sp;
273061da546Spatrick }
274061da546Spatrick
SetSelectedFrame(lldb_private::StackFrame * frame,bool broadcast)275061da546Spatrick uint32_t Thread::SetSelectedFrame(lldb_private::StackFrame *frame,
276061da546Spatrick bool broadcast) {
277061da546Spatrick uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
278061da546Spatrick if (broadcast)
279061da546Spatrick BroadcastSelectedFrameChange(frame->GetStackID());
280dda28197Spatrick FrameSelectedCallback(frame);
281061da546Spatrick return ret_value;
282061da546Spatrick }
283061da546Spatrick
SetSelectedFrameByIndex(uint32_t frame_idx,bool broadcast)284061da546Spatrick bool Thread::SetSelectedFrameByIndex(uint32_t frame_idx, bool broadcast) {
285061da546Spatrick StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex(frame_idx));
286061da546Spatrick if (frame_sp) {
287061da546Spatrick GetStackFrameList()->SetSelectedFrame(frame_sp.get());
288061da546Spatrick if (broadcast)
289061da546Spatrick BroadcastSelectedFrameChange(frame_sp->GetStackID());
290dda28197Spatrick FrameSelectedCallback(frame_sp.get());
291061da546Spatrick return true;
292061da546Spatrick } else
293061da546Spatrick return false;
294061da546Spatrick }
295061da546Spatrick
SetSelectedFrameByIndexNoisily(uint32_t frame_idx,Stream & output_stream)296061da546Spatrick bool Thread::SetSelectedFrameByIndexNoisily(uint32_t frame_idx,
297061da546Spatrick Stream &output_stream) {
298061da546Spatrick const bool broadcast = true;
299061da546Spatrick bool success = SetSelectedFrameByIndex(frame_idx, broadcast);
300061da546Spatrick if (success) {
301061da546Spatrick StackFrameSP frame_sp = GetSelectedFrame();
302061da546Spatrick if (frame_sp) {
303061da546Spatrick bool already_shown = false;
304061da546Spatrick SymbolContext frame_sc(
305061da546Spatrick frame_sp->GetSymbolContext(eSymbolContextLineEntry));
306061da546Spatrick if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() &&
307061da546Spatrick frame_sc.line_entry.file && frame_sc.line_entry.line != 0) {
308061da546Spatrick already_shown = Host::OpenFileInExternalEditor(
309061da546Spatrick frame_sc.line_entry.file, frame_sc.line_entry.line);
310061da546Spatrick }
311061da546Spatrick
312061da546Spatrick bool show_frame_info = true;
313061da546Spatrick bool show_source = !already_shown;
314dda28197Spatrick FrameSelectedCallback(frame_sp.get());
315061da546Spatrick return frame_sp->GetStatus(output_stream, show_frame_info, show_source);
316061da546Spatrick }
317061da546Spatrick return false;
318061da546Spatrick } else
319061da546Spatrick return false;
320061da546Spatrick }
321061da546Spatrick
FrameSelectedCallback(StackFrame * frame)322dda28197Spatrick void Thread::FrameSelectedCallback(StackFrame *frame) {
323dda28197Spatrick if (!frame)
324dda28197Spatrick return;
325dda28197Spatrick
326dda28197Spatrick if (frame->HasDebugInformation() &&
327dda28197Spatrick (GetProcess()->GetWarningsOptimization() ||
328dda28197Spatrick GetProcess()->GetWarningsUnsupportedLanguage())) {
329061da546Spatrick SymbolContext sc =
330061da546Spatrick frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextModule);
331061da546Spatrick GetProcess()->PrintWarningOptimization(sc);
332dda28197Spatrick GetProcess()->PrintWarningUnsupportedLanguage(sc);
333061da546Spatrick }
334061da546Spatrick }
335061da546Spatrick
GetStopInfo()336061da546Spatrick lldb::StopInfoSP Thread::GetStopInfo() {
337061da546Spatrick if (m_destroy_called)
338061da546Spatrick return m_stop_info_sp;
339061da546Spatrick
340061da546Spatrick ThreadPlanSP completed_plan_sp(GetCompletedPlan());
341061da546Spatrick ProcessSP process_sp(GetProcess());
342061da546Spatrick const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
343061da546Spatrick
344061da546Spatrick // Here we select the stop info according to priorirty: - m_stop_info_sp (if
345061da546Spatrick // not trace) - preset value - completed plan stop info - new value with plan
346061da546Spatrick // from completed plan stack - m_stop_info_sp (trace stop reason is OK now) -
347061da546Spatrick // ask GetPrivateStopInfo to set stop info
348061da546Spatrick
349061da546Spatrick bool have_valid_stop_info = m_stop_info_sp &&
350061da546Spatrick m_stop_info_sp ->IsValid() &&
351061da546Spatrick m_stop_info_stop_id == stop_id;
352061da546Spatrick bool have_valid_completed_plan = completed_plan_sp && completed_plan_sp->PlanSucceeded();
353061da546Spatrick bool plan_failed = completed_plan_sp && !completed_plan_sp->PlanSucceeded();
354061da546Spatrick bool plan_overrides_trace =
355061da546Spatrick have_valid_stop_info && have_valid_completed_plan
356061da546Spatrick && (m_stop_info_sp->GetStopReason() == eStopReasonTrace);
357061da546Spatrick
358061da546Spatrick if (have_valid_stop_info && !plan_overrides_trace && !plan_failed) {
359061da546Spatrick return m_stop_info_sp;
360061da546Spatrick } else if (completed_plan_sp) {
361061da546Spatrick return StopInfo::CreateStopReasonWithPlan(
362061da546Spatrick completed_plan_sp, GetReturnValueObject(), GetExpressionVariable());
363061da546Spatrick } else {
364061da546Spatrick GetPrivateStopInfo();
365061da546Spatrick return m_stop_info_sp;
366061da546Spatrick }
367061da546Spatrick }
368061da546Spatrick
CalculatePublicStopInfo()369061da546Spatrick void Thread::CalculatePublicStopInfo() {
370061da546Spatrick ResetStopInfo();
371061da546Spatrick SetStopInfo(GetStopInfo());
372061da546Spatrick }
373061da546Spatrick
GetPrivateStopInfo(bool calculate)374*f6aab3d8Srobert lldb::StopInfoSP Thread::GetPrivateStopInfo(bool calculate) {
375*f6aab3d8Srobert if (!calculate)
376*f6aab3d8Srobert return m_stop_info_sp;
377*f6aab3d8Srobert
378061da546Spatrick if (m_destroy_called)
379061da546Spatrick return m_stop_info_sp;
380061da546Spatrick
381061da546Spatrick ProcessSP process_sp(GetProcess());
382061da546Spatrick if (process_sp) {
383061da546Spatrick const uint32_t process_stop_id = process_sp->GetStopID();
384061da546Spatrick if (m_stop_info_stop_id != process_stop_id) {
385*f6aab3d8Srobert // We preserve the old stop info for a variety of reasons:
386*f6aab3d8Srobert // 1) Someone has already updated it by the time we get here
387*f6aab3d8Srobert // 2) We didn't get to execute the breakpoint instruction we stopped at
388*f6aab3d8Srobert // 3) This is a virtual step so we didn't actually run
389*f6aab3d8Srobert // 4) If this thread wasn't allowed to run the last time round.
390061da546Spatrick if (m_stop_info_sp) {
391061da546Spatrick if (m_stop_info_sp->IsValid() || IsStillAtLastBreakpointHit() ||
392*f6aab3d8Srobert GetCurrentPlan()->IsVirtualStep()
393*f6aab3d8Srobert || GetTemporaryResumeState() == eStateSuspended)
394061da546Spatrick SetStopInfo(m_stop_info_sp);
395061da546Spatrick else
396061da546Spatrick m_stop_info_sp.reset();
397061da546Spatrick }
398061da546Spatrick
399061da546Spatrick if (!m_stop_info_sp) {
400061da546Spatrick if (!CalculateStopInfo())
401061da546Spatrick SetStopInfo(StopInfoSP());
402061da546Spatrick }
403061da546Spatrick }
404061da546Spatrick
405061da546Spatrick // The stop info can be manually set by calling Thread::SetStopInfo() prior
406061da546Spatrick // to this function ever getting called, so we can't rely on
407061da546Spatrick // "m_stop_info_stop_id != process_stop_id" as the condition for the if
408061da546Spatrick // statement below, we must also check the stop info to see if we need to
409061da546Spatrick // override it. See the header documentation in
410dda28197Spatrick // Architecture::OverrideStopInfo() for more information on the stop
411061da546Spatrick // info override callback.
412061da546Spatrick if (m_stop_info_override_stop_id != process_stop_id) {
413061da546Spatrick m_stop_info_override_stop_id = process_stop_id;
414061da546Spatrick if (m_stop_info_sp) {
415061da546Spatrick if (const Architecture *arch =
416061da546Spatrick process_sp->GetTarget().GetArchitecturePlugin())
417061da546Spatrick arch->OverrideStopInfo(*this);
418061da546Spatrick }
419061da546Spatrick }
420061da546Spatrick }
421061da546Spatrick return m_stop_info_sp;
422061da546Spatrick }
423061da546Spatrick
GetStopReason()424061da546Spatrick lldb::StopReason Thread::GetStopReason() {
425061da546Spatrick lldb::StopInfoSP stop_info_sp(GetStopInfo());
426061da546Spatrick if (stop_info_sp)
427061da546Spatrick return stop_info_sp->GetStopReason();
428061da546Spatrick return eStopReasonNone;
429061da546Spatrick }
430061da546Spatrick
StopInfoIsUpToDate() const431061da546Spatrick bool Thread::StopInfoIsUpToDate() const {
432061da546Spatrick ProcessSP process_sp(GetProcess());
433061da546Spatrick if (process_sp)
434061da546Spatrick return m_stop_info_stop_id == process_sp->GetStopID();
435061da546Spatrick else
436061da546Spatrick return true; // Process is no longer around so stop info is always up to
437061da546Spatrick // date...
438061da546Spatrick }
439061da546Spatrick
ResetStopInfo()440061da546Spatrick void Thread::ResetStopInfo() {
441061da546Spatrick if (m_stop_info_sp) {
442061da546Spatrick m_stop_info_sp.reset();
443061da546Spatrick }
444061da546Spatrick }
445061da546Spatrick
SetStopInfo(const lldb::StopInfoSP & stop_info_sp)446061da546Spatrick void Thread::SetStopInfo(const lldb::StopInfoSP &stop_info_sp) {
447061da546Spatrick m_stop_info_sp = stop_info_sp;
448061da546Spatrick if (m_stop_info_sp) {
449061da546Spatrick m_stop_info_sp->MakeStopInfoValid();
450061da546Spatrick // If we are overriding the ShouldReportStop, do that here:
451061da546Spatrick if (m_override_should_notify != eLazyBoolCalculate)
452061da546Spatrick m_stop_info_sp->OverrideShouldNotify(m_override_should_notify ==
453061da546Spatrick eLazyBoolYes);
454061da546Spatrick }
455061da546Spatrick
456061da546Spatrick ProcessSP process_sp(GetProcess());
457061da546Spatrick if (process_sp)
458061da546Spatrick m_stop_info_stop_id = process_sp->GetStopID();
459061da546Spatrick else
460061da546Spatrick m_stop_info_stop_id = UINT32_MAX;
461*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Thread);
462061da546Spatrick LLDB_LOGF(log, "%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)",
463061da546Spatrick static_cast<void *>(this), GetID(),
464061da546Spatrick stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>",
465061da546Spatrick m_stop_info_stop_id);
466061da546Spatrick }
467061da546Spatrick
SetShouldReportStop(Vote vote)468061da546Spatrick void Thread::SetShouldReportStop(Vote vote) {
469061da546Spatrick if (vote == eVoteNoOpinion)
470061da546Spatrick return;
471061da546Spatrick else {
472061da546Spatrick m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
473061da546Spatrick if (m_stop_info_sp)
474061da546Spatrick m_stop_info_sp->OverrideShouldNotify(m_override_should_notify ==
475061da546Spatrick eLazyBoolYes);
476061da546Spatrick }
477061da546Spatrick }
478061da546Spatrick
SetStopInfoToNothing()479061da546Spatrick void Thread::SetStopInfoToNothing() {
480061da546Spatrick // Note, we can't just NULL out the private reason, or the native thread
481061da546Spatrick // implementation will try to go calculate it again. For now, just set it to
482061da546Spatrick // a Unix Signal with an invalid signal number.
483061da546Spatrick SetStopInfo(
484061da546Spatrick StopInfo::CreateStopReasonWithSignal(*this, LLDB_INVALID_SIGNAL_NUMBER));
485061da546Spatrick }
486061da546Spatrick
ThreadStoppedForAReason()487*f6aab3d8Srobert bool Thread::ThreadStoppedForAReason() { return (bool)GetPrivateStopInfo(); }
488061da546Spatrick
CheckpointThreadState(ThreadStateCheckpoint & saved_state)489061da546Spatrick bool Thread::CheckpointThreadState(ThreadStateCheckpoint &saved_state) {
490061da546Spatrick saved_state.register_backup_sp.reset();
491061da546Spatrick lldb::StackFrameSP frame_sp(GetStackFrameAtIndex(0));
492061da546Spatrick if (frame_sp) {
493061da546Spatrick lldb::RegisterCheckpointSP reg_checkpoint_sp(
494061da546Spatrick new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
495061da546Spatrick if (reg_checkpoint_sp) {
496061da546Spatrick lldb::RegisterContextSP reg_ctx_sp(frame_sp->GetRegisterContext());
497061da546Spatrick if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues(*reg_checkpoint_sp))
498061da546Spatrick saved_state.register_backup_sp = reg_checkpoint_sp;
499061da546Spatrick }
500061da546Spatrick }
501061da546Spatrick if (!saved_state.register_backup_sp)
502061da546Spatrick return false;
503061da546Spatrick
504061da546Spatrick saved_state.stop_info_sp = GetStopInfo();
505061da546Spatrick ProcessSP process_sp(GetProcess());
506061da546Spatrick if (process_sp)
507061da546Spatrick saved_state.orig_stop_id = process_sp->GetStopID();
508061da546Spatrick saved_state.current_inlined_depth = GetCurrentInlinedDepth();
509dda28197Spatrick saved_state.m_completed_plan_checkpoint =
510dda28197Spatrick GetPlans().CheckpointCompletedPlans();
511061da546Spatrick
512061da546Spatrick return true;
513061da546Spatrick }
514061da546Spatrick
RestoreRegisterStateFromCheckpoint(ThreadStateCheckpoint & saved_state)515061da546Spatrick bool Thread::RestoreRegisterStateFromCheckpoint(
516061da546Spatrick ThreadStateCheckpoint &saved_state) {
517061da546Spatrick if (saved_state.register_backup_sp) {
518061da546Spatrick lldb::StackFrameSP frame_sp(GetStackFrameAtIndex(0));
519061da546Spatrick if (frame_sp) {
520061da546Spatrick lldb::RegisterContextSP reg_ctx_sp(frame_sp->GetRegisterContext());
521061da546Spatrick if (reg_ctx_sp) {
522061da546Spatrick bool ret =
523061da546Spatrick reg_ctx_sp->WriteAllRegisterValues(*saved_state.register_backup_sp);
524061da546Spatrick
525061da546Spatrick // Clear out all stack frames as our world just changed.
526061da546Spatrick ClearStackFrames();
527061da546Spatrick reg_ctx_sp->InvalidateIfNeeded(true);
528061da546Spatrick if (m_unwinder_up)
529061da546Spatrick m_unwinder_up->Clear();
530061da546Spatrick return ret;
531061da546Spatrick }
532061da546Spatrick }
533061da546Spatrick }
534061da546Spatrick return false;
535061da546Spatrick }
536061da546Spatrick
RestoreThreadStateFromCheckpoint(ThreadStateCheckpoint & saved_state)537be691f3bSpatrick void Thread::RestoreThreadStateFromCheckpoint(
538061da546Spatrick ThreadStateCheckpoint &saved_state) {
539061da546Spatrick if (saved_state.stop_info_sp)
540061da546Spatrick saved_state.stop_info_sp->MakeStopInfoValid();
541061da546Spatrick SetStopInfo(saved_state.stop_info_sp);
542061da546Spatrick GetStackFrameList()->SetCurrentInlinedDepth(
543061da546Spatrick saved_state.current_inlined_depth);
544dda28197Spatrick GetPlans().RestoreCompletedPlanCheckpoint(
545dda28197Spatrick saved_state.m_completed_plan_checkpoint);
546061da546Spatrick }
547061da546Spatrick
GetState() const548061da546Spatrick StateType Thread::GetState() const {
549061da546Spatrick // If any other threads access this we will need a mutex for it
550061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
551061da546Spatrick return m_state;
552061da546Spatrick }
553061da546Spatrick
SetState(StateType state)554061da546Spatrick void Thread::SetState(StateType state) {
555061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
556061da546Spatrick m_state = state;
557061da546Spatrick }
558061da546Spatrick
GetStopDescription()559dda28197Spatrick std::string Thread::GetStopDescription() {
560dda28197Spatrick StackFrameSP frame_sp = GetStackFrameAtIndex(0);
561dda28197Spatrick
562dda28197Spatrick if (!frame_sp)
563dda28197Spatrick return GetStopDescriptionRaw();
564dda28197Spatrick
565dda28197Spatrick auto recognized_frame_sp = frame_sp->GetRecognizedFrame();
566dda28197Spatrick
567dda28197Spatrick if (!recognized_frame_sp)
568dda28197Spatrick return GetStopDescriptionRaw();
569dda28197Spatrick
570dda28197Spatrick std::string recognized_stop_description =
571dda28197Spatrick recognized_frame_sp->GetStopDescription();
572dda28197Spatrick
573dda28197Spatrick if (!recognized_stop_description.empty())
574dda28197Spatrick return recognized_stop_description;
575dda28197Spatrick
576dda28197Spatrick return GetStopDescriptionRaw();
577dda28197Spatrick }
578dda28197Spatrick
GetStopDescriptionRaw()579dda28197Spatrick std::string Thread::GetStopDescriptionRaw() {
580dda28197Spatrick StopInfoSP stop_info_sp = GetStopInfo();
581dda28197Spatrick std::string raw_stop_description;
582dda28197Spatrick if (stop_info_sp && stop_info_sp->IsValid()) {
583dda28197Spatrick raw_stop_description = stop_info_sp->GetDescription();
584dda28197Spatrick assert((!raw_stop_description.empty() ||
585dda28197Spatrick stop_info_sp->GetStopReason() == eStopReasonNone) &&
586dda28197Spatrick "StopInfo returned an empty description.");
587dda28197Spatrick }
588dda28197Spatrick return raw_stop_description;
589dda28197Spatrick }
590dda28197Spatrick
SelectMostRelevantFrame()591dda28197Spatrick void Thread::SelectMostRelevantFrame() {
592*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Thread);
593dda28197Spatrick
594dda28197Spatrick auto frames_list_sp = GetStackFrameList();
595dda28197Spatrick
596dda28197Spatrick // Only the top frame should be recognized.
597dda28197Spatrick auto frame_sp = frames_list_sp->GetFrameAtIndex(0);
598dda28197Spatrick
599dda28197Spatrick auto recognized_frame_sp = frame_sp->GetRecognizedFrame();
600dda28197Spatrick
601dda28197Spatrick if (!recognized_frame_sp) {
602dda28197Spatrick LLDB_LOG(log, "Frame #0 not recognized");
603dda28197Spatrick return;
604dda28197Spatrick }
605dda28197Spatrick
606dda28197Spatrick if (StackFrameSP most_relevant_frame_sp =
607dda28197Spatrick recognized_frame_sp->GetMostRelevantFrame()) {
608dda28197Spatrick LLDB_LOG(log, "Found most relevant frame at index {0}",
609dda28197Spatrick most_relevant_frame_sp->GetFrameIndex());
610dda28197Spatrick SetSelectedFrame(most_relevant_frame_sp.get());
611dda28197Spatrick } else {
612dda28197Spatrick LLDB_LOG(log, "No relevant frame!");
613dda28197Spatrick }
614dda28197Spatrick }
615dda28197Spatrick
WillStop()616061da546Spatrick void Thread::WillStop() {
617061da546Spatrick ThreadPlan *current_plan = GetCurrentPlan();
618061da546Spatrick
619dda28197Spatrick SelectMostRelevantFrame();
620dda28197Spatrick
621061da546Spatrick // FIXME: I may decide to disallow threads with no plans. In which
622061da546Spatrick // case this should go to an assert.
623061da546Spatrick
624061da546Spatrick if (!current_plan)
625061da546Spatrick return;
626061da546Spatrick
627061da546Spatrick current_plan->WillStop();
628061da546Spatrick }
629061da546Spatrick
SetupForResume()630061da546Spatrick void Thread::SetupForResume() {
631061da546Spatrick if (GetResumeState() != eStateSuspended) {
632061da546Spatrick // If we're at a breakpoint push the step-over breakpoint plan. Do this
633061da546Spatrick // before telling the current plan it will resume, since we might change
634061da546Spatrick // what the current plan is.
635061da546Spatrick
636061da546Spatrick lldb::RegisterContextSP reg_ctx_sp(GetRegisterContext());
637061da546Spatrick if (reg_ctx_sp) {
638061da546Spatrick const addr_t thread_pc = reg_ctx_sp->GetPC();
639061da546Spatrick BreakpointSiteSP bp_site_sp =
640061da546Spatrick GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc);
641061da546Spatrick if (bp_site_sp) {
642061da546Spatrick // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the
643061da546Spatrick // target may not require anything special to step over a breakpoint.
644061da546Spatrick
645061da546Spatrick ThreadPlan *cur_plan = GetCurrentPlan();
646061da546Spatrick
647061da546Spatrick bool push_step_over_bp_plan = false;
648061da546Spatrick if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint) {
649061da546Spatrick ThreadPlanStepOverBreakpoint *bp_plan =
650061da546Spatrick (ThreadPlanStepOverBreakpoint *)cur_plan;
651061da546Spatrick if (bp_plan->GetBreakpointLoadAddress() != thread_pc)
652061da546Spatrick push_step_over_bp_plan = true;
653061da546Spatrick } else
654061da546Spatrick push_step_over_bp_plan = true;
655061da546Spatrick
656061da546Spatrick if (push_step_over_bp_plan) {
657061da546Spatrick ThreadPlanSP step_bp_plan_sp(new ThreadPlanStepOverBreakpoint(*this));
658061da546Spatrick if (step_bp_plan_sp) {
659061da546Spatrick step_bp_plan_sp->SetPrivate(true);
660061da546Spatrick
661061da546Spatrick if (GetCurrentPlan()->RunState() != eStateStepping) {
662061da546Spatrick ThreadPlanStepOverBreakpoint *step_bp_plan =
663061da546Spatrick static_cast<ThreadPlanStepOverBreakpoint *>(
664061da546Spatrick step_bp_plan_sp.get());
665061da546Spatrick step_bp_plan->SetAutoContinue(true);
666061da546Spatrick }
667061da546Spatrick QueueThreadPlan(step_bp_plan_sp, false);
668061da546Spatrick }
669061da546Spatrick }
670061da546Spatrick }
671061da546Spatrick }
672061da546Spatrick }
673061da546Spatrick }
674061da546Spatrick
ShouldResume(StateType resume_state)675061da546Spatrick bool Thread::ShouldResume(StateType resume_state) {
676061da546Spatrick // At this point clear the completed plan stack.
677dda28197Spatrick GetPlans().WillResume();
678061da546Spatrick m_override_should_notify = eLazyBoolCalculate;
679061da546Spatrick
680061da546Spatrick StateType prev_resume_state = GetTemporaryResumeState();
681061da546Spatrick
682061da546Spatrick SetTemporaryResumeState(resume_state);
683061da546Spatrick
684061da546Spatrick lldb::ThreadSP backing_thread_sp(GetBackingThread());
685061da546Spatrick if (backing_thread_sp)
686061da546Spatrick backing_thread_sp->SetTemporaryResumeState(resume_state);
687061da546Spatrick
688061da546Spatrick // Make sure m_stop_info_sp is valid. Don't do this for threads we suspended
689061da546Spatrick // in the previous run.
690061da546Spatrick if (prev_resume_state != eStateSuspended)
691061da546Spatrick GetPrivateStopInfo();
692061da546Spatrick
693061da546Spatrick // This is a little dubious, but we are trying to limit how often we actually
694061da546Spatrick // fetch stop info from the target, 'cause that slows down single stepping.
695061da546Spatrick // So assume that if we got to the point where we're about to resume, and we
696061da546Spatrick // haven't yet had to fetch the stop reason, then it doesn't need to know
697061da546Spatrick // about the fact that we are resuming...
698061da546Spatrick const uint32_t process_stop_id = GetProcess()->GetStopID();
699061da546Spatrick if (m_stop_info_stop_id == process_stop_id &&
700061da546Spatrick (m_stop_info_sp && m_stop_info_sp->IsValid())) {
701061da546Spatrick StopInfo *stop_info = GetPrivateStopInfo().get();
702061da546Spatrick if (stop_info)
703061da546Spatrick stop_info->WillResume(resume_state);
704061da546Spatrick }
705061da546Spatrick
706061da546Spatrick // Tell all the plans that we are about to resume in case they need to clear
707061da546Spatrick // any state. We distinguish between the plan on the top of the stack and the
708061da546Spatrick // lower plans in case a plan needs to do any special business before it
709061da546Spatrick // runs.
710061da546Spatrick
711061da546Spatrick bool need_to_resume = false;
712061da546Spatrick ThreadPlan *plan_ptr = GetCurrentPlan();
713061da546Spatrick if (plan_ptr) {
714061da546Spatrick need_to_resume = plan_ptr->WillResume(resume_state, true);
715061da546Spatrick
716061da546Spatrick while ((plan_ptr = GetPreviousPlan(plan_ptr)) != nullptr) {
717061da546Spatrick plan_ptr->WillResume(resume_state, false);
718061da546Spatrick }
719061da546Spatrick
720061da546Spatrick // If the WillResume for the plan says we are faking a resume, then it will
721061da546Spatrick // have set an appropriate stop info. In that case, don't reset it here.
722061da546Spatrick
723061da546Spatrick if (need_to_resume && resume_state != eStateSuspended) {
724061da546Spatrick m_stop_info_sp.reset();
725061da546Spatrick }
726061da546Spatrick }
727061da546Spatrick
728061da546Spatrick if (need_to_resume) {
729061da546Spatrick ClearStackFrames();
730061da546Spatrick // Let Thread subclasses do any special work they need to prior to resuming
731061da546Spatrick WillResume(resume_state);
732061da546Spatrick }
733061da546Spatrick
734061da546Spatrick return need_to_resume;
735061da546Spatrick }
736061da546Spatrick
DidResume()737*f6aab3d8Srobert void Thread::DidResume() {
738*f6aab3d8Srobert SetResumeSignal(LLDB_INVALID_SIGNAL_NUMBER);
739*f6aab3d8Srobert // This will get recomputed each time when we stop.
740*f6aab3d8Srobert SetShouldRunBeforePublicStop(false);
741*f6aab3d8Srobert }
742061da546Spatrick
DidStop()743061da546Spatrick void Thread::DidStop() { SetState(eStateStopped); }
744061da546Spatrick
ShouldStop(Event * event_ptr)745061da546Spatrick bool Thread::ShouldStop(Event *event_ptr) {
746061da546Spatrick ThreadPlan *current_plan = GetCurrentPlan();
747061da546Spatrick
748061da546Spatrick bool should_stop = true;
749061da546Spatrick
750*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Step);
751061da546Spatrick
752061da546Spatrick if (GetResumeState() == eStateSuspended) {
753061da546Spatrick LLDB_LOGF(log,
754061da546Spatrick "Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64
755061da546Spatrick ", should_stop = 0 (ignore since thread was suspended)",
756061da546Spatrick __FUNCTION__, GetID(), GetProtocolID());
757061da546Spatrick return false;
758061da546Spatrick }
759061da546Spatrick
760061da546Spatrick if (GetTemporaryResumeState() == eStateSuspended) {
761061da546Spatrick LLDB_LOGF(log,
762061da546Spatrick "Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64
763061da546Spatrick ", should_stop = 0 (ignore since thread was suspended)",
764061da546Spatrick __FUNCTION__, GetID(), GetProtocolID());
765061da546Spatrick return false;
766061da546Spatrick }
767061da546Spatrick
768061da546Spatrick // Based on the current thread plan and process stop info, check if this
769061da546Spatrick // thread caused the process to stop. NOTE: this must take place before the
770061da546Spatrick // plan is moved from the current plan stack to the completed plan stack.
771061da546Spatrick if (!ThreadStoppedForAReason()) {
772061da546Spatrick LLDB_LOGF(log,
773061da546Spatrick "Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64
774061da546Spatrick ", pc = 0x%16.16" PRIx64
775061da546Spatrick ", should_stop = 0 (ignore since no stop reason)",
776061da546Spatrick __FUNCTION__, GetID(), GetProtocolID(),
777061da546Spatrick GetRegisterContext() ? GetRegisterContext()->GetPC()
778061da546Spatrick : LLDB_INVALID_ADDRESS);
779061da546Spatrick return false;
780061da546Spatrick }
781061da546Spatrick
782*f6aab3d8Srobert // Clear the "must run me before stop" if it was set:
783*f6aab3d8Srobert SetShouldRunBeforePublicStop(false);
784*f6aab3d8Srobert
785061da546Spatrick if (log) {
786061da546Spatrick LLDB_LOGF(log,
787061da546Spatrick "Thread::%s(%p) for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64
788061da546Spatrick ", pc = 0x%16.16" PRIx64,
789061da546Spatrick __FUNCTION__, static_cast<void *>(this), GetID(), GetProtocolID(),
790061da546Spatrick GetRegisterContext() ? GetRegisterContext()->GetPC()
791061da546Spatrick : LLDB_INVALID_ADDRESS);
792061da546Spatrick LLDB_LOGF(log, "^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
793061da546Spatrick StreamString s;
794061da546Spatrick s.IndentMore();
795dda28197Spatrick GetProcess()->DumpThreadPlansForTID(
796dda28197Spatrick s, GetID(), eDescriptionLevelVerbose, true /* internal */,
797dda28197Spatrick false /* condense_trivial */, true /* skip_unreported */);
798061da546Spatrick LLDB_LOGF(log, "Plan stack initial state:\n%s", s.GetData());
799061da546Spatrick }
800061da546Spatrick
801061da546Spatrick // The top most plan always gets to do the trace log...
802061da546Spatrick current_plan->DoTraceLog();
803061da546Spatrick
804061da546Spatrick // First query the stop info's ShouldStopSynchronous. This handles
805061da546Spatrick // "synchronous" stop reasons, for example the breakpoint command on internal
806061da546Spatrick // breakpoints. If a synchronous stop reason says we should not stop, then
807061da546Spatrick // we don't have to do any more work on this stop.
808061da546Spatrick StopInfoSP private_stop_info(GetPrivateStopInfo());
809061da546Spatrick if (private_stop_info &&
810061da546Spatrick !private_stop_info->ShouldStopSynchronous(event_ptr)) {
811061da546Spatrick LLDB_LOGF(log, "StopInfo::ShouldStop async callback says we should not "
812061da546Spatrick "stop, returning ShouldStop of false.");
813061da546Spatrick return false;
814061da546Spatrick }
815061da546Spatrick
816061da546Spatrick // If we've already been restarted, don't query the plans since the state
817061da546Spatrick // they would examine is not current.
818061da546Spatrick if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
819061da546Spatrick return false;
820061da546Spatrick
821061da546Spatrick // Before the plans see the state of the world, calculate the current inlined
822061da546Spatrick // depth.
823061da546Spatrick GetStackFrameList()->CalculateCurrentInlinedDepth();
824061da546Spatrick
825061da546Spatrick // If the base plan doesn't understand why we stopped, then we have to find a
826061da546Spatrick // plan that does. If that plan is still working, then we don't need to do
827061da546Spatrick // any more work. If the plan that explains the stop is done, then we should
828061da546Spatrick // pop all the plans below it, and pop it, and then let the plans above it
829061da546Spatrick // decide whether they still need to do more work.
830061da546Spatrick
831061da546Spatrick bool done_processing_current_plan = false;
832061da546Spatrick
833061da546Spatrick if (!current_plan->PlanExplainsStop(event_ptr)) {
834061da546Spatrick if (current_plan->TracerExplainsStop()) {
835061da546Spatrick done_processing_current_plan = true;
836061da546Spatrick should_stop = false;
837061da546Spatrick } else {
838061da546Spatrick // If the current plan doesn't explain the stop, then find one that does
839061da546Spatrick // and let it handle the situation.
840061da546Spatrick ThreadPlan *plan_ptr = current_plan;
841061da546Spatrick while ((plan_ptr = GetPreviousPlan(plan_ptr)) != nullptr) {
842061da546Spatrick if (plan_ptr->PlanExplainsStop(event_ptr)) {
843be691f3bSpatrick LLDB_LOGF(log, "Plan %s explains stop.", plan_ptr->GetName());
844be691f3bSpatrick
845061da546Spatrick should_stop = plan_ptr->ShouldStop(event_ptr);
846061da546Spatrick
847061da546Spatrick // plan_ptr explains the stop, next check whether plan_ptr is done,
848061da546Spatrick // if so, then we should take it and all the plans below it off the
849061da546Spatrick // stack.
850061da546Spatrick
851061da546Spatrick if (plan_ptr->MischiefManaged()) {
852061da546Spatrick // We're going to pop the plans up to and including the plan that
853061da546Spatrick // explains the stop.
854061da546Spatrick ThreadPlan *prev_plan_ptr = GetPreviousPlan(plan_ptr);
855061da546Spatrick
856061da546Spatrick do {
857061da546Spatrick if (should_stop)
858061da546Spatrick current_plan->WillStop();
859061da546Spatrick PopPlan();
860061da546Spatrick } while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
861061da546Spatrick // Now, if the responsible plan was not "Okay to discard" then
862061da546Spatrick // we're done, otherwise we forward this to the next plan in the
863061da546Spatrick // stack below.
864061da546Spatrick done_processing_current_plan =
865*f6aab3d8Srobert (plan_ptr->IsControllingPlan() && !plan_ptr->OkayToDiscard());
866*f6aab3d8Srobert } else {
867*f6aab3d8Srobert bool should_force_run = plan_ptr->ShouldRunBeforePublicStop();
868*f6aab3d8Srobert if (should_force_run) {
869*f6aab3d8Srobert SetShouldRunBeforePublicStop(true);
870*f6aab3d8Srobert should_stop = false;
871*f6aab3d8Srobert }
872061da546Spatrick done_processing_current_plan = true;
873*f6aab3d8Srobert }
874061da546Spatrick break;
875061da546Spatrick }
876061da546Spatrick }
877061da546Spatrick }
878061da546Spatrick }
879061da546Spatrick
880061da546Spatrick if (!done_processing_current_plan) {
881be691f3bSpatrick bool override_stop = false;
882061da546Spatrick
883061da546Spatrick // We're starting from the base plan, so just let it decide;
884dda28197Spatrick if (current_plan->IsBasePlan()) {
885061da546Spatrick should_stop = current_plan->ShouldStop(event_ptr);
886061da546Spatrick LLDB_LOGF(log, "Base plan says should stop: %i.", should_stop);
887061da546Spatrick } else {
888061da546Spatrick // Otherwise, don't let the base plan override what the other plans say
889061da546Spatrick // to do, since presumably if there were other plans they would know what
890061da546Spatrick // to do...
891061da546Spatrick while (true) {
892dda28197Spatrick if (current_plan->IsBasePlan())
893061da546Spatrick break;
894061da546Spatrick
895061da546Spatrick should_stop = current_plan->ShouldStop(event_ptr);
896061da546Spatrick LLDB_LOGF(log, "Plan %s should stop: %d.", current_plan->GetName(),
897061da546Spatrick should_stop);
898061da546Spatrick if (current_plan->MischiefManaged()) {
899061da546Spatrick if (should_stop)
900061da546Spatrick current_plan->WillStop();
901061da546Spatrick
902be691f3bSpatrick if (current_plan->ShouldAutoContinue(event_ptr)) {
903be691f3bSpatrick override_stop = true;
904be691f3bSpatrick LLDB_LOGF(log, "Plan %s auto-continue: true.",
905be691f3bSpatrick current_plan->GetName());
906be691f3bSpatrick }
907061da546Spatrick
908*f6aab3d8Srobert // If a Controlling Plan wants to stop, we let it. Otherwise, see if
909*f6aab3d8Srobert // the plan's parent wants to stop.
910be691f3bSpatrick
911be691f3bSpatrick PopPlan();
912*f6aab3d8Srobert if (should_stop && current_plan->IsControllingPlan() &&
913061da546Spatrick !current_plan->OkayToDiscard()) {
914061da546Spatrick break;
915be691f3bSpatrick }
916061da546Spatrick
917061da546Spatrick current_plan = GetCurrentPlan();
918061da546Spatrick if (current_plan == nullptr) {
919061da546Spatrick break;
920061da546Spatrick }
921061da546Spatrick } else {
922061da546Spatrick break;
923061da546Spatrick }
924061da546Spatrick }
925061da546Spatrick }
926061da546Spatrick
927be691f3bSpatrick if (override_stop)
928061da546Spatrick should_stop = false;
929061da546Spatrick }
930061da546Spatrick
931*f6aab3d8Srobert // One other potential problem is that we set up a controlling plan, then stop
932*f6aab3d8Srobert // in before it is complete - for instance by hitting a breakpoint during a
933061da546Spatrick // step-over - then do some step/finish/etc operations that wind up past the
934061da546Spatrick // end point condition of the initial plan. We don't want to strand the
935061da546Spatrick // original plan on the stack, This code clears stale plans off the stack.
936061da546Spatrick
937061da546Spatrick if (should_stop) {
938061da546Spatrick ThreadPlan *plan_ptr = GetCurrentPlan();
939061da546Spatrick
940061da546Spatrick // Discard the stale plans and all plans below them in the stack, plus move
941061da546Spatrick // the completed plans to the completed plan stack
942dda28197Spatrick while (!plan_ptr->IsBasePlan()) {
943061da546Spatrick bool stale = plan_ptr->IsPlanStale();
944061da546Spatrick ThreadPlan *examined_plan = plan_ptr;
945061da546Spatrick plan_ptr = GetPreviousPlan(examined_plan);
946061da546Spatrick
947061da546Spatrick if (stale) {
948061da546Spatrick LLDB_LOGF(
949061da546Spatrick log,
950061da546Spatrick "Plan %s being discarded in cleanup, it says it is already done.",
951061da546Spatrick examined_plan->GetName());
952061da546Spatrick while (GetCurrentPlan() != examined_plan) {
953061da546Spatrick DiscardPlan();
954061da546Spatrick }
955061da546Spatrick if (examined_plan->IsPlanComplete()) {
956061da546Spatrick // plan is complete but does not explain the stop (example: step to a
957061da546Spatrick // line with breakpoint), let us move the plan to
958061da546Spatrick // completed_plan_stack anyway
959061da546Spatrick PopPlan();
960061da546Spatrick } else
961061da546Spatrick DiscardPlan();
962061da546Spatrick }
963061da546Spatrick }
964061da546Spatrick }
965061da546Spatrick
966061da546Spatrick if (log) {
967061da546Spatrick StreamString s;
968061da546Spatrick s.IndentMore();
969dda28197Spatrick GetProcess()->DumpThreadPlansForTID(
970dda28197Spatrick s, GetID(), eDescriptionLevelVerbose, true /* internal */,
971dda28197Spatrick false /* condense_trivial */, true /* skip_unreported */);
972061da546Spatrick LLDB_LOGF(log, "Plan stack final state:\n%s", s.GetData());
973061da546Spatrick LLDB_LOGF(log, "vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv",
974061da546Spatrick should_stop);
975061da546Spatrick }
976061da546Spatrick return should_stop;
977061da546Spatrick }
978061da546Spatrick
ShouldReportStop(Event * event_ptr)979061da546Spatrick Vote Thread::ShouldReportStop(Event *event_ptr) {
980061da546Spatrick StateType thread_state = GetResumeState();
981061da546Spatrick StateType temp_thread_state = GetTemporaryResumeState();
982061da546Spatrick
983*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Step);
984061da546Spatrick
985061da546Spatrick if (thread_state == eStateSuspended || thread_state == eStateInvalid) {
986061da546Spatrick LLDB_LOGF(log,
987061da546Spatrick "Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
988061da546Spatrick ": returning vote %i (state was suspended or invalid)",
989061da546Spatrick GetID(), eVoteNoOpinion);
990061da546Spatrick return eVoteNoOpinion;
991061da546Spatrick }
992061da546Spatrick
993061da546Spatrick if (temp_thread_state == eStateSuspended ||
994061da546Spatrick temp_thread_state == eStateInvalid) {
995061da546Spatrick LLDB_LOGF(log,
996061da546Spatrick "Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
997061da546Spatrick ": returning vote %i (temporary state was suspended or invalid)",
998061da546Spatrick GetID(), eVoteNoOpinion);
999061da546Spatrick return eVoteNoOpinion;
1000061da546Spatrick }
1001061da546Spatrick
1002061da546Spatrick if (!ThreadStoppedForAReason()) {
1003061da546Spatrick LLDB_LOGF(log,
1004061da546Spatrick "Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
1005061da546Spatrick ": returning vote %i (thread didn't stop for a reason.)",
1006061da546Spatrick GetID(), eVoteNoOpinion);
1007061da546Spatrick return eVoteNoOpinion;
1008061da546Spatrick }
1009061da546Spatrick
1010dda28197Spatrick if (GetPlans().AnyCompletedPlans()) {
1011dda28197Spatrick // Pass skip_private = false to GetCompletedPlan, since we want to ask
1012dda28197Spatrick // the last plan, regardless of whether it is private or not.
1013061da546Spatrick LLDB_LOGF(log,
1014061da546Spatrick "Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
1015061da546Spatrick ": returning vote for complete stack's back plan",
1016061da546Spatrick GetID());
1017dda28197Spatrick return GetPlans().GetCompletedPlan(false)->ShouldReportStop(event_ptr);
1018061da546Spatrick } else {
1019061da546Spatrick Vote thread_vote = eVoteNoOpinion;
1020061da546Spatrick ThreadPlan *plan_ptr = GetCurrentPlan();
1021061da546Spatrick while (true) {
1022061da546Spatrick if (plan_ptr->PlanExplainsStop(event_ptr)) {
1023061da546Spatrick thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1024061da546Spatrick break;
1025061da546Spatrick }
1026dda28197Spatrick if (plan_ptr->IsBasePlan())
1027061da546Spatrick break;
1028061da546Spatrick else
1029061da546Spatrick plan_ptr = GetPreviousPlan(plan_ptr);
1030061da546Spatrick }
1031061da546Spatrick LLDB_LOGF(log,
1032061da546Spatrick "Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
1033061da546Spatrick ": returning vote %i for current plan",
1034061da546Spatrick GetID(), thread_vote);
1035061da546Spatrick
1036061da546Spatrick return thread_vote;
1037061da546Spatrick }
1038061da546Spatrick }
1039061da546Spatrick
ShouldReportRun(Event * event_ptr)1040061da546Spatrick Vote Thread::ShouldReportRun(Event *event_ptr) {
1041061da546Spatrick StateType thread_state = GetResumeState();
1042061da546Spatrick
1043061da546Spatrick if (thread_state == eStateSuspended || thread_state == eStateInvalid) {
1044061da546Spatrick return eVoteNoOpinion;
1045061da546Spatrick }
1046061da546Spatrick
1047*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Step);
1048dda28197Spatrick if (GetPlans().AnyCompletedPlans()) {
1049dda28197Spatrick // Pass skip_private = false to GetCompletedPlan, since we want to ask
1050dda28197Spatrick // the last plan, regardless of whether it is private or not.
1051061da546Spatrick LLDB_LOGF(log,
1052061da546Spatrick "Current Plan for thread %d(%p) (0x%4.4" PRIx64
1053061da546Spatrick ", %s): %s being asked whether we should report run.",
1054061da546Spatrick GetIndexID(), static_cast<void *>(this), GetID(),
1055061da546Spatrick StateAsCString(GetTemporaryResumeState()),
1056dda28197Spatrick GetCompletedPlan()->GetName());
1057061da546Spatrick
1058dda28197Spatrick return GetPlans().GetCompletedPlan(false)->ShouldReportRun(event_ptr);
1059061da546Spatrick } else {
1060061da546Spatrick LLDB_LOGF(log,
1061061da546Spatrick "Current Plan for thread %d(%p) (0x%4.4" PRIx64
1062061da546Spatrick ", %s): %s being asked whether we should report run.",
1063061da546Spatrick GetIndexID(), static_cast<void *>(this), GetID(),
1064061da546Spatrick StateAsCString(GetTemporaryResumeState()),
1065061da546Spatrick GetCurrentPlan()->GetName());
1066061da546Spatrick
1067061da546Spatrick return GetCurrentPlan()->ShouldReportRun(event_ptr);
1068061da546Spatrick }
1069061da546Spatrick }
1070061da546Spatrick
MatchesSpec(const ThreadSpec * spec)1071061da546Spatrick bool Thread::MatchesSpec(const ThreadSpec *spec) {
1072061da546Spatrick return (spec == nullptr) ? true : spec->ThreadPassesBasicTests(*this);
1073061da546Spatrick }
1074061da546Spatrick
GetPlans() const1075dda28197Spatrick ThreadPlanStack &Thread::GetPlans() const {
1076dda28197Spatrick ThreadPlanStack *plans = GetProcess()->FindThreadPlans(GetID());
1077dda28197Spatrick if (plans)
1078dda28197Spatrick return *plans;
1079061da546Spatrick
1080dda28197Spatrick // History threads don't have a thread plan, but they do ask get asked to
1081dda28197Spatrick // describe themselves, which usually involves pulling out the stop reason.
1082dda28197Spatrick // That in turn will check for a completed plan on the ThreadPlanStack.
1083dda28197Spatrick // Instead of special-casing at that point, we return a Stack with a
1084dda28197Spatrick // ThreadPlanNull as its base plan. That will give the right answers to the
1085dda28197Spatrick // queries GetDescription makes, and only assert if you try to run the thread.
1086dda28197Spatrick if (!m_null_plan_stack_up)
1087dda28197Spatrick m_null_plan_stack_up = std::make_unique<ThreadPlanStack>(*this, true);
1088*f6aab3d8Srobert return *m_null_plan_stack_up;
1089dda28197Spatrick }
1090dda28197Spatrick
PushPlan(ThreadPlanSP thread_plan_sp)1091dda28197Spatrick void Thread::PushPlan(ThreadPlanSP thread_plan_sp) {
1092dda28197Spatrick assert(thread_plan_sp && "Don't push an empty thread plan.");
1093061da546Spatrick
1094*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Step);
1095061da546Spatrick if (log) {
1096061da546Spatrick StreamString s;
1097061da546Spatrick thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull);
1098061da546Spatrick LLDB_LOGF(log, "Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
1099061da546Spatrick static_cast<void *>(this), s.GetData(),
1100061da546Spatrick thread_plan_sp->GetThread().GetID());
1101061da546Spatrick }
1102dda28197Spatrick
1103dda28197Spatrick GetPlans().PushPlan(std::move(thread_plan_sp));
1104061da546Spatrick }
1105061da546Spatrick
PopPlan()1106061da546Spatrick void Thread::PopPlan() {
1107*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Step);
1108dda28197Spatrick ThreadPlanSP popped_plan_sp = GetPlans().PopPlan();
1109061da546Spatrick if (log) {
1110061da546Spatrick LLDB_LOGF(log, "Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
1111dda28197Spatrick popped_plan_sp->GetName(), popped_plan_sp->GetThread().GetID());
1112061da546Spatrick }
1113061da546Spatrick }
1114061da546Spatrick
DiscardPlan()1115061da546Spatrick void Thread::DiscardPlan() {
1116*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Step);
1117*f6aab3d8Srobert ThreadPlanSP discarded_plan_sp = GetPlans().DiscardPlan();
1118dda28197Spatrick
1119061da546Spatrick LLDB_LOGF(log, "Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
1120dda28197Spatrick discarded_plan_sp->GetName(),
1121dda28197Spatrick discarded_plan_sp->GetThread().GetID());
1122061da546Spatrick }
1123061da546Spatrick
AutoCompleteThreadPlans(CompletionRequest & request) const1124be691f3bSpatrick void Thread::AutoCompleteThreadPlans(CompletionRequest &request) const {
1125be691f3bSpatrick const ThreadPlanStack &plans = GetPlans();
1126be691f3bSpatrick if (!plans.AnyPlans())
1127be691f3bSpatrick return;
1128be691f3bSpatrick
1129be691f3bSpatrick // Iterate from the second plan (index: 1) to skip the base plan.
1130be691f3bSpatrick ThreadPlanSP p;
1131be691f3bSpatrick uint32_t i = 1;
1132be691f3bSpatrick while ((p = plans.GetPlanByIndex(i, false))) {
1133be691f3bSpatrick StreamString strm;
1134be691f3bSpatrick p->GetDescription(&strm, eDescriptionLevelInitial);
1135be691f3bSpatrick request.TryCompleteCurrentArg(std::to_string(i), strm.GetString());
1136be691f3bSpatrick i++;
1137be691f3bSpatrick }
1138be691f3bSpatrick }
1139be691f3bSpatrick
GetCurrentPlan() const1140dda28197Spatrick ThreadPlan *Thread::GetCurrentPlan() const {
1141dda28197Spatrick return GetPlans().GetCurrentPlan().get();
1142061da546Spatrick }
1143061da546Spatrick
GetCompletedPlan() const1144dda28197Spatrick ThreadPlanSP Thread::GetCompletedPlan() const {
1145dda28197Spatrick return GetPlans().GetCompletedPlan();
1146061da546Spatrick }
1147061da546Spatrick
GetReturnValueObject() const1148dda28197Spatrick ValueObjectSP Thread::GetReturnValueObject() const {
1149dda28197Spatrick return GetPlans().GetReturnValueObject();
1150061da546Spatrick }
1151061da546Spatrick
GetExpressionVariable() const1152dda28197Spatrick ExpressionVariableSP Thread::GetExpressionVariable() const {
1153dda28197Spatrick return GetPlans().GetExpressionVariable();
1154061da546Spatrick }
1155061da546Spatrick
IsThreadPlanDone(ThreadPlan * plan) const1156dda28197Spatrick bool Thread::IsThreadPlanDone(ThreadPlan *plan) const {
1157dda28197Spatrick return GetPlans().IsPlanDone(plan);
1158061da546Spatrick }
1159061da546Spatrick
WasThreadPlanDiscarded(ThreadPlan * plan) const1160dda28197Spatrick bool Thread::WasThreadPlanDiscarded(ThreadPlan *plan) const {
1161dda28197Spatrick return GetPlans().WasPlanDiscarded(plan);
1162061da546Spatrick }
1163061da546Spatrick
CompletedPlanOverridesBreakpoint() const1164dda28197Spatrick bool Thread::CompletedPlanOverridesBreakpoint() const {
1165dda28197Spatrick return GetPlans().AnyCompletedPlans();
1166061da546Spatrick }
1167061da546Spatrick
GetPreviousPlan(ThreadPlan * current_plan) const1168dda28197Spatrick ThreadPlan *Thread::GetPreviousPlan(ThreadPlan *current_plan) const{
1169dda28197Spatrick return GetPlans().GetPreviousPlan(current_plan);
1170061da546Spatrick }
1171061da546Spatrick
QueueThreadPlan(ThreadPlanSP & thread_plan_sp,bool abort_other_plans)1172061da546Spatrick Status Thread::QueueThreadPlan(ThreadPlanSP &thread_plan_sp,
1173061da546Spatrick bool abort_other_plans) {
1174061da546Spatrick Status status;
1175061da546Spatrick StreamString s;
1176061da546Spatrick if (!thread_plan_sp->ValidatePlan(&s)) {
1177061da546Spatrick DiscardThreadPlansUpToPlan(thread_plan_sp);
1178061da546Spatrick thread_plan_sp.reset();
1179061da546Spatrick status.SetErrorString(s.GetString());
1180061da546Spatrick return status;
1181061da546Spatrick }
1182061da546Spatrick
1183061da546Spatrick if (abort_other_plans)
1184061da546Spatrick DiscardThreadPlans(true);
1185061da546Spatrick
1186061da546Spatrick PushPlan(thread_plan_sp);
1187061da546Spatrick
1188061da546Spatrick // This seems a little funny, but I don't want to have to split up the
1189061da546Spatrick // constructor and the DidPush in the scripted plan, that seems annoying.
1190061da546Spatrick // That means the constructor has to be in DidPush. So I have to validate the
1191061da546Spatrick // plan AFTER pushing it, and then take it off again...
1192061da546Spatrick if (!thread_plan_sp->ValidatePlan(&s)) {
1193061da546Spatrick DiscardThreadPlansUpToPlan(thread_plan_sp);
1194061da546Spatrick thread_plan_sp.reset();
1195061da546Spatrick status.SetErrorString(s.GetString());
1196061da546Spatrick return status;
1197061da546Spatrick }
1198061da546Spatrick
1199061da546Spatrick return status;
1200061da546Spatrick }
1201061da546Spatrick
DiscardUserThreadPlansUpToIndex(uint32_t plan_index)1202dda28197Spatrick bool Thread::DiscardUserThreadPlansUpToIndex(uint32_t plan_index) {
1203061da546Spatrick // Count the user thread plans from the back end to get the number of the one
1204061da546Spatrick // we want to discard:
1205061da546Spatrick
1206dda28197Spatrick ThreadPlan *up_to_plan_ptr = GetPlans().GetPlanByIndex(plan_index).get();
1207061da546Spatrick if (up_to_plan_ptr == nullptr)
1208061da546Spatrick return false;
1209061da546Spatrick
1210061da546Spatrick DiscardThreadPlansUpToPlan(up_to_plan_ptr);
1211061da546Spatrick return true;
1212061da546Spatrick }
1213061da546Spatrick
DiscardThreadPlansUpToPlan(lldb::ThreadPlanSP & up_to_plan_sp)1214061da546Spatrick void Thread::DiscardThreadPlansUpToPlan(lldb::ThreadPlanSP &up_to_plan_sp) {
1215061da546Spatrick DiscardThreadPlansUpToPlan(up_to_plan_sp.get());
1216061da546Spatrick }
1217061da546Spatrick
DiscardThreadPlansUpToPlan(ThreadPlan * up_to_plan_ptr)1218061da546Spatrick void Thread::DiscardThreadPlansUpToPlan(ThreadPlan *up_to_plan_ptr) {
1219*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Step);
1220061da546Spatrick LLDB_LOGF(log,
1221061da546Spatrick "Discarding thread plans for thread tid = 0x%4.4" PRIx64
1222061da546Spatrick ", up to %p",
1223061da546Spatrick GetID(), static_cast<void *>(up_to_plan_ptr));
1224dda28197Spatrick GetPlans().DiscardPlansUpToPlan(up_to_plan_ptr);
1225061da546Spatrick }
1226061da546Spatrick
DiscardThreadPlans(bool force)1227061da546Spatrick void Thread::DiscardThreadPlans(bool force) {
1228*f6aab3d8Srobert Log *log = GetLog(LLDBLog::Step);
1229061da546Spatrick if (log) {
1230061da546Spatrick LLDB_LOGF(log,
1231061da546Spatrick "Discarding thread plans for thread (tid = 0x%4.4" PRIx64
1232061da546Spatrick ", force %d)",
1233061da546Spatrick GetID(), force);
1234061da546Spatrick }
1235061da546Spatrick
1236061da546Spatrick if (force) {
1237dda28197Spatrick GetPlans().DiscardAllPlans();
1238061da546Spatrick return;
1239061da546Spatrick }
1240*f6aab3d8Srobert GetPlans().DiscardConsultingControllingPlans();
1241061da546Spatrick }
1242061da546Spatrick
UnwindInnermostExpression()1243061da546Spatrick Status Thread::UnwindInnermostExpression() {
1244061da546Spatrick Status error;
1245dda28197Spatrick ThreadPlan *innermost_expr_plan = GetPlans().GetInnermostExpression();
1246dda28197Spatrick if (!innermost_expr_plan) {
1247dda28197Spatrick error.SetErrorString("No expressions currently active on this thread");
1248061da546Spatrick return error;
1249061da546Spatrick }
1250dda28197Spatrick DiscardThreadPlansUpToPlan(innermost_expr_plan);
1251061da546Spatrick return error;
1252061da546Spatrick }
1253061da546Spatrick
QueueBasePlan(bool abort_other_plans)1254be691f3bSpatrick ThreadPlanSP Thread::QueueBasePlan(bool abort_other_plans) {
1255061da546Spatrick ThreadPlanSP thread_plan_sp(new ThreadPlanBase(*this));
1256061da546Spatrick QueueThreadPlan(thread_plan_sp, abort_other_plans);
1257061da546Spatrick return thread_plan_sp;
1258061da546Spatrick }
1259061da546Spatrick
QueueThreadPlanForStepSingleInstruction(bool step_over,bool abort_other_plans,bool stop_other_threads,Status & status)1260061da546Spatrick ThreadPlanSP Thread::QueueThreadPlanForStepSingleInstruction(
1261061da546Spatrick bool step_over, bool abort_other_plans, bool stop_other_threads,
1262061da546Spatrick Status &status) {
1263061da546Spatrick ThreadPlanSP thread_plan_sp(new ThreadPlanStepInstruction(
1264061da546Spatrick *this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1265061da546Spatrick status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1266061da546Spatrick return thread_plan_sp;
1267061da546Spatrick }
1268061da546Spatrick
QueueThreadPlanForStepOverRange(bool abort_other_plans,const AddressRange & range,const SymbolContext & addr_context,lldb::RunMode stop_other_threads,Status & status,LazyBool step_out_avoids_code_withoug_debug_info)1269061da546Spatrick ThreadPlanSP Thread::QueueThreadPlanForStepOverRange(
1270061da546Spatrick bool abort_other_plans, const AddressRange &range,
1271061da546Spatrick const SymbolContext &addr_context, lldb::RunMode stop_other_threads,
1272061da546Spatrick Status &status, LazyBool step_out_avoids_code_withoug_debug_info) {
1273061da546Spatrick ThreadPlanSP thread_plan_sp;
1274061da546Spatrick thread_plan_sp = std::make_shared<ThreadPlanStepOverRange>(
1275061da546Spatrick *this, range, addr_context, stop_other_threads,
1276061da546Spatrick step_out_avoids_code_withoug_debug_info);
1277061da546Spatrick
1278061da546Spatrick status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1279061da546Spatrick return thread_plan_sp;
1280061da546Spatrick }
1281061da546Spatrick
1282061da546Spatrick // Call the QueueThreadPlanForStepOverRange method which takes an address
1283061da546Spatrick // range.
QueueThreadPlanForStepOverRange(bool abort_other_plans,const LineEntry & line_entry,const SymbolContext & addr_context,lldb::RunMode stop_other_threads,Status & status,LazyBool step_out_avoids_code_withoug_debug_info)1284061da546Spatrick ThreadPlanSP Thread::QueueThreadPlanForStepOverRange(
1285061da546Spatrick bool abort_other_plans, const LineEntry &line_entry,
1286061da546Spatrick const SymbolContext &addr_context, lldb::RunMode stop_other_threads,
1287061da546Spatrick Status &status, LazyBool step_out_avoids_code_withoug_debug_info) {
1288061da546Spatrick const bool include_inlined_functions = true;
1289061da546Spatrick auto address_range =
1290061da546Spatrick line_entry.GetSameLineContiguousAddressRange(include_inlined_functions);
1291061da546Spatrick return QueueThreadPlanForStepOverRange(
1292061da546Spatrick abort_other_plans, address_range, addr_context, stop_other_threads,
1293061da546Spatrick status, step_out_avoids_code_withoug_debug_info);
1294061da546Spatrick }
1295061da546Spatrick
QueueThreadPlanForStepInRange(bool abort_other_plans,const AddressRange & range,const SymbolContext & addr_context,const char * step_in_target,lldb::RunMode stop_other_threads,Status & status,LazyBool step_in_avoids_code_without_debug_info,LazyBool step_out_avoids_code_without_debug_info)1296061da546Spatrick ThreadPlanSP Thread::QueueThreadPlanForStepInRange(
1297061da546Spatrick bool abort_other_plans, const AddressRange &range,
1298061da546Spatrick const SymbolContext &addr_context, const char *step_in_target,
1299061da546Spatrick lldb::RunMode stop_other_threads, Status &status,
1300061da546Spatrick LazyBool step_in_avoids_code_without_debug_info,
1301061da546Spatrick LazyBool step_out_avoids_code_without_debug_info) {
1302be691f3bSpatrick ThreadPlanSP thread_plan_sp(new ThreadPlanStepInRange(
1303be691f3bSpatrick *this, range, addr_context, step_in_target, stop_other_threads,
1304061da546Spatrick step_in_avoids_code_without_debug_info,
1305061da546Spatrick step_out_avoids_code_without_debug_info));
1306061da546Spatrick status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1307061da546Spatrick return thread_plan_sp;
1308061da546Spatrick }
1309061da546Spatrick
1310061da546Spatrick // Call the QueueThreadPlanForStepInRange method which takes an address range.
QueueThreadPlanForStepInRange(bool abort_other_plans,const LineEntry & line_entry,const SymbolContext & addr_context,const char * step_in_target,lldb::RunMode stop_other_threads,Status & status,LazyBool step_in_avoids_code_without_debug_info,LazyBool step_out_avoids_code_without_debug_info)1311061da546Spatrick ThreadPlanSP Thread::QueueThreadPlanForStepInRange(
1312061da546Spatrick bool abort_other_plans, const LineEntry &line_entry,
1313061da546Spatrick const SymbolContext &addr_context, const char *step_in_target,
1314061da546Spatrick lldb::RunMode stop_other_threads, Status &status,
1315061da546Spatrick LazyBool step_in_avoids_code_without_debug_info,
1316061da546Spatrick LazyBool step_out_avoids_code_without_debug_info) {
1317061da546Spatrick const bool include_inlined_functions = false;
1318061da546Spatrick return QueueThreadPlanForStepInRange(
1319061da546Spatrick abort_other_plans,
1320061da546Spatrick line_entry.GetSameLineContiguousAddressRange(include_inlined_functions),
1321061da546Spatrick addr_context, step_in_target, stop_other_threads, status,
1322061da546Spatrick step_in_avoids_code_without_debug_info,
1323061da546Spatrick step_out_avoids_code_without_debug_info);
1324061da546Spatrick }
1325061da546Spatrick
QueueThreadPlanForStepOut(bool abort_other_plans,SymbolContext * addr_context,bool first_insn,bool stop_other_threads,Vote report_stop_vote,Vote report_run_vote,uint32_t frame_idx,Status & status,LazyBool step_out_avoids_code_without_debug_info)1326061da546Spatrick ThreadPlanSP Thread::QueueThreadPlanForStepOut(
1327061da546Spatrick bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
1328be691f3bSpatrick bool stop_other_threads, Vote report_stop_vote, Vote report_run_vote,
1329be691f3bSpatrick uint32_t frame_idx, Status &status,
1330be691f3bSpatrick LazyBool step_out_avoids_code_without_debug_info) {
1331061da546Spatrick ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut(
1332be691f3bSpatrick *this, addr_context, first_insn, stop_other_threads, report_stop_vote,
1333be691f3bSpatrick report_run_vote, frame_idx, step_out_avoids_code_without_debug_info));
1334061da546Spatrick
1335061da546Spatrick status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1336061da546Spatrick return thread_plan_sp;
1337061da546Spatrick }
1338061da546Spatrick
QueueThreadPlanForStepOutNoShouldStop(bool abort_other_plans,SymbolContext * addr_context,bool first_insn,bool stop_other_threads,Vote report_stop_vote,Vote report_run_vote,uint32_t frame_idx,Status & status,bool continue_to_next_branch)1339061da546Spatrick ThreadPlanSP Thread::QueueThreadPlanForStepOutNoShouldStop(
1340061da546Spatrick bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
1341be691f3bSpatrick bool stop_other_threads, Vote report_stop_vote, Vote report_run_vote,
1342be691f3bSpatrick uint32_t frame_idx, Status &status, bool continue_to_next_branch) {
1343061da546Spatrick const bool calculate_return_value =
1344061da546Spatrick false; // No need to calculate the return value here.
1345061da546Spatrick ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut(
1346be691f3bSpatrick *this, addr_context, first_insn, stop_other_threads, report_stop_vote,
1347be691f3bSpatrick report_run_vote, frame_idx, eLazyBoolNo, continue_to_next_branch,
1348be691f3bSpatrick calculate_return_value));
1349061da546Spatrick
1350061da546Spatrick ThreadPlanStepOut *new_plan =
1351061da546Spatrick static_cast<ThreadPlanStepOut *>(thread_plan_sp.get());
1352061da546Spatrick new_plan->ClearShouldStopHereCallbacks();
1353061da546Spatrick
1354061da546Spatrick status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1355061da546Spatrick return thread_plan_sp;
1356061da546Spatrick }
1357061da546Spatrick
QueueThreadPlanForStepThrough(StackID & return_stack_id,bool abort_other_plans,bool stop_other_threads,Status & status)1358061da546Spatrick ThreadPlanSP Thread::QueueThreadPlanForStepThrough(StackID &return_stack_id,
1359061da546Spatrick bool abort_other_plans,
1360061da546Spatrick bool stop_other_threads,
1361061da546Spatrick Status &status) {
1362061da546Spatrick ThreadPlanSP thread_plan_sp(
1363061da546Spatrick new ThreadPlanStepThrough(*this, return_stack_id, stop_other_threads));
1364061da546Spatrick if (!thread_plan_sp || !thread_plan_sp->ValidatePlan(nullptr))
1365061da546Spatrick return ThreadPlanSP();
1366061da546Spatrick
1367061da546Spatrick status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1368061da546Spatrick return thread_plan_sp;
1369061da546Spatrick }
1370061da546Spatrick
QueueThreadPlanForRunToAddress(bool abort_other_plans,Address & target_addr,bool stop_other_threads,Status & status)1371061da546Spatrick ThreadPlanSP Thread::QueueThreadPlanForRunToAddress(bool abort_other_plans,
1372061da546Spatrick Address &target_addr,
1373061da546Spatrick bool stop_other_threads,
1374061da546Spatrick Status &status) {
1375061da546Spatrick ThreadPlanSP thread_plan_sp(
1376061da546Spatrick new ThreadPlanRunToAddress(*this, target_addr, stop_other_threads));
1377061da546Spatrick
1378061da546Spatrick status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1379061da546Spatrick return thread_plan_sp;
1380061da546Spatrick }
1381061da546Spatrick
QueueThreadPlanForStepUntil(bool abort_other_plans,lldb::addr_t * address_list,size_t num_addresses,bool stop_other_threads,uint32_t frame_idx,Status & status)1382061da546Spatrick ThreadPlanSP Thread::QueueThreadPlanForStepUntil(
1383061da546Spatrick bool abort_other_plans, lldb::addr_t *address_list, size_t num_addresses,
1384061da546Spatrick bool stop_other_threads, uint32_t frame_idx, Status &status) {
1385061da546Spatrick ThreadPlanSP thread_plan_sp(new ThreadPlanStepUntil(
1386061da546Spatrick *this, address_list, num_addresses, stop_other_threads, frame_idx));
1387061da546Spatrick
1388061da546Spatrick status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1389061da546Spatrick return thread_plan_sp;
1390061da546Spatrick }
1391061da546Spatrick
QueueThreadPlanForStepScripted(bool abort_other_plans,const char * class_name,StructuredData::ObjectSP extra_args_sp,bool stop_other_threads,Status & status)1392061da546Spatrick lldb::ThreadPlanSP Thread::QueueThreadPlanForStepScripted(
1393061da546Spatrick bool abort_other_plans, const char *class_name,
1394061da546Spatrick StructuredData::ObjectSP extra_args_sp, bool stop_other_threads,
1395061da546Spatrick Status &status) {
1396061da546Spatrick
1397*f6aab3d8Srobert ThreadPlanSP thread_plan_sp(new ThreadPlanPython(
1398*f6aab3d8Srobert *this, class_name, StructuredDataImpl(extra_args_sp)));
1399be691f3bSpatrick thread_plan_sp->SetStopOthers(stop_other_threads);
1400061da546Spatrick status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1401061da546Spatrick return thread_plan_sp;
1402061da546Spatrick }
1403061da546Spatrick
GetIndexID() const1404061da546Spatrick uint32_t Thread::GetIndexID() const { return m_index_id; }
1405061da546Spatrick
CalculateTarget()1406061da546Spatrick TargetSP Thread::CalculateTarget() {
1407061da546Spatrick TargetSP target_sp;
1408061da546Spatrick ProcessSP process_sp(GetProcess());
1409061da546Spatrick if (process_sp)
1410061da546Spatrick target_sp = process_sp->CalculateTarget();
1411061da546Spatrick return target_sp;
1412061da546Spatrick }
1413061da546Spatrick
CalculateProcess()1414061da546Spatrick ProcessSP Thread::CalculateProcess() { return GetProcess(); }
1415061da546Spatrick
CalculateThread()1416061da546Spatrick ThreadSP Thread::CalculateThread() { return shared_from_this(); }
1417061da546Spatrick
CalculateStackFrame()1418061da546Spatrick StackFrameSP Thread::CalculateStackFrame() { return StackFrameSP(); }
1419061da546Spatrick
CalculateExecutionContext(ExecutionContext & exe_ctx)1420061da546Spatrick void Thread::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1421061da546Spatrick exe_ctx.SetContext(shared_from_this());
1422061da546Spatrick }
1423061da546Spatrick
GetStackFrameList()1424061da546Spatrick StackFrameListSP Thread::GetStackFrameList() {
1425061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
1426061da546Spatrick
1427061da546Spatrick if (!m_curr_frames_sp)
1428061da546Spatrick m_curr_frames_sp =
1429061da546Spatrick std::make_shared<StackFrameList>(*this, m_prev_frames_sp, true);
1430061da546Spatrick
1431061da546Spatrick return m_curr_frames_sp;
1432061da546Spatrick }
1433061da546Spatrick
ClearStackFrames()1434061da546Spatrick void Thread::ClearStackFrames() {
1435061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
1436061da546Spatrick
1437dda28197Spatrick GetUnwinder().Clear();
1438061da546Spatrick
1439061da546Spatrick // Only store away the old "reference" StackFrameList if we got all its
1440061da546Spatrick // frames:
1441061da546Spatrick // FIXME: At some point we can try to splice in the frames we have fetched
1442061da546Spatrick // into
1443061da546Spatrick // the new frame as we make it, but let's not try that now.
1444061da546Spatrick if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
1445061da546Spatrick m_prev_frames_sp.swap(m_curr_frames_sp);
1446061da546Spatrick m_curr_frames_sp.reset();
1447061da546Spatrick
1448061da546Spatrick m_extended_info.reset();
1449061da546Spatrick m_extended_info_fetched = false;
1450061da546Spatrick }
1451061da546Spatrick
GetFrameWithConcreteFrameIndex(uint32_t unwind_idx)1452061da546Spatrick lldb::StackFrameSP Thread::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) {
1453061da546Spatrick return GetStackFrameList()->GetFrameWithConcreteFrameIndex(unwind_idx);
1454061da546Spatrick }
1455061da546Spatrick
ReturnFromFrameWithIndex(uint32_t frame_idx,lldb::ValueObjectSP return_value_sp,bool broadcast)1456061da546Spatrick Status Thread::ReturnFromFrameWithIndex(uint32_t frame_idx,
1457061da546Spatrick lldb::ValueObjectSP return_value_sp,
1458061da546Spatrick bool broadcast) {
1459061da546Spatrick StackFrameSP frame_sp = GetStackFrameAtIndex(frame_idx);
1460061da546Spatrick Status return_error;
1461061da546Spatrick
1462061da546Spatrick if (!frame_sp) {
1463061da546Spatrick return_error.SetErrorStringWithFormat(
1464061da546Spatrick "Could not find frame with index %d in thread 0x%" PRIx64 ".",
1465061da546Spatrick frame_idx, GetID());
1466061da546Spatrick }
1467061da546Spatrick
1468061da546Spatrick return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
1469061da546Spatrick }
1470061da546Spatrick
ReturnFromFrame(lldb::StackFrameSP frame_sp,lldb::ValueObjectSP return_value_sp,bool broadcast)1471061da546Spatrick Status Thread::ReturnFromFrame(lldb::StackFrameSP frame_sp,
1472061da546Spatrick lldb::ValueObjectSP return_value_sp,
1473061da546Spatrick bool broadcast) {
1474061da546Spatrick Status return_error;
1475061da546Spatrick
1476061da546Spatrick if (!frame_sp) {
1477061da546Spatrick return_error.SetErrorString("Can't return to a null frame.");
1478061da546Spatrick return return_error;
1479061da546Spatrick }
1480061da546Spatrick
1481061da546Spatrick Thread *thread = frame_sp->GetThread().get();
1482061da546Spatrick uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1483061da546Spatrick StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
1484061da546Spatrick if (!older_frame_sp) {
1485061da546Spatrick return_error.SetErrorString("No older frame to return to.");
1486061da546Spatrick return return_error;
1487061da546Spatrick }
1488061da546Spatrick
1489061da546Spatrick if (return_value_sp) {
1490061da546Spatrick lldb::ABISP abi = thread->GetProcess()->GetABI();
1491061da546Spatrick if (!abi) {
1492061da546Spatrick return_error.SetErrorString("Could not find ABI to set return value.");
1493061da546Spatrick return return_error;
1494061da546Spatrick }
1495061da546Spatrick SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1496061da546Spatrick
1497061da546Spatrick // FIXME: ValueObject::Cast doesn't currently work correctly, at least not
1498061da546Spatrick // for scalars.
1499061da546Spatrick // Turn that back on when that works.
1500061da546Spatrick if (/* DISABLES CODE */ (false) && sc.function != nullptr) {
1501061da546Spatrick Type *function_type = sc.function->GetType();
1502061da546Spatrick if (function_type) {
1503061da546Spatrick CompilerType return_type =
1504061da546Spatrick sc.function->GetCompilerType().GetFunctionReturnType();
1505061da546Spatrick if (return_type) {
1506061da546Spatrick StreamString s;
1507061da546Spatrick return_type.DumpTypeDescription(&s);
1508061da546Spatrick ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
1509061da546Spatrick if (cast_value_sp) {
1510061da546Spatrick cast_value_sp->SetFormat(eFormatHex);
1511061da546Spatrick return_value_sp = cast_value_sp;
1512061da546Spatrick }
1513061da546Spatrick }
1514061da546Spatrick }
1515061da546Spatrick }
1516061da546Spatrick
1517061da546Spatrick return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
1518061da546Spatrick if (!return_error.Success())
1519061da546Spatrick return return_error;
1520061da546Spatrick }
1521061da546Spatrick
1522061da546Spatrick // Now write the return registers for the chosen frame: Note, we can't use
1523061da546Spatrick // ReadAllRegisterValues->WriteAllRegisterValues, since the read & write cook
1524061da546Spatrick // their data
1525061da546Spatrick
1526061da546Spatrick StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
1527061da546Spatrick if (youngest_frame_sp) {
1528061da546Spatrick lldb::RegisterContextSP reg_ctx_sp(youngest_frame_sp->GetRegisterContext());
1529061da546Spatrick if (reg_ctx_sp) {
1530061da546Spatrick bool copy_success = reg_ctx_sp->CopyFromRegisterContext(
1531061da546Spatrick older_frame_sp->GetRegisterContext());
1532061da546Spatrick if (copy_success) {
1533061da546Spatrick thread->DiscardThreadPlans(true);
1534061da546Spatrick thread->ClearStackFrames();
1535061da546Spatrick if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1536061da546Spatrick BroadcastEvent(eBroadcastBitStackChanged,
1537061da546Spatrick new ThreadEventData(this->shared_from_this()));
1538061da546Spatrick } else {
1539061da546Spatrick return_error.SetErrorString("Could not reset register values.");
1540061da546Spatrick }
1541061da546Spatrick } else {
1542061da546Spatrick return_error.SetErrorString("Frame has no register context.");
1543061da546Spatrick }
1544061da546Spatrick } else {
1545061da546Spatrick return_error.SetErrorString("Returned past top frame.");
1546061da546Spatrick }
1547061da546Spatrick return return_error;
1548061da546Spatrick }
1549061da546Spatrick
DumpAddressList(Stream & s,const std::vector<Address> & list,ExecutionContextScope * exe_scope)1550061da546Spatrick static void DumpAddressList(Stream &s, const std::vector<Address> &list,
1551061da546Spatrick ExecutionContextScope *exe_scope) {
1552061da546Spatrick for (size_t n = 0; n < list.size(); n++) {
1553061da546Spatrick s << "\t";
1554061da546Spatrick list[n].Dump(&s, exe_scope, Address::DumpStyleResolvedDescription,
1555061da546Spatrick Address::DumpStyleSectionNameOffset);
1556061da546Spatrick s << "\n";
1557061da546Spatrick }
1558061da546Spatrick }
1559061da546Spatrick
JumpToLine(const FileSpec & file,uint32_t line,bool can_leave_function,std::string * warnings)1560061da546Spatrick Status Thread::JumpToLine(const FileSpec &file, uint32_t line,
1561061da546Spatrick bool can_leave_function, std::string *warnings) {
1562061da546Spatrick ExecutionContext exe_ctx(GetStackFrameAtIndex(0));
1563061da546Spatrick Target *target = exe_ctx.GetTargetPtr();
1564061da546Spatrick TargetSP target_sp = exe_ctx.GetTargetSP();
1565061da546Spatrick RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
1566061da546Spatrick StackFrame *frame = exe_ctx.GetFramePtr();
1567061da546Spatrick const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1568061da546Spatrick
1569061da546Spatrick // Find candidate locations.
1570061da546Spatrick std::vector<Address> candidates, within_function, outside_function;
1571061da546Spatrick target->GetImages().FindAddressesForLine(target_sp, file, line, sc.function,
1572061da546Spatrick within_function, outside_function);
1573061da546Spatrick
1574061da546Spatrick // If possible, we try and stay within the current function. Within a
1575061da546Spatrick // function, we accept multiple locations (optimized code may do this,
1576061da546Spatrick // there's no solution here so we do the best we can). However if we're
1577061da546Spatrick // trying to leave the function, we don't know how to pick the right
1578061da546Spatrick // location, so if there's more than one then we bail.
1579061da546Spatrick if (!within_function.empty())
1580061da546Spatrick candidates = within_function;
1581061da546Spatrick else if (outside_function.size() == 1 && can_leave_function)
1582061da546Spatrick candidates = outside_function;
1583061da546Spatrick
1584061da546Spatrick // Check if we got anything.
1585061da546Spatrick if (candidates.empty()) {
1586061da546Spatrick if (outside_function.empty()) {
1587061da546Spatrick return Status("Cannot locate an address for %s:%i.",
1588061da546Spatrick file.GetFilename().AsCString(), line);
1589061da546Spatrick } else if (outside_function.size() == 1) {
1590061da546Spatrick return Status("%s:%i is outside the current function.",
1591061da546Spatrick file.GetFilename().AsCString(), line);
1592061da546Spatrick } else {
1593061da546Spatrick StreamString sstr;
1594061da546Spatrick DumpAddressList(sstr, outside_function, target);
1595061da546Spatrick return Status("%s:%i has multiple candidate locations:\n%s",
1596061da546Spatrick file.GetFilename().AsCString(), line, sstr.GetData());
1597061da546Spatrick }
1598061da546Spatrick }
1599061da546Spatrick
1600061da546Spatrick // Accept the first location, warn about any others.
1601061da546Spatrick Address dest = candidates[0];
1602061da546Spatrick if (warnings && candidates.size() > 1) {
1603061da546Spatrick StreamString sstr;
1604061da546Spatrick sstr.Printf("%s:%i appears multiple times in this function, selecting the "
1605061da546Spatrick "first location:\n",
1606061da546Spatrick file.GetFilename().AsCString(), line);
1607061da546Spatrick DumpAddressList(sstr, candidates, target);
1608dda28197Spatrick *warnings = std::string(sstr.GetString());
1609061da546Spatrick }
1610061da546Spatrick
1611061da546Spatrick if (!reg_ctx->SetPC(dest))
1612061da546Spatrick return Status("Cannot change PC to target address.");
1613061da546Spatrick
1614061da546Spatrick return Status();
1615061da546Spatrick }
1616061da546Spatrick
DumpUsingSettingsFormat(Stream & strm,uint32_t frame_idx,bool stop_format)1617061da546Spatrick void Thread::DumpUsingSettingsFormat(Stream &strm, uint32_t frame_idx,
1618061da546Spatrick bool stop_format) {
1619061da546Spatrick ExecutionContext exe_ctx(shared_from_this());
1620061da546Spatrick Process *process = exe_ctx.GetProcessPtr();
1621061da546Spatrick if (process == nullptr)
1622061da546Spatrick return;
1623061da546Spatrick
1624061da546Spatrick StackFrameSP frame_sp;
1625061da546Spatrick SymbolContext frame_sc;
1626061da546Spatrick if (frame_idx != LLDB_INVALID_FRAME_ID) {
1627061da546Spatrick frame_sp = GetStackFrameAtIndex(frame_idx);
1628061da546Spatrick if (frame_sp) {
1629061da546Spatrick exe_ctx.SetFrameSP(frame_sp);
1630061da546Spatrick frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
1631061da546Spatrick }
1632061da546Spatrick }
1633061da546Spatrick
1634061da546Spatrick const FormatEntity::Entry *thread_format;
1635061da546Spatrick if (stop_format)
1636061da546Spatrick thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadStopFormat();
1637061da546Spatrick else
1638061da546Spatrick thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
1639061da546Spatrick
1640061da546Spatrick assert(thread_format);
1641061da546Spatrick
1642061da546Spatrick FormatEntity::Format(*thread_format, strm, frame_sp ? &frame_sc : nullptr,
1643061da546Spatrick &exe_ctx, nullptr, nullptr, false, false);
1644061da546Spatrick }
1645061da546Spatrick
SettingsInitialize()1646061da546Spatrick void Thread::SettingsInitialize() {}
1647061da546Spatrick
SettingsTerminate()1648061da546Spatrick void Thread::SettingsTerminate() {}
1649061da546Spatrick
GetThreadPointer()1650061da546Spatrick lldb::addr_t Thread::GetThreadPointer() { return LLDB_INVALID_ADDRESS; }
1651061da546Spatrick
GetThreadLocalData(const ModuleSP module,lldb::addr_t tls_file_addr)1652061da546Spatrick addr_t Thread::GetThreadLocalData(const ModuleSP module,
1653061da546Spatrick lldb::addr_t tls_file_addr) {
1654061da546Spatrick // The default implementation is to ask the dynamic loader for it. This can
1655061da546Spatrick // be overridden for specific platforms.
1656061da546Spatrick DynamicLoader *loader = GetProcess()->GetDynamicLoader();
1657061da546Spatrick if (loader)
1658061da546Spatrick return loader->GetThreadLocalData(module, shared_from_this(),
1659061da546Spatrick tls_file_addr);
1660061da546Spatrick else
1661061da546Spatrick return LLDB_INVALID_ADDRESS;
1662061da546Spatrick }
1663061da546Spatrick
SafeToCallFunctions()1664061da546Spatrick bool Thread::SafeToCallFunctions() {
1665061da546Spatrick Process *process = GetProcess().get();
1666061da546Spatrick if (process) {
1667*f6aab3d8Srobert DynamicLoader *loader = GetProcess()->GetDynamicLoader();
1668*f6aab3d8Srobert if (loader && loader->IsFullyInitialized() == false)
1669*f6aab3d8Srobert return false;
1670*f6aab3d8Srobert
1671061da546Spatrick SystemRuntime *runtime = process->GetSystemRuntime();
1672061da546Spatrick if (runtime) {
1673061da546Spatrick return runtime->SafeToCallFunctionsOnThisThread(shared_from_this());
1674061da546Spatrick }
1675061da546Spatrick }
1676061da546Spatrick return true;
1677061da546Spatrick }
1678061da546Spatrick
1679061da546Spatrick lldb::StackFrameSP
GetStackFrameSPForStackFramePtr(StackFrame * stack_frame_ptr)1680061da546Spatrick Thread::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) {
1681061da546Spatrick return GetStackFrameList()->GetStackFrameSPForStackFramePtr(stack_frame_ptr);
1682061da546Spatrick }
1683061da546Spatrick
StopReasonAsString(lldb::StopReason reason)1684be691f3bSpatrick std::string Thread::StopReasonAsString(lldb::StopReason reason) {
1685061da546Spatrick switch (reason) {
1686061da546Spatrick case eStopReasonInvalid:
1687061da546Spatrick return "invalid";
1688061da546Spatrick case eStopReasonNone:
1689061da546Spatrick return "none";
1690061da546Spatrick case eStopReasonTrace:
1691061da546Spatrick return "trace";
1692061da546Spatrick case eStopReasonBreakpoint:
1693061da546Spatrick return "breakpoint";
1694061da546Spatrick case eStopReasonWatchpoint:
1695061da546Spatrick return "watchpoint";
1696061da546Spatrick case eStopReasonSignal:
1697061da546Spatrick return "signal";
1698061da546Spatrick case eStopReasonException:
1699061da546Spatrick return "exception";
1700061da546Spatrick case eStopReasonExec:
1701061da546Spatrick return "exec";
1702be691f3bSpatrick case eStopReasonFork:
1703be691f3bSpatrick return "fork";
1704be691f3bSpatrick case eStopReasonVFork:
1705be691f3bSpatrick return "vfork";
1706be691f3bSpatrick case eStopReasonVForkDone:
1707be691f3bSpatrick return "vfork done";
1708061da546Spatrick case eStopReasonPlanComplete:
1709061da546Spatrick return "plan complete";
1710061da546Spatrick case eStopReasonThreadExiting:
1711061da546Spatrick return "thread exiting";
1712061da546Spatrick case eStopReasonInstrumentation:
1713061da546Spatrick return "instrumentation break";
1714be691f3bSpatrick case eStopReasonProcessorTrace:
1715be691f3bSpatrick return "processor trace";
1716061da546Spatrick }
1717061da546Spatrick
1718be691f3bSpatrick return "StopReason = " + std::to_string(reason);
1719061da546Spatrick }
1720061da546Spatrick
RunModeAsString(lldb::RunMode mode)1721be691f3bSpatrick std::string Thread::RunModeAsString(lldb::RunMode mode) {
1722061da546Spatrick switch (mode) {
1723061da546Spatrick case eOnlyThisThread:
1724061da546Spatrick return "only this thread";
1725061da546Spatrick case eAllThreads:
1726061da546Spatrick return "all threads";
1727061da546Spatrick case eOnlyDuringStepping:
1728061da546Spatrick return "only during stepping";
1729061da546Spatrick }
1730061da546Spatrick
1731be691f3bSpatrick return "RunMode = " + std::to_string(mode);
1732061da546Spatrick }
1733061da546Spatrick
GetStatus(Stream & strm,uint32_t start_frame,uint32_t num_frames,uint32_t num_frames_with_source,bool stop_format,bool only_stacks)1734061da546Spatrick size_t Thread::GetStatus(Stream &strm, uint32_t start_frame,
1735061da546Spatrick uint32_t num_frames, uint32_t num_frames_with_source,
1736061da546Spatrick bool stop_format, bool only_stacks) {
1737061da546Spatrick
1738061da546Spatrick if (!only_stacks) {
1739061da546Spatrick ExecutionContext exe_ctx(shared_from_this());
1740061da546Spatrick Target *target = exe_ctx.GetTargetPtr();
1741061da546Spatrick Process *process = exe_ctx.GetProcessPtr();
1742061da546Spatrick strm.Indent();
1743061da546Spatrick bool is_selected = false;
1744061da546Spatrick if (process) {
1745061da546Spatrick if (process->GetThreadList().GetSelectedThread().get() == this)
1746061da546Spatrick is_selected = true;
1747061da546Spatrick }
1748061da546Spatrick strm.Printf("%c ", is_selected ? '*' : ' ');
1749061da546Spatrick if (target && target->GetDebugger().GetUseExternalEditor()) {
1750061da546Spatrick StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
1751061da546Spatrick if (frame_sp) {
1752061da546Spatrick SymbolContext frame_sc(
1753061da546Spatrick frame_sp->GetSymbolContext(eSymbolContextLineEntry));
1754061da546Spatrick if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file) {
1755061da546Spatrick Host::OpenFileInExternalEditor(frame_sc.line_entry.file,
1756061da546Spatrick frame_sc.line_entry.line);
1757061da546Spatrick }
1758061da546Spatrick }
1759061da546Spatrick }
1760061da546Spatrick
1761061da546Spatrick DumpUsingSettingsFormat(strm, start_frame, stop_format);
1762061da546Spatrick }
1763061da546Spatrick
1764061da546Spatrick size_t num_frames_shown = 0;
1765061da546Spatrick if (num_frames > 0) {
1766061da546Spatrick strm.IndentMore();
1767061da546Spatrick
1768061da546Spatrick const bool show_frame_info = true;
1769061da546Spatrick const bool show_frame_unique = only_stacks;
1770061da546Spatrick const char *selected_frame_marker = nullptr;
1771061da546Spatrick if (num_frames == 1 || only_stacks ||
1772061da546Spatrick (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
1773061da546Spatrick strm.IndentMore();
1774061da546Spatrick else
1775061da546Spatrick selected_frame_marker = "* ";
1776061da546Spatrick
1777061da546Spatrick num_frames_shown = GetStackFrameList()->GetStatus(
1778061da546Spatrick strm, start_frame, num_frames, show_frame_info, num_frames_with_source,
1779061da546Spatrick show_frame_unique, selected_frame_marker);
1780061da546Spatrick if (num_frames == 1)
1781061da546Spatrick strm.IndentLess();
1782061da546Spatrick strm.IndentLess();
1783061da546Spatrick }
1784061da546Spatrick return num_frames_shown;
1785061da546Spatrick }
1786061da546Spatrick
GetDescription(Stream & strm,lldb::DescriptionLevel level,bool print_json_thread,bool print_json_stopinfo)1787061da546Spatrick bool Thread::GetDescription(Stream &strm, lldb::DescriptionLevel level,
1788061da546Spatrick bool print_json_thread, bool print_json_stopinfo) {
1789061da546Spatrick const bool stop_format = false;
1790061da546Spatrick DumpUsingSettingsFormat(strm, 0, stop_format);
1791061da546Spatrick strm.Printf("\n");
1792061da546Spatrick
1793061da546Spatrick StructuredData::ObjectSP thread_info = GetExtendedInfo();
1794061da546Spatrick
1795061da546Spatrick if (print_json_thread || print_json_stopinfo) {
1796061da546Spatrick if (thread_info && print_json_thread) {
1797061da546Spatrick thread_info->Dump(strm);
1798061da546Spatrick strm.Printf("\n");
1799061da546Spatrick }
1800061da546Spatrick
1801061da546Spatrick if (print_json_stopinfo && m_stop_info_sp) {
1802061da546Spatrick StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo();
1803061da546Spatrick if (stop_info) {
1804061da546Spatrick stop_info->Dump(strm);
1805061da546Spatrick strm.Printf("\n");
1806061da546Spatrick }
1807061da546Spatrick }
1808061da546Spatrick
1809061da546Spatrick return true;
1810061da546Spatrick }
1811061da546Spatrick
1812061da546Spatrick if (thread_info) {
1813061da546Spatrick StructuredData::ObjectSP activity =
1814061da546Spatrick thread_info->GetObjectForDotSeparatedPath("activity");
1815061da546Spatrick StructuredData::ObjectSP breadcrumb =
1816061da546Spatrick thread_info->GetObjectForDotSeparatedPath("breadcrumb");
1817061da546Spatrick StructuredData::ObjectSP messages =
1818061da546Spatrick thread_info->GetObjectForDotSeparatedPath("trace_messages");
1819061da546Spatrick
1820061da546Spatrick bool printed_activity = false;
1821061da546Spatrick if (activity && activity->GetType() == eStructuredDataTypeDictionary) {
1822061da546Spatrick StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
1823061da546Spatrick StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
1824061da546Spatrick StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
1825061da546Spatrick if (name && name->GetType() == eStructuredDataTypeString && id &&
1826061da546Spatrick id->GetType() == eStructuredDataTypeInteger) {
1827061da546Spatrick strm.Format(" Activity '{0}', {1:x}\n",
1828061da546Spatrick name->GetAsString()->GetValue(),
1829061da546Spatrick id->GetAsInteger()->GetValue());
1830061da546Spatrick }
1831061da546Spatrick printed_activity = true;
1832061da546Spatrick }
1833061da546Spatrick bool printed_breadcrumb = false;
1834061da546Spatrick if (breadcrumb && breadcrumb->GetType() == eStructuredDataTypeDictionary) {
1835061da546Spatrick if (printed_activity)
1836061da546Spatrick strm.Printf("\n");
1837061da546Spatrick StructuredData::Dictionary *breadcrumb_dict =
1838061da546Spatrick breadcrumb->GetAsDictionary();
1839061da546Spatrick StructuredData::ObjectSP breadcrumb_text =
1840061da546Spatrick breadcrumb_dict->GetValueForKey("name");
1841061da546Spatrick if (breadcrumb_text &&
1842061da546Spatrick breadcrumb_text->GetType() == eStructuredDataTypeString) {
1843061da546Spatrick strm.Format(" Current Breadcrumb: {0}\n",
1844061da546Spatrick breadcrumb_text->GetAsString()->GetValue());
1845061da546Spatrick }
1846061da546Spatrick printed_breadcrumb = true;
1847061da546Spatrick }
1848061da546Spatrick if (messages && messages->GetType() == eStructuredDataTypeArray) {
1849061da546Spatrick if (printed_breadcrumb)
1850061da546Spatrick strm.Printf("\n");
1851061da546Spatrick StructuredData::Array *messages_array = messages->GetAsArray();
1852061da546Spatrick const size_t msg_count = messages_array->GetSize();
1853061da546Spatrick if (msg_count > 0) {
1854061da546Spatrick strm.Printf(" %zu trace messages:\n", msg_count);
1855061da546Spatrick for (size_t i = 0; i < msg_count; i++) {
1856061da546Spatrick StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
1857061da546Spatrick if (message && message->GetType() == eStructuredDataTypeDictionary) {
1858061da546Spatrick StructuredData::Dictionary *message_dict =
1859061da546Spatrick message->GetAsDictionary();
1860061da546Spatrick StructuredData::ObjectSP message_text =
1861061da546Spatrick message_dict->GetValueForKey("message");
1862061da546Spatrick if (message_text &&
1863061da546Spatrick message_text->GetType() == eStructuredDataTypeString) {
1864061da546Spatrick strm.Format(" {0}\n", message_text->GetAsString()->GetValue());
1865061da546Spatrick }
1866061da546Spatrick }
1867061da546Spatrick }
1868061da546Spatrick }
1869061da546Spatrick }
1870061da546Spatrick }
1871061da546Spatrick
1872061da546Spatrick return true;
1873061da546Spatrick }
1874061da546Spatrick
GetStackFrameStatus(Stream & strm,uint32_t first_frame,uint32_t num_frames,bool show_frame_info,uint32_t num_frames_with_source)1875061da546Spatrick size_t Thread::GetStackFrameStatus(Stream &strm, uint32_t first_frame,
1876061da546Spatrick uint32_t num_frames, bool show_frame_info,
1877061da546Spatrick uint32_t num_frames_with_source) {
1878061da546Spatrick return GetStackFrameList()->GetStatus(
1879061da546Spatrick strm, first_frame, num_frames, show_frame_info, num_frames_with_source);
1880061da546Spatrick }
1881061da546Spatrick
GetUnwinder()1882dda28197Spatrick Unwind &Thread::GetUnwinder() {
1883dda28197Spatrick if (!m_unwinder_up)
1884dda28197Spatrick m_unwinder_up = std::make_unique<UnwindLLDB>(*this);
1885dda28197Spatrick return *m_unwinder_up;
1886061da546Spatrick }
1887061da546Spatrick
Flush()1888061da546Spatrick void Thread::Flush() {
1889061da546Spatrick ClearStackFrames();
1890061da546Spatrick m_reg_context_sp.reset();
1891061da546Spatrick }
1892061da546Spatrick
IsStillAtLastBreakpointHit()1893061da546Spatrick bool Thread::IsStillAtLastBreakpointHit() {
1894061da546Spatrick // If we are currently stopped at a breakpoint, always return that stopinfo
1895061da546Spatrick // and don't reset it. This allows threads to maintain their breakpoint
1896061da546Spatrick // stopinfo, such as when thread-stepping in multithreaded programs.
1897061da546Spatrick if (m_stop_info_sp) {
1898061da546Spatrick StopReason stop_reason = m_stop_info_sp->GetStopReason();
1899061da546Spatrick if (stop_reason == lldb::eStopReasonBreakpoint) {
1900061da546Spatrick uint64_t value = m_stop_info_sp->GetValue();
1901061da546Spatrick lldb::RegisterContextSP reg_ctx_sp(GetRegisterContext());
1902061da546Spatrick if (reg_ctx_sp) {
1903061da546Spatrick lldb::addr_t pc = reg_ctx_sp->GetPC();
1904061da546Spatrick BreakpointSiteSP bp_site_sp =
1905061da546Spatrick GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1906061da546Spatrick if (bp_site_sp && static_cast<break_id_t>(value) == bp_site_sp->GetID())
1907061da546Spatrick return true;
1908061da546Spatrick }
1909061da546Spatrick }
1910061da546Spatrick }
1911061da546Spatrick return false;
1912061da546Spatrick }
1913061da546Spatrick
StepIn(bool source_step,LazyBool step_in_avoids_code_without_debug_info,LazyBool step_out_avoids_code_without_debug_info)1914061da546Spatrick Status Thread::StepIn(bool source_step,
1915061da546Spatrick LazyBool step_in_avoids_code_without_debug_info,
1916061da546Spatrick LazyBool step_out_avoids_code_without_debug_info)
1917061da546Spatrick
1918061da546Spatrick {
1919061da546Spatrick Status error;
1920061da546Spatrick Process *process = GetProcess().get();
1921061da546Spatrick if (StateIsStoppedState(process->GetState(), true)) {
1922061da546Spatrick StackFrameSP frame_sp = GetStackFrameAtIndex(0);
1923061da546Spatrick ThreadPlanSP new_plan_sp;
1924061da546Spatrick const lldb::RunMode run_mode = eOnlyThisThread;
1925061da546Spatrick const bool abort_other_plans = false;
1926061da546Spatrick
1927061da546Spatrick if (source_step && frame_sp && frame_sp->HasDebugInformation()) {
1928061da546Spatrick SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
1929061da546Spatrick new_plan_sp = QueueThreadPlanForStepInRange(
1930061da546Spatrick abort_other_plans, sc.line_entry, sc, nullptr, run_mode, error,
1931061da546Spatrick step_in_avoids_code_without_debug_info,
1932061da546Spatrick step_out_avoids_code_without_debug_info);
1933061da546Spatrick } else {
1934061da546Spatrick new_plan_sp = QueueThreadPlanForStepSingleInstruction(
1935061da546Spatrick false, abort_other_plans, run_mode, error);
1936061da546Spatrick }
1937061da546Spatrick
1938*f6aab3d8Srobert new_plan_sp->SetIsControllingPlan(true);
1939061da546Spatrick new_plan_sp->SetOkayToDiscard(false);
1940061da546Spatrick
1941061da546Spatrick // Why do we need to set the current thread by ID here???
1942061da546Spatrick process->GetThreadList().SetSelectedThreadByID(GetID());
1943061da546Spatrick error = process->Resume();
1944061da546Spatrick } else {
1945061da546Spatrick error.SetErrorString("process not stopped");
1946061da546Spatrick }
1947061da546Spatrick return error;
1948061da546Spatrick }
1949061da546Spatrick
StepOver(bool source_step,LazyBool step_out_avoids_code_without_debug_info)1950061da546Spatrick Status Thread::StepOver(bool source_step,
1951061da546Spatrick LazyBool step_out_avoids_code_without_debug_info) {
1952061da546Spatrick Status error;
1953061da546Spatrick Process *process = GetProcess().get();
1954061da546Spatrick if (StateIsStoppedState(process->GetState(), true)) {
1955061da546Spatrick StackFrameSP frame_sp = GetStackFrameAtIndex(0);
1956061da546Spatrick ThreadPlanSP new_plan_sp;
1957061da546Spatrick
1958061da546Spatrick const lldb::RunMode run_mode = eOnlyThisThread;
1959061da546Spatrick const bool abort_other_plans = false;
1960061da546Spatrick
1961061da546Spatrick if (source_step && frame_sp && frame_sp->HasDebugInformation()) {
1962061da546Spatrick SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
1963061da546Spatrick new_plan_sp = QueueThreadPlanForStepOverRange(
1964061da546Spatrick abort_other_plans, sc.line_entry, sc, run_mode, error,
1965061da546Spatrick step_out_avoids_code_without_debug_info);
1966061da546Spatrick } else {
1967061da546Spatrick new_plan_sp = QueueThreadPlanForStepSingleInstruction(
1968061da546Spatrick true, abort_other_plans, run_mode, error);
1969061da546Spatrick }
1970061da546Spatrick
1971*f6aab3d8Srobert new_plan_sp->SetIsControllingPlan(true);
1972061da546Spatrick new_plan_sp->SetOkayToDiscard(false);
1973061da546Spatrick
1974061da546Spatrick // Why do we need to set the current thread by ID here???
1975061da546Spatrick process->GetThreadList().SetSelectedThreadByID(GetID());
1976061da546Spatrick error = process->Resume();
1977061da546Spatrick } else {
1978061da546Spatrick error.SetErrorString("process not stopped");
1979061da546Spatrick }
1980061da546Spatrick return error;
1981061da546Spatrick }
1982061da546Spatrick
StepOut(uint32_t frame_idx)1983*f6aab3d8Srobert Status Thread::StepOut(uint32_t frame_idx) {
1984061da546Spatrick Status error;
1985061da546Spatrick Process *process = GetProcess().get();
1986061da546Spatrick if (StateIsStoppedState(process->GetState(), true)) {
1987061da546Spatrick const bool first_instruction = false;
1988061da546Spatrick const bool stop_other_threads = false;
1989061da546Spatrick const bool abort_other_plans = false;
1990061da546Spatrick
1991061da546Spatrick ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut(
1992061da546Spatrick abort_other_plans, nullptr, first_instruction, stop_other_threads,
1993*f6aab3d8Srobert eVoteYes, eVoteNoOpinion, frame_idx, error));
1994061da546Spatrick
1995*f6aab3d8Srobert new_plan_sp->SetIsControllingPlan(true);
1996061da546Spatrick new_plan_sp->SetOkayToDiscard(false);
1997061da546Spatrick
1998061da546Spatrick // Why do we need to set the current thread by ID here???
1999061da546Spatrick process->GetThreadList().SetSelectedThreadByID(GetID());
2000061da546Spatrick error = process->Resume();
2001061da546Spatrick } else {
2002061da546Spatrick error.SetErrorString("process not stopped");
2003061da546Spatrick }
2004061da546Spatrick return error;
2005061da546Spatrick }
2006061da546Spatrick
GetCurrentException()2007061da546Spatrick ValueObjectSP Thread::GetCurrentException() {
2008061da546Spatrick if (auto frame_sp = GetStackFrameAtIndex(0))
2009061da546Spatrick if (auto recognized_frame = frame_sp->GetRecognizedFrame())
2010061da546Spatrick if (auto e = recognized_frame->GetExceptionObject())
2011061da546Spatrick return e;
2012061da546Spatrick
2013061da546Spatrick // NOTE: Even though this behavior is generalized, only ObjC is actually
2014061da546Spatrick // supported at the moment.
2015061da546Spatrick for (LanguageRuntime *runtime : GetProcess()->GetLanguageRuntimes()) {
2016061da546Spatrick if (auto e = runtime->GetExceptionObjectForThread(shared_from_this()))
2017061da546Spatrick return e;
2018061da546Spatrick }
2019061da546Spatrick
2020061da546Spatrick return ValueObjectSP();
2021061da546Spatrick }
2022061da546Spatrick
GetCurrentExceptionBacktrace()2023061da546Spatrick ThreadSP Thread::GetCurrentExceptionBacktrace() {
2024061da546Spatrick ValueObjectSP exception = GetCurrentException();
2025061da546Spatrick if (!exception)
2026061da546Spatrick return ThreadSP();
2027061da546Spatrick
2028061da546Spatrick // NOTE: Even though this behavior is generalized, only ObjC is actually
2029061da546Spatrick // supported at the moment.
2030061da546Spatrick for (LanguageRuntime *runtime : GetProcess()->GetLanguageRuntimes()) {
2031061da546Spatrick if (auto bt = runtime->GetBacktraceThreadFromException(exception))
2032061da546Spatrick return bt;
2033061da546Spatrick }
2034061da546Spatrick
2035061da546Spatrick return ThreadSP();
2036061da546Spatrick }
2037*f6aab3d8Srobert
GetSiginfoValue()2038*f6aab3d8Srobert lldb::ValueObjectSP Thread::GetSiginfoValue() {
2039*f6aab3d8Srobert ProcessSP process_sp = GetProcess();
2040*f6aab3d8Srobert assert(process_sp);
2041*f6aab3d8Srobert Target &target = process_sp->GetTarget();
2042*f6aab3d8Srobert PlatformSP platform_sp = target.GetPlatform();
2043*f6aab3d8Srobert assert(platform_sp);
2044*f6aab3d8Srobert ArchSpec arch = target.GetArchitecture();
2045*f6aab3d8Srobert
2046*f6aab3d8Srobert CompilerType type = platform_sp->GetSiginfoType(arch.GetTriple());
2047*f6aab3d8Srobert if (!type.IsValid())
2048*f6aab3d8Srobert return ValueObjectConstResult::Create(&target, Status("no siginfo_t for the platform"));
2049*f6aab3d8Srobert
2050*f6aab3d8Srobert std::optional<uint64_t> type_size = type.GetByteSize(nullptr);
2051*f6aab3d8Srobert assert(type_size);
2052*f6aab3d8Srobert llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> data =
2053*f6aab3d8Srobert GetSiginfo(*type_size);
2054*f6aab3d8Srobert if (!data)
2055*f6aab3d8Srobert return ValueObjectConstResult::Create(&target, Status(data.takeError()));
2056*f6aab3d8Srobert
2057*f6aab3d8Srobert DataExtractor data_extractor{data.get()->getBufferStart(), data.get()->getBufferSize(),
2058*f6aab3d8Srobert process_sp->GetByteOrder(), arch.GetAddressByteSize()};
2059*f6aab3d8Srobert return ValueObjectConstResult::Create(&target, type, ConstString("__lldb_siginfo"), data_extractor);
2060*f6aab3d8Srobert }
2061