blob: 9de1691cd47a8fc6bd8a786a14cc3cf806d5818a [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
420 // Adjust the stack frame's current inlined depth if it is needed.
421 GetStackFrameList()->CalculateCurrentInlinedDepth();
422
Chris Lattner24943d22010-06-08 16:52:24 +0000423 if (log)
424 {
Jim Ingham149d1f52012-01-31 23:09:20 +0000425 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
426 __FUNCTION__,
427 GetID (),
428 GetRegisterContext()->GetPC());
Jim Inghame8f4e112011-10-15 00:23:43 +0000429 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000430 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000431 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000432 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000433 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000434 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000435
436 // The top most plan always gets to do the trace log...
437 current_plan->DoTraceLog ();
Jim Inghame787c7e2012-04-20 21:16:56 +0000438
439 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
440 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
441 // do any more work on this stop.
442 StopInfoSP private_stop_info (GetPrivateStopReason());
443 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
444 {
445 if (log)
446 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
447 return false;
448 }
Chris Lattner24943d22010-06-08 16:52:24 +0000449
Jim Inghamad382c52011-12-03 01:52:59 +0000450 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
451 // If that plan is still working, then we don't need to do any more work. If the plan that explains
452 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
453 // whether they still need to do more work.
454
455 bool done_processing_current_plan = false;
456
457 if (!current_plan->PlanExplainsStop())
458 {
459 if (current_plan->TracerExplainsStop())
460 {
461 done_processing_current_plan = true;
462 should_stop = false;
463 }
464 else
465 {
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000466 // If the current plan doesn't explain the stop, then find one that
Jim Inghamad382c52011-12-03 01:52:59 +0000467 // does and let it handle the situation.
468 ThreadPlan *plan_ptr = current_plan;
469 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
470 {
471 if (plan_ptr->PlanExplainsStop())
472 {
473 should_stop = plan_ptr->ShouldStop (event_ptr);
474
475 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
476 // and all the plans below it off the stack.
477
478 if (plan_ptr->MischiefManaged())
479 {
Jim Inghamd1ec3d62012-05-11 23:49:49 +0000480 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000481 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Inghamad382c52011-12-03 01:52:59 +0000482
483 do
484 {
485 if (should_stop)
486 current_plan->WillStop();
487 PopPlan();
488 }
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000489 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
490 // Now, if the responsible plan was not "Okay to discard" then we're done,
491 // otherwise we forward this to the next plan in the stack below.
492 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
493 done_processing_current_plan = true;
494 else
495 done_processing_current_plan = false;
Jim Inghamad382c52011-12-03 01:52:59 +0000496 }
497 else
498 done_processing_current_plan = true;
499
500 break;
501 }
502
503 }
504 }
505 }
506
507 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000508 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000509 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000510
Jim Inghame8f4e112011-10-15 00:23:43 +0000511 if (log)
512 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
513
Jim Inghamf9f40c22011-02-08 05:20:59 +0000514 // We're starting from the base plan, so just let it decide;
515 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000516 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000517 should_stop = current_plan->ShouldStop (event_ptr);
518 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000519 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000520 }
521 else
522 {
523 // Otherwise, don't let the base plan override what the other plans say to do, since
524 // presumably if there were other plans they would know what to do...
525 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000526 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000527 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000528 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000529
530 should_stop = current_plan->ShouldStop(event_ptr);
531 if (log)
532 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
533 if (current_plan->MischiefManaged())
534 {
535 if (should_stop)
536 current_plan->WillStop();
537
538 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
539 // Otherwise, see if the plan's parent wants to stop.
540
541 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
542 {
543 PopPlan();
544 break;
545 }
546 else
547 {
548
549 PopPlan();
550
551 current_plan = GetCurrentPlan();
552 if (current_plan == NULL)
553 {
554 break;
555 }
556 }
Chris Lattner24943d22010-06-08 16:52:24 +0000557 }
558 else
559 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000560 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000561 }
Chris Lattner24943d22010-06-08 16:52:24 +0000562 }
563 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000564
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000565 if (over_ride_stop)
566 should_stop = false;
Jim Ingham88e3de22012-05-03 21:19:36 +0000567
568 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
569 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
570 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
571 // This code clears stale plans off the stack.
572
573 if (should_stop)
574 {
575 ThreadPlan *plan_ptr = GetCurrentPlan();
576 while (!PlanIsBasePlan(plan_ptr))
577 {
578 bool stale = plan_ptr->IsPlanStale ();
579 ThreadPlan *examined_plan = plan_ptr;
580 plan_ptr = GetPreviousPlan (examined_plan);
581
582 if (stale)
583 {
584 if (log)
585 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
586 DiscardThreadPlansUpToPlan(examined_plan);
587 }
588 }
589 }
590
Chris Lattner24943d22010-06-08 16:52:24 +0000591 }
Chris Lattner24943d22010-06-08 16:52:24 +0000592
Jim Inghame8f4e112011-10-15 00:23:43 +0000593 if (log)
594 {
595 StreamString s;
596 s.IndentMore();
597 DumpThreadPlans(&s);
598 log->Printf ("Plan stack final state:\n%s", s.GetData());
599 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
600 }
Chris Lattner24943d22010-06-08 16:52:24 +0000601 return should_stop;
602}
603
604Vote
605Thread::ShouldReportStop (Event* event_ptr)
606{
607 StateType thread_state = GetResumeState ();
Jim Ingham149d1f52012-01-31 23:09:20 +0000608 StateType temp_thread_state = GetTemporaryResumeState();
609
Greg Claytone005f2c2010-11-06 01:53:30 +0000610 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000611
612 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
613 {
614 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000615 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 +0000616 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000617 }
Chris Lattner24943d22010-06-08 16:52:24 +0000618
Jim Ingham149d1f52012-01-31 23:09:20 +0000619 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
620 {
621 if (log)
622 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
623 return eVoteNoOpinion;
624 }
625
626 if (!ThreadStoppedForAReason())
627 {
628 if (log)
629 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
630 return eVoteNoOpinion;
631 }
632
Chris Lattner24943d22010-06-08 16:52:24 +0000633 if (m_completed_plan_stack.size() > 0)
634 {
635 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000636 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000637 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 +0000638 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
639 }
640 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000641 {
642 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000643 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000644 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000645 }
Chris Lattner24943d22010-06-08 16:52:24 +0000646}
647
648Vote
649Thread::ShouldReportRun (Event* event_ptr)
650{
651 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000652
Chris Lattner24943d22010-06-08 16:52:24 +0000653 if (thread_state == eStateSuspended
654 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000655 {
Chris Lattner24943d22010-06-08 16:52:24 +0000656 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000657 }
658
659 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000660 if (m_completed_plan_stack.size() > 0)
661 {
662 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000663 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000664 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 +0000665 GetIndexID(),
666 GetID(),
667 m_completed_plan_stack.back()->GetName());
668
Chris Lattner24943d22010-06-08 16:52:24 +0000669 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
670 }
671 else
Jim Inghamac959662011-01-24 06:34:17 +0000672 {
673 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000674 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 +0000675 GetIndexID(),
676 GetID(),
677 GetCurrentPlan()->GetName());
678
Chris Lattner24943d22010-06-08 16:52:24 +0000679 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000680 }
Chris Lattner24943d22010-06-08 16:52:24 +0000681}
682
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000683bool
684Thread::MatchesSpec (const ThreadSpec *spec)
685{
686 if (spec == NULL)
687 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000688
Jim Inghama2664912012-03-07 22:03:04 +0000689 return spec->ThreadPassesBasicTests(*this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000690}
691
Chris Lattner24943d22010-06-08 16:52:24 +0000692void
693Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
694{
695 if (thread_plan_sp)
696 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000697 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
698 if (!thread_plan_sp->GetThreadPlanTracer())
699 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000700 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000701
Chris Lattner24943d22010-06-08 16:52:24 +0000702 thread_plan_sp->DidPush();
703
Greg Claytone005f2c2010-11-06 01:53:30 +0000704 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000705 if (log)
706 {
707 StreamString s;
708 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Clayton444e35b2011-10-19 18:09:39 +0000709 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
Chris Lattner24943d22010-06-08 16:52:24 +0000710 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000711 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000712 }
713 }
714}
715
716void
717Thread::PopPlan ()
718{
Greg Claytone005f2c2010-11-06 01:53:30 +0000719 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000720
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000721 if (m_plan_stack.size() <= 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000722 return;
723 else
724 {
725 ThreadPlanSP &plan = m_plan_stack.back();
726 if (log)
727 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000728 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000729 }
730 m_completed_plan_stack.push_back (plan);
731 plan->WillPop();
732 m_plan_stack.pop_back();
733 }
734}
735
736void
737Thread::DiscardPlan ()
738{
739 if (m_plan_stack.size() > 1)
740 {
741 ThreadPlanSP &plan = m_plan_stack.back();
742 m_discarded_plan_stack.push_back (plan);
743 plan->WillPop();
744 m_plan_stack.pop_back();
745 }
746}
747
748ThreadPlan *
749Thread::GetCurrentPlan ()
750{
Jim Ingham2f41d272012-04-19 00:17:05 +0000751 // There will always be at least the base plan. If somebody is mucking with a
752 // thread with an empty plan stack, we should assert right away.
753 assert (!m_plan_stack.empty());
754
755 return m_plan_stack.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000756}
757
758ThreadPlanSP
759Thread::GetCompletedPlan ()
760{
761 ThreadPlanSP empty_plan_sp;
762 if (!m_completed_plan_stack.empty())
763 {
764 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
765 {
766 ThreadPlanSP completed_plan_sp;
767 completed_plan_sp = m_completed_plan_stack[i];
768 if (!completed_plan_sp->GetPrivate ())
769 return completed_plan_sp;
770 }
771 }
772 return empty_plan_sp;
773}
774
Jim Ingham1586d972011-12-17 01:35:57 +0000775ValueObjectSP
776Thread::GetReturnValueObject ()
777{
778 if (!m_completed_plan_stack.empty())
779 {
780 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
781 {
782 ValueObjectSP return_valobj_sp;
783 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
784 if (return_valobj_sp)
785 return return_valobj_sp;
786 }
787 }
788 return ValueObjectSP();
789}
790
Chris Lattner24943d22010-06-08 16:52:24 +0000791bool
792Thread::IsThreadPlanDone (ThreadPlan *plan)
793{
Chris Lattner24943d22010-06-08 16:52:24 +0000794 if (!m_completed_plan_stack.empty())
795 {
796 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
797 {
798 if (m_completed_plan_stack[i].get() == plan)
799 return true;
800 }
801 }
802 return false;
803}
804
805bool
806Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
807{
Chris Lattner24943d22010-06-08 16:52:24 +0000808 if (!m_discarded_plan_stack.empty())
809 {
810 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
811 {
812 if (m_discarded_plan_stack[i].get() == plan)
813 return true;
814 }
815 }
816 return false;
817}
818
819ThreadPlan *
820Thread::GetPreviousPlan (ThreadPlan *current_plan)
821{
822 if (current_plan == NULL)
823 return NULL;
824
825 int stack_size = m_completed_plan_stack.size();
826 for (int i = stack_size - 1; i > 0; i--)
827 {
828 if (current_plan == m_completed_plan_stack[i].get())
829 return m_completed_plan_stack[i-1].get();
830 }
831
832 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
833 {
Chris Lattner24943d22010-06-08 16:52:24 +0000834 if (m_plan_stack.size() > 0)
835 return m_plan_stack.back().get();
836 else
837 return NULL;
838 }
839
840 stack_size = m_plan_stack.size();
841 for (int i = stack_size - 1; i > 0; i--)
842 {
843 if (current_plan == m_plan_stack[i].get())
844 return m_plan_stack[i-1].get();
845 }
846 return NULL;
847}
848
849void
850Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
851{
852 if (abort_other_plans)
853 DiscardThreadPlans(true);
854
855 PushPlan (thread_plan_sp);
856}
857
Jim Ingham745ac7a2010-11-11 19:26:09 +0000858
859void
860Thread::EnableTracer (bool value, bool single_stepping)
861{
862 int stack_size = m_plan_stack.size();
863 for (int i = 0; i < stack_size; i++)
864 {
865 if (m_plan_stack[i]->GetThreadPlanTracer())
866 {
867 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
868 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
869 }
870 }
871}
872
873void
874Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
875{
876 int stack_size = m_plan_stack.size();
877 for (int i = 0; i < stack_size; i++)
878 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
879}
880
Chris Lattner24943d22010-06-08 16:52:24 +0000881void
Jim Inghamea9d4262010-11-05 19:25:48 +0000882Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
883{
Jim Ingham88e3de22012-05-03 21:19:36 +0000884 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
885}
886
887void
888Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
889{
Greg Claytone005f2c2010-11-06 01:53:30 +0000890 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000891 if (log)
892 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000893 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 +0000894 }
895
896 int stack_size = m_plan_stack.size();
897
898 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
899 // stack, and if so discard up to and including it.
900
Jim Ingham88e3de22012-05-03 21:19:36 +0000901 if (up_to_plan_ptr == NULL)
Jim Inghamea9d4262010-11-05 19:25:48 +0000902 {
903 for (int i = stack_size - 1; i > 0; i--)
904 DiscardPlan();
905 }
906 else
907 {
908 bool found_it = false;
909 for (int i = stack_size - 1; i > 0; i--)
910 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000911 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000912 found_it = true;
913 }
914 if (found_it)
915 {
916 bool last_one = false;
917 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
918 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000919 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000920 last_one = true;
921 DiscardPlan();
922 }
923 }
924 }
925 return;
926}
927
928void
Chris Lattner24943d22010-06-08 16:52:24 +0000929Thread::DiscardThreadPlans(bool force)
930{
Greg Claytone005f2c2010-11-06 01:53:30 +0000931 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000932 if (log)
933 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000934 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000935 }
936
937 if (force)
938 {
939 int stack_size = m_plan_stack.size();
940 for (int i = stack_size - 1; i > 0; i--)
941 {
942 DiscardPlan();
943 }
944 return;
945 }
946
947 while (1)
948 {
949
950 int master_plan_idx;
951 bool discard;
952
953 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
954 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
955 {
956 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
957 {
958 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
959 break;
960 }
961 }
962
963 if (discard)
964 {
965 // First pop all the dependent plans:
966 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
967 {
968
969 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
970 // for the plan leaves it in a state that it is safe to pop the plan
971 // with no more notice?
972 DiscardPlan();
973 }
974
975 // Now discard the master plan itself.
976 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
977 // discard it's dependent plans, but not it...
978 if (master_plan_idx > 0)
979 {
980 DiscardPlan();
981 }
982 }
983 else
984 {
985 // If the master plan doesn't want to get discarded, then we're done.
986 break;
987 }
988
989 }
Chris Lattner24943d22010-06-08 16:52:24 +0000990}
991
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000992bool
993Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
994{
995 if (plan_ptr->IsBasePlan())
996 return true;
997 else if (m_plan_stack.size() == 0)
998 return false;
999 else
1000 return m_plan_stack[0].get() == plan_ptr;
1001}
1002
Chris Lattner24943d22010-06-08 16:52:24 +00001003ThreadPlan *
1004Thread::QueueFundamentalPlan (bool abort_other_plans)
1005{
1006 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
1007 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1008 return thread_plan_sp.get();
1009}
1010
1011ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001012Thread::QueueThreadPlanForStepSingleInstruction
1013(
1014 bool step_over,
1015 bool abort_other_plans,
1016 bool stop_other_threads
1017)
Chris Lattner24943d22010-06-08 16:52:24 +00001018{
1019 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1020 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1021 return thread_plan_sp.get();
1022}
1023
1024ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001025Thread::QueueThreadPlanForStepRange
1026(
1027 bool abort_other_plans,
1028 StepType type,
1029 const AddressRange &range,
1030 const SymbolContext &addr_context,
1031 lldb::RunMode stop_other_threads,
1032 bool avoid_code_without_debug_info
1033)
Chris Lattner24943d22010-06-08 16:52:24 +00001034{
1035 ThreadPlanSP thread_plan_sp;
1036 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001037 {
1038 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1039 if (avoid_code_without_debug_info)
1040 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1041 else
1042 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1043 thread_plan_sp.reset (plan);
1044 }
Chris Lattner24943d22010-06-08 16:52:24 +00001045 else
1046 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1047
1048 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1049 return thread_plan_sp.get();
1050}
1051
1052
1053ThreadPlan *
1054Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1055{
1056 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1057 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1058 return thread_plan_sp.get();
1059}
1060
1061ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001062Thread::QueueThreadPlanForStepOut
1063(
1064 bool abort_other_plans,
1065 SymbolContext *addr_context,
1066 bool first_insn,
1067 bool stop_other_threads,
1068 Vote stop_vote,
1069 Vote run_vote,
1070 uint32_t frame_idx
1071)
Chris Lattner24943d22010-06-08 16:52:24 +00001072{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001073 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1074 addr_context,
1075 first_insn,
1076 stop_other_threads,
1077 stop_vote,
1078 run_vote,
1079 frame_idx));
Sean Callananf6d5fea2012-07-31 22:19:25 +00001080
1081 if (thread_plan_sp->ValidatePlan(NULL))
1082 {
1083 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1084 return thread_plan_sp.get();
1085 }
1086 else
1087 {
1088 return NULL;
1089 }
Chris Lattner24943d22010-06-08 16:52:24 +00001090}
1091
1092ThreadPlan *
Jim Ingham038fa8e2012-05-10 01:35:39 +00001093Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner24943d22010-06-08 16:52:24 +00001094{
Jim Ingham038fa8e2012-05-10 01:35:39 +00001095 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Inghamad382c52011-12-03 01:52:59 +00001096 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1097 return NULL;
1098
Chris Lattner24943d22010-06-08 16:52:24 +00001099 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1100 return thread_plan_sp.get();
1101}
1102
1103ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001104Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1105 Address& function,
1106 lldb::addr_t arg,
1107 bool stop_other_threads,
1108 bool discard_on_error)
1109{
Jim Ingham016ef882011-12-22 19:12:40 +00001110 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner24943d22010-06-08 16:52:24 +00001111 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1112 return thread_plan_sp.get();
1113}
1114
1115ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001116Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1117 Address &target_addr,
1118 bool stop_other_threads)
1119{
1120 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1121 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1122 return thread_plan_sp.get();
1123}
1124
1125ThreadPlan *
1126Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001127 lldb::addr_t *address_list,
1128 size_t num_addresses,
1129 bool stop_other_threads,
1130 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001131{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001132 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +00001133 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1134 return thread_plan_sp.get();
1135
1136}
1137
1138uint32_t
1139Thread::GetIndexID () const
1140{
1141 return m_index_id;
1142}
1143
1144void
1145Thread::DumpThreadPlans (lldb_private::Stream *s) const
1146{
1147 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +00001148 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +00001149 s->Indent();
Greg Clayton444e35b2011-10-19 18:09:39 +00001150 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 +00001151 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +00001152 {
Chris Lattner24943d22010-06-08 16:52:24 +00001153 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +00001154 s->Indent();
1155 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +00001156 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +00001157 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +00001158 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +00001159 }
1160
Chris Lattner24943d22010-06-08 16:52:24 +00001161 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001162 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001163 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001164 s->Indent();
1165 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1166 for (i = stack_size - 1; i >= 0; i--)
1167 {
1168 s->IndentMore();
1169 s->Indent();
1170 s->Printf ("Element %d: ", i);
1171 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1172 s->EOL();
1173 s->IndentLess();
1174 }
Chris Lattner24943d22010-06-08 16:52:24 +00001175 }
1176
1177 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001178 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001179 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001180 s->Indent();
1181 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1182 for (i = stack_size - 1; i >= 0; i--)
1183 {
1184 s->IndentMore();
1185 s->Indent();
1186 s->Printf ("Element %d: ", i);
1187 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1188 s->EOL();
1189 s->IndentLess();
1190 }
Chris Lattner24943d22010-06-08 16:52:24 +00001191 }
1192
1193}
1194
Greg Clayton289afcb2012-02-18 05:35:26 +00001195TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001196Thread::CalculateTarget ()
1197{
Greg Claytonf4124de2012-02-21 00:09:25 +00001198 TargetSP target_sp;
1199 ProcessSP process_sp(GetProcess());
1200 if (process_sp)
1201 target_sp = process_sp->CalculateTarget();
1202 return target_sp;
1203
Chris Lattner24943d22010-06-08 16:52:24 +00001204}
1205
Greg Clayton289afcb2012-02-18 05:35:26 +00001206ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001207Thread::CalculateProcess ()
1208{
Greg Claytonf4124de2012-02-21 00:09:25 +00001209 return GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001210}
1211
Greg Clayton289afcb2012-02-18 05:35:26 +00001212ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001213Thread::CalculateThread ()
1214{
Greg Clayton289afcb2012-02-18 05:35:26 +00001215 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001216}
1217
Greg Clayton289afcb2012-02-18 05:35:26 +00001218StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001219Thread::CalculateStackFrame ()
1220{
Greg Clayton289afcb2012-02-18 05:35:26 +00001221 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001222}
1223
1224void
Greg Claytona830adb2010-10-04 01:05:56 +00001225Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001226{
Greg Claytonf4124de2012-02-21 00:09:25 +00001227 exe_ctx.SetContext (shared_from_this());
Chris Lattner24943d22010-06-08 16:52:24 +00001228}
1229
Greg Clayton782b9cc2010-08-25 00:35:26 +00001230
Greg Clayton2450cb12012-03-29 01:41:38 +00001231StackFrameListSP
Greg Clayton782b9cc2010-08-25 00:35:26 +00001232Thread::GetStackFrameList ()
1233{
Greg Clayton2450cb12012-03-29 01:41:38 +00001234 StackFrameListSP frame_list_sp;
1235 Mutex::Locker locker(m_frame_mutex);
1236 if (m_curr_frames_sp)
1237 {
1238 frame_list_sp = m_curr_frames_sp;
1239 }
1240 else
1241 {
1242 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1243 m_curr_frames_sp = frame_list_sp;
1244 }
1245 return frame_list_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +00001246}
1247
Greg Clayton782b9cc2010-08-25 00:35:26 +00001248void
1249Thread::ClearStackFrames ()
1250{
Greg Clayton2450cb12012-03-29 01:41:38 +00001251 Mutex::Locker locker(m_frame_mutex);
1252
Jim Inghambf97d742012-02-29 03:40:22 +00001253 // Only store away the old "reference" StackFrameList if we got all its frames:
1254 // FIXME: At some point we can try to splice in the frames we have fetched into
1255 // the new frame as we make it, but let's not try that now.
1256 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Claytonc51ffbf2011-08-12 21:40:01 +00001257 m_prev_frames_sp.swap (m_curr_frames_sp);
1258 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +00001259}
1260
1261lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001262Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1263{
Greg Clayton2450cb12012-03-29 01:41:38 +00001264 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001265}
1266
Chris Lattner24943d22010-06-08 16:52:24 +00001267void
Greg Claytona830adb2010-10-04 01:05:56 +00001268Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001269{
Greg Claytonf4124de2012-02-21 00:09:25 +00001270 ExecutionContext exe_ctx (shared_from_this());
1271 Process *process = exe_ctx.GetProcessPtr();
1272 if (process == NULL)
1273 return;
1274
Greg Clayton567e7f32011-09-22 04:58:26 +00001275 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +00001276 SymbolContext frame_sc;
Greg Claytona830adb2010-10-04 01:05:56 +00001277 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +00001278 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001279 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001280 if (frame_sp)
1281 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001282 exe_ctx.SetFrameSP(frame_sp);
1283 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001284 }
1285 }
1286
Greg Claytonf4124de2012-02-21 00:09:25 +00001287 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Claytona830adb2010-10-04 01:05:56 +00001288 assert (thread_format);
1289 const char *end = NULL;
1290 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001291 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001292 &exe_ctx,
1293 NULL,
1294 strm,
1295 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001296}
1297
Greg Clayton990de7b2010-11-18 23:32:35 +00001298void
Caroline Tice2a456812011-03-10 22:14:10 +00001299Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001300{
Greg Clayton990de7b2010-11-18 23:32:35 +00001301}
Jim Ingham20594b12010-09-08 03:14:33 +00001302
Greg Clayton990de7b2010-11-18 23:32:35 +00001303void
Caroline Tice2a456812011-03-10 22:14:10 +00001304Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001305{
Greg Clayton990de7b2010-11-18 23:32:35 +00001306}
Jim Ingham20594b12010-09-08 03:14:33 +00001307
Jim Inghamccd584d2010-09-23 17:40:12 +00001308lldb::StackFrameSP
1309Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1310{
Greg Clayton2450cb12012-03-29 01:41:38 +00001311 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghamccd584d2010-09-23 17:40:12 +00001312}
Caroline Tice7826c882010-10-26 03:11:13 +00001313
1314const char *
1315Thread::StopReasonAsCString (lldb::StopReason reason)
1316{
1317 switch (reason)
1318 {
1319 case eStopReasonInvalid: return "invalid";
1320 case eStopReasonNone: return "none";
1321 case eStopReasonTrace: return "trace";
1322 case eStopReasonBreakpoint: return "breakpoint";
1323 case eStopReasonWatchpoint: return "watchpoint";
1324 case eStopReasonSignal: return "signal";
1325 case eStopReasonException: return "exception";
1326 case eStopReasonPlanComplete: return "plan complete";
1327 }
1328
1329
1330 static char unknown_state_string[64];
1331 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1332 return unknown_state_string;
1333}
1334
1335const char *
1336Thread::RunModeAsCString (lldb::RunMode mode)
1337{
1338 switch (mode)
1339 {
1340 case eOnlyThisThread: return "only this thread";
1341 case eAllThreads: return "all threads";
1342 case eOnlyDuringStepping: return "only during stepping";
1343 }
1344
1345 static char unknown_state_string[64];
1346 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1347 return unknown_state_string;
1348}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001349
Greg Claytonabe0fed2011-04-18 08:33:37 +00001350size_t
1351Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1352{
Greg Claytonf4124de2012-02-21 00:09:25 +00001353 ExecutionContext exe_ctx (shared_from_this());
1354 Target *target = exe_ctx.GetTargetPtr();
1355 Process *process = exe_ctx.GetProcessPtr();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001356 size_t num_frames_shown = 0;
1357 strm.Indent();
Greg Claytonf4124de2012-02-21 00:09:25 +00001358 bool is_selected = false;
1359 if (process)
1360 {
1361 if (process->GetThreadList().GetSelectedThread().get() == this)
1362 is_selected = true;
1363 }
1364 strm.Printf("%c ", is_selected ? '*' : ' ');
1365 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001366 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001367 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001368 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001369 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001370 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1371 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1372 {
1373 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1374 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001375 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001376 }
1377
1378 DumpUsingSettingsFormat (strm, start_frame);
1379
1380 if (num_frames > 0)
1381 {
1382 strm.IndentMore();
1383
1384 const bool show_frame_info = true;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001385 strm.IndentMore ();
Greg Clayton2450cb12012-03-29 01:41:38 +00001386 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1387 start_frame,
1388 num_frames,
1389 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001390 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001391 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001392 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001393 }
1394 return num_frames_shown;
1395}
1396
1397size_t
1398Thread::GetStackFrameStatus (Stream& strm,
1399 uint32_t first_frame,
1400 uint32_t num_frames,
1401 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001402 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +00001403{
Greg Clayton2450cb12012-03-29 01:41:38 +00001404 return GetStackFrameList()->GetStatus (strm,
1405 first_frame,
1406 num_frames,
1407 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001408 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001409}
1410
Peter Collingbournee426d852011-06-03 20:40:54 +00001411bool
1412Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1413{
1414 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1415 if (frame_sp)
1416 {
1417 checkpoint.SetStackID(frame_sp->GetStackID());
1418 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1419 }
1420 return false;
1421}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001422
Peter Collingbournee426d852011-06-03 20:40:54 +00001423bool
1424Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1425{
1426 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1427 if (frame_sp)
1428 {
1429 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
1430
1431 // Clear out all stack frames as our world just changed.
1432 ClearStackFrames();
1433 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1434
1435 return ret;
1436 }
1437 return false;
1438}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001439
Greg Clayton37f962e2011-08-22 02:49:39 +00001440Unwind *
1441Thread::GetUnwinder ()
1442{
1443 if (m_unwinder_ap.get() == NULL)
1444 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001445 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton37f962e2011-08-22 02:49:39 +00001446 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1447 switch (machine)
1448 {
1449 case llvm::Triple::x86_64:
1450 case llvm::Triple::x86:
1451 case llvm::Triple::arm:
1452 case llvm::Triple::thumb:
1453 m_unwinder_ap.reset (new UnwindLLDB (*this));
1454 break;
1455
1456 default:
1457 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1458 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1459 break;
1460 }
1461 }
1462 return m_unwinder_ap.get();
1463}
1464
1465
Greg Claytoncf5927e2012-05-18 02:38:05 +00001466void
1467Thread::Flush ()
1468{
1469 ClearStackFrames ();
1470 m_reg_context_sp.reset();
1471}