blob: 7ff006c6bce43d3a08c2995fb8cfb9fe5514ac84 [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();
352 plan_ptr->WillResume(resume_state, true);
353
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
Greg Clayton643ee732010-08-04 01:40:35 +0000359 m_actual_stop_info_sp.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000360 return true;
361}
362
363void
364Thread::DidResume ()
365{
366 SetResumeSignal (LLDB_INVALID_SIGNAL_NUMBER);
367}
368
369bool
370Thread::ShouldStop (Event* event_ptr)
371{
372 ThreadPlan *current_plan = GetCurrentPlan();
373 bool should_stop = true;
374
Greg Claytone005f2c2010-11-06 01:53:30 +0000375 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Ingham149d1f52012-01-31 23:09:20 +0000376
377 if (GetResumeState () == eStateSuspended)
378 {
379 if (log)
380 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
381 __FUNCTION__,
382 GetID ());
383// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
384// __FUNCTION__,
385// GetID (),
386// GetRegisterContext()->GetPC());
387 return false;
388 }
389
390 if (GetTemporaryResumeState () == eStateSuspended)
391 {
392 if (log)
393 log->Printf ("Thread::%s for tid = 0x%4.4llx, should_stop = 0 (ignore since thread was suspended)",
394 __FUNCTION__,
395 GetID ());
396// log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since thread was suspended)",
397// __FUNCTION__,
398// GetID (),
399// GetRegisterContext()->GetPC());
400 return false;
401 }
402
403 if (ThreadStoppedForAReason() == false)
404 {
405 if (log)
406 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx, should_stop = 0 (ignore since no stop reason)",
407 __FUNCTION__,
408 GetID (),
409 GetRegisterContext()->GetPC());
410 return false;
411 }
412
Chris Lattner24943d22010-06-08 16:52:24 +0000413 if (log)
414 {
Jim Ingham149d1f52012-01-31 23:09:20 +0000415 log->Printf ("Thread::%s for tid = 0x%4.4llx, pc = 0x%16.16llx",
416 __FUNCTION__,
417 GetID (),
418 GetRegisterContext()->GetPC());
Jim Inghame8f4e112011-10-15 00:23:43 +0000419 log->Printf ("^^^^^^^^ Thread::ShouldStop Begin ^^^^^^^^");
Chris Lattner24943d22010-06-08 16:52:24 +0000420 StreamString s;
Jim Inghame8f4e112011-10-15 00:23:43 +0000421 s.IndentMore();
Chris Lattner24943d22010-06-08 16:52:24 +0000422 DumpThreadPlans(&s);
Jim Inghame8f4e112011-10-15 00:23:43 +0000423 log->Printf ("Plan stack initial state:\n%s", s.GetData());
Chris Lattner24943d22010-06-08 16:52:24 +0000424 }
Jim Ingham745ac7a2010-11-11 19:26:09 +0000425
426 // The top most plan always gets to do the trace log...
427 current_plan->DoTraceLog ();
Jim Inghame787c7e2012-04-20 21:16:56 +0000428
429 // First query the stop info's ShouldStopSynchronous. This handles "synchronous" stop reasons, for example the breakpoint
430 // command on internal breakpoints. If a synchronous stop reason says we should not stop, then we don't have to
431 // do any more work on this stop.
432 StopInfoSP private_stop_info (GetPrivateStopReason());
433 if (private_stop_info && private_stop_info->ShouldStopSynchronous(event_ptr) == false)
434 {
435 if (log)
436 log->Printf ("StopInfo::ShouldStop async callback says we should not stop, returning ShouldStop of false.");
437 return false;
438 }
Chris Lattner24943d22010-06-08 16:52:24 +0000439
Jim Inghamad382c52011-12-03 01:52:59 +0000440 // If the base plan doesn't understand why we stopped, then we have to find a plan that does.
441 // If that plan is still working, then we don't need to do any more work. If the plan that explains
442 // the stop is done, then we should pop all the plans below it, and pop it, and then let the plans above it decide
443 // whether they still need to do more work.
444
445 bool done_processing_current_plan = false;
446
447 if (!current_plan->PlanExplainsStop())
448 {
449 if (current_plan->TracerExplainsStop())
450 {
451 done_processing_current_plan = true;
452 should_stop = false;
453 }
454 else
455 {
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000456 // If the current plan doesn't explain the stop, then find one that
Jim Inghamad382c52011-12-03 01:52:59 +0000457 // does and let it handle the situation.
458 ThreadPlan *plan_ptr = current_plan;
459 while ((plan_ptr = GetPreviousPlan(plan_ptr)) != NULL)
460 {
461 if (plan_ptr->PlanExplainsStop())
462 {
463 should_stop = plan_ptr->ShouldStop (event_ptr);
464
465 // plan_ptr explains the stop, next check whether plan_ptr is done, if so, then we should take it
466 // and all the plans below it off the stack.
467
468 if (plan_ptr->MischiefManaged())
469 {
Jim Inghamd1ec3d62012-05-11 23:49:49 +0000470 // We're going to pop the plans up to and including the plan that explains the stop.
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000471 ThreadPlan *prev_plan_ptr = GetPreviousPlan (plan_ptr);
Jim Inghamad382c52011-12-03 01:52:59 +0000472
473 do
474 {
475 if (should_stop)
476 current_plan->WillStop();
477 PopPlan();
478 }
Jim Inghamd82bc6d2012-05-11 23:47:32 +0000479 while ((current_plan = GetCurrentPlan()) != prev_plan_ptr);
480 // Now, if the responsible plan was not "Okay to discard" then we're done,
481 // otherwise we forward this to the next plan in the stack below.
482 if (plan_ptr->IsMasterPlan() && !plan_ptr->OkayToDiscard())
483 done_processing_current_plan = true;
484 else
485 done_processing_current_plan = false;
Jim Inghamad382c52011-12-03 01:52:59 +0000486 }
487 else
488 done_processing_current_plan = true;
489
490 break;
491 }
492
493 }
494 }
495 }
496
497 if (!done_processing_current_plan)
Chris Lattner24943d22010-06-08 16:52:24 +0000498 {
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000499 bool over_ride_stop = current_plan->ShouldAutoContinue(event_ptr);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000500
Jim Inghame8f4e112011-10-15 00:23:43 +0000501 if (log)
502 log->Printf("Plan %s explains stop, auto-continue %i.", current_plan->GetName(), over_ride_stop);
503
Jim Inghamf9f40c22011-02-08 05:20:59 +0000504 // We're starting from the base plan, so just let it decide;
505 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000506 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000507 should_stop = current_plan->ShouldStop (event_ptr);
508 if (log)
Greg Clayton628cead2011-05-19 03:54:16 +0000509 log->Printf("Base plan says should stop: %i.", should_stop);
Jim Inghamf9f40c22011-02-08 05:20:59 +0000510 }
511 else
512 {
513 // Otherwise, don't let the base plan override what the other plans say to do, since
514 // presumably if there were other plans they would know what to do...
515 while (1)
Chris Lattner24943d22010-06-08 16:52:24 +0000516 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000517 if (PlanIsBasePlan(current_plan))
Chris Lattner24943d22010-06-08 16:52:24 +0000518 break;
Jim Inghamf9f40c22011-02-08 05:20:59 +0000519
520 should_stop = current_plan->ShouldStop(event_ptr);
521 if (log)
522 log->Printf("Plan %s should stop: %d.", current_plan->GetName(), should_stop);
523 if (current_plan->MischiefManaged())
524 {
525 if (should_stop)
526 current_plan->WillStop();
527
528 // If a Master Plan wants to stop, and wants to stick on the stack, we let it.
529 // Otherwise, see if the plan's parent wants to stop.
530
531 if (should_stop && current_plan->IsMasterPlan() && !current_plan->OkayToDiscard())
532 {
533 PopPlan();
534 break;
535 }
536 else
537 {
538
539 PopPlan();
540
541 current_plan = GetCurrentPlan();
542 if (current_plan == NULL)
543 {
544 break;
545 }
546 }
Chris Lattner24943d22010-06-08 16:52:24 +0000547 }
548 else
549 {
Jim Inghamf9f40c22011-02-08 05:20:59 +0000550 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000551 }
Chris Lattner24943d22010-06-08 16:52:24 +0000552 }
553 }
Jim Ingham88e3de22012-05-03 21:19:36 +0000554
Jim Ingham5a47e8b2010-06-19 04:45:32 +0000555 if (over_ride_stop)
556 should_stop = false;
Jim Ingham88e3de22012-05-03 21:19:36 +0000557
558 // One other potential problem is that we set up a master plan, then stop in before it is complete - for instance
559 // by hitting a breakpoint during a step-over - then do some step/finish/etc operations that wind up
560 // past the end point condition of the initial plan. We don't want to strand the original plan on the stack,
561 // This code clears stale plans off the stack.
562
563 if (should_stop)
564 {
565 ThreadPlan *plan_ptr = GetCurrentPlan();
566 while (!PlanIsBasePlan(plan_ptr))
567 {
568 bool stale = plan_ptr->IsPlanStale ();
569 ThreadPlan *examined_plan = plan_ptr;
570 plan_ptr = GetPreviousPlan (examined_plan);
571
572 if (stale)
573 {
574 if (log)
575 log->Printf("Plan %s being discarded in cleanup, it says it is already done.", examined_plan->GetName());
576 DiscardThreadPlansUpToPlan(examined_plan);
577 }
578 }
579 }
580
Chris Lattner24943d22010-06-08 16:52:24 +0000581 }
Chris Lattner24943d22010-06-08 16:52:24 +0000582
Jim Inghame8f4e112011-10-15 00:23:43 +0000583 if (log)
584 {
585 StreamString s;
586 s.IndentMore();
587 DumpThreadPlans(&s);
588 log->Printf ("Plan stack final state:\n%s", s.GetData());
589 log->Printf ("vvvvvvvv Thread::ShouldStop End (returning %i) vvvvvvvv", should_stop);
590 }
Chris Lattner24943d22010-06-08 16:52:24 +0000591 return should_stop;
592}
593
594Vote
595Thread::ShouldReportStop (Event* event_ptr)
596{
597 StateType thread_state = GetResumeState ();
Jim Ingham149d1f52012-01-31 23:09:20 +0000598 StateType temp_thread_state = GetTemporaryResumeState();
599
Greg Claytone005f2c2010-11-06 01:53:30 +0000600 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Greg Clayton5205f0b2010-09-03 17:10:42 +0000601
602 if (thread_state == eStateSuspended || thread_state == eStateInvalid)
603 {
604 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000605 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 +0000606 return eVoteNoOpinion;
Greg Clayton5205f0b2010-09-03 17:10:42 +0000607 }
Chris Lattner24943d22010-06-08 16:52:24 +0000608
Jim Ingham149d1f52012-01-31 23:09:20 +0000609 if (temp_thread_state == eStateSuspended || temp_thread_state == eStateInvalid)
610 {
611 if (log)
612 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (temporary state was suspended or invalid)\n", GetID(), eVoteNoOpinion);
613 return eVoteNoOpinion;
614 }
615
616 if (!ThreadStoppedForAReason())
617 {
618 if (log)
619 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote %i (thread didn't stop for a reason.)\n", GetID(), eVoteNoOpinion);
620 return eVoteNoOpinion;
621 }
622
Chris Lattner24943d22010-06-08 16:52:24 +0000623 if (m_completed_plan_stack.size() > 0)
624 {
625 // Don't use GetCompletedPlan here, since that suppresses private plans.
Greg Clayton5205f0b2010-09-03 17:10:42 +0000626 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000627 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 +0000628 return m_completed_plan_stack.back()->ShouldReportStop (event_ptr);
629 }
630 else
Greg Clayton5205f0b2010-09-03 17:10:42 +0000631 {
632 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000633 log->Printf ("Thread::ShouldReportStop() tid = 0x%4.4llx: returning vote for current plan\n", GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000634 return GetCurrentPlan()->ShouldReportStop (event_ptr);
Greg Clayton5205f0b2010-09-03 17:10:42 +0000635 }
Chris Lattner24943d22010-06-08 16:52:24 +0000636}
637
638Vote
639Thread::ShouldReportRun (Event* event_ptr)
640{
641 StateType thread_state = GetResumeState ();
Jim Inghamac959662011-01-24 06:34:17 +0000642
Chris Lattner24943d22010-06-08 16:52:24 +0000643 if (thread_state == eStateSuspended
644 || thread_state == eStateInvalid)
Jim Inghamac959662011-01-24 06:34:17 +0000645 {
Chris Lattner24943d22010-06-08 16:52:24 +0000646 return eVoteNoOpinion;
Jim Inghamac959662011-01-24 06:34:17 +0000647 }
648
649 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000650 if (m_completed_plan_stack.size() > 0)
651 {
652 // Don't use GetCompletedPlan here, since that suppresses private plans.
Jim Inghamac959662011-01-24 06:34:17 +0000653 if (log)
Greg Clayton444e35b2011-10-19 18:09:39 +0000654 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 +0000655 GetIndexID(),
656 GetID(),
657 m_completed_plan_stack.back()->GetName());
658
Chris Lattner24943d22010-06-08 16:52:24 +0000659 return m_completed_plan_stack.back()->ShouldReportRun (event_ptr);
660 }
661 else
Jim Inghamac959662011-01-24 06:34:17 +0000662 {
663 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 GetCurrentPlan()->GetName());
668
Chris Lattner24943d22010-06-08 16:52:24 +0000669 return GetCurrentPlan()->ShouldReportRun (event_ptr);
Jim Inghamac959662011-01-24 06:34:17 +0000670 }
Chris Lattner24943d22010-06-08 16:52:24 +0000671}
672
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000673bool
674Thread::MatchesSpec (const ThreadSpec *spec)
675{
676 if (spec == NULL)
677 return true;
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000678
Jim Inghama2664912012-03-07 22:03:04 +0000679 return spec->ThreadPassesBasicTests(*this);
Jim Ingham3c7b5b92010-06-16 02:00:15 +0000680}
681
Chris Lattner24943d22010-06-08 16:52:24 +0000682void
683Thread::PushPlan (ThreadPlanSP &thread_plan_sp)
684{
685 if (thread_plan_sp)
686 {
Jim Ingham745ac7a2010-11-11 19:26:09 +0000687 // If the thread plan doesn't already have a tracer, give it its parent's tracer:
688 if (!thread_plan_sp->GetThreadPlanTracer())
689 thread_plan_sp->SetThreadPlanTracer(m_plan_stack.back()->GetThreadPlanTracer());
Jim Ingham6297a3a2010-10-20 00:39:53 +0000690 m_plan_stack.push_back (thread_plan_sp);
Jim Ingham745ac7a2010-11-11 19:26:09 +0000691
Chris Lattner24943d22010-06-08 16:52:24 +0000692 thread_plan_sp->DidPush();
693
Greg Claytone005f2c2010-11-06 01:53:30 +0000694 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000695 if (log)
696 {
697 StreamString s;
698 thread_plan_sp->GetDescription (&s, lldb::eDescriptionLevelFull);
Greg Clayton444e35b2011-10-19 18:09:39 +0000699 log->Printf("Pushing plan: \"%s\", tid = 0x%4.4llx.",
Chris Lattner24943d22010-06-08 16:52:24 +0000700 s.GetData(),
Jim Ingham6297a3a2010-10-20 00:39:53 +0000701 thread_plan_sp->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000702 }
703 }
704}
705
706void
707Thread::PopPlan ()
708{
Greg Claytone005f2c2010-11-06 01:53:30 +0000709 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000710
Jim Inghamd0bdddf2012-08-22 21:34:33 +0000711 if (m_plan_stack.size() <= 1)
Chris Lattner24943d22010-06-08 16:52:24 +0000712 return;
713 else
714 {
715 ThreadPlanSP &plan = m_plan_stack.back();
716 if (log)
717 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000718 log->Printf("Popping plan: \"%s\", tid = 0x%4.4llx.", plan->GetName(), plan->GetThread().GetID());
Chris Lattner24943d22010-06-08 16:52:24 +0000719 }
720 m_completed_plan_stack.push_back (plan);
721 plan->WillPop();
722 m_plan_stack.pop_back();
723 }
724}
725
726void
727Thread::DiscardPlan ()
728{
729 if (m_plan_stack.size() > 1)
730 {
731 ThreadPlanSP &plan = m_plan_stack.back();
732 m_discarded_plan_stack.push_back (plan);
733 plan->WillPop();
734 m_plan_stack.pop_back();
735 }
736}
737
738ThreadPlan *
739Thread::GetCurrentPlan ()
740{
Jim Ingham2f41d272012-04-19 00:17:05 +0000741 // There will always be at least the base plan. If somebody is mucking with a
742 // thread with an empty plan stack, we should assert right away.
743 assert (!m_plan_stack.empty());
744
745 return m_plan_stack.back().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000746}
747
748ThreadPlanSP
749Thread::GetCompletedPlan ()
750{
751 ThreadPlanSP empty_plan_sp;
752 if (!m_completed_plan_stack.empty())
753 {
754 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
755 {
756 ThreadPlanSP completed_plan_sp;
757 completed_plan_sp = m_completed_plan_stack[i];
758 if (!completed_plan_sp->GetPrivate ())
759 return completed_plan_sp;
760 }
761 }
762 return empty_plan_sp;
763}
764
Jim Ingham1586d972011-12-17 01:35:57 +0000765ValueObjectSP
766Thread::GetReturnValueObject ()
767{
768 if (!m_completed_plan_stack.empty())
769 {
770 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
771 {
772 ValueObjectSP return_valobj_sp;
773 return_valobj_sp = m_completed_plan_stack[i]->GetReturnValueObject();
774 if (return_valobj_sp)
775 return return_valobj_sp;
776 }
777 }
778 return ValueObjectSP();
779}
780
Chris Lattner24943d22010-06-08 16:52:24 +0000781bool
782Thread::IsThreadPlanDone (ThreadPlan *plan)
783{
Chris Lattner24943d22010-06-08 16:52:24 +0000784 if (!m_completed_plan_stack.empty())
785 {
786 for (int i = m_completed_plan_stack.size() - 1; i >= 0; i--)
787 {
788 if (m_completed_plan_stack[i].get() == plan)
789 return true;
790 }
791 }
792 return false;
793}
794
795bool
796Thread::WasThreadPlanDiscarded (ThreadPlan *plan)
797{
Chris Lattner24943d22010-06-08 16:52:24 +0000798 if (!m_discarded_plan_stack.empty())
799 {
800 for (int i = m_discarded_plan_stack.size() - 1; i >= 0; i--)
801 {
802 if (m_discarded_plan_stack[i].get() == plan)
803 return true;
804 }
805 }
806 return false;
807}
808
809ThreadPlan *
810Thread::GetPreviousPlan (ThreadPlan *current_plan)
811{
812 if (current_plan == NULL)
813 return NULL;
814
815 int stack_size = m_completed_plan_stack.size();
816 for (int i = stack_size - 1; i > 0; i--)
817 {
818 if (current_plan == m_completed_plan_stack[i].get())
819 return m_completed_plan_stack[i-1].get();
820 }
821
822 if (stack_size > 0 && m_completed_plan_stack[0].get() == current_plan)
823 {
Chris Lattner24943d22010-06-08 16:52:24 +0000824 if (m_plan_stack.size() > 0)
825 return m_plan_stack.back().get();
826 else
827 return NULL;
828 }
829
830 stack_size = m_plan_stack.size();
831 for (int i = stack_size - 1; i > 0; i--)
832 {
833 if (current_plan == m_plan_stack[i].get())
834 return m_plan_stack[i-1].get();
835 }
836 return NULL;
837}
838
839void
840Thread::QueueThreadPlan (ThreadPlanSP &thread_plan_sp, bool abort_other_plans)
841{
842 if (abort_other_plans)
843 DiscardThreadPlans(true);
844
845 PushPlan (thread_plan_sp);
846}
847
Jim Ingham745ac7a2010-11-11 19:26:09 +0000848
849void
850Thread::EnableTracer (bool value, bool single_stepping)
851{
852 int stack_size = m_plan_stack.size();
853 for (int i = 0; i < stack_size; i++)
854 {
855 if (m_plan_stack[i]->GetThreadPlanTracer())
856 {
857 m_plan_stack[i]->GetThreadPlanTracer()->EnableTracing(value);
858 m_plan_stack[i]->GetThreadPlanTracer()->EnableSingleStep(single_stepping);
859 }
860 }
861}
862
863void
864Thread::SetTracer (lldb::ThreadPlanTracerSP &tracer_sp)
865{
866 int stack_size = m_plan_stack.size();
867 for (int i = 0; i < stack_size; i++)
868 m_plan_stack[i]->SetThreadPlanTracer(tracer_sp);
869}
870
Chris Lattner24943d22010-06-08 16:52:24 +0000871void
Jim Inghamea9d4262010-11-05 19:25:48 +0000872Thread::DiscardThreadPlansUpToPlan (lldb::ThreadPlanSP &up_to_plan_sp)
873{
Jim Ingham88e3de22012-05-03 21:19:36 +0000874 DiscardThreadPlansUpToPlan (up_to_plan_sp.get());
875}
876
877void
878Thread::DiscardThreadPlansUpToPlan (ThreadPlan *up_to_plan_ptr)
879{
Greg Claytone005f2c2010-11-06 01:53:30 +0000880 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Jim Inghamea9d4262010-11-05 19:25:48 +0000881 if (log)
882 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000883 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 +0000884 }
885
886 int stack_size = m_plan_stack.size();
887
888 // If the input plan is NULL, discard all plans. Otherwise make sure this plan is in the
889 // stack, and if so discard up to and including it.
890
Jim Ingham88e3de22012-05-03 21:19:36 +0000891 if (up_to_plan_ptr == NULL)
Jim Inghamea9d4262010-11-05 19:25:48 +0000892 {
893 for (int i = stack_size - 1; i > 0; i--)
894 DiscardPlan();
895 }
896 else
897 {
898 bool found_it = false;
899 for (int i = stack_size - 1; i > 0; i--)
900 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000901 if (m_plan_stack[i].get() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000902 found_it = true;
903 }
904 if (found_it)
905 {
906 bool last_one = false;
907 for (int i = stack_size - 1; i > 0 && !last_one ; i--)
908 {
Jim Ingham88e3de22012-05-03 21:19:36 +0000909 if (GetCurrentPlan() == up_to_plan_ptr)
Jim Inghamea9d4262010-11-05 19:25:48 +0000910 last_one = true;
911 DiscardPlan();
912 }
913 }
914 }
915 return;
916}
917
918void
Chris Lattner24943d22010-06-08 16:52:24 +0000919Thread::DiscardThreadPlans(bool force)
920{
Greg Claytone005f2c2010-11-06 01:53:30 +0000921 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_STEP));
Chris Lattner24943d22010-06-08 16:52:24 +0000922 if (log)
923 {
Greg Clayton444e35b2011-10-19 18:09:39 +0000924 log->Printf("Discarding thread plans for thread (tid = 0x%4.4llx, force %d)", GetID(), force);
Chris Lattner24943d22010-06-08 16:52:24 +0000925 }
926
927 if (force)
928 {
929 int stack_size = m_plan_stack.size();
930 for (int i = stack_size - 1; i > 0; i--)
931 {
932 DiscardPlan();
933 }
934 return;
935 }
936
937 while (1)
938 {
939
940 int master_plan_idx;
941 bool discard;
942
943 // Find the first master plan, see if it wants discarding, and if yes discard up to it.
944 for (master_plan_idx = m_plan_stack.size() - 1; master_plan_idx >= 0; master_plan_idx--)
945 {
946 if (m_plan_stack[master_plan_idx]->IsMasterPlan())
947 {
948 discard = m_plan_stack[master_plan_idx]->OkayToDiscard();
949 break;
950 }
951 }
952
953 if (discard)
954 {
955 // First pop all the dependent plans:
956 for (int i = m_plan_stack.size() - 1; i > master_plan_idx; i--)
957 {
958
959 // FIXME: Do we need a finalize here, or is the rule that "PrepareForStop"
960 // for the plan leaves it in a state that it is safe to pop the plan
961 // with no more notice?
962 DiscardPlan();
963 }
964
965 // Now discard the master plan itself.
966 // The bottom-most plan never gets discarded. "OkayToDiscard" for it means
967 // discard it's dependent plans, but not it...
968 if (master_plan_idx > 0)
969 {
970 DiscardPlan();
971 }
972 }
973 else
974 {
975 // If the master plan doesn't want to get discarded, then we're done.
976 break;
977 }
978
979 }
Chris Lattner24943d22010-06-08 16:52:24 +0000980}
981
Jim Ingham2bcbaf62012-04-09 22:37:39 +0000982bool
983Thread::PlanIsBasePlan (ThreadPlan *plan_ptr)
984{
985 if (plan_ptr->IsBasePlan())
986 return true;
987 else if (m_plan_stack.size() == 0)
988 return false;
989 else
990 return m_plan_stack[0].get() == plan_ptr;
991}
992
Chris Lattner24943d22010-06-08 16:52:24 +0000993ThreadPlan *
994Thread::QueueFundamentalPlan (bool abort_other_plans)
995{
996 ThreadPlanSP thread_plan_sp (new ThreadPlanBase(*this));
997 QueueThreadPlan (thread_plan_sp, abort_other_plans);
998 return thread_plan_sp.get();
999}
1000
1001ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001002Thread::QueueThreadPlanForStepSingleInstruction
1003(
1004 bool step_over,
1005 bool abort_other_plans,
1006 bool stop_other_threads
1007)
Chris Lattner24943d22010-06-08 16:52:24 +00001008{
1009 ThreadPlanSP thread_plan_sp (new ThreadPlanStepInstruction (*this, step_over, stop_other_threads, eVoteNoOpinion, eVoteNoOpinion));
1010 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1011 return thread_plan_sp.get();
1012}
1013
1014ThreadPlan *
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001015Thread::QueueThreadPlanForStepRange
1016(
1017 bool abort_other_plans,
1018 StepType type,
1019 const AddressRange &range,
1020 const SymbolContext &addr_context,
1021 lldb::RunMode stop_other_threads,
1022 bool avoid_code_without_debug_info
1023)
Chris Lattner24943d22010-06-08 16:52:24 +00001024{
1025 ThreadPlanSP thread_plan_sp;
1026 if (type == eStepTypeInto)
Greg Clayton8f5fd6b2010-06-12 18:59:55 +00001027 {
1028 ThreadPlanStepInRange *plan = new ThreadPlanStepInRange (*this, range, addr_context, stop_other_threads);
1029 if (avoid_code_without_debug_info)
1030 plan->GetFlags().Set (ThreadPlanShouldStopHere::eAvoidNoDebug);
1031 else
1032 plan->GetFlags().Clear (ThreadPlanShouldStopHere::eAvoidNoDebug);
1033 thread_plan_sp.reset (plan);
1034 }
Chris Lattner24943d22010-06-08 16:52:24 +00001035 else
1036 thread_plan_sp.reset (new ThreadPlanStepOverRange (*this, range, addr_context, stop_other_threads));
1037
1038 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1039 return thread_plan_sp.get();
1040}
1041
1042
1043ThreadPlan *
1044Thread::QueueThreadPlanForStepOverBreakpointPlan (bool abort_other_plans)
1045{
1046 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOverBreakpoint (*this));
1047 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1048 return thread_plan_sp.get();
1049}
1050
1051ThreadPlan *
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001052Thread::QueueThreadPlanForStepOut
1053(
1054 bool abort_other_plans,
1055 SymbolContext *addr_context,
1056 bool first_insn,
1057 bool stop_other_threads,
1058 Vote stop_vote,
1059 Vote run_vote,
1060 uint32_t frame_idx
1061)
Chris Lattner24943d22010-06-08 16:52:24 +00001062{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001063 ThreadPlanSP thread_plan_sp (new ThreadPlanStepOut (*this,
1064 addr_context,
1065 first_insn,
1066 stop_other_threads,
1067 stop_vote,
1068 run_vote,
1069 frame_idx));
Sean Callananf6d5fea2012-07-31 22:19:25 +00001070
1071 if (thread_plan_sp->ValidatePlan(NULL))
1072 {
1073 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1074 return thread_plan_sp.get();
1075 }
1076 else
1077 {
1078 return NULL;
1079 }
Chris Lattner24943d22010-06-08 16:52:24 +00001080}
1081
1082ThreadPlan *
Jim Ingham038fa8e2012-05-10 01:35:39 +00001083Thread::QueueThreadPlanForStepThrough (StackID &return_stack_id, bool abort_other_plans, bool stop_other_threads)
Chris Lattner24943d22010-06-08 16:52:24 +00001084{
Jim Ingham038fa8e2012-05-10 01:35:39 +00001085 ThreadPlanSP thread_plan_sp(new ThreadPlanStepThrough (*this, return_stack_id, stop_other_threads));
Jim Inghamad382c52011-12-03 01:52:59 +00001086 if (!thread_plan_sp || !thread_plan_sp->ValidatePlan (NULL))
1087 return NULL;
1088
Chris Lattner24943d22010-06-08 16:52:24 +00001089 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1090 return thread_plan_sp.get();
1091}
1092
1093ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001094Thread::QueueThreadPlanForCallFunction (bool abort_other_plans,
1095 Address& function,
1096 lldb::addr_t arg,
1097 bool stop_other_threads,
1098 bool discard_on_error)
1099{
Jim Ingham016ef882011-12-22 19:12:40 +00001100 ThreadPlanSP thread_plan_sp (new ThreadPlanCallFunction (*this, function, ClangASTType(), arg, stop_other_threads, discard_on_error));
Chris Lattner24943d22010-06-08 16:52:24 +00001101 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1102 return thread_plan_sp.get();
1103}
1104
1105ThreadPlan *
Chris Lattner24943d22010-06-08 16:52:24 +00001106Thread::QueueThreadPlanForRunToAddress (bool abort_other_plans,
1107 Address &target_addr,
1108 bool stop_other_threads)
1109{
1110 ThreadPlanSP thread_plan_sp (new ThreadPlanRunToAddress (*this, target_addr, stop_other_threads));
1111 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1112 return thread_plan_sp.get();
1113}
1114
1115ThreadPlan *
1116Thread::QueueThreadPlanForStepUntil (bool abort_other_plans,
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001117 lldb::addr_t *address_list,
1118 size_t num_addresses,
1119 bool stop_other_threads,
1120 uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001121{
Greg Clayton1ebdcc72011-01-21 06:11:58 +00001122 ThreadPlanSP thread_plan_sp (new ThreadPlanStepUntil (*this, address_list, num_addresses, stop_other_threads, frame_idx));
Chris Lattner24943d22010-06-08 16:52:24 +00001123 QueueThreadPlan (thread_plan_sp, abort_other_plans);
1124 return thread_plan_sp.get();
1125
1126}
1127
1128uint32_t
1129Thread::GetIndexID () const
1130{
1131 return m_index_id;
1132}
1133
1134void
1135Thread::DumpThreadPlans (lldb_private::Stream *s) const
1136{
1137 uint32_t stack_size = m_plan_stack.size();
Greg Claytonf04d6612010-09-03 22:45:01 +00001138 int i;
Jim Inghame8f4e112011-10-15 00:23:43 +00001139 s->Indent();
Greg Clayton444e35b2011-10-19 18:09:39 +00001140 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 +00001141 for (i = stack_size - 1; i >= 0; i--)
Chris Lattner24943d22010-06-08 16:52:24 +00001142 {
Chris Lattner24943d22010-06-08 16:52:24 +00001143 s->IndentMore();
Jim Inghame8f4e112011-10-15 00:23:43 +00001144 s->Indent();
1145 s->Printf ("Element %d: ", i);
Chris Lattner24943d22010-06-08 16:52:24 +00001146 m_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
Chris Lattner24943d22010-06-08 16:52:24 +00001147 s->EOL();
Jim Inghame8f4e112011-10-15 00:23:43 +00001148 s->IndentLess();
Chris Lattner24943d22010-06-08 16:52:24 +00001149 }
1150
Chris Lattner24943d22010-06-08 16:52:24 +00001151 stack_size = m_completed_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001152 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001153 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001154 s->Indent();
1155 s->Printf ("Completed Plan Stack: %d elements.\n", stack_size);
1156 for (i = stack_size - 1; i >= 0; i--)
1157 {
1158 s->IndentMore();
1159 s->Indent();
1160 s->Printf ("Element %d: ", i);
1161 m_completed_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1162 s->EOL();
1163 s->IndentLess();
1164 }
Chris Lattner24943d22010-06-08 16:52:24 +00001165 }
1166
1167 stack_size = m_discarded_plan_stack.size();
Jim Inghame8f4e112011-10-15 00:23:43 +00001168 if (stack_size > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001169 {
Jim Inghame8f4e112011-10-15 00:23:43 +00001170 s->Indent();
1171 s->Printf ("Discarded Plan Stack: %d elements.\n", stack_size);
1172 for (i = stack_size - 1; i >= 0; i--)
1173 {
1174 s->IndentMore();
1175 s->Indent();
1176 s->Printf ("Element %d: ", i);
1177 m_discarded_plan_stack[i]->GetDescription (s, eDescriptionLevelFull);
1178 s->EOL();
1179 s->IndentLess();
1180 }
Chris Lattner24943d22010-06-08 16:52:24 +00001181 }
1182
1183}
1184
Greg Clayton289afcb2012-02-18 05:35:26 +00001185TargetSP
Chris Lattner24943d22010-06-08 16:52:24 +00001186Thread::CalculateTarget ()
1187{
Greg Claytonf4124de2012-02-21 00:09:25 +00001188 TargetSP target_sp;
1189 ProcessSP process_sp(GetProcess());
1190 if (process_sp)
1191 target_sp = process_sp->CalculateTarget();
1192 return target_sp;
1193
Chris Lattner24943d22010-06-08 16:52:24 +00001194}
1195
Greg Clayton289afcb2012-02-18 05:35:26 +00001196ProcessSP
Chris Lattner24943d22010-06-08 16:52:24 +00001197Thread::CalculateProcess ()
1198{
Greg Claytonf4124de2012-02-21 00:09:25 +00001199 return GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001200}
1201
Greg Clayton289afcb2012-02-18 05:35:26 +00001202ThreadSP
Chris Lattner24943d22010-06-08 16:52:24 +00001203Thread::CalculateThread ()
1204{
Greg Clayton289afcb2012-02-18 05:35:26 +00001205 return shared_from_this();
Chris Lattner24943d22010-06-08 16:52:24 +00001206}
1207
Greg Clayton289afcb2012-02-18 05:35:26 +00001208StackFrameSP
Chris Lattner24943d22010-06-08 16:52:24 +00001209Thread::CalculateStackFrame ()
1210{
Greg Clayton289afcb2012-02-18 05:35:26 +00001211 return StackFrameSP();
Chris Lattner24943d22010-06-08 16:52:24 +00001212}
1213
1214void
Greg Claytona830adb2010-10-04 01:05:56 +00001215Thread::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner24943d22010-06-08 16:52:24 +00001216{
Greg Claytonf4124de2012-02-21 00:09:25 +00001217 exe_ctx.SetContext (shared_from_this());
Chris Lattner24943d22010-06-08 16:52:24 +00001218}
1219
Greg Clayton782b9cc2010-08-25 00:35:26 +00001220
Greg Clayton2450cb12012-03-29 01:41:38 +00001221StackFrameListSP
Greg Clayton782b9cc2010-08-25 00:35:26 +00001222Thread::GetStackFrameList ()
1223{
Greg Clayton2450cb12012-03-29 01:41:38 +00001224 StackFrameListSP frame_list_sp;
1225 Mutex::Locker locker(m_frame_mutex);
1226 if (m_curr_frames_sp)
1227 {
1228 frame_list_sp = m_curr_frames_sp;
1229 }
1230 else
1231 {
1232 frame_list_sp.reset(new StackFrameList (*this, m_prev_frames_sp, true));
1233 m_curr_frames_sp = frame_list_sp;
1234 }
1235 return frame_list_sp;
Greg Clayton782b9cc2010-08-25 00:35:26 +00001236}
1237
Greg Clayton782b9cc2010-08-25 00:35:26 +00001238void
1239Thread::ClearStackFrames ()
1240{
Greg Clayton2450cb12012-03-29 01:41:38 +00001241 Mutex::Locker locker(m_frame_mutex);
1242
Jim Inghambf97d742012-02-29 03:40:22 +00001243 // Only store away the old "reference" StackFrameList if we got all its frames:
1244 // FIXME: At some point we can try to splice in the frames we have fetched into
1245 // the new frame as we make it, but let's not try that now.
1246 if (m_curr_frames_sp && m_curr_frames_sp->GetAllFramesFetched())
Greg Claytonc51ffbf2011-08-12 21:40:01 +00001247 m_prev_frames_sp.swap (m_curr_frames_sp);
1248 m_curr_frames_sp.reset();
Jim Ingham71219082010-08-12 02:14:28 +00001249}
1250
1251lldb::StackFrameSP
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001252Thread::GetFrameWithConcreteFrameIndex (uint32_t unwind_idx)
1253{
Greg Clayton2450cb12012-03-29 01:41:38 +00001254 return GetStackFrameList()->GetFrameWithConcreteFrameIndex (unwind_idx);
Greg Clayton08d7d3a2011-01-06 22:15:06 +00001255}
1256
Chris Lattner24943d22010-06-08 16:52:24 +00001257void
Greg Claytona830adb2010-10-04 01:05:56 +00001258Thread::DumpUsingSettingsFormat (Stream &strm, uint32_t frame_idx)
Chris Lattner24943d22010-06-08 16:52:24 +00001259{
Greg Claytonf4124de2012-02-21 00:09:25 +00001260 ExecutionContext exe_ctx (shared_from_this());
1261 Process *process = exe_ctx.GetProcessPtr();
1262 if (process == NULL)
1263 return;
1264
Greg Clayton567e7f32011-09-22 04:58:26 +00001265 StackFrameSP frame_sp;
Greg Claytona830adb2010-10-04 01:05:56 +00001266 SymbolContext frame_sc;
Greg Claytona830adb2010-10-04 01:05:56 +00001267 if (frame_idx != LLDB_INVALID_INDEX32)
Chris Lattner24943d22010-06-08 16:52:24 +00001268 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001269 frame_sp = GetStackFrameAtIndex (frame_idx);
Chris Lattner24943d22010-06-08 16:52:24 +00001270 if (frame_sp)
1271 {
Greg Clayton567e7f32011-09-22 04:58:26 +00001272 exe_ctx.SetFrameSP(frame_sp);
1273 frame_sc = frame_sp->GetSymbolContext(eSymbolContextEverything);
Chris Lattner24943d22010-06-08 16:52:24 +00001274 }
1275 }
1276
Greg Claytonf4124de2012-02-21 00:09:25 +00001277 const char *thread_format = exe_ctx.GetTargetRef().GetDebugger().GetThreadFormat();
Greg Claytona830adb2010-10-04 01:05:56 +00001278 assert (thread_format);
1279 const char *end = NULL;
1280 Debugger::FormatPrompt (thread_format,
Greg Clayton567e7f32011-09-22 04:58:26 +00001281 frame_sp ? &frame_sc : NULL,
Greg Claytona830adb2010-10-04 01:05:56 +00001282 &exe_ctx,
1283 NULL,
1284 strm,
1285 &end);
Chris Lattner24943d22010-06-08 16:52:24 +00001286}
1287
Greg Clayton990de7b2010-11-18 23:32:35 +00001288void
Caroline Tice2a456812011-03-10 22:14:10 +00001289Thread::SettingsInitialize ()
Jim Ingham20594b12010-09-08 03:14:33 +00001290{
Greg Clayton990de7b2010-11-18 23:32:35 +00001291}
Jim Ingham20594b12010-09-08 03:14:33 +00001292
Greg Clayton990de7b2010-11-18 23:32:35 +00001293void
Caroline Tice2a456812011-03-10 22:14:10 +00001294Thread::SettingsTerminate ()
Greg Clayton990de7b2010-11-18 23:32:35 +00001295{
Greg Clayton990de7b2010-11-18 23:32:35 +00001296}
Jim Ingham20594b12010-09-08 03:14:33 +00001297
Jim Inghamccd584d2010-09-23 17:40:12 +00001298lldb::StackFrameSP
1299Thread::GetStackFrameSPForStackFramePtr (StackFrame *stack_frame_ptr)
1300{
Greg Clayton2450cb12012-03-29 01:41:38 +00001301 return GetStackFrameList()->GetStackFrameSPForStackFramePtr (stack_frame_ptr);
Jim Inghamccd584d2010-09-23 17:40:12 +00001302}
Caroline Tice7826c882010-10-26 03:11:13 +00001303
1304const char *
1305Thread::StopReasonAsCString (lldb::StopReason reason)
1306{
1307 switch (reason)
1308 {
1309 case eStopReasonInvalid: return "invalid";
1310 case eStopReasonNone: return "none";
1311 case eStopReasonTrace: return "trace";
1312 case eStopReasonBreakpoint: return "breakpoint";
1313 case eStopReasonWatchpoint: return "watchpoint";
1314 case eStopReasonSignal: return "signal";
1315 case eStopReasonException: return "exception";
1316 case eStopReasonPlanComplete: return "plan complete";
1317 }
1318
1319
1320 static char unknown_state_string[64];
1321 snprintf(unknown_state_string, sizeof (unknown_state_string), "StopReason = %i", reason);
1322 return unknown_state_string;
1323}
1324
1325const char *
1326Thread::RunModeAsCString (lldb::RunMode mode)
1327{
1328 switch (mode)
1329 {
1330 case eOnlyThisThread: return "only this thread";
1331 case eAllThreads: return "all threads";
1332 case eOnlyDuringStepping: return "only during stepping";
1333 }
1334
1335 static char unknown_state_string[64];
1336 snprintf(unknown_state_string, sizeof (unknown_state_string), "RunMode = %i", mode);
1337 return unknown_state_string;
1338}
Jim Ingham745ac7a2010-11-11 19:26:09 +00001339
Greg Claytonabe0fed2011-04-18 08:33:37 +00001340size_t
1341Thread::GetStatus (Stream &strm, uint32_t start_frame, uint32_t num_frames, uint32_t num_frames_with_source)
1342{
Greg Claytonf4124de2012-02-21 00:09:25 +00001343 ExecutionContext exe_ctx (shared_from_this());
1344 Target *target = exe_ctx.GetTargetPtr();
1345 Process *process = exe_ctx.GetProcessPtr();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001346 size_t num_frames_shown = 0;
1347 strm.Indent();
Greg Claytonf4124de2012-02-21 00:09:25 +00001348 bool is_selected = false;
1349 if (process)
1350 {
1351 if (process->GetThreadList().GetSelectedThread().get() == this)
1352 is_selected = true;
1353 }
1354 strm.Printf("%c ", is_selected ? '*' : ' ');
1355 if (target && target->GetDebugger().GetUseExternalEditor())
Greg Claytonabe0fed2011-04-18 08:33:37 +00001356 {
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001357 StackFrameSP frame_sp = GetStackFrameAtIndex(start_frame);
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001358 if (frame_sp)
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001359 {
Jim Inghamc2da8eb2011-08-16 00:07:28 +00001360 SymbolContext frame_sc(frame_sp->GetSymbolContext (eSymbolContextLineEntry));
1361 if (frame_sc.line_entry.line != 0 && frame_sc.line_entry.file)
1362 {
1363 Host::OpenFileInExternalEditor (frame_sc.line_entry.file, frame_sc.line_entry.line);
1364 }
Greg Claytonc5dca6c2011-08-12 06:47:54 +00001365 }
Greg Claytonabe0fed2011-04-18 08:33:37 +00001366 }
1367
1368 DumpUsingSettingsFormat (strm, start_frame);
1369
1370 if (num_frames > 0)
1371 {
1372 strm.IndentMore();
1373
1374 const bool show_frame_info = true;
Jim Ingham7868bcc2011-07-26 02:39:59 +00001375 strm.IndentMore ();
Greg Clayton2450cb12012-03-29 01:41:38 +00001376 num_frames_shown = GetStackFrameList ()->GetStatus (strm,
1377 start_frame,
1378 num_frames,
1379 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001380 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001381 strm.IndentLess();
Jim Ingham7868bcc2011-07-26 02:39:59 +00001382 strm.IndentLess();
Greg Claytonabe0fed2011-04-18 08:33:37 +00001383 }
1384 return num_frames_shown;
1385}
1386
1387size_t
1388Thread::GetStackFrameStatus (Stream& strm,
1389 uint32_t first_frame,
1390 uint32_t num_frames,
1391 bool show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001392 uint32_t num_frames_with_source)
Greg Claytonabe0fed2011-04-18 08:33:37 +00001393{
Greg Clayton2450cb12012-03-29 01:41:38 +00001394 return GetStackFrameList()->GetStatus (strm,
1395 first_frame,
1396 num_frames,
1397 show_frame_info,
Greg Claytona7d3dc72012-07-11 20:33:48 +00001398 num_frames_with_source);
Greg Claytonabe0fed2011-04-18 08:33:37 +00001399}
1400
Peter Collingbournee426d852011-06-03 20:40:54 +00001401bool
1402Thread::SaveFrameZeroState (RegisterCheckpoint &checkpoint)
1403{
1404 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1405 if (frame_sp)
1406 {
1407 checkpoint.SetStackID(frame_sp->GetStackID());
1408 return frame_sp->GetRegisterContext()->ReadAllRegisterValues (checkpoint.GetData());
1409 }
1410 return false;
1411}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001412
Peter Collingbournee426d852011-06-03 20:40:54 +00001413bool
1414Thread::RestoreSaveFrameZero (const RegisterCheckpoint &checkpoint)
1415{
1416 lldb::StackFrameSP frame_sp(GetStackFrameAtIndex (0));
1417 if (frame_sp)
1418 {
1419 bool ret = frame_sp->GetRegisterContext()->WriteAllRegisterValues (checkpoint.GetData());
1420
1421 // Clear out all stack frames as our world just changed.
1422 ClearStackFrames();
1423 frame_sp->GetRegisterContext()->InvalidateIfNeeded(true);
1424
1425 return ret;
1426 }
1427 return false;
1428}
Greg Claytonabe0fed2011-04-18 08:33:37 +00001429
Greg Clayton37f962e2011-08-22 02:49:39 +00001430Unwind *
1431Thread::GetUnwinder ()
1432{
1433 if (m_unwinder_ap.get() == NULL)
1434 {
Greg Claytonf4124de2012-02-21 00:09:25 +00001435 const ArchSpec target_arch (CalculateTarget()->GetArchitecture ());
Greg Clayton37f962e2011-08-22 02:49:39 +00001436 const llvm::Triple::ArchType machine = target_arch.GetMachine();
1437 switch (machine)
1438 {
1439 case llvm::Triple::x86_64:
1440 case llvm::Triple::x86:
1441 case llvm::Triple::arm:
1442 case llvm::Triple::thumb:
1443 m_unwinder_ap.reset (new UnwindLLDB (*this));
1444 break;
1445
1446 default:
1447 if (target_arch.GetTriple().getVendor() == llvm::Triple::Apple)
1448 m_unwinder_ap.reset (new UnwindMacOSXFrameBackchain (*this));
1449 break;
1450 }
1451 }
1452 return m_unwinder_ap.get();
1453}
1454
1455
Greg Claytoncf5927e2012-05-18 02:38:05 +00001456void
1457Thread::Flush ()
1458{
1459 ClearStackFrames ();
1460 m_reg_context_sp.reset();
1461}