blob: 1e91d18ee428a4224be184ef5826714cd5ff0e8a [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
10#include "lldb/lldb-private-log.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000011#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Clayton0603aa92010-10-04 01:05:56 +000012#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013#include "lldb/Core/Log.h"
14#include "lldb/Core/Stream.h"
15#include "lldb/Core/StreamString.h"
Jim Inghamee8aea12010-09-08 03:14:33 +000016#include "lldb/Core/RegularExpression.h"
Greg Clayton7260f622011-04-18 08:33:37 +000017#include "lldb/Host/Host.h"
Jim Ingham1f51e602012-09-27 01:15:29 +000018#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Target/DynamicLoader.h"
20#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000021#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Target/Process.h"
23#include "lldb/Target/RegisterContext.h"
Greg Claytonf4b47e12010-08-04 01:40:35 +000024#include "lldb/Target/StopInfo.h"
Greg Clayton896dff62010-07-23 16:45:51 +000025#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Target/Thread.h"
27#include "lldb/Target/ThreadPlan.h"
28#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Target/ThreadPlanBase.h"
30#include "lldb/Target/ThreadPlanStepInstruction.h"
31#include "lldb/Target/ThreadPlanStepOut.h"
32#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
33#include "lldb/Target/ThreadPlanStepThrough.h"
34#include "lldb/Target/ThreadPlanStepInRange.h"
35#include "lldb/Target/ThreadPlanStepOverRange.h"
36#include "lldb/Target/ThreadPlanRunToAddress.h"
37#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham1b54c882010-06-16 02:00:15 +000038#include "lldb/Target/ThreadSpec.h"
Jim Ingham87c11912010-08-12 02:14:28 +000039#include "lldb/Target/Unwind.h"
Greg Clayton56d9a1b2011-08-22 02:49:39 +000040#include "Plugins/Process/Utility/UnwindLLDB.h"
41#include "UnwindMacOSXFrameBackchain.h"
42
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
44using namespace lldb;
45using namespace lldb_private;
46
Greg Clayton67cc0632012-08-22 17:17:09 +000047
48const ThreadPropertiesSP &
49Thread::GetGlobalProperties()
50{
51 static ThreadPropertiesSP g_settings_sp;
52 if (!g_settings_sp)
53 g_settings_sp.reset (new ThreadProperties (true));
54 return g_settings_sp;
55}
56
57static PropertyDefinition
58g_properties[] =
59{
60 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
61 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
62 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
63};
64
65enum {
66 ePropertyStepAvoidRegex,
67 ePropertyEnableThreadTrace
68};
69
70
71class ThreadOptionValueProperties : public OptionValueProperties
72{
73public:
74 ThreadOptionValueProperties (const ConstString &name) :
75 OptionValueProperties (name)
76 {
77 }
78
79 // This constructor is used when creating ThreadOptionValueProperties when it
80 // is part of a new lldb_private::Thread instance. It will copy all current
81 // global property values as needed
82 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Clayton6920b522012-08-22 18:39:03 +000083 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton67cc0632012-08-22 17:17:09 +000084 {
85 }
86
87 virtual const Property *
88 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
89 {
90 // When gettings the value for a key from the thread options, we will always
91 // try and grab the setting from the current thread if there is one. Else we just
92 // use the one from this instance.
93 if (exe_ctx)
94 {
95 Thread *thread = exe_ctx->GetThreadPtr();
96 if (thread)
97 {
98 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
99 if (this != instance_properties)
100 return instance_properties->ProtectedGetPropertyAtIndex (idx);
101 }
102 }
103 return ProtectedGetPropertyAtIndex (idx);
104 }
105};
106
107
108
109ThreadProperties::ThreadProperties (bool is_global) :
110 Properties ()
111{
112 if (is_global)
113 {
114 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
115 m_collection_sp->Initialize(g_properties);
116 }
117 else
118 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
119}
120
121ThreadProperties::~ThreadProperties()
122{
123}
124
125const RegularExpression *
126ThreadProperties::GetSymbolsToAvoidRegexp()
127{
128 const uint32_t idx = ePropertyStepAvoidRegex;
129 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
130}
131
132bool
133ThreadProperties::GetTraceEnabledState() const
134{
135 const uint32_t idx = ePropertyEnableThreadTrace;
136 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
137}
138
Jim Ingham4f465cf2012-10-10 18:32:14 +0000139//------------------------------------------------------------------
140// Thread Event Data
141//------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +0000142
Jim Ingham4f465cf2012-10-10 18:32:14 +0000143
144const ConstString &
145Thread::ThreadEventData::GetFlavorString ()
146{
147 static ConstString g_flavor ("Thread::ThreadEventData");
148 return g_flavor;
149}
150
151Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp) :
152 m_thread_sp (thread_sp),
153 m_stack_id ()
154{
155}
156
157Thread::ThreadEventData::ThreadEventData (const lldb::ThreadSP thread_sp, const StackID &stack_id) :
158 m_thread_sp (thread_sp),
159 m_stack_id (stack_id)
160{
161}
162
163Thread::ThreadEventData::ThreadEventData () :
164 m_thread_sp (),
165 m_stack_id ()
166{
167}
168
169Thread::ThreadEventData::~ThreadEventData ()
170{
171}
172
173void
174Thread::ThreadEventData::Dump (Stream *s) const
175{
176
177}
178
179const Thread::ThreadEventData *
180Thread::ThreadEventData::GetEventDataFromEvent (const Event *event_ptr)
181{
182 if (event_ptr)
183 {
184 const EventData *event_data = event_ptr->GetData();
185 if (event_data && event_data->GetFlavor() == ThreadEventData::GetFlavorString())
186 return static_cast <const ThreadEventData *> (event_ptr->GetData());
187 }
188 return NULL;
189}
190
191ThreadSP
192Thread::ThreadEventData::GetThreadFromEvent (const Event *event_ptr)
193{
194 ThreadSP thread_sp;
195 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
196 if (event_data)
197 thread_sp = event_data->GetThread();
198 return thread_sp;
199}
200
201StackID
202Thread::ThreadEventData::GetStackIDFromEvent (const Event *event_ptr)
203{
204 StackID stack_id;
205 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
206 if (event_data)
207 stack_id = event_data->GetStackID();
208 return stack_id;
209}
210
211StackFrameSP
212Thread::ThreadEventData::GetStackFrameFromEvent (const Event *event_ptr)
213{
214 const ThreadEventData *event_data = GetEventDataFromEvent (event_ptr);
215 StackFrameSP frame_sp;
216 if (event_data)
217 {
218 ThreadSP thread_sp = event_data->GetThread();
219 if (thread_sp)
220 {
221 frame_sp = thread_sp->GetStackFrameList()->GetFrameWithStackID (event_data->GetStackID());
222 }
223 }
224 return frame_sp;
225}
226
227//------------------------------------------------------------------
228// Thread class
229//------------------------------------------------------------------
230
231ConstString &
232Thread::GetStaticBroadcasterClass ()
233{
234 static ConstString class_name ("lldb.thread");
235 return class_name;
236}
237
238Thread::Thread (Process &process, lldb::tid_t tid) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000239 ThreadProperties (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240 UserID (tid),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000241 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
242 m_process_wp (process.shared_from_this()),
Greg Claytonf4b47e12010-08-04 01:40:35 +0000243 m_actual_stop_info_sp (),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000244 m_index_id (process.GetNextThreadIndexID ()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245 m_reg_context_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246 m_state (eStateUnloaded),
Greg Clayton12daf9462010-08-25 00:35:26 +0000247 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248 m_plan_stack (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249 m_completed_plan_stack(),
Greg Clayton39d0ab32012-03-29 01:41:38 +0000250 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000251 m_curr_frames_sp (),
252 m_prev_frames_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham87c11912010-08-12 02:14:28 +0000254 m_resume_state (eStateRunning),
Jim Ingham92087d82012-01-31 23:09:20 +0000255 m_temporary_resume_state (eStateRunning),
Jim Ingham773d9812010-11-18 02:47:07 +0000256 m_unwinder_ap (),
Jim Ingham77787032011-01-20 02:03:18 +0000257 m_destroy_called (false),
258 m_thread_stop_reason_stop_id (0)
Jim Ingham87c11912010-08-12 02:14:28 +0000259
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000261 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000263 log->Printf ("%p Thread::Thread(tid = 0x%4.4llx)", this, GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264
Jim Ingham4f465cf2012-10-10 18:32:14 +0000265 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266 QueueFundamentalPlan(true);
267}
268
269
270Thread::~Thread()
271{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000272 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000274 log->Printf ("%p Thread::~Thread(tid = 0x%4.4llx)", this, GetID());
Jim Ingham773d9812010-11-18 02:47:07 +0000275 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
276 assert (m_destroy_called);
277}
278
279void
280Thread::DestroyThread ()
281{
Greg Clayton35a4cc52012-10-29 20:52:08 +0000282 m_destroy_called = true;
Jim Ingham773d9812010-11-18 02:47:07 +0000283 m_plan_stack.clear();
284 m_discarded_plan_stack.clear();
285 m_completed_plan_stack.clear();
Jim Inghamd8ba4642012-04-10 00:44:25 +0000286 m_actual_stop_info_sp.reset();
Greg Clayton35a4cc52012-10-29 20:52:08 +0000287 m_reg_context_sp.reset();
288 m_unwinder_ap.reset();
289 Mutex::Locker locker(m_frame_mutex);
290 m_curr_frames_sp.reset();
291 m_prev_frames_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292}
293
Jim Ingham4f465cf2012-10-10 18:32:14 +0000294void
295Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
296{
297 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
298 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
299}
300
301uint32_t
302Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
303{
304 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
305 if (broadcast)
306 BroadcastSelectedFrameChange(frame->GetStackID());
307 return ret_value;
308}
309
310bool
311Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
312{
313 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
314 if (frame_sp)
315 {
316 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
317 if (broadcast)
318 BroadcastSelectedFrameChange(frame_sp->GetStackID());
319 return true;
320 }
321 else
322 return false;
323}
324
325
Jim Inghamb15bfc72010-10-20 00:39:53 +0000326lldb::StopInfoSP
Greg Claytonf4b47e12010-08-04 01:40:35 +0000327Thread::GetStopInfo ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328{
Jim Inghamb15bfc72010-10-20 00:39:53 +0000329 ThreadPlanSP plan_sp (GetCompletedPlan());
Jim Inghamfbbfe6e2012-05-01 18:38:37 +0000330 if (plan_sp && plan_sp->PlanSucceeded())
Jim Ingham73ca05a2011-12-17 01:35:57 +0000331 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Jim Inghamb15bfc72010-10-20 00:39:53 +0000332 else
Jim Ingham79c35da2011-08-09 00:32:52 +0000333 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000334 ProcessSP process_sp (GetProcess());
335 if (process_sp
336 && m_actual_stop_info_sp
Jim Ingham79c35da2011-08-09 00:32:52 +0000337 && m_actual_stop_info_sp->IsValid()
Greg Clayton1ac04c32012-02-21 00:09:25 +0000338 && m_thread_stop_reason_stop_id == process_sp->GetStopID())
Jim Ingham79c35da2011-08-09 00:32:52 +0000339 return m_actual_stop_info_sp;
340 else
341 return GetPrivateStopReason ();
342 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343}
344
Greg Clayton97d5cf02012-09-25 02:40:06 +0000345lldb::StopReason
346Thread::GetStopReason()
347{
348 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
349 if (stop_info_sp)
Filipe Cabecinhase26391a2012-09-28 15:55:43 +0000350 return stop_info_sp->GetStopReason();
Greg Clayton97d5cf02012-09-25 02:40:06 +0000351 return eStopReasonNone;
352}
353
354
355
Jim Ingham77787032011-01-20 02:03:18 +0000356void
357Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
358{
359 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham79c35da2011-08-09 00:32:52 +0000360 if (m_actual_stop_info_sp)
361 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000362 ProcessSP process_sp (GetProcess());
363 if (process_sp)
364 m_thread_stop_reason_stop_id = process_sp->GetStopID();
365 else
366 m_thread_stop_reason_stop_id = UINT32_MAX;
Jim Ingham77787032011-01-20 02:03:18 +0000367}
368
369void
370Thread::SetStopInfoToNothing()
371{
372 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
373 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
374 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
375}
376
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377bool
378Thread::ThreadStoppedForAReason (void)
379{
Jim Inghamf94e1792012-08-11 00:35:26 +0000380 return (bool) GetPrivateStopReason ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381}
382
Jim Ingham77787032011-01-20 02:03:18 +0000383bool
384Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
385{
386 if (!SaveFrameZeroState(saved_state.register_backup))
387 return false;
388
389 saved_state.stop_info_sp = GetStopInfo();
Greg Clayton1ac04c32012-02-21 00:09:25 +0000390 ProcessSP process_sp (GetProcess());
391 if (process_sp)
392 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham625fca72012-09-07 23:36:43 +0000393 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
394
Jim Ingham77787032011-01-20 02:03:18 +0000395 return true;
396}
397
398bool
399Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
400{
401 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham0c270682011-01-25 02:47:23 +0000402 if (saved_state.stop_info_sp)
403 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham77787032011-01-20 02:03:18 +0000404 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham625fca72012-09-07 23:36:43 +0000405 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham77787032011-01-20 02:03:18 +0000406 return true;
407}
408
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409StateType
410Thread::GetState() const
411{
412 // If any other threads access this we will need a mutex for it
413 Mutex::Locker locker(m_state_mutex);
414 return m_state;
415}
416
417void
418Thread::SetState(StateType state)
419{
420 Mutex::Locker locker(m_state_mutex);
421 m_state = state;
422}
423
424void
425Thread::WillStop()
426{
427 ThreadPlan *current_plan = GetCurrentPlan();
428
429 // FIXME: I may decide to disallow threads with no plans. In which
430 // case this should go to an assert.
431
432 if (!current_plan)
433 return;
434
435 current_plan->WillStop();
436}
437
438void
439Thread::SetupForResume ()
440{
441 if (GetResumeState() != eStateSuspended)
442 {
443
444 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
445 // telling the current plan it will resume, since we might change what the current
446 // plan is.
447
Jim Ingham5d88a062012-10-16 00:09:33 +0000448 StopReason stop_reason = lldb::eStopReasonInvalid;
449 StopInfoSP stop_info_sp = GetStopInfo();
450 if (stop_info_sp.get())
451 stop_reason = stop_info_sp->GetStopReason();
452 if (stop_reason == lldb::eStopReasonBreakpoint)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 {
454 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
455 // special to step over a breakpoint.
Jim Inghamb01e7422010-06-19 04:45:32 +0000456
457 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458
Jim Inghamb01e7422010-06-19 04:45:32 +0000459 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
460 {
461 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
462 if (step_bp_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000464 ThreadPlanSP step_bp_plan_sp;
465 step_bp_plan->SetPrivate (true);
466
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 if (GetCurrentPlan()->RunState() != eStateStepping)
468 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000469 step_bp_plan->SetAutoContinue(true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470 }
Jim Inghamb01e7422010-06-19 04:45:32 +0000471 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 QueueThreadPlan (step_bp_plan_sp, false);
473 }
474 }
475 }
476 }
477}
478
479bool
480Thread::WillResume (StateType resume_state)
481{
482 // At this point clear the completed plan stack.
483 m_completed_plan_stack.clear();
484 m_discarded_plan_stack.clear();
485
Greg Clayton97d5cf02012-09-25 02:40:06 +0000486 m_temporary_resume_state = resume_state;
Jim Ingham92087d82012-01-31 23:09:20 +0000487
488 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
489 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
490 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
491 // about the fact that we are resuming...
Greg Clayton1ac04c32012-02-21 00:09:25 +0000492 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham92087d82012-01-31 23:09:20 +0000493 if (m_thread_stop_reason_stop_id == process_stop_id &&
494 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
495 {
496 StopInfo *stop_info = GetPrivateStopReason().get();
497 if (stop_info)
498 stop_info->WillResume (resume_state);
499 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000500
501 // Tell all the plans that we are about to resume in case they need to clear any state.
502 // We distinguish between the plan on the top of the stack and the lower
503 // plans in case a plan needs to do any special business before it runs.
504
505 ThreadPlan *plan_ptr = GetCurrentPlan();
Jim Ingham513c6bb2012-09-01 01:02:41 +0000506 bool need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507
508 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
509 {
510 plan_ptr->WillResume (resume_state, false);
511 }
Greg Claytonf4b47e12010-08-04 01:40:35 +0000512
Jim Ingham513c6bb2012-09-01 01:02:41 +0000513 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
514 // In that case, don't reset it here.
515
Jim Ingham5d88a062012-10-16 00:09:33 +0000516 if (need_to_resume && resume_state != eStateSuspended)
Jim Ingham513c6bb2012-09-01 01:02:41 +0000517 {
518 m_actual_stop_info_sp.reset();
519 }
520
521 return need_to_resume;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522}
523
524void
525Thread::DidResume ()
526{
527 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
528}
529
530bool
531Thread::ShouldStop (Event* event_ptr)
532{
533 ThreadPlan *current_plan = GetCurrentPlan();
534 bool should_stop = true;
535
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000536 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham92087d82012-01-31 23:09:20 +0000537
538 if (GetResumeState () == eStateSuspended)
539 {
540 if (log)
541 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
542 __FUNCTION__,
543 GetID ());
544// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
545// __FUNCTION__,
546// GetID (),
547// GetRegisterContext()->GetPC());
548 return false;
549 }
550
551 if (GetTemporaryResumeState () == eStateSuspended)
552 {
553 if (log)
554 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
555 __FUNCTION__,
556 GetID ());
557// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
558// __FUNCTION__,
559// GetID (),
560// GetRegisterContext()->GetPC());
561 return false;
562 }
563
564 if (ThreadStoppedForAReason() == false)
565 {
566 if (log)
567 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
568 __FUNCTION__,
569 GetID (),
570 GetRegisterContext()->GetPC());
571 return false;
572 }
Jim Ingham513c6bb2012-09-01 01:02:41 +0000573
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574 if (log)
575 {
Jim Ingham92087d82012-01-31 23:09:20 +0000576 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
577 __FUNCTION__,
578 GetID (),
579 GetRegisterContext()->GetPC());
Jim Ingham10c4b242011-10-15 00:23:43 +0000580 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 StreamString s;
Jim Ingham10c4b242011-10-15 00:23:43 +0000582 s.IndentMore();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 DumpThreadPlans(&s);
Jim Ingham10c4b242011-10-15 00:23:43 +0000584 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 }
Jim Ingham06e827c2010-11-11 19:26:09 +0000586
587 // The top most plan always gets to do the trace log...
588 current_plan->DoTraceLog ();
Jim Ingham6d66ce62012-04-20 21:16:56 +0000589
590 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
591 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
592 // do any more work on this stop.
593 StopInfoSP private_stop_info (GetPrivateStopReason());
594 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
595 {
596 if (log)
597 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
598 return false;
599 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600
Jim Inghambad39e42012-09-05 21:12:49 +0000601 // If we've already been restarted, don't query the plans since the state they would examine is not current.
602 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
603 return false;
604
605 // Before the plans see the state of the world, calculate the current inlined depth.
606 GetStackFrameList()->CalculateCurrentInlinedDepth();
607
Jim Ingham25f66702011-12-03 01:52:59 +0000608 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
609 // If that plan is still working, then we don't need to do any more work. If the plan that explains
610 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
611 // whether they still need to do more work.
612
613 bool done_processing_current_plan = false;
614
615 if (!current_plan->PlanExplainsStop())
616 {
617 if (current_plan->TracerExplainsStop())
618 {
619 done_processing_current_plan = true;
620 should_stop = false;
621 }
622 else
623 {
Jim Inghamcf274f92012-04-09 22:37:39 +0000624 // If the current plan doesn't explain the stop, then find one that
Jim Ingham25f66702011-12-03 01:52:59 +0000625 // does and let it handle the situation.
626 ThreadPlan *plan_ptr = current_plan;
627 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
628 {
629 if (plan_ptr->PlanExplainsStop())
630 {
631 should_stop = plan_ptr->ShouldStop (event_ptr);
632
633 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
634 // and all the plans below it off the stack.
635
636 if (plan_ptr->MischiefManaged())
637 {
Jim Ingham031177e2012-05-11 23:49:49 +0000638 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Ingham7ba6e992012-05-11 23:47:32 +0000639 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Ingham25f66702011-12-03 01:52:59 +0000640
641 do
642 {
643 if (should_stop)
644 current_plan->WillStop();
645 PopPlan();
646 }
Jim Ingham7ba6e992012-05-11 23:47:32 +0000647 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
648 // Now, if the responsible plan was not "Okay to discard" then we're done,
649 // otherwise we forward this to the next plan in the stack below.
650 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
651 done_processing_current_plan = true;
652 else
653 done_processing_current_plan = false;
Jim Ingham25f66702011-12-03 01:52:59 +0000654 }
655 else
656 done_processing_current_plan = true;
657
658 break;
659 }
660
661 }
662 }
663 }
664
665 if (!done_processing_current_plan)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000666 {
Jim Inghamb01e7422010-06-19 04:45:32 +0000667 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Ingham0f16e732011-02-08 05:20:59 +0000668
Jim Ingham10c4b242011-10-15 00:23:43 +0000669 if (log)
670 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
671
Jim Ingham0f16e732011-02-08 05:20:59 +0000672 // We're starting from the base plan, so just let it decide;
673 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000675 should_stop = current_plan->ShouldStop (event_ptr);
676 if (log)
Greg Claytonaf247d72011-05-19 03:54:16 +0000677 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Ingham0f16e732011-02-08 05:20:59 +0000678 }
679 else
680 {
681 // Otherwise, don't let the base plan override what the other plans say to do, since
682 // presumably if there were other plans they would know what to do...
683 while (1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000685 if (PlanIsBasePlan(current_plan))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686 break;
Jim Ingham0f16e732011-02-08 05:20:59 +0000687
688 should_stop = current_plan->ShouldStop(event_ptr);
689 if (log)
690 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
691 if (current_plan->MischiefManaged())
692 {
693 if (should_stop)
694 current_plan->WillStop();
695
696 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
697 // Otherwise, see if the plan's parent wants to stop.
698
699 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
700 {
701 PopPlan();
702 break;
703 }
704 else
705 {
706
707 PopPlan();
708
709 current_plan = GetCurrentPlan();
710 if (current_plan == NULL)
711 {
712 break;
713 }
714 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 }
716 else
717 {
Jim Ingham0f16e732011-02-08 05:20:59 +0000718 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720 }
721 }
Jim Ingham64e7ead2012-05-03 21:19:36 +0000722
Jim Inghamb01e7422010-06-19 04:45:32 +0000723 if (over_ride_stop)
724 should_stop = false;
Jim Ingham64e7ead2012-05-03 21:19:36 +0000725
726 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
727 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
728 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
729 // This code clears stale plans off the stack.
730
731 if (should_stop)
732 {
733 ThreadPlan *plan_ptr = GetCurrentPlan();
734 while (!PlanIsBasePlan(plan_ptr))
735 {
736 bool stale = plan_ptr->IsPlanStale ();
737 ThreadPlan *examined_plan = plan_ptr;
738 plan_ptr = GetPreviousPlan (examined_plan);
739
740 if (stale)
741 {
742 if (log)
743 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
744 DiscardThreadPlansUpToPlan(examined_plan);
745 }
746 }
747 }
748
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750
Jim Ingham10c4b242011-10-15 00:23:43 +0000751 if (log)
752 {
753 StreamString s;
754 s.IndentMore();
755 DumpThreadPlans(&s);
756 log->Printf ("Plan stack final state:\n%s", s.GetData());
757 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
758 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759 return should_stop;
760}
761
762Vote
763Thread::ShouldReportStop (Event* event_ptr)
764{
765 StateType thread_state = GetResumeState ();
Jim Ingham92087d82012-01-31 23:09:20 +0000766 StateType temp_thread_state = GetTemporaryResumeState();
767
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000768 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton2cad65a2010-09-03 17:10:42 +0000769
770 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
771 {
772 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000773 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774 return eVoteNoOpinion;
Greg Clayton2cad65a2010-09-03 17:10:42 +0000775 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776
Jim Ingham92087d82012-01-31 23:09:20 +0000777 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
778 {
779 if (log)
780 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
781 return eVoteNoOpinion;
782 }
783
784 if (!ThreadStoppedForAReason())
785 {
786 if (log)
787 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
788 return eVoteNoOpinion;
789 }
790
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791 if (m_completed_plan_stack.size() > 0)
792 {
793 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton2cad65a2010-09-03 17:10:42 +0000794 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000795 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for complete stack's back plan\n", GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
797 }
798 else
Greg Clayton2cad65a2010-09-03 17:10:42 +0000799 {
800 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000801 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton2cad65a2010-09-03 17:10:42 +0000803 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804}
805
806Vote
807Thread::ShouldReportRun (Event* event_ptr)
808{
809 StateType thread_state = GetResumeState ();
Jim Ingham444586b2011-01-24 06:34:17 +0000810
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811 if (thread_state == eStateSuspended
812 || thread_state == eStateInvalid)
Jim Ingham444586b2011-01-24 06:34:17 +0000813 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814 return eVoteNoOpinion;
Jim Ingham444586b2011-01-24 06:34:17 +0000815 }
816
817 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818 if (m_completed_plan_stack.size() > 0)
819 {
820 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Ingham444586b2011-01-24 06:34:17 +0000821 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000822 log->Printf ("Current Plan for thread %d (0x%4.4llx): %s being asked whether we should report run.",
Jim Ingham444586b2011-01-24 06:34:17 +0000823 GetIndexID(),
824 GetID(),
825 m_completed_plan_stack.back()->GetName());
826
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000827 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
828 }
829 else
Jim Ingham444586b2011-01-24 06:34:17 +0000830 {
831 if (log)
Greg Clayton81c22f62011-10-19 18:09:39 +0000832 log->Printf ("Current Plan for thread %d (0x%4.4llx): %s being asked whether we should report run.",
Jim Ingham444586b2011-01-24 06:34:17 +0000833 GetIndexID(),
834 GetID(),
835 GetCurrentPlan()->GetName());
836
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000837 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Ingham444586b2011-01-24 06:34:17 +0000838 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839}
840
Jim Ingham1b54c882010-06-16 02:00:15 +0000841bool
842Thread::MatchesSpec (const ThreadSpec *spec)
843{
844 if (spec == NULL)
845 return true;
Jim Ingham1b54c882010-06-16 02:00:15 +0000846
Jim Ingham3d902922012-03-07 22:03:04 +0000847 return spec->ThreadPassesBasicTests(*this);
Jim Ingham1b54c882010-06-16 02:00:15 +0000848}
849
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000850void
851Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
852{
853 if (thread_plan_sp)
854 {
Jim Ingham06e827c2010-11-11 19:26:09 +0000855 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
856 if (!thread_plan_sp->GetThreadPlanTracer())
857 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Inghamb15bfc72010-10-20 00:39:53 +0000858 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham06e827c2010-11-11 19:26:09 +0000859
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860 thread_plan_sp->DidPush();
861
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000862 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000863 if (log)
864 {
865 StreamString s;
866 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Clayton81c22f62011-10-19 18:09:39 +0000867 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868 s.GetData(),
Jim Inghamb15bfc72010-10-20 00:39:53 +0000869 thread_plan_sp->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000870 }
871 }
872}
873
874void
875Thread::PopPlan ()
876{
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000877 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878
Jim Ingham4fc6cb92012-08-22 21:34:33 +0000879 if (m_plan_stack.size() <= 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000880 return;
881 else
882 {
883 ThreadPlanSP &plan = m_plan_stack.back();
884 if (log)
885 {
Greg Clayton81c22f62011-10-19 18:09:39 +0000886 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000887 }
888 m_completed_plan_stack.push_back (plan);
889 plan->WillPop();
890 m_plan_stack.pop_back();
891 }
892}
893
894void
895Thread::DiscardPlan ()
896{
897 if (m_plan_stack.size() > 1)
898 {
899 ThreadPlanSP &plan = m_plan_stack.back();
900 m_discarded_plan_stack.push_back (plan);
901 plan->WillPop();
902 m_plan_stack.pop_back();
903 }
904}
905
906ThreadPlan *
907Thread::GetCurrentPlan ()
908{
Jim Inghame1471232012-04-19 00:17:05 +0000909 // There will always be at least the base plan. If somebody is mucking with a
910 // thread with an empty plan stack, we should assert right away.
911 assert (!m_plan_stack.empty());
912
913 return m_plan_stack.back().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000914}
915
916ThreadPlanSP
917Thread::GetCompletedPlan ()
918{
919 ThreadPlanSP empty_plan_sp;
920 if (!m_completed_plan_stack.empty())
921 {
922 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
923 {
924 ThreadPlanSP completed_plan_sp;
925 completed_plan_sp = m_completed_plan_stack[i];
926 if (!completed_plan_sp->GetPrivate ())
927 return completed_plan_sp;
928 }
929 }
930 return empty_plan_sp;
931}
932
Jim Ingham73ca05a2011-12-17 01:35:57 +0000933ValueObjectSP
934Thread::GetReturnValueObject ()
935{
936 if (!m_completed_plan_stack.empty())
937 {
938 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
939 {
940 ValueObjectSP return_valobj_sp;
941 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
942 if (return_valobj_sp)
943 return return_valobj_sp;
944 }
945 }
946 return ValueObjectSP();
947}
948
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949bool
950Thread::IsThreadPlanDone (ThreadPlan *plan)
951{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952 if (!m_completed_plan_stack.empty())
953 {
954 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
955 {
956 if (m_completed_plan_stack[i].get() == plan)
957 return true;
958 }
959 }
960 return false;
961}
962
963bool
964Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
965{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966 if (!m_discarded_plan_stack.empty())
967 {
968 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
969 {
970 if (m_discarded_plan_stack[i].get() == plan)
971 return true;
972 }
973 }
974 return false;
975}
976
977ThreadPlan *
978Thread::GetPreviousPlan (ThreadPlan *current_plan)
979{
980 if (current_plan == NULL)
981 return NULL;
982
983 int stack_size = m_completed_plan_stack.size();
984 for (int i = stack_size - 1; i > 0; i--)
985 {
986 if (current_plan == m_completed_plan_stack[i].get())
987 return m_completed_plan_stack[i-1].get();
988 }
989
990 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
991 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992 if (m_plan_stack.size() > 0)
993 return m_plan_stack.back().get();
994 else
995 return NULL;
996 }
997
998 stack_size = m_plan_stack.size();
999 for (int i = stack_size - 1; i > 0; i--)
1000 {
1001 if (current_plan == m_plan_stack[i].get())
1002 return m_plan_stack[i-1].get();
1003 }
1004 return NULL;
1005}
1006
1007void
1008Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1009{
1010 if (abort_other_plans)
1011 DiscardThreadPlans(true);
1012
1013 PushPlan (thread_plan_sp);
1014}
1015
Jim Ingham06e827c2010-11-11 19:26:09 +00001016
1017void
1018Thread::EnableTracer (bool value, bool single_stepping)
1019{
1020 int stack_size = m_plan_stack.size();
1021 for (int i = 0; i < stack_size; i++)
1022 {
1023 if (m_plan_stack[i]->GetThreadPlanTracer())
1024 {
1025 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1026 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1027 }
1028 }
1029}
1030
1031void
1032Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1033{
1034 int stack_size = m_plan_stack.size();
1035 for (int i = 0; i < stack_size; i++)
1036 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1037}
1038
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001039void
Jim Ingham399f1ca2010-11-05 19:25:48 +00001040Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1041{
Jim Ingham64e7ead2012-05-03 21:19:36 +00001042 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1043}
1044
1045void
1046Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1047{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00001048 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham399f1ca2010-11-05 19:25:48 +00001049 if (log)
1050 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001051 log->Printf("Discarding thread plans for thread tid = 0x%4.4llx, up to %p", GetID(), up_to_plan_ptr);
Jim Ingham399f1ca2010-11-05 19:25:48 +00001052 }
1053
1054 int stack_size = m_plan_stack.size();
1055
1056 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1057 // stack, and if so discard up to and including it.
1058
Jim Ingham64e7ead2012-05-03 21:19:36 +00001059 if (up_to_plan_ptr == NULL)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001060 {
1061 for (int i = stack_size - 1; i > 0; i--)
1062 DiscardPlan();
1063 }
1064 else
1065 {
1066 bool found_it = false;
1067 for (int i = stack_size - 1; i > 0; i--)
1068 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001069 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001070 found_it = true;
1071 }
1072 if (found_it)
1073 {
1074 bool last_one = false;
1075 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1076 {
Jim Ingham64e7ead2012-05-03 21:19:36 +00001077 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Ingham399f1ca2010-11-05 19:25:48 +00001078 last_one = true;
1079 DiscardPlan();
1080 }
1081 }
1082 }
1083 return;
1084}
1085
1086void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087Thread::DiscardThreadPlans(bool force)
1088{
Greg Clayton2d4edfb2010-11-06 01:53:30 +00001089 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090 if (log)
1091 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001092 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001093 }
1094
1095 if (force)
1096 {
1097 int stack_size = m_plan_stack.size();
1098 for (int i = stack_size - 1; i > 0; i--)
1099 {
1100 DiscardPlan();
1101 }
1102 return;
1103 }
1104
1105 while (1)
1106 {
1107
1108 int master_plan_idx;
Jim Inghama39ad072012-09-11 00:09:25 +00001109 bool discard = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110
1111 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1112 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1113 {
1114 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1115 {
1116 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1117 break;
1118 }
1119 }
1120
1121 if (discard)
1122 {
1123 // First pop all the dependent plans:
1124 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1125 {
1126
1127 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1128 // for the plan leaves it in a state that it is safe to pop the plan
1129 // with no more notice?
1130 DiscardPlan();
1131 }
1132
1133 // Now discard the master plan itself.
1134 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1135 // discard it's dependent plans, but not it...
1136 if (master_plan_idx > 0)
1137 {
1138 DiscardPlan();
1139 }
1140 }
1141 else
1142 {
1143 // If the master plan doesn't want to get discarded, then we're done.
1144 break;
1145 }
1146
1147 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001148}
1149
Jim Inghamcf274f92012-04-09 22:37:39 +00001150bool
1151Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1152{
1153 if (plan_ptr->IsBasePlan())
1154 return true;
1155 else if (m_plan_stack.size() == 0)
1156 return false;
1157 else
1158 return m_plan_stack[0].get() == plan_ptr;
1159}
1160
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001161ThreadPlan *
1162Thread::QueueFundamentalPlan (bool abort_other_plans)
1163{
1164 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1165 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1166 return thread_plan_sp.get();
1167}
1168
1169ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001170Thread::QueueThreadPlanForStepSingleInstruction
1171(
1172 bool step_over,
1173 bool abort_other_plans,
1174 bool stop_other_threads
1175)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001176{
1177 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1178 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1179 return thread_plan_sp.get();
1180}
1181
1182ThreadPlan *
Greg Clayton474966a2010-06-12 18:59:55 +00001183Thread::QueueThreadPlanForStepRange
1184(
1185 bool abort_other_plans,
1186 StepType type,
1187 const AddressRange &range,
1188 const SymbolContext &addr_context,
1189 lldb::RunMode stop_other_threads,
1190 bool avoid_code_without_debug_info
1191)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192{
1193 ThreadPlanSP thread_plan_sp;
1194 if (type == eStepTypeInto)
Greg Clayton474966a2010-06-12 18:59:55 +00001195 {
1196 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1197 if (avoid_code_without_debug_info)
1198 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1199 else
1200 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1201 thread_plan_sp.reset (plan);
1202 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203 else
1204 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1205
1206 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1207 return thread_plan_sp.get();
1208}
1209
1210
1211ThreadPlan *
1212Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1213{
1214 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1215 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1216 return thread_plan_sp.get();
1217}
1218
1219ThreadPlan *
Greg Clayton481cef22011-01-21 06:11:58 +00001220Thread::QueueThreadPlanForStepOut
1221(
1222 bool abort_other_plans,
1223 SymbolContext *addr_context,
1224 bool first_insn,
1225 bool stop_other_threads,
1226 Vote stop_vote,
1227 Vote run_vote,
1228 uint32_t frame_idx
1229)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230{
Greg Clayton481cef22011-01-21 06:11:58 +00001231 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1232 addr_context,
1233 first_insn,
1234 stop_other_threads,
1235 stop_vote,
1236 run_vote,
1237 frame_idx));
Sean Callanan708709c2012-07-31 22:19:25 +00001238
1239 if (thread_plan_sp->ValidatePlan(NULL))
1240 {
1241 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1242 return thread_plan_sp.get();
1243 }
1244 else
1245 {
1246 return NULL;
1247 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001248}
1249
1250ThreadPlan *
Jim Ingham18de2fd2012-05-10 01:35:39 +00001251Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252{
Jim Ingham18de2fd2012-05-10 01:35:39 +00001253 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Ingham25f66702011-12-03 01:52:59 +00001254 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1255 return NULL;
1256
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001257 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1258 return thread_plan_sp.get();
1259}
1260
1261ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1263 Address& function,
1264 lldb::addr_t arg,
1265 bool stop_other_threads,
1266 bool discard_on_error)
1267{
Jim Inghamef651602011-12-22 19:12:40 +00001268 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1270 return thread_plan_sp.get();
1271}
1272
1273ThreadPlan *
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1275 Address &target_addr,
1276 bool stop_other_threads)
1277{
1278 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1279 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1280 return thread_plan_sp.get();
1281}
1282
1283ThreadPlan *
1284Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton481cef22011-01-21 06:11:58 +00001285 lldb::addr_t *address_list,
1286 size_t num_addresses,
1287 bool stop_other_threads,
1288 uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001289{
Greg Clayton481cef22011-01-21 06:11:58 +00001290 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001291 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1292 return thread_plan_sp.get();
1293
1294}
1295
1296uint32_t
1297Thread::GetIndexID () const
1298{
1299 return m_index_id;
1300}
1301
1302void
1303Thread::DumpThreadPlans (lldb_private::Stream *s) const
1304{
1305 uint32_t stack_size = m_plan_stack.size();
Greg Clayton1346f7e2010-09-03 22:45:01 +00001306 int i;
Jim Ingham10c4b242011-10-15 00:23:43 +00001307 s->Indent();
Greg Clayton81c22f62011-10-19 18:09:39 +00001308 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4llx, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
Greg Clayton1346f7e2010-09-03 22:45:01 +00001309 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001310 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001311 s->IndentMore();
Jim Ingham10c4b242011-10-15 00:23:43 +00001312 s->Indent();
1313 s->Printf ("Element %d: ", i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001314 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001315 s->EOL();
Jim Ingham10c4b242011-10-15 00:23:43 +00001316 s->IndentLess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001317 }
1318
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001319 stack_size = m_completed_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001320 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001321 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001322 s->Indent();
1323 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1324 for (i = stack_size - 1; i >= 0; i--)
1325 {
1326 s->IndentMore();
1327 s->Indent();
1328 s->Printf ("Element %d: ", i);
1329 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1330 s->EOL();
1331 s->IndentLess();
1332 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001333 }
1334
1335 stack_size = m_discarded_plan_stack.size();
Jim Ingham10c4b242011-10-15 00:23:43 +00001336 if (stack_size > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001337 {
Jim Ingham10c4b242011-10-15 00:23:43 +00001338 s->Indent();
1339 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1340 for (i = stack_size - 1; i >= 0; i--)
1341 {
1342 s->IndentMore();
1343 s->Indent();
1344 s->Printf ("Element %d: ", i);
1345 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1346 s->EOL();
1347 s->IndentLess();
1348 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001349 }
1350
1351}
1352
Greg Claytond9e416c2012-02-18 05:35:26 +00001353TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354Thread::CalculateTarget ()
1355{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001356 TargetSP target_sp;
1357 ProcessSP process_sp(GetProcess());
1358 if (process_sp)
1359 target_sp = process_sp->CalculateTarget();
1360 return target_sp;
1361
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001362}
1363
Greg Claytond9e416c2012-02-18 05:35:26 +00001364ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365Thread::CalculateProcess ()
1366{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001367 return GetProcess();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001368}
1369
Greg Claytond9e416c2012-02-18 05:35:26 +00001370ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001371Thread::CalculateThread ()
1372{
Greg Claytond9e416c2012-02-18 05:35:26 +00001373 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374}
1375
Greg Claytond9e416c2012-02-18 05:35:26 +00001376StackFrameSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001377Thread::CalculateStackFrame ()
1378{
Greg Claytond9e416c2012-02-18 05:35:26 +00001379 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001380}
1381
1382void
Greg Clayton0603aa92010-10-04 01:05:56 +00001383Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001384{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001385 exe_ctx.SetContext (shared_from_this());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001386}
1387
Greg Clayton12daf9462010-08-25 00:35:26 +00001388
Greg Clayton39d0ab32012-03-29 01:41:38 +00001389StackFrameListSP
Greg Clayton12daf9462010-08-25 00:35:26 +00001390Thread::GetStackFrameList ()
1391{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001392 StackFrameListSP frame_list_sp;
1393 Mutex::Locker locker(m_frame_mutex);
1394 if (m_curr_frames_sp)
1395 {
1396 frame_list_sp = m_curr_frames_sp;
1397 }
1398 else
1399 {
1400 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1401 m_curr_frames_sp = frame_list_sp;
1402 }
1403 return frame_list_sp;
Greg Clayton12daf9462010-08-25 00:35:26 +00001404}
1405
Greg Clayton12daf9462010-08-25 00:35:26 +00001406void
1407Thread::ClearStackFrames ()
1408{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001409 Mutex::Locker locker(m_frame_mutex);
1410
Jim Inghamb0c72a52012-02-29 03:40:22 +00001411 // Only store away the old "reference" StackFrameList if we got all its frames:
1412 // FIXME: At some point we can try to splice in the frames we have fetched into
1413 // the new frame as we make it, but let's not try that now.
1414 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00001415 m_prev_frames_sp.swap (m_curr_frames_sp);
1416 m_curr_frames_sp.reset();
Jim Ingham87c11912010-08-12 02:14:28 +00001417}
1418
1419lldb::StackFrameSP
Greg Clayton5ccbd292011-01-06 22:15:06 +00001420Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1421{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001422 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton5ccbd292011-01-06 22:15:06 +00001423}
1424
Jim Ingham44137582012-09-12 00:40:39 +00001425
1426Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001427Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001428{
1429 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1430 Error return_error;
1431
1432 if (!frame_sp)
1433 {
1434 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%llx.", frame_idx, GetID());
1435 }
1436
Jim Ingham4f465cf2012-10-10 18:32:14 +00001437 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Ingham44137582012-09-12 00:40:39 +00001438}
1439
1440Error
Jim Ingham4f465cf2012-10-10 18:32:14 +00001441Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Ingham44137582012-09-12 00:40:39 +00001442{
1443 Error return_error;
1444
1445 if (!frame_sp)
1446 {
1447 return_error.SetErrorString("Can't return to a null frame.");
1448 return return_error;
1449 }
1450
1451 Thread *thread = frame_sp->GetThread().get();
Jim Inghamcb640dd2012-09-14 02:14:15 +00001452 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1453 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Ingham44137582012-09-12 00:40:39 +00001454
1455 if (return_value_sp)
Jim Ingham1f51e602012-09-27 01:15:29 +00001456 {
Jim Ingham44137582012-09-12 00:40:39 +00001457 lldb::ABISP abi = thread->GetProcess()->GetABI();
1458 if (!abi)
1459 {
1460 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham28eb5712012-10-12 17:34:26 +00001461 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001462 }
Jim Ingham1f51e602012-09-27 01:15:29 +00001463 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1464
1465 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1466 // Turn that back on when that works.
1467 if (0 && sc.function != NULL)
1468 {
1469 Type *function_type = sc.function->GetType();
1470 if (function_type)
1471 {
1472 clang_type_t return_type = sc.function->GetReturnClangType();
1473 if (return_type)
1474 {
1475 ClangASTType ast_type (function_type->GetClangAST(), return_type);
1476 StreamString s;
1477 ast_type.DumpTypeDescription(&s);
1478 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type);
1479 if (cast_value_sp)
1480 {
1481 cast_value_sp->SetFormat(eFormatHex);
1482 return_value_sp = cast_value_sp;
1483 }
1484 }
1485 }
1486 }
1487
Jim Inghamcb640dd2012-09-14 02:14:15 +00001488 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Ingham44137582012-09-12 00:40:39 +00001489 if (!return_error.Success())
1490 return return_error;
1491 }
1492
1493 // Now write the return registers for the chosen frame:
Jim Inghamcb640dd2012-09-14 02:14:15 +00001494 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
1495 // cook their data
1496 bool copy_success = thread->GetStackFrameAtIndex(0)->GetRegisterContext()->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1497 if (copy_success)
Jim Ingham44137582012-09-12 00:40:39 +00001498 {
1499 thread->DiscardThreadPlans(true);
Jim Inghamcb640dd2012-09-14 02:14:15 +00001500 thread->ClearStackFrames();
Jim Ingham4f465cf2012-10-10 18:32:14 +00001501 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1502 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
Jim Ingham44137582012-09-12 00:40:39 +00001503 return return_error;
1504 }
1505 else
1506 {
1507 return_error.SetErrorString("Could not reset register values.");
1508 return return_error;
Jim Ingham44137582012-09-12 00:40:39 +00001509 }
1510}
1511
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001512void
Greg Clayton0603aa92010-10-04 01:05:56 +00001513Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001515 ExecutionContext exe_ctx (shared_from_this());
1516 Process *process = exe_ctx.GetProcessPtr();
1517 if (process == NULL)
1518 return;
1519
Greg Claytonc14ee322011-09-22 04:58:26 +00001520 StackFrameSP frame_sp;
Greg Clayton0603aa92010-10-04 01:05:56 +00001521 SymbolContext frame_sc;
Greg Clayton0603aa92010-10-04 01:05:56 +00001522 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001523 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001524 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001525 if (frame_sp)
1526 {
Greg Claytonc14ee322011-09-22 04:58:26 +00001527 exe_ctx.SetFrameSP(frame_sp);
1528 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001529 }
1530 }
1531
Greg Clayton1ac04c32012-02-21 00:09:25 +00001532 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Clayton0603aa92010-10-04 01:05:56 +00001533 assert (thread_format);
1534 const char *end = NULL;
1535 Debugger::FormatPrompt (thread_format,
Greg Claytonc14ee322011-09-22 04:58:26 +00001536 frame_sp ? &frame_sc : NULL,
Greg Clayton0603aa92010-10-04 01:05:56 +00001537 &exe_ctx,
1538 NULL,
1539 strm,
1540 &end);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001541}
1542
Greg Clayton99d0faf2010-11-18 23:32:35 +00001543void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001544Thread::SettingsInitialize ()
Jim Inghamee8aea12010-09-08 03:14:33 +00001545{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001546}
Jim Inghamee8aea12010-09-08 03:14:33 +00001547
Greg Clayton99d0faf2010-11-18 23:32:35 +00001548void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001549Thread::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001550{
Greg Clayton99d0faf2010-11-18 23:32:35 +00001551}
Jim Inghamee8aea12010-09-08 03:14:33 +00001552
Jim Inghame4284b72010-09-23 17:40:12 +00001553lldb::StackFrameSP
1554Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1555{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001556 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghame4284b72010-09-23 17:40:12 +00001557}
Caroline Ticeceb6b132010-10-26 03:11:13 +00001558
1559const char *
1560Thread::StopReasonAsCString (lldb::StopReason reason)
1561{
1562 switch (reason)
1563 {
1564 case eStopReasonInvalid: return "invalid";
1565 case eStopReasonNone: return "none";
1566 case eStopReasonTrace: return "trace";
1567 case eStopReasonBreakpoint: return "breakpoint";
1568 case eStopReasonWatchpoint: return "watchpoint";
1569 case eStopReasonSignal: return "signal";
1570 case eStopReasonException: return "exception";
1571 case eStopReasonPlanComplete: return "plan complete";
1572 }
1573
1574
1575 static char unknown_state_string[64];
1576 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1577 return unknown_state_string;
1578}
1579
1580const char *
1581Thread::RunModeAsCString (lldb::RunMode mode)
1582{
1583 switch (mode)
1584 {
1585 case eOnlyThisThread: return "only this thread";
1586 case eAllThreads: return "all threads";
1587 case eOnlyDuringStepping: return "only during stepping";
1588 }
1589
1590 static char unknown_state_string[64];
1591 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1592 return unknown_state_string;
1593}
Jim Ingham06e827c2010-11-11 19:26:09 +00001594
Greg Clayton7260f622011-04-18 08:33:37 +00001595size_t
1596Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1597{
Greg Clayton1ac04c32012-02-21 00:09:25 +00001598 ExecutionContext exe_ctx (shared_from_this());
1599 Target *target = exe_ctx.GetTargetPtr();
1600 Process *process = exe_ctx.GetProcessPtr();
Greg Clayton7260f622011-04-18 08:33:37 +00001601 size_t num_frames_shown = 0;
1602 strm.Indent();
Greg Clayton1ac04c32012-02-21 00:09:25 +00001603 bool is_selected = false;
1604 if (process)
1605 {
1606 if (process->GetThreadList().GetSelectedThread().get() == this)
1607 is_selected = true;
1608 }
1609 strm.Printf("%c ", is_selected ? '*' : ' ');
1610 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Clayton7260f622011-04-18 08:33:37 +00001611 {
Greg Clayton5113dc82011-08-12 06:47:54 +00001612 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghame610d642011-08-16 00:07:28 +00001613 if (frame_sp)
Greg Clayton5113dc82011-08-12 06:47:54 +00001614 {
Jim Inghame610d642011-08-16 00:07:28 +00001615 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1616 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1617 {
1618 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1619 }
Greg Clayton5113dc82011-08-12 06:47:54 +00001620 }
Greg Clayton7260f622011-04-18 08:33:37 +00001621 }
1622
1623 DumpUsingSettingsFormat (strm, start_frame);
1624
1625 if (num_frames > 0)
1626 {
1627 strm.IndentMore();
1628
1629 const bool show_frame_info = true;
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001630 strm.IndentMore ();
Greg Clayton39d0ab32012-03-29 01:41:38 +00001631 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1632 start_frame,
1633 num_frames,
1634 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001635 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001636 strm.IndentLess();
Jim Ingham5c4df7a2011-07-26 02:39:59 +00001637 strm.IndentLess();
Greg Clayton7260f622011-04-18 08:33:37 +00001638 }
1639 return num_frames_shown;
1640}
1641
1642size_t
1643Thread::GetStackFrameStatus (Stream& strm,
1644 uint32_t first_frame,
1645 uint32_t num_frames,
1646 bool show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001647 uint32_t num_frames_with_source)
Greg Clayton7260f622011-04-18 08:33:37 +00001648{
Greg Clayton39d0ab32012-03-29 01:41:38 +00001649 return GetStackFrameList()->GetStatus (strm,
1650 first_frame,
1651 num_frames,
1652 show_frame_info,
Greg Clayton53eb7ad2012-07-11 20:33:48 +00001653 num_frames_with_source);
Greg Clayton7260f622011-04-18 08:33:37 +00001654}
1655
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001656bool
1657Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1658{
1659 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1660 if (frame_sp)
1661 {
1662 checkpoint.SetStackID(frame_sp->GetStackID());
1663 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1664 }
1665 return false;
1666}
Greg Clayton7260f622011-04-18 08:33:37 +00001667
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001668bool
1669Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1670{
Jim Ingham44137582012-09-12 00:40:39 +00001671 return ResetFrameZeroRegisters (checkpoint.GetData());
1672}
1673
1674bool
1675Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1676{
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001677 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1678 if (frame_sp)
1679 {
Jim Ingham44137582012-09-12 00:40:39 +00001680 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (register_data_sp);
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001681
1682 // Clear out all stack frames as our world just changed.
1683 ClearStackFrames();
1684 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
Jason Molendad525d072012-11-14 04:26:02 +00001685 if (m_unwinder_ap.get())
1686 m_unwinder_ap->Clear();
Peter Collingbourne5a6fa542011-06-03 20:40:54 +00001687
1688 return ret;
1689 }
1690 return false;
1691}
Greg Clayton7260f622011-04-18 08:33:37 +00001692
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001693Unwind *
1694Thread::GetUnwinder ()
1695{
1696 if (m_unwinder_ap.get() == NULL)
1697 {
Greg Clayton1ac04c32012-02-21 00:09:25 +00001698 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001699 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1700 switch (machine)
1701 {
1702 case llvm::Triple::x86_64:
1703 case llvm::Triple::x86:
1704 case llvm::Triple::arm:
1705 case llvm::Triple::thumb:
1706 m_unwinder_ap.reset (new UnwindLLDB (*this));
1707 break;
1708
1709 default:
1710 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1711 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1712 break;
1713 }
1714 }
1715 return m_unwinder_ap.get();
1716}
1717
1718
Greg Claytonfa559e52012-05-18 02:38:05 +00001719void
1720Thread::Flush ()
1721{
1722 ClearStackFrames ();
1723 m_reg_context_sp.reset();
1724}
Jim Ingham5d88a062012-10-16 00:09:33 +00001725
1726const bool
1727Thread::IsStillAtLastBreakpointHit ()
1728{
1729 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
1730 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
1731 // multithreaded programs.
1732 if (m_actual_stop_info_sp) {
1733 StopReason stop_reason = m_actual_stop_info_sp->GetStopReason();
1734 if (stop_reason == lldb::eStopReasonBreakpoint) {
1735 uint64_t value = m_actual_stop_info_sp->GetValue();
1736 lldb::addr_t pc = GetRegisterContext()->GetPC();
1737 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1738 if (bp_site_sp && value == bp_site_sp->GetID())
1739 return true;
1740 }
1741 }
1742 return false;
1743}