blob: 34b8600a81549e33578141e26112c25369f5ce2c [file] [log] [blame]
Chris Lattner24943d22010-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 Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "lldb/lldb-private-log.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000013#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Claytona830adb2010-10-04 01:05:56 +000014#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Core/Log.h"
16#include "lldb/Core/Stream.h"
17#include "lldb/Core/StreamString.h"
Jim Ingham20594b12010-09-08 03:14:33 +000018#include "lldb/Core/RegularExpression.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000019#include "lldb/Host/Host.h"
Jim Ingham46657452012-09-27 01:15:29 +000020#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Target/DynamicLoader.h"
22#include "lldb/Target/ExecutionContext.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000023#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Target/Process.h"
25#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000026#include "lldb/Target/StopInfo.h"
Greg Clayton7661a982010-07-23 16:45:51 +000027#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Target/Thread.h"
29#include "lldb/Target/ThreadPlan.h"
30#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000031#include "lldb/Target/ThreadPlanBase.h"
32#include "lldb/Target/ThreadPlanStepInstruction.h"
33#include "lldb/Target/ThreadPlanStepOut.h"
34#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
35#include "lldb/Target/ThreadPlanStepThrough.h"
36#include "lldb/Target/ThreadPlanStepInRange.h"
37#include "lldb/Target/ThreadPlanStepOverRange.h"
38#include "lldb/Target/ThreadPlanRunToAddress.h"
39#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000040#include "lldb/Target/ThreadSpec.h"
Jim Ingham71219082010-08-12 02:14:28 +000041#include "lldb/Target/Unwind.h"
Greg Clayton37f962e2011-08-22 02:49:39 +000042#include "Plugins/Process/Utility/UnwindLLDB.h"
43#include "UnwindMacOSXFrameBackchain.h"
44
Chris Lattner24943d22010-06-08 16:52:24 +000045
46using namespace lldb;
47using namespace lldb_private;
48
Greg Clayton73844aa2012-08-22 17:17:09 +000049
50const ThreadPropertiesSP &
51Thread::GetGlobalProperties()
52{
53 static ThreadPropertiesSP g_settings_sp;
54 if (!g_settings_sp)
55 g_settings_sp.reset (new ThreadProperties (true));
56 return g_settings_sp;
57}
58
59static PropertyDefinition
60g_properties[] =
61{
62 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
63 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
64 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
65};
66
67enum {
68 ePropertyStepAvoidRegex,
69 ePropertyEnableThreadTrace
70};
71
72
73class ThreadOptionValueProperties : public OptionValueProperties
74{
75public:
76 ThreadOptionValueProperties (const ConstString &name) :
77 OptionValueProperties (name)
78 {
79 }
80
81 // This constructor is used when creating ThreadOptionValueProperties when it
82 // is part of a new lldb_private::Thread instance. It will copy all current
83 // global property values as needed
84 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Claytonc6e82e42012-08-22 18:39:03 +000085 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton73844aa2012-08-22 17:17:09 +000086 {
87 }
88
89 virtual const Property *
90 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
91 {
92 // When gettings the value for a key from the thread options, we will always
93 // try and grab the setting from the current thread if there is one. Else we just
94 // use the one from this instance.
95 if (exe_ctx)
96 {
97 Thread *thread = exe_ctx->GetThreadPtr();
98 if (thread)
99 {
100 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
101 if (this != instance_properties)
102 return instance_properties->ProtectedGetPropertyAtIndex (idx);
103 }
104 }
105 return ProtectedGetPropertyAtIndex (idx);
106 }
107};
108
109
110
111ThreadProperties::ThreadProperties (bool is_global) :
112 Properties ()
113{
114 if (is_global)
115 {
116 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
117 m_collection_sp->Initialize(g_properties);
118 }
119 else
120 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
121}
122
123ThreadProperties::~ThreadProperties()
124{
125}
126
127const RegularExpression *
128ThreadProperties::GetSymbolsToAvoidRegexp()
129{
130 const uint32_t idx = ePropertyStepAvoidRegex;
131 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
132}
133
134bool
135ThreadProperties::GetTraceEnabledState() const
136{
137 const uint32_t idx = ePropertyEnableThreadTrace;
138 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
139}
140
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000141//------------------------------------------------------------------
142// Thread Event Data
143//------------------------------------------------------------------
Greg Clayton73844aa2012-08-22 17:17:09 +0000144
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000145
146const ConstString &
147Thread::ThreadEventData::GetFlavorString ()
148{
149 static ConstString g_flavor ("Thread::ThreadEventData");
150 return g_flavor;
151}
152
153Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
154 m_thread_sp (thread_sp),
155 m_stack_id ()
156{
157}
158
159Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
160 m_thread_sp (thread_sp),
161 m_stack_id (stack_id)
162{
163}
164
165Thread::ThreadEventData::ThreadEventData () :
166 m_thread_sp (),
167 m_stack_id ()
168{
169}
170
171Thread::ThreadEventData::~ThreadEventData ()
172{
173}
174
175void
176Thread::ThreadEventData::Dump (Stream *s) const
177{
178
179}
180
181const Thread::ThreadEventData *
182Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
183{
184 if (event_ptr)
185 {
186 const EventData *event_data = event_ptr->GetData();
187 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
188 return static_cast <const ThreadEventData *> (event_ptr->GetData());
189 }
190 return NULL;
191}
192
193ThreadSP
194Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
195{
196 ThreadSP thread_sp;
197 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
198 if (event_data)
199 thread_sp = event_data->GetThread();
200 return thread_sp;
201}
202
203StackID
204Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
205{
206 StackID stack_id;
207 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
208 if (event_data)
209 stack_id = event_data->GetStackID();
210 return stack_id;
211}
212
213StackFrameSP
214Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
215{
216 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
217 StackFrameSP frame_sp;
218 if (event_data)
219 {
220 ThreadSP thread_sp = event_data->GetThread();
221 if (thread_sp)
222 {
223 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
224 }
225 }
226 return frame_sp;
227}
228
229//------------------------------------------------------------------
230// Thread class
231//------------------------------------------------------------------
232
233ConstString &
234Thread::GetStaticBroadcasterClass ()
235{
236 static ConstString class_name ("lldb.thread");
237 return class_name;
238}
239
240Thread::Thread (Process &process, lldb::tid_t tid) :
Greg Clayton73844aa2012-08-22 17:17:09 +0000241 ThreadProperties (false),
Chris Lattner24943d22010-06-08 16:52:24 +0000242 UserID (tid),
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000243 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
244 m_process_wp (process.shared_from_this()),
Greg Clayton643ee732010-08-04 01:40:35 +0000245 m_actual_stop_info_sp (),
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000246 m_index_id (process.GetNextThreadIndexID ()),
Chris Lattner24943d22010-06-08 16:52:24 +0000247 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000248 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +0000249 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +0000250 m_plan_stack (),
Chris Lattner24943d22010-06-08 16:52:24 +0000251 m_completed_plan_stack(),
Greg Clayton2450cb12012-03-29 01:41:38 +0000252 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000253 m_curr_frames_sp (),
254 m_prev_frames_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000255 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +0000256 m_resume_state (eStateRunning),
Jim Ingham149d1f52012-01-31 23:09:20 +0000257 m_temporary_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +0000258 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000259 m_destroy_called (false),
260 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +0000261
Chris Lattner24943d22010-06-08 16:52:24 +0000262{
Greg Claytone005f2c2010-11-06 01:53:30 +0000263 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000264 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000265 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000266
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000267 CheckInWithManager();
Chris Lattner24943d22010-06-08 16:52:24 +0000268 QueueFundamentalPlan(true);
269}
270
271
272Thread::~Thread()
273{
Greg Claytone005f2c2010-11-06 01:53:30 +0000274 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000275 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000276 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +0000277 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
278 assert (m_destroy_called);
279}
280
281void
282Thread::DestroyThread ()
283{
Greg Clayton84332782012-10-29 20:52:08 +0000284 m_destroy_called = true;
Jim Inghamcdea2362010-11-18 02:47:07 +0000285 m_plan_stack.clear();
286 m_discarded_plan_stack.clear();
287 m_completed_plan_stack.clear();
Jim Inghame93055a2012-04-10 00:44:25 +0000288 m_actual_stop_info_sp.reset();
Greg Clayton84332782012-10-29 20:52:08 +0000289 m_reg_context_sp.reset();
290 m_unwinder_ap.reset();
291 Mutex::Locker locker(m_frame_mutex);
292 m_curr_frames_sp.reset();
293 m_prev_frames_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000294}
295
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000296void
297Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
298{
299 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
300 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
301}
302
303uint32_t
304Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
305{
306 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
307 if (broadcast)
308 BroadcastSelectedFrameChange(frame->GetStackID());
309 return ret_value;
310}
311
312bool
313Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
314{
315 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
316 if (frame_sp)
317 {
318 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
319 if (broadcast)
320 BroadcastSelectedFrameChange(frame_sp->GetStackID());
321 return true;
322 }
323 else
324 return false;
325}
326
327
Jim Ingham6297a3a2010-10-20 00:39:53 +0000328lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000329Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +0000330{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000331 ThreadPlanSP plan_sp (GetCompletedPlan());
Jim Ingham707b7a82012-05-01 18:38:37 +0000332 if (plan_sp && plan_sp->PlanSucceeded())
Jim Ingham1586d972011-12-17 01:35:57 +0000333 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000334 else
Jim Ingham24e0d612011-08-09 00:32:52 +0000335 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000336 ProcessSP process_sp (GetProcess());
337 if (process_sp
338 && m_actual_stop_info_sp
Jim Ingham24e0d612011-08-09 00:32:52 +0000339 && m_actual_stop_info_sp->IsValid()
Greg Claytonf4124de2012-02-21 00:09:25 +0000340 && m_thread_stop_reason_stop_id == process_sp->GetStopID())
Jim Ingham24e0d612011-08-09 00:32:52 +0000341 return m_actual_stop_info_sp;
342 else
343 return GetPrivateStopReason ();
344 }
Chris Lattner24943d22010-06-08 16:52:24 +0000345}
346
Greg Clayton3acaa922012-09-25 02:40:06 +0000347lldb::StopReason
348Thread::GetStopReason()
349{
350 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
351 if (stop_info_sp)
Filipe Cabecinhasc50a1dd2012-09-28 15:55:43 +0000352 return stop_info_sp->GetStopReason();
Greg Clayton3acaa922012-09-25 02:40:06 +0000353 return eStopReasonNone;
354}
355
356
357
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000358void
359Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
360{
361 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham24e0d612011-08-09 00:32:52 +0000362 if (m_actual_stop_info_sp)
363 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Claytonf4124de2012-02-21 00:09:25 +0000364 ProcessSP process_sp (GetProcess());
365 if (process_sp)
366 m_thread_stop_reason_stop_id = process_sp->GetStopID();
367 else
368 m_thread_stop_reason_stop_id = UINT32_MAX;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000369}
370
371void
372Thread::SetStopInfoToNothing()
373{
374 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
375 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
376 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
377}
378
Chris Lattner24943d22010-06-08 16:52:24 +0000379bool
380Thread::ThreadStoppedForAReason (void)
381{
Jim Ingham9880efa2012-08-11 00:35:26 +0000382 return (bool) GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +0000383}
384
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000385bool
386Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
387{
388 if (!SaveFrameZeroState(saved_state.register_backup))
389 return false;
390
391 saved_state.stop_info_sp = GetStopInfo();
Greg Claytonf4124de2012-02-21 00:09:25 +0000392 ProcessSP process_sp (GetProcess());
393 if (process_sp)
394 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham36de3c02012-09-07 23:36:43 +0000395 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
396
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000397 return true;
398}
399
400bool
Jim Ingham76b258d2012-11-26 23:52:18 +0000401Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000402{
403 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham76b258d2012-11-26 23:52:18 +0000404 return true;
405}
406
407bool
408Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
409{
Jim Ingham11a837d2011-01-25 02:47:23 +0000410 if (saved_state.stop_info_sp)
411 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000412 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham36de3c02012-09-07 23:36:43 +0000413 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000414 return true;
415}
416
Chris Lattner24943d22010-06-08 16:52:24 +0000417StateType
418Thread::GetState() const
419{
420 // If any other threads access this we will need a mutex for it
421 Mutex::Locker locker(m_state_mutex);
422 return m_state;
423}
424
425void
426Thread::SetState(StateType state)
427{
428 Mutex::Locker locker(m_state_mutex);
429 m_state = state;
430}
431
432void
433Thread::WillStop()
434{
435 ThreadPlan *current_plan = GetCurrentPlan();
436
437 // FIXME: I may decide to disallow threads with no plans. In which
438 // case this should go to an assert.
439
440 if (!current_plan)
441 return;
442
443 current_plan->WillStop();
444}
445
446void
447Thread::SetupForResume ()
448{
449 if (GetResumeState() != eStateSuspended)
450 {
451
452 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
453 // telling the current plan it will resume, since we might change what the current
454 // plan is.
455
Jim Ingham6bc24c12012-10-16 00:09:33 +0000456 StopReason stop_reason = lldb::eStopReasonInvalid;
457 StopInfoSP stop_info_sp = GetStopInfo();
458 if (stop_info_sp.get())
459 stop_reason = stop_info_sp->GetStopReason();
460 if (stop_reason == lldb::eStopReasonBreakpoint)
Chris Lattner24943d22010-06-08 16:52:24 +0000461 {
462 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
463 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000464
465 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000466
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000467 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
468 {
469 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
470 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000471 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000472 ThreadPlanSP step_bp_plan_sp;
473 step_bp_plan->SetPrivate (true);
474
Chris Lattner24943d22010-06-08 16:52:24 +0000475 if (GetCurrentPlan()->RunState() != eStateStepping)
476 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000477 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000478 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000479 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000480 QueueThreadPlan (step_bp_plan_sp, false);
481 }
482 }
483 }
484 }
485}
486
487bool
488Thread::WillResume (StateType resume_state)
489{
490 // At this point clear the completed plan stack.
491 m_completed_plan_stack.clear();
492 m_discarded_plan_stack.clear();
493
Greg Clayton3acaa922012-09-25 02:40:06 +0000494 m_temporary_resume_state = resume_state;
Jim Ingham149d1f52012-01-31 23:09:20 +0000495
496 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
497 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
498 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
499 // about the fact that we are resuming...
Greg Claytonf4124de2012-02-21 00:09:25 +0000500 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham149d1f52012-01-31 23:09:20 +0000501 if (m_thread_stop_reason_stop_id == process_stop_id &&
502 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
503 {
504 StopInfo *stop_info = GetPrivateStopReason().get();
505 if (stop_info)
506 stop_info->WillResume (resume_state);
507 }
Chris Lattner24943d22010-06-08 16:52:24 +0000508
509 // Tell all the plans that we are about to resume in case they need to clear any state.
510 // We distinguish between the plan on the top of the stack and the lower
511 // plans in case a plan needs to do any special business before it runs.
512
513 ThreadPlan *plan_ptr = GetCurrentPlan();
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000514 bool need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000515
516 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
517 {
518 plan_ptr->WillResume (resume_state, false);
519 }
Greg Clayton643ee732010-08-04 01:40:35 +0000520
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000521 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
522 // In that case, don't reset it here.
523
Jim Ingham6bc24c12012-10-16 00:09:33 +0000524 if (need_to_resume && resume_state != eStateSuspended)
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000525 {
526 m_actual_stop_info_sp.reset();
527 }
528
529 return need_to_resume;
Chris Lattner24943d22010-06-08 16:52:24 +0000530}
531
532void
533Thread::DidResume ()
534{
535 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
536}
537
538bool
539Thread::ShouldStop (Event* event_ptr)
540{
541 ThreadPlan *current_plan = GetCurrentPlan();
542 bool should_stop = true;
543
Greg Claytone005f2c2010-11-06 01:53:30 +0000544 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham149d1f52012-01-31 23:09:20 +0000545
546 if (GetResumeState () == eStateSuspended)
547 {
548 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000549 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham149d1f52012-01-31 23:09:20 +0000550 __FUNCTION__,
551 GetID ());
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000552// log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham149d1f52012-01-31 23:09:20 +0000553// __FUNCTION__,
554// GetID (),
555// GetRegisterContext()->GetPC());
556 return false;
557 }
558
559 if (GetTemporaryResumeState () == eStateSuspended)
560 {
561 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000562 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham149d1f52012-01-31 23:09:20 +0000563 __FUNCTION__,
564 GetID ());
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000565// log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since thread was suspended)",
Jim Ingham149d1f52012-01-31 23:09:20 +0000566// __FUNCTION__,
567// GetID (),
568// GetRegisterContext()->GetPC());
569 return false;
570 }
571
572 if (ThreadStoppedForAReason() == false)
573 {
574 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000575 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64 ", should_stop = 0 (ignore since no stop reason)",
Jim Ingham149d1f52012-01-31 23:09:20 +0000576 __FUNCTION__,
577 GetID (),
578 GetRegisterContext()->GetPC());
579 return false;
580 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000581
Chris Lattner24943d22010-06-08 16:52:24 +0000582 if (log)
583 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000584 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
Jim Ingham149d1f52012-01-31 23:09:20 +0000585 __FUNCTION__,
586 GetID (),
587 GetRegisterContext()->GetPC());
Jim Inghame8f4e112011-10-15 00:23:43 +0000588 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000589 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000590 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000591 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000592 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000593 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000594
595 // The top most plan always gets to do the trace log...
596 current_plan->DoTraceLog ();
Jim Inghame787c7e2012-04-20 21:16:56 +0000597
598 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
599 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
600 // do any more work on this stop.
601 StopInfoSP private_stop_info (GetPrivateStopReason());
602 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
603 {
604 if (log)
605 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
606 return false;
607 }
Chris Lattner24943d22010-06-08 16:52:24 +0000608
Jim Ingham98d50212012-09-05 21:12:49 +0000609 // If we've already been restarted, don't query the plans since the state they would examine is not current.
610 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
611 return false;
612
613 // Before the plans see the state of the world, calculate the current inlined depth.
614 GetStackFrameList()->CalculateCurrentInlinedDepth();
615
Jim Inghamad382c52011-12-03 01:52:59 +0000616 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
617 // If that plan is still working, then we don't need to do any more work. If the plan that explains
618 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
619 // whether they still need to do more work.
620
621 bool done_processing_current_plan = false;
622
623 if (!current_plan->PlanExplainsStop())
624 {
625 if (current_plan->TracerExplainsStop())
626 {
627 done_processing_current_plan = true;
628 should_stop = false;
629 }
630 else
631 {
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000632 // If the current plan doesn't explain the stop, then find one that
Jim Inghamad382c52011-12-03 01:52:59 +0000633 // does and let it handle the situation.
634 ThreadPlan *plan_ptr = current_plan;
635 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
636 {
637 if (plan_ptr->PlanExplainsStop())
638 {
639 should_stop = plan_ptr->ShouldStop (event_ptr);
640
641 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
642 // and all the plans below it off the stack.
643
644 if (plan_ptr->MischiefManaged())
645 {
Jim Inghamd1ec3d62012-05-11 23:49:49 +0000646 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000647 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Inghamad382c52011-12-03 01:52:59 +0000648
649 do
650 {
651 if (should_stop)
652 current_plan->WillStop();
653 PopPlan();
654 }
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000655 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
656 // Now, if the responsible plan was not "Okay to discard" then we're done,
657 // otherwise we forward this to the next plan in the stack below.
658 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
659 done_processing_current_plan = true;
660 else
661 done_processing_current_plan = false;
Jim Inghamad382c52011-12-03 01:52:59 +0000662 }
663 else
664 done_processing_current_plan = true;
665
666 break;
667 }
668
669 }
670 }
671 }
672
673 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000674 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000675 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000676
Jim Inghame8f4e112011-10-15 00:23:43 +0000677 if (log)
678 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
679
Jim Inghamf9f40c22011-02-08 05:20:59 +0000680 // We're starting from the base plan, so just let it decide;
681 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000682 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000683 should_stop = current_plan->ShouldStop (event_ptr);
684 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000685 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000686 }
687 else
688 {
689 // Otherwise, don't let the base plan override what the other plans say to do, since
690 // presumably if there were other plans they would know what to do...
691 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000692 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000693 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000694 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000695
696 should_stop = current_plan->ShouldStop(event_ptr);
697 if (log)
698 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
699 if (current_plan->MischiefManaged())
700 {
701 if (should_stop)
702 current_plan->WillStop();
703
704 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
705 // Otherwise, see if the plan's parent wants to stop.
706
707 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
708 {
709 PopPlan();
710 break;
711 }
712 else
713 {
714
715 PopPlan();
716
717 current_plan = GetCurrentPlan();
718 if (current_plan == NULL)
719 {
720 break;
721 }
722 }
Chris Lattner24943d22010-06-08 16:52:24 +0000723 }
724 else
725 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000726 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000727 }
Chris Lattner24943d22010-06-08 16:52:24 +0000728 }
729 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000730
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000731 if (over_ride_stop)
732 should_stop = false;
Jim Ingham88e3de22012-05-03 21:19:36 +0000733
734 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
735 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
736 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
737 // This code clears stale plans off the stack.
738
739 if (should_stop)
740 {
741 ThreadPlan *plan_ptr = GetCurrentPlan();
742 while (!PlanIsBasePlan(plan_ptr))
743 {
744 bool stale = plan_ptr->IsPlanStale ();
745 ThreadPlan *examined_plan = plan_ptr;
746 plan_ptr = GetPreviousPlan (examined_plan);
747
748 if (stale)
749 {
750 if (log)
751 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
752 DiscardThreadPlansUpToPlan(examined_plan);
753 }
754 }
755 }
756
Chris Lattner24943d22010-06-08 16:52:24 +0000757 }
Chris Lattner24943d22010-06-08 16:52:24 +0000758
Jim Inghame8f4e112011-10-15 00:23:43 +0000759 if (log)
760 {
761 StreamString s;
762 s.IndentMore();
763 DumpThreadPlans(&s);
764 log->Printf ("Plan stack final state:\n%s", s.GetData());
765 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
766 }
Chris Lattner24943d22010-06-08 16:52:24 +0000767 return should_stop;
768}
769
770Vote
771Thread::ShouldReportStop (Event* event_ptr)
772{
773 StateType thread_state = GetResumeState ();
Jim Ingham149d1f52012-01-31 23:09:20 +0000774 StateType temp_thread_state = GetTemporaryResumeState();
775
Greg Claytone005f2c2010-11-06 01:53:30 +0000776 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000777
778 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
779 {
780 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000781 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
Chris Lattner24943d22010-06-08 16:52:24 +0000782 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000783 }
Chris Lattner24943d22010-06-08 16:52:24 +0000784
Jim Ingham149d1f52012-01-31 23:09:20 +0000785 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
786 {
787 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000788 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
Jim Ingham149d1f52012-01-31 23:09:20 +0000789 return eVoteNoOpinion;
790 }
791
792 if (!ThreadStoppedForAReason())
793 {
794 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000795 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
Jim Ingham149d1f52012-01-31 23:09:20 +0000796 return eVoteNoOpinion;
797 }
798
Chris Lattner24943d22010-06-08 16:52:24 +0000799 if (m_completed_plan_stack.size() > 0)
800 {
801 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000802 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000803 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote for complete stack's back plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000804 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
805 }
806 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000807 {
808 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000809 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000810 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000811 }
Chris Lattner24943d22010-06-08 16:52:24 +0000812}
813
814Vote
815Thread::ShouldReportRun (Event* event_ptr)
816{
817 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000818
Chris Lattner24943d22010-06-08 16:52:24 +0000819 if (thread_state == eStateSuspended
820 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000821 {
Chris Lattner24943d22010-06-08 16:52:24 +0000822 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000823 }
824
825 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000826 if (m_completed_plan_stack.size() > 0)
827 {
828 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000829 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000830 log->Printf ("Current Plan for thread %d (0x%4.4" PRIx64 "): %s being asked whether we should report run.",
Jim Inghamac959662011-01-24 06:34:17 +0000831 GetIndexID(),
832 GetID(),
833 m_completed_plan_stack.back()->GetName());
834
Chris Lattner24943d22010-06-08 16:52:24 +0000835 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
836 }
837 else
Jim Inghamac959662011-01-24 06:34:17 +0000838 {
839 if (log)
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000840 log->Printf ("Current Plan for thread %d (0x%4.4" PRIx64 "): %s being asked whether we should report run.",
Jim Inghamac959662011-01-24 06:34:17 +0000841 GetIndexID(),
842 GetID(),
843 GetCurrentPlan()->GetName());
844
Chris Lattner24943d22010-06-08 16:52:24 +0000845 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000846 }
Chris Lattner24943d22010-06-08 16:52:24 +0000847}
848
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000849bool
850Thread::MatchesSpec (const ThreadSpec *spec)
851{
852 if (spec == NULL)
853 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000854
Jim Inghama2664912012-03-07 22:03:04 +0000855 return spec->ThreadPassesBasicTests(*this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000856}
857
Chris Lattner24943d22010-06-08 16:52:24 +0000858void
859Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
860{
861 if (thread_plan_sp)
862 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000863 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
864 if (!thread_plan_sp->GetThreadPlanTracer())
865 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000866 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000867
Chris Lattner24943d22010-06-08 16:52:24 +0000868 thread_plan_sp->DidPush();
869
Greg Claytone005f2c2010-11-06 01:53:30 +0000870 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000871 if (log)
872 {
873 StreamString s;
874 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000875 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
Chris Lattner24943d22010-06-08 16:52:24 +0000876 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000877 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000878 }
879 }
880}
881
882void
883Thread::PopPlan ()
884{
Greg Claytone005f2c2010-11-06 01:53:30 +0000885 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000886
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000887 if (m_plan_stack.size() <= 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000888 return;
889 else
890 {
891 ThreadPlanSP &plan = m_plan_stack.back();
892 if (log)
893 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000894 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000895 }
896 m_completed_plan_stack.push_back (plan);
897 plan->WillPop();
898 m_plan_stack.pop_back();
899 }
900}
901
902void
903Thread::DiscardPlan ()
904{
905 if (m_plan_stack.size() > 1)
906 {
907 ThreadPlanSP &plan = m_plan_stack.back();
908 m_discarded_plan_stack.push_back (plan);
909 plan->WillPop();
910 m_plan_stack.pop_back();
911 }
912}
913
914ThreadPlan *
915Thread::GetCurrentPlan ()
916{
Jim Ingham2f41d272012-04-19 00:17:05 +0000917 // There will always be at least the base plan. If somebody is mucking with a
918 // thread with an empty plan stack, we should assert right away.
919 assert (!m_plan_stack.empty());
920
921 return m_plan_stack.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000922}
923
924ThreadPlanSP
925Thread::GetCompletedPlan ()
926{
927 ThreadPlanSP empty_plan_sp;
928 if (!m_completed_plan_stack.empty())
929 {
930 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
931 {
932 ThreadPlanSP completed_plan_sp;
933 completed_plan_sp = m_completed_plan_stack[i];
934 if (!completed_plan_sp->GetPrivate ())
935 return completed_plan_sp;
936 }
937 }
938 return empty_plan_sp;
939}
940
Jim Ingham1586d972011-12-17 01:35:57 +0000941ValueObjectSP
942Thread::GetReturnValueObject ()
943{
944 if (!m_completed_plan_stack.empty())
945 {
946 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
947 {
948 ValueObjectSP return_valobj_sp;
949 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
950 if (return_valobj_sp)
951 return return_valobj_sp;
952 }
953 }
954 return ValueObjectSP();
955}
956
Chris Lattner24943d22010-06-08 16:52:24 +0000957bool
958Thread::IsThreadPlanDone (ThreadPlan *plan)
959{
Chris Lattner24943d22010-06-08 16:52:24 +0000960 if (!m_completed_plan_stack.empty())
961 {
962 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
963 {
964 if (m_completed_plan_stack[i].get() == plan)
965 return true;
966 }
967 }
968 return false;
969}
970
971bool
972Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
973{
Chris Lattner24943d22010-06-08 16:52:24 +0000974 if (!m_discarded_plan_stack.empty())
975 {
976 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
977 {
978 if (m_discarded_plan_stack[i].get() == plan)
979 return true;
980 }
981 }
982 return false;
983}
984
985ThreadPlan *
986Thread::GetPreviousPlan (ThreadPlan *current_plan)
987{
988 if (current_plan == NULL)
989 return NULL;
990
991 int stack_size = m_completed_plan_stack.size();
992 for (int i = stack_size - 1; i > 0; i--)
993 {
994 if (current_plan == m_completed_plan_stack[i].get())
995 return m_completed_plan_stack[i-1].get();
996 }
997
998 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
999 {
Chris Lattner24943d22010-06-08 16:52:24 +00001000 if (m_plan_stack.size() > 0)
1001 return m_plan_stack.back().get();
1002 else
1003 return NULL;
1004 }
1005
1006 stack_size = m_plan_stack.size();
1007 for (int i = stack_size - 1; i > 0; i--)
1008 {
1009 if (current_plan == m_plan_stack[i].get())
1010 return m_plan_stack[i-1].get();
1011 }
1012 return NULL;
1013}
1014
1015void
1016Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1017{
1018 if (abort_other_plans)
1019 DiscardThreadPlans(true);
1020
1021 PushPlan (thread_plan_sp);
1022}
1023
Jim Ingham745ac7a2010-11-11 19:26:09 +00001024
1025void
1026Thread::EnableTracer (bool value, bool single_stepping)
1027{
1028 int stack_size = m_plan_stack.size();
1029 for (int i = 0; i < stack_size; i++)
1030 {
1031 if (m_plan_stack[i]->GetThreadPlanTracer())
1032 {
1033 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1034 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1035 }
1036 }
1037}
1038
1039void
1040Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1041{
1042 int stack_size = m_plan_stack.size();
1043 for (int i = 0; i < stack_size; i++)
1044 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1045}
1046
Chris Lattner24943d22010-06-08 16:52:24 +00001047void
Jim Inghamea9d4262010-11-05 19:25:48 +00001048Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1049{
Jim Ingham88e3de22012-05-03 21:19:36 +00001050 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1051}
1052
1053void
1054Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1055{
Greg Claytone005f2c2010-11-06 01:53:30 +00001056 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +00001057 if (log)
1058 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001059 log->Printf("Discarding thread plans for thread tid = 0x%4.4" PRIx64 ", up to %p", GetID(), up_to_plan_ptr);
Jim Inghamea9d4262010-11-05 19:25:48 +00001060 }
1061
1062 int stack_size = m_plan_stack.size();
1063
1064 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1065 // stack, and if so discard up to and including it.
1066
Jim Ingham88e3de22012-05-03 21:19:36 +00001067 if (up_to_plan_ptr == NULL)
Jim Inghamea9d4262010-11-05 19:25:48 +00001068 {
1069 for (int i = stack_size - 1; i > 0; i--)
1070 DiscardPlan();
1071 }
1072 else
1073 {
1074 bool found_it = false;
1075 for (int i = stack_size - 1; i > 0; i--)
1076 {
Jim Ingham88e3de22012-05-03 21:19:36 +00001077 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +00001078 found_it = true;
1079 }
1080 if (found_it)
1081 {
1082 bool last_one = false;
1083 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1084 {
Jim Ingham88e3de22012-05-03 21:19:36 +00001085 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +00001086 last_one = true;
1087 DiscardPlan();
1088 }
1089 }
1090 }
1091 return;
1092}
1093
1094void
Chris Lattner24943d22010-06-08 16:52:24 +00001095Thread::DiscardThreadPlans(bool force)
1096{
Greg Claytone005f2c2010-11-06 01:53:30 +00001097 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +00001098 if (log)
1099 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001100 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +00001101 }
1102
1103 if (force)
1104 {
1105 int stack_size = m_plan_stack.size();
1106 for (int i = stack_size - 1; i > 0; i--)
1107 {
1108 DiscardPlan();
1109 }
1110 return;
1111 }
1112
1113 while (1)
1114 {
1115
1116 int master_plan_idx;
Jim Inghamf1dbb712012-09-11 00:09:25 +00001117 bool discard = true;
Chris Lattner24943d22010-06-08 16:52:24 +00001118
1119 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1120 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1121 {
1122 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1123 {
1124 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1125 break;
1126 }
1127 }
1128
1129 if (discard)
1130 {
1131 // First pop all the dependent plans:
1132 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1133 {
1134
1135 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1136 // for the plan leaves it in a state that it is safe to pop the plan
1137 // with no more notice?
1138 DiscardPlan();
1139 }
1140
1141 // Now discard the master plan itself.
1142 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1143 // discard it's dependent plans, but not it...
1144 if (master_plan_idx > 0)
1145 {
1146 DiscardPlan();
1147 }
1148 }
1149 else
1150 {
1151 // If the master plan doesn't want to get discarded, then we're done.
1152 break;
1153 }
1154
1155 }
Chris Lattner24943d22010-06-08 16:52:24 +00001156}
1157
Jim Ingham2bcbaf62012-04-09 22:37:39 +00001158bool
1159Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1160{
1161 if (plan_ptr->IsBasePlan())
1162 return true;
1163 else if (m_plan_stack.size() == 0)
1164 return false;
1165 else
1166 return m_plan_stack[0].get() == plan_ptr;
1167}
1168
Chris Lattner24943d22010-06-08 16:52:24 +00001169ThreadPlan *
1170Thread::QueueFundamentalPlan (bool abort_other_plans)
1171{
1172 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1173 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1174 return thread_plan_sp.get();
1175}
1176
1177ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001178Thread::QueueThreadPlanForStepSingleInstruction
1179(
1180 bool step_over,
1181 bool abort_other_plans,
1182 bool stop_other_threads
1183)
Chris Lattner24943d22010-06-08 16:52:24 +00001184{
1185 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1186 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1187 return thread_plan_sp.get();
1188}
1189
1190ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001191Thread::QueueThreadPlanForStepRange
1192(
1193 bool abort_other_plans,
1194 StepType type,
1195 const AddressRange &range,
1196 const SymbolContext &addr_context,
1197 lldb::RunMode stop_other_threads,
1198 bool avoid_code_without_debug_info
1199)
Chris Lattner24943d22010-06-08 16:52:24 +00001200{
1201 ThreadPlanSP thread_plan_sp;
1202 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001203 {
1204 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1205 if (avoid_code_without_debug_info)
1206 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1207 else
1208 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1209 thread_plan_sp.reset (plan);
1210 }
Chris Lattner24943d22010-06-08 16:52:24 +00001211 else
1212 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1213
1214 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1215 return thread_plan_sp.get();
1216}
1217
1218
1219ThreadPlan *
1220Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1221{
1222 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1223 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1224 return thread_plan_sp.get();
1225}
1226
1227ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001228Thread::QueueThreadPlanForStepOut
1229(
1230 bool abort_other_plans,
1231 SymbolContext *addr_context,
1232 bool first_insn,
1233 bool stop_other_threads,
1234 Vote stop_vote,
1235 Vote run_vote,
1236 uint32_t frame_idx
1237)
Chris Lattner24943d22010-06-08 16:52:24 +00001238{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001239 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1240 addr_context,
1241 first_insn,
1242 stop_other_threads,
1243 stop_vote,
1244 run_vote,
1245 frame_idx));
Sean Callananf6d5fea2012-07-31 22:19:25 +00001246
1247 if (thread_plan_sp->ValidatePlan(NULL))
1248 {
1249 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1250 return thread_plan_sp.get();
1251 }
1252 else
1253 {
1254 return NULL;
1255 }
Chris Lattner24943d22010-06-08 16:52:24 +00001256}
1257
1258ThreadPlan *
Jim Ingham038fa8e2012-05-10 01:35:39 +00001259Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner24943d22010-06-08 16:52:24 +00001260{
Jim Ingham038fa8e2012-05-10 01:35:39 +00001261 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Inghamad382c52011-12-03 01:52:59 +00001262 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1263 return NULL;
1264
Chris Lattner24943d22010-06-08 16:52:24 +00001265 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1266 return thread_plan_sp.get();
1267}
1268
1269ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001270Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1271 Address& function,
1272 lldb::addr_t arg,
1273 bool stop_other_threads,
1274 bool discard_on_error)
1275{
Jim Ingham016ef882011-12-22 19:12:40 +00001276 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner24943d22010-06-08 16:52:24 +00001277 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1278 return thread_plan_sp.get();
1279}
1280
1281ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001282Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1283 Address &target_addr,
1284 bool stop_other_threads)
1285{
1286 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1287 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1288 return thread_plan_sp.get();
1289}
1290
1291ThreadPlan *
1292Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001293 lldb::addr_t *address_list,
1294 size_t num_addresses,
1295 bool stop_other_threads,
1296 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001297{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001298 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +00001299 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1300 return thread_plan_sp.get();
1301
1302}
1303
1304uint32_t
1305Thread::GetIndexID () const
1306{
1307 return m_index_id;
1308}
1309
1310void
1311Thread::DumpThreadPlans (lldb_private::Stream *s) const
1312{
1313 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +00001314 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +00001315 s->Indent();
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001316 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4" PRIx64 ", stack_size = %d\n", GetIndexID(), GetID(), stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +00001317 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +00001318 {
Chris Lattner24943d22010-06-08 16:52:24 +00001319 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +00001320 s->Indent();
1321 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +00001322 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +00001323 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +00001324 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +00001325 }
1326
Chris Lattner24943d22010-06-08 16:52:24 +00001327 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001328 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001329 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001330 s->Indent();
1331 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1332 for (i = stack_size - 1; i >= 0; i--)
1333 {
1334 s->IndentMore();
1335 s->Indent();
1336 s->Printf ("Element %d: ", i);
1337 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1338 s->EOL();
1339 s->IndentLess();
1340 }
Chris Lattner24943d22010-06-08 16:52:24 +00001341 }
1342
1343 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001344 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001345 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001346 s->Indent();
1347 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1348 for (i = stack_size - 1; i >= 0; i--)
1349 {
1350 s->IndentMore();
1351 s->Indent();
1352 s->Printf ("Element %d: ", i);
1353 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1354 s->EOL();
1355 s->IndentLess();
1356 }
Chris Lattner24943d22010-06-08 16:52:24 +00001357 }
1358
1359}
1360
Greg Clayton289afcb2012-02-18 05:35:26 +00001361TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001362Thread::CalculateTarget ()
1363{
Greg Claytonf4124de2012-02-21 00:09:25 +00001364 TargetSP target_sp;
1365 ProcessSP process_sp(GetProcess());
1366 if (process_sp)
1367 target_sp = process_sp->CalculateTarget();
1368 return target_sp;
1369
Chris Lattner24943d22010-06-08 16:52:24 +00001370}
1371
Greg Clayton289afcb2012-02-18 05:35:26 +00001372ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001373Thread::CalculateProcess ()
1374{
Greg Claytonf4124de2012-02-21 00:09:25 +00001375 return GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001376}
1377
Greg Clayton289afcb2012-02-18 05:35:26 +00001378ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001379Thread::CalculateThread ()
1380{
Greg Clayton289afcb2012-02-18 05:35:26 +00001381 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001382}
1383
Greg Clayton289afcb2012-02-18 05:35:26 +00001384StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001385Thread::CalculateStackFrame ()
1386{
Greg Clayton289afcb2012-02-18 05:35:26 +00001387 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001388}
1389
1390void
Greg Claytona830adb2010-10-04 01:05:56 +00001391Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001392{
Greg Claytonf4124de2012-02-21 00:09:25 +00001393 exe_ctx.SetContext (shared_from_this());
Chris Lattner24943d22010-06-08 16:52:24 +00001394}
1395
Greg Clayton782b9cc2010-08-25 00:35:26 +00001396
Greg Clayton2450cb12012-03-29 01:41:38 +00001397StackFrameListSP
Greg Clayton782b9cc2010-08-25 00:35:26 +00001398Thread::GetStackFrameList ()
1399{
Greg Clayton2450cb12012-03-29 01:41:38 +00001400 StackFrameListSP frame_list_sp;
1401 Mutex::Locker locker(m_frame_mutex);
1402 if (m_curr_frames_sp)
1403 {
1404 frame_list_sp = m_curr_frames_sp;
1405 }
1406 else
1407 {
1408 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1409 m_curr_frames_sp = frame_list_sp;
1410 }
1411 return frame_list_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +00001412}
1413
Greg Clayton782b9cc2010-08-25 00:35:26 +00001414void
1415Thread::ClearStackFrames ()
1416{
Greg Clayton2450cb12012-03-29 01:41:38 +00001417 Mutex::Locker locker(m_frame_mutex);
1418
Jim Inghambf97d742012-02-29 03:40:22 +00001419 // Only store away the old "reference" StackFrameList if we got all its frames:
1420 // FIXME: At some point we can try to splice in the frames we have fetched into
1421 // the new frame as we make it, but let's not try that now.
1422 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Claytonc51ffbf2011-08-12 21:40:01 +00001423 m_prev_frames_sp.swap (m_curr_frames_sp);
1424 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +00001425}
1426
1427lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001428Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1429{
Greg Clayton2450cb12012-03-29 01:41:38 +00001430 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001431}
1432
Jim Inghama17a81a2012-09-12 00:40:39 +00001433
1434Error
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001435Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Inghama17a81a2012-09-12 00:40:39 +00001436{
1437 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1438 Error return_error;
1439
1440 if (!frame_sp)
1441 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001442 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Inghama17a81a2012-09-12 00:40:39 +00001443 }
1444
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001445 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Inghama17a81a2012-09-12 00:40:39 +00001446}
1447
1448Error
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001449Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Inghama17a81a2012-09-12 00:40:39 +00001450{
1451 Error return_error;
1452
1453 if (!frame_sp)
1454 {
1455 return_error.SetErrorString("Can't return to a null frame.");
1456 return return_error;
1457 }
1458
1459 Thread *thread = frame_sp->GetThread().get();
Jim Inghamf59388a2012-09-14 02:14:15 +00001460 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1461 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Inghama17a81a2012-09-12 00:40:39 +00001462
1463 if (return_value_sp)
Jim Ingham46657452012-09-27 01:15:29 +00001464 {
Jim Inghama17a81a2012-09-12 00:40:39 +00001465 lldb::ABISP abi = thread->GetProcess()->GetABI();
1466 if (!abi)
1467 {
1468 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham6f01c932012-10-12 17:34:26 +00001469 return return_error;
Jim Inghama17a81a2012-09-12 00:40:39 +00001470 }
Jim Ingham46657452012-09-27 01:15:29 +00001471 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1472
1473 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1474 // Turn that back on when that works.
1475 if (0 && sc.function != NULL)
1476 {
1477 Type *function_type = sc.function->GetType();
1478 if (function_type)
1479 {
1480 clang_type_t return_type = sc.function->GetReturnClangType();
1481 if (return_type)
1482 {
1483 ClangASTType ast_type (function_type->GetClangAST(), return_type);
1484 StreamString s;
1485 ast_type.DumpTypeDescription(&s);
1486 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type);
1487 if (cast_value_sp)
1488 {
1489 cast_value_sp->SetFormat(eFormatHex);
1490 return_value_sp = cast_value_sp;
1491 }
1492 }
1493 }
1494 }
1495
Jim Inghamf59388a2012-09-14 02:14:15 +00001496 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Inghama17a81a2012-09-12 00:40:39 +00001497 if (!return_error.Success())
1498 return return_error;
1499 }
1500
1501 // Now write the return registers for the chosen frame:
Jim Inghamf59388a2012-09-14 02:14:15 +00001502 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
1503 // cook their data
1504 bool copy_success = thread->GetStackFrameAtIndex(0)->GetRegisterContext()->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1505 if (copy_success)
Jim Inghama17a81a2012-09-12 00:40:39 +00001506 {
1507 thread->DiscardThreadPlans(true);
Jim Inghamf59388a2012-09-14 02:14:15 +00001508 thread->ClearStackFrames();
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001509 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1510 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
Jim Inghama17a81a2012-09-12 00:40:39 +00001511 return return_error;
1512 }
1513 else
1514 {
1515 return_error.SetErrorString("Could not reset register values.");
1516 return return_error;
Jim Inghama17a81a2012-09-12 00:40:39 +00001517 }
1518}
1519
Chris Lattner24943d22010-06-08 16:52:24 +00001520void
Greg Claytona830adb2010-10-04 01:05:56 +00001521Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001522{
Greg Claytonf4124de2012-02-21 00:09:25 +00001523 ExecutionContext exe_ctx (shared_from_this());
1524 Process *process = exe_ctx.GetProcessPtr();
1525 if (process == NULL)
1526 return;
1527
Greg Clayton567e7f32011-09-22 04:58:26 +00001528 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +00001529 SymbolContext frame_sc;
Greg Claytona830adb2010-10-04 01:05:56 +00001530 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +00001531 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001532 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001533 if (frame_sp)
1534 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001535 exe_ctx.SetFrameSP(frame_sp);
1536 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001537 }
1538 }
1539
Greg Claytonf4124de2012-02-21 00:09:25 +00001540 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Claytona830adb2010-10-04 01:05:56 +00001541 assert (thread_format);
1542 const char *end = NULL;
1543 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001544 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001545 &exe_ctx,
1546 NULL,
1547 strm,
1548 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001549}
1550
Greg Clayton990de7b2010-11-18 23:32:35 +00001551void
Caroline Tice2a456812011-03-10 22:14:10 +00001552Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001553{
Greg Clayton990de7b2010-11-18 23:32:35 +00001554}
Jim Ingham20594b12010-09-08 03:14:33 +00001555
Greg Clayton990de7b2010-11-18 23:32:35 +00001556void
Caroline Tice2a456812011-03-10 22:14:10 +00001557Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001558{
Greg Clayton990de7b2010-11-18 23:32:35 +00001559}
Jim Ingham20594b12010-09-08 03:14:33 +00001560
Jim Inghamccd584d2010-09-23 17:40:12 +00001561lldb::StackFrameSP
1562Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1563{
Greg Clayton2450cb12012-03-29 01:41:38 +00001564 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghamccd584d2010-09-23 17:40:12 +00001565}
Caroline Tice7826c882010-10-26 03:11:13 +00001566
1567const char *
1568Thread::StopReasonAsCString (lldb::StopReason reason)
1569{
1570 switch (reason)
1571 {
1572 case eStopReasonInvalid: return "invalid";
1573 case eStopReasonNone: return "none";
1574 case eStopReasonTrace: return "trace";
1575 case eStopReasonBreakpoint: return "breakpoint";
1576 case eStopReasonWatchpoint: return "watchpoint";
1577 case eStopReasonSignal: return "signal";
1578 case eStopReasonException: return "exception";
Greg Clayton0bce9a22012-12-05 00:16:59 +00001579 case eStopReasonExec: return "exec";
Caroline Tice7826c882010-10-26 03:11:13 +00001580 case eStopReasonPlanComplete: return "plan complete";
1581 }
1582
1583
1584 static char unknown_state_string[64];
1585 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1586 return unknown_state_string;
1587}
1588
1589const char *
1590Thread::RunModeAsCString (lldb::RunMode mode)
1591{
1592 switch (mode)
1593 {
1594 case eOnlyThisThread: return "only this thread";
1595 case eAllThreads: return "all threads";
1596 case eOnlyDuringStepping: return "only during stepping";
1597 }
1598
1599 static char unknown_state_string[64];
1600 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1601 return unknown_state_string;
1602}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001603
Greg Claytonabe0fed2011-04-18 08:33:37 +00001604size_t
1605Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1606{
Greg Claytonf4124de2012-02-21 00:09:25 +00001607 ExecutionContext exe_ctx (shared_from_this());
1608 Target *target = exe_ctx.GetTargetPtr();
1609 Process *process = exe_ctx.GetProcessPtr();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001610 size_t num_frames_shown = 0;
1611 strm.Indent();
Greg Claytonf4124de2012-02-21 00:09:25 +00001612 bool is_selected = false;
1613 if (process)
1614 {
1615 if (process->GetThreadList().GetSelectedThread().get() == this)
1616 is_selected = true;
1617 }
1618 strm.Printf("%c ", is_selected ? '*' : ' ');
1619 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001620 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001621 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001622 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001623 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001624 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1625 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1626 {
1627 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1628 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001629 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001630 }
1631
1632 DumpUsingSettingsFormat (strm, start_frame);
1633
1634 if (num_frames > 0)
1635 {
1636 strm.IndentMore();
1637
1638 const bool show_frame_info = true;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001639 strm.IndentMore ();
Greg Clayton2450cb12012-03-29 01:41:38 +00001640 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1641 start_frame,
1642 num_frames,
1643 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001644 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001645 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001646 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001647 }
1648 return num_frames_shown;
1649}
1650
1651size_t
1652Thread::GetStackFrameStatus (Stream& strm,
1653 uint32_t first_frame,
1654 uint32_t num_frames,
1655 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001656 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +00001657{
Greg Clayton2450cb12012-03-29 01:41:38 +00001658 return GetStackFrameList()->GetStatus (strm,
1659 first_frame,
1660 num_frames,
1661 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001662 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001663}
1664
Peter Collingbournee426d852011-06-03 20:40:54 +00001665bool
1666Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1667{
1668 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1669 if (frame_sp)
1670 {
1671 checkpoint.SetStackID(frame_sp->GetStackID());
1672 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1673 }
1674 return false;
1675}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001676
Peter Collingbournee426d852011-06-03 20:40:54 +00001677bool
1678Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1679{
Jim Inghama17a81a2012-09-12 00:40:39 +00001680 return ResetFrameZeroRegisters (checkpoint.GetData());
1681}
1682
1683bool
1684Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1685{
Peter Collingbournee426d852011-06-03 20:40:54 +00001686 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1687 if (frame_sp)
1688 {
Jim Inghama17a81a2012-09-12 00:40:39 +00001689 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (register_data_sp);
Peter Collingbournee426d852011-06-03 20:40:54 +00001690
1691 // Clear out all stack frames as our world just changed.
1692 ClearStackFrames();
1693 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
Jason Molenda9c5d3af2012-11-14 04:26:02 +00001694 if (m_unwinder_ap.get())
1695 m_unwinder_ap->Clear();
Peter Collingbournee426d852011-06-03 20:40:54 +00001696
1697 return ret;
1698 }
1699 return false;
1700}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001701
Greg Clayton37f962e2011-08-22 02:49:39 +00001702Unwind *
1703Thread::GetUnwinder ()
1704{
1705 if (m_unwinder_ap.get() == NULL)
1706 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001707 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton37f962e2011-08-22 02:49:39 +00001708 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1709 switch (machine)
1710 {
1711 case llvm::Triple::x86_64:
1712 case llvm::Triple::x86:
1713 case llvm::Triple::arm:
1714 case llvm::Triple::thumb:
1715 m_unwinder_ap.reset (new UnwindLLDB (*this));
1716 break;
1717
1718 default:
1719 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1720 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1721 break;
1722 }
1723 }
1724 return m_unwinder_ap.get();
1725}
1726
1727
Greg Claytoncf5927e2012-05-18 02:38:05 +00001728void
1729Thread::Flush ()
1730{
1731 ClearStackFrames ();
1732 m_reg_context_sp.reset();
1733}
Jim Ingham6bc24c12012-10-16 00:09:33 +00001734
Filipe Cabecinhasb2921e22012-11-20 00:11:13 +00001735bool
Jim Ingham6bc24c12012-10-16 00:09:33 +00001736Thread::IsStillAtLastBreakpointHit ()
1737{
1738 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
1739 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
1740 // multithreaded programs.
1741 if (m_actual_stop_info_sp) {
1742 StopReason stop_reason = m_actual_stop_info_sp->GetStopReason();
1743 if (stop_reason == lldb::eStopReasonBreakpoint) {
1744 uint64_t value = m_actual_stop_info_sp->GetValue();
1745 lldb::addr_t pc = GetRegisterContext()->GetPC();
1746 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1747 if (bp_site_sp && value == bp_site_sp->GetID())
1748 return true;
1749 }
1750 }
1751 return false;
1752}