blob: 26502f2eb4b0a600dad68b659fe09385dd0863fa [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
12// Other libraries and framework includes
13// Project includes
Jim Ingham1b54c882010-06-16 02:00:15 +000014#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000015#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Log.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000017#include "lldb/Core/FormatEntity.h"
Jason Molenda03c9cd02015-08-06 21:54:29 +000018#include "lldb/Core/Module.h"
Greg Clayton160c9d82013-05-01 21:54:04 +000019#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Stream.h"
21#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000022#include "lldb/Core/RegularExpression.h"
Jim Ingham0d5a2bd2015-09-03 01:40:51 +000023#include "lldb/Core/ValueObject.h"
Greg Clayton7260f622011-04-18 08:33:37 +000024#include "lldb/Host/Host.h"
Jim Ingham4da62062014-01-23 21:52:47 +000025#include "lldb/Interpreter/OptionValueFileSpecList.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000026#include "lldb/Interpreter/OptionValueProperties.h"
27#include "lldb/Interpreter/Property.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000028#include "lldb/Symbol/Function.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000029#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Target/DynamicLoader.h"
31#include "lldb/Target/ExecutionContext.h"
32#include "lldb/Target/Process.h"
33#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000034#include "lldb/Target/StopInfo.h"
Jason Molendab4892cd2014-05-13 22:02:48 +000035#include "lldb/Target/SystemRuntime.h"
Greg Clayton896dff62010-07-23 16:45:51 +000036#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Target/Thread.h"
38#include "lldb/Target/ThreadPlan.h"
39#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Target/ThreadPlanBase.h"
Jim Ingham2bdbfd52014-09-29 23:17:18 +000041#include "lldb/Target/ThreadPlanPython.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/Target/ThreadPlanStepInstruction.h"
43#include "lldb/Target/ThreadPlanStepOut.h"
44#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
45#include "lldb/Target/ThreadPlanStepThrough.h"
46#include "lldb/Target/ThreadPlanStepInRange.h"
47#include "lldb/Target/ThreadPlanStepOverRange.h"
48#include "lldb/Target/ThreadPlanRunToAddress.h"
49#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000050#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000051#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000052#include "Plugins/Process/Utility/UnwindLLDB.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000053#include "Plugins/Process/Utility/UnwindMacOSXFrameBackchain.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000054
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055using namespace lldb;
56using namespace lldb_private;
57
Greg Clayton67cc0632012-08-22 17:17:09 +000058const ThreadPropertiesSP &
59Thread::GetGlobalProperties()
60{
Greg Claytoncc2e27f2016-02-26 23:20:08 +000061 // NOTE: intentional leak so we don't crash if global destructor chain gets
62 // called as other threads still use the result of this function
63 static ThreadPropertiesSP *g_settings_sp_ptr = nullptr;
Greg Clayton04df8ee2016-02-26 19:38:18 +000064 static std::once_flag g_once_flag;
65 std::call_once(g_once_flag, []() {
Greg Claytoncc2e27f2016-02-26 23:20:08 +000066 g_settings_sp_ptr = new ThreadPropertiesSP(new ThreadProperties (true));
Greg Clayton04df8ee2016-02-26 19:38:18 +000067 });
Greg Claytoncc2e27f2016-02-26 23:20:08 +000068 return *g_settings_sp_ptr;
Greg Clayton67cc0632012-08-22 17:17:09 +000069}
70
71static PropertyDefinition
72g_properties[] =
73{
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000074 { "step-in-avoid-nodebug", OptionValue::eTypeBoolean, true, true, nullptr, nullptr, "If true, step-in will not stop in functions with no debug information." },
75 { "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 +000076 "debug information. Passing a frame argument to step-out will override this option." },
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +000077 { "step-avoid-regexp", OptionValue::eTypeRegex , true , 0, "^std::", nullptr, "A regular expression defining functions step-in won't stop in." },
78 { "step-avoid-libraries", OptionValue::eTypeFileSpecList , true , 0, nullptr, nullptr, "A list of libraries that source stepping won't stop in." },
79 { "trace-thread", OptionValue::eTypeBoolean, false, false, nullptr, nullptr, "If true, this thread will single-step and log execution." },
80 { nullptr , OptionValue::eTypeInvalid, false, 0 , nullptr, nullptr, nullptr }
Greg Clayton67cc0632012-08-22 17:17:09 +000081};
82
83enum {
Jim Ingham4b4b2472014-03-13 02:47:14 +000084 ePropertyStepInAvoidsNoDebug,
85 ePropertyStepOutAvoidsNoDebug,
Greg Clayton67cc0632012-08-22 17:17:09 +000086 ePropertyStepAvoidRegex,
Jim Ingham4da62062014-01-23 21:52:47 +000087 ePropertyStepAvoidLibraries,
Greg Clayton67cc0632012-08-22 17:17:09 +000088 ePropertyEnableThreadTrace
89};
90
Greg Clayton67cc0632012-08-22 17:17:09 +000091class ThreadOptionValueProperties : public OptionValueProperties
92{
93public:
94 ThreadOptionValueProperties (const ConstString &name) :
95 OptionValueProperties (name)
96 {
97 }
98
99 // This constructor is used when creating ThreadOptionValueProperties when it
100 // is part of a new lldb_private::Thread instance. It will copy all current
101 // global property values as needed
102 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +0000103 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +0000104 {
105 }
106
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000107 const Property *
108 GetPropertyAtIndex(const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const override
Greg Clayton67cc0632012-08-22 17:17:09 +0000109 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000110 // When getting the value for a key from the thread options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +0000111 // try and grab the setting from the current thread if there is one. Else we just
112 // use the one from this instance.
113 if (exe_ctx)
114 {
115 Thread *thread = exe_ctx->GetThreadPtr();
116 if (thread)
117 {
118 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
119 if (this != instance_properties)
120 return instance_properties->ProtectedGetPropertyAtIndex (idx);
121 }
122 }
123 return ProtectedGetPropertyAtIndex (idx);
124 }
125};
126
Greg Clayton67cc0632012-08-22 17:17:09 +0000127ThreadProperties::ThreadProperties (bool is_global) :
128 Properties ()
129{
130 if (is_global)
131 {
132 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
133 m_collection_sp->Initialize(g_properties);
134 }
135 else
136 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
137}
138
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000139ThreadProperties::~ThreadProperties() = default;
Greg Clayton67cc0632012-08-22 17:17:09 +0000140
141const RegularExpression *
142ThreadProperties::GetSymbolsToAvoidRegexp()
143{
144 const uint32_t idx = ePropertyStepAvoidRegex;
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000145 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +0000146}
147
Jim Ingham4da62062014-01-23 21:52:47 +0000148FileSpecList &
149ThreadProperties::GetLibrariesToAvoid() const
150{
151 const uint32_t idx = ePropertyStepAvoidLibraries;
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000152 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Jim Ingham4da62062014-01-23 21:52:47 +0000153 assert(option_value);
154 return option_value->GetCurrentValue();
155}
156
Greg Clayton67cc0632012-08-22 17:17:09 +0000157bool
158ThreadProperties::GetTraceEnabledState() const
159{
160 const uint32_t idx = ePropertyEnableThreadTrace;
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000161 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +0000162}
163
Jim Ingham4b4b2472014-03-13 02:47:14 +0000164bool
165ThreadProperties::GetStepInAvoidsNoDebug() const
166{
167 const uint32_t idx = ePropertyStepInAvoidsNoDebug;
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000168 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham4b4b2472014-03-13 02:47:14 +0000169}
170
171bool
172ThreadProperties::GetStepOutAvoidsNoDebug() const
173{
174 const uint32_t idx = ePropertyStepOutAvoidsNoDebug;
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000175 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham4b4b2472014-03-13 02:47:14 +0000176}
177
Jim Ingham4f465cf2012-10-10 18:32:14 +0000178//------------------------------------------------------------------
179// Thread Event Data
180//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000181
Jim Ingham4f465cf2012-10-10 18:32:14 +0000182const ConstString &
183Thread::ThreadEventData::GetFlavorString ()
184{
185 static ConstString g_flavor ("Thread::ThreadEventData");
186 return g_flavor;
187}
188
189Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
190 m_thread_sp (thread_sp),
191 m_stack_id ()
192{
193}
194
195Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
196 m_thread_sp (thread_sp),
197 m_stack_id (stack_id)
198{
199}
200
201Thread::ThreadEventData::ThreadEventData () :
202 m_thread_sp (),
203 m_stack_id ()
204{
205}
206
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000207Thread::ThreadEventData::~ThreadEventData() = default;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000208
209void
210Thread::ThreadEventData::Dump (Stream *s) const
211{
Jim Ingham4f465cf2012-10-10 18:32:14 +0000212}
213
214const Thread::ThreadEventData *
215Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
216{
217 if (event_ptr)
218 {
219 const EventData *event_data = event_ptr->GetData();
220 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
221 return static_cast <const ThreadEventData *> (event_ptr->GetData());
222 }
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000223 return nullptr;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000224}
225
226ThreadSP
227Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
228{
229 ThreadSP thread_sp;
230 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
231 if (event_data)
232 thread_sp = event_data->GetThread();
233 return thread_sp;
234}
235
236StackID
237Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
238{
239 StackID stack_id;
240 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
241 if (event_data)
242 stack_id = event_data->GetStackID();
243 return stack_id;
244}
245
Jason Molendab57e4a12013-11-04 09:33:30 +0000246StackFrameSP
Jim Ingham4f465cf2012-10-10 18:32:14 +0000247Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
248{
249 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
Jason Molendab57e4a12013-11-04 09:33:30 +0000250 StackFrameSP frame_sp;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000251 if (event_data)
252 {
253 ThreadSP thread_sp = event_data->GetThread();
254 if (thread_sp)
255 {
256 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
257 }
258 }
259 return frame_sp;
260}
261
262//------------------------------------------------------------------
263// Thread class
264//------------------------------------------------------------------
265
266ConstString &
267Thread::GetStaticBroadcasterClass ()
268{
269 static ConstString class_name ("lldb.thread");
270 return class_name;
271}
272
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000273Thread::Thread(Process &process, lldb::tid_t tid, bool use_invalid_index_id)
274 : ThreadProperties(false),
275 UserID(tid),
276 Broadcaster(process.GetTarget().GetDebugger().GetBroadcasterManager(),
277 Thread::GetStaticBroadcasterClass().AsCString()),
278 m_process_wp(process.shared_from_this()),
279 m_stop_info_sp(),
280 m_stop_info_stop_id(0),
281 m_stop_info_override_stop_id(0),
282 m_index_id(use_invalid_index_id ? LLDB_INVALID_INDEX32 : process.GetNextThreadIndexID(tid)),
283 m_reg_context_sp(),
284 m_state(eStateUnloaded),
285 m_state_mutex(),
286 m_plan_stack(),
287 m_completed_plan_stack(),
288 m_frame_mutex(),
289 m_curr_frames_sp(),
290 m_prev_frames_sp(),
291 m_resume_signal(LLDB_INVALID_SIGNAL_NUMBER),
292 m_resume_state(eStateRunning),
293 m_temporary_resume_state(eStateRunning),
294 m_unwinder_ap(),
295 m_destroy_called(false),
296 m_override_should_notify(eLazyBoolCalculate),
297 m_extended_info_fetched(false),
298 m_extended_info()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299{
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000300 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301 if (log)
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000302 log->Printf("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")", static_cast<void *>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303
Jim Ingham4f465cf2012-10-10 18:32:14 +0000304 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 QueueFundamentalPlan(true);
306}
307
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308Thread::~Thread()
309{
Greg Clayton5160ce52013-03-27 23:08:40 +0000310 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000312 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")",
313 static_cast<void*>(this), GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000314 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
315 assert (m_destroy_called);
316}
317
318void
319Thread::DestroyThread ()
320{
Jim Inghamb1499242013-10-18 17:11:02 +0000321 // Tell any plans on the plan stacks that the thread is being destroyed since
322 // any plans that have a thread go away in the middle of might need
323 // to do cleanup, or in some cases NOT do cleanup...
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000324 for (auto plan : m_plan_stack)
325 plan->ThreadDestroyed();
326
Jim Inghamb1499242013-10-18 17:11:02 +0000327 for (auto plan : m_discarded_plan_stack)
328 plan->ThreadDestroyed();
329
330 for (auto plan : m_completed_plan_stack)
331 plan->ThreadDestroyed();
332
Greg Clayton35a4cc52012-10-29 20:52:08 +0000333 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000334 m_plan_stack.clear();
335 m_discarded_plan_stack.clear();
336 m_completed_plan_stack.clear();
Greg Clayton6e10f142013-07-30 00:23:06 +0000337
338 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the
339 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
340 // without checking first whether it is destroyed, they won't crash.
341 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
342 m_plan_stack.push_back (null_plan_sp);
343
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000344 m_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000345 m_reg_context_sp.reset();
346 m_unwinder_ap.reset();
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000347 std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
Greg Clayton35a4cc52012-10-29 20:52:08 +0000348 m_curr_frames_sp.reset();
349 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350}
351
Jim Ingham4f465cf2012-10-10 18:32:14 +0000352void
353Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
354{
355 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
356 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
357}
358
Jason Molendaef7d6412015-08-06 03:27:10 +0000359lldb::StackFrameSP
360Thread::GetSelectedFrame()
361{
362 StackFrameListSP stack_frame_list_sp(GetStackFrameList());
363 StackFrameSP frame_sp = stack_frame_list_sp->GetFrameAtIndex (stack_frame_list_sp->GetSelectedFrameIndex());
364 FunctionOptimizationWarning (frame_sp.get());
365 return frame_sp;
366}
367
Jim Ingham4f465cf2012-10-10 18:32:14 +0000368uint32_t
Jason Molendab57e4a12013-11-04 09:33:30 +0000369Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
Jim Ingham4f465cf2012-10-10 18:32:14 +0000370{
371 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
372 if (broadcast)
373 BroadcastSelectedFrameChange(frame->GetStackID());
Jason Molendaef7d6412015-08-06 03:27:10 +0000374 FunctionOptimizationWarning (frame);
Jim Ingham4f465cf2012-10-10 18:32:14 +0000375 return ret_value;
376}
377
378bool
379Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
380{
Jason Molendab57e4a12013-11-04 09:33:30 +0000381 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
Jim Ingham4f465cf2012-10-10 18:32:14 +0000382 if (frame_sp)
383 {
384 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
385 if (broadcast)
386 BroadcastSelectedFrameChange(frame_sp->GetStackID());
Jason Molendaef7d6412015-08-06 03:27:10 +0000387 FunctionOptimizationWarning (frame_sp.get());
Jim Ingham4f465cf2012-10-10 18:32:14 +0000388 return true;
389 }
390 else
391 return false;
392}
393
Jim Ingham93208b82013-01-31 21:46:01 +0000394bool
395Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
396{
397 const bool broadcast = true;
398 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
399 if (success)
400 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000401 StackFrameSP frame_sp = GetSelectedFrame();
Jim Ingham93208b82013-01-31 21:46:01 +0000402 if (frame_sp)
403 {
404 bool already_shown = false;
405 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
406 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
407 {
408 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
409 }
410
411 bool show_frame_info = true;
412 bool show_source = !already_shown;
Jason Molendaef7d6412015-08-06 03:27:10 +0000413 FunctionOptimizationWarning (frame_sp.get());
Jim Ingham93208b82013-01-31 21:46:01 +0000414 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
415 }
416 return false;
417 }
418 else
419 return false;
420}
421
Jason Molendaef7d6412015-08-06 03:27:10 +0000422void
423Thread::FunctionOptimizationWarning (StackFrame *frame)
424{
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000425 if (frame && frame->HasDebugInformation() && GetProcess()->GetWarningsOptimization())
Jason Molendaef7d6412015-08-06 03:27:10 +0000426 {
Jason Molenda03c9cd02015-08-06 21:54:29 +0000427 SymbolContext sc = frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextModule);
Jason Molenda484900b2015-08-10 07:55:25 +0000428 GetProcess()->PrintWarningOptimization (sc);
Jason Molendaef7d6412015-08-06 03:27:10 +0000429 }
430}
431
Jim Inghamb15bfc72010-10-20 00:39:53 +0000432lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000433Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434{
Greg Clayton6e10f142013-07-30 00:23:06 +0000435 if (m_destroy_called)
436 return m_stop_info_sp;
437
Jim Inghamb15bfc72010-10-20 00:39:53 +0000438 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000439 ProcessSP process_sp (GetProcess());
440 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000441 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000442 {
Jim Ingham30fadaf2014-07-08 01:07:32 +0000443 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject(), GetExpressionVariable());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000444 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000445 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000446 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000447 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
448 (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 +0000449 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000450 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000451 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000452 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000453 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000454 GetPrivateStopInfo ();
455 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000456 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000457 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458}
459
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000460lldb::StopInfoSP
461Thread::GetPrivateStopInfo ()
462{
Greg Clayton6e10f142013-07-30 00:23:06 +0000463 if (m_destroy_called)
464 return m_stop_info_sp;
465
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000466 ProcessSP process_sp (GetProcess());
467 if (process_sp)
468 {
Daniel Malea246cb612013-05-14 15:20:12 +0000469 const uint32_t process_stop_id = process_sp->GetStopID();
470 if (m_stop_info_stop_id != process_stop_id)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000471 {
Daniel Malea246cb612013-05-14 15:20:12 +0000472 if (m_stop_info_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000473 {
Daniel Malea246cb612013-05-14 15:20:12 +0000474 if (m_stop_info_sp->IsValid()
475 || IsStillAtLastBreakpointHit()
476 || GetCurrentPlan()->IsVirtualStep())
477 SetStopInfo (m_stop_info_sp);
478 else
479 m_stop_info_sp.reset();
480 }
481
482 if (!m_stop_info_sp)
483 {
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000484 if (!CalculateStopInfo())
Daniel Malea246cb612013-05-14 15:20:12 +0000485 SetStopInfo (StopInfoSP());
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000486 }
487 }
Greg Claytona97c4d22014-12-09 23:31:02 +0000488
489 // The stop info can be manually set by calling Thread::SetStopInfo()
490 // prior to this function ever getting called, so we can't rely on
491 // "m_stop_info_stop_id != process_stop_id" as the condition for
492 // the if statement below, we must also check the stop info to see
493 // if we need to override it. See the header documentation in
494 // Process::GetStopInfoOverrideCallback() for more information on
495 // the stop info override callback.
496 if (m_stop_info_override_stop_id != process_stop_id)
497 {
498 m_stop_info_override_stop_id = process_stop_id;
499 if (m_stop_info_sp)
500 {
501 ArchSpec::StopInfoOverrideCallbackType callback = GetProcess()->GetStopInfoOverrideCallback();
502 if (callback)
503 callback(*this);
504 }
505 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000506 }
507 return m_stop_info_sp;
508}
509
Greg Clayton97d5cf02012-09-25 02:40:06 +0000510lldb::StopReason
511Thread::GetStopReason()
512{
513 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
514 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000515 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000516 return eStopReasonNone;
517}
518
Greg Clayton2e309072015-07-17 23:42:28 +0000519bool
520Thread::StopInfoIsUpToDate() const
521{
522 ProcessSP process_sp (GetProcess());
523 if (process_sp)
524 return m_stop_info_stop_id == process_sp->GetStopID();
525 else
526 return true; // Process is no longer around so stop info is always up to date...
527}
Greg Clayton97d5cf02012-09-25 02:40:06 +0000528
Jim Ingham77787032011-01-20 02:03:18 +0000529void
530Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
531{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000532 m_stop_info_sp = stop_info_sp;
533 if (m_stop_info_sp)
Jim Ingham221d51c2013-05-08 00:35:16 +0000534 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000535 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham221d51c2013-05-08 00:35:16 +0000536 // If we are overriding the ShouldReportStop, do that here:
537 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000538 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000539 }
540
Greg Clayton1ac04c32012-02-21 00:09:25 +0000541 ProcessSP process_sp (GetProcess());
542 if (process_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000543 m_stop_info_stop_id = process_sp->GetStopID();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000544 else
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000545 m_stop_info_stop_id = UINT32_MAX;
Greg Clayton8cda7f02013-05-21 21:55:59 +0000546 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
547 if (log)
Ed Maste2bc76432014-06-25 00:38:35 +0000548 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000549 static_cast<void*>(this), GetID(),
550 stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>",
551 m_stop_info_stop_id);
Jim Ingham77787032011-01-20 02:03:18 +0000552}
553
554void
Jim Ingham221d51c2013-05-08 00:35:16 +0000555Thread::SetShouldReportStop (Vote vote)
556{
557 if (vote == eVoteNoOpinion)
558 return;
559 else
560 {
561 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000562 if (m_stop_info_sp)
563 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000564 }
565}
566
567void
Jim Ingham77787032011-01-20 02:03:18 +0000568Thread::SetStopInfoToNothing()
569{
570 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
571 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
572 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
573}
574
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575bool
576Thread::ThreadStoppedForAReason (void)
577{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000578 return (bool) GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579}
580
Jim Ingham77787032011-01-20 02:03:18 +0000581bool
582Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
583{
Greg Claytonf74cf862013-11-13 23:28:31 +0000584 saved_state.register_backup_sp.reset();
585 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
586 if (frame_sp)
587 {
588 lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
589 if (reg_checkpoint_sp)
590 {
591 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
592 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp))
593 saved_state.register_backup_sp = reg_checkpoint_sp;
594 }
595 }
596 if (!saved_state.register_backup_sp)
Jim Ingham77787032011-01-20 02:03:18 +0000597 return false;
598
599 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000600 ProcessSP process_sp (GetProcess());
601 if (process_sp)
602 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000603 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
604
Jim Ingham77787032011-01-20 02:03:18 +0000605 return true;
606}
607
608bool
Jim Ingham8559a352012-11-26 23:52:18 +0000609Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000610{
Greg Claytonf74cf862013-11-13 23:28:31 +0000611 if (saved_state.register_backup_sp)
612 {
613 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
614 if (frame_sp)
615 {
616 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
617 if (reg_ctx_sp)
618 {
619 bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp);
620
621 // Clear out all stack frames as our world just changed.
622 ClearStackFrames();
623 reg_ctx_sp->InvalidateIfNeeded(true);
624 if (m_unwinder_ap.get())
625 m_unwinder_ap->Clear();
626 return ret;
627 }
628 }
629 }
630 return false;
Jim Ingham8559a352012-11-26 23:52:18 +0000631}
632
633bool
634Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
635{
Jim Ingham0c270682011-01-25 02:47:23 +0000636 if (saved_state.stop_info_sp)
637 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000638 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000639 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000640 return true;
641}
642
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643StateType
644Thread::GetState() const
645{
646 // If any other threads access this we will need a mutex for it
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000647 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648 return m_state;
649}
650
651void
652Thread::SetState(StateType state)
653{
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000654 std::lock_guard<std::recursive_mutex> guard(m_state_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655 m_state = state;
656}
657
658void
659Thread::WillStop()
660{
661 ThreadPlan *current_plan = GetCurrentPlan();
662
663 // FIXME: I may decide to disallow threads with no plans. In which
664 // case this should go to an assert.
665
666 if (!current_plan)
667 return;
668
669 current_plan->WillStop();
670}
671
672void
673Thread::SetupForResume ()
674{
675 if (GetResumeState() != eStateSuspended)
676 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
678 // telling the current plan it will resume, since we might change what the current
679 // plan is.
680
Greg Clayton1afa68e2013-04-02 20:32:37 +0000681 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
682 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 {
Greg Claytona97c4d22014-12-09 23:31:02 +0000684 const addr_t thread_pc = reg_ctx_sp->GetPC();
685 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000686 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000687 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000688 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
689 // special to step over a breakpoint.
690
691 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000692
Greg Claytona97c4d22014-12-09 23:31:02 +0000693 bool push_step_over_bp_plan = false;
694 if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint)
695 {
696 ThreadPlanStepOverBreakpoint *bp_plan = (ThreadPlanStepOverBreakpoint *)cur_plan;
697 if (bp_plan->GetBreakpointLoadAddress() != thread_pc)
698 push_step_over_bp_plan = true;
699 }
700 else
701 push_step_over_bp_plan = true;
702
703 if (push_step_over_bp_plan)
Greg Clayton1afa68e2013-04-02 20:32:37 +0000704 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000705 ThreadPlanSP step_bp_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
706 if (step_bp_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000708 step_bp_plan_sp->SetPrivate (true);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000709
710 if (GetCurrentPlan()->RunState() != eStateStepping)
711 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000712 ThreadPlanStepOverBreakpoint *step_bp_plan
713 = static_cast<ThreadPlanStepOverBreakpoint *>(step_bp_plan_sp.get());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000714 step_bp_plan->SetAutoContinue(true);
715 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000716 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 }
719 }
720 }
721 }
722}
723
724bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000725Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726{
727 // At this point clear the completed plan stack.
728 m_completed_plan_stack.clear();
729 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000730 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731
Jim Ingham569aaf92015-11-06 22:45:57 +0000732 StateType prev_resume_state = GetTemporaryResumeState();
733
734 SetTemporaryResumeState(resume_state);
Jim Ingham92087d82012-01-31 23:09:20 +0000735
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000736 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
737 if (backing_thread_sp)
Jim Ingham569aaf92015-11-06 22:45:57 +0000738 backing_thread_sp->SetTemporaryResumeState(resume_state);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000739
Jim Ingham569aaf92015-11-06 22:45:57 +0000740 // Make sure m_stop_info_sp is valid. Don't do this for threads we suspended in the previous run.
741 if (prev_resume_state != eStateSuspended)
742 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000743
Jim Ingham92087d82012-01-31 23:09:20 +0000744 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
745 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
746 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
747 // about the fact that we are resuming...
Jim Inghamacbea8f2015-06-02 20:26:13 +0000748 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000749 if (m_stop_info_stop_id == process_stop_id &&
750 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000751 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000752 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000753 if (stop_info)
754 stop_info->WillResume (resume_state);
755 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756
757 // Tell all the plans that we are about to resume in case they need to clear any state.
758 // We distinguish between the plan on the top of the stack and the lower
759 // plans in case a plan needs to do any special business before it runs.
760
Greg Claytond1d06e42013-04-20 00:27:58 +0000761 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000763 if (plan_ptr)
764 {
765 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000767 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != nullptr)
Greg Claytond1d06e42013-04-20 00:27:58 +0000768 {
769 plan_ptr->WillResume (resume_state, false);
770 }
771
772 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
773 // In that case, don't reset it here.
774
775 if (need_to_resume && resume_state != eStateSuspended)
776 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000777 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000778 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000779 }
780
Greg Clayton160c9d82013-05-01 21:54:04 +0000781 if (need_to_resume)
782 {
783 ClearStackFrames();
784 // Let Thread subclasses do any special work they need to prior to resuming
785 WillResume (resume_state);
786 }
787
Jim Ingham513c6bb2012-09-01 01:02:41 +0000788 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789}
790
791void
792Thread::DidResume ()
793{
794 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
795}
796
Andrew Kaylor29d65742013-05-10 17:19:04 +0000797void
798Thread::DidStop ()
799{
800 SetState (eStateStopped);
801}
802
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803bool
804Thread::ShouldStop (Event* event_ptr)
805{
806 ThreadPlan *current_plan = GetCurrentPlan();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000807
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 bool should_stop = true;
809
Greg Clayton5160ce52013-03-27 23:08:40 +0000810 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000811
Jim Ingham92087d82012-01-31 23:09:20 +0000812 if (GetResumeState () == eStateSuspended)
813 {
814 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000815 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 +0000816 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000817 return false;
818 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000819
Jim Ingham92087d82012-01-31 23:09:20 +0000820 if (GetTemporaryResumeState () == eStateSuspended)
821 {
822 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000823 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 +0000824 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000825 return false;
826 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000827
Daniel Malea246cb612013-05-14 15:20:12 +0000828 // Based on the current thread plan and process stop info, check if this
829 // thread caused the process to stop. NOTE: this must take place before
830 // the plan is moved from the current plan stack to the completed plan
831 // stack.
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000832 if (!ThreadStoppedForAReason())
Jim Ingham92087d82012-01-31 23:09:20 +0000833 {
834 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000835 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 +0000836 __FUNCTION__, GetID (), GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000837 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000838 return false;
839 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000840
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 if (log)
842 {
Jim Inghamdee1bc92013-06-22 00:27:45 +0000843 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 +0000844 __FUNCTION__, static_cast<void*>(this), GetID (),
Greg Clayton160c9d82013-05-01 21:54:04 +0000845 GetProtocolID (),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000846 GetRegisterContext()
847 ? GetRegisterContext()->GetPC()
848 : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000849 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000850 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000851 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000853 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000855
Jim Ingham06e827c2010-11-11 19:26:09 +0000856 // The top most plan always gets to do the trace log...
857 current_plan->DoTraceLog ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000858
Jim Ingham6d66ce62012-04-20 21:16:56 +0000859 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
860 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
861 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000862 StopInfoSP private_stop_info (GetPrivateStopInfo());
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000863 if (private_stop_info && !private_stop_info->ShouldStopSynchronous(event_ptr))
Jim Ingham6d66ce62012-04-20 21:16:56 +0000864 {
865 if (log)
866 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
867 return false;
868 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869
Jim Inghambad39e42012-09-05 21:12:49 +0000870 // If we've already been restarted, don't query the plans since the state they would examine is not current.
871 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
872 return false;
873
874 // Before the plans see the state of the world, calculate the current inlined depth.
875 GetStackFrameList()->CalculateCurrentInlinedDepth();
876
Jim Ingham25f66702011-12-03 01:52:59 +0000877 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
878 // If that plan is still working, then we don't need to do any more work. If the plan that explains
879 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
880 // whether they still need to do more work.
881
882 bool done_processing_current_plan = false;
883
Jim Ingham0161b492013-02-09 01:29:05 +0000884 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000885 {
886 if (current_plan->TracerExplainsStop())
887 {
888 done_processing_current_plan = true;
889 should_stop = false;
890 }
891 else
892 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000893 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000894 // does and let it handle the situation.
895 ThreadPlan *plan_ptr = current_plan;
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000896 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != nullptr)
Jim Ingham25f66702011-12-03 01:52:59 +0000897 {
Jim Ingham0161b492013-02-09 01:29:05 +0000898 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000899 {
900 should_stop = plan_ptr->ShouldStop (event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000901
Jim Ingham25f66702011-12-03 01:52:59 +0000902 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
903 // and all the plans below it off the stack.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000904
Jim Ingham25f66702011-12-03 01:52:59 +0000905 if (plan_ptr->MischiefManaged())
906 {
Jim Ingham031177e2012-05-11 23:49:49 +0000907 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000908 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000909
Jim Ingham25f66702011-12-03 01:52:59 +0000910 do
911 {
912 if (should_stop)
913 current_plan->WillStop();
914 PopPlan();
915 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000916 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
917 // Now, if the responsible plan was not "Okay to discard" then we're done,
918 // otherwise we forward this to the next plan in the stack below.
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000919 done_processing_current_plan = (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard());
Jim Ingham25f66702011-12-03 01:52:59 +0000920 }
921 else
922 done_processing_current_plan = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000923
Jim Ingham25f66702011-12-03 01:52:59 +0000924 break;
925 }
Jim Ingham25f66702011-12-03 01:52:59 +0000926 }
927 }
928 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000929
Jim Ingham25f66702011-12-03 01:52:59 +0000930 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000932 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000933
Jim Ingham10c4b242011-10-15 00:23:43 +0000934 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000935 log->Printf("Plan %s explains stop, auto-continue %i.",
936 current_plan->GetName(), over_ride_stop);
937
Jim Ingham0f16e732011-02-08 05:20:59 +0000938 // We're starting from the base plan, so just let it decide;
939 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000941 should_stop = current_plan->ShouldStop (event_ptr);
942 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000943 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000944 }
945 else
946 {
947 // Otherwise, don't let the base plan override what the other plans say to do, since
948 // presumably if there were other plans they would know what to do...
949 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000951 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000953
Jim Ingham0f16e732011-02-08 05:20:59 +0000954 should_stop = current_plan->ShouldStop(event_ptr);
955 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000956 log->Printf("Plan %s should stop: %d.",
957 current_plan->GetName(), should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000958 if (current_plan->MischiefManaged())
959 {
960 if (should_stop)
961 current_plan->WillStop();
962
963 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
964 // Otherwise, see if the plan's parent wants to stop.
965
966 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
967 {
968 PopPlan();
969 break;
970 }
971 else
972 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000973 PopPlan();
974
975 current_plan = GetCurrentPlan();
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +0000976 if (current_plan == nullptr)
Jim Ingham0f16e732011-02-08 05:20:59 +0000977 {
978 break;
979 }
980 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981 }
982 else
983 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000984 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986 }
987 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000988
Jim Inghamb01e7422010-06-19 04:45:32 +0000989 if (over_ride_stop)
990 should_stop = false;
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000991 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000992
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000993 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
994 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
995 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
996 // This code clears stale plans off the stack.
997
998 if (should_stop)
999 {
1000 ThreadPlan *plan_ptr = GetCurrentPlan();
1001 while (!PlanIsBasePlan(plan_ptr))
Jim Ingham64e7ead2012-05-03 21:19:36 +00001002 {
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001003 bool stale = plan_ptr->IsPlanStale ();
1004 ThreadPlan *examined_plan = plan_ptr;
1005 plan_ptr = GetPreviousPlan (examined_plan);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001006
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001007 if (stale)
1008 {
1009 if (log)
1010 log->Printf("Plan %s being discarded in cleanup, it says it is already done.",
1011 examined_plan->GetName());
1012 DiscardThreadPlansUpToPlan(examined_plan);
Jim Ingham64e7ead2012-05-03 21:19:36 +00001013 }
1014 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016
Jim Ingham10c4b242011-10-15 00:23:43 +00001017 if (log)
1018 {
1019 StreamString s;
1020 s.IndentMore();
1021 DumpThreadPlans(&s);
1022 log->Printf ("Plan stack final state:\n%s", s.GetData());
1023 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
1024 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025 return should_stop;
1026}
1027
1028Vote
1029Thread::ShouldReportStop (Event* event_ptr)
1030{
1031 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +00001032 StateType temp_thread_state = GetTemporaryResumeState();
1033
Greg Clayton5160ce52013-03-27 23:08:40 +00001034 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +00001035
1036 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
1037 {
1038 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001039 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 +00001040 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001041 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042
Jim Ingham92087d82012-01-31 23:09:20 +00001043 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
1044 {
1045 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001046 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 +00001047 return eVoteNoOpinion;
1048 }
1049
1050 if (!ThreadStoppedForAReason())
1051 {
1052 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001053 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 +00001054 return eVoteNoOpinion;
1055 }
1056
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057 if (m_completed_plan_stack.size() > 0)
1058 {
1059 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +00001060 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001061 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 +00001062 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
1063 }
1064 else
Greg Clayton2cad65a2010-09-03 17:10:42 +00001065 {
Jim Ingham0161b492013-02-09 01:29:05 +00001066 Vote thread_vote = eVoteNoOpinion;
1067 ThreadPlan *plan_ptr = GetCurrentPlan();
1068 while (1)
1069 {
1070 if (plan_ptr->PlanExplainsStop(event_ptr))
1071 {
1072 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1073 break;
1074 }
1075 if (PlanIsBasePlan(plan_ptr))
1076 break;
1077 else
1078 plan_ptr = GetPreviousPlan(plan_ptr);
1079 }
Greg Clayton2cad65a2010-09-03 17:10:42 +00001080 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001081 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 +00001082
1083 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001084 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001085}
1086
1087Vote
1088Thread::ShouldReportRun (Event* event_ptr)
1089{
1090 StateType thread_state = GetResumeState ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001091
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092 if (thread_state == eStateSuspended
1093 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +00001094 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +00001096 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001097
Greg Clayton5160ce52013-03-27 23:08:40 +00001098 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099 if (m_completed_plan_stack.size() > 0)
1100 {
1101 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +00001102 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001103 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 +00001104 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001105 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001106 m_completed_plan_stack.back()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001107
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
1109 }
1110 else
Jim Ingham444586b2011-01-24 06:34:17 +00001111 {
1112 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001113 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 +00001114 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001115 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001116 GetCurrentPlan()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001117
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +00001119 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120}
1121
Jim Ingham1b54c882010-06-16 02:00:15 +00001122bool
1123Thread::MatchesSpec (const ThreadSpec *spec)
1124{
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001125 return (spec == nullptr) ? true : spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +00001126}
1127
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128void
1129Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1130{
1131 if (thread_plan_sp)
1132 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001133 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1134 if (!thread_plan_sp->GetThreadPlanTracer())
1135 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001136 m_plan_stack.push_back (thread_plan_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001137
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138 thread_plan_sp->DidPush();
1139
Greg Clayton5160ce52013-03-27 23:08:40 +00001140 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141 if (log)
1142 {
1143 StreamString s;
1144 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamdee1bc92013-06-22 00:27:45 +00001145 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001146 static_cast<void*>(this), s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001147 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001148 }
1149 }
1150}
1151
1152void
1153Thread::PopPlan ()
1154{
Greg Clayton5160ce52013-03-27 23:08:40 +00001155 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001157 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 return;
1159 else
1160 {
1161 ThreadPlanSP &plan = m_plan_stack.back();
1162 if (log)
1163 {
Daniel Malead01b2952012-11-29 21:49:15 +00001164 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165 }
1166 m_completed_plan_stack.push_back (plan);
1167 plan->WillPop();
1168 m_plan_stack.pop_back();
1169 }
1170}
1171
1172void
1173Thread::DiscardPlan ()
1174{
Jim Inghamdee1bc92013-06-22 00:27:45 +00001175 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001176 if (m_plan_stack.size() > 1)
1177 {
1178 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamdee1bc92013-06-22 00:27:45 +00001179 if (log)
1180 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1181
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182 m_discarded_plan_stack.push_back (plan);
1183 plan->WillPop();
1184 m_plan_stack.pop_back();
1185 }
1186}
1187
1188ThreadPlan *
1189Thread::GetCurrentPlan ()
1190{
Jim Inghame1471232012-04-19 00:17:05 +00001191 // There will always be at least the base plan. If somebody is mucking with a
1192 // thread with an empty plan stack, we should assert right away.
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001193 return m_plan_stack.empty() ? nullptr : m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194}
1195
1196ThreadPlanSP
1197Thread::GetCompletedPlan ()
1198{
1199 ThreadPlanSP empty_plan_sp;
1200 if (!m_completed_plan_stack.empty())
1201 {
1202 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1203 {
1204 ThreadPlanSP completed_plan_sp;
1205 completed_plan_sp = m_completed_plan_stack[i];
1206 if (!completed_plan_sp->GetPrivate ())
1207 return completed_plan_sp;
1208 }
1209 }
1210 return empty_plan_sp;
1211}
1212
Jim Ingham73ca05a2011-12-17 01:35:57 +00001213ValueObjectSP
1214Thread::GetReturnValueObject ()
1215{
1216 if (!m_completed_plan_stack.empty())
1217 {
1218 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1219 {
1220 ValueObjectSP return_valobj_sp;
1221 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1222 if (return_valobj_sp)
Greg Claytone5214572015-07-01 23:28:31 +00001223 return return_valobj_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001224 }
1225 }
1226 return ValueObjectSP();
1227}
1228
Sean Callananbc8ac342015-09-04 20:49:51 +00001229ExpressionVariableSP
Jim Ingham30fadaf2014-07-08 01:07:32 +00001230Thread::GetExpressionVariable ()
1231{
1232 if (!m_completed_plan_stack.empty())
1233 {
1234 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1235 {
Sean Callananbc8ac342015-09-04 20:49:51 +00001236 ExpressionVariableSP expression_variable_sp;
Jim Ingham30fadaf2014-07-08 01:07:32 +00001237 expression_variable_sp = m_completed_plan_stack[i]->GetExpressionVariable();
1238 if (expression_variable_sp)
Greg Claytone5214572015-07-01 23:28:31 +00001239 return expression_variable_sp;
Jim Ingham30fadaf2014-07-08 01:07:32 +00001240 }
1241 }
Sean Callananbc8ac342015-09-04 20:49:51 +00001242 return ExpressionVariableSP();
Jim Ingham30fadaf2014-07-08 01:07:32 +00001243}
1244
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245bool
1246Thread::IsThreadPlanDone (ThreadPlan *plan)
1247{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001248 if (!m_completed_plan_stack.empty())
1249 {
1250 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1251 {
1252 if (m_completed_plan_stack[i].get() == plan)
1253 return true;
1254 }
1255 }
1256 return false;
1257}
1258
1259bool
1260Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1261{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262 if (!m_discarded_plan_stack.empty())
1263 {
1264 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1265 {
1266 if (m_discarded_plan_stack[i].get() == plan)
1267 return true;
1268 }
1269 }
1270 return false;
1271}
1272
1273ThreadPlan *
1274Thread::GetPreviousPlan (ThreadPlan *current_plan)
1275{
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001276 if (current_plan == nullptr)
1277 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001278
1279 int stack_size = m_completed_plan_stack.size();
1280 for (int i = stack_size - 1; i > 0; i--)
1281 {
1282 if (current_plan == m_completed_plan_stack[i].get())
1283 return m_completed_plan_stack[i-1].get();
1284 }
1285
1286 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1287 {
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001288 return GetCurrentPlan();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001289 }
1290
1291 stack_size = m_plan_stack.size();
1292 for (int i = stack_size - 1; i > 0; i--)
1293 {
1294 if (current_plan == m_plan_stack[i].get())
1295 return m_plan_stack[i-1].get();
1296 }
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001297 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001298}
1299
1300void
1301Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1302{
1303 if (abort_other_plans)
1304 DiscardThreadPlans(true);
1305
1306 PushPlan (thread_plan_sp);
1307}
1308
Jim Ingham06e827c2010-11-11 19:26:09 +00001309void
1310Thread::EnableTracer (bool value, bool single_stepping)
1311{
1312 int stack_size = m_plan_stack.size();
1313 for (int i = 0; i < stack_size; i++)
1314 {
1315 if (m_plan_stack[i]->GetThreadPlanTracer())
1316 {
1317 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1318 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1319 }
1320 }
1321}
1322
1323void
1324Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1325{
1326 int stack_size = m_plan_stack.size();
1327 for (int i = 0; i < stack_size; i++)
1328 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1329}
1330
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001331bool
1332Thread::DiscardUserThreadPlansUpToIndex (uint32_t thread_index)
1333{
1334 // Count the user thread plans from the back end to get the number of the one we want
1335 // to discard:
1336
1337 uint32_t idx = 0;
1338 ThreadPlan *up_to_plan_ptr = nullptr;
1339
1340 for (ThreadPlanSP plan_sp : m_plan_stack)
1341 {
1342 if (plan_sp->GetPrivate())
1343 continue;
1344 if (idx == thread_index)
1345 {
1346 up_to_plan_ptr = plan_sp.get();
1347 break;
1348 }
1349 else
1350 idx++;
1351 }
1352
1353 if (up_to_plan_ptr == nullptr)
1354 return false;
1355
1356 DiscardThreadPlansUpToPlan(up_to_plan_ptr);
1357 return true;
1358}
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001359
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001360void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001361Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1362{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001363 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1364}
1365
1366void
1367Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1368{
Greg Clayton5160ce52013-03-27 23:08:40 +00001369 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001370 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001371 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p",
1372 GetID(), static_cast<void*>(up_to_plan_ptr));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001373
1374 int stack_size = m_plan_stack.size();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001375
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001376 // If the input plan is nullptr, discard all plans. Otherwise make sure this plan is in the
Jim Ingham399f1ca2010-11-05 19:25:48 +00001377 // stack, and if so discard up to and including it.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001378
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001379 if (up_to_plan_ptr == nullptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001380 {
1381 for (int i = stack_size - 1; i > 0; i--)
1382 DiscardPlan();
1383 }
1384 else
1385 {
1386 bool found_it = false;
1387 for (int i = stack_size - 1; i > 0; i--)
1388 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001389 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001390 found_it = true;
1391 }
1392 if (found_it)
1393 {
1394 bool last_one = false;
1395 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1396 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001397 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001398 last_one = true;
1399 DiscardPlan();
1400 }
1401 }
1402 }
Jim Ingham399f1ca2010-11-05 19:25:48 +00001403}
1404
1405void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406Thread::DiscardThreadPlans(bool force)
1407{
Greg Clayton5160ce52013-03-27 23:08:40 +00001408 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001409 if (log)
1410 {
Daniel Malead01b2952012-11-29 21:49:15 +00001411 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412 }
1413
1414 if (force)
1415 {
1416 int stack_size = m_plan_stack.size();
1417 for (int i = stack_size - 1; i > 0; i--)
1418 {
1419 DiscardPlan();
1420 }
1421 return;
1422 }
1423
1424 while (1)
1425 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001426 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001427 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428
1429 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1430 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1431 {
1432 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1433 {
1434 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1435 break;
1436 }
1437 }
1438
1439 if (discard)
1440 {
1441 // First pop all the dependent plans:
1442 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1443 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001444 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1445 // for the plan leaves it in a state that it is safe to pop the plan
1446 // with no more notice?
1447 DiscardPlan();
1448 }
1449
1450 // Now discard the master plan itself.
1451 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1452 // discard it's dependent plans, but not it...
1453 if (master_plan_idx > 0)
1454 {
1455 DiscardPlan();
1456 }
1457 }
1458 else
1459 {
1460 // If the master plan doesn't want to get discarded, then we're done.
1461 break;
1462 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001463 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001464}
1465
Jim Inghamcf274f92012-04-09 22:37:39 +00001466bool
1467Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1468{
1469 if (plan_ptr->IsBasePlan())
1470 return true;
1471 else if (m_plan_stack.size() == 0)
1472 return false;
1473 else
1474 return m_plan_stack[0].get() == plan_ptr;
1475}
1476
Jim Ingham93208b82013-01-31 21:46:01 +00001477Error
1478Thread::UnwindInnermostExpression()
1479{
1480 Error error;
1481 int stack_size = m_plan_stack.size();
1482
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001483 // If the input plan is nullptr, discard all plans. Otherwise make sure this plan is in the
Jim Ingham93208b82013-01-31 21:46:01 +00001484 // stack, and if so discard up to and including it.
1485
1486 for (int i = stack_size - 1; i > 0; i--)
1487 {
1488 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1489 {
1490 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1491 return error;
1492 }
1493 }
1494 error.SetErrorString("No expressions currently active on this thread");
1495 return error;
1496}
1497
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001498ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001499Thread::QueueFundamentalPlan (bool abort_other_plans)
1500{
1501 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1502 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001503 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001504}
1505
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001506ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001507Thread::QueueThreadPlanForStepSingleInstruction(bool step_over,
1508 bool abort_other_plans,
1509 bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001510{
1511 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1512 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001513 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514}
1515
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001516ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001517Thread::QueueThreadPlanForStepOverRange(bool abort_other_plans,
1518 const AddressRange &range,
1519 const SymbolContext &addr_context,
1520 lldb::RunMode stop_other_threads,
1521 LazyBool step_out_avoids_code_withoug_debug_info)
Jim Inghamc6276822012-12-12 19:58:40 +00001522{
1523 ThreadPlanSP thread_plan_sp;
Jim Ingham4b4b2472014-03-13 02:47:14 +00001524 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads, step_out_avoids_code_withoug_debug_info));
1525
Jim Inghamc6276822012-12-12 19:58:40 +00001526 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001527 return thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001528}
1529
Jason Molenda25d5b102015-12-15 00:40:30 +00001530// Call the QueueThreadPlanForStepOverRange method which takes an address range.
1531ThreadPlanSP
1532Thread::QueueThreadPlanForStepOverRange(bool abort_other_plans,
1533 const LineEntry &line_entry,
1534 const SymbolContext &addr_context,
1535 lldb::RunMode stop_other_threads,
1536 LazyBool step_out_avoids_code_withoug_debug_info)
1537{
1538 return QueueThreadPlanForStepOverRange (abort_other_plans,
1539 line_entry.GetSameLineContiguousAddressRange(),
1540 addr_context,
1541 stop_other_threads,
1542 step_out_avoids_code_withoug_debug_info);
1543}
1544
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001545ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001546Thread::QueueThreadPlanForStepInRange(bool abort_other_plans,
1547 const AddressRange &range,
1548 const SymbolContext &addr_context,
1549 const char *step_in_target,
1550 lldb::RunMode stop_other_threads,
1551 LazyBool step_in_avoids_code_without_debug_info,
1552 LazyBool step_out_avoids_code_without_debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001553{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001554 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInRange (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001555 range,
1556 addr_context,
1557 stop_other_threads,
1558 step_in_avoids_code_without_debug_info,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001559 step_out_avoids_code_without_debug_info));
1560 ThreadPlanStepInRange *plan = static_cast<ThreadPlanStepInRange *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001561
Jim Inghamc6276822012-12-12 19:58:40 +00001562 if (step_in_target)
1563 plan->SetStepInTarget(step_in_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001564
1565 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001566 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001567}
1568
Jason Molenda25d5b102015-12-15 00:40:30 +00001569// Call the QueueThreadPlanForStepInRange method which takes an address range.
1570ThreadPlanSP
1571Thread::QueueThreadPlanForStepInRange(bool abort_other_plans,
1572 const LineEntry &line_entry,
1573 const SymbolContext &addr_context,
1574 const char *step_in_target,
1575 lldb::RunMode stop_other_threads,
1576 LazyBool step_in_avoids_code_without_debug_info,
1577 LazyBool step_out_avoids_code_without_debug_info)
1578{
1579 return QueueThreadPlanForStepInRange (abort_other_plans,
1580 line_entry.GetSameLineContiguousAddressRange(),
1581 addr_context,
1582 step_in_target,
1583 stop_other_threads,
1584 step_in_avoids_code_without_debug_info,
1585 step_out_avoids_code_without_debug_info);
1586}
1587
1588
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001589ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001590Thread::QueueThreadPlanForStepOut(bool abort_other_plans,
1591 SymbolContext *addr_context,
1592 bool first_insn,
1593 bool stop_other_threads,
1594 Vote stop_vote,
1595 Vote run_vote,
1596 uint32_t frame_idx,
Jason Molendafd4cea52016-01-08 21:40:11 +00001597 LazyBool step_out_avoids_code_without_debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001598{
Greg Clayton481cef22011-01-21 06:11:58 +00001599 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1600 addr_context,
1601 first_insn,
1602 stop_other_threads,
1603 stop_vote,
1604 run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001605 frame_idx,
Jason Molendafd4cea52016-01-08 21:40:11 +00001606 step_out_avoids_code_without_debug_info));
Jim Ingham4b4b2472014-03-13 02:47:14 +00001607
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001608 if (thread_plan_sp->ValidatePlan(nullptr))
Jim Ingham4b4b2472014-03-13 02:47:14 +00001609 {
1610 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1611 return thread_plan_sp;
1612 }
1613 else
1614 {
1615 return ThreadPlanSP();
1616 }
1617}
1618
1619ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001620Thread::QueueThreadPlanForStepOutNoShouldStop(bool abort_other_plans,
1621 SymbolContext *addr_context,
1622 bool first_insn,
1623 bool stop_other_threads,
1624 Vote stop_vote,
1625 Vote run_vote,
Jason Molendafd4cea52016-01-08 21:40:11 +00001626 uint32_t frame_idx,
1627 bool continue_to_next_branch)
Jim Ingham4b4b2472014-03-13 02:47:14 +00001628{
Jim Inghamb612ac32016-08-23 17:55:21 +00001629 const bool calculate_return_value = false; // No need to calculate the return value here.
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,
Jim Inghamb612ac32016-08-23 17:55:21 +00001638 continue_to_next_branch,
1639 calculate_return_value));
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001640
1641 ThreadPlanStepOut *new_plan = static_cast<ThreadPlanStepOut *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001642 new_plan->ClearShouldStopHereCallbacks();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001643
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001644 if (thread_plan_sp->ValidatePlan(nullptr))
Sean Callanan708709c2012-07-31 22:19:25 +00001645 {
1646 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001647 return thread_plan_sp;
Sean Callanan708709c2012-07-31 22:19:25 +00001648 }
1649 else
1650 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001651 return ThreadPlanSP();
Sean Callanan708709c2012-07-31 22:19:25 +00001652 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001653}
1654
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001655ThreadPlanSP
Jim Ingham18de2fd2012-05-10 01:35:39 +00001656Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001657{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001658 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001659 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan(nullptr))
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001660 return ThreadPlanSP();
Jim Ingham25f66702011-12-03 01:52:59 +00001661
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001662 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001663 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001664}
1665
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001666ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001667Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1668 Address &target_addr,
1669 bool stop_other_threads)
1670{
1671 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1672 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001673 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001674}
1675
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001676ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001677Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001678 lldb::addr_t *address_list,
1679 size_t num_addresses,
1680 bool stop_other_threads,
1681 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001682{
Greg Clayton481cef22011-01-21 06:11:58 +00001683 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001684 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001685 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001686
1687}
1688
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001689lldb::ThreadPlanSP
1690Thread::QueueThreadPlanForStepScripted (bool abort_other_plans,
1691 const char *class_name,
1692 bool stop_other_threads)
1693{
1694 ThreadPlanSP thread_plan_sp (new ThreadPlanPython (*this, class_name));
1695 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1696 // This seems a little funny, but I don't want to have to split up the constructor and the
1697 // DidPush in the scripted plan, that seems annoying.
1698 // That means the constructor has to be in DidPush.
1699 // So I have to validate the plan AFTER pushing it, and then take it off again...
1700 if (!thread_plan_sp->ValidatePlan(nullptr))
1701 {
1702 DiscardThreadPlansUpToPlan(thread_plan_sp);
1703 return ThreadPlanSP();
1704 }
1705 else
1706 return thread_plan_sp;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001707}
1708
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001709uint32_t
1710Thread::GetIndexID () const
1711{
1712 return m_index_id;
1713}
1714
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001715static void
1716PrintPlanElement (Stream *s, const ThreadPlanSP &plan, lldb::DescriptionLevel desc_level, int32_t elem_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001717{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001718 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001719 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001720 s->Printf ("Element %d: ", elem_idx);
1721 plan->GetDescription (s, desc_level);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001722 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001723 s->IndentLess();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001724}
1725
1726static void
1727PrintPlanStack (Stream *s, const std::vector<lldb::ThreadPlanSP> &plan_stack, lldb::DescriptionLevel desc_level, bool include_internal)
1728{
1729 int32_t print_idx = 0;
1730 for (ThreadPlanSP plan_sp : plan_stack)
1731 {
1732 if (include_internal || !plan_sp->GetPrivate())
1733 {
1734 PrintPlanElement (s, plan_sp, desc_level, print_idx++);
1735 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001737}
1738
1739void
1740Thread::DumpThreadPlans (Stream *s,
1741 lldb::DescriptionLevel desc_level,
1742 bool include_internal,
1743 bool ignore_boring_threads) const
1744{
Jason Molenda60b5da62014-10-16 07:53:46 +00001745 uint32_t stack_size;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001746
1747 if (ignore_boring_threads)
1748 {
1749 uint32_t stack_size = m_plan_stack.size();
1750 uint32_t completed_stack_size = m_completed_plan_stack.size();
1751 uint32_t discarded_stack_size = m_discarded_plan_stack.size();
1752 if (stack_size == 1 && completed_stack_size == 0 && discarded_stack_size == 0)
1753 {
1754 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 "\n", GetIndexID(), GetID());
1755 s->IndentMore();
1756 s->Indent();
1757 s->Printf("No active thread plans\n");
1758 s->IndentLess();
1759 return;
1760 }
1761 }
1762
1763 s->Indent();
1764 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 ":\n", GetIndexID(), GetID());
1765 s->IndentMore();
1766 s->Indent();
1767 s->Printf ("Active plan stack:\n");
1768 PrintPlanStack (s, m_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001769
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001770 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001771 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001772 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001773 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001774 s->Printf ("Completed Plan Stack:\n");
1775 PrintPlanStack (s, m_completed_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001776 }
1777
1778 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001779 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001780 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001781 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001782 s->Printf ("Discarded Plan Stack:\n");
1783 PrintPlanStack (s, m_discarded_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001784 }
1785
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001786 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001787}
1788
Greg Claytond9e416c2012-02-18 05:35:26 +00001789TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001790Thread::CalculateTarget ()
1791{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001792 TargetSP target_sp;
1793 ProcessSP process_sp(GetProcess());
1794 if (process_sp)
1795 target_sp = process_sp->CalculateTarget();
1796 return target_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001797}
1798
Greg Claytond9e416c2012-02-18 05:35:26 +00001799ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001800Thread::CalculateProcess ()
1801{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001802 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001803}
1804
Greg Claytond9e416c2012-02-18 05:35:26 +00001805ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001806Thread::CalculateThread ()
1807{
Greg Claytond9e416c2012-02-18 05:35:26 +00001808 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001809}
1810
Jason Molendab57e4a12013-11-04 09:33:30 +00001811StackFrameSP
1812Thread::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001813{
Jason Molendab57e4a12013-11-04 09:33:30 +00001814 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001815}
1816
1817void
Greg Clayton0603aa92010-10-04 01:05:56 +00001818Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001819{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001820 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001821}
1822
Greg Clayton39d0ab32012-03-29 01:41:38 +00001823StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001824Thread::GetStackFrameList ()
1825{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001826 StackFrameListSP frame_list_sp;
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001827 std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
Greg Clayton39d0ab32012-03-29 01:41:38 +00001828 if (m_curr_frames_sp)
1829 {
1830 frame_list_sp = m_curr_frames_sp;
1831 }
1832 else
1833 {
1834 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1835 m_curr_frames_sp = frame_list_sp;
1836 }
1837 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001838}
1839
Greg Clayton12daf9462010-08-25 00:35:26 +00001840void
1841Thread::ClearStackFrames ()
1842{
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00001843 std::lock_guard<std::recursive_mutex> guard(m_frame_mutex);
Greg Clayton39d0ab32012-03-29 01:41:38 +00001844
Greg Clayton160c9d82013-05-01 21:54:04 +00001845 Unwind *unwinder = GetUnwinder ();
1846 if (unwinder)
1847 unwinder->Clear();
1848
Jim Inghamb0c72a52012-02-29 03:40:22 +00001849 // Only store away the old "reference" StackFrameList if we got all its frames:
1850 // FIXME: At some point we can try to splice in the frames we have fetched into
1851 // the new frame as we make it, but let's not try that now.
1852 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001853 m_prev_frames_sp.swap (m_curr_frames_sp);
1854 m_curr_frames_sp.reset();
Jason Molenda705b1802014-06-13 02:37:02 +00001855
1856 m_extended_info.reset();
1857 m_extended_info_fetched = false;
Jim Ingham87c11912010-08-12 02:14:28 +00001858}
1859
Jason Molendab57e4a12013-11-04 09:33:30 +00001860lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001861Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1862{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001863 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001864}
1865
Jim Ingham44137582012-09-12 00:40:39 +00001866Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001867Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001868{
Jason Molendab57e4a12013-11-04 09:33:30 +00001869 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001870 Error return_error;
1871
1872 if (!frame_sp)
1873 {
Daniel Malead01b2952012-11-29 21:49:15 +00001874 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001875 }
1876
Jim Ingham4f465cf2012-10-10 18:32:14 +00001877 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001878}
1879
1880Error
Jason Molendab57e4a12013-11-04 09:33:30 +00001881Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001882{
1883 Error return_error;
1884
1885 if (!frame_sp)
1886 {
1887 return_error.SetErrorString("Can't return to a null frame.");
1888 return return_error;
1889 }
1890
1891 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001892 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
Jason Molendab57e4a12013-11-04 09:33:30 +00001893 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001894 if (!older_frame_sp)
1895 {
1896 return_error.SetErrorString("No older frame to return to.");
1897 return return_error;
1898 }
Jim Ingham44137582012-09-12 00:40:39 +00001899
1900 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001901 {
Jim Ingham44137582012-09-12 00:40:39 +00001902 lldb::ABISP abi = thread->GetProcess()->GetABI();
1903 if (!abi)
1904 {
1905 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001906 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001907 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001908 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1909
1910 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1911 // Turn that back on when that works.
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00001912 if (/* DISABLES CODE */ (0) && sc.function != nullptr)
Jim Ingham1f51e602012-09-27 01:15:29 +00001913 {
1914 Type *function_type = sc.function->GetType();
1915 if (function_type)
1916 {
Greg Clayton99558cc42015-08-24 23:46:31 +00001917 CompilerType return_type = sc.function->GetCompilerType().GetFunctionReturnType();
Jim Ingham1f51e602012-09-27 01:15:29 +00001918 if (return_type)
1919 {
Jim Ingham1f51e602012-09-27 01:15:29 +00001920 StreamString s;
Greg Clayton57ee3062013-07-11 22:46:58 +00001921 return_type.DumpTypeDescription(&s);
1922 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham1f51e602012-09-27 01:15:29 +00001923 if (cast_value_sp)
1924 {
1925 cast_value_sp->SetFormat(eFormatHex);
1926 return_value_sp = cast_value_sp;
1927 }
1928 }
1929 }
1930 }
1931
Jim Inghamcb640dd2012-09-14 02:14:15 +00001932 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001933 if (!return_error.Success())
1934 return return_error;
1935 }
1936
1937 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001938 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001939 // cook their data
1940
Jason Molendab57e4a12013-11-04 09:33:30 +00001941 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
Jim Ingham93208b82013-01-31 21:46:01 +00001942 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001943 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001944 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1945 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001946 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001947 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1948 if (copy_success)
1949 {
1950 thread->DiscardThreadPlans(true);
1951 thread->ClearStackFrames();
1952 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1953 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1954 }
1955 else
1956 {
1957 return_error.SetErrorString("Could not reset register values.");
1958 }
Jim Ingham93208b82013-01-31 21:46:01 +00001959 }
1960 else
1961 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001962 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001963 }
Jim Ingham44137582012-09-12 00:40:39 +00001964 }
1965 else
1966 {
Jim Ingham93208b82013-01-31 21:46:01 +00001967 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001968 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001969 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001970}
1971
Richard Mittonf86248d2013-09-12 02:20:34 +00001972static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1973{
1974 for (size_t n=0;n<list.size();n++)
1975 {
1976 s << "\t";
1977 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1978 s << "\n";
1979 }
1980}
1981
1982Error
1983Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1984{
1985 ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
1986 Target *target = exe_ctx.GetTargetPtr();
1987 TargetSP target_sp = exe_ctx.GetTargetSP();
1988 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00001989 StackFrame *frame = exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00001990 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1991
1992 // Find candidate locations.
1993 std::vector<Address> candidates, within_function, outside_function;
1994 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
1995
1996 // If possible, we try and stay within the current function.
1997 // Within a function, we accept multiple locations (optimized code may do this,
1998 // there's no solution here so we do the best we can).
1999 // However if we're trying to leave the function, we don't know how to pick the
2000 // right location, so if there's more than one then we bail.
2001 if (!within_function.empty())
2002 candidates = within_function;
2003 else if (outside_function.size() == 1 && can_leave_function)
2004 candidates = outside_function;
2005
2006 // Check if we got anything.
2007 if (candidates.empty())
2008 {
2009 if (outside_function.empty())
2010 {
2011 return Error("Cannot locate an address for %s:%i.",
2012 file.GetFilename().AsCString(), line);
2013 }
2014 else if (outside_function.size() == 1)
2015 {
2016 return Error("%s:%i is outside the current function.",
2017 file.GetFilename().AsCString(), line);
2018 }
2019 else
2020 {
2021 StreamString sstr;
2022 DumpAddressList(sstr, outside_function, target);
2023 return Error("%s:%i has multiple candidate locations:\n%s",
2024 file.GetFilename().AsCString(), line, sstr.GetString().c_str());
2025 }
2026 }
2027
2028 // Accept the first location, warn about any others.
2029 Address dest = candidates[0];
2030 if (warnings && candidates.size() > 1)
2031 {
2032 StreamString sstr;
2033 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
2034 file.GetFilename().AsCString(), line);
2035 DumpAddressList(sstr, candidates, target);
2036 *warnings = sstr.GetString();
2037 }
2038
2039 if (!reg_ctx->SetPC (dest))
2040 return Error("Cannot change PC to target address.");
2041
2042 return Error();
2043}
2044
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002045void
Greg Clayton0603aa92010-10-04 01:05:56 +00002046Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002047{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002048 ExecutionContext exe_ctx (shared_from_this());
2049 Process *process = exe_ctx.GetProcessPtr();
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002050 if (process == nullptr)
Greg Clayton1ac04c32012-02-21 00:09:25 +00002051 return;
2052
Jason Molendab57e4a12013-11-04 09:33:30 +00002053 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00002054 SymbolContext frame_sc;
Jim Inghamd1f25362015-01-28 01:17:26 +00002055 if (frame_idx != LLDB_INVALID_FRAME_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002056 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002057 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002058 if (frame_sp)
2059 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002060 exe_ctx.SetFrameSP(frame_sp);
2061 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002062 }
2063 }
2064
Greg Clayton554f68d2015-02-04 22:00:53 +00002065 const FormatEntity::Entry *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00002066 assert (thread_format);
Greg Clayton554f68d2015-02-04 22:00:53 +00002067
2068 FormatEntity::Format(*thread_format,
2069 strm,
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002070 frame_sp ? &frame_sc : nullptr,
Greg Clayton554f68d2015-02-04 22:00:53 +00002071 &exe_ctx,
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002072 nullptr,
2073 nullptr,
Greg Clayton554f68d2015-02-04 22:00:53 +00002074 false,
2075 false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002076}
2077
Greg Clayton99d0faf2010-11-18 23:32:35 +00002078void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002079Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00002080{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002081}
Jim Inghamee8aea12010-09-08 03:14:33 +00002082
Greg Clayton99d0faf2010-11-18 23:32:35 +00002083void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002084Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002085{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002086}
Jim Inghamee8aea12010-09-08 03:14:33 +00002087
Richard Mitton0a558352013-10-17 21:14:00 +00002088lldb::addr_t
2089Thread::GetThreadPointer ()
2090{
2091 return LLDB_INVALID_ADDRESS;
2092}
2093
2094addr_t
Greg Clayton63a27af2016-07-01 17:17:23 +00002095Thread::GetThreadLocalData(const ModuleSP module, lldb::addr_t tls_file_addr)
Richard Mitton0a558352013-10-17 21:14:00 +00002096{
2097 // The default implementation is to ask the dynamic loader for it.
2098 // This can be overridden for specific platforms.
2099 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
2100 if (loader)
Greg Clayton63a27af2016-07-01 17:17:23 +00002101 return loader->GetThreadLocalData(module, shared_from_this(), tls_file_addr);
Richard Mitton0a558352013-10-17 21:14:00 +00002102 else
2103 return LLDB_INVALID_ADDRESS;
2104}
2105
Jason Molendab4892cd2014-05-13 22:02:48 +00002106bool
2107Thread::SafeToCallFunctions ()
2108{
2109 Process *process = GetProcess().get();
2110 if (process)
2111 {
2112 SystemRuntime *runtime = process->GetSystemRuntime ();
2113 if (runtime)
2114 {
2115 return runtime->SafeToCallFunctionsOnThisThread (shared_from_this());
2116 }
2117 }
2118 return true;
2119}
2120
Jason Molendab57e4a12013-11-04 09:33:30 +00002121lldb::StackFrameSP
2122Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
Jim Inghame4284b72010-09-23 17:40:12 +00002123{
Jason Molendab57e4a12013-11-04 09:33:30 +00002124 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00002125}
Caroline Ticeceb6b132010-10-26 03:11:13 +00002126
2127const char *
2128Thread::StopReasonAsCString (lldb::StopReason reason)
2129{
2130 switch (reason)
2131 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00002132 case eStopReasonInvalid: return "invalid";
2133 case eStopReasonNone: return "none";
2134 case eStopReasonTrace: return "trace";
2135 case eStopReasonBreakpoint: return "breakpoint";
2136 case eStopReasonWatchpoint: return "watchpoint";
2137 case eStopReasonSignal: return "signal";
2138 case eStopReasonException: return "exception";
2139 case eStopReasonExec: return "exec";
2140 case eStopReasonPlanComplete: return "plan complete";
2141 case eStopReasonThreadExiting: return "thread exiting";
Kuba Breckaafdf8422014-10-10 23:43:03 +00002142 case eStopReasonInstrumentation: return "instrumentation break";
Caroline Ticeceb6b132010-10-26 03:11:13 +00002143 }
2144
Caroline Ticeceb6b132010-10-26 03:11:13 +00002145 static char unknown_state_string[64];
2146 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
2147 return unknown_state_string;
2148}
2149
2150const char *
2151Thread::RunModeAsCString (lldb::RunMode mode)
2152{
2153 switch (mode)
2154 {
2155 case eOnlyThisThread: return "only this thread";
2156 case eAllThreads: return "all threads";
2157 case eOnlyDuringStepping: return "only during stepping";
2158 }
2159
2160 static char unknown_state_string[64];
2161 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
2162 return unknown_state_string;
2163}
Jim Ingham06e827c2010-11-11 19:26:09 +00002164
Greg Clayton7260f622011-04-18 08:33:37 +00002165size_t
2166Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
2167{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002168 ExecutionContext exe_ctx (shared_from_this());
2169 Target *target = exe_ctx.GetTargetPtr();
2170 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00002171 size_t num_frames_shown = 0;
2172 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00002173 bool is_selected = false;
2174 if (process)
2175 {
2176 if (process->GetThreadList().GetSelectedThread().get() == this)
2177 is_selected = true;
2178 }
2179 strm.Printf("%c ", is_selected ? '*' : ' ');
2180 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00002181 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002182 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00002183 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00002184 {
Jim Inghame610d642011-08-16 00:07:28 +00002185 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
2186 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
2187 {
2188 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
2189 }
Greg Clayton5113dc82011-08-12 06:47:54 +00002190 }
Greg Clayton7260f622011-04-18 08:33:37 +00002191 }
2192
2193 DumpUsingSettingsFormat (strm, start_frame);
2194
2195 if (num_frames > 0)
2196 {
2197 strm.IndentMore();
2198
2199 const bool show_frame_info = true;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002200
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002201 const char *selected_frame_marker = nullptr;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002202 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
2203 strm.IndentMore ();
2204 else
2205 selected_frame_marker = "* ";
2206
Greg Clayton39d0ab32012-03-29 01:41:38 +00002207 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
2208 start_frame,
2209 num_frames,
2210 show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002211 num_frames_with_source,
2212 selected_frame_marker);
2213 if (num_frames == 1)
2214 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00002215 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00002216 }
2217 return num_frames_shown;
2218}
2219
Jason Molenda705b1802014-06-13 02:37:02 +00002220bool
Kuba Breckaafdf8422014-10-10 23:43:03 +00002221Thread::GetDescription (Stream &strm, lldb::DescriptionLevel level, bool print_json_thread, bool print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002222{
2223 DumpUsingSettingsFormat (strm, 0);
2224 strm.Printf("\n");
2225
2226 StructuredData::ObjectSP thread_info = GetExtendedInfo();
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002227
Kuba Breckaafdf8422014-10-10 23:43:03 +00002228 if (print_json_thread || print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002229 {
Kuba Breckaafdf8422014-10-10 23:43:03 +00002230 if (thread_info && print_json_thread)
2231 {
2232 thread_info->Dump (strm);
2233 strm.Printf("\n");
2234 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002235
2236 if (print_json_stopinfo && m_stop_info_sp)
Kuba Breckaafdf8422014-10-10 23:43:03 +00002237 {
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002238 StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo();
2239 if (stop_info)
2240 {
2241 stop_info->Dump (strm);
2242 strm.Printf("\n");
2243 }
Kuba Breckaafdf8422014-10-10 23:43:03 +00002244 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002245
Jason Molenda705b1802014-06-13 02:37:02 +00002246 return true;
2247 }
2248
2249 if (thread_info)
2250 {
2251 StructuredData::ObjectSP activity = thread_info->GetObjectForDotSeparatedPath("activity");
2252 StructuredData::ObjectSP breadcrumb = thread_info->GetObjectForDotSeparatedPath("breadcrumb");
2253 StructuredData::ObjectSP messages = thread_info->GetObjectForDotSeparatedPath("trace_messages");
2254
2255 bool printed_activity = false;
2256 if (activity && activity->GetType() == StructuredData::Type::eTypeDictionary)
2257 {
2258 StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
2259 StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
2260 StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
2261 if (name && name->GetType() == StructuredData::Type::eTypeString
2262 && id && id->GetType() == StructuredData::Type::eTypeInteger)
2263 {
2264 strm.Printf(" Activity '%s', 0x%" PRIx64 "\n", name->GetAsString()->GetValue().c_str(), id->GetAsInteger()->GetValue());
2265 }
2266 printed_activity = true;
2267 }
2268 bool printed_breadcrumb = false;
2269 if (breadcrumb && breadcrumb->GetType() == StructuredData::Type::eTypeDictionary)
2270 {
2271 if (printed_activity)
2272 strm.Printf ("\n");
2273 StructuredData::Dictionary *breadcrumb_dict = breadcrumb->GetAsDictionary();
2274 StructuredData::ObjectSP breadcrumb_text = breadcrumb_dict->GetValueForKey ("name");
2275 if (breadcrumb_text && breadcrumb_text->GetType() == StructuredData::Type::eTypeString)
2276 {
2277 strm.Printf (" Current Breadcrumb: %s\n", breadcrumb_text->GetAsString()->GetValue().c_str());
2278 }
2279 printed_breadcrumb = true;
2280 }
2281 if (messages && messages->GetType() == StructuredData::Type::eTypeArray)
2282 {
2283 if (printed_breadcrumb)
2284 strm.Printf("\n");
2285 StructuredData::Array *messages_array = messages->GetAsArray();
2286 const size_t msg_count = messages_array->GetSize();
2287 if (msg_count > 0)
2288 {
2289 strm.Printf (" %zu trace messages:\n", msg_count);
2290 for (size_t i = 0; i < msg_count; i++)
2291 {
2292 StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
2293 if (message && message->GetType() == StructuredData::Type::eTypeDictionary)
2294 {
2295 StructuredData::Dictionary *message_dict = message->GetAsDictionary();
2296 StructuredData::ObjectSP message_text = message_dict->GetValueForKey ("message");
2297 if (message_text && message_text->GetType() == StructuredData::Type::eTypeString)
2298 {
2299 strm.Printf (" %s\n", message_text->GetAsString()->GetValue().c_str());
2300 }
2301 }
2302 }
2303 }
2304 }
2305 }
2306
2307 return true;
2308}
2309
Greg Clayton7260f622011-04-18 08:33:37 +00002310size_t
2311Thread::GetStackFrameStatus (Stream& strm,
2312 uint32_t first_frame,
2313 uint32_t num_frames,
2314 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002315 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00002316{
Greg Clayton39d0ab32012-03-29 01:41:38 +00002317 return GetStackFrameList()->GetStatus (strm,
2318 first_frame,
2319 num_frames,
2320 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002321 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00002322}
2323
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002324Unwind *
2325Thread::GetUnwinder ()
2326{
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002327 if (!m_unwinder_ap)
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002328 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00002329 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002330 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2331 switch (machine)
2332 {
2333 case llvm::Triple::x86_64:
2334 case llvm::Triple::x86:
2335 case llvm::Triple::arm:
Todd Fialad8eaa172014-07-23 14:37:35 +00002336 case llvm::Triple::aarch64:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002337 case llvm::Triple::thumb:
Bhushan D. Attarde794a4d52015-05-15 06:53:30 +00002338 case llvm::Triple::mips:
2339 case llvm::Triple::mipsel:
Ed Masteb73f8442013-10-10 00:59:47 +00002340 case llvm::Triple::mips64:
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +00002341 case llvm::Triple::mips64el:
Justin Hibbits6256a0e2014-10-31 02:34:28 +00002342 case llvm::Triple::ppc:
Justin Hibbitsdb39cdf2014-10-31 15:57:52 +00002343 case llvm::Triple::ppc64:
Ulrich Weigandbb00d0b2016-04-14 14:28:34 +00002344 case llvm::Triple::systemz:
Deepak Panickal6d3df422014-02-19 11:16:46 +00002345 case llvm::Triple::hexagon:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002346 m_unwinder_ap.reset (new UnwindLLDB (*this));
2347 break;
2348
2349 default:
2350 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2351 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2352 break;
2353 }
2354 }
2355 return m_unwinder_ap.get();
2356}
2357
Greg Claytonfa559e52012-05-18 02:38:05 +00002358void
2359Thread::Flush ()
2360{
2361 ClearStackFrames ();
2362 m_reg_context_sp.reset();
2363}
Jim Ingham5d88a062012-10-16 00:09:33 +00002364
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00002365bool
Jim Ingham5d88a062012-10-16 00:09:33 +00002366Thread::IsStillAtLastBreakpointHit ()
2367{
2368 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2369 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2370 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002371 if (m_stop_info_sp) {
2372 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00002373 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002374 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00002375 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2376 if (reg_ctx_sp)
2377 {
2378 lldb::addr_t pc = reg_ctx_sp->GetPC();
2379 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002380 if (bp_site_sp &&
2381 static_cast<break_id_t>(value) == bp_site_sp->GetID())
Greg Clayton1afa68e2013-04-02 20:32:37 +00002382 return true;
2383 }
Jim Ingham5d88a062012-10-16 00:09:33 +00002384 }
2385 }
2386 return false;
2387}
Greg Clayton44d93782014-01-27 23:43:24 +00002388
Greg Clayton44d93782014-01-27 23:43:24 +00002389Error
2390Thread::StepIn (bool source_step,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002391 LazyBool step_in_avoids_code_without_debug_info,
2392 LazyBool step_out_avoids_code_without_debug_info)
2393
Greg Clayton44d93782014-01-27 23:43:24 +00002394{
2395 Error error;
2396 Process *process = GetProcess().get();
2397 if (StateIsStoppedState (process->GetState(), true))
2398 {
2399 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2400 ThreadPlanSP new_plan_sp;
2401 const lldb::RunMode run_mode = eOnlyThisThread;
2402 const bool abort_other_plans = false;
2403
2404 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2405 {
2406 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002407 new_plan_sp = QueueThreadPlanForStepInRange(abort_other_plans,
2408 sc.line_entry,
2409 sc,
2410 nullptr,
2411 run_mode,
2412 step_in_avoids_code_without_debug_info,
2413 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002414 }
2415 else
2416 {
2417 new_plan_sp = QueueThreadPlanForStepSingleInstruction (false,
2418 abort_other_plans,
2419 run_mode);
2420 }
2421
2422 new_plan_sp->SetIsMasterPlan(true);
2423 new_plan_sp->SetOkayToDiscard(false);
2424
2425 // Why do we need to set the current thread by ID here???
2426 process->GetThreadList().SetSelectedThreadByID (GetID());
2427 error = process->Resume();
2428 }
2429 else
2430 {
2431 error.SetErrorString("process not stopped");
2432 }
2433 return error;
2434}
2435
2436Error
Jim Ingham4b4b2472014-03-13 02:47:14 +00002437Thread::StepOver (bool source_step,
2438 LazyBool step_out_avoids_code_without_debug_info)
Greg Clayton44d93782014-01-27 23:43:24 +00002439{
2440 Error error;
2441 Process *process = GetProcess().get();
2442 if (StateIsStoppedState (process->GetState(), true))
2443 {
2444 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2445 ThreadPlanSP new_plan_sp;
2446
2447 const lldb::RunMode run_mode = eOnlyThisThread;
2448 const bool abort_other_plans = false;
2449
2450 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2451 {
2452 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2453 new_plan_sp = QueueThreadPlanForStepOverRange (abort_other_plans,
Jason Molenda25d5b102015-12-15 00:40:30 +00002454 sc.line_entry,
Greg Clayton44d93782014-01-27 23:43:24 +00002455 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002456 run_mode,
2457 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002458 }
2459 else
2460 {
2461 new_plan_sp = QueueThreadPlanForStepSingleInstruction (true,
2462 abort_other_plans,
2463 run_mode);
2464 }
2465
2466 new_plan_sp->SetIsMasterPlan(true);
2467 new_plan_sp->SetOkayToDiscard(false);
2468
2469 // Why do we need to set the current thread by ID here???
2470 process->GetThreadList().SetSelectedThreadByID (GetID());
2471 error = process->Resume();
2472 }
2473 else
2474 {
2475 error.SetErrorString("process not stopped");
2476 }
2477 return error;
2478}
2479
2480Error
2481Thread::StepOut ()
2482{
2483 Error error;
2484 Process *process = GetProcess().get();
2485 if (StateIsStoppedState (process->GetState(), true))
2486 {
2487 const bool first_instruction = false;
2488 const bool stop_other_threads = false;
2489 const bool abort_other_plans = false;
2490
Eugene Zelenkoe65b2cf2015-12-15 01:33:19 +00002491 ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut(abort_other_plans,
2492 nullptr,
2493 first_instruction,
2494 stop_other_threads,
2495 eVoteYes,
2496 eVoteNoOpinion,
2497 0));
Greg Clayton44d93782014-01-27 23:43:24 +00002498
2499 new_plan_sp->SetIsMasterPlan(true);
2500 new_plan_sp->SetOkayToDiscard(false);
2501
2502 // Why do we need to set the current thread by ID here???
2503 process->GetThreadList().SetSelectedThreadByID (GetID());
2504 error = process->Resume();
2505 }
2506 else
2507 {
2508 error.SetErrorString("process not stopped");
2509 }
2510 return error;
Jim Ingham4b4b2472014-03-13 02:47:14 +00002511}