blob: a1a91a58c280556abc48a257c76bdfacafb57422 [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
Eugene Zelenko8f30a652015-10-23 18:39:37 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Jim Ingham1b54c882010-06-16 02:00:15 +000014#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000015#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Log.h"
Greg Clayton554f68d2015-02-04 22:00:53 +000017#include "lldb/Core/FormatEntity.h"
Jason Molenda03c9cd02015-08-06 21:54:29 +000018#include "lldb/Core/Module.h"
Greg Clayton160c9d82013-05-01 21:54:04 +000019#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Stream.h"
21#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000022#include "lldb/Core/RegularExpression.h"
Jim Ingham0d5a2bd2015-09-03 01:40:51 +000023#include "lldb/Core/ValueObject.h"
Greg Clayton7260f622011-04-18 08:33:37 +000024#include "lldb/Host/Host.h"
Jim Ingham4da62062014-01-23 21:52:47 +000025#include "lldb/Interpreter/OptionValueFileSpecList.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000026#include "lldb/Interpreter/OptionValueProperties.h"
27#include "lldb/Interpreter/Property.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000028#include "lldb/Symbol/Function.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000029#include "lldb/Target/ABI.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Target/DynamicLoader.h"
31#include "lldb/Target/ExecutionContext.h"
32#include "lldb/Target/Process.h"
33#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000034#include "lldb/Target/StopInfo.h"
Jason Molendab4892cd2014-05-13 22:02:48 +000035#include "lldb/Target/SystemRuntime.h"
Greg Clayton896dff62010-07-23 16:45:51 +000036#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Target/Thread.h"
38#include "lldb/Target/ThreadPlan.h"
39#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Target/ThreadPlanBase.h"
Jim Ingham2bdbfd52014-09-29 23:17:18 +000041#include "lldb/Target/ThreadPlanPython.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042#include "lldb/Target/ThreadPlanStepInstruction.h"
43#include "lldb/Target/ThreadPlanStepOut.h"
44#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
45#include "lldb/Target/ThreadPlanStepThrough.h"
46#include "lldb/Target/ThreadPlanStepInRange.h"
47#include "lldb/Target/ThreadPlanStepOverRange.h"
48#include "lldb/Target/ThreadPlanRunToAddress.h"
49#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000050#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000051#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000052#include "Plugins/Process/Utility/UnwindLLDB.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000053#include "Plugins/Process/Utility/UnwindMacOSXFrameBackchain.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000054
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055using namespace lldb;
56using namespace lldb_private;
57
Greg Clayton67cc0632012-08-22 17:17:09 +000058const ThreadPropertiesSP &
59Thread::GetGlobalProperties()
60{
61 static ThreadPropertiesSP g_settings_sp;
62 if (!g_settings_sp)
63 g_settings_sp.reset (new ThreadProperties (true));
64 return g_settings_sp;
65}
66
67static PropertyDefinition
68g_properties[] =
69{
Jim Ingham4b4b2472014-03-13 02:47:14 +000070 { "step-in-avoid-nodebug", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, step-in will not stop in functions with no debug information." },
71 { "step-out-avoid-nodebug", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true, when step-in/step-out/step-over leave the current frame, they will continue to step out till they come to a function with "
72 "debug information. Passing a frame argument to step-out will override this option." },
Greg Clayton7bd4c602015-01-21 21:51:02 +000073 { "step-avoid-regexp", OptionValue::eTypeRegex , true , 0, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
74 { "step-avoid-libraries", OptionValue::eTypeFileSpecList , true , 0, NULL, NULL, "A list of libraries that source stepping won't stop in." },
Greg Clayton67cc0632012-08-22 17:17:09 +000075 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
76 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
77};
78
79enum {
Jim Ingham4b4b2472014-03-13 02:47:14 +000080 ePropertyStepInAvoidsNoDebug,
81 ePropertyStepOutAvoidsNoDebug,
Greg Clayton67cc0632012-08-22 17:17:09 +000082 ePropertyStepAvoidRegex,
Jim Ingham4da62062014-01-23 21:52:47 +000083 ePropertyStepAvoidLibraries,
Greg Clayton67cc0632012-08-22 17:17:09 +000084 ePropertyEnableThreadTrace
85};
86
Greg Clayton67cc0632012-08-22 17:17:09 +000087class ThreadOptionValueProperties : public OptionValueProperties
88{
89public:
90 ThreadOptionValueProperties (const ConstString &name) :
91 OptionValueProperties (name)
92 {
93 }
94
95 // This constructor is used when creating ThreadOptionValueProperties when it
96 // is part of a new lldb_private::Thread instance. It will copy all current
97 // global property values as needed
98 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +000099 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +0000100 {
101 }
102
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000103 const Property *
104 GetPropertyAtIndex(const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const override
Greg Clayton67cc0632012-08-22 17:17:09 +0000105 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +0000106 // When getting the value for a key from the thread options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +0000107 // try and grab the setting from the current thread if there is one. Else we just
108 // use the one from this instance.
109 if (exe_ctx)
110 {
111 Thread *thread = exe_ctx->GetThreadPtr();
112 if (thread)
113 {
114 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
115 if (this != instance_properties)
116 return instance_properties->ProtectedGetPropertyAtIndex (idx);
117 }
118 }
119 return ProtectedGetPropertyAtIndex (idx);
120 }
121};
122
Greg Clayton67cc0632012-08-22 17:17:09 +0000123ThreadProperties::ThreadProperties (bool is_global) :
124 Properties ()
125{
126 if (is_global)
127 {
128 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
129 m_collection_sp->Initialize(g_properties);
130 }
131 else
132 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
133}
134
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000135ThreadProperties::~ThreadProperties() = default;
Greg Clayton67cc0632012-08-22 17:17:09 +0000136
137const RegularExpression *
138ThreadProperties::GetSymbolsToAvoidRegexp()
139{
140 const uint32_t idx = ePropertyStepAvoidRegex;
141 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
142}
143
Jim Ingham4da62062014-01-23 21:52:47 +0000144FileSpecList &
145ThreadProperties::GetLibrariesToAvoid() const
146{
147 const uint32_t idx = ePropertyStepAvoidLibraries;
148 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
149 assert(option_value);
150 return option_value->GetCurrentValue();
151}
152
Greg Clayton67cc0632012-08-22 17:17:09 +0000153bool
154ThreadProperties::GetTraceEnabledState() const
155{
156 const uint32_t idx = ePropertyEnableThreadTrace;
157 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
158}
159
Jim Ingham4b4b2472014-03-13 02:47:14 +0000160bool
161ThreadProperties::GetStepInAvoidsNoDebug() const
162{
163 const uint32_t idx = ePropertyStepInAvoidsNoDebug;
164 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
165}
166
167bool
168ThreadProperties::GetStepOutAvoidsNoDebug() const
169{
170 const uint32_t idx = ePropertyStepOutAvoidsNoDebug;
171 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
172}
173
Jim Ingham4f465cf2012-10-10 18:32:14 +0000174//------------------------------------------------------------------
175// Thread Event Data
176//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000177
Jim Ingham4f465cf2012-10-10 18:32:14 +0000178const ConstString &
179Thread::ThreadEventData::GetFlavorString ()
180{
181 static ConstString g_flavor ("Thread::ThreadEventData");
182 return g_flavor;
183}
184
185Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
186 m_thread_sp (thread_sp),
187 m_stack_id ()
188{
189}
190
191Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
192 m_thread_sp (thread_sp),
193 m_stack_id (stack_id)
194{
195}
196
197Thread::ThreadEventData::ThreadEventData () :
198 m_thread_sp (),
199 m_stack_id ()
200{
201}
202
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000203Thread::ThreadEventData::~ThreadEventData() = default;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000204
205void
206Thread::ThreadEventData::Dump (Stream *s) const
207{
Jim Ingham4f465cf2012-10-10 18:32:14 +0000208}
209
210const Thread::ThreadEventData *
211Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
212{
213 if (event_ptr)
214 {
215 const EventData *event_data = event_ptr->GetData();
216 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
217 return static_cast <const ThreadEventData *> (event_ptr->GetData());
218 }
219 return NULL;
220}
221
222ThreadSP
223Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
224{
225 ThreadSP thread_sp;
226 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
227 if (event_data)
228 thread_sp = event_data->GetThread();
229 return thread_sp;
230}
231
232StackID
233Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
234{
235 StackID stack_id;
236 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
237 if (event_data)
238 stack_id = event_data->GetStackID();
239 return stack_id;
240}
241
Jason Molendab57e4a12013-11-04 09:33:30 +0000242StackFrameSP
Jim Ingham4f465cf2012-10-10 18:32:14 +0000243Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
244{
245 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
Jason Molendab57e4a12013-11-04 09:33:30 +0000246 StackFrameSP frame_sp;
Jim Ingham4f465cf2012-10-10 18:32:14 +0000247 if (event_data)
248 {
249 ThreadSP thread_sp = event_data->GetThread();
250 if (thread_sp)
251 {
252 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
253 }
254 }
255 return frame_sp;
256}
257
258//------------------------------------------------------------------
259// Thread class
260//------------------------------------------------------------------
261
262ConstString &
263Thread::GetStaticBroadcasterClass ()
264{
265 static ConstString class_name ("lldb.thread");
266 return class_name;
267}
268
Jason Molendaa8ff5432014-03-06 06:31:18 +0000269Thread::Thread (Process &process, lldb::tid_t tid, bool use_invalid_index_id) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000270 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000272 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
273 m_process_wp (process.shared_from_this()),
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000274 m_stop_info_sp (),
275 m_stop_info_stop_id (0),
Greg Claytona97c4d22014-12-09 23:31:02 +0000276 m_stop_info_override_stop_id (0),
Jason Molendaa8ff5432014-03-06 06:31:18 +0000277 m_index_id (use_invalid_index_id ? LLDB_INVALID_INDEX32 : process.GetNextThreadIndexID(tid)),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000280 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000283 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000284 m_curr_frames_sp (),
285 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000287 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000288 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000289 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000290 m_destroy_called (false),
Jason Molenda705b1802014-06-13 02:37:02 +0000291 m_override_should_notify (eLazyBoolCalculate),
292 m_extended_info_fetched (false),
293 m_extended_info ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294{
Greg Clayton5160ce52013-03-27 23:08:40 +0000295 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000296 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000297 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")",
298 static_cast<void*>(this), GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299
Jim Ingham4f465cf2012-10-10 18:32:14 +0000300 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301 QueueFundamentalPlan(true);
302}
303
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304Thread::~Thread()
305{
Greg Clayton5160ce52013-03-27 23:08:40 +0000306 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000308 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")",
309 static_cast<void*>(this), GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000310 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
311 assert (m_destroy_called);
312}
313
314void
315Thread::DestroyThread ()
316{
Jim Inghamb1499242013-10-18 17:11:02 +0000317 // Tell any plans on the plan stacks that the thread is being destroyed since
318 // any plans that have a thread go away in the middle of might need
319 // to do cleanup, or in some cases NOT do cleanup...
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000320 for (auto plan : m_plan_stack)
321 plan->ThreadDestroyed();
322
Jim Inghamb1499242013-10-18 17:11:02 +0000323 for (auto plan : m_discarded_plan_stack)
324 plan->ThreadDestroyed();
325
326 for (auto plan : m_completed_plan_stack)
327 plan->ThreadDestroyed();
328
Greg Clayton35a4cc52012-10-29 20:52:08 +0000329 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000330 m_plan_stack.clear();
331 m_discarded_plan_stack.clear();
332 m_completed_plan_stack.clear();
Greg Clayton6e10f142013-07-30 00:23:06 +0000333
334 // Push a ThreadPlanNull on the plan stack. That way we can continue assuming that the
335 // plan stack is never empty, but if somebody errantly asks questions of a destroyed thread
336 // without checking first whether it is destroyed, they won't crash.
337 ThreadPlanSP null_plan_sp(new ThreadPlanNull (*this));
338 m_plan_stack.push_back (null_plan_sp);
339
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000340 m_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000341 m_reg_context_sp.reset();
342 m_unwinder_ap.reset();
343 Mutex::Locker locker(m_frame_mutex);
344 m_curr_frames_sp.reset();
345 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346}
347
Jim Ingham4f465cf2012-10-10 18:32:14 +0000348void
349Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
350{
351 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
352 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
353}
354
Jason Molendaef7d6412015-08-06 03:27:10 +0000355lldb::StackFrameSP
356Thread::GetSelectedFrame()
357{
358 StackFrameListSP stack_frame_list_sp(GetStackFrameList());
359 StackFrameSP frame_sp = stack_frame_list_sp->GetFrameAtIndex (stack_frame_list_sp->GetSelectedFrameIndex());
360 FunctionOptimizationWarning (frame_sp.get());
361 return frame_sp;
362}
363
Jim Ingham4f465cf2012-10-10 18:32:14 +0000364uint32_t
Jason Molendab57e4a12013-11-04 09:33:30 +0000365Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
Jim Ingham4f465cf2012-10-10 18:32:14 +0000366{
367 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
368 if (broadcast)
369 BroadcastSelectedFrameChange(frame->GetStackID());
Jason Molendaef7d6412015-08-06 03:27:10 +0000370 FunctionOptimizationWarning (frame);
Jim Ingham4f465cf2012-10-10 18:32:14 +0000371 return ret_value;
372}
373
374bool
375Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
376{
Jason Molendab57e4a12013-11-04 09:33:30 +0000377 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
Jim Ingham4f465cf2012-10-10 18:32:14 +0000378 if (frame_sp)
379 {
380 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
381 if (broadcast)
382 BroadcastSelectedFrameChange(frame_sp->GetStackID());
Jason Molendaef7d6412015-08-06 03:27:10 +0000383 FunctionOptimizationWarning (frame_sp.get());
Jim Ingham4f465cf2012-10-10 18:32:14 +0000384 return true;
385 }
386 else
387 return false;
388}
389
Jim Ingham93208b82013-01-31 21:46:01 +0000390bool
391Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
392{
393 const bool broadcast = true;
394 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
395 if (success)
396 {
Jason Molendab57e4a12013-11-04 09:33:30 +0000397 StackFrameSP frame_sp = GetSelectedFrame();
Jim Ingham93208b82013-01-31 21:46:01 +0000398 if (frame_sp)
399 {
400 bool already_shown = false;
401 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
402 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
403 {
404 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
405 }
406
407 bool show_frame_info = true;
408 bool show_source = !already_shown;
Jason Molendaef7d6412015-08-06 03:27:10 +0000409 FunctionOptimizationWarning (frame_sp.get());
Jim Ingham93208b82013-01-31 21:46:01 +0000410 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
411 }
412 return false;
413 }
414 else
415 return false;
416}
417
Jason Molendaef7d6412015-08-06 03:27:10 +0000418void
419Thread::FunctionOptimizationWarning (StackFrame *frame)
420{
Jason Molenda484900b2015-08-10 07:55:25 +0000421 if (frame && frame->HasDebugInformation() && GetProcess()->GetWarningsOptimization() == true)
Jason Molendaef7d6412015-08-06 03:27:10 +0000422 {
Jason Molenda03c9cd02015-08-06 21:54:29 +0000423 SymbolContext sc = frame->GetSymbolContext (eSymbolContextFunction | eSymbolContextModule);
Jason Molenda484900b2015-08-10 07:55:25 +0000424 GetProcess()->PrintWarningOptimization (sc);
Jason Molendaef7d6412015-08-06 03:27:10 +0000425 }
426}
427
Jim Inghamb15bfc72010-10-20 00:39:53 +0000428lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000429Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430{
Greg Clayton6e10f142013-07-30 00:23:06 +0000431 if (m_destroy_called)
432 return m_stop_info_sp;
433
Jim Inghamb15bfc72010-10-20 00:39:53 +0000434 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000435 ProcessSP process_sp (GetProcess());
436 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000437 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000438 {
Jim Ingham30fadaf2014-07-08 01:07:32 +0000439 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject(), GetExpressionVariable());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000440 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000441 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000442 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000443 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
444 (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 +0000445 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000446 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000447 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000448 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000449 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000450 GetPrivateStopInfo ();
451 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000452 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000453 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454}
455
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000456lldb::StopInfoSP
457Thread::GetPrivateStopInfo ()
458{
Greg Clayton6e10f142013-07-30 00:23:06 +0000459 if (m_destroy_called)
460 return m_stop_info_sp;
461
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000462 ProcessSP process_sp (GetProcess());
463 if (process_sp)
464 {
Daniel Malea246cb612013-05-14 15:20:12 +0000465 const uint32_t process_stop_id = process_sp->GetStopID();
466 if (m_stop_info_stop_id != process_stop_id)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000467 {
Daniel Malea246cb612013-05-14 15:20:12 +0000468 if (m_stop_info_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000469 {
Daniel Malea246cb612013-05-14 15:20:12 +0000470 if (m_stop_info_sp->IsValid()
471 || IsStillAtLastBreakpointHit()
472 || GetCurrentPlan()->IsVirtualStep())
473 SetStopInfo (m_stop_info_sp);
474 else
475 m_stop_info_sp.reset();
476 }
477
478 if (!m_stop_info_sp)
479 {
480 if (CalculateStopInfo() == false)
481 SetStopInfo (StopInfoSP());
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000482 }
483 }
Greg Claytona97c4d22014-12-09 23:31:02 +0000484
485 // The stop info can be manually set by calling Thread::SetStopInfo()
486 // prior to this function ever getting called, so we can't rely on
487 // "m_stop_info_stop_id != process_stop_id" as the condition for
488 // the if statement below, we must also check the stop info to see
489 // if we need to override it. See the header documentation in
490 // Process::GetStopInfoOverrideCallback() for more information on
491 // the stop info override callback.
492 if (m_stop_info_override_stop_id != process_stop_id)
493 {
494 m_stop_info_override_stop_id = process_stop_id;
495 if (m_stop_info_sp)
496 {
497 ArchSpec::StopInfoOverrideCallbackType callback = GetProcess()->GetStopInfoOverrideCallback();
498 if (callback)
499 callback(*this);
500 }
501 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000502 }
503 return m_stop_info_sp;
504}
505
Greg Clayton97d5cf02012-09-25 02:40:06 +0000506lldb::StopReason
507Thread::GetStopReason()
508{
509 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
510 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000511 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000512 return eStopReasonNone;
513}
514
Greg Clayton2e309072015-07-17 23:42:28 +0000515bool
516Thread::StopInfoIsUpToDate() const
517{
518 ProcessSP process_sp (GetProcess());
519 if (process_sp)
520 return m_stop_info_stop_id == process_sp->GetStopID();
521 else
522 return true; // Process is no longer around so stop info is always up to date...
523}
Greg Clayton97d5cf02012-09-25 02:40:06 +0000524
Jim Ingham77787032011-01-20 02:03:18 +0000525void
526Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
527{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000528 m_stop_info_sp = stop_info_sp;
529 if (m_stop_info_sp)
Jim Ingham221d51c2013-05-08 00:35:16 +0000530 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000531 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham221d51c2013-05-08 00:35:16 +0000532 // If we are overriding the ShouldReportStop, do that here:
533 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000534 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000535 }
536
Greg Clayton1ac04c32012-02-21 00:09:25 +0000537 ProcessSP process_sp (GetProcess());
538 if (process_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000539 m_stop_info_stop_id = process_sp->GetStopID();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000540 else
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000541 m_stop_info_stop_id = UINT32_MAX;
Greg Clayton8cda7f02013-05-21 21:55:59 +0000542 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
543 if (log)
Ed Maste2bc76432014-06-25 00:38:35 +0000544 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000545 static_cast<void*>(this), GetID(),
546 stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>",
547 m_stop_info_stop_id);
Jim Ingham77787032011-01-20 02:03:18 +0000548}
549
550void
Jim Ingham221d51c2013-05-08 00:35:16 +0000551Thread::SetShouldReportStop (Vote vote)
552{
553 if (vote == eVoteNoOpinion)
554 return;
555 else
556 {
557 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000558 if (m_stop_info_sp)
559 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000560 }
561}
562
563void
Jim Ingham77787032011-01-20 02:03:18 +0000564Thread::SetStopInfoToNothing()
565{
566 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
567 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
568 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
569}
570
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571bool
572Thread::ThreadStoppedForAReason (void)
573{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000574 return (bool) GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575}
576
Jim Ingham77787032011-01-20 02:03:18 +0000577bool
578Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
579{
Greg Claytonf74cf862013-11-13 23:28:31 +0000580 saved_state.register_backup_sp.reset();
581 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
582 if (frame_sp)
583 {
584 lldb::RegisterCheckpointSP reg_checkpoint_sp(new RegisterCheckpoint(RegisterCheckpoint::Reason::eExpression));
585 if (reg_checkpoint_sp)
586 {
587 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
588 if (reg_ctx_sp && reg_ctx_sp->ReadAllRegisterValues (*reg_checkpoint_sp))
589 saved_state.register_backup_sp = reg_checkpoint_sp;
590 }
591 }
592 if (!saved_state.register_backup_sp)
Jim Ingham77787032011-01-20 02:03:18 +0000593 return false;
594
595 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000596 ProcessSP process_sp (GetProcess());
597 if (process_sp)
598 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000599 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
600
Jim Ingham77787032011-01-20 02:03:18 +0000601 return true;
602}
603
604bool
Jim Ingham8559a352012-11-26 23:52:18 +0000605Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000606{
Greg Claytonf74cf862013-11-13 23:28:31 +0000607 if (saved_state.register_backup_sp)
608 {
609 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
610 if (frame_sp)
611 {
612 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
613 if (reg_ctx_sp)
614 {
615 bool ret = reg_ctx_sp->WriteAllRegisterValues (*saved_state.register_backup_sp);
616
617 // Clear out all stack frames as our world just changed.
618 ClearStackFrames();
619 reg_ctx_sp->InvalidateIfNeeded(true);
620 if (m_unwinder_ap.get())
621 m_unwinder_ap->Clear();
622 return ret;
623 }
624 }
625 }
626 return false;
Jim Ingham8559a352012-11-26 23:52:18 +0000627}
628
629bool
630Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
631{
Jim Ingham0c270682011-01-25 02:47:23 +0000632 if (saved_state.stop_info_sp)
633 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000634 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000635 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000636 return true;
637}
638
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639StateType
640Thread::GetState() const
641{
642 // If any other threads access this we will need a mutex for it
643 Mutex::Locker locker(m_state_mutex);
644 return m_state;
645}
646
647void
648Thread::SetState(StateType state)
649{
650 Mutex::Locker locker(m_state_mutex);
651 m_state = state;
652}
653
654void
655Thread::WillStop()
656{
657 ThreadPlan *current_plan = GetCurrentPlan();
658
659 // FIXME: I may decide to disallow threads with no plans. In which
660 // case this should go to an assert.
661
662 if (!current_plan)
663 return;
664
665 current_plan->WillStop();
666}
667
668void
669Thread::SetupForResume ()
670{
671 if (GetResumeState() != eStateSuspended)
672 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
674 // telling the current plan it will resume, since we might change what the current
675 // plan is.
676
Greg Clayton1afa68e2013-04-02 20:32:37 +0000677 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
678 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679 {
Greg Claytona97c4d22014-12-09 23:31:02 +0000680 const addr_t thread_pc = reg_ctx_sp->GetPC();
681 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(thread_pc);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000682 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000683 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000684 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
685 // special to step over a breakpoint.
686
687 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000688
Greg Claytona97c4d22014-12-09 23:31:02 +0000689 bool push_step_over_bp_plan = false;
690 if (cur_plan->GetKind() == ThreadPlan::eKindStepOverBreakpoint)
691 {
692 ThreadPlanStepOverBreakpoint *bp_plan = (ThreadPlanStepOverBreakpoint *)cur_plan;
693 if (bp_plan->GetBreakpointLoadAddress() != thread_pc)
694 push_step_over_bp_plan = true;
695 }
696 else
697 push_step_over_bp_plan = true;
698
699 if (push_step_over_bp_plan)
Greg Clayton1afa68e2013-04-02 20:32:37 +0000700 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000701 ThreadPlanSP step_bp_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
702 if (step_bp_plan_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000704 ;
705 step_bp_plan_sp->SetPrivate (true);
Greg Clayton1afa68e2013-04-02 20:32:37 +0000706
707 if (GetCurrentPlan()->RunState() != eStateStepping)
708 {
Jim Ingham2bdbfd52014-09-29 23:17:18 +0000709 ThreadPlanStepOverBreakpoint *step_bp_plan
710 = static_cast<ThreadPlanStepOverBreakpoint *>(step_bp_plan_sp.get());
Greg Clayton1afa68e2013-04-02 20:32:37 +0000711 step_bp_plan->SetAutoContinue(true);
712 }
Greg Clayton1afa68e2013-04-02 20:32:37 +0000713 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 }
716 }
717 }
718 }
719}
720
721bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000722Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723{
724 // At this point clear the completed plan stack.
725 m_completed_plan_stack.clear();
726 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000727 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728
Greg Clayton97d5cf02012-09-25 02:40:06 +0000729 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000730
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000731 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
732 if (backing_thread_sp)
733 backing_thread_sp->m_temporary_resume_state = resume_state;
734
735 // Make sure m_stop_info_sp is valid
736 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000737
Jim Ingham92087d82012-01-31 23:09:20 +0000738 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
739 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
740 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
741 // about the fact that we are resuming...
Jim Inghamacbea8f2015-06-02 20:26:13 +0000742 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000743 if (m_stop_info_stop_id == process_stop_id &&
744 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000745 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000746 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000747 if (stop_info)
748 stop_info->WillResume (resume_state);
749 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750
751 // Tell all the plans that we are about to resume in case they need to clear any state.
752 // We distinguish between the plan on the top of the stack and the lower
753 // plans in case a plan needs to do any special business before it runs.
754
Greg Claytond1d06e42013-04-20 00:27:58 +0000755 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000757 if (plan_ptr)
758 {
759 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760
Greg Claytond1d06e42013-04-20 00:27:58 +0000761 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
762 {
763 plan_ptr->WillResume (resume_state, false);
764 }
765
766 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
767 // In that case, don't reset it here.
768
769 if (need_to_resume && resume_state != eStateSuspended)
770 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000771 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000772 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000773 }
774
Greg Clayton160c9d82013-05-01 21:54:04 +0000775 if (need_to_resume)
776 {
777 ClearStackFrames();
778 // Let Thread subclasses do any special work they need to prior to resuming
779 WillResume (resume_state);
780 }
781
Jim Ingham513c6bb2012-09-01 01:02:41 +0000782 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783}
784
785void
786Thread::DidResume ()
787{
788 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
789}
790
Andrew Kaylor29d65742013-05-10 17:19:04 +0000791void
792Thread::DidStop ()
793{
794 SetState (eStateStopped);
795}
796
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797bool
798Thread::ShouldStop (Event* event_ptr)
799{
800 ThreadPlan *current_plan = GetCurrentPlan();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000801
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802 bool should_stop = true;
803
Greg Clayton5160ce52013-03-27 23:08:40 +0000804 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000805
Jim Ingham92087d82012-01-31 23:09:20 +0000806 if (GetResumeState () == eStateSuspended)
807 {
808 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000809 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 +0000810 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000811 return false;
812 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000813
Jim Ingham92087d82012-01-31 23:09:20 +0000814 if (GetTemporaryResumeState () == eStateSuspended)
815 {
816 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000817 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000818 __FUNCTION__, GetID (), GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000819 return false;
820 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000821
Daniel Malea246cb612013-05-14 15:20:12 +0000822 // Based on the current thread plan and process stop info, check if this
823 // thread caused the process to stop. NOTE: this must take place before
824 // the plan is moved from the current plan stack to the completed plan
825 // stack.
Jim Ingham92087d82012-01-31 23:09:20 +0000826 if (ThreadStoppedForAReason() == false)
827 {
828 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000829 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 +0000830 __FUNCTION__, GetID (), GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000831 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000832 return false;
833 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000834
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835 if (log)
836 {
Jim Inghamdee1bc92013-06-22 00:27:45 +0000837 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 +0000838 __FUNCTION__, static_cast<void*>(this), GetID (),
Greg Clayton160c9d82013-05-01 21:54:04 +0000839 GetProtocolID (),
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000840 GetRegisterContext()
841 ? GetRegisterContext()->GetPC()
842 : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000843 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000845 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000846 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000847 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000849
Jim Ingham06e827c2010-11-11 19:26:09 +0000850 // The top most plan always gets to do the trace log...
851 current_plan->DoTraceLog ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000852
Jim Ingham6d66ce62012-04-20 21:16:56 +0000853 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
854 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
855 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000856 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Ingham6d66ce62012-04-20 21:16:56 +0000857 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
858 {
859 if (log)
860 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
861 return false;
862 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000863
Jim Inghambad39e42012-09-05 21:12:49 +0000864 // If we've already been restarted, don't query the plans since the state they would examine is not current.
865 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
866 return false;
867
868 // Before the plans see the state of the world, calculate the current inlined depth.
869 GetStackFrameList()->CalculateCurrentInlinedDepth();
870
Jim Ingham25f66702011-12-03 01:52:59 +0000871 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
872 // If that plan is still working, then we don't need to do any more work. If the plan that explains
873 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
874 // whether they still need to do more work.
875
876 bool done_processing_current_plan = false;
877
Jim Ingham0161b492013-02-09 01:29:05 +0000878 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000879 {
880 if (current_plan->TracerExplainsStop())
881 {
882 done_processing_current_plan = true;
883 should_stop = false;
884 }
885 else
886 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000887 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000888 // does and let it handle the situation.
889 ThreadPlan *plan_ptr = current_plan;
890 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
891 {
Jim Ingham0161b492013-02-09 01:29:05 +0000892 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000893 {
894 should_stop = plan_ptr->ShouldStop (event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000895
Jim Ingham25f66702011-12-03 01:52:59 +0000896 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
897 // and all the plans below it off the stack.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000898
Jim Ingham25f66702011-12-03 01:52:59 +0000899 if (plan_ptr->MischiefManaged())
900 {
Jim Ingham031177e2012-05-11 23:49:49 +0000901 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000902 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000903
Jim Ingham25f66702011-12-03 01:52:59 +0000904 do
905 {
906 if (should_stop)
907 current_plan->WillStop();
908 PopPlan();
909 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000910 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
911 // Now, if the responsible plan was not "Okay to discard" then we're done,
912 // otherwise we forward this to the next plan in the stack below.
913 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
914 done_processing_current_plan = true;
915 else
916 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000917 }
918 else
919 done_processing_current_plan = true;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000920
Jim Ingham25f66702011-12-03 01:52:59 +0000921 break;
922 }
Jim Ingham25f66702011-12-03 01:52:59 +0000923 }
924 }
925 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000926
Jim Ingham25f66702011-12-03 01:52:59 +0000927 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000929 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000930
Jim Ingham10c4b242011-10-15 00:23:43 +0000931 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000932 log->Printf("Plan %s explains stop, auto-continue %i.",
933 current_plan->GetName(), over_ride_stop);
934
Jim Ingham0f16e732011-02-08 05:20:59 +0000935 // We're starting from the base plan, so just let it decide;
936 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000937 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000938 should_stop = current_plan->ShouldStop (event_ptr);
939 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000940 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000941 }
942 else
943 {
944 // Otherwise, don't let the base plan override what the other plans say to do, since
945 // presumably if there were other plans they would know what to do...
946 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000947 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000948 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949 break;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000950
Jim Ingham0f16e732011-02-08 05:20:59 +0000951 should_stop = current_plan->ShouldStop(event_ptr);
952 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000953 log->Printf("Plan %s should stop: %d.",
954 current_plan->GetName(), should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000955 if (current_plan->MischiefManaged())
956 {
957 if (should_stop)
958 current_plan->WillStop();
959
960 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
961 // Otherwise, see if the plan's parent wants to stop.
962
963 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
964 {
965 PopPlan();
966 break;
967 }
968 else
969 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000970 PopPlan();
971
972 current_plan = GetCurrentPlan();
973 if (current_plan == NULL)
974 {
975 break;
976 }
977 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000978 }
979 else
980 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000981 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000982 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983 }
984 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000985
Jim Inghamb01e7422010-06-19 04:45:32 +0000986 if (over_ride_stop)
987 should_stop = false;
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000988 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000989
Jim Ingham8b91d0c2014-10-08 01:03:54 +0000990 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
991 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
992 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
993 // This code clears stale plans off the stack.
994
995 if (should_stop)
996 {
997 ThreadPlan *plan_ptr = GetCurrentPlan();
998 while (!PlanIsBasePlan(plan_ptr))
Jim Ingham64e7ead2012-05-03 21:19:36 +0000999 {
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001000 bool stale = plan_ptr->IsPlanStale ();
1001 ThreadPlan *examined_plan = plan_ptr;
1002 plan_ptr = GetPreviousPlan (examined_plan);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001003
Jim Ingham8b91d0c2014-10-08 01:03:54 +00001004 if (stale)
1005 {
1006 if (log)
1007 log->Printf("Plan %s being discarded in cleanup, it says it is already done.",
1008 examined_plan->GetName());
1009 DiscardThreadPlansUpToPlan(examined_plan);
Jim Ingham64e7ead2012-05-03 21:19:36 +00001010 }
1011 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001013
Jim Ingham10c4b242011-10-15 00:23:43 +00001014 if (log)
1015 {
1016 StreamString s;
1017 s.IndentMore();
1018 DumpThreadPlans(&s);
1019 log->Printf ("Plan stack final state:\n%s", s.GetData());
1020 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
1021 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022 return should_stop;
1023}
1024
1025Vote
1026Thread::ShouldReportStop (Event* event_ptr)
1027{
1028 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +00001029 StateType temp_thread_state = GetTemporaryResumeState();
1030
Greg Clayton5160ce52013-03-27 23:08:40 +00001031 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +00001032
1033 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
1034 {
1035 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001036 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 +00001037 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001038 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039
Jim Ingham92087d82012-01-31 23:09:20 +00001040 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
1041 {
1042 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001043 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 +00001044 return eVoteNoOpinion;
1045 }
1046
1047 if (!ThreadStoppedForAReason())
1048 {
1049 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001050 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 +00001051 return eVoteNoOpinion;
1052 }
1053
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001054 if (m_completed_plan_stack.size() > 0)
1055 {
1056 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +00001057 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001058 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 +00001059 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
1060 }
1061 else
Greg Clayton2cad65a2010-09-03 17:10:42 +00001062 {
Jim Ingham0161b492013-02-09 01:29:05 +00001063 Vote thread_vote = eVoteNoOpinion;
1064 ThreadPlan *plan_ptr = GetCurrentPlan();
1065 while (1)
1066 {
1067 if (plan_ptr->PlanExplainsStop(event_ptr))
1068 {
1069 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
1070 break;
1071 }
1072 if (PlanIsBasePlan(plan_ptr))
1073 break;
1074 else
1075 plan_ptr = GetPreviousPlan(plan_ptr);
1076 }
Greg Clayton2cad65a2010-09-03 17:10:42 +00001077 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +00001078 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 +00001079
1080 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +00001081 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082}
1083
1084Vote
1085Thread::ShouldReportRun (Event* event_ptr)
1086{
1087 StateType thread_state = GetResumeState ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001088
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001089 if (thread_state == eStateSuspended
1090 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +00001091 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +00001093 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001094
Greg Clayton5160ce52013-03-27 23:08:40 +00001095 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001096 if (m_completed_plan_stack.size() > 0)
1097 {
1098 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +00001099 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001100 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 +00001101 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001102 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001103 m_completed_plan_stack.back()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001104
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001105 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
1106 }
1107 else
Jim Ingham444586b2011-01-24 06:34:17 +00001108 {
1109 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +00001110 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 +00001111 GetIndexID(), static_cast<void*>(this), GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +00001112 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +00001113 GetCurrentPlan()->GetName());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001114
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +00001116 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001117}
1118
Jim Ingham1b54c882010-06-16 02:00:15 +00001119bool
1120Thread::MatchesSpec (const ThreadSpec *spec)
1121{
1122 if (spec == NULL)
1123 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +00001124
Jim Ingham3d902922012-03-07 22:03:04 +00001125 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +00001126}
1127
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128void
1129Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1130{
1131 if (thread_plan_sp)
1132 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001133 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1134 if (!thread_plan_sp->GetThreadPlanTracer())
1135 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001136 m_plan_stack.push_back (thread_plan_sp);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001137
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138 thread_plan_sp->DidPush();
1139
Greg Clayton5160ce52013-03-27 23:08:40 +00001140 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141 if (log)
1142 {
1143 StreamString s;
1144 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamdee1bc92013-06-22 00:27:45 +00001145 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001146 static_cast<void*>(this), s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001147 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001148 }
1149 }
1150}
1151
1152void
1153Thread::PopPlan ()
1154{
Greg Clayton5160ce52013-03-27 23:08:40 +00001155 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001157 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 return;
1159 else
1160 {
1161 ThreadPlanSP &plan = m_plan_stack.back();
1162 if (log)
1163 {
Daniel Malead01b2952012-11-29 21:49:15 +00001164 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165 }
1166 m_completed_plan_stack.push_back (plan);
1167 plan->WillPop();
1168 m_plan_stack.pop_back();
1169 }
1170}
1171
1172void
1173Thread::DiscardPlan ()
1174{
Jim Inghamdee1bc92013-06-22 00:27:45 +00001175 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001176 if (m_plan_stack.size() > 1)
1177 {
1178 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamdee1bc92013-06-22 00:27:45 +00001179 if (log)
1180 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1181
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182 m_discarded_plan_stack.push_back (plan);
1183 plan->WillPop();
1184 m_plan_stack.pop_back();
1185 }
1186}
1187
1188ThreadPlan *
1189Thread::GetCurrentPlan ()
1190{
Jim Inghame1471232012-04-19 00:17:05 +00001191 // There will always be at least the base plan. If somebody is mucking with a
1192 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +00001193 if (m_plan_stack.empty())
1194 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +00001195 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196}
1197
1198ThreadPlanSP
1199Thread::GetCompletedPlan ()
1200{
1201 ThreadPlanSP empty_plan_sp;
1202 if (!m_completed_plan_stack.empty())
1203 {
1204 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1205 {
1206 ThreadPlanSP completed_plan_sp;
1207 completed_plan_sp = m_completed_plan_stack[i];
1208 if (!completed_plan_sp->GetPrivate ())
1209 return completed_plan_sp;
1210 }
1211 }
1212 return empty_plan_sp;
1213}
1214
Jim Ingham73ca05a2011-12-17 01:35:57 +00001215ValueObjectSP
1216Thread::GetReturnValueObject ()
1217{
1218 if (!m_completed_plan_stack.empty())
1219 {
1220 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1221 {
1222 ValueObjectSP return_valobj_sp;
1223 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1224 if (return_valobj_sp)
Greg Claytone5214572015-07-01 23:28:31 +00001225 return return_valobj_sp;
Jim Ingham73ca05a2011-12-17 01:35:57 +00001226 }
1227 }
1228 return ValueObjectSP();
1229}
1230
Sean Callananbc8ac342015-09-04 20:49:51 +00001231ExpressionVariableSP
Jim Ingham30fadaf2014-07-08 01:07:32 +00001232Thread::GetExpressionVariable ()
1233{
1234 if (!m_completed_plan_stack.empty())
1235 {
1236 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1237 {
Sean Callananbc8ac342015-09-04 20:49:51 +00001238 ExpressionVariableSP expression_variable_sp;
Jim Ingham30fadaf2014-07-08 01:07:32 +00001239 expression_variable_sp = m_completed_plan_stack[i]->GetExpressionVariable();
1240 if (expression_variable_sp)
Greg Claytone5214572015-07-01 23:28:31 +00001241 return expression_variable_sp;
Jim Ingham30fadaf2014-07-08 01:07:32 +00001242 }
1243 }
Sean Callananbc8ac342015-09-04 20:49:51 +00001244 return ExpressionVariableSP();
Jim Ingham30fadaf2014-07-08 01:07:32 +00001245}
1246
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001247bool
1248Thread::IsThreadPlanDone (ThreadPlan *plan)
1249{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250 if (!m_completed_plan_stack.empty())
1251 {
1252 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1253 {
1254 if (m_completed_plan_stack[i].get() == plan)
1255 return true;
1256 }
1257 }
1258 return false;
1259}
1260
1261bool
1262Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1263{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001264 if (!m_discarded_plan_stack.empty())
1265 {
1266 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1267 {
1268 if (m_discarded_plan_stack[i].get() == plan)
1269 return true;
1270 }
1271 }
1272 return false;
1273}
1274
1275ThreadPlan *
1276Thread::GetPreviousPlan (ThreadPlan *current_plan)
1277{
1278 if (current_plan == NULL)
1279 return NULL;
1280
1281 int stack_size = m_completed_plan_stack.size();
1282 for (int i = stack_size - 1; i > 0; i--)
1283 {
1284 if (current_plan == m_completed_plan_stack[i].get())
1285 return m_completed_plan_stack[i-1].get();
1286 }
1287
1288 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1289 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001290 if (m_plan_stack.size() > 0)
1291 return m_plan_stack.back().get();
1292 else
1293 return NULL;
1294 }
1295
1296 stack_size = m_plan_stack.size();
1297 for (int i = stack_size - 1; i > 0; i--)
1298 {
1299 if (current_plan == m_plan_stack[i].get())
1300 return m_plan_stack[i-1].get();
1301 }
1302 return NULL;
1303}
1304
1305void
1306Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1307{
1308 if (abort_other_plans)
1309 DiscardThreadPlans(true);
1310
1311 PushPlan (thread_plan_sp);
1312}
1313
Jim Ingham06e827c2010-11-11 19:26:09 +00001314void
1315Thread::EnableTracer (bool value, bool single_stepping)
1316{
1317 int stack_size = m_plan_stack.size();
1318 for (int i = 0; i < stack_size; i++)
1319 {
1320 if (m_plan_stack[i]->GetThreadPlanTracer())
1321 {
1322 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1323 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1324 }
1325 }
1326}
1327
1328void
1329Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1330{
1331 int stack_size = m_plan_stack.size();
1332 for (int i = 0; i < stack_size; i++)
1333 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1334}
1335
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001336bool
1337Thread::DiscardUserThreadPlansUpToIndex (uint32_t thread_index)
1338{
1339 // Count the user thread plans from the back end to get the number of the one we want
1340 // to discard:
1341
1342 uint32_t idx = 0;
1343 ThreadPlan *up_to_plan_ptr = nullptr;
1344
1345 for (ThreadPlanSP plan_sp : m_plan_stack)
1346 {
1347 if (plan_sp->GetPrivate())
1348 continue;
1349 if (idx == thread_index)
1350 {
1351 up_to_plan_ptr = plan_sp.get();
1352 break;
1353 }
1354 else
1355 idx++;
1356 }
1357
1358 if (up_to_plan_ptr == nullptr)
1359 return false;
1360
1361 DiscardThreadPlansUpToPlan(up_to_plan_ptr);
1362 return true;
1363}
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001364
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001366Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1367{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001368 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1369}
1370
1371void
1372Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1373{
Greg Clayton5160ce52013-03-27 23:08:40 +00001374 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001375 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001376 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p",
1377 GetID(), static_cast<void*>(up_to_plan_ptr));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001378
1379 int stack_size = m_plan_stack.size();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001380
Jim Ingham399f1ca2010-11-05 19:25:48 +00001381 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1382 // stack, and if so discard up to and including it.
Saleem Abdulrasool324a1032014-04-04 04:06:10 +00001383
Jim Ingham64e7ead2012-05-03 21:19:36 +00001384 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001385 {
1386 for (int i = stack_size - 1; i > 0; i--)
1387 DiscardPlan();
1388 }
1389 else
1390 {
1391 bool found_it = false;
1392 for (int i = stack_size - 1; i > 0; i--)
1393 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001394 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001395 found_it = true;
1396 }
1397 if (found_it)
1398 {
1399 bool last_one = false;
1400 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1401 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001402 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001403 last_one = true;
1404 DiscardPlan();
1405 }
1406 }
1407 }
Jim Ingham399f1ca2010-11-05 19:25:48 +00001408}
1409
1410void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411Thread::DiscardThreadPlans(bool force)
1412{
Greg Clayton5160ce52013-03-27 23:08:40 +00001413 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414 if (log)
1415 {
Daniel Malead01b2952012-11-29 21:49:15 +00001416 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001417 }
1418
1419 if (force)
1420 {
1421 int stack_size = m_plan_stack.size();
1422 for (int i = stack_size - 1; i > 0; i--)
1423 {
1424 DiscardPlan();
1425 }
1426 return;
1427 }
1428
1429 while (1)
1430 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001431 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001432 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433
1434 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1435 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1436 {
1437 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1438 {
1439 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1440 break;
1441 }
1442 }
1443
1444 if (discard)
1445 {
1446 // First pop all the dependent plans:
1447 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1448 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001449 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1450 // for the plan leaves it in a state that it is safe to pop the plan
1451 // with no more notice?
1452 DiscardPlan();
1453 }
1454
1455 // Now discard the master plan itself.
1456 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1457 // discard it's dependent plans, but not it...
1458 if (master_plan_idx > 0)
1459 {
1460 DiscardPlan();
1461 }
1462 }
1463 else
1464 {
1465 // If the master plan doesn't want to get discarded, then we're done.
1466 break;
1467 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001468 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001469}
1470
Jim Inghamcf274f92012-04-09 22:37:39 +00001471bool
1472Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1473{
1474 if (plan_ptr->IsBasePlan())
1475 return true;
1476 else if (m_plan_stack.size() == 0)
1477 return false;
1478 else
1479 return m_plan_stack[0].get() == plan_ptr;
1480}
1481
Jim Ingham93208b82013-01-31 21:46:01 +00001482Error
1483Thread::UnwindInnermostExpression()
1484{
1485 Error error;
1486 int stack_size = m_plan_stack.size();
1487
1488 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1489 // stack, and if so discard up to and including it.
1490
1491 for (int i = stack_size - 1; i > 0; i--)
1492 {
1493 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1494 {
1495 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1496 return error;
1497 }
1498 }
1499 error.SetErrorString("No expressions currently active on this thread");
1500 return error;
1501}
1502
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001503ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001504Thread::QueueFundamentalPlan (bool abort_other_plans)
1505{
1506 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1507 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001508 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001509}
1510
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001511ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001512Thread::QueueThreadPlanForStepSingleInstruction(bool step_over,
1513 bool abort_other_plans,
1514 bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001515{
1516 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1517 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001518 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001519}
1520
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001521ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001522Thread::QueueThreadPlanForStepOverRange(bool abort_other_plans,
1523 const AddressRange &range,
1524 const SymbolContext &addr_context,
1525 lldb::RunMode stop_other_threads,
1526 LazyBool step_out_avoids_code_withoug_debug_info)
Jim Inghamc6276822012-12-12 19:58:40 +00001527{
1528 ThreadPlanSP thread_plan_sp;
Jim Ingham4b4b2472014-03-13 02:47:14 +00001529 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads, step_out_avoids_code_withoug_debug_info));
1530
Jim Inghamc6276822012-12-12 19:58:40 +00001531 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001532 return thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001533}
1534
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001535ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001536Thread::QueueThreadPlanForStepInRange(bool abort_other_plans,
1537 const AddressRange &range,
1538 const SymbolContext &addr_context,
1539 const char *step_in_target,
1540 lldb::RunMode stop_other_threads,
1541 LazyBool step_in_avoids_code_without_debug_info,
1542 LazyBool step_out_avoids_code_without_debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001543{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001544 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInRange (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001545 range,
1546 addr_context,
1547 stop_other_threads,
1548 step_in_avoids_code_without_debug_info,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001549 step_out_avoids_code_without_debug_info));
1550 ThreadPlanStepInRange *plan = static_cast<ThreadPlanStepInRange *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001551
Jim Inghamc6276822012-12-12 19:58:40 +00001552 if (step_in_target)
1553 plan->SetStepInTarget(step_in_target);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001554
1555 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001556 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001557}
1558
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001559ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001560Thread::QueueThreadPlanForStepOut(bool abort_other_plans,
1561 SymbolContext *addr_context,
1562 bool first_insn,
1563 bool stop_other_threads,
1564 Vote stop_vote,
1565 Vote run_vote,
1566 uint32_t frame_idx,
1567 LazyBool step_out_avoids_code_withoug_debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001568{
Greg Clayton481cef22011-01-21 06:11:58 +00001569 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1570 addr_context,
1571 first_insn,
1572 stop_other_threads,
1573 stop_vote,
1574 run_vote,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001575 frame_idx,
1576 step_out_avoids_code_withoug_debug_info));
1577
1578 if (thread_plan_sp->ValidatePlan(NULL))
1579 {
1580 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1581 return thread_plan_sp;
1582 }
1583 else
1584 {
1585 return ThreadPlanSP();
1586 }
1587}
1588
1589ThreadPlanSP
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001590Thread::QueueThreadPlanForStepOutNoShouldStop(bool abort_other_plans,
1591 SymbolContext *addr_context,
1592 bool first_insn,
1593 bool stop_other_threads,
1594 Vote stop_vote,
1595 Vote run_vote,
1596 uint32_t frame_idx)
Jim Ingham4b4b2472014-03-13 02:47:14 +00001597{
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001598 ThreadPlanSP thread_plan_sp(new ThreadPlanStepOut (*this,
Jim Ingham4b4b2472014-03-13 02:47:14 +00001599 addr_context,
1600 first_insn,
1601 stop_other_threads,
1602 stop_vote,
1603 run_vote,
1604 frame_idx,
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001605 eLazyBoolNo));
1606
1607 ThreadPlanStepOut *new_plan = static_cast<ThreadPlanStepOut *>(thread_plan_sp.get());
Jim Ingham4b4b2472014-03-13 02:47:14 +00001608 new_plan->ClearShouldStopHereCallbacks();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001609
Sean Callanan708709c2012-07-31 22:19:25 +00001610 if (thread_plan_sp->ValidatePlan(NULL))
1611 {
1612 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001613 return thread_plan_sp;
Sean Callanan708709c2012-07-31 22:19:25 +00001614 }
1615 else
1616 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001617 return ThreadPlanSP();
Sean Callanan708709c2012-07-31 22:19:25 +00001618 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001619}
1620
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001621ThreadPlanSP
Jim Ingham18de2fd2012-05-10 01:35:39 +00001622Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001623{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001624 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001625 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001626 return ThreadPlanSP();
Jim Ingham25f66702011-12-03 01:52:59 +00001627
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001628 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001629 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001630}
1631
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001632ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001633Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1634 Address &target_addr,
1635 bool stop_other_threads)
1636{
1637 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1638 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001639 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001640}
1641
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001642ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001643Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001644 lldb::addr_t *address_list,
1645 size_t num_addresses,
1646 bool stop_other_threads,
1647 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001648{
Greg Clayton481cef22011-01-21 06:11:58 +00001649 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001650 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001651 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001652
1653}
1654
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001655lldb::ThreadPlanSP
1656Thread::QueueThreadPlanForStepScripted (bool abort_other_plans,
1657 const char *class_name,
1658 bool stop_other_threads)
1659{
1660 ThreadPlanSP thread_plan_sp (new ThreadPlanPython (*this, class_name));
1661 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1662 // This seems a little funny, but I don't want to have to split up the constructor and the
1663 // DidPush in the scripted plan, that seems annoying.
1664 // That means the constructor has to be in DidPush.
1665 // So I have to validate the plan AFTER pushing it, and then take it off again...
1666 if (!thread_plan_sp->ValidatePlan(nullptr))
1667 {
1668 DiscardThreadPlansUpToPlan(thread_plan_sp);
1669 return ThreadPlanSP();
1670 }
1671 else
1672 return thread_plan_sp;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001673}
1674
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001675uint32_t
1676Thread::GetIndexID () const
1677{
1678 return m_index_id;
1679}
1680
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001681static void
1682PrintPlanElement (Stream *s, const ThreadPlanSP &plan, lldb::DescriptionLevel desc_level, int32_t elem_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001684 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001685 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001686 s->Printf ("Element %d: ", elem_idx);
1687 plan->GetDescription (s, desc_level);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001688 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001689 s->IndentLess();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001690}
1691
1692static void
1693PrintPlanStack (Stream *s, const std::vector<lldb::ThreadPlanSP> &plan_stack, lldb::DescriptionLevel desc_level, bool include_internal)
1694{
1695 int32_t print_idx = 0;
1696 for (ThreadPlanSP plan_sp : plan_stack)
1697 {
1698 if (include_internal || !plan_sp->GetPrivate())
1699 {
1700 PrintPlanElement (s, plan_sp, desc_level, print_idx++);
1701 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001702 }
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001703}
1704
1705void
1706Thread::DumpThreadPlans (Stream *s,
1707 lldb::DescriptionLevel desc_level,
1708 bool include_internal,
1709 bool ignore_boring_threads) const
1710{
Jason Molenda60b5da62014-10-16 07:53:46 +00001711 uint32_t stack_size;
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001712
1713 if (ignore_boring_threads)
1714 {
1715 uint32_t stack_size = m_plan_stack.size();
1716 uint32_t completed_stack_size = m_completed_plan_stack.size();
1717 uint32_t discarded_stack_size = m_discarded_plan_stack.size();
1718 if (stack_size == 1 && completed_stack_size == 0 && discarded_stack_size == 0)
1719 {
1720 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 "\n", GetIndexID(), GetID());
1721 s->IndentMore();
1722 s->Indent();
1723 s->Printf("No active thread plans\n");
1724 s->IndentLess();
1725 return;
1726 }
1727 }
1728
1729 s->Indent();
1730 s->Printf ("thread #%u: tid = 0x%4.4" PRIx64 ":\n", GetIndexID(), GetID());
1731 s->IndentMore();
1732 s->Indent();
1733 s->Printf ("Active plan stack:\n");
1734 PrintPlanStack (s, m_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001735
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001737 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001738 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001739 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001740 s->Printf ("Completed Plan Stack:\n");
1741 PrintPlanStack (s, m_completed_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001742 }
1743
1744 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001745 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001746 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001747 s->Indent();
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001748 s->Printf ("Discarded Plan Stack:\n");
1749 PrintPlanStack (s, m_discarded_plan_stack, desc_level, include_internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001750 }
1751
Jim Ingham2bdbfd52014-09-29 23:17:18 +00001752 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001753}
1754
Greg Claytond9e416c2012-02-18 05:35:26 +00001755TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001756Thread::CalculateTarget ()
1757{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001758 TargetSP target_sp;
1759 ProcessSP process_sp(GetProcess());
1760 if (process_sp)
1761 target_sp = process_sp->CalculateTarget();
1762 return target_sp;
1763
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001764}
1765
Greg Claytond9e416c2012-02-18 05:35:26 +00001766ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001767Thread::CalculateProcess ()
1768{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001769 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001770}
1771
Greg Claytond9e416c2012-02-18 05:35:26 +00001772ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001773Thread::CalculateThread ()
1774{
Greg Claytond9e416c2012-02-18 05:35:26 +00001775 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001776}
1777
Jason Molendab57e4a12013-11-04 09:33:30 +00001778StackFrameSP
1779Thread::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001780{
Jason Molendab57e4a12013-11-04 09:33:30 +00001781 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001782}
1783
1784void
Greg Clayton0603aa92010-10-04 01:05:56 +00001785Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001786{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001787 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001788}
1789
Greg Clayton39d0ab32012-03-29 01:41:38 +00001790StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001791Thread::GetStackFrameList ()
1792{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001793 StackFrameListSP frame_list_sp;
1794 Mutex::Locker locker(m_frame_mutex);
1795 if (m_curr_frames_sp)
1796 {
1797 frame_list_sp = m_curr_frames_sp;
1798 }
1799 else
1800 {
1801 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1802 m_curr_frames_sp = frame_list_sp;
1803 }
1804 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001805}
1806
Greg Clayton12daf9462010-08-25 00:35:26 +00001807void
1808Thread::ClearStackFrames ()
1809{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001810 Mutex::Locker locker(m_frame_mutex);
1811
Greg Clayton160c9d82013-05-01 21:54:04 +00001812 Unwind *unwinder = GetUnwinder ();
1813 if (unwinder)
1814 unwinder->Clear();
1815
Jim Inghamb0c72a52012-02-29 03:40:22 +00001816 // Only store away the old "reference" StackFrameList if we got all its frames:
1817 // FIXME: At some point we can try to splice in the frames we have fetched into
1818 // the new frame as we make it, but let's not try that now.
1819 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001820 m_prev_frames_sp.swap (m_curr_frames_sp);
1821 m_curr_frames_sp.reset();
Jason Molenda705b1802014-06-13 02:37:02 +00001822
1823 m_extended_info.reset();
1824 m_extended_info_fetched = false;
Jim Ingham87c11912010-08-12 02:14:28 +00001825}
1826
Jason Molendab57e4a12013-11-04 09:33:30 +00001827lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001828Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1829{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001830 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001831}
1832
Jim Ingham44137582012-09-12 00:40:39 +00001833Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001834Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001835{
Jason Molendab57e4a12013-11-04 09:33:30 +00001836 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001837 Error return_error;
1838
1839 if (!frame_sp)
1840 {
Daniel Malead01b2952012-11-29 21:49:15 +00001841 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001842 }
1843
Jim Ingham4f465cf2012-10-10 18:32:14 +00001844 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001845}
1846
1847Error
Jason Molendab57e4a12013-11-04 09:33:30 +00001848Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001849{
1850 Error return_error;
1851
1852 if (!frame_sp)
1853 {
1854 return_error.SetErrorString("Can't return to a null frame.");
1855 return return_error;
1856 }
1857
1858 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001859 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
Jason Molendab57e4a12013-11-04 09:33:30 +00001860 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001861 if (!older_frame_sp)
1862 {
1863 return_error.SetErrorString("No older frame to return to.");
1864 return return_error;
1865 }
Jim Ingham44137582012-09-12 00:40:39 +00001866
1867 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001868 {
Jim Ingham44137582012-09-12 00:40:39 +00001869 lldb::ABISP abi = thread->GetProcess()->GetABI();
1870 if (!abi)
1871 {
1872 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001873 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001874 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001875 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1876
1877 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1878 // Turn that back on when that works.
Jason Molenda97e704b2014-10-16 08:07:20 +00001879 if (/* DISABLES CODE */ (0) && sc.function != NULL)
Jim Ingham1f51e602012-09-27 01:15:29 +00001880 {
1881 Type *function_type = sc.function->GetType();
1882 if (function_type)
1883 {
Greg Clayton99558cc42015-08-24 23:46:31 +00001884 CompilerType return_type = sc.function->GetCompilerType().GetFunctionReturnType();
Jim Ingham1f51e602012-09-27 01:15:29 +00001885 if (return_type)
1886 {
Jim Ingham1f51e602012-09-27 01:15:29 +00001887 StreamString s;
Greg Clayton57ee3062013-07-11 22:46:58 +00001888 return_type.DumpTypeDescription(&s);
1889 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham1f51e602012-09-27 01:15:29 +00001890 if (cast_value_sp)
1891 {
1892 cast_value_sp->SetFormat(eFormatHex);
1893 return_value_sp = cast_value_sp;
1894 }
1895 }
1896 }
1897 }
1898
Jim Inghamcb640dd2012-09-14 02:14:15 +00001899 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001900 if (!return_error.Success())
1901 return return_error;
1902 }
1903
1904 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001905 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001906 // cook their data
1907
Jason Molendab57e4a12013-11-04 09:33:30 +00001908 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
Jim Ingham93208b82013-01-31 21:46:01 +00001909 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001910 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001911 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1912 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001913 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001914 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1915 if (copy_success)
1916 {
1917 thread->DiscardThreadPlans(true);
1918 thread->ClearStackFrames();
1919 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1920 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1921 }
1922 else
1923 {
1924 return_error.SetErrorString("Could not reset register values.");
1925 }
Jim Ingham93208b82013-01-31 21:46:01 +00001926 }
1927 else
1928 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001929 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001930 }
Jim Ingham44137582012-09-12 00:40:39 +00001931 }
1932 else
1933 {
Jim Ingham93208b82013-01-31 21:46:01 +00001934 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001935 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001936 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001937}
1938
Richard Mittonf86248d2013-09-12 02:20:34 +00001939static void DumpAddressList (Stream &s, const std::vector<Address> &list, ExecutionContextScope *exe_scope)
1940{
1941 for (size_t n=0;n<list.size();n++)
1942 {
1943 s << "\t";
1944 list[n].Dump (&s, exe_scope, Address::DumpStyleResolvedDescription, Address::DumpStyleSectionNameOffset);
1945 s << "\n";
1946 }
1947}
1948
1949Error
1950Thread::JumpToLine (const FileSpec &file, uint32_t line, bool can_leave_function, std::string *warnings)
1951{
1952 ExecutionContext exe_ctx (GetStackFrameAtIndex(0));
1953 Target *target = exe_ctx.GetTargetPtr();
1954 TargetSP target_sp = exe_ctx.GetTargetSP();
1955 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
Jason Molendab57e4a12013-11-04 09:33:30 +00001956 StackFrame *frame = exe_ctx.GetFramePtr();
Richard Mittonf86248d2013-09-12 02:20:34 +00001957 const SymbolContext &sc = frame->GetSymbolContext(eSymbolContextFunction);
1958
1959 // Find candidate locations.
1960 std::vector<Address> candidates, within_function, outside_function;
1961 target->GetImages().FindAddressesForLine (target_sp, file, line, sc.function, within_function, outside_function);
1962
1963 // If possible, we try and stay within the current function.
1964 // Within a function, we accept multiple locations (optimized code may do this,
1965 // there's no solution here so we do the best we can).
1966 // However if we're trying to leave the function, we don't know how to pick the
1967 // right location, so if there's more than one then we bail.
1968 if (!within_function.empty())
1969 candidates = within_function;
1970 else if (outside_function.size() == 1 && can_leave_function)
1971 candidates = outside_function;
1972
1973 // Check if we got anything.
1974 if (candidates.empty())
1975 {
1976 if (outside_function.empty())
1977 {
1978 return Error("Cannot locate an address for %s:%i.",
1979 file.GetFilename().AsCString(), line);
1980 }
1981 else if (outside_function.size() == 1)
1982 {
1983 return Error("%s:%i is outside the current function.",
1984 file.GetFilename().AsCString(), line);
1985 }
1986 else
1987 {
1988 StreamString sstr;
1989 DumpAddressList(sstr, outside_function, target);
1990 return Error("%s:%i has multiple candidate locations:\n%s",
1991 file.GetFilename().AsCString(), line, sstr.GetString().c_str());
1992 }
1993 }
1994
1995 // Accept the first location, warn about any others.
1996 Address dest = candidates[0];
1997 if (warnings && candidates.size() > 1)
1998 {
1999 StreamString sstr;
2000 sstr.Printf("%s:%i appears multiple times in this function, selecting the first location:\n",
2001 file.GetFilename().AsCString(), line);
2002 DumpAddressList(sstr, candidates, target);
2003 *warnings = sstr.GetString();
2004 }
2005
2006 if (!reg_ctx->SetPC (dest))
2007 return Error("Cannot change PC to target address.");
2008
2009 return Error();
2010}
2011
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002012void
Greg Clayton0603aa92010-10-04 01:05:56 +00002013Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002014{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002015 ExecutionContext exe_ctx (shared_from_this());
2016 Process *process = exe_ctx.GetProcessPtr();
2017 if (process == NULL)
2018 return;
2019
Jason Molendab57e4a12013-11-04 09:33:30 +00002020 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00002021 SymbolContext frame_sc;
Jim Inghamd1f25362015-01-28 01:17:26 +00002022 if (frame_idx != LLDB_INVALID_FRAME_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002023 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002024 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002025 if (frame_sp)
2026 {
Greg Claytonc14ee322011-09-22 04:58:26 +00002027 exe_ctx.SetFrameSP(frame_sp);
2028 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002029 }
2030 }
2031
Greg Clayton554f68d2015-02-04 22:00:53 +00002032 const FormatEntity::Entry *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00002033 assert (thread_format);
Greg Clayton554f68d2015-02-04 22:00:53 +00002034
2035 FormatEntity::Format(*thread_format,
2036 strm,
2037 frame_sp ? &frame_sc : NULL,
2038 &exe_ctx,
2039 NULL,
2040 NULL,
2041 false,
2042 false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002043}
2044
Greg Clayton99d0faf2010-11-18 23:32:35 +00002045void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002046Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00002047{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002048}
Jim Inghamee8aea12010-09-08 03:14:33 +00002049
Greg Clayton99d0faf2010-11-18 23:32:35 +00002050void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002051Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002052{
Greg Clayton99d0faf2010-11-18 23:32:35 +00002053}
Jim Inghamee8aea12010-09-08 03:14:33 +00002054
Richard Mitton0a558352013-10-17 21:14:00 +00002055lldb::addr_t
2056Thread::GetThreadPointer ()
2057{
2058 return LLDB_INVALID_ADDRESS;
2059}
2060
2061addr_t
2062Thread::GetThreadLocalData (const ModuleSP module)
2063{
2064 // The default implementation is to ask the dynamic loader for it.
2065 // This can be overridden for specific platforms.
2066 DynamicLoader *loader = GetProcess()->GetDynamicLoader();
2067 if (loader)
2068 return loader->GetThreadLocalData (module, shared_from_this());
2069 else
2070 return LLDB_INVALID_ADDRESS;
2071}
2072
Jason Molendab4892cd2014-05-13 22:02:48 +00002073bool
2074Thread::SafeToCallFunctions ()
2075{
2076 Process *process = GetProcess().get();
2077 if (process)
2078 {
2079 SystemRuntime *runtime = process->GetSystemRuntime ();
2080 if (runtime)
2081 {
2082 return runtime->SafeToCallFunctionsOnThisThread (shared_from_this());
2083 }
2084 }
2085 return true;
2086}
2087
Jason Molendab57e4a12013-11-04 09:33:30 +00002088lldb::StackFrameSP
2089Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
Jim Inghame4284b72010-09-23 17:40:12 +00002090{
Jason Molendab57e4a12013-11-04 09:33:30 +00002091 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00002092}
Caroline Ticeceb6b132010-10-26 03:11:13 +00002093
2094const char *
2095Thread::StopReasonAsCString (lldb::StopReason reason)
2096{
2097 switch (reason)
2098 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00002099 case eStopReasonInvalid: return "invalid";
2100 case eStopReasonNone: return "none";
2101 case eStopReasonTrace: return "trace";
2102 case eStopReasonBreakpoint: return "breakpoint";
2103 case eStopReasonWatchpoint: return "watchpoint";
2104 case eStopReasonSignal: return "signal";
2105 case eStopReasonException: return "exception";
2106 case eStopReasonExec: return "exec";
2107 case eStopReasonPlanComplete: return "plan complete";
2108 case eStopReasonThreadExiting: return "thread exiting";
Kuba Breckaafdf8422014-10-10 23:43:03 +00002109 case eStopReasonInstrumentation: return "instrumentation break";
Caroline Ticeceb6b132010-10-26 03:11:13 +00002110 }
2111
Caroline Ticeceb6b132010-10-26 03:11:13 +00002112 static char unknown_state_string[64];
2113 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
2114 return unknown_state_string;
2115}
2116
2117const char *
2118Thread::RunModeAsCString (lldb::RunMode mode)
2119{
2120 switch (mode)
2121 {
2122 case eOnlyThisThread: return "only this thread";
2123 case eAllThreads: return "all threads";
2124 case eOnlyDuringStepping: return "only during stepping";
2125 }
2126
2127 static char unknown_state_string[64];
2128 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
2129 return unknown_state_string;
2130}
Jim Ingham06e827c2010-11-11 19:26:09 +00002131
Greg Clayton7260f622011-04-18 08:33:37 +00002132size_t
2133Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
2134{
Greg Clayton1ac04c32012-02-21 00:09:25 +00002135 ExecutionContext exe_ctx (shared_from_this());
2136 Target *target = exe_ctx.GetTargetPtr();
2137 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00002138 size_t num_frames_shown = 0;
2139 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00002140 bool is_selected = false;
2141 if (process)
2142 {
2143 if (process->GetThreadList().GetSelectedThread().get() == this)
2144 is_selected = true;
2145 }
2146 strm.Printf("%c ", is_selected ? '*' : ' ');
2147 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00002148 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002149 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00002150 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00002151 {
Jim Inghame610d642011-08-16 00:07:28 +00002152 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
2153 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
2154 {
2155 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
2156 }
Greg Clayton5113dc82011-08-12 06:47:54 +00002157 }
Greg Clayton7260f622011-04-18 08:33:37 +00002158 }
2159
2160 DumpUsingSettingsFormat (strm, start_frame);
2161
2162 if (num_frames > 0)
2163 {
2164 strm.IndentMore();
2165
2166 const bool show_frame_info = true;
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002167
2168 const char *selected_frame_marker = NULL;
2169 if (num_frames == 1 || (GetID() != GetProcess()->GetThreadList().GetSelectedThread()->GetID()))
2170 strm.IndentMore ();
2171 else
2172 selected_frame_marker = "* ";
2173
Greg Clayton39d0ab32012-03-29 01:41:38 +00002174 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
2175 start_frame,
2176 num_frames,
2177 show_frame_info,
Jim Ingham8ec10ef2013-10-18 17:38:31 +00002178 num_frames_with_source,
2179 selected_frame_marker);
2180 if (num_frames == 1)
2181 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00002182 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00002183 }
2184 return num_frames_shown;
2185}
2186
Jason Molenda705b1802014-06-13 02:37:02 +00002187bool
Kuba Breckaafdf8422014-10-10 23:43:03 +00002188Thread::GetDescription (Stream &strm, lldb::DescriptionLevel level, bool print_json_thread, bool print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002189{
2190 DumpUsingSettingsFormat (strm, 0);
2191 strm.Printf("\n");
2192
2193 StructuredData::ObjectSP thread_info = GetExtendedInfo();
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002194
Kuba Breckaafdf8422014-10-10 23:43:03 +00002195 if (print_json_thread || print_json_stopinfo)
Jason Molenda705b1802014-06-13 02:37:02 +00002196 {
Kuba Breckaafdf8422014-10-10 23:43:03 +00002197 if (thread_info && print_json_thread)
2198 {
2199 thread_info->Dump (strm);
2200 strm.Printf("\n");
2201 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002202
2203 if (print_json_stopinfo && m_stop_info_sp)
Kuba Breckaafdf8422014-10-10 23:43:03 +00002204 {
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002205 StructuredData::ObjectSP stop_info = m_stop_info_sp->GetExtendedInfo();
2206 if (stop_info)
2207 {
2208 stop_info->Dump (strm);
2209 strm.Printf("\n");
2210 }
Kuba Breckaafdf8422014-10-10 23:43:03 +00002211 }
Chaoren Lin7c4f6c42015-04-08 21:19:12 +00002212
Jason Molenda705b1802014-06-13 02:37:02 +00002213 return true;
2214 }
2215
2216 if (thread_info)
2217 {
2218 StructuredData::ObjectSP activity = thread_info->GetObjectForDotSeparatedPath("activity");
2219 StructuredData::ObjectSP breadcrumb = thread_info->GetObjectForDotSeparatedPath("breadcrumb");
2220 StructuredData::ObjectSP messages = thread_info->GetObjectForDotSeparatedPath("trace_messages");
2221
2222 bool printed_activity = false;
2223 if (activity && activity->GetType() == StructuredData::Type::eTypeDictionary)
2224 {
2225 StructuredData::Dictionary *activity_dict = activity->GetAsDictionary();
2226 StructuredData::ObjectSP id = activity_dict->GetValueForKey("id");
2227 StructuredData::ObjectSP name = activity_dict->GetValueForKey("name");
2228 if (name && name->GetType() == StructuredData::Type::eTypeString
2229 && id && id->GetType() == StructuredData::Type::eTypeInteger)
2230 {
2231 strm.Printf(" Activity '%s', 0x%" PRIx64 "\n", name->GetAsString()->GetValue().c_str(), id->GetAsInteger()->GetValue());
2232 }
2233 printed_activity = true;
2234 }
2235 bool printed_breadcrumb = false;
2236 if (breadcrumb && breadcrumb->GetType() == StructuredData::Type::eTypeDictionary)
2237 {
2238 if (printed_activity)
2239 strm.Printf ("\n");
2240 StructuredData::Dictionary *breadcrumb_dict = breadcrumb->GetAsDictionary();
2241 StructuredData::ObjectSP breadcrumb_text = breadcrumb_dict->GetValueForKey ("name");
2242 if (breadcrumb_text && breadcrumb_text->GetType() == StructuredData::Type::eTypeString)
2243 {
2244 strm.Printf (" Current Breadcrumb: %s\n", breadcrumb_text->GetAsString()->GetValue().c_str());
2245 }
2246 printed_breadcrumb = true;
2247 }
2248 if (messages && messages->GetType() == StructuredData::Type::eTypeArray)
2249 {
2250 if (printed_breadcrumb)
2251 strm.Printf("\n");
2252 StructuredData::Array *messages_array = messages->GetAsArray();
2253 const size_t msg_count = messages_array->GetSize();
2254 if (msg_count > 0)
2255 {
2256 strm.Printf (" %zu trace messages:\n", msg_count);
2257 for (size_t i = 0; i < msg_count; i++)
2258 {
2259 StructuredData::ObjectSP message = messages_array->GetItemAtIndex(i);
2260 if (message && message->GetType() == StructuredData::Type::eTypeDictionary)
2261 {
2262 StructuredData::Dictionary *message_dict = message->GetAsDictionary();
2263 StructuredData::ObjectSP message_text = message_dict->GetValueForKey ("message");
2264 if (message_text && message_text->GetType() == StructuredData::Type::eTypeString)
2265 {
2266 strm.Printf (" %s\n", message_text->GetAsString()->GetValue().c_str());
2267 }
2268 }
2269 }
2270 }
2271 }
2272 }
2273
2274 return true;
2275}
2276
Greg Clayton7260f622011-04-18 08:33:37 +00002277size_t
2278Thread::GetStackFrameStatus (Stream& strm,
2279 uint32_t first_frame,
2280 uint32_t num_frames,
2281 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002282 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00002283{
Greg Clayton39d0ab32012-03-29 01:41:38 +00002284 return GetStackFrameList()->GetStatus (strm,
2285 first_frame,
2286 num_frames,
2287 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00002288 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00002289}
2290
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002291Unwind *
2292Thread::GetUnwinder ()
2293{
2294 if (m_unwinder_ap.get() == NULL)
2295 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00002296 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002297 const llvm::Triple::ArchType machine = target_arch.GetMachine();
2298 switch (machine)
2299 {
2300 case llvm::Triple::x86_64:
2301 case llvm::Triple::x86:
2302 case llvm::Triple::arm:
Todd Fialad8eaa172014-07-23 14:37:35 +00002303 case llvm::Triple::aarch64:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002304 case llvm::Triple::thumb:
Bhushan D. Attarde794a4d52015-05-15 06:53:30 +00002305 case llvm::Triple::mips:
2306 case llvm::Triple::mipsel:
Ed Masteb73f8442013-10-10 00:59:47 +00002307 case llvm::Triple::mips64:
Mohit K. Bhakkade8659b52015-04-23 06:36:20 +00002308 case llvm::Triple::mips64el:
Justin Hibbits6256a0e2014-10-31 02:34:28 +00002309 case llvm::Triple::ppc:
Justin Hibbitsdb39cdf2014-10-31 15:57:52 +00002310 case llvm::Triple::ppc64:
Deepak Panickal6d3df422014-02-19 11:16:46 +00002311 case llvm::Triple::hexagon:
Greg Clayton56d9a1b2011-08-22 02:49:39 +00002312 m_unwinder_ap.reset (new UnwindLLDB (*this));
2313 break;
2314
2315 default:
2316 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
2317 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
2318 break;
2319 }
2320 }
2321 return m_unwinder_ap.get();
2322}
2323
Greg Claytonfa559e52012-05-18 02:38:05 +00002324void
2325Thread::Flush ()
2326{
2327 ClearStackFrames ();
2328 m_reg_context_sp.reset();
2329}
Jim Ingham5d88a062012-10-16 00:09:33 +00002330
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00002331bool
Jim Ingham5d88a062012-10-16 00:09:33 +00002332Thread::IsStillAtLastBreakpointHit ()
2333{
2334 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
2335 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
2336 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002337 if (m_stop_info_sp) {
2338 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00002339 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00002340 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00002341 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
2342 if (reg_ctx_sp)
2343 {
2344 lldb::addr_t pc = reg_ctx_sp->GetPC();
2345 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002346 if (bp_site_sp &&
2347 static_cast<break_id_t>(value) == bp_site_sp->GetID())
Greg Clayton1afa68e2013-04-02 20:32:37 +00002348 return true;
2349 }
Jim Ingham5d88a062012-10-16 00:09:33 +00002350 }
2351 }
2352 return false;
2353}
Greg Clayton44d93782014-01-27 23:43:24 +00002354
Greg Clayton44d93782014-01-27 23:43:24 +00002355Error
2356Thread::StepIn (bool source_step,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002357 LazyBool step_in_avoids_code_without_debug_info,
2358 LazyBool step_out_avoids_code_without_debug_info)
2359
Greg Clayton44d93782014-01-27 23:43:24 +00002360{
2361 Error error;
2362 Process *process = GetProcess().get();
2363 if (StateIsStoppedState (process->GetState(), true))
2364 {
2365 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2366 ThreadPlanSP new_plan_sp;
2367 const lldb::RunMode run_mode = eOnlyThisThread;
2368 const bool abort_other_plans = false;
2369
2370 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2371 {
2372 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2373 new_plan_sp = QueueThreadPlanForStepInRange (abort_other_plans,
2374 sc.line_entry.range,
2375 sc,
2376 NULL,
2377 run_mode,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002378 step_in_avoids_code_without_debug_info,
2379 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002380 }
2381 else
2382 {
2383 new_plan_sp = QueueThreadPlanForStepSingleInstruction (false,
2384 abort_other_plans,
2385 run_mode);
2386 }
2387
2388 new_plan_sp->SetIsMasterPlan(true);
2389 new_plan_sp->SetOkayToDiscard(false);
2390
2391 // Why do we need to set the current thread by ID here???
2392 process->GetThreadList().SetSelectedThreadByID (GetID());
2393 error = process->Resume();
2394 }
2395 else
2396 {
2397 error.SetErrorString("process not stopped");
2398 }
2399 return error;
2400}
2401
2402Error
Jim Ingham4b4b2472014-03-13 02:47:14 +00002403Thread::StepOver (bool source_step,
2404 LazyBool step_out_avoids_code_without_debug_info)
Greg Clayton44d93782014-01-27 23:43:24 +00002405{
2406 Error error;
2407 Process *process = GetProcess().get();
2408 if (StateIsStoppedState (process->GetState(), true))
2409 {
2410 StackFrameSP frame_sp = GetStackFrameAtIndex (0);
2411 ThreadPlanSP new_plan_sp;
2412
2413 const lldb::RunMode run_mode = eOnlyThisThread;
2414 const bool abort_other_plans = false;
2415
2416 if (source_step && frame_sp && frame_sp->HasDebugInformation ())
2417 {
2418 SymbolContext sc(frame_sp->GetSymbolContext(eSymbolContextEverything));
2419 new_plan_sp = QueueThreadPlanForStepOverRange (abort_other_plans,
2420 sc.line_entry.range,
2421 sc,
Jim Ingham4b4b2472014-03-13 02:47:14 +00002422 run_mode,
2423 step_out_avoids_code_without_debug_info);
Greg Clayton44d93782014-01-27 23:43:24 +00002424 }
2425 else
2426 {
2427 new_plan_sp = QueueThreadPlanForStepSingleInstruction (true,
2428 abort_other_plans,
2429 run_mode);
2430 }
2431
2432 new_plan_sp->SetIsMasterPlan(true);
2433 new_plan_sp->SetOkayToDiscard(false);
2434
2435 // Why do we need to set the current thread by ID here???
2436 process->GetThreadList().SetSelectedThreadByID (GetID());
2437 error = process->Resume();
2438 }
2439 else
2440 {
2441 error.SetErrorString("process not stopped");
2442 }
2443 return error;
2444}
2445
2446Error
2447Thread::StepOut ()
2448{
2449 Error error;
2450 Process *process = GetProcess().get();
2451 if (StateIsStoppedState (process->GetState(), true))
2452 {
2453 const bool first_instruction = false;
2454 const bool stop_other_threads = false;
2455 const bool abort_other_plans = false;
2456
2457 ThreadPlanSP new_plan_sp(QueueThreadPlanForStepOut (abort_other_plans,
2458 NULL,
2459 first_instruction,
2460 stop_other_threads,
2461 eVoteYes,
2462 eVoteNoOpinion,
2463 0));
2464
2465 new_plan_sp->SetIsMasterPlan(true);
2466 new_plan_sp->SetOkayToDiscard(false);
2467
2468 // Why do we need to set the current thread by ID here???
2469 process->GetThreadList().SetSelectedThreadByID (GetID());
2470 error = process->Resume();
2471 }
2472 else
2473 {
2474 error.SetErrorString("process not stopped");
2475 }
2476 return error;
Jim Ingham4b4b2472014-03-13 02:47:14 +00002477}