blob: 401eac2b27ae202918322311ff34a7ef84f8e3c6 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/lldb-private-log.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000013#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000014#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Log.h"
Greg Clayton160c9d82013-05-01 21:54:04 +000016#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Stream.h"
18#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000019#include "lldb/Core/RegularExpression.h"
Greg Clayton7260f622011-04-18 08:33:37 +000020#include "lldb/Host/Host.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000021#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Target/DynamicLoader.h"
23#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000024#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Target/Process.h"
26#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000027#include "lldb/Target/StopInfo.h"
Greg Clayton896dff62010-07-23 16:45:51 +000028#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/Thread.h"
30#include "lldb/Target/ThreadPlan.h"
31#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Target/ThreadPlanBase.h"
33#include "lldb/Target/ThreadPlanStepInstruction.h"
34#include "lldb/Target/ThreadPlanStepOut.h"
35#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
36#include "lldb/Target/ThreadPlanStepThrough.h"
37#include "lldb/Target/ThreadPlanStepInRange.h"
38#include "lldb/Target/ThreadPlanStepOverRange.h"
39#include "lldb/Target/ThreadPlanRunToAddress.h"
40#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000041#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000042#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000043#include "Plugins/Process/Utility/UnwindLLDB.h"
44#include "UnwindMacOSXFrameBackchain.h"
45
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
47using namespace lldb;
48using namespace lldb_private;
49
Greg Clayton67cc0632012-08-22 17:17:09 +000050
51const ThreadPropertiesSP &
52Thread::GetGlobalProperties()
53{
54 static ThreadPropertiesSP g_settings_sp;
55 if (!g_settings_sp)
56 g_settings_sp.reset (new ThreadProperties (true));
57 return g_settings_sp;
58}
59
60static PropertyDefinition
61g_properties[] =
62{
63 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
64 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
65 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
66};
67
68enum {
69 ePropertyStepAvoidRegex,
70 ePropertyEnableThreadTrace
71};
72
73
74class ThreadOptionValueProperties : public OptionValueProperties
75{
76public:
77 ThreadOptionValueProperties (const ConstString &name) :
78 OptionValueProperties (name)
79 {
80 }
81
82 // This constructor is used when creating ThreadOptionValueProperties when it
83 // is part of a new lldb_private::Thread instance. It will copy all current
84 // global property values as needed
85 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +000086 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +000087 {
88 }
89
90 virtual const Property *
91 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
92 {
93 // When gettings the value for a key from the thread options, we will always
94 // try and grab the setting from the current thread if there is one. Else we just
95 // use the one from this instance.
96 if (exe_ctx)
97 {
98 Thread *thread = exe_ctx->GetThreadPtr();
99 if (thread)
100 {
101 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
102 if (this != instance_properties)
103 return instance_properties->ProtectedGetPropertyAtIndex (idx);
104 }
105 }
106 return ProtectedGetPropertyAtIndex (idx);
107 }
108};
109
110
111
112ThreadProperties::ThreadProperties (bool is_global) :
113 Properties ()
114{
115 if (is_global)
116 {
117 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
118 m_collection_sp->Initialize(g_properties);
119 }
120 else
121 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
122}
123
124ThreadProperties::~ThreadProperties()
125{
126}
127
128const RegularExpression *
129ThreadProperties::GetSymbolsToAvoidRegexp()
130{
131 const uint32_t idx = ePropertyStepAvoidRegex;
132 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
133}
134
135bool
136ThreadProperties::GetTraceEnabledState() const
137{
138 const uint32_t idx = ePropertyEnableThreadTrace;
139 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
140}
141
Jim Ingham4f465cf2012-10-10 18:32:14 +0000142//------------------------------------------------------------------
143// Thread Event Data
144//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000145
Jim Ingham4f465cf2012-10-10 18:32:14 +0000146
147const ConstString &
148Thread::ThreadEventData::GetFlavorString ()
149{
150 static ConstString g_flavor ("Thread::ThreadEventData");
151 return g_flavor;
152}
153
154Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
155 m_thread_sp (thread_sp),
156 m_stack_id ()
157{
158}
159
160Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
161 m_thread_sp (thread_sp),
162 m_stack_id (stack_id)
163{
164}
165
166Thread::ThreadEventData::ThreadEventData () :
167 m_thread_sp (),
168 m_stack_id ()
169{
170}
171
172Thread::ThreadEventData::~ThreadEventData ()
173{
174}
175
176void
177Thread::ThreadEventData::Dump (Stream *s) const
178{
179
180}
181
182const Thread::ThreadEventData *
183Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
184{
185 if (event_ptr)
186 {
187 const EventData *event_data = event_ptr->GetData();
188 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
189 return static_cast <const ThreadEventData *> (event_ptr->GetData());
190 }
191 return NULL;
192}
193
194ThreadSP
195Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
196{
197 ThreadSP thread_sp;
198 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
199 if (event_data)
200 thread_sp = event_data->GetThread();
201 return thread_sp;
202}
203
204StackID
205Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
206{
207 StackID stack_id;
208 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
209 if (event_data)
210 stack_id = event_data->GetStackID();
211 return stack_id;
212}
213
214StackFrameSP
215Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
216{
217 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
218 StackFrameSP frame_sp;
219 if (event_data)
220 {
221 ThreadSP thread_sp = event_data->GetThread();
222 if (thread_sp)
223 {
224 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
225 }
226 }
227 return frame_sp;
228}
229
230//------------------------------------------------------------------
231// Thread class
232//------------------------------------------------------------------
233
234ConstString &
235Thread::GetStaticBroadcasterClass ()
236{
237 static ConstString class_name ("lldb.thread");
238 return class_name;
239}
240
241Thread::Thread (Process &process, lldb::tid_t tid) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000242 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000244 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
245 m_process_wp (process.shared_from_this()),
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000246 m_stop_info_sp (),
247 m_stop_info_stop_id (0),
Han Ming Ongc2c423e2013-01-08 22:10:01 +0000248 m_index_id (process.GetNextThreadIndexID(tid)),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000251 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000254 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000255 m_curr_frames_sp (),
256 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000258 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000259 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000260 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000261 m_destroy_called (false),
Jim Ingham221d51c2013-05-08 00:35:16 +0000262 m_override_should_notify (eLazyBoolCalculate)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263{
Greg Clayton5160ce52013-03-27 23:08:40 +0000264 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000266 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267
Jim Ingham4f465cf2012-10-10 18:32:14 +0000268 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269 QueueFundamentalPlan(true);
270}
271
272
273Thread::~Thread()
274{
Greg Clayton5160ce52013-03-27 23:08:40 +0000275 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000277 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000278 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
279 assert (m_destroy_called);
280}
281
282void
283Thread::DestroyThread ()
284{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000285 // Tell any plans on the plan stack that the thread is being destroyed since
286 // any active plans that have a thread go away in the middle of might need
287 // to do cleanup.
288 for (auto plan : m_plan_stack)
289 plan->ThreadDestroyed();
290
Greg Clayton35a4cc52012-10-29 20:52:08 +0000291 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000292 m_plan_stack.clear();
293 m_discarded_plan_stack.clear();
294 m_completed_plan_stack.clear();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000295 m_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000296 m_reg_context_sp.reset();
297 m_unwinder_ap.reset();
298 Mutex::Locker locker(m_frame_mutex);
299 m_curr_frames_sp.reset();
300 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301}
302
Jim Ingham4f465cf2012-10-10 18:32:14 +0000303void
304Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
305{
306 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
307 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
308}
309
310uint32_t
311Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
312{
313 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
314 if (broadcast)
315 BroadcastSelectedFrameChange(frame->GetStackID());
316 return ret_value;
317}
318
319bool
320Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
321{
322 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
323 if (frame_sp)
324 {
325 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
326 if (broadcast)
327 BroadcastSelectedFrameChange(frame_sp->GetStackID());
328 return true;
329 }
330 else
331 return false;
332}
333
Jim Ingham93208b82013-01-31 21:46:01 +0000334bool
335Thread::SetSelectedFrameByIndexNoisily (uint32_t frame_idx, Stream &output_stream)
336{
337 const bool broadcast = true;
338 bool success = SetSelectedFrameByIndex (frame_idx, broadcast);
339 if (success)
340 {
341 StackFrameSP frame_sp = GetSelectedFrame();
342 if (frame_sp)
343 {
344 bool already_shown = false;
345 SymbolContext frame_sc(frame_sp->GetSymbolContext(eSymbolContextLineEntry));
346 if (GetProcess()->GetTarget().GetDebugger().GetUseExternalEditor() && frame_sc.line_entry.file && frame_sc.line_entry.line != 0)
347 {
348 already_shown = Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
349 }
350
351 bool show_frame_info = true;
352 bool show_source = !already_shown;
353 return frame_sp->GetStatus (output_stream, show_frame_info, show_source);
354 }
355 return false;
356 }
357 else
358 return false;
359}
360
Jim Ingham4f465cf2012-10-10 18:32:14 +0000361
Jim Inghamb15bfc72010-10-20 00:39:53 +0000362lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000363Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364{
Jim Inghamb15bfc72010-10-20 00:39:53 +0000365 ThreadPlanSP plan_sp (GetCompletedPlan());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000366 ProcessSP process_sp (GetProcess());
367 const uint32_t stop_id = process_sp ? process_sp->GetStopID() : UINT32_MAX;
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000368 if (plan_sp && plan_sp->PlanSucceeded())
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000369 {
Jim Ingham73ca05a2011-12-17 01:35:57 +0000370 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000371 }
Jim Inghamb15bfc72010-10-20 00:39:53 +0000372 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000373 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000374 if ((m_stop_info_stop_id == stop_id) || // Stop info is valid, just return what we have (even if empty)
375 (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 +0000376 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000377 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000378 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000379 else
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000380 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000381 GetPrivateStopInfo ();
382 return m_stop_info_sp;
Andrew Kaylorba4e61d2013-05-07 18:35:34 +0000383 }
Jim Ingham79c35da2011-08-09 00:32:52 +0000384 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385}
386
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000387lldb::StopInfoSP
388Thread::GetPrivateStopInfo ()
389{
390 ProcessSP process_sp (GetProcess());
391 if (process_sp)
392 {
Daniel Malea246cb612013-05-14 15:20:12 +0000393 const uint32_t process_stop_id = process_sp->GetStopID();
394 if (m_stop_info_stop_id != process_stop_id)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000395 {
Daniel Malea246cb612013-05-14 15:20:12 +0000396 if (m_stop_info_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000397 {
Daniel Malea246cb612013-05-14 15:20:12 +0000398 if (m_stop_info_sp->IsValid()
399 || IsStillAtLastBreakpointHit()
400 || GetCurrentPlan()->IsVirtualStep())
401 SetStopInfo (m_stop_info_sp);
402 else
403 m_stop_info_sp.reset();
404 }
405
406 if (!m_stop_info_sp)
407 {
408 if (CalculateStopInfo() == false)
409 SetStopInfo (StopInfoSP());
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000410 }
411 }
412 }
413 return m_stop_info_sp;
414}
415
416
Greg Clayton97d5cf02012-09-25 02:40:06 +0000417lldb::StopReason
418Thread::GetStopReason()
419{
420 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
421 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000422 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000423 return eStopReasonNone;
424}
425
426
427
Jim Ingham77787032011-01-20 02:03:18 +0000428void
429Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
430{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000431 m_stop_info_sp = stop_info_sp;
432 if (m_stop_info_sp)
Jim Ingham221d51c2013-05-08 00:35:16 +0000433 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000434 m_stop_info_sp->MakeStopInfoValid();
Jim Ingham221d51c2013-05-08 00:35:16 +0000435 // If we are overriding the ShouldReportStop, do that here:
436 if (m_override_should_notify != eLazyBoolCalculate)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000437 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000438 }
439
Greg Clayton1ac04c32012-02-21 00:09:25 +0000440 ProcessSP process_sp (GetProcess());
441 if (process_sp)
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000442 m_stop_info_stop_id = process_sp->GetStopID();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000443 else
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000444 m_stop_info_stop_id = UINT32_MAX;
Greg Clayton8cda7f02013-05-21 21:55:59 +0000445 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
446 if (log)
Matt Kopecef143712013-06-03 18:00:07 +0000447 log->Printf("%p: tid = 0x%" PRIx64 ": stop info = %s (stop_id = %u)\n", this, GetID(), stop_info_sp ? stop_info_sp->GetDescription() : "<NULL>", m_stop_info_stop_id);
Jim Ingham77787032011-01-20 02:03:18 +0000448}
449
450void
Jim Ingham221d51c2013-05-08 00:35:16 +0000451Thread::SetShouldReportStop (Vote vote)
452{
453 if (vote == eVoteNoOpinion)
454 return;
455 else
456 {
457 m_override_should_notify = (vote == eVoteYes ? eLazyBoolYes : eLazyBoolNo);
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000458 if (m_stop_info_sp)
459 m_stop_info_sp->OverrideShouldNotify (m_override_should_notify == eLazyBoolYes);
Jim Ingham221d51c2013-05-08 00:35:16 +0000460 }
461}
462
463void
Jim Ingham77787032011-01-20 02:03:18 +0000464Thread::SetStopInfoToNothing()
465{
466 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
467 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
468 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
469}
470
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471bool
472Thread::ThreadStoppedForAReason (void)
473{
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000474 return (bool) GetPrivateStopInfo ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475}
476
Jim Ingham77787032011-01-20 02:03:18 +0000477bool
478Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
479{
480 if (!SaveFrameZeroState(saved_state.register_backup))
481 return false;
482
483 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000484 ProcessSP process_sp (GetProcess());
485 if (process_sp)
486 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000487 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
488
Jim Ingham77787032011-01-20 02:03:18 +0000489 return true;
490}
491
492bool
Jim Ingham8559a352012-11-26 23:52:18 +0000493Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000494{
495 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham8559a352012-11-26 23:52:18 +0000496 return true;
497}
498
499bool
500Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
501{
Jim Ingham0c270682011-01-25 02:47:23 +0000502 if (saved_state.stop_info_sp)
503 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000504 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000505 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000506 return true;
507}
508
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509StateType
510Thread::GetState() const
511{
512 // If any other threads access this we will need a mutex for it
513 Mutex::Locker locker(m_state_mutex);
514 return m_state;
515}
516
517void
518Thread::SetState(StateType state)
519{
520 Mutex::Locker locker(m_state_mutex);
521 m_state = state;
522}
523
524void
525Thread::WillStop()
526{
527 ThreadPlan *current_plan = GetCurrentPlan();
528
529 // FIXME: I may decide to disallow threads with no plans. In which
530 // case this should go to an assert.
531
532 if (!current_plan)
533 return;
534
535 current_plan->WillStop();
536}
537
538void
539Thread::SetupForResume ()
540{
541 if (GetResumeState() != eStateSuspended)
542 {
543
544 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
545 // telling the current plan it will resume, since we might change what the current
546 // plan is.
547
Greg Clayton1afa68e2013-04-02 20:32:37 +0000548// StopReason stop_reason = lldb::eStopReasonInvalid;
549// StopInfoSP stop_info_sp = GetStopInfo();
550// if (stop_info_sp.get())
551// stop_reason = stop_info_sp->GetStopReason();
552// if (stop_reason == lldb::eStopReasonBreakpoint)
553 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
554 if (reg_ctx_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000556 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(reg_ctx_sp->GetPC());
557 if (bp_site_sp)
Jim Inghamb01e7422010-06-19 04:45:32 +0000558 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000559 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
560 // special to step over a breakpoint.
561
562 ThreadPlan *cur_plan = GetCurrentPlan();
Jim Inghamb01e7422010-06-19 04:45:32 +0000563
Greg Clayton1afa68e2013-04-02 20:32:37 +0000564 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
565 {
566 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
567 if (step_bp_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568 {
Greg Clayton1afa68e2013-04-02 20:32:37 +0000569 ThreadPlanSP step_bp_plan_sp;
570 step_bp_plan->SetPrivate (true);
571
572 if (GetCurrentPlan()->RunState() != eStateStepping)
573 {
574 step_bp_plan->SetAutoContinue(true);
575 }
576 step_bp_plan_sp.reset (step_bp_plan);
577 QueueThreadPlan (step_bp_plan_sp, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000578 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579 }
580 }
581 }
582 }
583}
584
585bool
Greg Clayton160c9d82013-05-01 21:54:04 +0000586Thread::ShouldResume (StateType resume_state)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587{
588 // At this point clear the completed plan stack.
589 m_completed_plan_stack.clear();
590 m_discarded_plan_stack.clear();
Jim Ingham221d51c2013-05-08 00:35:16 +0000591 m_override_should_notify = eLazyBoolCalculate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592
Greg Clayton97d5cf02012-09-25 02:40:06 +0000593 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000594
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000595 lldb::ThreadSP backing_thread_sp (GetBackingThread ());
596 if (backing_thread_sp)
597 backing_thread_sp->m_temporary_resume_state = resume_state;
598
599 // Make sure m_stop_info_sp is valid
600 GetPrivateStopInfo();
Greg Clayton160c9d82013-05-01 21:54:04 +0000601
Jim Ingham92087d82012-01-31 23:09:20 +0000602 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
603 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
604 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
605 // about the fact that we are resuming...
Greg Clayton1ac04c32012-02-21 00:09:25 +0000606 const uint32_t process_stop_id = GetProcess()->GetStopID();
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000607 if (m_stop_info_stop_id == process_stop_id &&
608 (m_stop_info_sp && m_stop_info_sp->IsValid()))
Jim Ingham92087d82012-01-31 23:09:20 +0000609 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000610 StopInfo *stop_info = GetPrivateStopInfo().get();
Jim Ingham92087d82012-01-31 23:09:20 +0000611 if (stop_info)
612 stop_info->WillResume (resume_state);
613 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614
615 // Tell all the plans that we are about to resume in case they need to clear any state.
616 // We distinguish between the plan on the top of the stack and the lower
617 // plans in case a plan needs to do any special business before it runs.
618
Greg Claytond1d06e42013-04-20 00:27:58 +0000619 bool need_to_resume = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 ThreadPlan *plan_ptr = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000621 if (plan_ptr)
622 {
623 need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624
Greg Claytond1d06e42013-04-20 00:27:58 +0000625 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
626 {
627 plan_ptr->WillResume (resume_state, false);
628 }
629
630 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
631 // In that case, don't reset it here.
632
633 if (need_to_resume && resume_state != eStateSuspended)
634 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000635 m_stop_info_sp.reset();
Greg Claytond1d06e42013-04-20 00:27:58 +0000636 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000637 }
638
Greg Clayton160c9d82013-05-01 21:54:04 +0000639 if (need_to_resume)
640 {
641 ClearStackFrames();
642 // Let Thread subclasses do any special work they need to prior to resuming
643 WillResume (resume_state);
644 }
645
Jim Ingham513c6bb2012-09-01 01:02:41 +0000646 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000647}
648
649void
650Thread::DidResume ()
651{
652 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
653}
654
Andrew Kaylor29d65742013-05-10 17:19:04 +0000655void
656Thread::DidStop ()
657{
658 SetState (eStateStopped);
659}
660
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661bool
662Thread::ShouldStop (Event* event_ptr)
663{
664 ThreadPlan *current_plan = GetCurrentPlan();
Greg Claytond1d06e42013-04-20 00:27:58 +0000665
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 bool should_stop = true;
667
Greg Clayton5160ce52013-03-27 23:08:40 +0000668 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham92087d82012-01-31 23:09:20 +0000669
670 if (GetResumeState () == eStateSuspended)
671 {
672 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000673 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham92087d82012-01-31 23:09:20 +0000674 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000675 GetID (),
676 GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000677 return false;
678 }
679
680 if (GetTemporaryResumeState () == eStateSuspended)
681 {
682 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000683 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham92087d82012-01-31 23:09:20 +0000684 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000685 GetID (),
686 GetProtocolID());
Jim Ingham92087d82012-01-31 23:09:20 +0000687 return false;
688 }
689
Daniel Malea246cb612013-05-14 15:20:12 +0000690 // Based on the current thread plan and process stop info, check if this
691 // thread caused the process to stop. NOTE: this must take place before
692 // the plan is moved from the current plan stack to the completed plan
693 // stack.
Jim Ingham92087d82012-01-31 23:09:20 +0000694 if (ThreadStoppedForAReason() == false)
695 {
696 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000697 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)",
Jim Ingham92087d82012-01-31 23:09:20 +0000698 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000699 GetID (),
700 GetProtocolID(),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000701 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham92087d82012-01-31 23:09:20 +0000702 return false;
703 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000704
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705 if (log)
706 {
Jim Inghamdee1bc92013-06-22 00:27:45 +0000707 log->Printf ("Thread::%s(%p) for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
708 __FUNCTION__,
709 this,
Greg Clayton160c9d82013-05-01 21:54:04 +0000710 GetID (),
711 GetProtocolID (),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000712 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000713 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000715 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000717 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 }
Jim Ingham06e827c2010-11-11 19:26:09 +0000719
720 // The top most plan always gets to do the trace log...
721 current_plan->DoTraceLog ();
Jim Ingham6d66ce62012-04-20 21:16:56 +0000722
723 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
724 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
725 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000726 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Ingham6d66ce62012-04-20 21:16:56 +0000727 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
728 {
729 if (log)
730 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
731 return false;
732 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733
Jim Inghambad39e42012-09-05 21:12:49 +0000734 // If we've already been restarted, don't query the plans since the state they would examine is not current.
735 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
736 return false;
737
738 // Before the plans see the state of the world, calculate the current inlined depth.
739 GetStackFrameList()->CalculateCurrentInlinedDepth();
740
Jim Ingham25f66702011-12-03 01:52:59 +0000741 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
742 // If that plan is still working, then we don't need to do any more work. If the plan that explains
743 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
744 // whether they still need to do more work.
745
746 bool done_processing_current_plan = false;
747
Jim Ingham0161b492013-02-09 01:29:05 +0000748 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000749 {
750 if (current_plan->TracerExplainsStop())
751 {
752 done_processing_current_plan = true;
753 should_stop = false;
754 }
755 else
756 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000757 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000758 // does and let it handle the situation.
759 ThreadPlan *plan_ptr = current_plan;
760 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
761 {
Jim Ingham0161b492013-02-09 01:29:05 +0000762 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000763 {
764 should_stop = plan_ptr->ShouldStop (event_ptr);
765
766 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
767 // and all the plans below it off the stack.
768
769 if (plan_ptr->MischiefManaged())
770 {
Jim Ingham031177e2012-05-11 23:49:49 +0000771 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000772 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Ingham25f66702011-12-03 01:52:59 +0000773
774 do
775 {
776 if (should_stop)
777 current_plan->WillStop();
778 PopPlan();
779 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000780 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
781 // Now, if the responsible plan was not "Okay to discard" then we're done,
782 // otherwise we forward this to the next plan in the stack below.
783 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
784 done_processing_current_plan = true;
785 else
786 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000787 }
788 else
789 done_processing_current_plan = true;
790
791 break;
792 }
793
794 }
795 }
796 }
797
798 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000800 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Ingham0f16e732011-02-08 05:20:59 +0000801
Jim Ingham10c4b242011-10-15 00:23:43 +0000802 if (log)
803 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
804
Jim Ingham0f16e732011-02-08 05:20:59 +0000805 // We're starting from the base plan, so just let it decide;
806 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000808 should_stop = current_plan->ShouldStop (event_ptr);
809 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000810 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000811 }
812 else
813 {
814 // Otherwise, don't let the base plan override what the other plans say to do, since
815 // presumably if there were other plans they would know what to do...
816 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000817 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000818 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819 break;
Jim Ingham0f16e732011-02-08 05:20:59 +0000820
821 should_stop = current_plan->ShouldStop(event_ptr);
822 if (log)
823 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
824 if (current_plan->MischiefManaged())
825 {
826 if (should_stop)
827 current_plan->WillStop();
828
829 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
830 // Otherwise, see if the plan's parent wants to stop.
831
832 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
833 {
834 PopPlan();
835 break;
836 }
837 else
838 {
839
840 PopPlan();
841
842 current_plan = GetCurrentPlan();
843 if (current_plan == NULL)
844 {
845 break;
846 }
847 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848 }
849 else
850 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000851 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000853 }
854 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000855
Jim Inghamb01e7422010-06-19 04:45:32 +0000856 if (over_ride_stop)
857 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000858
859 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
860 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
861 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
862 // This code clears stale plans off the stack.
863
864 if (should_stop)
865 {
866 ThreadPlan *plan_ptr = GetCurrentPlan();
867 while (!PlanIsBasePlan(plan_ptr))
868 {
869 bool stale = plan_ptr->IsPlanStale ();
870 ThreadPlan *examined_plan = plan_ptr;
871 plan_ptr = GetPreviousPlan (examined_plan);
872
873 if (stale)
874 {
875 if (log)
876 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
877 DiscardThreadPlansUpToPlan(examined_plan);
878 }
879 }
880 }
881
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000883
Jim Ingham10c4b242011-10-15 00:23:43 +0000884 if (log)
885 {
886 StreamString s;
887 s.IndentMore();
888 DumpThreadPlans(&s);
889 log->Printf ("Plan stack final state:\n%s", s.GetData());
890 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
891 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892 return should_stop;
893}
894
895Vote
896Thread::ShouldReportStop (Event* event_ptr)
897{
898 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +0000899 StateType temp_thread_state = GetTemporaryResumeState();
900
Greg Clayton5160ce52013-03-27 23:08:40 +0000901 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000902
903 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
904 {
905 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000906 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 +0000907 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000908 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000909
Jim Ingham92087d82012-01-31 23:09:20 +0000910 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
911 {
912 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000913 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 +0000914 return eVoteNoOpinion;
915 }
916
917 if (!ThreadStoppedForAReason())
918 {
919 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000920 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 +0000921 return eVoteNoOpinion;
922 }
923
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924 if (m_completed_plan_stack.size() > 0)
925 {
926 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +0000927 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000928 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 +0000929 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
930 }
931 else
Greg Clayton2cad65a2010-09-03 17:10:42 +0000932 {
Jim Ingham0161b492013-02-09 01:29:05 +0000933 Vote thread_vote = eVoteNoOpinion;
934 ThreadPlan *plan_ptr = GetCurrentPlan();
935 while (1)
936 {
937 if (plan_ptr->PlanExplainsStop(event_ptr))
938 {
939 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
940 break;
941 }
942 if (PlanIsBasePlan(plan_ptr))
943 break;
944 else
945 plan_ptr = GetPreviousPlan(plan_ptr);
946 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000947 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000948 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 +0000949
950 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000951 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952}
953
954Vote
955Thread::ShouldReportRun (Event* event_ptr)
956{
957 StateType thread_state = GetResumeState ();
Jim Ingham444586b2011-01-24 06:34:17 +0000958
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000959 if (thread_state == eStateSuspended
960 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +0000961 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +0000963 }
964
Greg Clayton5160ce52013-03-27 23:08:40 +0000965 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966 if (m_completed_plan_stack.size() > 0)
967 {
968 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +0000969 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +0000970 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
971 GetIndexID(),
972 this,
Jim Ingham444586b2011-01-24 06:34:17 +0000973 GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +0000974 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +0000975 m_completed_plan_stack.back()->GetName());
976
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
978 }
979 else
Jim Ingham444586b2011-01-24 06:34:17 +0000980 {
981 if (log)
Jim Inghamdee1bc92013-06-22 00:27:45 +0000982 log->Printf ("Current Plan for thread %d(%p) (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
983 GetIndexID(),
984 this,
Jim Ingham444586b2011-01-24 06:34:17 +0000985 GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +0000986 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +0000987 GetCurrentPlan()->GetName());
988
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +0000990 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000991}
992
Jim Ingham1b54c882010-06-16 02:00:15 +0000993bool
994Thread::MatchesSpec (const ThreadSpec *spec)
995{
996 if (spec == NULL)
997 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +0000998
Jim Ingham3d902922012-03-07 22:03:04 +0000999 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +00001000}
1001
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002void
1003Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1004{
1005 if (thread_plan_sp)
1006 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001007 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1008 if (!thread_plan_sp->GetThreadPlanTracer())
1009 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001010 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham06e827c2010-11-11 19:26:09 +00001011
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012 thread_plan_sp->DidPush();
1013
Greg Clayton5160ce52013-03-27 23:08:40 +00001014 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001015 if (log)
1016 {
1017 StreamString s;
1018 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Jim Inghamdee1bc92013-06-22 00:27:45 +00001019 log->Printf("Thread::PushPlan(0x%p): \"%s\", tid = 0x%4.4" PRIx64 ".",
1020 this,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021 s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001022 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001023 }
1024 }
1025}
1026
1027void
1028Thread::PopPlan ()
1029{
Greg Clayton5160ce52013-03-27 23:08:40 +00001030 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001031
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001032 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001033 return;
1034 else
1035 {
1036 ThreadPlanSP &plan = m_plan_stack.back();
1037 if (log)
1038 {
Daniel Malead01b2952012-11-29 21:49:15 +00001039 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001040 }
1041 m_completed_plan_stack.push_back (plan);
1042 plan->WillPop();
1043 m_plan_stack.pop_back();
1044 }
1045}
1046
1047void
1048Thread::DiscardPlan ()
1049{
Jim Inghamdee1bc92013-06-22 00:27:45 +00001050 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051 if (m_plan_stack.size() > 1)
1052 {
1053 ThreadPlanSP &plan = m_plan_stack.back();
Jim Inghamdee1bc92013-06-22 00:27:45 +00001054 if (log)
1055 log->Printf("Discarding plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
1056
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057 m_discarded_plan_stack.push_back (plan);
1058 plan->WillPop();
1059 m_plan_stack.pop_back();
1060 }
1061}
1062
1063ThreadPlan *
1064Thread::GetCurrentPlan ()
1065{
Jim Inghame1471232012-04-19 00:17:05 +00001066 // There will always be at least the base plan. If somebody is mucking with a
1067 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +00001068 if (m_plan_stack.empty())
1069 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +00001070 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001071}
1072
1073ThreadPlanSP
1074Thread::GetCompletedPlan ()
1075{
1076 ThreadPlanSP empty_plan_sp;
1077 if (!m_completed_plan_stack.empty())
1078 {
1079 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1080 {
1081 ThreadPlanSP completed_plan_sp;
1082 completed_plan_sp = m_completed_plan_stack[i];
1083 if (!completed_plan_sp->GetPrivate ())
1084 return completed_plan_sp;
1085 }
1086 }
1087 return empty_plan_sp;
1088}
1089
Jim Ingham73ca05a2011-12-17 01:35:57 +00001090ValueObjectSP
1091Thread::GetReturnValueObject ()
1092{
1093 if (!m_completed_plan_stack.empty())
1094 {
1095 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1096 {
1097 ValueObjectSP return_valobj_sp;
1098 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1099 if (return_valobj_sp)
1100 return return_valobj_sp;
1101 }
1102 }
1103 return ValueObjectSP();
1104}
1105
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106bool
1107Thread::IsThreadPlanDone (ThreadPlan *plan)
1108{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109 if (!m_completed_plan_stack.empty())
1110 {
1111 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1112 {
1113 if (m_completed_plan_stack[i].get() == plan)
1114 return true;
1115 }
1116 }
1117 return false;
1118}
1119
1120bool
1121Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1122{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123 if (!m_discarded_plan_stack.empty())
1124 {
1125 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1126 {
1127 if (m_discarded_plan_stack[i].get() == plan)
1128 return true;
1129 }
1130 }
1131 return false;
1132}
1133
1134ThreadPlan *
1135Thread::GetPreviousPlan (ThreadPlan *current_plan)
1136{
1137 if (current_plan == NULL)
1138 return NULL;
1139
1140 int stack_size = m_completed_plan_stack.size();
1141 for (int i = stack_size - 1; i > 0; i--)
1142 {
1143 if (current_plan == m_completed_plan_stack[i].get())
1144 return m_completed_plan_stack[i-1].get();
1145 }
1146
1147 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1148 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149 if (m_plan_stack.size() > 0)
1150 return m_plan_stack.back().get();
1151 else
1152 return NULL;
1153 }
1154
1155 stack_size = m_plan_stack.size();
1156 for (int i = stack_size - 1; i > 0; i--)
1157 {
1158 if (current_plan == m_plan_stack[i].get())
1159 return m_plan_stack[i-1].get();
1160 }
1161 return NULL;
1162}
1163
1164void
1165Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1166{
1167 if (abort_other_plans)
1168 DiscardThreadPlans(true);
1169
1170 PushPlan (thread_plan_sp);
1171}
1172
Jim Ingham06e827c2010-11-11 19:26:09 +00001173
1174void
1175Thread::EnableTracer (bool value, bool single_stepping)
1176{
1177 int stack_size = m_plan_stack.size();
1178 for (int i = 0; i < stack_size; i++)
1179 {
1180 if (m_plan_stack[i]->GetThreadPlanTracer())
1181 {
1182 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1183 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1184 }
1185 }
1186}
1187
1188void
1189Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1190{
1191 int stack_size = m_plan_stack.size();
1192 for (int i = 0; i < stack_size; i++)
1193 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1194}
1195
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001197Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1198{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001199 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1200}
1201
1202void
1203Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1204{
Greg Clayton5160ce52013-03-27 23:08:40 +00001205 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001206 if (log)
1207 {
Daniel Malead01b2952012-11-29 21:49:15 +00001208 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p", GetID(), up_to_plan_ptr);
Jim Ingham399f1ca2010-11-05 19:25:48 +00001209 }
1210
1211 int stack_size = m_plan_stack.size();
1212
1213 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1214 // stack, and if so discard up to and including it.
1215
Jim Ingham64e7ead2012-05-03 21:19:36 +00001216 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001217 {
1218 for (int i = stack_size - 1; i > 0; i--)
1219 DiscardPlan();
1220 }
1221 else
1222 {
1223 bool found_it = false;
1224 for (int i = stack_size - 1; i > 0; i--)
1225 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001226 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001227 found_it = true;
1228 }
1229 if (found_it)
1230 {
1231 bool last_one = false;
1232 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1233 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001234 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001235 last_one = true;
1236 DiscardPlan();
1237 }
1238 }
1239 }
1240 return;
1241}
1242
1243void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244Thread::DiscardThreadPlans(bool force)
1245{
Greg Clayton5160ce52013-03-27 23:08:40 +00001246 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001247 if (log)
1248 {
Daniel Malead01b2952012-11-29 21:49:15 +00001249 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250 }
1251
1252 if (force)
1253 {
1254 int stack_size = m_plan_stack.size();
1255 for (int i = stack_size - 1; i > 0; i--)
1256 {
1257 DiscardPlan();
1258 }
1259 return;
1260 }
1261
1262 while (1)
1263 {
1264
1265 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001266 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001267
1268 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1269 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1270 {
1271 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1272 {
1273 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1274 break;
1275 }
1276 }
1277
1278 if (discard)
1279 {
1280 // First pop all the dependent plans:
1281 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1282 {
1283
1284 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1285 // for the plan leaves it in a state that it is safe to pop the plan
1286 // with no more notice?
1287 DiscardPlan();
1288 }
1289
1290 // Now discard the master plan itself.
1291 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1292 // discard it's dependent plans, but not it...
1293 if (master_plan_idx > 0)
1294 {
1295 DiscardPlan();
1296 }
1297 }
1298 else
1299 {
1300 // If the master plan doesn't want to get discarded, then we're done.
1301 break;
1302 }
1303
1304 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001305}
1306
Jim Inghamcf274f92012-04-09 22:37:39 +00001307bool
1308Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1309{
1310 if (plan_ptr->IsBasePlan())
1311 return true;
1312 else if (m_plan_stack.size() == 0)
1313 return false;
1314 else
1315 return m_plan_stack[0].get() == plan_ptr;
1316}
1317
Jim Ingham93208b82013-01-31 21:46:01 +00001318Error
1319Thread::UnwindInnermostExpression()
1320{
1321 Error error;
1322 int stack_size = m_plan_stack.size();
1323
1324 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1325 // stack, and if so discard up to and including it.
1326
1327 for (int i = stack_size - 1; i > 0; i--)
1328 {
1329 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1330 {
1331 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1332 return error;
1333 }
1334 }
1335 error.SetErrorString("No expressions currently active on this thread");
1336 return error;
1337}
1338
1339
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001340ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001341Thread::QueueFundamentalPlan (bool abort_other_plans)
1342{
1343 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1344 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001345 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001346}
1347
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001348ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001349Thread::QueueThreadPlanForStepSingleInstruction
1350(
1351 bool step_over,
1352 bool abort_other_plans,
1353 bool stop_other_threads
1354)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001355{
1356 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1357 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001358 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001359}
1360
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001361ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001362Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001363(
1364 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001365 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001366 const SymbolContext &addr_context,
1367 lldb::RunMode stop_other_threads
1368)
1369{
1370 ThreadPlanSP thread_plan_sp;
1371 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1372
1373 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001374 return thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001375}
1376
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001377ThreadPlanSP
Jim Inghamc6276822012-12-12 19:58:40 +00001378Thread::QueueThreadPlanForStepInRange
1379(
1380 bool abort_other_plans,
1381 const AddressRange &range,
1382 const SymbolContext &addr_context,
1383 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001384 lldb::RunMode stop_other_threads,
1385 bool avoid_code_without_debug_info
1386)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001387{
1388 ThreadPlanSP thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001389 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1390 if (avoid_code_without_debug_info)
1391 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001392 else
Jim Inghamc6276822012-12-12 19:58:40 +00001393 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1394 if (step_in_target)
1395 plan->SetStepInTarget(step_in_target);
1396 thread_plan_sp.reset (plan);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397
1398 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001399 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001400}
1401
1402
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001403ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001404Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1405{
1406 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1407 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001408 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001409}
1410
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001411ThreadPlanSP
Greg Clayton481cef22011-01-21 06:11:58 +00001412Thread::QueueThreadPlanForStepOut
1413(
1414 bool abort_other_plans,
1415 SymbolContext *addr_context,
1416 bool first_insn,
1417 bool stop_other_threads,
1418 Vote stop_vote,
1419 Vote run_vote,
1420 uint32_t frame_idx
1421)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001422{
Greg Clayton481cef22011-01-21 06:11:58 +00001423 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1424 addr_context,
1425 first_insn,
1426 stop_other_threads,
1427 stop_vote,
1428 run_vote,
1429 frame_idx));
Sean Callanan708709c2012-07-31 22:19:25 +00001430
1431 if (thread_plan_sp->ValidatePlan(NULL))
1432 {
1433 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001434 return thread_plan_sp;
Sean Callanan708709c2012-07-31 22:19:25 +00001435 }
1436 else
1437 {
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001438 return ThreadPlanSP();
Sean Callanan708709c2012-07-31 22:19:25 +00001439 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001440}
1441
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001442ThreadPlanSP
Jim Ingham18de2fd2012-05-10 01:35:39 +00001443Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001444{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001445 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001446 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001447 return ThreadPlanSP();
Jim Ingham25f66702011-12-03 01:52:59 +00001448
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001449 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001450 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451}
1452
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001453ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001454Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1455 Address& function,
1456 lldb::addr_t arg,
1457 bool stop_other_threads,
Jim Ingham184e9812013-01-15 02:47:48 +00001458 bool unwind_on_error,
1459 bool ignore_breakpoints)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001460{
Jim Ingham184e9812013-01-15 02:47:48 +00001461 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this,
1462 function,
1463 ClangASTType(),
1464 arg,
1465 stop_other_threads,
1466 unwind_on_error,
1467 ignore_breakpoints));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001468 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001469 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001470}
1471
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001472ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001473Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1474 Address &target_addr,
1475 bool stop_other_threads)
1476{
1477 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1478 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001479 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480}
1481
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001482ThreadPlanSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001483Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001484 lldb::addr_t *address_list,
1485 size_t num_addresses,
1486 bool stop_other_threads,
1487 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001488{
Greg Clayton481cef22011-01-21 06:11:58 +00001489 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001490 QueueThreadPlan (thread_plan_sp, abort_other_plans);
Jim Ingham4d56e9c2013-07-18 21:48:26 +00001491 return thread_plan_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001492
1493}
1494
1495uint32_t
1496Thread::GetIndexID () const
1497{
1498 return m_index_id;
1499}
1500
1501void
1502Thread::DumpThreadPlans (lldb_private::Stream *s) const
1503{
1504 uint32_t stack_size = m_plan_stack.size();
Greg Clayton1346f7e2010-09-03 22:45:01 +00001505 int i;
Jim Ingham10c4b242011-10-15 00:23:43 +00001506 s->Indent();
Daniel Malead01b2952012-11-29 21:49:15 +00001507 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4" PRIx64 ", stack_size = %d\n", GetIndexID(), GetID(), stack_size);
Greg Clayton1346f7e2010-09-03 22:45:01 +00001508 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001509 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001510 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001511 s->Indent();
1512 s->Printf ("Element %d: ", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001513 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001515 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001516 }
1517
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001518 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001519 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001520 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001521 s->Indent();
1522 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1523 for (i = stack_size - 1; i >= 0; i--)
1524 {
1525 s->IndentMore();
1526 s->Indent();
1527 s->Printf ("Element %d: ", i);
1528 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1529 s->EOL();
1530 s->IndentLess();
1531 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001532 }
1533
1534 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001535 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001536 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001537 s->Indent();
1538 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1539 for (i = stack_size - 1; i >= 0; i--)
1540 {
1541 s->IndentMore();
1542 s->Indent();
1543 s->Printf ("Element %d: ", i);
1544 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1545 s->EOL();
1546 s->IndentLess();
1547 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001548 }
1549
1550}
1551
Greg Claytond9e416c2012-02-18 05:35:26 +00001552TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001553Thread::CalculateTarget ()
1554{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001555 TargetSP target_sp;
1556 ProcessSP process_sp(GetProcess());
1557 if (process_sp)
1558 target_sp = process_sp->CalculateTarget();
1559 return target_sp;
1560
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001561}
1562
Greg Claytond9e416c2012-02-18 05:35:26 +00001563ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001564Thread::CalculateProcess ()
1565{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001566 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001567}
1568
Greg Claytond9e416c2012-02-18 05:35:26 +00001569ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001570Thread::CalculateThread ()
1571{
Greg Claytond9e416c2012-02-18 05:35:26 +00001572 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001573}
1574
Greg Claytond9e416c2012-02-18 05:35:26 +00001575StackFrameSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001576Thread::CalculateStackFrame ()
1577{
Greg Claytond9e416c2012-02-18 05:35:26 +00001578 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579}
1580
1581void
Greg Clayton0603aa92010-10-04 01:05:56 +00001582Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001583{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001584 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001585}
1586
Greg Clayton12daf9462010-08-25 00:35:26 +00001587
Greg Clayton39d0ab32012-03-29 01:41:38 +00001588StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001589Thread::GetStackFrameList ()
1590{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001591 StackFrameListSP frame_list_sp;
1592 Mutex::Locker locker(m_frame_mutex);
1593 if (m_curr_frames_sp)
1594 {
1595 frame_list_sp = m_curr_frames_sp;
1596 }
1597 else
1598 {
1599 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1600 m_curr_frames_sp = frame_list_sp;
1601 }
1602 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001603}
1604
Greg Clayton12daf9462010-08-25 00:35:26 +00001605void
1606Thread::ClearStackFrames ()
1607{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001608 Mutex::Locker locker(m_frame_mutex);
1609
Greg Clayton160c9d82013-05-01 21:54:04 +00001610 Unwind *unwinder = GetUnwinder ();
1611 if (unwinder)
1612 unwinder->Clear();
1613
Jim Inghamb0c72a52012-02-29 03:40:22 +00001614 // Only store away the old "reference" StackFrameList if we got all its frames:
1615 // FIXME: At some point we can try to splice in the frames we have fetched into
1616 // the new frame as we make it, but let's not try that now.
1617 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001618 m_prev_frames_sp.swap (m_curr_frames_sp);
1619 m_curr_frames_sp.reset();
Jim Ingham87c11912010-08-12 02:14:28 +00001620}
1621
1622lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001623Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1624{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001625 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001626}
1627
Jim Ingham44137582012-09-12 00:40:39 +00001628
1629Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001630Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001631{
1632 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1633 Error return_error;
1634
1635 if (!frame_sp)
1636 {
Daniel Malead01b2952012-11-29 21:49:15 +00001637 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001638 }
1639
Jim Ingham4f465cf2012-10-10 18:32:14 +00001640 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001641}
1642
1643Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001644Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001645{
1646 Error return_error;
1647
1648 if (!frame_sp)
1649 {
1650 return_error.SetErrorString("Can't return to a null frame.");
1651 return return_error;
1652 }
1653
1654 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001655 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1656 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001657 if (!older_frame_sp)
1658 {
1659 return_error.SetErrorString("No older frame to return to.");
1660 return return_error;
1661 }
Jim Ingham44137582012-09-12 00:40:39 +00001662
1663 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001664 {
Jim Ingham44137582012-09-12 00:40:39 +00001665 lldb::ABISP abi = thread->GetProcess()->GetABI();
1666 if (!abi)
1667 {
1668 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001669 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001670 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001671 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1672
1673 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1674 // Turn that back on when that works.
1675 if (0 && sc.function != NULL)
1676 {
1677 Type *function_type = sc.function->GetType();
1678 if (function_type)
1679 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001680 ClangASTType return_type = sc.function->GetClangType().GetFunctionReturnType();
Jim Ingham1f51e602012-09-27 01:15:29 +00001681 if (return_type)
1682 {
Jim Ingham1f51e602012-09-27 01:15:29 +00001683 StreamString s;
Greg Clayton57ee3062013-07-11 22:46:58 +00001684 return_type.DumpTypeDescription(&s);
1685 ValueObjectSP cast_value_sp = return_value_sp->Cast(return_type);
Jim Ingham1f51e602012-09-27 01:15:29 +00001686 if (cast_value_sp)
1687 {
1688 cast_value_sp->SetFormat(eFormatHex);
1689 return_value_sp = cast_value_sp;
1690 }
1691 }
1692 }
1693 }
1694
Jim Inghamcb640dd2012-09-14 02:14:15 +00001695 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001696 if (!return_error.Success())
1697 return return_error;
1698 }
1699
1700 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001701 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001702 // cook their data
1703
1704 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
1705 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001706 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001707 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1708 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001709 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001710 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1711 if (copy_success)
1712 {
1713 thread->DiscardThreadPlans(true);
1714 thread->ClearStackFrames();
1715 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1716 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1717 }
1718 else
1719 {
1720 return_error.SetErrorString("Could not reset register values.");
1721 }
Jim Ingham93208b82013-01-31 21:46:01 +00001722 }
1723 else
1724 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001725 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001726 }
Jim Ingham44137582012-09-12 00:40:39 +00001727 }
1728 else
1729 {
Jim Ingham93208b82013-01-31 21:46:01 +00001730 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001731 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001732 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001733}
1734
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001735void
Greg Clayton0603aa92010-10-04 01:05:56 +00001736Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001737{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001738 ExecutionContext exe_ctx (shared_from_this());
1739 Process *process = exe_ctx.GetProcessPtr();
1740 if (process == NULL)
1741 return;
1742
Greg Claytonc14ee322011-09-22 04:58:26 +00001743 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00001744 SymbolContext frame_sc;
Greg Clayton0603aa92010-10-04 01:05:56 +00001745 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001746 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001747 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001748 if (frame_sp)
1749 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001750 exe_ctx.SetFrameSP(frame_sp);
1751 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752 }
1753 }
1754
Greg Clayton1ac04c32012-02-21 00:09:25 +00001755 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00001756 assert (thread_format);
Greg Clayton0603aa92010-10-04 01:05:56 +00001757 Debugger::FormatPrompt (thread_format,
Greg Claytonc14ee322011-09-22 04:58:26 +00001758 frame_sp ? &frame_sc : NULL,
Greg Clayton0603aa92010-10-04 01:05:56 +00001759 &exe_ctx,
1760 NULL,
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001761 strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001762}
1763
Greg Clayton99d0faf2010-11-18 23:32:35 +00001764void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001765Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00001766{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001767}
Jim Inghamee8aea12010-09-08 03:14:33 +00001768
Greg Clayton99d0faf2010-11-18 23:32:35 +00001769void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001770Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001771{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001772}
Jim Inghamee8aea12010-09-08 03:14:33 +00001773
Jim Inghame4284b72010-09-23 17:40:12 +00001774lldb::StackFrameSP
1775Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1776{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001777 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00001778}
Caroline Ticeceb6b132010-10-26 03:11:13 +00001779
1780const char *
1781Thread::StopReasonAsCString (lldb::StopReason reason)
1782{
1783 switch (reason)
1784 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00001785 case eStopReasonInvalid: return "invalid";
1786 case eStopReasonNone: return "none";
1787 case eStopReasonTrace: return "trace";
1788 case eStopReasonBreakpoint: return "breakpoint";
1789 case eStopReasonWatchpoint: return "watchpoint";
1790 case eStopReasonSignal: return "signal";
1791 case eStopReasonException: return "exception";
1792 case eStopReasonExec: return "exec";
1793 case eStopReasonPlanComplete: return "plan complete";
1794 case eStopReasonThreadExiting: return "thread exiting";
Caroline Ticeceb6b132010-10-26 03:11:13 +00001795 }
1796
1797
1798 static char unknown_state_string[64];
1799 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1800 return unknown_state_string;
1801}
1802
1803const char *
1804Thread::RunModeAsCString (lldb::RunMode mode)
1805{
1806 switch (mode)
1807 {
1808 case eOnlyThisThread: return "only this thread";
1809 case eAllThreads: return "all threads";
1810 case eOnlyDuringStepping: return "only during stepping";
1811 }
1812
1813 static char unknown_state_string[64];
1814 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1815 return unknown_state_string;
1816}
Jim Ingham06e827c2010-11-11 19:26:09 +00001817
Greg Clayton7260f622011-04-18 08:33:37 +00001818size_t
1819Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1820{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001821 ExecutionContext exe_ctx (shared_from_this());
1822 Target *target = exe_ctx.GetTargetPtr();
1823 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00001824 size_t num_frames_shown = 0;
1825 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00001826 bool is_selected = false;
1827 if (process)
1828 {
1829 if (process->GetThreadList().GetSelectedThread().get() == this)
1830 is_selected = true;
1831 }
1832 strm.Printf("%c ", is_selected ? '*' : ' ');
1833 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00001834 {
Greg Clayton5113dc82011-08-12 06:47:54 +00001835 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00001836 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00001837 {
Jim Inghame610d642011-08-16 00:07:28 +00001838 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1839 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1840 {
1841 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1842 }
Greg Clayton5113dc82011-08-12 06:47:54 +00001843 }
Greg Clayton7260f622011-04-18 08:33:37 +00001844 }
1845
1846 DumpUsingSettingsFormat (strm, start_frame);
1847
1848 if (num_frames > 0)
1849 {
1850 strm.IndentMore();
1851
1852 const bool show_frame_info = true;
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001853 strm.IndentMore ();
Greg Clayton39d0ab32012-03-29 01:41:38 +00001854 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1855 start_frame,
1856 num_frames,
1857 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001858 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001859 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001860 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00001861 }
1862 return num_frames_shown;
1863}
1864
1865size_t
1866Thread::GetStackFrameStatus (Stream& strm,
1867 uint32_t first_frame,
1868 uint32_t num_frames,
1869 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001870 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00001871{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001872 return GetStackFrameList()->GetStatus (strm,
1873 first_frame,
1874 num_frames,
1875 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001876 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001877}
1878
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001879bool
1880Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1881{
1882 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1883 if (frame_sp)
1884 {
1885 checkpoint.SetStackID(frame_sp->GetStackID());
Greg Clayton1afa68e2013-04-02 20:32:37 +00001886 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
1887 if (reg_ctx_sp)
1888 return reg_ctx_sp->ReadAllRegisterValues (checkpoint.GetData());
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001889 }
1890 return false;
1891}
Greg Clayton7260f622011-04-18 08:33:37 +00001892
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001893bool
1894Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1895{
Jim Ingham44137582012-09-12 00:40:39 +00001896 return ResetFrameZeroRegisters (checkpoint.GetData());
1897}
1898
1899bool
1900Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1901{
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001902 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1903 if (frame_sp)
1904 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001905 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
1906 if (reg_ctx_sp)
1907 {
1908 bool ret = reg_ctx_sp->WriteAllRegisterValues (register_data_sp);
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001909
Greg Clayton1afa68e2013-04-02 20:32:37 +00001910 // Clear out all stack frames as our world just changed.
1911 ClearStackFrames();
1912 reg_ctx_sp->InvalidateIfNeeded(true);
1913 if (m_unwinder_ap.get())
1914 m_unwinder_ap->Clear();
1915 return ret;
1916 }
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001917 }
1918 return false;
1919}
Greg Clayton7260f622011-04-18 08:33:37 +00001920
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001921Unwind *
1922Thread::GetUnwinder ()
1923{
1924 if (m_unwinder_ap.get() == NULL)
1925 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00001926 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001927 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1928 switch (machine)
1929 {
1930 case llvm::Triple::x86_64:
1931 case llvm::Triple::x86:
1932 case llvm::Triple::arm:
1933 case llvm::Triple::thumb:
1934 m_unwinder_ap.reset (new UnwindLLDB (*this));
1935 break;
1936
1937 default:
1938 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1939 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1940 break;
1941 }
1942 }
1943 return m_unwinder_ap.get();
1944}
1945
1946
Greg Claytonfa559e52012-05-18 02:38:05 +00001947void
1948Thread::Flush ()
1949{
1950 ClearStackFrames ();
1951 m_reg_context_sp.reset();
1952}
Jim Ingham5d88a062012-10-16 00:09:33 +00001953
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00001954bool
Jim Ingham5d88a062012-10-16 00:09:33 +00001955Thread::IsStillAtLastBreakpointHit ()
1956{
1957 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
1958 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
1959 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00001960 if (m_stop_info_sp) {
1961 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00001962 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00001963 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00001964 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
1965 if (reg_ctx_sp)
1966 {
1967 lldb::addr_t pc = reg_ctx_sp->GetPC();
1968 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1969 if (bp_site_sp && value == bp_site_sp->GetID())
1970 return true;
1971 }
Jim Ingham5d88a062012-10-16 00:09:33 +00001972 }
1973 }
1974 return false;
1975}