blob: 4750a4dc910ccbf11b42f2ef731e7858f8f896e7 [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"
16#include "lldb/Core/Stream.h"
17#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000018#include "lldb/Core/RegularExpression.h"
Greg Clayton7260f622011-04-18 08:33:37 +000019#include "lldb/Host/Host.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000020#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Target/DynamicLoader.h"
22#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000023#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Target/Process.h"
25#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000026#include "lldb/Target/StopInfo.h"
Greg Clayton896dff62010-07-23 16:45:51 +000027#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Target/Thread.h"
29#include "lldb/Target/ThreadPlan.h"
30#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-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 Ingham1b54c882010-06-16 02:00:15 +000040#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000041#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000042#include "Plugins/Process/Utility/UnwindLLDB.h"
43#include "UnwindMacOSXFrameBackchain.h"
44
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045
46using namespace lldb;
47using namespace lldb_private;
48
Greg Clayton67cc0632012-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 Clayton6920b522012-08-22 18:39:03 +000085 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-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 Ingham4f465cf2012-10-10 18:32:14 +0000141//------------------------------------------------------------------
142// Thread Event Data
143//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000144
Jim Ingham4f465cf2012-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 Clayton67cc0632012-08-22 17:17:09 +0000241 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000243 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
244 m_process_wp (process.shared_from_this()),
Greg Claytonf4b47e12010-08-04 01:40:35 +0000245 m_actual_stop_info_sp (),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000246 m_index_id (process.GetNextThreadIndexID ()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000249 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000252 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000253 m_curr_frames_sp (),
254 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000256 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000257 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000258 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000259 m_destroy_called (false),
260 m_thread_stop_reason_stop_id (0)
Jim Ingham87c11912010-08-12 02:14:28 +0000261
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000263 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000265 log->Printf ("%p Thread::Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266
Jim Ingham4f465cf2012-10-10 18:32:14 +0000267 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268 QueueFundamentalPlan(true);
269}
270
271
272Thread::~Thread()
273{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000274 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000276 log->Printf ("%p Thread::~Thread(tid = 0x%4.4" PRIx64 ")", this, GetID());
Jim Ingham773d9812010-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 Clayton35a4cc52012-10-29 20:52:08 +0000284 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000285 m_plan_stack.clear();
286 m_discarded_plan_stack.clear();
287 m_completed_plan_stack.clear();
Jim Inghamd8ba4642012-04-10 00:44:25 +0000288 m_actual_stop_info_sp.reset();
Greg Clayton35a4cc52012-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 Lattner30fdc8d2010-06-08 16:52:24 +0000294}
295
Jim Ingham4f465cf2012-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 Inghamb15bfc72010-10-20 00:39:53 +0000328lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000329Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000330{
Jim Inghamb15bfc72010-10-20 00:39:53 +0000331 ThreadPlanSP plan_sp (GetCompletedPlan());
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000332 if (plan_sp && plan_sp->PlanSucceeded())
Jim Ingham73ca05a2011-12-17 01:35:57 +0000333 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Jim Inghamb15bfc72010-10-20 00:39:53 +0000334 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000335 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000336 ProcessSP process_sp (GetProcess());
337 if (process_sp
338 && m_actual_stop_info_sp
Jim Ingham79c35da2011-08-09 00:32:52 +0000339 && m_actual_stop_info_sp->IsValid()
Greg Clayton1ac04c32012-02-21 00:09:25 +0000340 && m_thread_stop_reason_stop_id == process_sp->GetStopID())
Jim Ingham79c35da2011-08-09 00:32:52 +0000341 return m_actual_stop_info_sp;
342 else
343 return GetPrivateStopReason ();
344 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345}
346
Greg Clayton97d5cf02012-09-25 02:40:06 +0000347lldb::StopReason
348Thread::GetStopReason()
349{
350 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
351 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000352 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000353 return eStopReasonNone;
354}
355
356
357
Jim Ingham77787032011-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 Ingham79c35da2011-08-09 00:32:52 +0000362 if (m_actual_stop_info_sp)
363 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Clayton1ac04c32012-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 Ingham77787032011-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 Lattner30fdc8d2010-06-08 16:52:24 +0000379bool
380Thread::ThreadStoppedForAReason (void)
381{
Jim Inghamf94e1792012-08-11 00:35:26 +0000382 return (bool) GetPrivateStopReason ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383}
384
Jim Ingham77787032011-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 Clayton1ac04c32012-02-21 00:09:25 +0000392 ProcessSP process_sp (GetProcess());
393 if (process_sp)
394 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000395 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
396
Jim Ingham77787032011-01-20 02:03:18 +0000397 return true;
398}
399
400bool
Jim Ingham8559a352012-11-26 23:52:18 +0000401Thread::RestoreRegisterStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
Jim Ingham77787032011-01-20 02:03:18 +0000402{
403 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham8559a352012-11-26 23:52:18 +0000404 return true;
405}
406
407bool
408Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
409{
Jim Ingham0c270682011-01-25 02:47:23 +0000410 if (saved_state.stop_info_sp)
411 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000412 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000413 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000414 return true;
415}
416
Chris Lattner30fdc8d2010-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 Ingham5d88a062012-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 Lattner30fdc8d2010-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 Inghamb01e7422010-06-19 04:45:32 +0000464
465 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466
Jim Inghamb01e7422010-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 Lattner30fdc8d2010-06-08 16:52:24 +0000471 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000472 ThreadPlanSP step_bp_plan_sp;
473 step_bp_plan->SetPrivate (true);
474
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475 if (GetCurrentPlan()->RunState() != eStateStepping)
476 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000477 step_bp_plan->SetAutoContinue(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000479 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner30fdc8d2010-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 Clayton97d5cf02012-09-25 02:40:06 +0000494 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-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 Clayton1ac04c32012-02-21 00:09:25 +0000500 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham92087d82012-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 Lattner30fdc8d2010-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 Ingham513c6bb2012-09-01 01:02:41 +0000514 bool need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515
516 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
517 {
518 plan_ptr->WillResume (resume_state, false);
519 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000520
Jim Ingham513c6bb2012-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 Ingham5d88a062012-10-16 00:09:33 +0000524 if (need_to_resume && resume_state != eStateSuspended)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000525 {
526 m_actual_stop_info_sp.reset();
527 }
528
529 return need_to_resume;
Chris Lattner30fdc8d2010-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 Clayton2d4edfb2010-11-06 01:53:30 +0000544 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham92087d82012-01-31 23:09:20 +0000545
546 if (GetResumeState () == eStateSuspended)
547 {
548 if (log)
Daniel Malead01b2952012-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 Ingham92087d82012-01-31 23:09:20 +0000550 __FUNCTION__,
551 GetID ());
Daniel Malead01b2952012-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 Ingham92087d82012-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 Malead01b2952012-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 Ingham92087d82012-01-31 23:09:20 +0000563 __FUNCTION__,
564 GetID ());
Daniel Malead01b2952012-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 Ingham92087d82012-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 Malead01b2952012-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 Ingham92087d82012-01-31 23:09:20 +0000576 __FUNCTION__,
577 GetID (),
578 GetRegisterContext()->GetPC());
579 return false;
580 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000581
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582 if (log)
583 {
Daniel Malead01b2952012-11-29 21:49:15 +0000584 log->Printf ("Thread::%s for tid = 0x%4.4" PRIx64 ", pc = 0x%16.16" PRIx64,
Jim Ingham92087d82012-01-31 23:09:20 +0000585 __FUNCTION__,
586 GetID (),
587 GetRegisterContext()->GetPC());
Jim Ingham10c4b242011-10-15 00:23:43 +0000588 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000590 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000592 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000593 }
Jim Ingham06e827c2010-11-11 19:26:09 +0000594
595 // The top most plan always gets to do the trace log...
596 current_plan->DoTraceLog ();
Jim Ingham6d66ce62012-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 Lattner30fdc8d2010-06-08 16:52:24 +0000608
Jim Inghambad39e42012-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 Ingham25f66702011-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 Inghamcf274f92012-04-09 22:37:39 +0000632 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-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 Ingham031177e2012-05-11 23:49:49 +0000646 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000647 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Ingham25f66702011-12-03 01:52:59 +0000648
649 do
650 {
651 if (should_stop)
652 current_plan->WillStop();
653 PopPlan();
654 }
Jim Ingham7ba6e992012-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 Ingham25f66702011-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 Lattner30fdc8d2010-06-08 16:52:24 +0000674 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000675 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Ingham0f16e732011-02-08 05:20:59 +0000676
Jim Ingham10c4b242011-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 Ingham0f16e732011-02-08 05:20:59 +0000680 // We're starting from the base plan, so just let it decide;
681 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000683 should_stop = current_plan->ShouldStop (event_ptr);
684 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000685 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-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 Lattner30fdc8d2010-06-08 16:52:24 +0000692 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000693 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694 break;
Jim Ingham0f16e732011-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 Lattner30fdc8d2010-06-08 16:52:24 +0000723 }
724 else
725 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000726 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 }
729 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000730
Jim Inghamb01e7422010-06-19 04:45:32 +0000731 if (over_ride_stop)
732 should_stop = false;
Jim Ingham64e7ead2012-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 Lattner30fdc8d2010-06-08 16:52:24 +0000757 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758
Jim Ingham10c4b242011-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 Lattner30fdc8d2010-06-08 16:52:24 +0000767 return should_stop;
768}
769
770Vote
771Thread::ShouldReportStop (Event* event_ptr)
772{
773 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +0000774 StateType temp_thread_state = GetTemporaryResumeState();
775
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000776 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000777
778 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
779 {
780 if (log)
Daniel Malead01b2952012-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 Lattner30fdc8d2010-06-08 16:52:24 +0000782 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000783 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784
Jim Ingham92087d82012-01-31 23:09:20 +0000785 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
786 {
787 if (log)
Daniel Malead01b2952012-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 Ingham92087d82012-01-31 23:09:20 +0000789 return eVoteNoOpinion;
790 }
791
792 if (!ThreadStoppedForAReason())
793 {
794 if (log)
Daniel Malead01b2952012-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 Ingham92087d82012-01-31 23:09:20 +0000796 return eVoteNoOpinion;
797 }
798
Chris Lattner30fdc8d2010-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 Clayton2cad65a2010-09-03 17:10:42 +0000802 if (log)
Daniel Malead01b2952012-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 Lattner30fdc8d2010-06-08 16:52:24 +0000804 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
805 }
806 else
Greg Clayton2cad65a2010-09-03 17:10:42 +0000807 {
808 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000809 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4" PRIx64 ": returning vote for current plan\n", GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000811 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812}
813
814Vote
815Thread::ShouldReportRun (Event* event_ptr)
816{
817 StateType thread_state = GetResumeState ();
Jim Ingham444586b2011-01-24 06:34:17 +0000818
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819 if (thread_state == eStateSuspended
820 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +0000821 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000822 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +0000823 }
824
825 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-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 Ingham444586b2011-01-24 06:34:17 +0000829 if (log)
Daniel Malead01b2952012-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 Ingham444586b2011-01-24 06:34:17 +0000831 GetIndexID(),
832 GetID(),
833 m_completed_plan_stack.back()->GetName());
834
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
836 }
837 else
Jim Ingham444586b2011-01-24 06:34:17 +0000838 {
839 if (log)
Daniel Malead01b2952012-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 Ingham444586b2011-01-24 06:34:17 +0000841 GetIndexID(),
842 GetID(),
843 GetCurrentPlan()->GetName());
844
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000845 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +0000846 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847}
848
Jim Ingham1b54c882010-06-16 02:00:15 +0000849bool
850Thread::MatchesSpec (const ThreadSpec *spec)
851{
852 if (spec == NULL)
853 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +0000854
Jim Ingham3d902922012-03-07 22:03:04 +0000855 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +0000856}
857
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858void
859Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
860{
861 if (thread_plan_sp)
862 {
Jim Ingham06e827c2010-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 Inghamb15bfc72010-10-20 00:39:53 +0000866 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham06e827c2010-11-11 19:26:09 +0000867
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868 thread_plan_sp->DidPush();
869
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000870 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871 if (log)
872 {
873 StreamString s;
874 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Daniel Malead01b2952012-11-29 21:49:15 +0000875 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4" PRIx64 ".",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876 s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +0000877 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878 }
879 }
880}
881
882void
883Thread::PopPlan ()
884{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000885 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000887 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000888 return;
889 else
890 {
891 ThreadPlanSP &plan = m_plan_stack.back();
892 if (log)
893 {
Daniel Malead01b2952012-11-29 21:49:15 +0000894 log->Printf("Popping plan: \"%s\", tid = 0x%4.4" PRIx64 ".", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-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 Inghame1471232012-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 Lattner30fdc8d2010-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 Ingham73ca05a2011-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 Lattner30fdc8d2010-06-08 16:52:24 +0000957bool
958Thread::IsThreadPlanDone (ThreadPlan *plan)
959{
Chris Lattner30fdc8d2010-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 Lattner30fdc8d2010-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 Lattner30fdc8d2010-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 Ingham06e827c2010-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 Lattner30fdc8d2010-06-08 16:52:24 +00001047void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001048Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1049{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001050 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1051}
1052
1053void
1054Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1055{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00001056 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001057 if (log)
1058 {
Daniel Malead01b2952012-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 Ingham399f1ca2010-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 Ingham64e7ead2012-05-03 21:19:36 +00001067 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-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 Ingham64e7ead2012-05-03 21:19:36 +00001077 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-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 Ingham64e7ead2012-05-03 21:19:36 +00001085 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001086 last_one = true;
1087 DiscardPlan();
1088 }
1089 }
1090 }
1091 return;
1092}
1093
1094void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001095Thread::DiscardThreadPlans(bool force)
1096{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00001097 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001098 if (log)
1099 {
Daniel Malead01b2952012-11-29 21:49:15 +00001100 log->Printf("Discarding thread plans for thread (tid = 0x%4.4" PRIx64 ", force %d)", GetID(), force);
Chris Lattner30fdc8d2010-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 Inghama39ad072012-09-11 00:09:25 +00001117 bool discard = true;
Chris Lattner30fdc8d2010-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 Lattner30fdc8d2010-06-08 16:52:24 +00001156}
1157
Jim Inghamcf274f92012-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 Lattner30fdc8d2010-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 Clayton481cef22011-01-21 06:11:58 +00001178Thread::QueueThreadPlanForStepSingleInstruction
1179(
1180 bool step_over,
1181 bool abort_other_plans,
1182 bool stop_other_threads
1183)
Chris Lattner30fdc8d2010-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 *
Jim Inghamc6276822012-12-12 19:58:40 +00001191Thread::QueueThreadPlanForStepOverRange
Greg Clayton474966a2010-06-12 18:59:55 +00001192(
1193 bool abort_other_plans,
Greg Clayton474966a2010-06-12 18:59:55 +00001194 const AddressRange &range,
Jim Inghamc6276822012-12-12 19:58:40 +00001195 const SymbolContext &addr_context,
1196 lldb::RunMode stop_other_threads
1197)
1198{
1199 ThreadPlanSP thread_plan_sp;
1200 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1201
1202 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1203 return thread_plan_sp.get();
1204}
1205
1206ThreadPlan *
1207Thread::QueueThreadPlanForStepInRange
1208(
1209 bool abort_other_plans,
1210 const AddressRange &range,
1211 const SymbolContext &addr_context,
1212 const char *step_in_target,
Greg Clayton474966a2010-06-12 18:59:55 +00001213 lldb::RunMode stop_other_threads,
1214 bool avoid_code_without_debug_info
1215)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216{
1217 ThreadPlanSP thread_plan_sp;
Jim Inghamc6276822012-12-12 19:58:40 +00001218 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1219 if (avoid_code_without_debug_info)
1220 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221 else
Jim Inghamc6276822012-12-12 19:58:40 +00001222 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1223 if (step_in_target)
1224 plan->SetStepInTarget(step_in_target);
1225 thread_plan_sp.reset (plan);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226
1227 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1228 return thread_plan_sp.get();
1229}
1230
1231
1232ThreadPlan *
1233Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1234{
1235 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1236 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1237 return thread_plan_sp.get();
1238}
1239
1240ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001241Thread::QueueThreadPlanForStepOut
1242(
1243 bool abort_other_plans,
1244 SymbolContext *addr_context,
1245 bool first_insn,
1246 bool stop_other_threads,
1247 Vote stop_vote,
1248 Vote run_vote,
1249 uint32_t frame_idx
1250)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251{
Greg Clayton481cef22011-01-21 06:11:58 +00001252 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1253 addr_context,
1254 first_insn,
1255 stop_other_threads,
1256 stop_vote,
1257 run_vote,
1258 frame_idx));
Sean Callanan708709c2012-07-31 22:19:25 +00001259
1260 if (thread_plan_sp->ValidatePlan(NULL))
1261 {
1262 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1263 return thread_plan_sp.get();
1264 }
1265 else
1266 {
1267 return NULL;
1268 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269}
1270
1271ThreadPlan *
Jim Ingham18de2fd2012-05-10 01:35:39 +00001272Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001274 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001275 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1276 return NULL;
1277
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001278 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1279 return thread_plan_sp.get();
1280}
1281
1282ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1284 Address& function,
1285 lldb::addr_t arg,
1286 bool stop_other_threads,
1287 bool discard_on_error)
1288{
Jim Inghamef651602011-12-22 19:12:40 +00001289 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001290 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1291 return thread_plan_sp.get();
1292}
1293
1294ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1296 Address &target_addr,
1297 bool stop_other_threads)
1298{
1299 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1300 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1301 return thread_plan_sp.get();
1302}
1303
1304ThreadPlan *
1305Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001306 lldb::addr_t *address_list,
1307 size_t num_addresses,
1308 bool stop_other_threads,
1309 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001310{
Greg Clayton481cef22011-01-21 06:11:58 +00001311 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1313 return thread_plan_sp.get();
1314
1315}
1316
1317uint32_t
1318Thread::GetIndexID () const
1319{
1320 return m_index_id;
1321}
1322
1323void
1324Thread::DumpThreadPlans (lldb_private::Stream *s) const
1325{
1326 uint32_t stack_size = m_plan_stack.size();
Greg Clayton1346f7e2010-09-03 22:45:01 +00001327 int i;
Jim Ingham10c4b242011-10-15 00:23:43 +00001328 s->Indent();
Daniel Malead01b2952012-11-29 21:49:15 +00001329 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 +00001330 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001332 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001333 s->Indent();
1334 s->Printf ("Element %d: ", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001335 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001336 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001337 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001338 }
1339
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001340 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001341 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001342 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001343 s->Indent();
1344 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1345 for (i = stack_size - 1; i >= 0; i--)
1346 {
1347 s->IndentMore();
1348 s->Indent();
1349 s->Printf ("Element %d: ", i);
1350 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1351 s->EOL();
1352 s->IndentLess();
1353 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354 }
1355
1356 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001357 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001359 s->Indent();
1360 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1361 for (i = stack_size - 1; i >= 0; i--)
1362 {
1363 s->IndentMore();
1364 s->Indent();
1365 s->Printf ("Element %d: ", i);
1366 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1367 s->EOL();
1368 s->IndentLess();
1369 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001370 }
1371
1372}
1373
Greg Claytond9e416c2012-02-18 05:35:26 +00001374TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001375Thread::CalculateTarget ()
1376{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001377 TargetSP target_sp;
1378 ProcessSP process_sp(GetProcess());
1379 if (process_sp)
1380 target_sp = process_sp->CalculateTarget();
1381 return target_sp;
1382
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001383}
1384
Greg Claytond9e416c2012-02-18 05:35:26 +00001385ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001386Thread::CalculateProcess ()
1387{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001388 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389}
1390
Greg Claytond9e416c2012-02-18 05:35:26 +00001391ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001392Thread::CalculateThread ()
1393{
Greg Claytond9e416c2012-02-18 05:35:26 +00001394 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395}
1396
Greg Claytond9e416c2012-02-18 05:35:26 +00001397StackFrameSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001398Thread::CalculateStackFrame ()
1399{
Greg Claytond9e416c2012-02-18 05:35:26 +00001400 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001401}
1402
1403void
Greg Clayton0603aa92010-10-04 01:05:56 +00001404Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001406 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407}
1408
Greg Clayton12daf9462010-08-25 00:35:26 +00001409
Greg Clayton39d0ab32012-03-29 01:41:38 +00001410StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001411Thread::GetStackFrameList ()
1412{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001413 StackFrameListSP frame_list_sp;
1414 Mutex::Locker locker(m_frame_mutex);
1415 if (m_curr_frames_sp)
1416 {
1417 frame_list_sp = m_curr_frames_sp;
1418 }
1419 else
1420 {
1421 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1422 m_curr_frames_sp = frame_list_sp;
1423 }
1424 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001425}
1426
Greg Clayton12daf9462010-08-25 00:35:26 +00001427void
1428Thread::ClearStackFrames ()
1429{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001430 Mutex::Locker locker(m_frame_mutex);
1431
Jim Inghamb0c72a52012-02-29 03:40:22 +00001432 // Only store away the old "reference" StackFrameList if we got all its frames:
1433 // FIXME: At some point we can try to splice in the frames we have fetched into
1434 // the new frame as we make it, but let's not try that now.
1435 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001436 m_prev_frames_sp.swap (m_curr_frames_sp);
1437 m_curr_frames_sp.reset();
Jim Ingham87c11912010-08-12 02:14:28 +00001438}
1439
1440lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001441Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1442{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001443 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001444}
1445
Jim Ingham44137582012-09-12 00:40:39 +00001446
1447Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001448Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001449{
1450 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1451 Error return_error;
1452
1453 if (!frame_sp)
1454 {
Daniel Malead01b2952012-11-29 21:49:15 +00001455 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%" PRIx64 ".", frame_idx, GetID());
Jim Ingham44137582012-09-12 00:40:39 +00001456 }
1457
Jim Ingham4f465cf2012-10-10 18:32:14 +00001458 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001459}
1460
1461Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001462Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001463{
1464 Error return_error;
1465
1466 if (!frame_sp)
1467 {
1468 return_error.SetErrorString("Can't return to a null frame.");
1469 return return_error;
1470 }
1471
1472 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001473 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1474 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001475
1476 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001477 {
Jim Ingham44137582012-09-12 00:40:39 +00001478 lldb::ABISP abi = thread->GetProcess()->GetABI();
1479 if (!abi)
1480 {
1481 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001482 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001483 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001484 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1485
1486 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1487 // Turn that back on when that works.
1488 if (0 && sc.function != NULL)
1489 {
1490 Type *function_type = sc.function->GetType();
1491 if (function_type)
1492 {
1493 clang_type_t return_type = sc.function->GetReturnClangType();
1494 if (return_type)
1495 {
1496 ClangASTType ast_type (function_type->GetClangAST(), return_type);
1497 StreamString s;
1498 ast_type.DumpTypeDescription(&s);
1499 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type);
1500 if (cast_value_sp)
1501 {
1502 cast_value_sp->SetFormat(eFormatHex);
1503 return_value_sp = cast_value_sp;
1504 }
1505 }
1506 }
1507 }
1508
Jim Inghamcb640dd2012-09-14 02:14:15 +00001509 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001510 if (!return_error.Success())
1511 return return_error;
1512 }
1513
1514 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001515 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
1516 // cook their data
1517 bool copy_success = thread->GetStackFrameAtIndex(0)->GetRegisterContext()->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1518 if (copy_success)
Jim Ingham44137582012-09-12 00:40:39 +00001519 {
1520 thread->DiscardThreadPlans(true);
Jim Inghamcb640dd2012-09-14 02:14:15 +00001521 thread->ClearStackFrames();
Jim Ingham4f465cf2012-10-10 18:32:14 +00001522 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1523 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
Jim Ingham44137582012-09-12 00:40:39 +00001524 return return_error;
1525 }
1526 else
1527 {
1528 return_error.SetErrorString("Could not reset register values.");
1529 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001530 }
1531}
1532
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001533void
Greg Clayton0603aa92010-10-04 01:05:56 +00001534Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001535{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001536 ExecutionContext exe_ctx (shared_from_this());
1537 Process *process = exe_ctx.GetProcessPtr();
1538 if (process == NULL)
1539 return;
1540
Greg Claytonc14ee322011-09-22 04:58:26 +00001541 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00001542 SymbolContext frame_sc;
Greg Clayton0603aa92010-10-04 01:05:56 +00001543 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001544 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001545 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001546 if (frame_sp)
1547 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001548 exe_ctx.SetFrameSP(frame_sp);
1549 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001550 }
1551 }
1552
Greg Clayton1ac04c32012-02-21 00:09:25 +00001553 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00001554 assert (thread_format);
1555 const char *end = NULL;
1556 Debugger::FormatPrompt (thread_format,
Greg Claytonc14ee322011-09-22 04:58:26 +00001557 frame_sp ? &frame_sc : NULL,
Greg Clayton0603aa92010-10-04 01:05:56 +00001558 &exe_ctx,
1559 NULL,
1560 strm,
1561 &end);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001562}
1563
Greg Clayton99d0faf2010-11-18 23:32:35 +00001564void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001565Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00001566{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001567}
Jim Inghamee8aea12010-09-08 03:14:33 +00001568
Greg Clayton99d0faf2010-11-18 23:32:35 +00001569void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001570Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001571{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001572}
Jim Inghamee8aea12010-09-08 03:14:33 +00001573
Jim Inghame4284b72010-09-23 17:40:12 +00001574lldb::StackFrameSP
1575Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1576{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001577 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00001578}
Caroline Ticeceb6b132010-10-26 03:11:13 +00001579
1580const char *
1581Thread::StopReasonAsCString (lldb::StopReason reason)
1582{
1583 switch (reason)
1584 {
Andrew Kaylorf85defa2012-12-20 23:08:03 +00001585 case eStopReasonInvalid: return "invalid";
1586 case eStopReasonNone: return "none";
1587 case eStopReasonTrace: return "trace";
1588 case eStopReasonBreakpoint: return "breakpoint";
1589 case eStopReasonWatchpoint: return "watchpoint";
1590 case eStopReasonSignal: return "signal";
1591 case eStopReasonException: return "exception";
1592 case eStopReasonExec: return "exec";
1593 case eStopReasonPlanComplete: return "plan complete";
1594 case eStopReasonThreadExiting: return "thread exiting";
Caroline Ticeceb6b132010-10-26 03:11:13 +00001595 }
1596
1597
1598 static char unknown_state_string[64];
1599 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1600 return unknown_state_string;
1601}
1602
1603const char *
1604Thread::RunModeAsCString (lldb::RunMode mode)
1605{
1606 switch (mode)
1607 {
1608 case eOnlyThisThread: return "only this thread";
1609 case eAllThreads: return "all threads";
1610 case eOnlyDuringStepping: return "only during stepping";
1611 }
1612
1613 static char unknown_state_string[64];
1614 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1615 return unknown_state_string;
1616}
Jim Ingham06e827c2010-11-11 19:26:09 +00001617
Greg Clayton7260f622011-04-18 08:33:37 +00001618size_t
1619Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1620{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001621 ExecutionContext exe_ctx (shared_from_this());
1622 Target *target = exe_ctx.GetTargetPtr();
1623 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00001624 size_t num_frames_shown = 0;
1625 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00001626 bool is_selected = false;
1627 if (process)
1628 {
1629 if (process->GetThreadList().GetSelectedThread().get() == this)
1630 is_selected = true;
1631 }
1632 strm.Printf("%c ", is_selected ? '*' : ' ');
1633 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00001634 {
Greg Clayton5113dc82011-08-12 06:47:54 +00001635 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00001636 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00001637 {
Jim Inghame610d642011-08-16 00:07:28 +00001638 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1639 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1640 {
1641 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1642 }
Greg Clayton5113dc82011-08-12 06:47:54 +00001643 }
Greg Clayton7260f622011-04-18 08:33:37 +00001644 }
1645
1646 DumpUsingSettingsFormat (strm, start_frame);
1647
1648 if (num_frames > 0)
1649 {
1650 strm.IndentMore();
1651
1652 const bool show_frame_info = true;
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001653 strm.IndentMore ();
Greg Clayton39d0ab32012-03-29 01:41:38 +00001654 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1655 start_frame,
1656 num_frames,
1657 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001658 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001659 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001660 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00001661 }
1662 return num_frames_shown;
1663}
1664
1665size_t
1666Thread::GetStackFrameStatus (Stream& strm,
1667 uint32_t first_frame,
1668 uint32_t num_frames,
1669 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001670 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00001671{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001672 return GetStackFrameList()->GetStatus (strm,
1673 first_frame,
1674 num_frames,
1675 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001676 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001677}
1678
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001679bool
1680Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1681{
1682 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1683 if (frame_sp)
1684 {
1685 checkpoint.SetStackID(frame_sp->GetStackID());
1686 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1687 }
1688 return false;
1689}
Greg Clayton7260f622011-04-18 08:33:37 +00001690
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001691bool
1692Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1693{
Jim Ingham44137582012-09-12 00:40:39 +00001694 return ResetFrameZeroRegisters (checkpoint.GetData());
1695}
1696
1697bool
1698Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1699{
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001700 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1701 if (frame_sp)
1702 {
Jim Ingham44137582012-09-12 00:40:39 +00001703 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (register_data_sp);
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001704
1705 // Clear out all stack frames as our world just changed.
1706 ClearStackFrames();
1707 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
Jason Molendad525d072012-11-14 04:26:02 +00001708 if (m_unwinder_ap.get())
1709 m_unwinder_ap->Clear();
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001710
1711 return ret;
1712 }
1713 return false;
1714}
Greg Clayton7260f622011-04-18 08:33:37 +00001715
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001716Unwind *
1717Thread::GetUnwinder ()
1718{
1719 if (m_unwinder_ap.get() == NULL)
1720 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00001721 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001722 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1723 switch (machine)
1724 {
1725 case llvm::Triple::x86_64:
1726 case llvm::Triple::x86:
1727 case llvm::Triple::arm:
1728 case llvm::Triple::thumb:
1729 m_unwinder_ap.reset (new UnwindLLDB (*this));
1730 break;
1731
1732 default:
1733 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1734 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1735 break;
1736 }
1737 }
1738 return m_unwinder_ap.get();
1739}
1740
1741
Greg Claytonfa559e52012-05-18 02:38:05 +00001742void
1743Thread::Flush ()
1744{
1745 ClearStackFrames ();
1746 m_reg_context_sp.reset();
1747}
Jim Ingham5d88a062012-10-16 00:09:33 +00001748
Filipe Cabecinhasb3d5d712012-11-20 00:11:13 +00001749bool
Jim Ingham5d88a062012-10-16 00:09:33 +00001750Thread::IsStillAtLastBreakpointHit ()
1751{
1752 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
1753 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
1754 // multithreaded programs.
1755 if (m_actual_stop_info_sp) {
1756 StopReason stop_reason = m_actual_stop_info_sp->GetStopReason();
1757 if (stop_reason == lldb::eStopReasonBreakpoint) {
1758 uint64_t value = m_actual_stop_info_sp->GetValue();
1759 lldb::addr_t pc = GetRegisterContext()->GetPC();
1760 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1761 if (bp_site_sp && value == bp_site_sp->GetID())
1762 return true;
1763 }
1764 }
1765 return false;
1766}