blob: 3acc74a58bacc4f8f0a408bb226cabc47acde73a [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Jim Ingham1b54c882010-06-16 02:00:15 +000010#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000011#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/Log.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000013#include "lldb/Core/FormatEntity.h"
Jason Molenda03c9cd02015-08-06 21:54:29 +000014#include "lldb/Core/Module.h"
Greg Clayton160c9d82013-05-01 21:54:04 +000015#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Stream.h"
17#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000018#include "lldb/Core/RegularExpression.h"
Greg Clayton7260f622011-04-18 08:33:37 +000019#include "lldb/Host/Host.h"
Jim Ingham4da62062014-01-23 21:52:47 +000020#include "lldb/Interpreter/OptionValueFileSpecList.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000021#include "lldb/Interpreter/OptionValueProperties.h"
22#include "lldb/Interpreter/Property.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000023#include "lldb/Symbol/Function.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000024#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Target/DynamicLoader.h"
26#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000027#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Target/Process.h"
29#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000030#include "lldb/Target/StopInfo.h"
Jason Molendab4892cd2014-05-13 22:02:48 +000031#include "lldb/Target/SystemRuntime.h"
Greg Clayton896dff62010-07-23 16:45:51 +000032#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Target/Thread.h"
34#include "lldb/Target/ThreadPlan.h"
35#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Target/ThreadPlanBase.h"
Jim Ingham2bdbfd52014-09-29 23:17:18 +000037#include "lldb/Target/ThreadPlanPython.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "lldb/Target/ThreadPlanStepInstruction.h"
39#include "lldb/Target/ThreadPlanStepOut.h"
40#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
41#include "lldb/Target/ThreadPlanStepThrough.h"
42#include "lldb/Target/ThreadPlanStepInRange.h"
43#include "lldb/Target/ThreadPlanStepOverRange.h"
44#include "lldb/Target/ThreadPlanRunToAddress.h"
45#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000046#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000047#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000048#include "Plugins/Process/Utility/UnwindLLDB.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000049#include "Plugins/Process/Utility/UnwindMacOSXFrameBackchain.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000050
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051
52using namespace lldb;
53using namespace lldb_private;
54
Greg Clayton67cc0632012-08-22 17:17:09 +000055
56const ThreadPropertiesSP &
57Thread::GetGlobalProperties()
58{
59 static ThreadPropertiesSP g_settings_sp;
60 if (!g_settings_sp)
61 g_settings_sp.reset (new ThreadProperties (true));
62 return g_settings_sp;
63}
64
65static PropertyDefinition
66g_properties[] =
67{
Jim Ingham4b4b2472014-03-13 02:47:14 +000068 { "step-in-avoid-nodebug", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, step-in will not stop in functions with no debug information." },
69 { "step-out-avoid-nodebug", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true, when step-in/step-out/step-over leave the current frame, they will continue to step out till they come to a function with "
70 "debug information. Passing a frame argument to step-out will override this option." },
Greg Clayton7bd4c602015-01-21 21:51:02 +000071 { "step-avoid-regexp", OptionValue::eTypeRegex , true , 0, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
72 { "step-avoid-libraries", OptionValue::eTypeFileSpecList , true , 0, NULL, NULL, "A list of libraries that source stepping won't stop in." },
Greg Clayton67cc0632012-08-22 17:17:09 +000073 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
74 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
75};
76
77enum {
Jim Ingham4b4b2472014-03-13 02:47:14 +000078 ePropertyStepInAvoidsNoDebug,
79 ePropertyStepOutAvoidsNoDebug,
Greg Clayton67cc0632012-08-22 17:17:09 +000080 ePropertyStepAvoidRegex,
Jim Ingham4da62062014-01-23 21:52:47 +000081 ePropertyStepAvoidLibraries,
Greg Clayton67cc0632012-08-22 17:17:09 +000082 ePropertyEnableThreadTrace
83};
84
85
86class ThreadOptionValueProperties : public OptionValueProperties
87{
88public:
89 ThreadOptionValueProperties (const ConstString &name) :
90 OptionValueProperties (name)
91 {
92 }
93
94 // This constructor is used when creating ThreadOptionValueProperties when it
95 // is part of a new lldb_private::Thread instance. It will copy all current
96 // global property values as needed
97 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +000098 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +000099 {
100 }
101
102 virtual const Property *
103 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
104 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000105 // When getting the value for a key from the thread options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +0000106 // try and grab the setting from the current thread if there is one. Else we just
107 // use the one from this instance.
108 if (exe_ctx)
109 {
110 Thread *thread = exe_ctx->GetThreadPtr();
111 if (thread)
112 {
113 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
114 if (this != instance_properties)
115 return instance_properties->ProtectedGetPropertyAtIndex (idx);
116 }
117 }
118 return ProtectedGetPropertyAtIndex (idx);
119 }
120};
121
122
123
124ThreadProperties::ThreadProperties (bool is_global) :
125 Properties ()
126{
127 if (is_global)
128 {
129 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
130 m_collection_sp->Initialize(g_properties);
131 }
132 else
133 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
134}
135
136ThreadProperties::~ThreadProperties()
137{
138}
139
140const RegularExpression *
141ThreadProperties::GetSymbolsToAvoidRegexp()
142{
143 const uint32_t idx = ePropertyStepAvoidRegex;
144 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
145}
146
Jim Ingham4da62062014-01-23 21:52:47 +0000147FileSpecList &
148ThreadProperties::GetLibrariesToAvoid() const
149{
150 const uint32_t idx = ePropertyStepAvoidLibraries;
151 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
152 assert(option_value);
153 return option_value->GetCurrentValue();
154}
155
Greg Clayton67cc0632012-08-22 17:17:09 +0000156bool
157ThreadProperties::GetTraceEnabledState() const
158{
159 const uint32_t idx = ePropertyEnableThreadTrace;
160 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
161}
162
Jim Ingham4b4b2472014-03-13 02:47:14 +0000163bool
164ThreadProperties::GetStepInAvoidsNoDebug() const
165{
166 const uint32_t idx = ePropertyStepInAvoidsNoDebug;
167 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
168}
169
170bool
171ThreadProperties::GetStepOutAvoidsNoDebug() const
172{
173 const uint32_t idx = ePropertyStepOutAvoidsNoDebug;
174 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
175}
176
177
Jim Ingham4f465cf2012-10-10 18:32:14 +0000178//------------------------------------------------------------------
179// Thread Event Data
180//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000181
Jim Ingham4f465cf2012-10-10 18:32:14 +0000182
183const ConstString &
184Thread::ThreadEventData::GetFlavorString ()
185{
186 static ConstString g_flavor ("Thread::ThreadEventData");
187 return g_flavor;
188}
189
190Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
191 m_thread_sp (thread_sp),
192 m_stack_id ()
193{
194}
195
196Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
197 m_thread_sp (thread_sp),
198 m_stack_id (stack_id)
199{
200}
201
202Thread::ThreadEventData::ThreadEventData () :
203 m_thread_sp (),
204 m_stack_id ()
205{
206}
207
208Thread::ThreadEventData::~ThreadEventData ()
209{
210}
211
212void
213Thread::ThreadEventData::Dump (Stream *s) const
214{
215
216}
217
218const Thread::ThreadEventData *
219Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
220{
221 if (event_ptr)
222 {
223 const EventData *event_data = event_ptr->GetData();
224 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
225 return static_cast <const ThreadEventData *> (event_ptr->GetData());
226 }
227 return NULL;
228}
229
230ThreadSP
231Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
232{
233 ThreadSP thread_sp;
234 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
235 if (event_data)
236 thread_sp = event_data->GetThread();
237 return thread_sp;
238}
239
240StackID
241Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
242{
243 StackID stack_id;
244 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
245 if (event_data)
246 stack_id = event_data->GetStackID();
247 return stack_id;
248}
249
Jason Molendab57e4a12013-11-04 09:33:30 +0000250StackFrameSP
Jim Ingham4f465cf2012-10-10 18:32:14 +0000251Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
252{
253 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
Jason Molendab57e4a12013-11-04 09:33:30 +0000254 StackFrameSP frame_sp;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000255 if (event_data)
256 {
257 ThreadSP thread_sp = event_data->GetThread();
258 if (thread_sp)
259 {
260 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
261 }
262 }
263 return frame_sp;
264}
265
266//------------------------------------------------------------------
267// Thread class
268//------------------------------------------------------------------
269
270ConstString &
271Thread::GetStaticBroadcasterClass ()
272{
273 static ConstString class_name ("lldb.thread");
274 return class_name;
275}
276
Jason Molendaa8ff5432014-03-06 06:31:18 +0000277Thread::Thread (Process &process, lldb::tid_t tid, bool use_invalid_index_id) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000278 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000280 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
281 m_process_wp (process.shared_from_this()),
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000282 m_stop_info_sp (),
283 m_stop_info_stop_id (0),
Greg Claytona97c4d22014-12-09 23:31:02 +0000284 m_stop_info_override_stop_id (0),
Jason Molendaa8ff5432014-03-06 06:31:18 +0000285 m_index_id (use_invalid_index_id ? LLDB_INVALID_INDEX32 : process.GetNextThreadIndexID(tid)),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000288 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000291 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000292 m_curr_frames_sp (),
293 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000295 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000296 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000297 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000298 m_destroy_called (false),
Jason Molenda705b1802014-06-13 02:37:02 +0000299 m_override_should_notify (eLazyBoolCalculate),
300 m_extended_info_fetched (false),
301 m_extended_info ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302{
Greg Clayton5160ce52013-03-27 23:08:40 +0000303 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000305 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")",
306 static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307
Jim Ingham4f465cf2012-10-10 18:32:14 +0000308 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309 QueueFundamentalPlan(true);
310}
311
312
313Thread::~Thread()
314{
Greg Clayton5160ce52013-03-27 23:08:40 +0000315 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000317 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")",
318 static_cast<void*>(this), GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000319 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
320 assert (m_destroy_called);
321}
322
323void
324Thread::DestroyThread ()
325{
Jim Inghamb1499242013-10-18 17:11:02 +0000326 // Tell any plans on the plan stacks that the thread is being destroyed since
327 // any plans that have a thread go away in the middle of might need
328 // to do cleanup, or in some cases NOT do cleanup...
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000329 for (auto plan : m_plan_stack)
330 plan->ThreadDestroyed();
331
Jim Inghamb1499242013-10-18 17:11:02 +0000332 for (auto plan : m_discarded_plan_stack)
333 plan->ThreadDestroyed();
334
335 for (auto plan : m_completed_plan_stack)
336 plan->ThreadDestroyed();
337
Greg Clayton35a4cc52012-10-29 20:52:08 +0000338 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000339 m_plan_stack.clear();
340 m_discarded_plan_stack.clear();
341 m_completed_plan_stack.clear();
Greg Clayton6e10f142013-07-30 00:23:06 +0000342
343 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the
344 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
345 // without checking first whether it is destroyed, they won't crash.
346 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
347 m_plan_stack.push_back (null_plan_sp);
348
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000349 m_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000350 m_reg_context_sp.reset();
351 m_unwinder_ap.reset();
352 Mutex::Locker locker(m_frame_mutex);
353 m_curr_frames_sp.reset();
354 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355}
356
Jim Ingham4f465cf2012-10-10 18:32:14 +0000357void
358Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
359{
360 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
361 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
362}
363
Jason Molendaef7d6412015-08-06 03:27:10 +0000364lldb::StackFrameSP
365Thread::GetSelectedFrame()
366{
367 StackFrameListSP stack_frame_list_sp(GetStackFrameList());
368 StackFrameSP frame_sp = stack_frame_list_sp->GetFrameAtIndex (stack_frame_list_sp->GetSelectedFrameIndex());
369 FunctionOptimizationWarning (frame_sp.get());
370 return frame_sp;
371}
372
Jim Ingham4f465cf2012-10-10 18:32:14 +0000373uint32_t
Jason Molendab57e4a12013-11-04 09:33:30 +0000374Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
Jim Ingham4f465cf2012-10-10 18:32:14 +0000375{
376 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
377 if (broadcast)
378 BroadcastSelectedFrameChange(frame->GetStackID());
Jason Molendaef7d6412015-08-06 03:27:10 +0000379 FunctionOptimizationWarning (frame);
Jim Ingham4f465cf2012-10-10 18:32:14 +0000380 return ret_value;
381}
382
383bool
384Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
385{
Jason Molendab57e4a12013-11-04 09:33:30 +0000386 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
Jim Ingham4f465cf2012-10-10 18:32:14 +0000387 if (frame_sp)
388 {
389 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
390 if (broadcast)
391 BroadcastSelectedFrameChange(frame_sp->GetStackID());
Jason Molendaef7d6412015-08-06 03:27:10 +0000392 FunctionOptimizationWarning (frame_sp.get());
Jim Ingham4f465cf2012-10-10 18:32:14 +0000393 return true;
394 }
395 else
396 return false;
397}
398
Jim Ingham93208b82013-01-31 21:46:01 +0000399bool
400Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
401{
402 const bool broadcast = true;
403 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
404 if (success)
405 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000406 StackFrameSP frame_sp = GetSelectedFrame();
Jim Ingham93208b82013-01-31 21:46:01 +0000407 if (frame_sp)
408 {
409 bool already_shown = false;
410 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
411 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
412 {
413 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
414 }
415
416 bool show_frame_info = true;
417 bool show_source = !already_shown;
Jason Molendaef7d6412015-08-06 03:27:10 +0000418 FunctionOptimizationWarning (frame_sp.get());
Jim Ingham93208b82013-01-31 21:46:01 +0000419 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
420 }
421 return false;
422 }
423 else
424 return false;
425}
426
Jason Molendaef7d6412015-08-06 03:27:10 +0000427void
428Thread::FunctionOptimizationWarning (StackFrame *frame)
429{
430 if (frame && frame->HasDebugInformation())
431 {
Jason Molenda03c9cd02015-08-06 21:54:29 +0000432 SymbolContext sc = frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextModule);
433 if (sc.function && sc.function->GetIsOptimized() == true && sc.module_sp.get())
Jason Molendaef7d6412015-08-06 03:27:10 +0000434 {
Jason Molenda03c9cd02015-08-06 21:54:29 +0000435 if (sc.module_sp->GetFileSpec().GetFilename().IsEmpty() == false)
Jason Molendaef7d6412015-08-06 03:27:10 +0000436 {
Jason Molenda03c9cd02015-08-06 21:54:29 +0000437 GetProcess()->PrintWarning (Process::Warnings::eWarningsOptimization, sc.module_sp.get(), "%s was compiled with optimization - stepping may behave oddly; variables may not be available.\n", sc.module_sp->GetFileSpec().GetFilename().GetCString());
Jason Molendaef7d6412015-08-06 03:27:10 +0000438 }
439 }
440 }
441}
442
Jim Ingham4f465cf2012-10-10 18:32:14 +0000443
Jim Inghamb15bfc72010-10-20 00:39:53 +0000444lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000445Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446{
Greg Clayton6e10f142013-07-30 00:23:06 +0000447 if (m_destroy_called)
448 return m_stop_info_sp;
449
Jim Inghamb15bfc72010-10-20 00:39:53 +0000450 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000451 ProcessSP process_sp (GetProcess());
452 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000453 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000454 {
Jim Ingham30fadaf2014-07-08 01:07:32 +0000455 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject(), GetExpressionVariable());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000456 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000457 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000458 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000459 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
460 (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 +0000461 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000462 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000463 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000464 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000465 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000466 GetPrivateStopInfo ();
467 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000468 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000469 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470}
471
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000472lldb::StopInfoSP
473Thread::GetPrivateStopInfo ()
474{
Greg Clayton6e10f142013-07-30 00:23:06 +0000475 if (m_destroy_called)
476 return m_stop_info_sp;
477
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000478 ProcessSP process_sp (GetProcess());
479 if (process_sp)
480 {
Daniel Malea246cb612013-05-14 15:20:12 +0000481 const uint32_t process_stop_id = process_sp->GetStopID();
482 if (m_stop_info_stop_id != process_stop_id)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000483 {
Daniel Malea246cb612013-05-14 15:20:12 +0000484 if (m_stop_info_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000485 {
Daniel Malea246cb612013-05-14 15:20:12 +0000486 if (m_stop_info_sp->IsValid()
487 || IsStillAtLastBreakpointHit()
488 || GetCurrentPlan()->IsVirtualStep())
489 SetStopInfo (m_stop_info_sp);
490 else
491 m_stop_info_sp.reset();
492 }
493
494 if (!m_stop_info_sp)
495 {
496 if (CalculateStopInfo() == false)
497 SetStopInfo (StopInfoSP());
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000498 }
499 }
Greg Claytona97c4d22014-12-09 23:31:02 +0000500
501 // The stop info can be manually set by calling Thread::SetStopInfo()
502 // prior to this function ever getting called, so we can't rely on
503 // "m_stop_info_stop_id != process_stop_id" as the condition for
504 // the if statement below, we must also check the stop info to see
505 // if we need to override it. See the header documentation in
506 // Process::GetStopInfoOverrideCallback() for more information on
507 // the stop info override callback.
508 if (m_stop_info_override_stop_id != process_stop_id)
509 {
510 m_stop_info_override_stop_id = process_stop_id;
511 if (m_stop_info_sp)
512 {
513 ArchSpec::StopInfoOverrideCallbackType callback = GetProcess()->GetStopInfoOverrideCallback();
514 if (callback)
515 callback(*this);
516 }
517 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000518 }
519 return m_stop_info_sp;
520}
521
522
Greg Clayton97d5cf02012-09-25 02:40:06 +0000523lldb::StopReason
524Thread::GetStopReason()
525{
526 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
527 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000528 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000529 return eStopReasonNone;
530}
531
532
Greg Clayton2e309072015-07-17 23:42:28 +0000533bool
534Thread::StopInfoIsUpToDate() const
535{
536 ProcessSP process_sp (GetProcess());
537 if (process_sp)
538 return m_stop_info_stop_id == process_sp->GetStopID();
539 else
540 return true; // Process is no longer around so stop info is always up to date...
541}
Greg Clayton97d5cf02012-09-25 02:40:06 +0000542
Jim Ingham77787032011-01-20 02:03:18 +0000543void
544Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
545{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000546 m_stop_info_sp = stop_info_sp;
547 if (m_stop_info_sp)
Jim Ingham221d51c2013-05-08 00:35:16 +0000548 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000549 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham221d51c2013-05-08 00:35:16 +0000550 // If we are overriding the ShouldReportStop, do that here:
551 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000552 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000553 }
554
Greg Clayton1ac04c32012-02-21 00:09:25 +0000555 ProcessSP process_sp (GetProcess());
556 if (process_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000557 m_stop_info_stop_id = process_sp->GetStopID();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000558 else
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000559 m_stop_info_stop_id = UINT32_MAX;
Greg Clayton8cda7f02013-05-21 21:55:59 +0000560 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
561 if (log)
Ed Maste2bc76432014-06-25 00:38:35 +0000562 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000563 static_cast<void*>(this), GetID(),
564 stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>",
565 m_stop_info_stop_id);
Jim Ingham77787032011-01-20 02:03:18 +0000566}
567
568void
Jim Ingham221d51c2013-05-08 00:35:16 +0000569Thread::SetShouldReportStop (Vote vote)
570{
571 if (vote == eVoteNoOpinion)
572 return;
573 else
574 {
575 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000576 if (m_stop_info_sp)
577 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000578 }
579}
580
581void
Jim Ingham77787032011-01-20 02:03:18 +0000582Thread::SetStopInfoToNothing()
583{
584 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
585 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
586 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
587}
588
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589bool
590Thread::ThreadStoppedForAReason (void)
591{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000592 return (bool) GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000593}
594
Jim Ingham77787032011-01-20 02:03:18 +0000595bool
596Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
597{
Greg Claytonf74cf862013-11-13 23:28:31 +0000598 saved_state.register_backup_sp.reset();
599 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
600 if (frame_sp)
601 {
602 lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
603 if (reg_checkpoint_sp)
604 {
605 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
606 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp))
607 saved_state.register_backup_sp = reg_checkpoint_sp;
608 }
609 }
610 if (!saved_state.register_backup_sp)
Jim Ingham77787032011-01-20 02:03:18 +0000611 return false;
612
613 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000614 ProcessSP process_sp (GetProcess());
615 if (process_sp)
616 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000617 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
618
Jim Ingham77787032011-01-20 02:03:18 +0000619 return true;
620}
621
622bool
Jim Ingham8559a352012-11-26 23:52:18 +0000623Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000624{
Greg Claytonf74cf862013-11-13 23:28:31 +0000625 if (saved_state.register_backup_sp)
626 {
627 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
628 if (frame_sp)
629 {
630 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
631 if (reg_ctx_sp)
632 {
633 bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp);
634
635 // Clear out all stack frames as our world just changed.
636 ClearStackFrames();
637 reg_ctx_sp->InvalidateIfNeeded(true);
638 if (m_unwinder_ap.get())
639 m_unwinder_ap->Clear();
640 return ret;
641 }
642 }
643 }
644 return false;
Jim Ingham8559a352012-11-26 23:52:18 +0000645}
646
647bool
648Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
649{
Jim Ingham0c270682011-01-25 02:47:23 +0000650 if (saved_state.stop_info_sp)
651 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000652 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000653 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000654 return true;
655}
656
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657StateType
658Thread::GetState() const
659{
660 // If any other threads access this we will need a mutex for it
661 Mutex::Locker locker(m_state_mutex);
662 return m_state;
663}
664
665void
666Thread::SetState(StateType state)
667{
668 Mutex::Locker locker(m_state_mutex);
669 m_state = state;
670}
671
672void
673Thread::WillStop()
674{
675 ThreadPlan *current_plan = GetCurrentPlan();
676
677 // FIXME: I may decide to disallow threads with no plans. In which
678 // case this should go to an assert.
679
680 if (!current_plan)
681 return;
682
683 current_plan->WillStop();
684}
685
686void
687Thread::SetupForResume ()
688{
689 if (GetResumeState() != eStateSuspended)
690 {
691
692 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
693 // telling the current plan it will resume, since we might change what the current
694 // plan is.
695
Greg Clayton1afa68e2013-04-02 20:32:37 +0000696 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
697 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698 {
Greg Claytona97c4d22014-12-09 23:31:02 +0000699 const addr_t thread_pc = reg_ctx_sp->GetPC();
700 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000701 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000702 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000703 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
704 // special to step over a breakpoint.
705
706 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000707
Greg Claytona97c4d22014-12-09 23:31:02 +0000708 bool push_step_over_bp_plan = false;
709 if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint)
710 {
711 ThreadPlanStepOverBreakpoint *bp_plan = (ThreadPlanStepOverBreakpoint *)cur_plan;
712 if (bp_plan->GetBreakpointLoadAddress() != thread_pc)
713 push_step_over_bp_plan = true;
714 }
715 else
716 push_step_over_bp_plan = true;
717
718 if (push_step_over_bp_plan)
Greg Clayton1afa68e2013-04-02 20:32:37 +0000719 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000720 ThreadPlanSP step_bp_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
721 if (step_bp_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000722 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000723 ;
724 step_bp_plan_sp->SetPrivate (true);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000725
726 if (GetCurrentPlan()->RunState() != eStateStepping)
727 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000728 ThreadPlanStepOverBreakpoint *step_bp_plan
729 = static_cast<ThreadPlanStepOverBreakpoint *>(step_bp_plan_sp.get());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000730 step_bp_plan->SetAutoContinue(true);
731 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000732 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734 }
735 }
736 }
737 }
738}
739
740bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000741Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742{
743 // At this point clear the completed plan stack.
744 m_completed_plan_stack.clear();
745 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000746 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747
Greg Clayton97d5cf02012-09-25 02:40:06 +0000748 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000749
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000750 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
751 if (backing_thread_sp)
752 backing_thread_sp->m_temporary_resume_state = resume_state;
753
754 // Make sure m_stop_info_sp is valid
755 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000756
Jim Ingham92087d82012-01-31 23:09:20 +0000757 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
758 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
759 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
760 // about the fact that we are resuming...
Jim Inghamacbea8f2015-06-02 20:26:13 +0000761 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000762 if (m_stop_info_stop_id == process_stop_id &&
763 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000764 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000765 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000766 if (stop_info)
767 stop_info->WillResume (resume_state);
768 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769
770 // Tell all the plans that we are about to resume in case they need to clear any state.
771 // We distinguish between the plan on the top of the stack and the lower
772 // plans in case a plan needs to do any special business before it runs.
773
Greg Claytond1d06e42013-04-20 00:27:58 +0000774 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000776 if (plan_ptr)
777 {
778 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779
Greg Claytond1d06e42013-04-20 00:27:58 +0000780 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
781 {
782 plan_ptr->WillResume (resume_state, false);
783 }
784
785 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
786 // In that case, don't reset it here.
787
788 if (need_to_resume && resume_state != eStateSuspended)
789 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000790 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000791 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000792 }
793
Greg Clayton160c9d82013-05-01 21:54:04 +0000794 if (need_to_resume)
795 {
796 ClearStackFrames();
797 // Let Thread subclasses do any special work they need to prior to resuming
798 WillResume (resume_state);
799 }
800
Jim Ingham513c6bb2012-09-01 01:02:41 +0000801 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802}
803
804void
805Thread::DidResume ()
806{
807 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
808}
809
Andrew Kaylor29d65742013-05-10 17:19:04 +0000810void
811Thread::DidStop ()
812{
813 SetState (eStateStopped);
814}
815
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816bool
817Thread::ShouldStop (Event* event_ptr)
818{
819 ThreadPlan *current_plan = GetCurrentPlan();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000820
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000821 bool should_stop = true;
822
Greg Clayton5160ce52013-03-27 23:08:40 +0000823 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000824
Jim Ingham92087d82012-01-31 23:09:20 +0000825 if (GetResumeState () == eStateSuspended)
826 {
827 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000828 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 +0000829 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000830 return false;
831 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000832
Jim Ingham92087d82012-01-31 23:09:20 +0000833 if (GetTemporaryResumeState () == eStateSuspended)
834 {
835 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000836 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 +0000837 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000838 return false;
839 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000840
Daniel Malea246cb612013-05-14 15:20:12 +0000841 // Based on the current thread plan and process stop info, check if this
842 // thread caused the process to stop. NOTE: this must take place before
843 // the plan is moved from the current plan stack to the completed plan
844 // stack.
Jim Ingham92087d82012-01-31 23:09:20 +0000845 if (ThreadStoppedForAReason() == false)
846 {
847 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000848 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 +0000849 __FUNCTION__, GetID (), GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000850 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000851 return false;
852 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000853
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854 if (log)
855 {
Jim Inghamdee1bc92013-06-22 00:27:45 +0000856 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 +0000857 __FUNCTION__, static_cast<void*>(this), GetID (),
Greg Clayton160c9d82013-05-01 21:54:04 +0000858 GetProtocolID (),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000859 GetRegisterContext()
860 ? GetRegisterContext()->GetPC()
861 : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000862 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000863 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000864 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000866 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000868
Jim Ingham06e827c2010-11-11 19:26:09 +0000869 // The top most plan always gets to do the trace log...
870 current_plan->DoTraceLog ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000871
Jim Ingham6d66ce62012-04-20 21:16:56 +0000872 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
873 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
874 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000875 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Ingham6d66ce62012-04-20 21:16:56 +0000876 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
877 {
878 if (log)
879 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
880 return false;
881 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882
Jim Inghambad39e42012-09-05 21:12:49 +0000883 // If we've already been restarted, don't query the plans since the state they would examine is not current.
884 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
885 return false;
886
887 // Before the plans see the state of the world, calculate the current inlined depth.
888 GetStackFrameList()->CalculateCurrentInlinedDepth();
889
Jim Ingham25f66702011-12-03 01:52:59 +0000890 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
891 // If that plan is still working, then we don't need to do any more work. If the plan that explains
892 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
893 // whether they still need to do more work.
894
895 bool done_processing_current_plan = false;
896
Jim Ingham0161b492013-02-09 01:29:05 +0000897 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000898 {
899 if (current_plan->TracerExplainsStop())
900 {
901 done_processing_current_plan = true;
902 should_stop = false;
903 }
904 else
905 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000906 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000907 // does and let it handle the situation.
908 ThreadPlan *plan_ptr = current_plan;
909 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
910 {
Jim Ingham0161b492013-02-09 01:29:05 +0000911 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000912 {
913 should_stop = plan_ptr->ShouldStop (event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000914
Jim Ingham25f66702011-12-03 01:52:59 +0000915 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
916 // and all the plans below it off the stack.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000917
Jim Ingham25f66702011-12-03 01:52:59 +0000918 if (plan_ptr->MischiefManaged())
919 {
Jim Ingham031177e2012-05-11 23:49:49 +0000920 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000921 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000922
Jim Ingham25f66702011-12-03 01:52:59 +0000923 do
924 {
925 if (should_stop)
926 current_plan->WillStop();
927 PopPlan();
928 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000929 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
930 // Now, if the responsible plan was not "Okay to discard" then we're done,
931 // otherwise we forward this to the next plan in the stack below.
932 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
933 done_processing_current_plan = true;
934 else
935 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000936 }
937 else
938 done_processing_current_plan = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000939
Jim Ingham25f66702011-12-03 01:52:59 +0000940 break;
941 }
942
943 }
944 }
945 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000946
Jim Ingham25f66702011-12-03 01:52:59 +0000947 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000948 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000949 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000950
Jim Ingham10c4b242011-10-15 00:23:43 +0000951 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000952 log->Printf("Plan %s explains stop, auto-continue %i.",
953 current_plan->GetName(), over_ride_stop);
954
Jim Ingham0f16e732011-02-08 05:20:59 +0000955 // We're starting from the base plan, so just let it decide;
956 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000957 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000958 should_stop = current_plan->ShouldStop (event_ptr);
959 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000960 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000961 }
962 else
963 {
964 // Otherwise, don't let the base plan override what the other plans say to do, since
965 // presumably if there were other plans they would know what to do...
966 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000967 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000968 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000969 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000970
Jim Ingham0f16e732011-02-08 05:20:59 +0000971 should_stop = current_plan->ShouldStop(event_ptr);
972 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000973 log->Printf("Plan %s should stop: %d.",
974 current_plan->GetName(), should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000975 if (current_plan->MischiefManaged())
976 {
977 if (should_stop)
978 current_plan->WillStop();
979
980 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
981 // Otherwise, see if the plan's parent wants to stop.
982
983 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
984 {
985 PopPlan();
986 break;
987 }
988 else
989 {
990
991 PopPlan();
992
993 current_plan = GetCurrentPlan();
994 if (current_plan == NULL)
995 {
996 break;
997 }
998 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000999 }
1000 else
1001 {
Jim Ingham0f16e732011-02-08 05:20:59 +00001002 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001003 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001004 }
1005 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001006
Jim Inghamb01e7422010-06-19 04:45:32 +00001007 if (over_ride_stop)
1008 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +00001009
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001010 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001011
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001012 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
1013 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
1014 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
1015 // This code clears stale plans off the stack.
1016
1017 if (should_stop)
1018 {
1019 ThreadPlan *plan_ptr = GetCurrentPlan();
1020 while (!PlanIsBasePlan(plan_ptr))
Jim Ingham64e7ead2012-05-03 21:19:36 +00001021 {
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001022 bool stale = plan_ptr->IsPlanStale ();
1023 ThreadPlan *examined_plan = plan_ptr;
1024 plan_ptr = GetPreviousPlan (examined_plan);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001025
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001026 if (stale)
1027 {
1028 if (log)
1029 log->Printf("Plan %s being discarded in cleanup, it says it is already done.",
1030 examined_plan->GetName());
1031 DiscardThreadPlansUpToPlan(examined_plan);
Jim Ingham64e7ead2012-05-03 21:19:36 +00001032 }
1033 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001034 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001035
Jim Ingham10c4b242011-10-15 00:23:43 +00001036 if (log)
1037 {
1038 StreamString s;
1039 s.IndentMore();
1040 DumpThreadPlans(&s);
1041 log->Printf ("Plan stack final state:\n%s", s.GetData());
1042 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
1043 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001044 return should_stop;
1045}
1046
1047Vote
1048Thread::ShouldReportStop (Event* event_ptr)
1049{
1050 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +00001051 StateType temp_thread_state = GetTemporaryResumeState();
1052
Greg Clayton5160ce52013-03-27 23:08:40 +00001053 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +00001054
1055 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
1056 {
1057 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001058 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 +00001059 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001060 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001061
Jim Ingham92087d82012-01-31 23:09:20 +00001062 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
1063 {
1064 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001065 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 +00001066 return eVoteNoOpinion;
1067 }
1068
1069 if (!ThreadStoppedForAReason())
1070 {
1071 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001072 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 +00001073 return eVoteNoOpinion;
1074 }
1075
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076 if (m_completed_plan_stack.size() > 0)
1077 {
1078 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +00001079 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001080 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 +00001081 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
1082 }
1083 else
Greg Clayton2cad65a2010-09-03 17:10:42 +00001084 {
Jim Ingham0161b492013-02-09 01:29:05 +00001085 Vote thread_vote = eVoteNoOpinion;
1086 ThreadPlan *plan_ptr = GetCurrentPlan();
1087 while (1)
1088 {
1089 if (plan_ptr->PlanExplainsStop(event_ptr))
1090 {
1091 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1092 break;
1093 }
1094 if (PlanIsBasePlan(plan_ptr))
1095 break;
1096 else
1097 plan_ptr = GetPreviousPlan(plan_ptr);
1098 }
Greg Clayton2cad65a2010-09-03 17:10:42 +00001099 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001100 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 +00001101
1102 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001103 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104}
1105
1106Vote
1107Thread::ShouldReportRun (Event* event_ptr)
1108{
1109 StateType thread_state = GetResumeState ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001110
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111 if (thread_state == eStateSuspended
1112 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +00001113 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +00001115 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001116
Greg Clayton5160ce52013-03-27 23:08:40 +00001117 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118 if (m_completed_plan_stack.size() > 0)
1119 {
1120 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +00001121 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001122 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 +00001123 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001124 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001125 m_completed_plan_stack.back()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001126
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
1128 }
1129 else
Jim Ingham444586b2011-01-24 06:34:17 +00001130 {
1131 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001132 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 +00001133 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001134 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001135 GetCurrentPlan()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001136
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001137 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +00001138 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001139}
1140
Jim Ingham1b54c882010-06-16 02:00:15 +00001141bool
1142Thread::MatchesSpec (const ThreadSpec *spec)
1143{
1144 if (spec == NULL)
1145 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +00001146
Jim Ingham3d902922012-03-07 22:03:04 +00001147 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +00001148}
1149
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001150void
1151Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1152{
1153 if (thread_plan_sp)
1154 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001155 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1156 if (!thread_plan_sp->GetThreadPlanTracer())
1157 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001158 m_plan_stack.push_back (thread_plan_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001159
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001160 thread_plan_sp->DidPush();
1161
Greg Clayton5160ce52013-03-27 23:08:40 +00001162 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163 if (log)
1164 {
1165 StreamString s;
1166 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamdee1bc92013-06-22 00:27:45 +00001167 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001168 static_cast<void*>(this), s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001169 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001170 }
1171 }
1172}
1173
1174void
1175Thread::PopPlan ()
1176{
Greg Clayton5160ce52013-03-27 23:08:40 +00001177 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001179 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180 return;
1181 else
1182 {
1183 ThreadPlanSP &plan = m_plan_stack.back();
1184 if (log)
1185 {
Daniel Malead01b2952012-11-29 21:49:15 +00001186 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187 }
1188 m_completed_plan_stack.push_back (plan);
1189 plan->WillPop();
1190 m_plan_stack.pop_back();
1191 }
1192}
1193
1194void
1195Thread::DiscardPlan ()
1196{
Jim Inghamdee1bc92013-06-22 00:27:45 +00001197 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 if (m_plan_stack.size() > 1)
1199 {
1200 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamdee1bc92013-06-22 00:27:45 +00001201 if (log)
1202 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1203
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001204 m_discarded_plan_stack.push_back (plan);
1205 plan->WillPop();
1206 m_plan_stack.pop_back();
1207 }
1208}
1209
1210ThreadPlan *
1211Thread::GetCurrentPlan ()
1212{
Jim Inghame1471232012-04-19 00:17:05 +00001213 // There will always be at least the base plan. If somebody is mucking with a
1214 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +00001215 if (m_plan_stack.empty())
1216 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +00001217 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218}
1219
1220ThreadPlanSP
1221Thread::GetCompletedPlan ()
1222{
1223 ThreadPlanSP empty_plan_sp;
1224 if (!m_completed_plan_stack.empty())
1225 {
1226 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1227 {
1228 ThreadPlanSP completed_plan_sp;
1229 completed_plan_sp = m_completed_plan_stack[i];
1230 if (!completed_plan_sp->GetPrivate ())
1231 return completed_plan_sp;
1232 }
1233 }
1234 return empty_plan_sp;
1235}
1236
Jim Ingham73ca05a2011-12-17 01:35:57 +00001237ValueObjectSP
1238Thread::GetReturnValueObject ()
1239{
1240 if (!m_completed_plan_stack.empty())
1241 {
1242 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1243 {
1244 ValueObjectSP return_valobj_sp;
1245 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1246 if (return_valobj_sp)
Greg Claytone5214572015-07-01 23:28:31 +00001247 return return_valobj_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001248 }
1249 }
1250 return ValueObjectSP();
1251}
1252
Jim Ingham30fadaf2014-07-08 01:07:32 +00001253ClangExpressionVariableSP
1254Thread::GetExpressionVariable ()
1255{
1256 if (!m_completed_plan_stack.empty())
1257 {
1258 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1259 {
1260 ClangExpressionVariableSP expression_variable_sp;
1261 expression_variable_sp = m_completed_plan_stack[i]->GetExpressionVariable();
1262 if (expression_variable_sp)
Greg Claytone5214572015-07-01 23:28:31 +00001263 return expression_variable_sp;
Jim Ingham30fadaf2014-07-08 01:07:32 +00001264 }
1265 }
1266 return ClangExpressionVariableSP();
1267}
1268
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269bool
1270Thread::IsThreadPlanDone (ThreadPlan *plan)
1271{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001272 if (!m_completed_plan_stack.empty())
1273 {
1274 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1275 {
1276 if (m_completed_plan_stack[i].get() == plan)
1277 return true;
1278 }
1279 }
1280 return false;
1281}
1282
1283bool
1284Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1285{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286 if (!m_discarded_plan_stack.empty())
1287 {
1288 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1289 {
1290 if (m_discarded_plan_stack[i].get() == plan)
1291 return true;
1292 }
1293 }
1294 return false;
1295}
1296
1297ThreadPlan *
1298Thread::GetPreviousPlan (ThreadPlan *current_plan)
1299{
1300 if (current_plan == NULL)
1301 return NULL;
1302
1303 int stack_size = m_completed_plan_stack.size();
1304 for (int i = stack_size - 1; i > 0; i--)
1305 {
1306 if (current_plan == m_completed_plan_stack[i].get())
1307 return m_completed_plan_stack[i-1].get();
1308 }
1309
1310 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1311 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312 if (m_plan_stack.size() > 0)
1313 return m_plan_stack.back().get();
1314 else
1315 return NULL;
1316 }
1317
1318 stack_size = m_plan_stack.size();
1319 for (int i = stack_size - 1; i > 0; i--)
1320 {
1321 if (current_plan == m_plan_stack[i].get())
1322 return m_plan_stack[i-1].get();
1323 }
1324 return NULL;
1325}
1326
1327void
1328Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1329{
1330 if (abort_other_plans)
1331 DiscardThreadPlans(true);
1332
1333 PushPlan (thread_plan_sp);
1334}
1335
Jim Ingham06e827c2010-11-11 19:26:09 +00001336
1337void
1338Thread::EnableTracer (bool value, bool single_stepping)
1339{
1340 int stack_size = m_plan_stack.size();
1341 for (int i = 0; i < stack_size; i++)
1342 {
1343 if (m_plan_stack[i]->GetThreadPlanTracer())
1344 {
1345 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1346 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1347 }
1348 }
1349}
1350
1351void
1352Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1353{
1354 int stack_size = m_plan_stack.size();
1355 for (int i = 0; i < stack_size; i++)
1356 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1357}
1358
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001359bool
1360Thread::DiscardUserThreadPlansUpToIndex (uint32_t thread_index)
1361{
1362 // Count the user thread plans from the back end to get the number of the one we want
1363 // to discard:
1364
1365 uint32_t idx = 0;
1366 ThreadPlan *up_to_plan_ptr = nullptr;
1367
1368 for (ThreadPlanSP plan_sp : m_plan_stack)
1369 {
1370 if (plan_sp->GetPrivate())
1371 continue;
1372 if (idx == thread_index)
1373 {
1374 up_to_plan_ptr = plan_sp.get();
1375 break;
1376 }
1377 else
1378 idx++;
1379 }
1380
1381 if (up_to_plan_ptr == nullptr)
1382 return false;
1383
1384 DiscardThreadPlansUpToPlan(up_to_plan_ptr);
1385 return true;
1386}
1387
1388
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001390Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1391{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001392 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1393}
1394
1395void
1396Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1397{
Greg Clayton5160ce52013-03-27 23:08:40 +00001398 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001399 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001400 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p",
1401 GetID(), static_cast<void*>(up_to_plan_ptr));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001402
1403 int stack_size = m_plan_stack.size();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001404
Jim Ingham399f1ca2010-11-05 19:25:48 +00001405 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1406 // stack, and if so discard up to and including it.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001407
Jim Ingham64e7ead2012-05-03 21:19:36 +00001408 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001409 {
1410 for (int i = stack_size - 1; i > 0; i--)
1411 DiscardPlan();
1412 }
1413 else
1414 {
1415 bool found_it = false;
1416 for (int i = stack_size - 1; i > 0; i--)
1417 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001418 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001419 found_it = true;
1420 }
1421 if (found_it)
1422 {
1423 bool last_one = false;
1424 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1425 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001426 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001427 last_one = true;
1428 DiscardPlan();
1429 }
1430 }
1431 }
1432 return;
1433}
1434
1435void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436Thread::DiscardThreadPlans(bool force)
1437{
Greg Clayton5160ce52013-03-27 23:08:40 +00001438 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439 if (log)
1440 {
Daniel Malead01b2952012-11-29 21:49:15 +00001441 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001442 }
1443
1444 if (force)
1445 {
1446 int stack_size = m_plan_stack.size();
1447 for (int i = stack_size - 1; i > 0; i--)
1448 {
1449 DiscardPlan();
1450 }
1451 return;
1452 }
1453
1454 while (1)
1455 {
1456
1457 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001458 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001459
1460 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1461 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1462 {
1463 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1464 {
1465 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1466 break;
1467 }
1468 }
1469
1470 if (discard)
1471 {
1472 // First pop all the dependent plans:
1473 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1474 {
1475
1476 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1477 // for the plan leaves it in a state that it is safe to pop the plan
1478 // with no more notice?
1479 DiscardPlan();
1480 }
1481
1482 // Now discard the master plan itself.
1483 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1484 // discard it's dependent plans, but not it...
1485 if (master_plan_idx > 0)
1486 {
1487 DiscardPlan();
1488 }
1489 }
1490 else
1491 {
1492 // If the master plan doesn't want to get discarded, then we're done.
1493 break;
1494 }
1495
1496 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001497}
1498
Jim Inghamcf274f92012-04-09 22:37:39 +00001499bool
1500Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1501{
1502 if (plan_ptr->IsBasePlan())
1503 return true;
1504 else if (m_plan_stack.size() == 0)
1505 return false;
1506 else
1507 return m_plan_stack[0].get() == plan_ptr;
1508}
1509
Jim Ingham93208b82013-01-31 21:46:01 +00001510Error
1511Thread::UnwindInnermostExpression()
1512{
1513 Error error;
1514 int stack_size = m_plan_stack.size();
1515
1516 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1517 // stack, and if so discard up to and including it.
1518
1519 for (int i = stack_size - 1; i > 0; i--)
1520 {
1521 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1522 {
1523 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1524 return error;
1525 }
1526 }
1527 error.SetErrorString("No expressions currently active on this thread");
1528 return error;
1529}
1530
1531
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001532ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001533Thread::QueueFundamentalPlan (bool abort_other_plans)
1534{
1535 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1536 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001537 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001538}
1539
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001540ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001541Thread::QueueThreadPlanForStepSingleInstruction
1542(
1543 bool step_over,
1544 bool abort_other_plans,
1545 bool stop_other_threads
1546)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001547{
1548 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1549 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001550 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001551}
1552
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001553ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001554Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001555(
1556 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001557 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001558 const SymbolContext &addr_context,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001559 lldb::RunMode stop_other_threads,
1560 LazyBool step_out_avoids_code_withoug_debug_info
Jim Inghamc6276822012-12-12 19:58:40 +00001561)
1562{
1563 ThreadPlanSP thread_plan_sp;
Jim Ingham4b4b2472014-03-13 02:47:14 +00001564 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads, step_out_avoids_code_withoug_debug_info));
1565
Jim Inghamc6276822012-12-12 19:58:40 +00001566 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001567 return thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001568}
1569
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001570ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001571Thread::QueueThreadPlanForStepInRange
1572(
1573 bool abort_other_plans,
1574 const AddressRange &range,
1575 const SymbolContext &addr_context,
1576 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001577 lldb::RunMode stop_other_threads,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001578 LazyBool step_in_avoids_code_without_debug_info,
1579 LazyBool step_out_avoids_code_without_debug_info
Greg Clayton474966a2010-06-12 18:59:55 +00001580)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001581{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001582 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInRange (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001583 range,
1584 addr_context,
1585 stop_other_threads,
1586 step_in_avoids_code_without_debug_info,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001587 step_out_avoids_code_without_debug_info));
1588 ThreadPlanStepInRange *plan = static_cast<ThreadPlanStepInRange *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001589
Jim Inghamc6276822012-12-12 19:58:40 +00001590 if (step_in_target)
1591 plan->SetStepInTarget(step_in_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001592
1593 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001594 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001595}
1596
1597
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001598ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001599Thread::QueueThreadPlanForStepOut
1600(
1601 bool abort_other_plans,
1602 SymbolContext *addr_context,
1603 bool first_insn,
1604 bool stop_other_threads,
1605 Vote stop_vote,
1606 Vote run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001607 uint32_t frame_idx,
1608 LazyBool step_out_avoids_code_withoug_debug_info
Greg Clayton481cef22011-01-21 06:11:58 +00001609)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001610{
Greg Clayton481cef22011-01-21 06:11:58 +00001611 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1612 addr_context,
1613 first_insn,
1614 stop_other_threads,
1615 stop_vote,
1616 run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001617 frame_idx,
1618 step_out_avoids_code_withoug_debug_info));
1619
1620 if (thread_plan_sp->ValidatePlan(NULL))
1621 {
1622 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1623 return thread_plan_sp;
1624 }
1625 else
1626 {
1627 return ThreadPlanSP();
1628 }
1629}
1630
1631ThreadPlanSP
1632Thread::QueueThreadPlanForStepOutNoShouldStop
1633(
1634 bool abort_other_plans,
1635 SymbolContext *addr_context,
1636 bool first_insn,
1637 bool stop_other_threads,
1638 Vote stop_vote,
1639 Vote run_vote,
1640 uint32_t frame_idx
1641)
1642{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001643 ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001644 addr_context,
1645 first_insn,
1646 stop_other_threads,
1647 stop_vote,
1648 run_vote,
1649 frame_idx,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001650 eLazyBoolNo));
1651
1652 ThreadPlanStepOut *new_plan = static_cast<ThreadPlanStepOut *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001653 new_plan->ClearShouldStopHereCallbacks();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001654
Sean Callanan708709c2012-07-31 22:19:25 +00001655 if (thread_plan_sp->ValidatePlan(NULL))
1656 {
1657 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001658 return thread_plan_sp;
Sean Callanan708709c2012-07-31 22:19:25 +00001659 }
1660 else
1661 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001662 return ThreadPlanSP();
Sean Callanan708709c2012-07-31 22:19:25 +00001663 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001664}
1665
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001666ThreadPlanSP
Jim Ingham18de2fd2012-05-10 01:35:39 +00001667Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001668{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001669 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001670 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001671 return ThreadPlanSP();
Jim Ingham25f66702011-12-03 01:52:59 +00001672
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001673 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001674 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001675}
1676
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001677ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001678Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1679 Address &target_addr,
1680 bool stop_other_threads)
1681{
1682 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1683 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001684 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685}
1686
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001687ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001688Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001689 lldb::addr_t *address_list,
1690 size_t num_addresses,
1691 bool stop_other_threads,
1692 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001693{
Greg Clayton481cef22011-01-21 06:11:58 +00001694 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001695 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001696 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001697
1698}
1699
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001700lldb::ThreadPlanSP
1701Thread::QueueThreadPlanForStepScripted (bool abort_other_plans,
1702 const char *class_name,
1703 bool stop_other_threads)
1704{
1705 ThreadPlanSP thread_plan_sp (new ThreadPlanPython (*this, class_name));
1706 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1707 // This seems a little funny, but I don't want to have to split up the constructor and the
1708 // DidPush in the scripted plan, that seems annoying.
1709 // That means the constructor has to be in DidPush.
1710 // So I have to validate the plan AFTER pushing it, and then take it off again...
1711 if (!thread_plan_sp->ValidatePlan(nullptr))
1712 {
1713 DiscardThreadPlansUpToPlan(thread_plan_sp);
1714 return ThreadPlanSP();
1715 }
1716 else
1717 return thread_plan_sp;
1718
1719}
1720
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001721uint32_t
1722Thread::GetIndexID () const
1723{
1724 return m_index_id;
1725}
1726
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001727static void
1728PrintPlanElement (Stream *s, const ThreadPlanSP &plan, lldb::DescriptionLevel desc_level, int32_t elem_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001729{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001730 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001731 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001732 s->Printf ("Element %d: ", elem_idx);
1733 plan->GetDescription (s, desc_level);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001734 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001735 s->IndentLess();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001736}
1737
1738static void
1739PrintPlanStack (Stream *s, const std::vector<lldb::ThreadPlanSP> &plan_stack, lldb::DescriptionLevel desc_level, bool include_internal)
1740{
1741 int32_t print_idx = 0;
1742 for (ThreadPlanSP plan_sp : plan_stack)
1743 {
1744 if (include_internal || !plan_sp->GetPrivate())
1745 {
1746 PrintPlanElement (s, plan_sp, desc_level, print_idx++);
1747 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001748 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001749}
1750
1751void
1752Thread::DumpThreadPlans (Stream *s,
1753 lldb::DescriptionLevel desc_level,
1754 bool include_internal,
1755 bool ignore_boring_threads) const
1756{
Jason Molenda60b5da62014-10-16 07:53:46 +00001757 uint32_t stack_size;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001758
1759 if (ignore_boring_threads)
1760 {
1761 uint32_t stack_size = m_plan_stack.size();
1762 uint32_t completed_stack_size = m_completed_plan_stack.size();
1763 uint32_t discarded_stack_size = m_discarded_plan_stack.size();
1764 if (stack_size == 1 && completed_stack_size == 0 && discarded_stack_size == 0)
1765 {
1766 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 "\n", GetIndexID(), GetID());
1767 s->IndentMore();
1768 s->Indent();
1769 s->Printf("No active thread plans\n");
1770 s->IndentLess();
1771 return;
1772 }
1773 }
1774
1775 s->Indent();
1776 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 ":\n", GetIndexID(), GetID());
1777 s->IndentMore();
1778 s->Indent();
1779 s->Printf ("Active plan stack:\n");
1780 PrintPlanStack (s, m_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001781
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001782 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001783 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001784 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001785 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001786 s->Printf ("Completed Plan Stack:\n");
1787 PrintPlanStack (s, m_completed_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001788 }
1789
1790 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001791 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001792 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001793 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001794 s->Printf ("Discarded Plan Stack:\n");
1795 PrintPlanStack (s, m_discarded_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001796 }
1797
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001798 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001799}
1800
Greg Claytond9e416c2012-02-18 05:35:26 +00001801TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001802Thread::CalculateTarget ()
1803{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001804 TargetSP target_sp;
1805 ProcessSP process_sp(GetProcess());
1806 if (process_sp)
1807 target_sp = process_sp->CalculateTarget();
1808 return target_sp;
1809
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001810}
1811
Greg Claytond9e416c2012-02-18 05:35:26 +00001812ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001813Thread::CalculateProcess ()
1814{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001815 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001816}
1817
Greg Claytond9e416c2012-02-18 05:35:26 +00001818ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001819Thread::CalculateThread ()
1820{
Greg Claytond9e416c2012-02-18 05:35:26 +00001821 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001822}
1823
Jason Molendab57e4a12013-11-04 09:33:30 +00001824StackFrameSP
1825Thread::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001826{
Jason Molendab57e4a12013-11-04 09:33:30 +00001827 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001828}
1829
1830void
Greg Clayton0603aa92010-10-04 01:05:56 +00001831Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001832{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001833 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001834}
1835
Greg Clayton12daf9462010-08-25 00:35:26 +00001836
Greg Clayton39d0ab32012-03-29 01:41:38 +00001837StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001838Thread::GetStackFrameList ()
1839{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001840 StackFrameListSP frame_list_sp;
1841 Mutex::Locker locker(m_frame_mutex);
1842 if (m_curr_frames_sp)
1843 {
1844 frame_list_sp = m_curr_frames_sp;
1845 }
1846 else
1847 {
1848 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1849 m_curr_frames_sp = frame_list_sp;
1850 }
1851 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001852}
1853
Greg Clayton12daf9462010-08-25 00:35:26 +00001854void
1855Thread::ClearStackFrames ()
1856{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001857 Mutex::Locker locker(m_frame_mutex);
1858
Greg Clayton160c9d82013-05-01 21:54:04 +00001859 Unwind *unwinder = GetUnwinder ();
1860 if (unwinder)
1861 unwinder->Clear();
1862
Jim Inghamb0c72a52012-02-29 03:40:22 +00001863 // Only store away the old "reference" StackFrameList if we got all its frames:
1864 // FIXME: At some point we can try to splice in the frames we have fetched into
1865 // the new frame as we make it, but let's not try that now.
1866 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001867 m_prev_frames_sp.swap (m_curr_frames_sp);
1868 m_curr_frames_sp.reset();
Jason Molenda705b1802014-06-13 02:37:02 +00001869
1870 m_extended_info.reset();
1871 m_extended_info_fetched = false;
Jim Ingham87c11912010-08-12 02:14:28 +00001872}
1873
Jason Molendab57e4a12013-11-04 09:33:30 +00001874lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001875Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1876{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001877 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001878}
1879
Jim Ingham44137582012-09-12 00:40:39 +00001880
1881Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001882Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001883{
Jason Molendab57e4a12013-11-04 09:33:30 +00001884 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001885 Error return_error;
1886
1887 if (!frame_sp)
1888 {
Daniel Malead01b2952012-11-29 21:49:15 +00001889 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001890 }
1891
Jim Ingham4f465cf2012-10-10 18:32:14 +00001892 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001893}
1894
1895Error
Jason Molendab57e4a12013-11-04 09:33:30 +00001896Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001897{
1898 Error return_error;
1899
1900 if (!frame_sp)
1901 {
1902 return_error.SetErrorString("Can't return to a null frame.");
1903 return return_error;
1904 }
1905
1906 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001907 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
Jason Molendab57e4a12013-11-04 09:33:30 +00001908 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001909 if (!older_frame_sp)
1910 {
1911 return_error.SetErrorString("No older frame to return to.");
1912 return return_error;
1913 }
Jim Ingham44137582012-09-12 00:40:39 +00001914
1915 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001916 {
Jim Ingham44137582012-09-12 00:40:39 +00001917 lldb::ABISP abi = thread->GetProcess()->GetABI();
1918 if (!abi)
1919 {
1920 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001921 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001922 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001923 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1924
1925 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1926 // Turn that back on when that works.
Jason Molenda97e704b2014-10-16 08:07:20 +00001927 if (/* DISABLES CODE */ (0) && sc.function != NULL)
Jim Ingham1f51e602012-09-27 01:15:29 +00001928 {
1929 Type *function_type = sc.function->GetType();
1930 if (function_type)
1931 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001932 ClangASTType return_type = sc.function->GetClangType().GetFunctionReturnType();
Jim Ingham1f51e602012-09-27 01:15:29 +00001933 if (return_type)
1934 {
Jim Ingham1f51e602012-09-27 01:15:29 +00001935 StreamString s;
Greg Clayton57ee3062013-07-11 22:46:58 +00001936 return_type.DumpTypeDescription(&s);
1937 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham1f51e602012-09-27 01:15:29 +00001938 if (cast_value_sp)
1939 {
1940 cast_value_sp->SetFormat(eFormatHex);
1941 return_value_sp = cast_value_sp;
1942 }
1943 }
1944 }
1945 }
1946
Jim Inghamcb640dd2012-09-14 02:14:15 +00001947 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001948 if (!return_error.Success())
1949 return return_error;
1950 }
1951
1952 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001953 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001954 // cook their data
1955
Jason Molendab57e4a12013-11-04 09:33:30 +00001956 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
Jim Ingham93208b82013-01-31 21:46:01 +00001957 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001958 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001959 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1960 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001961 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001962 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1963 if (copy_success)
1964 {
1965 thread->DiscardThreadPlans(true);
1966 thread->ClearStackFrames();
1967 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1968 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1969 }
1970 else
1971 {
1972 return_error.SetErrorString("Could not reset register values.");
1973 }
Jim Ingham93208b82013-01-31 21:46:01 +00001974 }
1975 else
1976 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001977 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001978 }
Jim Ingham44137582012-09-12 00:40:39 +00001979 }
1980 else
1981 {
Jim Ingham93208b82013-01-31 21:46:01 +00001982 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001983 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001984 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001985}
1986
Richard Mittonf86248d2013-09-12 02:20:34 +00001987static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1988{
1989 for (size_t n=0;n<list.size();n++)
1990 {
1991 s << "\t";
1992 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1993 s << "\n";
1994 }
1995}
1996
1997Error
1998Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1999{
2000 ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
2001 Target *target = exe_ctx.GetTargetPtr();
2002 TargetSP target_sp = exe_ctx.GetTargetSP();
2003 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00002004 StackFrame *frame = exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00002005 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
2006
2007 // Find candidate locations.
2008 std::vector<Address> candidates, within_function, outside_function;
2009 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
2010
2011 // If possible, we try and stay within the current function.
2012 // Within a function, we accept multiple locations (optimized code may do this,
2013 // there's no solution here so we do the best we can).
2014 // However if we're trying to leave the function, we don't know how to pick the
2015 // right location, so if there's more than one then we bail.
2016 if (!within_function.empty())
2017 candidates = within_function;
2018 else if (outside_function.size() == 1 && can_leave_function)
2019 candidates = outside_function;
2020
2021 // Check if we got anything.
2022 if (candidates.empty())
2023 {
2024 if (outside_function.empty())
2025 {
2026 return Error("Cannot locate an address for %s:%i.",
2027 file.GetFilename().AsCString(), line);
2028 }
2029 else if (outside_function.size() == 1)
2030 {
2031 return Error("%s:%i is outside the current function.",
2032 file.GetFilename().AsCString(), line);
2033 }
2034 else
2035 {
2036 StreamString sstr;
2037 DumpAddressList(sstr, outside_function, target);
2038 return Error("%s:%i has multiple candidate locations:\n%s",
2039 file.GetFilename().AsCString(), line, sstr.GetString().c_str());
2040 }
2041 }
2042
2043 // Accept the first location, warn about any others.
2044 Address dest = candidates[0];
2045 if (warnings && candidates.size() > 1)
2046 {
2047 StreamString sstr;
2048 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
2049 file.GetFilename().AsCString(), line);
2050 DumpAddressList(sstr, candidates, target);
2051 *warnings = sstr.GetString();
2052 }
2053
2054 if (!reg_ctx->SetPC (dest))
2055 return Error("Cannot change PC to target address.");
2056
2057 return Error();
2058}
2059
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002060void
Greg Clayton0603aa92010-10-04 01:05:56 +00002061Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002062{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002063 ExecutionContext exe_ctx (shared_from_this());
2064 Process *process = exe_ctx.GetProcessPtr();
2065 if (process == NULL)
2066 return;
2067
Jason Molendab57e4a12013-11-04 09:33:30 +00002068 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00002069 SymbolContext frame_sc;
Jim Inghamd1f25362015-01-28 01:17:26 +00002070 if (frame_idx != LLDB_INVALID_FRAME_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002071 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002072 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002073 if (frame_sp)
2074 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002075 exe_ctx.SetFrameSP(frame_sp);
2076 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002077 }
2078 }
2079
Greg Clayton554f68d2015-02-04 22:00:53 +00002080 const FormatEntity::Entry *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00002081 assert (thread_format);
Greg Clayton554f68d2015-02-04 22:00:53 +00002082
2083 FormatEntity::Format(*thread_format,
2084 strm,
2085 frame_sp ? &frame_sc : NULL,
2086 &exe_ctx,
2087 NULL,
2088 NULL,
2089 false,
2090 false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002091}
2092
Greg Clayton99d0faf2010-11-18 23:32:35 +00002093void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002094Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00002095{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002096}
Jim Inghamee8aea12010-09-08 03:14:33 +00002097
Greg Clayton99d0faf2010-11-18 23:32:35 +00002098void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002099Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002100{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002101}
Jim Inghamee8aea12010-09-08 03:14:33 +00002102
Richard Mitton0a558352013-10-17 21:14:00 +00002103lldb::addr_t
2104Thread::GetThreadPointer ()
2105{
2106 return LLDB_INVALID_ADDRESS;
2107}
2108
2109addr_t
2110Thread::GetThreadLocalData (const ModuleSP module)
2111{
2112 // The default implementation is to ask the dynamic loader for it.
2113 // This can be overridden for specific platforms.
2114 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
2115 if (loader)
2116 return loader->GetThreadLocalData (module, shared_from_this());
2117 else
2118 return LLDB_INVALID_ADDRESS;
2119}
2120
Jason Molendab4892cd2014-05-13 22:02:48 +00002121bool
2122Thread::SafeToCallFunctions ()
2123{
2124 Process *process = GetProcess().get();
2125 if (process)
2126 {
2127 SystemRuntime *runtime = process->GetSystemRuntime ();
2128 if (runtime)
2129 {
2130 return runtime->SafeToCallFunctionsOnThisThread (shared_from_this());
2131 }
2132 }
2133 return true;
2134}
2135
Jason Molendab57e4a12013-11-04 09:33:30 +00002136lldb::StackFrameSP
2137Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
Jim Inghame4284b72010-09-23 17:40:12 +00002138{
Jason Molendab57e4a12013-11-04 09:33:30 +00002139 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00002140}
Caroline Ticeceb6b132010-10-26 03:11:13 +00002141
2142const char *
2143Thread::StopReasonAsCString (lldb::StopReason reason)
2144{
2145 switch (reason)
2146 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00002147 case eStopReasonInvalid: return "invalid";
2148 case eStopReasonNone: return "none";
2149 case eStopReasonTrace: return "trace";
2150 case eStopReasonBreakpoint: return "breakpoint";
2151 case eStopReasonWatchpoint: return "watchpoint";
2152 case eStopReasonSignal: return "signal";
2153 case eStopReasonException: return "exception";
2154 case eStopReasonExec: return "exec";
2155 case eStopReasonPlanComplete: return "plan complete";
2156 case eStopReasonThreadExiting: return "thread exiting";
Kuba Breckaafdf8422014-10-10 23:43:03 +00002157 case eStopReasonInstrumentation: return "instrumentation break";
Caroline Ticeceb6b132010-10-26 03:11:13 +00002158 }
2159
2160
2161 static char unknown_state_string[64];
2162 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
2163 return unknown_state_string;
2164}
2165
2166const char *
2167Thread::RunModeAsCString (lldb::RunMode mode)
2168{
2169 switch (mode)
2170 {
2171 case eOnlyThisThread: return "only this thread";
2172 case eAllThreads: return "all threads";
2173 case eOnlyDuringStepping: return "only during stepping";
2174 }
2175
2176 static char unknown_state_string[64];
2177 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
2178 return unknown_state_string;
2179}
Jim Ingham06e827c2010-11-11 19:26:09 +00002180
Greg Clayton7260f622011-04-18 08:33:37 +00002181size_t
2182Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
2183{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002184 ExecutionContext exe_ctx (shared_from_this());
2185 Target *target = exe_ctx.GetTargetPtr();
2186 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00002187 size_t num_frames_shown = 0;
2188 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00002189 bool is_selected = false;
2190 if (process)
2191 {
2192 if (process->GetThreadList().GetSelectedThread().get() == this)
2193 is_selected = true;
2194 }
2195 strm.Printf("%c ", is_selected ? '*' : ' ');
2196 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00002197 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002198 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00002199 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00002200 {
Jim Inghame610d642011-08-16 00:07:28 +00002201 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
2202 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
2203 {
2204 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
2205 }
Greg Clayton5113dc82011-08-12 06:47:54 +00002206 }
Greg Clayton7260f622011-04-18 08:33:37 +00002207 }
2208
2209 DumpUsingSettingsFormat (strm, start_frame);
2210
2211 if (num_frames > 0)
2212 {
2213 strm.IndentMore();
2214
2215 const bool show_frame_info = true;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002216
2217 const char *selected_frame_marker = NULL;
2218 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
2219 strm.IndentMore ();
2220 else
2221 selected_frame_marker = "* ";
2222
Greg Clayton39d0ab32012-03-29 01:41:38 +00002223 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
2224 start_frame,
2225 num_frames,
2226 show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002227 num_frames_with_source,
2228 selected_frame_marker);
2229 if (num_frames == 1)
2230 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00002231 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00002232 }
2233 return num_frames_shown;
2234}
2235
Jason Molenda705b1802014-06-13 02:37:02 +00002236bool
Kuba Breckaafdf8422014-10-10 23:43:03 +00002237Thread::GetDescription (Stream &strm, lldb::DescriptionLevel level, bool print_json_thread, bool print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002238{
2239 DumpUsingSettingsFormat (strm, 0);
2240 strm.Printf("\n");
2241
2242 StructuredData::ObjectSP thread_info = GetExtendedInfo();
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002243
Kuba Breckaafdf8422014-10-10 23:43:03 +00002244 if (print_json_thread || print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002245 {
Kuba Breckaafdf8422014-10-10 23:43:03 +00002246 if (thread_info && print_json_thread)
2247 {
2248 thread_info->Dump (strm);
2249 strm.Printf("\n");
2250 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002251
2252 if (print_json_stopinfo && m_stop_info_sp)
Kuba Breckaafdf8422014-10-10 23:43:03 +00002253 {
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002254 StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo();
2255 if (stop_info)
2256 {
2257 stop_info->Dump (strm);
2258 strm.Printf("\n");
2259 }
Kuba Breckaafdf8422014-10-10 23:43:03 +00002260 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002261
Jason Molenda705b1802014-06-13 02:37:02 +00002262 return true;
2263 }
2264
2265 if (thread_info)
2266 {
2267 StructuredData::ObjectSP activity = thread_info->GetObjectForDotSeparatedPath("activity");
2268 StructuredData::ObjectSP breadcrumb = thread_info->GetObjectForDotSeparatedPath("breadcrumb");
2269 StructuredData::ObjectSP messages = thread_info->GetObjectForDotSeparatedPath("trace_messages");
2270
2271 bool printed_activity = false;
2272 if (activity && activity->GetType() == StructuredData::Type::eTypeDictionary)
2273 {
2274 StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
2275 StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
2276 StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
2277 if (name && name->GetType() == StructuredData::Type::eTypeString
2278 && id && id->GetType() == StructuredData::Type::eTypeInteger)
2279 {
2280 strm.Printf(" Activity '%s', 0x%" PRIx64 "\n", name->GetAsString()->GetValue().c_str(), id->GetAsInteger()->GetValue());
2281 }
2282 printed_activity = true;
2283 }
2284 bool printed_breadcrumb = false;
2285 if (breadcrumb && breadcrumb->GetType() == StructuredData::Type::eTypeDictionary)
2286 {
2287 if (printed_activity)
2288 strm.Printf ("\n");
2289 StructuredData::Dictionary *breadcrumb_dict = breadcrumb->GetAsDictionary();
2290 StructuredData::ObjectSP breadcrumb_text = breadcrumb_dict->GetValueForKey ("name");
2291 if (breadcrumb_text && breadcrumb_text->GetType() == StructuredData::Type::eTypeString)
2292 {
2293 strm.Printf (" Current Breadcrumb: %s\n", breadcrumb_text->GetAsString()->GetValue().c_str());
2294 }
2295 printed_breadcrumb = true;
2296 }
2297 if (messages && messages->GetType() == StructuredData::Type::eTypeArray)
2298 {
2299 if (printed_breadcrumb)
2300 strm.Printf("\n");
2301 StructuredData::Array *messages_array = messages->GetAsArray();
2302 const size_t msg_count = messages_array->GetSize();
2303 if (msg_count > 0)
2304 {
2305 strm.Printf (" %zu trace messages:\n", msg_count);
2306 for (size_t i = 0; i < msg_count; i++)
2307 {
2308 StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
2309 if (message && message->GetType() == StructuredData::Type::eTypeDictionary)
2310 {
2311 StructuredData::Dictionary *message_dict = message->GetAsDictionary();
2312 StructuredData::ObjectSP message_text = message_dict->GetValueForKey ("message");
2313 if (message_text && message_text->GetType() == StructuredData::Type::eTypeString)
2314 {
2315 strm.Printf (" %s\n", message_text->GetAsString()->GetValue().c_str());
2316 }
2317 }
2318 }
2319 }
2320 }
2321 }
2322
2323 return true;
2324}
2325
Greg Clayton7260f622011-04-18 08:33:37 +00002326size_t
2327Thread::GetStackFrameStatus (Stream& strm,
2328 uint32_t first_frame,
2329 uint32_t num_frames,
2330 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002331 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00002332{
Greg Clayton39d0ab32012-03-29 01:41:38 +00002333 return GetStackFrameList()->GetStatus (strm,
2334 first_frame,
2335 num_frames,
2336 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002337 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00002338}
2339
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002340Unwind *
2341Thread::GetUnwinder ()
2342{
2343 if (m_unwinder_ap.get() == NULL)
2344 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00002345 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002346 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2347 switch (machine)
2348 {
2349 case llvm::Triple::x86_64:
2350 case llvm::Triple::x86:
2351 case llvm::Triple::arm:
Todd Fialad8eaa172014-07-23 14:37:35 +00002352 case llvm::Triple::aarch64:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002353 case llvm::Triple::thumb:
Bhushan D. Attarde794a4d52015-05-15 06:53:30 +00002354 case llvm::Triple::mips:
2355 case llvm::Triple::mipsel:
Ed Masteb73f8442013-10-10 00:59:47 +00002356 case llvm::Triple::mips64:
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +00002357 case llvm::Triple::mips64el:
Justin Hibbits6256a0e2014-10-31 02:34:28 +00002358 case llvm::Triple::ppc:
Justin Hibbitsdb39cdf2014-10-31 15:57:52 +00002359 case llvm::Triple::ppc64:
Deepak Panickal6d3df422014-02-19 11:16:46 +00002360 case llvm::Triple::hexagon:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002361 m_unwinder_ap.reset (new UnwindLLDB (*this));
2362 break;
2363
2364 default:
2365 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2366 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2367 break;
2368 }
2369 }
2370 return m_unwinder_ap.get();
2371}
2372
2373
Greg Claytonfa559e52012-05-18 02:38:05 +00002374void
2375Thread::Flush ()
2376{
2377 ClearStackFrames ();
2378 m_reg_context_sp.reset();
2379}
Jim Ingham5d88a062012-10-16 00:09:33 +00002380
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00002381bool
Jim Ingham5d88a062012-10-16 00:09:33 +00002382Thread::IsStillAtLastBreakpointHit ()
2383{
2384 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2385 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2386 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002387 if (m_stop_info_sp) {
2388 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00002389 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002390 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00002391 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2392 if (reg_ctx_sp)
2393 {
2394 lldb::addr_t pc = reg_ctx_sp->GetPC();
2395 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002396 if (bp_site_sp &&
2397 static_cast<break_id_t>(value) == bp_site_sp->GetID())
Greg Clayton1afa68e2013-04-02 20:32:37 +00002398 return true;
2399 }
Jim Ingham5d88a062012-10-16 00:09:33 +00002400 }
2401 }
2402 return false;
2403}
Greg Clayton44d93782014-01-27 23:43:24 +00002404
2405
2406Error
2407Thread::StepIn (bool source_step,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002408 LazyBool step_in_avoids_code_without_debug_info,
2409 LazyBool step_out_avoids_code_without_debug_info)
2410
Greg Clayton44d93782014-01-27 23:43:24 +00002411{
2412 Error error;
2413 Process *process = GetProcess().get();
2414 if (StateIsStoppedState (process->GetState(), true))
2415 {
2416 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2417 ThreadPlanSP new_plan_sp;
2418 const lldb::RunMode run_mode = eOnlyThisThread;
2419 const bool abort_other_plans = false;
2420
2421 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2422 {
2423 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2424 new_plan_sp = QueueThreadPlanForStepInRange (abort_other_plans,
2425 sc.line_entry.range,
2426 sc,
2427 NULL,
2428 run_mode,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002429 step_in_avoids_code_without_debug_info,
2430 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002431 }
2432 else
2433 {
2434 new_plan_sp = QueueThreadPlanForStepSingleInstruction (false,
2435 abort_other_plans,
2436 run_mode);
2437 }
2438
2439 new_plan_sp->SetIsMasterPlan(true);
2440 new_plan_sp->SetOkayToDiscard(false);
2441
2442 // Why do we need to set the current thread by ID here???
2443 process->GetThreadList().SetSelectedThreadByID (GetID());
2444 error = process->Resume();
2445 }
2446 else
2447 {
2448 error.SetErrorString("process not stopped");
2449 }
2450 return error;
2451}
2452
2453Error
Jim Ingham4b4b2472014-03-13 02:47:14 +00002454Thread::StepOver (bool source_step,
2455 LazyBool step_out_avoids_code_without_debug_info)
Greg Clayton44d93782014-01-27 23:43:24 +00002456{
2457 Error error;
2458 Process *process = GetProcess().get();
2459 if (StateIsStoppedState (process->GetState(), true))
2460 {
2461 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2462 ThreadPlanSP new_plan_sp;
2463
2464 const lldb::RunMode run_mode = eOnlyThisThread;
2465 const bool abort_other_plans = false;
2466
2467 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2468 {
2469 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2470 new_plan_sp = QueueThreadPlanForStepOverRange (abort_other_plans,
2471 sc.line_entry.range,
2472 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002473 run_mode,
2474 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002475 }
2476 else
2477 {
2478 new_plan_sp = QueueThreadPlanForStepSingleInstruction (true,
2479 abort_other_plans,
2480 run_mode);
2481 }
2482
2483 new_plan_sp->SetIsMasterPlan(true);
2484 new_plan_sp->SetOkayToDiscard(false);
2485
2486 // Why do we need to set the current thread by ID here???
2487 process->GetThreadList().SetSelectedThreadByID (GetID());
2488 error = process->Resume();
2489 }
2490 else
2491 {
2492 error.SetErrorString("process not stopped");
2493 }
2494 return error;
2495}
2496
2497Error
2498Thread::StepOut ()
2499{
2500 Error error;
2501 Process *process = GetProcess().get();
2502 if (StateIsStoppedState (process->GetState(), true))
2503 {
2504 const bool first_instruction = false;
2505 const bool stop_other_threads = false;
2506 const bool abort_other_plans = false;
2507
2508 ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut (abort_other_plans,
2509 NULL,
2510 first_instruction,
2511 stop_other_threads,
2512 eVoteYes,
2513 eVoteNoOpinion,
2514 0));
2515
2516 new_plan_sp->SetIsMasterPlan(true);
2517 new_plan_sp->SetOkayToDiscard(false);
2518
2519 // Why do we need to set the current thread by ID here???
2520 process->GetThreadList().SetSelectedThreadByID (GetID());
2521 error = process->Resume();
2522 }
2523 else
2524 {
2525 error.SetErrorString("process not stopped");
2526 }
2527 return error;
Jim Ingham4b4b2472014-03-13 02:47:14 +00002528}