blob: 6f28a6779782f7ee8653e8ab9c3ceef6e646ab88 [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 Clayton554f68d2015-02-04 22:00:53 +000016#include "lldb/Core/FormatEntity.h"
Greg Clayton160c9d82013-05-01 21:54:04 +000017#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Stream.h"
19#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000020#include "lldb/Core/RegularExpression.h"
Greg Clayton7260f622011-04-18 08:33:37 +000021#include "lldb/Host/Host.h"
Jim Ingham4da62062014-01-23 21:52:47 +000022#include "lldb/Interpreter/OptionValueFileSpecList.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000023#include "lldb/Interpreter/OptionValueProperties.h"
24#include "lldb/Interpreter/Property.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000025#include "lldb/Symbol/Function.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000026#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Target/DynamicLoader.h"
28#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000029#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Target/Process.h"
31#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000032#include "lldb/Target/StopInfo.h"
Jason Molendab4892cd2014-05-13 22:02:48 +000033#include "lldb/Target/SystemRuntime.h"
Greg Clayton896dff62010-07-23 16:45:51 +000034#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Target/Thread.h"
36#include "lldb/Target/ThreadPlan.h"
37#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "lldb/Target/ThreadPlanBase.h"
Jim Ingham2bdbfd52014-09-29 23:17:18 +000039#include "lldb/Target/ThreadPlanPython.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Target/ThreadPlanStepInstruction.h"
41#include "lldb/Target/ThreadPlanStepOut.h"
42#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
43#include "lldb/Target/ThreadPlanStepThrough.h"
44#include "lldb/Target/ThreadPlanStepInRange.h"
45#include "lldb/Target/ThreadPlanStepOverRange.h"
46#include "lldb/Target/ThreadPlanRunToAddress.h"
47#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000048#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000049#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000050#include "Plugins/Process/Utility/UnwindLLDB.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000051#include "Plugins/Process/Utility/UnwindMacOSXFrameBackchain.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000052
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
54using namespace lldb;
55using namespace lldb_private;
56
Greg Clayton67cc0632012-08-22 17:17:09 +000057
58const ThreadPropertiesSP &
59Thread::GetGlobalProperties()
60{
61 static ThreadPropertiesSP g_settings_sp;
62 if (!g_settings_sp)
63 g_settings_sp.reset (new ThreadProperties (true));
64 return g_settings_sp;
65}
66
67static PropertyDefinition
68g_properties[] =
69{
Jim Ingham4b4b2472014-03-13 02:47:14 +000070 { "step-in-avoid-nodebug", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, step-in will not stop in functions with no debug information." },
71 { "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 "
72 "debug information. Passing a frame argument to step-out will override this option." },
Greg Clayton7bd4c602015-01-21 21:51:02 +000073 { "step-avoid-regexp", OptionValue::eTypeRegex , true , 0, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
74 { "step-avoid-libraries", OptionValue::eTypeFileSpecList , true , 0, NULL, NULL, "A list of libraries that source stepping won't stop in." },
Greg Clayton67cc0632012-08-22 17:17:09 +000075 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
76 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
77};
78
79enum {
Jim Ingham4b4b2472014-03-13 02:47:14 +000080 ePropertyStepInAvoidsNoDebug,
81 ePropertyStepOutAvoidsNoDebug,
Greg Clayton67cc0632012-08-22 17:17:09 +000082 ePropertyStepAvoidRegex,
Jim Ingham4da62062014-01-23 21:52:47 +000083 ePropertyStepAvoidLibraries,
Greg Clayton67cc0632012-08-22 17:17:09 +000084 ePropertyEnableThreadTrace
85};
86
87
88class ThreadOptionValueProperties : public OptionValueProperties
89{
90public:
91 ThreadOptionValueProperties (const ConstString &name) :
92 OptionValueProperties (name)
93 {
94 }
95
96 // This constructor is used when creating ThreadOptionValueProperties when it
97 // is part of a new lldb_private::Thread instance. It will copy all current
98 // global property values as needed
99 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +0000100 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +0000101 {
102 }
103
104 virtual const Property *
105 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
106 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000107 // When getting the value for a key from the thread options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +0000108 // try and grab the setting from the current thread if there is one. Else we just
109 // use the one from this instance.
110 if (exe_ctx)
111 {
112 Thread *thread = exe_ctx->GetThreadPtr();
113 if (thread)
114 {
115 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
116 if (this != instance_properties)
117 return instance_properties->ProtectedGetPropertyAtIndex (idx);
118 }
119 }
120 return ProtectedGetPropertyAtIndex (idx);
121 }
122};
123
124
125
126ThreadProperties::ThreadProperties (bool is_global) :
127 Properties ()
128{
129 if (is_global)
130 {
131 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
132 m_collection_sp->Initialize(g_properties);
133 }
134 else
135 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
136}
137
138ThreadProperties::~ThreadProperties()
139{
140}
141
142const RegularExpression *
143ThreadProperties::GetSymbolsToAvoidRegexp()
144{
145 const uint32_t idx = ePropertyStepAvoidRegex;
146 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
147}
148
Jim Ingham4da62062014-01-23 21:52:47 +0000149FileSpecList &
150ThreadProperties::GetLibrariesToAvoid() const
151{
152 const uint32_t idx = ePropertyStepAvoidLibraries;
153 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
154 assert(option_value);
155 return option_value->GetCurrentValue();
156}
157
Greg Clayton67cc0632012-08-22 17:17:09 +0000158bool
159ThreadProperties::GetTraceEnabledState() const
160{
161 const uint32_t idx = ePropertyEnableThreadTrace;
162 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
163}
164
Jim Ingham4b4b2472014-03-13 02:47:14 +0000165bool
166ThreadProperties::GetStepInAvoidsNoDebug() const
167{
168 const uint32_t idx = ePropertyStepInAvoidsNoDebug;
169 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
170}
171
172bool
173ThreadProperties::GetStepOutAvoidsNoDebug() const
174{
175 const uint32_t idx = ePropertyStepOutAvoidsNoDebug;
176 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
177}
178
179
Jim Ingham4f465cf2012-10-10 18:32:14 +0000180//------------------------------------------------------------------
181// Thread Event Data
182//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000183
Jim Ingham4f465cf2012-10-10 18:32:14 +0000184
185const ConstString &
186Thread::ThreadEventData::GetFlavorString ()
187{
188 static ConstString g_flavor ("Thread::ThreadEventData");
189 return g_flavor;
190}
191
192Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
193 m_thread_sp (thread_sp),
194 m_stack_id ()
195{
196}
197
198Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
199 m_thread_sp (thread_sp),
200 m_stack_id (stack_id)
201{
202}
203
204Thread::ThreadEventData::ThreadEventData () :
205 m_thread_sp (),
206 m_stack_id ()
207{
208}
209
210Thread::ThreadEventData::~ThreadEventData ()
211{
212}
213
214void
215Thread::ThreadEventData::Dump (Stream *s) const
216{
217
218}
219
220const Thread::ThreadEventData *
221Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
222{
223 if (event_ptr)
224 {
225 const EventData *event_data = event_ptr->GetData();
226 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
227 return static_cast <const ThreadEventData *> (event_ptr->GetData());
228 }
229 return NULL;
230}
231
232ThreadSP
233Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
234{
235 ThreadSP thread_sp;
236 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
237 if (event_data)
238 thread_sp = event_data->GetThread();
239 return thread_sp;
240}
241
242StackID
243Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
244{
245 StackID stack_id;
246 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
247 if (event_data)
248 stack_id = event_data->GetStackID();
249 return stack_id;
250}
251
Jason Molendab57e4a12013-11-04 09:33:30 +0000252StackFrameSP
Jim Ingham4f465cf2012-10-10 18:32:14 +0000253Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
254{
255 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
Jason Molendab57e4a12013-11-04 09:33:30 +0000256 StackFrameSP frame_sp;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000257 if (event_data)
258 {
259 ThreadSP thread_sp = event_data->GetThread();
260 if (thread_sp)
261 {
262 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
263 }
264 }
265 return frame_sp;
266}
267
268//------------------------------------------------------------------
269// Thread class
270//------------------------------------------------------------------
271
272ConstString &
273Thread::GetStaticBroadcasterClass ()
274{
275 static ConstString class_name ("lldb.thread");
276 return class_name;
277}
278
Jason Molendaa8ff5432014-03-06 06:31:18 +0000279Thread::Thread (Process &process, lldb::tid_t tid, bool use_invalid_index_id) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000280 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000282 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
283 m_process_wp (process.shared_from_this()),
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000284 m_stop_info_sp (),
285 m_stop_info_stop_id (0),
Greg Claytona97c4d22014-12-09 23:31:02 +0000286 m_stop_info_override_stop_id (0),
Jason Molendaa8ff5432014-03-06 06:31:18 +0000287 m_index_id (use_invalid_index_id ? LLDB_INVALID_INDEX32 : process.GetNextThreadIndexID(tid)),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000290 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000293 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000294 m_curr_frames_sp (),
295 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000297 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000298 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000299 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000300 m_destroy_called (false),
Jason Molenda705b1802014-06-13 02:37:02 +0000301 m_override_should_notify (eLazyBoolCalculate),
302 m_extended_info_fetched (false),
303 m_extended_info ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304{
Greg Clayton5160ce52013-03-27 23:08:40 +0000305 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000307 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")",
308 static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309
Jim Ingham4f465cf2012-10-10 18:32:14 +0000310 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311 QueueFundamentalPlan(true);
312}
313
314
315Thread::~Thread()
316{
Greg Clayton5160ce52013-03-27 23:08:40 +0000317 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000318 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000319 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")",
320 static_cast<void*>(this), GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000321 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
322 assert (m_destroy_called);
323}
324
325void
326Thread::DestroyThread ()
327{
Jim Inghamb1499242013-10-18 17:11:02 +0000328 // Tell any plans on the plan stacks that the thread is being destroyed since
329 // any plans that have a thread go away in the middle of might need
330 // to do cleanup, or in some cases NOT do cleanup...
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000331 for (auto plan : m_plan_stack)
332 plan->ThreadDestroyed();
333
Jim Inghamb1499242013-10-18 17:11:02 +0000334 for (auto plan : m_discarded_plan_stack)
335 plan->ThreadDestroyed();
336
337 for (auto plan : m_completed_plan_stack)
338 plan->ThreadDestroyed();
339
Greg Clayton35a4cc52012-10-29 20:52:08 +0000340 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000341 m_plan_stack.clear();
342 m_discarded_plan_stack.clear();
343 m_completed_plan_stack.clear();
Greg Clayton6e10f142013-07-30 00:23:06 +0000344
345 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the
346 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
347 // without checking first whether it is destroyed, they won't crash.
348 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
349 m_plan_stack.push_back (null_plan_sp);
350
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000351 m_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000352 m_reg_context_sp.reset();
353 m_unwinder_ap.reset();
354 Mutex::Locker locker(m_frame_mutex);
355 m_curr_frames_sp.reset();
356 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357}
358
Jim Ingham4f465cf2012-10-10 18:32:14 +0000359void
360Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
361{
362 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
363 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
364}
365
366uint32_t
Jason Molendab57e4a12013-11-04 09:33:30 +0000367Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
Jim Ingham4f465cf2012-10-10 18:32:14 +0000368{
369 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
370 if (broadcast)
371 BroadcastSelectedFrameChange(frame->GetStackID());
372 return ret_value;
373}
374
375bool
376Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
377{
Jason Molendab57e4a12013-11-04 09:33:30 +0000378 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
Jim Ingham4f465cf2012-10-10 18:32:14 +0000379 if (frame_sp)
380 {
381 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
382 if (broadcast)
383 BroadcastSelectedFrameChange(frame_sp->GetStackID());
384 return true;
385 }
386 else
387 return false;
388}
389
Jim Ingham93208b82013-01-31 21:46:01 +0000390bool
391Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
392{
393 const bool broadcast = true;
394 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
395 if (success)
396 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000397 StackFrameSP frame_sp = GetSelectedFrame();
Jim Ingham93208b82013-01-31 21:46:01 +0000398 if (frame_sp)
399 {
400 bool already_shown = false;
401 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
402 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
403 {
404 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
405 }
406
407 bool show_frame_info = true;
408 bool show_source = !already_shown;
409 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
410 }
411 return false;
412 }
413 else
414 return false;
415}
416
Jim Ingham4f465cf2012-10-10 18:32:14 +0000417
Jim Inghamb15bfc72010-10-20 00:39:53 +0000418lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000419Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420{
Greg Clayton6e10f142013-07-30 00:23:06 +0000421 if (m_destroy_called)
422 return m_stop_info_sp;
423
Jim Inghamb15bfc72010-10-20 00:39:53 +0000424 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000425 ProcessSP process_sp (GetProcess());
426 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000427 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000428 {
Jim Ingham30fadaf2014-07-08 01:07:32 +0000429 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject(), GetExpressionVariable());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000430 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000431 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000432 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000433 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
434 (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 +0000435 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000436 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000437 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000438 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000439 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000440 GetPrivateStopInfo ();
441 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000442 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000443 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444}
445
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000446lldb::StopInfoSP
447Thread::GetPrivateStopInfo ()
448{
Greg Clayton6e10f142013-07-30 00:23:06 +0000449 if (m_destroy_called)
450 return m_stop_info_sp;
451
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000452 ProcessSP process_sp (GetProcess());
453 if (process_sp)
454 {
Daniel Malea246cb612013-05-14 15:20:12 +0000455 const uint32_t process_stop_id = process_sp->GetStopID();
456 if (m_stop_info_stop_id != process_stop_id)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000457 {
Daniel Malea246cb612013-05-14 15:20:12 +0000458 if (m_stop_info_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000459 {
Daniel Malea246cb612013-05-14 15:20:12 +0000460 if (m_stop_info_sp->IsValid()
461 || IsStillAtLastBreakpointHit()
462 || GetCurrentPlan()->IsVirtualStep())
463 SetStopInfo (m_stop_info_sp);
464 else
465 m_stop_info_sp.reset();
466 }
467
468 if (!m_stop_info_sp)
469 {
470 if (CalculateStopInfo() == false)
471 SetStopInfo (StopInfoSP());
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000472 }
473 }
Greg Claytona97c4d22014-12-09 23:31:02 +0000474
475 // The stop info can be manually set by calling Thread::SetStopInfo()
476 // prior to this function ever getting called, so we can't rely on
477 // "m_stop_info_stop_id != process_stop_id" as the condition for
478 // the if statement below, we must also check the stop info to see
479 // if we need to override it. See the header documentation in
480 // Process::GetStopInfoOverrideCallback() for more information on
481 // the stop info override callback.
482 if (m_stop_info_override_stop_id != process_stop_id)
483 {
484 m_stop_info_override_stop_id = process_stop_id;
485 if (m_stop_info_sp)
486 {
487 ArchSpec::StopInfoOverrideCallbackType callback = GetProcess()->GetStopInfoOverrideCallback();
488 if (callback)
489 callback(*this);
490 }
491 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000492 }
493 return m_stop_info_sp;
494}
495
496
Greg Clayton97d5cf02012-09-25 02:40:06 +0000497lldb::StopReason
498Thread::GetStopReason()
499{
500 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
501 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000502 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000503 return eStopReasonNone;
504}
505
506
507
Jim Ingham77787032011-01-20 02:03:18 +0000508void
509Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
510{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000511 m_stop_info_sp = stop_info_sp;
512 if (m_stop_info_sp)
Jim Ingham221d51c2013-05-08 00:35:16 +0000513 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000514 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham221d51c2013-05-08 00:35:16 +0000515 // If we are overriding the ShouldReportStop, do that here:
516 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000517 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000518 }
519
Greg Clayton1ac04c32012-02-21 00:09:25 +0000520 ProcessSP process_sp (GetProcess());
521 if (process_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000522 m_stop_info_stop_id = process_sp->GetStopID();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000523 else
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000524 m_stop_info_stop_id = UINT32_MAX;
Greg Clayton8cda7f02013-05-21 21:55:59 +0000525 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
526 if (log)
Ed Maste2bc76432014-06-25 00:38:35 +0000527 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000528 static_cast<void*>(this), GetID(),
529 stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>",
530 m_stop_info_stop_id);
Jim Ingham77787032011-01-20 02:03:18 +0000531}
532
533void
Jim Ingham221d51c2013-05-08 00:35:16 +0000534Thread::SetShouldReportStop (Vote vote)
535{
536 if (vote == eVoteNoOpinion)
537 return;
538 else
539 {
540 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000541 if (m_stop_info_sp)
542 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000543 }
544}
545
546void
Jim Ingham77787032011-01-20 02:03:18 +0000547Thread::SetStopInfoToNothing()
548{
549 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
550 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
551 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
552}
553
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554bool
555Thread::ThreadStoppedForAReason (void)
556{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000557 return (bool) GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558}
559
Jim Ingham77787032011-01-20 02:03:18 +0000560bool
561Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
562{
Greg Claytonf74cf862013-11-13 23:28:31 +0000563 saved_state.register_backup_sp.reset();
564 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
565 if (frame_sp)
566 {
567 lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
568 if (reg_checkpoint_sp)
569 {
570 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
571 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp))
572 saved_state.register_backup_sp = reg_checkpoint_sp;
573 }
574 }
575 if (!saved_state.register_backup_sp)
Jim Ingham77787032011-01-20 02:03:18 +0000576 return false;
577
578 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000579 ProcessSP process_sp (GetProcess());
580 if (process_sp)
581 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000582 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
583
Jim Ingham77787032011-01-20 02:03:18 +0000584 return true;
585}
586
587bool
Jim Ingham8559a352012-11-26 23:52:18 +0000588Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000589{
Greg Claytonf74cf862013-11-13 23:28:31 +0000590 if (saved_state.register_backup_sp)
591 {
592 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
593 if (frame_sp)
594 {
595 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
596 if (reg_ctx_sp)
597 {
598 bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp);
599
600 // Clear out all stack frames as our world just changed.
601 ClearStackFrames();
602 reg_ctx_sp->InvalidateIfNeeded(true);
603 if (m_unwinder_ap.get())
604 m_unwinder_ap->Clear();
605 return ret;
606 }
607 }
608 }
609 return false;
Jim Ingham8559a352012-11-26 23:52:18 +0000610}
611
612bool
613Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
614{
Jim Ingham0c270682011-01-25 02:47:23 +0000615 if (saved_state.stop_info_sp)
616 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000617 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000618 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000619 return true;
620}
621
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622StateType
623Thread::GetState() const
624{
625 // If any other threads access this we will need a mutex for it
626 Mutex::Locker locker(m_state_mutex);
627 return m_state;
628}
629
630void
631Thread::SetState(StateType state)
632{
633 Mutex::Locker locker(m_state_mutex);
634 m_state = state;
635}
636
637void
638Thread::WillStop()
639{
640 ThreadPlan *current_plan = GetCurrentPlan();
641
642 // FIXME: I may decide to disallow threads with no plans. In which
643 // case this should go to an assert.
644
645 if (!current_plan)
646 return;
647
648 current_plan->WillStop();
649}
650
651void
652Thread::SetupForResume ()
653{
654 if (GetResumeState() != eStateSuspended)
655 {
656
657 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
658 // telling the current plan it will resume, since we might change what the current
659 // plan is.
660
Greg Clayton1afa68e2013-04-02 20:32:37 +0000661// StopReason stop_reason = lldb::eStopReasonInvalid;
662// StopInfoSP stop_info_sp = GetStopInfo();
663// if (stop_info_sp.get())
664// stop_reason = stop_info_sp->GetStopReason();
665// if (stop_reason == lldb::eStopReasonBreakpoint)
666 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
667 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668 {
Greg Claytona97c4d22014-12-09 23:31:02 +0000669 const addr_t thread_pc = reg_ctx_sp->GetPC();
670 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000671 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000672 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000673 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
674 // special to step over a breakpoint.
675
676 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000677
Greg Claytona97c4d22014-12-09 23:31:02 +0000678 bool push_step_over_bp_plan = false;
679 if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint)
680 {
681 ThreadPlanStepOverBreakpoint *bp_plan = (ThreadPlanStepOverBreakpoint *)cur_plan;
682 if (bp_plan->GetBreakpointLoadAddress() != thread_pc)
683 push_step_over_bp_plan = true;
684 }
685 else
686 push_step_over_bp_plan = true;
687
688 if (push_step_over_bp_plan)
Greg Clayton1afa68e2013-04-02 20:32:37 +0000689 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000690 ThreadPlanSP step_bp_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
691 if (step_bp_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000693 ;
694 step_bp_plan_sp->SetPrivate (true);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000695
696 if (GetCurrentPlan()->RunState() != eStateStepping)
697 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000698 ThreadPlanStepOverBreakpoint *step_bp_plan
699 = static_cast<ThreadPlanStepOverBreakpoint *>(step_bp_plan_sp.get());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000700 step_bp_plan->SetAutoContinue(true);
701 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000702 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 }
705 }
706 }
707 }
708}
709
710bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000711Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712{
713 // At this point clear the completed plan stack.
714 m_completed_plan_stack.clear();
715 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000716 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717
Greg Clayton97d5cf02012-09-25 02:40:06 +0000718 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000719
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000720 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
721 if (backing_thread_sp)
722 backing_thread_sp->m_temporary_resume_state = resume_state;
723
724 // Make sure m_stop_info_sp is valid
725 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000726
Jim Ingham92087d82012-01-31 23:09:20 +0000727 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
728 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
729 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
730 // about the fact that we are resuming...
Greg Clayton1ac04c32012-02-21 00:09:25 +0000731 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000732 if (m_stop_info_stop_id == process_stop_id &&
733 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000734 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000735 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000736 if (stop_info)
737 stop_info->WillResume (resume_state);
738 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739
740 // Tell all the plans that we are about to resume in case they need to clear any state.
741 // We distinguish between the plan on the top of the stack and the lower
742 // plans in case a plan needs to do any special business before it runs.
743
Greg Claytond1d06e42013-04-20 00:27:58 +0000744 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000746 if (plan_ptr)
747 {
748 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749
Greg Claytond1d06e42013-04-20 00:27:58 +0000750 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
751 {
752 plan_ptr->WillResume (resume_state, false);
753 }
754
755 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
756 // In that case, don't reset it here.
757
758 if (need_to_resume && resume_state != eStateSuspended)
759 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000760 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000761 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000762 }
763
Greg Clayton160c9d82013-05-01 21:54:04 +0000764 if (need_to_resume)
765 {
766 ClearStackFrames();
767 // Let Thread subclasses do any special work they need to prior to resuming
768 WillResume (resume_state);
769 }
770
Jim Ingham513c6bb2012-09-01 01:02:41 +0000771 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772}
773
774void
775Thread::DidResume ()
776{
777 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
778}
779
Andrew Kaylor29d65742013-05-10 17:19:04 +0000780void
781Thread::DidStop ()
782{
783 SetState (eStateStopped);
784}
785
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786bool
787Thread::ShouldStop (Event* event_ptr)
788{
789 ThreadPlan *current_plan = GetCurrentPlan();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000790
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791 bool should_stop = true;
792
Greg Clayton5160ce52013-03-27 23:08:40 +0000793 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000794
Jim Ingham92087d82012-01-31 23:09:20 +0000795 if (GetResumeState () == eStateSuspended)
796 {
797 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000798 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 +0000799 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000800 return false;
801 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000802
Jim Ingham92087d82012-01-31 23:09:20 +0000803 if (GetTemporaryResumeState () == eStateSuspended)
804 {
805 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000806 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 +0000807 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000808 return false;
809 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000810
Daniel Malea246cb612013-05-14 15:20:12 +0000811 // Based on the current thread plan and process stop info, check if this
812 // thread caused the process to stop. NOTE: this must take place before
813 // the plan is moved from the current plan stack to the completed plan
814 // stack.
Jim Ingham92087d82012-01-31 23:09:20 +0000815 if (ThreadStoppedForAReason() == false)
816 {
817 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000818 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 +0000819 __FUNCTION__, GetID (), GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000820 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000821 return false;
822 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000823
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000824 if (log)
825 {
Jim Inghamdee1bc92013-06-22 00:27:45 +0000826 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 +0000827 __FUNCTION__, static_cast<void*>(this), GetID (),
Greg Clayton160c9d82013-05-01 21:54:04 +0000828 GetProtocolID (),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000829 GetRegisterContext()
830 ? GetRegisterContext()->GetPC()
831 : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000832 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000834 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000836 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000837 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000838
Jim Ingham06e827c2010-11-11 19:26:09 +0000839 // The top most plan always gets to do the trace log...
840 current_plan->DoTraceLog ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000841
Jim Ingham6d66ce62012-04-20 21:16:56 +0000842 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
843 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
844 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000845 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Ingham6d66ce62012-04-20 21:16:56 +0000846 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
847 {
848 if (log)
849 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
850 return false;
851 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852
Jim Inghambad39e42012-09-05 21:12:49 +0000853 // If we've already been restarted, don't query the plans since the state they would examine is not current.
854 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
855 return false;
856
857 // Before the plans see the state of the world, calculate the current inlined depth.
858 GetStackFrameList()->CalculateCurrentInlinedDepth();
859
Jim Ingham25f66702011-12-03 01:52:59 +0000860 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
861 // If that plan is still working, then we don't need to do any more work. If the plan that explains
862 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
863 // whether they still need to do more work.
864
865 bool done_processing_current_plan = false;
866
Jim Ingham0161b492013-02-09 01:29:05 +0000867 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000868 {
869 if (current_plan->TracerExplainsStop())
870 {
871 done_processing_current_plan = true;
872 should_stop = false;
873 }
874 else
875 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000876 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000877 // does and let it handle the situation.
878 ThreadPlan *plan_ptr = current_plan;
879 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
880 {
Jim Ingham0161b492013-02-09 01:29:05 +0000881 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000882 {
883 should_stop = plan_ptr->ShouldStop (event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000884
Jim Ingham25f66702011-12-03 01:52:59 +0000885 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
886 // and all the plans below it off the stack.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000887
Jim Ingham25f66702011-12-03 01:52:59 +0000888 if (plan_ptr->MischiefManaged())
889 {
Jim Ingham031177e2012-05-11 23:49:49 +0000890 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000891 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000892
Jim Ingham25f66702011-12-03 01:52:59 +0000893 do
894 {
895 if (should_stop)
896 current_plan->WillStop();
897 PopPlan();
898 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000899 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
900 // Now, if the responsible plan was not "Okay to discard" then we're done,
901 // otherwise we forward this to the next plan in the stack below.
902 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
903 done_processing_current_plan = true;
904 else
905 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000906 }
907 else
908 done_processing_current_plan = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000909
Jim Ingham25f66702011-12-03 01:52:59 +0000910 break;
911 }
912
913 }
914 }
915 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000916
Jim Ingham25f66702011-12-03 01:52:59 +0000917 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000918 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000919 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000920
Jim Ingham10c4b242011-10-15 00:23:43 +0000921 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000922 log->Printf("Plan %s explains stop, auto-continue %i.",
923 current_plan->GetName(), over_ride_stop);
924
Jim Ingham0f16e732011-02-08 05:20:59 +0000925 // We're starting from the base plan, so just let it decide;
926 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000928 should_stop = current_plan->ShouldStop (event_ptr);
929 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000930 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000931 }
932 else
933 {
934 // Otherwise, don't let the base plan override what the other plans say to do, since
935 // presumably if there were other plans they would know what to do...
936 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000937 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000938 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000939 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000940
Jim Ingham0f16e732011-02-08 05:20:59 +0000941 should_stop = current_plan->ShouldStop(event_ptr);
942 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000943 log->Printf("Plan %s should stop: %d.",
944 current_plan->GetName(), should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000945 if (current_plan->MischiefManaged())
946 {
947 if (should_stop)
948 current_plan->WillStop();
949
950 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
951 // Otherwise, see if the plan's parent wants to stop.
952
953 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
954 {
955 PopPlan();
956 break;
957 }
958 else
959 {
960
961 PopPlan();
962
963 current_plan = GetCurrentPlan();
964 if (current_plan == NULL)
965 {
966 break;
967 }
968 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000969 }
970 else
971 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000972 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000973 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000974 }
975 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000976
Jim Inghamb01e7422010-06-19 04:45:32 +0000977 if (over_ride_stop)
978 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000979
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000980 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000981
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000982 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
983 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
984 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
985 // This code clears stale plans off the stack.
986
987 if (should_stop)
988 {
989 ThreadPlan *plan_ptr = GetCurrentPlan();
990 while (!PlanIsBasePlan(plan_ptr))
Jim Ingham64e7ead2012-05-03 21:19:36 +0000991 {
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000992 bool stale = plan_ptr->IsPlanStale ();
993 ThreadPlan *examined_plan = plan_ptr;
994 plan_ptr = GetPreviousPlan (examined_plan);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000995
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000996 if (stale)
997 {
998 if (log)
999 log->Printf("Plan %s being discarded in cleanup, it says it is already done.",
1000 examined_plan->GetName());
1001 DiscardThreadPlansUpToPlan(examined_plan);
Jim Ingham64e7ead2012-05-03 21:19:36 +00001002 }
1003 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005
Jim Ingham10c4b242011-10-15 00:23:43 +00001006 if (log)
1007 {
1008 StreamString s;
1009 s.IndentMore();
1010 DumpThreadPlans(&s);
1011 log->Printf ("Plan stack final state:\n%s", s.GetData());
1012 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
1013 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001014 return should_stop;
1015}
1016
1017Vote
1018Thread::ShouldReportStop (Event* event_ptr)
1019{
1020 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +00001021 StateType temp_thread_state = GetTemporaryResumeState();
1022
Greg Clayton5160ce52013-03-27 23:08:40 +00001023 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +00001024
1025 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
1026 {
1027 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001028 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 +00001029 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001030 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001031
Jim Ingham92087d82012-01-31 23:09:20 +00001032 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
1033 {
1034 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001035 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 +00001036 return eVoteNoOpinion;
1037 }
1038
1039 if (!ThreadStoppedForAReason())
1040 {
1041 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001042 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 +00001043 return eVoteNoOpinion;
1044 }
1045
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046 if (m_completed_plan_stack.size() > 0)
1047 {
1048 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +00001049 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001050 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 +00001051 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
1052 }
1053 else
Greg Clayton2cad65a2010-09-03 17:10:42 +00001054 {
Jim Ingham0161b492013-02-09 01:29:05 +00001055 Vote thread_vote = eVoteNoOpinion;
1056 ThreadPlan *plan_ptr = GetCurrentPlan();
1057 while (1)
1058 {
1059 if (plan_ptr->PlanExplainsStop(event_ptr))
1060 {
1061 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1062 break;
1063 }
1064 if (PlanIsBasePlan(plan_ptr))
1065 break;
1066 else
1067 plan_ptr = GetPreviousPlan(plan_ptr);
1068 }
Greg Clayton2cad65a2010-09-03 17:10:42 +00001069 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001070 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 +00001071
1072 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001073 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074}
1075
1076Vote
1077Thread::ShouldReportRun (Event* event_ptr)
1078{
1079 StateType thread_state = GetResumeState ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001080
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081 if (thread_state == eStateSuspended
1082 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +00001083 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +00001085 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001086
Greg Clayton5160ce52013-03-27 23:08:40 +00001087 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001088 if (m_completed_plan_stack.size() > 0)
1089 {
1090 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +00001091 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001092 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 +00001093 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001094 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001095 m_completed_plan_stack.back()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001096
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001097 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
1098 }
1099 else
Jim Ingham444586b2011-01-24 06:34:17 +00001100 {
1101 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001102 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 +00001103 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001104 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001105 GetCurrentPlan()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001106
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001107 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +00001108 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109}
1110
Jim Ingham1b54c882010-06-16 02:00:15 +00001111bool
1112Thread::MatchesSpec (const ThreadSpec *spec)
1113{
1114 if (spec == NULL)
1115 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +00001116
Jim Ingham3d902922012-03-07 22:03:04 +00001117 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +00001118}
1119
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120void
1121Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1122{
1123 if (thread_plan_sp)
1124 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001125 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1126 if (!thread_plan_sp->GetThreadPlanTracer())
1127 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001128 m_plan_stack.push_back (thread_plan_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001129
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001130 thread_plan_sp->DidPush();
1131
Greg Clayton5160ce52013-03-27 23:08:40 +00001132 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133 if (log)
1134 {
1135 StreamString s;
1136 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamdee1bc92013-06-22 00:27:45 +00001137 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001138 static_cast<void*>(this), s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001139 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001140 }
1141 }
1142}
1143
1144void
1145Thread::PopPlan ()
1146{
Greg Clayton5160ce52013-03-27 23:08:40 +00001147 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001148
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001149 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001150 return;
1151 else
1152 {
1153 ThreadPlanSP &plan = m_plan_stack.back();
1154 if (log)
1155 {
Daniel Malead01b2952012-11-29 21:49:15 +00001156 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157 }
1158 m_completed_plan_stack.push_back (plan);
1159 plan->WillPop();
1160 m_plan_stack.pop_back();
1161 }
1162}
1163
1164void
1165Thread::DiscardPlan ()
1166{
Jim Inghamdee1bc92013-06-22 00:27:45 +00001167 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001168 if (m_plan_stack.size() > 1)
1169 {
1170 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamdee1bc92013-06-22 00:27:45 +00001171 if (log)
1172 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1173
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174 m_discarded_plan_stack.push_back (plan);
1175 plan->WillPop();
1176 m_plan_stack.pop_back();
1177 }
1178}
1179
1180ThreadPlan *
1181Thread::GetCurrentPlan ()
1182{
Jim Inghame1471232012-04-19 00:17:05 +00001183 // There will always be at least the base plan. If somebody is mucking with a
1184 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +00001185 if (m_plan_stack.empty())
1186 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +00001187 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188}
1189
1190ThreadPlanSP
1191Thread::GetCompletedPlan ()
1192{
1193 ThreadPlanSP empty_plan_sp;
1194 if (!m_completed_plan_stack.empty())
1195 {
1196 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1197 {
1198 ThreadPlanSP completed_plan_sp;
1199 completed_plan_sp = m_completed_plan_stack[i];
1200 if (!completed_plan_sp->GetPrivate ())
1201 return completed_plan_sp;
1202 }
1203 }
1204 return empty_plan_sp;
1205}
1206
Jim Ingham73ca05a2011-12-17 01:35:57 +00001207ValueObjectSP
1208Thread::GetReturnValueObject ()
1209{
1210 if (!m_completed_plan_stack.empty())
1211 {
1212 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1213 {
1214 ValueObjectSP return_valobj_sp;
1215 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1216 if (return_valobj_sp)
1217 return return_valobj_sp;
1218 }
1219 }
1220 return ValueObjectSP();
1221}
1222
Jim Ingham30fadaf2014-07-08 01:07:32 +00001223ClangExpressionVariableSP
1224Thread::GetExpressionVariable ()
1225{
1226 if (!m_completed_plan_stack.empty())
1227 {
1228 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1229 {
1230 ClangExpressionVariableSP expression_variable_sp;
1231 expression_variable_sp = m_completed_plan_stack[i]->GetExpressionVariable();
1232 if (expression_variable_sp)
1233 return expression_variable_sp;
1234 }
1235 }
1236 return ClangExpressionVariableSP();
1237}
1238
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239bool
1240Thread::IsThreadPlanDone (ThreadPlan *plan)
1241{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001242 if (!m_completed_plan_stack.empty())
1243 {
1244 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1245 {
1246 if (m_completed_plan_stack[i].get() == plan)
1247 return true;
1248 }
1249 }
1250 return false;
1251}
1252
1253bool
1254Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1255{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001256 if (!m_discarded_plan_stack.empty())
1257 {
1258 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1259 {
1260 if (m_discarded_plan_stack[i].get() == plan)
1261 return true;
1262 }
1263 }
1264 return false;
1265}
1266
1267ThreadPlan *
1268Thread::GetPreviousPlan (ThreadPlan *current_plan)
1269{
1270 if (current_plan == NULL)
1271 return NULL;
1272
1273 int stack_size = m_completed_plan_stack.size();
1274 for (int i = stack_size - 1; i > 0; i--)
1275 {
1276 if (current_plan == m_completed_plan_stack[i].get())
1277 return m_completed_plan_stack[i-1].get();
1278 }
1279
1280 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1281 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001282 if (m_plan_stack.size() > 0)
1283 return m_plan_stack.back().get();
1284 else
1285 return NULL;
1286 }
1287
1288 stack_size = m_plan_stack.size();
1289 for (int i = stack_size - 1; i > 0; i--)
1290 {
1291 if (current_plan == m_plan_stack[i].get())
1292 return m_plan_stack[i-1].get();
1293 }
1294 return NULL;
1295}
1296
1297void
1298Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1299{
1300 if (abort_other_plans)
1301 DiscardThreadPlans(true);
1302
1303 PushPlan (thread_plan_sp);
1304}
1305
Jim Ingham06e827c2010-11-11 19:26:09 +00001306
1307void
1308Thread::EnableTracer (bool value, bool single_stepping)
1309{
1310 int stack_size = m_plan_stack.size();
1311 for (int i = 0; i < stack_size; i++)
1312 {
1313 if (m_plan_stack[i]->GetThreadPlanTracer())
1314 {
1315 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1316 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1317 }
1318 }
1319}
1320
1321void
1322Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1323{
1324 int stack_size = m_plan_stack.size();
1325 for (int i = 0; i < stack_size; i++)
1326 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1327}
1328
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001329bool
1330Thread::DiscardUserThreadPlansUpToIndex (uint32_t thread_index)
1331{
1332 // Count the user thread plans from the back end to get the number of the one we want
1333 // to discard:
1334
1335 uint32_t idx = 0;
1336 ThreadPlan *up_to_plan_ptr = nullptr;
1337
1338 for (ThreadPlanSP plan_sp : m_plan_stack)
1339 {
1340 if (plan_sp->GetPrivate())
1341 continue;
1342 if (idx == thread_index)
1343 {
1344 up_to_plan_ptr = plan_sp.get();
1345 break;
1346 }
1347 else
1348 idx++;
1349 }
1350
1351 if (up_to_plan_ptr == nullptr)
1352 return false;
1353
1354 DiscardThreadPlansUpToPlan(up_to_plan_ptr);
1355 return true;
1356}
1357
1358
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001360Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1361{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001362 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1363}
1364
1365void
1366Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1367{
Greg Clayton5160ce52013-03-27 23:08:40 +00001368 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001369 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001370 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p",
1371 GetID(), static_cast<void*>(up_to_plan_ptr));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001372
1373 int stack_size = m_plan_stack.size();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001374
Jim Ingham399f1ca2010-11-05 19:25:48 +00001375 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1376 // stack, and if so discard up to and including it.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001377
Jim Ingham64e7ead2012-05-03 21:19:36 +00001378 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001379 {
1380 for (int i = stack_size - 1; i > 0; i--)
1381 DiscardPlan();
1382 }
1383 else
1384 {
1385 bool found_it = false;
1386 for (int i = stack_size - 1; i > 0; i--)
1387 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001388 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001389 found_it = true;
1390 }
1391 if (found_it)
1392 {
1393 bool last_one = false;
1394 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1395 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001396 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001397 last_one = true;
1398 DiscardPlan();
1399 }
1400 }
1401 }
1402 return;
1403}
1404
1405void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406Thread::DiscardThreadPlans(bool force)
1407{
Greg Clayton5160ce52013-03-27 23:08:40 +00001408 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001409 if (log)
1410 {
Daniel Malead01b2952012-11-29 21:49:15 +00001411 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412 }
1413
1414 if (force)
1415 {
1416 int stack_size = m_plan_stack.size();
1417 for (int i = stack_size - 1; i > 0; i--)
1418 {
1419 DiscardPlan();
1420 }
1421 return;
1422 }
1423
1424 while (1)
1425 {
1426
1427 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001428 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001429
1430 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1431 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1432 {
1433 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1434 {
1435 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1436 break;
1437 }
1438 }
1439
1440 if (discard)
1441 {
1442 // First pop all the dependent plans:
1443 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1444 {
1445
1446 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1447 // for the plan leaves it in a state that it is safe to pop the plan
1448 // with no more notice?
1449 DiscardPlan();
1450 }
1451
1452 // Now discard the master plan itself.
1453 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1454 // discard it's dependent plans, but not it...
1455 if (master_plan_idx > 0)
1456 {
1457 DiscardPlan();
1458 }
1459 }
1460 else
1461 {
1462 // If the master plan doesn't want to get discarded, then we're done.
1463 break;
1464 }
1465
1466 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001467}
1468
Jim Inghamcf274f92012-04-09 22:37:39 +00001469bool
1470Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1471{
1472 if (plan_ptr->IsBasePlan())
1473 return true;
1474 else if (m_plan_stack.size() == 0)
1475 return false;
1476 else
1477 return m_plan_stack[0].get() == plan_ptr;
1478}
1479
Jim Ingham93208b82013-01-31 21:46:01 +00001480Error
1481Thread::UnwindInnermostExpression()
1482{
1483 Error error;
1484 int stack_size = m_plan_stack.size();
1485
1486 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1487 // stack, and if so discard up to and including it.
1488
1489 for (int i = stack_size - 1; i > 0; i--)
1490 {
1491 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1492 {
1493 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1494 return error;
1495 }
1496 }
1497 error.SetErrorString("No expressions currently active on this thread");
1498 return error;
1499}
1500
1501
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001502ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503Thread::QueueFundamentalPlan (bool abort_other_plans)
1504{
1505 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1506 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001507 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508}
1509
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001510ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001511Thread::QueueThreadPlanForStepSingleInstruction
1512(
1513 bool step_over,
1514 bool abort_other_plans,
1515 bool stop_other_threads
1516)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001517{
1518 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1519 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001520 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001521}
1522
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001523ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001524Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001525(
1526 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001527 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001528 const SymbolContext &addr_context,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001529 lldb::RunMode stop_other_threads,
1530 LazyBool step_out_avoids_code_withoug_debug_info
Jim Inghamc6276822012-12-12 19:58:40 +00001531)
1532{
1533 ThreadPlanSP thread_plan_sp;
Jim Ingham4b4b2472014-03-13 02:47:14 +00001534 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads, step_out_avoids_code_withoug_debug_info));
1535
Jim Inghamc6276822012-12-12 19:58:40 +00001536 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001537 return thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001538}
1539
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001540ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001541Thread::QueueThreadPlanForStepInRange
1542(
1543 bool abort_other_plans,
1544 const AddressRange &range,
1545 const SymbolContext &addr_context,
1546 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001547 lldb::RunMode stop_other_threads,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001548 LazyBool step_in_avoids_code_without_debug_info,
1549 LazyBool step_out_avoids_code_without_debug_info
Greg Clayton474966a2010-06-12 18:59:55 +00001550)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001551{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001552 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInRange (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001553 range,
1554 addr_context,
1555 stop_other_threads,
1556 step_in_avoids_code_without_debug_info,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001557 step_out_avoids_code_without_debug_info));
1558 ThreadPlanStepInRange *plan = static_cast<ThreadPlanStepInRange *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001559
Jim Inghamc6276822012-12-12 19:58:40 +00001560 if (step_in_target)
1561 plan->SetStepInTarget(step_in_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001562
1563 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001564 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001565}
1566
1567
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001568ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001569Thread::QueueThreadPlanForStepOut
1570(
1571 bool abort_other_plans,
1572 SymbolContext *addr_context,
1573 bool first_insn,
1574 bool stop_other_threads,
1575 Vote stop_vote,
1576 Vote run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001577 uint32_t frame_idx,
1578 LazyBool step_out_avoids_code_withoug_debug_info
Greg Clayton481cef22011-01-21 06:11:58 +00001579)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001580{
Greg Clayton481cef22011-01-21 06:11:58 +00001581 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1582 addr_context,
1583 first_insn,
1584 stop_other_threads,
1585 stop_vote,
1586 run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001587 frame_idx,
1588 step_out_avoids_code_withoug_debug_info));
1589
1590 if (thread_plan_sp->ValidatePlan(NULL))
1591 {
1592 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1593 return thread_plan_sp;
1594 }
1595 else
1596 {
1597 return ThreadPlanSP();
1598 }
1599}
1600
1601ThreadPlanSP
1602Thread::QueueThreadPlanForStepOutNoShouldStop
1603(
1604 bool abort_other_plans,
1605 SymbolContext *addr_context,
1606 bool first_insn,
1607 bool stop_other_threads,
1608 Vote stop_vote,
1609 Vote run_vote,
1610 uint32_t frame_idx
1611)
1612{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001613 ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001614 addr_context,
1615 first_insn,
1616 stop_other_threads,
1617 stop_vote,
1618 run_vote,
1619 frame_idx,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001620 eLazyBoolNo));
1621
1622 ThreadPlanStepOut *new_plan = static_cast<ThreadPlanStepOut *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001623 new_plan->ClearShouldStopHereCallbacks();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001624
Sean Callanan708709c2012-07-31 22:19:25 +00001625 if (thread_plan_sp->ValidatePlan(NULL))
1626 {
1627 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001628 return thread_plan_sp;
Sean Callanan708709c2012-07-31 22:19:25 +00001629 }
1630 else
1631 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001632 return ThreadPlanSP();
Sean Callanan708709c2012-07-31 22:19:25 +00001633 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001634}
1635
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001636ThreadPlanSP
Jim Ingham18de2fd2012-05-10 01:35:39 +00001637Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001638{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001639 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001640 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001641 return ThreadPlanSP();
Jim Ingham25f66702011-12-03 01:52:59 +00001642
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001643 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001644 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001645}
1646
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001647ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001648Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1649 Address &target_addr,
1650 bool stop_other_threads)
1651{
1652 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1653 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001654 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655}
1656
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001657ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001658Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001659 lldb::addr_t *address_list,
1660 size_t num_addresses,
1661 bool stop_other_threads,
1662 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001663{
Greg Clayton481cef22011-01-21 06:11:58 +00001664 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001665 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001666 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001667
1668}
1669
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001670lldb::ThreadPlanSP
1671Thread::QueueThreadPlanForStepScripted (bool abort_other_plans,
1672 const char *class_name,
1673 bool stop_other_threads)
1674{
1675 ThreadPlanSP thread_plan_sp (new ThreadPlanPython (*this, class_name));
1676 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1677 // This seems a little funny, but I don't want to have to split up the constructor and the
1678 // DidPush in the scripted plan, that seems annoying.
1679 // That means the constructor has to be in DidPush.
1680 // So I have to validate the plan AFTER pushing it, and then take it off again...
1681 if (!thread_plan_sp->ValidatePlan(nullptr))
1682 {
1683 DiscardThreadPlansUpToPlan(thread_plan_sp);
1684 return ThreadPlanSP();
1685 }
1686 else
1687 return thread_plan_sp;
1688
1689}
1690
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001691uint32_t
1692Thread::GetIndexID () const
1693{
1694 return m_index_id;
1695}
1696
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001697static void
1698PrintPlanElement (Stream *s, const ThreadPlanSP &plan, lldb::DescriptionLevel desc_level, int32_t elem_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001701 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001702 s->Printf ("Element %d: ", elem_idx);
1703 plan->GetDescription (s, desc_level);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001704 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001705 s->IndentLess();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001706}
1707
1708static void
1709PrintPlanStack (Stream *s, const std::vector<lldb::ThreadPlanSP> &plan_stack, lldb::DescriptionLevel desc_level, bool include_internal)
1710{
1711 int32_t print_idx = 0;
1712 for (ThreadPlanSP plan_sp : plan_stack)
1713 {
1714 if (include_internal || !plan_sp->GetPrivate())
1715 {
1716 PrintPlanElement (s, plan_sp, desc_level, print_idx++);
1717 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001718 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001719}
1720
1721void
1722Thread::DumpThreadPlans (Stream *s,
1723 lldb::DescriptionLevel desc_level,
1724 bool include_internal,
1725 bool ignore_boring_threads) const
1726{
Jason Molenda60b5da62014-10-16 07:53:46 +00001727 uint32_t stack_size;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001728
1729 if (ignore_boring_threads)
1730 {
1731 uint32_t stack_size = m_plan_stack.size();
1732 uint32_t completed_stack_size = m_completed_plan_stack.size();
1733 uint32_t discarded_stack_size = m_discarded_plan_stack.size();
1734 if (stack_size == 1 && completed_stack_size == 0 && discarded_stack_size == 0)
1735 {
1736 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 "\n", GetIndexID(), GetID());
1737 s->IndentMore();
1738 s->Indent();
1739 s->Printf("No active thread plans\n");
1740 s->IndentLess();
1741 return;
1742 }
1743 }
1744
1745 s->Indent();
1746 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 ":\n", GetIndexID(), GetID());
1747 s->IndentMore();
1748 s->Indent();
1749 s->Printf ("Active plan stack:\n");
1750 PrintPlanStack (s, m_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001751
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001753 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001754 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001755 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001756 s->Printf ("Completed Plan Stack:\n");
1757 PrintPlanStack (s, m_completed_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001758 }
1759
1760 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001761 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001762 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001763 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001764 s->Printf ("Discarded Plan Stack:\n");
1765 PrintPlanStack (s, m_discarded_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001766 }
1767
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001768 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001769}
1770
Greg Claytond9e416c2012-02-18 05:35:26 +00001771TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001772Thread::CalculateTarget ()
1773{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001774 TargetSP target_sp;
1775 ProcessSP process_sp(GetProcess());
1776 if (process_sp)
1777 target_sp = process_sp->CalculateTarget();
1778 return target_sp;
1779
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001780}
1781
Greg Claytond9e416c2012-02-18 05:35:26 +00001782ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001783Thread::CalculateProcess ()
1784{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001785 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001786}
1787
Greg Claytond9e416c2012-02-18 05:35:26 +00001788ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001789Thread::CalculateThread ()
1790{
Greg Claytond9e416c2012-02-18 05:35:26 +00001791 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001792}
1793
Jason Molendab57e4a12013-11-04 09:33:30 +00001794StackFrameSP
1795Thread::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001796{
Jason Molendab57e4a12013-11-04 09:33:30 +00001797 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001798}
1799
1800void
Greg Clayton0603aa92010-10-04 01:05:56 +00001801Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001802{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001803 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001804}
1805
Greg Clayton12daf9462010-08-25 00:35:26 +00001806
Greg Clayton39d0ab32012-03-29 01:41:38 +00001807StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001808Thread::GetStackFrameList ()
1809{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001810 StackFrameListSP frame_list_sp;
1811 Mutex::Locker locker(m_frame_mutex);
1812 if (m_curr_frames_sp)
1813 {
1814 frame_list_sp = m_curr_frames_sp;
1815 }
1816 else
1817 {
1818 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1819 m_curr_frames_sp = frame_list_sp;
1820 }
1821 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001822}
1823
Greg Clayton12daf9462010-08-25 00:35:26 +00001824void
1825Thread::ClearStackFrames ()
1826{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001827 Mutex::Locker locker(m_frame_mutex);
1828
Greg Clayton160c9d82013-05-01 21:54:04 +00001829 Unwind *unwinder = GetUnwinder ();
1830 if (unwinder)
1831 unwinder->Clear();
1832
Jim Inghamb0c72a52012-02-29 03:40:22 +00001833 // Only store away the old "reference" StackFrameList if we got all its frames:
1834 // FIXME: At some point we can try to splice in the frames we have fetched into
1835 // the new frame as we make it, but let's not try that now.
1836 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001837 m_prev_frames_sp.swap (m_curr_frames_sp);
1838 m_curr_frames_sp.reset();
Jason Molenda705b1802014-06-13 02:37:02 +00001839
1840 m_extended_info.reset();
1841 m_extended_info_fetched = false;
Jim Ingham87c11912010-08-12 02:14:28 +00001842}
1843
Jason Molendab57e4a12013-11-04 09:33:30 +00001844lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001845Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1846{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001847 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001848}
1849
Jim Ingham44137582012-09-12 00:40:39 +00001850
1851Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001852Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001853{
Jason Molendab57e4a12013-11-04 09:33:30 +00001854 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001855 Error return_error;
1856
1857 if (!frame_sp)
1858 {
Daniel Malead01b2952012-11-29 21:49:15 +00001859 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001860 }
1861
Jim Ingham4f465cf2012-10-10 18:32:14 +00001862 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001863}
1864
1865Error
Jason Molendab57e4a12013-11-04 09:33:30 +00001866Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001867{
1868 Error return_error;
1869
1870 if (!frame_sp)
1871 {
1872 return_error.SetErrorString("Can't return to a null frame.");
1873 return return_error;
1874 }
1875
1876 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001877 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
Jason Molendab57e4a12013-11-04 09:33:30 +00001878 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001879 if (!older_frame_sp)
1880 {
1881 return_error.SetErrorString("No older frame to return to.");
1882 return return_error;
1883 }
Jim Ingham44137582012-09-12 00:40:39 +00001884
1885 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001886 {
Jim Ingham44137582012-09-12 00:40:39 +00001887 lldb::ABISP abi = thread->GetProcess()->GetABI();
1888 if (!abi)
1889 {
1890 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001891 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001892 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001893 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1894
1895 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1896 // Turn that back on when that works.
Jason Molenda97e704b2014-10-16 08:07:20 +00001897 if (/* DISABLES CODE */ (0) && sc.function != NULL)
Jim Ingham1f51e602012-09-27 01:15:29 +00001898 {
1899 Type *function_type = sc.function->GetType();
1900 if (function_type)
1901 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001902 ClangASTType return_type = sc.function->GetClangType().GetFunctionReturnType();
Jim Ingham1f51e602012-09-27 01:15:29 +00001903 if (return_type)
1904 {
Jim Ingham1f51e602012-09-27 01:15:29 +00001905 StreamString s;
Greg Clayton57ee3062013-07-11 22:46:58 +00001906 return_type.DumpTypeDescription(&s);
1907 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham1f51e602012-09-27 01:15:29 +00001908 if (cast_value_sp)
1909 {
1910 cast_value_sp->SetFormat(eFormatHex);
1911 return_value_sp = cast_value_sp;
1912 }
1913 }
1914 }
1915 }
1916
Jim Inghamcb640dd2012-09-14 02:14:15 +00001917 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001918 if (!return_error.Success())
1919 return return_error;
1920 }
1921
1922 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001923 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001924 // cook their data
1925
Jason Molendab57e4a12013-11-04 09:33:30 +00001926 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
Jim Ingham93208b82013-01-31 21:46:01 +00001927 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001928 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001929 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1930 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001931 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001932 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1933 if (copy_success)
1934 {
1935 thread->DiscardThreadPlans(true);
1936 thread->ClearStackFrames();
1937 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1938 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1939 }
1940 else
1941 {
1942 return_error.SetErrorString("Could not reset register values.");
1943 }
Jim Ingham93208b82013-01-31 21:46:01 +00001944 }
1945 else
1946 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001947 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001948 }
Jim Ingham44137582012-09-12 00:40:39 +00001949 }
1950 else
1951 {
Jim Ingham93208b82013-01-31 21:46:01 +00001952 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001953 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001954 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001955}
1956
Richard Mittonf86248d2013-09-12 02:20:34 +00001957static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1958{
1959 for (size_t n=0;n<list.size();n++)
1960 {
1961 s << "\t";
1962 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1963 s << "\n";
1964 }
1965}
1966
1967Error
1968Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1969{
1970 ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
1971 Target *target = exe_ctx.GetTargetPtr();
1972 TargetSP target_sp = exe_ctx.GetTargetSP();
1973 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00001974 StackFrame *frame = exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00001975 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1976
1977 // Find candidate locations.
1978 std::vector<Address> candidates, within_function, outside_function;
1979 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
1980
1981 // If possible, we try and stay within the current function.
1982 // Within a function, we accept multiple locations (optimized code may do this,
1983 // there's no solution here so we do the best we can).
1984 // However if we're trying to leave the function, we don't know how to pick the
1985 // right location, so if there's more than one then we bail.
1986 if (!within_function.empty())
1987 candidates = within_function;
1988 else if (outside_function.size() == 1 && can_leave_function)
1989 candidates = outside_function;
1990
1991 // Check if we got anything.
1992 if (candidates.empty())
1993 {
1994 if (outside_function.empty())
1995 {
1996 return Error("Cannot locate an address for %s:%i.",
1997 file.GetFilename().AsCString(), line);
1998 }
1999 else if (outside_function.size() == 1)
2000 {
2001 return Error("%s:%i is outside the current function.",
2002 file.GetFilename().AsCString(), line);
2003 }
2004 else
2005 {
2006 StreamString sstr;
2007 DumpAddressList(sstr, outside_function, target);
2008 return Error("%s:%i has multiple candidate locations:\n%s",
2009 file.GetFilename().AsCString(), line, sstr.GetString().c_str());
2010 }
2011 }
2012
2013 // Accept the first location, warn about any others.
2014 Address dest = candidates[0];
2015 if (warnings && candidates.size() > 1)
2016 {
2017 StreamString sstr;
2018 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
2019 file.GetFilename().AsCString(), line);
2020 DumpAddressList(sstr, candidates, target);
2021 *warnings = sstr.GetString();
2022 }
2023
2024 if (!reg_ctx->SetPC (dest))
2025 return Error("Cannot change PC to target address.");
2026
2027 return Error();
2028}
2029
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002030void
Greg Clayton0603aa92010-10-04 01:05:56 +00002031Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002032{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002033 ExecutionContext exe_ctx (shared_from_this());
2034 Process *process = exe_ctx.GetProcessPtr();
2035 if (process == NULL)
2036 return;
2037
Jason Molendab57e4a12013-11-04 09:33:30 +00002038 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00002039 SymbolContext frame_sc;
Jim Inghamd1f25362015-01-28 01:17:26 +00002040 if (frame_idx != LLDB_INVALID_FRAME_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002041 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002042 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002043 if (frame_sp)
2044 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002045 exe_ctx.SetFrameSP(frame_sp);
2046 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002047 }
2048 }
2049
Greg Clayton554f68d2015-02-04 22:00:53 +00002050 const FormatEntity::Entry *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00002051 assert (thread_format);
Greg Clayton554f68d2015-02-04 22:00:53 +00002052
2053 FormatEntity::Format(*thread_format,
2054 strm,
2055 frame_sp ? &frame_sc : NULL,
2056 &exe_ctx,
2057 NULL,
2058 NULL,
2059 false,
2060 false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002061}
2062
Greg Clayton99d0faf2010-11-18 23:32:35 +00002063void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002064Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00002065{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002066}
Jim Inghamee8aea12010-09-08 03:14:33 +00002067
Greg Clayton99d0faf2010-11-18 23:32:35 +00002068void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002069Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002070{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002071}
Jim Inghamee8aea12010-09-08 03:14:33 +00002072
Richard Mitton0a558352013-10-17 21:14:00 +00002073lldb::addr_t
2074Thread::GetThreadPointer ()
2075{
2076 return LLDB_INVALID_ADDRESS;
2077}
2078
2079addr_t
2080Thread::GetThreadLocalData (const ModuleSP module)
2081{
2082 // The default implementation is to ask the dynamic loader for it.
2083 // This can be overridden for specific platforms.
2084 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
2085 if (loader)
2086 return loader->GetThreadLocalData (module, shared_from_this());
2087 else
2088 return LLDB_INVALID_ADDRESS;
2089}
2090
Jason Molendab4892cd2014-05-13 22:02:48 +00002091bool
2092Thread::SafeToCallFunctions ()
2093{
2094 Process *process = GetProcess().get();
2095 if (process)
2096 {
2097 SystemRuntime *runtime = process->GetSystemRuntime ();
2098 if (runtime)
2099 {
2100 return runtime->SafeToCallFunctionsOnThisThread (shared_from_this());
2101 }
2102 }
2103 return true;
2104}
2105
Jason Molendab57e4a12013-11-04 09:33:30 +00002106lldb::StackFrameSP
2107Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
Jim Inghame4284b72010-09-23 17:40:12 +00002108{
Jason Molendab57e4a12013-11-04 09:33:30 +00002109 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00002110}
Caroline Ticeceb6b132010-10-26 03:11:13 +00002111
2112const char *
2113Thread::StopReasonAsCString (lldb::StopReason reason)
2114{
2115 switch (reason)
2116 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00002117 case eStopReasonInvalid: return "invalid";
2118 case eStopReasonNone: return "none";
2119 case eStopReasonTrace: return "trace";
2120 case eStopReasonBreakpoint: return "breakpoint";
2121 case eStopReasonWatchpoint: return "watchpoint";
2122 case eStopReasonSignal: return "signal";
2123 case eStopReasonException: return "exception";
2124 case eStopReasonExec: return "exec";
2125 case eStopReasonPlanComplete: return "plan complete";
2126 case eStopReasonThreadExiting: return "thread exiting";
Kuba Breckaafdf8422014-10-10 23:43:03 +00002127 case eStopReasonInstrumentation: return "instrumentation break";
Caroline Ticeceb6b132010-10-26 03:11:13 +00002128 }
2129
2130
2131 static char unknown_state_string[64];
2132 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
2133 return unknown_state_string;
2134}
2135
2136const char *
2137Thread::RunModeAsCString (lldb::RunMode mode)
2138{
2139 switch (mode)
2140 {
2141 case eOnlyThisThread: return "only this thread";
2142 case eAllThreads: return "all threads";
2143 case eOnlyDuringStepping: return "only during stepping";
2144 }
2145
2146 static char unknown_state_string[64];
2147 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
2148 return unknown_state_string;
2149}
Jim Ingham06e827c2010-11-11 19:26:09 +00002150
Greg Clayton7260f622011-04-18 08:33:37 +00002151size_t
2152Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
2153{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002154 ExecutionContext exe_ctx (shared_from_this());
2155 Target *target = exe_ctx.GetTargetPtr();
2156 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00002157 size_t num_frames_shown = 0;
2158 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00002159 bool is_selected = false;
2160 if (process)
2161 {
2162 if (process->GetThreadList().GetSelectedThread().get() == this)
2163 is_selected = true;
2164 }
2165 strm.Printf("%c ", is_selected ? '*' : ' ');
2166 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00002167 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002168 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00002169 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00002170 {
Jim Inghame610d642011-08-16 00:07:28 +00002171 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
2172 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
2173 {
2174 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
2175 }
Greg Clayton5113dc82011-08-12 06:47:54 +00002176 }
Greg Clayton7260f622011-04-18 08:33:37 +00002177 }
2178
2179 DumpUsingSettingsFormat (strm, start_frame);
2180
2181 if (num_frames > 0)
2182 {
2183 strm.IndentMore();
2184
2185 const bool show_frame_info = true;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002186
2187 const char *selected_frame_marker = NULL;
2188 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
2189 strm.IndentMore ();
2190 else
2191 selected_frame_marker = "* ";
2192
Greg Clayton39d0ab32012-03-29 01:41:38 +00002193 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
2194 start_frame,
2195 num_frames,
2196 show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002197 num_frames_with_source,
2198 selected_frame_marker);
2199 if (num_frames == 1)
2200 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00002201 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00002202 }
2203 return num_frames_shown;
2204}
2205
Jason Molenda705b1802014-06-13 02:37:02 +00002206bool
Kuba Breckaafdf8422014-10-10 23:43:03 +00002207Thread::GetDescription (Stream &strm, lldb::DescriptionLevel level, bool print_json_thread, bool print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002208{
2209 DumpUsingSettingsFormat (strm, 0);
2210 strm.Printf("\n");
2211
2212 StructuredData::ObjectSP thread_info = GetExtendedInfo();
Kuba Breckaafdf8422014-10-10 23:43:03 +00002213 StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo();
2214
2215 if (print_json_thread || print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002216 {
Kuba Breckaafdf8422014-10-10 23:43:03 +00002217 if (thread_info && print_json_thread)
2218 {
2219 thread_info->Dump (strm);
2220 strm.Printf("\n");
2221 }
2222
2223 if (stop_info && print_json_stopinfo)
2224 {
2225 stop_info->Dump (strm);
2226 strm.Printf("\n");
2227 }
2228
Jason Molenda705b1802014-06-13 02:37:02 +00002229 return true;
2230 }
2231
2232 if (thread_info)
2233 {
2234 StructuredData::ObjectSP activity = thread_info->GetObjectForDotSeparatedPath("activity");
2235 StructuredData::ObjectSP breadcrumb = thread_info->GetObjectForDotSeparatedPath("breadcrumb");
2236 StructuredData::ObjectSP messages = thread_info->GetObjectForDotSeparatedPath("trace_messages");
2237
2238 bool printed_activity = false;
2239 if (activity && activity->GetType() == StructuredData::Type::eTypeDictionary)
2240 {
2241 StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
2242 StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
2243 StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
2244 if (name && name->GetType() == StructuredData::Type::eTypeString
2245 && id && id->GetType() == StructuredData::Type::eTypeInteger)
2246 {
2247 strm.Printf(" Activity '%s', 0x%" PRIx64 "\n", name->GetAsString()->GetValue().c_str(), id->GetAsInteger()->GetValue());
2248 }
2249 printed_activity = true;
2250 }
2251 bool printed_breadcrumb = false;
2252 if (breadcrumb && breadcrumb->GetType() == StructuredData::Type::eTypeDictionary)
2253 {
2254 if (printed_activity)
2255 strm.Printf ("\n");
2256 StructuredData::Dictionary *breadcrumb_dict = breadcrumb->GetAsDictionary();
2257 StructuredData::ObjectSP breadcrumb_text = breadcrumb_dict->GetValueForKey ("name");
2258 if (breadcrumb_text && breadcrumb_text->GetType() == StructuredData::Type::eTypeString)
2259 {
2260 strm.Printf (" Current Breadcrumb: %s\n", breadcrumb_text->GetAsString()->GetValue().c_str());
2261 }
2262 printed_breadcrumb = true;
2263 }
2264 if (messages && messages->GetType() == StructuredData::Type::eTypeArray)
2265 {
2266 if (printed_breadcrumb)
2267 strm.Printf("\n");
2268 StructuredData::Array *messages_array = messages->GetAsArray();
2269 const size_t msg_count = messages_array->GetSize();
2270 if (msg_count > 0)
2271 {
2272 strm.Printf (" %zu trace messages:\n", msg_count);
2273 for (size_t i = 0; i < msg_count; i++)
2274 {
2275 StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
2276 if (message && message->GetType() == StructuredData::Type::eTypeDictionary)
2277 {
2278 StructuredData::Dictionary *message_dict = message->GetAsDictionary();
2279 StructuredData::ObjectSP message_text = message_dict->GetValueForKey ("message");
2280 if (message_text && message_text->GetType() == StructuredData::Type::eTypeString)
2281 {
2282 strm.Printf (" %s\n", message_text->GetAsString()->GetValue().c_str());
2283 }
2284 }
2285 }
2286 }
2287 }
2288 }
2289
2290 return true;
2291}
2292
Greg Clayton7260f622011-04-18 08:33:37 +00002293size_t
2294Thread::GetStackFrameStatus (Stream& strm,
2295 uint32_t first_frame,
2296 uint32_t num_frames,
2297 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002298 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00002299{
Greg Clayton39d0ab32012-03-29 01:41:38 +00002300 return GetStackFrameList()->GetStatus (strm,
2301 first_frame,
2302 num_frames,
2303 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002304 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00002305}
2306
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002307Unwind *
2308Thread::GetUnwinder ()
2309{
2310 if (m_unwinder_ap.get() == NULL)
2311 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00002312 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002313 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2314 switch (machine)
2315 {
2316 case llvm::Triple::x86_64:
2317 case llvm::Triple::x86:
2318 case llvm::Triple::arm:
Todd Fialad8eaa172014-07-23 14:37:35 +00002319 case llvm::Triple::aarch64:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002320 case llvm::Triple::thumb:
Ed Masteb73f8442013-10-10 00:59:47 +00002321 case llvm::Triple::mips64:
Justin Hibbits6256a0e2014-10-31 02:34:28 +00002322 case llvm::Triple::ppc:
Justin Hibbitsdb39cdf2014-10-31 15:57:52 +00002323 case llvm::Triple::ppc64:
Deepak Panickal6d3df422014-02-19 11:16:46 +00002324 case llvm::Triple::hexagon:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002325 m_unwinder_ap.reset (new UnwindLLDB (*this));
2326 break;
2327
2328 default:
2329 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2330 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2331 break;
2332 }
2333 }
2334 return m_unwinder_ap.get();
2335}
2336
2337
Greg Claytonfa559e52012-05-18 02:38:05 +00002338void
2339Thread::Flush ()
2340{
2341 ClearStackFrames ();
2342 m_reg_context_sp.reset();
2343}
Jim Ingham5d88a062012-10-16 00:09:33 +00002344
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00002345bool
Jim Ingham5d88a062012-10-16 00:09:33 +00002346Thread::IsStillAtLastBreakpointHit ()
2347{
2348 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2349 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2350 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002351 if (m_stop_info_sp) {
2352 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00002353 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002354 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00002355 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2356 if (reg_ctx_sp)
2357 {
2358 lldb::addr_t pc = reg_ctx_sp->GetPC();
2359 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002360 if (bp_site_sp &&
2361 static_cast<break_id_t>(value) == bp_site_sp->GetID())
Greg Clayton1afa68e2013-04-02 20:32:37 +00002362 return true;
2363 }
Jim Ingham5d88a062012-10-16 00:09:33 +00002364 }
2365 }
2366 return false;
2367}
Greg Clayton44d93782014-01-27 23:43:24 +00002368
2369
2370Error
2371Thread::StepIn (bool source_step,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002372 LazyBool step_in_avoids_code_without_debug_info,
2373 LazyBool step_out_avoids_code_without_debug_info)
2374
Greg Clayton44d93782014-01-27 23:43:24 +00002375{
2376 Error error;
2377 Process *process = GetProcess().get();
2378 if (StateIsStoppedState (process->GetState(), true))
2379 {
2380 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2381 ThreadPlanSP new_plan_sp;
2382 const lldb::RunMode run_mode = eOnlyThisThread;
2383 const bool abort_other_plans = false;
2384
2385 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2386 {
2387 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2388 new_plan_sp = QueueThreadPlanForStepInRange (abort_other_plans,
2389 sc.line_entry.range,
2390 sc,
2391 NULL,
2392 run_mode,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002393 step_in_avoids_code_without_debug_info,
2394 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002395 }
2396 else
2397 {
2398 new_plan_sp = QueueThreadPlanForStepSingleInstruction (false,
2399 abort_other_plans,
2400 run_mode);
2401 }
2402
2403 new_plan_sp->SetIsMasterPlan(true);
2404 new_plan_sp->SetOkayToDiscard(false);
2405
2406 // Why do we need to set the current thread by ID here???
2407 process->GetThreadList().SetSelectedThreadByID (GetID());
2408 error = process->Resume();
2409 }
2410 else
2411 {
2412 error.SetErrorString("process not stopped");
2413 }
2414 return error;
2415}
2416
2417Error
Jim Ingham4b4b2472014-03-13 02:47:14 +00002418Thread::StepOver (bool source_step,
2419 LazyBool step_out_avoids_code_without_debug_info)
Greg Clayton44d93782014-01-27 23:43:24 +00002420{
2421 Error error;
2422 Process *process = GetProcess().get();
2423 if (StateIsStoppedState (process->GetState(), true))
2424 {
2425 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2426 ThreadPlanSP new_plan_sp;
2427
2428 const lldb::RunMode run_mode = eOnlyThisThread;
2429 const bool abort_other_plans = false;
2430
2431 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2432 {
2433 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2434 new_plan_sp = QueueThreadPlanForStepOverRange (abort_other_plans,
2435 sc.line_entry.range,
2436 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002437 run_mode,
2438 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002439 }
2440 else
2441 {
2442 new_plan_sp = QueueThreadPlanForStepSingleInstruction (true,
2443 abort_other_plans,
2444 run_mode);
2445 }
2446
2447 new_plan_sp->SetIsMasterPlan(true);
2448 new_plan_sp->SetOkayToDiscard(false);
2449
2450 // Why do we need to set the current thread by ID here???
2451 process->GetThreadList().SetSelectedThreadByID (GetID());
2452 error = process->Resume();
2453 }
2454 else
2455 {
2456 error.SetErrorString("process not stopped");
2457 }
2458 return error;
2459}
2460
2461Error
2462Thread::StepOut ()
2463{
2464 Error error;
2465 Process *process = GetProcess().get();
2466 if (StateIsStoppedState (process->GetState(), true))
2467 {
2468 const bool first_instruction = false;
2469 const bool stop_other_threads = false;
2470 const bool abort_other_plans = false;
2471
2472 ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut (abort_other_plans,
2473 NULL,
2474 first_instruction,
2475 stop_other_threads,
2476 eVoteYes,
2477 eVoteNoOpinion,
2478 0));
2479
2480 new_plan_sp->SetIsMasterPlan(true);
2481 new_plan_sp->SetOkayToDiscard(false);
2482
2483 // Why do we need to set the current thread by ID here???
2484 process->GetThreadList().SetSelectedThreadByID (GetID());
2485 error = process->Resume();
2486 }
2487 else
2488 {
2489 error.SetErrorString("process not stopped");
2490 }
2491 return error;
Jim Ingham4b4b2472014-03-13 02:47:14 +00002492}