blob: 2d6c00cf1bb34a1ccddc6472096b89ede7bcb250 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- Thread.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/lldb-private-log.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000011#include "lldb/Breakpoint/BreakpointLocation.h"
Greg Claytona830adb2010-10-04 01:05:56 +000012#include "lldb/Core/Debugger.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "lldb/Core/Log.h"
14#include "lldb/Core/Stream.h"
15#include "lldb/Core/StreamString.h"
Jim Ingham20594b12010-09-08 03:14:33 +000016#include "lldb/Core/RegularExpression.h"
Greg Claytonabe0fed2011-04-18 08:33:37 +000017#include "lldb/Host/Host.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Target/DynamicLoader.h"
19#include "lldb/Target/ExecutionContext.h"
Jim Inghamb66cd072010-09-28 01:25:32 +000020#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Target/Process.h"
22#include "lldb/Target/RegisterContext.h"
Greg Clayton643ee732010-08-04 01:40:35 +000023#include "lldb/Target/StopInfo.h"
Greg Clayton7661a982010-07-23 16:45:51 +000024#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Target/Thread.h"
26#include "lldb/Target/ThreadPlan.h"
27#include "lldb/Target/ThreadPlanCallFunction.h"
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "lldb/Target/ThreadPlanBase.h"
29#include "lldb/Target/ThreadPlanStepInstruction.h"
30#include "lldb/Target/ThreadPlanStepOut.h"
31#include "lldb/Target/ThreadPlanStepOverBreakpoint.h"
32#include "lldb/Target/ThreadPlanStepThrough.h"
33#include "lldb/Target/ThreadPlanStepInRange.h"
34#include "lldb/Target/ThreadPlanStepOverRange.h"
35#include "lldb/Target/ThreadPlanRunToAddress.h"
36#include "lldb/Target/ThreadPlanStepUntil.h"
Jim Ingham3c7b5b92010-06-16 02:00:15 +000037#include "lldb/Target/ThreadSpec.h"
Jim Ingham71219082010-08-12 02:14:28 +000038#include "lldb/Target/Unwind.h"
Greg Clayton37f962e2011-08-22 02:49:39 +000039#include "Plugins/Process/Utility/UnwindLLDB.h"
40#include "UnwindMacOSXFrameBackchain.h"
41
Chris Lattner24943d22010-06-08 16:52:24 +000042
43using namespace lldb;
44using namespace lldb_private;
45
Greg Clayton73844aa2012-08-22 17:17:09 +000046
47const ThreadPropertiesSP &
48Thread::GetGlobalProperties()
49{
50 static ThreadPropertiesSP g_settings_sp;
51 if (!g_settings_sp)
52 g_settings_sp.reset (new ThreadProperties (true));
53 return g_settings_sp;
54}
55
56static PropertyDefinition
57g_properties[] =
58{
59 { "step-avoid-regexp", OptionValue::eTypeRegex , true , REG_EXTENDED, "^std::", NULL, "A regular expression defining functions step-in won't stop in." },
60 { "trace-thread", OptionValue::eTypeBoolean, false, false, NULL, NULL, "If true, this thread will single-step and log execution." },
61 { NULL , OptionValue::eTypeInvalid, false, 0 , NULL, NULL, NULL }
62};
63
64enum {
65 ePropertyStepAvoidRegex,
66 ePropertyEnableThreadTrace
67};
68
69
70class ThreadOptionValueProperties : public OptionValueProperties
71{
72public:
73 ThreadOptionValueProperties (const ConstString &name) :
74 OptionValueProperties (name)
75 {
76 }
77
78 // This constructor is used when creating ThreadOptionValueProperties when it
79 // is part of a new lldb_private::Thread instance. It will copy all current
80 // global property values as needed
81 ThreadOptionValueProperties (ThreadProperties *global_properties) :
Greg Claytonc6e82e42012-08-22 18:39:03 +000082 OptionValueProperties(*global_properties->GetValueProperties())
Greg Clayton73844aa2012-08-22 17:17:09 +000083 {
84 }
85
86 virtual const Property *
87 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
88 {
89 // When gettings the value for a key from the thread options, we will always
90 // try and grab the setting from the current thread if there is one. Else we just
91 // use the one from this instance.
92 if (exe_ctx)
93 {
94 Thread *thread = exe_ctx->GetThreadPtr();
95 if (thread)
96 {
97 ThreadOptionValueProperties *instance_properties = static_cast<ThreadOptionValueProperties *>(thread->GetValueProperties().get());
98 if (this != instance_properties)
99 return instance_properties->ProtectedGetPropertyAtIndex (idx);
100 }
101 }
102 return ProtectedGetPropertyAtIndex (idx);
103 }
104};
105
106
107
108ThreadProperties::ThreadProperties (bool is_global) :
109 Properties ()
110{
111 if (is_global)
112 {
113 m_collection_sp.reset (new ThreadOptionValueProperties(ConstString("thread")));
114 m_collection_sp->Initialize(g_properties);
115 }
116 else
117 m_collection_sp.reset (new ThreadOptionValueProperties(Thread::GetGlobalProperties().get()));
118}
119
120ThreadProperties::~ThreadProperties()
121{
122}
123
124const RegularExpression *
125ThreadProperties::GetSymbolsToAvoidRegexp()
126{
127 const uint32_t idx = ePropertyStepAvoidRegex;
128 return m_collection_sp->GetPropertyAtIndexAsOptionValueRegex (NULL, idx);
129}
130
131bool
132ThreadProperties::GetTraceEnabledState() const
133{
134 const uint32_t idx = ePropertyEnableThreadTrace;
135 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
136}
137
138
Greg Claytonf4124de2012-02-21 00:09:25 +0000139Thread::Thread (const ProcessSP &process_sp, lldb::tid_t tid) :
Greg Clayton73844aa2012-08-22 17:17:09 +0000140 ThreadProperties (false),
Chris Lattner24943d22010-06-08 16:52:24 +0000141 UserID (tid),
Greg Claytonf4124de2012-02-21 00:09:25 +0000142 m_process_wp (process_sp),
Greg Clayton643ee732010-08-04 01:40:35 +0000143 m_actual_stop_info_sp (),
Greg Claytonf4124de2012-02-21 00:09:25 +0000144 m_index_id (process_sp->GetNextThreadIndexID ()),
Chris Lattner24943d22010-06-08 16:52:24 +0000145 m_reg_context_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000146 m_state (eStateUnloaded),
Greg Clayton782b9cc2010-08-25 00:35:26 +0000147 m_state_mutex (Mutex::eMutexTypeRecursive),
Chris Lattner24943d22010-06-08 16:52:24 +0000148 m_plan_stack (),
Chris Lattner24943d22010-06-08 16:52:24 +0000149 m_completed_plan_stack(),
Greg Clayton2450cb12012-03-29 01:41:38 +0000150 m_frame_mutex (Mutex::eMutexTypeRecursive),
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000151 m_curr_frames_sp (),
152 m_prev_frames_sp (),
Chris Lattner24943d22010-06-08 16:52:24 +0000153 m_resume_signal (LLDB_INVALID_SIGNAL_NUMBER),
Jim Ingham71219082010-08-12 02:14:28 +0000154 m_resume_state (eStateRunning),
Jim Ingham149d1f52012-01-31 23:09:20 +0000155 m_temporary_resume_state (eStateRunning),
Jim Inghamcdea2362010-11-18 02:47:07 +0000156 m_unwinder_ap (),
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000157 m_destroy_called (false),
158 m_thread_stop_reason_stop_id (0)
Jim Ingham71219082010-08-12 02:14:28 +0000159
Chris Lattner24943d22010-06-08 16:52:24 +0000160{
Greg Claytone005f2c2010-11-06 01:53:30 +0000161 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000162 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000163 log->Printf ("%p Thread::Thread(tid = 0x%4.4llx)", this, GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000164
165 QueueFundamentalPlan(true);
166}
167
168
169Thread::~Thread()
170{
Greg Claytone005f2c2010-11-06 01:53:30 +0000171 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner24943d22010-06-08 16:52:24 +0000172 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000173 log->Printf ("%p Thread::~Thread(tid = 0x%4.4llx)", this, GetID());
Jim Inghamcdea2362010-11-18 02:47:07 +0000174 /// If you hit this assert, it means your derived class forgot to call DoDestroy in its destructor.
175 assert (m_destroy_called);
176}
177
178void
179Thread::DestroyThread ()
180{
181 m_plan_stack.clear();
182 m_discarded_plan_stack.clear();
183 m_completed_plan_stack.clear();
Jim Inghame93055a2012-04-10 00:44:25 +0000184 m_actual_stop_info_sp.reset();
Jim Inghamcdea2362010-11-18 02:47:07 +0000185 m_destroy_called = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000186}
187
Jim Ingham6297a3a2010-10-20 00:39:53 +0000188lldb::StopInfoSP
Greg Clayton643ee732010-08-04 01:40:35 +0000189Thread::GetStopInfo ()
Chris Lattner24943d22010-06-08 16:52:24 +0000190{
Jim Ingham6297a3a2010-10-20 00:39:53 +0000191 ThreadPlanSP plan_sp (GetCompletedPlan());
Jim Ingham707b7a82012-05-01 18:38:37 +0000192 if (plan_sp && plan_sp->PlanSucceeded())
Jim Ingham1586d972011-12-17 01:35:57 +0000193 return StopInfo::CreateStopReasonWithPlan (plan_sp, GetReturnValueObject());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000194 else
Jim Ingham24e0d612011-08-09 00:32:52 +0000195 {
Greg Claytonf4124de2012-02-21 00:09:25 +0000196 ProcessSP process_sp (GetProcess());
197 if (process_sp
198 && m_actual_stop_info_sp
Jim Ingham24e0d612011-08-09 00:32:52 +0000199 && m_actual_stop_info_sp->IsValid()
Greg Claytonf4124de2012-02-21 00:09:25 +0000200 && m_thread_stop_reason_stop_id == process_sp->GetStopID())
Jim Ingham24e0d612011-08-09 00:32:52 +0000201 return m_actual_stop_info_sp;
202 else
203 return GetPrivateStopReason ();
204 }
Chris Lattner24943d22010-06-08 16:52:24 +0000205}
206
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000207void
208Thread::SetStopInfo (const lldb::StopInfoSP &stop_info_sp)
209{
210 m_actual_stop_info_sp = stop_info_sp;
Jim Ingham24e0d612011-08-09 00:32:52 +0000211 if (m_actual_stop_info_sp)
212 m_actual_stop_info_sp->MakeStopInfoValid();
Greg Claytonf4124de2012-02-21 00:09:25 +0000213 ProcessSP process_sp (GetProcess());
214 if (process_sp)
215 m_thread_stop_reason_stop_id = process_sp->GetStopID();
216 else
217 m_thread_stop_reason_stop_id = UINT32_MAX;
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000218}
219
220void
221Thread::SetStopInfoToNothing()
222{
223 // Note, we can't just NULL out the private reason, or the native thread implementation will try to
224 // go calculate it again. For now, just set it to a Unix Signal with an invalid signal number.
225 SetStopInfo (StopInfo::CreateStopReasonWithSignal (*this, LLDB_INVALID_SIGNAL_NUMBER));
226}
227
Chris Lattner24943d22010-06-08 16:52:24 +0000228bool
229Thread::ThreadStoppedForAReason (void)
230{
Jim Ingham9880efa2012-08-11 00:35:26 +0000231 return (bool) GetPrivateStopReason ();
Chris Lattner24943d22010-06-08 16:52:24 +0000232}
233
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000234bool
235Thread::CheckpointThreadState (ThreadStateCheckpoint &saved_state)
236{
237 if (!SaveFrameZeroState(saved_state.register_backup))
238 return false;
239
240 saved_state.stop_info_sp = GetStopInfo();
Greg Claytonf4124de2012-02-21 00:09:25 +0000241 ProcessSP process_sp (GetProcess());
242 if (process_sp)
243 saved_state.orig_stop_id = process_sp->GetStopID();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000244 return true;
245}
246
247bool
248Thread::RestoreThreadStateFromCheckpoint (ThreadStateCheckpoint &saved_state)
249{
250 RestoreSaveFrameZero(saved_state.register_backup);
Jim Ingham11a837d2011-01-25 02:47:23 +0000251 if (saved_state.stop_info_sp)
252 saved_state.stop_info_sp->MakeStopInfoValid();
Jim Ingham15dcb7c2011-01-20 02:03:18 +0000253 SetStopInfo(saved_state.stop_info_sp);
254 return true;
255}
256
Chris Lattner24943d22010-06-08 16:52:24 +0000257StateType
258Thread::GetState() const
259{
260 // If any other threads access this we will need a mutex for it
261 Mutex::Locker locker(m_state_mutex);
262 return m_state;
263}
264
265void
266Thread::SetState(StateType state)
267{
268 Mutex::Locker locker(m_state_mutex);
269 m_state = state;
270}
271
272void
273Thread::WillStop()
274{
275 ThreadPlan *current_plan = GetCurrentPlan();
276
277 // FIXME: I may decide to disallow threads with no plans. In which
278 // case this should go to an assert.
279
280 if (!current_plan)
281 return;
282
283 current_plan->WillStop();
284}
285
286void
287Thread::SetupForResume ()
288{
289 if (GetResumeState() != eStateSuspended)
290 {
291
292 // If we're at a breakpoint push the step-over breakpoint plan. Do this before
293 // telling the current plan it will resume, since we might change what the current
294 // plan is.
295
296 lldb::addr_t pc = GetRegisterContext()->GetPC();
Greg Claytonf4124de2012-02-21 00:09:25 +0000297 BreakpointSiteSP bp_site_sp = GetProcess()->GetBreakpointSiteList().FindByAddress(pc);
Chris Lattner24943d22010-06-08 16:52:24 +0000298 if (bp_site_sp && bp_site_sp->IsEnabled())
299 {
300 // Note, don't assume there's a ThreadPlanStepOverBreakpoint, the target may not require anything
301 // special to step over a breakpoint.
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000302
303 ThreadPlan *cur_plan = GetCurrentPlan();
Chris Lattner24943d22010-06-08 16:52:24 +0000304
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000305 if (cur_plan->GetKind() != ThreadPlan::eKindStepOverBreakpoint)
306 {
307 ThreadPlanStepOverBreakpoint *step_bp_plan = new ThreadPlanStepOverBreakpoint (*this);
308 if (step_bp_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000309 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000310 ThreadPlanSP step_bp_plan_sp;
311 step_bp_plan->SetPrivate (true);
312
Chris Lattner24943d22010-06-08 16:52:24 +0000313 if (GetCurrentPlan()->RunState() != eStateStepping)
314 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000315 step_bp_plan->SetAutoContinue(true);
Chris Lattner24943d22010-06-08 16:52:24 +0000316 }
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000317 step_bp_plan_sp.reset (step_bp_plan);
Chris Lattner24943d22010-06-08 16:52:24 +0000318 QueueThreadPlan (step_bp_plan_sp, false);
319 }
320 }
321 }
322 }
323}
324
325bool
326Thread::WillResume (StateType resume_state)
327{
328 // At this point clear the completed plan stack.
329 m_completed_plan_stack.clear();
330 m_discarded_plan_stack.clear();
331
Jim Ingham149d1f52012-01-31 23:09:20 +0000332 SetTemporaryResumeState(resume_state);
333
334 // This is a little dubious, but we are trying to limit how often we actually fetch stop info from
335 // the target, 'cause that slows down single stepping. So assume that if we got to the point where
336 // we're about to resume, and we haven't yet had to fetch the stop reason, then it doesn't need to know
337 // about the fact that we are resuming...
Greg Claytonf4124de2012-02-21 00:09:25 +0000338 const uint32_t process_stop_id = GetProcess()->GetStopID();
Jim Ingham149d1f52012-01-31 23:09:20 +0000339 if (m_thread_stop_reason_stop_id == process_stop_id &&
340 (m_actual_stop_info_sp && m_actual_stop_info_sp->IsValid()))
341 {
342 StopInfo *stop_info = GetPrivateStopReason().get();
343 if (stop_info)
344 stop_info->WillResume (resume_state);
345 }
Chris Lattner24943d22010-06-08 16:52:24 +0000346
347 // Tell all the plans that we are about to resume in case they need to clear any state.
348 // We distinguish between the plan on the top of the stack and the lower
349 // plans in case a plan needs to do any special business before it runs.
350
351 ThreadPlan *plan_ptr = GetCurrentPlan();
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000352 bool need_to_resume = plan_ptr->WillResume(resume_state, true);
Chris Lattner24943d22010-06-08 16:52:24 +0000353
354 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
355 {
356 plan_ptr->WillResume (resume_state, false);
357 }
Greg Clayton643ee732010-08-04 01:40:35 +0000358
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000359 // If the WillResume for the plan says we are faking a resume, then it will have set an appropriate stop info.
360 // In that case, don't reset it here.
361
362 if (need_to_resume)
363 {
364 m_actual_stop_info_sp.reset();
365 }
366
367 return need_to_resume;
Chris Lattner24943d22010-06-08 16:52:24 +0000368}
369
370void
371Thread::DidResume ()
372{
373 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
374}
375
376bool
377Thread::ShouldStop (Event* event_ptr)
378{
379 ThreadPlan *current_plan = GetCurrentPlan();
380 bool should_stop = true;
381
Greg Claytone005f2c2010-11-06 01:53:30 +0000382 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham149d1f52012-01-31 23:09:20 +0000383
384 if (GetResumeState () == eStateSuspended)
385 {
386 if (log)
387 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
388 __FUNCTION__,
389 GetID ());
390// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
391// __FUNCTION__,
392// GetID (),
393// GetRegisterContext()->GetPC());
394 return false;
395 }
396
397 if (GetTemporaryResumeState () == eStateSuspended)
398 {
399 if (log)
400 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
401 __FUNCTION__,
402 GetID ());
403// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
404// __FUNCTION__,
405// GetID (),
406// GetRegisterContext()->GetPC());
407 return false;
408 }
409
410 if (ThreadStoppedForAReason() == false)
411 {
412 if (log)
413 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
414 __FUNCTION__,
415 GetID (),
416 GetRegisterContext()->GetPC());
417 return false;
418 }
Jim Ingham0c8fa2d2012-09-01 01:02:41 +0000419
Chris Lattner24943d22010-06-08 16:52:24 +0000420 if (log)
421 {
Jim Ingham149d1f52012-01-31 23:09:20 +0000422 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
423 __FUNCTION__,
424 GetID (),
425 GetRegisterContext()->GetPC());
Jim Inghame8f4e112011-10-15 00:23:43 +0000426 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000427 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000428 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000429 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000430 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000431 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000432
433 // The top most plan always gets to do the trace log...
434 current_plan->DoTraceLog ();
Jim Inghame787c7e2012-04-20 21:16:56 +0000435
436 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
437 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
438 // do any more work on this stop.
439 StopInfoSP private_stop_info (GetPrivateStopReason());
440 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
441 {
442 if (log)
443 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
444 return false;
445 }
Chris Lattner24943d22010-06-08 16:52:24 +0000446
Jim Ingham98d50212012-09-05 21:12:49 +0000447 // If we've already been restarted, don't query the plans since the state they would examine is not current.
448 if (Process::ProcessEventData::GetRestartedFromEvent(event_ptr))
449 return false;
450
451 // Before the plans see the state of the world, calculate the current inlined depth.
452 GetStackFrameList()->CalculateCurrentInlinedDepth();
453
Jim Inghamad382c52011-12-03 01:52:59 +0000454 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
455 // If that plan is still working, then we don't need to do any more work. If the plan that explains
456 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
457 // whether they still need to do more work.
458
459 bool done_processing_current_plan = false;
460
461 if (!current_plan->PlanExplainsStop())
462 {
463 if (current_plan->TracerExplainsStop())
464 {
465 done_processing_current_plan = true;
466 should_stop = false;
467 }
468 else
469 {
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000470 // If the current plan doesn't explain the stop, then find one that
Jim Inghamad382c52011-12-03 01:52:59 +0000471 // does and let it handle the situation.
472 ThreadPlan *plan_ptr = current_plan;
473 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
474 {
475 if (plan_ptr->PlanExplainsStop())
476 {
477 should_stop = plan_ptr->ShouldStop (event_ptr);
478
479 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
480 // and all the plans below it off the stack.
481
482 if (plan_ptr->MischiefManaged())
483 {
Jim Inghamd1ec3d62012-05-11 23:49:49 +0000484 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000485 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Inghamad382c52011-12-03 01:52:59 +0000486
487 do
488 {
489 if (should_stop)
490 current_plan->WillStop();
491 PopPlan();
492 }
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000493 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
494 // Now, if the responsible plan was not "Okay to discard" then we're done,
495 // otherwise we forward this to the next plan in the stack below.
496 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
497 done_processing_current_plan = true;
498 else
499 done_processing_current_plan = false;
Jim Inghamad382c52011-12-03 01:52:59 +0000500 }
501 else
502 done_processing_current_plan = true;
503
504 break;
505 }
506
507 }
508 }
509 }
510
511 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000512 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000513 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000514
Jim Inghame8f4e112011-10-15 00:23:43 +0000515 if (log)
516 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
517
Jim Inghamf9f40c22011-02-08 05:20:59 +0000518 // We're starting from the base plan, so just let it decide;
519 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000520 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000521 should_stop = current_plan->ShouldStop (event_ptr);
522 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000523 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000524 }
525 else
526 {
527 // Otherwise, don't let the base plan override what the other plans say to do, since
528 // presumably if there were other plans they would know what to do...
529 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000530 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000531 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000532 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000533
534 should_stop = current_plan->ShouldStop(event_ptr);
535 if (log)
536 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
537 if (current_plan->MischiefManaged())
538 {
539 if (should_stop)
540 current_plan->WillStop();
541
542 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
543 // Otherwise, see if the plan's parent wants to stop.
544
545 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
546 {
547 PopPlan();
548 break;
549 }
550 else
551 {
552
553 PopPlan();
554
555 current_plan = GetCurrentPlan();
556 if (current_plan == NULL)
557 {
558 break;
559 }
560 }
Chris Lattner24943d22010-06-08 16:52:24 +0000561 }
562 else
563 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000564 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000565 }
Chris Lattner24943d22010-06-08 16:52:24 +0000566 }
567 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000568
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000569 if (over_ride_stop)
570 should_stop = false;
Jim Ingham88e3de22012-05-03 21:19:36 +0000571
572 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
573 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
574 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
575 // This code clears stale plans off the stack.
576
577 if (should_stop)
578 {
579 ThreadPlan *plan_ptr = GetCurrentPlan();
580 while (!PlanIsBasePlan(plan_ptr))
581 {
582 bool stale = plan_ptr->IsPlanStale ();
583 ThreadPlan *examined_plan = plan_ptr;
584 plan_ptr = GetPreviousPlan (examined_plan);
585
586 if (stale)
587 {
588 if (log)
589 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
590 DiscardThreadPlansUpToPlan(examined_plan);
591 }
592 }
593 }
594
Chris Lattner24943d22010-06-08 16:52:24 +0000595 }
Chris Lattner24943d22010-06-08 16:52:24 +0000596
Jim Inghame8f4e112011-10-15 00:23:43 +0000597 if (log)
598 {
599 StreamString s;
600 s.IndentMore();
601 DumpThreadPlans(&s);
602 log->Printf ("Plan stack final state:\n%s", s.GetData());
603 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
604 }
Chris Lattner24943d22010-06-08 16:52:24 +0000605 return should_stop;
606}
607
608Vote
609Thread::ShouldReportStop (Event* event_ptr)
610{
611 StateType thread_state = GetResumeState ();
Jim Ingham149d1f52012-01-31 23:09:20 +0000612 StateType temp_thread_state = GetTemporaryResumeState();
613
Greg Claytone005f2c2010-11-06 01:53:30 +0000614 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000615
616 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
617 {
618 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000619 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 +0000620 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000621 }
Chris Lattner24943d22010-06-08 16:52:24 +0000622
Jim Ingham149d1f52012-01-31 23:09:20 +0000623 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
624 {
625 if (log)
626 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
627 return eVoteNoOpinion;
628 }
629
630 if (!ThreadStoppedForAReason())
631 {
632 if (log)
633 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
634 return eVoteNoOpinion;
635 }
636
Chris Lattner24943d22010-06-08 16:52:24 +0000637 if (m_completed_plan_stack.size() > 0)
638 {
639 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000640 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000641 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 +0000642 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
643 }
644 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000645 {
646 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000647 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000648 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000649 }
Chris Lattner24943d22010-06-08 16:52:24 +0000650}
651
652Vote
653Thread::ShouldReportRun (Event* event_ptr)
654{
655 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000656
Chris Lattner24943d22010-06-08 16:52:24 +0000657 if (thread_state == eStateSuspended
658 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000659 {
Chris Lattner24943d22010-06-08 16:52:24 +0000660 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000661 }
662
663 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000664 if (m_completed_plan_stack.size() > 0)
665 {
666 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000667 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000668 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 +0000669 GetIndexID(),
670 GetID(),
671 m_completed_plan_stack.back()->GetName());
672
Chris Lattner24943d22010-06-08 16:52:24 +0000673 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
674 }
675 else
Jim Inghamac959662011-01-24 06:34:17 +0000676 {
677 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000678 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 +0000679 GetIndexID(),
680 GetID(),
681 GetCurrentPlan()->GetName());
682
Chris Lattner24943d22010-06-08 16:52:24 +0000683 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000684 }
Chris Lattner24943d22010-06-08 16:52:24 +0000685}
686
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000687bool
688Thread::MatchesSpec (const ThreadSpec *spec)
689{
690 if (spec == NULL)
691 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000692
Jim Inghama2664912012-03-07 22:03:04 +0000693 return spec->ThreadPassesBasicTests(*this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000694}
695
Chris Lattner24943d22010-06-08 16:52:24 +0000696void
697Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
698{
699 if (thread_plan_sp)
700 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000701 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
702 if (!thread_plan_sp->GetThreadPlanTracer())
703 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000704 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000705
Chris Lattner24943d22010-06-08 16:52:24 +0000706 thread_plan_sp->DidPush();
707
Greg Claytone005f2c2010-11-06 01:53:30 +0000708 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000709 if (log)
710 {
711 StreamString s;
712 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Clayton444e35b2011-10-19 18:09:39 +0000713 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
Chris Lattner24943d22010-06-08 16:52:24 +0000714 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000715 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000716 }
717 }
718}
719
720void
721Thread::PopPlan ()
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
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000725 if (m_plan_stack.size() <= 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000726 return;
727 else
728 {
729 ThreadPlanSP &plan = m_plan_stack.back();
730 if (log)
731 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000732 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000733 }
734 m_completed_plan_stack.push_back (plan);
735 plan->WillPop();
736 m_plan_stack.pop_back();
737 }
738}
739
740void
741Thread::DiscardPlan ()
742{
743 if (m_plan_stack.size() > 1)
744 {
745 ThreadPlanSP &plan = m_plan_stack.back();
746 m_discarded_plan_stack.push_back (plan);
747 plan->WillPop();
748 m_plan_stack.pop_back();
749 }
750}
751
752ThreadPlan *
753Thread::GetCurrentPlan ()
754{
Jim Ingham2f41d272012-04-19 00:17:05 +0000755 // There will always be at least the base plan. If somebody is mucking with a
756 // thread with an empty plan stack, we should assert right away.
757 assert (!m_plan_stack.empty());
758
759 return m_plan_stack.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000760}
761
762ThreadPlanSP
763Thread::GetCompletedPlan ()
764{
765 ThreadPlanSP empty_plan_sp;
766 if (!m_completed_plan_stack.empty())
767 {
768 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
769 {
770 ThreadPlanSP completed_plan_sp;
771 completed_plan_sp = m_completed_plan_stack[i];
772 if (!completed_plan_sp->GetPrivate ())
773 return completed_plan_sp;
774 }
775 }
776 return empty_plan_sp;
777}
778
Jim Ingham1586d972011-12-17 01:35:57 +0000779ValueObjectSP
780Thread::GetReturnValueObject ()
781{
782 if (!m_completed_plan_stack.empty())
783 {
784 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
785 {
786 ValueObjectSP return_valobj_sp;
787 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
788 if (return_valobj_sp)
789 return return_valobj_sp;
790 }
791 }
792 return ValueObjectSP();
793}
794
Chris Lattner24943d22010-06-08 16:52:24 +0000795bool
796Thread::IsThreadPlanDone (ThreadPlan *plan)
797{
Chris Lattner24943d22010-06-08 16:52:24 +0000798 if (!m_completed_plan_stack.empty())
799 {
800 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
801 {
802 if (m_completed_plan_stack[i].get() == plan)
803 return true;
804 }
805 }
806 return false;
807}
808
809bool
810Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
811{
Chris Lattner24943d22010-06-08 16:52:24 +0000812 if (!m_discarded_plan_stack.empty())
813 {
814 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
815 {
816 if (m_discarded_plan_stack[i].get() == plan)
817 return true;
818 }
819 }
820 return false;
821}
822
823ThreadPlan *
824Thread::GetPreviousPlan (ThreadPlan *current_plan)
825{
826 if (current_plan == NULL)
827 return NULL;
828
829 int stack_size = m_completed_plan_stack.size();
830 for (int i = stack_size - 1; i > 0; i--)
831 {
832 if (current_plan == m_completed_plan_stack[i].get())
833 return m_completed_plan_stack[i-1].get();
834 }
835
836 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
837 {
Chris Lattner24943d22010-06-08 16:52:24 +0000838 if (m_plan_stack.size() > 0)
839 return m_plan_stack.back().get();
840 else
841 return NULL;
842 }
843
844 stack_size = m_plan_stack.size();
845 for (int i = stack_size - 1; i > 0; i--)
846 {
847 if (current_plan == m_plan_stack[i].get())
848 return m_plan_stack[i-1].get();
849 }
850 return NULL;
851}
852
853void
854Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
855{
856 if (abort_other_plans)
857 DiscardThreadPlans(true);
858
859 PushPlan (thread_plan_sp);
860}
861
Jim Ingham745ac7a2010-11-11 19:26:09 +0000862
863void
864Thread::EnableTracer (bool value, bool single_stepping)
865{
866 int stack_size = m_plan_stack.size();
867 for (int i = 0; i < stack_size; i++)
868 {
869 if (m_plan_stack[i]->GetThreadPlanTracer())
870 {
871 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
872 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
873 }
874 }
875}
876
877void
878Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
879{
880 int stack_size = m_plan_stack.size();
881 for (int i = 0; i < stack_size; i++)
882 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
883}
884
Chris Lattner24943d22010-06-08 16:52:24 +0000885void
Jim Inghamea9d4262010-11-05 19:25:48 +0000886Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
887{
Jim Ingham88e3de22012-05-03 21:19:36 +0000888 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
889}
890
891void
892Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
893{
Greg Claytone005f2c2010-11-06 01:53:30 +0000894 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000895 if (log)
896 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000897 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 +0000898 }
899
900 int stack_size = m_plan_stack.size();
901
902 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
903 // stack, and if so discard up to and including it.
904
Jim Ingham88e3de22012-05-03 21:19:36 +0000905 if (up_to_plan_ptr == NULL)
Jim Inghamea9d4262010-11-05 19:25:48 +0000906 {
907 for (int i = stack_size - 1; i > 0; i--)
908 DiscardPlan();
909 }
910 else
911 {
912 bool found_it = false;
913 for (int i = stack_size - 1; i > 0; i--)
914 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000915 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000916 found_it = true;
917 }
918 if (found_it)
919 {
920 bool last_one = false;
921 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
922 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000923 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000924 last_one = true;
925 DiscardPlan();
926 }
927 }
928 }
929 return;
930}
931
932void
Chris Lattner24943d22010-06-08 16:52:24 +0000933Thread::DiscardThreadPlans(bool force)
934{
Greg Claytone005f2c2010-11-06 01:53:30 +0000935 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000936 if (log)
937 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000938 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000939 }
940
941 if (force)
942 {
943 int stack_size = m_plan_stack.size();
944 for (int i = stack_size - 1; i > 0; i--)
945 {
946 DiscardPlan();
947 }
948 return;
949 }
950
951 while (1)
952 {
953
954 int master_plan_idx;
955 bool discard;
956
957 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
958 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
959 {
960 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
961 {
962 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
963 break;
964 }
965 }
966
967 if (discard)
968 {
969 // First pop all the dependent plans:
970 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
971 {
972
973 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
974 // for the plan leaves it in a state that it is safe to pop the plan
975 // with no more notice?
976 DiscardPlan();
977 }
978
979 // Now discard the master plan itself.
980 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
981 // discard it's dependent plans, but not it...
982 if (master_plan_idx > 0)
983 {
984 DiscardPlan();
985 }
986 }
987 else
988 {
989 // If the master plan doesn't want to get discarded, then we're done.
990 break;
991 }
992
993 }
Chris Lattner24943d22010-06-08 16:52:24 +0000994}
995
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000996bool
997Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
998{
999 if (plan_ptr->IsBasePlan())
1000 return true;
1001 else if (m_plan_stack.size() == 0)
1002 return false;
1003 else
1004 return m_plan_stack[0].get() == plan_ptr;
1005}
1006
Chris Lattner24943d22010-06-08 16:52:24 +00001007ThreadPlan *
1008Thread::QueueFundamentalPlan (bool abort_other_plans)
1009{
1010 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1011 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1012 return thread_plan_sp.get();
1013}
1014
1015ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001016Thread::QueueThreadPlanForStepSingleInstruction
1017(
1018 bool step_over,
1019 bool abort_other_plans,
1020 bool stop_other_threads
1021)
Chris Lattner24943d22010-06-08 16:52:24 +00001022{
1023 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1024 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1025 return thread_plan_sp.get();
1026}
1027
1028ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001029Thread::QueueThreadPlanForStepRange
1030(
1031 bool abort_other_plans,
1032 StepType type,
1033 const AddressRange &range,
1034 const SymbolContext &addr_context,
1035 lldb::RunMode stop_other_threads,
1036 bool avoid_code_without_debug_info
1037)
Chris Lattner24943d22010-06-08 16:52:24 +00001038{
1039 ThreadPlanSP thread_plan_sp;
1040 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001041 {
1042 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1043 if (avoid_code_without_debug_info)
1044 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1045 else
1046 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1047 thread_plan_sp.reset (plan);
1048 }
Chris Lattner24943d22010-06-08 16:52:24 +00001049 else
1050 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1051
1052 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1053 return thread_plan_sp.get();
1054}
1055
1056
1057ThreadPlan *
1058Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1059{
1060 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1061 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1062 return thread_plan_sp.get();
1063}
1064
1065ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001066Thread::QueueThreadPlanForStepOut
1067(
1068 bool abort_other_plans,
1069 SymbolContext *addr_context,
1070 bool first_insn,
1071 bool stop_other_threads,
1072 Vote stop_vote,
1073 Vote run_vote,
1074 uint32_t frame_idx
1075)
Chris Lattner24943d22010-06-08 16:52:24 +00001076{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001077 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1078 addr_context,
1079 first_insn,
1080 stop_other_threads,
1081 stop_vote,
1082 run_vote,
1083 frame_idx));
Sean Callananf6d5fea2012-07-31 22:19:25 +00001084
1085 if (thread_plan_sp->ValidatePlan(NULL))
1086 {
1087 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1088 return thread_plan_sp.get();
1089 }
1090 else
1091 {
1092 return NULL;
1093 }
Chris Lattner24943d22010-06-08 16:52:24 +00001094}
1095
1096ThreadPlan *
Jim Ingham038fa8e2012-05-10 01:35:39 +00001097Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner24943d22010-06-08 16:52:24 +00001098{
Jim Ingham038fa8e2012-05-10 01:35:39 +00001099 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Inghamad382c52011-12-03 01:52:59 +00001100 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1101 return NULL;
1102
Chris Lattner24943d22010-06-08 16:52:24 +00001103 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1104 return thread_plan_sp.get();
1105}
1106
1107ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001108Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1109 Address& function,
1110 lldb::addr_t arg,
1111 bool stop_other_threads,
1112 bool discard_on_error)
1113{
Jim Ingham016ef882011-12-22 19:12:40 +00001114 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner24943d22010-06-08 16:52:24 +00001115 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1116 return thread_plan_sp.get();
1117}
1118
1119ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001120Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1121 Address &target_addr,
1122 bool stop_other_threads)
1123{
1124 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1125 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1126 return thread_plan_sp.get();
1127}
1128
1129ThreadPlan *
1130Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001131 lldb::addr_t *address_list,
1132 size_t num_addresses,
1133 bool stop_other_threads,
1134 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001135{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001136 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +00001137 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1138 return thread_plan_sp.get();
1139
1140}
1141
1142uint32_t
1143Thread::GetIndexID () const
1144{
1145 return m_index_id;
1146}
1147
1148void
1149Thread::DumpThreadPlans (lldb_private::Stream *s) const
1150{
1151 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +00001152 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +00001153 s->Indent();
Greg Clayton444e35b2011-10-19 18:09:39 +00001154 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 +00001155 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +00001156 {
Chris Lattner24943d22010-06-08 16:52:24 +00001157 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +00001158 s->Indent();
1159 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +00001160 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +00001161 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +00001162 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +00001163 }
1164
Chris Lattner24943d22010-06-08 16:52:24 +00001165 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001166 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001167 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001168 s->Indent();
1169 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1170 for (i = stack_size - 1; i >= 0; i--)
1171 {
1172 s->IndentMore();
1173 s->Indent();
1174 s->Printf ("Element %d: ", i);
1175 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1176 s->EOL();
1177 s->IndentLess();
1178 }
Chris Lattner24943d22010-06-08 16:52:24 +00001179 }
1180
1181 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001182 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001183 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001184 s->Indent();
1185 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1186 for (i = stack_size - 1; i >= 0; i--)
1187 {
1188 s->IndentMore();
1189 s->Indent();
1190 s->Printf ("Element %d: ", i);
1191 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1192 s->EOL();
1193 s->IndentLess();
1194 }
Chris Lattner24943d22010-06-08 16:52:24 +00001195 }
1196
1197}
1198
Greg Clayton289afcb2012-02-18 05:35:26 +00001199TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001200Thread::CalculateTarget ()
1201{
Greg Claytonf4124de2012-02-21 00:09:25 +00001202 TargetSP target_sp;
1203 ProcessSP process_sp(GetProcess());
1204 if (process_sp)
1205 target_sp = process_sp->CalculateTarget();
1206 return target_sp;
1207
Chris Lattner24943d22010-06-08 16:52:24 +00001208}
1209
Greg Clayton289afcb2012-02-18 05:35:26 +00001210ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001211Thread::CalculateProcess ()
1212{
Greg Claytonf4124de2012-02-21 00:09:25 +00001213 return GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001214}
1215
Greg Clayton289afcb2012-02-18 05:35:26 +00001216ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001217Thread::CalculateThread ()
1218{
Greg Clayton289afcb2012-02-18 05:35:26 +00001219 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001220}
1221
Greg Clayton289afcb2012-02-18 05:35:26 +00001222StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001223Thread::CalculateStackFrame ()
1224{
Greg Clayton289afcb2012-02-18 05:35:26 +00001225 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001226}
1227
1228void
Greg Claytona830adb2010-10-04 01:05:56 +00001229Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001230{
Greg Claytonf4124de2012-02-21 00:09:25 +00001231 exe_ctx.SetContext (shared_from_this());
Chris Lattner24943d22010-06-08 16:52:24 +00001232}
1233
Greg Clayton782b9cc2010-08-25 00:35:26 +00001234
Greg Clayton2450cb12012-03-29 01:41:38 +00001235StackFrameListSP
Greg Clayton782b9cc2010-08-25 00:35:26 +00001236Thread::GetStackFrameList ()
1237{
Greg Clayton2450cb12012-03-29 01:41:38 +00001238 StackFrameListSP frame_list_sp;
1239 Mutex::Locker locker(m_frame_mutex);
1240 if (m_curr_frames_sp)
1241 {
1242 frame_list_sp = m_curr_frames_sp;
1243 }
1244 else
1245 {
1246 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1247 m_curr_frames_sp = frame_list_sp;
1248 }
1249 return frame_list_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +00001250}
1251
Greg Clayton782b9cc2010-08-25 00:35:26 +00001252void
1253Thread::ClearStackFrames ()
1254{
Greg Clayton2450cb12012-03-29 01:41:38 +00001255 Mutex::Locker locker(m_frame_mutex);
1256
Jim Inghambf97d742012-02-29 03:40:22 +00001257 // Only store away the old "reference" StackFrameList if we got all its frames:
1258 // FIXME: At some point we can try to splice in the frames we have fetched into
1259 // the new frame as we make it, but let's not try that now.
1260 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Claytonc51ffbf2011-08-12 21:40:01 +00001261 m_prev_frames_sp.swap (m_curr_frames_sp);
1262 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +00001263}
1264
1265lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001266Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1267{
Greg Clayton2450cb12012-03-29 01:41:38 +00001268 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001269}
1270
Chris Lattner24943d22010-06-08 16:52:24 +00001271void
Greg Claytona830adb2010-10-04 01:05:56 +00001272Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001273{
Greg Claytonf4124de2012-02-21 00:09:25 +00001274 ExecutionContext exe_ctx (shared_from_this());
1275 Process *process = exe_ctx.GetProcessPtr();
1276 if (process == NULL)
1277 return;
1278
Greg Clayton567e7f32011-09-22 04:58:26 +00001279 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +00001280 SymbolContext frame_sc;
Greg Claytona830adb2010-10-04 01:05:56 +00001281 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +00001282 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001283 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001284 if (frame_sp)
1285 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001286 exe_ctx.SetFrameSP(frame_sp);
1287 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001288 }
1289 }
1290
Greg Claytonf4124de2012-02-21 00:09:25 +00001291 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Claytona830adb2010-10-04 01:05:56 +00001292 assert (thread_format);
1293 const char *end = NULL;
1294 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001295 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001296 &exe_ctx,
1297 NULL,
1298 strm,
1299 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001300}
1301
Greg Clayton990de7b2010-11-18 23:32:35 +00001302void
Caroline Tice2a456812011-03-10 22:14:10 +00001303Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001304{
Greg Clayton990de7b2010-11-18 23:32:35 +00001305}
Jim Ingham20594b12010-09-08 03:14:33 +00001306
Greg Clayton990de7b2010-11-18 23:32:35 +00001307void
Caroline Tice2a456812011-03-10 22:14:10 +00001308Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001309{
Greg Clayton990de7b2010-11-18 23:32:35 +00001310}
Jim Ingham20594b12010-09-08 03:14:33 +00001311
Jim Inghamccd584d2010-09-23 17:40:12 +00001312lldb::StackFrameSP
1313Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1314{
Greg Clayton2450cb12012-03-29 01:41:38 +00001315 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghamccd584d2010-09-23 17:40:12 +00001316}
Caroline Tice7826c882010-10-26 03:11:13 +00001317
1318const char *
1319Thread::StopReasonAsCString (lldb::StopReason reason)
1320{
1321 switch (reason)
1322 {
1323 case eStopReasonInvalid: return "invalid";
1324 case eStopReasonNone: return "none";
1325 case eStopReasonTrace: return "trace";
1326 case eStopReasonBreakpoint: return "breakpoint";
1327 case eStopReasonWatchpoint: return "watchpoint";
1328 case eStopReasonSignal: return "signal";
1329 case eStopReasonException: return "exception";
1330 case eStopReasonPlanComplete: return "plan complete";
1331 }
1332
1333
1334 static char unknown_state_string[64];
1335 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1336 return unknown_state_string;
1337}
1338
1339const char *
1340Thread::RunModeAsCString (lldb::RunMode mode)
1341{
1342 switch (mode)
1343 {
1344 case eOnlyThisThread: return "only this thread";
1345 case eAllThreads: return "all threads";
1346 case eOnlyDuringStepping: return "only during stepping";
1347 }
1348
1349 static char unknown_state_string[64];
1350 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1351 return unknown_state_string;
1352}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001353
Greg Claytonabe0fed2011-04-18 08:33:37 +00001354size_t
1355Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1356{
Greg Claytonf4124de2012-02-21 00:09:25 +00001357 ExecutionContext exe_ctx (shared_from_this());
1358 Target *target = exe_ctx.GetTargetPtr();
1359 Process *process = exe_ctx.GetProcessPtr();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001360 size_t num_frames_shown = 0;
1361 strm.Indent();
Greg Claytonf4124de2012-02-21 00:09:25 +00001362 bool is_selected = false;
1363 if (process)
1364 {
1365 if (process->GetThreadList().GetSelectedThread().get() == this)
1366 is_selected = true;
1367 }
1368 strm.Printf("%c ", is_selected ? '*' : ' ');
1369 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001370 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001371 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001372 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001373 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001374 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1375 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1376 {
1377 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1378 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001379 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001380 }
1381
1382 DumpUsingSettingsFormat (strm, start_frame);
1383
1384 if (num_frames > 0)
1385 {
1386 strm.IndentMore();
1387
1388 const bool show_frame_info = true;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001389 strm.IndentMore ();
Greg Clayton2450cb12012-03-29 01:41:38 +00001390 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1391 start_frame,
1392 num_frames,
1393 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001394 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001395 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001396 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001397 }
1398 return num_frames_shown;
1399}
1400
1401size_t
1402Thread::GetStackFrameStatus (Stream& strm,
1403 uint32_t first_frame,
1404 uint32_t num_frames,
1405 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001406 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +00001407{
Greg Clayton2450cb12012-03-29 01:41:38 +00001408 return GetStackFrameList()->GetStatus (strm,
1409 first_frame,
1410 num_frames,
1411 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001412 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001413}
1414
Peter Collingbournee426d852011-06-03 20:40:54 +00001415bool
1416Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1417{
1418 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1419 if (frame_sp)
1420 {
1421 checkpoint.SetStackID(frame_sp->GetStackID());
1422 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1423 }
1424 return false;
1425}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001426
Peter Collingbournee426d852011-06-03 20:40:54 +00001427bool
1428Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1429{
1430 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1431 if (frame_sp)
1432 {
1433 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
1434
1435 // Clear out all stack frames as our world just changed.
1436 ClearStackFrames();
1437 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1438
1439 return ret;
1440 }
1441 return false;
1442}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001443
Greg Clayton37f962e2011-08-22 02:49:39 +00001444Unwind *
1445Thread::GetUnwinder ()
1446{
1447 if (m_unwinder_ap.get() == NULL)
1448 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001449 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton37f962e2011-08-22 02:49:39 +00001450 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1451 switch (machine)
1452 {
1453 case llvm::Triple::x86_64:
1454 case llvm::Triple::x86:
1455 case llvm::Triple::arm:
1456 case llvm::Triple::thumb:
1457 m_unwinder_ap.reset (new UnwindLLDB (*this));
1458 break;
1459
1460 default:
1461 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1462 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1463 break;
1464 }
1465 }
1466 return m_unwinder_ap.get();
1467}
1468
1469
Greg Claytoncf5927e2012-05-18 02:38:05 +00001470void
1471Thread::Flush ()
1472{
1473 ClearStackFrames ();
1474 m_reg_context_sp.reset();
1475}