blob: e002ec9a2f02c54cfa9209b801aaa0d07f266647 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/lldb-private-log.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000011#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Claytona830adb2010-10-04 01:05:56 +000012#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "lldb/Core/Log.h"
14#include "lldb/Core/Stream.h"
15#include "lldb/Core/StreamString.h"
Jim Ingham20594b12010-09-08 03:14:33 +000016#include "lldb/Core/RegularExpression.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000017#include "lldb/Host/Host.h"
Jim Ingham46657452012-09-27 01:15:29 +000018#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Target/DynamicLoader.h"
20#include "lldb/Target/ExecutionContext.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000021#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Target/Process.h"
23#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000024#include "lldb/Target/StopInfo.h"
Greg Clayton7661a982010-07-23 16:45:51 +000025#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026#include "lldb/Target/Thread.h"
27#include "lldb/Target/ThreadPlan.h"
28#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000029#include "lldb/Target/ThreadPlanBase.h"
30#include "lldb/Target/ThreadPlanStepInstruction.h"
31#include "lldb/Target/ThreadPlanStepOut.h"
32#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
33#include "lldb/Target/ThreadPlanStepThrough.h"
34#include "lldb/Target/ThreadPlanStepInRange.h"
35#include "lldb/Target/ThreadPlanStepOverRange.h"
36#include "lldb/Target/ThreadPlanRunToAddress.h"
37#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000038#include "lldb/Target/ThreadSpec.h"
Jim Ingham71219082010-08-12 02:14:28 +000039#include "lldb/Target/Unwind.h"
Greg Clayton37f962e2011-08-22 02:49:39 +000040#include "Plugins/Process/Utility/UnwindLLDB.h"
41#include "UnwindMacOSXFrameBackchain.h"
42
Chris Lattner24943d22010-06-08 16:52:24 +000043
44using namespace lldb;
45using namespace lldb_private;
46
Greg Clayton73844aa2012-08-22 17:17:09 +000047
48const ThreadPropertiesSP &
49Thread::GetGlobalProperties()
50{
51 static ThreadPropertiesSP g_settings_sp;
52 if (!g_settings_sp)
53 g_settings_sp.reset (new ThreadProperties (true));
54 return g_settings_sp;
55}
56
57static PropertyDefinition
58g_properties[] =
59{
60 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
61 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
62 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
63};
64
65enum {
66 ePropertyStepAvoidRegex,
67 ePropertyEnableThreadTrace
68};
69
70
71class ThreadOptionValueProperties : public OptionValueProperties
72{
73public:
74 ThreadOptionValueProperties (const ConstString &name) :
75 OptionValueProperties (name)
76 {
77 }
78
79 // This constructor is used when creating ThreadOptionValueProperties when it
80 // is part of a new lldb_private::Thread instance. It will copy all current
81 // global property values as needed
82 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Claytonc6e82e42012-08-22 18:39:03 +000083 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton73844aa2012-08-22 17:17:09 +000084 {
85 }
86
87 virtual const Property *
88 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
89 {
90 // When gettings the value for a key from the thread options, we will always
91 // try and grab the setting from the current thread if there is one. Else we just
92 // use the one from this instance.
93 if (exe_ctx)
94 {
95 Thread *thread = exe_ctx->GetThreadPtr();
96 if (thread)
97 {
98 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
99 if (this != instance_properties)
100 return instance_properties->ProtectedGetPropertyAtIndex (idx);
101 }
102 }
103 return ProtectedGetPropertyAtIndex (idx);
104 }
105};
106
107
108
109ThreadProperties::ThreadProperties (bool is_global) :
110 Properties ()
111{
112 if (is_global)
113 {
114 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
115 m_collection_sp->Initialize(g_properties);
116 }
117 else
118 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
119}
120
121ThreadProperties::~ThreadProperties()
122{
123}
124
125const RegularExpression *
126ThreadProperties::GetSymbolsToAvoidRegexp()
127{
128 const uint32_t idx = ePropertyStepAvoidRegex;
129 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
130}
131
132bool
133ThreadProperties::GetTraceEnabledState() const
134{
135 const uint32_t idx = ePropertyEnableThreadTrace;
136 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
137}
138
139
Greg Claytonf4124de2012-02-21 00:09:25 +0000140Thread::Thread (const ProcessSP &process_sp, lldb::tid_t tid) :
Greg Clayton73844aa2012-08-22 17:17:09 +0000141 ThreadProperties (false),
Chris Lattner24943d22010-06-08 16:52:24 +0000142 UserID (tid),
Greg Claytonf4124de2012-02-21 00:09:25 +0000143 m_process_wp (process_sp),
Greg Clayton643ee732010-08-04 01:40:35 +0000144 m_actual_stop_info_sp (),
Greg Claytonf4124de2012-02-21 00:09:25 +0000145 m_index_id (process_sp->GetNextThreadIndexID ()),
Chris Lattner24943d22010-06-08 16:52:24 +0000146 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000147 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +0000148 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +0000149 m_plan_stack (),
Chris Lattner24943d22010-06-08 16:52:24 +0000150 m_completed_plan_stack(),
Greg Clayton2450cb12012-03-29 01:41:38 +0000151 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000152 m_curr_frames_sp (),
153 m_prev_frames_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000154 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +0000155 m_resume_state (eStateRunning),
Jim Ingham149d1f52012-01-31 23:09:20 +0000156 m_temporary_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +0000157 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000158 m_destroy_called (false),
159 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +0000160
Chris Lattner24943d22010-06-08 16:52:24 +0000161{
Greg Claytone005f2c2010-11-06 01:53:30 +0000162 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000163 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000164 log->Printf ("%p Thread::Thread(tid = 0x%4.4llx)", this, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000165
166 QueueFundamentalPlan(true);
167}
168
169
170Thread::~Thread()
171{
Greg Claytone005f2c2010-11-06 01:53:30 +0000172 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000173 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000174 log->Printf ("%p Thread::~Thread(tid = 0x%4.4llx)", this, GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +0000175 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
176 assert (m_destroy_called);
177}
178
179void
180Thread::DestroyThread ()
181{
182 m_plan_stack.clear();
183 m_discarded_plan_stack.clear();
184 m_completed_plan_stack.clear();
Jim Inghame93055a2012-04-10 00:44:25 +0000185 m_actual_stop_info_sp.reset();
Jim Inghamcdea2362010-11-18 02:47:07 +0000186 m_destroy_called = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000187}
188
Jim Ingham6297a3a2010-10-20 00:39:53 +0000189lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000190Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +0000191{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000192 ThreadPlanSP plan_sp (GetCompletedPlan());
Jim Ingham707b7a82012-05-01 18:38:37 +0000193 if (plan_sp && plan_sp->PlanSucceeded())
Jim Ingham1586d972011-12-17 01:35:57 +0000194 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000195 else
Jim Ingham24e0d612011-08-09 00:32:52 +0000196 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000197 ProcessSP process_sp (GetProcess());
198 if (process_sp
199 && m_actual_stop_info_sp
Jim Ingham24e0d612011-08-09 00:32:52 +0000200 && m_actual_stop_info_sp->IsValid()
Greg Claytonf4124de2012-02-21 00:09:25 +0000201 && m_thread_stop_reason_stop_id == process_sp->GetStopID())
Jim Ingham24e0d612011-08-09 00:32:52 +0000202 return m_actual_stop_info_sp;
203 else
204 return GetPrivateStopReason ();
205 }
Chris Lattner24943d22010-06-08 16:52:24 +0000206}
207
Greg Clayton3acaa922012-09-25 02:40:06 +0000208lldb::StopReason
209Thread::GetStopReason()
210{
211 lldb::StopInfoSP stop_info_sp (GetStopInfo ());
212 if (stop_info_sp)
Filipe Cabecinhasc50a1dd2012-09-28 15:55:43 +0000213 return stop_info_sp->GetStopReason();
Greg Clayton3acaa922012-09-25 02:40:06 +0000214 return eStopReasonNone;
215}
216
217
218
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000219void
220Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
221{
222 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham24e0d612011-08-09 00:32:52 +0000223 if (m_actual_stop_info_sp)
224 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Claytonf4124de2012-02-21 00:09:25 +0000225 ProcessSP process_sp (GetProcess());
226 if (process_sp)
227 m_thread_stop_reason_stop_id = process_sp->GetStopID();
228 else
229 m_thread_stop_reason_stop_id = UINT32_MAX;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000230}
231
232void
233Thread::SetStopInfoToNothing()
234{
235 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
236 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
237 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
238}
239
Chris Lattner24943d22010-06-08 16:52:24 +0000240bool
241Thread::ThreadStoppedForAReason (void)
242{
Jim Ingham9880efa2012-08-11 00:35:26 +0000243 return (bool) GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +0000244}
245
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000246bool
247Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
248{
249 if (!SaveFrameZeroState(saved_state.register_backup))
250 return false;
251
252 saved_state.stop_info_sp = GetStopInfo();
Greg Claytonf4124de2012-02-21 00:09:25 +0000253 ProcessSP process_sp (GetProcess());
254 if (process_sp)
255 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham36de3c02012-09-07 23:36:43 +0000256 saved_state.current_inlined_depth = GetCurrentInlinedDepth();
257
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000258 return true;
259}
260
261bool
262Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
263{
264 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham11a837d2011-01-25 02:47:23 +0000265 if (saved_state.stop_info_sp)
266 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000267 SetStopInfo(saved_state.stop_info_sp);
Jim Ingham36de3c02012-09-07 23:36:43 +0000268 GetStackFrameList()->SetCurrentInlinedDepth (saved_state.current_inlined_depth);
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000269 return true;
270}
271
Chris Lattner24943d22010-06-08 16:52:24 +0000272StateType
273Thread::GetState() const
274{
275 // If any other threads access this we will need a mutex for it
276 Mutex::Locker locker(m_state_mutex);
277 return m_state;
278}
279
280void
281Thread::SetState(StateType state)
282{
283 Mutex::Locker locker(m_state_mutex);
284 m_state = state;
285}
286
287void
288Thread::WillStop()
289{
290 ThreadPlan *current_plan = GetCurrentPlan();
291
292 // FIXME: I may decide to disallow threads with no plans. In which
293 // case this should go to an assert.
294
295 if (!current_plan)
296 return;
297
298 current_plan->WillStop();
299}
300
301void
302Thread::SetupForResume ()
303{
304 if (GetResumeState() != eStateSuspended)
305 {
306
307 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
308 // telling the current plan it will resume, since we might change what the current
309 // plan is.
310
311 lldb::addr_t pc = GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +0000312 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Chris Lattner24943d22010-06-08 16:52:24 +0000313 if (bp_site_sp && bp_site_sp->IsEnabled())
314 {
315 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
316 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000317
318 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000319
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000320 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
321 {
322 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
323 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000324 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000325 ThreadPlanSP step_bp_plan_sp;
326 step_bp_plan->SetPrivate (true);
327
Chris Lattner24943d22010-06-08 16:52:24 +0000328 if (GetCurrentPlan()->RunState() != eStateStepping)
329 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000330 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000331 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000332 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000333 QueueThreadPlan (step_bp_plan_sp, false);
334 }
335 }
336 }
337 }
338}
339
340bool
341Thread::WillResume (StateType resume_state)
342{
343 // At this point clear the completed plan stack.
344 m_completed_plan_stack.clear();
345 m_discarded_plan_stack.clear();
346
Greg Clayton3acaa922012-09-25 02:40:06 +0000347 m_temporary_resume_state = resume_state;
Jim Ingham149d1f52012-01-31 23:09:20 +0000348
349 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
350 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
351 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
352 // about the fact that we are resuming...
Greg Claytonf4124de2012-02-21 00:09:25 +0000353 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham149d1f52012-01-31 23:09:20 +0000354 if (m_thread_stop_reason_stop_id == process_stop_id &&
355 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
356 {
357 StopInfo *stop_info = GetPrivateStopReason().get();
358 if (stop_info)
359 stop_info->WillResume (resume_state);
360 }
Chris Lattner24943d22010-06-08 16:52:24 +0000361
362 // Tell all the plans that we are about to resume in case they need to clear any state.
363 // We distinguish between the plan on the top of the stack and the lower
364 // plans in case a plan needs to do any special business before it runs.
365
366 ThreadPlan *plan_ptr = GetCurrentPlan();
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000367 bool need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000368
369 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
370 {
371 plan_ptr->WillResume (resume_state, false);
372 }
Greg Clayton643ee732010-08-04 01:40:35 +0000373
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000374 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
375 // In that case, don't reset it here.
376
377 if (need_to_resume)
378 {
379 m_actual_stop_info_sp.reset();
380 }
381
382 return need_to_resume;
Chris Lattner24943d22010-06-08 16:52:24 +0000383}
384
385void
386Thread::DidResume ()
387{
388 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
389}
390
391bool
392Thread::ShouldStop (Event* event_ptr)
393{
394 ThreadPlan *current_plan = GetCurrentPlan();
395 bool should_stop = true;
396
Greg Claytone005f2c2010-11-06 01:53:30 +0000397 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham149d1f52012-01-31 23:09:20 +0000398
399 if (GetResumeState () == eStateSuspended)
400 {
401 if (log)
402 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
403 __FUNCTION__,
404 GetID ());
405// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
406// __FUNCTION__,
407// GetID (),
408// GetRegisterContext()->GetPC());
409 return false;
410 }
411
412 if (GetTemporaryResumeState () == eStateSuspended)
413 {
414 if (log)
415 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
416 __FUNCTION__,
417 GetID ());
418// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
419// __FUNCTION__,
420// GetID (),
421// GetRegisterContext()->GetPC());
422 return false;
423 }
424
425 if (ThreadStoppedForAReason() == false)
426 {
427 if (log)
428 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
429 __FUNCTION__,
430 GetID (),
431 GetRegisterContext()->GetPC());
432 return false;
433 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000434
Chris Lattner24943d22010-06-08 16:52:24 +0000435 if (log)
436 {
Jim Ingham149d1f52012-01-31 23:09:20 +0000437 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
438 __FUNCTION__,
439 GetID (),
440 GetRegisterContext()->GetPC());
Jim Inghame8f4e112011-10-15 00:23:43 +0000441 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000442 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000443 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000444 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000445 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000446 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000447
448 // The top most plan always gets to do the trace log...
449 current_plan->DoTraceLog ();
Jim Inghame787c7e2012-04-20 21:16:56 +0000450
451 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
452 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
453 // do any more work on this stop.
454 StopInfoSP private_stop_info (GetPrivateStopReason());
455 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
456 {
457 if (log)
458 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
459 return false;
460 }
Chris Lattner24943d22010-06-08 16:52:24 +0000461
Jim Ingham98d50212012-09-05 21:12:49 +0000462 // If we've already been restarted, don't query the plans since the state they would examine is not current.
463 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
464 return false;
465
466 // Before the plans see the state of the world, calculate the current inlined depth.
467 GetStackFrameList()->CalculateCurrentInlinedDepth();
468
Jim Inghamad382c52011-12-03 01:52:59 +0000469 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
470 // If that plan is still working, then we don't need to do any more work. If the plan that explains
471 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
472 // whether they still need to do more work.
473
474 bool done_processing_current_plan = false;
475
476 if (!current_plan->PlanExplainsStop())
477 {
478 if (current_plan->TracerExplainsStop())
479 {
480 done_processing_current_plan = true;
481 should_stop = false;
482 }
483 else
484 {
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000485 // If the current plan doesn't explain the stop, then find one that
Jim Inghamad382c52011-12-03 01:52:59 +0000486 // does and let it handle the situation.
487 ThreadPlan *plan_ptr = current_plan;
488 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
489 {
490 if (plan_ptr->PlanExplainsStop())
491 {
492 should_stop = plan_ptr->ShouldStop (event_ptr);
493
494 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
495 // and all the plans below it off the stack.
496
497 if (plan_ptr->MischiefManaged())
498 {
Jim Inghamd1ec3d62012-05-11 23:49:49 +0000499 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000500 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Inghamad382c52011-12-03 01:52:59 +0000501
502 do
503 {
504 if (should_stop)
505 current_plan->WillStop();
506 PopPlan();
507 }
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000508 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
509 // Now, if the responsible plan was not "Okay to discard" then we're done,
510 // otherwise we forward this to the next plan in the stack below.
511 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
512 done_processing_current_plan = true;
513 else
514 done_processing_current_plan = false;
Jim Inghamad382c52011-12-03 01:52:59 +0000515 }
516 else
517 done_processing_current_plan = true;
518
519 break;
520 }
521
522 }
523 }
524 }
525
526 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000527 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000528 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000529
Jim Inghame8f4e112011-10-15 00:23:43 +0000530 if (log)
531 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
532
Jim Inghamf9f40c22011-02-08 05:20:59 +0000533 // We're starting from the base plan, so just let it decide;
534 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000535 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000536 should_stop = current_plan->ShouldStop (event_ptr);
537 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000538 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000539 }
540 else
541 {
542 // Otherwise, don't let the base plan override what the other plans say to do, since
543 // presumably if there were other plans they would know what to do...
544 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000545 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000546 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000547 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000548
549 should_stop = current_plan->ShouldStop(event_ptr);
550 if (log)
551 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
552 if (current_plan->MischiefManaged())
553 {
554 if (should_stop)
555 current_plan->WillStop();
556
557 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
558 // Otherwise, see if the plan's parent wants to stop.
559
560 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
561 {
562 PopPlan();
563 break;
564 }
565 else
566 {
567
568 PopPlan();
569
570 current_plan = GetCurrentPlan();
571 if (current_plan == NULL)
572 {
573 break;
574 }
575 }
Chris Lattner24943d22010-06-08 16:52:24 +0000576 }
577 else
578 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000579 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000580 }
Chris Lattner24943d22010-06-08 16:52:24 +0000581 }
582 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000583
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000584 if (over_ride_stop)
585 should_stop = false;
Jim Ingham88e3de22012-05-03 21:19:36 +0000586
587 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
588 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
589 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
590 // This code clears stale plans off the stack.
591
592 if (should_stop)
593 {
594 ThreadPlan *plan_ptr = GetCurrentPlan();
595 while (!PlanIsBasePlan(plan_ptr))
596 {
597 bool stale = plan_ptr->IsPlanStale ();
598 ThreadPlan *examined_plan = plan_ptr;
599 plan_ptr = GetPreviousPlan (examined_plan);
600
601 if (stale)
602 {
603 if (log)
604 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
605 DiscardThreadPlansUpToPlan(examined_plan);
606 }
607 }
608 }
609
Chris Lattner24943d22010-06-08 16:52:24 +0000610 }
Chris Lattner24943d22010-06-08 16:52:24 +0000611
Jim Inghame8f4e112011-10-15 00:23:43 +0000612 if (log)
613 {
614 StreamString s;
615 s.IndentMore();
616 DumpThreadPlans(&s);
617 log->Printf ("Plan stack final state:\n%s", s.GetData());
618 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
619 }
Chris Lattner24943d22010-06-08 16:52:24 +0000620 return should_stop;
621}
622
623Vote
624Thread::ShouldReportStop (Event* event_ptr)
625{
626 StateType thread_state = GetResumeState ();
Jim Ingham149d1f52012-01-31 23:09:20 +0000627 StateType temp_thread_state = GetTemporaryResumeState();
628
Greg Claytone005f2c2010-11-06 01:53:30 +0000629 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000630
631 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
632 {
633 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000634 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 +0000635 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000636 }
Chris Lattner24943d22010-06-08 16:52:24 +0000637
Jim Ingham149d1f52012-01-31 23:09:20 +0000638 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
639 {
640 if (log)
641 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
642 return eVoteNoOpinion;
643 }
644
645 if (!ThreadStoppedForAReason())
646 {
647 if (log)
648 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
649 return eVoteNoOpinion;
650 }
651
Chris Lattner24943d22010-06-08 16:52:24 +0000652 if (m_completed_plan_stack.size() > 0)
653 {
654 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000655 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000656 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 +0000657 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
658 }
659 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000660 {
661 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000662 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000663 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000664 }
Chris Lattner24943d22010-06-08 16:52:24 +0000665}
666
667Vote
668Thread::ShouldReportRun (Event* event_ptr)
669{
670 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000671
Chris Lattner24943d22010-06-08 16:52:24 +0000672 if (thread_state == eStateSuspended
673 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000674 {
Chris Lattner24943d22010-06-08 16:52:24 +0000675 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000676 }
677
678 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000679 if (m_completed_plan_stack.size() > 0)
680 {
681 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000682 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000683 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 +0000684 GetIndexID(),
685 GetID(),
686 m_completed_plan_stack.back()->GetName());
687
Chris Lattner24943d22010-06-08 16:52:24 +0000688 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
689 }
690 else
Jim Inghamac959662011-01-24 06:34:17 +0000691 {
692 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000693 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 +0000694 GetIndexID(),
695 GetID(),
696 GetCurrentPlan()->GetName());
697
Chris Lattner24943d22010-06-08 16:52:24 +0000698 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000699 }
Chris Lattner24943d22010-06-08 16:52:24 +0000700}
701
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000702bool
703Thread::MatchesSpec (const ThreadSpec *spec)
704{
705 if (spec == NULL)
706 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000707
Jim Inghama2664912012-03-07 22:03:04 +0000708 return spec->ThreadPassesBasicTests(*this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000709}
710
Chris Lattner24943d22010-06-08 16:52:24 +0000711void
712Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
713{
714 if (thread_plan_sp)
715 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000716 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
717 if (!thread_plan_sp->GetThreadPlanTracer())
718 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000719 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000720
Chris Lattner24943d22010-06-08 16:52:24 +0000721 thread_plan_sp->DidPush();
722
Greg Claytone005f2c2010-11-06 01:53:30 +0000723 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000724 if (log)
725 {
726 StreamString s;
727 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Clayton444e35b2011-10-19 18:09:39 +0000728 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
Chris Lattner24943d22010-06-08 16:52:24 +0000729 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000730 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000731 }
732 }
733}
734
735void
736Thread::PopPlan ()
737{
Greg Claytone005f2c2010-11-06 01:53:30 +0000738 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000739
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000740 if (m_plan_stack.size() <= 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000741 return;
742 else
743 {
744 ThreadPlanSP &plan = m_plan_stack.back();
745 if (log)
746 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000747 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000748 }
749 m_completed_plan_stack.push_back (plan);
750 plan->WillPop();
751 m_plan_stack.pop_back();
752 }
753}
754
755void
756Thread::DiscardPlan ()
757{
758 if (m_plan_stack.size() > 1)
759 {
760 ThreadPlanSP &plan = m_plan_stack.back();
761 m_discarded_plan_stack.push_back (plan);
762 plan->WillPop();
763 m_plan_stack.pop_back();
764 }
765}
766
767ThreadPlan *
768Thread::GetCurrentPlan ()
769{
Jim Ingham2f41d272012-04-19 00:17:05 +0000770 // There will always be at least the base plan. If somebody is mucking with a
771 // thread with an empty plan stack, we should assert right away.
772 assert (!m_plan_stack.empty());
773
774 return m_plan_stack.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000775}
776
777ThreadPlanSP
778Thread::GetCompletedPlan ()
779{
780 ThreadPlanSP empty_plan_sp;
781 if (!m_completed_plan_stack.empty())
782 {
783 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
784 {
785 ThreadPlanSP completed_plan_sp;
786 completed_plan_sp = m_completed_plan_stack[i];
787 if (!completed_plan_sp->GetPrivate ())
788 return completed_plan_sp;
789 }
790 }
791 return empty_plan_sp;
792}
793
Jim Ingham1586d972011-12-17 01:35:57 +0000794ValueObjectSP
795Thread::GetReturnValueObject ()
796{
797 if (!m_completed_plan_stack.empty())
798 {
799 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
800 {
801 ValueObjectSP return_valobj_sp;
802 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
803 if (return_valobj_sp)
804 return return_valobj_sp;
805 }
806 }
807 return ValueObjectSP();
808}
809
Chris Lattner24943d22010-06-08 16:52:24 +0000810bool
811Thread::IsThreadPlanDone (ThreadPlan *plan)
812{
Chris Lattner24943d22010-06-08 16:52:24 +0000813 if (!m_completed_plan_stack.empty())
814 {
815 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
816 {
817 if (m_completed_plan_stack[i].get() == plan)
818 return true;
819 }
820 }
821 return false;
822}
823
824bool
825Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
826{
Chris Lattner24943d22010-06-08 16:52:24 +0000827 if (!m_discarded_plan_stack.empty())
828 {
829 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
830 {
831 if (m_discarded_plan_stack[i].get() == plan)
832 return true;
833 }
834 }
835 return false;
836}
837
838ThreadPlan *
839Thread::GetPreviousPlan (ThreadPlan *current_plan)
840{
841 if (current_plan == NULL)
842 return NULL;
843
844 int stack_size = m_completed_plan_stack.size();
845 for (int i = stack_size - 1; i > 0; i--)
846 {
847 if (current_plan == m_completed_plan_stack[i].get())
848 return m_completed_plan_stack[i-1].get();
849 }
850
851 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
852 {
Chris Lattner24943d22010-06-08 16:52:24 +0000853 if (m_plan_stack.size() > 0)
854 return m_plan_stack.back().get();
855 else
856 return NULL;
857 }
858
859 stack_size = m_plan_stack.size();
860 for (int i = stack_size - 1; i > 0; i--)
861 {
862 if (current_plan == m_plan_stack[i].get())
863 return m_plan_stack[i-1].get();
864 }
865 return NULL;
866}
867
868void
869Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
870{
871 if (abort_other_plans)
872 DiscardThreadPlans(true);
873
874 PushPlan (thread_plan_sp);
875}
876
Jim Ingham745ac7a2010-11-11 19:26:09 +0000877
878void
879Thread::EnableTracer (bool value, bool single_stepping)
880{
881 int stack_size = m_plan_stack.size();
882 for (int i = 0; i < stack_size; i++)
883 {
884 if (m_plan_stack[i]->GetThreadPlanTracer())
885 {
886 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
887 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
888 }
889 }
890}
891
892void
893Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
894{
895 int stack_size = m_plan_stack.size();
896 for (int i = 0; i < stack_size; i++)
897 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
898}
899
Chris Lattner24943d22010-06-08 16:52:24 +0000900void
Jim Inghamea9d4262010-11-05 19:25:48 +0000901Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
902{
Jim Ingham88e3de22012-05-03 21:19:36 +0000903 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
904}
905
906void
907Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
908{
Greg Claytone005f2c2010-11-06 01:53:30 +0000909 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000910 if (log)
911 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000912 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 +0000913 }
914
915 int stack_size = m_plan_stack.size();
916
917 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
918 // stack, and if so discard up to and including it.
919
Jim Ingham88e3de22012-05-03 21:19:36 +0000920 if (up_to_plan_ptr == NULL)
Jim Inghamea9d4262010-11-05 19:25:48 +0000921 {
922 for (int i = stack_size - 1; i > 0; i--)
923 DiscardPlan();
924 }
925 else
926 {
927 bool found_it = false;
928 for (int i = stack_size - 1; i > 0; i--)
929 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000930 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000931 found_it = true;
932 }
933 if (found_it)
934 {
935 bool last_one = false;
936 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
937 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000938 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000939 last_one = true;
940 DiscardPlan();
941 }
942 }
943 }
944 return;
945}
946
947void
Chris Lattner24943d22010-06-08 16:52:24 +0000948Thread::DiscardThreadPlans(bool force)
949{
Greg Claytone005f2c2010-11-06 01:53:30 +0000950 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000951 if (log)
952 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000953 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000954 }
955
956 if (force)
957 {
958 int stack_size = m_plan_stack.size();
959 for (int i = stack_size - 1; i > 0; i--)
960 {
961 DiscardPlan();
962 }
963 return;
964 }
965
966 while (1)
967 {
968
969 int master_plan_idx;
Jim Inghamf1dbb712012-09-11 00:09:25 +0000970 bool discard = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000971
972 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
973 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
974 {
975 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
976 {
977 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
978 break;
979 }
980 }
981
982 if (discard)
983 {
984 // First pop all the dependent plans:
985 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
986 {
987
988 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
989 // for the plan leaves it in a state that it is safe to pop the plan
990 // with no more notice?
991 DiscardPlan();
992 }
993
994 // Now discard the master plan itself.
995 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
996 // discard it's dependent plans, but not it...
997 if (master_plan_idx > 0)
998 {
999 DiscardPlan();
1000 }
1001 }
1002 else
1003 {
1004 // If the master plan doesn't want to get discarded, then we're done.
1005 break;
1006 }
1007
1008 }
Chris Lattner24943d22010-06-08 16:52:24 +00001009}
1010
Jim Ingham2bcbaf62012-04-09 22:37:39 +00001011bool
1012Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
1013{
1014 if (plan_ptr->IsBasePlan())
1015 return true;
1016 else if (m_plan_stack.size() == 0)
1017 return false;
1018 else
1019 return m_plan_stack[0].get() == plan_ptr;
1020}
1021
Chris Lattner24943d22010-06-08 16:52:24 +00001022ThreadPlan *
1023Thread::QueueFundamentalPlan (bool abort_other_plans)
1024{
1025 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1026 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1027 return thread_plan_sp.get();
1028}
1029
1030ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001031Thread::QueueThreadPlanForStepSingleInstruction
1032(
1033 bool step_over,
1034 bool abort_other_plans,
1035 bool stop_other_threads
1036)
Chris Lattner24943d22010-06-08 16:52:24 +00001037{
1038 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1039 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1040 return thread_plan_sp.get();
1041}
1042
1043ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001044Thread::QueueThreadPlanForStepRange
1045(
1046 bool abort_other_plans,
1047 StepType type,
1048 const AddressRange &range,
1049 const SymbolContext &addr_context,
1050 lldb::RunMode stop_other_threads,
1051 bool avoid_code_without_debug_info
1052)
Chris Lattner24943d22010-06-08 16:52:24 +00001053{
1054 ThreadPlanSP thread_plan_sp;
1055 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001056 {
1057 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1058 if (avoid_code_without_debug_info)
1059 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1060 else
1061 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1062 thread_plan_sp.reset (plan);
1063 }
Chris Lattner24943d22010-06-08 16:52:24 +00001064 else
1065 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1066
1067 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1068 return thread_plan_sp.get();
1069}
1070
1071
1072ThreadPlan *
1073Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1074{
1075 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1076 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1077 return thread_plan_sp.get();
1078}
1079
1080ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001081Thread::QueueThreadPlanForStepOut
1082(
1083 bool abort_other_plans,
1084 SymbolContext *addr_context,
1085 bool first_insn,
1086 bool stop_other_threads,
1087 Vote stop_vote,
1088 Vote run_vote,
1089 uint32_t frame_idx
1090)
Chris Lattner24943d22010-06-08 16:52:24 +00001091{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001092 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1093 addr_context,
1094 first_insn,
1095 stop_other_threads,
1096 stop_vote,
1097 run_vote,
1098 frame_idx));
Sean Callananf6d5fea2012-07-31 22:19:25 +00001099
1100 if (thread_plan_sp->ValidatePlan(NULL))
1101 {
1102 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1103 return thread_plan_sp.get();
1104 }
1105 else
1106 {
1107 return NULL;
1108 }
Chris Lattner24943d22010-06-08 16:52:24 +00001109}
1110
1111ThreadPlan *
Jim Ingham038fa8e2012-05-10 01:35:39 +00001112Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner24943d22010-06-08 16:52:24 +00001113{
Jim Ingham038fa8e2012-05-10 01:35:39 +00001114 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Inghamad382c52011-12-03 01:52:59 +00001115 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1116 return NULL;
1117
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::QueueThreadPlanForCallFunction (bool abort_other_plans,
1124 Address& function,
1125 lldb::addr_t arg,
1126 bool stop_other_threads,
1127 bool discard_on_error)
1128{
Jim Ingham016ef882011-12-22 19:12:40 +00001129 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner24943d22010-06-08 16:52:24 +00001130 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1131 return thread_plan_sp.get();
1132}
1133
1134ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001135Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1136 Address &target_addr,
1137 bool stop_other_threads)
1138{
1139 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1140 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1141 return thread_plan_sp.get();
1142}
1143
1144ThreadPlan *
1145Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001146 lldb::addr_t *address_list,
1147 size_t num_addresses,
1148 bool stop_other_threads,
1149 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001150{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001151 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +00001152 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1153 return thread_plan_sp.get();
1154
1155}
1156
1157uint32_t
1158Thread::GetIndexID () const
1159{
1160 return m_index_id;
1161}
1162
1163void
1164Thread::DumpThreadPlans (lldb_private::Stream *s) const
1165{
1166 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +00001167 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +00001168 s->Indent();
Greg Clayton444e35b2011-10-19 18:09:39 +00001169 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 +00001170 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +00001171 {
Chris Lattner24943d22010-06-08 16:52:24 +00001172 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +00001173 s->Indent();
1174 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +00001175 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +00001176 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +00001177 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +00001178 }
1179
Chris Lattner24943d22010-06-08 16:52:24 +00001180 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001181 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001182 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001183 s->Indent();
1184 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1185 for (i = stack_size - 1; i >= 0; i--)
1186 {
1187 s->IndentMore();
1188 s->Indent();
1189 s->Printf ("Element %d: ", i);
1190 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1191 s->EOL();
1192 s->IndentLess();
1193 }
Chris Lattner24943d22010-06-08 16:52:24 +00001194 }
1195
1196 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001197 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001198 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001199 s->Indent();
1200 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1201 for (i = stack_size - 1; i >= 0; i--)
1202 {
1203 s->IndentMore();
1204 s->Indent();
1205 s->Printf ("Element %d: ", i);
1206 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1207 s->EOL();
1208 s->IndentLess();
1209 }
Chris Lattner24943d22010-06-08 16:52:24 +00001210 }
1211
1212}
1213
Greg Clayton289afcb2012-02-18 05:35:26 +00001214TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001215Thread::CalculateTarget ()
1216{
Greg Claytonf4124de2012-02-21 00:09:25 +00001217 TargetSP target_sp;
1218 ProcessSP process_sp(GetProcess());
1219 if (process_sp)
1220 target_sp = process_sp->CalculateTarget();
1221 return target_sp;
1222
Chris Lattner24943d22010-06-08 16:52:24 +00001223}
1224
Greg Clayton289afcb2012-02-18 05:35:26 +00001225ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001226Thread::CalculateProcess ()
1227{
Greg Claytonf4124de2012-02-21 00:09:25 +00001228 return GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001229}
1230
Greg Clayton289afcb2012-02-18 05:35:26 +00001231ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001232Thread::CalculateThread ()
1233{
Greg Clayton289afcb2012-02-18 05:35:26 +00001234 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001235}
1236
Greg Clayton289afcb2012-02-18 05:35:26 +00001237StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001238Thread::CalculateStackFrame ()
1239{
Greg Clayton289afcb2012-02-18 05:35:26 +00001240 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001241}
1242
1243void
Greg Claytona830adb2010-10-04 01:05:56 +00001244Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001245{
Greg Claytonf4124de2012-02-21 00:09:25 +00001246 exe_ctx.SetContext (shared_from_this());
Chris Lattner24943d22010-06-08 16:52:24 +00001247}
1248
Greg Clayton782b9cc2010-08-25 00:35:26 +00001249
Greg Clayton2450cb12012-03-29 01:41:38 +00001250StackFrameListSP
Greg Clayton782b9cc2010-08-25 00:35:26 +00001251Thread::GetStackFrameList ()
1252{
Greg Clayton2450cb12012-03-29 01:41:38 +00001253 StackFrameListSP frame_list_sp;
1254 Mutex::Locker locker(m_frame_mutex);
1255 if (m_curr_frames_sp)
1256 {
1257 frame_list_sp = m_curr_frames_sp;
1258 }
1259 else
1260 {
1261 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1262 m_curr_frames_sp = frame_list_sp;
1263 }
1264 return frame_list_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +00001265}
1266
Greg Clayton782b9cc2010-08-25 00:35:26 +00001267void
1268Thread::ClearStackFrames ()
1269{
Greg Clayton2450cb12012-03-29 01:41:38 +00001270 Mutex::Locker locker(m_frame_mutex);
1271
Jim Inghambf97d742012-02-29 03:40:22 +00001272 // Only store away the old "reference" StackFrameList if we got all its frames:
1273 // FIXME: At some point we can try to splice in the frames we have fetched into
1274 // the new frame as we make it, but let's not try that now.
1275 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Claytonc51ffbf2011-08-12 21:40:01 +00001276 m_prev_frames_sp.swap (m_curr_frames_sp);
1277 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +00001278}
1279
1280lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001281Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1282{
Greg Clayton2450cb12012-03-29 01:41:38 +00001283 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001284}
1285
Jim Inghama17a81a2012-09-12 00:40:39 +00001286
1287Error
Jim Inghamf59388a2012-09-14 02:14:15 +00001288Thread::ReturnFromFrameWithIndex (uint32_t frame_idx, lldb::ValueObjectSP return_value_sp)
Jim Inghama17a81a2012-09-12 00:40:39 +00001289{
1290 StackFrameSP frame_sp = GetStackFrameAtIndex (frame_idx);
1291 Error return_error;
1292
1293 if (!frame_sp)
1294 {
1295 return_error.SetErrorStringWithFormat("Could not find frame with index %d in thread 0x%llx.", frame_idx, GetID());
1296 }
1297
Jim Inghamf59388a2012-09-14 02:14:15 +00001298 return ReturnFromFrame(frame_sp, return_value_sp);
Jim Inghama17a81a2012-09-12 00:40:39 +00001299}
1300
1301Error
Jim Inghamf59388a2012-09-14 02:14:15 +00001302Thread::ReturnFromFrame (lldb::StackFrameSP frame_sp, lldb::ValueObjectSP return_value_sp)
Jim Inghama17a81a2012-09-12 00:40:39 +00001303{
1304 Error return_error;
1305
1306 if (!frame_sp)
1307 {
1308 return_error.SetErrorString("Can't return to a null frame.");
1309 return return_error;
1310 }
1311
1312 Thread *thread = frame_sp->GetThread().get();
Jim Inghamf59388a2012-09-14 02:14:15 +00001313 uint32_t older_frame_idx = frame_sp->GetFrameIndex() + 1;
1314 StackFrameSP older_frame_sp = thread->GetStackFrameAtIndex(older_frame_idx);
Jim Inghama17a81a2012-09-12 00:40:39 +00001315
1316 if (return_value_sp)
Jim Ingham46657452012-09-27 01:15:29 +00001317 {
Jim Inghama17a81a2012-09-12 00:40:39 +00001318 lldb::ABISP abi = thread->GetProcess()->GetABI();
1319 if (!abi)
1320 {
1321 return_error.SetErrorString("Could not find ABI to set return value.");
1322 }
Jim Ingham46657452012-09-27 01:15:29 +00001323 SymbolContext sc = frame_sp->GetSymbolContext(eSymbolContextFunction);
1324
1325 // FIXME: ValueObject::Cast doesn't currently work correctly, at least not for scalars.
1326 // Turn that back on when that works.
1327 if (0 && sc.function != NULL)
1328 {
1329 Type *function_type = sc.function->GetType();
1330 if (function_type)
1331 {
1332 clang_type_t return_type = sc.function->GetReturnClangType();
1333 if (return_type)
1334 {
1335 ClangASTType ast_type (function_type->GetClangAST(), return_type);
1336 StreamString s;
1337 ast_type.DumpTypeDescription(&s);
1338 ValueObjectSP cast_value_sp = return_value_sp->Cast(ast_type);
1339 if (cast_value_sp)
1340 {
1341 cast_value_sp->SetFormat(eFormatHex);
1342 return_value_sp = cast_value_sp;
1343 }
1344 }
1345 }
1346 }
1347
Jim Inghamf59388a2012-09-14 02:14:15 +00001348 return_error = abi->SetReturnValueObject(older_frame_sp, return_value_sp);
Jim Inghama17a81a2012-09-12 00:40:39 +00001349 if (!return_error.Success())
1350 return return_error;
1351 }
1352
1353 // Now write the return registers for the chosen frame:
Jim Inghamf59388a2012-09-14 02:14:15 +00001354 // Note, we can't use ReadAllRegisterValues->WriteAllRegisterValues, since the read & write
1355 // cook their data
1356 bool copy_success = thread->GetStackFrameAtIndex(0)->GetRegisterContext()->CopyFromRegisterContext(older_frame_sp->GetRegisterContext());
1357 if (copy_success)
Jim Inghama17a81a2012-09-12 00:40:39 +00001358 {
1359 thread->DiscardThreadPlans(true);
Jim Inghamf59388a2012-09-14 02:14:15 +00001360 thread->ClearStackFrames();
Jim Inghama17a81a2012-09-12 00:40:39 +00001361 return return_error;
1362 }
1363 else
1364 {
1365 return_error.SetErrorString("Could not reset register values.");
1366 return return_error;
Jim Inghama17a81a2012-09-12 00:40:39 +00001367 }
1368}
1369
Chris Lattner24943d22010-06-08 16:52:24 +00001370void
Greg Claytona830adb2010-10-04 01:05:56 +00001371Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001372{
Greg Claytonf4124de2012-02-21 00:09:25 +00001373 ExecutionContext exe_ctx (shared_from_this());
1374 Process *process = exe_ctx.GetProcessPtr();
1375 if (process == NULL)
1376 return;
1377
Greg Clayton567e7f32011-09-22 04:58:26 +00001378 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +00001379 SymbolContext frame_sc;
Greg Claytona830adb2010-10-04 01:05:56 +00001380 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +00001381 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001382 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001383 if (frame_sp)
1384 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001385 exe_ctx.SetFrameSP(frame_sp);
1386 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001387 }
1388 }
1389
Greg Claytonf4124de2012-02-21 00:09:25 +00001390 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Claytona830adb2010-10-04 01:05:56 +00001391 assert (thread_format);
1392 const char *end = NULL;
1393 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001394 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001395 &exe_ctx,
1396 NULL,
1397 strm,
1398 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001399}
1400
Greg Clayton990de7b2010-11-18 23:32:35 +00001401void
Caroline Tice2a456812011-03-10 22:14:10 +00001402Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001403{
Greg Clayton990de7b2010-11-18 23:32:35 +00001404}
Jim Ingham20594b12010-09-08 03:14:33 +00001405
Greg Clayton990de7b2010-11-18 23:32:35 +00001406void
Caroline Tice2a456812011-03-10 22:14:10 +00001407Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001408{
Greg Clayton990de7b2010-11-18 23:32:35 +00001409}
Jim Ingham20594b12010-09-08 03:14:33 +00001410
Jim Inghamccd584d2010-09-23 17:40:12 +00001411lldb::StackFrameSP
1412Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1413{
Greg Clayton2450cb12012-03-29 01:41:38 +00001414 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghamccd584d2010-09-23 17:40:12 +00001415}
Caroline Tice7826c882010-10-26 03:11:13 +00001416
1417const char *
1418Thread::StopReasonAsCString (lldb::StopReason reason)
1419{
1420 switch (reason)
1421 {
1422 case eStopReasonInvalid: return "invalid";
1423 case eStopReasonNone: return "none";
1424 case eStopReasonTrace: return "trace";
1425 case eStopReasonBreakpoint: return "breakpoint";
1426 case eStopReasonWatchpoint: return "watchpoint";
1427 case eStopReasonSignal: return "signal";
1428 case eStopReasonException: return "exception";
1429 case eStopReasonPlanComplete: return "plan complete";
1430 }
1431
1432
1433 static char unknown_state_string[64];
1434 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1435 return unknown_state_string;
1436}
1437
1438const char *
1439Thread::RunModeAsCString (lldb::RunMode mode)
1440{
1441 switch (mode)
1442 {
1443 case eOnlyThisThread: return "only this thread";
1444 case eAllThreads: return "all threads";
1445 case eOnlyDuringStepping: return "only during stepping";
1446 }
1447
1448 static char unknown_state_string[64];
1449 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1450 return unknown_state_string;
1451}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001452
Greg Claytonabe0fed2011-04-18 08:33:37 +00001453size_t
1454Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1455{
Greg Claytonf4124de2012-02-21 00:09:25 +00001456 ExecutionContext exe_ctx (shared_from_this());
1457 Target *target = exe_ctx.GetTargetPtr();
1458 Process *process = exe_ctx.GetProcessPtr();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001459 size_t num_frames_shown = 0;
1460 strm.Indent();
Greg Claytonf4124de2012-02-21 00:09:25 +00001461 bool is_selected = false;
1462 if (process)
1463 {
1464 if (process->GetThreadList().GetSelectedThread().get() == this)
1465 is_selected = true;
1466 }
1467 strm.Printf("%c ", is_selected ? '*' : ' ');
1468 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001469 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001470 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001471 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001472 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001473 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1474 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1475 {
1476 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1477 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001478 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001479 }
1480
1481 DumpUsingSettingsFormat (strm, start_frame);
1482
1483 if (num_frames > 0)
1484 {
1485 strm.IndentMore();
1486
1487 const bool show_frame_info = true;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001488 strm.IndentMore ();
Greg Clayton2450cb12012-03-29 01:41:38 +00001489 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1490 start_frame,
1491 num_frames,
1492 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001493 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001494 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001495 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001496 }
1497 return num_frames_shown;
1498}
1499
1500size_t
1501Thread::GetStackFrameStatus (Stream& strm,
1502 uint32_t first_frame,
1503 uint32_t num_frames,
1504 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001505 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +00001506{
Greg Clayton2450cb12012-03-29 01:41:38 +00001507 return GetStackFrameList()->GetStatus (strm,
1508 first_frame,
1509 num_frames,
1510 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001511 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001512}
1513
Peter Collingbournee426d852011-06-03 20:40:54 +00001514bool
1515Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1516{
1517 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1518 if (frame_sp)
1519 {
1520 checkpoint.SetStackID(frame_sp->GetStackID());
1521 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1522 }
1523 return false;
1524}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001525
Peter Collingbournee426d852011-06-03 20:40:54 +00001526bool
1527Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1528{
Jim Inghama17a81a2012-09-12 00:40:39 +00001529 return ResetFrameZeroRegisters (checkpoint.GetData());
1530}
1531
1532bool
1533Thread::ResetFrameZeroRegisters (lldb::DataBufferSP register_data_sp)
1534{
Peter Collingbournee426d852011-06-03 20:40:54 +00001535 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1536 if (frame_sp)
1537 {
Jim Inghama17a81a2012-09-12 00:40:39 +00001538 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (register_data_sp);
Peter Collingbournee426d852011-06-03 20:40:54 +00001539
1540 // Clear out all stack frames as our world just changed.
1541 ClearStackFrames();
1542 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1543
1544 return ret;
1545 }
1546 return false;
1547}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001548
Greg Clayton37f962e2011-08-22 02:49:39 +00001549Unwind *
1550Thread::GetUnwinder ()
1551{
1552 if (m_unwinder_ap.get() == NULL)
1553 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001554 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton37f962e2011-08-22 02:49:39 +00001555 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1556 switch (machine)
1557 {
1558 case llvm::Triple::x86_64:
1559 case llvm::Triple::x86:
1560 case llvm::Triple::arm:
1561 case llvm::Triple::thumb:
1562 m_unwinder_ap.reset (new UnwindLLDB (*this));
1563 break;
1564
1565 default:
1566 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1567 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1568 break;
1569 }
1570 }
1571 return m_unwinder_ap.get();
1572}
1573
1574
Greg Claytoncf5927e2012-05-18 02:38:05 +00001575void
1576Thread::Flush ()
1577{
1578 ClearStackFrames ();
1579 m_reg_context_sp.reset();
1580}