blob: 0527cb169f8d7777f96b3cafefb3b8eee656152d [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 {
Greg Clayton160c9d82013-05-01 21:54:04 +0000707 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 " 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
Jim Ingham92087d82012-01-31 23:09:20 +0000708 __FUNCTION__,
Greg Clayton160c9d82013-05-01 21:54:04 +0000709 GetID (),
710 GetProtocolID (),
Greg Clayton1afa68e2013-04-02 20:32:37 +0000711 GetRegisterContext() ? GetRegisterContext()->GetPC() : LLDB_INVALID_ADDRESS);
Jim Ingham10c4b242011-10-15 00:23:43 +0000712 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000714 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000716 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 }
Jim Ingham06e827c2010-11-11 19:26:09 +0000718
719 // The top most plan always gets to do the trace log...
720 current_plan->DoTraceLog ();
Jim Ingham6d66ce62012-04-20 21:16:56 +0000721
722 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
723 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
724 // do any more work on this stop.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000725 StopInfoSP private_stop_info (GetPrivateStopInfo());
Jim Ingham6d66ce62012-04-20 21:16:56 +0000726 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
727 {
728 if (log)
729 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
730 return false;
731 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732
Jim Inghambad39e42012-09-05 21:12:49 +0000733 // If we've already been restarted, don't query the plans since the state they would examine is not current.
734 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
735 return false;
736
737 // Before the plans see the state of the world, calculate the current inlined depth.
738 GetStackFrameList()->CalculateCurrentInlinedDepth();
739
Jim Ingham25f66702011-12-03 01:52:59 +0000740 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
741 // If that plan is still working, then we don't need to do any more work. If the plan that explains
742 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
743 // whether they still need to do more work.
744
745 bool done_processing_current_plan = false;
746
Jim Ingham0161b492013-02-09 01:29:05 +0000747 if (!current_plan->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000748 {
749 if (current_plan->TracerExplainsStop())
750 {
751 done_processing_current_plan = true;
752 should_stop = false;
753 }
754 else
755 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000756 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000757 // does and let it handle the situation.
758 ThreadPlan *plan_ptr = current_plan;
759 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
760 {
Jim Ingham0161b492013-02-09 01:29:05 +0000761 if (plan_ptr->PlanExplainsStop(event_ptr))
Jim Ingham25f66702011-12-03 01:52:59 +0000762 {
763 should_stop = plan_ptr->ShouldStop (event_ptr);
764
765 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
766 // and all the plans below it off the stack.
767
768 if (plan_ptr->MischiefManaged())
769 {
Jim Ingham031177e2012-05-11 23:49:49 +0000770 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000771 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Ingham25f66702011-12-03 01:52:59 +0000772
773 do
774 {
775 if (should_stop)
776 current_plan->WillStop();
777 PopPlan();
778 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000779 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
780 // Now, if the responsible plan was not "Okay to discard" then we're done,
781 // otherwise we forward this to the next plan in the stack below.
782 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
783 done_processing_current_plan = true;
784 else
785 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000786 }
787 else
788 done_processing_current_plan = true;
789
790 break;
791 }
792
793 }
794 }
795 }
796
797 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000799 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Ingham0f16e732011-02-08 05:20:59 +0000800
Jim Ingham10c4b242011-10-15 00:23:43 +0000801 if (log)
802 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
803
Jim Ingham0f16e732011-02-08 05:20:59 +0000804 // We're starting from the base plan, so just let it decide;
805 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000807 should_stop = current_plan->ShouldStop (event_ptr);
808 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000809 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000810 }
811 else
812 {
813 // Otherwise, don't let the base plan override what the other plans say to do, since
814 // presumably if there were other plans they would know what to do...
815 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000817 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818 break;
Jim Ingham0f16e732011-02-08 05:20:59 +0000819
820 should_stop = current_plan->ShouldStop(event_ptr);
821 if (log)
822 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
823 if (current_plan->MischiefManaged())
824 {
825 if (should_stop)
826 current_plan->WillStop();
827
828 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
829 // Otherwise, see if the plan's parent wants to stop.
830
831 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
832 {
833 PopPlan();
834 break;
835 }
836 else
837 {
838
839 PopPlan();
840
841 current_plan = GetCurrentPlan();
842 if (current_plan == NULL)
843 {
844 break;
845 }
846 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847 }
848 else
849 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000850 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000851 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852 }
853 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000854
Jim Inghamb01e7422010-06-19 04:45:32 +0000855 if (over_ride_stop)
856 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000857
858 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
859 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
860 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
861 // This code clears stale plans off the stack.
862
863 if (should_stop)
864 {
865 ThreadPlan *plan_ptr = GetCurrentPlan();
866 while (!PlanIsBasePlan(plan_ptr))
867 {
868 bool stale = plan_ptr->IsPlanStale ();
869 ThreadPlan *examined_plan = plan_ptr;
870 plan_ptr = GetPreviousPlan (examined_plan);
871
872 if (stale)
873 {
874 if (log)
875 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
876 DiscardThreadPlansUpToPlan(examined_plan);
877 }
878 }
879 }
880
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000882
Jim Ingham10c4b242011-10-15 00:23:43 +0000883 if (log)
884 {
885 StreamString s;
886 s.IndentMore();
887 DumpThreadPlans(&s);
888 log->Printf ("Plan stack final state:\n%s", s.GetData());
889 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
890 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891 return should_stop;
892}
893
894Vote
895Thread::ShouldReportStop (Event* event_ptr)
896{
897 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +0000898 StateType temp_thread_state = GetTemporaryResumeState();
899
Greg Clayton5160ce52013-03-27 23:08:40 +0000900 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000901
902 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
903 {
904 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000905 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 +0000906 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000907 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908
Jim Ingham92087d82012-01-31 23:09:20 +0000909 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
910 {
911 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000912 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 +0000913 return eVoteNoOpinion;
914 }
915
916 if (!ThreadStoppedForAReason())
917 {
918 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000919 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 +0000920 return eVoteNoOpinion;
921 }
922
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000923 if (m_completed_plan_stack.size() > 0)
924 {
925 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +0000926 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000927 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 +0000928 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
929 }
930 else
Greg Clayton2cad65a2010-09-03 17:10:42 +0000931 {
Jim Ingham0161b492013-02-09 01:29:05 +0000932 Vote thread_vote = eVoteNoOpinion;
933 ThreadPlan *plan_ptr = GetCurrentPlan();
934 while (1)
935 {
936 if (plan_ptr->PlanExplainsStop(event_ptr))
937 {
938 thread_vote = plan_ptr->ShouldReportStop(event_ptr);
939 break;
940 }
941 if (PlanIsBasePlan(plan_ptr))
942 break;
943 else
944 plan_ptr = GetPreviousPlan(plan_ptr);
945 }
Greg Clayton2cad65a2010-09-03 17:10:42 +0000946 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000947 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 +0000948
949 return thread_vote;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000950 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951}
952
953Vote
954Thread::ShouldReportRun (Event* event_ptr)
955{
956 StateType thread_state = GetResumeState ();
Jim Ingham444586b2011-01-24 06:34:17 +0000957
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000958 if (thread_state == eStateSuspended
959 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +0000960 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000961 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +0000962 }
963
Greg Clayton5160ce52013-03-27 23:08:40 +0000964 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965 if (m_completed_plan_stack.size() > 0)
966 {
967 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +0000968 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000969 log->Printf ("Current Plan for thread %d (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
Jim Ingham444586b2011-01-24 06:34:17 +0000970 GetIndexID(),
971 GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +0000972 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +0000973 m_completed_plan_stack.back()->GetName());
974
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000975 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
976 }
977 else
Jim Ingham444586b2011-01-24 06:34:17 +0000978 {
979 if (log)
Greg Clayton160c9d82013-05-01 21:54:04 +0000980 log->Printf ("Current Plan for thread %d (0x%4.4" PRIx64 ", %s): %s being asked whether we should report run.",
Jim Ingham444586b2011-01-24 06:34:17 +0000981 GetIndexID(),
982 GetID(),
Greg Clayton160c9d82013-05-01 21:54:04 +0000983 StateAsCString(GetTemporaryResumeState()),
Jim Ingham444586b2011-01-24 06:34:17 +0000984 GetCurrentPlan()->GetName());
985
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000986 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +0000987 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988}
989
Jim Ingham1b54c882010-06-16 02:00:15 +0000990bool
991Thread::MatchesSpec (const ThreadSpec *spec)
992{
993 if (spec == NULL)
994 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +0000995
Jim Ingham3d902922012-03-07 22:03:04 +0000996 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +0000997}
998
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000999void
1000Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
1001{
1002 if (thread_plan_sp)
1003 {
Jim Ingham06e827c2010-11-11 19:26:09 +00001004 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
1005 if (!thread_plan_sp->GetThreadPlanTracer())
1006 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +00001007 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham06e827c2010-11-11 19:26:09 +00001008
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001009 thread_plan_sp->DidPush();
1010
Greg Clayton5160ce52013-03-27 23:08:40 +00001011 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012 if (log)
1013 {
1014 StreamString s;
1015 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Daniel Malead01b2952012-11-29 21:49:15 +00001016 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017 s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +00001018 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019 }
1020 }
1021}
1022
1023void
1024Thread::PopPlan ()
1025{
Greg Clayton5160ce52013-03-27 23:08:40 +00001026 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027
Jim Ingham4fc6cb92012-08-22 21:34:33 +00001028 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029 return;
1030 else
1031 {
1032 ThreadPlanSP &plan = m_plan_stack.back();
1033 if (log)
1034 {
Daniel Malead01b2952012-11-29 21:49:15 +00001035 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001036 }
1037 m_completed_plan_stack.push_back (plan);
1038 plan->WillPop();
1039 m_plan_stack.pop_back();
1040 }
1041}
1042
1043void
1044Thread::DiscardPlan ()
1045{
1046 if (m_plan_stack.size() > 1)
1047 {
1048 ThreadPlanSP &plan = m_plan_stack.back();
1049 m_discarded_plan_stack.push_back (plan);
1050 plan->WillPop();
1051 m_plan_stack.pop_back();
1052 }
1053}
1054
1055ThreadPlan *
1056Thread::GetCurrentPlan ()
1057{
Jim Inghame1471232012-04-19 00:17:05 +00001058 // There will always be at least the base plan. If somebody is mucking with a
1059 // thread with an empty plan stack, we should assert right away.
Greg Claytond1d06e42013-04-20 00:27:58 +00001060 if (m_plan_stack.empty())
1061 return NULL;
Jim Inghame1471232012-04-19 00:17:05 +00001062 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001063}
1064
1065ThreadPlanSP
1066Thread::GetCompletedPlan ()
1067{
1068 ThreadPlanSP empty_plan_sp;
1069 if (!m_completed_plan_stack.empty())
1070 {
1071 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1072 {
1073 ThreadPlanSP completed_plan_sp;
1074 completed_plan_sp = m_completed_plan_stack[i];
1075 if (!completed_plan_sp->GetPrivate ())
1076 return completed_plan_sp;
1077 }
1078 }
1079 return empty_plan_sp;
1080}
1081
Jim Ingham73ca05a2011-12-17 01:35:57 +00001082ValueObjectSP
1083Thread::GetReturnValueObject ()
1084{
1085 if (!m_completed_plan_stack.empty())
1086 {
1087 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1088 {
1089 ValueObjectSP return_valobj_sp;
1090 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
1091 if (return_valobj_sp)
1092 return return_valobj_sp;
1093 }
1094 }
1095 return ValueObjectSP();
1096}
1097
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001098bool
1099Thread::IsThreadPlanDone (ThreadPlan *plan)
1100{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101 if (!m_completed_plan_stack.empty())
1102 {
1103 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
1104 {
1105 if (m_completed_plan_stack[i].get() == plan)
1106 return true;
1107 }
1108 }
1109 return false;
1110}
1111
1112bool
1113Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
1114{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115 if (!m_discarded_plan_stack.empty())
1116 {
1117 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
1118 {
1119 if (m_discarded_plan_stack[i].get() == plan)
1120 return true;
1121 }
1122 }
1123 return false;
1124}
1125
1126ThreadPlan *
1127Thread::GetPreviousPlan (ThreadPlan *current_plan)
1128{
1129 if (current_plan == NULL)
1130 return NULL;
1131
1132 int stack_size = m_completed_plan_stack.size();
1133 for (int i = stack_size - 1; i > 0; i--)
1134 {
1135 if (current_plan == m_completed_plan_stack[i].get())
1136 return m_completed_plan_stack[i-1].get();
1137 }
1138
1139 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
1140 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141 if (m_plan_stack.size() > 0)
1142 return m_plan_stack.back().get();
1143 else
1144 return NULL;
1145 }
1146
1147 stack_size = m_plan_stack.size();
1148 for (int i = stack_size - 1; i > 0; i--)
1149 {
1150 if (current_plan == m_plan_stack[i].get())
1151 return m_plan_stack[i-1].get();
1152 }
1153 return NULL;
1154}
1155
1156void
1157Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1158{
1159 if (abort_other_plans)
1160 DiscardThreadPlans(true);
1161
1162 PushPlan (thread_plan_sp);
1163}
1164
Jim Ingham06e827c2010-11-11 19:26:09 +00001165
1166void
1167Thread::EnableTracer (bool value, bool single_stepping)
1168{
1169 int stack_size = m_plan_stack.size();
1170 for (int i = 0; i < stack_size; i++)
1171 {
1172 if (m_plan_stack[i]->GetThreadPlanTracer())
1173 {
1174 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1175 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1176 }
1177 }
1178}
1179
1180void
1181Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1182{
1183 int stack_size = m_plan_stack.size();
1184 for (int i = 0; i < stack_size; i++)
1185 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1186}
1187
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001189Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1190{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001191 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1192}
1193
1194void
1195Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1196{
Greg Clayton5160ce52013-03-27 23:08:40 +00001197 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001198 if (log)
1199 {
Daniel Malead01b2952012-11-29 21:49:15 +00001200 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 +00001201 }
1202
1203 int stack_size = m_plan_stack.size();
1204
1205 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1206 // stack, and if so discard up to and including it.
1207
Jim Ingham64e7ead2012-05-03 21:19:36 +00001208 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001209 {
1210 for (int i = stack_size - 1; i > 0; i--)
1211 DiscardPlan();
1212 }
1213 else
1214 {
1215 bool found_it = false;
1216 for (int i = stack_size - 1; i > 0; i--)
1217 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001218 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001219 found_it = true;
1220 }
1221 if (found_it)
1222 {
1223 bool last_one = false;
1224 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1225 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001226 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001227 last_one = true;
1228 DiscardPlan();
1229 }
1230 }
1231 }
1232 return;
1233}
1234
1235void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001236Thread::DiscardThreadPlans(bool force)
1237{
Greg Clayton5160ce52013-03-27 23:08:40 +00001238 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239 if (log)
1240 {
Daniel Malead01b2952012-11-29 21:49:15 +00001241 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001242 }
1243
1244 if (force)
1245 {
1246 int stack_size = m_plan_stack.size();
1247 for (int i = stack_size - 1; i > 0; i--)
1248 {
1249 DiscardPlan();
1250 }
1251 return;
1252 }
1253
1254 while (1)
1255 {
1256
1257 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001258 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259
1260 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1261 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1262 {
1263 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1264 {
1265 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1266 break;
1267 }
1268 }
1269
1270 if (discard)
1271 {
1272 // First pop all the dependent plans:
1273 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1274 {
1275
1276 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1277 // for the plan leaves it in a state that it is safe to pop the plan
1278 // with no more notice?
1279 DiscardPlan();
1280 }
1281
1282 // Now discard the master plan itself.
1283 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1284 // discard it's dependent plans, but not it...
1285 if (master_plan_idx > 0)
1286 {
1287 DiscardPlan();
1288 }
1289 }
1290 else
1291 {
1292 // If the master plan doesn't want to get discarded, then we're done.
1293 break;
1294 }
1295
1296 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297}
1298
Jim Inghamcf274f92012-04-09 22:37:39 +00001299bool
1300Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1301{
1302 if (plan_ptr->IsBasePlan())
1303 return true;
1304 else if (m_plan_stack.size() == 0)
1305 return false;
1306 else
1307 return m_plan_stack[0].get() == plan_ptr;
1308}
1309
Jim Ingham93208b82013-01-31 21:46:01 +00001310Error
1311Thread::UnwindInnermostExpression()
1312{
1313 Error error;
1314 int stack_size = m_plan_stack.size();
1315
1316 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1317 // stack, and if so discard up to and including it.
1318
1319 for (int i = stack_size - 1; i > 0; i--)
1320 {
1321 if (m_plan_stack[i]->GetKind() == ThreadPlan::eKindCallFunction)
1322 {
1323 DiscardThreadPlansUpToPlan(m_plan_stack[i].get());
1324 return error;
1325 }
1326 }
1327 error.SetErrorString("No expressions currently active on this thread");
1328 return error;
1329}
1330
1331
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001332ThreadPlan *
1333Thread::QueueFundamentalPlan (bool abort_other_plans)
1334{
1335 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1336 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1337 return thread_plan_sp.get();
1338}
1339
1340ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001341Thread::QueueThreadPlanForStepSingleInstruction
1342(
1343 bool step_over,
1344 bool abort_other_plans,
1345 bool stop_other_threads
1346)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001347{
1348 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1349 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1350 return thread_plan_sp.get();
1351}
1352
1353ThreadPlan *
Jim Inghamc6276822012-12-12 19:58:40 +00001354Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001355(
1356 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001357 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001358 const SymbolContext &addr_context,
1359 lldb::RunMode stop_other_threads
1360)
1361{
1362 ThreadPlanSP thread_plan_sp;
1363 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1364
1365 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1366 return thread_plan_sp.get();
1367}
1368
1369ThreadPlan *
1370Thread::QueueThreadPlanForStepInRange
1371(
1372 bool abort_other_plans,
1373 const AddressRange &range,
1374 const SymbolContext &addr_context,
1375 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001376 lldb::RunMode stop_other_threads,
1377 bool avoid_code_without_debug_info
1378)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379{
1380 ThreadPlanSP thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001381 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1382 if (avoid_code_without_debug_info)
1383 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001384 else
Jim Inghamc6276822012-12-12 19:58:40 +00001385 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1386 if (step_in_target)
1387 plan->SetStepInTarget(step_in_target);
1388 thread_plan_sp.reset (plan);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389
1390 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1391 return thread_plan_sp.get();
1392}
1393
1394
1395ThreadPlan *
1396Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1397{
1398 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1399 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1400 return thread_plan_sp.get();
1401}
1402
1403ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001404Thread::QueueThreadPlanForStepOut
1405(
1406 bool abort_other_plans,
1407 SymbolContext *addr_context,
1408 bool first_insn,
1409 bool stop_other_threads,
1410 Vote stop_vote,
1411 Vote run_vote,
1412 uint32_t frame_idx
1413)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414{
Greg Clayton481cef22011-01-21 06:11:58 +00001415 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1416 addr_context,
1417 first_insn,
1418 stop_other_threads,
1419 stop_vote,
1420 run_vote,
1421 frame_idx));
Sean Callanan708709c2012-07-31 22:19:25 +00001422
1423 if (thread_plan_sp->ValidatePlan(NULL))
1424 {
1425 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1426 return thread_plan_sp.get();
1427 }
1428 else
1429 {
1430 return NULL;
1431 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001432}
1433
1434ThreadPlan *
Jim Ingham18de2fd2012-05-10 01:35:39 +00001435Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001437 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001438 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1439 return NULL;
1440
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001441 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1442 return thread_plan_sp.get();
1443}
1444
1445ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001446Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1447 Address& function,
1448 lldb::addr_t arg,
1449 bool stop_other_threads,
Jim Ingham184e9812013-01-15 02:47:48 +00001450 bool unwind_on_error,
1451 bool ignore_breakpoints)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001452{
Jim Ingham184e9812013-01-15 02:47:48 +00001453 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this,
1454 function,
1455 ClangASTType(),
1456 arg,
1457 stop_other_threads,
1458 unwind_on_error,
1459 ignore_breakpoints));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001460 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1461 return thread_plan_sp.get();
1462}
1463
1464ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001465Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1466 Address &target_addr,
1467 bool stop_other_threads)
1468{
1469 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1470 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1471 return thread_plan_sp.get();
1472}
1473
1474ThreadPlan *
1475Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001476 lldb::addr_t *address_list,
1477 size_t num_addresses,
1478 bool stop_other_threads,
1479 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480{
Greg Clayton481cef22011-01-21 06:11:58 +00001481 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001482 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1483 return thread_plan_sp.get();
1484
1485}
1486
1487uint32_t
1488Thread::GetIndexID () const
1489{
1490 return m_index_id;
1491}
1492
1493void
1494Thread::DumpThreadPlans (lldb_private::Stream *s) const
1495{
1496 uint32_t stack_size = m_plan_stack.size();
Greg Clayton1346f7e2010-09-03 22:45:01 +00001497 int i;
Jim Ingham10c4b242011-10-15 00:23:43 +00001498 s->Indent();
Daniel Malead01b2952012-11-29 21:49:15 +00001499 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 +00001500 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001501 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001502 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001503 s->Indent();
1504 s->Printf ("Element %d: ", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001505 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001506 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001507 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508 }
1509
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001510 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001511 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001512 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001513 s->Indent();
1514 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1515 for (i = stack_size - 1; i >= 0; i--)
1516 {
1517 s->IndentMore();
1518 s->Indent();
1519 s->Printf ("Element %d: ", i);
1520 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1521 s->EOL();
1522 s->IndentLess();
1523 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001524 }
1525
1526 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001527 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001528 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001529 s->Indent();
1530 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1531 for (i = stack_size - 1; i >= 0; i--)
1532 {
1533 s->IndentMore();
1534 s->Indent();
1535 s->Printf ("Element %d: ", i);
1536 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1537 s->EOL();
1538 s->IndentLess();
1539 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001540 }
1541
1542}
1543
Greg Claytond9e416c2012-02-18 05:35:26 +00001544TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001545Thread::CalculateTarget ()
1546{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001547 TargetSP target_sp;
1548 ProcessSP process_sp(GetProcess());
1549 if (process_sp)
1550 target_sp = process_sp->CalculateTarget();
1551 return target_sp;
1552
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001553}
1554
Greg Claytond9e416c2012-02-18 05:35:26 +00001555ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001556Thread::CalculateProcess ()
1557{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001558 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001559}
1560
Greg Claytond9e416c2012-02-18 05:35:26 +00001561ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001562Thread::CalculateThread ()
1563{
Greg Claytond9e416c2012-02-18 05:35:26 +00001564 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001565}
1566
Greg Claytond9e416c2012-02-18 05:35:26 +00001567StackFrameSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001568Thread::CalculateStackFrame ()
1569{
Greg Claytond9e416c2012-02-18 05:35:26 +00001570 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571}
1572
1573void
Greg Clayton0603aa92010-10-04 01:05:56 +00001574Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001575{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001576 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001577}
1578
Greg Clayton12daf9462010-08-25 00:35:26 +00001579
Greg Clayton39d0ab32012-03-29 01:41:38 +00001580StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001581Thread::GetStackFrameList ()
1582{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001583 StackFrameListSP frame_list_sp;
1584 Mutex::Locker locker(m_frame_mutex);
1585 if (m_curr_frames_sp)
1586 {
1587 frame_list_sp = m_curr_frames_sp;
1588 }
1589 else
1590 {
1591 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1592 m_curr_frames_sp = frame_list_sp;
1593 }
1594 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001595}
1596
Greg Clayton12daf9462010-08-25 00:35:26 +00001597void
1598Thread::ClearStackFrames ()
1599{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001600 Mutex::Locker locker(m_frame_mutex);
1601
Greg Clayton160c9d82013-05-01 21:54:04 +00001602 Unwind *unwinder = GetUnwinder ();
1603 if (unwinder)
1604 unwinder->Clear();
1605
Jim Inghamb0c72a52012-02-29 03:40:22 +00001606 // Only store away the old "reference" StackFrameList if we got all its frames:
1607 // FIXME: At some point we can try to splice in the frames we have fetched into
1608 // the new frame as we make it, but let's not try that now.
1609 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001610 m_prev_frames_sp.swap (m_curr_frames_sp);
1611 m_curr_frames_sp.reset();
Jim Ingham87c11912010-08-12 02:14:28 +00001612}
1613
1614lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001615Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1616{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001617 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001618}
1619
Jim Ingham44137582012-09-12 00:40:39 +00001620
1621Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001622Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001623{
1624 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1625 Error return_error;
1626
1627 if (!frame_sp)
1628 {
Daniel Malead01b2952012-11-29 21:49:15 +00001629 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001630 }
1631
Jim Ingham4f465cf2012-10-10 18:32:14 +00001632 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001633}
1634
1635Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001636Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001637{
1638 Error return_error;
1639
1640 if (!frame_sp)
1641 {
1642 return_error.SetErrorString("Can't return to a null frame.");
1643 return return_error;
1644 }
1645
1646 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001647 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1648 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham93208b82013-01-31 21:46:01 +00001649 if (!older_frame_sp)
1650 {
1651 return_error.SetErrorString("No older frame to return to.");
1652 return return_error;
1653 }
Jim Ingham44137582012-09-12 00:40:39 +00001654
1655 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001656 {
Jim Ingham44137582012-09-12 00:40:39 +00001657 lldb::ABISP abi = thread->GetProcess()->GetABI();
1658 if (!abi)
1659 {
1660 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001661 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001662 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001663 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1664
1665 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1666 // Turn that back on when that works.
1667 if (0 && sc.function != NULL)
1668 {
1669 Type *function_type = sc.function->GetType();
1670 if (function_type)
1671 {
1672 clang_type_t return_type = sc.function->GetReturnClangType();
1673 if (return_type)
1674 {
1675 ClangASTType ast_type (function_type->GetClangAST(), return_type);
1676 StreamString s;
1677 ast_type.DumpTypeDescription(&s);
1678 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type);
1679 if (cast_value_sp)
1680 {
1681 cast_value_sp->SetFormat(eFormatHex);
1682 return_value_sp = cast_value_sp;
1683 }
1684 }
1685 }
1686 }
1687
Jim Inghamcb640dd2012-09-14 02:14:15 +00001688 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001689 if (!return_error.Success())
1690 return return_error;
1691 }
1692
1693 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001694 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
Jim Ingham93208b82013-01-31 21:46:01 +00001695 // cook their data
1696
1697 StackFrameSP youngest_frame_sp = thread->GetStackFrameAtIndex(0);
1698 if (youngest_frame_sp)
Jim Ingham44137582012-09-12 00:40:39 +00001699 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001700 lldb::RegisterContextSP reg_ctx_sp (youngest_frame_sp->GetRegisterContext());
1701 if (reg_ctx_sp)
Jim Ingham93208b82013-01-31 21:46:01 +00001702 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001703 bool copy_success = reg_ctx_sp->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1704 if (copy_success)
1705 {
1706 thread->DiscardThreadPlans(true);
1707 thread->ClearStackFrames();
1708 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1709 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
1710 }
1711 else
1712 {
1713 return_error.SetErrorString("Could not reset register values.");
1714 }
Jim Ingham93208b82013-01-31 21:46:01 +00001715 }
1716 else
1717 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001718 return_error.SetErrorString("Frame has no register context.");
Jim Ingham93208b82013-01-31 21:46:01 +00001719 }
Jim Ingham44137582012-09-12 00:40:39 +00001720 }
1721 else
1722 {
Jim Ingham93208b82013-01-31 21:46:01 +00001723 return_error.SetErrorString("Returned past top frame.");
Jim Ingham44137582012-09-12 00:40:39 +00001724 }
Greg Claytonabb487f2013-02-01 02:52:31 +00001725 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001726}
1727
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001728void
Greg Clayton0603aa92010-10-04 01:05:56 +00001729Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001730{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001731 ExecutionContext exe_ctx (shared_from_this());
1732 Process *process = exe_ctx.GetProcessPtr();
1733 if (process == NULL)
1734 return;
1735
Greg Claytonc14ee322011-09-22 04:58:26 +00001736 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00001737 SymbolContext frame_sc;
Greg Clayton0603aa92010-10-04 01:05:56 +00001738 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001739 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001740 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001741 if (frame_sp)
1742 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001743 exe_ctx.SetFrameSP(frame_sp);
1744 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001745 }
1746 }
1747
Greg Clayton1ac04c32012-02-21 00:09:25 +00001748 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00001749 assert (thread_format);
Greg Clayton0603aa92010-10-04 01:05:56 +00001750 Debugger::FormatPrompt (thread_format,
Greg Claytonc14ee322011-09-22 04:58:26 +00001751 frame_sp ? &frame_sc : NULL,
Greg Clayton0603aa92010-10-04 01:05:56 +00001752 &exe_ctx,
1753 NULL,
Michael Sartainc3ce7f272013-05-23 20:47:45 +00001754 strm);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001755}
1756
Greg Clayton99d0faf2010-11-18 23:32:35 +00001757void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001758Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00001759{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001760}
Jim Inghamee8aea12010-09-08 03:14:33 +00001761
Greg Clayton99d0faf2010-11-18 23:32:35 +00001762void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001763Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001764{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001765}
Jim Inghamee8aea12010-09-08 03:14:33 +00001766
Jim Inghame4284b72010-09-23 17:40:12 +00001767lldb::StackFrameSP
1768Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1769{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001770 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00001771}
Caroline Ticeceb6b132010-10-26 03:11:13 +00001772
1773const char *
1774Thread::StopReasonAsCString (lldb::StopReason reason)
1775{
1776 switch (reason)
1777 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00001778 case eStopReasonInvalid: return "invalid";
1779 case eStopReasonNone: return "none";
1780 case eStopReasonTrace: return "trace";
1781 case eStopReasonBreakpoint: return "breakpoint";
1782 case eStopReasonWatchpoint: return "watchpoint";
1783 case eStopReasonSignal: return "signal";
1784 case eStopReasonException: return "exception";
1785 case eStopReasonExec: return "exec";
1786 case eStopReasonPlanComplete: return "plan complete";
1787 case eStopReasonThreadExiting: return "thread exiting";
Caroline Ticeceb6b132010-10-26 03:11:13 +00001788 }
1789
1790
1791 static char unknown_state_string[64];
1792 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1793 return unknown_state_string;
1794}
1795
1796const char *
1797Thread::RunModeAsCString (lldb::RunMode mode)
1798{
1799 switch (mode)
1800 {
1801 case eOnlyThisThread: return "only this thread";
1802 case eAllThreads: return "all threads";
1803 case eOnlyDuringStepping: return "only during stepping";
1804 }
1805
1806 static char unknown_state_string[64];
1807 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1808 return unknown_state_string;
1809}
Jim Ingham06e827c2010-11-11 19:26:09 +00001810
Greg Clayton7260f622011-04-18 08:33:37 +00001811size_t
1812Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1813{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001814 ExecutionContext exe_ctx (shared_from_this());
1815 Target *target = exe_ctx.GetTargetPtr();
1816 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00001817 size_t num_frames_shown = 0;
1818 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00001819 bool is_selected = false;
1820 if (process)
1821 {
1822 if (process->GetThreadList().GetSelectedThread().get() == this)
1823 is_selected = true;
1824 }
1825 strm.Printf("%c ", is_selected ? '*' : ' ');
1826 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00001827 {
Greg Clayton5113dc82011-08-12 06:47:54 +00001828 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00001829 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00001830 {
Jim Inghame610d642011-08-16 00:07:28 +00001831 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1832 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1833 {
1834 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1835 }
Greg Clayton5113dc82011-08-12 06:47:54 +00001836 }
Greg Clayton7260f622011-04-18 08:33:37 +00001837 }
1838
1839 DumpUsingSettingsFormat (strm, start_frame);
1840
1841 if (num_frames > 0)
1842 {
1843 strm.IndentMore();
1844
1845 const bool show_frame_info = true;
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001846 strm.IndentMore ();
Greg Clayton39d0ab32012-03-29 01:41:38 +00001847 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1848 start_frame,
1849 num_frames,
1850 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001851 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001852 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001853 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00001854 }
1855 return num_frames_shown;
1856}
1857
1858size_t
1859Thread::GetStackFrameStatus (Stream& strm,
1860 uint32_t first_frame,
1861 uint32_t num_frames,
1862 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001863 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00001864{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001865 return GetStackFrameList()->GetStatus (strm,
1866 first_frame,
1867 num_frames,
1868 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001869 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001870}
1871
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001872bool
1873Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1874{
1875 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1876 if (frame_sp)
1877 {
1878 checkpoint.SetStackID(frame_sp->GetStackID());
Greg Clayton1afa68e2013-04-02 20:32:37 +00001879 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
1880 if (reg_ctx_sp)
1881 return reg_ctx_sp->ReadAllRegisterValues (checkpoint.GetData());
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001882 }
1883 return false;
1884}
Greg Clayton7260f622011-04-18 08:33:37 +00001885
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001886bool
1887Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1888{
Jim Ingham44137582012-09-12 00:40:39 +00001889 return ResetFrameZeroRegisters (checkpoint.GetData());
1890}
1891
1892bool
1893Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1894{
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001895 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1896 if (frame_sp)
1897 {
Greg Clayton1afa68e2013-04-02 20:32:37 +00001898 lldb::RegisterContextSP reg_ctx_sp (frame_sp->GetRegisterContext());
1899 if (reg_ctx_sp)
1900 {
1901 bool ret = reg_ctx_sp->WriteAllRegisterValues (register_data_sp);
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001902
Greg Clayton1afa68e2013-04-02 20:32:37 +00001903 // Clear out all stack frames as our world just changed.
1904 ClearStackFrames();
1905 reg_ctx_sp->InvalidateIfNeeded(true);
1906 if (m_unwinder_ap.get())
1907 m_unwinder_ap->Clear();
1908 return ret;
1909 }
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001910 }
1911 return false;
1912}
Greg Clayton7260f622011-04-18 08:33:37 +00001913
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001914Unwind *
1915Thread::GetUnwinder ()
1916{
1917 if (m_unwinder_ap.get() == NULL)
1918 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00001919 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001920 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1921 switch (machine)
1922 {
1923 case llvm::Triple::x86_64:
1924 case llvm::Triple::x86:
1925 case llvm::Triple::arm:
1926 case llvm::Triple::thumb:
1927 m_unwinder_ap.reset (new UnwindLLDB (*this));
1928 break;
1929
1930 default:
1931 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1932 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1933 break;
1934 }
1935 }
1936 return m_unwinder_ap.get();
1937}
1938
1939
Greg Claytonfa559e52012-05-18 02:38:05 +00001940void
1941Thread::Flush ()
1942{
1943 ClearStackFrames ();
1944 m_reg_context_sp.reset();
1945}
Jim Ingham5d88a062012-10-16 00:09:33 +00001946
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00001947bool
Jim Ingham5d88a062012-10-16 00:09:33 +00001948Thread::IsStillAtLastBreakpointHit ()
1949{
1950 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
1951 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
1952 // multithreaded programs.
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00001953 if (m_stop_info_sp) {
1954 StopReason stop_reason = m_stop_info_sp->GetStopReason();
Jim Ingham5d88a062012-10-16 00:09:33 +00001955 if (stop_reason == lldb::eStopReasonBreakpoint) {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +00001956 uint64_t value = m_stop_info_sp->GetValue();
Greg Clayton1afa68e2013-04-02 20:32:37 +00001957 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
1958 if (reg_ctx_sp)
1959 {
1960 lldb::addr_t pc = reg_ctx_sp->GetPC();
1961 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1962 if (bp_site_sp && value == bp_site_sp->GetID())
1963 return true;
1964 }
Jim Ingham5d88a062012-10-16 00:09:33 +00001965 }
1966 }
1967 return false;
1968}