blob: 39157dd148fab59963ea82ee383cb9246ae6281c [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/lldb-private-log.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000011#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Claytona830adb2010-10-04 01:05:56 +000012#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "lldb/Core/Log.h"
14#include "lldb/Core/Stream.h"
15#include "lldb/Core/StreamString.h"
Jim Ingham20594b12010-09-08 03:14:33 +000016#include "lldb/Core/RegularExpression.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000017#include "lldb/Host/Host.h"
Jim Ingham46657452012-09-27 01:15:29 +000018#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Target/DynamicLoader.h"
20#include "lldb/Target/ExecutionContext.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000021#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Target/Process.h"
23#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000024#include "lldb/Target/StopInfo.h"
Greg Clayton7661a982010-07-23 16:45:51 +000025#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Target/Thread.h"
27#include "lldb/Target/ThreadPlan.h"
28#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-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 Ingham3c7b5b92010-06-16 02:00:15 +000038#include "lldb/Target/ThreadSpec.h"
Jim Ingham71219082010-08-12 02:14:28 +000039#include "lldb/Target/Unwind.h"
Greg Clayton37f962e2011-08-22 02:49:39 +000040#include "Plugins/Process/Utility/UnwindLLDB.h"
41#include "UnwindMacOSXFrameBackchain.h"
42
Chris Lattner24943d22010-06-08 16:52:24 +000043
44using namespace lldb;
45using namespace lldb_private;
46
Greg Clayton73844aa2012-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 Claytonc6e82e42012-08-22 18:39:03 +000083 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton73844aa2012-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 Ingham94a5d0d2012-10-10 18:32:14 +0000139//------------------------------------------------------------------
140// Thread Event Data
141//------------------------------------------------------------------
Greg Clayton73844aa2012-08-22 17:17:09 +0000142
Jim Ingham94a5d0d2012-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 Clayton73844aa2012-08-22 17:17:09 +0000239 ThreadProperties (false),
Chris Lattner24943d22010-06-08 16:52:24 +0000240 UserID (tid),
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000241 Broadcaster(&process.GetTarget().GetDebugger(), Thread::GetStaticBroadcasterClass().AsCString()),
242 m_process_wp (process.shared_from_this()),
Greg Clayton643ee732010-08-04 01:40:35 +0000243 m_actual_stop_info_sp (),
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000244 m_index_id (process.GetNextThreadIndexID ()),
Chris Lattner24943d22010-06-08 16:52:24 +0000245 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000246 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +0000247 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +0000248 m_plan_stack (),
Chris Lattner24943d22010-06-08 16:52:24 +0000249 m_completed_plan_stack(),
Greg Clayton2450cb12012-03-29 01:41:38 +0000250 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000251 m_curr_frames_sp (),
252 m_prev_frames_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000253 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +0000254 m_resume_state (eStateRunning),
Jim Ingham149d1f52012-01-31 23:09:20 +0000255 m_temporary_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +0000256 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000257 m_destroy_called (false),
258 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +0000259
Chris Lattner24943d22010-06-08 16:52:24 +0000260{
Greg Claytone005f2c2010-11-06 01:53:30 +0000261 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000262 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000263 log->Printf ("%p Thread::Thread(tid = 0x%4.4llx)", this, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000264
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000265 CheckInWithManager();
Chris Lattner24943d22010-06-08 16:52:24 +0000266 QueueFundamentalPlan(true);
267}
268
269
270Thread::~Thread()
271{
Greg Claytone005f2c2010-11-06 01:53:30 +0000272 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000273 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000274 log->Printf ("%p Thread::~Thread(tid = 0x%4.4llx)", this, GetID());
Jim Inghamcdea2362010-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{
282 m_plan_stack.clear();
283 m_discarded_plan_stack.clear();
284 m_completed_plan_stack.clear();
Jim Inghame93055a2012-04-10 00:44:25 +0000285 m_actual_stop_info_sp.reset();
Jim Inghamcdea2362010-11-18 02:47:07 +0000286 m_destroy_called = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000287}
288
Jim Ingham94a5d0d2012-10-10 18:32:14 +0000289void
290Thread::BroadcastSelectedFrameChange(StackID &new_frame_id)
291{
292 if (EventTypeHasListeners(eBroadcastBitSelectedFrameChanged))
293 BroadcastEvent(eBroadcastBitSelectedFrameChanged, new ThreadEventData (this->shared_from_this(), new_frame_id));
294}
295
296uint32_t
297Thread::SetSelectedFrame (lldb_private::StackFrame *frame, bool broadcast)
298{
299 uint32_t ret_value = GetStackFrameList()->SetSelectedFrame(frame);
300 if (broadcast)
301 BroadcastSelectedFrameChange(frame->GetStackID());
302 return ret_value;
303}
304
305bool
306Thread::SetSelectedFrameByIndex (uint32_t frame_idx, bool broadcast)
307{
308 StackFrameSP frame_sp(GetStackFrameList()->GetFrameAtIndex (frame_idx));
309 if (frame_sp)
310 {
311 GetStackFrameList()->SetSelectedFrame(frame_sp.get());
312 if (broadcast)
313 BroadcastSelectedFrameChange(frame_sp->GetStackID());
314 return true;
315 }
316 else
317 return false;
318}
319
320
Jim Ingham6297a3a2010-10-20 00:39:53 +0000321lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000322Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +0000323{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000324 ThreadPlanSP plan_sp (GetCompletedPlan());
Jim Ingham707b7a82012-05-01 18:38:37 +0000325 if (plan_sp && plan_sp->PlanSucceeded())
Jim Ingham1586d972011-12-17 01:35:57 +0000326 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000327 else
Jim Ingham24e0d612011-08-09 00:32:52 +0000328 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000329 ProcessSP process_sp (GetProcess());
330 if (process_sp
331 && m_actual_stop_info_sp
Jim Ingham24e0d612011-08-09 00:32:52 +0000332 && m_actual_stop_info_sp->IsValid()
Greg Claytonf4124de2012-02-21 00:09:25 +0000333 && m_thread_stop_reason_stop_id == process_sp->GetStopID())
Jim Ingham24e0d612011-08-09 00:32:52 +0000334 return m_actual_stop_info_sp;
335 else
336 return GetPrivateStopReason ();
337 }
Chris Lattner24943d22010-06-08 16:52:24 +0000338}
339
Greg Clayton3acaa922012-09-25 02:40:06 +0000340lldb::StopReason
341Thread::GetStopReason()
342{
343 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
344 if (stop_info_sp)
Filipe Cabecinhasc50a1dd2012-09-28 15:55:43 +0000345 return stop_info_sp->GetStopReason();
Greg Clayton3acaa922012-09-25 02:40:06 +0000346 return eStopReasonNone;
347}
348
349
350
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000351void
352Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
353{
354 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham24e0d612011-08-09 00:32:52 +0000355 if (m_actual_stop_info_sp)
356 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Claytonf4124de2012-02-21 00:09:25 +0000357 ProcessSP process_sp (GetProcess());
358 if (process_sp)
359 m_thread_stop_reason_stop_id = process_sp->GetStopID();
360 else
361 m_thread_stop_reason_stop_id = UINT32_MAX;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000362}
363
364void
365Thread::SetStopInfoToNothing()
366{
367 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
368 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
369 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
370}
371
Chris Lattner24943d22010-06-08 16:52:24 +0000372bool
373Thread::ThreadStoppedForAReason (void)
374{
Jim Ingham9880efa2012-08-11 00:35:26 +0000375 return (bool) GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +0000376}
377
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000378bool
379Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
380{
381 if (!SaveFrameZeroState(saved_state.register_backup))
382 return false;
383
384 saved_state.stop_info_sp = GetStopInfo();
Greg Claytonf4124de2012-02-21 00:09:25 +0000385 ProcessSP process_sp (GetProcess());
386 if (process_sp)
387 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham36de3c02012-09-07 23:36:43 +0000388 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
389
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000390 return true;
391}
392
393bool
394Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
395{
396 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham11a837d2011-01-25 02:47:23 +0000397 if (saved_state.stop_info_sp)
398 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000399 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham36de3c02012-09-07 23:36:43 +0000400 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000401 return true;
402}
403
Chris Lattner24943d22010-06-08 16:52:24 +0000404StateType
405Thread::GetState() const
406{
407 // If any other threads access this we will need a mutex for it
408 Mutex::Locker locker(m_state_mutex);
409 return m_state;
410}
411
412void
413Thread::SetState(StateType state)
414{
415 Mutex::Locker locker(m_state_mutex);
416 m_state = state;
417}
418
419void
420Thread::WillStop()
421{
422 ThreadPlan *current_plan = GetCurrentPlan();
423
424 // FIXME: I may decide to disallow threads with no plans. In which
425 // case this should go to an assert.
426
427 if (!current_plan)
428 return;
429
430 current_plan->WillStop();
431}
432
433void
434Thread::SetupForResume ()
435{
436 if (GetResumeState() != eStateSuspended)
437 {
438
439 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
440 // telling the current plan it will resume, since we might change what the current
441 // plan is.
442
Jim Ingham6bc24c12012-10-16 00:09:33 +0000443 StopReason stop_reason = lldb::eStopReasonInvalid;
444 StopInfoSP stop_info_sp = GetStopInfo();
445 if (stop_info_sp.get())
446 stop_reason = stop_info_sp->GetStopReason();
447 if (stop_reason == lldb::eStopReasonBreakpoint)
Chris Lattner24943d22010-06-08 16:52:24 +0000448 {
449 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
450 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000451
452 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000453
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000454 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
455 {
456 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
457 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000458 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000459 ThreadPlanSP step_bp_plan_sp;
460 step_bp_plan->SetPrivate (true);
461
Chris Lattner24943d22010-06-08 16:52:24 +0000462 if (GetCurrentPlan()->RunState() != eStateStepping)
463 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000464 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000465 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000466 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000467 QueueThreadPlan (step_bp_plan_sp, false);
468 }
469 }
470 }
471 }
472}
473
474bool
475Thread::WillResume (StateType resume_state)
476{
477 // At this point clear the completed plan stack.
478 m_completed_plan_stack.clear();
479 m_discarded_plan_stack.clear();
480
Greg Clayton3acaa922012-09-25 02:40:06 +0000481 m_temporary_resume_state = resume_state;
Jim Ingham149d1f52012-01-31 23:09:20 +0000482
483 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
484 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
485 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
486 // about the fact that we are resuming...
Greg Claytonf4124de2012-02-21 00:09:25 +0000487 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham149d1f52012-01-31 23:09:20 +0000488 if (m_thread_stop_reason_stop_id == process_stop_id &&
489 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
490 {
491 StopInfo *stop_info = GetPrivateStopReason().get();
492 if (stop_info)
493 stop_info->WillResume (resume_state);
494 }
Chris Lattner24943d22010-06-08 16:52:24 +0000495
496 // Tell all the plans that we are about to resume in case they need to clear any state.
497 // We distinguish between the plan on the top of the stack and the lower
498 // plans in case a plan needs to do any special business before it runs.
499
500 ThreadPlan *plan_ptr = GetCurrentPlan();
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000501 bool need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000502
503 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
504 {
505 plan_ptr->WillResume (resume_state, false);
506 }
Greg Clayton643ee732010-08-04 01:40:35 +0000507
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000508 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
509 // In that case, don't reset it here.
510
Jim Ingham6bc24c12012-10-16 00:09:33 +0000511 if (need_to_resume && resume_state != eStateSuspended)
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000512 {
513 m_actual_stop_info_sp.reset();
514 }
515
516 return need_to_resume;
Chris Lattner24943d22010-06-08 16:52:24 +0000517}
518
519void
520Thread::DidResume ()
521{
522 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
523}
524
525bool
526Thread::ShouldStop (Event* event_ptr)
527{
528 ThreadPlan *current_plan = GetCurrentPlan();
529 bool should_stop = true;
530
Greg Claytone005f2c2010-11-06 01:53:30 +0000531 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham149d1f52012-01-31 23:09:20 +0000532
533 if (GetResumeState () == eStateSuspended)
534 {
535 if (log)
536 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
537 __FUNCTION__,
538 GetID ());
539// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
540// __FUNCTION__,
541// GetID (),
542// GetRegisterContext()->GetPC());
543 return false;
544 }
545
546 if (GetTemporaryResumeState () == eStateSuspended)
547 {
548 if (log)
549 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
550 __FUNCTION__,
551 GetID ());
552// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
553// __FUNCTION__,
554// GetID (),
555// GetRegisterContext()->GetPC());
556 return false;
557 }
558
559 if (ThreadStoppedForAReason() == false)
560 {
561 if (log)
562 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
563 __FUNCTION__,
564 GetID (),
565 GetRegisterContext()->GetPC());
566 return false;
567 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000568
Chris Lattner24943d22010-06-08 16:52:24 +0000569 if (log)
570 {
Jim Ingham149d1f52012-01-31 23:09:20 +0000571 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
572 __FUNCTION__,
573 GetID (),
574 GetRegisterContext()->GetPC());
Jim Inghame8f4e112011-10-15 00:23:43 +0000575 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000576 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000577 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000578 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000579 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000580 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000581
582 // The top most plan always gets to do the trace log...
583 current_plan->DoTraceLog ();
Jim Inghame787c7e2012-04-20 21:16:56 +0000584
585 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
586 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
587 // do any more work on this stop.
588 StopInfoSP private_stop_info (GetPrivateStopReason());
589 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
590 {
591 if (log)
592 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
593 return false;
594 }
Chris Lattner24943d22010-06-08 16:52:24 +0000595
Jim Ingham98d50212012-09-05 21:12:49 +0000596 // If we've already been restarted, don't query the plans since the state they would examine is not current.
597 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
598 return false;
599
600 // Before the plans see the state of the world, calculate the current inlined depth.
601 GetStackFrameList()->CalculateCurrentInlinedDepth();
602
Jim Inghamad382c52011-12-03 01:52:59 +0000603 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
604 // If that plan is still working, then we don't need to do any more work. If the plan that explains
605 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
606 // whether they still need to do more work.
607
608 bool done_processing_current_plan = false;
609
610 if (!current_plan->PlanExplainsStop())
611 {
612 if (current_plan->TracerExplainsStop())
613 {
614 done_processing_current_plan = true;
615 should_stop = false;
616 }
617 else
618 {
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000619 // If the current plan doesn't explain the stop, then find one that
Jim Inghamad382c52011-12-03 01:52:59 +0000620 // does and let it handle the situation.
621 ThreadPlan *plan_ptr = current_plan;
622 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
623 {
624 if (plan_ptr->PlanExplainsStop())
625 {
626 should_stop = plan_ptr->ShouldStop (event_ptr);
627
628 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
629 // and all the plans below it off the stack.
630
631 if (plan_ptr->MischiefManaged())
632 {
Jim Inghamd1ec3d62012-05-11 23:49:49 +0000633 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000634 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Inghamad382c52011-12-03 01:52:59 +0000635
636 do
637 {
638 if (should_stop)
639 current_plan->WillStop();
640 PopPlan();
641 }
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000642 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
643 // Now, if the responsible plan was not "Okay to discard" then we're done,
644 // otherwise we forward this to the next plan in the stack below.
645 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
646 done_processing_current_plan = true;
647 else
648 done_processing_current_plan = false;
Jim Inghamad382c52011-12-03 01:52:59 +0000649 }
650 else
651 done_processing_current_plan = true;
652
653 break;
654 }
655
656 }
657 }
658 }
659
660 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000661 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000662 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000663
Jim Inghame8f4e112011-10-15 00:23:43 +0000664 if (log)
665 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
666
Jim Inghamf9f40c22011-02-08 05:20:59 +0000667 // We're starting from the base plan, so just let it decide;
668 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000669 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000670 should_stop = current_plan->ShouldStop (event_ptr);
671 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000672 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000673 }
674 else
675 {
676 // Otherwise, don't let the base plan override what the other plans say to do, since
677 // presumably if there were other plans they would know what to do...
678 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000679 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000680 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000681 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000682
683 should_stop = current_plan->ShouldStop(event_ptr);
684 if (log)
685 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
686 if (current_plan->MischiefManaged())
687 {
688 if (should_stop)
689 current_plan->WillStop();
690
691 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
692 // Otherwise, see if the plan's parent wants to stop.
693
694 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
695 {
696 PopPlan();
697 break;
698 }
699 else
700 {
701
702 PopPlan();
703
704 current_plan = GetCurrentPlan();
705 if (current_plan == NULL)
706 {
707 break;
708 }
709 }
Chris Lattner24943d22010-06-08 16:52:24 +0000710 }
711 else
712 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000713 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000714 }
Chris Lattner24943d22010-06-08 16:52:24 +0000715 }
716 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000717
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000718 if (over_ride_stop)
719 should_stop = false;
Jim Ingham88e3de22012-05-03 21:19:36 +0000720
721 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
722 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
723 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
724 // This code clears stale plans off the stack.
725
726 if (should_stop)
727 {
728 ThreadPlan *plan_ptr = GetCurrentPlan();
729 while (!PlanIsBasePlan(plan_ptr))
730 {
731 bool stale = plan_ptr->IsPlanStale ();
732 ThreadPlan *examined_plan = plan_ptr;
733 plan_ptr = GetPreviousPlan (examined_plan);
734
735 if (stale)
736 {
737 if (log)
738 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
739 DiscardThreadPlansUpToPlan(examined_plan);
740 }
741 }
742 }
743
Chris Lattner24943d22010-06-08 16:52:24 +0000744 }
Chris Lattner24943d22010-06-08 16:52:24 +0000745
Jim Inghame8f4e112011-10-15 00:23:43 +0000746 if (log)
747 {
748 StreamString s;
749 s.IndentMore();
750 DumpThreadPlans(&s);
751 log->Printf ("Plan stack final state:\n%s", s.GetData());
752 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
753 }
Chris Lattner24943d22010-06-08 16:52:24 +0000754 return should_stop;
755}
756
757Vote
758Thread::ShouldReportStop (Event* event_ptr)
759{
760 StateType thread_state = GetResumeState ();
Jim Ingham149d1f52012-01-31 23:09:20 +0000761 StateType temp_thread_state = GetTemporaryResumeState();
762
Greg Claytone005f2c2010-11-06 01:53:30 +0000763 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000764
765 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
766 {
767 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000768 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
Chris Lattner24943d22010-06-08 16:52:24 +0000769 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000770 }
Chris Lattner24943d22010-06-08 16:52:24 +0000771
Jim Ingham149d1f52012-01-31 23:09:20 +0000772 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
773 {
774 if (log)
775 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
776 return eVoteNoOpinion;
777 }
778
779 if (!ThreadStoppedForAReason())
780 {
781 if (log)
782 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
783 return eVoteNoOpinion;
784 }
785
Chris Lattner24943d22010-06-08 16:52:24 +0000786 if (m_completed_plan_stack.size() > 0)
787 {
788 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000789 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000790 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for complete stack's back plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000791 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
792 }
793 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000794 {
795 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000796 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000797 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000798 }
Chris Lattner24943d22010-06-08 16:52:24 +0000799}
800
801Vote
802Thread::ShouldReportRun (Event* event_ptr)
803{
804 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000805
Chris Lattner24943d22010-06-08 16:52:24 +0000806 if (thread_state == eStateSuspended
807 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000808 {
Chris Lattner24943d22010-06-08 16:52:24 +0000809 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000810 }
811
812 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000813 if (m_completed_plan_stack.size() > 0)
814 {
815 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000816 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000817 log->Printf ("Current Plan for thread %d (0x%4.4llx): %s being asked whether we should report run.",
Jim Inghamac959662011-01-24 06:34:17 +0000818 GetIndexID(),
819 GetID(),
820 m_completed_plan_stack.back()->GetName());
821
Chris Lattner24943d22010-06-08 16:52:24 +0000822 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
823 }
824 else
Jim Inghamac959662011-01-24 06:34:17 +0000825 {
826 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000827 log->Printf ("Current Plan for thread %d (0x%4.4llx): %s being asked whether we should report run.",
Jim Inghamac959662011-01-24 06:34:17 +0000828 GetIndexID(),
829 GetID(),
830 GetCurrentPlan()->GetName());
831
Chris Lattner24943d22010-06-08 16:52:24 +0000832 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000833 }
Chris Lattner24943d22010-06-08 16:52:24 +0000834}
835
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000836bool
837Thread::MatchesSpec (const ThreadSpec *spec)
838{
839 if (spec == NULL)
840 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000841
Jim Inghama2664912012-03-07 22:03:04 +0000842 return spec->ThreadPassesBasicTests(*this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000843}
844
Chris Lattner24943d22010-06-08 16:52:24 +0000845void
846Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
847{
848 if (thread_plan_sp)
849 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000850 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
851 if (!thread_plan_sp->GetThreadPlanTracer())
852 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000853 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000854
Chris Lattner24943d22010-06-08 16:52:24 +0000855 thread_plan_sp->DidPush();
856
Greg Claytone005f2c2010-11-06 01:53:30 +0000857 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000858 if (log)
859 {
860 StreamString s;
861 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Clayton444e35b2011-10-19 18:09:39 +0000862 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
Chris Lattner24943d22010-06-08 16:52:24 +0000863 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000864 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000865 }
866 }
867}
868
869void
870Thread::PopPlan ()
871{
Greg Claytone005f2c2010-11-06 01:53:30 +0000872 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000873
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000874 if (m_plan_stack.size() <= 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000875 return;
876 else
877 {
878 ThreadPlanSP &plan = m_plan_stack.back();
879 if (log)
880 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000881 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000882 }
883 m_completed_plan_stack.push_back (plan);
884 plan->WillPop();
885 m_plan_stack.pop_back();
886 }
887}
888
889void
890Thread::DiscardPlan ()
891{
892 if (m_plan_stack.size() > 1)
893 {
894 ThreadPlanSP &plan = m_plan_stack.back();
895 m_discarded_plan_stack.push_back (plan);
896 plan->WillPop();
897 m_plan_stack.pop_back();
898 }
899}
900
901ThreadPlan *
902Thread::GetCurrentPlan ()
903{
Jim Ingham2f41d272012-04-19 00:17:05 +0000904 // There will always be at least the base plan. If somebody is mucking with a
905 // thread with an empty plan stack, we should assert right away.
906 assert (!m_plan_stack.empty());
907
908 return m_plan_stack.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000909}
910
911ThreadPlanSP
912Thread::GetCompletedPlan ()
913{
914 ThreadPlanSP empty_plan_sp;
915 if (!m_completed_plan_stack.empty())
916 {
917 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
918 {
919 ThreadPlanSP completed_plan_sp;
920 completed_plan_sp = m_completed_plan_stack[i];
921 if (!completed_plan_sp->GetPrivate ())
922 return completed_plan_sp;
923 }
924 }
925 return empty_plan_sp;
926}
927
Jim Ingham1586d972011-12-17 01:35:57 +0000928ValueObjectSP
929Thread::GetReturnValueObject ()
930{
931 if (!m_completed_plan_stack.empty())
932 {
933 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
934 {
935 ValueObjectSP return_valobj_sp;
936 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
937 if (return_valobj_sp)
938 return return_valobj_sp;
939 }
940 }
941 return ValueObjectSP();
942}
943
Chris Lattner24943d22010-06-08 16:52:24 +0000944bool
945Thread::IsThreadPlanDone (ThreadPlan *plan)
946{
Chris Lattner24943d22010-06-08 16:52:24 +0000947 if (!m_completed_plan_stack.empty())
948 {
949 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
950 {
951 if (m_completed_plan_stack[i].get() == plan)
952 return true;
953 }
954 }
955 return false;
956}
957
958bool
959Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
960{
Chris Lattner24943d22010-06-08 16:52:24 +0000961 if (!m_discarded_plan_stack.empty())
962 {
963 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
964 {
965 if (m_discarded_plan_stack[i].get() == plan)
966 return true;
967 }
968 }
969 return false;
970}
971
972ThreadPlan *
973Thread::GetPreviousPlan (ThreadPlan *current_plan)
974{
975 if (current_plan == NULL)
976 return NULL;
977
978 int stack_size = m_completed_plan_stack.size();
979 for (int i = stack_size - 1; i > 0; i--)
980 {
981 if (current_plan == m_completed_plan_stack[i].get())
982 return m_completed_plan_stack[i-1].get();
983 }
984
985 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
986 {
Chris Lattner24943d22010-06-08 16:52:24 +0000987 if (m_plan_stack.size() > 0)
988 return m_plan_stack.back().get();
989 else
990 return NULL;
991 }
992
993 stack_size = m_plan_stack.size();
994 for (int i = stack_size - 1; i > 0; i--)
995 {
996 if (current_plan == m_plan_stack[i].get())
997 return m_plan_stack[i-1].get();
998 }
999 return NULL;
1000}
1001
1002void
1003Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
1004{
1005 if (abort_other_plans)
1006 DiscardThreadPlans(true);
1007
1008 PushPlan (thread_plan_sp);
1009}
1010
Jim Ingham745ac7a2010-11-11 19:26:09 +00001011
1012void
1013Thread::EnableTracer (bool value, bool single_stepping)
1014{
1015 int stack_size = m_plan_stack.size();
1016 for (int i = 0; i < stack_size; i++)
1017 {
1018 if (m_plan_stack[i]->GetThreadPlanTracer())
1019 {
1020 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
1021 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
1022 }
1023 }
1024}
1025
1026void
1027Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
1028{
1029 int stack_size = m_plan_stack.size();
1030 for (int i = 0; i < stack_size; i++)
1031 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
1032}
1033
Chris Lattner24943d22010-06-08 16:52:24 +00001034void
Jim Inghamea9d4262010-11-05 19:25:48 +00001035Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
1036{
Jim Ingham88e3de22012-05-03 21:19:36 +00001037 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
1038}
1039
1040void
1041Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
1042{
Greg Claytone005f2c2010-11-06 01:53:30 +00001043 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +00001044 if (log)
1045 {
Jim Ingham88e3de22012-05-03 21:19:36 +00001046 log->Printf("Discarding thread plans for thread tid = 0x%4.4llx, up to %p", GetID(), up_to_plan_ptr);
Jim Inghamea9d4262010-11-05 19:25:48 +00001047 }
1048
1049 int stack_size = m_plan_stack.size();
1050
1051 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
1052 // stack, and if so discard up to and including it.
1053
Jim Ingham88e3de22012-05-03 21:19:36 +00001054 if (up_to_plan_ptr == NULL)
Jim Inghamea9d4262010-11-05 19:25:48 +00001055 {
1056 for (int i = stack_size - 1; i > 0; i--)
1057 DiscardPlan();
1058 }
1059 else
1060 {
1061 bool found_it = false;
1062 for (int i = stack_size - 1; i > 0; i--)
1063 {
Jim Ingham88e3de22012-05-03 21:19:36 +00001064 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +00001065 found_it = true;
1066 }
1067 if (found_it)
1068 {
1069 bool last_one = false;
1070 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
1071 {
Jim Ingham88e3de22012-05-03 21:19:36 +00001072 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +00001073 last_one = true;
1074 DiscardPlan();
1075 }
1076 }
1077 }
1078 return;
1079}
1080
1081void
Chris Lattner24943d22010-06-08 16:52:24 +00001082Thread::DiscardThreadPlans(bool force)
1083{
Greg Claytone005f2c2010-11-06 01:53:30 +00001084 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +00001085 if (log)
1086 {
Greg Clayton444e35b2011-10-19 18:09:39 +00001087 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +00001088 }
1089
1090 if (force)
1091 {
1092 int stack_size = m_plan_stack.size();
1093 for (int i = stack_size - 1; i > 0; i--)
1094 {
1095 DiscardPlan();
1096 }
1097 return;
1098 }
1099
1100 while (1)
1101 {
1102
1103 int master_plan_idx;
Jim Inghamf1dbb712012-09-11 00:09:25 +00001104 bool discard = true;
Chris Lattner24943d22010-06-08 16:52:24 +00001105
1106 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
1107 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
1108 {
1109 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
1110 {
1111 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
1112 break;
1113 }
1114 }
1115
1116 if (discard)
1117 {
1118 // First pop all the dependent plans:
1119 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
1120 {
1121
1122 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
1123 // for the plan leaves it in a state that it is safe to pop the plan
1124 // with no more notice?
1125 DiscardPlan();
1126 }
1127
1128 // Now discard the master plan itself.
1129 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
1130 // discard it's dependent plans, but not it...
1131 if (master_plan_idx > 0)
1132 {
1133 DiscardPlan();
1134 }
1135 }
1136 else
1137 {
1138 // If the master plan doesn't want to get discarded, then we're done.
1139 break;
1140 }
1141
1142 }
Chris Lattner24943d22010-06-08 16:52:24 +00001143}
1144
Jim Ingham2bcbaf62012-04-09 22:37:39 +00001145bool
1146Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1147{
1148 if (plan_ptr->IsBasePlan())
1149 return true;
1150 else if (m_plan_stack.size() == 0)
1151 return false;
1152 else
1153 return m_plan_stack[0].get() == plan_ptr;
1154}
1155
Chris Lattner24943d22010-06-08 16:52:24 +00001156ThreadPlan *
1157Thread::QueueFundamentalPlan (bool abort_other_plans)
1158{
1159 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1160 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1161 return thread_plan_sp.get();
1162}
1163
1164ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001165Thread::QueueThreadPlanForStepSingleInstruction
1166(
1167 bool step_over,
1168 bool abort_other_plans,
1169 bool stop_other_threads
1170)
Chris Lattner24943d22010-06-08 16:52:24 +00001171{
1172 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1173 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1174 return thread_plan_sp.get();
1175}
1176
1177ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001178Thread::QueueThreadPlanForStepRange
1179(
1180 bool abort_other_plans,
1181 StepType type,
1182 const AddressRange &range,
1183 const SymbolContext &addr_context,
1184 lldb::RunMode stop_other_threads,
1185 bool avoid_code_without_debug_info
1186)
Chris Lattner24943d22010-06-08 16:52:24 +00001187{
1188 ThreadPlanSP thread_plan_sp;
1189 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001190 {
1191 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1192 if (avoid_code_without_debug_info)
1193 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1194 else
1195 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1196 thread_plan_sp.reset (plan);
1197 }
Chris Lattner24943d22010-06-08 16:52:24 +00001198 else
1199 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1200
1201 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1202 return thread_plan_sp.get();
1203}
1204
1205
1206ThreadPlan *
1207Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1208{
1209 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1210 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1211 return thread_plan_sp.get();
1212}
1213
1214ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001215Thread::QueueThreadPlanForStepOut
1216(
1217 bool abort_other_plans,
1218 SymbolContext *addr_context,
1219 bool first_insn,
1220 bool stop_other_threads,
1221 Vote stop_vote,
1222 Vote run_vote,
1223 uint32_t frame_idx
1224)
Chris Lattner24943d22010-06-08 16:52:24 +00001225{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001226 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1227 addr_context,
1228 first_insn,
1229 stop_other_threads,
1230 stop_vote,
1231 run_vote,
1232 frame_idx));
Sean Callananf6d5fea2012-07-31 22:19:25 +00001233
1234 if (thread_plan_sp->ValidatePlan(NULL))
1235 {
1236 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1237 return thread_plan_sp.get();
1238 }
1239 else
1240 {
1241 return NULL;
1242 }
Chris Lattner24943d22010-06-08 16:52:24 +00001243}
1244
1245ThreadPlan *
Jim Ingham038fa8e2012-05-10 01:35:39 +00001246Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner24943d22010-06-08 16:52:24 +00001247{
Jim Ingham038fa8e2012-05-10 01:35:39 +00001248 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Inghamad382c52011-12-03 01:52:59 +00001249 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1250 return NULL;
1251
Chris Lattner24943d22010-06-08 16:52:24 +00001252 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1253 return thread_plan_sp.get();
1254}
1255
1256ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001257Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1258 Address& function,
1259 lldb::addr_t arg,
1260 bool stop_other_threads,
1261 bool discard_on_error)
1262{
Jim Ingham016ef882011-12-22 19:12:40 +00001263 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner24943d22010-06-08 16:52:24 +00001264 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1265 return thread_plan_sp.get();
1266}
1267
1268ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001269Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1270 Address &target_addr,
1271 bool stop_other_threads)
1272{
1273 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1274 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1275 return thread_plan_sp.get();
1276}
1277
1278ThreadPlan *
1279Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001280 lldb::addr_t *address_list,
1281 size_t num_addresses,
1282 bool stop_other_threads,
1283 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001284{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001285 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +00001286 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1287 return thread_plan_sp.get();
1288
1289}
1290
1291uint32_t
1292Thread::GetIndexID () const
1293{
1294 return m_index_id;
1295}
1296
1297void
1298Thread::DumpThreadPlans (lldb_private::Stream *s) const
1299{
1300 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +00001301 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +00001302 s->Indent();
Greg Clayton444e35b2011-10-19 18:09:39 +00001303 s->Printf ("Plan Stack for thread #%u: tid = 0x%4.4llx, stack_size = %d\n", GetIndexID(), GetID(), stack_size);
Greg Claytonf04d6612010-09-03 22:45:01 +00001304 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +00001305 {
Chris Lattner24943d22010-06-08 16:52:24 +00001306 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +00001307 s->Indent();
1308 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +00001309 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +00001310 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +00001311 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +00001312 }
1313
Chris Lattner24943d22010-06-08 16:52:24 +00001314 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001315 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001316 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001317 s->Indent();
1318 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1319 for (i = stack_size - 1; i >= 0; i--)
1320 {
1321 s->IndentMore();
1322 s->Indent();
1323 s->Printf ("Element %d: ", i);
1324 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1325 s->EOL();
1326 s->IndentLess();
1327 }
Chris Lattner24943d22010-06-08 16:52:24 +00001328 }
1329
1330 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001331 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001332 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001333 s->Indent();
1334 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1335 for (i = stack_size - 1; i >= 0; i--)
1336 {
1337 s->IndentMore();
1338 s->Indent();
1339 s->Printf ("Element %d: ", i);
1340 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1341 s->EOL();
1342 s->IndentLess();
1343 }
Chris Lattner24943d22010-06-08 16:52:24 +00001344 }
1345
1346}
1347
Greg Clayton289afcb2012-02-18 05:35:26 +00001348TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001349Thread::CalculateTarget ()
1350{
Greg Claytonf4124de2012-02-21 00:09:25 +00001351 TargetSP target_sp;
1352 ProcessSP process_sp(GetProcess());
1353 if (process_sp)
1354 target_sp = process_sp->CalculateTarget();
1355 return target_sp;
1356
Chris Lattner24943d22010-06-08 16:52:24 +00001357}
1358
Greg Clayton289afcb2012-02-18 05:35:26 +00001359ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001360Thread::CalculateProcess ()
1361{
Greg Claytonf4124de2012-02-21 00:09:25 +00001362 return GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001363}
1364
Greg Clayton289afcb2012-02-18 05:35:26 +00001365ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001366Thread::CalculateThread ()
1367{
Greg Clayton289afcb2012-02-18 05:35:26 +00001368 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001369}
1370
Greg Clayton289afcb2012-02-18 05:35:26 +00001371StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001372Thread::CalculateStackFrame ()
1373{
Greg Clayton289afcb2012-02-18 05:35:26 +00001374 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001375}
1376
1377void
Greg Claytona830adb2010-10-04 01:05:56 +00001378Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001379{
Greg Claytonf4124de2012-02-21 00:09:25 +00001380 exe_ctx.SetContext (shared_from_this());
Chris Lattner24943d22010-06-08 16:52:24 +00001381}
1382
Greg Clayton782b9cc2010-08-25 00:35:26 +00001383
Greg Clayton2450cb12012-03-29 01:41:38 +00001384StackFrameListSP
Greg Clayton782b9cc2010-08-25 00:35:26 +00001385Thread::GetStackFrameList ()
1386{
Greg Clayton2450cb12012-03-29 01:41:38 +00001387 StackFrameListSP frame_list_sp;
1388 Mutex::Locker locker(m_frame_mutex);
1389 if (m_curr_frames_sp)
1390 {
1391 frame_list_sp = m_curr_frames_sp;
1392 }
1393 else
1394 {
1395 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1396 m_curr_frames_sp = frame_list_sp;
1397 }
1398 return frame_list_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +00001399}
1400
Greg Clayton782b9cc2010-08-25 00:35:26 +00001401void
1402Thread::ClearStackFrames ()
1403{
Greg Clayton2450cb12012-03-29 01:41:38 +00001404 Mutex::Locker locker(m_frame_mutex);
1405
Jim Inghambf97d742012-02-29 03:40:22 +00001406 // Only store away the old "reference" StackFrameList if we got all its frames:
1407 // FIXME: At some point we can try to splice in the frames we have fetched into
1408 // the new frame as we make it, but let's not try that now.
1409 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Claytonc51ffbf2011-08-12 21:40:01 +00001410 m_prev_frames_sp.swap (m_curr_frames_sp);
1411 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +00001412}
1413
1414lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001415Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1416{
Greg Clayton2450cb12012-03-29 01:41:38 +00001417 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001418}
1419
Jim Inghama17a81a2012-09-12 00:40:39 +00001420
1421Error
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001422Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Inghama17a81a2012-09-12 00:40:39 +00001423{
1424 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1425 Error return_error;
1426
1427 if (!frame_sp)
1428 {
1429 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%llx.", frame_idx, GetID());
1430 }
1431
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001432 return ReturnFromFrame(frame_sp, return_value_sp, broadcast);
Jim Inghama17a81a2012-09-12 00:40:39 +00001433}
1434
1435Error
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001436Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp, bool broadcast)
Jim Inghama17a81a2012-09-12 00:40:39 +00001437{
1438 Error return_error;
1439
1440 if (!frame_sp)
1441 {
1442 return_error.SetErrorString("Can't return to a null frame.");
1443 return return_error;
1444 }
1445
1446 Thread *thread = frame_sp->GetThread().get();
Jim Inghamf59388a2012-09-14 02:14:15 +00001447 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1448 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Inghama17a81a2012-09-12 00:40:39 +00001449
1450 if (return_value_sp)
Jim Ingham46657452012-09-27 01:15:29 +00001451 {
Jim Inghama17a81a2012-09-12 00:40:39 +00001452 lldb::ABISP abi = thread->GetProcess()->GetABI();
1453 if (!abi)
1454 {
1455 return_error.SetErrorString("Could not find ABI to set return value.");
Jim Ingham6f01c932012-10-12 17:34:26 +00001456 return return_error;
Jim Inghama17a81a2012-09-12 00:40:39 +00001457 }
Jim Ingham46657452012-09-27 01:15:29 +00001458 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1459
1460 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1461 // Turn that back on when that works.
1462 if (0 && sc.function != NULL)
1463 {
1464 Type *function_type = sc.function->GetType();
1465 if (function_type)
1466 {
1467 clang_type_t return_type = sc.function->GetReturnClangType();
1468 if (return_type)
1469 {
1470 ClangASTType ast_type (function_type->GetClangAST(), return_type);
1471 StreamString s;
1472 ast_type.DumpTypeDescription(&s);
1473 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type);
1474 if (cast_value_sp)
1475 {
1476 cast_value_sp->SetFormat(eFormatHex);
1477 return_value_sp = cast_value_sp;
1478 }
1479 }
1480 }
1481 }
1482
Jim Inghamf59388a2012-09-14 02:14:15 +00001483 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Inghama17a81a2012-09-12 00:40:39 +00001484 if (!return_error.Success())
1485 return return_error;
1486 }
1487
1488 // Now write the return registers for the chosen frame:
Jim Inghamf59388a2012-09-14 02:14:15 +00001489 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
1490 // cook their data
1491 bool copy_success = thread->GetStackFrameAtIndex(0)->GetRegisterContext()->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1492 if (copy_success)
Jim Inghama17a81a2012-09-12 00:40:39 +00001493 {
1494 thread->DiscardThreadPlans(true);
Jim Inghamf59388a2012-09-14 02:14:15 +00001495 thread->ClearStackFrames();
Jim Ingham94a5d0d2012-10-10 18:32:14 +00001496 if (broadcast && EventTypeHasListeners(eBroadcastBitStackChanged))
1497 BroadcastEvent(eBroadcastBitStackChanged, new ThreadEventData (this->shared_from_this()));
Jim Inghama17a81a2012-09-12 00:40:39 +00001498 return return_error;
1499 }
1500 else
1501 {
1502 return_error.SetErrorString("Could not reset register values.");
1503 return return_error;
Jim Inghama17a81a2012-09-12 00:40:39 +00001504 }
1505}
1506
Chris Lattner24943d22010-06-08 16:52:24 +00001507void
Greg Claytona830adb2010-10-04 01:05:56 +00001508Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001509{
Greg Claytonf4124de2012-02-21 00:09:25 +00001510 ExecutionContext exe_ctx (shared_from_this());
1511 Process *process = exe_ctx.GetProcessPtr();
1512 if (process == NULL)
1513 return;
1514
Greg Clayton567e7f32011-09-22 04:58:26 +00001515 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +00001516 SymbolContext frame_sc;
Greg Claytona830adb2010-10-04 01:05:56 +00001517 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +00001518 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001519 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001520 if (frame_sp)
1521 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001522 exe_ctx.SetFrameSP(frame_sp);
1523 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001524 }
1525 }
1526
Greg Claytonf4124de2012-02-21 00:09:25 +00001527 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Claytona830adb2010-10-04 01:05:56 +00001528 assert (thread_format);
1529 const char *end = NULL;
1530 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001531 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001532 &exe_ctx,
1533 NULL,
1534 strm,
1535 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001536}
1537
Greg Clayton990de7b2010-11-18 23:32:35 +00001538void
Caroline Tice2a456812011-03-10 22:14:10 +00001539Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001540{
Greg Clayton990de7b2010-11-18 23:32:35 +00001541}
Jim Ingham20594b12010-09-08 03:14:33 +00001542
Greg Clayton990de7b2010-11-18 23:32:35 +00001543void
Caroline Tice2a456812011-03-10 22:14:10 +00001544Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001545{
Greg Clayton990de7b2010-11-18 23:32:35 +00001546}
Jim Ingham20594b12010-09-08 03:14:33 +00001547
Jim Inghamccd584d2010-09-23 17:40:12 +00001548lldb::StackFrameSP
1549Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1550{
Greg Clayton2450cb12012-03-29 01:41:38 +00001551 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghamccd584d2010-09-23 17:40:12 +00001552}
Caroline Tice7826c882010-10-26 03:11:13 +00001553
1554const char *
1555Thread::StopReasonAsCString (lldb::StopReason reason)
1556{
1557 switch (reason)
1558 {
1559 case eStopReasonInvalid: return "invalid";
1560 case eStopReasonNone: return "none";
1561 case eStopReasonTrace: return "trace";
1562 case eStopReasonBreakpoint: return "breakpoint";
1563 case eStopReasonWatchpoint: return "watchpoint";
1564 case eStopReasonSignal: return "signal";
1565 case eStopReasonException: return "exception";
1566 case eStopReasonPlanComplete: return "plan complete";
1567 }
1568
1569
1570 static char unknown_state_string[64];
1571 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1572 return unknown_state_string;
1573}
1574
1575const char *
1576Thread::RunModeAsCString (lldb::RunMode mode)
1577{
1578 switch (mode)
1579 {
1580 case eOnlyThisThread: return "only this thread";
1581 case eAllThreads: return "all threads";
1582 case eOnlyDuringStepping: return "only during stepping";
1583 }
1584
1585 static char unknown_state_string[64];
1586 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1587 return unknown_state_string;
1588}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001589
Greg Claytonabe0fed2011-04-18 08:33:37 +00001590size_t
1591Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1592{
Greg Claytonf4124de2012-02-21 00:09:25 +00001593 ExecutionContext exe_ctx (shared_from_this());
1594 Target *target = exe_ctx.GetTargetPtr();
1595 Process *process = exe_ctx.GetProcessPtr();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001596 size_t num_frames_shown = 0;
1597 strm.Indent();
Greg Claytonf4124de2012-02-21 00:09:25 +00001598 bool is_selected = false;
1599 if (process)
1600 {
1601 if (process->GetThreadList().GetSelectedThread().get() == this)
1602 is_selected = true;
1603 }
1604 strm.Printf("%c ", is_selected ? '*' : ' ');
1605 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001606 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001607 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001608 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001609 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001610 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1611 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1612 {
1613 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1614 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001615 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001616 }
1617
1618 DumpUsingSettingsFormat (strm, start_frame);
1619
1620 if (num_frames > 0)
1621 {
1622 strm.IndentMore();
1623
1624 const bool show_frame_info = true;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001625 strm.IndentMore ();
Greg Clayton2450cb12012-03-29 01:41:38 +00001626 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1627 start_frame,
1628 num_frames,
1629 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001630 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001631 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001632 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001633 }
1634 return num_frames_shown;
1635}
1636
1637size_t
1638Thread::GetStackFrameStatus (Stream& strm,
1639 uint32_t first_frame,
1640 uint32_t num_frames,
1641 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001642 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +00001643{
Greg Clayton2450cb12012-03-29 01:41:38 +00001644 return GetStackFrameList()->GetStatus (strm,
1645 first_frame,
1646 num_frames,
1647 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001648 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001649}
1650
Peter Collingbournee426d852011-06-03 20:40:54 +00001651bool
1652Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1653{
1654 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1655 if (frame_sp)
1656 {
1657 checkpoint.SetStackID(frame_sp->GetStackID());
1658 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1659 }
1660 return false;
1661}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001662
Peter Collingbournee426d852011-06-03 20:40:54 +00001663bool
1664Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1665{
Jim Inghama17a81a2012-09-12 00:40:39 +00001666 return ResetFrameZeroRegisters (checkpoint.GetData());
1667}
1668
1669bool
1670Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1671{
Peter Collingbournee426d852011-06-03 20:40:54 +00001672 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1673 if (frame_sp)
1674 {
Jim Inghama17a81a2012-09-12 00:40:39 +00001675 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (register_data_sp);
Peter Collingbournee426d852011-06-03 20:40:54 +00001676
1677 // Clear out all stack frames as our world just changed.
1678 ClearStackFrames();
1679 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1680
1681 return ret;
1682 }
1683 return false;
1684}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001685
Greg Clayton37f962e2011-08-22 02:49:39 +00001686Unwind *
1687Thread::GetUnwinder ()
1688{
1689 if (m_unwinder_ap.get() == NULL)
1690 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001691 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton37f962e2011-08-22 02:49:39 +00001692 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1693 switch (machine)
1694 {
1695 case llvm::Triple::x86_64:
1696 case llvm::Triple::x86:
1697 case llvm::Triple::arm:
1698 case llvm::Triple::thumb:
1699 m_unwinder_ap.reset (new UnwindLLDB (*this));
1700 break;
1701
1702 default:
1703 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1704 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1705 break;
1706 }
1707 }
1708 return m_unwinder_ap.get();
1709}
1710
1711
Greg Claytoncf5927e2012-05-18 02:38:05 +00001712void
1713Thread::Flush ()
1714{
1715 ClearStackFrames ();
1716 m_reg_context_sp.reset();
1717}
Jim Ingham6bc24c12012-10-16 00:09:33 +00001718
1719const bool
1720Thread::IsStillAtLastBreakpointHit ()
1721{
1722 // If we are currently stopped at a breakpoint, always return that stopinfo and don't reset it.
1723 // This allows threads to maintain their breakpoint stopinfo, such as when thread-stepping in
1724 // multithreaded programs.
1725 if (m_actual_stop_info_sp) {
1726 StopReason stop_reason = m_actual_stop_info_sp->GetStopReason();
1727 if (stop_reason == lldb::eStopReasonBreakpoint) {
1728 uint64_t value = m_actual_stop_info_sp->GetValue();
1729 lldb::addr_t pc = GetRegisterContext()->GetPC();
1730 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
1731 if (bp_site_sp && value == bp_site_sp->GetID())
1732 return true;
1733 }
1734 }
1735 return false;
1736}