blob: 666fbf076c108f91345d651feee2b0a8dcdcb483 [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
Jim Ingham1b54c882010-06-16 02:00:15 +000010#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000011#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/Log.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000013#include "lldb/Core/FormatEntity.h"
Jason Molenda03c9cd02015-08-06 21:54:29 +000014#include "lldb/Core/Module.h"
Greg Clayton160c9d82013-05-01 21:54:04 +000015#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Stream.h"
17#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000018#include "lldb/Core/RegularExpression.h"
Jim Ingham0d5a2bd2015-09-03 01:40:51 +000019#include "lldb/Core/ValueObject.h"
Greg Clayton7260f622011-04-18 08:33:37 +000020#include "lldb/Host/Host.h"
Jim Ingham4da62062014-01-23 21:52:47 +000021#include "lldb/Interpreter/OptionValueFileSpecList.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000022#include "lldb/Interpreter/OptionValueProperties.h"
23#include "lldb/Interpreter/Property.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000024#include "lldb/Symbol/Function.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000025#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Target/DynamicLoader.h"
27#include "lldb/Target/ExecutionContext.h"
28#include "lldb/Target/Process.h"
29#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000030#include "lldb/Target/StopInfo.h"
Jason Molendab4892cd2014-05-13 22:02:48 +000031#include "lldb/Target/SystemRuntime.h"
Greg Clayton896dff62010-07-23 16:45:51 +000032#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Target/Thread.h"
34#include "lldb/Target/ThreadPlan.h"
35#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Target/ThreadPlanBase.h"
Jim Ingham2bdbfd52014-09-29 23:17:18 +000037#include "lldb/Target/ThreadPlanPython.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "lldb/Target/ThreadPlanStepInstruction.h"
39#include "lldb/Target/ThreadPlanStepOut.h"
40#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
41#include "lldb/Target/ThreadPlanStepThrough.h"
42#include "lldb/Target/ThreadPlanStepInRange.h"
43#include "lldb/Target/ThreadPlanStepOverRange.h"
44#include "lldb/Target/ThreadPlanRunToAddress.h"
45#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000046#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000047#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000048#include "Plugins/Process/Utility/UnwindLLDB.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000049#include "Plugins/Process/Utility/UnwindMacOSXFrameBackchain.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000050
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051
52using namespace lldb;
53using namespace lldb_private;
54
Greg Clayton67cc0632012-08-22 17:17:09 +000055
56const ThreadPropertiesSP &
57Thread::GetGlobalProperties()
58{
59 static ThreadPropertiesSP g_settings_sp;
60 if (!g_settings_sp)
61 g_settings_sp.reset (new ThreadProperties (true));
62 return g_settings_sp;
63}
64
65static PropertyDefinition
66g_properties[] =
67{
Jim Ingham4b4b2472014-03-13 02:47:14 +000068 { "step-in-avoid-nodebug", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, step-in will not stop in functions with no debug information." },
69 { "step-out-avoid-nodebug", OptionValue::eTypeBoolean, true, false, NULL, NULL, "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 "
70 "debug information. Passing a frame argument to step-out will override this option." },
Greg Clayton7bd4c602015-01-21 21:51:02 +000071 { "step-avoid-regexp", OptionValue::eTypeRegex , true , 0, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
72 { "step-avoid-libraries", OptionValue::eTypeFileSpecList , true , 0, NULL, NULL, "A list of libraries that source stepping won't stop in." },
Greg Clayton67cc0632012-08-22 17:17:09 +000073 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
74 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
75};
76
77enum {
Jim Ingham4b4b2472014-03-13 02:47:14 +000078 ePropertyStepInAvoidsNoDebug,
79 ePropertyStepOutAvoidsNoDebug,
Greg Clayton67cc0632012-08-22 17:17:09 +000080 ePropertyStepAvoidRegex,
Jim Ingham4da62062014-01-23 21:52:47 +000081 ePropertyStepAvoidLibraries,
Greg Clayton67cc0632012-08-22 17:17:09 +000082 ePropertyEnableThreadTrace
83};
84
85
86class ThreadOptionValueProperties : public OptionValueProperties
87{
88public:
89 ThreadOptionValueProperties (const ConstString &name) :
90 OptionValueProperties (name)
91 {
92 }
93
94 // This constructor is used when creating ThreadOptionValueProperties when it
95 // is part of a new lldb_private::Thread instance. It will copy all current
96 // global property values as needed
97 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +000098 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +000099 {
100 }
101
102 virtual const Property *
103 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
104 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000105 // When getting the value for a key from the thread options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +0000106 // try and grab the setting from the current thread if there is one. Else we just
107 // use the one from this instance.
108 if (exe_ctx)
109 {
110 Thread *thread = exe_ctx->GetThreadPtr();
111 if (thread)
112 {
113 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
114 if (this != instance_properties)
115 return instance_properties->ProtectedGetPropertyAtIndex (idx);
116 }
117 }
118 return ProtectedGetPropertyAtIndex (idx);
119 }
120};
121
122
123
124ThreadProperties::ThreadProperties (bool is_global) :
125 Properties ()
126{
127 if (is_global)
128 {
129 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
130 m_collection_sp->Initialize(g_properties);
131 }
132 else
133 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
134}
135
136ThreadProperties::~ThreadProperties()
137{
138}
139
140const RegularExpression *
141ThreadProperties::GetSymbolsToAvoidRegexp()
142{
143 const uint32_t idx = ePropertyStepAvoidRegex;
144 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
145}
146
Jim Ingham4da62062014-01-23 21:52:47 +0000147FileSpecList &
148ThreadProperties::GetLibrariesToAvoid() const
149{
150 const uint32_t idx = ePropertyStepAvoidLibraries;
151 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
152 assert(option_value);
153 return option_value->GetCurrentValue();
154}
155
Greg Clayton67cc0632012-08-22 17:17:09 +0000156bool
157ThreadProperties::GetTraceEnabledState() const
158{
159 const uint32_t idx = ePropertyEnableThreadTrace;
160 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
161}
162
Jim Ingham4b4b2472014-03-13 02:47:14 +0000163bool
164ThreadProperties::GetStepInAvoidsNoDebug() const
165{
166 const uint32_t idx = ePropertyStepInAvoidsNoDebug;
167 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
168}
169
170bool
171ThreadProperties::GetStepOutAvoidsNoDebug() const
172{
173 const uint32_t idx = ePropertyStepOutAvoidsNoDebug;
174 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
175}
176
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 +0000182
183const ConstString &
184Thread::ThreadEventData::GetFlavorString ()
185{
186 static ConstString g_flavor ("Thread::ThreadEventData");
187 return g_flavor;
188}
189
190Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
191 m_thread_sp (thread_sp),
192 m_stack_id ()
193{
194}
195
196Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
197 m_thread_sp (thread_sp),
198 m_stack_id (stack_id)
199{
200}
201
202Thread::ThreadEventData::ThreadEventData () :
203 m_thread_sp (),
204 m_stack_id ()
205{
206}
207
208Thread::ThreadEventData::~ThreadEventData ()
209{
210}
211
212void
213Thread::ThreadEventData::Dump (Stream *s) const
214{
215
216}
217
218const Thread::ThreadEventData *
219Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
220{
221 if (event_ptr)
222 {
223 const EventData *event_data = event_ptr->GetData();
224 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
225 return static_cast <const ThreadEventData *> (event_ptr->GetData());
226 }
227 return NULL;
228}
229
230ThreadSP
231Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
232{
233 ThreadSP thread_sp;
234 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
235 if (event_data)
236 thread_sp = event_data->GetThread();
237 return thread_sp;
238}
239
240StackID
241Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
242{
243 StackID stack_id;
244 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
245 if (event_data)
246 stack_id = event_data->GetStackID();
247 return stack_id;
248}
249
Jason Molendab57e4a12013-11-04 09:33:30 +0000250StackFrameSP
Jim Ingham4f465cf2012-10-10 18:32:14 +0000251Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
252{
253 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
Jason Molendab57e4a12013-11-04 09:33:30 +0000254 StackFrameSP frame_sp;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000255 if (event_data)
256 {
257 ThreadSP thread_sp = event_data->GetThread();
258 if (thread_sp)
259 {
260 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
261 }
262 }
263 return frame_sp;
264}
265
266//------------------------------------------------------------------
267// Thread class
268//------------------------------------------------------------------
269
270ConstString &
271Thread::GetStaticBroadcasterClass ()
272{
273 static ConstString class_name ("lldb.thread");
274 return class_name;
275}
276
Jason Molendaa8ff5432014-03-06 06:31:18 +0000277Thread::Thread (Process &process, lldb::tid_t tid, bool use_invalid_index_id) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000278 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000280 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
281 m_process_wp (process.shared_from_this()),
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000282 m_stop_info_sp (),
283 m_stop_info_stop_id (0),
Greg Claytona97c4d22014-12-09 23:31:02 +0000284 m_stop_info_override_stop_id (0),
Jason Molendaa8ff5432014-03-06 06:31:18 +0000285 m_index_id (use_invalid_index_id ? LLDB_INVALID_INDEX32 : process.GetNextThreadIndexID(tid)),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000288 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000291 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000292 m_curr_frames_sp (),
293 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000295 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000296 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000297 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000298 m_destroy_called (false),
Jason Molenda705b1802014-06-13 02:37:02 +0000299 m_override_should_notify (eLazyBoolCalculate),
300 m_extended_info_fetched (false),
301 m_extended_info ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302{
Greg Clayton5160ce52013-03-27 23:08:40 +0000303 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000305 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")",
306 static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307
Jim Ingham4f465cf2012-10-10 18:32:14 +0000308 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309 QueueFundamentalPlan(true);
310}
311
312
313Thread::~Thread()
314{
Greg Clayton5160ce52013-03-27 23:08:40 +0000315 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000317 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")",
318 static_cast<void*>(this), GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000319 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
320 assert (m_destroy_called);
321}
322
323void
324Thread::DestroyThread ()
325{
Jim Inghamb1499242013-10-18 17:11:02 +0000326 // Tell any plans on the plan stacks that the thread is being destroyed since
327 // any plans that have a thread go away in the middle of might need
328 // to do cleanup, or in some cases NOT do cleanup...
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000329 for (auto plan : m_plan_stack)
330 plan->ThreadDestroyed();
331
Jim Inghamb1499242013-10-18 17:11:02 +0000332 for (auto plan : m_discarded_plan_stack)
333 plan->ThreadDestroyed();
334
335 for (auto plan : m_completed_plan_stack)
336 plan->ThreadDestroyed();
337
Greg Clayton35a4cc52012-10-29 20:52:08 +0000338 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000339 m_plan_stack.clear();
340 m_discarded_plan_stack.clear();
341 m_completed_plan_stack.clear();
Greg Clayton6e10f142013-07-30 00:23:06 +0000342
343 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the
344 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
345 // without checking first whether it is destroyed, they won't crash.
346 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
347 m_plan_stack.push_back (null_plan_sp);
348
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000349 m_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000350 m_reg_context_sp.reset();
351 m_unwinder_ap.reset();
352 Mutex::Locker locker(m_frame_mutex);
353 m_curr_frames_sp.reset();
354 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355}
356
Jim Ingham4f465cf2012-10-10 18:32:14 +0000357void
358Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
359{
360 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
361 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
362}
363
Jason Molendaef7d6412015-08-06 03:27:10 +0000364lldb::StackFrameSP
365Thread::GetSelectedFrame()
366{
367 StackFrameListSP stack_frame_list_sp(GetStackFrameList());
368 StackFrameSP frame_sp = stack_frame_list_sp->GetFrameAtIndex (stack_frame_list_sp->GetSelectedFrameIndex());
369 FunctionOptimizationWarning (frame_sp.get());
370 return frame_sp;
371}
372
Jim Ingham4f465cf2012-10-10 18:32:14 +0000373uint32_t
Jason Molendab57e4a12013-11-04 09:33:30 +0000374Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
Jim Ingham4f465cf2012-10-10 18:32:14 +0000375{
376 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
377 if (broadcast)
378 BroadcastSelectedFrameChange(frame->GetStackID());
Jason Molendaef7d6412015-08-06 03:27:10 +0000379 FunctionOptimizationWarning (frame);
Jim Ingham4f465cf2012-10-10 18:32:14 +0000380 return ret_value;
381}
382
383bool
384Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
385{
Jason Molendab57e4a12013-11-04 09:33:30 +0000386 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
Jim Ingham4f465cf2012-10-10 18:32:14 +0000387 if (frame_sp)
388 {
389 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
390 if (broadcast)
391 BroadcastSelectedFrameChange(frame_sp->GetStackID());
Jason Molendaef7d6412015-08-06 03:27:10 +0000392 FunctionOptimizationWarning (frame_sp.get());
Jim Ingham4f465cf2012-10-10 18:32:14 +0000393 return true;
394 }
395 else
396 return false;
397}
398
Jim Ingham93208b82013-01-31 21:46:01 +0000399bool
400Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
401{
402 const bool broadcast = true;
403 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
404 if (success)
405 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000406 StackFrameSP frame_sp = GetSelectedFrame();
Jim Ingham93208b82013-01-31 21:46:01 +0000407 if (frame_sp)
408 {
409 bool already_shown = false;
410 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
411 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
412 {
413 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
414 }
415
416 bool show_frame_info = true;
417 bool show_source = !already_shown;
Jason Molendaef7d6412015-08-06 03:27:10 +0000418 FunctionOptimizationWarning (frame_sp.get());
Jim Ingham93208b82013-01-31 21:46:01 +0000419 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
420 }
421 return false;
422 }
423 else
424 return false;
425}
426
Jason Molendaef7d6412015-08-06 03:27:10 +0000427void
428Thread::FunctionOptimizationWarning (StackFrame *frame)
429{
Jason Molenda484900b2015-08-10 07:55:25 +0000430 if (frame && frame->HasDebugInformation() && GetProcess()->GetWarningsOptimization() == true)
Jason Molendaef7d6412015-08-06 03:27:10 +0000431 {
Jason Molenda03c9cd02015-08-06 21:54:29 +0000432 SymbolContext sc = frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextModule);
Jason Molenda484900b2015-08-10 07:55:25 +0000433 GetProcess()->PrintWarningOptimization (sc);
Jason Molendaef7d6412015-08-06 03:27:10 +0000434 }
435}
436
Jim Ingham4f465cf2012-10-10 18:32:14 +0000437
Jim Inghamb15bfc72010-10-20 00:39:53 +0000438lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000439Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440{
Greg Clayton6e10f142013-07-30 00:23:06 +0000441 if (m_destroy_called)
442 return m_stop_info_sp;
443
Jim Inghamb15bfc72010-10-20 00:39:53 +0000444 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000445 ProcessSP process_sp (GetProcess());
446 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000447 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000448 {
Jim Ingham30fadaf2014-07-08 01:07:32 +0000449 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject(), GetExpressionVariable());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000450 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000451 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000452 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000453 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
454 (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 +0000455 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000456 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000457 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000458 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000459 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000460 GetPrivateStopInfo ();
461 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000462 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000463 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464}
465
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000466lldb::StopInfoSP
467Thread::GetPrivateStopInfo ()
468{
Greg Clayton6e10f142013-07-30 00:23:06 +0000469 if (m_destroy_called)
470 return m_stop_info_sp;
471
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000472 ProcessSP process_sp (GetProcess());
473 if (process_sp)
474 {
Daniel Malea246cb612013-05-14 15:20:12 +0000475 const uint32_t process_stop_id = process_sp->GetStopID();
476 if (m_stop_info_stop_id != process_stop_id)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000477 {
Daniel Malea246cb612013-05-14 15:20:12 +0000478 if (m_stop_info_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000479 {
Daniel Malea246cb612013-05-14 15:20:12 +0000480 if (m_stop_info_sp->IsValid()
481 || IsStillAtLastBreakpointHit()
482 || GetCurrentPlan()->IsVirtualStep())
483 SetStopInfo (m_stop_info_sp);
484 else
485 m_stop_info_sp.reset();
486 }
487
488 if (!m_stop_info_sp)
489 {
490 if (CalculateStopInfo() == false)
491 SetStopInfo (StopInfoSP());
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000492 }
493 }
Greg Claytona97c4d22014-12-09 23:31:02 +0000494
495 // The stop info can be manually set by calling Thread::SetStopInfo()
496 // prior to this function ever getting called, so we can't rely on
497 // "m_stop_info_stop_id != process_stop_id" as the condition for
498 // the if statement below, we must also check the stop info to see
499 // if we need to override it. See the header documentation in
500 // Process::GetStopInfoOverrideCallback() for more information on
501 // the stop info override callback.
502 if (m_stop_info_override_stop_id != process_stop_id)
503 {
504 m_stop_info_override_stop_id = process_stop_id;
505 if (m_stop_info_sp)
506 {
507 ArchSpec::StopInfoOverrideCallbackType callback = GetProcess()->GetStopInfoOverrideCallback();
508 if (callback)
509 callback(*this);
510 }
511 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000512 }
513 return m_stop_info_sp;
514}
515
516
Greg Clayton97d5cf02012-09-25 02:40:06 +0000517lldb::StopReason
518Thread::GetStopReason()
519{
520 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
521 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000522 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000523 return eStopReasonNone;
524}
525
526
Greg Clayton2e309072015-07-17 23:42:28 +0000527bool
528Thread::StopInfoIsUpToDate() const
529{
530 ProcessSP process_sp (GetProcess());
531 if (process_sp)
532 return m_stop_info_stop_id == process_sp->GetStopID();
533 else
534 return true; // Process is no longer around so stop info is always up to date...
535}
Greg Clayton97d5cf02012-09-25 02:40:06 +0000536
Jim Ingham77787032011-01-20 02:03:18 +0000537void
538Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
539{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000540 m_stop_info_sp = stop_info_sp;
541 if (m_stop_info_sp)
Jim Ingham221d51c2013-05-08 00:35:16 +0000542 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000543 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham221d51c2013-05-08 00:35:16 +0000544 // If we are overriding the ShouldReportStop, do that here:
545 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000546 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000547 }
548
Greg Clayton1ac04c32012-02-21 00:09:25 +0000549 ProcessSP process_sp (GetProcess());
550 if (process_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000551 m_stop_info_stop_id = process_sp->GetStopID();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000552 else
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000553 m_stop_info_stop_id = UINT32_MAX;
Greg Clayton8cda7f02013-05-21 21:55:59 +0000554 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
555 if (log)
Ed Maste2bc76432014-06-25 00:38:35 +0000556 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000557 static_cast<void*>(this), GetID(),
558 stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>",
559 m_stop_info_stop_id);
Jim Ingham77787032011-01-20 02:03:18 +0000560}
561
562void
Jim Ingham221d51c2013-05-08 00:35:16 +0000563Thread::SetShouldReportStop (Vote vote)
564{
565 if (vote == eVoteNoOpinion)
566 return;
567 else
568 {
569 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000570 if (m_stop_info_sp)
571 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000572 }
573}
574
575void
Jim Ingham77787032011-01-20 02:03:18 +0000576Thread::SetStopInfoToNothing()
577{
578 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
579 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
580 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
581}
582
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583bool
584Thread::ThreadStoppedForAReason (void)
585{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000586 return (bool) GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
Jim Ingham77787032011-01-20 02:03:18 +0000589bool
590Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
591{
Greg Claytonf74cf862013-11-13 23:28:31 +0000592 saved_state.register_backup_sp.reset();
593 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
594 if (frame_sp)
595 {
596 lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
597 if (reg_checkpoint_sp)
598 {
599 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
600 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp))
601 saved_state.register_backup_sp = reg_checkpoint_sp;
602 }
603 }
604 if (!saved_state.register_backup_sp)
Jim Ingham77787032011-01-20 02:03:18 +0000605 return false;
606
607 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000608 ProcessSP process_sp (GetProcess());
609 if (process_sp)
610 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000611 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
612
Jim Ingham77787032011-01-20 02:03:18 +0000613 return true;
614}
615
616bool
Jim Ingham8559a352012-11-26 23:52:18 +0000617Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000618{
Greg Claytonf74cf862013-11-13 23:28:31 +0000619 if (saved_state.register_backup_sp)
620 {
621 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
622 if (frame_sp)
623 {
624 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
625 if (reg_ctx_sp)
626 {
627 bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp);
628
629 // Clear out all stack frames as our world just changed.
630 ClearStackFrames();
631 reg_ctx_sp->InvalidateIfNeeded(true);
632 if (m_unwinder_ap.get())
633 m_unwinder_ap->Clear();
634 return ret;
635 }
636 }
637 }
638 return false;
Jim Ingham8559a352012-11-26 23:52:18 +0000639}
640
641bool
642Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
643{
Jim Ingham0c270682011-01-25 02:47:23 +0000644 if (saved_state.stop_info_sp)
645 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000646 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000647 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000648 return true;
649}
650
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000651StateType
652Thread::GetState() const
653{
654 // If any other threads access this we will need a mutex for it
655 Mutex::Locker locker(m_state_mutex);
656 return m_state;
657}
658
659void
660Thread::SetState(StateType state)
661{
662 Mutex::Locker locker(m_state_mutex);
663 m_state = state;
664}
665
666void
667Thread::WillStop()
668{
669 ThreadPlan *current_plan = GetCurrentPlan();
670
671 // FIXME: I may decide to disallow threads with no plans. In which
672 // case this should go to an assert.
673
674 if (!current_plan)
675 return;
676
677 current_plan->WillStop();
678}
679
680void
681Thread::SetupForResume ()
682{
683 if (GetResumeState() != eStateSuspended)
684 {
685
686 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
687 // telling the current plan it will resume, since we might change what the current
688 // plan is.
689
Greg Clayton1afa68e2013-04-02 20:32:37 +0000690 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
691 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692 {
Greg Claytona97c4d22014-12-09 23:31:02 +0000693 const addr_t thread_pc = reg_ctx_sp->GetPC();
694 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000695 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000696 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000697 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
698 // special to step over a breakpoint.
699
700 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000701
Greg Claytona97c4d22014-12-09 23:31:02 +0000702 bool push_step_over_bp_plan = false;
703 if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint)
704 {
705 ThreadPlanStepOverBreakpoint *bp_plan = (ThreadPlanStepOverBreakpoint *)cur_plan;
706 if (bp_plan->GetBreakpointLoadAddress() != thread_pc)
707 push_step_over_bp_plan = true;
708 }
709 else
710 push_step_over_bp_plan = true;
711
712 if (push_step_over_bp_plan)
Greg Clayton1afa68e2013-04-02 20:32:37 +0000713 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000714 ThreadPlanSP step_bp_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
715 if (step_bp_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000717 ;
718 step_bp_plan_sp->SetPrivate (true);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000719
720 if (GetCurrentPlan()->RunState() != eStateStepping)
721 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000722 ThreadPlanStepOverBreakpoint *step_bp_plan
723 = static_cast<ThreadPlanStepOverBreakpoint *>(step_bp_plan_sp.get());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000724 step_bp_plan->SetAutoContinue(true);
725 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000726 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 }
729 }
730 }
731 }
732}
733
734bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000735Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736{
737 // At this point clear the completed plan stack.
738 m_completed_plan_stack.clear();
739 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000740 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741
Greg Clayton97d5cf02012-09-25 02:40:06 +0000742 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000743
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000744 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
745 if (backing_thread_sp)
746 backing_thread_sp->m_temporary_resume_state = resume_state;
747
748 // Make sure m_stop_info_sp is valid
749 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000750
Jim Ingham92087d82012-01-31 23:09:20 +0000751 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
752 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
753 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
754 // about the fact that we are resuming...
Jim Inghamacbea8f2015-06-02 20:26:13 +0000755 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000756 if (m_stop_info_stop_id == process_stop_id &&
757 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000758 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000759 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000760 if (stop_info)
761 stop_info->WillResume (resume_state);
762 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763
764 // Tell all the plans that we are about to resume in case they need to clear any state.
765 // We distinguish between the plan on the top of the stack and the lower
766 // plans in case a plan needs to do any special business before it runs.
767
Greg Claytond1d06e42013-04-20 00:27:58 +0000768 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000770 if (plan_ptr)
771 {
772 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773
Greg Claytond1d06e42013-04-20 00:27:58 +0000774 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
775 {
776 plan_ptr->WillResume (resume_state, false);
777 }
778
779 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
780 // In that case, don't reset it here.
781
782 if (need_to_resume && resume_state != eStateSuspended)
783 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000784 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000785 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000786 }
787
Greg Clayton160c9d82013-05-01 21:54:04 +0000788 if (need_to_resume)
789 {
790 ClearStackFrames();
791 // Let Thread subclasses do any special work they need to prior to resuming
792 WillResume (resume_state);
793 }
794
Jim Ingham513c6bb2012-09-01 01:02:41 +0000795 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796}
797
798void
799Thread::DidResume ()
800{
801 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
802}
803
Andrew Kaylor29d65742013-05-10 17:19:04 +0000804void
805Thread::DidStop ()
806{
807 SetState (eStateStopped);
808}
809
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810bool
811Thread::ShouldStop (Event* event_ptr)
812{
813 ThreadPlan *current_plan = GetCurrentPlan();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000814
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815 bool should_stop = true;
816
Greg Clayton5160ce52013-03-27 23:08:40 +0000817 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000818
Jim Ingham92087d82012-01-31 23:09:20 +0000819 if (GetResumeState () == eStateSuspended)
820 {
821 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000822 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 +0000823 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000824 return false;
825 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000826
Jim Ingham92087d82012-01-31 23:09:20 +0000827 if (GetTemporaryResumeState () == eStateSuspended)
828 {
829 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000830 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 +0000831 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000832 return false;
833 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000834
Daniel Malea246cb612013-05-14 15:20:12 +0000835 // Based on the current thread plan and process stop info, check if this
836 // thread caused the process to stop. NOTE: this must take place before
837 // the plan is moved from the current plan stack to the completed plan
838 // stack.
Jim Ingham92087d82012-01-31 23:09:20 +0000839 if (ThreadStoppedForAReason() == false)
840 {
841 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000842 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 +0000843 __FUNCTION__, GetID (), GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000844 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000845 return false;
846 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000847
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848 if (log)
849 {
Jim Inghamdee1bc92013-06-22 00:27:45 +0000850 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 +0000851 __FUNCTION__, static_cast<void*>(this), GetID (),
Greg Clayton160c9d82013-05-01 21:54:04 +0000852 GetProtocolID (),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000853 GetRegisterContext()
854 ? GetRegisterContext()->GetPC()
855 : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000856 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000858 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000860 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000861 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000862
Jim Ingham06e827c2010-11-11 19:26:09 +0000863 // The top most plan always gets to do the trace log...
864 current_plan->DoTraceLog ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000865
Jim Ingham6d66ce62012-04-20 21:16:56 +0000866 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
867 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
868 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000869 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Ingham6d66ce62012-04-20 21:16:56 +0000870 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
871 {
872 if (log)
873 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
874 return false;
875 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876
Jim Inghambad39e42012-09-05 21:12:49 +0000877 // If we've already been restarted, don't query the plans since the state they would examine is not current.
878 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
879 return false;
880
881 // Before the plans see the state of the world, calculate the current inlined depth.
882 GetStackFrameList()->CalculateCurrentInlinedDepth();
883
Jim Ingham25f66702011-12-03 01:52:59 +0000884 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
885 // If that plan is still working, then we don't need to do any more work. If the plan that explains
886 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
887 // whether they still need to do more work.
888
889 bool done_processing_current_plan = false;
890
Jim Ingham0161b492013-02-09 01:29:05 +0000891 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000892 {
893 if (current_plan->TracerExplainsStop())
894 {
895 done_processing_current_plan = true;
896 should_stop = false;
897 }
898 else
899 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000900 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000901 // does and let it handle the situation.
902 ThreadPlan *plan_ptr = current_plan;
903 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
904 {
Jim Ingham0161b492013-02-09 01:29:05 +0000905 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000906 {
907 should_stop = plan_ptr->ShouldStop (event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000908
Jim Ingham25f66702011-12-03 01:52:59 +0000909 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
910 // and all the plans below it off the stack.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000911
Jim Ingham25f66702011-12-03 01:52:59 +0000912 if (plan_ptr->MischiefManaged())
913 {
Jim Ingham031177e2012-05-11 23:49:49 +0000914 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000915 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000916
Jim Ingham25f66702011-12-03 01:52:59 +0000917 do
918 {
919 if (should_stop)
920 current_plan->WillStop();
921 PopPlan();
922 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000923 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
924 // Now, if the responsible plan was not "Okay to discard" then we're done,
925 // otherwise we forward this to the next plan in the stack below.
926 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
927 done_processing_current_plan = true;
928 else
929 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000930 }
931 else
932 done_processing_current_plan = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000933
Jim Ingham25f66702011-12-03 01:52:59 +0000934 break;
935 }
936
937 }
938 }
939 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000940
Jim Ingham25f66702011-12-03 01:52:59 +0000941 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000942 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000943 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000944
Jim Ingham10c4b242011-10-15 00:23:43 +0000945 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000946 log->Printf("Plan %s explains stop, auto-continue %i.",
947 current_plan->GetName(), over_ride_stop);
948
Jim Ingham0f16e732011-02-08 05:20:59 +0000949 // We're starting from the base plan, so just let it decide;
950 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000952 should_stop = current_plan->ShouldStop (event_ptr);
953 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000954 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000955 }
956 else
957 {
958 // Otherwise, don't let the base plan override what the other plans say to do, since
959 // presumably if there were other plans they would know what to do...
960 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000962 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000964
Jim Ingham0f16e732011-02-08 05:20:59 +0000965 should_stop = current_plan->ShouldStop(event_ptr);
966 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000967 log->Printf("Plan %s should stop: %d.",
968 current_plan->GetName(), should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000969 if (current_plan->MischiefManaged())
970 {
971 if (should_stop)
972 current_plan->WillStop();
973
974 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
975 // Otherwise, see if the plan's parent wants to stop.
976
977 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
978 {
979 PopPlan();
980 break;
981 }
982 else
983 {
984
985 PopPlan();
986
987 current_plan = GetCurrentPlan();
988 if (current_plan == NULL)
989 {
990 break;
991 }
992 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000993 }
994 else
995 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000996 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000998 }
999 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001000
Jim Inghamb01e7422010-06-19 04:45:32 +00001001 if (over_ride_stop)
1002 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +00001003
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001004 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001005
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001006 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
1007 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
1008 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
1009 // This code clears stale plans off the stack.
1010
1011 if (should_stop)
1012 {
1013 ThreadPlan *plan_ptr = GetCurrentPlan();
1014 while (!PlanIsBasePlan(plan_ptr))
Jim Ingham64e7ead2012-05-03 21:19:36 +00001015 {
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001016 bool stale = plan_ptr->IsPlanStale ();
1017 ThreadPlan *examined_plan = plan_ptr;
1018 plan_ptr = GetPreviousPlan (examined_plan);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001019
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001020 if (stale)
1021 {
1022 if (log)
1023 log->Printf("Plan %s being discarded in cleanup, it says it is already done.",
1024 examined_plan->GetName());
1025 DiscardThreadPlansUpToPlan(examined_plan);
Jim Ingham64e7ead2012-05-03 21:19:36 +00001026 }
1027 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029
Jim Ingham10c4b242011-10-15 00:23:43 +00001030 if (log)
1031 {
1032 StreamString s;
1033 s.IndentMore();
1034 DumpThreadPlans(&s);
1035 log->Printf ("Plan stack final state:\n%s", s.GetData());
1036 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
1037 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038 return should_stop;
1039}
1040
1041Vote
1042Thread::ShouldReportStop (Event* event_ptr)
1043{
1044 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +00001045 StateType temp_thread_state = GetTemporaryResumeState();
1046
Greg Clayton5160ce52013-03-27 23:08:40 +00001047 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +00001048
1049 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
1050 {
1051 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001052 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 +00001053 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001054 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001055
Jim Ingham92087d82012-01-31 23:09:20 +00001056 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
1057 {
1058 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001059 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 +00001060 return eVoteNoOpinion;
1061 }
1062
1063 if (!ThreadStoppedForAReason())
1064 {
1065 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001066 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 +00001067 return eVoteNoOpinion;
1068 }
1069
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001070 if (m_completed_plan_stack.size() > 0)
1071 {
1072 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +00001073 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001074 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 +00001075 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
1076 }
1077 else
Greg Clayton2cad65a2010-09-03 17:10:42 +00001078 {
Jim Ingham0161b492013-02-09 01:29:05 +00001079 Vote thread_vote = eVoteNoOpinion;
1080 ThreadPlan *plan_ptr = GetCurrentPlan();
1081 while (1)
1082 {
1083 if (plan_ptr->PlanExplainsStop(event_ptr))
1084 {
1085 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1086 break;
1087 }
1088 if (PlanIsBasePlan(plan_ptr))
1089 break;
1090 else
1091 plan_ptr = GetPreviousPlan(plan_ptr);
1092 }
Greg Clayton2cad65a2010-09-03 17:10:42 +00001093 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001094 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 +00001095
1096 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001097 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001098}
1099
1100Vote
1101Thread::ShouldReportRun (Event* event_ptr)
1102{
1103 StateType thread_state = GetResumeState ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001104
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001105 if (thread_state == eStateSuspended
1106 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +00001107 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +00001109 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001110
Greg Clayton5160ce52013-03-27 23:08:40 +00001111 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001112 if (m_completed_plan_stack.size() > 0)
1113 {
1114 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +00001115 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001116 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 +00001117 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001118 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001119 m_completed_plan_stack.back()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001120
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
1122 }
1123 else
Jim Ingham444586b2011-01-24 06:34:17 +00001124 {
1125 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001126 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 +00001127 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001128 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001129 GetCurrentPlan()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001130
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +00001132 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133}
1134
Jim Ingham1b54c882010-06-16 02:00:15 +00001135bool
1136Thread::MatchesSpec (const ThreadSpec *spec)
1137{
1138 if (spec == NULL)
1139 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +00001140
Jim Ingham3d902922012-03-07 22:03:04 +00001141 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +00001142}
1143
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144void
1145Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1146{
1147 if (thread_plan_sp)
1148 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001149 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1150 if (!thread_plan_sp->GetThreadPlanTracer())
1151 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001152 m_plan_stack.push_back (thread_plan_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001153
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001154 thread_plan_sp->DidPush();
1155
Greg Clayton5160ce52013-03-27 23:08:40 +00001156 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157 if (log)
1158 {
1159 StreamString s;
1160 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamdee1bc92013-06-22 00:27:45 +00001161 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001162 static_cast<void*>(this), s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001163 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001164 }
1165 }
1166}
1167
1168void
1169Thread::PopPlan ()
1170{
Greg Clayton5160ce52013-03-27 23:08:40 +00001171 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001172
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001173 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174 return;
1175 else
1176 {
1177 ThreadPlanSP &plan = m_plan_stack.back();
1178 if (log)
1179 {
Daniel Malead01b2952012-11-29 21:49:15 +00001180 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181 }
1182 m_completed_plan_stack.push_back (plan);
1183 plan->WillPop();
1184 m_plan_stack.pop_back();
1185 }
1186}
1187
1188void
1189Thread::DiscardPlan ()
1190{
Jim Inghamdee1bc92013-06-22 00:27:45 +00001191 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192 if (m_plan_stack.size() > 1)
1193 {
1194 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamdee1bc92013-06-22 00:27:45 +00001195 if (log)
1196 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1197
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 m_discarded_plan_stack.push_back (plan);
1199 plan->WillPop();
1200 m_plan_stack.pop_back();
1201 }
1202}
1203
1204ThreadPlan *
1205Thread::GetCurrentPlan ()
1206{
Jim Inghame1471232012-04-19 00:17:05 +00001207 // There will always be at least the base plan. If somebody is mucking with a
1208 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +00001209 if (m_plan_stack.empty())
1210 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +00001211 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001212}
1213
1214ThreadPlanSP
1215Thread::GetCompletedPlan ()
1216{
1217 ThreadPlanSP empty_plan_sp;
1218 if (!m_completed_plan_stack.empty())
1219 {
1220 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1221 {
1222 ThreadPlanSP completed_plan_sp;
1223 completed_plan_sp = m_completed_plan_stack[i];
1224 if (!completed_plan_sp->GetPrivate ())
1225 return completed_plan_sp;
1226 }
1227 }
1228 return empty_plan_sp;
1229}
1230
Jim Ingham73ca05a2011-12-17 01:35:57 +00001231ValueObjectSP
1232Thread::GetReturnValueObject ()
1233{
1234 if (!m_completed_plan_stack.empty())
1235 {
1236 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1237 {
1238 ValueObjectSP return_valobj_sp;
1239 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1240 if (return_valobj_sp)
Greg Claytone5214572015-07-01 23:28:31 +00001241 return return_valobj_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001242 }
1243 }
1244 return ValueObjectSP();
1245}
1246
Jim Ingham30fadaf2014-07-08 01:07:32 +00001247ClangExpressionVariableSP
1248Thread::GetExpressionVariable ()
1249{
1250 if (!m_completed_plan_stack.empty())
1251 {
1252 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1253 {
1254 ClangExpressionVariableSP expression_variable_sp;
1255 expression_variable_sp = m_completed_plan_stack[i]->GetExpressionVariable();
1256 if (expression_variable_sp)
Greg Claytone5214572015-07-01 23:28:31 +00001257 return expression_variable_sp;
Jim Ingham30fadaf2014-07-08 01:07:32 +00001258 }
1259 }
1260 return ClangExpressionVariableSP();
1261}
1262
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001263bool
1264Thread::IsThreadPlanDone (ThreadPlan *plan)
1265{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001266 if (!m_completed_plan_stack.empty())
1267 {
1268 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1269 {
1270 if (m_completed_plan_stack[i].get() == plan)
1271 return true;
1272 }
1273 }
1274 return false;
1275}
1276
1277bool
1278Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1279{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001280 if (!m_discarded_plan_stack.empty())
1281 {
1282 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1283 {
1284 if (m_discarded_plan_stack[i].get() == plan)
1285 return true;
1286 }
1287 }
1288 return false;
1289}
1290
1291ThreadPlan *
1292Thread::GetPreviousPlan (ThreadPlan *current_plan)
1293{
1294 if (current_plan == NULL)
1295 return NULL;
1296
1297 int stack_size = m_completed_plan_stack.size();
1298 for (int i = stack_size - 1; i > 0; i--)
1299 {
1300 if (current_plan == m_completed_plan_stack[i].get())
1301 return m_completed_plan_stack[i-1].get();
1302 }
1303
1304 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1305 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001306 if (m_plan_stack.size() > 0)
1307 return m_plan_stack.back().get();
1308 else
1309 return NULL;
1310 }
1311
1312 stack_size = m_plan_stack.size();
1313 for (int i = stack_size - 1; i > 0; i--)
1314 {
1315 if (current_plan == m_plan_stack[i].get())
1316 return m_plan_stack[i-1].get();
1317 }
1318 return NULL;
1319}
1320
1321void
1322Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1323{
1324 if (abort_other_plans)
1325 DiscardThreadPlans(true);
1326
1327 PushPlan (thread_plan_sp);
1328}
1329
Jim Ingham06e827c2010-11-11 19:26:09 +00001330
1331void
1332Thread::EnableTracer (bool value, bool single_stepping)
1333{
1334 int stack_size = m_plan_stack.size();
1335 for (int i = 0; i < stack_size; i++)
1336 {
1337 if (m_plan_stack[i]->GetThreadPlanTracer())
1338 {
1339 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1340 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1341 }
1342 }
1343}
1344
1345void
1346Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1347{
1348 int stack_size = m_plan_stack.size();
1349 for (int i = 0; i < stack_size; i++)
1350 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1351}
1352
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001353bool
1354Thread::DiscardUserThreadPlansUpToIndex (uint32_t thread_index)
1355{
1356 // Count the user thread plans from the back end to get the number of the one we want
1357 // to discard:
1358
1359 uint32_t idx = 0;
1360 ThreadPlan *up_to_plan_ptr = nullptr;
1361
1362 for (ThreadPlanSP plan_sp : m_plan_stack)
1363 {
1364 if (plan_sp->GetPrivate())
1365 continue;
1366 if (idx == thread_index)
1367 {
1368 up_to_plan_ptr = plan_sp.get();
1369 break;
1370 }
1371 else
1372 idx++;
1373 }
1374
1375 if (up_to_plan_ptr == nullptr)
1376 return false;
1377
1378 DiscardThreadPlansUpToPlan(up_to_plan_ptr);
1379 return true;
1380}
1381
1382
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001383void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001384Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1385{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001386 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1387}
1388
1389void
1390Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1391{
Greg Clayton5160ce52013-03-27 23:08:40 +00001392 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001393 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001394 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p",
1395 GetID(), static_cast<void*>(up_to_plan_ptr));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001396
1397 int stack_size = m_plan_stack.size();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001398
Jim Ingham399f1ca2010-11-05 19:25:48 +00001399 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1400 // stack, and if so discard up to and including it.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001401
Jim Ingham64e7ead2012-05-03 21:19:36 +00001402 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001403 {
1404 for (int i = stack_size - 1; i > 0; i--)
1405 DiscardPlan();
1406 }
1407 else
1408 {
1409 bool found_it = false;
1410 for (int i = stack_size - 1; i > 0; i--)
1411 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001412 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001413 found_it = true;
1414 }
1415 if (found_it)
1416 {
1417 bool last_one = false;
1418 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1419 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001420 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001421 last_one = true;
1422 DiscardPlan();
1423 }
1424 }
1425 }
1426 return;
1427}
1428
1429void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001430Thread::DiscardThreadPlans(bool force)
1431{
Greg Clayton5160ce52013-03-27 23:08:40 +00001432 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433 if (log)
1434 {
Daniel Malead01b2952012-11-29 21:49:15 +00001435 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436 }
1437
1438 if (force)
1439 {
1440 int stack_size = m_plan_stack.size();
1441 for (int i = stack_size - 1; i > 0; i--)
1442 {
1443 DiscardPlan();
1444 }
1445 return;
1446 }
1447
1448 while (1)
1449 {
1450
1451 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001452 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001453
1454 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1455 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1456 {
1457 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1458 {
1459 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1460 break;
1461 }
1462 }
1463
1464 if (discard)
1465 {
1466 // First pop all the dependent plans:
1467 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1468 {
1469
1470 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1471 // for the plan leaves it in a state that it is safe to pop the plan
1472 // with no more notice?
1473 DiscardPlan();
1474 }
1475
1476 // Now discard the master plan itself.
1477 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1478 // discard it's dependent plans, but not it...
1479 if (master_plan_idx > 0)
1480 {
1481 DiscardPlan();
1482 }
1483 }
1484 else
1485 {
1486 // If the master plan doesn't want to get discarded, then we're done.
1487 break;
1488 }
1489
1490 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001491}
1492
Jim Inghamcf274f92012-04-09 22:37:39 +00001493bool
1494Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1495{
1496 if (plan_ptr->IsBasePlan())
1497 return true;
1498 else if (m_plan_stack.size() == 0)
1499 return false;
1500 else
1501 return m_plan_stack[0].get() == plan_ptr;
1502}
1503
Jim Ingham93208b82013-01-31 21:46:01 +00001504Error
1505Thread::UnwindInnermostExpression()
1506{
1507 Error error;
1508 int stack_size = m_plan_stack.size();
1509
1510 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1511 // stack, and if so discard up to and including it.
1512
1513 for (int i = stack_size - 1; i > 0; i--)
1514 {
1515 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1516 {
1517 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1518 return error;
1519 }
1520 }
1521 error.SetErrorString("No expressions currently active on this thread");
1522 return error;
1523}
1524
1525
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001526ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001527Thread::QueueFundamentalPlan (bool abort_other_plans)
1528{
1529 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1530 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001531 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001532}
1533
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001534ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001535Thread::QueueThreadPlanForStepSingleInstruction
1536(
1537 bool step_over,
1538 bool abort_other_plans,
1539 bool stop_other_threads
1540)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001541{
1542 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1543 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001544 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001545}
1546
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001547ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001548Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001549(
1550 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001551 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001552 const SymbolContext &addr_context,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001553 lldb::RunMode stop_other_threads,
1554 LazyBool step_out_avoids_code_withoug_debug_info
Jim Inghamc6276822012-12-12 19:58:40 +00001555)
1556{
1557 ThreadPlanSP thread_plan_sp;
Jim Ingham4b4b2472014-03-13 02:47:14 +00001558 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads, step_out_avoids_code_withoug_debug_info));
1559
Jim Inghamc6276822012-12-12 19:58:40 +00001560 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001561 return thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001562}
1563
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001564ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001565Thread::QueueThreadPlanForStepInRange
1566(
1567 bool abort_other_plans,
1568 const AddressRange &range,
1569 const SymbolContext &addr_context,
1570 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001571 lldb::RunMode stop_other_threads,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001572 LazyBool step_in_avoids_code_without_debug_info,
1573 LazyBool step_out_avoids_code_without_debug_info
Greg Clayton474966a2010-06-12 18:59:55 +00001574)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001575{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001576 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInRange (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001577 range,
1578 addr_context,
1579 stop_other_threads,
1580 step_in_avoids_code_without_debug_info,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001581 step_out_avoids_code_without_debug_info));
1582 ThreadPlanStepInRange *plan = static_cast<ThreadPlanStepInRange *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001583
Jim Inghamc6276822012-12-12 19:58:40 +00001584 if (step_in_target)
1585 plan->SetStepInTarget(step_in_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001586
1587 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001588 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001589}
1590
1591
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001592ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001593Thread::QueueThreadPlanForStepOut
1594(
1595 bool abort_other_plans,
1596 SymbolContext *addr_context,
1597 bool first_insn,
1598 bool stop_other_threads,
1599 Vote stop_vote,
1600 Vote run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001601 uint32_t frame_idx,
1602 LazyBool step_out_avoids_code_withoug_debug_info
Greg Clayton481cef22011-01-21 06:11:58 +00001603)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001604{
Greg Clayton481cef22011-01-21 06:11:58 +00001605 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1606 addr_context,
1607 first_insn,
1608 stop_other_threads,
1609 stop_vote,
1610 run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001611 frame_idx,
1612 step_out_avoids_code_withoug_debug_info));
1613
1614 if (thread_plan_sp->ValidatePlan(NULL))
1615 {
1616 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1617 return thread_plan_sp;
1618 }
1619 else
1620 {
1621 return ThreadPlanSP();
1622 }
1623}
1624
1625ThreadPlanSP
1626Thread::QueueThreadPlanForStepOutNoShouldStop
1627(
1628 bool abort_other_plans,
1629 SymbolContext *addr_context,
1630 bool first_insn,
1631 bool stop_other_threads,
1632 Vote stop_vote,
1633 Vote run_vote,
1634 uint32_t frame_idx
1635)
1636{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001637 ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001638 addr_context,
1639 first_insn,
1640 stop_other_threads,
1641 stop_vote,
1642 run_vote,
1643 frame_idx,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001644 eLazyBoolNo));
1645
1646 ThreadPlanStepOut *new_plan = static_cast<ThreadPlanStepOut *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001647 new_plan->ClearShouldStopHereCallbacks();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001648
Sean Callanan708709c2012-07-31 22:19:25 +00001649 if (thread_plan_sp->ValidatePlan(NULL))
1650 {
1651 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001652 return thread_plan_sp;
Sean Callanan708709c2012-07-31 22:19:25 +00001653 }
1654 else
1655 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001656 return ThreadPlanSP();
Sean Callanan708709c2012-07-31 22:19:25 +00001657 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001658}
1659
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001660ThreadPlanSP
Jim Ingham18de2fd2012-05-10 01:35:39 +00001661Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001662{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001663 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001664 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001665 return ThreadPlanSP();
Jim Ingham25f66702011-12-03 01:52:59 +00001666
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001667 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001668 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001669}
1670
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001671ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001672Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1673 Address &target_addr,
1674 bool stop_other_threads)
1675{
1676 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1677 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001678 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001679}
1680
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001681ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001682Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001683 lldb::addr_t *address_list,
1684 size_t num_addresses,
1685 bool stop_other_threads,
1686 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001687{
Greg Clayton481cef22011-01-21 06:11:58 +00001688 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001689 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001690 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001691
1692}
1693
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001694lldb::ThreadPlanSP
1695Thread::QueueThreadPlanForStepScripted (bool abort_other_plans,
1696 const char *class_name,
1697 bool stop_other_threads)
1698{
1699 ThreadPlanSP thread_plan_sp (new ThreadPlanPython (*this, class_name));
1700 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1701 // This seems a little funny, but I don't want to have to split up the constructor and the
1702 // DidPush in the scripted plan, that seems annoying.
1703 // That means the constructor has to be in DidPush.
1704 // So I have to validate the plan AFTER pushing it, and then take it off again...
1705 if (!thread_plan_sp->ValidatePlan(nullptr))
1706 {
1707 DiscardThreadPlansUpToPlan(thread_plan_sp);
1708 return ThreadPlanSP();
1709 }
1710 else
1711 return thread_plan_sp;
1712
1713}
1714
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001715uint32_t
1716Thread::GetIndexID () const
1717{
1718 return m_index_id;
1719}
1720
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001721static void
1722PrintPlanElement (Stream *s, const ThreadPlanSP &plan, lldb::DescriptionLevel desc_level, int32_t elem_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001723{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001724 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001725 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001726 s->Printf ("Element %d: ", elem_idx);
1727 plan->GetDescription (s, desc_level);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001728 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001729 s->IndentLess();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001730}
1731
1732static void
1733PrintPlanStack (Stream *s, const std::vector<lldb::ThreadPlanSP> &plan_stack, lldb::DescriptionLevel desc_level, bool include_internal)
1734{
1735 int32_t print_idx = 0;
1736 for (ThreadPlanSP plan_sp : plan_stack)
1737 {
1738 if (include_internal || !plan_sp->GetPrivate())
1739 {
1740 PrintPlanElement (s, plan_sp, desc_level, print_idx++);
1741 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001742 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001743}
1744
1745void
1746Thread::DumpThreadPlans (Stream *s,
1747 lldb::DescriptionLevel desc_level,
1748 bool include_internal,
1749 bool ignore_boring_threads) const
1750{
Jason Molenda60b5da62014-10-16 07:53:46 +00001751 uint32_t stack_size;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001752
1753 if (ignore_boring_threads)
1754 {
1755 uint32_t stack_size = m_plan_stack.size();
1756 uint32_t completed_stack_size = m_completed_plan_stack.size();
1757 uint32_t discarded_stack_size = m_discarded_plan_stack.size();
1758 if (stack_size == 1 && completed_stack_size == 0 && discarded_stack_size == 0)
1759 {
1760 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 "\n", GetIndexID(), GetID());
1761 s->IndentMore();
1762 s->Indent();
1763 s->Printf("No active thread plans\n");
1764 s->IndentLess();
1765 return;
1766 }
1767 }
1768
1769 s->Indent();
1770 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 ":\n", GetIndexID(), GetID());
1771 s->IndentMore();
1772 s->Indent();
1773 s->Printf ("Active plan stack:\n");
1774 PrintPlanStack (s, m_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001775
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001776 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001777 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001778 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001779 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001780 s->Printf ("Completed Plan Stack:\n");
1781 PrintPlanStack (s, m_completed_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001782 }
1783
1784 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001785 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001786 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001787 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001788 s->Printf ("Discarded Plan Stack:\n");
1789 PrintPlanStack (s, m_discarded_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001790 }
1791
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001792 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001793}
1794
Greg Claytond9e416c2012-02-18 05:35:26 +00001795TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001796Thread::CalculateTarget ()
1797{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001798 TargetSP target_sp;
1799 ProcessSP process_sp(GetProcess());
1800 if (process_sp)
1801 target_sp = process_sp->CalculateTarget();
1802 return target_sp;
1803
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001804}
1805
Greg Claytond9e416c2012-02-18 05:35:26 +00001806ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001807Thread::CalculateProcess ()
1808{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001809 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001810}
1811
Greg Claytond9e416c2012-02-18 05:35:26 +00001812ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001813Thread::CalculateThread ()
1814{
Greg Claytond9e416c2012-02-18 05:35:26 +00001815 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001816}
1817
Jason Molendab57e4a12013-11-04 09:33:30 +00001818StackFrameSP
1819Thread::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001820{
Jason Molendab57e4a12013-11-04 09:33:30 +00001821 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001822}
1823
1824void
Greg Clayton0603aa92010-10-04 01:05:56 +00001825Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001826{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001827 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001828}
1829
Greg Clayton12daf9462010-08-25 00:35:26 +00001830
Greg Clayton39d0ab32012-03-29 01:41:38 +00001831StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001832Thread::GetStackFrameList ()
1833{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001834 StackFrameListSP frame_list_sp;
1835 Mutex::Locker locker(m_frame_mutex);
1836 if (m_curr_frames_sp)
1837 {
1838 frame_list_sp = m_curr_frames_sp;
1839 }
1840 else
1841 {
1842 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1843 m_curr_frames_sp = frame_list_sp;
1844 }
1845 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001846}
1847
Greg Clayton12daf9462010-08-25 00:35:26 +00001848void
1849Thread::ClearStackFrames ()
1850{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001851 Mutex::Locker locker(m_frame_mutex);
1852
Greg Clayton160c9d82013-05-01 21:54:04 +00001853 Unwind *unwinder = GetUnwinder ();
1854 if (unwinder)
1855 unwinder->Clear();
1856
Jim Inghamb0c72a52012-02-29 03:40:22 +00001857 // Only store away the old "reference" StackFrameList if we got all its frames:
1858 // FIXME: At some point we can try to splice in the frames we have fetched into
1859 // the new frame as we make it, but let's not try that now.
1860 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001861 m_prev_frames_sp.swap (m_curr_frames_sp);
1862 m_curr_frames_sp.reset();
Jason Molenda705b1802014-06-13 02:37:02 +00001863
1864 m_extended_info.reset();
1865 m_extended_info_fetched = false;
Jim Ingham87c11912010-08-12 02:14:28 +00001866}
1867
Jason Molendab57e4a12013-11-04 09:33:30 +00001868lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001869Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1870{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001871 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001872}
1873
Jim Ingham44137582012-09-12 00:40:39 +00001874
1875Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001876Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001877{
Jason Molendab57e4a12013-11-04 09:33:30 +00001878 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001879 Error return_error;
1880
1881 if (!frame_sp)
1882 {
Daniel Malead01b2952012-11-29 21:49:15 +00001883 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001884 }
1885
Jim Ingham4f465cf2012-10-10 18:32:14 +00001886 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001887}
1888
1889Error
Jason Molendab57e4a12013-11-04 09:33:30 +00001890Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001891{
1892 Error return_error;
1893
1894 if (!frame_sp)
1895 {
1896 return_error.SetErrorString("Can't return to a null frame.");
1897 return return_error;
1898 }
1899
1900 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001901 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
Jason Molendab57e4a12013-11-04 09:33:30 +00001902 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001903 if (!older_frame_sp)
1904 {
1905 return_error.SetErrorString("No older frame to return to.");
1906 return return_error;
1907 }
Jim Ingham44137582012-09-12 00:40:39 +00001908
1909 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001910 {
Jim Ingham44137582012-09-12 00:40:39 +00001911 lldb::ABISP abi = thread->GetProcess()->GetABI();
1912 if (!abi)
1913 {
1914 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001915 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001916 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001917 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1918
1919 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1920 // Turn that back on when that works.
Jason Molenda97e704b2014-10-16 08:07:20 +00001921 if (/* DISABLES CODE */ (0) && sc.function != NULL)
Jim Ingham1f51e602012-09-27 01:15:29 +00001922 {
1923 Type *function_type = sc.function->GetType();
1924 if (function_type)
1925 {
Greg Clayton99558cc42015-08-24 23:46:31 +00001926 CompilerType return_type = sc.function->GetCompilerType().GetFunctionReturnType();
Jim Ingham1f51e602012-09-27 01:15:29 +00001927 if (return_type)
1928 {
Jim Ingham1f51e602012-09-27 01:15:29 +00001929 StreamString s;
Greg Clayton57ee3062013-07-11 22:46:58 +00001930 return_type.DumpTypeDescription(&s);
1931 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham1f51e602012-09-27 01:15:29 +00001932 if (cast_value_sp)
1933 {
1934 cast_value_sp->SetFormat(eFormatHex);
1935 return_value_sp = cast_value_sp;
1936 }
1937 }
1938 }
1939 }
1940
Jim Inghamcb640dd2012-09-14 02:14:15 +00001941 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001942 if (!return_error.Success())
1943 return return_error;
1944 }
1945
1946 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001947 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001948 // cook their data
1949
Jason Molendab57e4a12013-11-04 09:33:30 +00001950 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
Jim Ingham93208b82013-01-31 21:46:01 +00001951 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001952 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001953 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1954 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001955 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001956 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1957 if (copy_success)
1958 {
1959 thread->DiscardThreadPlans(true);
1960 thread->ClearStackFrames();
1961 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1962 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1963 }
1964 else
1965 {
1966 return_error.SetErrorString("Could not reset register values.");
1967 }
Jim Ingham93208b82013-01-31 21:46:01 +00001968 }
1969 else
1970 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001971 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001972 }
Jim Ingham44137582012-09-12 00:40:39 +00001973 }
1974 else
1975 {
Jim Ingham93208b82013-01-31 21:46:01 +00001976 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001977 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001978 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001979}
1980
Richard Mittonf86248d2013-09-12 02:20:34 +00001981static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1982{
1983 for (size_t n=0;n<list.size();n++)
1984 {
1985 s << "\t";
1986 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1987 s << "\n";
1988 }
1989}
1990
1991Error
1992Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1993{
1994 ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
1995 Target *target = exe_ctx.GetTargetPtr();
1996 TargetSP target_sp = exe_ctx.GetTargetSP();
1997 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00001998 StackFrame *frame = exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00001999 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
2000
2001 // Find candidate locations.
2002 std::vector<Address> candidates, within_function, outside_function;
2003 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
2004
2005 // If possible, we try and stay within the current function.
2006 // Within a function, we accept multiple locations (optimized code may do this,
2007 // there's no solution here so we do the best we can).
2008 // However if we're trying to leave the function, we don't know how to pick the
2009 // right location, so if there's more than one then we bail.
2010 if (!within_function.empty())
2011 candidates = within_function;
2012 else if (outside_function.size() == 1 && can_leave_function)
2013 candidates = outside_function;
2014
2015 // Check if we got anything.
2016 if (candidates.empty())
2017 {
2018 if (outside_function.empty())
2019 {
2020 return Error("Cannot locate an address for %s:%i.",
2021 file.GetFilename().AsCString(), line);
2022 }
2023 else if (outside_function.size() == 1)
2024 {
2025 return Error("%s:%i is outside the current function.",
2026 file.GetFilename().AsCString(), line);
2027 }
2028 else
2029 {
2030 StreamString sstr;
2031 DumpAddressList(sstr, outside_function, target);
2032 return Error("%s:%i has multiple candidate locations:\n%s",
2033 file.GetFilename().AsCString(), line, sstr.GetString().c_str());
2034 }
2035 }
2036
2037 // Accept the first location, warn about any others.
2038 Address dest = candidates[0];
2039 if (warnings && candidates.size() > 1)
2040 {
2041 StreamString sstr;
2042 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
2043 file.GetFilename().AsCString(), line);
2044 DumpAddressList(sstr, candidates, target);
2045 *warnings = sstr.GetString();
2046 }
2047
2048 if (!reg_ctx->SetPC (dest))
2049 return Error("Cannot change PC to target address.");
2050
2051 return Error();
2052}
2053
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002054void
Greg Clayton0603aa92010-10-04 01:05:56 +00002055Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002056{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002057 ExecutionContext exe_ctx (shared_from_this());
2058 Process *process = exe_ctx.GetProcessPtr();
2059 if (process == NULL)
2060 return;
2061
Jason Molendab57e4a12013-11-04 09:33:30 +00002062 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00002063 SymbolContext frame_sc;
Jim Inghamd1f25362015-01-28 01:17:26 +00002064 if (frame_idx != LLDB_INVALID_FRAME_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002065 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002066 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002067 if (frame_sp)
2068 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002069 exe_ctx.SetFrameSP(frame_sp);
2070 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002071 }
2072 }
2073
Greg Clayton554f68d2015-02-04 22:00:53 +00002074 const FormatEntity::Entry *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00002075 assert (thread_format);
Greg Clayton554f68d2015-02-04 22:00:53 +00002076
2077 FormatEntity::Format(*thread_format,
2078 strm,
2079 frame_sp ? &frame_sc : NULL,
2080 &exe_ctx,
2081 NULL,
2082 NULL,
2083 false,
2084 false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002085}
2086
Greg Clayton99d0faf2010-11-18 23:32:35 +00002087void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002088Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00002089{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002090}
Jim Inghamee8aea12010-09-08 03:14:33 +00002091
Greg Clayton99d0faf2010-11-18 23:32:35 +00002092void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002093Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002094{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002095}
Jim Inghamee8aea12010-09-08 03:14:33 +00002096
Richard Mitton0a558352013-10-17 21:14:00 +00002097lldb::addr_t
2098Thread::GetThreadPointer ()
2099{
2100 return LLDB_INVALID_ADDRESS;
2101}
2102
2103addr_t
2104Thread::GetThreadLocalData (const ModuleSP module)
2105{
2106 // The default implementation is to ask the dynamic loader for it.
2107 // This can be overridden for specific platforms.
2108 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
2109 if (loader)
2110 return loader->GetThreadLocalData (module, shared_from_this());
2111 else
2112 return LLDB_INVALID_ADDRESS;
2113}
2114
Jason Molendab4892cd2014-05-13 22:02:48 +00002115bool
2116Thread::SafeToCallFunctions ()
2117{
2118 Process *process = GetProcess().get();
2119 if (process)
2120 {
2121 SystemRuntime *runtime = process->GetSystemRuntime ();
2122 if (runtime)
2123 {
2124 return runtime->SafeToCallFunctionsOnThisThread (shared_from_this());
2125 }
2126 }
2127 return true;
2128}
2129
Jason Molendab57e4a12013-11-04 09:33:30 +00002130lldb::StackFrameSP
2131Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
Jim Inghame4284b72010-09-23 17:40:12 +00002132{
Jason Molendab57e4a12013-11-04 09:33:30 +00002133 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00002134}
Caroline Ticeceb6b132010-10-26 03:11:13 +00002135
2136const char *
2137Thread::StopReasonAsCString (lldb::StopReason reason)
2138{
2139 switch (reason)
2140 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00002141 case eStopReasonInvalid: return "invalid";
2142 case eStopReasonNone: return "none";
2143 case eStopReasonTrace: return "trace";
2144 case eStopReasonBreakpoint: return "breakpoint";
2145 case eStopReasonWatchpoint: return "watchpoint";
2146 case eStopReasonSignal: return "signal";
2147 case eStopReasonException: return "exception";
2148 case eStopReasonExec: return "exec";
2149 case eStopReasonPlanComplete: return "plan complete";
2150 case eStopReasonThreadExiting: return "thread exiting";
Kuba Breckaafdf8422014-10-10 23:43:03 +00002151 case eStopReasonInstrumentation: return "instrumentation break";
Caroline Ticeceb6b132010-10-26 03:11:13 +00002152 }
2153
2154
2155 static char unknown_state_string[64];
2156 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
2157 return unknown_state_string;
2158}
2159
2160const char *
2161Thread::RunModeAsCString (lldb::RunMode mode)
2162{
2163 switch (mode)
2164 {
2165 case eOnlyThisThread: return "only this thread";
2166 case eAllThreads: return "all threads";
2167 case eOnlyDuringStepping: return "only during stepping";
2168 }
2169
2170 static char unknown_state_string[64];
2171 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
2172 return unknown_state_string;
2173}
Jim Ingham06e827c2010-11-11 19:26:09 +00002174
Greg Clayton7260f622011-04-18 08:33:37 +00002175size_t
2176Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
2177{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002178 ExecutionContext exe_ctx (shared_from_this());
2179 Target *target = exe_ctx.GetTargetPtr();
2180 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00002181 size_t num_frames_shown = 0;
2182 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00002183 bool is_selected = false;
2184 if (process)
2185 {
2186 if (process->GetThreadList().GetSelectedThread().get() == this)
2187 is_selected = true;
2188 }
2189 strm.Printf("%c ", is_selected ? '*' : ' ');
2190 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00002191 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002192 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00002193 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00002194 {
Jim Inghame610d642011-08-16 00:07:28 +00002195 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
2196 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
2197 {
2198 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
2199 }
Greg Clayton5113dc82011-08-12 06:47:54 +00002200 }
Greg Clayton7260f622011-04-18 08:33:37 +00002201 }
2202
2203 DumpUsingSettingsFormat (strm, start_frame);
2204
2205 if (num_frames > 0)
2206 {
2207 strm.IndentMore();
2208
2209 const bool show_frame_info = true;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002210
2211 const char *selected_frame_marker = NULL;
2212 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
2213 strm.IndentMore ();
2214 else
2215 selected_frame_marker = "* ";
2216
Greg Clayton39d0ab32012-03-29 01:41:38 +00002217 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
2218 start_frame,
2219 num_frames,
2220 show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002221 num_frames_with_source,
2222 selected_frame_marker);
2223 if (num_frames == 1)
2224 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00002225 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00002226 }
2227 return num_frames_shown;
2228}
2229
Jason Molenda705b1802014-06-13 02:37:02 +00002230bool
Kuba Breckaafdf8422014-10-10 23:43:03 +00002231Thread::GetDescription (Stream &strm, lldb::DescriptionLevel level, bool print_json_thread, bool print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002232{
2233 DumpUsingSettingsFormat (strm, 0);
2234 strm.Printf("\n");
2235
2236 StructuredData::ObjectSP thread_info = GetExtendedInfo();
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002237
Kuba Breckaafdf8422014-10-10 23:43:03 +00002238 if (print_json_thread || print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002239 {
Kuba Breckaafdf8422014-10-10 23:43:03 +00002240 if (thread_info && print_json_thread)
2241 {
2242 thread_info->Dump (strm);
2243 strm.Printf("\n");
2244 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002245
2246 if (print_json_stopinfo && m_stop_info_sp)
Kuba Breckaafdf8422014-10-10 23:43:03 +00002247 {
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002248 StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo();
2249 if (stop_info)
2250 {
2251 stop_info->Dump (strm);
2252 strm.Printf("\n");
2253 }
Kuba Breckaafdf8422014-10-10 23:43:03 +00002254 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002255
Jason Molenda705b1802014-06-13 02:37:02 +00002256 return true;
2257 }
2258
2259 if (thread_info)
2260 {
2261 StructuredData::ObjectSP activity = thread_info->GetObjectForDotSeparatedPath("activity");
2262 StructuredData::ObjectSP breadcrumb = thread_info->GetObjectForDotSeparatedPath("breadcrumb");
2263 StructuredData::ObjectSP messages = thread_info->GetObjectForDotSeparatedPath("trace_messages");
2264
2265 bool printed_activity = false;
2266 if (activity && activity->GetType() == StructuredData::Type::eTypeDictionary)
2267 {
2268 StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
2269 StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
2270 StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
2271 if (name && name->GetType() == StructuredData::Type::eTypeString
2272 && id && id->GetType() == StructuredData::Type::eTypeInteger)
2273 {
2274 strm.Printf(" Activity '%s', 0x%" PRIx64 "\n", name->GetAsString()->GetValue().c_str(), id->GetAsInteger()->GetValue());
2275 }
2276 printed_activity = true;
2277 }
2278 bool printed_breadcrumb = false;
2279 if (breadcrumb && breadcrumb->GetType() == StructuredData::Type::eTypeDictionary)
2280 {
2281 if (printed_activity)
2282 strm.Printf ("\n");
2283 StructuredData::Dictionary *breadcrumb_dict = breadcrumb->GetAsDictionary();
2284 StructuredData::ObjectSP breadcrumb_text = breadcrumb_dict->GetValueForKey ("name");
2285 if (breadcrumb_text && breadcrumb_text->GetType() == StructuredData::Type::eTypeString)
2286 {
2287 strm.Printf (" Current Breadcrumb: %s\n", breadcrumb_text->GetAsString()->GetValue().c_str());
2288 }
2289 printed_breadcrumb = true;
2290 }
2291 if (messages && messages->GetType() == StructuredData::Type::eTypeArray)
2292 {
2293 if (printed_breadcrumb)
2294 strm.Printf("\n");
2295 StructuredData::Array *messages_array = messages->GetAsArray();
2296 const size_t msg_count = messages_array->GetSize();
2297 if (msg_count > 0)
2298 {
2299 strm.Printf (" %zu trace messages:\n", msg_count);
2300 for (size_t i = 0; i < msg_count; i++)
2301 {
2302 StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
2303 if (message && message->GetType() == StructuredData::Type::eTypeDictionary)
2304 {
2305 StructuredData::Dictionary *message_dict = message->GetAsDictionary();
2306 StructuredData::ObjectSP message_text = message_dict->GetValueForKey ("message");
2307 if (message_text && message_text->GetType() == StructuredData::Type::eTypeString)
2308 {
2309 strm.Printf (" %s\n", message_text->GetAsString()->GetValue().c_str());
2310 }
2311 }
2312 }
2313 }
2314 }
2315 }
2316
2317 return true;
2318}
2319
Greg Clayton7260f622011-04-18 08:33:37 +00002320size_t
2321Thread::GetStackFrameStatus (Stream& strm,
2322 uint32_t first_frame,
2323 uint32_t num_frames,
2324 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002325 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00002326{
Greg Clayton39d0ab32012-03-29 01:41:38 +00002327 return GetStackFrameList()->GetStatus (strm,
2328 first_frame,
2329 num_frames,
2330 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002331 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00002332}
2333
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002334Unwind *
2335Thread::GetUnwinder ()
2336{
2337 if (m_unwinder_ap.get() == NULL)
2338 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00002339 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002340 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2341 switch (machine)
2342 {
2343 case llvm::Triple::x86_64:
2344 case llvm::Triple::x86:
2345 case llvm::Triple::arm:
Todd Fialad8eaa172014-07-23 14:37:35 +00002346 case llvm::Triple::aarch64:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002347 case llvm::Triple::thumb:
Bhushan D. Attarde794a4d52015-05-15 06:53:30 +00002348 case llvm::Triple::mips:
2349 case llvm::Triple::mipsel:
Ed Masteb73f8442013-10-10 00:59:47 +00002350 case llvm::Triple::mips64:
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +00002351 case llvm::Triple::mips64el:
Justin Hibbits6256a0e2014-10-31 02:34:28 +00002352 case llvm::Triple::ppc:
Justin Hibbitsdb39cdf2014-10-31 15:57:52 +00002353 case llvm::Triple::ppc64:
Deepak Panickal6d3df422014-02-19 11:16:46 +00002354 case llvm::Triple::hexagon:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002355 m_unwinder_ap.reset (new UnwindLLDB (*this));
2356 break;
2357
2358 default:
2359 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2360 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2361 break;
2362 }
2363 }
2364 return m_unwinder_ap.get();
2365}
2366
2367
Greg Claytonfa559e52012-05-18 02:38:05 +00002368void
2369Thread::Flush ()
2370{
2371 ClearStackFrames ();
2372 m_reg_context_sp.reset();
2373}
Jim Ingham5d88a062012-10-16 00:09:33 +00002374
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00002375bool
Jim Ingham5d88a062012-10-16 00:09:33 +00002376Thread::IsStillAtLastBreakpointHit ()
2377{
2378 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2379 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2380 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002381 if (m_stop_info_sp) {
2382 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00002383 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002384 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00002385 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2386 if (reg_ctx_sp)
2387 {
2388 lldb::addr_t pc = reg_ctx_sp->GetPC();
2389 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002390 if (bp_site_sp &&
2391 static_cast<break_id_t>(value) == bp_site_sp->GetID())
Greg Clayton1afa68e2013-04-02 20:32:37 +00002392 return true;
2393 }
Jim Ingham5d88a062012-10-16 00:09:33 +00002394 }
2395 }
2396 return false;
2397}
Greg Clayton44d93782014-01-27 23:43:24 +00002398
2399
2400Error
2401Thread::StepIn (bool source_step,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002402 LazyBool step_in_avoids_code_without_debug_info,
2403 LazyBool step_out_avoids_code_without_debug_info)
2404
Greg Clayton44d93782014-01-27 23:43:24 +00002405{
2406 Error error;
2407 Process *process = GetProcess().get();
2408 if (StateIsStoppedState (process->GetState(), true))
2409 {
2410 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2411 ThreadPlanSP new_plan_sp;
2412 const lldb::RunMode run_mode = eOnlyThisThread;
2413 const bool abort_other_plans = false;
2414
2415 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2416 {
2417 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2418 new_plan_sp = QueueThreadPlanForStepInRange (abort_other_plans,
2419 sc.line_entry.range,
2420 sc,
2421 NULL,
2422 run_mode,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002423 step_in_avoids_code_without_debug_info,
2424 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002425 }
2426 else
2427 {
2428 new_plan_sp = QueueThreadPlanForStepSingleInstruction (false,
2429 abort_other_plans,
2430 run_mode);
2431 }
2432
2433 new_plan_sp->SetIsMasterPlan(true);
2434 new_plan_sp->SetOkayToDiscard(false);
2435
2436 // Why do we need to set the current thread by ID here???
2437 process->GetThreadList().SetSelectedThreadByID (GetID());
2438 error = process->Resume();
2439 }
2440 else
2441 {
2442 error.SetErrorString("process not stopped");
2443 }
2444 return error;
2445}
2446
2447Error
Jim Ingham4b4b2472014-03-13 02:47:14 +00002448Thread::StepOver (bool source_step,
2449 LazyBool step_out_avoids_code_without_debug_info)
Greg Clayton44d93782014-01-27 23:43:24 +00002450{
2451 Error error;
2452 Process *process = GetProcess().get();
2453 if (StateIsStoppedState (process->GetState(), true))
2454 {
2455 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2456 ThreadPlanSP new_plan_sp;
2457
2458 const lldb::RunMode run_mode = eOnlyThisThread;
2459 const bool abort_other_plans = false;
2460
2461 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2462 {
2463 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2464 new_plan_sp = QueueThreadPlanForStepOverRange (abort_other_plans,
2465 sc.line_entry.range,
2466 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002467 run_mode,
2468 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002469 }
2470 else
2471 {
2472 new_plan_sp = QueueThreadPlanForStepSingleInstruction (true,
2473 abort_other_plans,
2474 run_mode);
2475 }
2476
2477 new_plan_sp->SetIsMasterPlan(true);
2478 new_plan_sp->SetOkayToDiscard(false);
2479
2480 // Why do we need to set the current thread by ID here???
2481 process->GetThreadList().SetSelectedThreadByID (GetID());
2482 error = process->Resume();
2483 }
2484 else
2485 {
2486 error.SetErrorString("process not stopped");
2487 }
2488 return error;
2489}
2490
2491Error
2492Thread::StepOut ()
2493{
2494 Error error;
2495 Process *process = GetProcess().get();
2496 if (StateIsStoppedState (process->GetState(), true))
2497 {
2498 const bool first_instruction = false;
2499 const bool stop_other_threads = false;
2500 const bool abort_other_plans = false;
2501
2502 ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut (abort_other_plans,
2503 NULL,
2504 first_instruction,
2505 stop_other_threads,
2506 eVoteYes,
2507 eVoteNoOpinion,
2508 0));
2509
2510 new_plan_sp->SetIsMasterPlan(true);
2511 new_plan_sp->SetOkayToDiscard(false);
2512
2513 // Why do we need to set the current thread by ID here???
2514 process->GetThreadList().SetSelectedThreadByID (GetID());
2515 error = process->Resume();
2516 }
2517 else
2518 {
2519 error.SetErrorString("process not stopped");
2520 }
2521 return error;
Jim Ingham4b4b2472014-03-13 02:47:14 +00002522}