blob: 29ba86a7e84d50ef0f582cd45dc760fc1fd45481 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Jim Ingham1b54c882010-06-16 02:00:15 +000010#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000011#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/Log.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000013#include "lldb/Core/FormatEntity.h"
Greg Clayton160c9d82013-05-01 21:54:04 +000014#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Stream.h"
16#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000017#include "lldb/Core/RegularExpression.h"
Greg Clayton7260f622011-04-18 08:33:37 +000018#include "lldb/Host/Host.h"
Jim Ingham4da62062014-01-23 21:52:47 +000019#include "lldb/Interpreter/OptionValueFileSpecList.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000020#include "lldb/Interpreter/OptionValueProperties.h"
21#include "lldb/Interpreter/Property.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000022#include "lldb/Symbol/Function.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000023#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Target/DynamicLoader.h"
25#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000026#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Target/Process.h"
28#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000029#include "lldb/Target/StopInfo.h"
Jason Molendab4892cd2014-05-13 22:02:48 +000030#include "lldb/Target/SystemRuntime.h"
Greg Clayton896dff62010-07-23 16:45:51 +000031#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Target/Thread.h"
33#include "lldb/Target/ThreadPlan.h"
34#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Target/ThreadPlanBase.h"
Jim Ingham2bdbfd52014-09-29 23:17:18 +000036#include "lldb/Target/ThreadPlanPython.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Target/ThreadPlanStepInstruction.h"
38#include "lldb/Target/ThreadPlanStepOut.h"
39#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
40#include "lldb/Target/ThreadPlanStepThrough.h"
41#include "lldb/Target/ThreadPlanStepInRange.h"
42#include "lldb/Target/ThreadPlanStepOverRange.h"
43#include "lldb/Target/ThreadPlanRunToAddress.h"
44#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000045#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000046#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000047#include "Plugins/Process/Utility/UnwindLLDB.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000048#include "Plugins/Process/Utility/UnwindMacOSXFrameBackchain.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000049
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050
51using namespace lldb;
52using namespace lldb_private;
53
Greg Clayton67cc0632012-08-22 17:17:09 +000054
55const ThreadPropertiesSP &
56Thread::GetGlobalProperties()
57{
58 static ThreadPropertiesSP g_settings_sp;
59 if (!g_settings_sp)
60 g_settings_sp.reset (new ThreadProperties (true));
61 return g_settings_sp;
62}
63
64static PropertyDefinition
65g_properties[] =
66{
Jim Ingham4b4b2472014-03-13 02:47:14 +000067 { "step-in-avoid-nodebug", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, step-in will not stop in functions with no debug information." },
68 { "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 "
69 "debug information. Passing a frame argument to step-out will override this option." },
Greg Clayton7bd4c602015-01-21 21:51:02 +000070 { "step-avoid-regexp", OptionValue::eTypeRegex , true , 0, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
71 { "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 +000072 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
73 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
74};
75
76enum {
Jim Ingham4b4b2472014-03-13 02:47:14 +000077 ePropertyStepInAvoidsNoDebug,
78 ePropertyStepOutAvoidsNoDebug,
Greg Clayton67cc0632012-08-22 17:17:09 +000079 ePropertyStepAvoidRegex,
Jim Ingham4da62062014-01-23 21:52:47 +000080 ePropertyStepAvoidLibraries,
Greg Clayton67cc0632012-08-22 17:17:09 +000081 ePropertyEnableThreadTrace
82};
83
84
85class ThreadOptionValueProperties : public OptionValueProperties
86{
87public:
88 ThreadOptionValueProperties (const ConstString &name) :
89 OptionValueProperties (name)
90 {
91 }
92
93 // This constructor is used when creating ThreadOptionValueProperties when it
94 // is part of a new lldb_private::Thread instance. It will copy all current
95 // global property values as needed
96 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +000097 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +000098 {
99 }
100
101 virtual const Property *
102 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
103 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000104 // When getting the value for a key from the thread options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +0000105 // try and grab the setting from the current thread if there is one. Else we just
106 // use the one from this instance.
107 if (exe_ctx)
108 {
109 Thread *thread = exe_ctx->GetThreadPtr();
110 if (thread)
111 {
112 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
113 if (this != instance_properties)
114 return instance_properties->ProtectedGetPropertyAtIndex (idx);
115 }
116 }
117 return ProtectedGetPropertyAtIndex (idx);
118 }
119};
120
121
122
123ThreadProperties::ThreadProperties (bool is_global) :
124 Properties ()
125{
126 if (is_global)
127 {
128 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
129 m_collection_sp->Initialize(g_properties);
130 }
131 else
132 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
133}
134
135ThreadProperties::~ThreadProperties()
136{
137}
138
139const RegularExpression *
140ThreadProperties::GetSymbolsToAvoidRegexp()
141{
142 const uint32_t idx = ePropertyStepAvoidRegex;
143 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
144}
145
Jim Ingham4da62062014-01-23 21:52:47 +0000146FileSpecList &
147ThreadProperties::GetLibrariesToAvoid() const
148{
149 const uint32_t idx = ePropertyStepAvoidLibraries;
150 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
151 assert(option_value);
152 return option_value->GetCurrentValue();
153}
154
Greg Clayton67cc0632012-08-22 17:17:09 +0000155bool
156ThreadProperties::GetTraceEnabledState() const
157{
158 const uint32_t idx = ePropertyEnableThreadTrace;
159 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
160}
161
Jim Ingham4b4b2472014-03-13 02:47:14 +0000162bool
163ThreadProperties::GetStepInAvoidsNoDebug() const
164{
165 const uint32_t idx = ePropertyStepInAvoidsNoDebug;
166 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
167}
168
169bool
170ThreadProperties::GetStepOutAvoidsNoDebug() const
171{
172 const uint32_t idx = ePropertyStepOutAvoidsNoDebug;
173 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
174}
175
176
Jim Ingham4f465cf2012-10-10 18:32:14 +0000177//------------------------------------------------------------------
178// Thread Event Data
179//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000180
Jim Ingham4f465cf2012-10-10 18:32:14 +0000181
182const ConstString &
183Thread::ThreadEventData::GetFlavorString ()
184{
185 static ConstString g_flavor ("Thread::ThreadEventData");
186 return g_flavor;
187}
188
189Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
190 m_thread_sp (thread_sp),
191 m_stack_id ()
192{
193}
194
195Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
196 m_thread_sp (thread_sp),
197 m_stack_id (stack_id)
198{
199}
200
201Thread::ThreadEventData::ThreadEventData () :
202 m_thread_sp (),
203 m_stack_id ()
204{
205}
206
207Thread::ThreadEventData::~ThreadEventData ()
208{
209}
210
211void
212Thread::ThreadEventData::Dump (Stream *s) const
213{
214
215}
216
217const Thread::ThreadEventData *
218Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
219{
220 if (event_ptr)
221 {
222 const EventData *event_data = event_ptr->GetData();
223 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
224 return static_cast <const ThreadEventData *> (event_ptr->GetData());
225 }
226 return NULL;
227}
228
229ThreadSP
230Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
231{
232 ThreadSP thread_sp;
233 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
234 if (event_data)
235 thread_sp = event_data->GetThread();
236 return thread_sp;
237}
238
239StackID
240Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
241{
242 StackID stack_id;
243 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
244 if (event_data)
245 stack_id = event_data->GetStackID();
246 return stack_id;
247}
248
Jason Molendab57e4a12013-11-04 09:33:30 +0000249StackFrameSP
Jim Ingham4f465cf2012-10-10 18:32:14 +0000250Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
251{
252 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
Jason Molendab57e4a12013-11-04 09:33:30 +0000253 StackFrameSP frame_sp;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000254 if (event_data)
255 {
256 ThreadSP thread_sp = event_data->GetThread();
257 if (thread_sp)
258 {
259 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
260 }
261 }
262 return frame_sp;
263}
264
265//------------------------------------------------------------------
266// Thread class
267//------------------------------------------------------------------
268
269ConstString &
270Thread::GetStaticBroadcasterClass ()
271{
272 static ConstString class_name ("lldb.thread");
273 return class_name;
274}
275
Jason Molendaa8ff5432014-03-06 06:31:18 +0000276Thread::Thread (Process &process, lldb::tid_t tid, bool use_invalid_index_id) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000277 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000279 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
280 m_process_wp (process.shared_from_this()),
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000281 m_stop_info_sp (),
282 m_stop_info_stop_id (0),
Greg Claytona97c4d22014-12-09 23:31:02 +0000283 m_stop_info_override_stop_id (0),
Jason Molendaa8ff5432014-03-06 06:31:18 +0000284 m_index_id (use_invalid_index_id ? LLDB_INVALID_INDEX32 : process.GetNextThreadIndexID(tid)),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000287 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000290 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000291 m_curr_frames_sp (),
292 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000294 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000295 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000296 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000297 m_destroy_called (false),
Jason Molenda705b1802014-06-13 02:37:02 +0000298 m_override_should_notify (eLazyBoolCalculate),
299 m_extended_info_fetched (false),
300 m_extended_info ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301{
Greg Clayton5160ce52013-03-27 23:08:40 +0000302 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000304 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")",
305 static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306
Jim Ingham4f465cf2012-10-10 18:32:14 +0000307 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308 QueueFundamentalPlan(true);
309}
310
311
312Thread::~Thread()
313{
Greg Clayton5160ce52013-03-27 23:08:40 +0000314 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000316 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")",
317 static_cast<void*>(this), GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000318 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
319 assert (m_destroy_called);
320}
321
322void
323Thread::DestroyThread ()
324{
Jim Inghamb1499242013-10-18 17:11:02 +0000325 // Tell any plans on the plan stacks that the thread is being destroyed since
326 // any plans that have a thread go away in the middle of might need
327 // to do cleanup, or in some cases NOT do cleanup...
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000328 for (auto plan : m_plan_stack)
329 plan->ThreadDestroyed();
330
Jim Inghamb1499242013-10-18 17:11:02 +0000331 for (auto plan : m_discarded_plan_stack)
332 plan->ThreadDestroyed();
333
334 for (auto plan : m_completed_plan_stack)
335 plan->ThreadDestroyed();
336
Greg Clayton35a4cc52012-10-29 20:52:08 +0000337 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000338 m_plan_stack.clear();
339 m_discarded_plan_stack.clear();
340 m_completed_plan_stack.clear();
Greg Clayton6e10f142013-07-30 00:23:06 +0000341
342 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the
343 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
344 // without checking first whether it is destroyed, they won't crash.
345 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
346 m_plan_stack.push_back (null_plan_sp);
347
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000348 m_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000349 m_reg_context_sp.reset();
350 m_unwinder_ap.reset();
351 Mutex::Locker locker(m_frame_mutex);
352 m_curr_frames_sp.reset();
353 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354}
355
Jim Ingham4f465cf2012-10-10 18:32:14 +0000356void
357Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
358{
359 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
360 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
361}
362
363uint32_t
Jason Molendab57e4a12013-11-04 09:33:30 +0000364Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
Jim Ingham4f465cf2012-10-10 18:32:14 +0000365{
366 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
367 if (broadcast)
368 BroadcastSelectedFrameChange(frame->GetStackID());
369 return ret_value;
370}
371
372bool
373Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
374{
Jason Molendab57e4a12013-11-04 09:33:30 +0000375 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
Jim Ingham4f465cf2012-10-10 18:32:14 +0000376 if (frame_sp)
377 {
378 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
379 if (broadcast)
380 BroadcastSelectedFrameChange(frame_sp->GetStackID());
381 return true;
382 }
383 else
384 return false;
385}
386
Jim Ingham93208b82013-01-31 21:46:01 +0000387bool
388Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
389{
390 const bool broadcast = true;
391 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
392 if (success)
393 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000394 StackFrameSP frame_sp = GetSelectedFrame();
Jim Ingham93208b82013-01-31 21:46:01 +0000395 if (frame_sp)
396 {
397 bool already_shown = false;
398 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
399 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
400 {
401 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
402 }
403
404 bool show_frame_info = true;
405 bool show_source = !already_shown;
406 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
407 }
408 return false;
409 }
410 else
411 return false;
412}
413
Jim Ingham4f465cf2012-10-10 18:32:14 +0000414
Jim Inghamb15bfc72010-10-20 00:39:53 +0000415lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000416Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417{
Greg Clayton6e10f142013-07-30 00:23:06 +0000418 if (m_destroy_called)
419 return m_stop_info_sp;
420
Jim Inghamb15bfc72010-10-20 00:39:53 +0000421 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000422 ProcessSP process_sp (GetProcess());
423 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000424 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000425 {
Jim Ingham30fadaf2014-07-08 01:07:32 +0000426 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject(), GetExpressionVariable());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000427 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000428 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000429 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000430 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
431 (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 +0000432 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000433 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000434 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000435 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000436 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000437 GetPrivateStopInfo ();
438 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000439 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000440 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441}
442
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000443lldb::StopInfoSP
444Thread::GetPrivateStopInfo ()
445{
Greg Clayton6e10f142013-07-30 00:23:06 +0000446 if (m_destroy_called)
447 return m_stop_info_sp;
448
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000449 ProcessSP process_sp (GetProcess());
450 if (process_sp)
451 {
Daniel Malea246cb612013-05-14 15:20:12 +0000452 const uint32_t process_stop_id = process_sp->GetStopID();
453 if (m_stop_info_stop_id != process_stop_id)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000454 {
Daniel Malea246cb612013-05-14 15:20:12 +0000455 if (m_stop_info_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000456 {
Daniel Malea246cb612013-05-14 15:20:12 +0000457 if (m_stop_info_sp->IsValid()
458 || IsStillAtLastBreakpointHit()
459 || GetCurrentPlan()->IsVirtualStep())
460 SetStopInfo (m_stop_info_sp);
461 else
462 m_stop_info_sp.reset();
463 }
464
465 if (!m_stop_info_sp)
466 {
467 if (CalculateStopInfo() == false)
468 SetStopInfo (StopInfoSP());
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000469 }
470 }
Greg Claytona97c4d22014-12-09 23:31:02 +0000471
472 // The stop info can be manually set by calling Thread::SetStopInfo()
473 // prior to this function ever getting called, so we can't rely on
474 // "m_stop_info_stop_id != process_stop_id" as the condition for
475 // the if statement below, we must also check the stop info to see
476 // if we need to override it. See the header documentation in
477 // Process::GetStopInfoOverrideCallback() for more information on
478 // the stop info override callback.
479 if (m_stop_info_override_stop_id != process_stop_id)
480 {
481 m_stop_info_override_stop_id = process_stop_id;
482 if (m_stop_info_sp)
483 {
484 ArchSpec::StopInfoOverrideCallbackType callback = GetProcess()->GetStopInfoOverrideCallback();
485 if (callback)
486 callback(*this);
487 }
488 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000489 }
490 return m_stop_info_sp;
491}
492
493
Greg Clayton97d5cf02012-09-25 02:40:06 +0000494lldb::StopReason
495Thread::GetStopReason()
496{
497 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
498 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000499 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000500 return eStopReasonNone;
501}
502
503
504
Jim Ingham77787032011-01-20 02:03:18 +0000505void
506Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
507{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000508 m_stop_info_sp = stop_info_sp;
509 if (m_stop_info_sp)
Jim Ingham221d51c2013-05-08 00:35:16 +0000510 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000511 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham221d51c2013-05-08 00:35:16 +0000512 // If we are overriding the ShouldReportStop, do that here:
513 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000514 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000515 }
516
Greg Clayton1ac04c32012-02-21 00:09:25 +0000517 ProcessSP process_sp (GetProcess());
518 if (process_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000519 m_stop_info_stop_id = process_sp->GetStopID();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000520 else
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000521 m_stop_info_stop_id = UINT32_MAX;
Greg Clayton8cda7f02013-05-21 21:55:59 +0000522 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
523 if (log)
Ed Maste2bc76432014-06-25 00:38:35 +0000524 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000525 static_cast<void*>(this), GetID(),
526 stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>",
527 m_stop_info_stop_id);
Jim Ingham77787032011-01-20 02:03:18 +0000528}
529
530void
Jim Ingham221d51c2013-05-08 00:35:16 +0000531Thread::SetShouldReportStop (Vote vote)
532{
533 if (vote == eVoteNoOpinion)
534 return;
535 else
536 {
537 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000538 if (m_stop_info_sp)
539 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000540 }
541}
542
543void
Jim Ingham77787032011-01-20 02:03:18 +0000544Thread::SetStopInfoToNothing()
545{
546 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
547 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
548 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
549}
550
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551bool
552Thread::ThreadStoppedForAReason (void)
553{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000554 return (bool) GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555}
556
Jim Ingham77787032011-01-20 02:03:18 +0000557bool
558Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
559{
Greg Claytonf74cf862013-11-13 23:28:31 +0000560 saved_state.register_backup_sp.reset();
561 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
562 if (frame_sp)
563 {
564 lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
565 if (reg_checkpoint_sp)
566 {
567 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
568 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp))
569 saved_state.register_backup_sp = reg_checkpoint_sp;
570 }
571 }
572 if (!saved_state.register_backup_sp)
Jim Ingham77787032011-01-20 02:03:18 +0000573 return false;
574
575 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000576 ProcessSP process_sp (GetProcess());
577 if (process_sp)
578 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000579 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
580
Jim Ingham77787032011-01-20 02:03:18 +0000581 return true;
582}
583
584bool
Jim Ingham8559a352012-11-26 23:52:18 +0000585Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000586{
Greg Claytonf74cf862013-11-13 23:28:31 +0000587 if (saved_state.register_backup_sp)
588 {
589 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
590 if (frame_sp)
591 {
592 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
593 if (reg_ctx_sp)
594 {
595 bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp);
596
597 // Clear out all stack frames as our world just changed.
598 ClearStackFrames();
599 reg_ctx_sp->InvalidateIfNeeded(true);
600 if (m_unwinder_ap.get())
601 m_unwinder_ap->Clear();
602 return ret;
603 }
604 }
605 }
606 return false;
Jim Ingham8559a352012-11-26 23:52:18 +0000607}
608
609bool
610Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
611{
Jim Ingham0c270682011-01-25 02:47:23 +0000612 if (saved_state.stop_info_sp)
613 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000614 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000615 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000616 return true;
617}
618
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619StateType
620Thread::GetState() const
621{
622 // If any other threads access this we will need a mutex for it
623 Mutex::Locker locker(m_state_mutex);
624 return m_state;
625}
626
627void
628Thread::SetState(StateType state)
629{
630 Mutex::Locker locker(m_state_mutex);
631 m_state = state;
632}
633
634void
635Thread::WillStop()
636{
637 ThreadPlan *current_plan = GetCurrentPlan();
638
639 // FIXME: I may decide to disallow threads with no plans. In which
640 // case this should go to an assert.
641
642 if (!current_plan)
643 return;
644
645 current_plan->WillStop();
646}
647
648void
649Thread::SetupForResume ()
650{
651 if (GetResumeState() != eStateSuspended)
652 {
653
654 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
655 // telling the current plan it will resume, since we might change what the current
656 // plan is.
657
Greg Clayton1afa68e2013-04-02 20:32:37 +0000658 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
659 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000660 {
Greg Claytona97c4d22014-12-09 23:31:02 +0000661 const addr_t thread_pc = reg_ctx_sp->GetPC();
662 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000663 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000664 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000665 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
666 // special to step over a breakpoint.
667
668 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000669
Greg Claytona97c4d22014-12-09 23:31:02 +0000670 bool push_step_over_bp_plan = false;
671 if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint)
672 {
673 ThreadPlanStepOverBreakpoint *bp_plan = (ThreadPlanStepOverBreakpoint *)cur_plan;
674 if (bp_plan->GetBreakpointLoadAddress() != thread_pc)
675 push_step_over_bp_plan = true;
676 }
677 else
678 push_step_over_bp_plan = true;
679
680 if (push_step_over_bp_plan)
Greg Clayton1afa68e2013-04-02 20:32:37 +0000681 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000682 ThreadPlanSP step_bp_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
683 if (step_bp_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000685 ;
686 step_bp_plan_sp->SetPrivate (true);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000687
688 if (GetCurrentPlan()->RunState() != eStateStepping)
689 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000690 ThreadPlanStepOverBreakpoint *step_bp_plan
691 = static_cast<ThreadPlanStepOverBreakpoint *>(step_bp_plan_sp.get());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000692 step_bp_plan->SetAutoContinue(true);
693 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000694 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 }
697 }
698 }
699 }
700}
701
702bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000703Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704{
705 // At this point clear the completed plan stack.
706 m_completed_plan_stack.clear();
707 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000708 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709
Greg Clayton97d5cf02012-09-25 02:40:06 +0000710 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000711
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000712 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
713 if (backing_thread_sp)
714 backing_thread_sp->m_temporary_resume_state = resume_state;
715
716 // Make sure m_stop_info_sp is valid
717 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000718
Jim Ingham92087d82012-01-31 23:09:20 +0000719 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
720 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
721 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
722 // about the fact that we are resuming...
Jim Inghamacbea8f2015-06-02 20:26:13 +0000723 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000724 if (m_stop_info_stop_id == process_stop_id &&
725 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000726 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000727 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000728 if (stop_info)
729 stop_info->WillResume (resume_state);
730 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731
732 // Tell all the plans that we are about to resume in case they need to clear any state.
733 // We distinguish between the plan on the top of the stack and the lower
734 // plans in case a plan needs to do any special business before it runs.
735
Greg Claytond1d06e42013-04-20 00:27:58 +0000736 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000738 if (plan_ptr)
739 {
740 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741
Greg Claytond1d06e42013-04-20 00:27:58 +0000742 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
743 {
744 plan_ptr->WillResume (resume_state, false);
745 }
746
747 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
748 // In that case, don't reset it here.
749
750 if (need_to_resume && resume_state != eStateSuspended)
751 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000752 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000753 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000754 }
755
Greg Clayton160c9d82013-05-01 21:54:04 +0000756 if (need_to_resume)
757 {
758 ClearStackFrames();
759 // Let Thread subclasses do any special work they need to prior to resuming
760 WillResume (resume_state);
761 }
762
Jim Ingham513c6bb2012-09-01 01:02:41 +0000763 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764}
765
766void
767Thread::DidResume ()
768{
769 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
770}
771
Andrew Kaylor29d65742013-05-10 17:19:04 +0000772void
773Thread::DidStop ()
774{
775 SetState (eStateStopped);
776}
777
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000778bool
779Thread::ShouldStop (Event* event_ptr)
780{
781 ThreadPlan *current_plan = GetCurrentPlan();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000782
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783 bool should_stop = true;
784
Greg Clayton5160ce52013-03-27 23:08:40 +0000785 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000786
Jim Ingham92087d82012-01-31 23:09:20 +0000787 if (GetResumeState () == eStateSuspended)
788 {
789 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000790 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 +0000791 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000792 return false;
793 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000794
Jim Ingham92087d82012-01-31 23:09:20 +0000795 if (GetTemporaryResumeState () == eStateSuspended)
796 {
797 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000798 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000799 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000800 return false;
801 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000802
Daniel Malea246cb612013-05-14 15:20:12 +0000803 // Based on the current thread plan and process stop info, check if this
804 // thread caused the process to stop. NOTE: this must take place before
805 // the plan is moved from the current plan stack to the completed plan
806 // stack.
Jim Ingham92087d82012-01-31 23:09:20 +0000807 if (ThreadStoppedForAReason() == false)
808 {
809 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000810 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 +0000811 __FUNCTION__, GetID (), GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000812 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000813 return false;
814 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000815
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 if (log)
817 {
Jim Inghamdee1bc92013-06-22 00:27:45 +0000818 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 +0000819 __FUNCTION__, static_cast<void*>(this), GetID (),
Greg Clayton160c9d82013-05-01 21:54:04 +0000820 GetProtocolID (),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000821 GetRegisterContext()
822 ? GetRegisterContext()->GetPC()
823 : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000824 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000825 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000826 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000827 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000828 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000830
Jim Ingham06e827c2010-11-11 19:26:09 +0000831 // The top most plan always gets to do the trace log...
832 current_plan->DoTraceLog ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000833
Jim Ingham6d66ce62012-04-20 21:16:56 +0000834 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
835 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
836 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000837 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Ingham6d66ce62012-04-20 21:16:56 +0000838 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
839 {
840 if (log)
841 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
842 return false;
843 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844
Jim Inghambad39e42012-09-05 21:12:49 +0000845 // If we've already been restarted, don't query the plans since the state they would examine is not current.
846 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
847 return false;
848
849 // Before the plans see the state of the world, calculate the current inlined depth.
850 GetStackFrameList()->CalculateCurrentInlinedDepth();
851
Jim Ingham25f66702011-12-03 01:52:59 +0000852 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
853 // If that plan is still working, then we don't need to do any more work. If the plan that explains
854 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
855 // whether they still need to do more work.
856
857 bool done_processing_current_plan = false;
858
Jim Ingham0161b492013-02-09 01:29:05 +0000859 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000860 {
861 if (current_plan->TracerExplainsStop())
862 {
863 done_processing_current_plan = true;
864 should_stop = false;
865 }
866 else
867 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000868 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000869 // does and let it handle the situation.
870 ThreadPlan *plan_ptr = current_plan;
871 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
872 {
Jim Ingham0161b492013-02-09 01:29:05 +0000873 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000874 {
875 should_stop = plan_ptr->ShouldStop (event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000876
Jim Ingham25f66702011-12-03 01:52:59 +0000877 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
878 // and all the plans below it off the stack.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000879
Jim Ingham25f66702011-12-03 01:52:59 +0000880 if (plan_ptr->MischiefManaged())
881 {
Jim Ingham031177e2012-05-11 23:49:49 +0000882 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000883 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000884
Jim Ingham25f66702011-12-03 01:52:59 +0000885 do
886 {
887 if (should_stop)
888 current_plan->WillStop();
889 PopPlan();
890 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000891 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
892 // Now, if the responsible plan was not "Okay to discard" then we're done,
893 // otherwise we forward this to the next plan in the stack below.
894 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
895 done_processing_current_plan = true;
896 else
897 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000898 }
899 else
900 done_processing_current_plan = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000901
Jim Ingham25f66702011-12-03 01:52:59 +0000902 break;
903 }
904
905 }
906 }
907 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000908
Jim Ingham25f66702011-12-03 01:52:59 +0000909 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000911 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000912
Jim Ingham10c4b242011-10-15 00:23:43 +0000913 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000914 log->Printf("Plan %s explains stop, auto-continue %i.",
915 current_plan->GetName(), over_ride_stop);
916
Jim Ingham0f16e732011-02-08 05:20:59 +0000917 // We're starting from the base plan, so just let it decide;
918 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000920 should_stop = current_plan->ShouldStop (event_ptr);
921 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000922 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000923 }
924 else
925 {
926 // Otherwise, don't let the base plan override what the other plans say to do, since
927 // presumably if there were other plans they would know what to do...
928 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000929 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000930 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000931 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000932
Jim Ingham0f16e732011-02-08 05:20:59 +0000933 should_stop = current_plan->ShouldStop(event_ptr);
934 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000935 log->Printf("Plan %s should stop: %d.",
936 current_plan->GetName(), should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000937 if (current_plan->MischiefManaged())
938 {
939 if (should_stop)
940 current_plan->WillStop();
941
942 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
943 // Otherwise, see if the plan's parent wants to stop.
944
945 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
946 {
947 PopPlan();
948 break;
949 }
950 else
951 {
952
953 PopPlan();
954
955 current_plan = GetCurrentPlan();
956 if (current_plan == NULL)
957 {
958 break;
959 }
960 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961 }
962 else
963 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000964 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966 }
967 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000968
Jim Inghamb01e7422010-06-19 04:45:32 +0000969 if (over_ride_stop)
970 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000971
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000972 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000973
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000974 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
975 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
976 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
977 // This code clears stale plans off the stack.
978
979 if (should_stop)
980 {
981 ThreadPlan *plan_ptr = GetCurrentPlan();
982 while (!PlanIsBasePlan(plan_ptr))
Jim Ingham64e7ead2012-05-03 21:19:36 +0000983 {
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000984 bool stale = plan_ptr->IsPlanStale ();
985 ThreadPlan *examined_plan = plan_ptr;
986 plan_ptr = GetPreviousPlan (examined_plan);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000987
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000988 if (stale)
989 {
990 if (log)
991 log->Printf("Plan %s being discarded in cleanup, it says it is already done.",
992 examined_plan->GetName());
993 DiscardThreadPlansUpToPlan(examined_plan);
Jim Ingham64e7ead2012-05-03 21:19:36 +0000994 }
995 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997
Jim Ingham10c4b242011-10-15 00:23:43 +0000998 if (log)
999 {
1000 StreamString s;
1001 s.IndentMore();
1002 DumpThreadPlans(&s);
1003 log->Printf ("Plan stack final state:\n%s", s.GetData());
1004 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
1005 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001006 return should_stop;
1007}
1008
1009Vote
1010Thread::ShouldReportStop (Event* event_ptr)
1011{
1012 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +00001013 StateType temp_thread_state = GetTemporaryResumeState();
1014
Greg Clayton5160ce52013-03-27 23:08:40 +00001015 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +00001016
1017 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
1018 {
1019 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001020 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 +00001021 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001022 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023
Jim Ingham92087d82012-01-31 23:09:20 +00001024 if (temp_thread_state == eStateSuspended || temp_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 (temporary state was suspended or invalid)", GetID(), eVoteNoOpinion);
Jim Ingham92087d82012-01-31 23:09:20 +00001028 return eVoteNoOpinion;
1029 }
1030
1031 if (!ThreadStoppedForAReason())
1032 {
1033 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001034 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 +00001035 return eVoteNoOpinion;
1036 }
1037
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001038 if (m_completed_plan_stack.size() > 0)
1039 {
1040 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +00001041 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001042 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 +00001043 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
1044 }
1045 else
Greg Clayton2cad65a2010-09-03 17:10:42 +00001046 {
Jim Ingham0161b492013-02-09 01:29:05 +00001047 Vote thread_vote = eVoteNoOpinion;
1048 ThreadPlan *plan_ptr = GetCurrentPlan();
1049 while (1)
1050 {
1051 if (plan_ptr->PlanExplainsStop(event_ptr))
1052 {
1053 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1054 break;
1055 }
1056 if (PlanIsBasePlan(plan_ptr))
1057 break;
1058 else
1059 plan_ptr = GetPreviousPlan(plan_ptr);
1060 }
Greg Clayton2cad65a2010-09-03 17:10:42 +00001061 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001062 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 +00001063
1064 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001065 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001066}
1067
1068Vote
1069Thread::ShouldReportRun (Event* event_ptr)
1070{
1071 StateType thread_state = GetResumeState ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001072
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073 if (thread_state == eStateSuspended
1074 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +00001075 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +00001077 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001078
Greg Clayton5160ce52013-03-27 23:08:40 +00001079 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080 if (m_completed_plan_stack.size() > 0)
1081 {
1082 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +00001083 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001084 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 +00001085 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001086 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001087 m_completed_plan_stack.back()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001088
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
1090 }
1091 else
Jim Ingham444586b2011-01-24 06:34:17 +00001092 {
1093 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001094 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 +00001095 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001096 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001097 GetCurrentPlan()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001098
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +00001100 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101}
1102
Jim Ingham1b54c882010-06-16 02:00:15 +00001103bool
1104Thread::MatchesSpec (const ThreadSpec *spec)
1105{
1106 if (spec == NULL)
1107 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +00001108
Jim Ingham3d902922012-03-07 22:03:04 +00001109 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +00001110}
1111
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001112void
1113Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1114{
1115 if (thread_plan_sp)
1116 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001117 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1118 if (!thread_plan_sp->GetThreadPlanTracer())
1119 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001120 m_plan_stack.push_back (thread_plan_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001121
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122 thread_plan_sp->DidPush();
1123
Greg Clayton5160ce52013-03-27 23:08:40 +00001124 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125 if (log)
1126 {
1127 StreamString s;
1128 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamdee1bc92013-06-22 00:27:45 +00001129 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001130 static_cast<void*>(this), s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001131 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132 }
1133 }
1134}
1135
1136void
1137Thread::PopPlan ()
1138{
Greg Clayton5160ce52013-03-27 23:08:40 +00001139 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001140
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001141 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001142 return;
1143 else
1144 {
1145 ThreadPlanSP &plan = m_plan_stack.back();
1146 if (log)
1147 {
Daniel Malead01b2952012-11-29 21:49:15 +00001148 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149 }
1150 m_completed_plan_stack.push_back (plan);
1151 plan->WillPop();
1152 m_plan_stack.pop_back();
1153 }
1154}
1155
1156void
1157Thread::DiscardPlan ()
1158{
Jim Inghamdee1bc92013-06-22 00:27:45 +00001159 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001160 if (m_plan_stack.size() > 1)
1161 {
1162 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamdee1bc92013-06-22 00:27:45 +00001163 if (log)
1164 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1165
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166 m_discarded_plan_stack.push_back (plan);
1167 plan->WillPop();
1168 m_plan_stack.pop_back();
1169 }
1170}
1171
1172ThreadPlan *
1173Thread::GetCurrentPlan ()
1174{
Jim Inghame1471232012-04-19 00:17:05 +00001175 // There will always be at least the base plan. If somebody is mucking with a
1176 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +00001177 if (m_plan_stack.empty())
1178 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +00001179 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180}
1181
1182ThreadPlanSP
1183Thread::GetCompletedPlan ()
1184{
1185 ThreadPlanSP empty_plan_sp;
1186 if (!m_completed_plan_stack.empty())
1187 {
1188 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1189 {
1190 ThreadPlanSP completed_plan_sp;
1191 completed_plan_sp = m_completed_plan_stack[i];
1192 if (!completed_plan_sp->GetPrivate ())
1193 return completed_plan_sp;
1194 }
1195 }
1196 return empty_plan_sp;
1197}
1198
Jim Ingham73ca05a2011-12-17 01:35:57 +00001199ValueObjectSP
1200Thread::GetReturnValueObject ()
1201{
1202 if (!m_completed_plan_stack.empty())
1203 {
1204 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1205 {
1206 ValueObjectSP return_valobj_sp;
1207 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1208 if (return_valobj_sp)
Greg Claytone5214572015-07-01 23:28:31 +00001209 return return_valobj_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001210 }
1211 }
1212 return ValueObjectSP();
1213}
1214
Jim Ingham30fadaf2014-07-08 01:07:32 +00001215ClangExpressionVariableSP
1216Thread::GetExpressionVariable ()
1217{
1218 if (!m_completed_plan_stack.empty())
1219 {
1220 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1221 {
1222 ClangExpressionVariableSP expression_variable_sp;
1223 expression_variable_sp = m_completed_plan_stack[i]->GetExpressionVariable();
1224 if (expression_variable_sp)
Greg Claytone5214572015-07-01 23:28:31 +00001225 return expression_variable_sp;
Jim Ingham30fadaf2014-07-08 01:07:32 +00001226 }
1227 }
1228 return ClangExpressionVariableSP();
1229}
1230
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231bool
1232Thread::IsThreadPlanDone (ThreadPlan *plan)
1233{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001234 if (!m_completed_plan_stack.empty())
1235 {
1236 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1237 {
1238 if (m_completed_plan_stack[i].get() == plan)
1239 return true;
1240 }
1241 }
1242 return false;
1243}
1244
1245bool
1246Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1247{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001248 if (!m_discarded_plan_stack.empty())
1249 {
1250 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1251 {
1252 if (m_discarded_plan_stack[i].get() == plan)
1253 return true;
1254 }
1255 }
1256 return false;
1257}
1258
1259ThreadPlan *
1260Thread::GetPreviousPlan (ThreadPlan *current_plan)
1261{
1262 if (current_plan == NULL)
1263 return NULL;
1264
1265 int stack_size = m_completed_plan_stack.size();
1266 for (int i = stack_size - 1; i > 0; i--)
1267 {
1268 if (current_plan == m_completed_plan_stack[i].get())
1269 return m_completed_plan_stack[i-1].get();
1270 }
1271
1272 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1273 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274 if (m_plan_stack.size() > 0)
1275 return m_plan_stack.back().get();
1276 else
1277 return NULL;
1278 }
1279
1280 stack_size = m_plan_stack.size();
1281 for (int i = stack_size - 1; i > 0; i--)
1282 {
1283 if (current_plan == m_plan_stack[i].get())
1284 return m_plan_stack[i-1].get();
1285 }
1286 return NULL;
1287}
1288
1289void
1290Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1291{
1292 if (abort_other_plans)
1293 DiscardThreadPlans(true);
1294
1295 PushPlan (thread_plan_sp);
1296}
1297
Jim Ingham06e827c2010-11-11 19:26:09 +00001298
1299void
1300Thread::EnableTracer (bool value, bool single_stepping)
1301{
1302 int stack_size = m_plan_stack.size();
1303 for (int i = 0; i < stack_size; i++)
1304 {
1305 if (m_plan_stack[i]->GetThreadPlanTracer())
1306 {
1307 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1308 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1309 }
1310 }
1311}
1312
1313void
1314Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1315{
1316 int stack_size = m_plan_stack.size();
1317 for (int i = 0; i < stack_size; i++)
1318 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1319}
1320
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001321bool
1322Thread::DiscardUserThreadPlansUpToIndex (uint32_t thread_index)
1323{
1324 // Count the user thread plans from the back end to get the number of the one we want
1325 // to discard:
1326
1327 uint32_t idx = 0;
1328 ThreadPlan *up_to_plan_ptr = nullptr;
1329
1330 for (ThreadPlanSP plan_sp : m_plan_stack)
1331 {
1332 if (plan_sp->GetPrivate())
1333 continue;
1334 if (idx == thread_index)
1335 {
1336 up_to_plan_ptr = plan_sp.get();
1337 break;
1338 }
1339 else
1340 idx++;
1341 }
1342
1343 if (up_to_plan_ptr == nullptr)
1344 return false;
1345
1346 DiscardThreadPlansUpToPlan(up_to_plan_ptr);
1347 return true;
1348}
1349
1350
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001351void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001352Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1353{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001354 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1355}
1356
1357void
1358Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1359{
Greg Clayton5160ce52013-03-27 23:08:40 +00001360 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001361 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001362 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p",
1363 GetID(), static_cast<void*>(up_to_plan_ptr));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001364
1365 int stack_size = m_plan_stack.size();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001366
Jim Ingham399f1ca2010-11-05 19:25:48 +00001367 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1368 // stack, and if so discard up to and including it.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001369
Jim Ingham64e7ead2012-05-03 21:19:36 +00001370 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001371 {
1372 for (int i = stack_size - 1; i > 0; i--)
1373 DiscardPlan();
1374 }
1375 else
1376 {
1377 bool found_it = false;
1378 for (int i = stack_size - 1; i > 0; i--)
1379 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001380 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001381 found_it = true;
1382 }
1383 if (found_it)
1384 {
1385 bool last_one = false;
1386 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1387 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001388 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001389 last_one = true;
1390 DiscardPlan();
1391 }
1392 }
1393 }
1394 return;
1395}
1396
1397void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001398Thread::DiscardThreadPlans(bool force)
1399{
Greg Clayton5160ce52013-03-27 23:08:40 +00001400 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401 if (log)
1402 {
Daniel Malead01b2952012-11-29 21:49:15 +00001403 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001404 }
1405
1406 if (force)
1407 {
1408 int stack_size = m_plan_stack.size();
1409 for (int i = stack_size - 1; i > 0; i--)
1410 {
1411 DiscardPlan();
1412 }
1413 return;
1414 }
1415
1416 while (1)
1417 {
1418
1419 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001420 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001421
1422 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1423 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1424 {
1425 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1426 {
1427 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1428 break;
1429 }
1430 }
1431
1432 if (discard)
1433 {
1434 // First pop all the dependent plans:
1435 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1436 {
1437
1438 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1439 // for the plan leaves it in a state that it is safe to pop the plan
1440 // with no more notice?
1441 DiscardPlan();
1442 }
1443
1444 // Now discard the master plan itself.
1445 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1446 // discard it's dependent plans, but not it...
1447 if (master_plan_idx > 0)
1448 {
1449 DiscardPlan();
1450 }
1451 }
1452 else
1453 {
1454 // If the master plan doesn't want to get discarded, then we're done.
1455 break;
1456 }
1457
1458 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001459}
1460
Jim Inghamcf274f92012-04-09 22:37:39 +00001461bool
1462Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1463{
1464 if (plan_ptr->IsBasePlan())
1465 return true;
1466 else if (m_plan_stack.size() == 0)
1467 return false;
1468 else
1469 return m_plan_stack[0].get() == plan_ptr;
1470}
1471
Jim Ingham93208b82013-01-31 21:46:01 +00001472Error
1473Thread::UnwindInnermostExpression()
1474{
1475 Error error;
1476 int stack_size = m_plan_stack.size();
1477
1478 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1479 // stack, and if so discard up to and including it.
1480
1481 for (int i = stack_size - 1; i > 0; i--)
1482 {
1483 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1484 {
1485 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1486 return error;
1487 }
1488 }
1489 error.SetErrorString("No expressions currently active on this thread");
1490 return error;
1491}
1492
1493
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001494ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495Thread::QueueFundamentalPlan (bool abort_other_plans)
1496{
1497 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1498 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001499 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001500}
1501
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001502ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001503Thread::QueueThreadPlanForStepSingleInstruction
1504(
1505 bool step_over,
1506 bool abort_other_plans,
1507 bool stop_other_threads
1508)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001509{
1510 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1511 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001512 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001513}
1514
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001515ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001516Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001517(
1518 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001519 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001520 const SymbolContext &addr_context,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001521 lldb::RunMode stop_other_threads,
1522 LazyBool step_out_avoids_code_withoug_debug_info
Jim Inghamc6276822012-12-12 19:58:40 +00001523)
1524{
1525 ThreadPlanSP thread_plan_sp;
Jim Ingham4b4b2472014-03-13 02:47:14 +00001526 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads, step_out_avoids_code_withoug_debug_info));
1527
Jim Inghamc6276822012-12-12 19:58:40 +00001528 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001529 return thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001530}
1531
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001532ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001533Thread::QueueThreadPlanForStepInRange
1534(
1535 bool abort_other_plans,
1536 const AddressRange &range,
1537 const SymbolContext &addr_context,
1538 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001539 lldb::RunMode stop_other_threads,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001540 LazyBool step_in_avoids_code_without_debug_info,
1541 LazyBool step_out_avoids_code_without_debug_info
Greg Clayton474966a2010-06-12 18:59:55 +00001542)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001543{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001544 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInRange (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001545 range,
1546 addr_context,
1547 stop_other_threads,
1548 step_in_avoids_code_without_debug_info,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001549 step_out_avoids_code_without_debug_info));
1550 ThreadPlanStepInRange *plan = static_cast<ThreadPlanStepInRange *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001551
Jim Inghamc6276822012-12-12 19:58:40 +00001552 if (step_in_target)
1553 plan->SetStepInTarget(step_in_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001554
1555 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001556 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001557}
1558
1559
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001560ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001561Thread::QueueThreadPlanForStepOut
1562(
1563 bool abort_other_plans,
1564 SymbolContext *addr_context,
1565 bool first_insn,
1566 bool stop_other_threads,
1567 Vote stop_vote,
1568 Vote run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001569 uint32_t frame_idx,
1570 LazyBool step_out_avoids_code_withoug_debug_info
Greg Clayton481cef22011-01-21 06:11:58 +00001571)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001572{
Greg Clayton481cef22011-01-21 06:11:58 +00001573 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1574 addr_context,
1575 first_insn,
1576 stop_other_threads,
1577 stop_vote,
1578 run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001579 frame_idx,
1580 step_out_avoids_code_withoug_debug_info));
1581
1582 if (thread_plan_sp->ValidatePlan(NULL))
1583 {
1584 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1585 return thread_plan_sp;
1586 }
1587 else
1588 {
1589 return ThreadPlanSP();
1590 }
1591}
1592
1593ThreadPlanSP
1594Thread::QueueThreadPlanForStepOutNoShouldStop
1595(
1596 bool abort_other_plans,
1597 SymbolContext *addr_context,
1598 bool first_insn,
1599 bool stop_other_threads,
1600 Vote stop_vote,
1601 Vote run_vote,
1602 uint32_t frame_idx
1603)
1604{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001605 ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001606 addr_context,
1607 first_insn,
1608 stop_other_threads,
1609 stop_vote,
1610 run_vote,
1611 frame_idx,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001612 eLazyBoolNo));
1613
1614 ThreadPlanStepOut *new_plan = static_cast<ThreadPlanStepOut *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001615 new_plan->ClearShouldStopHereCallbacks();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001616
Sean Callanan708709c2012-07-31 22:19:25 +00001617 if (thread_plan_sp->ValidatePlan(NULL))
1618 {
1619 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001620 return thread_plan_sp;
Sean Callanan708709c2012-07-31 22:19:25 +00001621 }
1622 else
1623 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001624 return ThreadPlanSP();
Sean Callanan708709c2012-07-31 22:19:25 +00001625 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001626}
1627
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001628ThreadPlanSP
Jim Ingham18de2fd2012-05-10 01:35:39 +00001629Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001630{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001631 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001632 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001633 return ThreadPlanSP();
Jim Ingham25f66702011-12-03 01:52:59 +00001634
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001635 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001636 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001637}
1638
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001639ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001640Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1641 Address &target_addr,
1642 bool stop_other_threads)
1643{
1644 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1645 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001646 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001647}
1648
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001649ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001650Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001651 lldb::addr_t *address_list,
1652 size_t num_addresses,
1653 bool stop_other_threads,
1654 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655{
Greg Clayton481cef22011-01-21 06:11:58 +00001656 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001657 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001658 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001659
1660}
1661
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001662lldb::ThreadPlanSP
1663Thread::QueueThreadPlanForStepScripted (bool abort_other_plans,
1664 const char *class_name,
1665 bool stop_other_threads)
1666{
1667 ThreadPlanSP thread_plan_sp (new ThreadPlanPython (*this, class_name));
1668 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1669 // This seems a little funny, but I don't want to have to split up the constructor and the
1670 // DidPush in the scripted plan, that seems annoying.
1671 // That means the constructor has to be in DidPush.
1672 // So I have to validate the plan AFTER pushing it, and then take it off again...
1673 if (!thread_plan_sp->ValidatePlan(nullptr))
1674 {
1675 DiscardThreadPlansUpToPlan(thread_plan_sp);
1676 return ThreadPlanSP();
1677 }
1678 else
1679 return thread_plan_sp;
1680
1681}
1682
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683uint32_t
1684Thread::GetIndexID () const
1685{
1686 return m_index_id;
1687}
1688
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001689static void
1690PrintPlanElement (Stream *s, const ThreadPlanSP &plan, lldb::DescriptionLevel desc_level, int32_t elem_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001691{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001692 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001693 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001694 s->Printf ("Element %d: ", elem_idx);
1695 plan->GetDescription (s, desc_level);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001696 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001697 s->IndentLess();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001698}
1699
1700static void
1701PrintPlanStack (Stream *s, const std::vector<lldb::ThreadPlanSP> &plan_stack, lldb::DescriptionLevel desc_level, bool include_internal)
1702{
1703 int32_t print_idx = 0;
1704 for (ThreadPlanSP plan_sp : plan_stack)
1705 {
1706 if (include_internal || !plan_sp->GetPrivate())
1707 {
1708 PrintPlanElement (s, plan_sp, desc_level, print_idx++);
1709 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001710 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001711}
1712
1713void
1714Thread::DumpThreadPlans (Stream *s,
1715 lldb::DescriptionLevel desc_level,
1716 bool include_internal,
1717 bool ignore_boring_threads) const
1718{
Jason Molenda60b5da62014-10-16 07:53:46 +00001719 uint32_t stack_size;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001720
1721 if (ignore_boring_threads)
1722 {
1723 uint32_t stack_size = m_plan_stack.size();
1724 uint32_t completed_stack_size = m_completed_plan_stack.size();
1725 uint32_t discarded_stack_size = m_discarded_plan_stack.size();
1726 if (stack_size == 1 && completed_stack_size == 0 && discarded_stack_size == 0)
1727 {
1728 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 "\n", GetIndexID(), GetID());
1729 s->IndentMore();
1730 s->Indent();
1731 s->Printf("No active thread plans\n");
1732 s->IndentLess();
1733 return;
1734 }
1735 }
1736
1737 s->Indent();
1738 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 ":\n", GetIndexID(), GetID());
1739 s->IndentMore();
1740 s->Indent();
1741 s->Printf ("Active plan stack:\n");
1742 PrintPlanStack (s, m_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001743
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001744 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001745 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001746 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001747 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001748 s->Printf ("Completed Plan Stack:\n");
1749 PrintPlanStack (s, m_completed_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001750 }
1751
1752 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001753 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001754 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001755 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001756 s->Printf ("Discarded Plan Stack:\n");
1757 PrintPlanStack (s, m_discarded_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001758 }
1759
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001760 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001761}
1762
Greg Claytond9e416c2012-02-18 05:35:26 +00001763TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001764Thread::CalculateTarget ()
1765{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001766 TargetSP target_sp;
1767 ProcessSP process_sp(GetProcess());
1768 if (process_sp)
1769 target_sp = process_sp->CalculateTarget();
1770 return target_sp;
1771
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001772}
1773
Greg Claytond9e416c2012-02-18 05:35:26 +00001774ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001775Thread::CalculateProcess ()
1776{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001777 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001778}
1779
Greg Claytond9e416c2012-02-18 05:35:26 +00001780ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001781Thread::CalculateThread ()
1782{
Greg Claytond9e416c2012-02-18 05:35:26 +00001783 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001784}
1785
Jason Molendab57e4a12013-11-04 09:33:30 +00001786StackFrameSP
1787Thread::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001788{
Jason Molendab57e4a12013-11-04 09:33:30 +00001789 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001790}
1791
1792void
Greg Clayton0603aa92010-10-04 01:05:56 +00001793Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001794{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001795 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001796}
1797
Greg Clayton12daf9462010-08-25 00:35:26 +00001798
Greg Clayton39d0ab32012-03-29 01:41:38 +00001799StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001800Thread::GetStackFrameList ()
1801{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001802 StackFrameListSP frame_list_sp;
1803 Mutex::Locker locker(m_frame_mutex);
1804 if (m_curr_frames_sp)
1805 {
1806 frame_list_sp = m_curr_frames_sp;
1807 }
1808 else
1809 {
1810 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1811 m_curr_frames_sp = frame_list_sp;
1812 }
1813 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001814}
1815
Greg Clayton12daf9462010-08-25 00:35:26 +00001816void
1817Thread::ClearStackFrames ()
1818{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001819 Mutex::Locker locker(m_frame_mutex);
1820
Greg Clayton160c9d82013-05-01 21:54:04 +00001821 Unwind *unwinder = GetUnwinder ();
1822 if (unwinder)
1823 unwinder->Clear();
1824
Jim Inghamb0c72a52012-02-29 03:40:22 +00001825 // Only store away the old "reference" StackFrameList if we got all its frames:
1826 // FIXME: At some point we can try to splice in the frames we have fetched into
1827 // the new frame as we make it, but let's not try that now.
1828 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001829 m_prev_frames_sp.swap (m_curr_frames_sp);
1830 m_curr_frames_sp.reset();
Jason Molenda705b1802014-06-13 02:37:02 +00001831
1832 m_extended_info.reset();
1833 m_extended_info_fetched = false;
Jim Ingham87c11912010-08-12 02:14:28 +00001834}
1835
Jason Molendab57e4a12013-11-04 09:33:30 +00001836lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001837Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1838{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001839 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001840}
1841
Jim Ingham44137582012-09-12 00:40:39 +00001842
1843Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001844Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001845{
Jason Molendab57e4a12013-11-04 09:33:30 +00001846 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001847 Error return_error;
1848
1849 if (!frame_sp)
1850 {
Daniel Malead01b2952012-11-29 21:49:15 +00001851 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001852 }
1853
Jim Ingham4f465cf2012-10-10 18:32:14 +00001854 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001855}
1856
1857Error
Jason Molendab57e4a12013-11-04 09:33:30 +00001858Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001859{
1860 Error return_error;
1861
1862 if (!frame_sp)
1863 {
1864 return_error.SetErrorString("Can't return to a null frame.");
1865 return return_error;
1866 }
1867
1868 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001869 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
Jason Molendab57e4a12013-11-04 09:33:30 +00001870 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001871 if (!older_frame_sp)
1872 {
1873 return_error.SetErrorString("No older frame to return to.");
1874 return return_error;
1875 }
Jim Ingham44137582012-09-12 00:40:39 +00001876
1877 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001878 {
Jim Ingham44137582012-09-12 00:40:39 +00001879 lldb::ABISP abi = thread->GetProcess()->GetABI();
1880 if (!abi)
1881 {
1882 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001883 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001884 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001885 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1886
1887 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1888 // Turn that back on when that works.
Jason Molenda97e704b2014-10-16 08:07:20 +00001889 if (/* DISABLES CODE */ (0) && sc.function != NULL)
Jim Ingham1f51e602012-09-27 01:15:29 +00001890 {
1891 Type *function_type = sc.function->GetType();
1892 if (function_type)
1893 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001894 ClangASTType return_type = sc.function->GetClangType().GetFunctionReturnType();
Jim Ingham1f51e602012-09-27 01:15:29 +00001895 if (return_type)
1896 {
Jim Ingham1f51e602012-09-27 01:15:29 +00001897 StreamString s;
Greg Clayton57ee3062013-07-11 22:46:58 +00001898 return_type.DumpTypeDescription(&s);
1899 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham1f51e602012-09-27 01:15:29 +00001900 if (cast_value_sp)
1901 {
1902 cast_value_sp->SetFormat(eFormatHex);
1903 return_value_sp = cast_value_sp;
1904 }
1905 }
1906 }
1907 }
1908
Jim Inghamcb640dd2012-09-14 02:14:15 +00001909 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001910 if (!return_error.Success())
1911 return return_error;
1912 }
1913
1914 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001915 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001916 // cook their data
1917
Jason Molendab57e4a12013-11-04 09:33:30 +00001918 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
Jim Ingham93208b82013-01-31 21:46:01 +00001919 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001920 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001921 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1922 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001923 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001924 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1925 if (copy_success)
1926 {
1927 thread->DiscardThreadPlans(true);
1928 thread->ClearStackFrames();
1929 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1930 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1931 }
1932 else
1933 {
1934 return_error.SetErrorString("Could not reset register values.");
1935 }
Jim Ingham93208b82013-01-31 21:46:01 +00001936 }
1937 else
1938 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001939 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001940 }
Jim Ingham44137582012-09-12 00:40:39 +00001941 }
1942 else
1943 {
Jim Ingham93208b82013-01-31 21:46:01 +00001944 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001945 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001946 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001947}
1948
Richard Mittonf86248d2013-09-12 02:20:34 +00001949static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1950{
1951 for (size_t n=0;n<list.size();n++)
1952 {
1953 s << "\t";
1954 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1955 s << "\n";
1956 }
1957}
1958
1959Error
1960Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1961{
1962 ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
1963 Target *target = exe_ctx.GetTargetPtr();
1964 TargetSP target_sp = exe_ctx.GetTargetSP();
1965 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00001966 StackFrame *frame = exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00001967 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1968
1969 // Find candidate locations.
1970 std::vector<Address> candidates, within_function, outside_function;
1971 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
1972
1973 // If possible, we try and stay within the current function.
1974 // Within a function, we accept multiple locations (optimized code may do this,
1975 // there's no solution here so we do the best we can).
1976 // However if we're trying to leave the function, we don't know how to pick the
1977 // right location, so if there's more than one then we bail.
1978 if (!within_function.empty())
1979 candidates = within_function;
1980 else if (outside_function.size() == 1 && can_leave_function)
1981 candidates = outside_function;
1982
1983 // Check if we got anything.
1984 if (candidates.empty())
1985 {
1986 if (outside_function.empty())
1987 {
1988 return Error("Cannot locate an address for %s:%i.",
1989 file.GetFilename().AsCString(), line);
1990 }
1991 else if (outside_function.size() == 1)
1992 {
1993 return Error("%s:%i is outside the current function.",
1994 file.GetFilename().AsCString(), line);
1995 }
1996 else
1997 {
1998 StreamString sstr;
1999 DumpAddressList(sstr, outside_function, target);
2000 return Error("%s:%i has multiple candidate locations:\n%s",
2001 file.GetFilename().AsCString(), line, sstr.GetString().c_str());
2002 }
2003 }
2004
2005 // Accept the first location, warn about any others.
2006 Address dest = candidates[0];
2007 if (warnings && candidates.size() > 1)
2008 {
2009 StreamString sstr;
2010 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
2011 file.GetFilename().AsCString(), line);
2012 DumpAddressList(sstr, candidates, target);
2013 *warnings = sstr.GetString();
2014 }
2015
2016 if (!reg_ctx->SetPC (dest))
2017 return Error("Cannot change PC to target address.");
2018
2019 return Error();
2020}
2021
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002022void
Greg Clayton0603aa92010-10-04 01:05:56 +00002023Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002024{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002025 ExecutionContext exe_ctx (shared_from_this());
2026 Process *process = exe_ctx.GetProcessPtr();
2027 if (process == NULL)
2028 return;
2029
Jason Molendab57e4a12013-11-04 09:33:30 +00002030 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00002031 SymbolContext frame_sc;
Jim Inghamd1f25362015-01-28 01:17:26 +00002032 if (frame_idx != LLDB_INVALID_FRAME_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002033 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002034 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002035 if (frame_sp)
2036 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002037 exe_ctx.SetFrameSP(frame_sp);
2038 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002039 }
2040 }
2041
Greg Clayton554f68d2015-02-04 22:00:53 +00002042 const FormatEntity::Entry *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00002043 assert (thread_format);
Greg Clayton554f68d2015-02-04 22:00:53 +00002044
2045 FormatEntity::Format(*thread_format,
2046 strm,
2047 frame_sp ? &frame_sc : NULL,
2048 &exe_ctx,
2049 NULL,
2050 NULL,
2051 false,
2052 false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002053}
2054
Greg Clayton99d0faf2010-11-18 23:32:35 +00002055void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002056Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00002057{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002058}
Jim Inghamee8aea12010-09-08 03:14:33 +00002059
Greg Clayton99d0faf2010-11-18 23:32:35 +00002060void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002061Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002062{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002063}
Jim Inghamee8aea12010-09-08 03:14:33 +00002064
Richard Mitton0a558352013-10-17 21:14:00 +00002065lldb::addr_t
2066Thread::GetThreadPointer ()
2067{
2068 return LLDB_INVALID_ADDRESS;
2069}
2070
2071addr_t
2072Thread::GetThreadLocalData (const ModuleSP module)
2073{
2074 // The default implementation is to ask the dynamic loader for it.
2075 // This can be overridden for specific platforms.
2076 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
2077 if (loader)
2078 return loader->GetThreadLocalData (module, shared_from_this());
2079 else
2080 return LLDB_INVALID_ADDRESS;
2081}
2082
Jason Molendab4892cd2014-05-13 22:02:48 +00002083bool
2084Thread::SafeToCallFunctions ()
2085{
2086 Process *process = GetProcess().get();
2087 if (process)
2088 {
2089 SystemRuntime *runtime = process->GetSystemRuntime ();
2090 if (runtime)
2091 {
2092 return runtime->SafeToCallFunctionsOnThisThread (shared_from_this());
2093 }
2094 }
2095 return true;
2096}
2097
Jason Molendab57e4a12013-11-04 09:33:30 +00002098lldb::StackFrameSP
2099Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
Jim Inghame4284b72010-09-23 17:40:12 +00002100{
Jason Molendab57e4a12013-11-04 09:33:30 +00002101 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00002102}
Caroline Ticeceb6b132010-10-26 03:11:13 +00002103
2104const char *
2105Thread::StopReasonAsCString (lldb::StopReason reason)
2106{
2107 switch (reason)
2108 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00002109 case eStopReasonInvalid: return "invalid";
2110 case eStopReasonNone: return "none";
2111 case eStopReasonTrace: return "trace";
2112 case eStopReasonBreakpoint: return "breakpoint";
2113 case eStopReasonWatchpoint: return "watchpoint";
2114 case eStopReasonSignal: return "signal";
2115 case eStopReasonException: return "exception";
2116 case eStopReasonExec: return "exec";
2117 case eStopReasonPlanComplete: return "plan complete";
2118 case eStopReasonThreadExiting: return "thread exiting";
Kuba Breckaafdf8422014-10-10 23:43:03 +00002119 case eStopReasonInstrumentation: return "instrumentation break";
Caroline Ticeceb6b132010-10-26 03:11:13 +00002120 }
2121
2122
2123 static char unknown_state_string[64];
2124 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
2125 return unknown_state_string;
2126}
2127
2128const char *
2129Thread::RunModeAsCString (lldb::RunMode mode)
2130{
2131 switch (mode)
2132 {
2133 case eOnlyThisThread: return "only this thread";
2134 case eAllThreads: return "all threads";
2135 case eOnlyDuringStepping: return "only during stepping";
2136 }
2137
2138 static char unknown_state_string[64];
2139 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
2140 return unknown_state_string;
2141}
Jim Ingham06e827c2010-11-11 19:26:09 +00002142
Greg Clayton7260f622011-04-18 08:33:37 +00002143size_t
2144Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
2145{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002146 ExecutionContext exe_ctx (shared_from_this());
2147 Target *target = exe_ctx.GetTargetPtr();
2148 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00002149 size_t num_frames_shown = 0;
2150 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00002151 bool is_selected = false;
2152 if (process)
2153 {
2154 if (process->GetThreadList().GetSelectedThread().get() == this)
2155 is_selected = true;
2156 }
2157 strm.Printf("%c ", is_selected ? '*' : ' ');
2158 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00002159 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002160 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00002161 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00002162 {
Jim Inghame610d642011-08-16 00:07:28 +00002163 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
2164 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
2165 {
2166 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
2167 }
Greg Clayton5113dc82011-08-12 06:47:54 +00002168 }
Greg Clayton7260f622011-04-18 08:33:37 +00002169 }
2170
2171 DumpUsingSettingsFormat (strm, start_frame);
2172
2173 if (num_frames > 0)
2174 {
2175 strm.IndentMore();
2176
2177 const bool show_frame_info = true;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002178
2179 const char *selected_frame_marker = NULL;
2180 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
2181 strm.IndentMore ();
2182 else
2183 selected_frame_marker = "* ";
2184
Greg Clayton39d0ab32012-03-29 01:41:38 +00002185 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
2186 start_frame,
2187 num_frames,
2188 show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002189 num_frames_with_source,
2190 selected_frame_marker);
2191 if (num_frames == 1)
2192 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00002193 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00002194 }
2195 return num_frames_shown;
2196}
2197
Jason Molenda705b1802014-06-13 02:37:02 +00002198bool
Kuba Breckaafdf8422014-10-10 23:43:03 +00002199Thread::GetDescription (Stream &strm, lldb::DescriptionLevel level, bool print_json_thread, bool print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002200{
2201 DumpUsingSettingsFormat (strm, 0);
2202 strm.Printf("\n");
2203
2204 StructuredData::ObjectSP thread_info = GetExtendedInfo();
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002205
Kuba Breckaafdf8422014-10-10 23:43:03 +00002206 if (print_json_thread || print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002207 {
Kuba Breckaafdf8422014-10-10 23:43:03 +00002208 if (thread_info && print_json_thread)
2209 {
2210 thread_info->Dump (strm);
2211 strm.Printf("\n");
2212 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002213
2214 if (print_json_stopinfo && m_stop_info_sp)
Kuba Breckaafdf8422014-10-10 23:43:03 +00002215 {
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002216 StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo();
2217 if (stop_info)
2218 {
2219 stop_info->Dump (strm);
2220 strm.Printf("\n");
2221 }
Kuba Breckaafdf8422014-10-10 23:43:03 +00002222 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002223
Jason Molenda705b1802014-06-13 02:37:02 +00002224 return true;
2225 }
2226
2227 if (thread_info)
2228 {
2229 StructuredData::ObjectSP activity = thread_info->GetObjectForDotSeparatedPath("activity");
2230 StructuredData::ObjectSP breadcrumb = thread_info->GetObjectForDotSeparatedPath("breadcrumb");
2231 StructuredData::ObjectSP messages = thread_info->GetObjectForDotSeparatedPath("trace_messages");
2232
2233 bool printed_activity = false;
2234 if (activity && activity->GetType() == StructuredData::Type::eTypeDictionary)
2235 {
2236 StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
2237 StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
2238 StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
2239 if (name && name->GetType() == StructuredData::Type::eTypeString
2240 && id && id->GetType() == StructuredData::Type::eTypeInteger)
2241 {
2242 strm.Printf(" Activity '%s', 0x%" PRIx64 "\n", name->GetAsString()->GetValue().c_str(), id->GetAsInteger()->GetValue());
2243 }
2244 printed_activity = true;
2245 }
2246 bool printed_breadcrumb = false;
2247 if (breadcrumb && breadcrumb->GetType() == StructuredData::Type::eTypeDictionary)
2248 {
2249 if (printed_activity)
2250 strm.Printf ("\n");
2251 StructuredData::Dictionary *breadcrumb_dict = breadcrumb->GetAsDictionary();
2252 StructuredData::ObjectSP breadcrumb_text = breadcrumb_dict->GetValueForKey ("name");
2253 if (breadcrumb_text && breadcrumb_text->GetType() == StructuredData::Type::eTypeString)
2254 {
2255 strm.Printf (" Current Breadcrumb: %s\n", breadcrumb_text->GetAsString()->GetValue().c_str());
2256 }
2257 printed_breadcrumb = true;
2258 }
2259 if (messages && messages->GetType() == StructuredData::Type::eTypeArray)
2260 {
2261 if (printed_breadcrumb)
2262 strm.Printf("\n");
2263 StructuredData::Array *messages_array = messages->GetAsArray();
2264 const size_t msg_count = messages_array->GetSize();
2265 if (msg_count > 0)
2266 {
2267 strm.Printf (" %zu trace messages:\n", msg_count);
2268 for (size_t i = 0; i < msg_count; i++)
2269 {
2270 StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
2271 if (message && message->GetType() == StructuredData::Type::eTypeDictionary)
2272 {
2273 StructuredData::Dictionary *message_dict = message->GetAsDictionary();
2274 StructuredData::ObjectSP message_text = message_dict->GetValueForKey ("message");
2275 if (message_text && message_text->GetType() == StructuredData::Type::eTypeString)
2276 {
2277 strm.Printf (" %s\n", message_text->GetAsString()->GetValue().c_str());
2278 }
2279 }
2280 }
2281 }
2282 }
2283 }
2284
2285 return true;
2286}
2287
Greg Clayton7260f622011-04-18 08:33:37 +00002288size_t
2289Thread::GetStackFrameStatus (Stream& strm,
2290 uint32_t first_frame,
2291 uint32_t num_frames,
2292 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002293 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00002294{
Greg Clayton39d0ab32012-03-29 01:41:38 +00002295 return GetStackFrameList()->GetStatus (strm,
2296 first_frame,
2297 num_frames,
2298 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002299 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00002300}
2301
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002302Unwind *
2303Thread::GetUnwinder ()
2304{
2305 if (m_unwinder_ap.get() == NULL)
2306 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00002307 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002308 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2309 switch (machine)
2310 {
2311 case llvm::Triple::x86_64:
2312 case llvm::Triple::x86:
2313 case llvm::Triple::arm:
Todd Fialad8eaa172014-07-23 14:37:35 +00002314 case llvm::Triple::aarch64:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002315 case llvm::Triple::thumb:
Bhushan D. Attarde794a4d52015-05-15 06:53:30 +00002316 case llvm::Triple::mips:
2317 case llvm::Triple::mipsel:
Ed Masteb73f8442013-10-10 00:59:47 +00002318 case llvm::Triple::mips64:
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +00002319 case llvm::Triple::mips64el:
Justin Hibbits6256a0e2014-10-31 02:34:28 +00002320 case llvm::Triple::ppc:
Justin Hibbitsdb39cdf2014-10-31 15:57:52 +00002321 case llvm::Triple::ppc64:
Deepak Panickal6d3df422014-02-19 11:16:46 +00002322 case llvm::Triple::hexagon:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002323 m_unwinder_ap.reset (new UnwindLLDB (*this));
2324 break;
2325
2326 default:
2327 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2328 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2329 break;
2330 }
2331 }
2332 return m_unwinder_ap.get();
2333}
2334
2335
Greg Claytonfa559e52012-05-18 02:38:05 +00002336void
2337Thread::Flush ()
2338{
2339 ClearStackFrames ();
2340 m_reg_context_sp.reset();
2341}
Jim Ingham5d88a062012-10-16 00:09:33 +00002342
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00002343bool
Jim Ingham5d88a062012-10-16 00:09:33 +00002344Thread::IsStillAtLastBreakpointHit ()
2345{
2346 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2347 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2348 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002349 if (m_stop_info_sp) {
2350 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00002351 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002352 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00002353 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2354 if (reg_ctx_sp)
2355 {
2356 lldb::addr_t pc = reg_ctx_sp->GetPC();
2357 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002358 if (bp_site_sp &&
2359 static_cast<break_id_t>(value) == bp_site_sp->GetID())
Greg Clayton1afa68e2013-04-02 20:32:37 +00002360 return true;
2361 }
Jim Ingham5d88a062012-10-16 00:09:33 +00002362 }
2363 }
2364 return false;
2365}
Greg Clayton44d93782014-01-27 23:43:24 +00002366
2367
2368Error
2369Thread::StepIn (bool source_step,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002370 LazyBool step_in_avoids_code_without_debug_info,
2371 LazyBool step_out_avoids_code_without_debug_info)
2372
Greg Clayton44d93782014-01-27 23:43:24 +00002373{
2374 Error error;
2375 Process *process = GetProcess().get();
2376 if (StateIsStoppedState (process->GetState(), true))
2377 {
2378 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2379 ThreadPlanSP new_plan_sp;
2380 const lldb::RunMode run_mode = eOnlyThisThread;
2381 const bool abort_other_plans = false;
2382
2383 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2384 {
2385 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2386 new_plan_sp = QueueThreadPlanForStepInRange (abort_other_plans,
2387 sc.line_entry.range,
2388 sc,
2389 NULL,
2390 run_mode,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002391 step_in_avoids_code_without_debug_info,
2392 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002393 }
2394 else
2395 {
2396 new_plan_sp = QueueThreadPlanForStepSingleInstruction (false,
2397 abort_other_plans,
2398 run_mode);
2399 }
2400
2401 new_plan_sp->SetIsMasterPlan(true);
2402 new_plan_sp->SetOkayToDiscard(false);
2403
2404 // Why do we need to set the current thread by ID here???
2405 process->GetThreadList().SetSelectedThreadByID (GetID());
2406 error = process->Resume();
2407 }
2408 else
2409 {
2410 error.SetErrorString("process not stopped");
2411 }
2412 return error;
2413}
2414
2415Error
Jim Ingham4b4b2472014-03-13 02:47:14 +00002416Thread::StepOver (bool source_step,
2417 LazyBool step_out_avoids_code_without_debug_info)
Greg Clayton44d93782014-01-27 23:43:24 +00002418{
2419 Error error;
2420 Process *process = GetProcess().get();
2421 if (StateIsStoppedState (process->GetState(), true))
2422 {
2423 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2424 ThreadPlanSP new_plan_sp;
2425
2426 const lldb::RunMode run_mode = eOnlyThisThread;
2427 const bool abort_other_plans = false;
2428
2429 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2430 {
2431 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2432 new_plan_sp = QueueThreadPlanForStepOverRange (abort_other_plans,
2433 sc.line_entry.range,
2434 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002435 run_mode,
2436 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002437 }
2438 else
2439 {
2440 new_plan_sp = QueueThreadPlanForStepSingleInstruction (true,
2441 abort_other_plans,
2442 run_mode);
2443 }
2444
2445 new_plan_sp->SetIsMasterPlan(true);
2446 new_plan_sp->SetOkayToDiscard(false);
2447
2448 // Why do we need to set the current thread by ID here???
2449 process->GetThreadList().SetSelectedThreadByID (GetID());
2450 error = process->Resume();
2451 }
2452 else
2453 {
2454 error.SetErrorString("process not stopped");
2455 }
2456 return error;
2457}
2458
2459Error
2460Thread::StepOut ()
2461{
2462 Error error;
2463 Process *process = GetProcess().get();
2464 if (StateIsStoppedState (process->GetState(), true))
2465 {
2466 const bool first_instruction = false;
2467 const bool stop_other_threads = false;
2468 const bool abort_other_plans = false;
2469
2470 ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut (abort_other_plans,
2471 NULL,
2472 first_instruction,
2473 stop_other_threads,
2474 eVoteYes,
2475 eVoteNoOpinion,
2476 0));
2477
2478 new_plan_sp->SetIsMasterPlan(true);
2479 new_plan_sp->SetOkayToDiscard(false);
2480
2481 // Why do we need to set the current thread by ID here???
2482 process->GetThreadList().SetSelectedThreadByID (GetID());
2483 error = process->Resume();
2484 }
2485 else
2486 {
2487 error.SetErrorString("process not stopped");
2488 }
2489 return error;
Jim Ingham4b4b2472014-03-13 02:47:14 +00002490}