blob: 17f06194fc1abb7d98e81d30f9058bb61621db54 [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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/lldb-private-log.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000013#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000014#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Log.h"
Greg Clayton160c9d82013-05-01 21:54:04 +000016#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Stream.h"
18#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000019#include "lldb/Core/RegularExpression.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"
Jim Ingham1f51e602012-09-27 01:15:29 +000022#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Target/DynamicLoader.h"
24#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000025#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Target/Process.h"
27#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000028#include "lldb/Target/StopInfo.h"
Jason Molendab4892cd2014-05-13 22:02:48 +000029#include "lldb/Target/SystemRuntime.h"
Greg Clayton896dff62010-07-23 16:45:51 +000030#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Target/Thread.h"
32#include "lldb/Target/ThreadPlan.h"
33#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Target/ThreadPlanBase.h"
Jim Ingham2bdbfd52014-09-29 23:17:18 +000035#include "lldb/Target/ThreadPlanPython.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Target/ThreadPlanStepInstruction.h"
37#include "lldb/Target/ThreadPlanStepOut.h"
38#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
39#include "lldb/Target/ThreadPlanStepThrough.h"
40#include "lldb/Target/ThreadPlanStepInRange.h"
41#include "lldb/Target/ThreadPlanStepOverRange.h"
42#include "lldb/Target/ThreadPlanRunToAddress.h"
43#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000044#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000045#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000046#include "Plugins/Process/Utility/UnwindLLDB.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000047#include "Plugins/Process/Utility/UnwindMacOSXFrameBackchain.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000048
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
50using namespace lldb;
51using namespace lldb_private;
52
Greg Clayton67cc0632012-08-22 17:17:09 +000053
54const ThreadPropertiesSP &
55Thread::GetGlobalProperties()
56{
57 static ThreadPropertiesSP g_settings_sp;
58 if (!g_settings_sp)
59 g_settings_sp.reset (new ThreadProperties (true));
60 return g_settings_sp;
61}
62
63static PropertyDefinition
64g_properties[] =
65{
Jim Ingham4b4b2472014-03-13 02:47:14 +000066 { "step-in-avoid-nodebug", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, step-in will not stop in functions with no debug information." },
67 { "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 "
68 "debug information. Passing a frame argument to step-out will override this option." },
Greg Clayton67cc0632012-08-22 17:17:09 +000069 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
Jim Ingham4da62062014-01-23 21:52:47 +000070 { "step-avoid-libraries", OptionValue::eTypeFileSpecList , true , REG_EXTENDED, NULL, NULL, "A list of libraries that source stepping won't stop in." },
Greg Clayton67cc0632012-08-22 17:17:09 +000071 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
72 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
73};
74
75enum {
Jim Ingham4b4b2472014-03-13 02:47:14 +000076 ePropertyStepInAvoidsNoDebug,
77 ePropertyStepOutAvoidsNoDebug,
Greg Clayton67cc0632012-08-22 17:17:09 +000078 ePropertyStepAvoidRegex,
Jim Ingham4da62062014-01-23 21:52:47 +000079 ePropertyStepAvoidLibraries,
Greg Clayton67cc0632012-08-22 17:17:09 +000080 ePropertyEnableThreadTrace
81};
82
83
84class ThreadOptionValueProperties : public OptionValueProperties
85{
86public:
87 ThreadOptionValueProperties (const ConstString &name) :
88 OptionValueProperties (name)
89 {
90 }
91
92 // This constructor is used when creating ThreadOptionValueProperties when it
93 // is part of a new lldb_private::Thread instance. It will copy all current
94 // global property values as needed
95 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +000096 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +000097 {
98 }
99
100 virtual const Property *
101 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
102 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000103 // When getting the value for a key from the thread options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +0000104 // try and grab the setting from the current thread if there is one. Else we just
105 // use the one from this instance.
106 if (exe_ctx)
107 {
108 Thread *thread = exe_ctx->GetThreadPtr();
109 if (thread)
110 {
111 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
112 if (this != instance_properties)
113 return instance_properties->ProtectedGetPropertyAtIndex (idx);
114 }
115 }
116 return ProtectedGetPropertyAtIndex (idx);
117 }
118};
119
120
121
122ThreadProperties::ThreadProperties (bool is_global) :
123 Properties ()
124{
125 if (is_global)
126 {
127 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
128 m_collection_sp->Initialize(g_properties);
129 }
130 else
131 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
132}
133
134ThreadProperties::~ThreadProperties()
135{
136}
137
138const RegularExpression *
139ThreadProperties::GetSymbolsToAvoidRegexp()
140{
141 const uint32_t idx = ePropertyStepAvoidRegex;
142 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
143}
144
Jim Ingham4da62062014-01-23 21:52:47 +0000145FileSpecList &
146ThreadProperties::GetLibrariesToAvoid() const
147{
148 const uint32_t idx = ePropertyStepAvoidLibraries;
149 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
150 assert(option_value);
151 return option_value->GetCurrentValue();
152}
153
Greg Clayton67cc0632012-08-22 17:17:09 +0000154bool
155ThreadProperties::GetTraceEnabledState() const
156{
157 const uint32_t idx = ePropertyEnableThreadTrace;
158 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
159}
160
Jim Ingham4b4b2472014-03-13 02:47:14 +0000161bool
162ThreadProperties::GetStepInAvoidsNoDebug() const
163{
164 const uint32_t idx = ePropertyStepInAvoidsNoDebug;
165 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
166}
167
168bool
169ThreadProperties::GetStepOutAvoidsNoDebug() const
170{
171 const uint32_t idx = ePropertyStepOutAvoidsNoDebug;
172 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
173}
174
175
Jim Ingham4f465cf2012-10-10 18:32:14 +0000176//------------------------------------------------------------------
177// Thread Event Data
178//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000179
Jim Ingham4f465cf2012-10-10 18:32:14 +0000180
181const ConstString &
182Thread::ThreadEventData::GetFlavorString ()
183{
184 static ConstString g_flavor ("Thread::ThreadEventData");
185 return g_flavor;
186}
187
188Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
189 m_thread_sp (thread_sp),
190 m_stack_id ()
191{
192}
193
194Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
195 m_thread_sp (thread_sp),
196 m_stack_id (stack_id)
197{
198}
199
200Thread::ThreadEventData::ThreadEventData () :
201 m_thread_sp (),
202 m_stack_id ()
203{
204}
205
206Thread::ThreadEventData::~ThreadEventData ()
207{
208}
209
210void
211Thread::ThreadEventData::Dump (Stream *s) const
212{
213
214}
215
216const Thread::ThreadEventData *
217Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
218{
219 if (event_ptr)
220 {
221 const EventData *event_data = event_ptr->GetData();
222 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
223 return static_cast <const ThreadEventData *> (event_ptr->GetData());
224 }
225 return NULL;
226}
227
228ThreadSP
229Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
230{
231 ThreadSP thread_sp;
232 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
233 if (event_data)
234 thread_sp = event_data->GetThread();
235 return thread_sp;
236}
237
238StackID
239Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
240{
241 StackID stack_id;
242 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
243 if (event_data)
244 stack_id = event_data->GetStackID();
245 return stack_id;
246}
247
Jason Molendab57e4a12013-11-04 09:33:30 +0000248StackFrameSP
Jim Ingham4f465cf2012-10-10 18:32:14 +0000249Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
250{
251 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
Jason Molendab57e4a12013-11-04 09:33:30 +0000252 StackFrameSP frame_sp;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000253 if (event_data)
254 {
255 ThreadSP thread_sp = event_data->GetThread();
256 if (thread_sp)
257 {
258 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
259 }
260 }
261 return frame_sp;
262}
263
264//------------------------------------------------------------------
265// Thread class
266//------------------------------------------------------------------
267
268ConstString &
269Thread::GetStaticBroadcasterClass ()
270{
271 static ConstString class_name ("lldb.thread");
272 return class_name;
273}
274
Jason Molendaa8ff5432014-03-06 06:31:18 +0000275Thread::Thread (Process &process, lldb::tid_t tid, bool use_invalid_index_id) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000276 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000278 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
279 m_process_wp (process.shared_from_this()),
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000280 m_stop_info_sp (),
281 m_stop_info_stop_id (0),
Jason Molendaa8ff5432014-03-06 06:31:18 +0000282 m_index_id (use_invalid_index_id ? LLDB_INVALID_INDEX32 : process.GetNextThreadIndexID(tid)),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000285 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000288 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000289 m_curr_frames_sp (),
290 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000292 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000293 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000294 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000295 m_destroy_called (false),
Jason Molenda705b1802014-06-13 02:37:02 +0000296 m_override_should_notify (eLazyBoolCalculate),
297 m_extended_info_fetched (false),
298 m_extended_info ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299{
Greg Clayton5160ce52013-03-27 23:08:40 +0000300 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000302 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")",
303 static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304
Jim Ingham4f465cf2012-10-10 18:32:14 +0000305 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306 QueueFundamentalPlan(true);
307}
308
309
310Thread::~Thread()
311{
Greg Clayton5160ce52013-03-27 23:08:40 +0000312 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000314 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")",
315 static_cast<void*>(this), GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000316 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
317 assert (m_destroy_called);
318}
319
320void
321Thread::DestroyThread ()
322{
Jim Inghamb1499242013-10-18 17:11:02 +0000323 // Tell any plans on the plan stacks that the thread is being destroyed since
324 // any plans that have a thread go away in the middle of might need
325 // to do cleanup, or in some cases NOT do cleanup...
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000326 for (auto plan : m_plan_stack)
327 plan->ThreadDestroyed();
328
Jim Inghamb1499242013-10-18 17:11:02 +0000329 for (auto plan : m_discarded_plan_stack)
330 plan->ThreadDestroyed();
331
332 for (auto plan : m_completed_plan_stack)
333 plan->ThreadDestroyed();
334
Greg Clayton35a4cc52012-10-29 20:52:08 +0000335 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000336 m_plan_stack.clear();
337 m_discarded_plan_stack.clear();
338 m_completed_plan_stack.clear();
Greg Clayton6e10f142013-07-30 00:23:06 +0000339
340 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the
341 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
342 // without checking first whether it is destroyed, they won't crash.
343 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
344 m_plan_stack.push_back (null_plan_sp);
345
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000346 m_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000347 m_reg_context_sp.reset();
348 m_unwinder_ap.reset();
349 Mutex::Locker locker(m_frame_mutex);
350 m_curr_frames_sp.reset();
351 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352}
353
Jim Ingham4f465cf2012-10-10 18:32:14 +0000354void
355Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
356{
357 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
358 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
359}
360
361uint32_t
Jason Molendab57e4a12013-11-04 09:33:30 +0000362Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
Jim Ingham4f465cf2012-10-10 18:32:14 +0000363{
364 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
365 if (broadcast)
366 BroadcastSelectedFrameChange(frame->GetStackID());
367 return ret_value;
368}
369
370bool
371Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
372{
Jason Molendab57e4a12013-11-04 09:33:30 +0000373 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
Jim Ingham4f465cf2012-10-10 18:32:14 +0000374 if (frame_sp)
375 {
376 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
377 if (broadcast)
378 BroadcastSelectedFrameChange(frame_sp->GetStackID());
379 return true;
380 }
381 else
382 return false;
383}
384
Jim Ingham93208b82013-01-31 21:46:01 +0000385bool
386Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
387{
388 const bool broadcast = true;
389 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
390 if (success)
391 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000392 StackFrameSP frame_sp = GetSelectedFrame();
Jim Ingham93208b82013-01-31 21:46:01 +0000393 if (frame_sp)
394 {
395 bool already_shown = false;
396 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
397 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
398 {
399 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
400 }
401
402 bool show_frame_info = true;
403 bool show_source = !already_shown;
404 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
405 }
406 return false;
407 }
408 else
409 return false;
410}
411
Jim Ingham4f465cf2012-10-10 18:32:14 +0000412
Jim Inghamb15bfc72010-10-20 00:39:53 +0000413lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000414Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415{
Greg Clayton6e10f142013-07-30 00:23:06 +0000416 if (m_destroy_called)
417 return m_stop_info_sp;
418
Jim Inghamb15bfc72010-10-20 00:39:53 +0000419 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000420 ProcessSP process_sp (GetProcess());
421 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000422 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000423 {
Jim Ingham30fadaf2014-07-08 01:07:32 +0000424 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject(), GetExpressionVariable());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000425 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000426 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000427 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000428 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
429 (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 +0000430 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000431 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000432 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000433 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000434 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000435 GetPrivateStopInfo ();
436 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000437 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000438 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439}
440
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000441lldb::StopInfoSP
442Thread::GetPrivateStopInfo ()
443{
Greg Clayton6e10f142013-07-30 00:23:06 +0000444 if (m_destroy_called)
445 return m_stop_info_sp;
446
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000447 ProcessSP process_sp (GetProcess());
448 if (process_sp)
449 {
Daniel Malea246cb612013-05-14 15:20:12 +0000450 const uint32_t process_stop_id = process_sp->GetStopID();
451 if (m_stop_info_stop_id != process_stop_id)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000452 {
Daniel Malea246cb612013-05-14 15:20:12 +0000453 if (m_stop_info_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000454 {
Daniel Malea246cb612013-05-14 15:20:12 +0000455 if (m_stop_info_sp->IsValid()
456 || IsStillAtLastBreakpointHit()
457 || GetCurrentPlan()->IsVirtualStep())
458 SetStopInfo (m_stop_info_sp);
459 else
460 m_stop_info_sp.reset();
461 }
462
463 if (!m_stop_info_sp)
464 {
465 if (CalculateStopInfo() == false)
466 SetStopInfo (StopInfoSP());
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000467 }
468 }
469 }
470 return m_stop_info_sp;
471}
472
473
Greg Clayton97d5cf02012-09-25 02:40:06 +0000474lldb::StopReason
475Thread::GetStopReason()
476{
477 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
478 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000479 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000480 return eStopReasonNone;
481}
482
483
484
Jim Ingham77787032011-01-20 02:03:18 +0000485void
486Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
487{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000488 m_stop_info_sp = stop_info_sp;
489 if (m_stop_info_sp)
Jim Ingham221d51c2013-05-08 00:35:16 +0000490 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000491 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham221d51c2013-05-08 00:35:16 +0000492 // If we are overriding the ShouldReportStop, do that here:
493 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000494 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000495 }
496
Greg Clayton1ac04c32012-02-21 00:09:25 +0000497 ProcessSP process_sp (GetProcess());
498 if (process_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000499 m_stop_info_stop_id = process_sp->GetStopID();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000500 else
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000501 m_stop_info_stop_id = UINT32_MAX;
Greg Clayton8cda7f02013-05-21 21:55:59 +0000502 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
503 if (log)
Ed Maste2bc76432014-06-25 00:38:35 +0000504 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000505 static_cast<void*>(this), GetID(),
506 stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>",
507 m_stop_info_stop_id);
Jim Ingham77787032011-01-20 02:03:18 +0000508}
509
510void
Jim Ingham221d51c2013-05-08 00:35:16 +0000511Thread::SetShouldReportStop (Vote vote)
512{
513 if (vote == eVoteNoOpinion)
514 return;
515 else
516 {
517 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000518 if (m_stop_info_sp)
519 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000520 }
521}
522
523void
Jim Ingham77787032011-01-20 02:03:18 +0000524Thread::SetStopInfoToNothing()
525{
526 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
527 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
528 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
529}
530
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531bool
532Thread::ThreadStoppedForAReason (void)
533{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000534 return (bool) GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535}
536
Jim Ingham77787032011-01-20 02:03:18 +0000537bool
538Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
539{
Greg Claytonf74cf862013-11-13 23:28:31 +0000540 saved_state.register_backup_sp.reset();
541 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
542 if (frame_sp)
543 {
544 lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
545 if (reg_checkpoint_sp)
546 {
547 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
548 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp))
549 saved_state.register_backup_sp = reg_checkpoint_sp;
550 }
551 }
552 if (!saved_state.register_backup_sp)
Jim Ingham77787032011-01-20 02:03:18 +0000553 return false;
554
555 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000556 ProcessSP process_sp (GetProcess());
557 if (process_sp)
558 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000559 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
560
Jim Ingham77787032011-01-20 02:03:18 +0000561 return true;
562}
563
564bool
Jim Ingham8559a352012-11-26 23:52:18 +0000565Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000566{
Greg Claytonf74cf862013-11-13 23:28:31 +0000567 if (saved_state.register_backup_sp)
568 {
569 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
570 if (frame_sp)
571 {
572 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
573 if (reg_ctx_sp)
574 {
575 bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp);
576
577 // Clear out all stack frames as our world just changed.
578 ClearStackFrames();
579 reg_ctx_sp->InvalidateIfNeeded(true);
580 if (m_unwinder_ap.get())
581 m_unwinder_ap->Clear();
582 return ret;
583 }
584 }
585 }
586 return false;
Jim Ingham8559a352012-11-26 23:52:18 +0000587}
588
589bool
590Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
591{
Jim Ingham0c270682011-01-25 02:47:23 +0000592 if (saved_state.stop_info_sp)
593 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000594 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000595 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000596 return true;
597}
598
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599StateType
600Thread::GetState() const
601{
602 // If any other threads access this we will need a mutex for it
603 Mutex::Locker locker(m_state_mutex);
604 return m_state;
605}
606
607void
608Thread::SetState(StateType state)
609{
610 Mutex::Locker locker(m_state_mutex);
611 m_state = state;
612}
613
614void
615Thread::WillStop()
616{
617 ThreadPlan *current_plan = GetCurrentPlan();
618
619 // FIXME: I may decide to disallow threads with no plans. In which
620 // case this should go to an assert.
621
622 if (!current_plan)
623 return;
624
625 current_plan->WillStop();
626}
627
628void
629Thread::SetupForResume ()
630{
631 if (GetResumeState() != eStateSuspended)
632 {
633
634 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
635 // telling the current plan it will resume, since we might change what the current
636 // plan is.
637
Greg Clayton1afa68e2013-04-02 20:32:37 +0000638// StopReason stop_reason = lldb::eStopReasonInvalid;
639// StopInfoSP stop_info_sp = GetStopInfo();
640// if (stop_info_sp.get())
641// stop_reason = stop_info_sp->GetStopReason();
642// if (stop_reason == lldb::eStopReasonBreakpoint)
643 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
644 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000645 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000646 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(reg_ctx_sp->GetPC());
647 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000648 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000649 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
650 // special to step over a breakpoint.
651
652 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000653
Greg Clayton1afa68e2013-04-02 20:32:37 +0000654 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
655 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000656 ThreadPlanSP step_bp_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
657 if (step_bp_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000659 ;
660 step_bp_plan_sp->SetPrivate (true);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000661
662 if (GetCurrentPlan()->RunState() != eStateStepping)
663 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000664 ThreadPlanStepOverBreakpoint *step_bp_plan
665 = static_cast<ThreadPlanStepOverBreakpoint *>(step_bp_plan_sp.get());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000666 step_bp_plan->SetAutoContinue(true);
667 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000668 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670 }
671 }
672 }
673 }
674}
675
676bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000677Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000678{
679 // At this point clear the completed plan stack.
680 m_completed_plan_stack.clear();
681 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000682 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683
Greg Clayton97d5cf02012-09-25 02:40:06 +0000684 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000685
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000686 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
687 if (backing_thread_sp)
688 backing_thread_sp->m_temporary_resume_state = resume_state;
689
690 // Make sure m_stop_info_sp is valid
691 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000692
Jim Ingham92087d82012-01-31 23:09:20 +0000693 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
694 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
695 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
696 // about the fact that we are resuming...
Greg Clayton1ac04c32012-02-21 00:09:25 +0000697 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000698 if (m_stop_info_stop_id == process_stop_id &&
699 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000700 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000701 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000702 if (stop_info)
703 stop_info->WillResume (resume_state);
704 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705
706 // Tell all the plans that we are about to resume in case they need to clear any state.
707 // We distinguish between the plan on the top of the stack and the lower
708 // plans in case a plan needs to do any special business before it runs.
709
Greg Claytond1d06e42013-04-20 00:27:58 +0000710 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000712 if (plan_ptr)
713 {
714 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715
Greg Claytond1d06e42013-04-20 00:27:58 +0000716 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
717 {
718 plan_ptr->WillResume (resume_state, false);
719 }
720
721 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
722 // In that case, don't reset it here.
723
724 if (need_to_resume && resume_state != eStateSuspended)
725 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000726 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000727 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000728 }
729
Greg Clayton160c9d82013-05-01 21:54:04 +0000730 if (need_to_resume)
731 {
732 ClearStackFrames();
733 // Let Thread subclasses do any special work they need to prior to resuming
734 WillResume (resume_state);
735 }
736
Jim Ingham513c6bb2012-09-01 01:02:41 +0000737 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738}
739
740void
741Thread::DidResume ()
742{
743 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
744}
745
Andrew Kaylor29d65742013-05-10 17:19:04 +0000746void
747Thread::DidStop ()
748{
749 SetState (eStateStopped);
750}
751
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752bool
753Thread::ShouldStop (Event* event_ptr)
754{
755 ThreadPlan *current_plan = GetCurrentPlan();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000756
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757 bool should_stop = true;
758
Greg Clayton5160ce52013-03-27 23:08:40 +0000759 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000760
Jim Ingham92087d82012-01-31 23:09:20 +0000761 if (GetResumeState () == eStateSuspended)
762 {
763 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000764 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 +0000765 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000766 return false;
767 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000768
Jim Ingham92087d82012-01-31 23:09:20 +0000769 if (GetTemporaryResumeState () == eStateSuspended)
770 {
771 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000772 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 +0000773 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000774 return false;
775 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000776
Daniel Malea246cb612013-05-14 15:20:12 +0000777 // Based on the current thread plan and process stop info, check if this
778 // thread caused the process to stop. NOTE: this must take place before
779 // the plan is moved from the current plan stack to the completed plan
780 // stack.
Jim Ingham92087d82012-01-31 23:09:20 +0000781 if (ThreadStoppedForAReason() == false)
782 {
783 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000784 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 +0000785 __FUNCTION__, GetID (), GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000786 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000787 return false;
788 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000789
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790 if (log)
791 {
Jim Inghamdee1bc92013-06-22 00:27:45 +0000792 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 +0000793 __FUNCTION__, static_cast<void*>(this), GetID (),
Greg Clayton160c9d82013-05-01 21:54:04 +0000794 GetProtocolID (),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000795 GetRegisterContext()
796 ? GetRegisterContext()->GetPC()
797 : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000798 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000800 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000802 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000804
Jim Ingham06e827c2010-11-11 19:26:09 +0000805 // The top most plan always gets to do the trace log...
806 current_plan->DoTraceLog ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000807
Jim Ingham6d66ce62012-04-20 21:16:56 +0000808 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
809 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
810 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000811 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Ingham6d66ce62012-04-20 21:16:56 +0000812 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
813 {
814 if (log)
815 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
816 return false;
817 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818
Jim Inghambad39e42012-09-05 21:12:49 +0000819 // If we've already been restarted, don't query the plans since the state they would examine is not current.
820 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
821 return false;
822
823 // Before the plans see the state of the world, calculate the current inlined depth.
824 GetStackFrameList()->CalculateCurrentInlinedDepth();
825
Jim Ingham25f66702011-12-03 01:52:59 +0000826 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
827 // If that plan is still working, then we don't need to do any more work. If the plan that explains
828 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
829 // whether they still need to do more work.
830
831 bool done_processing_current_plan = false;
832
Jim Ingham0161b492013-02-09 01:29:05 +0000833 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000834 {
835 if (current_plan->TracerExplainsStop())
836 {
837 done_processing_current_plan = true;
838 should_stop = false;
839 }
840 else
841 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000842 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000843 // does and let it handle the situation.
844 ThreadPlan *plan_ptr = current_plan;
845 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
846 {
Jim Ingham0161b492013-02-09 01:29:05 +0000847 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000848 {
849 should_stop = plan_ptr->ShouldStop (event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000850
Jim Ingham25f66702011-12-03 01:52:59 +0000851 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
852 // and all the plans below it off the stack.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000853
Jim Ingham25f66702011-12-03 01:52:59 +0000854 if (plan_ptr->MischiefManaged())
855 {
Jim Ingham031177e2012-05-11 23:49:49 +0000856 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000857 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000858
Jim Ingham25f66702011-12-03 01:52:59 +0000859 do
860 {
861 if (should_stop)
862 current_plan->WillStop();
863 PopPlan();
864 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000865 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
866 // Now, if the responsible plan was not "Okay to discard" then we're done,
867 // otherwise we forward this to the next plan in the stack below.
868 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
869 done_processing_current_plan = true;
870 else
871 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000872 }
873 else
874 done_processing_current_plan = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000875
Jim Ingham25f66702011-12-03 01:52:59 +0000876 break;
877 }
878
879 }
880 }
881 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000882
Jim Ingham25f66702011-12-03 01:52:59 +0000883 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000885 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000886
Jim Ingham10c4b242011-10-15 00:23:43 +0000887 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000888 log->Printf("Plan %s explains stop, auto-continue %i.",
889 current_plan->GetName(), over_ride_stop);
890
Jim Ingham0f16e732011-02-08 05:20:59 +0000891 // We're starting from the base plan, so just let it decide;
892 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000893 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000894 should_stop = current_plan->ShouldStop (event_ptr);
895 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000896 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000897 }
898 else
899 {
900 // Otherwise, don't let the base plan override what the other plans say to do, since
901 // presumably if there were other plans they would know what to do...
902 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000903 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000904 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000905 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000906
Jim Ingham0f16e732011-02-08 05:20:59 +0000907 should_stop = current_plan->ShouldStop(event_ptr);
908 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000909 log->Printf("Plan %s should stop: %d.",
910 current_plan->GetName(), should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000911 if (current_plan->MischiefManaged())
912 {
913 if (should_stop)
914 current_plan->WillStop();
915
916 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
917 // Otherwise, see if the plan's parent wants to stop.
918
919 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
920 {
921 PopPlan();
922 break;
923 }
924 else
925 {
926
927 PopPlan();
928
929 current_plan = GetCurrentPlan();
930 if (current_plan == NULL)
931 {
932 break;
933 }
934 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000935 }
936 else
937 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000938 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940 }
941 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000942
Jim Inghamb01e7422010-06-19 04:45:32 +0000943 if (over_ride_stop)
944 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000945
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000946 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000947
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000948 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
949 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
950 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
951 // This code clears stale plans off the stack.
952
953 if (should_stop)
954 {
955 ThreadPlan *plan_ptr = GetCurrentPlan();
956 while (!PlanIsBasePlan(plan_ptr))
Jim Ingham64e7ead2012-05-03 21:19:36 +0000957 {
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000958 bool stale = plan_ptr->IsPlanStale ();
959 ThreadPlan *examined_plan = plan_ptr;
960 plan_ptr = GetPreviousPlan (examined_plan);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000961
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000962 if (stale)
963 {
964 if (log)
965 log->Printf("Plan %s being discarded in cleanup, it says it is already done.",
966 examined_plan->GetName());
967 DiscardThreadPlansUpToPlan(examined_plan);
Jim Ingham64e7ead2012-05-03 21:19:36 +0000968 }
969 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000970 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971
Jim Ingham10c4b242011-10-15 00:23:43 +0000972 if (log)
973 {
974 StreamString s;
975 s.IndentMore();
976 DumpThreadPlans(&s);
977 log->Printf ("Plan stack final state:\n%s", s.GetData());
978 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
979 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980 return should_stop;
981}
982
983Vote
984Thread::ShouldReportStop (Event* event_ptr)
985{
986 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +0000987 StateType temp_thread_state = GetTemporaryResumeState();
988
Greg Clayton5160ce52013-03-27 23:08:40 +0000989 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000990
991 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
992 {
993 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000994 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 +0000995 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000996 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997
Jim Ingham92087d82012-01-31 23:09:20 +0000998 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
999 {
1000 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001001 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 +00001002 return eVoteNoOpinion;
1003 }
1004
1005 if (!ThreadStoppedForAReason())
1006 {
1007 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001008 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 +00001009 return eVoteNoOpinion;
1010 }
1011
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012 if (m_completed_plan_stack.size() > 0)
1013 {
1014 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +00001015 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001016 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 +00001017 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
1018 }
1019 else
Greg Clayton2cad65a2010-09-03 17:10:42 +00001020 {
Jim Ingham0161b492013-02-09 01:29:05 +00001021 Vote thread_vote = eVoteNoOpinion;
1022 ThreadPlan *plan_ptr = GetCurrentPlan();
1023 while (1)
1024 {
1025 if (plan_ptr->PlanExplainsStop(event_ptr))
1026 {
1027 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1028 break;
1029 }
1030 if (PlanIsBasePlan(plan_ptr))
1031 break;
1032 else
1033 plan_ptr = GetPreviousPlan(plan_ptr);
1034 }
Greg Clayton2cad65a2010-09-03 17:10:42 +00001035 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001036 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 +00001037
1038 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001039 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040}
1041
1042Vote
1043Thread::ShouldReportRun (Event* event_ptr)
1044{
1045 StateType thread_state = GetResumeState ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001046
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001047 if (thread_state == eStateSuspended
1048 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +00001049 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +00001051 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001052
Greg Clayton5160ce52013-03-27 23:08:40 +00001053 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054 if (m_completed_plan_stack.size() > 0)
1055 {
1056 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +00001057 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001058 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 +00001059 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001060 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001061 m_completed_plan_stack.back()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001062
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
1064 }
1065 else
Jim Ingham444586b2011-01-24 06:34:17 +00001066 {
1067 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001068 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 +00001069 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001070 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001071 GetCurrentPlan()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001072
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +00001074 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001075}
1076
Jim Ingham1b54c882010-06-16 02:00:15 +00001077bool
1078Thread::MatchesSpec (const ThreadSpec *spec)
1079{
1080 if (spec == NULL)
1081 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +00001082
Jim Ingham3d902922012-03-07 22:03:04 +00001083 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +00001084}
1085
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086void
1087Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1088{
1089 if (thread_plan_sp)
1090 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001091 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1092 if (!thread_plan_sp->GetThreadPlanTracer())
1093 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001094 m_plan_stack.push_back (thread_plan_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001095
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096 thread_plan_sp->DidPush();
1097
Greg Clayton5160ce52013-03-27 23:08:40 +00001098 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099 if (log)
1100 {
1101 StreamString s;
1102 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamdee1bc92013-06-22 00:27:45 +00001103 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001104 static_cast<void*>(this), s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001105 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106 }
1107 }
1108}
1109
1110void
1111Thread::PopPlan ()
1112{
Greg Clayton5160ce52013-03-27 23:08:40 +00001113 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001115 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001116 return;
1117 else
1118 {
1119 ThreadPlanSP &plan = m_plan_stack.back();
1120 if (log)
1121 {
Daniel Malead01b2952012-11-29 21:49:15 +00001122 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123 }
1124 m_completed_plan_stack.push_back (plan);
1125 plan->WillPop();
1126 m_plan_stack.pop_back();
1127 }
1128}
1129
1130void
1131Thread::DiscardPlan ()
1132{
Jim Inghamdee1bc92013-06-22 00:27:45 +00001133 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134 if (m_plan_stack.size() > 1)
1135 {
1136 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamdee1bc92013-06-22 00:27:45 +00001137 if (log)
1138 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1139
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001140 m_discarded_plan_stack.push_back (plan);
1141 plan->WillPop();
1142 m_plan_stack.pop_back();
1143 }
1144}
1145
1146ThreadPlan *
1147Thread::GetCurrentPlan ()
1148{
Jim Inghame1471232012-04-19 00:17:05 +00001149 // There will always be at least the base plan. If somebody is mucking with a
1150 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +00001151 if (m_plan_stack.empty())
1152 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +00001153 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001154}
1155
1156ThreadPlanSP
1157Thread::GetCompletedPlan ()
1158{
1159 ThreadPlanSP empty_plan_sp;
1160 if (!m_completed_plan_stack.empty())
1161 {
1162 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1163 {
1164 ThreadPlanSP completed_plan_sp;
1165 completed_plan_sp = m_completed_plan_stack[i];
1166 if (!completed_plan_sp->GetPrivate ())
1167 return completed_plan_sp;
1168 }
1169 }
1170 return empty_plan_sp;
1171}
1172
Jim Ingham73ca05a2011-12-17 01:35:57 +00001173ValueObjectSP
1174Thread::GetReturnValueObject ()
1175{
1176 if (!m_completed_plan_stack.empty())
1177 {
1178 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1179 {
1180 ValueObjectSP return_valobj_sp;
1181 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1182 if (return_valobj_sp)
1183 return return_valobj_sp;
1184 }
1185 }
1186 return ValueObjectSP();
1187}
1188
Jim Ingham30fadaf2014-07-08 01:07:32 +00001189ClangExpressionVariableSP
1190Thread::GetExpressionVariable ()
1191{
1192 if (!m_completed_plan_stack.empty())
1193 {
1194 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1195 {
1196 ClangExpressionVariableSP expression_variable_sp;
1197 expression_variable_sp = m_completed_plan_stack[i]->GetExpressionVariable();
1198 if (expression_variable_sp)
1199 return expression_variable_sp;
1200 }
1201 }
1202 return ClangExpressionVariableSP();
1203}
1204
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001205bool
1206Thread::IsThreadPlanDone (ThreadPlan *plan)
1207{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208 if (!m_completed_plan_stack.empty())
1209 {
1210 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1211 {
1212 if (m_completed_plan_stack[i].get() == plan)
1213 return true;
1214 }
1215 }
1216 return false;
1217}
1218
1219bool
1220Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1221{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001222 if (!m_discarded_plan_stack.empty())
1223 {
1224 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1225 {
1226 if (m_discarded_plan_stack[i].get() == plan)
1227 return true;
1228 }
1229 }
1230 return false;
1231}
1232
1233ThreadPlan *
1234Thread::GetPreviousPlan (ThreadPlan *current_plan)
1235{
1236 if (current_plan == NULL)
1237 return NULL;
1238
1239 int stack_size = m_completed_plan_stack.size();
1240 for (int i = stack_size - 1; i > 0; i--)
1241 {
1242 if (current_plan == m_completed_plan_stack[i].get())
1243 return m_completed_plan_stack[i-1].get();
1244 }
1245
1246 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1247 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001248 if (m_plan_stack.size() > 0)
1249 return m_plan_stack.back().get();
1250 else
1251 return NULL;
1252 }
1253
1254 stack_size = m_plan_stack.size();
1255 for (int i = stack_size - 1; i > 0; i--)
1256 {
1257 if (current_plan == m_plan_stack[i].get())
1258 return m_plan_stack[i-1].get();
1259 }
1260 return NULL;
1261}
1262
1263void
1264Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1265{
1266 if (abort_other_plans)
1267 DiscardThreadPlans(true);
1268
1269 PushPlan (thread_plan_sp);
1270}
1271
Jim Ingham06e827c2010-11-11 19:26:09 +00001272
1273void
1274Thread::EnableTracer (bool value, bool single_stepping)
1275{
1276 int stack_size = m_plan_stack.size();
1277 for (int i = 0; i < stack_size; i++)
1278 {
1279 if (m_plan_stack[i]->GetThreadPlanTracer())
1280 {
1281 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1282 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1283 }
1284 }
1285}
1286
1287void
1288Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1289{
1290 int stack_size = m_plan_stack.size();
1291 for (int i = 0; i < stack_size; i++)
1292 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1293}
1294
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001295bool
1296Thread::DiscardUserThreadPlansUpToIndex (uint32_t thread_index)
1297{
1298 // Count the user thread plans from the back end to get the number of the one we want
1299 // to discard:
1300
1301 uint32_t idx = 0;
1302 ThreadPlan *up_to_plan_ptr = nullptr;
1303
1304 for (ThreadPlanSP plan_sp : m_plan_stack)
1305 {
1306 if (plan_sp->GetPrivate())
1307 continue;
1308 if (idx == thread_index)
1309 {
1310 up_to_plan_ptr = plan_sp.get();
1311 break;
1312 }
1313 else
1314 idx++;
1315 }
1316
1317 if (up_to_plan_ptr == nullptr)
1318 return false;
1319
1320 DiscardThreadPlansUpToPlan(up_to_plan_ptr);
1321 return true;
1322}
1323
1324
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001325void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001326Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1327{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001328 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1329}
1330
1331void
1332Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1333{
Greg Clayton5160ce52013-03-27 23:08:40 +00001334 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001335 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001336 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p",
1337 GetID(), static_cast<void*>(up_to_plan_ptr));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001338
1339 int stack_size = m_plan_stack.size();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001340
Jim Ingham399f1ca2010-11-05 19:25:48 +00001341 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1342 // stack, and if so discard up to and including it.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001343
Jim Ingham64e7ead2012-05-03 21:19:36 +00001344 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001345 {
1346 for (int i = stack_size - 1; i > 0; i--)
1347 DiscardPlan();
1348 }
1349 else
1350 {
1351 bool found_it = false;
1352 for (int i = stack_size - 1; i > 0; i--)
1353 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001354 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001355 found_it = true;
1356 }
1357 if (found_it)
1358 {
1359 bool last_one = false;
1360 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1361 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001362 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001363 last_one = true;
1364 DiscardPlan();
1365 }
1366 }
1367 }
1368 return;
1369}
1370
1371void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001372Thread::DiscardThreadPlans(bool force)
1373{
Greg Clayton5160ce52013-03-27 23:08:40 +00001374 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001375 if (log)
1376 {
Daniel Malead01b2952012-11-29 21:49:15 +00001377 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378 }
1379
1380 if (force)
1381 {
1382 int stack_size = m_plan_stack.size();
1383 for (int i = stack_size - 1; i > 0; i--)
1384 {
1385 DiscardPlan();
1386 }
1387 return;
1388 }
1389
1390 while (1)
1391 {
1392
1393 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001394 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395
1396 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1397 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1398 {
1399 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1400 {
1401 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1402 break;
1403 }
1404 }
1405
1406 if (discard)
1407 {
1408 // First pop all the dependent plans:
1409 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1410 {
1411
1412 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1413 // for the plan leaves it in a state that it is safe to pop the plan
1414 // with no more notice?
1415 DiscardPlan();
1416 }
1417
1418 // Now discard the master plan itself.
1419 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1420 // discard it's dependent plans, but not it...
1421 if (master_plan_idx > 0)
1422 {
1423 DiscardPlan();
1424 }
1425 }
1426 else
1427 {
1428 // If the master plan doesn't want to get discarded, then we're done.
1429 break;
1430 }
1431
1432 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433}
1434
Jim Inghamcf274f92012-04-09 22:37:39 +00001435bool
1436Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1437{
1438 if (plan_ptr->IsBasePlan())
1439 return true;
1440 else if (m_plan_stack.size() == 0)
1441 return false;
1442 else
1443 return m_plan_stack[0].get() == plan_ptr;
1444}
1445
Jim Ingham93208b82013-01-31 21:46:01 +00001446Error
1447Thread::UnwindInnermostExpression()
1448{
1449 Error error;
1450 int stack_size = m_plan_stack.size();
1451
1452 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1453 // stack, and if so discard up to and including it.
1454
1455 for (int i = stack_size - 1; i > 0; i--)
1456 {
1457 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1458 {
1459 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1460 return error;
1461 }
1462 }
1463 error.SetErrorString("No expressions currently active on this thread");
1464 return error;
1465}
1466
1467
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001468ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001469Thread::QueueFundamentalPlan (bool abort_other_plans)
1470{
1471 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1472 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001473 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001474}
1475
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001476ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001477Thread::QueueThreadPlanForStepSingleInstruction
1478(
1479 bool step_over,
1480 bool abort_other_plans,
1481 bool stop_other_threads
1482)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001483{
1484 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1485 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001486 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001487}
1488
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001489ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001490Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001491(
1492 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001493 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001494 const SymbolContext &addr_context,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001495 lldb::RunMode stop_other_threads,
1496 LazyBool step_out_avoids_code_withoug_debug_info
Jim Inghamc6276822012-12-12 19:58:40 +00001497)
1498{
1499 ThreadPlanSP thread_plan_sp;
Jim Ingham4b4b2472014-03-13 02:47:14 +00001500 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads, step_out_avoids_code_withoug_debug_info));
1501
Jim Inghamc6276822012-12-12 19:58:40 +00001502 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001503 return thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001504}
1505
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001506ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001507Thread::QueueThreadPlanForStepInRange
1508(
1509 bool abort_other_plans,
1510 const AddressRange &range,
1511 const SymbolContext &addr_context,
1512 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001513 lldb::RunMode stop_other_threads,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001514 LazyBool step_in_avoids_code_without_debug_info,
1515 LazyBool step_out_avoids_code_without_debug_info
Greg Clayton474966a2010-06-12 18:59:55 +00001516)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001517{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001518 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInRange (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001519 range,
1520 addr_context,
1521 stop_other_threads,
1522 step_in_avoids_code_without_debug_info,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001523 step_out_avoids_code_without_debug_info));
1524 ThreadPlanStepInRange *plan = static_cast<ThreadPlanStepInRange *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001525
Jim Inghamc6276822012-12-12 19:58:40 +00001526 if (step_in_target)
1527 plan->SetStepInTarget(step_in_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001528
1529 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001530 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001531}
1532
1533
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001534ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001535Thread::QueueThreadPlanForStepOut
1536(
1537 bool abort_other_plans,
1538 SymbolContext *addr_context,
1539 bool first_insn,
1540 bool stop_other_threads,
1541 Vote stop_vote,
1542 Vote run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001543 uint32_t frame_idx,
1544 LazyBool step_out_avoids_code_withoug_debug_info
Greg Clayton481cef22011-01-21 06:11:58 +00001545)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001546{
Greg Clayton481cef22011-01-21 06:11:58 +00001547 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1548 addr_context,
1549 first_insn,
1550 stop_other_threads,
1551 stop_vote,
1552 run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001553 frame_idx,
1554 step_out_avoids_code_withoug_debug_info));
1555
1556 if (thread_plan_sp->ValidatePlan(NULL))
1557 {
1558 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1559 return thread_plan_sp;
1560 }
1561 else
1562 {
1563 return ThreadPlanSP();
1564 }
1565}
1566
1567ThreadPlanSP
1568Thread::QueueThreadPlanForStepOutNoShouldStop
1569(
1570 bool abort_other_plans,
1571 SymbolContext *addr_context,
1572 bool first_insn,
1573 bool stop_other_threads,
1574 Vote stop_vote,
1575 Vote run_vote,
1576 uint32_t frame_idx
1577)
1578{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001579 ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001580 addr_context,
1581 first_insn,
1582 stop_other_threads,
1583 stop_vote,
1584 run_vote,
1585 frame_idx,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001586 eLazyBoolNo));
1587
1588 ThreadPlanStepOut *new_plan = static_cast<ThreadPlanStepOut *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001589 new_plan->ClearShouldStopHereCallbacks();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001590
Sean Callanan708709c2012-07-31 22:19:25 +00001591 if (thread_plan_sp->ValidatePlan(NULL))
1592 {
1593 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001594 return thread_plan_sp;
Sean Callanan708709c2012-07-31 22:19:25 +00001595 }
1596 else
1597 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001598 return ThreadPlanSP();
Sean Callanan708709c2012-07-31 22:19:25 +00001599 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001600}
1601
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001602ThreadPlanSP
Jim Ingham18de2fd2012-05-10 01:35:39 +00001603Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001604{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001605 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001606 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001607 return ThreadPlanSP();
Jim Ingham25f66702011-12-03 01:52:59 +00001608
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001609 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001610 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001611}
1612
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001613ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001614Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1615 Address &target_addr,
1616 bool stop_other_threads)
1617{
1618 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1619 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001620 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001621}
1622
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001623ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001624Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001625 lldb::addr_t *address_list,
1626 size_t num_addresses,
1627 bool stop_other_threads,
1628 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001629{
Greg Clayton481cef22011-01-21 06:11:58 +00001630 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001631 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001632 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001633
1634}
1635
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001636lldb::ThreadPlanSP
1637Thread::QueueThreadPlanForStepScripted (bool abort_other_plans,
1638 const char *class_name,
1639 bool stop_other_threads)
1640{
1641 ThreadPlanSP thread_plan_sp (new ThreadPlanPython (*this, class_name));
1642 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1643 // This seems a little funny, but I don't want to have to split up the constructor and the
1644 // DidPush in the scripted plan, that seems annoying.
1645 // That means the constructor has to be in DidPush.
1646 // So I have to validate the plan AFTER pushing it, and then take it off again...
1647 if (!thread_plan_sp->ValidatePlan(nullptr))
1648 {
1649 DiscardThreadPlansUpToPlan(thread_plan_sp);
1650 return ThreadPlanSP();
1651 }
1652 else
1653 return thread_plan_sp;
1654
1655}
1656
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001657uint32_t
1658Thread::GetIndexID () const
1659{
1660 return m_index_id;
1661}
1662
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001663static void
1664PrintPlanElement (Stream *s, const ThreadPlanSP &plan, lldb::DescriptionLevel desc_level, int32_t elem_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001665{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001666 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001667 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001668 s->Printf ("Element %d: ", elem_idx);
1669 plan->GetDescription (s, desc_level);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001670 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001671 s->IndentLess();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001672}
1673
1674static void
1675PrintPlanStack (Stream *s, const std::vector<lldb::ThreadPlanSP> &plan_stack, lldb::DescriptionLevel desc_level, bool include_internal)
1676{
1677 int32_t print_idx = 0;
1678 for (ThreadPlanSP plan_sp : plan_stack)
1679 {
1680 if (include_internal || !plan_sp->GetPrivate())
1681 {
1682 PrintPlanElement (s, plan_sp, desc_level, print_idx++);
1683 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001684 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001685}
1686
1687void
1688Thread::DumpThreadPlans (Stream *s,
1689 lldb::DescriptionLevel desc_level,
1690 bool include_internal,
1691 bool ignore_boring_threads) const
1692{
1693 uint32_t stack_size = m_plan_stack.size();
1694
1695 if (ignore_boring_threads)
1696 {
1697 uint32_t stack_size = m_plan_stack.size();
1698 uint32_t completed_stack_size = m_completed_plan_stack.size();
1699 uint32_t discarded_stack_size = m_discarded_plan_stack.size();
1700 if (stack_size == 1 && completed_stack_size == 0 && discarded_stack_size == 0)
1701 {
1702 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 "\n", GetIndexID(), GetID());
1703 s->IndentMore();
1704 s->Indent();
1705 s->Printf("No active thread plans\n");
1706 s->IndentLess();
1707 return;
1708 }
1709 }
1710
1711 s->Indent();
1712 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 ":\n", GetIndexID(), GetID());
1713 s->IndentMore();
1714 s->Indent();
1715 s->Printf ("Active plan stack:\n");
1716 PrintPlanStack (s, m_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001717
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001718 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001719 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001720 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001721 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001722 s->Printf ("Completed Plan Stack:\n");
1723 PrintPlanStack (s, m_completed_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001724 }
1725
1726 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001727 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001728 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001729 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001730 s->Printf ("Discarded Plan Stack:\n");
1731 PrintPlanStack (s, m_discarded_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001732 }
1733
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001734 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001735}
1736
Greg Claytond9e416c2012-02-18 05:35:26 +00001737TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001738Thread::CalculateTarget ()
1739{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001740 TargetSP target_sp;
1741 ProcessSP process_sp(GetProcess());
1742 if (process_sp)
1743 target_sp = process_sp->CalculateTarget();
1744 return target_sp;
1745
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001746}
1747
Greg Claytond9e416c2012-02-18 05:35:26 +00001748ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001749Thread::CalculateProcess ()
1750{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001751 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752}
1753
Greg Claytond9e416c2012-02-18 05:35:26 +00001754ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001755Thread::CalculateThread ()
1756{
Greg Claytond9e416c2012-02-18 05:35:26 +00001757 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001758}
1759
Jason Molendab57e4a12013-11-04 09:33:30 +00001760StackFrameSP
1761Thread::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001762{
Jason Molendab57e4a12013-11-04 09:33:30 +00001763 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001764}
1765
1766void
Greg Clayton0603aa92010-10-04 01:05:56 +00001767Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001768{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001769 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001770}
1771
Greg Clayton12daf9462010-08-25 00:35:26 +00001772
Greg Clayton39d0ab32012-03-29 01:41:38 +00001773StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001774Thread::GetStackFrameList ()
1775{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001776 StackFrameListSP frame_list_sp;
1777 Mutex::Locker locker(m_frame_mutex);
1778 if (m_curr_frames_sp)
1779 {
1780 frame_list_sp = m_curr_frames_sp;
1781 }
1782 else
1783 {
1784 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1785 m_curr_frames_sp = frame_list_sp;
1786 }
1787 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001788}
1789
Greg Clayton12daf9462010-08-25 00:35:26 +00001790void
1791Thread::ClearStackFrames ()
1792{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001793 Mutex::Locker locker(m_frame_mutex);
1794
Greg Clayton160c9d82013-05-01 21:54:04 +00001795 Unwind *unwinder = GetUnwinder ();
1796 if (unwinder)
1797 unwinder->Clear();
1798
Jim Inghamb0c72a52012-02-29 03:40:22 +00001799 // Only store away the old "reference" StackFrameList if we got all its frames:
1800 // FIXME: At some point we can try to splice in the frames we have fetched into
1801 // the new frame as we make it, but let's not try that now.
1802 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001803 m_prev_frames_sp.swap (m_curr_frames_sp);
1804 m_curr_frames_sp.reset();
Jason Molenda705b1802014-06-13 02:37:02 +00001805
1806 m_extended_info.reset();
1807 m_extended_info_fetched = false;
Jim Ingham87c11912010-08-12 02:14:28 +00001808}
1809
Jason Molendab57e4a12013-11-04 09:33:30 +00001810lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001811Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1812{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001813 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001814}
1815
Jim Ingham44137582012-09-12 00:40:39 +00001816
1817Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001818Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001819{
Jason Molendab57e4a12013-11-04 09:33:30 +00001820 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001821 Error return_error;
1822
1823 if (!frame_sp)
1824 {
Daniel Malead01b2952012-11-29 21:49:15 +00001825 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001826 }
1827
Jim Ingham4f465cf2012-10-10 18:32:14 +00001828 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001829}
1830
1831Error
Jason Molendab57e4a12013-11-04 09:33:30 +00001832Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001833{
1834 Error return_error;
1835
1836 if (!frame_sp)
1837 {
1838 return_error.SetErrorString("Can't return to a null frame.");
1839 return return_error;
1840 }
1841
1842 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001843 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
Jason Molendab57e4a12013-11-04 09:33:30 +00001844 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001845 if (!older_frame_sp)
1846 {
1847 return_error.SetErrorString("No older frame to return to.");
1848 return return_error;
1849 }
Jim Ingham44137582012-09-12 00:40:39 +00001850
1851 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001852 {
Jim Ingham44137582012-09-12 00:40:39 +00001853 lldb::ABISP abi = thread->GetProcess()->GetABI();
1854 if (!abi)
1855 {
1856 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001857 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001858 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001859 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1860
1861 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1862 // Turn that back on when that works.
1863 if (0 && sc.function != NULL)
1864 {
1865 Type *function_type = sc.function->GetType();
1866 if (function_type)
1867 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001868 ClangASTType return_type = sc.function->GetClangType().GetFunctionReturnType();
Jim Ingham1f51e602012-09-27 01:15:29 +00001869 if (return_type)
1870 {
Jim Ingham1f51e602012-09-27 01:15:29 +00001871 StreamString s;
Greg Clayton57ee3062013-07-11 22:46:58 +00001872 return_type.DumpTypeDescription(&s);
1873 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham1f51e602012-09-27 01:15:29 +00001874 if (cast_value_sp)
1875 {
1876 cast_value_sp->SetFormat(eFormatHex);
1877 return_value_sp = cast_value_sp;
1878 }
1879 }
1880 }
1881 }
1882
Jim Inghamcb640dd2012-09-14 02:14:15 +00001883 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001884 if (!return_error.Success())
1885 return return_error;
1886 }
1887
1888 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001889 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001890 // cook their data
1891
Jason Molendab57e4a12013-11-04 09:33:30 +00001892 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
Jim Ingham93208b82013-01-31 21:46:01 +00001893 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001894 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001895 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1896 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001897 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001898 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1899 if (copy_success)
1900 {
1901 thread->DiscardThreadPlans(true);
1902 thread->ClearStackFrames();
1903 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1904 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1905 }
1906 else
1907 {
1908 return_error.SetErrorString("Could not reset register values.");
1909 }
Jim Ingham93208b82013-01-31 21:46:01 +00001910 }
1911 else
1912 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001913 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001914 }
Jim Ingham44137582012-09-12 00:40:39 +00001915 }
1916 else
1917 {
Jim Ingham93208b82013-01-31 21:46:01 +00001918 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001919 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001920 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001921}
1922
Richard Mittonf86248d2013-09-12 02:20:34 +00001923static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1924{
1925 for (size_t n=0;n<list.size();n++)
1926 {
1927 s << "\t";
1928 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1929 s << "\n";
1930 }
1931}
1932
1933Error
1934Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1935{
1936 ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
1937 Target *target = exe_ctx.GetTargetPtr();
1938 TargetSP target_sp = exe_ctx.GetTargetSP();
1939 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00001940 StackFrame *frame = exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00001941 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1942
1943 // Find candidate locations.
1944 std::vector<Address> candidates, within_function, outside_function;
1945 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
1946
1947 // If possible, we try and stay within the current function.
1948 // Within a function, we accept multiple locations (optimized code may do this,
1949 // there's no solution here so we do the best we can).
1950 // However if we're trying to leave the function, we don't know how to pick the
1951 // right location, so if there's more than one then we bail.
1952 if (!within_function.empty())
1953 candidates = within_function;
1954 else if (outside_function.size() == 1 && can_leave_function)
1955 candidates = outside_function;
1956
1957 // Check if we got anything.
1958 if (candidates.empty())
1959 {
1960 if (outside_function.empty())
1961 {
1962 return Error("Cannot locate an address for %s:%i.",
1963 file.GetFilename().AsCString(), line);
1964 }
1965 else if (outside_function.size() == 1)
1966 {
1967 return Error("%s:%i is outside the current function.",
1968 file.GetFilename().AsCString(), line);
1969 }
1970 else
1971 {
1972 StreamString sstr;
1973 DumpAddressList(sstr, outside_function, target);
1974 return Error("%s:%i has multiple candidate locations:\n%s",
1975 file.GetFilename().AsCString(), line, sstr.GetString().c_str());
1976 }
1977 }
1978
1979 // Accept the first location, warn about any others.
1980 Address dest = candidates[0];
1981 if (warnings && candidates.size() > 1)
1982 {
1983 StreamString sstr;
1984 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
1985 file.GetFilename().AsCString(), line);
1986 DumpAddressList(sstr, candidates, target);
1987 *warnings = sstr.GetString();
1988 }
1989
1990 if (!reg_ctx->SetPC (dest))
1991 return Error("Cannot change PC to target address.");
1992
1993 return Error();
1994}
1995
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001996void
Greg Clayton0603aa92010-10-04 01:05:56 +00001997Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001998{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001999 ExecutionContext exe_ctx (shared_from_this());
2000 Process *process = exe_ctx.GetProcessPtr();
2001 if (process == NULL)
2002 return;
2003
Jason Molendab57e4a12013-11-04 09:33:30 +00002004 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00002005 SymbolContext frame_sc;
Greg Clayton0603aa92010-10-04 01:05:56 +00002006 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002007 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002008 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002009 if (frame_sp)
2010 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002011 exe_ctx.SetFrameSP(frame_sp);
2012 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002013 }
2014 }
2015
Greg Clayton1ac04c32012-02-21 00:09:25 +00002016 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00002017 assert (thread_format);
Greg Clayton0603aa92010-10-04 01:05:56 +00002018 Debugger::FormatPrompt (thread_format,
Greg Claytonc14ee322011-09-22 04:58:26 +00002019 frame_sp ? &frame_sc : NULL,
Greg Clayton0603aa92010-10-04 01:05:56 +00002020 &exe_ctx,
2021 NULL,
Michael Sartainc3ce7f272013-05-23 20:47:45 +00002022 strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002023}
2024
Greg Clayton99d0faf2010-11-18 23:32:35 +00002025void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002026Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00002027{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002028}
Jim Inghamee8aea12010-09-08 03:14:33 +00002029
Greg Clayton99d0faf2010-11-18 23:32:35 +00002030void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002031Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002032{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002033}
Jim Inghamee8aea12010-09-08 03:14:33 +00002034
Richard Mitton0a558352013-10-17 21:14:00 +00002035lldb::addr_t
2036Thread::GetThreadPointer ()
2037{
2038 return LLDB_INVALID_ADDRESS;
2039}
2040
2041addr_t
2042Thread::GetThreadLocalData (const ModuleSP module)
2043{
2044 // The default implementation is to ask the dynamic loader for it.
2045 // This can be overridden for specific platforms.
2046 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
2047 if (loader)
2048 return loader->GetThreadLocalData (module, shared_from_this());
2049 else
2050 return LLDB_INVALID_ADDRESS;
2051}
2052
Jason Molendab4892cd2014-05-13 22:02:48 +00002053bool
2054Thread::SafeToCallFunctions ()
2055{
2056 Process *process = GetProcess().get();
2057 if (process)
2058 {
2059 SystemRuntime *runtime = process->GetSystemRuntime ();
2060 if (runtime)
2061 {
2062 return runtime->SafeToCallFunctionsOnThisThread (shared_from_this());
2063 }
2064 }
2065 return true;
2066}
2067
Jason Molendab57e4a12013-11-04 09:33:30 +00002068lldb::StackFrameSP
2069Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
Jim Inghame4284b72010-09-23 17:40:12 +00002070{
Jason Molendab57e4a12013-11-04 09:33:30 +00002071 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00002072}
Caroline Ticeceb6b132010-10-26 03:11:13 +00002073
2074const char *
2075Thread::StopReasonAsCString (lldb::StopReason reason)
2076{
2077 switch (reason)
2078 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00002079 case eStopReasonInvalid: return "invalid";
2080 case eStopReasonNone: return "none";
2081 case eStopReasonTrace: return "trace";
2082 case eStopReasonBreakpoint: return "breakpoint";
2083 case eStopReasonWatchpoint: return "watchpoint";
2084 case eStopReasonSignal: return "signal";
2085 case eStopReasonException: return "exception";
2086 case eStopReasonExec: return "exec";
2087 case eStopReasonPlanComplete: return "plan complete";
2088 case eStopReasonThreadExiting: return "thread exiting";
Kuba Breckaafdf8422014-10-10 23:43:03 +00002089 case eStopReasonInstrumentation: return "instrumentation break";
Caroline Ticeceb6b132010-10-26 03:11:13 +00002090 }
2091
2092
2093 static char unknown_state_string[64];
2094 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
2095 return unknown_state_string;
2096}
2097
2098const char *
2099Thread::RunModeAsCString (lldb::RunMode mode)
2100{
2101 switch (mode)
2102 {
2103 case eOnlyThisThread: return "only this thread";
2104 case eAllThreads: return "all threads";
2105 case eOnlyDuringStepping: return "only during stepping";
2106 }
2107
2108 static char unknown_state_string[64];
2109 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
2110 return unknown_state_string;
2111}
Jim Ingham06e827c2010-11-11 19:26:09 +00002112
Greg Clayton7260f622011-04-18 08:33:37 +00002113size_t
2114Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
2115{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002116 ExecutionContext exe_ctx (shared_from_this());
2117 Target *target = exe_ctx.GetTargetPtr();
2118 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00002119 size_t num_frames_shown = 0;
2120 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00002121 bool is_selected = false;
2122 if (process)
2123 {
2124 if (process->GetThreadList().GetSelectedThread().get() == this)
2125 is_selected = true;
2126 }
2127 strm.Printf("%c ", is_selected ? '*' : ' ');
2128 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00002129 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002130 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00002131 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00002132 {
Jim Inghame610d642011-08-16 00:07:28 +00002133 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
2134 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
2135 {
2136 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
2137 }
Greg Clayton5113dc82011-08-12 06:47:54 +00002138 }
Greg Clayton7260f622011-04-18 08:33:37 +00002139 }
2140
2141 DumpUsingSettingsFormat (strm, start_frame);
2142
2143 if (num_frames > 0)
2144 {
2145 strm.IndentMore();
2146
2147 const bool show_frame_info = true;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002148
2149 const char *selected_frame_marker = NULL;
2150 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
2151 strm.IndentMore ();
2152 else
2153 selected_frame_marker = "* ";
2154
Greg Clayton39d0ab32012-03-29 01:41:38 +00002155 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
2156 start_frame,
2157 num_frames,
2158 show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002159 num_frames_with_source,
2160 selected_frame_marker);
2161 if (num_frames == 1)
2162 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00002163 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00002164 }
2165 return num_frames_shown;
2166}
2167
Jason Molenda705b1802014-06-13 02:37:02 +00002168bool
Kuba Breckaafdf8422014-10-10 23:43:03 +00002169Thread::GetDescription (Stream &strm, lldb::DescriptionLevel level, bool print_json_thread, bool print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002170{
2171 DumpUsingSettingsFormat (strm, 0);
2172 strm.Printf("\n");
2173
2174 StructuredData::ObjectSP thread_info = GetExtendedInfo();
Kuba Breckaafdf8422014-10-10 23:43:03 +00002175 StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo();
2176
2177 if (print_json_thread || print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002178 {
Kuba Breckaafdf8422014-10-10 23:43:03 +00002179 if (thread_info && print_json_thread)
2180 {
2181 thread_info->Dump (strm);
2182 strm.Printf("\n");
2183 }
2184
2185 if (stop_info && print_json_stopinfo)
2186 {
2187 stop_info->Dump (strm);
2188 strm.Printf("\n");
2189 }
2190
Jason Molenda705b1802014-06-13 02:37:02 +00002191 return true;
2192 }
2193
2194 if (thread_info)
2195 {
2196 StructuredData::ObjectSP activity = thread_info->GetObjectForDotSeparatedPath("activity");
2197 StructuredData::ObjectSP breadcrumb = thread_info->GetObjectForDotSeparatedPath("breadcrumb");
2198 StructuredData::ObjectSP messages = thread_info->GetObjectForDotSeparatedPath("trace_messages");
2199
2200 bool printed_activity = false;
2201 if (activity && activity->GetType() == StructuredData::Type::eTypeDictionary)
2202 {
2203 StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
2204 StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
2205 StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
2206 if (name && name->GetType() == StructuredData::Type::eTypeString
2207 && id && id->GetType() == StructuredData::Type::eTypeInteger)
2208 {
2209 strm.Printf(" Activity '%s', 0x%" PRIx64 "\n", name->GetAsString()->GetValue().c_str(), id->GetAsInteger()->GetValue());
2210 }
2211 printed_activity = true;
2212 }
2213 bool printed_breadcrumb = false;
2214 if (breadcrumb && breadcrumb->GetType() == StructuredData::Type::eTypeDictionary)
2215 {
2216 if (printed_activity)
2217 strm.Printf ("\n");
2218 StructuredData::Dictionary *breadcrumb_dict = breadcrumb->GetAsDictionary();
2219 StructuredData::ObjectSP breadcrumb_text = breadcrumb_dict->GetValueForKey ("name");
2220 if (breadcrumb_text && breadcrumb_text->GetType() == StructuredData::Type::eTypeString)
2221 {
2222 strm.Printf (" Current Breadcrumb: %s\n", breadcrumb_text->GetAsString()->GetValue().c_str());
2223 }
2224 printed_breadcrumb = true;
2225 }
2226 if (messages && messages->GetType() == StructuredData::Type::eTypeArray)
2227 {
2228 if (printed_breadcrumb)
2229 strm.Printf("\n");
2230 StructuredData::Array *messages_array = messages->GetAsArray();
2231 const size_t msg_count = messages_array->GetSize();
2232 if (msg_count > 0)
2233 {
2234 strm.Printf (" %zu trace messages:\n", msg_count);
2235 for (size_t i = 0; i < msg_count; i++)
2236 {
2237 StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
2238 if (message && message->GetType() == StructuredData::Type::eTypeDictionary)
2239 {
2240 StructuredData::Dictionary *message_dict = message->GetAsDictionary();
2241 StructuredData::ObjectSP message_text = message_dict->GetValueForKey ("message");
2242 if (message_text && message_text->GetType() == StructuredData::Type::eTypeString)
2243 {
2244 strm.Printf (" %s\n", message_text->GetAsString()->GetValue().c_str());
2245 }
2246 }
2247 }
2248 }
2249 }
2250 }
2251
2252 return true;
2253}
2254
Greg Clayton7260f622011-04-18 08:33:37 +00002255size_t
2256Thread::GetStackFrameStatus (Stream& strm,
2257 uint32_t first_frame,
2258 uint32_t num_frames,
2259 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002260 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00002261{
Greg Clayton39d0ab32012-03-29 01:41:38 +00002262 return GetStackFrameList()->GetStatus (strm,
2263 first_frame,
2264 num_frames,
2265 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002266 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00002267}
2268
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002269Unwind *
2270Thread::GetUnwinder ()
2271{
2272 if (m_unwinder_ap.get() == NULL)
2273 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00002274 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002275 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2276 switch (machine)
2277 {
2278 case llvm::Triple::x86_64:
2279 case llvm::Triple::x86:
2280 case llvm::Triple::arm:
Todd Fialad8eaa172014-07-23 14:37:35 +00002281 case llvm::Triple::aarch64:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002282 case llvm::Triple::thumb:
Ed Masteb73f8442013-10-10 00:59:47 +00002283 case llvm::Triple::mips64:
Deepak Panickal6d3df422014-02-19 11:16:46 +00002284 case llvm::Triple::hexagon:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002285 m_unwinder_ap.reset (new UnwindLLDB (*this));
2286 break;
2287
2288 default:
2289 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2290 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2291 break;
2292 }
2293 }
2294 return m_unwinder_ap.get();
2295}
2296
2297
Greg Claytonfa559e52012-05-18 02:38:05 +00002298void
2299Thread::Flush ()
2300{
2301 ClearStackFrames ();
2302 m_reg_context_sp.reset();
2303}
Jim Ingham5d88a062012-10-16 00:09:33 +00002304
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00002305bool
Jim Ingham5d88a062012-10-16 00:09:33 +00002306Thread::IsStillAtLastBreakpointHit ()
2307{
2308 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2309 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2310 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002311 if (m_stop_info_sp) {
2312 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00002313 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002314 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00002315 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2316 if (reg_ctx_sp)
2317 {
2318 lldb::addr_t pc = reg_ctx_sp->GetPC();
2319 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002320 if (bp_site_sp &&
2321 static_cast<break_id_t>(value) == bp_site_sp->GetID())
Greg Clayton1afa68e2013-04-02 20:32:37 +00002322 return true;
2323 }
Jim Ingham5d88a062012-10-16 00:09:33 +00002324 }
2325 }
2326 return false;
2327}
Greg Clayton44d93782014-01-27 23:43:24 +00002328
2329
2330Error
2331Thread::StepIn (bool source_step,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002332 LazyBool step_in_avoids_code_without_debug_info,
2333 LazyBool step_out_avoids_code_without_debug_info)
2334
Greg Clayton44d93782014-01-27 23:43:24 +00002335{
2336 Error error;
2337 Process *process = GetProcess().get();
2338 if (StateIsStoppedState (process->GetState(), true))
2339 {
2340 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2341 ThreadPlanSP new_plan_sp;
2342 const lldb::RunMode run_mode = eOnlyThisThread;
2343 const bool abort_other_plans = false;
2344
2345 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2346 {
2347 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2348 new_plan_sp = QueueThreadPlanForStepInRange (abort_other_plans,
2349 sc.line_entry.range,
2350 sc,
2351 NULL,
2352 run_mode,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002353 step_in_avoids_code_without_debug_info,
2354 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002355 }
2356 else
2357 {
2358 new_plan_sp = QueueThreadPlanForStepSingleInstruction (false,
2359 abort_other_plans,
2360 run_mode);
2361 }
2362
2363 new_plan_sp->SetIsMasterPlan(true);
2364 new_plan_sp->SetOkayToDiscard(false);
2365
2366 // Why do we need to set the current thread by ID here???
2367 process->GetThreadList().SetSelectedThreadByID (GetID());
2368 error = process->Resume();
2369 }
2370 else
2371 {
2372 error.SetErrorString("process not stopped");
2373 }
2374 return error;
2375}
2376
2377Error
Jim Ingham4b4b2472014-03-13 02:47:14 +00002378Thread::StepOver (bool source_step,
2379 LazyBool step_out_avoids_code_without_debug_info)
Greg Clayton44d93782014-01-27 23:43:24 +00002380{
2381 Error error;
2382 Process *process = GetProcess().get();
2383 if (StateIsStoppedState (process->GetState(), true))
2384 {
2385 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2386 ThreadPlanSP new_plan_sp;
2387
2388 const lldb::RunMode run_mode = eOnlyThisThread;
2389 const bool abort_other_plans = false;
2390
2391 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2392 {
2393 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2394 new_plan_sp = QueueThreadPlanForStepOverRange (abort_other_plans,
2395 sc.line_entry.range,
2396 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002397 run_mode,
2398 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002399 }
2400 else
2401 {
2402 new_plan_sp = QueueThreadPlanForStepSingleInstruction (true,
2403 abort_other_plans,
2404 run_mode);
2405 }
2406
2407 new_plan_sp->SetIsMasterPlan(true);
2408 new_plan_sp->SetOkayToDiscard(false);
2409
2410 // Why do we need to set the current thread by ID here???
2411 process->GetThreadList().SetSelectedThreadByID (GetID());
2412 error = process->Resume();
2413 }
2414 else
2415 {
2416 error.SetErrorString("process not stopped");
2417 }
2418 return error;
2419}
2420
2421Error
2422Thread::StepOut ()
2423{
2424 Error error;
2425 Process *process = GetProcess().get();
2426 if (StateIsStoppedState (process->GetState(), true))
2427 {
2428 const bool first_instruction = false;
2429 const bool stop_other_threads = false;
2430 const bool abort_other_plans = false;
2431
2432 ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut (abort_other_plans,
2433 NULL,
2434 first_instruction,
2435 stop_other_threads,
2436 eVoteYes,
2437 eVoteNoOpinion,
2438 0));
2439
2440 new_plan_sp->SetIsMasterPlan(true);
2441 new_plan_sp->SetOkayToDiscard(false);
2442
2443 // Why do we need to set the current thread by ID here???
2444 process->GetThreadList().SetSelectedThreadByID (GetID());
2445 error = process->Resume();
2446 }
2447 else
2448 {
2449 error.SetErrorString("process not stopped");
2450 }
2451 return error;
Jim Ingham4b4b2472014-03-13 02:47:14 +00002452}