blob: c75b9c9434984dc347f357d3e06e22ff0e51c558 [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"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Target/DynamicLoader.h"
19#include "lldb/Target/ExecutionContext.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000020#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Target/Process.h"
22#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000023#include "lldb/Target/StopInfo.h"
Greg Clayton7661a982010-07-23 16:45:51 +000024#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Target/Thread.h"
26#include "lldb/Target/ThreadPlan.h"
27#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Target/ThreadPlanBase.h"
29#include "lldb/Target/ThreadPlanStepInstruction.h"
30#include "lldb/Target/ThreadPlanStepOut.h"
31#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
32#include "lldb/Target/ThreadPlanStepThrough.h"
33#include "lldb/Target/ThreadPlanStepInRange.h"
34#include "lldb/Target/ThreadPlanStepOverRange.h"
35#include "lldb/Target/ThreadPlanRunToAddress.h"
36#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000037#include "lldb/Target/ThreadSpec.h"
Jim Ingham71219082010-08-12 02:14:28 +000038#include "lldb/Target/Unwind.h"
Greg Clayton37f962e2011-08-22 02:49:39 +000039#include "Plugins/Process/Utility/UnwindLLDB.h"
40#include "UnwindMacOSXFrameBackchain.h"
41
Chris Lattner24943d22010-06-08 16:52:24 +000042
43using namespace lldb;
44using namespace lldb_private;
45
Greg Clayton73844aa2012-08-22 17:17:09 +000046
47const ThreadPropertiesSP &
48Thread::GetGlobalProperties()
49{
50 static ThreadPropertiesSP g_settings_sp;
51 if (!g_settings_sp)
52 g_settings_sp.reset (new ThreadProperties (true));
53 return g_settings_sp;
54}
55
56static PropertyDefinition
57g_properties[] =
58{
59 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
60 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
61 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
62};
63
64enum {
65 ePropertyStepAvoidRegex,
66 ePropertyEnableThreadTrace
67};
68
69
70class ThreadOptionValueProperties : public OptionValueProperties
71{
72public:
73 ThreadOptionValueProperties (const ConstString &name) :
74 OptionValueProperties (name)
75 {
76 }
77
78 // This constructor is used when creating ThreadOptionValueProperties when it
79 // is part of a new lldb_private::Thread instance. It will copy all current
80 // global property values as needed
81 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Claytonc6e82e42012-08-22 18:39:03 +000082 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton73844aa2012-08-22 17:17:09 +000083 {
84 }
85
86 virtual const Property *
87 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
88 {
89 // When gettings the value for a key from the thread options, we will always
90 // try and grab the setting from the current thread if there is one. Else we just
91 // use the one from this instance.
92 if (exe_ctx)
93 {
94 Thread *thread = exe_ctx->GetThreadPtr();
95 if (thread)
96 {
97 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
98 if (this != instance_properties)
99 return instance_properties->ProtectedGetPropertyAtIndex (idx);
100 }
101 }
102 return ProtectedGetPropertyAtIndex (idx);
103 }
104};
105
106
107
108ThreadProperties::ThreadProperties (bool is_global) :
109 Properties ()
110{
111 if (is_global)
112 {
113 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
114 m_collection_sp->Initialize(g_properties);
115 }
116 else
117 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
118}
119
120ThreadProperties::~ThreadProperties()
121{
122}
123
124const RegularExpression *
125ThreadProperties::GetSymbolsToAvoidRegexp()
126{
127 const uint32_t idx = ePropertyStepAvoidRegex;
128 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
129}
130
131bool
132ThreadProperties::GetTraceEnabledState() const
133{
134 const uint32_t idx = ePropertyEnableThreadTrace;
135 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
136}
137
138
Greg Claytonf4124de2012-02-21 00:09:25 +0000139Thread::Thread (const ProcessSP &process_sp, lldb::tid_t tid) :
Greg Clayton73844aa2012-08-22 17:17:09 +0000140 ThreadProperties (false),
Chris Lattner24943d22010-06-08 16:52:24 +0000141 UserID (tid),
Greg Claytonf4124de2012-02-21 00:09:25 +0000142 m_process_wp (process_sp),
Greg Clayton643ee732010-08-04 01:40:35 +0000143 m_actual_stop_info_sp (),
Greg Claytonf4124de2012-02-21 00:09:25 +0000144 m_index_id (process_sp->GetNextThreadIndexID ()),
Chris Lattner24943d22010-06-08 16:52:24 +0000145 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000146 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +0000147 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +0000148 m_plan_stack (),
Chris Lattner24943d22010-06-08 16:52:24 +0000149 m_completed_plan_stack(),
Greg Clayton2450cb12012-03-29 01:41:38 +0000150 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000151 m_curr_frames_sp (),
152 m_prev_frames_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000153 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +0000154 m_resume_state (eStateRunning),
Jim Ingham149d1f52012-01-31 23:09:20 +0000155 m_temporary_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +0000156 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000157 m_destroy_called (false),
158 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +0000159
Chris Lattner24943d22010-06-08 16:52:24 +0000160{
Greg Claytone005f2c2010-11-06 01:53:30 +0000161 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000162 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000163 log->Printf ("%p Thread::Thread(tid = 0x%4.4llx)", this, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000164
165 QueueFundamentalPlan(true);
166}
167
168
169Thread::~Thread()
170{
Greg Claytone005f2c2010-11-06 01:53:30 +0000171 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000172 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000173 log->Printf ("%p Thread::~Thread(tid = 0x%4.4llx)", this, GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +0000174 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
175 assert (m_destroy_called);
176}
177
178void
179Thread::DestroyThread ()
180{
181 m_plan_stack.clear();
182 m_discarded_plan_stack.clear();
183 m_completed_plan_stack.clear();
Jim Inghame93055a2012-04-10 00:44:25 +0000184 m_actual_stop_info_sp.reset();
Jim Inghamcdea2362010-11-18 02:47:07 +0000185 m_destroy_called = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000186}
187
Jim Ingham6297a3a2010-10-20 00:39:53 +0000188lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000189Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +0000190{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000191 ThreadPlanSP plan_sp (GetCompletedPlan());
Jim Ingham707b7a82012-05-01 18:38:37 +0000192 if (plan_sp && plan_sp->PlanSucceeded())
Jim Ingham1586d972011-12-17 01:35:57 +0000193 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000194 else
Jim Ingham24e0d612011-08-09 00:32:52 +0000195 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000196 ProcessSP process_sp (GetProcess());
197 if (process_sp
198 && m_actual_stop_info_sp
Jim Ingham24e0d612011-08-09 00:32:52 +0000199 && m_actual_stop_info_sp->IsValid()
Greg Claytonf4124de2012-02-21 00:09:25 +0000200 && m_thread_stop_reason_stop_id == process_sp->GetStopID())
Jim Ingham24e0d612011-08-09 00:32:52 +0000201 return m_actual_stop_info_sp;
202 else
203 return GetPrivateStopReason ();
204 }
Chris Lattner24943d22010-06-08 16:52:24 +0000205}
206
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000207void
208Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
209{
210 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham24e0d612011-08-09 00:32:52 +0000211 if (m_actual_stop_info_sp)
212 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Claytonf4124de2012-02-21 00:09:25 +0000213 ProcessSP process_sp (GetProcess());
214 if (process_sp)
215 m_thread_stop_reason_stop_id = process_sp->GetStopID();
216 else
217 m_thread_stop_reason_stop_id = UINT32_MAX;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000218}
219
220void
221Thread::SetStopInfoToNothing()
222{
223 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
224 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
225 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
226}
227
Chris Lattner24943d22010-06-08 16:52:24 +0000228bool
229Thread::ThreadStoppedForAReason (void)
230{
Jim Ingham9880efa2012-08-11 00:35:26 +0000231 return (bool) GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +0000232}
233
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000234bool
235Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
236{
237 if (!SaveFrameZeroState(saved_state.register_backup))
238 return false;
239
240 saved_state.stop_info_sp = GetStopInfo();
Greg Claytonf4124de2012-02-21 00:09:25 +0000241 ProcessSP process_sp (GetProcess());
242 if (process_sp)
243 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham36de3c02012-09-07 23:36:43 +0000244 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
245
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000246 return true;
247}
248
249bool
250Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
251{
252 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham11a837d2011-01-25 02:47:23 +0000253 if (saved_state.stop_info_sp)
254 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000255 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham36de3c02012-09-07 23:36:43 +0000256 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000257 return true;
258}
259
Chris Lattner24943d22010-06-08 16:52:24 +0000260StateType
261Thread::GetState() const
262{
263 // If any other threads access this we will need a mutex for it
264 Mutex::Locker locker(m_state_mutex);
265 return m_state;
266}
267
268void
269Thread::SetState(StateType state)
270{
271 Mutex::Locker locker(m_state_mutex);
272 m_state = state;
273}
274
275void
276Thread::WillStop()
277{
278 ThreadPlan *current_plan = GetCurrentPlan();
279
280 // FIXME: I may decide to disallow threads with no plans. In which
281 // case this should go to an assert.
282
283 if (!current_plan)
284 return;
285
286 current_plan->WillStop();
287}
288
289void
290Thread::SetupForResume ()
291{
292 if (GetResumeState() != eStateSuspended)
293 {
294
295 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
296 // telling the current plan it will resume, since we might change what the current
297 // plan is.
298
299 lldb::addr_t pc = GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +0000300 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Chris Lattner24943d22010-06-08 16:52:24 +0000301 if (bp_site_sp && bp_site_sp->IsEnabled())
302 {
303 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
304 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000305
306 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000307
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000308 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
309 {
310 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
311 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000312 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000313 ThreadPlanSP step_bp_plan_sp;
314 step_bp_plan->SetPrivate (true);
315
Chris Lattner24943d22010-06-08 16:52:24 +0000316 if (GetCurrentPlan()->RunState() != eStateStepping)
317 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000318 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000319 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000320 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000321 QueueThreadPlan (step_bp_plan_sp, false);
322 }
323 }
324 }
325 }
326}
327
328bool
329Thread::WillResume (StateType resume_state)
330{
331 // At this point clear the completed plan stack.
332 m_completed_plan_stack.clear();
333 m_discarded_plan_stack.clear();
334
Jim Ingham149d1f52012-01-31 23:09:20 +0000335 SetTemporaryResumeState(resume_state);
336
337 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
338 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
339 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
340 // about the fact that we are resuming...
Greg Claytonf4124de2012-02-21 00:09:25 +0000341 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham149d1f52012-01-31 23:09:20 +0000342 if (m_thread_stop_reason_stop_id == process_stop_id &&
343 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
344 {
345 StopInfo *stop_info = GetPrivateStopReason().get();
346 if (stop_info)
347 stop_info->WillResume (resume_state);
348 }
Chris Lattner24943d22010-06-08 16:52:24 +0000349
350 // Tell all the plans that we are about to resume in case they need to clear any state.
351 // We distinguish between the plan on the top of the stack and the lower
352 // plans in case a plan needs to do any special business before it runs.
353
354 ThreadPlan *plan_ptr = GetCurrentPlan();
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000355 bool need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000356
357 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
358 {
359 plan_ptr->WillResume (resume_state, false);
360 }
Greg Clayton643ee732010-08-04 01:40:35 +0000361
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000362 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
363 // In that case, don't reset it here.
364
365 if (need_to_resume)
366 {
367 m_actual_stop_info_sp.reset();
368 }
369
370 return need_to_resume;
Chris Lattner24943d22010-06-08 16:52:24 +0000371}
372
373void
374Thread::DidResume ()
375{
376 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
377}
378
379bool
380Thread::ShouldStop (Event* event_ptr)
381{
382 ThreadPlan *current_plan = GetCurrentPlan();
383 bool should_stop = true;
384
Greg Claytone005f2c2010-11-06 01:53:30 +0000385 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham149d1f52012-01-31 23:09:20 +0000386
387 if (GetResumeState () == eStateSuspended)
388 {
389 if (log)
390 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
391 __FUNCTION__,
392 GetID ());
393// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
394// __FUNCTION__,
395// GetID (),
396// GetRegisterContext()->GetPC());
397 return false;
398 }
399
400 if (GetTemporaryResumeState () == eStateSuspended)
401 {
402 if (log)
403 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
404 __FUNCTION__,
405 GetID ());
406// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
407// __FUNCTION__,
408// GetID (),
409// GetRegisterContext()->GetPC());
410 return false;
411 }
412
413 if (ThreadStoppedForAReason() == false)
414 {
415 if (log)
416 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
417 __FUNCTION__,
418 GetID (),
419 GetRegisterContext()->GetPC());
420 return false;
421 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000422
Chris Lattner24943d22010-06-08 16:52:24 +0000423 if (log)
424 {
Jim Ingham149d1f52012-01-31 23:09:20 +0000425 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
426 __FUNCTION__,
427 GetID (),
428 GetRegisterContext()->GetPC());
Jim Inghame8f4e112011-10-15 00:23:43 +0000429 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000430 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000431 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000432 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000433 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000434 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000435
436 // The top most plan always gets to do the trace log...
437 current_plan->DoTraceLog ();
Jim Inghame787c7e2012-04-20 21:16:56 +0000438
439 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
440 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
441 // do any more work on this stop.
442 StopInfoSP private_stop_info (GetPrivateStopReason());
443 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
444 {
445 if (log)
446 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
447 return false;
448 }
Chris Lattner24943d22010-06-08 16:52:24 +0000449
Jim Ingham98d50212012-09-05 21:12:49 +0000450 // If we've already been restarted, don't query the plans since the state they would examine is not current.
451 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
452 return false;
453
454 // Before the plans see the state of the world, calculate the current inlined depth.
455 GetStackFrameList()->CalculateCurrentInlinedDepth();
456
Jim Inghamad382c52011-12-03 01:52:59 +0000457 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
458 // If that plan is still working, then we don't need to do any more work. If the plan that explains
459 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
460 // whether they still need to do more work.
461
462 bool done_processing_current_plan = false;
463
464 if (!current_plan->PlanExplainsStop())
465 {
466 if (current_plan->TracerExplainsStop())
467 {
468 done_processing_current_plan = true;
469 should_stop = false;
470 }
471 else
472 {
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000473 // If the current plan doesn't explain the stop, then find one that
Jim Inghamad382c52011-12-03 01:52:59 +0000474 // does and let it handle the situation.
475 ThreadPlan *plan_ptr = current_plan;
476 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
477 {
478 if (plan_ptr->PlanExplainsStop())
479 {
480 should_stop = plan_ptr->ShouldStop (event_ptr);
481
482 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
483 // and all the plans below it off the stack.
484
485 if (plan_ptr->MischiefManaged())
486 {
Jim Inghamd1ec3d62012-05-11 23:49:49 +0000487 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000488 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Inghamad382c52011-12-03 01:52:59 +0000489
490 do
491 {
492 if (should_stop)
493 current_plan->WillStop();
494 PopPlan();
495 }
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000496 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
497 // Now, if the responsible plan was not "Okay to discard" then we're done,
498 // otherwise we forward this to the next plan in the stack below.
499 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
500 done_processing_current_plan = true;
501 else
502 done_processing_current_plan = false;
Jim Inghamad382c52011-12-03 01:52:59 +0000503 }
504 else
505 done_processing_current_plan = true;
506
507 break;
508 }
509
510 }
511 }
512 }
513
514 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000515 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000516 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000517
Jim Inghame8f4e112011-10-15 00:23:43 +0000518 if (log)
519 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
520
Jim Inghamf9f40c22011-02-08 05:20:59 +0000521 // We're starting from the base plan, so just let it decide;
522 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000523 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000524 should_stop = current_plan->ShouldStop (event_ptr);
525 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000526 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000527 }
528 else
529 {
530 // Otherwise, don't let the base plan override what the other plans say to do, since
531 // presumably if there were other plans they would know what to do...
532 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000533 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000534 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000535 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000536
537 should_stop = current_plan->ShouldStop(event_ptr);
538 if (log)
539 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
540 if (current_plan->MischiefManaged())
541 {
542 if (should_stop)
543 current_plan->WillStop();
544
545 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
546 // Otherwise, see if the plan's parent wants to stop.
547
548 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
549 {
550 PopPlan();
551 break;
552 }
553 else
554 {
555
556 PopPlan();
557
558 current_plan = GetCurrentPlan();
559 if (current_plan == NULL)
560 {
561 break;
562 }
563 }
Chris Lattner24943d22010-06-08 16:52:24 +0000564 }
565 else
566 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000567 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000568 }
Chris Lattner24943d22010-06-08 16:52:24 +0000569 }
570 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000571
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000572 if (over_ride_stop)
573 should_stop = false;
Jim Ingham88e3de22012-05-03 21:19:36 +0000574
575 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
576 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
577 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
578 // This code clears stale plans off the stack.
579
580 if (should_stop)
581 {
582 ThreadPlan *plan_ptr = GetCurrentPlan();
583 while (!PlanIsBasePlan(plan_ptr))
584 {
585 bool stale = plan_ptr->IsPlanStale ();
586 ThreadPlan *examined_plan = plan_ptr;
587 plan_ptr = GetPreviousPlan (examined_plan);
588
589 if (stale)
590 {
591 if (log)
592 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
593 DiscardThreadPlansUpToPlan(examined_plan);
594 }
595 }
596 }
597
Chris Lattner24943d22010-06-08 16:52:24 +0000598 }
Chris Lattner24943d22010-06-08 16:52:24 +0000599
Jim Inghame8f4e112011-10-15 00:23:43 +0000600 if (log)
601 {
602 StreamString s;
603 s.IndentMore();
604 DumpThreadPlans(&s);
605 log->Printf ("Plan stack final state:\n%s", s.GetData());
606 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
607 }
Chris Lattner24943d22010-06-08 16:52:24 +0000608 return should_stop;
609}
610
611Vote
612Thread::ShouldReportStop (Event* event_ptr)
613{
614 StateType thread_state = GetResumeState ();
Jim Ingham149d1f52012-01-31 23:09:20 +0000615 StateType temp_thread_state = GetTemporaryResumeState();
616
Greg Claytone005f2c2010-11-06 01:53:30 +0000617 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000618
619 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
620 {
621 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000622 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 +0000623 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000624 }
Chris Lattner24943d22010-06-08 16:52:24 +0000625
Jim Ingham149d1f52012-01-31 23:09:20 +0000626 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
627 {
628 if (log)
629 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
630 return eVoteNoOpinion;
631 }
632
633 if (!ThreadStoppedForAReason())
634 {
635 if (log)
636 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
637 return eVoteNoOpinion;
638 }
639
Chris Lattner24943d22010-06-08 16:52:24 +0000640 if (m_completed_plan_stack.size() > 0)
641 {
642 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000643 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000644 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 +0000645 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
646 }
647 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000648 {
649 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000650 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000651 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000652 }
Chris Lattner24943d22010-06-08 16:52:24 +0000653}
654
655Vote
656Thread::ShouldReportRun (Event* event_ptr)
657{
658 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000659
Chris Lattner24943d22010-06-08 16:52:24 +0000660 if (thread_state == eStateSuspended
661 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000662 {
Chris Lattner24943d22010-06-08 16:52:24 +0000663 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000664 }
665
666 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000667 if (m_completed_plan_stack.size() > 0)
668 {
669 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000670 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000671 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 +0000672 GetIndexID(),
673 GetID(),
674 m_completed_plan_stack.back()->GetName());
675
Chris Lattner24943d22010-06-08 16:52:24 +0000676 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
677 }
678 else
Jim Inghamac959662011-01-24 06:34:17 +0000679 {
680 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000681 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 +0000682 GetIndexID(),
683 GetID(),
684 GetCurrentPlan()->GetName());
685
Chris Lattner24943d22010-06-08 16:52:24 +0000686 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000687 }
Chris Lattner24943d22010-06-08 16:52:24 +0000688}
689
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000690bool
691Thread::MatchesSpec (const ThreadSpec *spec)
692{
693 if (spec == NULL)
694 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000695
Jim Inghama2664912012-03-07 22:03:04 +0000696 return spec->ThreadPassesBasicTests(*this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000697}
698
Chris Lattner24943d22010-06-08 16:52:24 +0000699void
700Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
701{
702 if (thread_plan_sp)
703 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000704 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
705 if (!thread_plan_sp->GetThreadPlanTracer())
706 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000707 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000708
Chris Lattner24943d22010-06-08 16:52:24 +0000709 thread_plan_sp->DidPush();
710
Greg Claytone005f2c2010-11-06 01:53:30 +0000711 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000712 if (log)
713 {
714 StreamString s;
715 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Clayton444e35b2011-10-19 18:09:39 +0000716 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
Chris Lattner24943d22010-06-08 16:52:24 +0000717 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000718 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000719 }
720 }
721}
722
723void
724Thread::PopPlan ()
725{
Greg Claytone005f2c2010-11-06 01:53:30 +0000726 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000727
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000728 if (m_plan_stack.size() <= 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000729 return;
730 else
731 {
732 ThreadPlanSP &plan = m_plan_stack.back();
733 if (log)
734 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000735 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000736 }
737 m_completed_plan_stack.push_back (plan);
738 plan->WillPop();
739 m_plan_stack.pop_back();
740 }
741}
742
743void
744Thread::DiscardPlan ()
745{
746 if (m_plan_stack.size() > 1)
747 {
748 ThreadPlanSP &plan = m_plan_stack.back();
749 m_discarded_plan_stack.push_back (plan);
750 plan->WillPop();
751 m_plan_stack.pop_back();
752 }
753}
754
755ThreadPlan *
756Thread::GetCurrentPlan ()
757{
Jim Ingham2f41d272012-04-19 00:17:05 +0000758 // There will always be at least the base plan. If somebody is mucking with a
759 // thread with an empty plan stack, we should assert right away.
760 assert (!m_plan_stack.empty());
761
762 return m_plan_stack.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000763}
764
765ThreadPlanSP
766Thread::GetCompletedPlan ()
767{
768 ThreadPlanSP empty_plan_sp;
769 if (!m_completed_plan_stack.empty())
770 {
771 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
772 {
773 ThreadPlanSP completed_plan_sp;
774 completed_plan_sp = m_completed_plan_stack[i];
775 if (!completed_plan_sp->GetPrivate ())
776 return completed_plan_sp;
777 }
778 }
779 return empty_plan_sp;
780}
781
Jim Ingham1586d972011-12-17 01:35:57 +0000782ValueObjectSP
783Thread::GetReturnValueObject ()
784{
785 if (!m_completed_plan_stack.empty())
786 {
787 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
788 {
789 ValueObjectSP return_valobj_sp;
790 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
791 if (return_valobj_sp)
792 return return_valobj_sp;
793 }
794 }
795 return ValueObjectSP();
796}
797
Chris Lattner24943d22010-06-08 16:52:24 +0000798bool
799Thread::IsThreadPlanDone (ThreadPlan *plan)
800{
Chris Lattner24943d22010-06-08 16:52:24 +0000801 if (!m_completed_plan_stack.empty())
802 {
803 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
804 {
805 if (m_completed_plan_stack[i].get() == plan)
806 return true;
807 }
808 }
809 return false;
810}
811
812bool
813Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
814{
Chris Lattner24943d22010-06-08 16:52:24 +0000815 if (!m_discarded_plan_stack.empty())
816 {
817 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
818 {
819 if (m_discarded_plan_stack[i].get() == plan)
820 return true;
821 }
822 }
823 return false;
824}
825
826ThreadPlan *
827Thread::GetPreviousPlan (ThreadPlan *current_plan)
828{
829 if (current_plan == NULL)
830 return NULL;
831
832 int stack_size = m_completed_plan_stack.size();
833 for (int i = stack_size - 1; i > 0; i--)
834 {
835 if (current_plan == m_completed_plan_stack[i].get())
836 return m_completed_plan_stack[i-1].get();
837 }
838
839 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
840 {
Chris Lattner24943d22010-06-08 16:52:24 +0000841 if (m_plan_stack.size() > 0)
842 return m_plan_stack.back().get();
843 else
844 return NULL;
845 }
846
847 stack_size = m_plan_stack.size();
848 for (int i = stack_size - 1; i > 0; i--)
849 {
850 if (current_plan == m_plan_stack[i].get())
851 return m_plan_stack[i-1].get();
852 }
853 return NULL;
854}
855
856void
857Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
858{
859 if (abort_other_plans)
860 DiscardThreadPlans(true);
861
862 PushPlan (thread_plan_sp);
863}
864
Jim Ingham745ac7a2010-11-11 19:26:09 +0000865
866void
867Thread::EnableTracer (bool value, bool single_stepping)
868{
869 int stack_size = m_plan_stack.size();
870 for (int i = 0; i < stack_size; i++)
871 {
872 if (m_plan_stack[i]->GetThreadPlanTracer())
873 {
874 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
875 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
876 }
877 }
878}
879
880void
881Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
882{
883 int stack_size = m_plan_stack.size();
884 for (int i = 0; i < stack_size; i++)
885 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
886}
887
Chris Lattner24943d22010-06-08 16:52:24 +0000888void
Jim Inghamea9d4262010-11-05 19:25:48 +0000889Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
890{
Jim Ingham88e3de22012-05-03 21:19:36 +0000891 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
892}
893
894void
895Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
896{
Greg Claytone005f2c2010-11-06 01:53:30 +0000897 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000898 if (log)
899 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000900 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 +0000901 }
902
903 int stack_size = m_plan_stack.size();
904
905 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
906 // stack, and if so discard up to and including it.
907
Jim Ingham88e3de22012-05-03 21:19:36 +0000908 if (up_to_plan_ptr == NULL)
Jim Inghamea9d4262010-11-05 19:25:48 +0000909 {
910 for (int i = stack_size - 1; i > 0; i--)
911 DiscardPlan();
912 }
913 else
914 {
915 bool found_it = false;
916 for (int i = stack_size - 1; i > 0; i--)
917 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000918 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000919 found_it = true;
920 }
921 if (found_it)
922 {
923 bool last_one = false;
924 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
925 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000926 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000927 last_one = true;
928 DiscardPlan();
929 }
930 }
931 }
932 return;
933}
934
935void
Chris Lattner24943d22010-06-08 16:52:24 +0000936Thread::DiscardThreadPlans(bool force)
937{
Greg Claytone005f2c2010-11-06 01:53:30 +0000938 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000939 if (log)
940 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000941 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000942 }
943
944 if (force)
945 {
946 int stack_size = m_plan_stack.size();
947 for (int i = stack_size - 1; i > 0; i--)
948 {
949 DiscardPlan();
950 }
951 return;
952 }
953
954 while (1)
955 {
956
957 int master_plan_idx;
Jim Inghamf1dbb712012-09-11 00:09:25 +0000958 bool discard = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000959
960 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
961 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
962 {
963 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
964 {
965 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
966 break;
967 }
968 }
969
970 if (discard)
971 {
972 // First pop all the dependent plans:
973 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
974 {
975
976 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
977 // for the plan leaves it in a state that it is safe to pop the plan
978 // with no more notice?
979 DiscardPlan();
980 }
981
982 // Now discard the master plan itself.
983 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
984 // discard it's dependent plans, but not it...
985 if (master_plan_idx > 0)
986 {
987 DiscardPlan();
988 }
989 }
990 else
991 {
992 // If the master plan doesn't want to get discarded, then we're done.
993 break;
994 }
995
996 }
Chris Lattner24943d22010-06-08 16:52:24 +0000997}
998
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000999bool
1000Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1001{
1002 if (plan_ptr->IsBasePlan())
1003 return true;
1004 else if (m_plan_stack.size() == 0)
1005 return false;
1006 else
1007 return m_plan_stack[0].get() == plan_ptr;
1008}
1009
Chris Lattner24943d22010-06-08 16:52:24 +00001010ThreadPlan *
1011Thread::QueueFundamentalPlan (bool abort_other_plans)
1012{
1013 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1014 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1015 return thread_plan_sp.get();
1016}
1017
1018ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001019Thread::QueueThreadPlanForStepSingleInstruction
1020(
1021 bool step_over,
1022 bool abort_other_plans,
1023 bool stop_other_threads
1024)
Chris Lattner24943d22010-06-08 16:52:24 +00001025{
1026 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1027 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1028 return thread_plan_sp.get();
1029}
1030
1031ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001032Thread::QueueThreadPlanForStepRange
1033(
1034 bool abort_other_plans,
1035 StepType type,
1036 const AddressRange &range,
1037 const SymbolContext &addr_context,
1038 lldb::RunMode stop_other_threads,
1039 bool avoid_code_without_debug_info
1040)
Chris Lattner24943d22010-06-08 16:52:24 +00001041{
1042 ThreadPlanSP thread_plan_sp;
1043 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001044 {
1045 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1046 if (avoid_code_without_debug_info)
1047 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1048 else
1049 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1050 thread_plan_sp.reset (plan);
1051 }
Chris Lattner24943d22010-06-08 16:52:24 +00001052 else
1053 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1054
1055 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1056 return thread_plan_sp.get();
1057}
1058
1059
1060ThreadPlan *
1061Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1062{
1063 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1064 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1065 return thread_plan_sp.get();
1066}
1067
1068ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001069Thread::QueueThreadPlanForStepOut
1070(
1071 bool abort_other_plans,
1072 SymbolContext *addr_context,
1073 bool first_insn,
1074 bool stop_other_threads,
1075 Vote stop_vote,
1076 Vote run_vote,
1077 uint32_t frame_idx
1078)
Chris Lattner24943d22010-06-08 16:52:24 +00001079{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001080 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1081 addr_context,
1082 first_insn,
1083 stop_other_threads,
1084 stop_vote,
1085 run_vote,
1086 frame_idx));
Sean Callananf6d5fea2012-07-31 22:19:25 +00001087
1088 if (thread_plan_sp->ValidatePlan(NULL))
1089 {
1090 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1091 return thread_plan_sp.get();
1092 }
1093 else
1094 {
1095 return NULL;
1096 }
Chris Lattner24943d22010-06-08 16:52:24 +00001097}
1098
1099ThreadPlan *
Jim Ingham038fa8e2012-05-10 01:35:39 +00001100Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner24943d22010-06-08 16:52:24 +00001101{
Jim Ingham038fa8e2012-05-10 01:35:39 +00001102 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Inghamad382c52011-12-03 01:52:59 +00001103 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1104 return NULL;
1105
Chris Lattner24943d22010-06-08 16:52:24 +00001106 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1107 return thread_plan_sp.get();
1108}
1109
1110ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001111Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1112 Address& function,
1113 lldb::addr_t arg,
1114 bool stop_other_threads,
1115 bool discard_on_error)
1116{
Jim Ingham016ef882011-12-22 19:12:40 +00001117 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner24943d22010-06-08 16:52:24 +00001118 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1119 return thread_plan_sp.get();
1120}
1121
1122ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001123Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1124 Address &target_addr,
1125 bool stop_other_threads)
1126{
1127 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1128 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1129 return thread_plan_sp.get();
1130}
1131
1132ThreadPlan *
1133Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001134 lldb::addr_t *address_list,
1135 size_t num_addresses,
1136 bool stop_other_threads,
1137 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001138{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001139 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +00001140 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1141 return thread_plan_sp.get();
1142
1143}
1144
1145uint32_t
1146Thread::GetIndexID () const
1147{
1148 return m_index_id;
1149}
1150
1151void
1152Thread::DumpThreadPlans (lldb_private::Stream *s) const
1153{
1154 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +00001155 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +00001156 s->Indent();
Greg Clayton444e35b2011-10-19 18:09:39 +00001157 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 +00001158 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +00001159 {
Chris Lattner24943d22010-06-08 16:52:24 +00001160 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +00001161 s->Indent();
1162 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +00001163 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +00001164 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +00001165 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +00001166 }
1167
Chris Lattner24943d22010-06-08 16:52:24 +00001168 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001169 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001170 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001171 s->Indent();
1172 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1173 for (i = stack_size - 1; i >= 0; i--)
1174 {
1175 s->IndentMore();
1176 s->Indent();
1177 s->Printf ("Element %d: ", i);
1178 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1179 s->EOL();
1180 s->IndentLess();
1181 }
Chris Lattner24943d22010-06-08 16:52:24 +00001182 }
1183
1184 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001185 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001186 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001187 s->Indent();
1188 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1189 for (i = stack_size - 1; i >= 0; i--)
1190 {
1191 s->IndentMore();
1192 s->Indent();
1193 s->Printf ("Element %d: ", i);
1194 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1195 s->EOL();
1196 s->IndentLess();
1197 }
Chris Lattner24943d22010-06-08 16:52:24 +00001198 }
1199
1200}
1201
Greg Clayton289afcb2012-02-18 05:35:26 +00001202TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001203Thread::CalculateTarget ()
1204{
Greg Claytonf4124de2012-02-21 00:09:25 +00001205 TargetSP target_sp;
1206 ProcessSP process_sp(GetProcess());
1207 if (process_sp)
1208 target_sp = process_sp->CalculateTarget();
1209 return target_sp;
1210
Chris Lattner24943d22010-06-08 16:52:24 +00001211}
1212
Greg Clayton289afcb2012-02-18 05:35:26 +00001213ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001214Thread::CalculateProcess ()
1215{
Greg Claytonf4124de2012-02-21 00:09:25 +00001216 return GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001217}
1218
Greg Clayton289afcb2012-02-18 05:35:26 +00001219ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001220Thread::CalculateThread ()
1221{
Greg Clayton289afcb2012-02-18 05:35:26 +00001222 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001223}
1224
Greg Clayton289afcb2012-02-18 05:35:26 +00001225StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001226Thread::CalculateStackFrame ()
1227{
Greg Clayton289afcb2012-02-18 05:35:26 +00001228 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001229}
1230
1231void
Greg Claytona830adb2010-10-04 01:05:56 +00001232Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001233{
Greg Claytonf4124de2012-02-21 00:09:25 +00001234 exe_ctx.SetContext (shared_from_this());
Chris Lattner24943d22010-06-08 16:52:24 +00001235}
1236
Greg Clayton782b9cc2010-08-25 00:35:26 +00001237
Greg Clayton2450cb12012-03-29 01:41:38 +00001238StackFrameListSP
Greg Clayton782b9cc2010-08-25 00:35:26 +00001239Thread::GetStackFrameList ()
1240{
Greg Clayton2450cb12012-03-29 01:41:38 +00001241 StackFrameListSP frame_list_sp;
1242 Mutex::Locker locker(m_frame_mutex);
1243 if (m_curr_frames_sp)
1244 {
1245 frame_list_sp = m_curr_frames_sp;
1246 }
1247 else
1248 {
1249 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1250 m_curr_frames_sp = frame_list_sp;
1251 }
1252 return frame_list_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +00001253}
1254
Greg Clayton782b9cc2010-08-25 00:35:26 +00001255void
1256Thread::ClearStackFrames ()
1257{
Greg Clayton2450cb12012-03-29 01:41:38 +00001258 Mutex::Locker locker(m_frame_mutex);
1259
Jim Inghambf97d742012-02-29 03:40:22 +00001260 // Only store away the old "reference" StackFrameList if we got all its frames:
1261 // FIXME: At some point we can try to splice in the frames we have fetched into
1262 // the new frame as we make it, but let's not try that now.
1263 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Claytonc51ffbf2011-08-12 21:40:01 +00001264 m_prev_frames_sp.swap (m_curr_frames_sp);
1265 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +00001266}
1267
1268lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001269Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1270{
Greg Clayton2450cb12012-03-29 01:41:38 +00001271 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001272}
1273
Jim Inghama17a81a2012-09-12 00:40:39 +00001274
1275Error
Jim Inghamf59388a2012-09-14 02:14:15 +00001276Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp)
Jim Inghama17a81a2012-09-12 00:40:39 +00001277{
1278 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1279 Error return_error;
1280
1281 if (!frame_sp)
1282 {
1283 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%llx.", frame_idx, GetID());
1284 }
1285
Jim Inghamf59388a2012-09-14 02:14:15 +00001286 return ReturnFromFrame(frame_sp, return_value_sp);
Jim Inghama17a81a2012-09-12 00:40:39 +00001287}
1288
1289Error
Jim Inghamf59388a2012-09-14 02:14:15 +00001290Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp)
Jim Inghama17a81a2012-09-12 00:40:39 +00001291{
1292 Error return_error;
1293
1294 if (!frame_sp)
1295 {
1296 return_error.SetErrorString("Can't return to a null frame.");
1297 return return_error;
1298 }
1299
1300 Thread *thread = frame_sp->GetThread().get();
Jim Inghamf59388a2012-09-14 02:14:15 +00001301 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1302 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Inghama17a81a2012-09-12 00:40:39 +00001303
1304 if (return_value_sp)
1305 {
Jim Inghamf59388a2012-09-14 02:14:15 +00001306 // TODO: coerce the return_value_sp to the type of the function in frame_sp.
1307
Jim Inghama17a81a2012-09-12 00:40:39 +00001308 lldb::ABISP abi = thread->GetProcess()->GetABI();
1309 if (!abi)
1310 {
1311 return_error.SetErrorString("Could not find ABI to set return value.");
1312 }
Jim Inghamf59388a2012-09-14 02:14:15 +00001313 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Inghama17a81a2012-09-12 00:40:39 +00001314 if (!return_error.Success())
1315 return return_error;
1316 }
1317
1318 // Now write the return registers for the chosen frame:
Jim Inghamf59388a2012-09-14 02:14:15 +00001319 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
1320 // cook their data
1321 bool copy_success = thread->GetStackFrameAtIndex(0)->GetRegisterContext()->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1322 if (copy_success)
Jim Inghama17a81a2012-09-12 00:40:39 +00001323 {
1324 thread->DiscardThreadPlans(true);
Jim Inghamf59388a2012-09-14 02:14:15 +00001325 thread->ClearStackFrames();
Jim Inghama17a81a2012-09-12 00:40:39 +00001326 return return_error;
1327 }
1328 else
1329 {
1330 return_error.SetErrorString("Could not reset register values.");
1331 return return_error;
Jim Inghama17a81a2012-09-12 00:40:39 +00001332 }
1333}
1334
Chris Lattner24943d22010-06-08 16:52:24 +00001335void
Greg Claytona830adb2010-10-04 01:05:56 +00001336Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001337{
Greg Claytonf4124de2012-02-21 00:09:25 +00001338 ExecutionContext exe_ctx (shared_from_this());
1339 Process *process = exe_ctx.GetProcessPtr();
1340 if (process == NULL)
1341 return;
1342
Greg Clayton567e7f32011-09-22 04:58:26 +00001343 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +00001344 SymbolContext frame_sc;
Greg Claytona830adb2010-10-04 01:05:56 +00001345 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +00001346 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001347 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001348 if (frame_sp)
1349 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001350 exe_ctx.SetFrameSP(frame_sp);
1351 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001352 }
1353 }
1354
Greg Claytonf4124de2012-02-21 00:09:25 +00001355 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Claytona830adb2010-10-04 01:05:56 +00001356 assert (thread_format);
1357 const char *end = NULL;
1358 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001359 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001360 &exe_ctx,
1361 NULL,
1362 strm,
1363 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001364}
1365
Greg Clayton990de7b2010-11-18 23:32:35 +00001366void
Caroline Tice2a456812011-03-10 22:14:10 +00001367Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001368{
Greg Clayton990de7b2010-11-18 23:32:35 +00001369}
Jim Ingham20594b12010-09-08 03:14:33 +00001370
Greg Clayton990de7b2010-11-18 23:32:35 +00001371void
Caroline Tice2a456812011-03-10 22:14:10 +00001372Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001373{
Greg Clayton990de7b2010-11-18 23:32:35 +00001374}
Jim Ingham20594b12010-09-08 03:14:33 +00001375
Jim Inghamccd584d2010-09-23 17:40:12 +00001376lldb::StackFrameSP
1377Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1378{
Greg Clayton2450cb12012-03-29 01:41:38 +00001379 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghamccd584d2010-09-23 17:40:12 +00001380}
Caroline Tice7826c882010-10-26 03:11:13 +00001381
1382const char *
1383Thread::StopReasonAsCString (lldb::StopReason reason)
1384{
1385 switch (reason)
1386 {
1387 case eStopReasonInvalid: return "invalid";
1388 case eStopReasonNone: return "none";
1389 case eStopReasonTrace: return "trace";
1390 case eStopReasonBreakpoint: return "breakpoint";
1391 case eStopReasonWatchpoint: return "watchpoint";
1392 case eStopReasonSignal: return "signal";
1393 case eStopReasonException: return "exception";
1394 case eStopReasonPlanComplete: return "plan complete";
1395 }
1396
1397
1398 static char unknown_state_string[64];
1399 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1400 return unknown_state_string;
1401}
1402
1403const char *
1404Thread::RunModeAsCString (lldb::RunMode mode)
1405{
1406 switch (mode)
1407 {
1408 case eOnlyThisThread: return "only this thread";
1409 case eAllThreads: return "all threads";
1410 case eOnlyDuringStepping: return "only during stepping";
1411 }
1412
1413 static char unknown_state_string[64];
1414 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1415 return unknown_state_string;
1416}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001417
Greg Claytonabe0fed2011-04-18 08:33:37 +00001418size_t
1419Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1420{
Greg Claytonf4124de2012-02-21 00:09:25 +00001421 ExecutionContext exe_ctx (shared_from_this());
1422 Target *target = exe_ctx.GetTargetPtr();
1423 Process *process = exe_ctx.GetProcessPtr();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001424 size_t num_frames_shown = 0;
1425 strm.Indent();
Greg Claytonf4124de2012-02-21 00:09:25 +00001426 bool is_selected = false;
1427 if (process)
1428 {
1429 if (process->GetThreadList().GetSelectedThread().get() == this)
1430 is_selected = true;
1431 }
1432 strm.Printf("%c ", is_selected ? '*' : ' ');
1433 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001434 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001435 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001436 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001437 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001438 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1439 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1440 {
1441 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1442 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001443 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001444 }
1445
1446 DumpUsingSettingsFormat (strm, start_frame);
1447
1448 if (num_frames > 0)
1449 {
1450 strm.IndentMore();
1451
1452 const bool show_frame_info = true;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001453 strm.IndentMore ();
Greg Clayton2450cb12012-03-29 01:41:38 +00001454 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1455 start_frame,
1456 num_frames,
1457 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001458 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001459 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001460 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001461 }
1462 return num_frames_shown;
1463}
1464
1465size_t
1466Thread::GetStackFrameStatus (Stream& strm,
1467 uint32_t first_frame,
1468 uint32_t num_frames,
1469 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001470 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +00001471{
Greg Clayton2450cb12012-03-29 01:41:38 +00001472 return GetStackFrameList()->GetStatus (strm,
1473 first_frame,
1474 num_frames,
1475 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001476 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001477}
1478
Peter Collingbournee426d852011-06-03 20:40:54 +00001479bool
1480Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1481{
1482 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1483 if (frame_sp)
1484 {
1485 checkpoint.SetStackID(frame_sp->GetStackID());
1486 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1487 }
1488 return false;
1489}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001490
Peter Collingbournee426d852011-06-03 20:40:54 +00001491bool
1492Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1493{
Jim Inghama17a81a2012-09-12 00:40:39 +00001494 return ResetFrameZeroRegisters (checkpoint.GetData());
1495}
1496
1497bool
1498Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1499{
Peter Collingbournee426d852011-06-03 20:40:54 +00001500 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1501 if (frame_sp)
1502 {
Jim Inghama17a81a2012-09-12 00:40:39 +00001503 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (register_data_sp);
Peter Collingbournee426d852011-06-03 20:40:54 +00001504
1505 // Clear out all stack frames as our world just changed.
1506 ClearStackFrames();
1507 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1508
1509 return ret;
1510 }
1511 return false;
1512}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001513
Greg Clayton37f962e2011-08-22 02:49:39 +00001514Unwind *
1515Thread::GetUnwinder ()
1516{
1517 if (m_unwinder_ap.get() == NULL)
1518 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001519 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton37f962e2011-08-22 02:49:39 +00001520 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1521 switch (machine)
1522 {
1523 case llvm::Triple::x86_64:
1524 case llvm::Triple::x86:
1525 case llvm::Triple::arm:
1526 case llvm::Triple::thumb:
1527 m_unwinder_ap.reset (new UnwindLLDB (*this));
1528 break;
1529
1530 default:
1531 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1532 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1533 break;
1534 }
1535 }
1536 return m_unwinder_ap.get();
1537}
1538
1539
Greg Claytoncf5927e2012-05-18 02:38:05 +00001540void
1541Thread::Flush ()
1542{
1543 ClearStackFrames ();
1544 m_reg_context_sp.reset();
1545}