1ac7ddfbfSEd Maste //===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10435933ddSDimitry Andric #include "lldb/Target/Thread.h"
11435933ddSDimitry Andric #include "Plugins/Process/Utility/UnwindLLDB.h"
12435933ddSDimitry Andric #include "Plugins/Process/Utility/UnwindMacOSXFrameBackchain.h"
13ac7ddfbfSEd Maste #include "lldb/Breakpoint/BreakpointLocation.h"
14ac7ddfbfSEd Maste #include "lldb/Core/Debugger.h"
151c3bbb01SEd Maste #include "lldb/Core/FormatEntity.h"
169f2f44ceSEd Maste #include "lldb/Core/Module.h"
179f2f44ceSEd Maste #include "lldb/Core/ValueObject.h"
18ac7ddfbfSEd Maste #include "lldb/Host/Host.h"
1912b93ac6SEd Maste #include "lldb/Interpreter/OptionValueFileSpecList.h"
201c3bbb01SEd Maste #include "lldb/Interpreter/OptionValueProperties.h"
211c3bbb01SEd Maste #include "lldb/Interpreter/Property.h"
22ac7ddfbfSEd Maste #include "lldb/Symbol/Function.h"
231c3bbb01SEd Maste #include "lldb/Target/ABI.h"
24ac7ddfbfSEd Maste #include "lldb/Target/DynamicLoader.h"
25ac7ddfbfSEd Maste #include "lldb/Target/ExecutionContext.h"
26*b5893f02SDimitry Andric #include "lldb/Target/ObjCLanguageRuntime.h"
27ac7ddfbfSEd Maste #include "lldb/Target/Process.h"
28ac7ddfbfSEd Maste #include "lldb/Target/RegisterContext.h"
29*b5893f02SDimitry Andric #include "lldb/Target/StackFrameRecognizer.h"
30ac7ddfbfSEd Maste #include "lldb/Target/StopInfo.h"
310127ef0fSEd Maste #include "lldb/Target/SystemRuntime.h"
32ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
33ac7ddfbfSEd Maste #include "lldb/Target/ThreadPlan.h"
34ac7ddfbfSEd Maste #include "lldb/Target/ThreadPlanBase.h"
35435933ddSDimitry Andric #include "lldb/Target/ThreadPlanCallFunction.h"
367aa51b79SEd Maste #include "lldb/Target/ThreadPlanPython.h"
37435933ddSDimitry Andric #include "lldb/Target/ThreadPlanRunToAddress.h"
38435933ddSDimitry Andric #include "lldb/Target/ThreadPlanStepInRange.h"
39ac7ddfbfSEd Maste #include "lldb/Target/ThreadPlanStepInstruction.h"
40ac7ddfbfSEd Maste #include "lldb/Target/ThreadPlanStepOut.h"
41ac7ddfbfSEd Maste #include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
42ac7ddfbfSEd Maste #include "lldb/Target/ThreadPlanStepOverRange.h"
43435933ddSDimitry Andric #include "lldb/Target/ThreadPlanStepThrough.h"
44ac7ddfbfSEd Maste #include "lldb/Target/ThreadPlanStepUntil.h"
45ac7ddfbfSEd Maste #include "lldb/Target/ThreadSpec.h"
46ac7ddfbfSEd Maste #include "lldb/Target/Unwind.h"
47f678e45dSDimitry Andric #include "lldb/Utility/Log.h"
48f678e45dSDimitry Andric #include "lldb/Utility/RegularExpression.h"
49*b5893f02SDimitry Andric #include "lldb/Utility/State.h"
50f678e45dSDimitry Andric #include "lldb/Utility/Stream.h"
51f678e45dSDimitry Andric #include "lldb/Utility/StreamString.h"
52302affcbSDimitry Andric #include "lldb/lldb-enumerations.h"
53ac7ddfbfSEd Maste 
54ac7ddfbfSEd Maste using namespace lldb;
55ac7ddfbfSEd Maste using namespace lldb_private;
56ac7ddfbfSEd Maste 
GetGlobalProperties()57435933ddSDimitry Andric const ThreadPropertiesSP &Thread::GetGlobalProperties() {
584bb0738eSEd Maste   // NOTE: intentional leak so we don't crash if global destructor chain gets
594bb0738eSEd Maste   // called as other threads still use the result of this function
60435933ddSDimitry Andric   static ThreadPropertiesSP *g_settings_sp_ptr =
61435933ddSDimitry Andric       new ThreadPropertiesSP(new ThreadProperties(true));
624bb0738eSEd Maste   return *g_settings_sp_ptr;
63ac7ddfbfSEd Maste }
64ac7ddfbfSEd Maste 
65*b5893f02SDimitry Andric static constexpr PropertyDefinition g_properties[] = {
66435933ddSDimitry Andric     {"step-in-avoid-nodebug", OptionValue::eTypeBoolean, true, true, nullptr,
67*b5893f02SDimitry Andric      {},
68435933ddSDimitry Andric      "If true, step-in will not stop in functions with no debug information."},
69435933ddSDimitry Andric     {"step-out-avoid-nodebug", OptionValue::eTypeBoolean, true, false, nullptr,
70*b5893f02SDimitry Andric      {}, "If true, when step-in/step-out/step-over leave the current frame, "
71*b5893f02SDimitry Andric          "they will continue to step out till they come to a function with "
72435933ddSDimitry Andric          "debug information. Passing a frame argument to step-out will "
73435933ddSDimitry Andric          "override this option."},
74*b5893f02SDimitry Andric     {"step-avoid-regexp", OptionValue::eTypeRegex, true, 0, "^std::", {},
75435933ddSDimitry Andric      "A regular expression defining functions step-in won't stop in."},
76435933ddSDimitry Andric     {"step-avoid-libraries", OptionValue::eTypeFileSpecList, true, 0, nullptr,
77*b5893f02SDimitry Andric      {}, "A list of libraries that source stepping won't stop in."},
78*b5893f02SDimitry Andric     {"trace-thread", OptionValue::eTypeBoolean, false, false, nullptr, {},
79435933ddSDimitry Andric      "If true, this thread will single-step and log execution."},
80*b5893f02SDimitry Andric     {"max-backtrace-depth", OptionValue::eTypeUInt64, false, 300000, nullptr,
81*b5893f02SDimitry Andric      {}, "Maximum number of frames to backtrace."}};
82ac7ddfbfSEd Maste 
83ac7ddfbfSEd Maste enum {
840127ef0fSEd Maste   ePropertyStepInAvoidsNoDebug,
850127ef0fSEd Maste   ePropertyStepOutAvoidsNoDebug,
86ac7ddfbfSEd Maste   ePropertyStepAvoidRegex,
8712b93ac6SEd Maste   ePropertyStepAvoidLibraries,
88*b5893f02SDimitry Andric   ePropertyEnableThreadTrace,
89*b5893f02SDimitry Andric   ePropertyMaxBacktraceDepth
90ac7ddfbfSEd Maste };
91ac7ddfbfSEd Maste 
92435933ddSDimitry Andric class ThreadOptionValueProperties : public OptionValueProperties {
93ac7ddfbfSEd Maste public:
ThreadOptionValueProperties(const ConstString & name)94435933ddSDimitry Andric   ThreadOptionValueProperties(const ConstString &name)
95435933ddSDimitry Andric       : OptionValueProperties(name) {}
96ac7ddfbfSEd Maste 
97ac7ddfbfSEd Maste   // This constructor is used when creating ThreadOptionValueProperties when it
98ac7ddfbfSEd Maste   // is part of a new lldb_private::Thread instance. It will copy all current
99ac7ddfbfSEd Maste   // global property values as needed
ThreadOptionValueProperties(ThreadProperties * global_properties)100435933ddSDimitry Andric   ThreadOptionValueProperties(ThreadProperties *global_properties)
101435933ddSDimitry Andric       : OptionValueProperties(*global_properties->GetValueProperties()) {}
102ac7ddfbfSEd Maste 
GetPropertyAtIndex(const ExecutionContext * exe_ctx,bool will_modify,uint32_t idx) const103435933ddSDimitry Andric   const Property *GetPropertyAtIndex(const ExecutionContext *exe_ctx,
104435933ddSDimitry Andric                                      bool will_modify,
105435933ddSDimitry Andric                                      uint32_t idx) const override {
1060127ef0fSEd Maste     // When getting the value for a key from the thread options, we will always
1074ba319b5SDimitry Andric     // try and grab the setting from the current thread if there is one. Else
1084ba319b5SDimitry Andric     // we just use the one from this instance.
109435933ddSDimitry Andric     if (exe_ctx) {
110ac7ddfbfSEd Maste       Thread *thread = exe_ctx->GetThreadPtr();
111435933ddSDimitry Andric       if (thread) {
112435933ddSDimitry Andric         ThreadOptionValueProperties *instance_properties =
113435933ddSDimitry Andric             static_cast<ThreadOptionValueProperties *>(
114435933ddSDimitry Andric                 thread->GetValueProperties().get());
115ac7ddfbfSEd Maste         if (this != instance_properties)
116ac7ddfbfSEd Maste           return instance_properties->ProtectedGetPropertyAtIndex(idx);
117ac7ddfbfSEd Maste       }
118ac7ddfbfSEd Maste     }
119ac7ddfbfSEd Maste     return ProtectedGetPropertyAtIndex(idx);
120ac7ddfbfSEd Maste   }
121ac7ddfbfSEd Maste };
122ac7ddfbfSEd Maste 
ThreadProperties(bool is_global)123435933ddSDimitry Andric ThreadProperties::ThreadProperties(bool is_global) : Properties() {
124435933ddSDimitry Andric   if (is_global) {
125435933ddSDimitry Andric     m_collection_sp.reset(
126435933ddSDimitry Andric         new ThreadOptionValueProperties(ConstString("thread")));
127ac7ddfbfSEd Maste     m_collection_sp->Initialize(g_properties);
128435933ddSDimitry Andric   } else
129435933ddSDimitry Andric     m_collection_sp.reset(
130435933ddSDimitry Andric         new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
131ac7ddfbfSEd Maste }
132ac7ddfbfSEd Maste 
1339f2f44ceSEd Maste ThreadProperties::~ThreadProperties() = default;
134ac7ddfbfSEd Maste 
GetSymbolsToAvoidRegexp()135435933ddSDimitry Andric const RegularExpression *ThreadProperties::GetSymbolsToAvoidRegexp() {
136ac7ddfbfSEd Maste   const uint32_t idx = ePropertyStepAvoidRegex;
1379f2f44ceSEd Maste   return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex(nullptr, idx);
138ac7ddfbfSEd Maste }
139ac7ddfbfSEd Maste 
GetLibrariesToAvoid() const140435933ddSDimitry Andric FileSpecList &ThreadProperties::GetLibrariesToAvoid() const {
14112b93ac6SEd Maste   const uint32_t idx = ePropertyStepAvoidLibraries;
142435933ddSDimitry Andric   OptionValueFileSpecList *option_value =
143435933ddSDimitry Andric       m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
144435933ddSDimitry Andric                                                                    false, idx);
14512b93ac6SEd Maste   assert(option_value);
14612b93ac6SEd Maste   return option_value->GetCurrentValue();
14712b93ac6SEd Maste }
14812b93ac6SEd Maste 
GetTraceEnabledState() const149435933ddSDimitry Andric bool ThreadProperties::GetTraceEnabledState() const {
150ac7ddfbfSEd Maste   const uint32_t idx = ePropertyEnableThreadTrace;
151435933ddSDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
152435933ddSDimitry Andric       nullptr, idx, g_properties[idx].default_uint_value != 0);
153ac7ddfbfSEd Maste }
154ac7ddfbfSEd Maste 
GetStepInAvoidsNoDebug() const155435933ddSDimitry Andric bool ThreadProperties::GetStepInAvoidsNoDebug() const {
1560127ef0fSEd Maste   const uint32_t idx = ePropertyStepInAvoidsNoDebug;
157435933ddSDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
158435933ddSDimitry Andric       nullptr, idx, g_properties[idx].default_uint_value != 0);
1590127ef0fSEd Maste }
1600127ef0fSEd Maste 
GetStepOutAvoidsNoDebug() const161435933ddSDimitry Andric bool ThreadProperties::GetStepOutAvoidsNoDebug() const {
1620127ef0fSEd Maste   const uint32_t idx = ePropertyStepOutAvoidsNoDebug;
163435933ddSDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsBoolean(
164435933ddSDimitry Andric       nullptr, idx, g_properties[idx].default_uint_value != 0);
1650127ef0fSEd Maste }
1660127ef0fSEd Maste 
GetMaxBacktraceDepth() const167*b5893f02SDimitry Andric uint64_t ThreadProperties::GetMaxBacktraceDepth() const {
168*b5893f02SDimitry Andric   const uint32_t idx = ePropertyMaxBacktraceDepth;
169*b5893f02SDimitry Andric   return m_collection_sp->GetPropertyAtIndexAsUInt64(
170*b5893f02SDimitry Andric       nullptr, idx, g_properties[idx].default_uint_value != 0);
171*b5893f02SDimitry Andric }
172*b5893f02SDimitry Andric 
173ac7ddfbfSEd Maste //------------------------------------------------------------------
174ac7ddfbfSEd Maste // Thread Event Data
175ac7ddfbfSEd Maste //------------------------------------------------------------------
176ac7ddfbfSEd Maste 
GetFlavorString()177435933ddSDimitry Andric const ConstString &Thread::ThreadEventData::GetFlavorString() {
178ac7ddfbfSEd Maste   static ConstString g_flavor("Thread::ThreadEventData");
179ac7ddfbfSEd Maste   return g_flavor;
180ac7ddfbfSEd Maste }
181ac7ddfbfSEd Maste 
ThreadEventData(const lldb::ThreadSP thread_sp)182435933ddSDimitry Andric Thread::ThreadEventData::ThreadEventData(const lldb::ThreadSP thread_sp)
183435933ddSDimitry Andric     : m_thread_sp(thread_sp), m_stack_id() {}
184ac7ddfbfSEd Maste 
ThreadEventData(const lldb::ThreadSP thread_sp,const StackID & stack_id)185435933ddSDimitry Andric Thread::ThreadEventData::ThreadEventData(const lldb::ThreadSP thread_sp,
186435933ddSDimitry Andric                                          const StackID &stack_id)
187435933ddSDimitry Andric     : m_thread_sp(thread_sp), m_stack_id(stack_id) {}
188ac7ddfbfSEd Maste 
ThreadEventData()189435933ddSDimitry Andric Thread::ThreadEventData::ThreadEventData() : m_thread_sp(), m_stack_id() {}
190ac7ddfbfSEd Maste 
1919f2f44ceSEd Maste Thread::ThreadEventData::~ThreadEventData() = default;
192ac7ddfbfSEd Maste 
Dump(Stream * s) const193435933ddSDimitry Andric void Thread::ThreadEventData::Dump(Stream *s) const {}
194ac7ddfbfSEd Maste 
195ac7ddfbfSEd Maste const Thread::ThreadEventData *
GetEventDataFromEvent(const Event * event_ptr)196435933ddSDimitry Andric Thread::ThreadEventData::GetEventDataFromEvent(const Event *event_ptr) {
197435933ddSDimitry Andric   if (event_ptr) {
198ac7ddfbfSEd Maste     const EventData *event_data = event_ptr->GetData();
199435933ddSDimitry Andric     if (event_data &&
200435933ddSDimitry Andric         event_data->GetFlavor() == ThreadEventData::GetFlavorString())
201ac7ddfbfSEd Maste       return static_cast<const ThreadEventData *>(event_ptr->GetData());
202ac7ddfbfSEd Maste   }
2039f2f44ceSEd Maste   return nullptr;
204ac7ddfbfSEd Maste }
205ac7ddfbfSEd Maste 
GetThreadFromEvent(const Event * event_ptr)206435933ddSDimitry Andric ThreadSP Thread::ThreadEventData::GetThreadFromEvent(const Event *event_ptr) {
207ac7ddfbfSEd Maste   ThreadSP thread_sp;
208ac7ddfbfSEd Maste   const ThreadEventData *event_data = GetEventDataFromEvent(event_ptr);
209ac7ddfbfSEd Maste   if (event_data)
210ac7ddfbfSEd Maste     thread_sp = event_data->GetThread();
211ac7ddfbfSEd Maste   return thread_sp;
212ac7ddfbfSEd Maste }
213ac7ddfbfSEd Maste 
GetStackIDFromEvent(const Event * event_ptr)214435933ddSDimitry Andric StackID Thread::ThreadEventData::GetStackIDFromEvent(const Event *event_ptr) {
215ac7ddfbfSEd Maste   StackID stack_id;
216ac7ddfbfSEd Maste   const ThreadEventData *event_data = GetEventDataFromEvent(event_ptr);
217ac7ddfbfSEd Maste   if (event_data)
218ac7ddfbfSEd Maste     stack_id = event_data->GetStackID();
219ac7ddfbfSEd Maste   return stack_id;
220ac7ddfbfSEd Maste }
221ac7ddfbfSEd Maste 
222ac7ddfbfSEd Maste StackFrameSP
GetStackFrameFromEvent(const Event * event_ptr)223435933ddSDimitry Andric Thread::ThreadEventData::GetStackFrameFromEvent(const Event *event_ptr) {
224ac7ddfbfSEd Maste   const ThreadEventData *event_data = GetEventDataFromEvent(event_ptr);
225ac7ddfbfSEd Maste   StackFrameSP frame_sp;
226435933ddSDimitry Andric   if (event_data) {
227ac7ddfbfSEd Maste     ThreadSP thread_sp = event_data->GetThread();
228435933ddSDimitry Andric     if (thread_sp) {
229435933ddSDimitry Andric       frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID(
230435933ddSDimitry Andric           event_data->GetStackID());
231ac7ddfbfSEd Maste     }
232ac7ddfbfSEd Maste   }
233ac7ddfbfSEd Maste   return frame_sp;
234ac7ddfbfSEd Maste }
235ac7ddfbfSEd Maste 
236ac7ddfbfSEd Maste //------------------------------------------------------------------
237ac7ddfbfSEd Maste // Thread class
238ac7ddfbfSEd Maste //------------------------------------------------------------------
239ac7ddfbfSEd Maste 
GetStaticBroadcasterClass()240435933ddSDimitry Andric ConstString &Thread::GetStaticBroadcasterClass() {
241ac7ddfbfSEd Maste   static ConstString class_name("lldb.thread");
242ac7ddfbfSEd Maste   return class_name;
243ac7ddfbfSEd Maste }
244ac7ddfbfSEd Maste 
Thread(Process & process,lldb::tid_t tid,bool use_invalid_index_id)2454bb0738eSEd Maste Thread::Thread(Process &process, lldb::tid_t tid, bool use_invalid_index_id)
246435933ddSDimitry Andric     : ThreadProperties(false), UserID(tid),
2474bb0738eSEd Maste       Broadcaster(process.GetTarget().GetDebugger().GetBroadcasterManager(),
2484bb0738eSEd Maste                   Thread::GetStaticBroadcasterClass().AsCString()),
249435933ddSDimitry Andric       m_process_wp(process.shared_from_this()), m_stop_info_sp(),
250435933ddSDimitry Andric       m_stop_info_stop_id(0), m_stop_info_override_stop_id(0),
251435933ddSDimitry Andric       m_index_id(use_invalid_index_id ? LLDB_INVALID_INDEX32
252435933ddSDimitry Andric                                       : process.GetNextThreadIndexID(tid)),
253435933ddSDimitry Andric       m_reg_context_sp(), m_state(eStateUnloaded), m_state_mutex(),
254435933ddSDimitry Andric       m_plan_stack(), m_completed_plan_stack(), m_frame_mutex(),
255435933ddSDimitry Andric       m_curr_frames_sp(), m_prev_frames_sp(),
256ac7ddfbfSEd Maste       m_resume_signal(LLDB_INVALID_SIGNAL_NUMBER),
257435933ddSDimitry Andric       m_resume_state(eStateRunning), m_temporary_resume_state(eStateRunning),
258435933ddSDimitry Andric       m_unwinder_ap(), m_destroy_called(false),
2590127ef0fSEd Maste       m_override_should_notify(eLazyBoolCalculate),
260435933ddSDimitry Andric       m_extended_info_fetched(false), m_extended_info() {
261ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
262ac7ddfbfSEd Maste   if (log)
263435933ddSDimitry Andric     log->Printf("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")",
264435933ddSDimitry Andric                 static_cast<void *>(this), GetID());
265ac7ddfbfSEd Maste 
266ac7ddfbfSEd Maste   CheckInWithManager();
267*b5893f02SDimitry Andric 
268ac7ddfbfSEd Maste   QueueFundamentalPlan(true);
269ac7ddfbfSEd Maste }
270ac7ddfbfSEd Maste 
~Thread()271435933ddSDimitry Andric Thread::~Thread() {
272ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
273ac7ddfbfSEd Maste   if (log)
2740127ef0fSEd Maste     log->Printf("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")",
2750127ef0fSEd Maste                 static_cast<void *>(this), GetID());
276435933ddSDimitry Andric   /// If you hit this assert, it means your derived class forgot to call
277435933ddSDimitry Andric   /// DoDestroy in its destructor.
278ac7ddfbfSEd Maste   assert(m_destroy_called);
279ac7ddfbfSEd Maste }
280ac7ddfbfSEd Maste 
DestroyThread()281435933ddSDimitry Andric void Thread::DestroyThread() {
28235617911SEd Maste   // Tell any plans on the plan stacks that the thread is being destroyed since
2834ba319b5SDimitry Andric   // any plans that have a thread go away in the middle of might need to do
2844ba319b5SDimitry Andric   // cleanup, or in some cases NOT do cleanup...
285ac7ddfbfSEd Maste   for (auto plan : m_plan_stack)
286ac7ddfbfSEd Maste     plan->ThreadDestroyed();
287ac7ddfbfSEd Maste 
28835617911SEd Maste   for (auto plan : m_discarded_plan_stack)
28935617911SEd Maste     plan->ThreadDestroyed();
29035617911SEd Maste 
29135617911SEd Maste   for (auto plan : m_completed_plan_stack)
29235617911SEd Maste     plan->ThreadDestroyed();
29335617911SEd Maste 
294ac7ddfbfSEd Maste   m_destroy_called = true;
295ac7ddfbfSEd Maste   m_plan_stack.clear();
296ac7ddfbfSEd Maste   m_discarded_plan_stack.clear();
297ac7ddfbfSEd Maste   m_completed_plan_stack.clear();
298ac7ddfbfSEd Maste 
2994ba319b5SDimitry Andric   // Push a ThreadPlanNull on the plan stack.  That way we can continue
3004ba319b5SDimitry Andric   // assuming that the plan stack is never empty, but if somebody errantly asks
3014ba319b5SDimitry Andric   // questions of a destroyed thread without checking first whether it is
3024ba319b5SDimitry Andric   // destroyed, they won't crash.
303ac7ddfbfSEd Maste   ThreadPlanSP null_plan_sp(new ThreadPlanNull(*this));
304ac7ddfbfSEd Maste   m_plan_stack.push_back(null_plan_sp);
305ac7ddfbfSEd Maste 
306ac7ddfbfSEd Maste   m_stop_info_sp.reset();
307ac7ddfbfSEd Maste   m_reg_context_sp.reset();
308ac7ddfbfSEd Maste   m_unwinder_ap.reset();
3094bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
310ac7ddfbfSEd Maste   m_curr_frames_sp.reset();
311ac7ddfbfSEd Maste   m_prev_frames_sp.reset();
312ac7ddfbfSEd Maste }
313ac7ddfbfSEd Maste 
BroadcastSelectedFrameChange(StackID & new_frame_id)314435933ddSDimitry Andric void Thread::BroadcastSelectedFrameChange(StackID &new_frame_id) {
315ac7ddfbfSEd Maste   if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
316435933ddSDimitry Andric     BroadcastEvent(eBroadcastBitSelectedFrameChanged,
317435933ddSDimitry Andric                    new ThreadEventData(this->shared_from_this(), new_frame_id));
318ac7ddfbfSEd Maste }
319ac7ddfbfSEd Maste 
GetSelectedFrame()320435933ddSDimitry Andric lldb::StackFrameSP Thread::GetSelectedFrame() {
3219f2f44ceSEd Maste   StackFrameListSP stack_frame_list_sp(GetStackFrameList());
322435933ddSDimitry Andric   StackFrameSP frame_sp = stack_frame_list_sp->GetFrameAtIndex(
323435933ddSDimitry Andric       stack_frame_list_sp->GetSelectedFrameIndex());
3249f2f44ceSEd Maste   FunctionOptimizationWarning(frame_sp.get());
3259f2f44ceSEd Maste   return frame_sp;
3269f2f44ceSEd Maste }
3279f2f44ceSEd Maste 
SetSelectedFrame(lldb_private::StackFrame * frame,bool broadcast)328435933ddSDimitry Andric uint32_t Thread::SetSelectedFrame(lldb_private::StackFrame *frame,
329435933ddSDimitry Andric                                   bool broadcast) {
330ac7ddfbfSEd Maste   uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
331ac7ddfbfSEd Maste   if (broadcast)
332ac7ddfbfSEd Maste     BroadcastSelectedFrameChange(frame->GetStackID());
3339f2f44ceSEd Maste   FunctionOptimizationWarning(frame);
334ac7ddfbfSEd Maste   return ret_value;
335ac7ddfbfSEd Maste }
336ac7ddfbfSEd Maste 
SetSelectedFrameByIndex(uint32_t frame_idx,bool broadcast)337435933ddSDimitry Andric bool Thread::SetSelectedFrameByIndex(uint32_t frame_idx, bool broadcast) {
338ac7ddfbfSEd Maste   StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex(frame_idx));
339435933ddSDimitry Andric   if (frame_sp) {
340ac7ddfbfSEd Maste     GetStackFrameList()->SetSelectedFrame(frame_sp.get());
341ac7ddfbfSEd Maste     if (broadcast)
342ac7ddfbfSEd Maste       BroadcastSelectedFrameChange(frame_sp->GetStackID());
3439f2f44ceSEd Maste     FunctionOptimizationWarning(frame_sp.get());
344ac7ddfbfSEd Maste     return true;
345435933ddSDimitry Andric   } else
346ac7ddfbfSEd Maste     return false;
347ac7ddfbfSEd Maste }
348ac7ddfbfSEd Maste 
SetSelectedFrameByIndexNoisily(uint32_t frame_idx,Stream & output_stream)349435933ddSDimitry Andric bool Thread::SetSelectedFrameByIndexNoisily(uint32_t frame_idx,
350435933ddSDimitry Andric                                             Stream &output_stream) {
351ac7ddfbfSEd Maste   const bool broadcast = true;
352ac7ddfbfSEd Maste   bool success = SetSelectedFrameByIndex(frame_idx, broadcast);
353435933ddSDimitry Andric   if (success) {
354ac7ddfbfSEd Maste     StackFrameSP frame_sp = GetSelectedFrame();
355435933ddSDimitry Andric     if (frame_sp) {
356ac7ddfbfSEd Maste       bool already_shown = false;
357435933ddSDimitry Andric       SymbolContext frame_sc(
358435933ddSDimitry Andric           frame_sp->GetSymbolContext(eSymbolContextLineEntry));
359435933ddSDimitry Andric       if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() &&
360435933ddSDimitry Andric           frame_sc.line_entry.file && frame_sc.line_entry.line != 0) {
361435933ddSDimitry Andric         already_shown = Host::OpenFileInExternalEditor(
362435933ddSDimitry Andric             frame_sc.line_entry.file, frame_sc.line_entry.line);
363ac7ddfbfSEd Maste       }
364ac7ddfbfSEd Maste 
365ac7ddfbfSEd Maste       bool show_frame_info = true;
366ac7ddfbfSEd Maste       bool show_source = !already_shown;
3679f2f44ceSEd Maste       FunctionOptimizationWarning(frame_sp.get());
368ac7ddfbfSEd Maste       return frame_sp->GetStatus(output_stream, show_frame_info, show_source);
369ac7ddfbfSEd Maste     }
370ac7ddfbfSEd Maste     return false;
371435933ddSDimitry Andric   } else
372ac7ddfbfSEd Maste     return false;
373ac7ddfbfSEd Maste }
374ac7ddfbfSEd Maste 
FunctionOptimizationWarning(StackFrame * frame)375435933ddSDimitry Andric void Thread::FunctionOptimizationWarning(StackFrame *frame) {
376435933ddSDimitry Andric   if (frame && frame->HasDebugInformation() &&
377435933ddSDimitry Andric       GetProcess()->GetWarningsOptimization()) {
378435933ddSDimitry Andric     SymbolContext sc =
379435933ddSDimitry Andric         frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextModule);
3809f2f44ceSEd Maste     GetProcess()->PrintWarningOptimization(sc);
3819f2f44ceSEd Maste   }
3829f2f44ceSEd Maste }
383ac7ddfbfSEd Maste 
GetStopInfo()384435933ddSDimitry Andric lldb::StopInfoSP Thread::GetStopInfo() {
385ac7ddfbfSEd Maste   if (m_destroy_called)
386ac7ddfbfSEd Maste     return m_stop_info_sp;
387ac7ddfbfSEd Maste 
388f678e45dSDimitry Andric   ThreadPlanSP completed_plan_sp(GetCompletedPlan());
389ac7ddfbfSEd Maste   ProcessSP process_sp(GetProcess());
390ac7ddfbfSEd Maste   const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
391f678e45dSDimitry Andric 
3924ba319b5SDimitry Andric   // Here we select the stop info according to priorirty: - m_stop_info_sp (if
3934ba319b5SDimitry Andric   // not trace) - preset value - completed plan stop info - new value with plan
3944ba319b5SDimitry Andric   // from completed plan stack - m_stop_info_sp (trace stop reason is OK now) -
3954ba319b5SDimitry Andric   // ask GetPrivateStopInfo to set stop info
396f678e45dSDimitry Andric 
397f678e45dSDimitry Andric   bool have_valid_stop_info = m_stop_info_sp &&
398f678e45dSDimitry Andric       m_stop_info_sp ->IsValid() &&
399f678e45dSDimitry Andric       m_stop_info_stop_id == stop_id;
400f678e45dSDimitry Andric   bool have_valid_completed_plan = completed_plan_sp && completed_plan_sp->PlanSucceeded();
401*b5893f02SDimitry Andric   bool plan_failed = completed_plan_sp && !completed_plan_sp->PlanSucceeded();
402f678e45dSDimitry Andric   bool plan_overrides_trace =
403f678e45dSDimitry Andric     have_valid_stop_info && have_valid_completed_plan
404f678e45dSDimitry Andric     && (m_stop_info_sp->GetStopReason() == eStopReasonTrace);
405f678e45dSDimitry Andric 
406*b5893f02SDimitry Andric   if (have_valid_stop_info && !plan_overrides_trace && !plan_failed) {
407ac7ddfbfSEd Maste     return m_stop_info_sp;
408*b5893f02SDimitry Andric   } else if (completed_plan_sp) {
409f678e45dSDimitry Andric     return StopInfo::CreateStopReasonWithPlan(
410f678e45dSDimitry Andric         completed_plan_sp, GetReturnValueObject(), GetExpressionVariable());
411435933ddSDimitry Andric   } else {
412ac7ddfbfSEd Maste     GetPrivateStopInfo();
413ac7ddfbfSEd Maste     return m_stop_info_sp;
414ac7ddfbfSEd Maste   }
415ac7ddfbfSEd Maste }
416ac7ddfbfSEd Maste 
GetPrivateStopInfo()417435933ddSDimitry Andric lldb::StopInfoSP Thread::GetPrivateStopInfo() {
418ac7ddfbfSEd Maste   if (m_destroy_called)
419ac7ddfbfSEd Maste     return m_stop_info_sp;
420ac7ddfbfSEd Maste 
421ac7ddfbfSEd Maste   ProcessSP process_sp(GetProcess());
422435933ddSDimitry Andric   if (process_sp) {
423ac7ddfbfSEd Maste     const uint32_t process_stop_id = process_sp->GetStopID();
424435933ddSDimitry Andric     if (m_stop_info_stop_id != process_stop_id) {
425435933ddSDimitry Andric       if (m_stop_info_sp) {
426435933ddSDimitry Andric         if (m_stop_info_sp->IsValid() || IsStillAtLastBreakpointHit() ||
427435933ddSDimitry Andric             GetCurrentPlan()->IsVirtualStep())
428ac7ddfbfSEd Maste           SetStopInfo(m_stop_info_sp);
429ac7ddfbfSEd Maste         else
430ac7ddfbfSEd Maste           m_stop_info_sp.reset();
431ac7ddfbfSEd Maste       }
432ac7ddfbfSEd Maste 
433435933ddSDimitry Andric       if (!m_stop_info_sp) {
4349f2f44ceSEd Maste         if (!CalculateStopInfo())
435ac7ddfbfSEd Maste           SetStopInfo(StopInfoSP());
436ac7ddfbfSEd Maste       }
437ac7ddfbfSEd Maste     }
4387aa51b79SEd Maste 
4394ba319b5SDimitry Andric     // The stop info can be manually set by calling Thread::SetStopInfo() prior
4404ba319b5SDimitry Andric     // to this function ever getting called, so we can't rely on
4414ba319b5SDimitry Andric     // "m_stop_info_stop_id != process_stop_id" as the condition for the if
4424ba319b5SDimitry Andric     // statement below, we must also check the stop info to see if we need to
4434ba319b5SDimitry Andric     // override it. See the header documentation in
4444ba319b5SDimitry Andric     // Process::GetStopInfoOverrideCallback() for more information on the stop
4454ba319b5SDimitry Andric     // info override callback.
446435933ddSDimitry Andric     if (m_stop_info_override_stop_id != process_stop_id) {
4477aa51b79SEd Maste       m_stop_info_override_stop_id = process_stop_id;
448435933ddSDimitry Andric       if (m_stop_info_sp) {
4494ba319b5SDimitry Andric         if (const Architecture *arch =
450acac075bSDimitry Andric                 process_sp->GetTarget().GetArchitecturePlugin())
451acac075bSDimitry Andric           arch->OverrideStopInfo(*this);
4527aa51b79SEd Maste       }
4537aa51b79SEd Maste     }
454ac7ddfbfSEd Maste   }
455ac7ddfbfSEd Maste   return m_stop_info_sp;
456ac7ddfbfSEd Maste }
457ac7ddfbfSEd Maste 
GetStopReason()458435933ddSDimitry Andric lldb::StopReason Thread::GetStopReason() {
459ac7ddfbfSEd Maste   lldb::StopInfoSP stop_info_sp(GetStopInfo());
460ac7ddfbfSEd Maste   if (stop_info_sp)
461ac7ddfbfSEd Maste     return stop_info_sp->GetStopReason();
462ac7ddfbfSEd Maste   return eStopReasonNone;
463ac7ddfbfSEd Maste }
464ac7ddfbfSEd Maste 
StopInfoIsUpToDate() const465435933ddSDimitry Andric bool Thread::StopInfoIsUpToDate() const {
4669f2f44ceSEd Maste   ProcessSP process_sp(GetProcess());
4679f2f44ceSEd Maste   if (process_sp)
4689f2f44ceSEd Maste     return m_stop_info_stop_id == process_sp->GetStopID();
4699f2f44ceSEd Maste   else
470435933ddSDimitry Andric     return true; // Process is no longer around so stop info is always up to
471435933ddSDimitry Andric                  // date...
4729f2f44ceSEd Maste }
473ac7ddfbfSEd Maste 
ResetStopInfo()474f678e45dSDimitry Andric void Thread::ResetStopInfo() {
475f678e45dSDimitry Andric   if (m_stop_info_sp) {
476f678e45dSDimitry Andric     m_stop_info_sp.reset();
477f678e45dSDimitry Andric   }
478f678e45dSDimitry Andric }
479f678e45dSDimitry Andric 
SetStopInfo(const lldb::StopInfoSP & stop_info_sp)480435933ddSDimitry Andric void Thread::SetStopInfo(const lldb::StopInfoSP &stop_info_sp) {
481ac7ddfbfSEd Maste   m_stop_info_sp = stop_info_sp;
482435933ddSDimitry Andric   if (m_stop_info_sp) {
483ac7ddfbfSEd Maste     m_stop_info_sp->MakeStopInfoValid();
484ac7ddfbfSEd Maste     // If we are overriding the ShouldReportStop, do that here:
485ac7ddfbfSEd Maste     if (m_override_should_notify != eLazyBoolCalculate)
486435933ddSDimitry Andric       m_stop_info_sp->OverrideShouldNotify(m_override_should_notify ==
487435933ddSDimitry Andric                                            eLazyBoolYes);
488ac7ddfbfSEd Maste   }
489ac7ddfbfSEd Maste 
490ac7ddfbfSEd Maste   ProcessSP process_sp(GetProcess());
491ac7ddfbfSEd Maste   if (process_sp)
492ac7ddfbfSEd Maste     m_stop_info_stop_id = process_sp->GetStopID();
493ac7ddfbfSEd Maste   else
494ac7ddfbfSEd Maste     m_stop_info_stop_id = UINT32_MAX;
495ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD));
496ac7ddfbfSEd Maste   if (log)
4970127ef0fSEd Maste     log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)",
4980127ef0fSEd Maste                 static_cast<void *>(this), GetID(),
4990127ef0fSEd Maste                 stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>",
5000127ef0fSEd Maste                 m_stop_info_stop_id);
501ac7ddfbfSEd Maste }
502ac7ddfbfSEd Maste 
SetShouldReportStop(Vote vote)503435933ddSDimitry Andric void Thread::SetShouldReportStop(Vote vote) {
504ac7ddfbfSEd Maste   if (vote == eVoteNoOpinion)
505ac7ddfbfSEd Maste     return;
506435933ddSDimitry Andric   else {
507ac7ddfbfSEd Maste     m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
508ac7ddfbfSEd Maste     if (m_stop_info_sp)
509435933ddSDimitry Andric       m_stop_info_sp->OverrideShouldNotify(m_override_should_notify ==
510435933ddSDimitry Andric                                            eLazyBoolYes);
511ac7ddfbfSEd Maste   }
512ac7ddfbfSEd Maste }
513ac7ddfbfSEd Maste 
SetStopInfoToNothing()514435933ddSDimitry Andric void Thread::SetStopInfoToNothing() {
515435933ddSDimitry Andric   // Note, we can't just NULL out the private reason, or the native thread
5164ba319b5SDimitry Andric   // implementation will try to go calculate it again.  For now, just set it to
5174ba319b5SDimitry Andric   // a Unix Signal with an invalid signal number.
518435933ddSDimitry Andric   SetStopInfo(
519435933ddSDimitry Andric       StopInfo::CreateStopReasonWithSignal(*this, LLDB_INVALID_SIGNAL_NUMBER));
520ac7ddfbfSEd Maste }
521ac7ddfbfSEd Maste 
ThreadStoppedForAReason(void)522435933ddSDimitry Andric bool Thread::ThreadStoppedForAReason(void) {
523ac7ddfbfSEd Maste   return (bool)GetPrivateStopInfo();
524ac7ddfbfSEd Maste }
525ac7ddfbfSEd Maste 
CheckpointThreadState(ThreadStateCheckpoint & saved_state)526435933ddSDimitry Andric bool Thread::CheckpointThreadState(ThreadStateCheckpoint &saved_state) {
527b952cd58SEd Maste   saved_state.register_backup_sp.reset();
528b952cd58SEd Maste   lldb::StackFrameSP frame_sp(GetStackFrameAtIndex(0));
529435933ddSDimitry Andric   if (frame_sp) {
530435933ddSDimitry Andric     lldb::RegisterCheckpointSP reg_checkpoint_sp(
531435933ddSDimitry Andric         new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
532435933ddSDimitry Andric     if (reg_checkpoint_sp) {
533b952cd58SEd Maste       lldb::RegisterContextSP reg_ctx_sp(frame_sp->GetRegisterContext());
534b952cd58SEd Maste       if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues(*reg_checkpoint_sp))
535b952cd58SEd Maste         saved_state.register_backup_sp = reg_checkpoint_sp;
536b952cd58SEd Maste     }
537b952cd58SEd Maste   }
538b952cd58SEd Maste   if (!saved_state.register_backup_sp)
539ac7ddfbfSEd Maste     return false;
540ac7ddfbfSEd Maste 
541ac7ddfbfSEd Maste   saved_state.stop_info_sp = GetStopInfo();
542ac7ddfbfSEd Maste   ProcessSP process_sp(GetProcess());
543ac7ddfbfSEd Maste   if (process_sp)
544ac7ddfbfSEd Maste     saved_state.orig_stop_id = process_sp->GetStopID();
545ac7ddfbfSEd Maste   saved_state.current_inlined_depth = GetCurrentInlinedDepth();
546f678e45dSDimitry Andric   saved_state.m_completed_plan_stack = m_completed_plan_stack;
547ac7ddfbfSEd Maste 
548ac7ddfbfSEd Maste   return true;
549ac7ddfbfSEd Maste }
550ac7ddfbfSEd Maste 
RestoreRegisterStateFromCheckpoint(ThreadStateCheckpoint & saved_state)551435933ddSDimitry Andric bool Thread::RestoreRegisterStateFromCheckpoint(
552435933ddSDimitry Andric     ThreadStateCheckpoint &saved_state) {
553435933ddSDimitry Andric   if (saved_state.register_backup_sp) {
554b952cd58SEd Maste     lldb::StackFrameSP frame_sp(GetStackFrameAtIndex(0));
555435933ddSDimitry Andric     if (frame_sp) {
556b952cd58SEd Maste       lldb::RegisterContextSP reg_ctx_sp(frame_sp->GetRegisterContext());
557435933ddSDimitry Andric       if (reg_ctx_sp) {
558435933ddSDimitry Andric         bool ret =
559435933ddSDimitry Andric             reg_ctx_sp->WriteAllRegisterValues(*saved_state.register_backup_sp);
560b952cd58SEd Maste 
561b952cd58SEd Maste         // Clear out all stack frames as our world just changed.
562b952cd58SEd Maste         ClearStackFrames();
563b952cd58SEd Maste         reg_ctx_sp->InvalidateIfNeeded(true);
564b952cd58SEd Maste         if (m_unwinder_ap.get())
565b952cd58SEd Maste           m_unwinder_ap->Clear();
566b952cd58SEd Maste         return ret;
567b952cd58SEd Maste       }
568b952cd58SEd Maste     }
569b952cd58SEd Maste   }
570b952cd58SEd Maste   return false;
571ac7ddfbfSEd Maste }
572ac7ddfbfSEd Maste 
RestoreThreadStateFromCheckpoint(ThreadStateCheckpoint & saved_state)573435933ddSDimitry Andric bool Thread::RestoreThreadStateFromCheckpoint(
574435933ddSDimitry Andric     ThreadStateCheckpoint &saved_state) {
575ac7ddfbfSEd Maste   if (saved_state.stop_info_sp)
576ac7ddfbfSEd Maste     saved_state.stop_info_sp->MakeStopInfoValid();
577ac7ddfbfSEd Maste   SetStopInfo(saved_state.stop_info_sp);
578435933ddSDimitry Andric   GetStackFrameList()->SetCurrentInlinedDepth(
579435933ddSDimitry Andric       saved_state.current_inlined_depth);
580f678e45dSDimitry Andric   m_completed_plan_stack = saved_state.m_completed_plan_stack;
581ac7ddfbfSEd Maste   return true;
582ac7ddfbfSEd Maste }
583ac7ddfbfSEd Maste 
GetState() const584435933ddSDimitry Andric StateType Thread::GetState() const {
585ac7ddfbfSEd Maste   // If any other threads access this we will need a mutex for it
5864bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
587ac7ddfbfSEd Maste   return m_state;
588ac7ddfbfSEd Maste }
589ac7ddfbfSEd Maste 
SetState(StateType state)590435933ddSDimitry Andric void Thread::SetState(StateType state) {
5914bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
592ac7ddfbfSEd Maste   m_state = state;
593ac7ddfbfSEd Maste }
594ac7ddfbfSEd Maste 
WillStop()595435933ddSDimitry Andric void Thread::WillStop() {
596ac7ddfbfSEd Maste   ThreadPlan *current_plan = GetCurrentPlan();
597ac7ddfbfSEd Maste 
598ac7ddfbfSEd Maste   // FIXME: I may decide to disallow threads with no plans.  In which
599ac7ddfbfSEd Maste   // case this should go to an assert.
600ac7ddfbfSEd Maste 
601ac7ddfbfSEd Maste   if (!current_plan)
602ac7ddfbfSEd Maste     return;
603ac7ddfbfSEd Maste 
604ac7ddfbfSEd Maste   current_plan->WillStop();
605ac7ddfbfSEd Maste }
606ac7ddfbfSEd Maste 
SetupForResume()607435933ddSDimitry Andric void Thread::SetupForResume() {
608435933ddSDimitry Andric   if (GetResumeState() != eStateSuspended) {
609435933ddSDimitry Andric     // If we're at a breakpoint push the step-over breakpoint plan.  Do this
6104ba319b5SDimitry Andric     // before telling the current plan it will resume, since we might change
6114ba319b5SDimitry Andric     // what the current plan is.
612ac7ddfbfSEd Maste 
613ac7ddfbfSEd Maste     lldb::RegisterContextSP reg_ctx_sp(GetRegisterContext());
614435933ddSDimitry Andric     if (reg_ctx_sp) {
6157aa51b79SEd Maste       const addr_t thread_pc = reg_ctx_sp->GetPC();
616435933ddSDimitry Andric       BreakpointSiteSP bp_site_sp =
617435933ddSDimitry Andric           GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc);
618435933ddSDimitry Andric       if (bp_site_sp) {
6194ba319b5SDimitry Andric         // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the
6204ba319b5SDimitry Andric         // target may not require anything special to step over a breakpoint.
621ac7ddfbfSEd Maste 
622ac7ddfbfSEd Maste         ThreadPlan *cur_plan = GetCurrentPlan();
623ac7ddfbfSEd Maste 
6247aa51b79SEd Maste         bool push_step_over_bp_plan = false;
625435933ddSDimitry Andric         if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint) {
626435933ddSDimitry Andric           ThreadPlanStepOverBreakpoint *bp_plan =
627435933ddSDimitry Andric               (ThreadPlanStepOverBreakpoint *)cur_plan;
6287aa51b79SEd Maste           if (bp_plan->GetBreakpointLoadAddress() != thread_pc)
6297aa51b79SEd Maste             push_step_over_bp_plan = true;
630435933ddSDimitry Andric         } else
6317aa51b79SEd Maste           push_step_over_bp_plan = true;
6327aa51b79SEd Maste 
633435933ddSDimitry Andric         if (push_step_over_bp_plan) {
6347aa51b79SEd Maste           ThreadPlanSP step_bp_plan_sp(new ThreadPlanStepOverBreakpoint(*this));
635435933ddSDimitry Andric           if (step_bp_plan_sp) {
6367aa51b79SEd Maste             step_bp_plan_sp->SetPrivate(true);
637ac7ddfbfSEd Maste 
638435933ddSDimitry Andric             if (GetCurrentPlan()->RunState() != eStateStepping) {
639435933ddSDimitry Andric               ThreadPlanStepOverBreakpoint *step_bp_plan =
640435933ddSDimitry Andric                   static_cast<ThreadPlanStepOverBreakpoint *>(
641435933ddSDimitry Andric                       step_bp_plan_sp.get());
642ac7ddfbfSEd Maste               step_bp_plan->SetAutoContinue(true);
643ac7ddfbfSEd Maste             }
644ac7ddfbfSEd Maste             QueueThreadPlan(step_bp_plan_sp, false);
645ac7ddfbfSEd Maste           }
646ac7ddfbfSEd Maste         }
647ac7ddfbfSEd Maste       }
648ac7ddfbfSEd Maste     }
649ac7ddfbfSEd Maste   }
650ac7ddfbfSEd Maste }
651ac7ddfbfSEd Maste 
ShouldResume(StateType resume_state)652435933ddSDimitry Andric bool Thread::ShouldResume(StateType resume_state) {
653ac7ddfbfSEd Maste   // At this point clear the completed plan stack.
654ac7ddfbfSEd Maste   m_completed_plan_stack.clear();
655ac7ddfbfSEd Maste   m_discarded_plan_stack.clear();
656ac7ddfbfSEd Maste   m_override_should_notify = eLazyBoolCalculate;
657ac7ddfbfSEd Maste 
6589f2f44ceSEd Maste   StateType prev_resume_state = GetTemporaryResumeState();
6599f2f44ceSEd Maste 
6609f2f44ceSEd Maste   SetTemporaryResumeState(resume_state);
661ac7ddfbfSEd Maste 
662ac7ddfbfSEd Maste   lldb::ThreadSP backing_thread_sp(GetBackingThread());
663ac7ddfbfSEd Maste   if (backing_thread_sp)
6649f2f44ceSEd Maste     backing_thread_sp->SetTemporaryResumeState(resume_state);
665ac7ddfbfSEd Maste 
666435933ddSDimitry Andric   // Make sure m_stop_info_sp is valid.  Don't do this for threads we suspended
667435933ddSDimitry Andric   // in the previous run.
6689f2f44ceSEd Maste   if (prev_resume_state != eStateSuspended)
669ac7ddfbfSEd Maste     GetPrivateStopInfo();
670ac7ddfbfSEd Maste 
671435933ddSDimitry Andric   // This is a little dubious, but we are trying to limit how often we actually
6724ba319b5SDimitry Andric   // fetch stop info from the target, 'cause that slows down single stepping.
6734ba319b5SDimitry Andric   // So assume that if we got to the point where we're about to resume, and we
6744ba319b5SDimitry Andric   // haven't yet had to fetch the stop reason, then it doesn't need to know
675ac7ddfbfSEd Maste   // about the fact that we are resuming...
676ac7ddfbfSEd Maste   const uint32_t process_stop_id = GetProcess()->GetStopID();
677ac7ddfbfSEd Maste   if (m_stop_info_stop_id == process_stop_id &&
678435933ddSDimitry Andric       (m_stop_info_sp && m_stop_info_sp->IsValid())) {
679ac7ddfbfSEd Maste     StopInfo *stop_info = GetPrivateStopInfo().get();
680ac7ddfbfSEd Maste     if (stop_info)
681ac7ddfbfSEd Maste       stop_info->WillResume(resume_state);
682ac7ddfbfSEd Maste   }
683ac7ddfbfSEd Maste 
684435933ddSDimitry Andric   // Tell all the plans that we are about to resume in case they need to clear
6854ba319b5SDimitry Andric   // any state. We distinguish between the plan on the top of the stack and the
6864ba319b5SDimitry Andric   // lower plans in case a plan needs to do any special business before it
6874ba319b5SDimitry Andric   // runs.
688ac7ddfbfSEd Maste 
689ac7ddfbfSEd Maste   bool need_to_resume = false;
690ac7ddfbfSEd Maste   ThreadPlan *plan_ptr = GetCurrentPlan();
691435933ddSDimitry Andric   if (plan_ptr) {
692ac7ddfbfSEd Maste     need_to_resume = plan_ptr->WillResume(resume_state, true);
693ac7ddfbfSEd Maste 
694435933ddSDimitry Andric     while ((plan_ptr = GetPreviousPlan(plan_ptr)) != nullptr) {
695ac7ddfbfSEd Maste       plan_ptr->WillResume(resume_state, false);
696ac7ddfbfSEd Maste     }
697ac7ddfbfSEd Maste 
698435933ddSDimitry Andric     // If the WillResume for the plan says we are faking a resume, then it will
6994ba319b5SDimitry Andric     // have set an appropriate stop info. In that case, don't reset it here.
700ac7ddfbfSEd Maste 
701435933ddSDimitry Andric     if (need_to_resume && resume_state != eStateSuspended) {
702ac7ddfbfSEd Maste       m_stop_info_sp.reset();
703ac7ddfbfSEd Maste     }
704ac7ddfbfSEd Maste   }
705ac7ddfbfSEd Maste 
706435933ddSDimitry Andric   if (need_to_resume) {
707ac7ddfbfSEd Maste     ClearStackFrames();
708ac7ddfbfSEd Maste     // Let Thread subclasses do any special work they need to prior to resuming
709ac7ddfbfSEd Maste     WillResume(resume_state);
710ac7ddfbfSEd Maste   }
711ac7ddfbfSEd Maste 
712ac7ddfbfSEd Maste   return need_to_resume;
713ac7ddfbfSEd Maste }
714ac7ddfbfSEd Maste 
DidResume()715435933ddSDimitry Andric void Thread::DidResume() { SetResumeSignal(LLDB_INVALID_SIGNAL_NUMBER); }
716ac7ddfbfSEd Maste 
DidStop()717435933ddSDimitry Andric void Thread::DidStop() { SetState(eStateStopped); }
718ac7ddfbfSEd Maste 
ShouldStop(Event * event_ptr)719435933ddSDimitry Andric bool Thread::ShouldStop(Event *event_ptr) {
720ac7ddfbfSEd Maste   ThreadPlan *current_plan = GetCurrentPlan();
721ac7ddfbfSEd Maste 
722ac7ddfbfSEd Maste   bool should_stop = true;
723ac7ddfbfSEd Maste 
724ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
725ac7ddfbfSEd Maste 
726435933ddSDimitry Andric   if (GetResumeState() == eStateSuspended) {
727ac7ddfbfSEd Maste     if (log)
728435933ddSDimitry Andric       log->Printf("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64
729435933ddSDimitry Andric                   ", should_stop = 0 (ignore since thread was suspended)",
7300127ef0fSEd Maste                   __FUNCTION__, GetID(), GetProtocolID());
731ac7ddfbfSEd Maste     return false;
732ac7ddfbfSEd Maste   }
733ac7ddfbfSEd Maste 
734435933ddSDimitry Andric   if (GetTemporaryResumeState() == eStateSuspended) {
735ac7ddfbfSEd Maste     if (log)
736435933ddSDimitry Andric       log->Printf("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64
737435933ddSDimitry Andric                   ", should_stop = 0 (ignore since thread was suspended)",
7380127ef0fSEd Maste                   __FUNCTION__, GetID(), GetProtocolID());
739ac7ddfbfSEd Maste     return false;
740ac7ddfbfSEd Maste   }
741ac7ddfbfSEd Maste 
742ac7ddfbfSEd Maste   // Based on the current thread plan and process stop info, check if this
7434ba319b5SDimitry Andric   // thread caused the process to stop. NOTE: this must take place before the
7444ba319b5SDimitry Andric   // plan is moved from the current plan stack to the completed plan stack.
745435933ddSDimitry Andric   if (!ThreadStoppedForAReason()) {
746ac7ddfbfSEd Maste     if (log)
747435933ddSDimitry Andric       log->Printf("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64
748435933ddSDimitry Andric                   ", pc = 0x%16.16" PRIx64
749435933ddSDimitry Andric                   ", should_stop = 0 (ignore since no stop reason)",
7500127ef0fSEd Maste                   __FUNCTION__, GetID(), GetProtocolID(),
751435933ddSDimitry Andric                   GetRegisterContext() ? GetRegisterContext()->GetPC()
752435933ddSDimitry Andric                                        : LLDB_INVALID_ADDRESS);
753ac7ddfbfSEd Maste     return false;
754ac7ddfbfSEd Maste   }
755ac7ddfbfSEd Maste 
756435933ddSDimitry Andric   if (log) {
757435933ddSDimitry Andric     log->Printf("Thread::%s(%p) for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64
758435933ddSDimitry Andric                 ", pc = 0x%16.16" PRIx64,
7590127ef0fSEd Maste                 __FUNCTION__, static_cast<void *>(this), GetID(),
760ac7ddfbfSEd Maste                 GetProtocolID(),
761435933ddSDimitry Andric                 GetRegisterContext() ? GetRegisterContext()->GetPC()
7620127ef0fSEd Maste                                      : LLDB_INVALID_ADDRESS);
763ac7ddfbfSEd Maste     log->Printf("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
764ac7ddfbfSEd Maste     StreamString s;
765ac7ddfbfSEd Maste     s.IndentMore();
766ac7ddfbfSEd Maste     DumpThreadPlans(&s);
767ac7ddfbfSEd Maste     log->Printf("Plan stack initial state:\n%s", s.GetData());
768ac7ddfbfSEd Maste   }
769ac7ddfbfSEd Maste 
770ac7ddfbfSEd Maste   // The top most plan always gets to do the trace log...
771ac7ddfbfSEd Maste   current_plan->DoTraceLog();
772ac7ddfbfSEd Maste 
773435933ddSDimitry Andric   // First query the stop info's ShouldStopSynchronous.  This handles
7744ba319b5SDimitry Andric   // "synchronous" stop reasons, for example the breakpoint command on internal
7754ba319b5SDimitry Andric   // breakpoints.  If a synchronous stop reason says we should not stop, then
7764ba319b5SDimitry Andric   // we don't have to do any more work on this stop.
777ac7ddfbfSEd Maste   StopInfoSP private_stop_info(GetPrivateStopInfo());
778435933ddSDimitry Andric   if (private_stop_info &&
779435933ddSDimitry Andric       !private_stop_info->ShouldStopSynchronous(event_ptr)) {
780ac7ddfbfSEd Maste     if (log)
781435933ddSDimitry Andric       log->Printf("StopInfo::ShouldStop async callback says we should not "
782435933ddSDimitry Andric                   "stop, returning ShouldStop of false.");
783ac7ddfbfSEd Maste     return false;
784ac7ddfbfSEd Maste   }
785ac7ddfbfSEd Maste 
7864ba319b5SDimitry Andric   // If we've already been restarted, don't query the plans since the state
7874ba319b5SDimitry Andric   // they would examine is not current.
788ac7ddfbfSEd Maste   if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
789ac7ddfbfSEd Maste     return false;
790ac7ddfbfSEd Maste 
791435933ddSDimitry Andric   // Before the plans see the state of the world, calculate the current inlined
792435933ddSDimitry Andric   // depth.
793ac7ddfbfSEd Maste   GetStackFrameList()->CalculateCurrentInlinedDepth();
794ac7ddfbfSEd Maste 
795435933ddSDimitry Andric   // If the base plan doesn't understand why we stopped, then we have to find a
7964ba319b5SDimitry Andric   // plan that does. If that plan is still working, then we don't need to do
7974ba319b5SDimitry Andric   // any more work.  If the plan that explains the stop is done, then we should
7984ba319b5SDimitry Andric   // pop all the plans below it, and pop it, and then let the plans above it
7994ba319b5SDimitry Andric   // decide whether they still need to do more work.
800ac7ddfbfSEd Maste 
801ac7ddfbfSEd Maste   bool done_processing_current_plan = false;
802ac7ddfbfSEd Maste 
803435933ddSDimitry Andric   if (!current_plan->PlanExplainsStop(event_ptr)) {
804435933ddSDimitry Andric     if (current_plan->TracerExplainsStop()) {
805ac7ddfbfSEd Maste       done_processing_current_plan = true;
806ac7ddfbfSEd Maste       should_stop = false;
807435933ddSDimitry Andric     } else {
8084ba319b5SDimitry Andric       // If the current plan doesn't explain the stop, then find one that does
8094ba319b5SDimitry Andric       // and let it handle the situation.
810ac7ddfbfSEd Maste       ThreadPlan *plan_ptr = current_plan;
811435933ddSDimitry Andric       while ((plan_ptr = GetPreviousPlan(plan_ptr)) != nullptr) {
812435933ddSDimitry Andric         if (plan_ptr->PlanExplainsStop(event_ptr)) {
813ac7ddfbfSEd Maste           should_stop = plan_ptr->ShouldStop(event_ptr);
814ac7ddfbfSEd Maste 
8154ba319b5SDimitry Andric           // plan_ptr explains the stop, next check whether plan_ptr is done,
8164ba319b5SDimitry Andric           // if so, then we should take it and all the plans below it off the
8174ba319b5SDimitry Andric           // stack.
818ac7ddfbfSEd Maste 
819435933ddSDimitry Andric           if (plan_ptr->MischiefManaged()) {
820435933ddSDimitry Andric             // We're going to pop the plans up to and including the plan that
821435933ddSDimitry Andric             // explains the stop.
822ac7ddfbfSEd Maste             ThreadPlan *prev_plan_ptr = GetPreviousPlan(plan_ptr);
823ac7ddfbfSEd Maste 
824435933ddSDimitry Andric             do {
825ac7ddfbfSEd Maste               if (should_stop)
826ac7ddfbfSEd Maste                 current_plan->WillStop();
827ac7ddfbfSEd Maste               PopPlan();
828435933ddSDimitry Andric             } while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
8294ba319b5SDimitry Andric             // Now, if the responsible plan was not "Okay to discard" then
8304ba319b5SDimitry Andric             // we're done, otherwise we forward this to the next plan in the
8314ba319b5SDimitry Andric             // stack below.
832435933ddSDimitry Andric             done_processing_current_plan =
833435933ddSDimitry Andric                 (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard());
834435933ddSDimitry Andric           } else
835ac7ddfbfSEd Maste             done_processing_current_plan = true;
836ac7ddfbfSEd Maste 
837ac7ddfbfSEd Maste           break;
838ac7ddfbfSEd Maste         }
839ac7ddfbfSEd Maste       }
840ac7ddfbfSEd Maste     }
841ac7ddfbfSEd Maste   }
842ac7ddfbfSEd Maste 
843435933ddSDimitry Andric   if (!done_processing_current_plan) {
844ac7ddfbfSEd Maste     bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
845ac7ddfbfSEd Maste 
846ac7ddfbfSEd Maste     if (log)
8470127ef0fSEd Maste       log->Printf("Plan %s explains stop, auto-continue %i.",
8480127ef0fSEd Maste                   current_plan->GetName(), over_ride_stop);
849ac7ddfbfSEd Maste 
850ac7ddfbfSEd Maste     // We're starting from the base plan, so just let it decide;
851435933ddSDimitry Andric     if (PlanIsBasePlan(current_plan)) {
852ac7ddfbfSEd Maste       should_stop = current_plan->ShouldStop(event_ptr);
853ac7ddfbfSEd Maste       if (log)
854ac7ddfbfSEd Maste         log->Printf("Base plan says should stop: %i.", should_stop);
855435933ddSDimitry Andric     } else {
8564ba319b5SDimitry Andric       // Otherwise, don't let the base plan override what the other plans say
8574ba319b5SDimitry Andric       // to do, since presumably if there were other plans they would know what
8584ba319b5SDimitry Andric       // to do...
859435933ddSDimitry Andric       while (1) {
860ac7ddfbfSEd Maste         if (PlanIsBasePlan(current_plan))
861ac7ddfbfSEd Maste           break;
862ac7ddfbfSEd Maste 
863ac7ddfbfSEd Maste         should_stop = current_plan->ShouldStop(event_ptr);
864ac7ddfbfSEd Maste         if (log)
865435933ddSDimitry Andric           log->Printf("Plan %s should stop: %d.", current_plan->GetName(),
866435933ddSDimitry Andric                       should_stop);
867435933ddSDimitry Andric         if (current_plan->MischiefManaged()) {
868ac7ddfbfSEd Maste           if (should_stop)
869ac7ddfbfSEd Maste             current_plan->WillStop();
870ac7ddfbfSEd Maste 
8714ba319b5SDimitry Andric           // If a Master Plan wants to stop, and wants to stick on the stack,
8724ba319b5SDimitry Andric           // we let it. Otherwise, see if the plan's parent wants to stop.
873ac7ddfbfSEd Maste 
874435933ddSDimitry Andric           if (should_stop && current_plan->IsMasterPlan() &&
875435933ddSDimitry Andric               !current_plan->OkayToDiscard()) {
876ac7ddfbfSEd Maste             PopPlan();
877ac7ddfbfSEd Maste             break;
878435933ddSDimitry Andric           } else {
879ac7ddfbfSEd Maste             PopPlan();
880ac7ddfbfSEd Maste 
881ac7ddfbfSEd Maste             current_plan = GetCurrentPlan();
882435933ddSDimitry Andric             if (current_plan == nullptr) {
883ac7ddfbfSEd Maste               break;
884ac7ddfbfSEd Maste             }
885ac7ddfbfSEd Maste           }
886435933ddSDimitry Andric         } else {
887ac7ddfbfSEd Maste           break;
888ac7ddfbfSEd Maste         }
889ac7ddfbfSEd Maste       }
890ac7ddfbfSEd Maste     }
891ac7ddfbfSEd Maste 
892ac7ddfbfSEd Maste     if (over_ride_stop)
893ac7ddfbfSEd Maste       should_stop = false;
8947aa51b79SEd Maste   }
8957aa51b79SEd Maste 
896435933ddSDimitry Andric   // One other potential problem is that we set up a master plan, then stop in
8974ba319b5SDimitry Andric   // before it is complete - for instance by hitting a breakpoint during a
8984ba319b5SDimitry Andric   // step-over - then do some step/finish/etc operations that wind up past the
8994ba319b5SDimitry Andric   // end point condition of the initial plan.  We don't want to strand the
9004ba319b5SDimitry Andric   // original plan on the stack, This code clears stale plans off the stack.
901ac7ddfbfSEd Maste 
902435933ddSDimitry Andric   if (should_stop) {
903ac7ddfbfSEd Maste     ThreadPlan *plan_ptr = GetCurrentPlan();
904f678e45dSDimitry Andric 
9054ba319b5SDimitry Andric     // Discard the stale plans and all plans below them in the stack, plus move
9064ba319b5SDimitry Andric     // the completed plans to the completed plan stack
907435933ddSDimitry Andric     while (!PlanIsBasePlan(plan_ptr)) {
908ac7ddfbfSEd Maste       bool stale = plan_ptr->IsPlanStale();
909ac7ddfbfSEd Maste       ThreadPlan *examined_plan = plan_ptr;
910ac7ddfbfSEd Maste       plan_ptr = GetPreviousPlan(examined_plan);
911ac7ddfbfSEd Maste 
912435933ddSDimitry Andric       if (stale) {
913ac7ddfbfSEd Maste         if (log)
914435933ddSDimitry Andric           log->Printf(
915435933ddSDimitry Andric               "Plan %s being discarded in cleanup, it says it is already done.",
9160127ef0fSEd Maste               examined_plan->GetName());
917f678e45dSDimitry Andric         while (GetCurrentPlan() != examined_plan) {
918f678e45dSDimitry Andric           DiscardPlan();
919f678e45dSDimitry Andric         }
920f678e45dSDimitry Andric         if (examined_plan->IsPlanComplete()) {
9214ba319b5SDimitry Andric           // plan is complete but does not explain the stop (example: step to a
9224ba319b5SDimitry Andric           // line with breakpoint), let us move the plan to
9234ba319b5SDimitry Andric           // completed_plan_stack anyway
924f678e45dSDimitry Andric           PopPlan();
925f678e45dSDimitry Andric         } else
926f678e45dSDimitry Andric           DiscardPlan();
927ac7ddfbfSEd Maste       }
928ac7ddfbfSEd Maste     }
929ac7ddfbfSEd Maste   }
930ac7ddfbfSEd Maste 
931435933ddSDimitry Andric   if (log) {
932ac7ddfbfSEd Maste     StreamString s;
933ac7ddfbfSEd Maste     s.IndentMore();
934ac7ddfbfSEd Maste     DumpThreadPlans(&s);
935ac7ddfbfSEd Maste     log->Printf("Plan stack final state:\n%s", s.GetData());
936435933ddSDimitry Andric     log->Printf("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv",
937435933ddSDimitry Andric                 should_stop);
938ac7ddfbfSEd Maste   }
939ac7ddfbfSEd Maste   return should_stop;
940ac7ddfbfSEd Maste }
941ac7ddfbfSEd Maste 
ShouldReportStop(Event * event_ptr)942435933ddSDimitry Andric Vote Thread::ShouldReportStop(Event *event_ptr) {
943ac7ddfbfSEd Maste   StateType thread_state = GetResumeState();
944ac7ddfbfSEd Maste   StateType temp_thread_state = GetTemporaryResumeState();
945ac7ddfbfSEd Maste 
946ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
947ac7ddfbfSEd Maste 
948435933ddSDimitry Andric   if (thread_state == eStateSuspended || thread_state == eStateInvalid) {
949ac7ddfbfSEd Maste     if (log)
950435933ddSDimitry Andric       log->Printf("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
951435933ddSDimitry Andric                   ": returning vote %i (state was suspended or invalid)",
952435933ddSDimitry Andric                   GetID(), eVoteNoOpinion);
953ac7ddfbfSEd Maste     return eVoteNoOpinion;
954ac7ddfbfSEd Maste   }
955ac7ddfbfSEd Maste 
956435933ddSDimitry Andric   if (temp_thread_state == eStateSuspended ||
957435933ddSDimitry Andric       temp_thread_state == eStateInvalid) {
958ac7ddfbfSEd Maste     if (log)
959435933ddSDimitry Andric       log->Printf(
960435933ddSDimitry Andric           "Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
961435933ddSDimitry Andric           ": returning vote %i (temporary state was suspended or invalid)",
962435933ddSDimitry Andric           GetID(), eVoteNoOpinion);
963ac7ddfbfSEd Maste     return eVoteNoOpinion;
964ac7ddfbfSEd Maste   }
965ac7ddfbfSEd Maste 
966435933ddSDimitry Andric   if (!ThreadStoppedForAReason()) {
967ac7ddfbfSEd Maste     if (log)
968435933ddSDimitry Andric       log->Printf("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
969435933ddSDimitry Andric                   ": returning vote %i (thread didn't stop for a reason.)",
970435933ddSDimitry Andric                   GetID(), eVoteNoOpinion);
971ac7ddfbfSEd Maste     return eVoteNoOpinion;
972ac7ddfbfSEd Maste   }
973ac7ddfbfSEd Maste 
974435933ddSDimitry Andric   if (m_completed_plan_stack.size() > 0) {
975ac7ddfbfSEd Maste     // Don't use GetCompletedPlan here, since that suppresses private plans.
976ac7ddfbfSEd Maste     if (log)
977435933ddSDimitry Andric       log->Printf("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
978435933ddSDimitry Andric                   ": returning vote  for complete stack's back plan",
979435933ddSDimitry Andric                   GetID());
980ac7ddfbfSEd Maste     return m_completed_plan_stack.back()->ShouldReportStop(event_ptr);
981435933ddSDimitry Andric   } else {
982ac7ddfbfSEd Maste     Vote thread_vote = eVoteNoOpinion;
983ac7ddfbfSEd Maste     ThreadPlan *plan_ptr = GetCurrentPlan();
984435933ddSDimitry Andric     while (1) {
985435933ddSDimitry Andric       if (plan_ptr->PlanExplainsStop(event_ptr)) {
986ac7ddfbfSEd Maste         thread_vote = plan_ptr->ShouldReportStop(event_ptr);
987ac7ddfbfSEd Maste         break;
988ac7ddfbfSEd Maste       }
989ac7ddfbfSEd Maste       if (PlanIsBasePlan(plan_ptr))
990ac7ddfbfSEd Maste         break;
991ac7ddfbfSEd Maste       else
992ac7ddfbfSEd Maste         plan_ptr = GetPreviousPlan(plan_ptr);
993ac7ddfbfSEd Maste     }
994ac7ddfbfSEd Maste     if (log)
995435933ddSDimitry Andric       log->Printf("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64
996435933ddSDimitry Andric                   ": returning vote %i for current plan",
997435933ddSDimitry Andric                   GetID(), thread_vote);
998ac7ddfbfSEd Maste 
999ac7ddfbfSEd Maste     return thread_vote;
1000ac7ddfbfSEd Maste   }
1001ac7ddfbfSEd Maste }
1002ac7ddfbfSEd Maste 
ShouldReportRun(Event * event_ptr)1003435933ddSDimitry Andric Vote Thread::ShouldReportRun(Event *event_ptr) {
1004ac7ddfbfSEd Maste   StateType thread_state = GetResumeState();
1005ac7ddfbfSEd Maste 
1006435933ddSDimitry Andric   if (thread_state == eStateSuspended || thread_state == eStateInvalid) {
1007ac7ddfbfSEd Maste     return eVoteNoOpinion;
1008ac7ddfbfSEd Maste   }
1009ac7ddfbfSEd Maste 
1010ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1011435933ddSDimitry Andric   if (m_completed_plan_stack.size() > 0) {
1012ac7ddfbfSEd Maste     // Don't use GetCompletedPlan here, since that suppresses private plans.
1013ac7ddfbfSEd Maste     if (log)
1014435933ddSDimitry Andric       log->Printf("Current Plan for thread %d(%p) (0x%4.4" PRIx64
1015435933ddSDimitry Andric                   ", %s): %s being asked whether we should report run.",
10160127ef0fSEd Maste                   GetIndexID(), static_cast<void *>(this), GetID(),
1017ac7ddfbfSEd Maste                   StateAsCString(GetTemporaryResumeState()),
1018ac7ddfbfSEd Maste                   m_completed_plan_stack.back()->GetName());
1019ac7ddfbfSEd Maste 
1020ac7ddfbfSEd Maste     return m_completed_plan_stack.back()->ShouldReportRun(event_ptr);
1021435933ddSDimitry Andric   } else {
1022ac7ddfbfSEd Maste     if (log)
1023435933ddSDimitry Andric       log->Printf("Current Plan for thread %d(%p) (0x%4.4" PRIx64
1024435933ddSDimitry Andric                   ", %s): %s being asked whether we should report run.",
10250127ef0fSEd Maste                   GetIndexID(), static_cast<void *>(this), GetID(),
1026ac7ddfbfSEd Maste                   StateAsCString(GetTemporaryResumeState()),
1027ac7ddfbfSEd Maste                   GetCurrentPlan()->GetName());
1028ac7ddfbfSEd Maste 
1029ac7ddfbfSEd Maste     return GetCurrentPlan()->ShouldReportRun(event_ptr);
1030ac7ddfbfSEd Maste   }
1031ac7ddfbfSEd Maste }
1032ac7ddfbfSEd Maste 
MatchesSpec(const ThreadSpec * spec)1033435933ddSDimitry Andric bool Thread::MatchesSpec(const ThreadSpec *spec) {
10349f2f44ceSEd Maste   return (spec == nullptr) ? true : spec->ThreadPassesBasicTests(*this);
1035ac7ddfbfSEd Maste }
1036ac7ddfbfSEd Maste 
PushPlan(ThreadPlanSP & thread_plan_sp)1037435933ddSDimitry Andric void Thread::PushPlan(ThreadPlanSP &thread_plan_sp) {
1038435933ddSDimitry Andric   if (thread_plan_sp) {
1039435933ddSDimitry Andric     // If the thread plan doesn't already have a tracer, give it its parent's
1040435933ddSDimitry Andric     // tracer:
1041*b5893f02SDimitry Andric     if (!thread_plan_sp->GetThreadPlanTracer()) {
1042*b5893f02SDimitry Andric       assert(!m_plan_stack.empty());
1043435933ddSDimitry Andric       thread_plan_sp->SetThreadPlanTracer(
1044435933ddSDimitry Andric           m_plan_stack.back()->GetThreadPlanTracer());
1045*b5893f02SDimitry Andric     }
1046ac7ddfbfSEd Maste     m_plan_stack.push_back(thread_plan_sp);
1047ac7ddfbfSEd Maste 
1048ac7ddfbfSEd Maste     thread_plan_sp->DidPush();
1049ac7ddfbfSEd Maste 
1050ac7ddfbfSEd Maste     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1051435933ddSDimitry Andric     if (log) {
1052ac7ddfbfSEd Maste       StreamString s;
1053ac7ddfbfSEd Maste       thread_plan_sp->GetDescription(&s, lldb::eDescriptionLevelFull);
1054ac7ddfbfSEd Maste       log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
10550127ef0fSEd Maste                   static_cast<void *>(this), s.GetData(),
1056ac7ddfbfSEd Maste                   thread_plan_sp->GetThread().GetID());
1057ac7ddfbfSEd Maste     }
1058ac7ddfbfSEd Maste   }
1059ac7ddfbfSEd Maste }
1060ac7ddfbfSEd Maste 
PopPlan()1061435933ddSDimitry Andric void Thread::PopPlan() {
1062ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1063ac7ddfbfSEd Maste 
1064ac7ddfbfSEd Maste   if (m_plan_stack.size() <= 1)
1065ac7ddfbfSEd Maste     return;
1066435933ddSDimitry Andric   else {
1067ac7ddfbfSEd Maste     ThreadPlanSP &plan = m_plan_stack.back();
1068435933ddSDimitry Andric     if (log) {
1069435933ddSDimitry Andric       log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
1070435933ddSDimitry Andric                   plan->GetName(), plan->GetThread().GetID());
1071ac7ddfbfSEd Maste     }
1072ac7ddfbfSEd Maste     m_completed_plan_stack.push_back(plan);
1073ac7ddfbfSEd Maste     plan->WillPop();
1074ac7ddfbfSEd Maste     m_plan_stack.pop_back();
1075ac7ddfbfSEd Maste   }
1076ac7ddfbfSEd Maste }
1077ac7ddfbfSEd Maste 
DiscardPlan()1078435933ddSDimitry Andric void Thread::DiscardPlan() {
1079ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1080435933ddSDimitry Andric   if (m_plan_stack.size() > 1) {
1081ac7ddfbfSEd Maste     ThreadPlanSP &plan = m_plan_stack.back();
1082ac7ddfbfSEd Maste     if (log)
1083435933ddSDimitry Andric       log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
1084435933ddSDimitry Andric                   plan->GetName(), plan->GetThread().GetID());
1085ac7ddfbfSEd Maste 
1086ac7ddfbfSEd Maste     m_discarded_plan_stack.push_back(plan);
1087ac7ddfbfSEd Maste     plan->WillPop();
1088ac7ddfbfSEd Maste     m_plan_stack.pop_back();
1089ac7ddfbfSEd Maste   }
1090ac7ddfbfSEd Maste }
1091ac7ddfbfSEd Maste 
GetCurrentPlan()1092435933ddSDimitry Andric ThreadPlan *Thread::GetCurrentPlan() {
10934ba319b5SDimitry Andric   // There will always be at least the base plan.  If somebody is mucking with
10944ba319b5SDimitry Andric   // a thread with an empty plan stack, we should assert right away.
10959f2f44ceSEd Maste   return m_plan_stack.empty() ? nullptr : m_plan_stack.back().get();
1096ac7ddfbfSEd Maste }
1097ac7ddfbfSEd Maste 
GetCompletedPlan()1098435933ddSDimitry Andric ThreadPlanSP Thread::GetCompletedPlan() {
1099ac7ddfbfSEd Maste   ThreadPlanSP empty_plan_sp;
1100435933ddSDimitry Andric   if (!m_completed_plan_stack.empty()) {
1101435933ddSDimitry Andric     for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) {
1102ac7ddfbfSEd Maste       ThreadPlanSP completed_plan_sp;
1103ac7ddfbfSEd Maste       completed_plan_sp = m_completed_plan_stack[i];
1104ac7ddfbfSEd Maste       if (!completed_plan_sp->GetPrivate())
1105ac7ddfbfSEd Maste         return completed_plan_sp;
1106ac7ddfbfSEd Maste     }
1107ac7ddfbfSEd Maste   }
1108ac7ddfbfSEd Maste   return empty_plan_sp;
1109ac7ddfbfSEd Maste }
1110ac7ddfbfSEd Maste 
GetReturnValueObject()1111435933ddSDimitry Andric ValueObjectSP Thread::GetReturnValueObject() {
1112435933ddSDimitry Andric   if (!m_completed_plan_stack.empty()) {
1113435933ddSDimitry Andric     for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) {
1114ac7ddfbfSEd Maste       ValueObjectSP return_valobj_sp;
1115ac7ddfbfSEd Maste       return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1116ac7ddfbfSEd Maste       if (return_valobj_sp)
1117ac7ddfbfSEd Maste         return return_valobj_sp;
1118ac7ddfbfSEd Maste     }
1119ac7ddfbfSEd Maste   }
1120ac7ddfbfSEd Maste   return ValueObjectSP();
1121ac7ddfbfSEd Maste }
1122ac7ddfbfSEd Maste 
GetExpressionVariable()1123435933ddSDimitry Andric ExpressionVariableSP Thread::GetExpressionVariable() {
1124435933ddSDimitry Andric   if (!m_completed_plan_stack.empty()) {
1125435933ddSDimitry Andric     for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) {
11269f2f44ceSEd Maste       ExpressionVariableSP expression_variable_sp;
1127435933ddSDimitry Andric       expression_variable_sp =
1128435933ddSDimitry Andric           m_completed_plan_stack[i]->GetExpressionVariable();
11290127ef0fSEd Maste       if (expression_variable_sp)
11300127ef0fSEd Maste         return expression_variable_sp;
11310127ef0fSEd Maste     }
11320127ef0fSEd Maste   }
11339f2f44ceSEd Maste   return ExpressionVariableSP();
11340127ef0fSEd Maste }
11350127ef0fSEd Maste 
IsThreadPlanDone(ThreadPlan * plan)1136435933ddSDimitry Andric bool Thread::IsThreadPlanDone(ThreadPlan *plan) {
1137435933ddSDimitry Andric   if (!m_completed_plan_stack.empty()) {
1138435933ddSDimitry Andric     for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--) {
1139ac7ddfbfSEd Maste       if (m_completed_plan_stack[i].get() == plan)
1140ac7ddfbfSEd Maste         return true;
1141ac7ddfbfSEd Maste     }
1142ac7ddfbfSEd Maste   }
1143ac7ddfbfSEd Maste   return false;
1144ac7ddfbfSEd Maste }
1145ac7ddfbfSEd Maste 
WasThreadPlanDiscarded(ThreadPlan * plan)1146435933ddSDimitry Andric bool Thread::WasThreadPlanDiscarded(ThreadPlan *plan) {
1147435933ddSDimitry Andric   if (!m_discarded_plan_stack.empty()) {
1148435933ddSDimitry Andric     for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--) {
1149ac7ddfbfSEd Maste       if (m_discarded_plan_stack[i].get() == plan)
1150ac7ddfbfSEd Maste         return true;
1151ac7ddfbfSEd Maste     }
1152ac7ddfbfSEd Maste   }
1153ac7ddfbfSEd Maste   return false;
1154ac7ddfbfSEd Maste }
1155ac7ddfbfSEd Maste 
CompletedPlanOverridesBreakpoint()1156f678e45dSDimitry Andric bool Thread::CompletedPlanOverridesBreakpoint() {
1157f678e45dSDimitry Andric   return (!m_completed_plan_stack.empty()) ;
1158f678e45dSDimitry Andric }
1159f678e45dSDimitry Andric 
GetPreviousPlan(ThreadPlan * current_plan)1160435933ddSDimitry Andric ThreadPlan *Thread::GetPreviousPlan(ThreadPlan *current_plan) {
11619f2f44ceSEd Maste   if (current_plan == nullptr)
11629f2f44ceSEd Maste     return nullptr;
1163ac7ddfbfSEd Maste 
1164ac7ddfbfSEd Maste   int stack_size = m_completed_plan_stack.size();
1165435933ddSDimitry Andric   for (int i = stack_size - 1; i > 0; i--) {
1166ac7ddfbfSEd Maste     if (current_plan == m_completed_plan_stack[i].get())
1167ac7ddfbfSEd Maste       return m_completed_plan_stack[i - 1].get();
1168ac7ddfbfSEd Maste   }
1169ac7ddfbfSEd Maste 
1170435933ddSDimitry Andric   if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan) {
11719f2f44ceSEd Maste     return GetCurrentPlan();
1172ac7ddfbfSEd Maste   }
1173ac7ddfbfSEd Maste 
1174ac7ddfbfSEd Maste   stack_size = m_plan_stack.size();
1175435933ddSDimitry Andric   for (int i = stack_size - 1; i > 0; i--) {
1176ac7ddfbfSEd Maste     if (current_plan == m_plan_stack[i].get())
1177ac7ddfbfSEd Maste       return m_plan_stack[i - 1].get();
1178ac7ddfbfSEd Maste   }
11799f2f44ceSEd Maste   return nullptr;
1180ac7ddfbfSEd Maste }
1181ac7ddfbfSEd Maste 
QueueThreadPlan(ThreadPlanSP & thread_plan_sp,bool abort_other_plans)1182*b5893f02SDimitry Andric Status Thread::QueueThreadPlan(ThreadPlanSP &thread_plan_sp,
1183435933ddSDimitry Andric                                bool abort_other_plans) {
1184*b5893f02SDimitry Andric   Status status;
1185*b5893f02SDimitry Andric   StreamString s;
1186*b5893f02SDimitry Andric   if (!thread_plan_sp->ValidatePlan(&s)) {
1187*b5893f02SDimitry Andric     DiscardThreadPlansUpToPlan(thread_plan_sp);
1188*b5893f02SDimitry Andric     thread_plan_sp.reset();
1189*b5893f02SDimitry Andric     status.SetErrorString(s.GetString());
1190*b5893f02SDimitry Andric     return status;
1191*b5893f02SDimitry Andric   }
1192*b5893f02SDimitry Andric 
1193ac7ddfbfSEd Maste   if (abort_other_plans)
1194ac7ddfbfSEd Maste     DiscardThreadPlans(true);
1195ac7ddfbfSEd Maste 
1196ac7ddfbfSEd Maste   PushPlan(thread_plan_sp);
1197*b5893f02SDimitry Andric 
1198*b5893f02SDimitry Andric   // This seems a little funny, but I don't want to have to split up the
1199*b5893f02SDimitry Andric   // constructor and the DidPush in the scripted plan, that seems annoying.
1200*b5893f02SDimitry Andric   // That means the constructor has to be in DidPush. So I have to validate the
1201*b5893f02SDimitry Andric   // plan AFTER pushing it, and then take it off again...
1202*b5893f02SDimitry Andric   if (!thread_plan_sp->ValidatePlan(&s)) {
1203*b5893f02SDimitry Andric     DiscardThreadPlansUpToPlan(thread_plan_sp);
1204*b5893f02SDimitry Andric     thread_plan_sp.reset();
1205*b5893f02SDimitry Andric     status.SetErrorString(s.GetString());
1206*b5893f02SDimitry Andric     return status;
1207*b5893f02SDimitry Andric   }
1208*b5893f02SDimitry Andric 
1209*b5893f02SDimitry Andric   return status;
1210ac7ddfbfSEd Maste }
1211ac7ddfbfSEd Maste 
EnableTracer(bool value,bool single_stepping)1212435933ddSDimitry Andric void Thread::EnableTracer(bool value, bool single_stepping) {
1213ac7ddfbfSEd Maste   int stack_size = m_plan_stack.size();
1214435933ddSDimitry Andric   for (int i = 0; i < stack_size; i++) {
1215435933ddSDimitry Andric     if (m_plan_stack[i]->GetThreadPlanTracer()) {
1216ac7ddfbfSEd Maste       m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1217ac7ddfbfSEd Maste       m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1218ac7ddfbfSEd Maste     }
1219ac7ddfbfSEd Maste   }
1220ac7ddfbfSEd Maste }
1221ac7ddfbfSEd Maste 
SetTracer(lldb::ThreadPlanTracerSP & tracer_sp)1222435933ddSDimitry Andric void Thread::SetTracer(lldb::ThreadPlanTracerSP &tracer_sp) {
1223ac7ddfbfSEd Maste   int stack_size = m_plan_stack.size();
1224ac7ddfbfSEd Maste   for (int i = 0; i < stack_size; i++)
1225ac7ddfbfSEd Maste     m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1226ac7ddfbfSEd Maste }
1227ac7ddfbfSEd Maste 
DiscardUserThreadPlansUpToIndex(uint32_t thread_index)1228435933ddSDimitry Andric bool Thread::DiscardUserThreadPlansUpToIndex(uint32_t thread_index) {
1229435933ddSDimitry Andric   // Count the user thread plans from the back end to get the number of the one
12304ba319b5SDimitry Andric   // we want to discard:
12317aa51b79SEd Maste 
12327aa51b79SEd Maste   uint32_t idx = 0;
12337aa51b79SEd Maste   ThreadPlan *up_to_plan_ptr = nullptr;
12347aa51b79SEd Maste 
1235435933ddSDimitry Andric   for (ThreadPlanSP plan_sp : m_plan_stack) {
12367aa51b79SEd Maste     if (plan_sp->GetPrivate())
12377aa51b79SEd Maste       continue;
1238435933ddSDimitry Andric     if (idx == thread_index) {
12397aa51b79SEd Maste       up_to_plan_ptr = plan_sp.get();
12407aa51b79SEd Maste       break;
1241435933ddSDimitry Andric     } else
12427aa51b79SEd Maste       idx++;
12437aa51b79SEd Maste   }
12447aa51b79SEd Maste 
12457aa51b79SEd Maste   if (up_to_plan_ptr == nullptr)
12467aa51b79SEd Maste     return false;
12477aa51b79SEd Maste 
12487aa51b79SEd Maste   DiscardThreadPlansUpToPlan(up_to_plan_ptr);
12497aa51b79SEd Maste   return true;
12507aa51b79SEd Maste }
12517aa51b79SEd Maste 
DiscardThreadPlansUpToPlan(lldb::ThreadPlanSP & up_to_plan_sp)1252435933ddSDimitry Andric void Thread::DiscardThreadPlansUpToPlan(lldb::ThreadPlanSP &up_to_plan_sp) {
1253ac7ddfbfSEd Maste   DiscardThreadPlansUpToPlan(up_to_plan_sp.get());
1254ac7ddfbfSEd Maste }
1255ac7ddfbfSEd Maste 
DiscardThreadPlansUpToPlan(ThreadPlan * up_to_plan_ptr)1256435933ddSDimitry Andric void Thread::DiscardThreadPlansUpToPlan(ThreadPlan *up_to_plan_ptr) {
1257ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1258ac7ddfbfSEd Maste   if (log)
1259435933ddSDimitry Andric     log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64
1260435933ddSDimitry Andric                 ", up to %p",
12610127ef0fSEd Maste                 GetID(), static_cast<void *>(up_to_plan_ptr));
1262ac7ddfbfSEd Maste 
1263ac7ddfbfSEd Maste   int stack_size = m_plan_stack.size();
1264ac7ddfbfSEd Maste 
1265435933ddSDimitry Andric   // If the input plan is nullptr, discard all plans.  Otherwise make sure this
12664ba319b5SDimitry Andric   // plan is in the stack, and if so discard up to and including it.
1267ac7ddfbfSEd Maste 
1268435933ddSDimitry Andric   if (up_to_plan_ptr == nullptr) {
1269ac7ddfbfSEd Maste     for (int i = stack_size - 1; i > 0; i--)
1270ac7ddfbfSEd Maste       DiscardPlan();
1271435933ddSDimitry Andric   } else {
1272ac7ddfbfSEd Maste     bool found_it = false;
1273435933ddSDimitry Andric     for (int i = stack_size - 1; i > 0; i--) {
1274ac7ddfbfSEd Maste       if (m_plan_stack[i].get() == up_to_plan_ptr)
1275ac7ddfbfSEd Maste         found_it = true;
1276ac7ddfbfSEd Maste     }
1277435933ddSDimitry Andric     if (found_it) {
1278ac7ddfbfSEd Maste       bool last_one = false;
1279435933ddSDimitry Andric       for (int i = stack_size - 1; i > 0 && !last_one; i--) {
1280ac7ddfbfSEd Maste         if (GetCurrentPlan() == up_to_plan_ptr)
1281ac7ddfbfSEd Maste           last_one = true;
1282ac7ddfbfSEd Maste         DiscardPlan();
1283ac7ddfbfSEd Maste       }
1284ac7ddfbfSEd Maste     }
1285ac7ddfbfSEd Maste   }
1286ac7ddfbfSEd Maste }
1287ac7ddfbfSEd Maste 
DiscardThreadPlans(bool force)1288435933ddSDimitry Andric void Thread::DiscardThreadPlans(bool force) {
1289ac7ddfbfSEd Maste   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_STEP));
1290435933ddSDimitry Andric   if (log) {
1291435933ddSDimitry Andric     log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64
1292435933ddSDimitry Andric                 ", force %d)",
1293435933ddSDimitry Andric                 GetID(), force);
1294ac7ddfbfSEd Maste   }
1295ac7ddfbfSEd Maste 
1296435933ddSDimitry Andric   if (force) {
1297ac7ddfbfSEd Maste     int stack_size = m_plan_stack.size();
1298435933ddSDimitry Andric     for (int i = stack_size - 1; i > 0; i--) {
1299ac7ddfbfSEd Maste       DiscardPlan();
1300ac7ddfbfSEd Maste     }
1301ac7ddfbfSEd Maste     return;
1302ac7ddfbfSEd Maste   }
1303ac7ddfbfSEd Maste 
1304435933ddSDimitry Andric   while (1) {
1305ac7ddfbfSEd Maste     int master_plan_idx;
1306ac7ddfbfSEd Maste     bool discard = true;
1307ac7ddfbfSEd Maste 
1308435933ddSDimitry Andric     // Find the first master plan, see if it wants discarding, and if yes
1309435933ddSDimitry Andric     // discard up to it.
1310435933ddSDimitry Andric     for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0;
1311435933ddSDimitry Andric          master_plan_idx--) {
1312435933ddSDimitry Andric       if (m_plan_stack[master_plan_idx]->IsMasterPlan()) {
1313ac7ddfbfSEd Maste         discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1314ac7ddfbfSEd Maste         break;
1315ac7ddfbfSEd Maste       }
1316ac7ddfbfSEd Maste     }
1317ac7ddfbfSEd Maste 
1318435933ddSDimitry Andric     if (discard) {
1319ac7ddfbfSEd Maste       // First pop all the dependent plans:
1320435933ddSDimitry Andric       for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--) {
1321435933ddSDimitry Andric         // FIXME: Do we need a finalize here, or is the rule that
1322435933ddSDimitry Andric         // "PrepareForStop"
1323ac7ddfbfSEd Maste         // for the plan leaves it in a state that it is safe to pop the plan
1324ac7ddfbfSEd Maste         // with no more notice?
1325ac7ddfbfSEd Maste         DiscardPlan();
1326ac7ddfbfSEd Maste       }
1327ac7ddfbfSEd Maste 
1328ac7ddfbfSEd Maste       // Now discard the master plan itself.
1329435933ddSDimitry Andric       // The bottom-most plan never gets discarded.  "OkayToDiscard" for it
13304ba319b5SDimitry Andric       // means discard it's dependent plans, but not it...
1331435933ddSDimitry Andric       if (master_plan_idx > 0) {
1332ac7ddfbfSEd Maste         DiscardPlan();
1333ac7ddfbfSEd Maste       }
1334435933ddSDimitry Andric     } else {
1335ac7ddfbfSEd Maste       // If the master plan doesn't want to get discarded, then we're done.
1336ac7ddfbfSEd Maste       break;
1337ac7ddfbfSEd Maste     }
1338ac7ddfbfSEd Maste   }
1339ac7ddfbfSEd Maste }
1340ac7ddfbfSEd Maste 
PlanIsBasePlan(ThreadPlan * plan_ptr)1341435933ddSDimitry Andric bool Thread::PlanIsBasePlan(ThreadPlan *plan_ptr) {
1342ac7ddfbfSEd Maste   if (plan_ptr->IsBasePlan())
1343ac7ddfbfSEd Maste     return true;
1344ac7ddfbfSEd Maste   else if (m_plan_stack.size() == 0)
1345ac7ddfbfSEd Maste     return false;
1346ac7ddfbfSEd Maste   else
1347ac7ddfbfSEd Maste     return m_plan_stack[0].get() == plan_ptr;
1348ac7ddfbfSEd Maste }
1349ac7ddfbfSEd Maste 
UnwindInnermostExpression()13505517e702SDimitry Andric Status Thread::UnwindInnermostExpression() {
13515517e702SDimitry Andric   Status error;
1352ac7ddfbfSEd Maste   int stack_size = m_plan_stack.size();
1353ac7ddfbfSEd Maste 
1354435933ddSDimitry Andric   // If the input plan is nullptr, discard all plans.  Otherwise make sure this
13554ba319b5SDimitry Andric   // plan is in the stack, and if so discard up to and including it.
1356ac7ddfbfSEd Maste 
1357435933ddSDimitry Andric   for (int i = stack_size - 1; i > 0; i--) {
1358435933ddSDimitry Andric     if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction) {
1359ac7ddfbfSEd Maste       DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1360ac7ddfbfSEd Maste       return error;
1361ac7ddfbfSEd Maste     }
1362ac7ddfbfSEd Maste   }
1363ac7ddfbfSEd Maste   error.SetErrorString("No expressions currently active on this thread");
1364ac7ddfbfSEd Maste   return error;
1365ac7ddfbfSEd Maste }
1366ac7ddfbfSEd Maste 
QueueFundamentalPlan(bool abort_other_plans)1367435933ddSDimitry Andric ThreadPlanSP Thread::QueueFundamentalPlan(bool abort_other_plans) {
1368ac7ddfbfSEd Maste   ThreadPlanSP thread_plan_sp(new ThreadPlanBase(*this));
1369ac7ddfbfSEd Maste   QueueThreadPlan(thread_plan_sp, abort_other_plans);
1370ac7ddfbfSEd Maste   return thread_plan_sp;
1371ac7ddfbfSEd Maste }
1372ac7ddfbfSEd Maste 
QueueThreadPlanForStepSingleInstruction(bool step_over,bool abort_other_plans,bool stop_other_threads,Status & status)1373435933ddSDimitry Andric ThreadPlanSP Thread::QueueThreadPlanForStepSingleInstruction(
1374*b5893f02SDimitry Andric     bool step_over, bool abort_other_plans, bool stop_other_threads,
1375*b5893f02SDimitry Andric     Status &status) {
1376435933ddSDimitry Andric   ThreadPlanSP thread_plan_sp(new ThreadPlanStepInstruction(
1377435933ddSDimitry Andric       *this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1378*b5893f02SDimitry Andric   status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1379ac7ddfbfSEd Maste   return thread_plan_sp;
1380ac7ddfbfSEd Maste }
1381ac7ddfbfSEd Maste 
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)1382435933ddSDimitry Andric ThreadPlanSP Thread::QueueThreadPlanForStepOverRange(
1383435933ddSDimitry Andric     bool abort_other_plans, const AddressRange &range,
1384435933ddSDimitry Andric     const SymbolContext &addr_context, lldb::RunMode stop_other_threads,
1385*b5893f02SDimitry Andric     Status &status, LazyBool step_out_avoids_code_withoug_debug_info) {
1386ac7ddfbfSEd Maste   ThreadPlanSP thread_plan_sp;
1387435933ddSDimitry Andric   thread_plan_sp.reset(new ThreadPlanStepOverRange(
1388435933ddSDimitry Andric       *this, range, addr_context, stop_other_threads,
1389435933ddSDimitry Andric       step_out_avoids_code_withoug_debug_info));
1390ac7ddfbfSEd Maste 
1391*b5893f02SDimitry Andric   status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1392ac7ddfbfSEd Maste   return thread_plan_sp;
1393ac7ddfbfSEd Maste }
1394ac7ddfbfSEd Maste 
13954ba319b5SDimitry Andric // Call the QueueThreadPlanForStepOverRange method which takes an address
13964ba319b5SDimitry Andric // 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)1397435933ddSDimitry Andric ThreadPlanSP Thread::QueueThreadPlanForStepOverRange(
1398435933ddSDimitry Andric     bool abort_other_plans, const LineEntry &line_entry,
1399435933ddSDimitry Andric     const SymbolContext &addr_context, lldb::RunMode stop_other_threads,
1400*b5893f02SDimitry Andric     Status &status, LazyBool step_out_avoids_code_withoug_debug_info) {
1401435933ddSDimitry Andric   return QueueThreadPlanForStepOverRange(
1402435933ddSDimitry Andric       abort_other_plans, line_entry.GetSameLineContiguousAddressRange(),
1403*b5893f02SDimitry Andric       addr_context, stop_other_threads, status,
14049f2f44ceSEd Maste       step_out_avoids_code_withoug_debug_info);
14059f2f44ceSEd Maste }
14069f2f44ceSEd Maste 
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)1407435933ddSDimitry Andric ThreadPlanSP Thread::QueueThreadPlanForStepInRange(
1408435933ddSDimitry Andric     bool abort_other_plans, const AddressRange &range,
1409435933ddSDimitry Andric     const SymbolContext &addr_context, const char *step_in_target,
1410*b5893f02SDimitry Andric     lldb::RunMode stop_other_threads, Status &status,
14110127ef0fSEd Maste     LazyBool step_in_avoids_code_without_debug_info,
1412435933ddSDimitry Andric     LazyBool step_out_avoids_code_without_debug_info) {
1413435933ddSDimitry Andric   ThreadPlanSP thread_plan_sp(
1414435933ddSDimitry Andric       new ThreadPlanStepInRange(*this, range, addr_context, stop_other_threads,
14150127ef0fSEd Maste                                 step_in_avoids_code_without_debug_info,
14167aa51b79SEd Maste                                 step_out_avoids_code_without_debug_info));
1417435933ddSDimitry Andric   ThreadPlanStepInRange *plan =
1418435933ddSDimitry Andric       static_cast<ThreadPlanStepInRange *>(thread_plan_sp.get());
14190127ef0fSEd Maste 
1420ac7ddfbfSEd Maste   if (step_in_target)
1421ac7ddfbfSEd Maste     plan->SetStepInTarget(step_in_target);
14220127ef0fSEd Maste 
1423*b5893f02SDimitry Andric   status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1424ac7ddfbfSEd Maste   return thread_plan_sp;
1425ac7ddfbfSEd Maste }
1426ac7ddfbfSEd Maste 
14279f2f44ceSEd Maste // 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)1428435933ddSDimitry Andric ThreadPlanSP Thread::QueueThreadPlanForStepInRange(
1429435933ddSDimitry Andric     bool abort_other_plans, const LineEntry &line_entry,
1430435933ddSDimitry Andric     const SymbolContext &addr_context, const char *step_in_target,
1431*b5893f02SDimitry Andric     lldb::RunMode stop_other_threads, Status &status,
14329f2f44ceSEd Maste     LazyBool step_in_avoids_code_without_debug_info,
1433435933ddSDimitry Andric     LazyBool step_out_avoids_code_without_debug_info) {
1434435933ddSDimitry Andric   return QueueThreadPlanForStepInRange(
1435435933ddSDimitry Andric       abort_other_plans, line_entry.GetSameLineContiguousAddressRange(),
1436*b5893f02SDimitry Andric       addr_context, step_in_target, stop_other_threads, status,
14379f2f44ceSEd Maste       step_in_avoids_code_without_debug_info,
14389f2f44ceSEd Maste       step_out_avoids_code_without_debug_info);
14399f2f44ceSEd Maste }
14409f2f44ceSEd Maste 
QueueThreadPlanForStepOut(bool abort_other_plans,SymbolContext * addr_context,bool first_insn,bool stop_other_threads,Vote stop_vote,Vote run_vote,uint32_t frame_idx,Status & status,LazyBool step_out_avoids_code_without_debug_info)1441435933ddSDimitry Andric ThreadPlanSP Thread::QueueThreadPlanForStepOut(
1442435933ddSDimitry Andric     bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
1443435933ddSDimitry Andric     bool stop_other_threads, Vote stop_vote, Vote run_vote, uint32_t frame_idx,
1444*b5893f02SDimitry Andric     Status &status, LazyBool step_out_avoids_code_without_debug_info) {
1445435933ddSDimitry Andric   ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut(
1446435933ddSDimitry Andric       *this, addr_context, first_insn, stop_other_threads, stop_vote, run_vote,
1447435933ddSDimitry Andric       frame_idx, step_out_avoids_code_without_debug_info));
1448ac7ddfbfSEd Maste 
1449*b5893f02SDimitry Andric   status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
14500127ef0fSEd Maste   return thread_plan_sp;
14510127ef0fSEd Maste }
14520127ef0fSEd Maste 
QueueThreadPlanForStepOutNoShouldStop(bool abort_other_plans,SymbolContext * addr_context,bool first_insn,bool stop_other_threads,Vote stop_vote,Vote run_vote,uint32_t frame_idx,Status & status,bool continue_to_next_branch)1453435933ddSDimitry Andric ThreadPlanSP Thread::QueueThreadPlanForStepOutNoShouldStop(
1454435933ddSDimitry Andric     bool abort_other_plans, SymbolContext *addr_context, bool first_insn,
1455435933ddSDimitry Andric     bool stop_other_threads, Vote stop_vote, Vote run_vote, uint32_t frame_idx,
1456*b5893f02SDimitry Andric     Status &status, bool continue_to_next_branch) {
1457435933ddSDimitry Andric   const bool calculate_return_value =
1458435933ddSDimitry Andric       false; // No need to calculate the return value here.
1459435933ddSDimitry Andric   ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut(
1460435933ddSDimitry Andric       *this, addr_context, first_insn, stop_other_threads, stop_vote, run_vote,
1461435933ddSDimitry Andric       frame_idx, eLazyBoolNo, continue_to_next_branch, calculate_return_value));
14627aa51b79SEd Maste 
1463435933ddSDimitry Andric   ThreadPlanStepOut *new_plan =
1464435933ddSDimitry Andric       static_cast<ThreadPlanStepOut *>(thread_plan_sp.get());
14650127ef0fSEd Maste   new_plan->ClearShouldStopHereCallbacks();
1466ac7ddfbfSEd Maste 
1467*b5893f02SDimitry Andric   status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1468ac7ddfbfSEd Maste   return thread_plan_sp;
1469ac7ddfbfSEd Maste }
1470ac7ddfbfSEd Maste 
QueueThreadPlanForStepThrough(StackID & return_stack_id,bool abort_other_plans,bool stop_other_threads,Status & status)1471435933ddSDimitry Andric ThreadPlanSP Thread::QueueThreadPlanForStepThrough(StackID &return_stack_id,
1472435933ddSDimitry Andric                                                    bool abort_other_plans,
1473*b5893f02SDimitry Andric                                                    bool stop_other_threads,
1474*b5893f02SDimitry Andric                                                    Status &status) {
1475435933ddSDimitry Andric   ThreadPlanSP thread_plan_sp(
1476435933ddSDimitry Andric       new ThreadPlanStepThrough(*this, return_stack_id, stop_other_threads));
14779f2f44ceSEd Maste   if (!thread_plan_sp || !thread_plan_sp->ValidatePlan(nullptr))
1478ac7ddfbfSEd Maste     return ThreadPlanSP();
1479ac7ddfbfSEd Maste 
1480*b5893f02SDimitry Andric   status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1481ac7ddfbfSEd Maste   return thread_plan_sp;
1482ac7ddfbfSEd Maste }
1483ac7ddfbfSEd Maste 
QueueThreadPlanForRunToAddress(bool abort_other_plans,Address & target_addr,bool stop_other_threads,Status & status)1484435933ddSDimitry Andric ThreadPlanSP Thread::QueueThreadPlanForRunToAddress(bool abort_other_plans,
1485ac7ddfbfSEd Maste                                                     Address &target_addr,
1486*b5893f02SDimitry Andric                                                     bool stop_other_threads,
1487*b5893f02SDimitry Andric                                                     Status &status) {
1488435933ddSDimitry Andric   ThreadPlanSP thread_plan_sp(
1489435933ddSDimitry Andric       new ThreadPlanRunToAddress(*this, target_addr, stop_other_threads));
1490*b5893f02SDimitry Andric 
1491*b5893f02SDimitry Andric   status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1492ac7ddfbfSEd Maste   return thread_plan_sp;
1493ac7ddfbfSEd Maste }
1494ac7ddfbfSEd Maste 
QueueThreadPlanForStepUntil(bool abort_other_plans,lldb::addr_t * address_list,size_t num_addresses,bool stop_other_threads,uint32_t frame_idx,Status & status)1495*b5893f02SDimitry Andric ThreadPlanSP Thread::QueueThreadPlanForStepUntil(
1496*b5893f02SDimitry Andric     bool abort_other_plans, lldb::addr_t *address_list, size_t num_addresses,
1497*b5893f02SDimitry Andric     bool stop_other_threads, uint32_t frame_idx, Status &status) {
1498435933ddSDimitry Andric   ThreadPlanSP thread_plan_sp(new ThreadPlanStepUntil(
1499435933ddSDimitry Andric       *this, address_list, num_addresses, stop_other_threads, frame_idx));
1500*b5893f02SDimitry Andric 
1501*b5893f02SDimitry Andric   status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
1502ac7ddfbfSEd Maste   return thread_plan_sp;
1503ac7ddfbfSEd Maste }
1504ac7ddfbfSEd Maste 
QueueThreadPlanForStepScripted(bool abort_other_plans,const char * class_name,bool stop_other_threads,Status & status)1505435933ddSDimitry Andric lldb::ThreadPlanSP Thread::QueueThreadPlanForStepScripted(
1506*b5893f02SDimitry Andric     bool abort_other_plans, const char *class_name, bool stop_other_threads,
1507*b5893f02SDimitry Andric     Status &status) {
15087aa51b79SEd Maste   ThreadPlanSP thread_plan_sp(new ThreadPlanPython(*this, class_name));
1509*b5893f02SDimitry Andric 
1510*b5893f02SDimitry Andric   status = QueueThreadPlan(thread_plan_sp, abort_other_plans);
15117aa51b79SEd Maste   return thread_plan_sp;
15127aa51b79SEd Maste }
15137aa51b79SEd Maste 
GetIndexID() const1514435933ddSDimitry Andric uint32_t Thread::GetIndexID() const { return m_index_id; }
1515ac7ddfbfSEd Maste 
PrintPlanElement(Stream * s,const ThreadPlanSP & plan,lldb::DescriptionLevel desc_level,int32_t elem_idx)1516435933ddSDimitry Andric static void PrintPlanElement(Stream *s, const ThreadPlanSP &plan,
1517435933ddSDimitry Andric                              lldb::DescriptionLevel desc_level,
1518435933ddSDimitry Andric                              int32_t elem_idx) {
1519ac7ddfbfSEd Maste   s->IndentMore();
1520ac7ddfbfSEd Maste   s->Indent();
15217aa51b79SEd Maste   s->Printf("Element %d: ", elem_idx);
15227aa51b79SEd Maste   plan->GetDescription(s, desc_level);
1523ac7ddfbfSEd Maste   s->EOL();
1524ac7ddfbfSEd Maste   s->IndentLess();
1525ac7ddfbfSEd Maste }
1526ac7ddfbfSEd Maste 
PrintPlanStack(Stream * s,const std::vector<lldb::ThreadPlanSP> & plan_stack,lldb::DescriptionLevel desc_level,bool include_internal)1527435933ddSDimitry Andric static void PrintPlanStack(Stream *s,
1528435933ddSDimitry Andric                            const std::vector<lldb::ThreadPlanSP> &plan_stack,
1529435933ddSDimitry Andric                            lldb::DescriptionLevel desc_level,
1530435933ddSDimitry Andric                            bool include_internal) {
15317aa51b79SEd Maste   int32_t print_idx = 0;
1532435933ddSDimitry Andric   for (ThreadPlanSP plan_sp : plan_stack) {
1533435933ddSDimitry Andric     if (include_internal || !plan_sp->GetPrivate()) {
15347aa51b79SEd Maste       PrintPlanElement(s, plan_sp, desc_level, print_idx++);
15357aa51b79SEd Maste     }
15367aa51b79SEd Maste   }
15377aa51b79SEd Maste }
15387aa51b79SEd Maste 
DumpThreadPlans(Stream * s,lldb::DescriptionLevel desc_level,bool include_internal,bool ignore_boring_threads) const1539435933ddSDimitry Andric void Thread::DumpThreadPlans(Stream *s, lldb::DescriptionLevel desc_level,
15407aa51b79SEd Maste                              bool include_internal,
1541435933ddSDimitry Andric                              bool ignore_boring_threads) const {
15427aa51b79SEd Maste   uint32_t stack_size;
15437aa51b79SEd Maste 
1544435933ddSDimitry Andric   if (ignore_boring_threads) {
15457aa51b79SEd Maste     uint32_t stack_size = m_plan_stack.size();
15467aa51b79SEd Maste     uint32_t completed_stack_size = m_completed_plan_stack.size();
15477aa51b79SEd Maste     uint32_t discarded_stack_size = m_discarded_plan_stack.size();
1548435933ddSDimitry Andric     if (stack_size == 1 && completed_stack_size == 0 &&
1549435933ddSDimitry Andric         discarded_stack_size == 0) {
15507aa51b79SEd Maste       s->Printf("thread #%u: tid = 0x%4.4" PRIx64 "\n", GetIndexID(), GetID());
15517aa51b79SEd Maste       s->IndentMore();
15527aa51b79SEd Maste       s->Indent();
15537aa51b79SEd Maste       s->Printf("No active thread plans\n");
15547aa51b79SEd Maste       s->IndentLess();
15557aa51b79SEd Maste       return;
15567aa51b79SEd Maste     }
15577aa51b79SEd Maste   }
15587aa51b79SEd Maste 
15597aa51b79SEd Maste   s->Indent();
15607aa51b79SEd Maste   s->Printf("thread #%u: tid = 0x%4.4" PRIx64 ":\n", GetIndexID(), GetID());
15617aa51b79SEd Maste   s->IndentMore();
15627aa51b79SEd Maste   s->Indent();
15637aa51b79SEd Maste   s->Printf("Active plan stack:\n");
15647aa51b79SEd Maste   PrintPlanStack(s, m_plan_stack, desc_level, include_internal);
15657aa51b79SEd Maste 
1566ac7ddfbfSEd Maste   stack_size = m_completed_plan_stack.size();
1567435933ddSDimitry Andric   if (stack_size > 0) {
1568ac7ddfbfSEd Maste     s->Indent();
15697aa51b79SEd Maste     s->Printf("Completed Plan Stack:\n");
15707aa51b79SEd Maste     PrintPlanStack(s, m_completed_plan_stack, desc_level, include_internal);
1571ac7ddfbfSEd Maste   }
1572ac7ddfbfSEd Maste 
1573ac7ddfbfSEd Maste   stack_size = m_discarded_plan_stack.size();
1574435933ddSDimitry Andric   if (stack_size > 0) {
1575ac7ddfbfSEd Maste     s->Indent();
15767aa51b79SEd Maste     s->Printf("Discarded Plan Stack:\n");
15777aa51b79SEd Maste     PrintPlanStack(s, m_discarded_plan_stack, desc_level, include_internal);
1578ac7ddfbfSEd Maste   }
1579ac7ddfbfSEd Maste 
15807aa51b79SEd Maste   s->IndentLess();
1581ac7ddfbfSEd Maste }
1582ac7ddfbfSEd Maste 
CalculateTarget()1583435933ddSDimitry Andric TargetSP Thread::CalculateTarget() {
1584ac7ddfbfSEd Maste   TargetSP target_sp;
1585ac7ddfbfSEd Maste   ProcessSP process_sp(GetProcess());
1586ac7ddfbfSEd Maste   if (process_sp)
1587ac7ddfbfSEd Maste     target_sp = process_sp->CalculateTarget();
1588ac7ddfbfSEd Maste   return target_sp;
1589ac7ddfbfSEd Maste }
1590ac7ddfbfSEd Maste 
CalculateProcess()1591435933ddSDimitry Andric ProcessSP Thread::CalculateProcess() { return GetProcess(); }
1592ac7ddfbfSEd Maste 
CalculateThread()1593435933ddSDimitry Andric ThreadSP Thread::CalculateThread() { return shared_from_this(); }
1594ac7ddfbfSEd Maste 
CalculateStackFrame()1595435933ddSDimitry Andric StackFrameSP Thread::CalculateStackFrame() { return StackFrameSP(); }
1596ac7ddfbfSEd Maste 
CalculateExecutionContext(ExecutionContext & exe_ctx)1597435933ddSDimitry Andric void Thread::CalculateExecutionContext(ExecutionContext &exe_ctx) {
1598ac7ddfbfSEd Maste   exe_ctx.SetContext(shared_from_this());
1599ac7ddfbfSEd Maste }
1600ac7ddfbfSEd Maste 
GetStackFrameList()1601435933ddSDimitry Andric StackFrameListSP Thread::GetStackFrameList() {
1602ac7ddfbfSEd Maste   StackFrameListSP frame_list_sp;
16034bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
1604435933ddSDimitry Andric   if (m_curr_frames_sp) {
1605ac7ddfbfSEd Maste     frame_list_sp = m_curr_frames_sp;
1606435933ddSDimitry Andric   } else {
1607ac7ddfbfSEd Maste     frame_list_sp.reset(new StackFrameList(*this, m_prev_frames_sp, true));
1608ac7ddfbfSEd Maste     m_curr_frames_sp = frame_list_sp;
1609ac7ddfbfSEd Maste   }
1610ac7ddfbfSEd Maste   return frame_list_sp;
1611ac7ddfbfSEd Maste }
1612ac7ddfbfSEd Maste 
ClearStackFrames()1613435933ddSDimitry Andric void Thread::ClearStackFrames() {
16144bb0738eSEd Maste   std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
1615ac7ddfbfSEd Maste 
1616ac7ddfbfSEd Maste   Unwind *unwinder = GetUnwinder();
1617ac7ddfbfSEd Maste   if (unwinder)
1618ac7ddfbfSEd Maste     unwinder->Clear();
1619ac7ddfbfSEd Maste 
1620435933ddSDimitry Andric   // Only store away the old "reference" StackFrameList if we got all its
1621435933ddSDimitry Andric   // frames:
1622435933ddSDimitry Andric   // FIXME: At some point we can try to splice in the frames we have fetched
1623435933ddSDimitry Andric   // into
1624ac7ddfbfSEd Maste   // the new frame as we make it, but let's not try that now.
1625ac7ddfbfSEd Maste   if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
1626ac7ddfbfSEd Maste     m_prev_frames_sp.swap(m_curr_frames_sp);
1627ac7ddfbfSEd Maste   m_curr_frames_sp.reset();
16280127ef0fSEd Maste 
16290127ef0fSEd Maste   m_extended_info.reset();
16300127ef0fSEd Maste   m_extended_info_fetched = false;
1631ac7ddfbfSEd Maste }
1632ac7ddfbfSEd Maste 
GetFrameWithConcreteFrameIndex(uint32_t unwind_idx)1633435933ddSDimitry Andric lldb::StackFrameSP Thread::GetFrameWithConcreteFrameIndex(uint32_t unwind_idx) {
1634ac7ddfbfSEd Maste   return GetStackFrameList()->GetFrameWithConcreteFrameIndex(unwind_idx);
1635ac7ddfbfSEd Maste }
1636ac7ddfbfSEd Maste 
ReturnFromFrameWithIndex(uint32_t frame_idx,lldb::ValueObjectSP return_value_sp,bool broadcast)16375517e702SDimitry Andric Status Thread::ReturnFromFrameWithIndex(uint32_t frame_idx,
1638435933ddSDimitry Andric                                         lldb::ValueObjectSP return_value_sp,
1639435933ddSDimitry Andric                                         bool broadcast) {
1640ac7ddfbfSEd Maste   StackFrameSP frame_sp = GetStackFrameAtIndex(frame_idx);
16415517e702SDimitry Andric   Status return_error;
1642ac7ddfbfSEd Maste 
1643435933ddSDimitry Andric   if (!frame_sp) {
1644435933ddSDimitry Andric     return_error.SetErrorStringWithFormat(
1645435933ddSDimitry Andric         "Could not find frame with index %d in thread 0x%" PRIx64 ".",
1646435933ddSDimitry Andric         frame_idx, GetID());
1647ac7ddfbfSEd Maste   }
1648ac7ddfbfSEd Maste 
1649ac7ddfbfSEd Maste   return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
1650ac7ddfbfSEd Maste }
1651ac7ddfbfSEd Maste 
ReturnFromFrame(lldb::StackFrameSP frame_sp,lldb::ValueObjectSP return_value_sp,bool broadcast)16525517e702SDimitry Andric Status Thread::ReturnFromFrame(lldb::StackFrameSP frame_sp,
1653435933ddSDimitry Andric                                lldb::ValueObjectSP return_value_sp,
1654435933ddSDimitry Andric                                bool broadcast) {
16555517e702SDimitry Andric   Status return_error;
1656ac7ddfbfSEd Maste 
1657435933ddSDimitry Andric   if (!frame_sp) {
1658ac7ddfbfSEd Maste     return_error.SetErrorString("Can't return to a null frame.");
1659ac7ddfbfSEd Maste     return return_error;
1660ac7ddfbfSEd Maste   }
1661ac7ddfbfSEd Maste 
1662ac7ddfbfSEd Maste   Thread *thread = frame_sp->GetThread().get();
1663ac7ddfbfSEd Maste   uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1664ac7ddfbfSEd Maste   StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
1665435933ddSDimitry Andric   if (!older_frame_sp) {
1666ac7ddfbfSEd Maste     return_error.SetErrorString("No older frame to return to.");
1667ac7ddfbfSEd Maste     return return_error;
1668ac7ddfbfSEd Maste   }
1669ac7ddfbfSEd Maste 
1670435933ddSDimitry Andric   if (return_value_sp) {
1671ac7ddfbfSEd Maste     lldb::ABISP abi = thread->GetProcess()->GetABI();
1672435933ddSDimitry Andric     if (!abi) {
1673ac7ddfbfSEd Maste       return_error.SetErrorString("Could not find ABI to set return value.");
1674ac7ddfbfSEd Maste       return return_error;
1675ac7ddfbfSEd Maste     }
1676ac7ddfbfSEd Maste     SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1677ac7ddfbfSEd Maste 
1678435933ddSDimitry Andric     // FIXME: ValueObject::Cast doesn't currently work correctly, at least not
1679435933ddSDimitry Andric     // for scalars.
1680ac7ddfbfSEd Maste     // Turn that back on when that works.
1681435933ddSDimitry Andric     if (/* DISABLES CODE */ (0) && sc.function != nullptr) {
1682ac7ddfbfSEd Maste       Type *function_type = sc.function->GetType();
1683435933ddSDimitry Andric       if (function_type) {
1684435933ddSDimitry Andric         CompilerType return_type =
1685435933ddSDimitry Andric             sc.function->GetCompilerType().GetFunctionReturnType();
1686435933ddSDimitry Andric         if (return_type) {
1687ac7ddfbfSEd Maste           StreamString s;
1688ac7ddfbfSEd Maste           return_type.DumpTypeDescription(&s);
1689ac7ddfbfSEd Maste           ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
1690435933ddSDimitry Andric           if (cast_value_sp) {
1691ac7ddfbfSEd Maste             cast_value_sp->SetFormat(eFormatHex);
1692ac7ddfbfSEd Maste             return_value_sp = cast_value_sp;
1693ac7ddfbfSEd Maste           }
1694ac7ddfbfSEd Maste         }
1695ac7ddfbfSEd Maste       }
1696ac7ddfbfSEd Maste     }
1697ac7ddfbfSEd Maste 
1698ac7ddfbfSEd Maste     return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
1699ac7ddfbfSEd Maste     if (!return_error.Success())
1700ac7ddfbfSEd Maste       return return_error;
1701ac7ddfbfSEd Maste   }
1702ac7ddfbfSEd Maste 
17034ba319b5SDimitry Andric   // Now write the return registers for the chosen frame: Note, we can't use
17044ba319b5SDimitry Andric   // ReadAllRegisterValues->WriteAllRegisterValues, since the read & write cook
17054ba319b5SDimitry Andric   // their data
1706ac7ddfbfSEd Maste 
1707ac7ddfbfSEd Maste   StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
1708435933ddSDimitry Andric   if (youngest_frame_sp) {
1709ac7ddfbfSEd Maste     lldb::RegisterContextSP reg_ctx_sp(youngest_frame_sp->GetRegisterContext());
1710435933ddSDimitry Andric     if (reg_ctx_sp) {
1711435933ddSDimitry Andric       bool copy_success = reg_ctx_sp->CopyFromRegisterContext(
1712435933ddSDimitry Andric           older_frame_sp->GetRegisterContext());
1713435933ddSDimitry Andric       if (copy_success) {
1714ac7ddfbfSEd Maste         thread->DiscardThreadPlans(true);
1715ac7ddfbfSEd Maste         thread->ClearStackFrames();
1716ac7ddfbfSEd Maste         if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1717435933ddSDimitry Andric           BroadcastEvent(eBroadcastBitStackChanged,
1718435933ddSDimitry Andric                          new ThreadEventData(this->shared_from_this()));
1719435933ddSDimitry Andric       } else {
1720ac7ddfbfSEd Maste         return_error.SetErrorString("Could not reset register values.");
1721ac7ddfbfSEd Maste       }
1722435933ddSDimitry Andric     } else {
1723ac7ddfbfSEd Maste       return_error.SetErrorString("Frame has no register context.");
1724ac7ddfbfSEd Maste     }
1725435933ddSDimitry Andric   } else {
1726ac7ddfbfSEd Maste     return_error.SetErrorString("Returned past top frame.");
1727ac7ddfbfSEd Maste   }
1728ac7ddfbfSEd Maste   return return_error;
1729ac7ddfbfSEd Maste }
1730ac7ddfbfSEd Maste 
DumpAddressList(Stream & s,const std::vector<Address> & list,ExecutionContextScope * exe_scope)1731435933ddSDimitry Andric static void DumpAddressList(Stream &s, const std::vector<Address> &list,
1732435933ddSDimitry Andric                             ExecutionContextScope *exe_scope) {
1733435933ddSDimitry Andric   for (size_t n = 0; n < list.size(); n++) {
173435617911SEd Maste     s << "\t";
1735435933ddSDimitry Andric     list[n].Dump(&s, exe_scope, Address::DumpStyleResolvedDescription,
1736435933ddSDimitry Andric                  Address::DumpStyleSectionNameOffset);
173735617911SEd Maste     s << "\n";
173835617911SEd Maste   }
173935617911SEd Maste }
174035617911SEd Maste 
JumpToLine(const FileSpec & file,uint32_t line,bool can_leave_function,std::string * warnings)17415517e702SDimitry Andric Status Thread::JumpToLine(const FileSpec &file, uint32_t line,
1742435933ddSDimitry Andric                           bool can_leave_function, std::string *warnings) {
174335617911SEd Maste   ExecutionContext exe_ctx(GetStackFrameAtIndex(0));
174435617911SEd Maste   Target *target = exe_ctx.GetTargetPtr();
174535617911SEd Maste   TargetSP target_sp = exe_ctx.GetTargetSP();
174635617911SEd Maste   RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
174735617911SEd Maste   StackFrame *frame = exe_ctx.GetFramePtr();
174835617911SEd Maste   const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
174935617911SEd Maste 
175035617911SEd Maste   // Find candidate locations.
175135617911SEd Maste   std::vector<Address> candidates, within_function, outside_function;
1752435933ddSDimitry Andric   target->GetImages().FindAddressesForLine(target_sp, file, line, sc.function,
1753435933ddSDimitry Andric                                            within_function, outside_function);
175435617911SEd Maste 
17554ba319b5SDimitry Andric   // If possible, we try and stay within the current function. Within a
17564ba319b5SDimitry Andric   // function, we accept multiple locations (optimized code may do this,
17574ba319b5SDimitry Andric   // there's no solution here so we do the best we can). However if we're
17584ba319b5SDimitry Andric   // trying to leave the function, we don't know how to pick the right
17594ba319b5SDimitry Andric   // location, so if there's more than one then we bail.
176035617911SEd Maste   if (!within_function.empty())
176135617911SEd Maste     candidates = within_function;
176235617911SEd Maste   else if (outside_function.size() == 1 && can_leave_function)
176335617911SEd Maste     candidates = outside_function;
176435617911SEd Maste 
176535617911SEd Maste   // Check if we got anything.
1766435933ddSDimitry Andric   if (candidates.empty()) {
1767435933ddSDimitry Andric     if (outside_function.empty()) {
17685517e702SDimitry Andric       return Status("Cannot locate an address for %s:%i.",
176935617911SEd Maste                     file.GetFilename().AsCString(), line);
1770435933ddSDimitry Andric     } else if (outside_function.size() == 1) {
17715517e702SDimitry Andric       return Status("%s:%i is outside the current function.",
177235617911SEd Maste                     file.GetFilename().AsCString(), line);
1773435933ddSDimitry Andric     } else {
177435617911SEd Maste       StreamString sstr;
177535617911SEd Maste       DumpAddressList(sstr, outside_function, target);
17765517e702SDimitry Andric       return Status("%s:%i has multiple candidate locations:\n%s",
1777435933ddSDimitry Andric                     file.GetFilename().AsCString(), line, sstr.GetData());
177835617911SEd Maste     }
177935617911SEd Maste   }
178035617911SEd Maste 
178135617911SEd Maste   // Accept the first location, warn about any others.
178235617911SEd Maste   Address dest = candidates[0];
1783435933ddSDimitry Andric   if (warnings && candidates.size() > 1) {
178435617911SEd Maste     StreamString sstr;
1785435933ddSDimitry Andric     sstr.Printf("%s:%i appears multiple times in this function, selecting the "
1786435933ddSDimitry Andric                 "first location:\n",
178735617911SEd Maste                 file.GetFilename().AsCString(), line);
178835617911SEd Maste     DumpAddressList(sstr, candidates, target);
178935617911SEd Maste     *warnings = sstr.GetString();
179035617911SEd Maste   }
179135617911SEd Maste 
179235617911SEd Maste   if (!reg_ctx->SetPC(dest))
17935517e702SDimitry Andric     return Status("Cannot change PC to target address.");
179435617911SEd Maste 
17955517e702SDimitry Andric   return Status();
179635617911SEd Maste }
179735617911SEd Maste 
DumpUsingSettingsFormat(Stream & strm,uint32_t frame_idx,bool stop_format)1798435933ddSDimitry Andric void Thread::DumpUsingSettingsFormat(Stream &strm, uint32_t frame_idx,
1799435933ddSDimitry Andric                                      bool stop_format) {
1800ac7ddfbfSEd Maste   ExecutionContext exe_ctx(shared_from_this());
1801ac7ddfbfSEd Maste   Process *process = exe_ctx.GetProcessPtr();
18029f2f44ceSEd Maste   if (process == nullptr)
1803ac7ddfbfSEd Maste     return;
1804ac7ddfbfSEd Maste 
1805ac7ddfbfSEd Maste   StackFrameSP frame_sp;
1806ac7ddfbfSEd Maste   SymbolContext frame_sc;
1807435933ddSDimitry Andric   if (frame_idx != LLDB_INVALID_FRAME_ID) {
1808ac7ddfbfSEd Maste     frame_sp = GetStackFrameAtIndex(frame_idx);
1809435933ddSDimitry Andric     if (frame_sp) {
1810ac7ddfbfSEd Maste       exe_ctx.SetFrameSP(frame_sp);
1811ac7ddfbfSEd Maste       frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
1812ac7ddfbfSEd Maste     }
1813ac7ddfbfSEd Maste   }
1814ac7ddfbfSEd Maste 
1815435933ddSDimitry Andric   const FormatEntity::Entry *thread_format;
1816435933ddSDimitry Andric   if (stop_format)
1817435933ddSDimitry Andric     thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadStopFormat();
1818435933ddSDimitry Andric   else
1819435933ddSDimitry Andric     thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
1820435933ddSDimitry Andric 
1821ac7ddfbfSEd Maste   assert(thread_format);
18221c3bbb01SEd Maste 
1823435933ddSDimitry Andric   FormatEntity::Format(*thread_format, strm, frame_sp ? &frame_sc : nullptr,
1824435933ddSDimitry Andric                        &exe_ctx, nullptr, nullptr, false, false);
1825ac7ddfbfSEd Maste }
1826ac7ddfbfSEd Maste 
SettingsInitialize()1827435933ddSDimitry Andric void Thread::SettingsInitialize() {}
1828ac7ddfbfSEd Maste 
SettingsTerminate()1829435933ddSDimitry Andric void Thread::SettingsTerminate() {}
1830ac7ddfbfSEd Maste 
GetThreadPointer()1831435933ddSDimitry Andric lldb::addr_t Thread::GetThreadPointer() { return LLDB_INVALID_ADDRESS; }
183235617911SEd Maste 
GetThreadLocalData(const ModuleSP module,lldb::addr_t tls_file_addr)1833435933ddSDimitry Andric addr_t Thread::GetThreadLocalData(const ModuleSP module,
1834435933ddSDimitry Andric                                   lldb::addr_t tls_file_addr) {
18354ba319b5SDimitry Andric   // The default implementation is to ask the dynamic loader for it. This can
18364ba319b5SDimitry Andric   // be overridden for specific platforms.
183735617911SEd Maste   DynamicLoader *loader = GetProcess()->GetDynamicLoader();
183835617911SEd Maste   if (loader)
1839435933ddSDimitry Andric     return loader->GetThreadLocalData(module, shared_from_this(),
1840435933ddSDimitry Andric                                       tls_file_addr);
184135617911SEd Maste   else
184235617911SEd Maste     return LLDB_INVALID_ADDRESS;
184335617911SEd Maste }
184435617911SEd Maste 
SafeToCallFunctions()1845435933ddSDimitry Andric bool Thread::SafeToCallFunctions() {
18460127ef0fSEd Maste   Process *process = GetProcess().get();
1847435933ddSDimitry Andric   if (process) {
18480127ef0fSEd Maste     SystemRuntime *runtime = process->GetSystemRuntime();
1849435933ddSDimitry Andric     if (runtime) {
18500127ef0fSEd Maste       return runtime->SafeToCallFunctionsOnThisThread(shared_from_this());
18510127ef0fSEd Maste     }
18520127ef0fSEd Maste   }
18530127ef0fSEd Maste   return true;
18540127ef0fSEd Maste }
18550127ef0fSEd Maste 
1856ac7ddfbfSEd Maste lldb::StackFrameSP
GetStackFrameSPForStackFramePtr(StackFrame * stack_frame_ptr)1857435933ddSDimitry Andric Thread::GetStackFrameSPForStackFramePtr(StackFrame *stack_frame_ptr) {
1858ac7ddfbfSEd Maste   return GetStackFrameList()->GetStackFrameSPForStackFramePtr(stack_frame_ptr);
1859ac7ddfbfSEd Maste }
1860ac7ddfbfSEd Maste 
StopReasonAsCString(lldb::StopReason reason)1861435933ddSDimitry Andric const char *Thread::StopReasonAsCString(lldb::StopReason reason) {
1862435933ddSDimitry Andric   switch (reason) {
1863435933ddSDimitry Andric   case eStopReasonInvalid:
1864435933ddSDimitry Andric     return "invalid";
1865435933ddSDimitry Andric   case eStopReasonNone:
1866435933ddSDimitry Andric     return "none";
1867435933ddSDimitry Andric   case eStopReasonTrace:
1868435933ddSDimitry Andric     return "trace";
1869435933ddSDimitry Andric   case eStopReasonBreakpoint:
1870435933ddSDimitry Andric     return "breakpoint";
1871435933ddSDimitry Andric   case eStopReasonWatchpoint:
1872435933ddSDimitry Andric     return "watchpoint";
1873435933ddSDimitry Andric   case eStopReasonSignal:
1874435933ddSDimitry Andric     return "signal";
1875435933ddSDimitry Andric   case eStopReasonException:
1876435933ddSDimitry Andric     return "exception";
1877435933ddSDimitry Andric   case eStopReasonExec:
1878435933ddSDimitry Andric     return "exec";
1879435933ddSDimitry Andric   case eStopReasonPlanComplete:
1880435933ddSDimitry Andric     return "plan complete";
1881435933ddSDimitry Andric   case eStopReasonThreadExiting:
1882435933ddSDimitry Andric     return "thread exiting";
1883435933ddSDimitry Andric   case eStopReasonInstrumentation:
1884435933ddSDimitry Andric     return "instrumentation break";
1885ac7ddfbfSEd Maste   }
1886ac7ddfbfSEd Maste 
1887ac7ddfbfSEd Maste   static char unknown_state_string[64];
1888435933ddSDimitry Andric   snprintf(unknown_state_string, sizeof(unknown_state_string),
1889435933ddSDimitry Andric            "StopReason = %i", reason);
1890ac7ddfbfSEd Maste   return unknown_state_string;
1891ac7ddfbfSEd Maste }
1892ac7ddfbfSEd Maste 
RunModeAsCString(lldb::RunMode mode)1893435933ddSDimitry Andric const char *Thread::RunModeAsCString(lldb::RunMode mode) {
1894435933ddSDimitry Andric   switch (mode) {
1895435933ddSDimitry Andric   case eOnlyThisThread:
1896435933ddSDimitry Andric     return "only this thread";
1897435933ddSDimitry Andric   case eAllThreads:
1898435933ddSDimitry Andric     return "all threads";
1899435933ddSDimitry Andric   case eOnlyDuringStepping:
1900435933ddSDimitry Andric     return "only during stepping";
1901ac7ddfbfSEd Maste   }
1902ac7ddfbfSEd Maste 
1903ac7ddfbfSEd Maste   static char unknown_state_string[64];
1904435933ddSDimitry Andric   snprintf(unknown_state_string, sizeof(unknown_state_string), "RunMode = %i",
1905435933ddSDimitry Andric            mode);
1906ac7ddfbfSEd Maste   return unknown_state_string;
1907ac7ddfbfSEd Maste }
1908ac7ddfbfSEd Maste 
GetStatus(Stream & strm,uint32_t start_frame,uint32_t num_frames,uint32_t num_frames_with_source,bool stop_format,bool only_stacks)1909435933ddSDimitry Andric size_t Thread::GetStatus(Stream &strm, uint32_t start_frame,
1910435933ddSDimitry Andric                          uint32_t num_frames, uint32_t num_frames_with_source,
191124d58133SDimitry Andric                          bool stop_format, bool only_stacks) {
191224d58133SDimitry Andric 
191324d58133SDimitry Andric   if (!only_stacks) {
1914ac7ddfbfSEd Maste     ExecutionContext exe_ctx(shared_from_this());
1915ac7ddfbfSEd Maste     Target *target = exe_ctx.GetTargetPtr();
1916ac7ddfbfSEd Maste     Process *process = exe_ctx.GetProcessPtr();
1917ac7ddfbfSEd Maste     strm.Indent();
1918ac7ddfbfSEd Maste     bool is_selected = false;
1919435933ddSDimitry Andric     if (process) {
1920ac7ddfbfSEd Maste       if (process->GetThreadList().GetSelectedThread().get() == this)
1921ac7ddfbfSEd Maste         is_selected = true;
1922ac7ddfbfSEd Maste     }
1923ac7ddfbfSEd Maste     strm.Printf("%c ", is_selected ? '*' : ' ');
1924435933ddSDimitry Andric     if (target && target->GetDebugger().GetUseExternalEditor()) {
1925ac7ddfbfSEd Maste       StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
1926435933ddSDimitry Andric       if (frame_sp) {
1927435933ddSDimitry Andric         SymbolContext frame_sc(
1928435933ddSDimitry Andric             frame_sp->GetSymbolContext(eSymbolContextLineEntry));
1929435933ddSDimitry Andric         if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file) {
1930435933ddSDimitry Andric           Host::OpenFileInExternalEditor(frame_sc.line_entry.file,
1931435933ddSDimitry Andric                                          frame_sc.line_entry.line);
1932ac7ddfbfSEd Maste         }
1933ac7ddfbfSEd Maste       }
1934ac7ddfbfSEd Maste     }
1935ac7ddfbfSEd Maste 
1936435933ddSDimitry Andric     DumpUsingSettingsFormat(strm, start_frame, stop_format);
193724d58133SDimitry Andric   }
1938ac7ddfbfSEd Maste 
193924d58133SDimitry Andric   size_t num_frames_shown = 0;
1940435933ddSDimitry Andric   if (num_frames > 0) {
1941ac7ddfbfSEd Maste     strm.IndentMore();
1942ac7ddfbfSEd Maste 
1943ac7ddfbfSEd Maste     const bool show_frame_info = true;
194424d58133SDimitry Andric     const bool show_frame_unique = only_stacks;
19459f2f44ceSEd Maste     const char *selected_frame_marker = nullptr;
194624d58133SDimitry Andric     if (num_frames == 1 || only_stacks ||
1947435933ddSDimitry Andric         (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
1948ac7ddfbfSEd Maste       strm.IndentMore();
194935617911SEd Maste     else
195035617911SEd Maste       selected_frame_marker = "* ";
195135617911SEd Maste 
1952435933ddSDimitry Andric     num_frames_shown = GetStackFrameList()->GetStatus(
1953435933ddSDimitry Andric         strm, start_frame, num_frames, show_frame_info, num_frames_with_source,
195424d58133SDimitry Andric         show_frame_unique, selected_frame_marker);
195535617911SEd Maste     if (num_frames == 1)
1956ac7ddfbfSEd Maste       strm.IndentLess();
1957ac7ddfbfSEd Maste     strm.IndentLess();
1958ac7ddfbfSEd Maste   }
1959ac7ddfbfSEd Maste   return num_frames_shown;
1960ac7ddfbfSEd Maste }
1961ac7ddfbfSEd Maste 
GetDescription(Stream & strm,lldb::DescriptionLevel level,bool print_json_thread,bool print_json_stopinfo)1962435933ddSDimitry Andric bool Thread::GetDescription(Stream &strm, lldb::DescriptionLevel level,
1963435933ddSDimitry Andric                             bool print_json_thread, bool print_json_stopinfo) {
1964435933ddSDimitry Andric   const bool stop_format = false;
1965435933ddSDimitry Andric   DumpUsingSettingsFormat(strm, 0, stop_format);
19660127ef0fSEd Maste   strm.Printf("\n");
19670127ef0fSEd Maste 
19680127ef0fSEd Maste   StructuredData::ObjectSP thread_info = GetExtendedInfo();
19690127ef0fSEd Maste 
1970435933ddSDimitry Andric   if (print_json_thread || print_json_stopinfo) {
1971435933ddSDimitry Andric     if (thread_info && print_json_thread) {
19720127ef0fSEd Maste       thread_info->Dump(strm);
19730127ef0fSEd Maste       strm.Printf("\n");
19747aa51b79SEd Maste     }
19757aa51b79SEd Maste 
1976435933ddSDimitry Andric     if (print_json_stopinfo && m_stop_info_sp) {
19771c3bbb01SEd Maste       StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo();
1978435933ddSDimitry Andric       if (stop_info) {
19797aa51b79SEd Maste         stop_info->Dump(strm);
19807aa51b79SEd Maste         strm.Printf("\n");
19817aa51b79SEd Maste       }
19821c3bbb01SEd Maste     }
19837aa51b79SEd Maste 
19840127ef0fSEd Maste     return true;
19850127ef0fSEd Maste   }
19860127ef0fSEd Maste 
1987435933ddSDimitry Andric   if (thread_info) {
1988435933ddSDimitry Andric     StructuredData::ObjectSP activity =
1989435933ddSDimitry Andric         thread_info->GetObjectForDotSeparatedPath("activity");
1990435933ddSDimitry Andric     StructuredData::ObjectSP breadcrumb =
1991435933ddSDimitry Andric         thread_info->GetObjectForDotSeparatedPath("breadcrumb");
1992435933ddSDimitry Andric     StructuredData::ObjectSP messages =
1993435933ddSDimitry Andric         thread_info->GetObjectForDotSeparatedPath("trace_messages");
19940127ef0fSEd Maste 
19950127ef0fSEd Maste     bool printed_activity = false;
1996302affcbSDimitry Andric     if (activity && activity->GetType() == eStructuredDataTypeDictionary) {
19970127ef0fSEd Maste       StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
19980127ef0fSEd Maste       StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
19990127ef0fSEd Maste       StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
2000302affcbSDimitry Andric       if (name && name->GetType() == eStructuredDataTypeString && id &&
2001302affcbSDimitry Andric           id->GetType() == eStructuredDataTypeInteger) {
20025517e702SDimitry Andric         strm.Format("  Activity '{0}', {1:x}\n",
20035517e702SDimitry Andric                     name->GetAsString()->GetValue(),
2004435933ddSDimitry Andric                     id->GetAsInteger()->GetValue());
20050127ef0fSEd Maste       }
20060127ef0fSEd Maste       printed_activity = true;
20070127ef0fSEd Maste     }
20080127ef0fSEd Maste     bool printed_breadcrumb = false;
2009302affcbSDimitry Andric     if (breadcrumb && breadcrumb->GetType() == eStructuredDataTypeDictionary) {
20100127ef0fSEd Maste       if (printed_activity)
20110127ef0fSEd Maste         strm.Printf("\n");
2012435933ddSDimitry Andric       StructuredData::Dictionary *breadcrumb_dict =
2013435933ddSDimitry Andric           breadcrumb->GetAsDictionary();
2014435933ddSDimitry Andric       StructuredData::ObjectSP breadcrumb_text =
2015435933ddSDimitry Andric           breadcrumb_dict->GetValueForKey("name");
2016435933ddSDimitry Andric       if (breadcrumb_text &&
2017302affcbSDimitry Andric           breadcrumb_text->GetType() == eStructuredDataTypeString) {
20185517e702SDimitry Andric         strm.Format("  Current Breadcrumb: {0}\n",
20195517e702SDimitry Andric                     breadcrumb_text->GetAsString()->GetValue());
20200127ef0fSEd Maste       }
20210127ef0fSEd Maste       printed_breadcrumb = true;
20220127ef0fSEd Maste     }
2023302affcbSDimitry Andric     if (messages && messages->GetType() == eStructuredDataTypeArray) {
20240127ef0fSEd Maste       if (printed_breadcrumb)
20250127ef0fSEd Maste         strm.Printf("\n");
20260127ef0fSEd Maste       StructuredData::Array *messages_array = messages->GetAsArray();
20270127ef0fSEd Maste       const size_t msg_count = messages_array->GetSize();
2028435933ddSDimitry Andric       if (msg_count > 0) {
20290127ef0fSEd Maste         strm.Printf("  %zu trace messages:\n", msg_count);
2030435933ddSDimitry Andric         for (size_t i = 0; i < msg_count; i++) {
20310127ef0fSEd Maste           StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
2032302affcbSDimitry Andric           if (message && message->GetType() == eStructuredDataTypeDictionary) {
2033435933ddSDimitry Andric             StructuredData::Dictionary *message_dict =
2034435933ddSDimitry Andric                 message->GetAsDictionary();
2035435933ddSDimitry Andric             StructuredData::ObjectSP message_text =
2036435933ddSDimitry Andric                 message_dict->GetValueForKey("message");
2037435933ddSDimitry Andric             if (message_text &&
2038302affcbSDimitry Andric                 message_text->GetType() == eStructuredDataTypeString) {
20395517e702SDimitry Andric               strm.Format("    {0}\n", message_text->GetAsString()->GetValue());
20400127ef0fSEd Maste             }
20410127ef0fSEd Maste           }
20420127ef0fSEd Maste         }
20430127ef0fSEd Maste       }
20440127ef0fSEd Maste     }
20450127ef0fSEd Maste   }
20460127ef0fSEd Maste 
20470127ef0fSEd Maste   return true;
20480127ef0fSEd Maste }
20490127ef0fSEd Maste 
GetStackFrameStatus(Stream & strm,uint32_t first_frame,uint32_t num_frames,bool show_frame_info,uint32_t num_frames_with_source)2050435933ddSDimitry Andric size_t Thread::GetStackFrameStatus(Stream &strm, uint32_t first_frame,
2051435933ddSDimitry Andric                                    uint32_t num_frames, bool show_frame_info,
2052435933ddSDimitry Andric                                    uint32_t num_frames_with_source) {
2053435933ddSDimitry Andric   return GetStackFrameList()->GetStatus(
2054435933ddSDimitry Andric       strm, first_frame, num_frames, show_frame_info, num_frames_with_source);
2055ac7ddfbfSEd Maste }
2056ac7ddfbfSEd Maste 
GetUnwinder()2057435933ddSDimitry Andric Unwind *Thread::GetUnwinder() {
2058435933ddSDimitry Andric   if (!m_unwinder_ap) {
2059ac7ddfbfSEd Maste     const ArchSpec target_arch(CalculateTarget()->GetArchitecture());
2060ac7ddfbfSEd Maste     const llvm::Triple::ArchType machine = target_arch.GetMachine();
2061435933ddSDimitry Andric     switch (machine) {
2062ac7ddfbfSEd Maste     case llvm::Triple::x86_64:
2063ac7ddfbfSEd Maste     case llvm::Triple::x86:
2064ac7ddfbfSEd Maste     case llvm::Triple::arm:
20650127ef0fSEd Maste     case llvm::Triple::aarch64:
2066ac7ddfbfSEd Maste     case llvm::Triple::thumb:
20671c3bbb01SEd Maste     case llvm::Triple::mips:
20681c3bbb01SEd Maste     case llvm::Triple::mipsel:
206935617911SEd Maste     case llvm::Triple::mips64:
20701c3bbb01SEd Maste     case llvm::Triple::mips64el:
20717aa51b79SEd Maste     case llvm::Triple::ppc:
20727aa51b79SEd Maste     case llvm::Triple::ppc64:
2073acac075bSDimitry Andric     case llvm::Triple::ppc64le:
20744bb0738eSEd Maste     case llvm::Triple::systemz:
20756fcb8242SEd Maste     case llvm::Triple::hexagon:
2076ac7ddfbfSEd Maste       m_unwinder_ap.reset(new UnwindLLDB(*this));
2077ac7ddfbfSEd Maste       break;
2078ac7ddfbfSEd Maste 
2079ac7ddfbfSEd Maste     default:
2080ac7ddfbfSEd Maste       if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2081ac7ddfbfSEd Maste         m_unwinder_ap.reset(new UnwindMacOSXFrameBackchain(*this));
2082ac7ddfbfSEd Maste       break;
2083ac7ddfbfSEd Maste     }
2084ac7ddfbfSEd Maste   }
2085ac7ddfbfSEd Maste   return m_unwinder_ap.get();
2086ac7ddfbfSEd Maste }
2087ac7ddfbfSEd Maste 
Flush()2088435933ddSDimitry Andric void Thread::Flush() {
2089ac7ddfbfSEd Maste   ClearStackFrames();
2090ac7ddfbfSEd Maste   m_reg_context_sp.reset();
2091ac7ddfbfSEd Maste }
2092ac7ddfbfSEd Maste 
IsStillAtLastBreakpointHit()2093435933ddSDimitry Andric bool Thread::IsStillAtLastBreakpointHit() {
2094435933ddSDimitry Andric   // If we are currently stopped at a breakpoint, always return that stopinfo
20954ba319b5SDimitry Andric   // and don't reset it. This allows threads to maintain their breakpoint
20964ba319b5SDimitry Andric   // stopinfo, such as when thread-stepping in multithreaded programs.
2097ac7ddfbfSEd Maste   if (m_stop_info_sp) {
2098ac7ddfbfSEd Maste     StopReason stop_reason = m_stop_info_sp->GetStopReason();
2099ac7ddfbfSEd Maste     if (stop_reason == lldb::eStopReasonBreakpoint) {
2100ac7ddfbfSEd Maste       uint64_t value = m_stop_info_sp->GetValue();
2101ac7ddfbfSEd Maste       lldb::RegisterContextSP reg_ctx_sp(GetRegisterContext());
2102435933ddSDimitry Andric       if (reg_ctx_sp) {
2103ac7ddfbfSEd Maste         lldb::addr_t pc = reg_ctx_sp->GetPC();
2104435933ddSDimitry Andric         BreakpointSiteSP bp_site_sp =
2105435933ddSDimitry Andric             GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
2106435933ddSDimitry Andric         if (bp_site_sp && static_cast<break_id_t>(value) == bp_site_sp->GetID())
2107ac7ddfbfSEd Maste           return true;
2108ac7ddfbfSEd Maste       }
2109ac7ddfbfSEd Maste     }
2110ac7ddfbfSEd Maste   }
2111ac7ddfbfSEd Maste   return false;
2112ac7ddfbfSEd Maste }
211312b93ac6SEd Maste 
StepIn(bool source_step,LazyBool step_in_avoids_code_without_debug_info,LazyBool step_out_avoids_code_without_debug_info)21145517e702SDimitry Andric Status Thread::StepIn(bool source_step,
21150127ef0fSEd Maste                       LazyBool step_in_avoids_code_without_debug_info,
21160127ef0fSEd Maste                       LazyBool step_out_avoids_code_without_debug_info)
211712b93ac6SEd Maste 
211812b93ac6SEd Maste {
21195517e702SDimitry Andric   Status error;
212012b93ac6SEd Maste   Process *process = GetProcess().get();
2121435933ddSDimitry Andric   if (StateIsStoppedState(process->GetState(), true)) {
212212b93ac6SEd Maste     StackFrameSP frame_sp = GetStackFrameAtIndex(0);
212312b93ac6SEd Maste     ThreadPlanSP new_plan_sp;
212412b93ac6SEd Maste     const lldb::RunMode run_mode = eOnlyThisThread;
212512b93ac6SEd Maste     const bool abort_other_plans = false;
212612b93ac6SEd Maste 
2127435933ddSDimitry Andric     if (source_step && frame_sp && frame_sp->HasDebugInformation()) {
212812b93ac6SEd Maste       SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2129435933ddSDimitry Andric       new_plan_sp = QueueThreadPlanForStepInRange(
2130*b5893f02SDimitry Andric           abort_other_plans, sc.line_entry, sc, nullptr, run_mode, error,
21310127ef0fSEd Maste           step_in_avoids_code_without_debug_info,
21320127ef0fSEd Maste           step_out_avoids_code_without_debug_info);
2133435933ddSDimitry Andric     } else {
2134435933ddSDimitry Andric       new_plan_sp = QueueThreadPlanForStepSingleInstruction(
2135*b5893f02SDimitry Andric           false, abort_other_plans, run_mode, error);
213612b93ac6SEd Maste     }
213712b93ac6SEd Maste 
213812b93ac6SEd Maste     new_plan_sp->SetIsMasterPlan(true);
213912b93ac6SEd Maste     new_plan_sp->SetOkayToDiscard(false);
214012b93ac6SEd Maste 
214112b93ac6SEd Maste     // Why do we need to set the current thread by ID here???
214212b93ac6SEd Maste     process->GetThreadList().SetSelectedThreadByID(GetID());
214312b93ac6SEd Maste     error = process->Resume();
2144435933ddSDimitry Andric   } else {
214512b93ac6SEd Maste     error.SetErrorString("process not stopped");
214612b93ac6SEd Maste   }
214712b93ac6SEd Maste   return error;
214812b93ac6SEd Maste }
214912b93ac6SEd Maste 
StepOver(bool source_step,LazyBool step_out_avoids_code_without_debug_info)21505517e702SDimitry Andric Status Thread::StepOver(bool source_step,
2151435933ddSDimitry Andric                         LazyBool step_out_avoids_code_without_debug_info) {
21525517e702SDimitry Andric   Status error;
215312b93ac6SEd Maste   Process *process = GetProcess().get();
2154435933ddSDimitry Andric   if (StateIsStoppedState(process->GetState(), true)) {
215512b93ac6SEd Maste     StackFrameSP frame_sp = GetStackFrameAtIndex(0);
215612b93ac6SEd Maste     ThreadPlanSP new_plan_sp;
215712b93ac6SEd Maste 
215812b93ac6SEd Maste     const lldb::RunMode run_mode = eOnlyThisThread;
215912b93ac6SEd Maste     const bool abort_other_plans = false;
216012b93ac6SEd Maste 
2161435933ddSDimitry Andric     if (source_step && frame_sp && frame_sp->HasDebugInformation()) {
216212b93ac6SEd Maste       SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2163435933ddSDimitry Andric       new_plan_sp = QueueThreadPlanForStepOverRange(
2164*b5893f02SDimitry Andric           abort_other_plans, sc.line_entry, sc, run_mode, error,
21650127ef0fSEd Maste           step_out_avoids_code_without_debug_info);
2166435933ddSDimitry Andric     } else {
2167435933ddSDimitry Andric       new_plan_sp = QueueThreadPlanForStepSingleInstruction(
2168*b5893f02SDimitry Andric           true, abort_other_plans, run_mode, error);
216912b93ac6SEd Maste     }
217012b93ac6SEd Maste 
217112b93ac6SEd Maste     new_plan_sp->SetIsMasterPlan(true);
217212b93ac6SEd Maste     new_plan_sp->SetOkayToDiscard(false);
217312b93ac6SEd Maste 
217412b93ac6SEd Maste     // Why do we need to set the current thread by ID here???
217512b93ac6SEd Maste     process->GetThreadList().SetSelectedThreadByID(GetID());
217612b93ac6SEd Maste     error = process->Resume();
2177435933ddSDimitry Andric   } else {
217812b93ac6SEd Maste     error.SetErrorString("process not stopped");
217912b93ac6SEd Maste   }
218012b93ac6SEd Maste   return error;
218112b93ac6SEd Maste }
218212b93ac6SEd Maste 
StepOut()21835517e702SDimitry Andric Status Thread::StepOut() {
21845517e702SDimitry Andric   Status error;
218512b93ac6SEd Maste   Process *process = GetProcess().get();
2186435933ddSDimitry Andric   if (StateIsStoppedState(process->GetState(), true)) {
218712b93ac6SEd Maste     const bool first_instruction = false;
218812b93ac6SEd Maste     const bool stop_other_threads = false;
218912b93ac6SEd Maste     const bool abort_other_plans = false;
219012b93ac6SEd Maste 
2191435933ddSDimitry Andric     ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut(
2192435933ddSDimitry Andric         abort_other_plans, nullptr, first_instruction, stop_other_threads,
2193*b5893f02SDimitry Andric         eVoteYes, eVoteNoOpinion, 0, error));
219412b93ac6SEd Maste 
219512b93ac6SEd Maste     new_plan_sp->SetIsMasterPlan(true);
219612b93ac6SEd Maste     new_plan_sp->SetOkayToDiscard(false);
219712b93ac6SEd Maste 
219812b93ac6SEd Maste     // Why do we need to set the current thread by ID here???
219912b93ac6SEd Maste     process->GetThreadList().SetSelectedThreadByID(GetID());
220012b93ac6SEd Maste     error = process->Resume();
2201435933ddSDimitry Andric   } else {
220212b93ac6SEd Maste     error.SetErrorString("process not stopped");
220312b93ac6SEd Maste   }
220412b93ac6SEd Maste   return error;
220512b93ac6SEd Maste }
2206*b5893f02SDimitry Andric 
GetCurrentException()2207*b5893f02SDimitry Andric ValueObjectSP Thread::GetCurrentException() {
2208*b5893f02SDimitry Andric   if (auto frame_sp = GetStackFrameAtIndex(0))
2209*b5893f02SDimitry Andric     if (auto recognized_frame = frame_sp->GetRecognizedFrame())
2210*b5893f02SDimitry Andric       if (auto e = recognized_frame->GetExceptionObject())
2211*b5893f02SDimitry Andric         return e;
2212*b5893f02SDimitry Andric 
2213*b5893f02SDimitry Andric   // FIXME: For now, only ObjC exceptions are supported. This should really
2214*b5893f02SDimitry Andric   // iterate over all language runtimes and ask them all to give us the current
2215*b5893f02SDimitry Andric   // exception.
2216*b5893f02SDimitry Andric   if (auto runtime = GetProcess()->GetObjCLanguageRuntime())
2217*b5893f02SDimitry Andric     if (auto e = runtime->GetExceptionObjectForThread(shared_from_this()))
2218*b5893f02SDimitry Andric       return e;
2219*b5893f02SDimitry Andric 
2220*b5893f02SDimitry Andric   return ValueObjectSP();
2221*b5893f02SDimitry Andric }
2222*b5893f02SDimitry Andric 
GetCurrentExceptionBacktrace()2223*b5893f02SDimitry Andric ThreadSP Thread::GetCurrentExceptionBacktrace() {
2224*b5893f02SDimitry Andric   ValueObjectSP exception = GetCurrentException();
2225*b5893f02SDimitry Andric   if (!exception) return ThreadSP();
2226*b5893f02SDimitry Andric 
2227*b5893f02SDimitry Andric   // FIXME: For now, only ObjC exceptions are supported. This should really
2228*b5893f02SDimitry Andric   // iterate over all language runtimes and ask them all to give us the current
2229*b5893f02SDimitry Andric   // exception.
2230*b5893f02SDimitry Andric   auto runtime = GetProcess()->GetObjCLanguageRuntime();
2231*b5893f02SDimitry Andric   if (!runtime) return ThreadSP();
2232*b5893f02SDimitry Andric 
2233*b5893f02SDimitry Andric   return runtime->GetBacktraceThreadFromException(exception);
2234*b5893f02SDimitry Andric }
2235