blob: 3062623af8fc6ab731cb6a6aa9c16a75d5b05c5f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eugene Zelenko8f30a652015-10-23 18:39:37 +000010// C Includes
11// C++ Includes
Greg Clayton04df8ee2016-02-26 19:38:18 +000012#include <mutex>
Eugene Zelenko8f30a652015-10-23 18:39:37 +000013// Other libraries and framework includes
14// Project includes
Jim Ingham1b54c882010-06-16 02:00:15 +000015#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000016#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Log.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000018#include "lldb/Core/FormatEntity.h"
Jason Molenda03c9cd02015-08-06 21:54:29 +000019#include "lldb/Core/Module.h"
Greg Clayton160c9d82013-05-01 21:54:04 +000020#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Stream.h"
22#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000023#include "lldb/Core/RegularExpression.h"
Jim Ingham0d5a2bd2015-09-03 01:40:51 +000024#include "lldb/Core/ValueObject.h"
Greg Clayton7260f622011-04-18 08:33:37 +000025#include "lldb/Host/Host.h"
Jim Ingham4da62062014-01-23 21:52:47 +000026#include "lldb/Interpreter/OptionValueFileSpecList.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000027#include "lldb/Interpreter/OptionValueProperties.h"
28#include "lldb/Interpreter/Property.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000029#include "lldb/Symbol/Function.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000030#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Target/DynamicLoader.h"
32#include "lldb/Target/ExecutionContext.h"
33#include "lldb/Target/Process.h"
34#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000035#include "lldb/Target/StopInfo.h"
Jason Molendab4892cd2014-05-13 22:02:48 +000036#include "lldb/Target/SystemRuntime.h"
Greg Clayton896dff62010-07-23 16:45:51 +000037#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "lldb/Target/Thread.h"
39#include "lldb/Target/ThreadPlan.h"
40#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Target/ThreadPlanBase.h"
Jim Ingham2bdbfd52014-09-29 23:17:18 +000042#include "lldb/Target/ThreadPlanPython.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Target/ThreadPlanStepInstruction.h"
44#include "lldb/Target/ThreadPlanStepOut.h"
45#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
46#include "lldb/Target/ThreadPlanStepThrough.h"
47#include "lldb/Target/ThreadPlanStepInRange.h"
48#include "lldb/Target/ThreadPlanStepOverRange.h"
49#include "lldb/Target/ThreadPlanRunToAddress.h"
50#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000051#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000052#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000053#include "Plugins/Process/Utility/UnwindLLDB.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000054#include "Plugins/Process/Utility/UnwindMacOSXFrameBackchain.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000055
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056using namespace lldb;
57using namespace lldb_private;
58
Greg Clayton67cc0632012-08-22 17:17:09 +000059const ThreadPropertiesSP &
60Thread::GetGlobalProperties()
61{
Greg Claytoncc2e27f2016-02-26 23:20:08 +000062 // NOTE: intentional leak so we don't crash if global destructor chain gets
63 // called as other threads still use the result of this function
64 static ThreadPropertiesSP *g_settings_sp_ptr = nullptr;
Greg Clayton04df8ee2016-02-26 19:38:18 +000065 static std::once_flag g_once_flag;
66 std::call_once(g_once_flag, []() {
Greg Claytoncc2e27f2016-02-26 23:20:08 +000067 g_settings_sp_ptr = new ThreadPropertiesSP(new ThreadProperties (true));
Greg Clayton04df8ee2016-02-26 19:38:18 +000068 });
Greg Claytoncc2e27f2016-02-26 23:20:08 +000069 return *g_settings_sp_ptr;
Greg Clayton67cc0632012-08-22 17:17:09 +000070}
71
72static PropertyDefinition
73g_properties[] =
74{
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000075 { "step-in-avoid-nodebug", OptionValue::eTypeBoolean, true, true, nullptr, nullptr, "If true, step-in will not stop in functions with no debug information." },
76 { "step-out-avoid-nodebug", OptionValue::eTypeBoolean, true, false, nullptr, nullptr, "If true, when step-in/step-out/step-over leave the current frame, they will continue to step out till they come to a function with "
Jim Ingham4b4b2472014-03-13 02:47:14 +000077 "debug information. Passing a frame argument to step-out will override this option." },
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000078 { "step-avoid-regexp", OptionValue::eTypeRegex , true , 0, "^std::", nullptr, "A regular expression defining functions step-in won't stop in." },
79 { "step-avoid-libraries", OptionValue::eTypeFileSpecList , true , 0, nullptr, nullptr, "A list of libraries that source stepping won't stop in." },
80 { "trace-thread", OptionValue::eTypeBoolean, false, false, nullptr, nullptr, "If true, this thread will single-step and log execution." },
81 { nullptr , OptionValue::eTypeInvalid, false, 0 , nullptr, nullptr, nullptr }
Greg Clayton67cc0632012-08-22 17:17:09 +000082};
83
84enum {
Jim Ingham4b4b2472014-03-13 02:47:14 +000085 ePropertyStepInAvoidsNoDebug,
86 ePropertyStepOutAvoidsNoDebug,
Greg Clayton67cc0632012-08-22 17:17:09 +000087 ePropertyStepAvoidRegex,
Jim Ingham4da62062014-01-23 21:52:47 +000088 ePropertyStepAvoidLibraries,
Greg Clayton67cc0632012-08-22 17:17:09 +000089 ePropertyEnableThreadTrace
90};
91
Greg Clayton67cc0632012-08-22 17:17:09 +000092class ThreadOptionValueProperties : public OptionValueProperties
93{
94public:
95 ThreadOptionValueProperties (const ConstString &name) :
96 OptionValueProperties (name)
97 {
98 }
99
100 // This constructor is used when creating ThreadOptionValueProperties when it
101 // is part of a new lldb_private::Thread instance. It will copy all current
102 // global property values as needed
103 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +0000104 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +0000105 {
106 }
107
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000108 const Property *
109 GetPropertyAtIndex(const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const override
Greg Clayton67cc0632012-08-22 17:17:09 +0000110 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000111 // When getting the value for a key from the thread options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +0000112 // try and grab the setting from the current thread if there is one. Else we just
113 // use the one from this instance.
114 if (exe_ctx)
115 {
116 Thread *thread = exe_ctx->GetThreadPtr();
117 if (thread)
118 {
119 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
120 if (this != instance_properties)
121 return instance_properties->ProtectedGetPropertyAtIndex (idx);
122 }
123 }
124 return ProtectedGetPropertyAtIndex (idx);
125 }
126};
127
Greg Clayton67cc0632012-08-22 17:17:09 +0000128ThreadProperties::ThreadProperties (bool is_global) :
129 Properties ()
130{
131 if (is_global)
132 {
133 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
134 m_collection_sp->Initialize(g_properties);
135 }
136 else
137 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
138}
139
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000140ThreadProperties::~ThreadProperties() = default;
Greg Clayton67cc0632012-08-22 17:17:09 +0000141
142const RegularExpression *
143ThreadProperties::GetSymbolsToAvoidRegexp()
144{
145 const uint32_t idx = ePropertyStepAvoidRegex;
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000146 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000147}
148
Jim Ingham4da62062014-01-23 21:52:47 +0000149FileSpecList &
150ThreadProperties::GetLibrariesToAvoid() const
151{
152 const uint32_t idx = ePropertyStepAvoidLibraries;
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000153 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Jim Ingham4da62062014-01-23 21:52:47 +0000154 assert(option_value);
155 return option_value->GetCurrentValue();
156}
157
Greg Clayton67cc0632012-08-22 17:17:09 +0000158bool
159ThreadProperties::GetTraceEnabledState() const
160{
161 const uint32_t idx = ePropertyEnableThreadTrace;
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000162 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000163}
164
Jim Ingham4b4b2472014-03-13 02:47:14 +0000165bool
166ThreadProperties::GetStepInAvoidsNoDebug() const
167{
168 const uint32_t idx = ePropertyStepInAvoidsNoDebug;
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000169 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham4b4b2472014-03-13 02:47:14 +0000170}
171
172bool
173ThreadProperties::GetStepOutAvoidsNoDebug() const
174{
175 const uint32_t idx = ePropertyStepOutAvoidsNoDebug;
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000176 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham4b4b2472014-03-13 02:47:14 +0000177}
178
Jim Ingham4f465cf2012-10-10 18:32:14 +0000179//------------------------------------------------------------------
180// Thread Event Data
181//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000182
Jim Ingham4f465cf2012-10-10 18:32:14 +0000183const ConstString &
184Thread::ThreadEventData::GetFlavorString ()
185{
186 static ConstString g_flavor ("Thread::ThreadEventData");
187 return g_flavor;
188}
189
190Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
191 m_thread_sp (thread_sp),
192 m_stack_id ()
193{
194}
195
196Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
197 m_thread_sp (thread_sp),
198 m_stack_id (stack_id)
199{
200}
201
202Thread::ThreadEventData::ThreadEventData () :
203 m_thread_sp (),
204 m_stack_id ()
205{
206}
207
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000208Thread::ThreadEventData::~ThreadEventData() = default;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000209
210void
211Thread::ThreadEventData::Dump (Stream *s) const
212{
Jim Ingham4f465cf2012-10-10 18:32:14 +0000213}
214
215const Thread::ThreadEventData *
216Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
217{
218 if (event_ptr)
219 {
220 const EventData *event_data = event_ptr->GetData();
221 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
222 return static_cast <const ThreadEventData *> (event_ptr->GetData());
223 }
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000224 return nullptr;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000225}
226
227ThreadSP
228Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
229{
230 ThreadSP thread_sp;
231 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
232 if (event_data)
233 thread_sp = event_data->GetThread();
234 return thread_sp;
235}
236
237StackID
238Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
239{
240 StackID stack_id;
241 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
242 if (event_data)
243 stack_id = event_data->GetStackID();
244 return stack_id;
245}
246
Jason Molendab57e4a12013-11-04 09:33:30 +0000247StackFrameSP
Jim Ingham4f465cf2012-10-10 18:32:14 +0000248Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
249{
250 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
Jason Molendab57e4a12013-11-04 09:33:30 +0000251 StackFrameSP frame_sp;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000252 if (event_data)
253 {
254 ThreadSP thread_sp = event_data->GetThread();
255 if (thread_sp)
256 {
257 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
258 }
259 }
260 return frame_sp;
261}
262
263//------------------------------------------------------------------
264// Thread class
265//------------------------------------------------------------------
266
267ConstString &
268Thread::GetStaticBroadcasterClass ()
269{
270 static ConstString class_name ("lldb.thread");
271 return class_name;
272}
273
Jason Molendaa8ff5432014-03-06 06:31:18 +0000274Thread::Thread (Process &process, lldb::tid_t tid, bool use_invalid_index_id) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000275 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276 UserID (tid),
Jim Ingham583bbb12016-03-07 21:50:25 +0000277 Broadcaster(process.GetTarget().GetDebugger().GetBroadcasterManager(), Thread::GetStaticBroadcasterClass().AsCString()),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000278 m_process_wp (process.shared_from_this()),
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000279 m_stop_info_sp (),
280 m_stop_info_stop_id (0),
Greg Claytona97c4d22014-12-09 23:31:02 +0000281 m_stop_info_override_stop_id (0),
Jason Molendaa8ff5432014-03-06 06:31:18 +0000282 m_index_id (use_invalid_index_id ? LLDB_INVALID_INDEX32 : process.GetNextThreadIndexID(tid)),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000285 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000288 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000289 m_curr_frames_sp (),
290 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000292 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000293 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000294 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000295 m_destroy_called (false),
Jason Molenda705b1802014-06-13 02:37:02 +0000296 m_override_should_notify (eLazyBoolCalculate),
297 m_extended_info_fetched (false),
298 m_extended_info ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299{
Greg Clayton5160ce52013-03-27 23:08:40 +0000300 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000302 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")",
303 static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304
Jim Ingham4f465cf2012-10-10 18:32:14 +0000305 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306 QueueFundamentalPlan(true);
307}
308
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309Thread::~Thread()
310{
Greg Clayton5160ce52013-03-27 23:08:40 +0000311 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000313 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")",
314 static_cast<void*>(this), GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000315 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
316 assert (m_destroy_called);
317}
318
319void
320Thread::DestroyThread ()
321{
Jim Inghamb1499242013-10-18 17:11:02 +0000322 // Tell any plans on the plan stacks that the thread is being destroyed since
323 // any plans that have a thread go away in the middle of might need
324 // to do cleanup, or in some cases NOT do cleanup...
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000325 for (auto plan : m_plan_stack)
326 plan->ThreadDestroyed();
327
Jim Inghamb1499242013-10-18 17:11:02 +0000328 for (auto plan : m_discarded_plan_stack)
329 plan->ThreadDestroyed();
330
331 for (auto plan : m_completed_plan_stack)
332 plan->ThreadDestroyed();
333
Greg Clayton35a4cc52012-10-29 20:52:08 +0000334 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000335 m_plan_stack.clear();
336 m_discarded_plan_stack.clear();
337 m_completed_plan_stack.clear();
Greg Clayton6e10f142013-07-30 00:23:06 +0000338
339 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the
340 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
341 // without checking first whether it is destroyed, they won't crash.
342 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
343 m_plan_stack.push_back (null_plan_sp);
344
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000345 m_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000346 m_reg_context_sp.reset();
347 m_unwinder_ap.reset();
348 Mutex::Locker locker(m_frame_mutex);
349 m_curr_frames_sp.reset();
350 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351}
352
Jim Ingham4f465cf2012-10-10 18:32:14 +0000353void
354Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
355{
356 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
357 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
358}
359
Jason Molendaef7d6412015-08-06 03:27:10 +0000360lldb::StackFrameSP
361Thread::GetSelectedFrame()
362{
363 StackFrameListSP stack_frame_list_sp(GetStackFrameList());
364 StackFrameSP frame_sp = stack_frame_list_sp->GetFrameAtIndex (stack_frame_list_sp->GetSelectedFrameIndex());
365 FunctionOptimizationWarning (frame_sp.get());
366 return frame_sp;
367}
368
Jim Ingham4f465cf2012-10-10 18:32:14 +0000369uint32_t
Jason Molendab57e4a12013-11-04 09:33:30 +0000370Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
Jim Ingham4f465cf2012-10-10 18:32:14 +0000371{
372 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
373 if (broadcast)
374 BroadcastSelectedFrameChange(frame->GetStackID());
Jason Molendaef7d6412015-08-06 03:27:10 +0000375 FunctionOptimizationWarning (frame);
Jim Ingham4f465cf2012-10-10 18:32:14 +0000376 return ret_value;
377}
378
379bool
380Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
381{
Jason Molendab57e4a12013-11-04 09:33:30 +0000382 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
Jim Ingham4f465cf2012-10-10 18:32:14 +0000383 if (frame_sp)
384 {
385 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
386 if (broadcast)
387 BroadcastSelectedFrameChange(frame_sp->GetStackID());
Jason Molendaef7d6412015-08-06 03:27:10 +0000388 FunctionOptimizationWarning (frame_sp.get());
Jim Ingham4f465cf2012-10-10 18:32:14 +0000389 return true;
390 }
391 else
392 return false;
393}
394
Jim Ingham93208b82013-01-31 21:46:01 +0000395bool
396Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
397{
398 const bool broadcast = true;
399 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
400 if (success)
401 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000402 StackFrameSP frame_sp = GetSelectedFrame();
Jim Ingham93208b82013-01-31 21:46:01 +0000403 if (frame_sp)
404 {
405 bool already_shown = false;
406 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
407 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
408 {
409 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
410 }
411
412 bool show_frame_info = true;
413 bool show_source = !already_shown;
Jason Molendaef7d6412015-08-06 03:27:10 +0000414 FunctionOptimizationWarning (frame_sp.get());
Jim Ingham93208b82013-01-31 21:46:01 +0000415 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
416 }
417 return false;
418 }
419 else
420 return false;
421}
422
Jason Molendaef7d6412015-08-06 03:27:10 +0000423void
424Thread::FunctionOptimizationWarning (StackFrame *frame)
425{
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000426 if (frame && frame->HasDebugInformation() && GetProcess()->GetWarningsOptimization())
Jason Molendaef7d6412015-08-06 03:27:10 +0000427 {
Jason Molenda03c9cd02015-08-06 21:54:29 +0000428 SymbolContext sc = frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextModule);
Jason Molenda484900b2015-08-10 07:55:25 +0000429 GetProcess()->PrintWarningOptimization (sc);
Jason Molendaef7d6412015-08-06 03:27:10 +0000430 }
431}
432
Jim Inghamb15bfc72010-10-20 00:39:53 +0000433lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000434Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435{
Greg Clayton6e10f142013-07-30 00:23:06 +0000436 if (m_destroy_called)
437 return m_stop_info_sp;
438
Jim Inghamb15bfc72010-10-20 00:39:53 +0000439 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000440 ProcessSP process_sp (GetProcess());
441 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000442 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000443 {
Jim Ingham30fadaf2014-07-08 01:07:32 +0000444 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject(), GetExpressionVariable());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000445 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000446 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000447 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000448 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
449 (m_stop_info_sp && m_stop_info_sp->IsValid())) // Stop info is valid, just return what we have
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000450 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000451 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000452 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000453 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000454 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000455 GetPrivateStopInfo ();
456 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000457 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000458 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459}
460
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000461lldb::StopInfoSP
462Thread::GetPrivateStopInfo ()
463{
Greg Clayton6e10f142013-07-30 00:23:06 +0000464 if (m_destroy_called)
465 return m_stop_info_sp;
466
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000467 ProcessSP process_sp (GetProcess());
468 if (process_sp)
469 {
Daniel Malea246cb612013-05-14 15:20:12 +0000470 const uint32_t process_stop_id = process_sp->GetStopID();
471 if (m_stop_info_stop_id != process_stop_id)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000472 {
Daniel Malea246cb612013-05-14 15:20:12 +0000473 if (m_stop_info_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000474 {
Daniel Malea246cb612013-05-14 15:20:12 +0000475 if (m_stop_info_sp->IsValid()
476 || IsStillAtLastBreakpointHit()
477 || GetCurrentPlan()->IsVirtualStep())
478 SetStopInfo (m_stop_info_sp);
479 else
480 m_stop_info_sp.reset();
481 }
482
483 if (!m_stop_info_sp)
484 {
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000485 if (!CalculateStopInfo())
Daniel Malea246cb612013-05-14 15:20:12 +0000486 SetStopInfo (StopInfoSP());
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000487 }
488 }
Greg Claytona97c4d22014-12-09 23:31:02 +0000489
490 // The stop info can be manually set by calling Thread::SetStopInfo()
491 // prior to this function ever getting called, so we can't rely on
492 // "m_stop_info_stop_id != process_stop_id" as the condition for
493 // the if statement below, we must also check the stop info to see
494 // if we need to override it. See the header documentation in
495 // Process::GetStopInfoOverrideCallback() for more information on
496 // the stop info override callback.
497 if (m_stop_info_override_stop_id != process_stop_id)
498 {
499 m_stop_info_override_stop_id = process_stop_id;
500 if (m_stop_info_sp)
501 {
502 ArchSpec::StopInfoOverrideCallbackType callback = GetProcess()->GetStopInfoOverrideCallback();
503 if (callback)
504 callback(*this);
505 }
506 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000507 }
508 return m_stop_info_sp;
509}
510
Greg Clayton97d5cf02012-09-25 02:40:06 +0000511lldb::StopReason
512Thread::GetStopReason()
513{
514 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
515 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000516 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000517 return eStopReasonNone;
518}
519
Greg Clayton2e309072015-07-17 23:42:28 +0000520bool
521Thread::StopInfoIsUpToDate() const
522{
523 ProcessSP process_sp (GetProcess());
524 if (process_sp)
525 return m_stop_info_stop_id == process_sp->GetStopID();
526 else
527 return true; // Process is no longer around so stop info is always up to date...
528}
Greg Clayton97d5cf02012-09-25 02:40:06 +0000529
Jim Ingham77787032011-01-20 02:03:18 +0000530void
531Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
532{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000533 m_stop_info_sp = stop_info_sp;
534 if (m_stop_info_sp)
Jim Ingham221d51c2013-05-08 00:35:16 +0000535 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000536 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham221d51c2013-05-08 00:35:16 +0000537 // If we are overriding the ShouldReportStop, do that here:
538 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000539 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000540 }
541
Greg Clayton1ac04c32012-02-21 00:09:25 +0000542 ProcessSP process_sp (GetProcess());
543 if (process_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000544 m_stop_info_stop_id = process_sp->GetStopID();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000545 else
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000546 m_stop_info_stop_id = UINT32_MAX;
Greg Clayton8cda7f02013-05-21 21:55:59 +0000547 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
548 if (log)
Ed Maste2bc76432014-06-25 00:38:35 +0000549 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000550 static_cast<void*>(this), GetID(),
551 stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>",
552 m_stop_info_stop_id);
Jim Ingham77787032011-01-20 02:03:18 +0000553}
554
555void
Jim Ingham221d51c2013-05-08 00:35:16 +0000556Thread::SetShouldReportStop (Vote vote)
557{
558 if (vote == eVoteNoOpinion)
559 return;
560 else
561 {
562 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000563 if (m_stop_info_sp)
564 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000565 }
566}
567
568void
Jim Ingham77787032011-01-20 02:03:18 +0000569Thread::SetStopInfoToNothing()
570{
571 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
572 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
573 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
574}
575
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576bool
577Thread::ThreadStoppedForAReason (void)
578{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000579 return (bool) GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580}
581
Jim Ingham77787032011-01-20 02:03:18 +0000582bool
583Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
584{
Greg Claytonf74cf862013-11-13 23:28:31 +0000585 saved_state.register_backup_sp.reset();
586 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
587 if (frame_sp)
588 {
589 lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
590 if (reg_checkpoint_sp)
591 {
592 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
593 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp))
594 saved_state.register_backup_sp = reg_checkpoint_sp;
595 }
596 }
597 if (!saved_state.register_backup_sp)
Jim Ingham77787032011-01-20 02:03:18 +0000598 return false;
599
600 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000601 ProcessSP process_sp (GetProcess());
602 if (process_sp)
603 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000604 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
605
Jim Ingham77787032011-01-20 02:03:18 +0000606 return true;
607}
608
609bool
Jim Ingham8559a352012-11-26 23:52:18 +0000610Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000611{
Greg Claytonf74cf862013-11-13 23:28:31 +0000612 if (saved_state.register_backup_sp)
613 {
614 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
615 if (frame_sp)
616 {
617 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
618 if (reg_ctx_sp)
619 {
620 bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp);
621
622 // Clear out all stack frames as our world just changed.
623 ClearStackFrames();
624 reg_ctx_sp->InvalidateIfNeeded(true);
625 if (m_unwinder_ap.get())
626 m_unwinder_ap->Clear();
627 return ret;
628 }
629 }
630 }
631 return false;
Jim Ingham8559a352012-11-26 23:52:18 +0000632}
633
634bool
635Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
636{
Jim Ingham0c270682011-01-25 02:47:23 +0000637 if (saved_state.stop_info_sp)
638 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000639 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000640 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000641 return true;
642}
643
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644StateType
645Thread::GetState() const
646{
647 // If any other threads access this we will need a mutex for it
648 Mutex::Locker locker(m_state_mutex);
649 return m_state;
650}
651
652void
653Thread::SetState(StateType state)
654{
655 Mutex::Locker locker(m_state_mutex);
656 m_state = state;
657}
658
659void
660Thread::WillStop()
661{
662 ThreadPlan *current_plan = GetCurrentPlan();
663
664 // FIXME: I may decide to disallow threads with no plans. In which
665 // case this should go to an assert.
666
667 if (!current_plan)
668 return;
669
670 current_plan->WillStop();
671}
672
673void
674Thread::SetupForResume ()
675{
676 if (GetResumeState() != eStateSuspended)
677 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
679 // telling the current plan it will resume, since we might change what the current
680 // plan is.
681
Greg Clayton1afa68e2013-04-02 20:32:37 +0000682 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
683 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 {
Greg Claytona97c4d22014-12-09 23:31:02 +0000685 const addr_t thread_pc = reg_ctx_sp->GetPC();
686 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000687 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000688 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000689 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
690 // special to step over a breakpoint.
691
692 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000693
Greg Claytona97c4d22014-12-09 23:31:02 +0000694 bool push_step_over_bp_plan = false;
695 if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint)
696 {
697 ThreadPlanStepOverBreakpoint *bp_plan = (ThreadPlanStepOverBreakpoint *)cur_plan;
698 if (bp_plan->GetBreakpointLoadAddress() != thread_pc)
699 push_step_over_bp_plan = true;
700 }
701 else
702 push_step_over_bp_plan = true;
703
704 if (push_step_over_bp_plan)
Greg Clayton1afa68e2013-04-02 20:32:37 +0000705 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000706 ThreadPlanSP step_bp_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
707 if (step_bp_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000709 step_bp_plan_sp->SetPrivate (true);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000710
711 if (GetCurrentPlan()->RunState() != eStateStepping)
712 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000713 ThreadPlanStepOverBreakpoint *step_bp_plan
714 = static_cast<ThreadPlanStepOverBreakpoint *>(step_bp_plan_sp.get());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000715 step_bp_plan->SetAutoContinue(true);
716 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000717 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 }
720 }
721 }
722 }
723}
724
725bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000726Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727{
728 // At this point clear the completed plan stack.
729 m_completed_plan_stack.clear();
730 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000731 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732
Jim Ingham569aaf92015-11-06 22:45:57 +0000733 StateType prev_resume_state = GetTemporaryResumeState();
734
735 SetTemporaryResumeState(resume_state);
Jim Ingham92087d82012-01-31 23:09:20 +0000736
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000737 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
738 if (backing_thread_sp)
Jim Ingham569aaf92015-11-06 22:45:57 +0000739 backing_thread_sp->SetTemporaryResumeState(resume_state);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000740
Jim Ingham569aaf92015-11-06 22:45:57 +0000741 // Make sure m_stop_info_sp is valid. Don't do this for threads we suspended in the previous run.
742 if (prev_resume_state != eStateSuspended)
743 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000744
Jim Ingham92087d82012-01-31 23:09:20 +0000745 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
746 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
747 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
748 // about the fact that we are resuming...
Jim Inghamacbea8f2015-06-02 20:26:13 +0000749 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000750 if (m_stop_info_stop_id == process_stop_id &&
751 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000752 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000753 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000754 if (stop_info)
755 stop_info->WillResume (resume_state);
756 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757
758 // Tell all the plans that we are about to resume in case they need to clear any state.
759 // We distinguish between the plan on the top of the stack and the lower
760 // plans in case a plan needs to do any special business before it runs.
761
Greg Claytond1d06e42013-04-20 00:27:58 +0000762 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000764 if (plan_ptr)
765 {
766 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000767
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000768 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != nullptr)
Greg Claytond1d06e42013-04-20 00:27:58 +0000769 {
770 plan_ptr->WillResume (resume_state, false);
771 }
772
773 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
774 // In that case, don't reset it here.
775
776 if (need_to_resume && resume_state != eStateSuspended)
777 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000778 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000779 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000780 }
781
Greg Clayton160c9d82013-05-01 21:54:04 +0000782 if (need_to_resume)
783 {
784 ClearStackFrames();
785 // Let Thread subclasses do any special work they need to prior to resuming
786 WillResume (resume_state);
787 }
788
Jim Ingham513c6bb2012-09-01 01:02:41 +0000789 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790}
791
792void
793Thread::DidResume ()
794{
795 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
796}
797
Andrew Kaylor29d65742013-05-10 17:19:04 +0000798void
799Thread::DidStop ()
800{
801 SetState (eStateStopped);
802}
803
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804bool
805Thread::ShouldStop (Event* event_ptr)
806{
807 ThreadPlan *current_plan = GetCurrentPlan();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000808
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000809 bool should_stop = true;
810
Greg Clayton5160ce52013-03-27 23:08:40 +0000811 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000812
Jim Ingham92087d82012-01-31 23:09:20 +0000813 if (GetResumeState () == eStateSuspended)
814 {
815 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000816 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000817 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000818 return false;
819 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000820
Jim Ingham92087d82012-01-31 23:09:20 +0000821 if (GetTemporaryResumeState () == eStateSuspended)
822 {
823 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000824 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000825 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000826 return false;
827 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000828
Daniel Malea246cb612013-05-14 15:20:12 +0000829 // Based on the current thread plan and process stop info, check if this
830 // thread caused the process to stop. NOTE: this must take place before
831 // the plan is moved from the current plan stack to the completed plan
832 // stack.
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000833 if (!ThreadStoppedForAReason())
Jim Ingham92087d82012-01-31 23:09:20 +0000834 {
835 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000836 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since no stop reason)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000837 __FUNCTION__, GetID (), GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000838 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000839 return false;
840 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000841
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000842 if (log)
843 {
Jim Inghamdee1bc92013-06-22 00:27:45 +0000844 log->Printf ("Thread::%s(%p) for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000845 __FUNCTION__, static_cast<void*>(this), GetID (),
Greg Clayton160c9d82013-05-01 21:54:04 +0000846 GetProtocolID (),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000847 GetRegisterContext()
848 ? GetRegisterContext()->GetPC()
849 : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000850 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000852 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000853 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000854 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000856
Jim Ingham06e827c2010-11-11 19:26:09 +0000857 // The top most plan always gets to do the trace log...
858 current_plan->DoTraceLog ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000859
Jim Ingham6d66ce62012-04-20 21:16:56 +0000860 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
861 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
862 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000863 StopInfoSP private_stop_info (GetPrivateStopInfo());
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000864 if (private_stop_info && !private_stop_info->ShouldStopSynchronous(event_ptr))
Jim Ingham6d66ce62012-04-20 21:16:56 +0000865 {
866 if (log)
867 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
868 return false;
869 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000870
Jim Inghambad39e42012-09-05 21:12:49 +0000871 // If we've already been restarted, don't query the plans since the state they would examine is not current.
872 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
873 return false;
874
875 // Before the plans see the state of the world, calculate the current inlined depth.
876 GetStackFrameList()->CalculateCurrentInlinedDepth();
877
Jim Ingham25f66702011-12-03 01:52:59 +0000878 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
879 // If that plan is still working, then we don't need to do any more work. If the plan that explains
880 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
881 // whether they still need to do more work.
882
883 bool done_processing_current_plan = false;
884
Jim Ingham0161b492013-02-09 01:29:05 +0000885 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000886 {
887 if (current_plan->TracerExplainsStop())
888 {
889 done_processing_current_plan = true;
890 should_stop = false;
891 }
892 else
893 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000894 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000895 // does and let it handle the situation.
896 ThreadPlan *plan_ptr = current_plan;
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000897 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != nullptr)
Jim Ingham25f66702011-12-03 01:52:59 +0000898 {
Jim Ingham0161b492013-02-09 01:29:05 +0000899 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000900 {
901 should_stop = plan_ptr->ShouldStop (event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000902
Jim Ingham25f66702011-12-03 01:52:59 +0000903 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
904 // and all the plans below it off the stack.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000905
Jim Ingham25f66702011-12-03 01:52:59 +0000906 if (plan_ptr->MischiefManaged())
907 {
Jim Ingham031177e2012-05-11 23:49:49 +0000908 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000909 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000910
Jim Ingham25f66702011-12-03 01:52:59 +0000911 do
912 {
913 if (should_stop)
914 current_plan->WillStop();
915 PopPlan();
916 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000917 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
918 // Now, if the responsible plan was not "Okay to discard" then we're done,
919 // otherwise we forward this to the next plan in the stack below.
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000920 done_processing_current_plan = (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard());
Jim Ingham25f66702011-12-03 01:52:59 +0000921 }
922 else
923 done_processing_current_plan = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000924
Jim Ingham25f66702011-12-03 01:52:59 +0000925 break;
926 }
Jim Ingham25f66702011-12-03 01:52:59 +0000927 }
928 }
929 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000930
Jim Ingham25f66702011-12-03 01:52:59 +0000931 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000933 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000934
Jim Ingham10c4b242011-10-15 00:23:43 +0000935 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000936 log->Printf("Plan %s explains stop, auto-continue %i.",
937 current_plan->GetName(), over_ride_stop);
938
Jim Ingham0f16e732011-02-08 05:20:59 +0000939 // We're starting from the base plan, so just let it decide;
940 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000942 should_stop = current_plan->ShouldStop (event_ptr);
943 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000944 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000945 }
946 else
947 {
948 // Otherwise, don't let the base plan override what the other plans say to do, since
949 // presumably if there were other plans they would know what to do...
950 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000952 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000953 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000954
Jim Ingham0f16e732011-02-08 05:20:59 +0000955 should_stop = current_plan->ShouldStop(event_ptr);
956 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000957 log->Printf("Plan %s should stop: %d.",
958 current_plan->GetName(), should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000959 if (current_plan->MischiefManaged())
960 {
961 if (should_stop)
962 current_plan->WillStop();
963
964 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
965 // Otherwise, see if the plan's parent wants to stop.
966
967 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
968 {
969 PopPlan();
970 break;
971 }
972 else
973 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000974 PopPlan();
975
976 current_plan = GetCurrentPlan();
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000977 if (current_plan == nullptr)
Jim Ingham0f16e732011-02-08 05:20:59 +0000978 {
979 break;
980 }
981 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000982 }
983 else
984 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000985 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000987 }
988 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000989
Jim Inghamb01e7422010-06-19 04:45:32 +0000990 if (over_ride_stop)
991 should_stop = false;
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000992 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000993
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000994 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
995 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
996 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
997 // This code clears stale plans off the stack.
998
999 if (should_stop)
1000 {
1001 ThreadPlan *plan_ptr = GetCurrentPlan();
1002 while (!PlanIsBasePlan(plan_ptr))
Jim Ingham64e7ead2012-05-03 21:19:36 +00001003 {
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001004 bool stale = plan_ptr->IsPlanStale ();
1005 ThreadPlan *examined_plan = plan_ptr;
1006 plan_ptr = GetPreviousPlan (examined_plan);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001007
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001008 if (stale)
1009 {
1010 if (log)
1011 log->Printf("Plan %s being discarded in cleanup, it says it is already done.",
1012 examined_plan->GetName());
1013 DiscardThreadPlansUpToPlan(examined_plan);
Jim Ingham64e7ead2012-05-03 21:19:36 +00001014 }
1015 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017
Jim Ingham10c4b242011-10-15 00:23:43 +00001018 if (log)
1019 {
1020 StreamString s;
1021 s.IndentMore();
1022 DumpThreadPlans(&s);
1023 log->Printf ("Plan stack final state:\n%s", s.GetData());
1024 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
1025 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026 return should_stop;
1027}
1028
1029Vote
1030Thread::ShouldReportStop (Event* event_ptr)
1031{
1032 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +00001033 StateType temp_thread_state = GetTemporaryResumeState();
1034
Greg Clayton5160ce52013-03-27 23:08:40 +00001035 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +00001036
1037 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
1038 {
1039 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001040 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (state was suspended or invalid)", GetID(), eVoteNoOpinion);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001041 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001042 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001043
Jim Ingham92087d82012-01-31 23:09:20 +00001044 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
1045 {
1046 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001047 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (temporary state was suspended or invalid)", GetID(), eVoteNoOpinion);
Jim Ingham92087d82012-01-31 23:09:20 +00001048 return eVoteNoOpinion;
1049 }
1050
1051 if (!ThreadStoppedForAReason())
1052 {
1053 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001054 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (thread didn't stop for a reason.)", GetID(), eVoteNoOpinion);
Jim Ingham92087d82012-01-31 23:09:20 +00001055 return eVoteNoOpinion;
1056 }
1057
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001058 if (m_completed_plan_stack.size() > 0)
1059 {
1060 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +00001061 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001062 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote for complete stack's back plan", GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
1064 }
1065 else
Greg Clayton2cad65a2010-09-03 17:10:42 +00001066 {
Jim Ingham0161b492013-02-09 01:29:05 +00001067 Vote thread_vote = eVoteNoOpinion;
1068 ThreadPlan *plan_ptr = GetCurrentPlan();
1069 while (1)
1070 {
1071 if (plan_ptr->PlanExplainsStop(event_ptr))
1072 {
1073 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1074 break;
1075 }
1076 if (PlanIsBasePlan(plan_ptr))
1077 break;
1078 else
1079 plan_ptr = GetPreviousPlan(plan_ptr);
1080 }
Greg Clayton2cad65a2010-09-03 17:10:42 +00001081 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001082 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i for current plan", GetID(), thread_vote);
Jim Ingham0161b492013-02-09 01:29:05 +00001083
1084 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001085 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086}
1087
1088Vote
1089Thread::ShouldReportRun (Event* event_ptr)
1090{
1091 StateType thread_state = GetResumeState ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001092
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001093 if (thread_state == eStateSuspended
1094 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +00001095 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +00001097 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001098
Greg Clayton5160ce52013-03-27 23:08:40 +00001099 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001100 if (m_completed_plan_stack.size() > 0)
1101 {
1102 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +00001103 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001104 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001105 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001106 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001107 m_completed_plan_stack.back()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001108
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
1110 }
1111 else
Jim Ingham444586b2011-01-24 06:34:17 +00001112 {
1113 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001114 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001115 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001116 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001117 GetCurrentPlan()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001118
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +00001120 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121}
1122
Jim Ingham1b54c882010-06-16 02:00:15 +00001123bool
1124Thread::MatchesSpec (const ThreadSpec *spec)
1125{
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001126 return (spec == nullptr) ? true : spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +00001127}
1128
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129void
1130Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1131{
1132 if (thread_plan_sp)
1133 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001134 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1135 if (!thread_plan_sp->GetThreadPlanTracer())
1136 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001137 m_plan_stack.push_back (thread_plan_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001138
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139 thread_plan_sp->DidPush();
1140
Greg Clayton5160ce52013-03-27 23:08:40 +00001141 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001142 if (log)
1143 {
1144 StreamString s;
1145 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamdee1bc92013-06-22 00:27:45 +00001146 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001147 static_cast<void*>(this), s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001148 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149 }
1150 }
1151}
1152
1153void
1154Thread::PopPlan ()
1155{
Greg Clayton5160ce52013-03-27 23:08:40 +00001156 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001158 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159 return;
1160 else
1161 {
1162 ThreadPlanSP &plan = m_plan_stack.back();
1163 if (log)
1164 {
Daniel Malead01b2952012-11-29 21:49:15 +00001165 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166 }
1167 m_completed_plan_stack.push_back (plan);
1168 plan->WillPop();
1169 m_plan_stack.pop_back();
1170 }
1171}
1172
1173void
1174Thread::DiscardPlan ()
1175{
Jim Inghamdee1bc92013-06-22 00:27:45 +00001176 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177 if (m_plan_stack.size() > 1)
1178 {
1179 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamdee1bc92013-06-22 00:27:45 +00001180 if (log)
1181 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1182
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 m_discarded_plan_stack.push_back (plan);
1184 plan->WillPop();
1185 m_plan_stack.pop_back();
1186 }
1187}
1188
1189ThreadPlan *
1190Thread::GetCurrentPlan ()
1191{
Jim Inghame1471232012-04-19 00:17:05 +00001192 // There will always be at least the base plan. If somebody is mucking with a
1193 // thread with an empty plan stack, we should assert right away.
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001194 return m_plan_stack.empty() ? nullptr : m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195}
1196
1197ThreadPlanSP
1198Thread::GetCompletedPlan ()
1199{
1200 ThreadPlanSP empty_plan_sp;
1201 if (!m_completed_plan_stack.empty())
1202 {
1203 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1204 {
1205 ThreadPlanSP completed_plan_sp;
1206 completed_plan_sp = m_completed_plan_stack[i];
1207 if (!completed_plan_sp->GetPrivate ())
1208 return completed_plan_sp;
1209 }
1210 }
1211 return empty_plan_sp;
1212}
1213
Jim Ingham73ca05a2011-12-17 01:35:57 +00001214ValueObjectSP
1215Thread::GetReturnValueObject ()
1216{
1217 if (!m_completed_plan_stack.empty())
1218 {
1219 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1220 {
1221 ValueObjectSP return_valobj_sp;
1222 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1223 if (return_valobj_sp)
Greg Claytone5214572015-07-01 23:28:31 +00001224 return return_valobj_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001225 }
1226 }
1227 return ValueObjectSP();
1228}
1229
Sean Callananbc8ac342015-09-04 20:49:51 +00001230ExpressionVariableSP
Jim Ingham30fadaf2014-07-08 01:07:32 +00001231Thread::GetExpressionVariable ()
1232{
1233 if (!m_completed_plan_stack.empty())
1234 {
1235 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1236 {
Sean Callananbc8ac342015-09-04 20:49:51 +00001237 ExpressionVariableSP expression_variable_sp;
Jim Ingham30fadaf2014-07-08 01:07:32 +00001238 expression_variable_sp = m_completed_plan_stack[i]->GetExpressionVariable();
1239 if (expression_variable_sp)
Greg Claytone5214572015-07-01 23:28:31 +00001240 return expression_variable_sp;
Jim Ingham30fadaf2014-07-08 01:07:32 +00001241 }
1242 }
Sean Callananbc8ac342015-09-04 20:49:51 +00001243 return ExpressionVariableSP();
Jim Ingham30fadaf2014-07-08 01:07:32 +00001244}
1245
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001246bool
1247Thread::IsThreadPlanDone (ThreadPlan *plan)
1248{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249 if (!m_completed_plan_stack.empty())
1250 {
1251 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1252 {
1253 if (m_completed_plan_stack[i].get() == plan)
1254 return true;
1255 }
1256 }
1257 return false;
1258}
1259
1260bool
1261Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1262{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001263 if (!m_discarded_plan_stack.empty())
1264 {
1265 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1266 {
1267 if (m_discarded_plan_stack[i].get() == plan)
1268 return true;
1269 }
1270 }
1271 return false;
1272}
1273
1274ThreadPlan *
1275Thread::GetPreviousPlan (ThreadPlan *current_plan)
1276{
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001277 if (current_plan == nullptr)
1278 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001279
1280 int stack_size = m_completed_plan_stack.size();
1281 for (int i = stack_size - 1; i > 0; i--)
1282 {
1283 if (current_plan == m_completed_plan_stack[i].get())
1284 return m_completed_plan_stack[i-1].get();
1285 }
1286
1287 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1288 {
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001289 return GetCurrentPlan();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001290 }
1291
1292 stack_size = m_plan_stack.size();
1293 for (int i = stack_size - 1; i > 0; i--)
1294 {
1295 if (current_plan == m_plan_stack[i].get())
1296 return m_plan_stack[i-1].get();
1297 }
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001298 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001299}
1300
1301void
1302Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1303{
1304 if (abort_other_plans)
1305 DiscardThreadPlans(true);
1306
1307 PushPlan (thread_plan_sp);
1308}
1309
Jim Ingham06e827c2010-11-11 19:26:09 +00001310void
1311Thread::EnableTracer (bool value, bool single_stepping)
1312{
1313 int stack_size = m_plan_stack.size();
1314 for (int i = 0; i < stack_size; i++)
1315 {
1316 if (m_plan_stack[i]->GetThreadPlanTracer())
1317 {
1318 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1319 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1320 }
1321 }
1322}
1323
1324void
1325Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1326{
1327 int stack_size = m_plan_stack.size();
1328 for (int i = 0; i < stack_size; i++)
1329 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1330}
1331
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001332bool
1333Thread::DiscardUserThreadPlansUpToIndex (uint32_t thread_index)
1334{
1335 // Count the user thread plans from the back end to get the number of the one we want
1336 // to discard:
1337
1338 uint32_t idx = 0;
1339 ThreadPlan *up_to_plan_ptr = nullptr;
1340
1341 for (ThreadPlanSP plan_sp : m_plan_stack)
1342 {
1343 if (plan_sp->GetPrivate())
1344 continue;
1345 if (idx == thread_index)
1346 {
1347 up_to_plan_ptr = plan_sp.get();
1348 break;
1349 }
1350 else
1351 idx++;
1352 }
1353
1354 if (up_to_plan_ptr == nullptr)
1355 return false;
1356
1357 DiscardThreadPlansUpToPlan(up_to_plan_ptr);
1358 return true;
1359}
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001360
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001362Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1363{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001364 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1365}
1366
1367void
1368Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1369{
Greg Clayton5160ce52013-03-27 23:08:40 +00001370 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001371 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001372 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p",
1373 GetID(), static_cast<void*>(up_to_plan_ptr));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001374
1375 int stack_size = m_plan_stack.size();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001376
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001377 // If the input plan is nullptr, discard all plans. Otherwise make sure this plan is in the
Jim Ingham399f1ca2010-11-05 19:25:48 +00001378 // stack, and if so discard up to and including it.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001379
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001380 if (up_to_plan_ptr == nullptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001381 {
1382 for (int i = stack_size - 1; i > 0; i--)
1383 DiscardPlan();
1384 }
1385 else
1386 {
1387 bool found_it = false;
1388 for (int i = stack_size - 1; i > 0; i--)
1389 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001390 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001391 found_it = true;
1392 }
1393 if (found_it)
1394 {
1395 bool last_one = false;
1396 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1397 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001398 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001399 last_one = true;
1400 DiscardPlan();
1401 }
1402 }
1403 }
Jim Ingham399f1ca2010-11-05 19:25:48 +00001404}
1405
1406void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407Thread::DiscardThreadPlans(bool force)
1408{
Greg Clayton5160ce52013-03-27 23:08:40 +00001409 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001410 if (log)
1411 {
Daniel Malead01b2952012-11-29 21:49:15 +00001412 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001413 }
1414
1415 if (force)
1416 {
1417 int stack_size = m_plan_stack.size();
1418 for (int i = stack_size - 1; i > 0; i--)
1419 {
1420 DiscardPlan();
1421 }
1422 return;
1423 }
1424
1425 while (1)
1426 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001427 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001428 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001429
1430 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1431 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1432 {
1433 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1434 {
1435 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1436 break;
1437 }
1438 }
1439
1440 if (discard)
1441 {
1442 // First pop all the dependent plans:
1443 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1444 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001445 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1446 // for the plan leaves it in a state that it is safe to pop the plan
1447 // with no more notice?
1448 DiscardPlan();
1449 }
1450
1451 // Now discard the master plan itself.
1452 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1453 // discard it's dependent plans, but not it...
1454 if (master_plan_idx > 0)
1455 {
1456 DiscardPlan();
1457 }
1458 }
1459 else
1460 {
1461 // If the master plan doesn't want to get discarded, then we're done.
1462 break;
1463 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001464 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001465}
1466
Jim Inghamcf274f92012-04-09 22:37:39 +00001467bool
1468Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1469{
1470 if (plan_ptr->IsBasePlan())
1471 return true;
1472 else if (m_plan_stack.size() == 0)
1473 return false;
1474 else
1475 return m_plan_stack[0].get() == plan_ptr;
1476}
1477
Jim Ingham93208b82013-01-31 21:46:01 +00001478Error
1479Thread::UnwindInnermostExpression()
1480{
1481 Error error;
1482 int stack_size = m_plan_stack.size();
1483
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001484 // If the input plan is nullptr, discard all plans. Otherwise make sure this plan is in the
Jim Ingham93208b82013-01-31 21:46:01 +00001485 // stack, and if so discard up to and including it.
1486
1487 for (int i = stack_size - 1; i > 0; i--)
1488 {
1489 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1490 {
1491 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1492 return error;
1493 }
1494 }
1495 error.SetErrorString("No expressions currently active on this thread");
1496 return error;
1497}
1498
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001499ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001500Thread::QueueFundamentalPlan (bool abort_other_plans)
1501{
1502 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1503 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001504 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001505}
1506
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001507ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001508Thread::QueueThreadPlanForStepSingleInstruction(bool step_over,
1509 bool abort_other_plans,
1510 bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001511{
1512 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1513 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001514 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001515}
1516
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001517ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001518Thread::QueueThreadPlanForStepOverRange(bool abort_other_plans,
1519 const AddressRange &range,
1520 const SymbolContext &addr_context,
1521 lldb::RunMode stop_other_threads,
1522 LazyBool step_out_avoids_code_withoug_debug_info)
Jim Inghamc6276822012-12-12 19:58:40 +00001523{
1524 ThreadPlanSP thread_plan_sp;
Jim Ingham4b4b2472014-03-13 02:47:14 +00001525 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads, step_out_avoids_code_withoug_debug_info));
1526
Jim Inghamc6276822012-12-12 19:58:40 +00001527 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001528 return thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001529}
1530
Jason Molenda25d5b102015-12-15 00:40:30 +00001531// Call the QueueThreadPlanForStepOverRange method which takes an address range.
1532ThreadPlanSP
1533Thread::QueueThreadPlanForStepOverRange(bool abort_other_plans,
1534 const LineEntry &line_entry,
1535 const SymbolContext &addr_context,
1536 lldb::RunMode stop_other_threads,
1537 LazyBool step_out_avoids_code_withoug_debug_info)
1538{
1539 return QueueThreadPlanForStepOverRange (abort_other_plans,
1540 line_entry.GetSameLineContiguousAddressRange(),
1541 addr_context,
1542 stop_other_threads,
1543 step_out_avoids_code_withoug_debug_info);
1544}
1545
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001546ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001547Thread::QueueThreadPlanForStepInRange(bool abort_other_plans,
1548 const AddressRange &range,
1549 const SymbolContext &addr_context,
1550 const char *step_in_target,
1551 lldb::RunMode stop_other_threads,
1552 LazyBool step_in_avoids_code_without_debug_info,
1553 LazyBool step_out_avoids_code_without_debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001554{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001555 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInRange (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001556 range,
1557 addr_context,
1558 stop_other_threads,
1559 step_in_avoids_code_without_debug_info,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001560 step_out_avoids_code_without_debug_info));
1561 ThreadPlanStepInRange *plan = static_cast<ThreadPlanStepInRange *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001562
Jim Inghamc6276822012-12-12 19:58:40 +00001563 if (step_in_target)
1564 plan->SetStepInTarget(step_in_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001565
1566 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001567 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001568}
1569
Jason Molenda25d5b102015-12-15 00:40:30 +00001570// Call the QueueThreadPlanForStepInRange method which takes an address range.
1571ThreadPlanSP
1572Thread::QueueThreadPlanForStepInRange(bool abort_other_plans,
1573 const LineEntry &line_entry,
1574 const SymbolContext &addr_context,
1575 const char *step_in_target,
1576 lldb::RunMode stop_other_threads,
1577 LazyBool step_in_avoids_code_without_debug_info,
1578 LazyBool step_out_avoids_code_without_debug_info)
1579{
1580 return QueueThreadPlanForStepInRange (abort_other_plans,
1581 line_entry.GetSameLineContiguousAddressRange(),
1582 addr_context,
1583 step_in_target,
1584 stop_other_threads,
1585 step_in_avoids_code_without_debug_info,
1586 step_out_avoids_code_without_debug_info);
1587}
1588
1589
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001590ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001591Thread::QueueThreadPlanForStepOut(bool abort_other_plans,
1592 SymbolContext *addr_context,
1593 bool first_insn,
1594 bool stop_other_threads,
1595 Vote stop_vote,
1596 Vote run_vote,
1597 uint32_t frame_idx,
Jason Molendafd4cea52016-01-08 21:40:11 +00001598 LazyBool step_out_avoids_code_without_debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001599{
Greg Clayton481cef22011-01-21 06:11:58 +00001600 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1601 addr_context,
1602 first_insn,
1603 stop_other_threads,
1604 stop_vote,
1605 run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001606 frame_idx,
Jason Molendafd4cea52016-01-08 21:40:11 +00001607 step_out_avoids_code_without_debug_info));
Jim Ingham4b4b2472014-03-13 02:47:14 +00001608
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001609 if (thread_plan_sp->ValidatePlan(nullptr))
Jim Ingham4b4b2472014-03-13 02:47:14 +00001610 {
1611 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1612 return thread_plan_sp;
1613 }
1614 else
1615 {
1616 return ThreadPlanSP();
1617 }
1618}
1619
1620ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001621Thread::QueueThreadPlanForStepOutNoShouldStop(bool abort_other_plans,
1622 SymbolContext *addr_context,
1623 bool first_insn,
1624 bool stop_other_threads,
1625 Vote stop_vote,
1626 Vote run_vote,
Jason Molendafd4cea52016-01-08 21:40:11 +00001627 uint32_t frame_idx,
1628 bool continue_to_next_branch)
Jim Ingham4b4b2472014-03-13 02:47:14 +00001629{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001630 ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001631 addr_context,
1632 first_insn,
1633 stop_other_threads,
1634 stop_vote,
1635 run_vote,
1636 frame_idx,
Jason Molendafd4cea52016-01-08 21:40:11 +00001637 eLazyBoolNo,
1638 continue_to_next_branch));
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001639
1640 ThreadPlanStepOut *new_plan = static_cast<ThreadPlanStepOut *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001641 new_plan->ClearShouldStopHereCallbacks();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001642
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001643 if (thread_plan_sp->ValidatePlan(nullptr))
Sean Callanan708709c2012-07-31 22:19:25 +00001644 {
1645 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001646 return thread_plan_sp;
Sean Callanan708709c2012-07-31 22:19:25 +00001647 }
1648 else
1649 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001650 return ThreadPlanSP();
Sean Callanan708709c2012-07-31 22:19:25 +00001651 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001652}
1653
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001654ThreadPlanSP
Jim Ingham18de2fd2012-05-10 01:35:39 +00001655Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001656{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001657 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001658 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan(nullptr))
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001659 return ThreadPlanSP();
Jim Ingham25f66702011-12-03 01:52:59 +00001660
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001661 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001662 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001663}
1664
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001665ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001666Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1667 Address &target_addr,
1668 bool stop_other_threads)
1669{
1670 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1671 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001672 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001673}
1674
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001675ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001676Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001677 lldb::addr_t *address_list,
1678 size_t num_addresses,
1679 bool stop_other_threads,
1680 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001681{
Greg Clayton481cef22011-01-21 06:11:58 +00001682 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001684 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685
1686}
1687
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001688lldb::ThreadPlanSP
1689Thread::QueueThreadPlanForStepScripted (bool abort_other_plans,
1690 const char *class_name,
1691 bool stop_other_threads)
1692{
1693 ThreadPlanSP thread_plan_sp (new ThreadPlanPython (*this, class_name));
1694 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1695 // This seems a little funny, but I don't want to have to split up the constructor and the
1696 // DidPush in the scripted plan, that seems annoying.
1697 // That means the constructor has to be in DidPush.
1698 // So I have to validate the plan AFTER pushing it, and then take it off again...
1699 if (!thread_plan_sp->ValidatePlan(nullptr))
1700 {
1701 DiscardThreadPlansUpToPlan(thread_plan_sp);
1702 return ThreadPlanSP();
1703 }
1704 else
1705 return thread_plan_sp;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001706}
1707
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001708uint32_t
1709Thread::GetIndexID () const
1710{
1711 return m_index_id;
1712}
1713
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001714static void
1715PrintPlanElement (Stream *s, const ThreadPlanSP &plan, lldb::DescriptionLevel desc_level, int32_t elem_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001716{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001717 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001718 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001719 s->Printf ("Element %d: ", elem_idx);
1720 plan->GetDescription (s, desc_level);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001721 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001722 s->IndentLess();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001723}
1724
1725static void
1726PrintPlanStack (Stream *s, const std::vector<lldb::ThreadPlanSP> &plan_stack, lldb::DescriptionLevel desc_level, bool include_internal)
1727{
1728 int32_t print_idx = 0;
1729 for (ThreadPlanSP plan_sp : plan_stack)
1730 {
1731 if (include_internal || !plan_sp->GetPrivate())
1732 {
1733 PrintPlanElement (s, plan_sp, desc_level, print_idx++);
1734 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001735 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001736}
1737
1738void
1739Thread::DumpThreadPlans (Stream *s,
1740 lldb::DescriptionLevel desc_level,
1741 bool include_internal,
1742 bool ignore_boring_threads) const
1743{
Jason Molenda60b5da62014-10-16 07:53:46 +00001744 uint32_t stack_size;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001745
1746 if (ignore_boring_threads)
1747 {
1748 uint32_t stack_size = m_plan_stack.size();
1749 uint32_t completed_stack_size = m_completed_plan_stack.size();
1750 uint32_t discarded_stack_size = m_discarded_plan_stack.size();
1751 if (stack_size == 1 && completed_stack_size == 0 && discarded_stack_size == 0)
1752 {
1753 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 "\n", GetIndexID(), GetID());
1754 s->IndentMore();
1755 s->Indent();
1756 s->Printf("No active thread plans\n");
1757 s->IndentLess();
1758 return;
1759 }
1760 }
1761
1762 s->Indent();
1763 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 ":\n", GetIndexID(), GetID());
1764 s->IndentMore();
1765 s->Indent();
1766 s->Printf ("Active plan stack:\n");
1767 PrintPlanStack (s, m_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001768
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001769 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001770 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001771 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001772 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001773 s->Printf ("Completed Plan Stack:\n");
1774 PrintPlanStack (s, m_completed_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001775 }
1776
1777 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001778 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001779 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001780 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001781 s->Printf ("Discarded Plan Stack:\n");
1782 PrintPlanStack (s, m_discarded_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001783 }
1784
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001785 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001786}
1787
Greg Claytond9e416c2012-02-18 05:35:26 +00001788TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001789Thread::CalculateTarget ()
1790{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001791 TargetSP target_sp;
1792 ProcessSP process_sp(GetProcess());
1793 if (process_sp)
1794 target_sp = process_sp->CalculateTarget();
1795 return target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001796}
1797
Greg Claytond9e416c2012-02-18 05:35:26 +00001798ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001799Thread::CalculateProcess ()
1800{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001801 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001802}
1803
Greg Claytond9e416c2012-02-18 05:35:26 +00001804ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001805Thread::CalculateThread ()
1806{
Greg Claytond9e416c2012-02-18 05:35:26 +00001807 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001808}
1809
Jason Molendab57e4a12013-11-04 09:33:30 +00001810StackFrameSP
1811Thread::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001812{
Jason Molendab57e4a12013-11-04 09:33:30 +00001813 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001814}
1815
1816void
Greg Clayton0603aa92010-10-04 01:05:56 +00001817Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001818{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001819 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001820}
1821
Greg Clayton39d0ab32012-03-29 01:41:38 +00001822StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001823Thread::GetStackFrameList ()
1824{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001825 StackFrameListSP frame_list_sp;
1826 Mutex::Locker locker(m_frame_mutex);
1827 if (m_curr_frames_sp)
1828 {
1829 frame_list_sp = m_curr_frames_sp;
1830 }
1831 else
1832 {
1833 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1834 m_curr_frames_sp = frame_list_sp;
1835 }
1836 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001837}
1838
Greg Clayton12daf9462010-08-25 00:35:26 +00001839void
1840Thread::ClearStackFrames ()
1841{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001842 Mutex::Locker locker(m_frame_mutex);
1843
Greg Clayton160c9d82013-05-01 21:54:04 +00001844 Unwind *unwinder = GetUnwinder ();
1845 if (unwinder)
1846 unwinder->Clear();
1847
Jim Inghamb0c72a52012-02-29 03:40:22 +00001848 // Only store away the old "reference" StackFrameList if we got all its frames:
1849 // FIXME: At some point we can try to splice in the frames we have fetched into
1850 // the new frame as we make it, but let's not try that now.
1851 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001852 m_prev_frames_sp.swap (m_curr_frames_sp);
1853 m_curr_frames_sp.reset();
Jason Molenda705b1802014-06-13 02:37:02 +00001854
1855 m_extended_info.reset();
1856 m_extended_info_fetched = false;
Jim Ingham87c11912010-08-12 02:14:28 +00001857}
1858
Jason Molendab57e4a12013-11-04 09:33:30 +00001859lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001860Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1861{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001862 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001863}
1864
Jim Ingham44137582012-09-12 00:40:39 +00001865Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001866Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001867{
Jason Molendab57e4a12013-11-04 09:33:30 +00001868 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001869 Error return_error;
1870
1871 if (!frame_sp)
1872 {
Daniel Malead01b2952012-11-29 21:49:15 +00001873 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001874 }
1875
Jim Ingham4f465cf2012-10-10 18:32:14 +00001876 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001877}
1878
1879Error
Jason Molendab57e4a12013-11-04 09:33:30 +00001880Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001881{
1882 Error return_error;
1883
1884 if (!frame_sp)
1885 {
1886 return_error.SetErrorString("Can't return to a null frame.");
1887 return return_error;
1888 }
1889
1890 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001891 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
Jason Molendab57e4a12013-11-04 09:33:30 +00001892 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001893 if (!older_frame_sp)
1894 {
1895 return_error.SetErrorString("No older frame to return to.");
1896 return return_error;
1897 }
Jim Ingham44137582012-09-12 00:40:39 +00001898
1899 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001900 {
Jim Ingham44137582012-09-12 00:40:39 +00001901 lldb::ABISP abi = thread->GetProcess()->GetABI();
1902 if (!abi)
1903 {
1904 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001905 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001906 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001907 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1908
1909 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1910 // Turn that back on when that works.
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001911 if (/* DISABLES CODE */ (0) && sc.function != nullptr)
Jim Ingham1f51e602012-09-27 01:15:29 +00001912 {
1913 Type *function_type = sc.function->GetType();
1914 if (function_type)
1915 {
Greg Clayton99558cc42015-08-24 23:46:31 +00001916 CompilerType return_type = sc.function->GetCompilerType().GetFunctionReturnType();
Jim Ingham1f51e602012-09-27 01:15:29 +00001917 if (return_type)
1918 {
Jim Ingham1f51e602012-09-27 01:15:29 +00001919 StreamString s;
Greg Clayton57ee3062013-07-11 22:46:58 +00001920 return_type.DumpTypeDescription(&s);
1921 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham1f51e602012-09-27 01:15:29 +00001922 if (cast_value_sp)
1923 {
1924 cast_value_sp->SetFormat(eFormatHex);
1925 return_value_sp = cast_value_sp;
1926 }
1927 }
1928 }
1929 }
1930
Jim Inghamcb640dd2012-09-14 02:14:15 +00001931 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001932 if (!return_error.Success())
1933 return return_error;
1934 }
1935
1936 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001937 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001938 // cook their data
1939
Jason Molendab57e4a12013-11-04 09:33:30 +00001940 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
Jim Ingham93208b82013-01-31 21:46:01 +00001941 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001942 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001943 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1944 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001945 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001946 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1947 if (copy_success)
1948 {
1949 thread->DiscardThreadPlans(true);
1950 thread->ClearStackFrames();
1951 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1952 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1953 }
1954 else
1955 {
1956 return_error.SetErrorString("Could not reset register values.");
1957 }
Jim Ingham93208b82013-01-31 21:46:01 +00001958 }
1959 else
1960 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001961 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001962 }
Jim Ingham44137582012-09-12 00:40:39 +00001963 }
1964 else
1965 {
Jim Ingham93208b82013-01-31 21:46:01 +00001966 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001967 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001968 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001969}
1970
Richard Mittonf86248d2013-09-12 02:20:34 +00001971static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1972{
1973 for (size_t n=0;n<list.size();n++)
1974 {
1975 s << "\t";
1976 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1977 s << "\n";
1978 }
1979}
1980
1981Error
1982Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1983{
1984 ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
1985 Target *target = exe_ctx.GetTargetPtr();
1986 TargetSP target_sp = exe_ctx.GetTargetSP();
1987 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00001988 StackFrame *frame = exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00001989 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1990
1991 // Find candidate locations.
1992 std::vector<Address> candidates, within_function, outside_function;
1993 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
1994
1995 // If possible, we try and stay within the current function.
1996 // Within a function, we accept multiple locations (optimized code may do this,
1997 // there's no solution here so we do the best we can).
1998 // However if we're trying to leave the function, we don't know how to pick the
1999 // right location, so if there's more than one then we bail.
2000 if (!within_function.empty())
2001 candidates = within_function;
2002 else if (outside_function.size() == 1 && can_leave_function)
2003 candidates = outside_function;
2004
2005 // Check if we got anything.
2006 if (candidates.empty())
2007 {
2008 if (outside_function.empty())
2009 {
2010 return Error("Cannot locate an address for %s:%i.",
2011 file.GetFilename().AsCString(), line);
2012 }
2013 else if (outside_function.size() == 1)
2014 {
2015 return Error("%s:%i is outside the current function.",
2016 file.GetFilename().AsCString(), line);
2017 }
2018 else
2019 {
2020 StreamString sstr;
2021 DumpAddressList(sstr, outside_function, target);
2022 return Error("%s:%i has multiple candidate locations:\n%s",
2023 file.GetFilename().AsCString(), line, sstr.GetString().c_str());
2024 }
2025 }
2026
2027 // Accept the first location, warn about any others.
2028 Address dest = candidates[0];
2029 if (warnings && candidates.size() > 1)
2030 {
2031 StreamString sstr;
2032 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
2033 file.GetFilename().AsCString(), line);
2034 DumpAddressList(sstr, candidates, target);
2035 *warnings = sstr.GetString();
2036 }
2037
2038 if (!reg_ctx->SetPC (dest))
2039 return Error("Cannot change PC to target address.");
2040
2041 return Error();
2042}
2043
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002044void
Greg Clayton0603aa92010-10-04 01:05:56 +00002045Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002046{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002047 ExecutionContext exe_ctx (shared_from_this());
2048 Process *process = exe_ctx.GetProcessPtr();
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002049 if (process == nullptr)
Greg Clayton1ac04c32012-02-21 00:09:25 +00002050 return;
2051
Jason Molendab57e4a12013-11-04 09:33:30 +00002052 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00002053 SymbolContext frame_sc;
Jim Inghamd1f25362015-01-28 01:17:26 +00002054 if (frame_idx != LLDB_INVALID_FRAME_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002055 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002056 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002057 if (frame_sp)
2058 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002059 exe_ctx.SetFrameSP(frame_sp);
2060 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002061 }
2062 }
2063
Greg Clayton554f68d2015-02-04 22:00:53 +00002064 const FormatEntity::Entry *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00002065 assert (thread_format);
Greg Clayton554f68d2015-02-04 22:00:53 +00002066
2067 FormatEntity::Format(*thread_format,
2068 strm,
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002069 frame_sp ? &frame_sc : nullptr,
Greg Clayton554f68d2015-02-04 22:00:53 +00002070 &exe_ctx,
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002071 nullptr,
2072 nullptr,
Greg Clayton554f68d2015-02-04 22:00:53 +00002073 false,
2074 false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002075}
2076
Greg Clayton99d0faf2010-11-18 23:32:35 +00002077void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002078Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00002079{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002080}
Jim Inghamee8aea12010-09-08 03:14:33 +00002081
Greg Clayton99d0faf2010-11-18 23:32:35 +00002082void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002083Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002084{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002085}
Jim Inghamee8aea12010-09-08 03:14:33 +00002086
Richard Mitton0a558352013-10-17 21:14:00 +00002087lldb::addr_t
2088Thread::GetThreadPointer ()
2089{
2090 return LLDB_INVALID_ADDRESS;
2091}
2092
2093addr_t
2094Thread::GetThreadLocalData (const ModuleSP module)
2095{
2096 // The default implementation is to ask the dynamic loader for it.
2097 // This can be overridden for specific platforms.
2098 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
2099 if (loader)
2100 return loader->GetThreadLocalData (module, shared_from_this());
2101 else
2102 return LLDB_INVALID_ADDRESS;
2103}
2104
Jason Molendab4892cd2014-05-13 22:02:48 +00002105bool
2106Thread::SafeToCallFunctions ()
2107{
2108 Process *process = GetProcess().get();
2109 if (process)
2110 {
2111 SystemRuntime *runtime = process->GetSystemRuntime ();
2112 if (runtime)
2113 {
2114 return runtime->SafeToCallFunctionsOnThisThread (shared_from_this());
2115 }
2116 }
2117 return true;
2118}
2119
Jason Molendab57e4a12013-11-04 09:33:30 +00002120lldb::StackFrameSP
2121Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
Jim Inghame4284b72010-09-23 17:40:12 +00002122{
Jason Molendab57e4a12013-11-04 09:33:30 +00002123 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00002124}
Caroline Ticeceb6b132010-10-26 03:11:13 +00002125
2126const char *
2127Thread::StopReasonAsCString (lldb::StopReason reason)
2128{
2129 switch (reason)
2130 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00002131 case eStopReasonInvalid: return "invalid";
2132 case eStopReasonNone: return "none";
2133 case eStopReasonTrace: return "trace";
2134 case eStopReasonBreakpoint: return "breakpoint";
2135 case eStopReasonWatchpoint: return "watchpoint";
2136 case eStopReasonSignal: return "signal";
2137 case eStopReasonException: return "exception";
2138 case eStopReasonExec: return "exec";
2139 case eStopReasonPlanComplete: return "plan complete";
2140 case eStopReasonThreadExiting: return "thread exiting";
Kuba Breckaafdf8422014-10-10 23:43:03 +00002141 case eStopReasonInstrumentation: return "instrumentation break";
Caroline Ticeceb6b132010-10-26 03:11:13 +00002142 }
2143
Caroline Ticeceb6b132010-10-26 03:11:13 +00002144 static char unknown_state_string[64];
2145 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
2146 return unknown_state_string;
2147}
2148
2149const char *
2150Thread::RunModeAsCString (lldb::RunMode mode)
2151{
2152 switch (mode)
2153 {
2154 case eOnlyThisThread: return "only this thread";
2155 case eAllThreads: return "all threads";
2156 case eOnlyDuringStepping: return "only during stepping";
2157 }
2158
2159 static char unknown_state_string[64];
2160 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
2161 return unknown_state_string;
2162}
Jim Ingham06e827c2010-11-11 19:26:09 +00002163
Greg Clayton7260f622011-04-18 08:33:37 +00002164size_t
2165Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
2166{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002167 ExecutionContext exe_ctx (shared_from_this());
2168 Target *target = exe_ctx.GetTargetPtr();
2169 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00002170 size_t num_frames_shown = 0;
2171 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00002172 bool is_selected = false;
2173 if (process)
2174 {
2175 if (process->GetThreadList().GetSelectedThread().get() == this)
2176 is_selected = true;
2177 }
2178 strm.Printf("%c ", is_selected ? '*' : ' ');
2179 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00002180 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002181 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00002182 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00002183 {
Jim Inghame610d642011-08-16 00:07:28 +00002184 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
2185 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
2186 {
2187 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
2188 }
Greg Clayton5113dc82011-08-12 06:47:54 +00002189 }
Greg Clayton7260f622011-04-18 08:33:37 +00002190 }
2191
2192 DumpUsingSettingsFormat (strm, start_frame);
2193
2194 if (num_frames > 0)
2195 {
2196 strm.IndentMore();
2197
2198 const bool show_frame_info = true;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002199
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002200 const char *selected_frame_marker = nullptr;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002201 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
2202 strm.IndentMore ();
2203 else
2204 selected_frame_marker = "* ";
2205
Greg Clayton39d0ab32012-03-29 01:41:38 +00002206 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
2207 start_frame,
2208 num_frames,
2209 show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002210 num_frames_with_source,
2211 selected_frame_marker);
2212 if (num_frames == 1)
2213 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00002214 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00002215 }
2216 return num_frames_shown;
2217}
2218
Jason Molenda705b1802014-06-13 02:37:02 +00002219bool
Kuba Breckaafdf8422014-10-10 23:43:03 +00002220Thread::GetDescription (Stream &strm, lldb::DescriptionLevel level, bool print_json_thread, bool print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002221{
2222 DumpUsingSettingsFormat (strm, 0);
2223 strm.Printf("\n");
2224
2225 StructuredData::ObjectSP thread_info = GetExtendedInfo();
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002226
Kuba Breckaafdf8422014-10-10 23:43:03 +00002227 if (print_json_thread || print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002228 {
Kuba Breckaafdf8422014-10-10 23:43:03 +00002229 if (thread_info && print_json_thread)
2230 {
2231 thread_info->Dump (strm);
2232 strm.Printf("\n");
2233 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002234
2235 if (print_json_stopinfo && m_stop_info_sp)
Kuba Breckaafdf8422014-10-10 23:43:03 +00002236 {
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002237 StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo();
2238 if (stop_info)
2239 {
2240 stop_info->Dump (strm);
2241 strm.Printf("\n");
2242 }
Kuba Breckaafdf8422014-10-10 23:43:03 +00002243 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002244
Jason Molenda705b1802014-06-13 02:37:02 +00002245 return true;
2246 }
2247
2248 if (thread_info)
2249 {
2250 StructuredData::ObjectSP activity = thread_info->GetObjectForDotSeparatedPath("activity");
2251 StructuredData::ObjectSP breadcrumb = thread_info->GetObjectForDotSeparatedPath("breadcrumb");
2252 StructuredData::ObjectSP messages = thread_info->GetObjectForDotSeparatedPath("trace_messages");
2253
2254 bool printed_activity = false;
2255 if (activity && activity->GetType() == StructuredData::Type::eTypeDictionary)
2256 {
2257 StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
2258 StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
2259 StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
2260 if (name && name->GetType() == StructuredData::Type::eTypeString
2261 && id && id->GetType() == StructuredData::Type::eTypeInteger)
2262 {
2263 strm.Printf(" Activity '%s', 0x%" PRIx64 "\n", name->GetAsString()->GetValue().c_str(), id->GetAsInteger()->GetValue());
2264 }
2265 printed_activity = true;
2266 }
2267 bool printed_breadcrumb = false;
2268 if (breadcrumb && breadcrumb->GetType() == StructuredData::Type::eTypeDictionary)
2269 {
2270 if (printed_activity)
2271 strm.Printf ("\n");
2272 StructuredData::Dictionary *breadcrumb_dict = breadcrumb->GetAsDictionary();
2273 StructuredData::ObjectSP breadcrumb_text = breadcrumb_dict->GetValueForKey ("name");
2274 if (breadcrumb_text && breadcrumb_text->GetType() == StructuredData::Type::eTypeString)
2275 {
2276 strm.Printf (" Current Breadcrumb: %s\n", breadcrumb_text->GetAsString()->GetValue().c_str());
2277 }
2278 printed_breadcrumb = true;
2279 }
2280 if (messages && messages->GetType() == StructuredData::Type::eTypeArray)
2281 {
2282 if (printed_breadcrumb)
2283 strm.Printf("\n");
2284 StructuredData::Array *messages_array = messages->GetAsArray();
2285 const size_t msg_count = messages_array->GetSize();
2286 if (msg_count > 0)
2287 {
2288 strm.Printf (" %zu trace messages:\n", msg_count);
2289 for (size_t i = 0; i < msg_count; i++)
2290 {
2291 StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
2292 if (message && message->GetType() == StructuredData::Type::eTypeDictionary)
2293 {
2294 StructuredData::Dictionary *message_dict = message->GetAsDictionary();
2295 StructuredData::ObjectSP message_text = message_dict->GetValueForKey ("message");
2296 if (message_text && message_text->GetType() == StructuredData::Type::eTypeString)
2297 {
2298 strm.Printf (" %s\n", message_text->GetAsString()->GetValue().c_str());
2299 }
2300 }
2301 }
2302 }
2303 }
2304 }
2305
2306 return true;
2307}
2308
Greg Clayton7260f622011-04-18 08:33:37 +00002309size_t
2310Thread::GetStackFrameStatus (Stream& strm,
2311 uint32_t first_frame,
2312 uint32_t num_frames,
2313 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002314 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00002315{
Greg Clayton39d0ab32012-03-29 01:41:38 +00002316 return GetStackFrameList()->GetStatus (strm,
2317 first_frame,
2318 num_frames,
2319 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002320 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00002321}
2322
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002323Unwind *
2324Thread::GetUnwinder ()
2325{
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002326 if (!m_unwinder_ap)
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002327 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00002328 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002329 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2330 switch (machine)
2331 {
2332 case llvm::Triple::x86_64:
2333 case llvm::Triple::x86:
2334 case llvm::Triple::arm:
Todd Fialad8eaa172014-07-23 14:37:35 +00002335 case llvm::Triple::aarch64:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002336 case llvm::Triple::thumb:
Bhushan D. Attarde794a4d52015-05-15 06:53:30 +00002337 case llvm::Triple::mips:
2338 case llvm::Triple::mipsel:
Ed Masteb73f8442013-10-10 00:59:47 +00002339 case llvm::Triple::mips64:
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +00002340 case llvm::Triple::mips64el:
Justin Hibbits6256a0e2014-10-31 02:34:28 +00002341 case llvm::Triple::ppc:
Justin Hibbitsdb39cdf2014-10-31 15:57:52 +00002342 case llvm::Triple::ppc64:
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00002343 case llvm::Triple::systemz:
Deepak Panickal6d3df422014-02-19 11:16:46 +00002344 case llvm::Triple::hexagon:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002345 m_unwinder_ap.reset (new UnwindLLDB (*this));
2346 break;
2347
2348 default:
2349 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2350 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2351 break;
2352 }
2353 }
2354 return m_unwinder_ap.get();
2355}
2356
Greg Claytonfa559e52012-05-18 02:38:05 +00002357void
2358Thread::Flush ()
2359{
2360 ClearStackFrames ();
2361 m_reg_context_sp.reset();
2362}
Jim Ingham5d88a062012-10-16 00:09:33 +00002363
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00002364bool
Jim Ingham5d88a062012-10-16 00:09:33 +00002365Thread::IsStillAtLastBreakpointHit ()
2366{
2367 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2368 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2369 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002370 if (m_stop_info_sp) {
2371 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00002372 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002373 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00002374 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2375 if (reg_ctx_sp)
2376 {
2377 lldb::addr_t pc = reg_ctx_sp->GetPC();
2378 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002379 if (bp_site_sp &&
2380 static_cast<break_id_t>(value) == bp_site_sp->GetID())
Greg Clayton1afa68e2013-04-02 20:32:37 +00002381 return true;
2382 }
Jim Ingham5d88a062012-10-16 00:09:33 +00002383 }
2384 }
2385 return false;
2386}
Greg Clayton44d93782014-01-27 23:43:24 +00002387
Greg Clayton44d93782014-01-27 23:43:24 +00002388Error
2389Thread::StepIn (bool source_step,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002390 LazyBool step_in_avoids_code_without_debug_info,
2391 LazyBool step_out_avoids_code_without_debug_info)
2392
Greg Clayton44d93782014-01-27 23:43:24 +00002393{
2394 Error error;
2395 Process *process = GetProcess().get();
2396 if (StateIsStoppedState (process->GetState(), true))
2397 {
2398 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2399 ThreadPlanSP new_plan_sp;
2400 const lldb::RunMode run_mode = eOnlyThisThread;
2401 const bool abort_other_plans = false;
2402
2403 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2404 {
2405 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002406 new_plan_sp = QueueThreadPlanForStepInRange(abort_other_plans,
2407 sc.line_entry,
2408 sc,
2409 nullptr,
2410 run_mode,
2411 step_in_avoids_code_without_debug_info,
2412 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002413 }
2414 else
2415 {
2416 new_plan_sp = QueueThreadPlanForStepSingleInstruction (false,
2417 abort_other_plans,
2418 run_mode);
2419 }
2420
2421 new_plan_sp->SetIsMasterPlan(true);
2422 new_plan_sp->SetOkayToDiscard(false);
2423
2424 // Why do we need to set the current thread by ID here???
2425 process->GetThreadList().SetSelectedThreadByID (GetID());
2426 error = process->Resume();
2427 }
2428 else
2429 {
2430 error.SetErrorString("process not stopped");
2431 }
2432 return error;
2433}
2434
2435Error
Jim Ingham4b4b2472014-03-13 02:47:14 +00002436Thread::StepOver (bool source_step,
2437 LazyBool step_out_avoids_code_without_debug_info)
Greg Clayton44d93782014-01-27 23:43:24 +00002438{
2439 Error error;
2440 Process *process = GetProcess().get();
2441 if (StateIsStoppedState (process->GetState(), true))
2442 {
2443 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2444 ThreadPlanSP new_plan_sp;
2445
2446 const lldb::RunMode run_mode = eOnlyThisThread;
2447 const bool abort_other_plans = false;
2448
2449 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2450 {
2451 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2452 new_plan_sp = QueueThreadPlanForStepOverRange (abort_other_plans,
Jason Molenda25d5b102015-12-15 00:40:30 +00002453 sc.line_entry,
Greg Clayton44d93782014-01-27 23:43:24 +00002454 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002455 run_mode,
2456 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002457 }
2458 else
2459 {
2460 new_plan_sp = QueueThreadPlanForStepSingleInstruction (true,
2461 abort_other_plans,
2462 run_mode);
2463 }
2464
2465 new_plan_sp->SetIsMasterPlan(true);
2466 new_plan_sp->SetOkayToDiscard(false);
2467
2468 // Why do we need to set the current thread by ID here???
2469 process->GetThreadList().SetSelectedThreadByID (GetID());
2470 error = process->Resume();
2471 }
2472 else
2473 {
2474 error.SetErrorString("process not stopped");
2475 }
2476 return error;
2477}
2478
2479Error
2480Thread::StepOut ()
2481{
2482 Error error;
2483 Process *process = GetProcess().get();
2484 if (StateIsStoppedState (process->GetState(), true))
2485 {
2486 const bool first_instruction = false;
2487 const bool stop_other_threads = false;
2488 const bool abort_other_plans = false;
2489
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002490 ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut(abort_other_plans,
2491 nullptr,
2492 first_instruction,
2493 stop_other_threads,
2494 eVoteYes,
2495 eVoteNoOpinion,
2496 0));
Greg Clayton44d93782014-01-27 23:43:24 +00002497
2498 new_plan_sp->SetIsMasterPlan(true);
2499 new_plan_sp->SetOkayToDiscard(false);
2500
2501 // Why do we need to set the current thread by ID here???
2502 process->GetThreadList().SetSelectedThreadByID (GetID());
2503 error = process->Resume();
2504 }
2505 else
2506 {
2507 error.SetErrorString("process not stopped");
2508 }
2509 return error;
Jim Ingham4b4b2472014-03-13 02:47:14 +00002510}