blob: cbfffe98e7c505d2f67b642626b9296ba903d459 [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
Jim Ingham1b54c882010-06-16 02:00:15 +000012#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000013#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/Log.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000015#include "lldb/Core/FormatEntity.h"
Greg Clayton160c9d82013-05-01 21:54:04 +000016#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Stream.h"
18#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000019#include "lldb/Core/RegularExpression.h"
Greg Clayton7260f622011-04-18 08:33:37 +000020#include "lldb/Host/Host.h"
Jim Ingham4da62062014-01-23 21:52:47 +000021#include "lldb/Interpreter/OptionValueFileSpecList.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000022#include "lldb/Interpreter/OptionValueProperties.h"
23#include "lldb/Interpreter/Property.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000024#include "lldb/Symbol/Function.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000025#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Target/DynamicLoader.h"
27#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000028#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Process.h"
30#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000031#include "lldb/Target/StopInfo.h"
Jason Molendab4892cd2014-05-13 22:02:48 +000032#include "lldb/Target/SystemRuntime.h"
Greg Clayton896dff62010-07-23 16:45:51 +000033#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034#include "lldb/Target/Thread.h"
35#include "lldb/Target/ThreadPlan.h"
36#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Target/ThreadPlanBase.h"
Jim Ingham2bdbfd52014-09-29 23:17:18 +000038#include "lldb/Target/ThreadPlanPython.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Target/ThreadPlanStepInstruction.h"
40#include "lldb/Target/ThreadPlanStepOut.h"
41#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
42#include "lldb/Target/ThreadPlanStepThrough.h"
43#include "lldb/Target/ThreadPlanStepInRange.h"
44#include "lldb/Target/ThreadPlanStepOverRange.h"
45#include "lldb/Target/ThreadPlanRunToAddress.h"
46#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000047#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000048#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000049#include "Plugins/Process/Utility/UnwindLLDB.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000050#include "Plugins/Process/Utility/UnwindMacOSXFrameBackchain.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000051
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052
53using namespace lldb;
54using namespace lldb_private;
55
Greg Clayton67cc0632012-08-22 17:17:09 +000056
57const ThreadPropertiesSP &
58Thread::GetGlobalProperties()
59{
60 static ThreadPropertiesSP g_settings_sp;
61 if (!g_settings_sp)
62 g_settings_sp.reset (new ThreadProperties (true));
63 return g_settings_sp;
64}
65
66static PropertyDefinition
67g_properties[] =
68{
Jim Ingham4b4b2472014-03-13 02:47:14 +000069 { "step-in-avoid-nodebug", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, step-in will not stop in functions with no debug information." },
70 { "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 "
71 "debug information. Passing a frame argument to step-out will override this option." },
Greg Clayton7bd4c602015-01-21 21:51:02 +000072 { "step-avoid-regexp", OptionValue::eTypeRegex , true , 0, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
73 { "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 +000074 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
75 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
76};
77
78enum {
Jim Ingham4b4b2472014-03-13 02:47:14 +000079 ePropertyStepInAvoidsNoDebug,
80 ePropertyStepOutAvoidsNoDebug,
Greg Clayton67cc0632012-08-22 17:17:09 +000081 ePropertyStepAvoidRegex,
Jim Ingham4da62062014-01-23 21:52:47 +000082 ePropertyStepAvoidLibraries,
Greg Clayton67cc0632012-08-22 17:17:09 +000083 ePropertyEnableThreadTrace
84};
85
86
87class ThreadOptionValueProperties : public OptionValueProperties
88{
89public:
90 ThreadOptionValueProperties (const ConstString &name) :
91 OptionValueProperties (name)
92 {
93 }
94
95 // This constructor is used when creating ThreadOptionValueProperties when it
96 // is part of a new lldb_private::Thread instance. It will copy all current
97 // global property values as needed
98 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +000099 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +0000100 {
101 }
102
103 virtual const Property *
104 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
105 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000106 // When getting the value for a key from the thread options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +0000107 // try and grab the setting from the current thread if there is one. Else we just
108 // use the one from this instance.
109 if (exe_ctx)
110 {
111 Thread *thread = exe_ctx->GetThreadPtr();
112 if (thread)
113 {
114 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
115 if (this != instance_properties)
116 return instance_properties->ProtectedGetPropertyAtIndex (idx);
117 }
118 }
119 return ProtectedGetPropertyAtIndex (idx);
120 }
121};
122
123
124
125ThreadProperties::ThreadProperties (bool is_global) :
126 Properties ()
127{
128 if (is_global)
129 {
130 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
131 m_collection_sp->Initialize(g_properties);
132 }
133 else
134 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
135}
136
137ThreadProperties::~ThreadProperties()
138{
139}
140
141const RegularExpression *
142ThreadProperties::GetSymbolsToAvoidRegexp()
143{
144 const uint32_t idx = ePropertyStepAvoidRegex;
145 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
146}
147
Jim Ingham4da62062014-01-23 21:52:47 +0000148FileSpecList &
149ThreadProperties::GetLibrariesToAvoid() const
150{
151 const uint32_t idx = ePropertyStepAvoidLibraries;
152 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
153 assert(option_value);
154 return option_value->GetCurrentValue();
155}
156
Greg Clayton67cc0632012-08-22 17:17:09 +0000157bool
158ThreadProperties::GetTraceEnabledState() const
159{
160 const uint32_t idx = ePropertyEnableThreadTrace;
161 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
162}
163
Jim Ingham4b4b2472014-03-13 02:47:14 +0000164bool
165ThreadProperties::GetStepInAvoidsNoDebug() const
166{
167 const uint32_t idx = ePropertyStepInAvoidsNoDebug;
168 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
169}
170
171bool
172ThreadProperties::GetStepOutAvoidsNoDebug() const
173{
174 const uint32_t idx = ePropertyStepOutAvoidsNoDebug;
175 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
176}
177
178
Jim Ingham4f465cf2012-10-10 18:32:14 +0000179//------------------------------------------------------------------
180// Thread Event Data
181//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000182
Jim Ingham4f465cf2012-10-10 18:32:14 +0000183
184const ConstString &
185Thread::ThreadEventData::GetFlavorString ()
186{
187 static ConstString g_flavor ("Thread::ThreadEventData");
188 return g_flavor;
189}
190
191Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
192 m_thread_sp (thread_sp),
193 m_stack_id ()
194{
195}
196
197Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
198 m_thread_sp (thread_sp),
199 m_stack_id (stack_id)
200{
201}
202
203Thread::ThreadEventData::ThreadEventData () :
204 m_thread_sp (),
205 m_stack_id ()
206{
207}
208
209Thread::ThreadEventData::~ThreadEventData ()
210{
211}
212
213void
214Thread::ThreadEventData::Dump (Stream *s) const
215{
216
217}
218
219const Thread::ThreadEventData *
220Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
221{
222 if (event_ptr)
223 {
224 const EventData *event_data = event_ptr->GetData();
225 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
226 return static_cast <const ThreadEventData *> (event_ptr->GetData());
227 }
228 return NULL;
229}
230
231ThreadSP
232Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
233{
234 ThreadSP thread_sp;
235 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
236 if (event_data)
237 thread_sp = event_data->GetThread();
238 return thread_sp;
239}
240
241StackID
242Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
243{
244 StackID stack_id;
245 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
246 if (event_data)
247 stack_id = event_data->GetStackID();
248 return stack_id;
249}
250
Jason Molendab57e4a12013-11-04 09:33:30 +0000251StackFrameSP
Jim Ingham4f465cf2012-10-10 18:32:14 +0000252Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
253{
254 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
Jason Molendab57e4a12013-11-04 09:33:30 +0000255 StackFrameSP frame_sp;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000256 if (event_data)
257 {
258 ThreadSP thread_sp = event_data->GetThread();
259 if (thread_sp)
260 {
261 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
262 }
263 }
264 return frame_sp;
265}
266
267//------------------------------------------------------------------
268// Thread class
269//------------------------------------------------------------------
270
271ConstString &
272Thread::GetStaticBroadcasterClass ()
273{
274 static ConstString class_name ("lldb.thread");
275 return class_name;
276}
277
Jason Molendaa8ff5432014-03-06 06:31:18 +0000278Thread::Thread (Process &process, lldb::tid_t tid, bool use_invalid_index_id) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000279 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000281 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
282 m_process_wp (process.shared_from_this()),
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000283 m_stop_info_sp (),
284 m_stop_info_stop_id (0),
Greg Claytona97c4d22014-12-09 23:31:02 +0000285 m_stop_info_override_stop_id (0),
Jason Molendaa8ff5432014-03-06 06:31:18 +0000286 m_index_id (use_invalid_index_id ? LLDB_INVALID_INDEX32 : process.GetNextThreadIndexID(tid)),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000289 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000292 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000293 m_curr_frames_sp (),
294 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000296 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000297 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000298 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000299 m_destroy_called (false),
Jason Molenda705b1802014-06-13 02:37:02 +0000300 m_override_should_notify (eLazyBoolCalculate),
301 m_extended_info_fetched (false),
302 m_extended_info ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303{
Greg Clayton5160ce52013-03-27 23:08:40 +0000304 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000306 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")",
307 static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308
Jim Ingham4f465cf2012-10-10 18:32:14 +0000309 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310 QueueFundamentalPlan(true);
311}
312
313
314Thread::~Thread()
315{
Greg Clayton5160ce52013-03-27 23:08:40 +0000316 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000318 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")",
319 static_cast<void*>(this), GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000320 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
321 assert (m_destroy_called);
322}
323
324void
325Thread::DestroyThread ()
326{
Jim Inghamb1499242013-10-18 17:11:02 +0000327 // Tell any plans on the plan stacks that the thread is being destroyed since
328 // any plans that have a thread go away in the middle of might need
329 // to do cleanup, or in some cases NOT do cleanup...
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000330 for (auto plan : m_plan_stack)
331 plan->ThreadDestroyed();
332
Jim Inghamb1499242013-10-18 17:11:02 +0000333 for (auto plan : m_discarded_plan_stack)
334 plan->ThreadDestroyed();
335
336 for (auto plan : m_completed_plan_stack)
337 plan->ThreadDestroyed();
338
Greg Clayton35a4cc52012-10-29 20:52:08 +0000339 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000340 m_plan_stack.clear();
341 m_discarded_plan_stack.clear();
342 m_completed_plan_stack.clear();
Greg Clayton6e10f142013-07-30 00:23:06 +0000343
344 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the
345 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
346 // without checking first whether it is destroyed, they won't crash.
347 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
348 m_plan_stack.push_back (null_plan_sp);
349
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000350 m_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000351 m_reg_context_sp.reset();
352 m_unwinder_ap.reset();
353 Mutex::Locker locker(m_frame_mutex);
354 m_curr_frames_sp.reset();
355 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000356}
357
Jim Ingham4f465cf2012-10-10 18:32:14 +0000358void
359Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
360{
361 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
362 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
363}
364
365uint32_t
Jason Molendab57e4a12013-11-04 09:33:30 +0000366Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
Jim Ingham4f465cf2012-10-10 18:32:14 +0000367{
368 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
369 if (broadcast)
370 BroadcastSelectedFrameChange(frame->GetStackID());
371 return ret_value;
372}
373
374bool
375Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
376{
Jason Molendab57e4a12013-11-04 09:33:30 +0000377 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
Jim Ingham4f465cf2012-10-10 18:32:14 +0000378 if (frame_sp)
379 {
380 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
381 if (broadcast)
382 BroadcastSelectedFrameChange(frame_sp->GetStackID());
383 return true;
384 }
385 else
386 return false;
387}
388
Jim Ingham93208b82013-01-31 21:46:01 +0000389bool
390Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
391{
392 const bool broadcast = true;
393 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
394 if (success)
395 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000396 StackFrameSP frame_sp = GetSelectedFrame();
Jim Ingham93208b82013-01-31 21:46:01 +0000397 if (frame_sp)
398 {
399 bool already_shown = false;
400 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
401 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
402 {
403 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
404 }
405
406 bool show_frame_info = true;
407 bool show_source = !already_shown;
408 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
409 }
410 return false;
411 }
412 else
413 return false;
414}
415
Jim Ingham4f465cf2012-10-10 18:32:14 +0000416
Jim Inghamb15bfc72010-10-20 00:39:53 +0000417lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000418Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419{
Greg Clayton6e10f142013-07-30 00:23:06 +0000420 if (m_destroy_called)
421 return m_stop_info_sp;
422
Jim Inghamb15bfc72010-10-20 00:39:53 +0000423 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000424 ProcessSP process_sp (GetProcess());
425 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000426 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000427 {
Jim Ingham30fadaf2014-07-08 01:07:32 +0000428 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject(), GetExpressionVariable());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000429 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000430 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000431 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000432 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
433 (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 +0000434 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000435 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000436 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000437 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000438 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000439 GetPrivateStopInfo ();
440 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000441 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000442 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443}
444
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000445lldb::StopInfoSP
446Thread::GetPrivateStopInfo ()
447{
Greg Clayton6e10f142013-07-30 00:23:06 +0000448 if (m_destroy_called)
449 return m_stop_info_sp;
450
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000451 ProcessSP process_sp (GetProcess());
452 if (process_sp)
453 {
Daniel Malea246cb612013-05-14 15:20:12 +0000454 const uint32_t process_stop_id = process_sp->GetStopID();
455 if (m_stop_info_stop_id != process_stop_id)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000456 {
Daniel Malea246cb612013-05-14 15:20:12 +0000457 if (m_stop_info_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000458 {
Daniel Malea246cb612013-05-14 15:20:12 +0000459 if (m_stop_info_sp->IsValid()
460 || IsStillAtLastBreakpointHit()
461 || GetCurrentPlan()->IsVirtualStep())
462 SetStopInfo (m_stop_info_sp);
463 else
464 m_stop_info_sp.reset();
465 }
466
467 if (!m_stop_info_sp)
468 {
469 if (CalculateStopInfo() == false)
470 SetStopInfo (StopInfoSP());
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000471 }
472 }
Greg Claytona97c4d22014-12-09 23:31:02 +0000473
474 // The stop info can be manually set by calling Thread::SetStopInfo()
475 // prior to this function ever getting called, so we can't rely on
476 // "m_stop_info_stop_id != process_stop_id" as the condition for
477 // the if statement below, we must also check the stop info to see
478 // if we need to override it. See the header documentation in
479 // Process::GetStopInfoOverrideCallback() for more information on
480 // the stop info override callback.
481 if (m_stop_info_override_stop_id != process_stop_id)
482 {
483 m_stop_info_override_stop_id = process_stop_id;
484 if (m_stop_info_sp)
485 {
486 ArchSpec::StopInfoOverrideCallbackType callback = GetProcess()->GetStopInfoOverrideCallback();
487 if (callback)
488 callback(*this);
489 }
490 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000491 }
492 return m_stop_info_sp;
493}
494
495
Greg Clayton97d5cf02012-09-25 02:40:06 +0000496lldb::StopReason
497Thread::GetStopReason()
498{
499 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
500 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000501 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000502 return eStopReasonNone;
503}
504
505
506
Jim Ingham77787032011-01-20 02:03:18 +0000507void
508Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
509{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000510 m_stop_info_sp = stop_info_sp;
511 if (m_stop_info_sp)
Jim Ingham221d51c2013-05-08 00:35:16 +0000512 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000513 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham221d51c2013-05-08 00:35:16 +0000514 // If we are overriding the ShouldReportStop, do that here:
515 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000516 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000517 }
518
Greg Clayton1ac04c32012-02-21 00:09:25 +0000519 ProcessSP process_sp (GetProcess());
520 if (process_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000521 m_stop_info_stop_id = process_sp->GetStopID();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000522 else
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000523 m_stop_info_stop_id = UINT32_MAX;
Greg Clayton8cda7f02013-05-21 21:55:59 +0000524 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
525 if (log)
Ed Maste2bc76432014-06-25 00:38:35 +0000526 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000527 static_cast<void*>(this), GetID(),
528 stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>",
529 m_stop_info_stop_id);
Jim Ingham77787032011-01-20 02:03:18 +0000530}
531
532void
Jim Ingham221d51c2013-05-08 00:35:16 +0000533Thread::SetShouldReportStop (Vote vote)
534{
535 if (vote == eVoteNoOpinion)
536 return;
537 else
538 {
539 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000540 if (m_stop_info_sp)
541 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000542 }
543}
544
545void
Jim Ingham77787032011-01-20 02:03:18 +0000546Thread::SetStopInfoToNothing()
547{
548 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
549 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
550 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
551}
552
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553bool
554Thread::ThreadStoppedForAReason (void)
555{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000556 return (bool) GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557}
558
Jim Ingham77787032011-01-20 02:03:18 +0000559bool
560Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
561{
Greg Claytonf74cf862013-11-13 23:28:31 +0000562 saved_state.register_backup_sp.reset();
563 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
564 if (frame_sp)
565 {
566 lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
567 if (reg_checkpoint_sp)
568 {
569 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
570 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp))
571 saved_state.register_backup_sp = reg_checkpoint_sp;
572 }
573 }
574 if (!saved_state.register_backup_sp)
Jim Ingham77787032011-01-20 02:03:18 +0000575 return false;
576
577 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000578 ProcessSP process_sp (GetProcess());
579 if (process_sp)
580 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000581 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
582
Jim Ingham77787032011-01-20 02:03:18 +0000583 return true;
584}
585
586bool
Jim Ingham8559a352012-11-26 23:52:18 +0000587Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000588{
Greg Claytonf74cf862013-11-13 23:28:31 +0000589 if (saved_state.register_backup_sp)
590 {
591 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
592 if (frame_sp)
593 {
594 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
595 if (reg_ctx_sp)
596 {
597 bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp);
598
599 // Clear out all stack frames as our world just changed.
600 ClearStackFrames();
601 reg_ctx_sp->InvalidateIfNeeded(true);
602 if (m_unwinder_ap.get())
603 m_unwinder_ap->Clear();
604 return ret;
605 }
606 }
607 }
608 return false;
Jim Ingham8559a352012-11-26 23:52:18 +0000609}
610
611bool
612Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
613{
Jim Ingham0c270682011-01-25 02:47:23 +0000614 if (saved_state.stop_info_sp)
615 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000616 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000617 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000618 return true;
619}
620
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621StateType
622Thread::GetState() const
623{
624 // If any other threads access this we will need a mutex for it
625 Mutex::Locker locker(m_state_mutex);
626 return m_state;
627}
628
629void
630Thread::SetState(StateType state)
631{
632 Mutex::Locker locker(m_state_mutex);
633 m_state = state;
634}
635
636void
637Thread::WillStop()
638{
639 ThreadPlan *current_plan = GetCurrentPlan();
640
641 // FIXME: I may decide to disallow threads with no plans. In which
642 // case this should go to an assert.
643
644 if (!current_plan)
645 return;
646
647 current_plan->WillStop();
648}
649
650void
651Thread::SetupForResume ()
652{
653 if (GetResumeState() != eStateSuspended)
654 {
655
656 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
657 // telling the current plan it will resume, since we might change what the current
658 // plan is.
659
Greg Clayton1afa68e2013-04-02 20:32:37 +0000660// StopReason stop_reason = lldb::eStopReasonInvalid;
661// StopInfoSP stop_info_sp = GetStopInfo();
662// if (stop_info_sp.get())
663// stop_reason = stop_info_sp->GetStopReason();
664// if (stop_reason == lldb::eStopReasonBreakpoint)
665 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
666 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667 {
Greg Claytona97c4d22014-12-09 23:31:02 +0000668 const addr_t thread_pc = reg_ctx_sp->GetPC();
669 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000670 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000671 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000672 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
673 // special to step over a breakpoint.
674
675 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000676
Greg Claytona97c4d22014-12-09 23:31:02 +0000677 bool push_step_over_bp_plan = false;
678 if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint)
679 {
680 ThreadPlanStepOverBreakpoint *bp_plan = (ThreadPlanStepOverBreakpoint *)cur_plan;
681 if (bp_plan->GetBreakpointLoadAddress() != thread_pc)
682 push_step_over_bp_plan = true;
683 }
684 else
685 push_step_over_bp_plan = true;
686
687 if (push_step_over_bp_plan)
Greg Clayton1afa68e2013-04-02 20:32:37 +0000688 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000689 ThreadPlanSP step_bp_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
690 if (step_bp_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000692 ;
693 step_bp_plan_sp->SetPrivate (true);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000694
695 if (GetCurrentPlan()->RunState() != eStateStepping)
696 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000697 ThreadPlanStepOverBreakpoint *step_bp_plan
698 = static_cast<ThreadPlanStepOverBreakpoint *>(step_bp_plan_sp.get());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000699 step_bp_plan->SetAutoContinue(true);
700 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000701 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 }
704 }
705 }
706 }
707}
708
709bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000710Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711{
712 // At this point clear the completed plan stack.
713 m_completed_plan_stack.clear();
714 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000715 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716
Greg Clayton97d5cf02012-09-25 02:40:06 +0000717 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000718
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000719 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
720 if (backing_thread_sp)
721 backing_thread_sp->m_temporary_resume_state = resume_state;
722
723 // Make sure m_stop_info_sp is valid
724 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000725
Jim Ingham92087d82012-01-31 23:09:20 +0000726 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
727 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
728 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
729 // about the fact that we are resuming...
Greg Clayton1ac04c32012-02-21 00:09:25 +0000730 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000731 if (m_stop_info_stop_id == process_stop_id &&
732 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000733 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000734 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000735 if (stop_info)
736 stop_info->WillResume (resume_state);
737 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738
739 // Tell all the plans that we are about to resume in case they need to clear any state.
740 // We distinguish between the plan on the top of the stack and the lower
741 // plans in case a plan needs to do any special business before it runs.
742
Greg Claytond1d06e42013-04-20 00:27:58 +0000743 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000745 if (plan_ptr)
746 {
747 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748
Greg Claytond1d06e42013-04-20 00:27:58 +0000749 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
750 {
751 plan_ptr->WillResume (resume_state, false);
752 }
753
754 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
755 // In that case, don't reset it here.
756
757 if (need_to_resume && resume_state != eStateSuspended)
758 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000759 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000760 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000761 }
762
Greg Clayton160c9d82013-05-01 21:54:04 +0000763 if (need_to_resume)
764 {
765 ClearStackFrames();
766 // Let Thread subclasses do any special work they need to prior to resuming
767 WillResume (resume_state);
768 }
769
Jim Ingham513c6bb2012-09-01 01:02:41 +0000770 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771}
772
773void
774Thread::DidResume ()
775{
776 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
777}
778
Andrew Kaylor29d65742013-05-10 17:19:04 +0000779void
780Thread::DidStop ()
781{
782 SetState (eStateStopped);
783}
784
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785bool
786Thread::ShouldStop (Event* event_ptr)
787{
788 ThreadPlan *current_plan = GetCurrentPlan();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000789
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790 bool should_stop = true;
791
Greg Clayton5160ce52013-03-27 23:08:40 +0000792 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000793
Jim Ingham92087d82012-01-31 23:09:20 +0000794 if (GetResumeState () == eStateSuspended)
795 {
796 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000797 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 +0000798 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000799 return false;
800 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000801
Jim Ingham92087d82012-01-31 23:09:20 +0000802 if (GetTemporaryResumeState () == eStateSuspended)
803 {
804 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000805 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 +0000806 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000807 return false;
808 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000809
Daniel Malea246cb612013-05-14 15:20:12 +0000810 // Based on the current thread plan and process stop info, check if this
811 // thread caused the process to stop. NOTE: this must take place before
812 // the plan is moved from the current plan stack to the completed plan
813 // stack.
Jim Ingham92087d82012-01-31 23:09:20 +0000814 if (ThreadStoppedForAReason() == false)
815 {
816 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000817 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 +0000818 __FUNCTION__, GetID (), GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000819 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000820 return false;
821 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000822
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000823 if (log)
824 {
Jim Inghamdee1bc92013-06-22 00:27:45 +0000825 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 +0000826 __FUNCTION__, static_cast<void*>(this), GetID (),
Greg Clayton160c9d82013-05-01 21:54:04 +0000827 GetProtocolID (),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000828 GetRegisterContext()
829 ? GetRegisterContext()->GetPC()
830 : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000831 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000833 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000835 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000837
Jim Ingham06e827c2010-11-11 19:26:09 +0000838 // The top most plan always gets to do the trace log...
839 current_plan->DoTraceLog ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000840
Jim Ingham6d66ce62012-04-20 21:16:56 +0000841 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
842 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
843 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000844 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Ingham6d66ce62012-04-20 21:16:56 +0000845 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
846 {
847 if (log)
848 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
849 return false;
850 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851
Jim Inghambad39e42012-09-05 21:12:49 +0000852 // If we've already been restarted, don't query the plans since the state they would examine is not current.
853 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
854 return false;
855
856 // Before the plans see the state of the world, calculate the current inlined depth.
857 GetStackFrameList()->CalculateCurrentInlinedDepth();
858
Jim Ingham25f66702011-12-03 01:52:59 +0000859 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
860 // If that plan is still working, then we don't need to do any more work. If the plan that explains
861 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
862 // whether they still need to do more work.
863
864 bool done_processing_current_plan = false;
865
Jim Ingham0161b492013-02-09 01:29:05 +0000866 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000867 {
868 if (current_plan->TracerExplainsStop())
869 {
870 done_processing_current_plan = true;
871 should_stop = false;
872 }
873 else
874 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000875 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000876 // does and let it handle the situation.
877 ThreadPlan *plan_ptr = current_plan;
878 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
879 {
Jim Ingham0161b492013-02-09 01:29:05 +0000880 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000881 {
882 should_stop = plan_ptr->ShouldStop (event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000883
Jim Ingham25f66702011-12-03 01:52:59 +0000884 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
885 // and all the plans below it off the stack.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000886
Jim Ingham25f66702011-12-03 01:52:59 +0000887 if (plan_ptr->MischiefManaged())
888 {
Jim Ingham031177e2012-05-11 23:49:49 +0000889 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000890 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000891
Jim Ingham25f66702011-12-03 01:52:59 +0000892 do
893 {
894 if (should_stop)
895 current_plan->WillStop();
896 PopPlan();
897 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000898 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
899 // Now, if the responsible plan was not "Okay to discard" then we're done,
900 // otherwise we forward this to the next plan in the stack below.
901 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
902 done_processing_current_plan = true;
903 else
904 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000905 }
906 else
907 done_processing_current_plan = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000908
Jim Ingham25f66702011-12-03 01:52:59 +0000909 break;
910 }
911
912 }
913 }
914 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000915
Jim Ingham25f66702011-12-03 01:52:59 +0000916 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000917 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000918 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000919
Jim Ingham10c4b242011-10-15 00:23:43 +0000920 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000921 log->Printf("Plan %s explains stop, auto-continue %i.",
922 current_plan->GetName(), over_ride_stop);
923
Jim Ingham0f16e732011-02-08 05:20:59 +0000924 // We're starting from the base plan, so just let it decide;
925 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000926 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000927 should_stop = current_plan->ShouldStop (event_ptr);
928 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000929 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000930 }
931 else
932 {
933 // Otherwise, don't let the base plan override what the other plans say to do, since
934 // presumably if there were other plans they would know what to do...
935 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000936 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000937 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000938 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000939
Jim Ingham0f16e732011-02-08 05:20:59 +0000940 should_stop = current_plan->ShouldStop(event_ptr);
941 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000942 log->Printf("Plan %s should stop: %d.",
943 current_plan->GetName(), should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000944 if (current_plan->MischiefManaged())
945 {
946 if (should_stop)
947 current_plan->WillStop();
948
949 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
950 // Otherwise, see if the plan's parent wants to stop.
951
952 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
953 {
954 PopPlan();
955 break;
956 }
957 else
958 {
959
960 PopPlan();
961
962 current_plan = GetCurrentPlan();
963 if (current_plan == NULL)
964 {
965 break;
966 }
967 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000968 }
969 else
970 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000971 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000973 }
974 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000975
Jim Inghamb01e7422010-06-19 04:45:32 +0000976 if (over_ride_stop)
977 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000978
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000979 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000980
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000981 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
982 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
983 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
984 // This code clears stale plans off the stack.
985
986 if (should_stop)
987 {
988 ThreadPlan *plan_ptr = GetCurrentPlan();
989 while (!PlanIsBasePlan(plan_ptr))
Jim Ingham64e7ead2012-05-03 21:19:36 +0000990 {
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000991 bool stale = plan_ptr->IsPlanStale ();
992 ThreadPlan *examined_plan = plan_ptr;
993 plan_ptr = GetPreviousPlan (examined_plan);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000994
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000995 if (stale)
996 {
997 if (log)
998 log->Printf("Plan %s being discarded in cleanup, it says it is already done.",
999 examined_plan->GetName());
1000 DiscardThreadPlansUpToPlan(examined_plan);
Jim Ingham64e7ead2012-05-03 21:19:36 +00001001 }
1002 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004
Jim Ingham10c4b242011-10-15 00:23:43 +00001005 if (log)
1006 {
1007 StreamString s;
1008 s.IndentMore();
1009 DumpThreadPlans(&s);
1010 log->Printf ("Plan stack final state:\n%s", s.GetData());
1011 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
1012 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013 return should_stop;
1014}
1015
1016Vote
1017Thread::ShouldReportStop (Event* event_ptr)
1018{
1019 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +00001020 StateType temp_thread_state = GetTemporaryResumeState();
1021
Greg Clayton5160ce52013-03-27 23:08:40 +00001022 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +00001023
1024 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
1025 {
1026 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001027 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 +00001028 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001029 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030
Jim Ingham92087d82012-01-31 23:09:20 +00001031 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
1032 {
1033 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001034 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 +00001035 return eVoteNoOpinion;
1036 }
1037
1038 if (!ThreadStoppedForAReason())
1039 {
1040 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001041 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 +00001042 return eVoteNoOpinion;
1043 }
1044
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001045 if (m_completed_plan_stack.size() > 0)
1046 {
1047 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +00001048 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001049 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 +00001050 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
1051 }
1052 else
Greg Clayton2cad65a2010-09-03 17:10:42 +00001053 {
Jim Ingham0161b492013-02-09 01:29:05 +00001054 Vote thread_vote = eVoteNoOpinion;
1055 ThreadPlan *plan_ptr = GetCurrentPlan();
1056 while (1)
1057 {
1058 if (plan_ptr->PlanExplainsStop(event_ptr))
1059 {
1060 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1061 break;
1062 }
1063 if (PlanIsBasePlan(plan_ptr))
1064 break;
1065 else
1066 plan_ptr = GetPreviousPlan(plan_ptr);
1067 }
Greg Clayton2cad65a2010-09-03 17:10:42 +00001068 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001069 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 +00001070
1071 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001072 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073}
1074
1075Vote
1076Thread::ShouldReportRun (Event* event_ptr)
1077{
1078 StateType thread_state = GetResumeState ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001079
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080 if (thread_state == eStateSuspended
1081 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +00001082 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +00001084 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001085
Greg Clayton5160ce52013-03-27 23:08:40 +00001086 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087 if (m_completed_plan_stack.size() > 0)
1088 {
1089 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +00001090 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001091 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 +00001092 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001093 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001094 m_completed_plan_stack.back()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001095
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
1097 }
1098 else
Jim Ingham444586b2011-01-24 06:34:17 +00001099 {
1100 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001101 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 +00001102 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001103 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001104 GetCurrentPlan()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001105
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +00001107 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001108}
1109
Jim Ingham1b54c882010-06-16 02:00:15 +00001110bool
1111Thread::MatchesSpec (const ThreadSpec *spec)
1112{
1113 if (spec == NULL)
1114 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +00001115
Jim Ingham3d902922012-03-07 22:03:04 +00001116 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +00001117}
1118
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119void
1120Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1121{
1122 if (thread_plan_sp)
1123 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001124 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1125 if (!thread_plan_sp->GetThreadPlanTracer())
1126 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001127 m_plan_stack.push_back (thread_plan_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001128
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129 thread_plan_sp->DidPush();
1130
Greg Clayton5160ce52013-03-27 23:08:40 +00001131 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132 if (log)
1133 {
1134 StreamString s;
1135 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamdee1bc92013-06-22 00:27:45 +00001136 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001137 static_cast<void*>(this), s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001138 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139 }
1140 }
1141}
1142
1143void
1144Thread::PopPlan ()
1145{
Greg Clayton5160ce52013-03-27 23:08:40 +00001146 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001148 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149 return;
1150 else
1151 {
1152 ThreadPlanSP &plan = m_plan_stack.back();
1153 if (log)
1154 {
Daniel Malead01b2952012-11-29 21:49:15 +00001155 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156 }
1157 m_completed_plan_stack.push_back (plan);
1158 plan->WillPop();
1159 m_plan_stack.pop_back();
1160 }
1161}
1162
1163void
1164Thread::DiscardPlan ()
1165{
Jim Inghamdee1bc92013-06-22 00:27:45 +00001166 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167 if (m_plan_stack.size() > 1)
1168 {
1169 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamdee1bc92013-06-22 00:27:45 +00001170 if (log)
1171 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1172
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173 m_discarded_plan_stack.push_back (plan);
1174 plan->WillPop();
1175 m_plan_stack.pop_back();
1176 }
1177}
1178
1179ThreadPlan *
1180Thread::GetCurrentPlan ()
1181{
Jim Inghame1471232012-04-19 00:17:05 +00001182 // There will always be at least the base plan. If somebody is mucking with a
1183 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +00001184 if (m_plan_stack.empty())
1185 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +00001186 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187}
1188
1189ThreadPlanSP
1190Thread::GetCompletedPlan ()
1191{
1192 ThreadPlanSP empty_plan_sp;
1193 if (!m_completed_plan_stack.empty())
1194 {
1195 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1196 {
1197 ThreadPlanSP completed_plan_sp;
1198 completed_plan_sp = m_completed_plan_stack[i];
1199 if (!completed_plan_sp->GetPrivate ())
1200 return completed_plan_sp;
1201 }
1202 }
1203 return empty_plan_sp;
1204}
1205
Jim Ingham73ca05a2011-12-17 01:35:57 +00001206ValueObjectSP
1207Thread::GetReturnValueObject ()
1208{
1209 if (!m_completed_plan_stack.empty())
1210 {
1211 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1212 {
1213 ValueObjectSP return_valobj_sp;
1214 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1215 if (return_valobj_sp)
1216 return return_valobj_sp;
1217 }
1218 }
1219 return ValueObjectSP();
1220}
1221
Jim Ingham30fadaf2014-07-08 01:07:32 +00001222ClangExpressionVariableSP
1223Thread::GetExpressionVariable ()
1224{
1225 if (!m_completed_plan_stack.empty())
1226 {
1227 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1228 {
1229 ClangExpressionVariableSP expression_variable_sp;
1230 expression_variable_sp = m_completed_plan_stack[i]->GetExpressionVariable();
1231 if (expression_variable_sp)
1232 return expression_variable_sp;
1233 }
1234 }
1235 return ClangExpressionVariableSP();
1236}
1237
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001238bool
1239Thread::IsThreadPlanDone (ThreadPlan *plan)
1240{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001241 if (!m_completed_plan_stack.empty())
1242 {
1243 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1244 {
1245 if (m_completed_plan_stack[i].get() == plan)
1246 return true;
1247 }
1248 }
1249 return false;
1250}
1251
1252bool
1253Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1254{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255 if (!m_discarded_plan_stack.empty())
1256 {
1257 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1258 {
1259 if (m_discarded_plan_stack[i].get() == plan)
1260 return true;
1261 }
1262 }
1263 return false;
1264}
1265
1266ThreadPlan *
1267Thread::GetPreviousPlan (ThreadPlan *current_plan)
1268{
1269 if (current_plan == NULL)
1270 return NULL;
1271
1272 int stack_size = m_completed_plan_stack.size();
1273 for (int i = stack_size - 1; i > 0; i--)
1274 {
1275 if (current_plan == m_completed_plan_stack[i].get())
1276 return m_completed_plan_stack[i-1].get();
1277 }
1278
1279 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1280 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001281 if (m_plan_stack.size() > 0)
1282 return m_plan_stack.back().get();
1283 else
1284 return NULL;
1285 }
1286
1287 stack_size = m_plan_stack.size();
1288 for (int i = stack_size - 1; i > 0; i--)
1289 {
1290 if (current_plan == m_plan_stack[i].get())
1291 return m_plan_stack[i-1].get();
1292 }
1293 return NULL;
1294}
1295
1296void
1297Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1298{
1299 if (abort_other_plans)
1300 DiscardThreadPlans(true);
1301
1302 PushPlan (thread_plan_sp);
1303}
1304
Jim Ingham06e827c2010-11-11 19:26:09 +00001305
1306void
1307Thread::EnableTracer (bool value, bool single_stepping)
1308{
1309 int stack_size = m_plan_stack.size();
1310 for (int i = 0; i < stack_size; i++)
1311 {
1312 if (m_plan_stack[i]->GetThreadPlanTracer())
1313 {
1314 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1315 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1316 }
1317 }
1318}
1319
1320void
1321Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1322{
1323 int stack_size = m_plan_stack.size();
1324 for (int i = 0; i < stack_size; i++)
1325 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1326}
1327
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001328bool
1329Thread::DiscardUserThreadPlansUpToIndex (uint32_t thread_index)
1330{
1331 // Count the user thread plans from the back end to get the number of the one we want
1332 // to discard:
1333
1334 uint32_t idx = 0;
1335 ThreadPlan *up_to_plan_ptr = nullptr;
1336
1337 for (ThreadPlanSP plan_sp : m_plan_stack)
1338 {
1339 if (plan_sp->GetPrivate())
1340 continue;
1341 if (idx == thread_index)
1342 {
1343 up_to_plan_ptr = plan_sp.get();
1344 break;
1345 }
1346 else
1347 idx++;
1348 }
1349
1350 if (up_to_plan_ptr == nullptr)
1351 return false;
1352
1353 DiscardThreadPlansUpToPlan(up_to_plan_ptr);
1354 return true;
1355}
1356
1357
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001359Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1360{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001361 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1362}
1363
1364void
1365Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1366{
Greg Clayton5160ce52013-03-27 23:08:40 +00001367 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001368 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001369 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p",
1370 GetID(), static_cast<void*>(up_to_plan_ptr));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001371
1372 int stack_size = m_plan_stack.size();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001373
Jim Ingham399f1ca2010-11-05 19:25:48 +00001374 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1375 // stack, and if so discard up to and including it.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001376
Jim Ingham64e7ead2012-05-03 21:19:36 +00001377 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001378 {
1379 for (int i = stack_size - 1; i > 0; i--)
1380 DiscardPlan();
1381 }
1382 else
1383 {
1384 bool found_it = false;
1385 for (int i = stack_size - 1; i > 0; i--)
1386 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001387 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001388 found_it = true;
1389 }
1390 if (found_it)
1391 {
1392 bool last_one = false;
1393 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1394 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001395 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001396 last_one = true;
1397 DiscardPlan();
1398 }
1399 }
1400 }
1401 return;
1402}
1403
1404void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405Thread::DiscardThreadPlans(bool force)
1406{
Greg Clayton5160ce52013-03-27 23:08:40 +00001407 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 if (log)
1409 {
Daniel Malead01b2952012-11-29 21:49:15 +00001410 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411 }
1412
1413 if (force)
1414 {
1415 int stack_size = m_plan_stack.size();
1416 for (int i = stack_size - 1; i > 0; i--)
1417 {
1418 DiscardPlan();
1419 }
1420 return;
1421 }
1422
1423 while (1)
1424 {
1425
1426 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001427 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428
1429 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1430 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1431 {
1432 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1433 {
1434 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1435 break;
1436 }
1437 }
1438
1439 if (discard)
1440 {
1441 // First pop all the dependent plans:
1442 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1443 {
1444
1445 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1446 // for the plan leaves it in a state that it is safe to pop the plan
1447 // with no more notice?
1448 DiscardPlan();
1449 }
1450
1451 // Now discard the master plan itself.
1452 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1453 // discard it's dependent plans, but not it...
1454 if (master_plan_idx > 0)
1455 {
1456 DiscardPlan();
1457 }
1458 }
1459 else
1460 {
1461 // If the master plan doesn't want to get discarded, then we're done.
1462 break;
1463 }
1464
1465 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001466}
1467
Jim Inghamcf274f92012-04-09 22:37:39 +00001468bool
1469Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1470{
1471 if (plan_ptr->IsBasePlan())
1472 return true;
1473 else if (m_plan_stack.size() == 0)
1474 return false;
1475 else
1476 return m_plan_stack[0].get() == plan_ptr;
1477}
1478
Jim Ingham93208b82013-01-31 21:46:01 +00001479Error
1480Thread::UnwindInnermostExpression()
1481{
1482 Error error;
1483 int stack_size = m_plan_stack.size();
1484
1485 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1486 // stack, and if so discard up to and including it.
1487
1488 for (int i = stack_size - 1; i > 0; i--)
1489 {
1490 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1491 {
1492 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1493 return error;
1494 }
1495 }
1496 error.SetErrorString("No expressions currently active on this thread");
1497 return error;
1498}
1499
1500
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001501ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502Thread::QueueFundamentalPlan (bool abort_other_plans)
1503{
1504 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1505 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001506 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001507}
1508
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001509ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001510Thread::QueueThreadPlanForStepSingleInstruction
1511(
1512 bool step_over,
1513 bool abort_other_plans,
1514 bool stop_other_threads
1515)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001516{
1517 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1518 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001519 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001520}
1521
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001522ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001523Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001524(
1525 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001526 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001527 const SymbolContext &addr_context,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001528 lldb::RunMode stop_other_threads,
1529 LazyBool step_out_avoids_code_withoug_debug_info
Jim Inghamc6276822012-12-12 19:58:40 +00001530)
1531{
1532 ThreadPlanSP thread_plan_sp;
Jim Ingham4b4b2472014-03-13 02:47:14 +00001533 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads, step_out_avoids_code_withoug_debug_info));
1534
Jim Inghamc6276822012-12-12 19:58:40 +00001535 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001536 return thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001537}
1538
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001539ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001540Thread::QueueThreadPlanForStepInRange
1541(
1542 bool abort_other_plans,
1543 const AddressRange &range,
1544 const SymbolContext &addr_context,
1545 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001546 lldb::RunMode stop_other_threads,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001547 LazyBool step_in_avoids_code_without_debug_info,
1548 LazyBool step_out_avoids_code_without_debug_info
Greg Clayton474966a2010-06-12 18:59:55 +00001549)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001550{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001551 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInRange (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001552 range,
1553 addr_context,
1554 stop_other_threads,
1555 step_in_avoids_code_without_debug_info,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001556 step_out_avoids_code_without_debug_info));
1557 ThreadPlanStepInRange *plan = static_cast<ThreadPlanStepInRange *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001558
Jim Inghamc6276822012-12-12 19:58:40 +00001559 if (step_in_target)
1560 plan->SetStepInTarget(step_in_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001561
1562 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001563 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001564}
1565
1566
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001567ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001568Thread::QueueThreadPlanForStepOut
1569(
1570 bool abort_other_plans,
1571 SymbolContext *addr_context,
1572 bool first_insn,
1573 bool stop_other_threads,
1574 Vote stop_vote,
1575 Vote run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001576 uint32_t frame_idx,
1577 LazyBool step_out_avoids_code_withoug_debug_info
Greg Clayton481cef22011-01-21 06:11:58 +00001578)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579{
Greg Clayton481cef22011-01-21 06:11:58 +00001580 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1581 addr_context,
1582 first_insn,
1583 stop_other_threads,
1584 stop_vote,
1585 run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001586 frame_idx,
1587 step_out_avoids_code_withoug_debug_info));
1588
1589 if (thread_plan_sp->ValidatePlan(NULL))
1590 {
1591 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1592 return thread_plan_sp;
1593 }
1594 else
1595 {
1596 return ThreadPlanSP();
1597 }
1598}
1599
1600ThreadPlanSP
1601Thread::QueueThreadPlanForStepOutNoShouldStop
1602(
1603 bool abort_other_plans,
1604 SymbolContext *addr_context,
1605 bool first_insn,
1606 bool stop_other_threads,
1607 Vote stop_vote,
1608 Vote run_vote,
1609 uint32_t frame_idx
1610)
1611{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001612 ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001613 addr_context,
1614 first_insn,
1615 stop_other_threads,
1616 stop_vote,
1617 run_vote,
1618 frame_idx,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001619 eLazyBoolNo));
1620
1621 ThreadPlanStepOut *new_plan = static_cast<ThreadPlanStepOut *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001622 new_plan->ClearShouldStopHereCallbacks();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001623
Sean Callanan708709c2012-07-31 22:19:25 +00001624 if (thread_plan_sp->ValidatePlan(NULL))
1625 {
1626 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001627 return thread_plan_sp;
Sean Callanan708709c2012-07-31 22:19:25 +00001628 }
1629 else
1630 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001631 return ThreadPlanSP();
Sean Callanan708709c2012-07-31 22:19:25 +00001632 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001633}
1634
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001635ThreadPlanSP
Jim Ingham18de2fd2012-05-10 01:35:39 +00001636Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001637{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001638 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001639 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001640 return ThreadPlanSP();
Jim Ingham25f66702011-12-03 01:52:59 +00001641
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001642 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001643 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001644}
1645
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001646ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001647Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1648 Address &target_addr,
1649 bool stop_other_threads)
1650{
1651 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1652 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001653 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001654}
1655
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001656ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001657Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001658 lldb::addr_t *address_list,
1659 size_t num_addresses,
1660 bool stop_other_threads,
1661 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001662{
Greg Clayton481cef22011-01-21 06:11:58 +00001663 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001664 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001665 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001666
1667}
1668
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001669lldb::ThreadPlanSP
1670Thread::QueueThreadPlanForStepScripted (bool abort_other_plans,
1671 const char *class_name,
1672 bool stop_other_threads)
1673{
1674 ThreadPlanSP thread_plan_sp (new ThreadPlanPython (*this, class_name));
1675 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1676 // This seems a little funny, but I don't want to have to split up the constructor and the
1677 // DidPush in the scripted plan, that seems annoying.
1678 // That means the constructor has to be in DidPush.
1679 // So I have to validate the plan AFTER pushing it, and then take it off again...
1680 if (!thread_plan_sp->ValidatePlan(nullptr))
1681 {
1682 DiscardThreadPlansUpToPlan(thread_plan_sp);
1683 return ThreadPlanSP();
1684 }
1685 else
1686 return thread_plan_sp;
1687
1688}
1689
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001690uint32_t
1691Thread::GetIndexID () const
1692{
1693 return m_index_id;
1694}
1695
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001696static void
1697PrintPlanElement (Stream *s, const ThreadPlanSP &plan, lldb::DescriptionLevel desc_level, int32_t elem_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001698{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001700 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001701 s->Printf ("Element %d: ", elem_idx);
1702 plan->GetDescription (s, desc_level);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001703 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001704 s->IndentLess();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001705}
1706
1707static void
1708PrintPlanStack (Stream *s, const std::vector<lldb::ThreadPlanSP> &plan_stack, lldb::DescriptionLevel desc_level, bool include_internal)
1709{
1710 int32_t print_idx = 0;
1711 for (ThreadPlanSP plan_sp : plan_stack)
1712 {
1713 if (include_internal || !plan_sp->GetPrivate())
1714 {
1715 PrintPlanElement (s, plan_sp, desc_level, print_idx++);
1716 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001717 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001718}
1719
1720void
1721Thread::DumpThreadPlans (Stream *s,
1722 lldb::DescriptionLevel desc_level,
1723 bool include_internal,
1724 bool ignore_boring_threads) const
1725{
Jason Molenda60b5da62014-10-16 07:53:46 +00001726 uint32_t stack_size;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001727
1728 if (ignore_boring_threads)
1729 {
1730 uint32_t stack_size = m_plan_stack.size();
1731 uint32_t completed_stack_size = m_completed_plan_stack.size();
1732 uint32_t discarded_stack_size = m_discarded_plan_stack.size();
1733 if (stack_size == 1 && completed_stack_size == 0 && discarded_stack_size == 0)
1734 {
1735 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 "\n", GetIndexID(), GetID());
1736 s->IndentMore();
1737 s->Indent();
1738 s->Printf("No active thread plans\n");
1739 s->IndentLess();
1740 return;
1741 }
1742 }
1743
1744 s->Indent();
1745 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 ":\n", GetIndexID(), GetID());
1746 s->IndentMore();
1747 s->Indent();
1748 s->Printf ("Active plan stack:\n");
1749 PrintPlanStack (s, m_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001750
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001751 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001752 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001753 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001754 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001755 s->Printf ("Completed Plan Stack:\n");
1756 PrintPlanStack (s, m_completed_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001757 }
1758
1759 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001760 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001761 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001762 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001763 s->Printf ("Discarded Plan Stack:\n");
1764 PrintPlanStack (s, m_discarded_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001765 }
1766
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001767 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001768}
1769
Greg Claytond9e416c2012-02-18 05:35:26 +00001770TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001771Thread::CalculateTarget ()
1772{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001773 TargetSP target_sp;
1774 ProcessSP process_sp(GetProcess());
1775 if (process_sp)
1776 target_sp = process_sp->CalculateTarget();
1777 return target_sp;
1778
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001779}
1780
Greg Claytond9e416c2012-02-18 05:35:26 +00001781ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001782Thread::CalculateProcess ()
1783{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001784 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001785}
1786
Greg Claytond9e416c2012-02-18 05:35:26 +00001787ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001788Thread::CalculateThread ()
1789{
Greg Claytond9e416c2012-02-18 05:35:26 +00001790 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001791}
1792
Jason Molendab57e4a12013-11-04 09:33:30 +00001793StackFrameSP
1794Thread::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001795{
Jason Molendab57e4a12013-11-04 09:33:30 +00001796 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001797}
1798
1799void
Greg Clayton0603aa92010-10-04 01:05:56 +00001800Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001801{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001802 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001803}
1804
Greg Clayton12daf9462010-08-25 00:35:26 +00001805
Greg Clayton39d0ab32012-03-29 01:41:38 +00001806StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001807Thread::GetStackFrameList ()
1808{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001809 StackFrameListSP frame_list_sp;
1810 Mutex::Locker locker(m_frame_mutex);
1811 if (m_curr_frames_sp)
1812 {
1813 frame_list_sp = m_curr_frames_sp;
1814 }
1815 else
1816 {
1817 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1818 m_curr_frames_sp = frame_list_sp;
1819 }
1820 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001821}
1822
Greg Clayton12daf9462010-08-25 00:35:26 +00001823void
1824Thread::ClearStackFrames ()
1825{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001826 Mutex::Locker locker(m_frame_mutex);
1827
Greg Clayton160c9d82013-05-01 21:54:04 +00001828 Unwind *unwinder = GetUnwinder ();
1829 if (unwinder)
1830 unwinder->Clear();
1831
Jim Inghamb0c72a52012-02-29 03:40:22 +00001832 // Only store away the old "reference" StackFrameList if we got all its frames:
1833 // FIXME: At some point we can try to splice in the frames we have fetched into
1834 // the new frame as we make it, but let's not try that now.
1835 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001836 m_prev_frames_sp.swap (m_curr_frames_sp);
1837 m_curr_frames_sp.reset();
Jason Molenda705b1802014-06-13 02:37:02 +00001838
1839 m_extended_info.reset();
1840 m_extended_info_fetched = false;
Jim Ingham87c11912010-08-12 02:14:28 +00001841}
1842
Jason Molendab57e4a12013-11-04 09:33:30 +00001843lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001844Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1845{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001846 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001847}
1848
Jim Ingham44137582012-09-12 00:40:39 +00001849
1850Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001851Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001852{
Jason Molendab57e4a12013-11-04 09:33:30 +00001853 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001854 Error return_error;
1855
1856 if (!frame_sp)
1857 {
Daniel Malead01b2952012-11-29 21:49:15 +00001858 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001859 }
1860
Jim Ingham4f465cf2012-10-10 18:32:14 +00001861 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001862}
1863
1864Error
Jason Molendab57e4a12013-11-04 09:33:30 +00001865Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001866{
1867 Error return_error;
1868
1869 if (!frame_sp)
1870 {
1871 return_error.SetErrorString("Can't return to a null frame.");
1872 return return_error;
1873 }
1874
1875 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001876 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
Jason Molendab57e4a12013-11-04 09:33:30 +00001877 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001878 if (!older_frame_sp)
1879 {
1880 return_error.SetErrorString("No older frame to return to.");
1881 return return_error;
1882 }
Jim Ingham44137582012-09-12 00:40:39 +00001883
1884 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001885 {
Jim Ingham44137582012-09-12 00:40:39 +00001886 lldb::ABISP abi = thread->GetProcess()->GetABI();
1887 if (!abi)
1888 {
1889 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001890 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001891 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001892 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1893
1894 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1895 // Turn that back on when that works.
Jason Molenda97e704b2014-10-16 08:07:20 +00001896 if (/* DISABLES CODE */ (0) && sc.function != NULL)
Jim Ingham1f51e602012-09-27 01:15:29 +00001897 {
1898 Type *function_type = sc.function->GetType();
1899 if (function_type)
1900 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001901 ClangASTType return_type = sc.function->GetClangType().GetFunctionReturnType();
Jim Ingham1f51e602012-09-27 01:15:29 +00001902 if (return_type)
1903 {
Jim Ingham1f51e602012-09-27 01:15:29 +00001904 StreamString s;
Greg Clayton57ee3062013-07-11 22:46:58 +00001905 return_type.DumpTypeDescription(&s);
1906 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham1f51e602012-09-27 01:15:29 +00001907 if (cast_value_sp)
1908 {
1909 cast_value_sp->SetFormat(eFormatHex);
1910 return_value_sp = cast_value_sp;
1911 }
1912 }
1913 }
1914 }
1915
Jim Inghamcb640dd2012-09-14 02:14:15 +00001916 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001917 if (!return_error.Success())
1918 return return_error;
1919 }
1920
1921 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001922 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001923 // cook their data
1924
Jason Molendab57e4a12013-11-04 09:33:30 +00001925 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
Jim Ingham93208b82013-01-31 21:46:01 +00001926 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001927 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001928 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1929 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001930 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001931 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1932 if (copy_success)
1933 {
1934 thread->DiscardThreadPlans(true);
1935 thread->ClearStackFrames();
1936 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1937 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1938 }
1939 else
1940 {
1941 return_error.SetErrorString("Could not reset register values.");
1942 }
Jim Ingham93208b82013-01-31 21:46:01 +00001943 }
1944 else
1945 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001946 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001947 }
Jim Ingham44137582012-09-12 00:40:39 +00001948 }
1949 else
1950 {
Jim Ingham93208b82013-01-31 21:46:01 +00001951 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001952 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001953 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001954}
1955
Richard Mittonf86248d2013-09-12 02:20:34 +00001956static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1957{
1958 for (size_t n=0;n<list.size();n++)
1959 {
1960 s << "\t";
1961 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1962 s << "\n";
1963 }
1964}
1965
1966Error
1967Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1968{
1969 ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
1970 Target *target = exe_ctx.GetTargetPtr();
1971 TargetSP target_sp = exe_ctx.GetTargetSP();
1972 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00001973 StackFrame *frame = exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00001974 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1975
1976 // Find candidate locations.
1977 std::vector<Address> candidates, within_function, outside_function;
1978 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
1979
1980 // If possible, we try and stay within the current function.
1981 // Within a function, we accept multiple locations (optimized code may do this,
1982 // there's no solution here so we do the best we can).
1983 // However if we're trying to leave the function, we don't know how to pick the
1984 // right location, so if there's more than one then we bail.
1985 if (!within_function.empty())
1986 candidates = within_function;
1987 else if (outside_function.size() == 1 && can_leave_function)
1988 candidates = outside_function;
1989
1990 // Check if we got anything.
1991 if (candidates.empty())
1992 {
1993 if (outside_function.empty())
1994 {
1995 return Error("Cannot locate an address for %s:%i.",
1996 file.GetFilename().AsCString(), line);
1997 }
1998 else if (outside_function.size() == 1)
1999 {
2000 return Error("%s:%i is outside the current function.",
2001 file.GetFilename().AsCString(), line);
2002 }
2003 else
2004 {
2005 StreamString sstr;
2006 DumpAddressList(sstr, outside_function, target);
2007 return Error("%s:%i has multiple candidate locations:\n%s",
2008 file.GetFilename().AsCString(), line, sstr.GetString().c_str());
2009 }
2010 }
2011
2012 // Accept the first location, warn about any others.
2013 Address dest = candidates[0];
2014 if (warnings && candidates.size() > 1)
2015 {
2016 StreamString sstr;
2017 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
2018 file.GetFilename().AsCString(), line);
2019 DumpAddressList(sstr, candidates, target);
2020 *warnings = sstr.GetString();
2021 }
2022
2023 if (!reg_ctx->SetPC (dest))
2024 return Error("Cannot change PC to target address.");
2025
2026 return Error();
2027}
2028
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002029void
Greg Clayton0603aa92010-10-04 01:05:56 +00002030Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002031{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002032 ExecutionContext exe_ctx (shared_from_this());
2033 Process *process = exe_ctx.GetProcessPtr();
2034 if (process == NULL)
2035 return;
2036
Jason Molendab57e4a12013-11-04 09:33:30 +00002037 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00002038 SymbolContext frame_sc;
Jim Inghamd1f25362015-01-28 01:17:26 +00002039 if (frame_idx != LLDB_INVALID_FRAME_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002040 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002041 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002042 if (frame_sp)
2043 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002044 exe_ctx.SetFrameSP(frame_sp);
2045 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002046 }
2047 }
2048
Greg Clayton554f68d2015-02-04 22:00:53 +00002049 const FormatEntity::Entry *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00002050 assert (thread_format);
Greg Clayton554f68d2015-02-04 22:00:53 +00002051
2052 FormatEntity::Format(*thread_format,
2053 strm,
2054 frame_sp ? &frame_sc : NULL,
2055 &exe_ctx,
2056 NULL,
2057 NULL,
2058 false,
2059 false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002060}
2061
Greg Clayton99d0faf2010-11-18 23:32:35 +00002062void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002063Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00002064{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002065}
Jim Inghamee8aea12010-09-08 03:14:33 +00002066
Greg Clayton99d0faf2010-11-18 23:32:35 +00002067void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002068Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002069{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002070}
Jim Inghamee8aea12010-09-08 03:14:33 +00002071
Richard Mitton0a558352013-10-17 21:14:00 +00002072lldb::addr_t
2073Thread::GetThreadPointer ()
2074{
2075 return LLDB_INVALID_ADDRESS;
2076}
2077
2078addr_t
2079Thread::GetThreadLocalData (const ModuleSP module)
2080{
2081 // The default implementation is to ask the dynamic loader for it.
2082 // This can be overridden for specific platforms.
2083 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
2084 if (loader)
2085 return loader->GetThreadLocalData (module, shared_from_this());
2086 else
2087 return LLDB_INVALID_ADDRESS;
2088}
2089
Jason Molendab4892cd2014-05-13 22:02:48 +00002090bool
2091Thread::SafeToCallFunctions ()
2092{
2093 Process *process = GetProcess().get();
2094 if (process)
2095 {
2096 SystemRuntime *runtime = process->GetSystemRuntime ();
2097 if (runtime)
2098 {
2099 return runtime->SafeToCallFunctionsOnThisThread (shared_from_this());
2100 }
2101 }
2102 return true;
2103}
2104
Jason Molendab57e4a12013-11-04 09:33:30 +00002105lldb::StackFrameSP
2106Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
Jim Inghame4284b72010-09-23 17:40:12 +00002107{
Jason Molendab57e4a12013-11-04 09:33:30 +00002108 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00002109}
Caroline Ticeceb6b132010-10-26 03:11:13 +00002110
2111const char *
2112Thread::StopReasonAsCString (lldb::StopReason reason)
2113{
2114 switch (reason)
2115 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00002116 case eStopReasonInvalid: return "invalid";
2117 case eStopReasonNone: return "none";
2118 case eStopReasonTrace: return "trace";
2119 case eStopReasonBreakpoint: return "breakpoint";
2120 case eStopReasonWatchpoint: return "watchpoint";
2121 case eStopReasonSignal: return "signal";
2122 case eStopReasonException: return "exception";
2123 case eStopReasonExec: return "exec";
2124 case eStopReasonPlanComplete: return "plan complete";
2125 case eStopReasonThreadExiting: return "thread exiting";
Kuba Breckaafdf8422014-10-10 23:43:03 +00002126 case eStopReasonInstrumentation: return "instrumentation break";
Caroline Ticeceb6b132010-10-26 03:11:13 +00002127 }
2128
2129
2130 static char unknown_state_string[64];
2131 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
2132 return unknown_state_string;
2133}
2134
2135const char *
2136Thread::RunModeAsCString (lldb::RunMode mode)
2137{
2138 switch (mode)
2139 {
2140 case eOnlyThisThread: return "only this thread";
2141 case eAllThreads: return "all threads";
2142 case eOnlyDuringStepping: return "only during stepping";
2143 }
2144
2145 static char unknown_state_string[64];
2146 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
2147 return unknown_state_string;
2148}
Jim Ingham06e827c2010-11-11 19:26:09 +00002149
Greg Clayton7260f622011-04-18 08:33:37 +00002150size_t
2151Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
2152{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002153 ExecutionContext exe_ctx (shared_from_this());
2154 Target *target = exe_ctx.GetTargetPtr();
2155 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00002156 size_t num_frames_shown = 0;
2157 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00002158 bool is_selected = false;
2159 if (process)
2160 {
2161 if (process->GetThreadList().GetSelectedThread().get() == this)
2162 is_selected = true;
2163 }
2164 strm.Printf("%c ", is_selected ? '*' : ' ');
2165 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00002166 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002167 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00002168 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00002169 {
Jim Inghame610d642011-08-16 00:07:28 +00002170 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
2171 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
2172 {
2173 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
2174 }
Greg Clayton5113dc82011-08-12 06:47:54 +00002175 }
Greg Clayton7260f622011-04-18 08:33:37 +00002176 }
2177
2178 DumpUsingSettingsFormat (strm, start_frame);
2179
2180 if (num_frames > 0)
2181 {
2182 strm.IndentMore();
2183
2184 const bool show_frame_info = true;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002185
2186 const char *selected_frame_marker = NULL;
2187 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
2188 strm.IndentMore ();
2189 else
2190 selected_frame_marker = "* ";
2191
Greg Clayton39d0ab32012-03-29 01:41:38 +00002192 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
2193 start_frame,
2194 num_frames,
2195 show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002196 num_frames_with_source,
2197 selected_frame_marker);
2198 if (num_frames == 1)
2199 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00002200 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00002201 }
2202 return num_frames_shown;
2203}
2204
Jason Molenda705b1802014-06-13 02:37:02 +00002205bool
Kuba Breckaafdf8422014-10-10 23:43:03 +00002206Thread::GetDescription (Stream &strm, lldb::DescriptionLevel level, bool print_json_thread, bool print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002207{
2208 DumpUsingSettingsFormat (strm, 0);
2209 strm.Printf("\n");
2210
2211 StructuredData::ObjectSP thread_info = GetExtendedInfo();
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002212
Kuba Breckaafdf8422014-10-10 23:43:03 +00002213 if (print_json_thread || print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002214 {
Kuba Breckaafdf8422014-10-10 23:43:03 +00002215 if (thread_info && print_json_thread)
2216 {
2217 thread_info->Dump (strm);
2218 strm.Printf("\n");
2219 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002220
2221 if (print_json_stopinfo && m_stop_info_sp)
Kuba Breckaafdf8422014-10-10 23:43:03 +00002222 {
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002223 StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo();
2224 if (stop_info)
2225 {
2226 stop_info->Dump (strm);
2227 strm.Printf("\n");
2228 }
Kuba Breckaafdf8422014-10-10 23:43:03 +00002229 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002230
Jason Molenda705b1802014-06-13 02:37:02 +00002231 return true;
2232 }
2233
2234 if (thread_info)
2235 {
2236 StructuredData::ObjectSP activity = thread_info->GetObjectForDotSeparatedPath("activity");
2237 StructuredData::ObjectSP breadcrumb = thread_info->GetObjectForDotSeparatedPath("breadcrumb");
2238 StructuredData::ObjectSP messages = thread_info->GetObjectForDotSeparatedPath("trace_messages");
2239
2240 bool printed_activity = false;
2241 if (activity && activity->GetType() == StructuredData::Type::eTypeDictionary)
2242 {
2243 StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
2244 StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
2245 StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
2246 if (name && name->GetType() == StructuredData::Type::eTypeString
2247 && id && id->GetType() == StructuredData::Type::eTypeInteger)
2248 {
2249 strm.Printf(" Activity '%s', 0x%" PRIx64 "\n", name->GetAsString()->GetValue().c_str(), id->GetAsInteger()->GetValue());
2250 }
2251 printed_activity = true;
2252 }
2253 bool printed_breadcrumb = false;
2254 if (breadcrumb && breadcrumb->GetType() == StructuredData::Type::eTypeDictionary)
2255 {
2256 if (printed_activity)
2257 strm.Printf ("\n");
2258 StructuredData::Dictionary *breadcrumb_dict = breadcrumb->GetAsDictionary();
2259 StructuredData::ObjectSP breadcrumb_text = breadcrumb_dict->GetValueForKey ("name");
2260 if (breadcrumb_text && breadcrumb_text->GetType() == StructuredData::Type::eTypeString)
2261 {
2262 strm.Printf (" Current Breadcrumb: %s\n", breadcrumb_text->GetAsString()->GetValue().c_str());
2263 }
2264 printed_breadcrumb = true;
2265 }
2266 if (messages && messages->GetType() == StructuredData::Type::eTypeArray)
2267 {
2268 if (printed_breadcrumb)
2269 strm.Printf("\n");
2270 StructuredData::Array *messages_array = messages->GetAsArray();
2271 const size_t msg_count = messages_array->GetSize();
2272 if (msg_count > 0)
2273 {
2274 strm.Printf (" %zu trace messages:\n", msg_count);
2275 for (size_t i = 0; i < msg_count; i++)
2276 {
2277 StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
2278 if (message && message->GetType() == StructuredData::Type::eTypeDictionary)
2279 {
2280 StructuredData::Dictionary *message_dict = message->GetAsDictionary();
2281 StructuredData::ObjectSP message_text = message_dict->GetValueForKey ("message");
2282 if (message_text && message_text->GetType() == StructuredData::Type::eTypeString)
2283 {
2284 strm.Printf (" %s\n", message_text->GetAsString()->GetValue().c_str());
2285 }
2286 }
2287 }
2288 }
2289 }
2290 }
2291
2292 return true;
2293}
2294
Greg Clayton7260f622011-04-18 08:33:37 +00002295size_t
2296Thread::GetStackFrameStatus (Stream& strm,
2297 uint32_t first_frame,
2298 uint32_t num_frames,
2299 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002300 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00002301{
Greg Clayton39d0ab32012-03-29 01:41:38 +00002302 return GetStackFrameList()->GetStatus (strm,
2303 first_frame,
2304 num_frames,
2305 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002306 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00002307}
2308
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002309Unwind *
2310Thread::GetUnwinder ()
2311{
2312 if (m_unwinder_ap.get() == NULL)
2313 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00002314 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002315 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2316 switch (machine)
2317 {
2318 case llvm::Triple::x86_64:
2319 case llvm::Triple::x86:
2320 case llvm::Triple::arm:
Todd Fialad8eaa172014-07-23 14:37:35 +00002321 case llvm::Triple::aarch64:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002322 case llvm::Triple::thumb:
Ed Masteb73f8442013-10-10 00:59:47 +00002323 case llvm::Triple::mips64:
Justin Hibbits6256a0e2014-10-31 02:34:28 +00002324 case llvm::Triple::ppc:
Justin Hibbitsdb39cdf2014-10-31 15:57:52 +00002325 case llvm::Triple::ppc64:
Deepak Panickal6d3df422014-02-19 11:16:46 +00002326 case llvm::Triple::hexagon:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002327 m_unwinder_ap.reset (new UnwindLLDB (*this));
2328 break;
2329
2330 default:
2331 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2332 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2333 break;
2334 }
2335 }
2336 return m_unwinder_ap.get();
2337}
2338
2339
Greg Claytonfa559e52012-05-18 02:38:05 +00002340void
2341Thread::Flush ()
2342{
2343 ClearStackFrames ();
2344 m_reg_context_sp.reset();
2345}
Jim Ingham5d88a062012-10-16 00:09:33 +00002346
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00002347bool
Jim Ingham5d88a062012-10-16 00:09:33 +00002348Thread::IsStillAtLastBreakpointHit ()
2349{
2350 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2351 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2352 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002353 if (m_stop_info_sp) {
2354 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00002355 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002356 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00002357 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2358 if (reg_ctx_sp)
2359 {
2360 lldb::addr_t pc = reg_ctx_sp->GetPC();
2361 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002362 if (bp_site_sp &&
2363 static_cast<break_id_t>(value) == bp_site_sp->GetID())
Greg Clayton1afa68e2013-04-02 20:32:37 +00002364 return true;
2365 }
Jim Ingham5d88a062012-10-16 00:09:33 +00002366 }
2367 }
2368 return false;
2369}
Greg Clayton44d93782014-01-27 23:43:24 +00002370
2371
2372Error
2373Thread::StepIn (bool source_step,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002374 LazyBool step_in_avoids_code_without_debug_info,
2375 LazyBool step_out_avoids_code_without_debug_info)
2376
Greg Clayton44d93782014-01-27 23:43:24 +00002377{
2378 Error error;
2379 Process *process = GetProcess().get();
2380 if (StateIsStoppedState (process->GetState(), true))
2381 {
2382 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2383 ThreadPlanSP new_plan_sp;
2384 const lldb::RunMode run_mode = eOnlyThisThread;
2385 const bool abort_other_plans = false;
2386
2387 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2388 {
2389 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2390 new_plan_sp = QueueThreadPlanForStepInRange (abort_other_plans,
2391 sc.line_entry.range,
2392 sc,
2393 NULL,
2394 run_mode,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002395 step_in_avoids_code_without_debug_info,
2396 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002397 }
2398 else
2399 {
2400 new_plan_sp = QueueThreadPlanForStepSingleInstruction (false,
2401 abort_other_plans,
2402 run_mode);
2403 }
2404
2405 new_plan_sp->SetIsMasterPlan(true);
2406 new_plan_sp->SetOkayToDiscard(false);
2407
2408 // Why do we need to set the current thread by ID here???
2409 process->GetThreadList().SetSelectedThreadByID (GetID());
2410 error = process->Resume();
2411 }
2412 else
2413 {
2414 error.SetErrorString("process not stopped");
2415 }
2416 return error;
2417}
2418
2419Error
Jim Ingham4b4b2472014-03-13 02:47:14 +00002420Thread::StepOver (bool source_step,
2421 LazyBool step_out_avoids_code_without_debug_info)
Greg Clayton44d93782014-01-27 23:43:24 +00002422{
2423 Error error;
2424 Process *process = GetProcess().get();
2425 if (StateIsStoppedState (process->GetState(), true))
2426 {
2427 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2428 ThreadPlanSP new_plan_sp;
2429
2430 const lldb::RunMode run_mode = eOnlyThisThread;
2431 const bool abort_other_plans = false;
2432
2433 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2434 {
2435 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2436 new_plan_sp = QueueThreadPlanForStepOverRange (abort_other_plans,
2437 sc.line_entry.range,
2438 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002439 run_mode,
2440 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002441 }
2442 else
2443 {
2444 new_plan_sp = QueueThreadPlanForStepSingleInstruction (true,
2445 abort_other_plans,
2446 run_mode);
2447 }
2448
2449 new_plan_sp->SetIsMasterPlan(true);
2450 new_plan_sp->SetOkayToDiscard(false);
2451
2452 // Why do we need to set the current thread by ID here???
2453 process->GetThreadList().SetSelectedThreadByID (GetID());
2454 error = process->Resume();
2455 }
2456 else
2457 {
2458 error.SetErrorString("process not stopped");
2459 }
2460 return error;
2461}
2462
2463Error
2464Thread::StepOut ()
2465{
2466 Error error;
2467 Process *process = GetProcess().get();
2468 if (StateIsStoppedState (process->GetState(), true))
2469 {
2470 const bool first_instruction = false;
2471 const bool stop_other_threads = false;
2472 const bool abort_other_plans = false;
2473
2474 ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut (abort_other_plans,
2475 NULL,
2476 first_instruction,
2477 stop_other_threads,
2478 eVoteYes,
2479 eVoteNoOpinion,
2480 0));
2481
2482 new_plan_sp->SetIsMasterPlan(true);
2483 new_plan_sp->SetOkayToDiscard(false);
2484
2485 // Why do we need to set the current thread by ID here???
2486 process->GetThreadList().SetSelectedThreadByID (GetID());
2487 error = process->Resume();
2488 }
2489 else
2490 {
2491 error.SetErrorString("process not stopped");
2492 }
2493 return error;
Jim Ingham4b4b2472014-03-13 02:47:14 +00002494}