blob: 5cffe2d2624cd9e4c013a58e046c9870e847c60c [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
Greg Clayton3acaa922012-09-25 02:40:06 +0000207lldb::StopReason
208Thread::GetStopReason()
209{
210 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
211 if (stop_info_sp)
212 stop_info_sp->GetStopReason();
213 return eStopReasonNone;
214}
215
216
217
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000218void
219Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
220{
221 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham24e0d612011-08-09 00:32:52 +0000222 if (m_actual_stop_info_sp)
223 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Claytonf4124de2012-02-21 00:09:25 +0000224 ProcessSP process_sp (GetProcess());
225 if (process_sp)
226 m_thread_stop_reason_stop_id = process_sp->GetStopID();
227 else
228 m_thread_stop_reason_stop_id = UINT32_MAX;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000229}
230
231void
232Thread::SetStopInfoToNothing()
233{
234 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
235 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
236 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
237}
238
Chris Lattner24943d22010-06-08 16:52:24 +0000239bool
240Thread::ThreadStoppedForAReason (void)
241{
Jim Ingham9880efa2012-08-11 00:35:26 +0000242 return (bool) GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +0000243}
244
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000245bool
246Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
247{
248 if (!SaveFrameZeroState(saved_state.register_backup))
249 return false;
250
251 saved_state.stop_info_sp = GetStopInfo();
Greg Claytonf4124de2012-02-21 00:09:25 +0000252 ProcessSP process_sp (GetProcess());
253 if (process_sp)
254 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham36de3c02012-09-07 23:36:43 +0000255 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
256
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000257 return true;
258}
259
260bool
261Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
262{
263 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham11a837d2011-01-25 02:47:23 +0000264 if (saved_state.stop_info_sp)
265 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000266 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham36de3c02012-09-07 23:36:43 +0000267 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000268 return true;
269}
270
Chris Lattner24943d22010-06-08 16:52:24 +0000271StateType
272Thread::GetState() const
273{
274 // If any other threads access this we will need a mutex for it
275 Mutex::Locker locker(m_state_mutex);
276 return m_state;
277}
278
279void
280Thread::SetState(StateType state)
281{
282 Mutex::Locker locker(m_state_mutex);
283 m_state = state;
284}
285
286void
287Thread::WillStop()
288{
289 ThreadPlan *current_plan = GetCurrentPlan();
290
291 // FIXME: I may decide to disallow threads with no plans. In which
292 // case this should go to an assert.
293
294 if (!current_plan)
295 return;
296
297 current_plan->WillStop();
298}
299
300void
301Thread::SetupForResume ()
302{
303 if (GetResumeState() != eStateSuspended)
304 {
305
306 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
307 // telling the current plan it will resume, since we might change what the current
308 // plan is.
309
310 lldb::addr_t pc = GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +0000311 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Chris Lattner24943d22010-06-08 16:52:24 +0000312 if (bp_site_sp && bp_site_sp->IsEnabled())
313 {
314 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
315 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000316
317 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000318
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000319 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
320 {
321 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
322 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000323 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000324 ThreadPlanSP step_bp_plan_sp;
325 step_bp_plan->SetPrivate (true);
326
Chris Lattner24943d22010-06-08 16:52:24 +0000327 if (GetCurrentPlan()->RunState() != eStateStepping)
328 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000329 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000330 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000331 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000332 QueueThreadPlan (step_bp_plan_sp, false);
333 }
334 }
335 }
336 }
337}
338
339bool
340Thread::WillResume (StateType resume_state)
341{
342 // At this point clear the completed plan stack.
343 m_completed_plan_stack.clear();
344 m_discarded_plan_stack.clear();
345
Greg Clayton3acaa922012-09-25 02:40:06 +0000346 m_temporary_resume_state = resume_state;
Jim Ingham149d1f52012-01-31 23:09:20 +0000347
348 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
349 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
350 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
351 // about the fact that we are resuming...
Greg Claytonf4124de2012-02-21 00:09:25 +0000352 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham149d1f52012-01-31 23:09:20 +0000353 if (m_thread_stop_reason_stop_id == process_stop_id &&
354 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
355 {
356 StopInfo *stop_info = GetPrivateStopReason().get();
357 if (stop_info)
358 stop_info->WillResume (resume_state);
359 }
Chris Lattner24943d22010-06-08 16:52:24 +0000360
361 // Tell all the plans that we are about to resume in case they need to clear any state.
362 // We distinguish between the plan on the top of the stack and the lower
363 // plans in case a plan needs to do any special business before it runs.
364
365 ThreadPlan *plan_ptr = GetCurrentPlan();
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000366 bool need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000367
368 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
369 {
370 plan_ptr->WillResume (resume_state, false);
371 }
Greg Clayton643ee732010-08-04 01:40:35 +0000372
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000373 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
374 // In that case, don't reset it here.
375
376 if (need_to_resume)
377 {
378 m_actual_stop_info_sp.reset();
379 }
380
381 return need_to_resume;
Chris Lattner24943d22010-06-08 16:52:24 +0000382}
383
384void
385Thread::DidResume ()
386{
387 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
388}
389
390bool
391Thread::ShouldStop (Event* event_ptr)
392{
393 ThreadPlan *current_plan = GetCurrentPlan();
394 bool should_stop = true;
395
Greg Claytone005f2c2010-11-06 01:53:30 +0000396 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham149d1f52012-01-31 23:09:20 +0000397
398 if (GetResumeState () == eStateSuspended)
399 {
400 if (log)
401 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
402 __FUNCTION__,
403 GetID ());
404// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
405// __FUNCTION__,
406// GetID (),
407// GetRegisterContext()->GetPC());
408 return false;
409 }
410
411 if (GetTemporaryResumeState () == eStateSuspended)
412 {
413 if (log)
414 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
415 __FUNCTION__,
416 GetID ());
417// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
418// __FUNCTION__,
419// GetID (),
420// GetRegisterContext()->GetPC());
421 return false;
422 }
423
424 if (ThreadStoppedForAReason() == false)
425 {
426 if (log)
427 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
428 __FUNCTION__,
429 GetID (),
430 GetRegisterContext()->GetPC());
431 return false;
432 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000433
Chris Lattner24943d22010-06-08 16:52:24 +0000434 if (log)
435 {
Jim Ingham149d1f52012-01-31 23:09:20 +0000436 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
437 __FUNCTION__,
438 GetID (),
439 GetRegisterContext()->GetPC());
Jim Inghame8f4e112011-10-15 00:23:43 +0000440 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000441 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000442 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000443 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000444 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000445 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000446
447 // The top most plan always gets to do the trace log...
448 current_plan->DoTraceLog ();
Jim Inghame787c7e2012-04-20 21:16:56 +0000449
450 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
451 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
452 // do any more work on this stop.
453 StopInfoSP private_stop_info (GetPrivateStopReason());
454 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
455 {
456 if (log)
457 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
458 return false;
459 }
Chris Lattner24943d22010-06-08 16:52:24 +0000460
Jim Ingham98d50212012-09-05 21:12:49 +0000461 // If we've already been restarted, don't query the plans since the state they would examine is not current.
462 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
463 return false;
464
465 // Before the plans see the state of the world, calculate the current inlined depth.
466 GetStackFrameList()->CalculateCurrentInlinedDepth();
467
Jim Inghamad382c52011-12-03 01:52:59 +0000468 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
469 // If that plan is still working, then we don't need to do any more work. If the plan that explains
470 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
471 // whether they still need to do more work.
472
473 bool done_processing_current_plan = false;
474
475 if (!current_plan->PlanExplainsStop())
476 {
477 if (current_plan->TracerExplainsStop())
478 {
479 done_processing_current_plan = true;
480 should_stop = false;
481 }
482 else
483 {
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000484 // If the current plan doesn't explain the stop, then find one that
Jim Inghamad382c52011-12-03 01:52:59 +0000485 // does and let it handle the situation.
486 ThreadPlan *plan_ptr = current_plan;
487 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
488 {
489 if (plan_ptr->PlanExplainsStop())
490 {
491 should_stop = plan_ptr->ShouldStop (event_ptr);
492
493 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
494 // and all the plans below it off the stack.
495
496 if (plan_ptr->MischiefManaged())
497 {
Jim Inghamd1ec3d62012-05-11 23:49:49 +0000498 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000499 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Inghamad382c52011-12-03 01:52:59 +0000500
501 do
502 {
503 if (should_stop)
504 current_plan->WillStop();
505 PopPlan();
506 }
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000507 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
508 // Now, if the responsible plan was not "Okay to discard" then we're done,
509 // otherwise we forward this to the next plan in the stack below.
510 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
511 done_processing_current_plan = true;
512 else
513 done_processing_current_plan = false;
Jim Inghamad382c52011-12-03 01:52:59 +0000514 }
515 else
516 done_processing_current_plan = true;
517
518 break;
519 }
520
521 }
522 }
523 }
524
525 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000526 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000527 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000528
Jim Inghame8f4e112011-10-15 00:23:43 +0000529 if (log)
530 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
531
Jim Inghamf9f40c22011-02-08 05:20:59 +0000532 // We're starting from the base plan, so just let it decide;
533 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000534 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000535 should_stop = current_plan->ShouldStop (event_ptr);
536 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000537 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000538 }
539 else
540 {
541 // Otherwise, don't let the base plan override what the other plans say to do, since
542 // presumably if there were other plans they would know what to do...
543 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000544 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000545 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000546 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000547
548 should_stop = current_plan->ShouldStop(event_ptr);
549 if (log)
550 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
551 if (current_plan->MischiefManaged())
552 {
553 if (should_stop)
554 current_plan->WillStop();
555
556 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
557 // Otherwise, see if the plan's parent wants to stop.
558
559 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
560 {
561 PopPlan();
562 break;
563 }
564 else
565 {
566
567 PopPlan();
568
569 current_plan = GetCurrentPlan();
570 if (current_plan == NULL)
571 {
572 break;
573 }
574 }
Chris Lattner24943d22010-06-08 16:52:24 +0000575 }
576 else
577 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000578 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000579 }
Chris Lattner24943d22010-06-08 16:52:24 +0000580 }
581 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000582
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000583 if (over_ride_stop)
584 should_stop = false;
Jim Ingham88e3de22012-05-03 21:19:36 +0000585
586 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
587 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
588 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
589 // This code clears stale plans off the stack.
590
591 if (should_stop)
592 {
593 ThreadPlan *plan_ptr = GetCurrentPlan();
594 while (!PlanIsBasePlan(plan_ptr))
595 {
596 bool stale = plan_ptr->IsPlanStale ();
597 ThreadPlan *examined_plan = plan_ptr;
598 plan_ptr = GetPreviousPlan (examined_plan);
599
600 if (stale)
601 {
602 if (log)
603 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
604 DiscardThreadPlansUpToPlan(examined_plan);
605 }
606 }
607 }
608
Chris Lattner24943d22010-06-08 16:52:24 +0000609 }
Chris Lattner24943d22010-06-08 16:52:24 +0000610
Jim Inghame8f4e112011-10-15 00:23:43 +0000611 if (log)
612 {
613 StreamString s;
614 s.IndentMore();
615 DumpThreadPlans(&s);
616 log->Printf ("Plan stack final state:\n%s", s.GetData());
617 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
618 }
Chris Lattner24943d22010-06-08 16:52:24 +0000619 return should_stop;
620}
621
622Vote
623Thread::ShouldReportStop (Event* event_ptr)
624{
625 StateType thread_state = GetResumeState ();
Jim Ingham149d1f52012-01-31 23:09:20 +0000626 StateType temp_thread_state = GetTemporaryResumeState();
627
Greg Claytone005f2c2010-11-06 01:53:30 +0000628 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000629
630 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
631 {
632 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000633 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 +0000634 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000635 }
Chris Lattner24943d22010-06-08 16:52:24 +0000636
Jim Ingham149d1f52012-01-31 23:09:20 +0000637 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
638 {
639 if (log)
640 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
641 return eVoteNoOpinion;
642 }
643
644 if (!ThreadStoppedForAReason())
645 {
646 if (log)
647 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
648 return eVoteNoOpinion;
649 }
650
Chris Lattner24943d22010-06-08 16:52:24 +0000651 if (m_completed_plan_stack.size() > 0)
652 {
653 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000654 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000655 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 +0000656 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
657 }
658 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000659 {
660 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000661 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000662 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000663 }
Chris Lattner24943d22010-06-08 16:52:24 +0000664}
665
666Vote
667Thread::ShouldReportRun (Event* event_ptr)
668{
669 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000670
Chris Lattner24943d22010-06-08 16:52:24 +0000671 if (thread_state == eStateSuspended
672 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000673 {
Chris Lattner24943d22010-06-08 16:52:24 +0000674 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000675 }
676
677 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000678 if (m_completed_plan_stack.size() > 0)
679 {
680 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000681 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000682 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 +0000683 GetIndexID(),
684 GetID(),
685 m_completed_plan_stack.back()->GetName());
686
Chris Lattner24943d22010-06-08 16:52:24 +0000687 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
688 }
689 else
Jim Inghamac959662011-01-24 06:34:17 +0000690 {
691 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000692 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 +0000693 GetIndexID(),
694 GetID(),
695 GetCurrentPlan()->GetName());
696
Chris Lattner24943d22010-06-08 16:52:24 +0000697 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000698 }
Chris Lattner24943d22010-06-08 16:52:24 +0000699}
700
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000701bool
702Thread::MatchesSpec (const ThreadSpec *spec)
703{
704 if (spec == NULL)
705 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000706
Jim Inghama2664912012-03-07 22:03:04 +0000707 return spec->ThreadPassesBasicTests(*this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000708}
709
Chris Lattner24943d22010-06-08 16:52:24 +0000710void
711Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
712{
713 if (thread_plan_sp)
714 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000715 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
716 if (!thread_plan_sp->GetThreadPlanTracer())
717 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000718 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000719
Chris Lattner24943d22010-06-08 16:52:24 +0000720 thread_plan_sp->DidPush();
721
Greg Claytone005f2c2010-11-06 01:53:30 +0000722 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000723 if (log)
724 {
725 StreamString s;
726 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Clayton444e35b2011-10-19 18:09:39 +0000727 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
Chris Lattner24943d22010-06-08 16:52:24 +0000728 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000729 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000730 }
731 }
732}
733
734void
735Thread::PopPlan ()
736{
Greg Claytone005f2c2010-11-06 01:53:30 +0000737 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000738
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000739 if (m_plan_stack.size() <= 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000740 return;
741 else
742 {
743 ThreadPlanSP &plan = m_plan_stack.back();
744 if (log)
745 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000746 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000747 }
748 m_completed_plan_stack.push_back (plan);
749 plan->WillPop();
750 m_plan_stack.pop_back();
751 }
752}
753
754void
755Thread::DiscardPlan ()
756{
757 if (m_plan_stack.size() > 1)
758 {
759 ThreadPlanSP &plan = m_plan_stack.back();
760 m_discarded_plan_stack.push_back (plan);
761 plan->WillPop();
762 m_plan_stack.pop_back();
763 }
764}
765
766ThreadPlan *
767Thread::GetCurrentPlan ()
768{
Jim Ingham2f41d272012-04-19 00:17:05 +0000769 // There will always be at least the base plan. If somebody is mucking with a
770 // thread with an empty plan stack, we should assert right away.
771 assert (!m_plan_stack.empty());
772
773 return m_plan_stack.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000774}
775
776ThreadPlanSP
777Thread::GetCompletedPlan ()
778{
779 ThreadPlanSP empty_plan_sp;
780 if (!m_completed_plan_stack.empty())
781 {
782 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
783 {
784 ThreadPlanSP completed_plan_sp;
785 completed_plan_sp = m_completed_plan_stack[i];
786 if (!completed_plan_sp->GetPrivate ())
787 return completed_plan_sp;
788 }
789 }
790 return empty_plan_sp;
791}
792
Jim Ingham1586d972011-12-17 01:35:57 +0000793ValueObjectSP
794Thread::GetReturnValueObject ()
795{
796 if (!m_completed_plan_stack.empty())
797 {
798 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
799 {
800 ValueObjectSP return_valobj_sp;
801 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
802 if (return_valobj_sp)
803 return return_valobj_sp;
804 }
805 }
806 return ValueObjectSP();
807}
808
Chris Lattner24943d22010-06-08 16:52:24 +0000809bool
810Thread::IsThreadPlanDone (ThreadPlan *plan)
811{
Chris Lattner24943d22010-06-08 16:52:24 +0000812 if (!m_completed_plan_stack.empty())
813 {
814 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
815 {
816 if (m_completed_plan_stack[i].get() == plan)
817 return true;
818 }
819 }
820 return false;
821}
822
823bool
824Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
825{
Chris Lattner24943d22010-06-08 16:52:24 +0000826 if (!m_discarded_plan_stack.empty())
827 {
828 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
829 {
830 if (m_discarded_plan_stack[i].get() == plan)
831 return true;
832 }
833 }
834 return false;
835}
836
837ThreadPlan *
838Thread::GetPreviousPlan (ThreadPlan *current_plan)
839{
840 if (current_plan == NULL)
841 return NULL;
842
843 int stack_size = m_completed_plan_stack.size();
844 for (int i = stack_size - 1; i > 0; i--)
845 {
846 if (current_plan == m_completed_plan_stack[i].get())
847 return m_completed_plan_stack[i-1].get();
848 }
849
850 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
851 {
Chris Lattner24943d22010-06-08 16:52:24 +0000852 if (m_plan_stack.size() > 0)
853 return m_plan_stack.back().get();
854 else
855 return NULL;
856 }
857
858 stack_size = m_plan_stack.size();
859 for (int i = stack_size - 1; i > 0; i--)
860 {
861 if (current_plan == m_plan_stack[i].get())
862 return m_plan_stack[i-1].get();
863 }
864 return NULL;
865}
866
867void
868Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
869{
870 if (abort_other_plans)
871 DiscardThreadPlans(true);
872
873 PushPlan (thread_plan_sp);
874}
875
Jim Ingham745ac7a2010-11-11 19:26:09 +0000876
877void
878Thread::EnableTracer (bool value, bool single_stepping)
879{
880 int stack_size = m_plan_stack.size();
881 for (int i = 0; i < stack_size; i++)
882 {
883 if (m_plan_stack[i]->GetThreadPlanTracer())
884 {
885 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
886 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
887 }
888 }
889}
890
891void
892Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
893{
894 int stack_size = m_plan_stack.size();
895 for (int i = 0; i < stack_size; i++)
896 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
897}
898
Chris Lattner24943d22010-06-08 16:52:24 +0000899void
Jim Inghamea9d4262010-11-05 19:25:48 +0000900Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
901{
Jim Ingham88e3de22012-05-03 21:19:36 +0000902 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
903}
904
905void
906Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
907{
Greg Claytone005f2c2010-11-06 01:53:30 +0000908 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000909 if (log)
910 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000911 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 +0000912 }
913
914 int stack_size = m_plan_stack.size();
915
916 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
917 // stack, and if so discard up to and including it.
918
Jim Ingham88e3de22012-05-03 21:19:36 +0000919 if (up_to_plan_ptr == NULL)
Jim Inghamea9d4262010-11-05 19:25:48 +0000920 {
921 for (int i = stack_size - 1; i > 0; i--)
922 DiscardPlan();
923 }
924 else
925 {
926 bool found_it = false;
927 for (int i = stack_size - 1; i > 0; i--)
928 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000929 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000930 found_it = true;
931 }
932 if (found_it)
933 {
934 bool last_one = false;
935 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
936 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000937 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000938 last_one = true;
939 DiscardPlan();
940 }
941 }
942 }
943 return;
944}
945
946void
Chris Lattner24943d22010-06-08 16:52:24 +0000947Thread::DiscardThreadPlans(bool force)
948{
Greg Claytone005f2c2010-11-06 01:53:30 +0000949 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000950 if (log)
951 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000952 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000953 }
954
955 if (force)
956 {
957 int stack_size = m_plan_stack.size();
958 for (int i = stack_size - 1; i > 0; i--)
959 {
960 DiscardPlan();
961 }
962 return;
963 }
964
965 while (1)
966 {
967
968 int master_plan_idx;
Jim Inghamf1dbb712012-09-11 00:09:25 +0000969 bool discard = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000970
971 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
972 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
973 {
974 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
975 {
976 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
977 break;
978 }
979 }
980
981 if (discard)
982 {
983 // First pop all the dependent plans:
984 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
985 {
986
987 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
988 // for the plan leaves it in a state that it is safe to pop the plan
989 // with no more notice?
990 DiscardPlan();
991 }
992
993 // Now discard the master plan itself.
994 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
995 // discard it's dependent plans, but not it...
996 if (master_plan_idx > 0)
997 {
998 DiscardPlan();
999 }
1000 }
1001 else
1002 {
1003 // If the master plan doesn't want to get discarded, then we're done.
1004 break;
1005 }
1006
1007 }
Chris Lattner24943d22010-06-08 16:52:24 +00001008}
1009
Jim Ingham2bcbaf62012-04-09 22:37:39 +00001010bool
1011Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1012{
1013 if (plan_ptr->IsBasePlan())
1014 return true;
1015 else if (m_plan_stack.size() == 0)
1016 return false;
1017 else
1018 return m_plan_stack[0].get() == plan_ptr;
1019}
1020
Chris Lattner24943d22010-06-08 16:52:24 +00001021ThreadPlan *
1022Thread::QueueFundamentalPlan (bool abort_other_plans)
1023{
1024 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1025 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1026 return thread_plan_sp.get();
1027}
1028
1029ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001030Thread::QueueThreadPlanForStepSingleInstruction
1031(
1032 bool step_over,
1033 bool abort_other_plans,
1034 bool stop_other_threads
1035)
Chris Lattner24943d22010-06-08 16:52:24 +00001036{
1037 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1038 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1039 return thread_plan_sp.get();
1040}
1041
1042ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001043Thread::QueueThreadPlanForStepRange
1044(
1045 bool abort_other_plans,
1046 StepType type,
1047 const AddressRange &range,
1048 const SymbolContext &addr_context,
1049 lldb::RunMode stop_other_threads,
1050 bool avoid_code_without_debug_info
1051)
Chris Lattner24943d22010-06-08 16:52:24 +00001052{
1053 ThreadPlanSP thread_plan_sp;
1054 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001055 {
1056 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1057 if (avoid_code_without_debug_info)
1058 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1059 else
1060 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1061 thread_plan_sp.reset (plan);
1062 }
Chris Lattner24943d22010-06-08 16:52:24 +00001063 else
1064 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1065
1066 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1067 return thread_plan_sp.get();
1068}
1069
1070
1071ThreadPlan *
1072Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1073{
1074 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1075 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1076 return thread_plan_sp.get();
1077}
1078
1079ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001080Thread::QueueThreadPlanForStepOut
1081(
1082 bool abort_other_plans,
1083 SymbolContext *addr_context,
1084 bool first_insn,
1085 bool stop_other_threads,
1086 Vote stop_vote,
1087 Vote run_vote,
1088 uint32_t frame_idx
1089)
Chris Lattner24943d22010-06-08 16:52:24 +00001090{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001091 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1092 addr_context,
1093 first_insn,
1094 stop_other_threads,
1095 stop_vote,
1096 run_vote,
1097 frame_idx));
Sean Callananf6d5fea2012-07-31 22:19:25 +00001098
1099 if (thread_plan_sp->ValidatePlan(NULL))
1100 {
1101 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1102 return thread_plan_sp.get();
1103 }
1104 else
1105 {
1106 return NULL;
1107 }
Chris Lattner24943d22010-06-08 16:52:24 +00001108}
1109
1110ThreadPlan *
Jim Ingham038fa8e2012-05-10 01:35:39 +00001111Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner24943d22010-06-08 16:52:24 +00001112{
Jim Ingham038fa8e2012-05-10 01:35:39 +00001113 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Inghamad382c52011-12-03 01:52:59 +00001114 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1115 return NULL;
1116
Chris Lattner24943d22010-06-08 16:52:24 +00001117 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1118 return thread_plan_sp.get();
1119}
1120
1121ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001122Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1123 Address& function,
1124 lldb::addr_t arg,
1125 bool stop_other_threads,
1126 bool discard_on_error)
1127{
Jim Ingham016ef882011-12-22 19:12:40 +00001128 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner24943d22010-06-08 16:52:24 +00001129 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1130 return thread_plan_sp.get();
1131}
1132
1133ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001134Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1135 Address &target_addr,
1136 bool stop_other_threads)
1137{
1138 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1139 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1140 return thread_plan_sp.get();
1141}
1142
1143ThreadPlan *
1144Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001145 lldb::addr_t *address_list,
1146 size_t num_addresses,
1147 bool stop_other_threads,
1148 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001149{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001150 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +00001151 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1152 return thread_plan_sp.get();
1153
1154}
1155
1156uint32_t
1157Thread::GetIndexID () const
1158{
1159 return m_index_id;
1160}
1161
1162void
1163Thread::DumpThreadPlans (lldb_private::Stream *s) const
1164{
1165 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +00001166 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +00001167 s->Indent();
Greg Clayton444e35b2011-10-19 18:09:39 +00001168 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 +00001169 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +00001170 {
Chris Lattner24943d22010-06-08 16:52:24 +00001171 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +00001172 s->Indent();
1173 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +00001174 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +00001175 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +00001176 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +00001177 }
1178
Chris Lattner24943d22010-06-08 16:52:24 +00001179 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001180 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001181 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001182 s->Indent();
1183 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1184 for (i = stack_size - 1; i >= 0; i--)
1185 {
1186 s->IndentMore();
1187 s->Indent();
1188 s->Printf ("Element %d: ", i);
1189 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1190 s->EOL();
1191 s->IndentLess();
1192 }
Chris Lattner24943d22010-06-08 16:52:24 +00001193 }
1194
1195 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001196 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001197 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001198 s->Indent();
1199 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1200 for (i = stack_size - 1; i >= 0; i--)
1201 {
1202 s->IndentMore();
1203 s->Indent();
1204 s->Printf ("Element %d: ", i);
1205 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1206 s->EOL();
1207 s->IndentLess();
1208 }
Chris Lattner24943d22010-06-08 16:52:24 +00001209 }
1210
1211}
1212
Greg Clayton289afcb2012-02-18 05:35:26 +00001213TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001214Thread::CalculateTarget ()
1215{
Greg Claytonf4124de2012-02-21 00:09:25 +00001216 TargetSP target_sp;
1217 ProcessSP process_sp(GetProcess());
1218 if (process_sp)
1219 target_sp = process_sp->CalculateTarget();
1220 return target_sp;
1221
Chris Lattner24943d22010-06-08 16:52:24 +00001222}
1223
Greg Clayton289afcb2012-02-18 05:35:26 +00001224ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001225Thread::CalculateProcess ()
1226{
Greg Claytonf4124de2012-02-21 00:09:25 +00001227 return GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001228}
1229
Greg Clayton289afcb2012-02-18 05:35:26 +00001230ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001231Thread::CalculateThread ()
1232{
Greg Clayton289afcb2012-02-18 05:35:26 +00001233 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001234}
1235
Greg Clayton289afcb2012-02-18 05:35:26 +00001236StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001237Thread::CalculateStackFrame ()
1238{
Greg Clayton289afcb2012-02-18 05:35:26 +00001239 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001240}
1241
1242void
Greg Claytona830adb2010-10-04 01:05:56 +00001243Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001244{
Greg Claytonf4124de2012-02-21 00:09:25 +00001245 exe_ctx.SetContext (shared_from_this());
Chris Lattner24943d22010-06-08 16:52:24 +00001246}
1247
Greg Clayton782b9cc2010-08-25 00:35:26 +00001248
Greg Clayton2450cb12012-03-29 01:41:38 +00001249StackFrameListSP
Greg Clayton782b9cc2010-08-25 00:35:26 +00001250Thread::GetStackFrameList ()
1251{
Greg Clayton2450cb12012-03-29 01:41:38 +00001252 StackFrameListSP frame_list_sp;
1253 Mutex::Locker locker(m_frame_mutex);
1254 if (m_curr_frames_sp)
1255 {
1256 frame_list_sp = m_curr_frames_sp;
1257 }
1258 else
1259 {
1260 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1261 m_curr_frames_sp = frame_list_sp;
1262 }
1263 return frame_list_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +00001264}
1265
Greg Clayton782b9cc2010-08-25 00:35:26 +00001266void
1267Thread::ClearStackFrames ()
1268{
Greg Clayton2450cb12012-03-29 01:41:38 +00001269 Mutex::Locker locker(m_frame_mutex);
1270
Jim Inghambf97d742012-02-29 03:40:22 +00001271 // Only store away the old "reference" StackFrameList if we got all its frames:
1272 // FIXME: At some point we can try to splice in the frames we have fetched into
1273 // the new frame as we make it, but let's not try that now.
1274 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Claytonc51ffbf2011-08-12 21:40:01 +00001275 m_prev_frames_sp.swap (m_curr_frames_sp);
1276 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +00001277}
1278
1279lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001280Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1281{
Greg Clayton2450cb12012-03-29 01:41:38 +00001282 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001283}
1284
Jim Inghama17a81a2012-09-12 00:40:39 +00001285
1286Error
Jim Inghamf59388a2012-09-14 02:14:15 +00001287Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp)
Jim Inghama17a81a2012-09-12 00:40:39 +00001288{
1289 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1290 Error return_error;
1291
1292 if (!frame_sp)
1293 {
1294 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%llx.", frame_idx, GetID());
1295 }
1296
Jim Inghamf59388a2012-09-14 02:14:15 +00001297 return ReturnFromFrame(frame_sp, return_value_sp);
Jim Inghama17a81a2012-09-12 00:40:39 +00001298}
1299
1300Error
Jim Inghamf59388a2012-09-14 02:14:15 +00001301Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp)
Jim Inghama17a81a2012-09-12 00:40:39 +00001302{
1303 Error return_error;
1304
1305 if (!frame_sp)
1306 {
1307 return_error.SetErrorString("Can't return to a null frame.");
1308 return return_error;
1309 }
1310
1311 Thread *thread = frame_sp->GetThread().get();
Jim Inghamf59388a2012-09-14 02:14:15 +00001312 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1313 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Inghama17a81a2012-09-12 00:40:39 +00001314
1315 if (return_value_sp)
1316 {
Jim Inghamf59388a2012-09-14 02:14:15 +00001317 // TODO: coerce the return_value_sp to the type of the function in frame_sp.
1318
Jim Inghama17a81a2012-09-12 00:40:39 +00001319 lldb::ABISP abi = thread->GetProcess()->GetABI();
1320 if (!abi)
1321 {
1322 return_error.SetErrorString("Could not find ABI to set return value.");
1323 }
Jim Inghamf59388a2012-09-14 02:14:15 +00001324 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Inghama17a81a2012-09-12 00:40:39 +00001325 if (!return_error.Success())
1326 return return_error;
1327 }
1328
1329 // Now write the return registers for the chosen frame:
Jim Inghamf59388a2012-09-14 02:14:15 +00001330 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
1331 // cook their data
1332 bool copy_success = thread->GetStackFrameAtIndex(0)->GetRegisterContext()->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1333 if (copy_success)
Jim Inghama17a81a2012-09-12 00:40:39 +00001334 {
1335 thread->DiscardThreadPlans(true);
Jim Inghamf59388a2012-09-14 02:14:15 +00001336 thread->ClearStackFrames();
Jim Inghama17a81a2012-09-12 00:40:39 +00001337 return return_error;
1338 }
1339 else
1340 {
1341 return_error.SetErrorString("Could not reset register values.");
1342 return return_error;
Jim Inghama17a81a2012-09-12 00:40:39 +00001343 }
1344}
1345
Chris Lattner24943d22010-06-08 16:52:24 +00001346void
Greg Claytona830adb2010-10-04 01:05:56 +00001347Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001348{
Greg Claytonf4124de2012-02-21 00:09:25 +00001349 ExecutionContext exe_ctx (shared_from_this());
1350 Process *process = exe_ctx.GetProcessPtr();
1351 if (process == NULL)
1352 return;
1353
Greg Clayton567e7f32011-09-22 04:58:26 +00001354 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +00001355 SymbolContext frame_sc;
Greg Claytona830adb2010-10-04 01:05:56 +00001356 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +00001357 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001358 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001359 if (frame_sp)
1360 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001361 exe_ctx.SetFrameSP(frame_sp);
1362 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001363 }
1364 }
1365
Greg Claytonf4124de2012-02-21 00:09:25 +00001366 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Claytona830adb2010-10-04 01:05:56 +00001367 assert (thread_format);
1368 const char *end = NULL;
1369 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001370 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001371 &exe_ctx,
1372 NULL,
1373 strm,
1374 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001375}
1376
Greg Clayton990de7b2010-11-18 23:32:35 +00001377void
Caroline Tice2a456812011-03-10 22:14:10 +00001378Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001379{
Greg Clayton990de7b2010-11-18 23:32:35 +00001380}
Jim Ingham20594b12010-09-08 03:14:33 +00001381
Greg Clayton990de7b2010-11-18 23:32:35 +00001382void
Caroline Tice2a456812011-03-10 22:14:10 +00001383Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001384{
Greg Clayton990de7b2010-11-18 23:32:35 +00001385}
Jim Ingham20594b12010-09-08 03:14:33 +00001386
Jim Inghamccd584d2010-09-23 17:40:12 +00001387lldb::StackFrameSP
1388Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1389{
Greg Clayton2450cb12012-03-29 01:41:38 +00001390 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghamccd584d2010-09-23 17:40:12 +00001391}
Caroline Tice7826c882010-10-26 03:11:13 +00001392
1393const char *
1394Thread::StopReasonAsCString (lldb::StopReason reason)
1395{
1396 switch (reason)
1397 {
1398 case eStopReasonInvalid: return "invalid";
1399 case eStopReasonNone: return "none";
1400 case eStopReasonTrace: return "trace";
1401 case eStopReasonBreakpoint: return "breakpoint";
1402 case eStopReasonWatchpoint: return "watchpoint";
1403 case eStopReasonSignal: return "signal";
1404 case eStopReasonException: return "exception";
1405 case eStopReasonPlanComplete: return "plan complete";
1406 }
1407
1408
1409 static char unknown_state_string[64];
1410 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1411 return unknown_state_string;
1412}
1413
1414const char *
1415Thread::RunModeAsCString (lldb::RunMode mode)
1416{
1417 switch (mode)
1418 {
1419 case eOnlyThisThread: return "only this thread";
1420 case eAllThreads: return "all threads";
1421 case eOnlyDuringStepping: return "only during stepping";
1422 }
1423
1424 static char unknown_state_string[64];
1425 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1426 return unknown_state_string;
1427}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001428
Greg Claytonabe0fed2011-04-18 08:33:37 +00001429size_t
1430Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1431{
Greg Claytonf4124de2012-02-21 00:09:25 +00001432 ExecutionContext exe_ctx (shared_from_this());
1433 Target *target = exe_ctx.GetTargetPtr();
1434 Process *process = exe_ctx.GetProcessPtr();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001435 size_t num_frames_shown = 0;
1436 strm.Indent();
Greg Claytonf4124de2012-02-21 00:09:25 +00001437 bool is_selected = false;
1438 if (process)
1439 {
1440 if (process->GetThreadList().GetSelectedThread().get() == this)
1441 is_selected = true;
1442 }
1443 strm.Printf("%c ", is_selected ? '*' : ' ');
1444 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001445 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001446 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001447 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001448 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001449 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1450 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1451 {
1452 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1453 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001454 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001455 }
1456
1457 DumpUsingSettingsFormat (strm, start_frame);
1458
1459 if (num_frames > 0)
1460 {
1461 strm.IndentMore();
1462
1463 const bool show_frame_info = true;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001464 strm.IndentMore ();
Greg Clayton2450cb12012-03-29 01:41:38 +00001465 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1466 start_frame,
1467 num_frames,
1468 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001469 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001470 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001471 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001472 }
1473 return num_frames_shown;
1474}
1475
1476size_t
1477Thread::GetStackFrameStatus (Stream& strm,
1478 uint32_t first_frame,
1479 uint32_t num_frames,
1480 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001481 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +00001482{
Greg Clayton2450cb12012-03-29 01:41:38 +00001483 return GetStackFrameList()->GetStatus (strm,
1484 first_frame,
1485 num_frames,
1486 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001487 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001488}
1489
Peter Collingbournee426d852011-06-03 20:40:54 +00001490bool
1491Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1492{
1493 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1494 if (frame_sp)
1495 {
1496 checkpoint.SetStackID(frame_sp->GetStackID());
1497 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1498 }
1499 return false;
1500}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001501
Peter Collingbournee426d852011-06-03 20:40:54 +00001502bool
1503Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1504{
Jim Inghama17a81a2012-09-12 00:40:39 +00001505 return ResetFrameZeroRegisters (checkpoint.GetData());
1506}
1507
1508bool
1509Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1510{
Peter Collingbournee426d852011-06-03 20:40:54 +00001511 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1512 if (frame_sp)
1513 {
Jim Inghama17a81a2012-09-12 00:40:39 +00001514 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (register_data_sp);
Peter Collingbournee426d852011-06-03 20:40:54 +00001515
1516 // Clear out all stack frames as our world just changed.
1517 ClearStackFrames();
1518 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1519
1520 return ret;
1521 }
1522 return false;
1523}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001524
Greg Clayton37f962e2011-08-22 02:49:39 +00001525Unwind *
1526Thread::GetUnwinder ()
1527{
1528 if (m_unwinder_ap.get() == NULL)
1529 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001530 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton37f962e2011-08-22 02:49:39 +00001531 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1532 switch (machine)
1533 {
1534 case llvm::Triple::x86_64:
1535 case llvm::Triple::x86:
1536 case llvm::Triple::arm:
1537 case llvm::Triple::thumb:
1538 m_unwinder_ap.reset (new UnwindLLDB (*this));
1539 break;
1540
1541 default:
1542 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1543 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1544 break;
1545 }
1546 }
1547 return m_unwinder_ap.get();
1548}
1549
1550
Greg Claytoncf5927e2012-05-18 02:38:05 +00001551void
1552Thread::Flush ()
1553{
1554 ClearStackFrames ();
1555 m_reg_context_sp.reset();
1556}